|
| 1 | +import consola from "consola" |
| 2 | +import { events } from "fetch-event-stream" |
| 3 | + |
| 4 | +import { copilotHeaders, copilotBaseUrl } from "~/lib/api-config" |
| 5 | +import { HTTPError } from "~/lib/error" |
| 6 | +import { state } from "~/lib/state" |
| 7 | + |
| 8 | +export const createResponses = async (payload: ResponsesPayload) => { |
| 9 | + if (!state.copilotToken) throw new Error("Copilot token not found") |
| 10 | + |
| 11 | + const enableVision = |
| 12 | + Array.isArray(payload.input) |
| 13 | + && payload.input.some( |
| 14 | + (x) => |
| 15 | + Array.isArray(x.content) |
| 16 | + && x.content.some((part) => part.type === "input_image"), |
| 17 | + ) |
| 18 | + |
| 19 | + const isAgentCall = |
| 20 | + Array.isArray(payload.input) |
| 21 | + && payload.input.some((msg) => ["assistant", "tool"].includes(msg.role)) |
| 22 | + |
| 23 | + const headers: Record<string, string> = { |
| 24 | + ...copilotHeaders(state, enableVision), |
| 25 | + "X-Initiator": isAgentCall ? "agent" : "user", |
| 26 | + } |
| 27 | + |
| 28 | + const response = await fetch(`${copilotBaseUrl(state)}/responses`, { |
| 29 | + method: "POST", |
| 30 | + headers, |
| 31 | + body: JSON.stringify(payload), |
| 32 | + }) |
| 33 | + |
| 34 | + if (!response.ok) { |
| 35 | + consola.error("Failed to create response", response) |
| 36 | + throw new HTTPError("Failed to create response", response) |
| 37 | + } |
| 38 | + |
| 39 | + if (payload.stream) { |
| 40 | + return events(response) |
| 41 | + } |
| 42 | + |
| 43 | + return (await response.json()) as ResponseObject |
| 44 | +} |
| 45 | + |
| 46 | +// Payload types |
| 47 | + |
| 48 | +export interface ResponsesPayload { |
| 49 | + model: string |
| 50 | + input: string | Array<InputMessage> |
| 51 | + stream?: boolean | null |
| 52 | + temperature?: number | null |
| 53 | + top_p?: number | null |
| 54 | + max_output_tokens?: number | null |
| 55 | + tools?: Array<ResponseTool> | null |
| 56 | + tool_choice?: |
| 57 | + | "auto" |
| 58 | + | "none" |
| 59 | + | "required" |
| 60 | + | { type: "function"; name: string } |
| 61 | + | null |
| 62 | + previous_response_id?: string | null |
| 63 | + instructions?: string | null |
| 64 | + reasoning?: { effort: "low" | "medium" | "high" } | null |
| 65 | + metadata?: Record<string, string> | null |
| 66 | + user?: string | null |
| 67 | +} |
| 68 | + |
| 69 | +export interface InputMessage { |
| 70 | + role: "user" | "assistant" | "system" | "developer" | "tool" |
| 71 | + content: string | Array<InputContentPart> |
| 72 | + name?: string |
| 73 | + tool_call_id?: string |
| 74 | +} |
| 75 | + |
| 76 | +export type InputContentPart = InputTextPart | InputImagePart | InputFilePart |
| 77 | + |
| 78 | +export interface InputTextPart { |
| 79 | + type: "input_text" |
| 80 | + text: string |
| 81 | +} |
| 82 | + |
| 83 | +export interface InputImagePart { |
| 84 | + type: "input_image" |
| 85 | + image_url?: { url: string; detail?: "low" | "high" | "auto" } |
| 86 | + file_id?: string |
| 87 | +} |
| 88 | + |
| 89 | +export interface InputFilePart { |
| 90 | + type: "input_file" |
| 91 | + file_id?: string |
| 92 | + file_url?: string |
| 93 | + filename?: string |
| 94 | +} |
| 95 | + |
| 96 | +export interface ResponseTool { |
| 97 | + type: "function" |
| 98 | + name: string |
| 99 | + description?: string |
| 100 | + parameters?: Record<string, unknown> |
| 101 | + strict?: boolean | null |
| 102 | +} |
| 103 | + |
| 104 | +// Response types (non-streaming) |
| 105 | + |
| 106 | +export interface ResponseObject { |
| 107 | + id: string |
| 108 | + object: "response" |
| 109 | + created_at: number |
| 110 | + model: string |
| 111 | + output: Array<OutputItem> |
| 112 | + status: "completed" | "incomplete" | "failed" | "cancelled" |
| 113 | + usage?: ResponseUsage |
| 114 | + instructions?: string | null |
| 115 | + error?: ResponseError | null |
| 116 | + metadata?: Record<string, string> | null |
| 117 | +} |
| 118 | + |
| 119 | +export type OutputItem = MessageOutputItem | FunctionCallOutputItem |
| 120 | + |
| 121 | +export interface MessageOutputItem { |
| 122 | + type: "message" |
| 123 | + id: string |
| 124 | + role: "assistant" |
| 125 | + content: Array<OutputContentPart> |
| 126 | + status: "completed" | "incomplete" |
| 127 | +} |
| 128 | + |
| 129 | +export type OutputContentPart = OutputTextPart | RefusalPart |
| 130 | + |
| 131 | +export interface OutputTextPart { |
| 132 | + type: "output_text" |
| 133 | + text: string |
| 134 | + annotations?: Array<unknown> |
| 135 | +} |
| 136 | + |
| 137 | +export interface RefusalPart { |
| 138 | + type: "refusal" |
| 139 | + refusal: string |
| 140 | +} |
| 141 | + |
| 142 | +export interface FunctionCallOutputItem { |
| 143 | + type: "function_call" |
| 144 | + id: string |
| 145 | + call_id: string |
| 146 | + name: string |
| 147 | + arguments: string |
| 148 | + status: "completed" |
| 149 | +} |
| 150 | + |
| 151 | +export interface ResponseUsage { |
| 152 | + input_tokens: number |
| 153 | + output_tokens: number |
| 154 | + total_tokens: number |
| 155 | + input_tokens_details?: { |
| 156 | + cached_tokens: number |
| 157 | + } |
| 158 | + output_tokens_details?: { |
| 159 | + reasoning_tokens: number |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +export interface ResponseError { |
| 164 | + code: string |
| 165 | + message: string |
| 166 | +} |
0 commit comments