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.0 KB
Raw Blame
import type { GenerationProtocolVersion } from '../protocol';
import type { QueuedMessageOrigin } from './types';

/** Server-side cap on a usage touch (mirrors `FILES_USAGE_MAX_IDS`). */
export const QUEUE_USAGE_MAX_FILES = 10;

export type SteerErrorCode =
  | 'NO_ACTIVE_RUN'
  | 'RUN_PAUSED'
  | 'RUN_REPLACED'
  | 'STEER_QUEUE_FULL'
  | 'STEER_UNSUPPORTED'
  | string;

export type SubmitSteerOptions = {
  preempt?: boolean;
  queuedOrigin?: QueuedMessageOrigin;
  /** Stable across transport retries so the server can dedupe a committed POST. */
  clientSteerId?: string;
  /** Preserve the generation identity on retries instead of targeting a newer
   * turn that may now occupy the same conversation-scoped stream id. */
  generationCreatedAt?: number;
  /** Same-id retries retain the original submission timestamp so a receipt
   * replay cannot reorder this steer behind a later queued follow-up. */
  createdAt?: number;
  /** Interpret a retry receipt using the source generation's negotiated
   * protocol, not whichever replacement generation is currently active. */
  generationProtocolVersion?: GenerationProtocolVersion;
};

export function getSteerErrorCode(error: unknown): SteerErrorCode | undefined {
  const response = (error as { response?: { data?: { code?: string } } } | undefined)?.response;
  return response?.data?.code;
}

export function isDefiniteSteerRejection(error: unknown): boolean {
  const status = (error as { response?: { status?: number } } | undefined)?.response?.status;
  return (
    status != null &&
    status >= 400 &&
    status < 500 &&
    // These statuses can be generated by an intermediary while the origin
    // request continues; keep their delivery outcome explicitly uncertain.
    status !== 408 &&
    status !== 425
  );
}

function queuedTurnErrorResponse(error: unknown): { status?: number; code?: string } {
  const response = (
    error as { response?: { status?: unknown; data?: { code?: unknown } } } | undefined
  )?.response;
  return {
    ...(typeof response?.status === 'number' && { status: response.status }),
    ...(typeof response?.data?.code === 'string' && { code: response.data.code }),
  };
}

export function isDefiniteQueuedTurnsUnsupported(error: unknown): boolean {
  const { status, code } = queuedTurnErrorResponse(error);
  return (
    (status === 404 && code == null) ||
    (status === 501 &&
      (code == null ||
        code === 'QUEUED_TURNS_UNSUPPORTED' ||
        code === 'QUEUED_TURN_PRIORITY_UNSUPPORTED'))
  );
}

/** A bounded origin 4xx proves the queued row was not committed. Timeouts and
 * early-data responses can be generated while the origin request continues,
 * so those remain outcome-ambiguous and must reconcile by request identity. */
export function isDefiniteQueuedTurnRejection(error: unknown): boolean {
  const { status } = queuedTurnErrorResponse(error);
  return (
    !isDefiniteQueuedTurnsUnsupported(error) &&
    typeof status === 'number' &&
    status >= 400 &&
    status < 500 &&
    status !== 408 &&
    status !== 425
  );
}