The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * The `OpenAiSchema` module defines the request, response, streaming, and |
| 3 | * embedding schemas used by the handwritten OpenAI client. These schemas are |
| 4 | * the transport boundary for JSON sent to and decoded from the Responses, |
| 5 | * embeddings, and decisions endpoints. |
| 6 | * |
| 7 | * @stability unstable |
| 8 | * @since 4.0.0 |
| 9 | */ |
| 10 | import * as Effect from "effect/Effect" |
| 11 | import * as Predicate from "effect/Predicate" |
| 12 | import * as Schema from "effect/Schema" |
| 13 | import * as SchemaTransformation from "effect/SchemaTransformation" |
| 14 | |
| 15 | const UnknownRecord = Schema.Record(Schema.String, Schema.Unknown) |
| 16 | |
| 17 | const JsonObject = Schema.Record(Schema.String, Schema.Unknown) |
| 18 | |
| 19 | const MessageRole = Schema.Literals(["system", "developer", "user", "assistant"]) |
| 20 | |
| 21 | const ImageDetail = Schema.Literals(["low", "high", "auto"]) |
| 22 | |
| 23 | const PromptCacheBreakpoint = Schema.Struct({ |
| 24 | mode: Schema.Literal("explicit") |
| 25 | }) |
| 26 | |
| 27 | /** |
| 28 | * Schema for optional `include` values supported by the local handwritten |
| 29 | * Responses client schema. |
| 30 | * |
| 31 | * **Details** |
| 32 | * |
| 33 | * These values request additional response fields such as image URLs, encrypted |
| 34 | * reasoning content, output logprobs, code interpreter outputs, or web search |
| 35 | * sources. This schema enumerates the include values supported by this client |
| 36 | * path. |
| 37 | * |
| 38 | * @stability unstable |
| 39 | * @category schemas |
| 40 | * @since 4.0.0 |
| 41 | */ |
| 42 | export const IncludeEnum = Schema.Literals([ |
| 43 | "message.input_image.image_url", |
| 44 | "reasoning.encrypted_content", |
| 45 | "message.output_text.logprobs", |
| 46 | "code_interpreter_call.outputs", |
| 47 | "web_search_call.action.sources" |
| 48 | ]) |
| 49 | |
| 50 | /** |
| 51 | * Type of optional `include` values accepted by OpenAI Responses requests. |
| 52 | * |
| 53 | * @stability unstable |
| 54 | * @category models |
| 55 | * @since 4.0.0 |
| 56 | */ |
| 57 | export type IncludeEnum = typeof IncludeEnum.Type |
| 58 | |
| 59 | /** |
| 60 | * Schema for lifecycle statuses shared by messages, reasoning items, and tool calls. |
| 61 | * |
| 62 | * **Details** |
| 63 | * |
| 64 | * Accepted values are `"in_progress"`, `"completed"`, and `"incomplete"`. |
| 65 | * This item-level status is used by message, reasoning, and tool-call shapes. |
| 66 | * |
| 67 | * @stability unstable |
| 68 | * @category schemas |
| 69 | * @since 4.0.0 |
| 70 | */ |
| 71 | export const MessageStatus = Schema.Literals(["in_progress", "completed", "incomplete"]) |
| 72 | |
| 73 | /** |
| 74 | * Lifecycle status shared by messages, reasoning items, and tool calls. |
| 75 | * |
| 76 | * **Details** |
| 77 | * |
| 78 | * Accepted values are `"in_progress"`, `"completed"`, and `"incomplete"`. |
| 79 | * |
| 80 | * @stability unstable |
| 81 | * @category models |
| 82 | * @since 4.0.0 |
| 83 | */ |
| 84 | export type MessageStatus = typeof MessageStatus.Type |
| 85 | |
| 86 | const InputTextContent = Schema.Struct({ |
| 87 | type: Schema.Literal("input_text"), |
| 88 | text: Schema.String, |
| 89 | prompt_cache_breakpoint: Schema.optional(PromptCacheBreakpoint) |
| 90 | }) |
| 91 | |
| 92 | const InputImageContent = Schema.Struct({ |
| 93 | type: Schema.Literal("input_image"), |
| 94 | image_url: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 95 | file_id: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 96 | detail: Schema.optionalKey(Schema.NullOr(ImageDetail)), |
| 97 | prompt_cache_breakpoint: Schema.optional(PromptCacheBreakpoint) |
| 98 | }) |
| 99 | |
| 100 | const InputFileContent = Schema.Struct({ |
| 101 | type: Schema.Literal("input_file"), |
| 102 | file_id: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 103 | filename: Schema.optionalKey(Schema.String), |
| 104 | file_url: Schema.optionalKey(Schema.String), |
| 105 | file_data: Schema.optionalKey(Schema.String), |
| 106 | prompt_cache_breakpoint: Schema.optional(PromptCacheBreakpoint) |
| 107 | }) |
| 108 | |
| 109 | /** |
| 110 | * Schema for content blocks accepted in OpenAI Responses input messages. |
| 111 | * |
| 112 | * **Details** |
| 113 | * |
| 114 | * Accepted block variants are `input_text`, `input_image`, and `input_file`. |
| 115 | * |
| 116 | * @see {@link InputItem} for request input item shapes that can contain these content blocks |
| 117 | * |
| 118 | * @stability unstable |
| 119 | * @category schemas |
| 120 | * @since 4.0.0 |
| 121 | */ |
| 122 | export const InputContent = Schema.Union([ |
| 123 | InputTextContent, |
| 124 | InputImageContent, |
| 125 | InputFileContent |
| 126 | ]) |
| 127 | |
| 128 | /** |
| 129 | * Content block accepted in OpenAI Responses input messages. |
| 130 | * |
| 131 | * **Details** |
| 132 | * |
| 133 | * Accepted block variants are `input_text`, `input_image`, and `input_file`. |
| 134 | * |
| 135 | * @stability unstable |
| 136 | * @category models |
| 137 | * @since 4.0.0 |
| 138 | */ |
| 139 | export type InputContent = typeof InputContent.Type |
| 140 | |
| 141 | /** |
| 142 | * Schema for a text block containing a model-provided reasoning summary. |
| 143 | * |
| 144 | * **Details** |
| 145 | * |
| 146 | * The decoded shape is `type: "summary_text"` plus `text` containing the |
| 147 | * reasoning summary text. |
| 148 | * |
| 149 | * @see {@link ReasoningItem} for reasoning output items that contain summary text blocks |
| 150 | * |
| 151 | * @stability unstable |
| 152 | * @category schemas |
| 153 | * @since 4.0.0 |
| 154 | */ |
| 155 | export const SummaryTextContent = Schema.Struct({ |
| 156 | type: Schema.Literal("summary_text"), |
| 157 | text: Schema.String |
| 158 | }) |
| 159 | |
| 160 | /** |
| 161 | * Text content block used for model-provided reasoning summaries. |
| 162 | * |
| 163 | * @stability unstable |
| 164 | * @category models |
| 165 | * @since 4.0.0 |
| 166 | */ |
| 167 | export type SummaryTextContent = typeof SummaryTextContent.Type |
| 168 | |
| 169 | const ReasoningTextContent = Schema.Struct({ |
| 170 | type: Schema.Literal("reasoning_text"), |
| 171 | text: Schema.String |
| 172 | }) |
| 173 | |
| 174 | const RefusalContent = Schema.Struct({ |
| 175 | type: Schema.Literal("refusal"), |
| 176 | refusal: Schema.String |
| 177 | }) |
| 178 | |
| 179 | const TextContent = Schema.Struct({ |
| 180 | type: Schema.Literal("text"), |
| 181 | text: Schema.String |
| 182 | }) |
| 183 | |
| 184 | const ComputerScreenshotContent = Schema.Struct({ |
| 185 | type: Schema.Literal("computer_screenshot"), |
| 186 | image_url: Schema.NullOr(Schema.String), |
| 187 | file_id: Schema.NullOr(Schema.String) |
| 188 | }) |
| 189 | |
| 190 | const FileCitationAnnotation = Schema.Struct({ |
| 191 | type: Schema.Literal("file_citation"), |
| 192 | file_id: Schema.String, |
| 193 | index: Schema.Int, |
| 194 | filename: Schema.String |
| 195 | }) |
| 196 | |
| 197 | const UrlCitationAnnotation = Schema.Struct({ |
| 198 | type: Schema.Literal("url_citation"), |
| 199 | url: Schema.String, |
| 200 | start_index: Schema.Int, |
| 201 | end_index: Schema.Int, |
| 202 | title: Schema.String |
| 203 | }) |
| 204 | |
| 205 | const ContainerFileCitationAnnotation = Schema.Struct({ |
| 206 | type: Schema.Literal("container_file_citation"), |
| 207 | container_id: Schema.String, |
| 208 | file_id: Schema.String, |
| 209 | start_index: Schema.Int, |
| 210 | end_index: Schema.Int, |
| 211 | filename: Schema.String |
| 212 | }) |
| 213 | |
| 214 | const FilePathAnnotation = Schema.Struct({ |
| 215 | type: Schema.Literal("file_path"), |
| 216 | file_id: Schema.String, |
| 217 | index: Schema.Int |
| 218 | }) |
| 219 | |
| 220 | /** |
| 221 | * Schema for citation and file-path annotations attached to output text content. |
| 222 | * |
| 223 | * **Details** |
| 224 | * |
| 225 | * Accepts annotation objects discriminated by `type`: `file_citation`, |
| 226 | * `url_citation`, `container_file_citation`, or `file_path`. |
| 227 | * |
| 228 | * @stability unstable |
| 229 | * @category schemas |
| 230 | * @since 4.0.0 |
| 231 | */ |
| 232 | export const Annotation = Schema.Union([ |
| 233 | FileCitationAnnotation, |
| 234 | UrlCitationAnnotation, |
| 235 | ContainerFileCitationAnnotation, |
| 236 | FilePathAnnotation |
| 237 | ]) |
| 238 | |
| 239 | /** |
| 240 | * Citation or file-path annotation attached to output text content. |
| 241 | * |
| 242 | * **Details** |
| 243 | * |
| 244 | * Accepted annotation variants are `file_citation`, `url_citation`, |
| 245 | * `container_file_citation`, and `file_path`. |
| 246 | * |
| 247 | * @stability unstable |
| 248 | * @category models |
| 249 | * @since 4.0.0 |
| 250 | */ |
| 251 | export type Annotation = typeof Annotation.Type |
| 252 | |
| 253 | const OutputTextContent = Schema.Struct({ |
| 254 | type: Schema.Literal("output_text"), |
| 255 | text: Schema.String, |
| 256 | annotations: Schema.Array(Annotation), |
| 257 | logprobs: Schema.optionalKey(Schema.Array(Schema.Unknown)) |
| 258 | }) |
| 259 | |
| 260 | const OutputMessageContent = Schema.Union([ |
| 261 | InputTextContent, |
| 262 | OutputTextContent, |
| 263 | TextContent, |
| 264 | SummaryTextContent, |
| 265 | ReasoningTextContent, |
| 266 | RefusalContent, |
| 267 | InputImageContent, |
| 268 | ComputerScreenshotContent, |
| 269 | InputFileContent |
| 270 | ]) |
| 271 | |
| 272 | const OutputMessage = Schema.Struct({ |
| 273 | id: Schema.String, |
| 274 | type: Schema.Literal("message"), |
| 275 | role: Schema.Literal("assistant"), |
| 276 | content: Schema.Array(OutputMessageContent), |
| 277 | status: MessageStatus |
| 278 | }) |
| 279 | |
| 280 | /** |
| 281 | * Schema for a reasoning output item containing encrypted content, summaries, and optional reasoning text. |
| 282 | * |
| 283 | * **When to use** |
| 284 | * |
| 285 | * Use when decoding or encoding OpenAI Responses reasoning items that may be |
| 286 | * carried into later request input. |
| 287 | * |
| 288 | * **Details** |
| 289 | * |
| 290 | * Reasoning items represent model reasoning content. `summary` is required, |
| 291 | * while `content` and `status` are optional. |
| 292 | * |
| 293 | * **Gotchas** |
| 294 | * |
| 295 | * `encrypted_content` is populated only when `reasoning.encrypted_content` is |
| 296 | * requested through `include`. |
| 297 | * |
| 298 | * @see {@link InputItem} for request input items that can carry reasoning items |
| 299 | * @see {@link IncludeEnum} for requesting encrypted reasoning content |
| 300 | * |
| 301 | * @stability unstable |
| 302 | * @category schemas |
| 303 | * @since 4.0.0 |
| 304 | */ |
| 305 | export const ReasoningItem = Schema.Struct({ |
| 306 | type: Schema.Literal("reasoning"), |
| 307 | id: Schema.String, |
| 308 | encrypted_content: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 309 | summary: Schema.Array(SummaryTextContent), |
| 310 | content: Schema.optionalKey(Schema.Array(ReasoningTextContent)), |
| 311 | status: Schema.optionalKey(MessageStatus) |
| 312 | }) |
| 313 | |
| 314 | /** |
| 315 | * Reasoning output item containing encrypted content, summaries, and optional reasoning text. |
| 316 | * |
| 317 | * **When to use** |
| 318 | * |
| 319 | * Use when typing OpenAI Responses reasoning items that may be carried into |
| 320 | * later request input. |
| 321 | * |
| 322 | * **Details** |
| 323 | * |
| 324 | * Reasoning items represent model reasoning content. `summary` is required, |
| 325 | * while `content` and `status` are optional. |
| 326 | * |
| 327 | * **Gotchas** |
| 328 | * |
| 329 | * `encrypted_content` is populated only when `reasoning.encrypted_content` is |
| 330 | * requested through `include`. |
| 331 | * |
| 332 | * @stability unstable |
| 333 | * @category models |
| 334 | * @since 4.0.0 |
| 335 | */ |
| 336 | export type ReasoningItem = typeof ReasoningItem.Type |
| 337 | |
| 338 | const FunctionCall = Schema.Struct({ |
| 339 | id: Schema.optionalKey(Schema.String), |
| 340 | type: Schema.Literal("function_call"), |
| 341 | call_id: Schema.String, |
| 342 | name: Schema.String, |
| 343 | arguments: Schema.String, |
| 344 | status: Schema.optionalKey(MessageStatus) |
| 345 | }) |
| 346 | |
| 347 | const FunctionCallOutput = Schema.Struct({ |
| 348 | id: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 349 | type: Schema.Literal("function_call_output"), |
| 350 | call_id: Schema.String, |
| 351 | output: Schema.Union([ |
| 352 | Schema.String, |
| 353 | Schema.Array(InputContent) |
| 354 | ]), |
| 355 | status: Schema.optionalKey(Schema.NullOr(MessageStatus)) |
| 356 | }) |
| 357 | |
| 358 | const ItemReference = Schema.Struct({ |
| 359 | type: Schema.Literal("item_reference"), |
| 360 | id: Schema.String |
| 361 | }) |
| 362 | |
| 363 | const LocalShellCall = Schema.Struct({ |
| 364 | id: Schema.optionalKey(Schema.String), |
| 365 | type: Schema.Literal("local_shell_call"), |
| 366 | call_id: Schema.String, |
| 367 | action: Schema.Unknown, |
| 368 | status: Schema.optionalKey(MessageStatus) |
| 369 | }) |
| 370 | |
| 371 | const LocalShellCallOutput = Schema.Struct({ |
| 372 | id: Schema.optionalKey(Schema.String), |
| 373 | type: Schema.Literal("local_shell_call_output"), |
| 374 | call_id: Schema.String, |
| 375 | output: Schema.Unknown, |
| 376 | status: Schema.optionalKey(MessageStatus) |
| 377 | }) |
| 378 | |
| 379 | const ShellCall = Schema.Struct({ |
| 380 | id: Schema.optionalKey(Schema.String), |
| 381 | type: Schema.Literal("shell_call"), |
| 382 | call_id: Schema.String, |
| 383 | action: Schema.Unknown, |
| 384 | status: Schema.optionalKey(MessageStatus) |
| 385 | }) |
| 386 | |
| 387 | const ShellCallOutput = Schema.Struct({ |
| 388 | id: Schema.optionalKey(Schema.String), |
| 389 | type: Schema.Literal("shell_call_output"), |
| 390 | call_id: Schema.String, |
| 391 | output: Schema.Unknown, |
| 392 | status: Schema.optionalKey(MessageStatus) |
| 393 | }) |
| 394 | |
| 395 | const ApplyPatchCallOutput = Schema.Struct({ |
| 396 | id: Schema.optionalKey(Schema.String), |
| 397 | type: Schema.Literal("apply_patch_call_output"), |
| 398 | call_id: Schema.String, |
| 399 | status: Schema.optionalKey(MessageStatus), |
| 400 | output: Schema.optionalKey(Schema.Unknown) |
| 401 | }) |
| 402 | |
| 403 | const McpApprovalResponse = Schema.Struct({ |
| 404 | type: Schema.Literal("mcp_approval_response"), |
| 405 | approval_request_id: Schema.String, |
| 406 | approve: Schema.Boolean |
| 407 | }) |
| 408 | |
| 409 | const RequestMessageItem = Schema.Struct({ |
| 410 | type: Schema.optionalKey(Schema.Literal("message")), |
| 411 | role: MessageRole, |
| 412 | status: Schema.optionalKey(MessageStatus), |
| 413 | content: Schema.Union([ |
| 414 | Schema.String, |
| 415 | Schema.Array(InputContent) |
| 416 | ]) |
| 417 | }) |
| 418 | |
| 419 | const InputCodeInterpreterCall = Schema.Struct({ |
| 420 | id: Schema.String, |
| 421 | type: Schema.Literal("code_interpreter_call"), |
| 422 | code: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 423 | container_id: Schema.String, |
| 424 | outputs: Schema.optionalKey(Schema.NullOr(Schema.Array(Schema.Unknown))), |
| 425 | status: Schema.optionalKey( |
| 426 | Schema.Literals(["in_progress", "completed", "incomplete", "interpreting", "failed"]) |
| 427 | ) |
| 428 | }) |
| 429 | |
| 430 | const InputFileSearchCall = Schema.Struct({ |
| 431 | id: Schema.String, |
| 432 | type: Schema.Literal("file_search_call"), |
| 433 | status: Schema.optionalKey(Schema.String), |
| 434 | queries: Schema.optionalKey(Schema.Array(Schema.String)), |
| 435 | results: Schema.optionalKey(Schema.NullOr(Schema.Unknown)) |
| 436 | }) |
| 437 | |
| 438 | const InputImageGenerationCall = Schema.Struct({ |
| 439 | id: Schema.String, |
| 440 | type: Schema.Literal("image_generation_call"), |
| 441 | result: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 442 | status: Schema.optionalKey(Schema.Literals(["in_progress", "completed", "generating", "failed"])) |
| 443 | }) |
| 444 | |
| 445 | const InputWebSearchCall = Schema.Struct({ |
| 446 | id: Schema.String, |
| 447 | type: Schema.Literal("web_search_call"), |
| 448 | action: Schema.optionalKey(Schema.Unknown), |
| 449 | status: Schema.optionalKey(Schema.String) |
| 450 | }) |
| 451 | |
| 452 | /** |
| 453 | * Schema for item shapes accepted by an OpenAI Responses request `input` field. |
| 454 | * |
| 455 | * **When to use** |
| 456 | * |
| 457 | * Use when validating structured `CreateResponse.input` array items. |
| 458 | * |
| 459 | * **Details** |
| 460 | * |
| 461 | * Accepted item families include request/output messages, function call and |
| 462 | * function call output, reasoning items, item references, shell and local shell |
| 463 | * calls and outputs, apply-patch output, and MCP approval responses. |
| 464 | * |
| 465 | * @see {@link CreateResponse} for the request schema that consumes input items |
| 466 | * @see {@link InputContent} for content blocks inside message items |
| 467 | * |
| 468 | * @stability unstable |
| 469 | * @category schemas |
| 470 | * @since 4.0.0 |
| 471 | */ |
| 472 | export const InputItem = Schema.Union([ |
| 473 | RequestMessageItem, |
| 474 | OutputMessage, |
| 475 | FunctionCall, |
| 476 | FunctionCallOutput, |
| 477 | ReasoningItem, |
| 478 | ItemReference, |
| 479 | LocalShellCall, |
| 480 | LocalShellCallOutput, |
| 481 | ShellCall, |
| 482 | ShellCallOutput, |
| 483 | ApplyPatchCallOutput, |
| 484 | McpApprovalResponse, |
| 485 | InputCodeInterpreterCall, |
| 486 | InputFileSearchCall, |
| 487 | InputImageGenerationCall, |
| 488 | InputWebSearchCall |
| 489 | ]) |
| 490 | |
| 491 | /** |
| 492 | * Item shape accepted by an OpenAI Responses request `input` field. |
| 493 | * |
| 494 | * **When to use** |
| 495 | * |
| 496 | * Use when typing structured `CreateResponse.input` array items. |
| 497 | * |
| 498 | * **Details** |
| 499 | * |
| 500 | * Accepted item families include request/output messages, function call and |
| 501 | * function call output, reasoning items, item references, shell and local shell |
| 502 | * calls and outputs, apply-patch output, and MCP approval responses. |
| 503 | * |
| 504 | * @stability unstable |
| 505 | * @category models |
| 506 | * @since 4.0.0 |
| 507 | */ |
| 508 | export type InputItem = typeof InputItem.Type |
| 509 | |
| 510 | const FunctionTool = Schema.Struct({ |
| 511 | type: Schema.Literal("function"), |
| 512 | name: Schema.String, |
| 513 | description: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 514 | parameters: Schema.optionalKey(Schema.NullOr(JsonObject)), |
| 515 | strict: Schema.optionalKey(Schema.NullOr(Schema.Boolean)) |
| 516 | }) |
| 517 | |
| 518 | const CustomTool = Schema.Struct({ |
| 519 | type: Schema.Literal("custom"), |
| 520 | name: Schema.String, |
| 521 | description: Schema.optionalKey(Schema.String), |
| 522 | format: Schema.optionalKey(Schema.Unknown) |
| 523 | }) |
| 524 | |
| 525 | const ProviderDefinedTool = Schema.StructWithRest( |
| 526 | Schema.Struct({ |
| 527 | type: Schema.Literals([ |
| 528 | "apply_patch", |
| 529 | "code_interpreter", |
| 530 | "file_search", |
| 531 | "image_generation", |
| 532 | "local_shell", |
| 533 | "mcp", |
| 534 | "shell", |
| 535 | "web_search", |
| 536 | "web_search_preview" |
| 537 | ]) |
| 538 | }), |
| 539 | [UnknownRecord] |
| 540 | ) |
| 541 | |
| 542 | /** |
| 543 | * Schema for tool definitions that can be supplied to an OpenAI Responses request. |
| 544 | * |
| 545 | * **When to use** |
| 546 | * |
| 547 | * Use when validating or encoding the `tools` array for a Responses request, |
| 548 | * including provider-defined tool records with provider-specific fields. |
| 549 | * |
| 550 | * **Details** |
| 551 | * |
| 552 | * Accepted variants are function tools, custom tools, and provider-defined |
| 553 | * OpenAI tools. Provider-defined `type` literals include `apply_patch`, |
| 554 | * `code_interpreter`, `file_search`, `image_generation`, `local_shell`, `mcp`, |
| 555 | * `shell`, `web_search`, and `web_search_preview`. |
| 556 | * |
| 557 | * **Gotchas** |
| 558 | * |
| 559 | * Provider-defined tools use `Schema.StructWithRest`, so this schema checks the |
| 560 | * provider tool `type` and permits additional provider fields rather than fully |
| 561 | * validating every provider-specific tool payload. |
| 562 | * |
| 563 | * @see {@link ToolChoice} for selecting whether and which tools the model may call |
| 564 | * @see {@link CreateResponse} for the request schema that consumes tools |
| 565 | * |
| 566 | * @stability unstable |
| 567 | * @category schemas |
| 568 | * @since 4.0.0 |
| 569 | */ |
| 570 | export const Tool = Schema.Union([ |
| 571 | FunctionTool, |
| 572 | CustomTool, |
| 573 | ProviderDefinedTool |
| 574 | ]) |
| 575 | |
| 576 | /** |
| 577 | * Tool definition that can be supplied to an OpenAI Responses request. |
| 578 | * |
| 579 | * @stability unstable |
| 580 | * @category models |
| 581 | * @since 4.0.0 |
| 582 | */ |
| 583 | export type Tool = typeof Tool.Type |
| 584 | |
| 585 | /** |
| 586 | * Schema for selecting whether and which tools the model may call in a Responses request. |
| 587 | * |
| 588 | * **When to use** |
| 589 | * |
| 590 | * Use when validating or encoding the `tool_choice` field that constrains model |
| 591 | * tool use separately from the tool definitions themselves. |
| 592 | * |
| 593 | * **Details** |
| 594 | * |
| 595 | * Accepted forms are `"none"`, `"auto"`, `"required"`, an allowed-tools set, |
| 596 | * a named function or custom tool, or a provider-defined tool choice. |
| 597 | * |
| 598 | * @see {@link Tool} for tool definitions referenced by tool choices |
| 599 | * @see {@link CreateResponse} for the request schema that consumes `tool_choice` |
| 600 | * |
| 601 | * @stability unstable |
| 602 | * @category schemas |
| 603 | * @since 4.0.0 |
| 604 | */ |
| 605 | export const ToolChoice = Schema.Union([ |
| 606 | Schema.Literals(["none", "auto", "required"]), |
| 607 | Schema.Struct({ |
| 608 | type: Schema.Literal("allowed_tools"), |
| 609 | mode: Schema.Literals(["auto", "required"]), |
| 610 | tools: Schema.Array(JsonObject) |
| 611 | }), |
| 612 | Schema.Struct({ |
| 613 | type: Schema.Literal("function"), |
| 614 | name: Schema.String |
| 615 | }), |
| 616 | Schema.Struct({ |
| 617 | type: Schema.Literal("custom"), |
| 618 | name: Schema.String |
| 619 | }), |
| 620 | Schema.StructWithRest( |
| 621 | Schema.Struct({ |
| 622 | type: Schema.Literals([ |
| 623 | "apply_patch", |
| 624 | "code_interpreter", |
| 625 | "file_search", |
| 626 | "image_generation", |
| 627 | "local_shell", |
| 628 | "mcp", |
| 629 | "shell", |
| 630 | "web_search", |
| 631 | "web_search_preview" |
| 632 | ]) |
| 633 | }), |
| 634 | [UnknownRecord] |
| 635 | ) |
| 636 | ]) |
| 637 | |
| 638 | /** |
| 639 | * Tool selection mode or named tool choice for a Responses request. |
| 640 | * |
| 641 | * **Details** |
| 642 | * |
| 643 | * Accepted forms are `"none"`, `"auto"`, `"required"`, an allowed-tools set, |
| 644 | * a named function or custom tool, or a provider-defined tool choice. |
| 645 | * |
| 646 | * @stability unstable |
| 647 | * @category models |
| 648 | * @since 4.0.0 |
| 649 | */ |
| 650 | export type ToolChoice = typeof ToolChoice.Type |
| 651 | |
| 652 | /** |
| 653 | * Schema for text output format configuration, including plain text, JSON object, and JSON Schema responses. |
| 654 | * |
| 655 | * **When to use** |
| 656 | * |
| 657 | * Use when validating or encoding the `text.format` setting for a Responses |
| 658 | * request, especially when choosing structured JSON Schema output. |
| 659 | * |
| 660 | * **Details** |
| 661 | * |
| 662 | * Accepted variants are `text`, `json_schema`, and `json_object`. |
| 663 | * |
| 664 | * **Gotchas** |
| 665 | * |
| 666 | * `json_object` is the older JSON mode. Prefer `json_schema` for models that |
| 667 | * support it. |
| 668 | * |
| 669 | * @see {@link CreateResponse} for the request schema that consumes text format configuration |
| 670 | * |
| 671 | * @stability unstable |
| 672 | * @category schemas |
| 673 | * @since 4.0.0 |
| 674 | */ |
| 675 | export const TextResponseFormatConfiguration = Schema.Union([ |
| 676 | Schema.Struct({ type: Schema.Literal("text") }), |
| 677 | Schema.Struct({ |
| 678 | type: Schema.Literal("json_schema"), |
| 679 | description: Schema.optionalKey(Schema.String), |
| 680 | name: Schema.String, |
| 681 | schema: JsonObject, |
| 682 | strict: Schema.optionalKey(Schema.NullOr(Schema.Boolean)) |
| 683 | }), |
| 684 | Schema.Struct({ type: Schema.Literal("json_object") }) |
| 685 | ]) |
| 686 | |
| 687 | /** |
| 688 | * Text output format configuration for plain text, JSON object, or JSON Schema responses. |
| 689 | * |
| 690 | * @stability unstable |
| 691 | * @category models |
| 692 | * @since 4.0.0 |
| 693 | */ |
| 694 | export type TextResponseFormatConfiguration = typeof TextResponseFormatConfiguration.Type |
| 695 | |
| 696 | /** |
| 697 | * Schema for request options used to create an OpenAI Responses API response. |
| 698 | * |
| 699 | * **When to use** |
| 700 | * |
| 701 | * Use to validate or encode payloads sent to the OpenAI Responses API. |
| 702 | * |
| 703 | * **Details** |
| 704 | * |
| 705 | * Validates the Responses API request payload, including input content, model |
| 706 | * selection, instructions, reasoning options, text output format, tools, |
| 707 | * `tool_choice`, streaming, storage, response continuation, sampling options, |
| 708 | * safety identifiers, prompt caching, and optional response fields requested |
| 709 | * through `include`. |
| 710 | * |
| 711 | * **Gotchas** |
| 712 | * |
| 713 | * When `stream` is `true`, the API returns stream events instead of a single |
| 714 | * response object. |
| 715 | * |
| 716 | * @see {@link Response} for decoded non-streaming response objects |
| 717 | * @see {@link ResponseStreamEvent} for decoded streaming event objects |
| 718 | * |
| 719 | * @stability unstable |
| 720 | * @category schemas |
| 721 | * @since 4.0.0 |
| 722 | */ |
| 723 | export const CreateResponse = Schema.Struct({ |
| 724 | metadata: Schema.optional(Schema.Record(Schema.String, Schema.String)), |
| 725 | top_logprobs: Schema.optional(Schema.Int), |
| 726 | temperature: Schema.optional(Schema.Finite), |
| 727 | top_p: Schema.optional(Schema.Finite), |
| 728 | user: Schema.optional(Schema.String), |
| 729 | safety_identifier: Schema.optional(Schema.String), |
| 730 | prompt_cache_key: Schema.optional(Schema.String), |
| 731 | prompt_cache_retention: Schema.optional(Schema.Literals(["in_memory", "24h"])), |
| 732 | prompt_cache_options: Schema.optional(Schema.Struct({ |
| 733 | mode: Schema.optional(Schema.Literals(["implicit", "explicit"])), |
| 734 | ttl: Schema.optional(Schema.Literal("30m")), |
| 735 | comparison_response_id: Schema.optional(Schema.NullOr(Schema.String)) |
| 736 | })), |
| 737 | service_tier: Schema.optional(Schema.String), |
| 738 | previous_response_id: Schema.optional(Schema.String), |
| 739 | model: Schema.optional(Schema.String), |
| 740 | reasoning: Schema.optional(Schema.Struct({ |
| 741 | effort: Schema.optional(Schema.Literals(["none", "minimal", "low", "medium", "high", "xhigh", "max"])), |
| 742 | |
| 743 | summary: Schema.optional(Schema.Literals(["auto", "concise", "detailed"])), |
| 744 | generate_summary: Schema.optional(Schema.Literals(["auto", "concise", "detailed"])) |
| 745 | })), |
| 746 | background: Schema.optional(Schema.Boolean), |
| 747 | max_output_tokens: Schema.optional(Schema.Int), |
| 748 | max_tool_calls: Schema.optional(Schema.Int), |
| 749 | text: Schema.optional( |
| 750 | Schema.Struct({ |
| 751 | format: Schema.optional(TextResponseFormatConfiguration), |
| 752 | verbosity: Schema.optional(Schema.Literals(["low", "medium", "high"])) |
| 753 | }) |
| 754 | ), |
| 755 | tools: Schema.optional(Schema.Array(Tool)), |
| 756 | tool_choice: Schema.optional(ToolChoice), |
| 757 | truncation: Schema.optional(Schema.Literals(["auto", "disabled"])), |
| 758 | input: Schema.optional( |
| 759 | Schema.Union([ |
| 760 | Schema.String, |
| 761 | Schema.Array(InputItem) |
| 762 | ]) |
| 763 | ), |
| 764 | include: Schema.optional(Schema.Array(IncludeEnum)), |
| 765 | store: Schema.optional(Schema.Boolean), |
| 766 | instructions: Schema.optional(Schema.String), |
| 767 | stream: Schema.optional(Schema.Boolean), |
| 768 | conversation: Schema.optional(Schema.String), |
| 769 | modalities: Schema.optional(Schema.Array(Schema.Literals(["text", "audio"]))), |
| 770 | seed: Schema.optional(Schema.Int) |
| 771 | }) |
| 772 | |
| 773 | /** |
| 774 | * Request options used to create an OpenAI Responses API response. |
| 775 | * |
| 776 | * @stability unstable |
| 777 | * @category options |
| 778 | * @since 4.0.0 |
| 779 | */ |
| 780 | export type CreateResponse = typeof CreateResponse.Type |
| 781 | |
| 782 | /** |
| 783 | * Schema for token accounting reported on OpenAI Responses API response objects. |
| 784 | * |
| 785 | * **Details** |
| 786 | * |
| 787 | * The required counters are `input_tokens`, `output_tokens`, and |
| 788 | * `total_tokens`. Provider-specific token detail objects are preserved through |
| 789 | * `input_tokens_details`, `output_tokens_details`, and additional fields. |
| 790 | * |
| 791 | * @stability unstable |
| 792 | * @category schemas |
| 793 | * @since 4.0.0 |
| 794 | */ |
| 795 | export const ResponseUsage = Schema.StructWithRest( |
| 796 | Schema.Struct({ |
| 797 | input_tokens: Schema.Int, |
| 798 | output_tokens: Schema.Int, |
| 799 | total_tokens: Schema.Int, |
| 800 | input_tokens_details: Schema.optionalKey(Schema.Unknown), |
| 801 | output_tokens_details: Schema.optionalKey(Schema.Unknown) |
| 802 | }), |
| 803 | [UnknownRecord] |
| 804 | ) |
| 805 | |
| 806 | /** |
| 807 | * Token accounting reported on OpenAI Responses API response objects. |
| 808 | * |
| 809 | * **Details** |
| 810 | * |
| 811 | * Includes total input, output, and combined token counts, with provider-specific |
| 812 | * token detail fields preserved when present. |
| 813 | * |
| 814 | * @stability unstable |
| 815 | * @category models |
| 816 | * @since 4.0.0 |
| 817 | */ |
| 818 | export type ResponseUsage = typeof ResponseUsage.Type |
| 819 | |
| 820 | const ApplyPatchOperation = Schema.Struct({ |
| 821 | type: Schema.String, |
| 822 | path: Schema.String, |
| 823 | diff: Schema.optionalKey(Schema.String) |
| 824 | }) |
| 825 | |
| 826 | const ApplyPatchCall = Schema.Struct({ |
| 827 | id: Schema.String, |
| 828 | type: Schema.Literal("apply_patch_call"), |
| 829 | call_id: Schema.String, |
| 830 | operation: ApplyPatchOperation, |
| 831 | status: Schema.optionalKey(MessageStatus) |
| 832 | }) |
| 833 | |
| 834 | const CodeInterpreterCall = Schema.Struct({ |
| 835 | id: Schema.String, |
| 836 | type: Schema.Literal("code_interpreter_call"), |
| 837 | code: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 838 | container_id: Schema.String, |
| 839 | outputs: Schema.optionalKey(Schema.NullOr(Schema.Array(Schema.Unknown))), |
| 840 | status: Schema.optionalKey( |
| 841 | Schema.Literals(["in_progress", "completed", "incomplete", "interpreting", "failed"]) |
| 842 | ) |
| 843 | }) |
| 844 | |
| 845 | const ComputerCall = Schema.Struct({ |
| 846 | id: Schema.String, |
| 847 | type: Schema.Literal("computer_call"), |
| 848 | status: Schema.optionalKey(MessageStatus) |
| 849 | }) |
| 850 | |
| 851 | const FileSearchCall = Schema.Struct({ |
| 852 | id: Schema.String, |
| 853 | type: Schema.Literal("file_search_call"), |
| 854 | status: Schema.optionalKey(Schema.String), |
| 855 | queries: Schema.optionalKey(Schema.Array(Schema.String)), |
| 856 | results: Schema.optionalKey(Schema.NullOr(Schema.Unknown)) |
| 857 | }) |
| 858 | |
| 859 | const ImageGenerationCall = Schema.Struct({ |
| 860 | id: Schema.String, |
| 861 | type: Schema.Literal("image_generation_call"), |
| 862 | result: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 863 | status: Schema.optionalKey(Schema.Literals(["in_progress", "completed", "generating", "failed"])) |
| 864 | }) |
| 865 | |
| 866 | const McpCall = Schema.Struct({ |
| 867 | id: Schema.String, |
| 868 | type: Schema.Literal("mcp_call"), |
| 869 | approval_request_id: Schema.optionalKey(Schema.NullOr(Schema.String)), |
| 870 | name: Schema.String, |
| 871 | arguments: Schema.Unknown, |
| 872 | output: Schema.optionalKey(Schema.Unknown), |
| 873 | error: Schema.optionalKey(Schema.Unknown), |
| 874 | server_label: Schema.optionalKey(Schema.NullOr(Schema.String)) |
| 875 | }) |
| 876 | |
| 877 | const McpListTools = Schema.Struct({ |
| 878 | id: Schema.String, |
| 879 | type: Schema.Literal("mcp_list_tools") |
| 880 | }) |
| 881 | |
| 882 | const McpApprovalRequest = Schema.Struct({ |
| 883 | id: Schema.String, |
| 884 | type: Schema.Literal("mcp_approval_request"), |
| 885 | approval_request_id: Schema.optionalKey(Schema.String), |
| 886 | name: Schema.String, |
| 887 | arguments: Schema.Unknown |
| 888 | }) |
| 889 | |
| 890 | const WebSearchCall = Schema.Struct({ |
| 891 | id: Schema.String, |
| 892 | type: Schema.Literal("web_search_call"), |
| 893 | action: Schema.optionalKey(Schema.Unknown), |
| 894 | status: Schema.optionalKey(Schema.String) |
| 895 | }) |
| 896 | |
| 897 | const OutputItem = Schema.Union([ |
| 898 | ApplyPatchCall, |
| 899 | CodeInterpreterCall, |
| 900 | ComputerCall, |
| 901 | FileSearchCall, |
| 902 | FunctionCall, |
| 903 | ImageGenerationCall, |
| 904 | LocalShellCall, |
| 905 | McpCall, |
| 906 | McpListTools, |
| 907 | McpApprovalRequest, |
| 908 | OutputMessage, |
| 909 | ReasoningItem, |
| 910 | ShellCall, |
| 911 | WebSearchCall |
| 912 | ]) |
| 913 | |
| 914 | const ResponseError = Schema.Struct({ |
| 915 | code: Schema.String, |
| 916 | message: Schema.String |
| 917 | }) |
| 918 | |
| 919 | /** |
| 920 | * Schema for prompt cache diagnostics comparing two responses. |
| 921 | * |
| 922 | * **Details** |
| 923 | * |
| 924 | * Cache misses report a reason and token estimates, separate from response usage. |
| 925 | * |
| 926 | * @stability unstable |
| 927 | * @category schemas |
| 928 | * @since 4.0.0 |
| 929 | */ |
| 930 | export const PromptCacheDiagnostics = Schema.Union([ |
| 931 | Schema.Struct({ |
| 932 | type: Schema.Literal("cache_miss"), |
| 933 | reason: Schema.Literals([ |
| 934 | "model_changed", |
| 935 | "prompt_cache_key_changed", |
| 936 | "tools_changed", |
| 937 | "text_format_changed", |
| 938 | "reasoning_effort_changed", |
| 939 | "verbosity_changed", |
| 940 | "context_compacted", |
| 941 | "input_changed", |
| 942 | "service_tier_changed" |
| 943 | ]), |
| 944 | cache_missed_tokens: Schema.Int, |
| 945 | comparison_reusable_tokens: Schema.optionalKey(Schema.Int) |
| 946 | }), |
| 947 | Schema.Struct({ type: Schema.Literal("cache_hit") }), |
| 948 | Schema.Struct({ type: Schema.Literal("comparison_response_not_found") }), |
| 949 | Schema.Struct({ type: Schema.Literal("unavailable") }) |
| 950 | ]) |
| 951 | |
| 952 | /** |
| 953 | * Prompt cache comparison result reported by the OpenAI Responses API. |
| 954 | * |
| 955 | * @stability unstable |
| 956 | * @category models |
| 957 | * @since 4.0.0 |
| 958 | */ |
| 959 | export type PromptCacheDiagnostics = typeof PromptCacheDiagnostics.Type |
| 960 | |
| 961 | /** |
| 962 | * Schema for an OpenAI Responses API response object. |
| 963 | * |
| 964 | * **When to use** |
| 965 | * |
| 966 | * Use to decode non-streaming OpenAI Responses API responses. |
| 967 | * |
| 968 | * **Details** |
| 969 | * |
| 970 | * Response objects include the response id, model, creation time, output items, |
| 971 | * optional token usage, optional incomplete details, optional service tier, and |
| 972 | * optional prompt cache diagnostics. |
| 973 | * |
| 974 | * @see {@link CreateResponse} for the request schema that creates responses |
| 975 | * @see {@link ResponseUsage} for token accounting on responses |
| 976 | * @see {@link ResponseStreamEvent} for streaming response events |
| 977 | * |
| 978 | * @stability unstable |
| 979 | * @category schemas |
| 980 | * @since 4.0.0 |
| 981 | */ |
| 982 | export const Response = Schema.Struct({ |
| 983 | id: Schema.String, |
| 984 | object: Schema.optionalKey(Schema.Literal("response")), |
| 985 | model: Schema.String, |
| 986 | created_at: Schema.Int, |
| 987 | output: Schema.Array(OutputItem).pipe( |
| 988 | Schema.withDecodingDefault(Effect.succeed([])) |
| 989 | ), |
| 990 | usage: Schema.optionalKey(Schema.NullOr(ResponseUsage)), |
| 991 | error: Schema.optionalKey(Schema.NullOr(ResponseError)), |
| 992 | incomplete_details: Schema.optionalKey( |
| 993 | Schema.NullOr( |
| 994 | Schema.Struct({ |
| 995 | reason: Schema.optionalKey(Schema.Literals(["max_output_tokens", "content_filter"])) |
| 996 | }) |
| 997 | ) |
| 998 | ), |
| 999 | service_tier: Schema.optionalKey(Schema.String), |
| 1000 | prompt_cache_diagnostics: Schema.optionalKey(PromptCacheDiagnostics) |
| 1001 | }) |
| 1002 | |
| 1003 | /** |
| 1004 | * OpenAI Responses API response object. |
| 1005 | * |
| 1006 | * **When to use** |
| 1007 | * |
| 1008 | * Use when typing non-streaming OpenAI Responses API responses. |
| 1009 | * |
| 1010 | * **Details** |
| 1011 | * |
| 1012 | * Response objects include metadata, output items, optional token usage, and |
| 1013 | * optional incomplete details. |
| 1014 | * |
| 1015 | * @stability unstable |
| 1016 | * @category models |
| 1017 | * @since 4.0.0 |
| 1018 | */ |
| 1019 | export type Response = typeof Response.Type |
| 1020 | |
| 1021 | const ResponseCreatedEvent = Schema.Struct({ |
| 1022 | type: Schema.Literal("response.created"), |
| 1023 | response: Response, |
| 1024 | sequence_number: Schema.optionalKey(Schema.Int) |
| 1025 | }) |
| 1026 | |
| 1027 | const ResponseCompletedEvent = Schema.Struct({ |
| 1028 | type: Schema.Literal("response.completed"), |
| 1029 | response: Response, |
| 1030 | sequence_number: Schema.optionalKey(Schema.Int) |
| 1031 | }) |
| 1032 | |
| 1033 | const ResponseIncompleteEvent = Schema.Struct({ |
| 1034 | type: Schema.Literal("response.incomplete"), |
| 1035 | response: Response, |
| 1036 | sequence_number: Schema.optionalKey(Schema.Int) |
| 1037 | }) |
| 1038 | |
| 1039 | const ResponseFailedEvent = Schema.Struct({ |
| 1040 | type: Schema.Literal("response.failed"), |
| 1041 | response: Response, |
| 1042 | sequence_number: Schema.optionalKey(Schema.Int) |
| 1043 | }) |
| 1044 | |
| 1045 | const ResponseOutputItemAddedEvent = Schema.Struct({ |
| 1046 | type: Schema.Literal("response.output_item.added"), |
| 1047 | output_index: Schema.Int, |
| 1048 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1049 | item: OutputItem |
| 1050 | }) |
| 1051 | |
| 1052 | const ResponseOutputItemDoneEvent = Schema.Struct({ |
| 1053 | type: Schema.Literal("response.output_item.done"), |
| 1054 | output_index: Schema.Int, |
| 1055 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1056 | item: OutputItem |
| 1057 | }) |
| 1058 | |
| 1059 | const ResponseOutputTextDeltaEvent = Schema.Struct({ |
| 1060 | type: Schema.Literal("response.output_text.delta"), |
| 1061 | item_id: Schema.String, |
| 1062 | output_index: Schema.Int, |
| 1063 | content_index: Schema.Int, |
| 1064 | delta: Schema.String, |
| 1065 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1066 | logprobs: Schema.optionalKey(Schema.Array(Schema.Unknown)) |
| 1067 | }) |
| 1068 | |
| 1069 | const ResponseOutputTextAnnotationAddedEvent = Schema.Struct({ |
| 1070 | type: Schema.Literal("response.output_text.annotation.added"), |
| 1071 | item_id: Schema.String, |
| 1072 | output_index: Schema.Int, |
| 1073 | content_index: Schema.Int, |
| 1074 | annotation_index: Schema.Int, |
| 1075 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1076 | annotation: Annotation |
| 1077 | }) |
| 1078 | |
| 1079 | const ResponseReasoningSummaryPartAddedEvent = Schema.Struct({ |
| 1080 | type: Schema.Literal("response.reasoning_summary_part.added"), |
| 1081 | item_id: Schema.String, |
| 1082 | output_index: Schema.Int, |
| 1083 | summary_index: Schema.Int, |
| 1084 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1085 | part: SummaryTextContent |
| 1086 | }) |
| 1087 | |
| 1088 | const ResponseReasoningSummaryPartDoneEvent = Schema.Struct({ |
| 1089 | type: Schema.Literal("response.reasoning_summary_part.done"), |
| 1090 | item_id: Schema.String, |
| 1091 | output_index: Schema.Int, |
| 1092 | summary_index: Schema.Int, |
| 1093 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1094 | part: SummaryTextContent |
| 1095 | }) |
| 1096 | |
| 1097 | const ResponseReasoningSummaryTextDeltaEvent = Schema.Struct({ |
| 1098 | type: Schema.Literal("response.reasoning_summary_text.delta"), |
| 1099 | item_id: Schema.String, |
| 1100 | output_index: Schema.Int, |
| 1101 | summary_index: Schema.Int, |
| 1102 | delta: Schema.String, |
| 1103 | sequence_number: Schema.optionalKey(Schema.Int) |
| 1104 | }) |
| 1105 | |
| 1106 | const ResponseFunctionCallArgumentsDeltaEvent = Schema.Struct({ |
| 1107 | type: Schema.Literal("response.function_call_arguments.delta"), |
| 1108 | item_id: Schema.String, |
| 1109 | output_index: Schema.Int, |
| 1110 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1111 | delta: Schema.String |
| 1112 | }) |
| 1113 | |
| 1114 | const ResponseFunctionCallArgumentsDoneEvent = Schema.Struct({ |
| 1115 | type: Schema.Literal("response.function_call_arguments.done"), |
| 1116 | item_id: Schema.String, |
| 1117 | output_index: Schema.Int, |
| 1118 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1119 | arguments: Schema.String |
| 1120 | }) |
| 1121 | |
| 1122 | const ResponseCodeInterpreterCallCodeDeltaEvent = Schema.Struct({ |
| 1123 | type: Schema.Literal("response.code_interpreter_call_code.delta"), |
| 1124 | item_id: Schema.String, |
| 1125 | output_index: Schema.Int, |
| 1126 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1127 | delta: Schema.String |
| 1128 | }) |
| 1129 | |
| 1130 | const ResponseCodeInterpreterCallCodeDoneEvent = Schema.Struct({ |
| 1131 | type: Schema.Literal("response.code_interpreter_call_code.done"), |
| 1132 | item_id: Schema.String, |
| 1133 | output_index: Schema.Int, |
| 1134 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1135 | code: Schema.String |
| 1136 | }) |
| 1137 | |
| 1138 | const ResponseApplyPatchCallOperationDiffDeltaEvent = Schema.Struct({ |
| 1139 | type: Schema.Literal("response.apply_patch_call_operation_diff.delta"), |
| 1140 | item_id: Schema.String, |
| 1141 | output_index: Schema.Int, |
| 1142 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1143 | delta: Schema.String |
| 1144 | }) |
| 1145 | |
| 1146 | const ResponseApplyPatchCallOperationDiffDoneEvent = Schema.Struct({ |
| 1147 | type: Schema.Literal("response.apply_patch_call_operation_diff.done"), |
| 1148 | item_id: Schema.String, |
| 1149 | output_index: Schema.Int, |
| 1150 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1151 | delta: Schema.optionalKey(Schema.String) |
| 1152 | }) |
| 1153 | |
| 1154 | const ResponseImageGenerationCallPartialImageEvent = Schema.Struct({ |
| 1155 | type: Schema.Literal("response.image_generation_call.partial_image"), |
| 1156 | item_id: Schema.String, |
| 1157 | output_index: Schema.Int, |
| 1158 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1159 | partial_image_b64: Schema.String |
| 1160 | }) |
| 1161 | |
| 1162 | const ResponseErrorEvent = Schema.Struct({ |
| 1163 | type: Schema.Literal("error"), |
| 1164 | code: Schema.NullOr(Schema.String), |
| 1165 | message: Schema.String, |
| 1166 | param: Schema.NullOr(Schema.String), |
| 1167 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1168 | status: Schema.optionalKey(Schema.Int) |
| 1169 | }) |
| 1170 | |
| 1171 | // OpenAI can nest stream error details under `error`. |
| 1172 | const NestedResponseErrorEvent = Schema.Struct({ |
| 1173 | type: Schema.Literal("error"), |
| 1174 | error: Schema.Struct({ |
| 1175 | code: Schema.NullOr(Schema.String), |
| 1176 | message: Schema.String, |
| 1177 | param: Schema.NullOr(Schema.String) |
| 1178 | }), |
| 1179 | sequence_number: Schema.optionalKey(Schema.Int), |
| 1180 | status: Schema.optionalKey(Schema.Int) |
| 1181 | }).pipe( |
| 1182 | Schema.decodeTo( |
| 1183 | ResponseErrorEvent, |
| 1184 | SchemaTransformation.transform({ |
| 1185 | decode: ({ error, ...rest }) => ({ ...rest, ...error }), |
| 1186 | encode: ({ code, message, param, ...rest }) => ({ ...rest, error: { code, message, param } }) |
| 1187 | }) |
| 1188 | ) |
| 1189 | ) |
| 1190 | |
| 1191 | const knownResponseStreamEventTypes = new Set([ |
| 1192 | "response.created", |
| 1193 | "response.completed", |
| 1194 | "response.incomplete", |
| 1195 | "response.failed", |
| 1196 | "response.output_item.added", |
| 1197 | "response.output_item.done", |
| 1198 | "response.output_text.delta", |
| 1199 | "response.output_text.annotation.added", |
| 1200 | "response.reasoning_summary_part.added", |
| 1201 | "response.reasoning_summary_part.done", |
| 1202 | "response.reasoning_summary_text.delta", |
| 1203 | "response.function_call_arguments.delta", |
| 1204 | "response.function_call_arguments.done", |
| 1205 | "response.code_interpreter_call_code.delta", |
| 1206 | "response.code_interpreter_call_code.done", |
| 1207 | "response.apply_patch_call_operation_diff.delta", |
| 1208 | "response.apply_patch_call_operation_diff.done", |
| 1209 | "response.image_generation_call.partial_image", |
| 1210 | "error" |
| 1211 | ]) |
| 1212 | |
| 1213 | /** |
| 1214 | * Fallback event shape for future or provider-specific response stream events. |
| 1215 | * |
| 1216 | * @stability unstable |
| 1217 | * @category models |
| 1218 | * @since 4.0.0 |
| 1219 | */ |
| 1220 | export type UnknownResponseStreamEvent = { |
| 1221 | readonly type: string |
| 1222 | readonly [key: string]: unknown |
| 1223 | } |
| 1224 | |
| 1225 | const UnknownResponseStreamEvent = Schema.declare<UnknownResponseStreamEvent>( |
| 1226 | (value): value is UnknownResponseStreamEvent => |
| 1227 | Predicate.hasProperty(value, "type") && |
| 1228 | typeof value.type === "string" && |
| 1229 | !knownResponseStreamEventTypes.has(value.type), |
| 1230 | { |
| 1231 | identifier: "UnknownResponseStreamEvent", |
| 1232 | description: "Fallback for unknown future stream events" |
| 1233 | } |
| 1234 | ) |
| 1235 | |
| 1236 | /** |
| 1237 | * Schema for server-sent event shapes emitted by OpenAI Responses API streams. |
| 1238 | * |
| 1239 | * **When to use** |
| 1240 | * |
| 1241 | * Use to decode events from a streaming OpenAI Responses API request. |
| 1242 | * |
| 1243 | * **Details** |
| 1244 | * |
| 1245 | * Known event variants include response lifecycle events, output item events, |
| 1246 | * text and reasoning deltas, tool-call deltas, partial image events, and error |
| 1247 | * events. |
| 1248 | * |
| 1249 | * **Gotchas** |
| 1250 | * |
| 1251 | * Future event types decode through the fallback only when their `type` is not |
| 1252 | * one of the known event types. Malformed known events still fail to decode. |
| 1253 | * |
| 1254 | * @see {@link Response} for complete response objects carried by lifecycle events |
| 1255 | * @see {@link UnknownResponseStreamEvent} for the fallback shape for future event types |
| 1256 | * |
| 1257 | * @stability unstable |
| 1258 | * @category schemas |
| 1259 | * @since 4.0.0 |
| 1260 | */ |
| 1261 | export const ResponseStreamEvent = Schema.Union([ |
| 1262 | ResponseCreatedEvent, |
| 1263 | ResponseCompletedEvent, |
| 1264 | ResponseIncompleteEvent, |
| 1265 | ResponseFailedEvent, |
| 1266 | ResponseOutputItemAddedEvent, |
| 1267 | ResponseOutputItemDoneEvent, |
| 1268 | ResponseOutputTextDeltaEvent, |
| 1269 | ResponseOutputTextAnnotationAddedEvent, |
| 1270 | ResponseReasoningSummaryPartAddedEvent, |
| 1271 | ResponseReasoningSummaryPartDoneEvent, |
| 1272 | ResponseReasoningSummaryTextDeltaEvent, |
| 1273 | ResponseFunctionCallArgumentsDeltaEvent, |
| 1274 | ResponseFunctionCallArgumentsDoneEvent, |
| 1275 | ResponseCodeInterpreterCallCodeDeltaEvent, |
| 1276 | ResponseCodeInterpreterCallCodeDoneEvent, |
| 1277 | ResponseApplyPatchCallOperationDiffDeltaEvent, |
| 1278 | ResponseApplyPatchCallOperationDiffDoneEvent, |
| 1279 | ResponseImageGenerationCallPartialImageEvent, |
| 1280 | ResponseErrorEvent, |
| 1281 | NestedResponseErrorEvent, |
| 1282 | UnknownResponseStreamEvent |
| 1283 | ]) |
| 1284 | |
| 1285 | /** |
| 1286 | * Server-sent event shape emitted by OpenAI Responses API streams. |
| 1287 | * |
| 1288 | * **When to use** |
| 1289 | * |
| 1290 | * Use when typing events from a streaming OpenAI Responses API request. |
| 1291 | * |
| 1292 | * **Details** |
| 1293 | * |
| 1294 | * Includes known response stream events plus a fallback shape for unknown future |
| 1295 | * event types. |
| 1296 | * |
| 1297 | * @stability unstable |
| 1298 | * @category models |
| 1299 | * @since 4.0.0 |
| 1300 | */ |
| 1301 | export type ResponseStreamEvent = typeof ResponseStreamEvent.Type |
| 1302 | |
| 1303 | /** |
| 1304 | * Schema for one embedding item returned by the OpenAI embeddings API. |
| 1305 | * |
| 1306 | * **When to use** |
| 1307 | * |
| 1308 | * Use when validating individual embedding entries at the OpenAI client boundary |
| 1309 | * before assuming the embedding payload is a numeric vector. |
| 1310 | * |
| 1311 | * **Details** |
| 1312 | * |
| 1313 | * An embedding item contains its `index`, optional `object` marker, and an |
| 1314 | * `embedding` represented either as a numeric vector or as a string. |
| 1315 | * |
| 1316 | * **Gotchas** |
| 1317 | * |
| 1318 | * Callers that need numeric vectors must account for string embeddings, such as |
| 1319 | * base64-encoded embeddings returned for string encoding formats. |
| 1320 | * |
| 1321 | * @stability unstable |
| 1322 | * @category schemas |
| 1323 | * @since 4.0.0 |
| 1324 | */ |
| 1325 | export const Embedding = Schema.Struct({ |
| 1326 | embedding: Schema.Union([ |
| 1327 | Schema.Array(Schema.Finite), |
| 1328 | Schema.String |
| 1329 | ]), |
| 1330 | index: Schema.Int, |
| 1331 | object: Schema.optionalKey(Schema.String) |
| 1332 | }) |
| 1333 | |
| 1334 | /** |
| 1335 | * One embedding item returned by the OpenAI embeddings API. |
| 1336 | * |
| 1337 | * **Details** |
| 1338 | * |
| 1339 | * Contains the item index and embedding payload. The embedding payload may be a |
| 1340 | * numeric vector or a string. |
| 1341 | * |
| 1342 | * @stability unstable |
| 1343 | * @category models |
| 1344 | * @since 4.0.0 |
| 1345 | */ |
| 1346 | export type Embedding = typeof Embedding.Type |
| 1347 | |
| 1348 | /** |
| 1349 | * Schema for the request payload sent to the OpenAI embeddings endpoint. |
| 1350 | * |
| 1351 | * **When to use** |
| 1352 | * |
| 1353 | * Use when validating or encoding embeddings requests before sending them to |
| 1354 | * OpenAI, while leaving model-specific limits to the provider. |
| 1355 | * |
| 1356 | * **Details** |
| 1357 | * |
| 1358 | * Requires `input` and `model`. `input` may be a string, an array of strings, |
| 1359 | * a token array, or an array of token arrays. Optional fields configure the |
| 1360 | * embedding encoding format, requested dimensions, and user identifier. |
| 1361 | * |
| 1362 | * **Gotchas** |
| 1363 | * |
| 1364 | * This schema validates the transport shape, but OpenAI still enforces |
| 1365 | * provider-side constraints such as non-empty input, integer token ids, input |
| 1366 | * size limits, positive dimensions, and model-specific dimension support. |
| 1367 | * |
| 1368 | * @stability unstable |
| 1369 | * @category schemas |
| 1370 | * @since 4.0.0 |
| 1371 | */ |
| 1372 | export const CreateEmbeddingRequest = Schema.Struct({ |
| 1373 | input: Schema.Union([ |
| 1374 | Schema.String, |
| 1375 | Schema.Array(Schema.String), |
| 1376 | Schema.Array(Schema.Int), |
| 1377 | Schema.Array(Schema.Array(Schema.Int)) |
| 1378 | ]), |
| 1379 | model: Schema.String, |
| 1380 | encoding_format: Schema.optionalKey(Schema.Literals(["float", "base64"])), |
| 1381 | dimensions: Schema.optionalKey(Schema.Int), |
| 1382 | user: Schema.optionalKey(Schema.String) |
| 1383 | }) |
| 1384 | |
| 1385 | /** |
| 1386 | * Request payload sent to the OpenAI embeddings endpoint. |
| 1387 | * |
| 1388 | * @stability unstable |
| 1389 | * @category models |
| 1390 | * @since 4.0.0 |
| 1391 | */ |
| 1392 | export type CreateEmbeddingRequest = typeof CreateEmbeddingRequest.Type |
| 1393 | |
| 1394 | /** |
| 1395 | * Schema for a successful response payload returned by the OpenAI embeddings endpoint. |
| 1396 | * |
| 1397 | * **When to use** |
| 1398 | * |
| 1399 | * Use when you need to validate embeddings responses at an OpenAI client |
| 1400 | * boundary before trusting item shapes, especially when numeric and string |
| 1401 | * embeddings are both allowed. |
| 1402 | * |
| 1403 | * **Details** |
| 1404 | * |
| 1405 | * The response contains an array of `Embedding` items, the model name, an |
| 1406 | * optional `object: "list"` marker, and optional token usage counts for prompt |
| 1407 | * and total tokens. |
| 1408 | * |
| 1409 | * **Gotchas** |
| 1410 | * |
| 1411 | * Each `Embedding` may contain either a numeric vector or a string embedding. |
| 1412 | * Callers that require numeric vectors must account for string embeddings. |
| 1413 | * |
| 1414 | * @see {@link CreateEmbeddingRequest} for the request schema sent to the embeddings endpoint |
| 1415 | * @see {@link Embedding} for individual embedding items in the response |
| 1416 | * |
| 1417 | * @stability unstable |
| 1418 | * @category schemas |
| 1419 | * @since 4.0.0 |
| 1420 | */ |
| 1421 | export const CreateEmbeddingResponse = Schema.Struct({ |
| 1422 | data: Schema.Array(Embedding), |
| 1423 | model: Schema.String, |
| 1424 | object: Schema.optionalKey(Schema.Literal("list")), |
| 1425 | usage: Schema.optionalKey( |
| 1426 | Schema.Struct({ |
| 1427 | prompt_tokens: Schema.Int, |
| 1428 | total_tokens: Schema.Int |
| 1429 | }) |
| 1430 | ) |
| 1431 | }) |
| 1432 | |
| 1433 | /** |
| 1434 | * Successful response payload returned by the OpenAI embeddings endpoint. |
| 1435 | * |
| 1436 | * **When to use** |
| 1437 | * |
| 1438 | * Use when typing successful OpenAI embeddings responses. |
| 1439 | * |
| 1440 | * **Details** |
| 1441 | * |
| 1442 | * Contains embedding items, the model name, optional list marker, and optional |
| 1443 | * token usage counts. |
| 1444 | * |
| 1445 | * @stability unstable |
| 1446 | * @category models |
| 1447 | * @since 4.0.0 |
| 1448 | */ |
| 1449 | export type CreateEmbeddingResponse = typeof CreateEmbeddingResponse.Type |
| 1450 | |
| 1451 | // ============================================================================= |
| 1452 | // Decisions |
| 1453 | // ============================================================================= |
| 1454 | |
| 1455 | /** |
| 1456 | * Schema for a question sent to the OpenAI Decisions API. |
| 1457 | * |
| 1458 | * **Details** |
| 1459 | * |
| 1460 | * A `predicate` asks for the probability that a statement holds. A `choice` |
| 1461 | * picks one of the supplied `choices`. A `score` rates against `levels`, |
| 1462 | * ordered from lowest to highest. Each question needs a `name` that is unique |
| 1463 | * within the request. |
| 1464 | * |
| 1465 | * @stability unstable |
| 1466 | * @category schemas |
| 1467 | * @since 4.0.5 |
| 1468 | */ |
| 1469 | export const DecisionQuestion = Schema.Union([ |
| 1470 | Schema.Struct({ |
| 1471 | type: Schema.Literal("predicate"), |
| 1472 | name: Schema.String, |
| 1473 | instructions: Schema.String |
| 1474 | }), |
| 1475 | Schema.Struct({ |
| 1476 | type: Schema.Literal("choice"), |
| 1477 | name: Schema.String, |
| 1478 | instructions: Schema.String, |
| 1479 | choices: Schema.Array(Schema.Struct({ value: Schema.String, description: Schema.String })) |
| 1480 | }), |
| 1481 | Schema.Struct({ |
| 1482 | type: Schema.Literal("score"), |
| 1483 | name: Schema.String, |
| 1484 | instructions: Schema.String, |
| 1485 | levels: Schema.Array(Schema.Struct({ label: Schema.String, description: Schema.String })) |
| 1486 | }) |
| 1487 | ]) |
| 1488 | |
| 1489 | /** |
| 1490 | * A question sent to the OpenAI Decisions API. |
| 1491 | * |
| 1492 | * @stability unstable |
| 1493 | * @category models |
| 1494 | * @since 4.0.5 |
| 1495 | */ |
| 1496 | export type DecisionQuestion = typeof DecisionQuestion.Type |
| 1497 | |
| 1498 | /** |
| 1499 | * Schema for a content part in a Decisions API input message. |
| 1500 | * |
| 1501 | * **Gotchas** |
| 1502 | * |
| 1503 | * `image_url` must be an inline base64 `data:` URL. OpenAI rejects hosted |
| 1504 | * URLs and file ids. |
| 1505 | * |
| 1506 | * @stability unstable |
| 1507 | * @category schemas |
| 1508 | * @since 4.0.5 |
| 1509 | */ |
| 1510 | export const DecisionInputContent = Schema.Union([ |
| 1511 | Schema.Struct({ type: Schema.Literal("input_text"), text: Schema.String }), |
| 1512 | Schema.Struct({ type: Schema.Literal("input_image"), image_url: Schema.String }) |
| 1513 | ]) |
| 1514 | |
| 1515 | /** |
| 1516 | * A content part in a Decisions API input message. |
| 1517 | * |
| 1518 | * @stability unstable |
| 1519 | * @category models |
| 1520 | * @since 4.0.5 |
| 1521 | */ |
| 1522 | export type DecisionInputContent = typeof DecisionInputContent.Type |
| 1523 | |
| 1524 | /** |
| 1525 | * Schema for the request payload sent to `POST /decisions`. |
| 1526 | * |
| 1527 | * **Details** |
| 1528 | * |
| 1529 | * `input` is either a string or a list of user messages made of text and image |
| 1530 | * parts. |
| 1531 | * |
| 1532 | * @stability unstable |
| 1533 | * @category schemas |
| 1534 | * @since 4.0.5 |
| 1535 | */ |
| 1536 | export const CreateDecisionsRequest = Schema.Struct({ |
| 1537 | model: Schema.Union([Schema.Literal("gpt-6-luna"), Schema.StringForLiteralAutocomplete]), |
| 1538 | input: Schema.Union([ |
| 1539 | Schema.String, |
| 1540 | Schema.Array(Schema.Struct({ role: Schema.Literal("user"), content: Schema.Array(DecisionInputContent) })) |
| 1541 | ]), |
| 1542 | questions: Schema.Array(DecisionQuestion) |
| 1543 | }) |
| 1544 | |
| 1545 | /** |
| 1546 | * Request payload sent to `POST /decisions`. |
| 1547 | * |
| 1548 | * @stability unstable |
| 1549 | * @category models |
| 1550 | * @since 4.0.5 |
| 1551 | */ |
| 1552 | export type CreateDecisionsRequest = typeof CreateDecisionsRequest.Type |
| 1553 | |
| 1554 | /** |
| 1555 | * Schema for an answer returned by the OpenAI Decisions API. |
| 1556 | * |
| 1557 | * **Details** |
| 1558 | * |
| 1559 | * Each answer echoes the `name` of its question. Answers to `predicate`, |
| 1560 | * `choice`, and `score` questions have the same `type` as the question. A |
| 1561 | * `score` is a probability-weighted average of the 0-based level indices, so |
| 1562 | * it can fall between levels. |
| 1563 | * |
| 1564 | * **Gotchas** |
| 1565 | * |
| 1566 | * OpenAI can refuse a single question. That answer has `type: "refusal"` and |
| 1567 | * no result fields. |
| 1568 | * |
| 1569 | * @stability unstable |
| 1570 | * @category schemas |
| 1571 | * @since 4.0.5 |
| 1572 | */ |
| 1573 | export const DecisionAnswer = Schema.Union([ |
| 1574 | Schema.Struct({ |
| 1575 | type: Schema.Literal("predicate"), |
| 1576 | name: Schema.String, |
| 1577 | probability: Schema.Number |
| 1578 | }), |
| 1579 | Schema.Struct({ |
| 1580 | type: Schema.Literal("choice"), |
| 1581 | name: Schema.String, |
| 1582 | choice: Schema.String, |
| 1583 | probabilities: Schema.Array(Schema.Struct({ value: Schema.String, probability: Schema.Number })), |
| 1584 | confidence: Schema.Number |
| 1585 | }), |
| 1586 | Schema.Struct({ |
| 1587 | type: Schema.Literal("score"), |
| 1588 | name: Schema.String, |
| 1589 | score: Schema.Number, |
| 1590 | probabilities: Schema.Array(Schema.Struct({ |
| 1591 | value: Schema.Int, |
| 1592 | label: Schema.String, |
| 1593 | probability: Schema.Number |
| 1594 | })), |
| 1595 | confidence: Schema.Number |
| 1596 | }), |
| 1597 | Schema.Struct({ |
| 1598 | type: Schema.Literal("refusal"), |
| 1599 | name: Schema.String |
| 1600 | }) |
| 1601 | ]) |
| 1602 | |
| 1603 | /** |
| 1604 | * An answer returned by the OpenAI Decisions API. |
| 1605 | * |
| 1606 | * @stability unstable |
| 1607 | * @category models |
| 1608 | * @since 4.0.5 |
| 1609 | */ |
| 1610 | export type DecisionAnswer = typeof DecisionAnswer.Type |
| 1611 | |
| 1612 | /** |
| 1613 | * Schema for a successful response from `POST /decisions`. |
| 1614 | * |
| 1615 | * **Gotchas** |
| 1616 | * |
| 1617 | * OpenAI does not document usage for this endpoint, so `usage` and its token |
| 1618 | * counts are optional. |
| 1619 | * |
| 1620 | * @stability unstable |
| 1621 | * @category schemas |
| 1622 | * @since 4.0.5 |
| 1623 | */ |
| 1624 | export const CreateDecisionsResponse = Schema.Struct({ |
| 1625 | answers: Schema.Array(DecisionAnswer), |
| 1626 | usage: Schema.optionalKey(Schema.Struct({ |
| 1627 | input_tokens: Schema.optionalKey(Schema.Int), |
| 1628 | output_tokens: Schema.optionalKey(Schema.Int) |
| 1629 | })) |
| 1630 | }) |
| 1631 | |
| 1632 | /** |
| 1633 | * Successful response from `POST /decisions`. |
| 1634 | * |
| 1635 | * @stability unstable |
| 1636 | * @category models |
| 1637 | * @since 4.0.5 |
| 1638 | */ |
| 1639 | export type CreateDecisionsResponse = typeof CreateDecisionsResponse.Type |