|
| 1 | +/** |
| 2 | + * Create Responses Service |
| 3 | + * PR #195: Add Codex CLI support (@albanx) |
| 4 | + * |
| 5 | + * This service handles the /v1/responses endpoint for OpenAI Codex CLI compatibility. |
| 6 | + * The Codex CLI requires this endpoint to function properly. |
| 7 | + * |
| 8 | + * Docs: |
| 9 | + * - https://developers.openai.com/codex/config-advanced |
| 10 | + * - https://github.com/openai/codex/discussions/7782 |
| 11 | + */ |
| 12 | + |
| 13 | +import consola from "consola" |
| 14 | +import { events } from "fetch-event-stream" |
| 15 | + |
| 16 | +import { copilotBaseUrl, copilotHeaders } from "~/lib/api-config" |
| 17 | +import { HTTPError } from "~/lib/error" |
| 18 | +import { state } from "~/lib/state" |
| 19 | + |
| 20 | +export interface ResponsesPayload { |
| 21 | + model: string |
| 22 | + input: string | Array<ResponseInputItem> |
| 23 | + instructions?: string |
| 24 | + max_output_tokens?: number |
| 25 | + temperature?: number |
| 26 | + top_p?: number |
| 27 | + stream?: boolean |
| 28 | + tools?: Array<ResponseTool> |
| 29 | + tool_choice?: |
| 30 | + | "auto" |
| 31 | + | "none" |
| 32 | + | "required" |
| 33 | + | { type: "function"; name: string } |
| 34 | + reasoning?: { |
| 35 | + effort?: "low" | "medium" | "high" |
| 36 | + } |
| 37 | + truncation?: "auto" | "disabled" |
| 38 | +} |
| 39 | + |
| 40 | +export interface ResponseInputItem { |
| 41 | + type: "message" | "item_reference" |
| 42 | + role?: "user" | "assistant" | "system" |
| 43 | + content?: string | Array<{ type: "input_text"; text: string }> |
| 44 | + id?: string |
| 45 | +} |
| 46 | + |
| 47 | +export interface ResponseTool { |
| 48 | + type: "function" |
| 49 | + name: string |
| 50 | + description?: string |
| 51 | + parameters?: Record<string, unknown> |
| 52 | +} |
| 53 | + |
| 54 | +export interface ResponseOutput { |
| 55 | + id: string |
| 56 | + object: "response" |
| 57 | + created_at: number |
| 58 | + model: string |
| 59 | + output: Array<ResponseOutputItem> |
| 60 | + usage?: { |
| 61 | + input_tokens: number |
| 62 | + output_tokens: number |
| 63 | + total_tokens: number |
| 64 | + } |
| 65 | + status: "completed" | "failed" | "cancelled" | "incomplete" |
| 66 | +} |
| 67 | + |
| 68 | +export interface ResponseOutputItem { |
| 69 | + type: "message" |
| 70 | + id: string |
| 71 | + role: "assistant" |
| 72 | + content: Array<{ type: "output_text"; text: string }> |
| 73 | +} |
| 74 | + |
| 75 | +export const createResponse = async (payload: ResponsesPayload) => { |
| 76 | + if (!state.copilotToken) throw new Error("Copilot token not found") |
| 77 | + |
| 78 | + const headers: Record<string, string> = { |
| 79 | + ...copilotHeaders(state, false), |
| 80 | + "X-Initiator": "agent", |
| 81 | + } |
| 82 | + |
| 83 | + // Copilot uses /responses endpoint |
| 84 | + const response = await fetch(`${copilotBaseUrl(state)}/responses`, { |
| 85 | + method: "POST", |
| 86 | + headers, |
| 87 | + body: JSON.stringify(payload), |
| 88 | + }) |
| 89 | + |
| 90 | + if (!response.ok) { |
| 91 | + consola.error("Failed to create response", response) |
| 92 | + throw new HTTPError("Failed to create response", response) |
| 93 | + } |
| 94 | + |
| 95 | + if (payload.stream) { |
| 96 | + return events(response) |
| 97 | + } |
| 98 | + |
| 99 | + return (await response.json()) as ResponseOutput |
| 100 | +} |
0 commit comments