Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

3.9 KB
Raw Blame
import type { NativePromptSelection } from 'librechat-data-provider';
import type {
  PromptStore,
  PromptRecord,
  PromptProjection,
  PromptGroupRecord,
  PromptSourceAdapter,
  NativeResolvedPrompt,
} from './types';

type ResolvablePrompt = Pick<PromptRecord, '_id' | 'groupId' | 'prompt' | 'type'>;
type NativeResolvedValue = Omit<NativeResolvedPrompt, 'source'>;

function isMatchingRevision(
  revision: PromptRecord | PromptProjection | null | undefined,
  groupId: string,
  promptId?: string,
): revision is ResolvablePrompt {
  return (
    revision != null &&
    revision._id != null &&
    revision.groupId === groupId &&
    (promptId == null || revision._id === promptId)
  );
}

/** A revision stored without a type is a text prompt, the same default the client uses. */
function resolveValue(revision: ResolvablePrompt): NativeResolvedValue {
  return {
    groupId: revision.groupId,
    promptId: revision._id,
    prompt: revision.prompt,
    type: revision.type ?? 'text',
  };
}

/** Resolves an exact revision, reading it when `loadedRevision` does not already
 *  match this group and promptId. */
async function resolveExact(
  store: Pick<PromptStore, 'getPrompt'>,
  groupId: string,
  promptId: string,
  loadedRevision?: PromptRecord | null,
): Promise<NativeResolvedValue | null> {
  let revision = loadedRevision;
  if (!isMatchingRevision(revision, groupId, promptId)) {
    revision = await store.getPrompt(promptId);
  }
  return isMatchingRevision(revision, groupId, promptId) ? resolveValue(revision) : null;
}

/**
 * Resolves a group's Production revision. The caller (the service) has already loaded
 * `group`, so this only re-reads the revision itself: when the group's inlined
 * `productionPrompt` is absent or stale it falls back to one `getPrompt` read.
 */
async function resolveProduction(
  store: Pick<PromptStore, 'getPrompt'>,
  group: PromptGroupRecord,
): Promise<NativeResolvedValue | null> {
  if (group.productionId == null) {
    return null;
  }
  if (isMatchingRevision(group.productionPrompt, group._id, group.productionId)) {
    return resolveValue(group.productionPrompt);
  }
  const revision = await store.getPrompt(group.productionId);
  return isMatchingRevision(revision, group._id, group.productionId)
    ? resolveValue(revision)
    : null;
}

export function selectionUnavailableReason(selection: {
  readonly type: 'production' | 'exact';
}): 'production' | 'revision' {
  return selection.type === 'production' ? 'production' : 'revision';
}

/** Native prompts stored in the LibreChat database; resolves content through the
 *  store's `getPrompt` and lists revisions through its `getPrompts`. */
export function createNativePromptAdapter(
  store: Pick<PromptStore, 'getPrompt' | 'getPrompts'>,
): PromptSourceAdapter<NativePromptSelection> {
  return {
    resolvePrompt: async ({ group, selection, loadedRevision }) => {
      const resolved =
        selection.type === 'exact'
          ? await resolveExact(store, group._id, selection.promptId, loadedRevision)
          : await resolveProduction(store, group);
      if (resolved == null) {
        return {
          ok: false,
          error: { type: 'unavailable_selection', reason: selectionUnavailableReason(selection) },
        };
      }
      return { ok: true, value: { source: 'native', ...resolved } };
    },

    listRevisions: async ({ group }) => {
      // getPrompts returns revisions newest first; version is derived from position, not stored.
      const revisions = await store.getPrompts(group._id);
      const total = revisions.length;
      return {
        ok: true,
        value: {
          source: 'native',
          groupId: group._id,
          revisions: revisions.map((revision, index) => ({
            source: 'native',
            promptId: revision._id,
            version: total - index,
            isProduction: revision._id === group.productionId,
          })),
        },
      };
    },
  };
}