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

1.3 KB
Raw Blame
import type { PromptGroupRecord, PromptRecord } from './types';

const ID_FIELDS = ['_id', 'groupId', 'author', 'productionId'] as const;

function withStringIds(record: object): Record<string, unknown> {
  const plain: Record<string, unknown> = { ...record };
  for (const field of ID_FIELDS) {
    const value = plain[field];
    if (value != null && typeof value !== 'string') {
      plain[field] = String(value);
    }
  }
  return plain;
}

/**
 * Converts a stored revision to plain data with string IDs. Other fields, such as dates,
 * `__v` and `tenantId`, stay as stored, so the JSON response does not change.
 */
export function toPromptRecord(record: object): PromptRecord {
  return withStringIds(record) as unknown as PromptRecord;
}

/**
 * Converts a stored group, and its Production revision when present, to string IDs.
 * A stored group with no `source` predates the field; it defaults here to `native`,
 * the only place that default is applied.
 */
export function toPromptGroupRecord(record: object): PromptGroupRecord {
  const plain = withStringIds(record);
  const production = plain.productionPrompt;
  if (production != null && typeof production === 'object') {
    plain.productionPrompt = withStringIds(production);
  }
  if (plain.source == null) {
    plain.source = 'native';
  }
  return plain as unknown as PromptGroupRecord;
}