UNPKG

@bitkyc08/opencodex

Version:

Universal provider proxy for OpenAI Codex & Claude Code — use any LLM with Codex CLI/App/SDK and Claude Code

220 lines (204 loc) • 9.61 kB
import * as z from "zod/v4"; import { isCompactionItemType } from "./compaction"; const inputTextSchema = z.object({ type: z.literal("input_text"), text: z.string() }); const plainTextSchema = z.object({ type: z.literal("text"), text: z.string() }); const inputImageBlockSchema = z.object({ type: z.literal("input_image"), // codex-rs ImageDetail: auto|low|high|original (view_image --detail original). detail: z.enum(["auto", "low", "high", "original"]).optional(), image_url: z.string().optional(), file_id: z.string().optional(), }).refine(v => typeof v.image_url === "string" || typeof v.file_id === "string", { message: "input_image requires at least one of image_url or file_id", }); const inputVideoBlockSchema = z.object({ type: z.literal("input_video"), video_url: z.string().min(1), // Gemini agentic video understanding (#3271). z.object() strips unknown keys, // so without declaring it here the mode is dropped before any adapter sees it // and the request silently degrades to frame-by-frame decoding. processing: z.string().min(1).optional(), }); const inputFileBlockSchema = z.object({ type: z.literal("input_file"), file_id: z.string().optional(), filename: z.string().optional(), file_data: z.string().optional(), }); // codex-rs protocol/src/models.rs sends audio as input_audio with an audio_url, in // both user content and tool output. The translated IR preserves supported inline // WAV/MP3 bytes and the Chat adapter sends them as input_audio; other wires and // unsupported formats are refused at the final translated-adapter boundary. const inputAudioBlockSchema = z.object({ type: z.literal("input_audio"), audio_url: z.string().min(1), format: z.string().optional(), }); const outputTextSchema = z.object({ type: z.literal("output_text"), text: z.string() }); const outputRefusalSchema = z.object({ type: z.literal("refusal"), refusal: z.string() }); const summaryTextSchema = z.object({ type: z.literal("summary_text"), text: z.string() }); const reasoningTextSchema = z.object({ type: z.literal("reasoning_text"), text: z.string() }); // codex-rs FunctionCallOutputContentItem (protocol/src/models.rs): input_text | input_image | encrypted_content. const encryptedContentBlockSchema = z.object({ type: z.literal("encrypted_content"), encrypted_content: z.string() }); const inputContentBlockSchema = z.union([inputTextSchema, plainTextSchema, inputImageBlockSchema, inputVideoBlockSchema, inputAudioBlockSchema, inputFileBlockSchema]); const outputContentBlockSchema = z.union([outputTextSchema, plainTextSchema, outputRefusalSchema]); // Tool outputs on the wire mix codex-rs FunctionCallOutputContentItem with legacy output blocks. const toolOutputContentBlockSchema = z.union([ outputTextSchema, plainTextSchema, outputRefusalSchema, inputTextSchema, inputImageBlockSchema, inputAudioBlockSchema, encryptedContentBlockSchema, ]); const toolOutputSchema = z.union([z.string(), z.array(toolOutputContentBlockSchema)]); const userMessageItemSchema = z.object({ type: z.literal("message").optional(), role: z.union([z.literal("user"), z.literal("developer")]), content: z.union([z.string(), z.array(inputContentBlockSchema)]).optional(), }); const systemMessageItemSchema = z.object({ type: z.literal("message").optional(), role: z.literal("system"), content: z.union([z.string(), z.array(inputContentBlockSchema)]).optional(), }); const assistantMessageItemSchema = z.object({ type: z.literal("message").optional(), id: z.string().optional(), role: z.literal("assistant"), content: z.union([z.string(), z.array(outputContentBlockSchema)]).optional(), phase: z.enum(["commentary", "final_answer"]).optional(), }); const reasoningItemSchema = z.object({ type: z.literal("reasoning"), id: z.string().optional(), summary: z.array(summaryTextSchema).optional(), content: z.array(reasoningTextSchema).optional(), // Round-tripped opaque payload (native OpenAI encryption OR the proxy's ocxr1 envelope). encrypted_content: z.string().optional(), }); /** A native v1 replacement history may establish replay provenance only after body success. */ export const nativeCompactResponseSchema = z.object({ status: z.literal("completed").optional(), error: z.null().optional(), output: z.array(z.union([ userMessageItemSchema.required({ content: true }), systemMessageItemSchema.required({ content: true }), assistantMessageItemSchema.required({ content: true }), reasoningItemSchema, z.object({ type: z.string().refine(isCompactionItemType), encrypted_content: z.string().refine(value => value.trim().length > 0), }), ])).min(1).refine(items => items.some(item => { if (isCompactionItemType(item.type)) return true; if (!("role" in item)) return false; return typeof item.content === "string" ? item.content.trim().length > 0 : item.content.some(part => "text" in part && part.text.trim().length > 0); })), }); const functionCallItemSchema = z.object({ type: z.literal("function_call"), id: z.string().optional(), call_id: z.string().min(1), name: z.string().min(1), namespace: z.string().optional(), arguments: z.string().optional(), // Provider-opaque metadata that must survive the round trip verbatim (issue #1735). The shape // is bounded on purpose: only the one nested key we round-trip is modeled, so an unexpected // payload cannot ride through as arbitrary passthrough state. extra_content: z.object({ google: z.object({ thought_signature: z.string().optional() }).optional(), }).optional(), }); const functionCallOutputItemSchema = z.object({ type: z.literal("function_call_output"), call_id: z.string().min(1), output: toolOutputSchema.optional(), }); const customToolCallItemSchema = z.object({ type: z.literal("custom_tool_call"), id: z.string().optional(), call_id: z.string().min(1), name: z.string().min(1), input: z.string(), }); const customToolCallOutputItemSchema = z.object({ type: z.literal("custom_tool_call_output"), call_id: z.string().min(1), // codex-rs CustomToolCallOutput carries FunctionCallOutputPayload: string OR content items. output: toolOutputSchema, }); export const inputItemSchema = z.union([ userMessageItemSchema, systemMessageItemSchema, assistantMessageItemSchema, reasoningItemSchema, functionCallItemSchema, functionCallOutputItemSchema, customToolCallItemSchema, customToolCallOutputItemSchema, z.object({ type: z.string() }).loose(), ]); export const toolSchema = z.object({ type: z.literal("function"), name: z.string().min(1), description: z.string().optional(), parameters: z.record(z.string(), z.unknown()).optional(), strict: z.boolean().optional(), // Unknown keys are stripped here, so a field the parser is expected to read has to be // declared: an undeclared allowed_callers never reached buildTools at all (#5210). allowed_callers: z.array(z.string()).optional(), }); const builtinToolSchema = z.object({ type: z.string() }).loose(); /** * Hosted tool types a client may declare on an inbound Responses request. Exported so the * provider-side capability vocabulary in `src/responses/hosted-tool-policy.ts` can be * asserted to cover all of them: a gateway must be able to deny anything it can be sent. */ export const HOSTED_TOOL_TYPES = [ "web_search", "web_search_preview", "file_search", "computer_use_preview", "code_interpreter", "image_generation", "mcp", "tool_search", "computer_use", "image_gen", ] as const; const hostedToolType = z.enum(HOSTED_TOOL_TYPES); const allowedToolEntrySchema = z.union([ z.object({ type: z.literal("mcp"), server_label: z.string().min(1), name: z.string().nullable().optional() }), z.object({ type: z.string(), name: z.string().optional(), namespace: z.string().optional() }), ]); export const toolChoiceSchema = z.union([ z.literal("auto"), z.literal("none"), z.literal("required"), z.object({ type: z.enum(["function", "custom"]), name: z.string().min(1), namespace: z.string().optional() }), z.object({ type: hostedToolType }), z.object({ type: z.literal("allowed_tools"), mode: z.enum(["auto", "required"]), tools: z.array(allowedToolEntrySchema) }), ]); export const reasoningConfigSchema = z.object({ enabled: z.boolean().optional(), effort: z.string().optional(), summary: z.enum(["auto", "concise", "detailed", "none"]).optional(), }); export const stopSchema = z.union([z.string(), z.array(z.string()), z.null()]); export const responsesRequestSchema = z.object({ model: z.string().min(1), input: z.union([z.string(), z.array(inputItemSchema)]).optional(), instructions: z.union([z.string(), z.null()]).optional(), tools: z.array(z.union([toolSchema, builtinToolSchema])).optional(), tool_choice: toolChoiceSchema.optional(), max_output_tokens: z.number().optional(), temperature: z.number().optional(), top_p: z.number().optional(), stop: stopSchema.optional(), stream: z.boolean().optional(), reasoning: reasoningConfigSchema.nullable().optional(), store: z.boolean().optional(), previous_response_id: z.string().optional(), parallel_tool_calls: z.boolean().optional(), prompt_cache_key: z.string().optional(), metadata: z.unknown().optional(), user: z.string().optional(), service_tier: z.string().optional(), presence_penalty: z.number().optional(), frequency_penalty: z.number().optional(), background: z.unknown().optional(), include: z.unknown().optional(), prompt: z.unknown().optional(), text: z.unknown().optional(), truncation: z.unknown().optional(), });