Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

7.3 KB
Raw Blame
/**
 * Shared types for the server-owned tool loop.
 * All consumers use `import type` — no runtime imports.
 */

// ─── §5.4 Provider Leg ─────────────────────────────────────────────────────

export interface ProviderLegUsage {
  prompt_tokens: number;
  completion_tokens: number;
  total_tokens: number;
  cached_tokens?: number;
  cache_read_input_tokens?: number;
  cache_creation_input_tokens?: number;
  reasoning_tokens?: number;
  cost_in_usd_ticks?: number;
}

export interface ProviderLegReceipt {
  index: number;
  connectionId: string;
  provider: string;
  model: string;
  startedAt: string;
  endedAt: string;
  latencyMs: number;
  httpStatus: number;
  errorType: string | null;
  usage: ProviderLegUsage | null;
  serviceTier: string | null;
  computedCostUsd: number | null;
  toolCalls: Array<{ id: string; name: string }>;
  termination: string;
  clientVisible: boolean;
}

export interface ChatCoreErrorResult {
  success: false;
  status: number;
  response: Response;
  error?: string;
  errorCode?: string;
  errorType?: string;
  retryAfterMs?: number;
  originalError?: unknown;
  rawMessage?: string;
  upstreamHeaders?: Headers;
  upstreamErrorBody?: unknown;
}

export type NonStreamingProviderLegResult =
  | {
      kind: "ok";
      response: Record<string, unknown>;
      responseForMemoryExtraction: Record<string, unknown>;
      providerBody: Record<string, unknown>;
      providerRequest: Record<string, unknown>;
      usage: ProviderLegUsage | null;
      responsePayloadFormat: string;
      looksLikeSSE: boolean;
      connectionId: string;
      headers: Headers;
      requestHeaders?: Record<string, string>;
      requestUrl?: string;
      upstreamResponse?: Response;
      receipt: ProviderLegReceipt;
    }
  | {
      kind: "error";
      result: ChatCoreErrorResult;
      upstreamDiagnostic?: Record<string, unknown>;
      receipt: ProviderLegReceipt;
      usage: ProviderLegUsage | null;
    };

// ─── §5.5 Tool Loop ────────────────────────────────────────────────────────

export interface ServerOwnedToolLoopOptions {
  initialLeg: NonStreamingProviderLegResult & { kind: "ok" };
  sourceBody: Record<string, unknown>;
  sourceFormat: "openai" | "claude";
  skillsModelId: string;
  executionContext: ExecutionContext;
  abortSignal?: AbortSignal;
  now?: () => number;
  executeServerOwned: (
    calls: ToolCall[],
    context: ExecutionContext
  ) => Promise<ExecutedToolResult[]>;
  resumeUpstream: (
    nextSourceBody: Record<string, unknown>,
    expectedConnectionId: string,
    deadlineAtMs: number
  ) => Promise<NonStreamingProviderLegResult>;
  maxFollowUps?: number;
  maxResultBytes?: number;
  maxTotalResultBytes?: number;
  deadlineAtMs: number;
}

export interface ServerOwnedToolLoopResult {
  kind: "ok" | "error";
  response?: Record<string, unknown>;
  responseForMemoryExtraction?: Record<string, unknown>;
  finalProviderBody?: Record<string, unknown>;
  finalProviderRequest?: Record<string, unknown>;
  errorResult?: ChatCoreErrorResult;
  cumulativeUsage: ProviderLegUsage | null;
  totalCostUsd: number;
  receipts: ProviderLegReceipt[];
  followUps: number;
  termination:
    | "completed"
    | "client_tools"
    | "mixed_tools"
    | "max_followups"
    | "tool_output_budget"
    | "deadline"
    | "client_abort"
    | "provider_error"
    | "connection_mismatch"
    | "execution_in_progress"
    | "execution_unknown"
    | "execution_identity_conflict"
    | "execution_error"
    | "execution_timeout";
}

// ─── §5.1 Shared Context ───────────────────────────────────────────────────

export interface ToolCall {
  id: string;
  name: string;
  arguments: Record<string, unknown>;
}

export interface ExecutionContext {
  apiKeyId: string;
  sessionId: string;
  requestId: string;
  requestIdentity?: string;
  builtinToolNames?: string[];
  injectedCustomSkillNames?: string[];
  customSkillExecutionEnabled?: boolean;
  executionFenceEnabled?: boolean;
  provider?: string;
  model?: string;
}

export interface ExecutedToolResult {
  id: string;
  name: string;
  result: unknown;
  replayed: boolean;
}

// ─── §5.2 Transcript Builder ───────────────────────────────────────────────

export interface BuildFollowUpTranscriptInput {
  sourceBody: Record<string, unknown>;
  previousResponse: Record<string, unknown>;
  toolCalls: ToolCall[];
  results: ExecutedToolResult[];
  sourceFormat: "openai" | "claude";
  maxResultBytes: number;
  maxTotalResultBytes?: number;
  serializedResultTextById?: Map<string, string>;
}

export interface BoundedToolResult {
  text: string;
  truncated: boolean;
  originalBytes: number;
}

// ─── §5.3 Client Translate ─────────────────────────────────────────────────

export interface NonStreamingClientTranslateInput {
  responseBody: Record<string, unknown>;
  responsePayloadFormat: string;
  clientResponseFormat: string;
  sourceFormat: string;
  provider: string;
  model: string;
  requestBody: Record<string, unknown>;
  /**
   * The ORIGINAL client request body (before translation to the provider format).
   * Used to honor client intent that translation drops — e.g. `response_format`
   * is turned into a system instruction for Claude targets, so only the client
   * body still says JSON mode was requested. Falls back to `requestBody`.
   */
  clientRequestBody?: Record<string, unknown> | null;
  /**
   * Transcript used for no-tool_calls reasoning replay (#1628).
   * Must be the client-translated Chat `messages` (parent: `translatedBody.messages`),
   * not `finalBody` — Responses-shaped `finalBody` has `input`, not `messages`. For a
   * Responses-shaped body the parent passes the pivot transcript `translateRequest`
   * reports through `onReasoningReplayHistory` instead.
   */
  historyMessages?: unknown[] | null;
  responseToolNameMap: Map<string, string> | null;
  customToolNames?: ReadonlySet<string>;
  requestToolIdentityMap: Map<string, { namespace?: string; name: string }> | null;
  reasoningCacheScope: string | null;
  /** Never retain reasoning from a response that may echo video transcript cues. */
  videoTranscriptSensitive?: boolean;
  clientHeaders: Headers | Record<string, unknown> | null;
  isClaudeCodeCompatible: boolean;
  /**
   * The client's explicit thinking intent for THIS request (same value the
   * streaming path threads into the SSE translator). Without it the
   * non-streaming OpenAI→Claude conversion falls back to its legacy
   * "always relay a thinking block" default, so the very same request answered
   * with `stream:false` leaked reasoning that `stream:true` correctly withheld.
   * `undefined` keeps the legacy relay for callers that cannot express intent.
   */
  requestedThinking?: boolean;
  phase: "intermediate" | "final";
}

export interface NonStreamingClientTranslateResult {
  response: Record<string, unknown>;
  responseForMemoryExtraction: Record<string, unknown>;
}