* OpenAI Responses API endpoint encapsulation using the fetch API. * * The Responses API uses a different request and response shape from Chat Completions: * - **Request body**: `input` (string or array of `PromptSection`) instead of `messages`. * - **Response body**: text is returned inside `ou
(url: string, headers: object, defaultParams: object)
| 229 | * @param defaultParams Additional JSON body parameters merged into every request (e.g. `{ model }`). |
| 230 | */ |
| 231 | function createResponsesFetchLanguageModel(url: string, headers: object, defaultParams: object) { |
| 232 | const model: TypeChatLanguageModel = { |
| 233 | complete |
| 234 | }; |
| 235 | return model; |
| 236 | |
| 237 | async function complete(prompt: string | PromptSection[]) { |
| 238 | let retryCount = 0; |
| 239 | const retryMaxAttempts = model.retryMaxAttempts ?? 3; |
| 240 | const retryPauseMs = model.retryPauseMs ?? 1000; |
| 241 | const input = typeof prompt === "string" ? prompt : (prompt as PromptSection[]); |
| 242 | while (true) { |
| 243 | const options = { |
| 244 | method: "POST", |
| 245 | body: JSON.stringify({ |
| 246 | ...defaultParams, |
| 247 | input, |
| 248 | temperature: 0, |
| 249 | }), |
| 250 | headers: { |
| 251 | "content-type": "application/json", |
| 252 | ...headers |
| 253 | } |
| 254 | } |
| 255 | const response = await fetch(url, options); |
| 256 | if (response.ok) { |
| 257 | type ResponsesAPIOutputItem = { |
| 258 | type: string; |
| 259 | role?: string; |
| 260 | content: { type: string; text: string }[]; |
| 261 | }; |
| 262 | const json = await response.json() as { output: ResponsesAPIOutputItem[] }; |
| 263 | const message = json.output?.find(o => o.type === "message"); |
| 264 | const textContent = message?.content?.find(c => c.type === "output_text"); |
| 265 | if (textContent?.text !== undefined) { |
| 266 | return success(textContent.text); |
| 267 | } else { |
| 268 | return error(`REST API unexpected response format: ${JSON.stringify(json)}`); |
| 269 | } |
| 270 | } |
| 271 | if (!isTransientHttpError(response.status) || retryCount >= retryMaxAttempts) { |
| 272 | return error(`REST API error ${response.status}: ${response.statusText}`); |
| 273 | } |
| 274 | await sleep(getRetryDelayMs(response, retryPauseMs, retryPauseMs * retryMaxAttempts)); |
| 275 | retryCount++; |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Returns the number of milliseconds to wait before the next retry attempt. |
no outgoing calls
no test coverage detected