@warlock.js/ai-openai 5.25.0 → 5.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/cjs/index.cjs +1043 -272
  3. package/cjs/index.cjs.map +1 -1
  4. package/esm/config.type.d.mts +36 -2
  5. package/esm/config.type.d.mts.map +1 -1
  6. package/esm/known-reasoning-models.mjs +4 -2
  7. package/esm/known-reasoning-models.mjs.map +1 -1
  8. package/esm/known-vision-models.mjs +3 -0
  9. package/esm/known-vision-models.mjs.map +1 -1
  10. package/esm/model.mjs +52 -99
  11. package/esm/model.mjs.map +1 -1
  12. package/esm/responses-model.mjs +329 -0
  13. package/esm/responses-model.mjs.map +1 -0
  14. package/esm/sdk.d.mts +7 -0
  15. package/esm/sdk.d.mts.map +1 -1
  16. package/esm/sdk.mjs +21 -1
  17. package/esm/sdk.mjs.map +1 -1
  18. package/esm/utils/build-usage.mjs +23 -0
  19. package/esm/utils/build-usage.mjs.map +1 -0
  20. package/esm/utils/index.mjs +9 -1
  21. package/esm/utils/map-responses-status.mjs +54 -0
  22. package/esm/utils/map-responses-status.mjs.map +1 -0
  23. package/esm/utils/prompt-cache-params.mjs +17 -0
  24. package/esm/utils/prompt-cache-params.mjs.map +1 -0
  25. package/esm/utils/reasoning-effort.mjs +35 -0
  26. package/esm/utils/reasoning-effort.mjs.map +1 -0
  27. package/esm/utils/reasoning-replay.mjs +85 -0
  28. package/esm/utils/reasoning-replay.mjs.map +1 -0
  29. package/esm/utils/structured-output.mjs +84 -0
  30. package/esm/utils/structured-output.mjs.map +1 -0
  31. package/esm/utils/to-openai-messages.mjs +49 -5
  32. package/esm/utils/to-openai-messages.mjs.map +1 -1
  33. package/esm/utils/to-openai-tools.mjs +3 -3
  34. package/esm/utils/to-openai-tools.mjs.map +1 -1
  35. package/esm/utils/to-responses-input.mjs +112 -0
  36. package/esm/utils/to-responses-input.mjs.map +1 -0
  37. package/esm/utils/to-responses-tools.mjs +51 -0
  38. package/esm/utils/to-responses-tools.mjs.map +1 -0
  39. package/esm/utils/wrap-openai-error.mjs +7 -2
  40. package/esm/utils/wrap-openai-error.mjs.map +1 -1
  41. package/llms-full.txt +43 -4
  42. package/llms.txt +1 -1
  43. package/package.json +4 -4
  44. package/skills/setup-openai/SKILL.md +43 -4
@@ -1,4 +1,4 @@
1
- import { ClientOptions } from "openai";
1
+ import OpenAI, { ClientOptions } from "openai";
2
2
  import { EmbedderConfig, ImageModelConfig, ModelConfig, ModelPricing, SpeechModelConfig, TranscriptionModelConfig } from "@warlock.js/ai";
3
3
 
4
4
  //#region ../ai-openai/src/config.type.d.ts
@@ -64,6 +64,18 @@ type OpenAISDKConfig = Omit<ClientOptions, "provider"> & {
64
64
  * openai.model({ name: "some-legacy-model", responseFormat: "json_object" });
65
65
  */
66
66
  type OpenAIResponseFormat = "json_schema" | "json_object" | "text";
67
+ /**
68
+ * Which OpenAI endpoint a model speaks.
69
+ *
70
+ * - `"chat"` (default) - Chat Completions. Reasoning models reject function
71
+ * tools here unless reasoning is off, so the adapter sends
72
+ * `reasoning_effort: "none"` when tools are attached.
73
+ * - `"responses"` - the Responses API, stateless (`store: false`). Reasoning
74
+ * stays at the provider default even with tools attached. Direct OpenAI
75
+ * only: `OpenAISDK.model()` throws for any other provider label, so the
76
+ * wrapper packages (deepseek, groq, xai, mistral) can never reach it.
77
+ */
78
+ type OpenAIApi = "chat" | "responses";
67
79
  /**
68
80
  * Per-model configuration for `OpenAISDK.model()`. Extends the neutral
69
81
  * `ModelConfig` with OpenAI-specific capability overrides.
@@ -71,8 +83,18 @@ type OpenAIResponseFormat = "json_schema" | "json_object" | "text";
71
83
  * @example
72
84
  * openai.model({ name: "gpt-4o-mini" }); // vision auto-true
73
85
  * openai.model({ name: "fine-tuned-x", vision: true }); // dev override
86
+ *
87
+ * @example
88
+ * // Reasoning model + tools on the Responses API (direct OpenAI only):
89
+ * openai.model({ name: "gpt-5.6", api: "responses" });
74
90
  */
75
91
  type OpenAIModelConfig = ModelConfig & {
92
+ /**
93
+ * Wire API to use. Default `"chat"` (Chat Completions). `"responses"`
94
+ * selects the stateless Responses adapter and is accepted only on the
95
+ * direct `"openai"` provider label; see {@link OpenAIApi}.
96
+ */
97
+ api?: OpenAIApi;
76
98
  /**
77
99
  * Override the auto-inferred vision capability. When omitted, the
78
100
  * adapter checks the model name against a known-prefix list (see
@@ -108,13 +130,25 @@ type OpenAIModelConfig = ModelConfig & {
108
130
  * Override the auto-inferred `reasoning` capability. When omitted,
109
131
  * the adapter checks the model name against a known-prefix list (see
110
132
  * `known-reasoning-models.ts`) — `true` for the o-series (`o1*`,
111
- * `o3*`, `o4*`) and the `gpt-5*` family, `false` otherwise. Setting
133
+ * `o3*`, `o4*`) and the `gpt-5*` / `gpt-6*` families, `false` otherwise. Setting
112
134
  * this explicitly always wins over inference — useful for fine-tuned
113
135
  * reasoning models or gateways exposing reasoning under a custom
114
136
  * name. When `false`, `ModelCallOptions.reasoning` is ignored rather
115
137
  * than forwarded as an unsupported `reasoning_effort` param.
116
138
  */
117
139
  reasoning?: boolean;
140
+ /**
141
+ * Stable request bucket used by OpenAI to improve prompt-cache matching.
142
+ * Sent only by models whose SDK provider label is `"openai"`; compatible
143
+ * wrapper endpoints deliberately never receive this OpenAI-only field.
144
+ */
145
+ promptCacheKey?: string;
146
+ /**
147
+ * OpenAI's maximum prompt-cache retention policy. `"24h"` enables
148
+ * extended retention; `"in_memory"` keeps the cache within memory.
149
+ * Sent only by models whose SDK provider label is `"openai"`.
150
+ */
151
+ promptCacheRetention?: Exclude<OpenAI.Chat.Completions.ChatCompletionCreateParams["prompt_cache_retention"], null>;
118
152
  /**
119
153
  * Opt into PDF / document **input**. Default `false` — OpenAI accepts
120
154
  * PDF `file` content parts only on specific models (the `gpt-4o`
@@ -1 +1 @@
1
- {"version":3,"file":"config.type.d.mts","names":[],"sources":["../../../../../../ai-openai/src/config.type.ts"],"mappings":";;;;;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AAA;;;;;;EAMA,oBAAA,GAAuB,OAAA,CACrB,MAAA,CAAO,IAAA,CAAK,WAAA,CAAY,0BAAA;;;;;;;;;EAW1B,GAAA;;;;;;;;EAQA,KAAA;AAAA;;;;;;;;;;;KAaU,oBAAA,GAAuB,cAAc;;;;;;;;;;;;;KAcrC,iBAAA,GAAoB,gBAAgB;;;;;;;;;;;;KAapC,kBAAA,GAAqB,iBAAiB;;;;;;;;;;;;KAatC,yBAAA,GAA4B,wBAAwB"}
@@ -17,11 +17,12 @@ const REASONING_CAPABLE_PREFIXES = [
17
17
  "o1",
18
18
  "o3",
19
19
  "o4",
20
- "gpt-5"
20
+ "gpt-5",
21
+ "gpt-6"
21
22
  ];
22
23
  /**
23
24
  * Infer whether a given OpenAI model name is a reasoning model (o-series
24
- * and the gpt-5 family) based on the known-prefix list. Unknown models
25
+ * and the gpt-5 / gpt-6 families) based on the known-prefix list. Unknown models
25
26
  * default to `false` so the adapter never forwards an unsupported
26
27
  * `reasoning_effort` param to a non-reasoning model (which would 400).
27
28
  *
@@ -29,6 +30,7 @@ const REASONING_CAPABLE_PREFIXES = [
29
30
  * inferReasoningCapability("o3-mini"); // → true
30
31
  * inferReasoningCapability("o4-mini"); // → true
31
32
  * inferReasoningCapability("gpt-5-pro"); // → true
33
+ * inferReasoningCapability("gpt-6-astra"); // → true
32
34
  * inferReasoningCapability("gpt-4o"); // → false
33
35
  * inferReasoningCapability("custom-llm"); // → false
34
36
  */
@@ -1 +1 @@
1
- {"version":3,"file":"known-reasoning-models.mjs","names":[],"sources":["../../../../../../ai-openai/src/known-reasoning-models.ts"],"sourcesContent":["/**\n * Model-name prefixes for OpenAI families that expose internal\n * reasoning / thinking tokens and accept the `reasoning_effort`\n * request parameter on the Chat Completions API.\n *\n * Matched as a prefix so dated variants (`o3-2025-04-16`) and\n * `-mini` / `-pro` suffixes (`o4-mini`, `gpt-5-pro`) are covered\n * without listing every release tag explicitly.\n *\n * Maintenance: append a new prefix when OpenAI ships a reasoning\n * model family that doesn't already match. Devs can always override\n * per-model via `openai.model({ name, reasoning: true | false })` —\n * explicit config wins over inference in either direction.\n */\nconst REASONING_CAPABLE_PREFIXES = [\"o1\", \"o3\", \"o4\", \"gpt-5\"];\n\n/**\n * Infer whether a given OpenAI model name is a reasoning model (o-series\n * and the gpt-5 family) based on the known-prefix list. Unknown models\n * default to `false` so the adapter never forwards an unsupported\n * `reasoning_effort` param to a non-reasoning model (which would 400).\n *\n * @example\n * inferReasoningCapability(\"o3-mini\"); // → true\n * inferReasoningCapability(\"o4-mini\"); // → true\n * inferReasoningCapability(\"gpt-5-pro\"); // → true\n * inferReasoningCapability(\"gpt-4o\"); // → false\n * inferReasoningCapability(\"custom-llm\"); // → false\n */\nexport function inferReasoningCapability(modelName: string): boolean {\n const normalized = modelName.toLowerCase();\n\n return REASONING_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,MAAM,6BAA6B;CAAC;CAAM;CAAM;CAAM;AAAO;;;;;;;;;;;;;;AAe7D,SAAgB,yBAAyB,WAA4B;CACnE,MAAM,aAAa,UAAU,YAAY;CAEzC,OAAO,2BAA2B,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAClF"}
1
+ {"version":3,"file":"known-reasoning-models.mjs","names":[],"sources":["../../../../../../ai-openai/src/known-reasoning-models.ts"],"sourcesContent":["/**\n * Model-name prefixes for OpenAI families that expose internal\n * reasoning / thinking tokens and accept the `reasoning_effort`\n * request parameter on the Chat Completions API.\n *\n * Matched as a prefix so dated variants (`o3-2025-04-16`) and\n * `-mini` / `-pro` suffixes (`o4-mini`, `gpt-5-pro`) are covered\n * without listing every release tag explicitly.\n *\n * Maintenance: append a new prefix when OpenAI ships a reasoning\n * model family that doesn't already match. Devs can always override\n * per-model via `openai.model({ name, reasoning: true | false })` —\n * explicit config wins over inference in either direction.\n */\nconst REASONING_CAPABLE_PREFIXES = [\"o1\", \"o3\", \"o4\", \"gpt-5\", \"gpt-6\"];\n\n/**\n * Infer whether a given OpenAI model name is a reasoning model (o-series\n * and the gpt-5 / gpt-6 families) based on the known-prefix list. Unknown models\n * default to `false` so the adapter never forwards an unsupported\n * `reasoning_effort` param to a non-reasoning model (which would 400).\n *\n * @example\n * inferReasoningCapability(\"o3-mini\"); // → true\n * inferReasoningCapability(\"o4-mini\"); // → true\n * inferReasoningCapability(\"gpt-5-pro\"); // → true\n * inferReasoningCapability(\"gpt-6-astra\"); // → true\n * inferReasoningCapability(\"gpt-4o\"); // → false\n * inferReasoningCapability(\"custom-llm\"); // → false\n */\nexport function inferReasoningCapability(modelName: string): boolean {\n const normalized = modelName.toLowerCase();\n\n return REASONING_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAcA,MAAM,6BAA6B;CAAC;CAAM;CAAM;CAAM;CAAS;AAAO;;;;;;;;;;;;;;;AAgBtE,SAAgB,yBAAyB,WAA4B;CACnE,MAAM,aAAa,UAAU,YAAY;CAEzC,OAAO,2BAA2B,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAClF"}
@@ -16,8 +16,11 @@ const VISION_CAPABLE_PREFIXES = [
16
16
  "gpt-4o",
17
17
  "gpt-4-turbo",
18
18
  "gpt-4.1",
19
+ "gpt-5",
20
+ "gpt-6",
19
21
  "o1",
20
22
  "o3",
23
+ "o4",
21
24
  "chatgpt-4o"
22
25
  ];
23
26
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"known-vision-models.mjs","names":[],"sources":["../../../../../../ai-openai/src/known-vision-models.ts"],"sourcesContent":["/**\n * Model-name prefixes for OpenAI families that support vision input\n * (image attachments) on the Chat Completions API.\n *\n * Matched as a prefix so dated variants (`gpt-4o-2024-08-06`) and\n * `-mini` / `-preview` suffixes (`gpt-4o-mini`, `gpt-4-turbo-preview`)\n * are covered without listing every release tag explicitly.\n *\n * Maintenance: append a new prefix when OpenAI ships a vision-capable\n * model family that doesn't already match. Devs can always override\n * per-model via `openai.model({ name, vision: true | false })` —\n * explicit config wins over inference in either direction.\n */\nconst VISION_CAPABLE_PREFIXES = [\n \"gpt-4o\",\n \"gpt-4-turbo\",\n \"gpt-4.1\",\n \"o1\",\n \"o3\",\n \"chatgpt-4o\",\n];\n\n/**\n * Infer whether a given OpenAI model name supports vision based on the\n * known-prefix list. Unknown models default to `false` so that passing\n * an image attachment to an unsupported model surfaces a clear,\n * agent-side capability error instead of an opaque OpenAI 400.\n *\n * @example\n * inferVisionCapability(\"gpt-4o-mini\"); // → true\n * inferVisionCapability(\"gpt-4o-2024-08-06\"); // → true\n * inferVisionCapability(\"gpt-3.5-turbo\"); // → false\n * inferVisionCapability(\"custom-llm\"); // → false\n */\nexport function inferVisionCapability(modelName: string): boolean {\n const normalized = modelName.toLowerCase();\n\n return VISION_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;;;;AAcA,SAAgB,sBAAsB,WAA4B;CAChE,MAAM,aAAa,UAAU,YAAY;CAEzC,OAAO,wBAAwB,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAC/E"}
1
+ {"version":3,"file":"known-vision-models.mjs","names":[],"sources":["../../../../../../ai-openai/src/known-vision-models.ts"],"sourcesContent":["/**\n * Model-name prefixes for OpenAI families that support vision input\n * (image attachments) on the Chat Completions API.\n *\n * Matched as a prefix so dated variants (`gpt-4o-2024-08-06`) and\n * `-mini` / `-preview` suffixes (`gpt-4o-mini`, `gpt-4-turbo-preview`)\n * are covered without listing every release tag explicitly.\n *\n * Maintenance: append a new prefix when OpenAI ships a vision-capable\n * model family that doesn't already match. Devs can always override\n * per-model via `openai.model({ name, vision: true | false })` —\n * explicit config wins over inference in either direction.\n */\nconst VISION_CAPABLE_PREFIXES = [\n \"gpt-4o\",\n \"gpt-4-turbo\",\n \"gpt-4.1\",\n \"gpt-5\",\n \"gpt-6\",\n \"o1\",\n \"o3\",\n \"o4\",\n \"chatgpt-4o\",\n];\n\n/**\n * Infer whether a given OpenAI model name supports vision based on the\n * known-prefix list. Unknown models default to `false` so that passing\n * an image attachment to an unsupported model surfaces a clear,\n * agent-side capability error instead of an opaque OpenAI 400.\n *\n * @example\n * inferVisionCapability(\"gpt-4o-mini\"); // → true\n * inferVisionCapability(\"gpt-4o-2024-08-06\"); // → true\n * inferVisionCapability(\"gpt-3.5-turbo\"); // → false\n * inferVisionCapability(\"custom-llm\"); // → false\n */\nexport function inferVisionCapability(modelName: string): boolean {\n const normalized = modelName.toLowerCase();\n\n return VISION_CAPABLE_PREFIXES.some((prefix) => normalized.startsWith(prefix));\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,MAAM,0BAA0B;CAC9B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF;;;;;;;;;;;;;AAcA,SAAgB,sBAAsB,WAA4B;CAChE,MAAM,aAAa,UAAU,YAAY;CAEzC,OAAO,wBAAwB,MAAM,WAAW,WAAW,WAAW,MAAM,CAAC;AAC/E"}
package/esm/model.mjs CHANGED
@@ -1,29 +1,20 @@
1
+ import { buildUsage } from "./utils/build-usage.mjs";
2
+ import { wrapOpenAIError } from "./utils/wrap-openai-error.mjs";
1
3
  import { mapFinishReason } from "./utils/map-finish-reason.mjs";
4
+ import { buildPromptCacheParams } from "./utils/prompt-cache-params.mjs";
5
+ import { toOpenAIReasoningEffort } from "./utils/reasoning-effort.mjs";
6
+ import { inferStructuredOutput, planStructuredOutput } from "./utils/structured-output.mjs";
2
7
  import { toOpenAIMessages } from "./utils/to-openai-messages.mjs";
3
8
  import { toOpenAITools } from "./utils/to-openai-tools.mjs";
4
- import { wrapOpenAIError } from "./utils/wrap-openai-error.mjs";
5
9
  import "./utils/index.mjs";
6
10
  import { inferReasoningCapability } from "./known-reasoning-models.mjs";
7
11
  import { inferVisionCapability } from "./known-vision-models.mjs";
8
- import { ProviderError, safeJsonParse } from "@warlock.js/ai";
12
+ import { InvalidRequestError, ProviderError, safeJsonParse } from "@warlock.js/ai";
9
13
  import { log } from "@warlock.js/logger";
10
14
 
11
15
  //#region ../ai-openai/src/model.ts
12
16
  const LOG_MODULE = "ai.openai";
13
17
  /**
14
- * Map an explicit `responseFormat` override to the default
15
- * `structuredOutput` capability. Loose wire modes (`"json_object"`,
16
- * `"text"`) don't enforce shape, so the agent needs to see the soft
17
- * schema hint in the system prompt — that only happens when the
18
- * capability is `false`. Default (no override) stays `true` to
19
- * preserve the prior assumption that OpenAI models support strict
20
- * structured output.
21
- */
22
- function inferStructuredOutput(responseFormat) {
23
- if (responseFormat === "json_object" || responseFormat === "text") return false;
24
- return true;
25
- }
26
- /**
27
18
  * OpenAI-backed implementation of `ModelContract`.
28
19
  *
29
20
  * **Role.** The provider-facing bridge between the vendor-neutral
@@ -63,6 +54,11 @@ function inferStructuredOutput(responseFormat) {
63
54
  var OpenAIModel = class {
64
55
  constructor(client, config, provider = "openai") {
65
56
  this.logger = log;
57
+ if (config.api === "responses") throw new InvalidRequestError("OpenAIModel speaks Chat Completions only and cannot serve api: \"responses\". Use OpenAISDK.model({ api: \"responses\" }) on the direct OpenAI provider.", { context: {
58
+ provider,
59
+ model: config.name,
60
+ api: config.api
61
+ } });
66
62
  this.client = client;
67
63
  this.config = config;
68
64
  this.name = config.name;
@@ -72,7 +68,7 @@ var OpenAIModel = class {
72
68
  structuredOutput: config.structuredOutput ?? inferStructuredOutput(config.responseFormat),
73
69
  vision: config.vision ?? inferVisionCapability(config.name),
74
70
  reasoning: config.reasoning ?? inferReasoningCapability(config.name),
75
- promptCaching: true,
71
+ promptCaching: this.provider === "openai",
76
72
  pdf: config.pdf ?? false,
77
73
  audio: config.audio ?? false
78
74
  };
@@ -94,9 +90,9 @@ var OpenAIModel = class {
94
90
  try {
95
91
  response = await this.client.chat.completions.create({
96
92
  model: this.name,
97
- messages: toOpenAIMessages(messages),
98
- temperature: options?.temperature ?? this.config.temperature,
99
- max_tokens: options?.maxTokens ?? this.config.maxTokens,
93
+ messages: toOpenAIMessages(messages, this.promptCacheBreakpoints(options)),
94
+ ...this.buildSamplingParams(options),
95
+ ...buildPromptCacheParams(this.provider, this.config),
100
96
  tools: toOpenAITools(options?.tools),
101
97
  ...this.buildResponseFormat(options?.responseSchema),
102
98
  ...this.buildReasoningParams(options?.reasoning, Boolean(options?.tools?.length))
@@ -145,9 +141,9 @@ var OpenAIModel = class {
145
141
  try {
146
142
  stream = await this.client.chat.completions.create({
147
143
  model: this.name,
148
- messages: toOpenAIMessages(messages),
149
- temperature: options?.temperature ?? this.config.temperature,
150
- max_tokens: options?.maxTokens ?? this.config.maxTokens,
144
+ messages: toOpenAIMessages(messages, this.promptCacheBreakpoints(options)),
145
+ ...this.buildSamplingParams(options),
146
+ ...buildPromptCacheParams(this.provider, this.config),
151
147
  tools: toOpenAITools(options?.tools),
152
148
  stream: true,
153
149
  stream_options: { include_usage: true },
@@ -228,89 +224,31 @@ var OpenAIModel = class {
228
224
  usage
229
225
  };
230
226
  }
227
+ /** Map the neutral cache hint only for the direct OpenAI provider. */
228
+ promptCacheBreakpoints(options) {
229
+ return this.provider === "openai" ? options?.cacheControl?.breakpoints ?? 0 : 0;
230
+ }
231
231
  /**
232
232
  * Translate the neutral `responseSchema` option into OpenAI's
233
- * `response_format` parameter.
234
- *
235
- * When `config.responseFormat` is set, it wins: `"text"` emits no
236
- * `response_format` at all, `"json_object"` always picks the loose
237
- * mode, and `"json_schema"` picks strict mode (with the same
238
- * `isStrictCompatible` safety check — a malformed schema still
239
- * degrades to `json_object` rather than 400). The override exists
240
- * because some targets (older OpenAI models, OpenRouter routes,
241
- * Ollama OpenAI-compat) reject strict `json_schema` outright.
242
- *
243
- * When the override is omitted, uses strict `json_schema` mode
244
- * (token-level enforcement) only when the schema is a proper
245
- * root-object JSON Schema (`{ type: "object", properties: ... }`).
246
- * For anything else — malformed extractor output, non-object
247
- * schemas, or future shapes we haven't tested — falls back to loose
248
- * `json_object` mode, which guarantees *some* valid JSON without
249
- * enforcing shape. The agent's soft instruction already embeds the
250
- * schema text in the system prompt when the model declares no
251
- * native structured-output capability, so shape validation still
252
- * runs client-side via the Standard Schema `validate()` call.
233
+ * `response_format` parameter. The mode decision (override handling,
234
+ * strict-compatibility downgrade to `json_object`) lives in
235
+ * `planStructuredOutput`, shared with the Responses adapter.
253
236
  *
254
237
  * Returns an empty spread when no schema was supplied, so the caller
255
238
  * can unconditionally `...buildResponseFormat(...)` into the request.
256
239
  */
257
240
  buildResponseFormat(responseSchema) {
258
- if (!responseSchema) return {};
259
- const override = this.config.responseFormat;
260
- if (override === "text") return {};
261
- if (override === "json_object") return { response_format: { type: "json_object" } };
262
- if (this.isStrictCompatible(responseSchema)) return { response_format: {
241
+ const plan = planStructuredOutput(responseSchema, this.config.responseFormat);
242
+ if (plan.mode === "json_schema") return { response_format: {
263
243
  type: "json_schema",
264
244
  json_schema: {
265
245
  name: "response",
266
- schema: responseSchema,
246
+ schema: plan.schema,
267
247
  strict: true
268
248
  }
269
249
  } };
270
- return { response_format: { type: "json_object" } };
271
- }
272
- /**
273
- * OpenAI strict `json_schema` mode requires the root to be a JSON
274
- * Schema object type (`{ type: "object", properties: ... }`). Anything
275
- * else (top-level arrays, primitives, unknown shapes) is rejected with
276
- * a 400 before a token is sampled. We check structurally here so the
277
- * first call doesn't crash on a malformed extraction — loose
278
- * `json_object` mode is a safe degradation.
279
- */
280
- isStrictCompatible(schema) {
281
- return schema.type === "object" && typeof schema.properties === "object" && schema.properties !== null && this.isStrictSafeNode(schema);
282
- }
283
- /**
284
- * Recursively check the one strict-mode rule schemas most often trip on:
285
- * every object must list ALL of its `properties` in `required` (OpenAI
286
- * strict has no notion of optional — optional fields must be expressed
287
- * as nullable, e.g. `type: ["string", "null"]`, and still appear in
288
- * `required`). A schema that violates this anywhere in the tree is NOT
289
- * sent in strict `json_schema` mode — it degrades to loose
290
- * `json_object` so a hand-built or optional-bearing schema can't 400
291
- * the call ("'required' ... must include every key in properties").
292
- * Client-side `validate()` still enforces the full shape.
293
- */
294
- isStrictSafeNode(node) {
295
- if (!node || typeof node !== "object") return true;
296
- const record = node;
297
- if (record.type === "object" && record.properties && typeof record.properties === "object") {
298
- const properties = record.properties;
299
- const keys = Object.keys(properties);
300
- const required = Array.isArray(record.required) ? record.required : [];
301
- if (keys.some((key) => !required.includes(key))) return false;
302
- for (const key of keys) if (!this.isStrictSafeNode(properties[key])) return false;
303
- }
304
- if (record.items !== void 0 && !this.isStrictSafeNode(record.items)) return false;
305
- for (const branch of [
306
- "anyOf",
307
- "allOf",
308
- "oneOf"
309
- ]) {
310
- const value = record[branch];
311
- if (Array.isArray(value) && value.some((sub) => !this.isStrictSafeNode(sub))) return false;
312
- }
313
- return true;
250
+ if (plan.mode === "json_object") return { response_format: { type: "json_object" } };
251
+ return {};
314
252
  }
315
253
  /**
316
254
  * Normalize OpenAI's `usage` block (which may be absent on some responses
@@ -332,15 +270,13 @@ var OpenAIModel = class {
332
270
  output: 0,
333
271
  total: 0
334
272
  };
335
- const cachedTokens = raw.prompt_tokens_details?.cached_tokens;
336
- const reasoningTokens = raw.completion_tokens_details?.reasoning_tokens;
337
- return {
273
+ return buildUsage({
338
274
  input: raw.prompt_tokens,
339
275
  output: raw.completion_tokens,
340
276
  total: raw.total_tokens,
341
- ...cachedTokens !== void 0 && cachedTokens > 0 ? { cachedTokens } : {},
342
- ...reasoningTokens !== void 0 && reasoningTokens > 0 ? { reasoningTokens } : {}
343
- };
277
+ cachedTokens: raw.prompt_tokens_details?.cached_tokens,
278
+ reasoningTokens: raw.completion_tokens_details?.reasoning_tokens
279
+ });
344
280
  }
345
281
  /**
346
282
  * Translate the neutral `ModelCallOptions.reasoning` hint into OpenAI's
@@ -377,10 +313,27 @@ var OpenAIModel = class {
377
313
  * Returns an empty spread when nothing applies, so the caller can
378
314
  * unconditionally `...buildReasoningParams(...)` into the request.
379
315
  */
316
+ /**
317
+ * Output-length and sampling params, in current Chat Completions shapes only.
318
+ *
319
+ * - `max_completion_tokens`, never the legacy `max_tokens`: current
320
+ * models (the gpt-5 family and later) answer `400 unsupported_parameter`
321
+ * to `max_tokens`, and `max_completion_tokens` is accepted everywhere.
322
+ * - No `temperature` for a reasoning-capable model: those accept only the
323
+ * default value and reject any other with a 400.
324
+ */
325
+ buildSamplingParams(options) {
326
+ const maxTokens = options?.maxTokens ?? this.config.maxTokens;
327
+ const temperature = options?.temperature ?? this.config.temperature;
328
+ return {
329
+ max_completion_tokens: maxTokens,
330
+ ...!this.capabilities.reasoning && { temperature }
331
+ };
332
+ }
380
333
  buildReasoningParams(reasoning, hasTools) {
381
334
  if (!this.capabilities.reasoning) return {};
382
335
  if (!reasoning?.effort) return hasTools ? { reasoning_effort: "none" } : {};
383
- return { reasoning_effort: reasoning.effort };
336
+ return { reasoning_effort: toOpenAIReasoningEffort(reasoning.effort) };
384
337
  }
385
338
  /**
386
339
  * Reshape OpenAI's `tool_calls` array into the neutral
package/esm/model.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"model.mjs","names":[],"sources":["../../../../../../ai-openai/src/model.ts"],"sourcesContent":["import {\n ProviderError,\n safeJsonParse,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type OpenAI from \"openai\";\nimport type { OpenAIModelConfig, OpenAIResponseFormat } from \"./config.type\";\nimport { inferReasoningCapability } from \"./known-reasoning-models\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport { mapFinishReason, toOpenAIMessages, toOpenAITools, wrapOpenAIError } from \"./utils\";\n\nconst LOG_MODULE = \"ai.openai\";\n\n/**\n * Map an explicit `responseFormat` override to the default\n * `structuredOutput` capability. Loose wire modes (`\"json_object\"`,\n * `\"text\"`) don't enforce shape, so the agent needs to see the soft\n * schema hint in the system prompt — that only happens when the\n * capability is `false`. Default (no override) stays `true` to\n * preserve the prior assumption that OpenAI models support strict\n * structured output.\n */\nfunction inferStructuredOutput(responseFormat: OpenAIResponseFormat | undefined): boolean {\n if (responseFormat === \"json_object\" || responseFormat === \"text\") {\n return false;\n }\n\n return true;\n}\n\n/**\n * OpenAI-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the official `openai` SDK. Agents,\n * workflows, and supervisors never talk to OpenAI directly — they hold a\n * `ModelContract`, and this class is what makes that contract concrete for\n * any OpenAI-compatible endpoint (OpenAI, Azure OpenAI, OpenRouter, local\n * gateways that speak the Chat Completions protocol).\n *\n * **Responsibility.**\n * - Owns: a long-lived `OpenAI` client + frozen `ModelConfig` (name,\n * temperature, maxTokens) used as defaults for every call.\n * - Owns: translating vendor-neutral `Message[]` and\n * `ToolContract[]` into OpenAI wire shapes on the way out, and\n * translating OpenAI's response (content, finish reason, tool calls,\n * usage) back into the neutral shapes on the way in.\n * - Does NOT own: dispatching tools, deciding whether to loop, tracking\n * conversation history, or retrying on failure — those are agent\n * concerns. The model is a stateless (per-call) protocol adapter.\n *\n * Because it holds a live client and shared defaults, it is modeled as a\n * class (see §4.2 of code-style.md — \"long-lived state across calls\").\n *\n * @example\n * import OpenAI from \"openai\";\n * const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n * const model = new OpenAIModel(client, { name: \"gpt-4o\", temperature: 0.3 });\n *\n * const myAgent = agent({\n * model,\n * systemPrompt: \"You are a helpful assistant.\",\n * tools: [searchTool],\n * });\n *\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class OpenAIModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly client: OpenAI;\n private readonly config: OpenAIModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(client: OpenAI, config: OpenAIModelConfig, provider: string = \"openai\") {\n this.client = client;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n this.capabilities = {\n structuredOutput: config.structuredOutput ?? inferStructuredOutput(config.responseFormat),\n vision: config.vision ?? inferVisionCapability(config.name),\n // o-series + gpt-5 models surface a reasoning channel and accept\n // the `reasoning_effort` param. Explicit config wins over the\n // name-prefix inference.\n reasoning: config.reasoning ?? inferReasoningCapability(config.name),\n // OpenAI prompt caching is automatic on the Chat Completions API\n // (no caller-supplied breakpoints — the platform caches long\n // prompt prefixes server-side and reports the hit count via\n // `prompt_tokens_details.cached_tokens`). We therefore advertise\n // the read-side accounting capability as always available while\n // treating `ModelCallOptions.cacheControl` write breakpoints as a\n // no-op (see `buildReasoningParams` siblings — there is no cache\n // param to emit).\n promptCaching: true,\n // PDF + audio INPUT are off by default — OpenAI accepts `file`\n // (PDF) and `input_audio` parts only on specific models, so the\n // flags are conservative/honest and opt-in via config rather than\n // name-inferred. When set, the agent admits the attachments and\n // `toOpenAIMessages` maps them to the real wire parts.\n pdf: config.pdf ?? false,\n audio: config.audio ?? false,\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to the Chat\n * Completions endpoint, waits for the terminal response, and reshapes it\n * into a vendor-neutral `ModelResponse`. Per-call `options` override the\n * instance's `ModelConfig` defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n // Per-call request/response logs are hot-path in production agents\n // — keep them at `debug` so `info` stays reserved for lifecycle\n // events (agent starting/completed, etc.). Operators who need to\n // audit every LLM call can raise log-level at runtime.\n this.logger.debug(LOG_MODULE, \"request\", \"Starting call to chat.completions\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let response: OpenAI.Chat.Completions.ChatCompletion;\n\n try {\n response = await this.client.chat.completions.create(\n {\n model: this.name,\n messages: toOpenAIMessages(messages),\n temperature: options?.temperature ?? this.config.temperature,\n max_tokens: options?.maxTokens ?? this.config.maxTokens,\n tools: toOpenAITools(options?.tools),\n ...this.buildResponseFormat(options?.responseSchema),\n ...this.buildReasoningParams(options?.reasoning, Boolean(options?.tools?.length)),\n },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n const wrapped = wrapOpenAIError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const [choice] = response.choices;\n\n if (choice === undefined) {\n // A 200 carrying an empty `choices` array. Every line below reads off\n // `choice`, so without this the caller got a bare `TypeError: Cannot\n // read properties of undefined` — no provider, no model, no hint that\n // the response itself was the problem rather than our handling of it.\n //\n // `ProviderError` because that is what this is: the provider answered\n // successfully and returned nothing usable. Callers already branch on\n // `instanceof ProviderError` for any provider-side failure.\n throw new ProviderError(\"OpenAI returned a response with no choices.\", {\n context: { provider: \"openai\", model: this.name, choices: response.choices.length },\n });\n }\n\n const finishReason = mapFinishReason(choice.finish_reason);\n const usage = this.extractUsage(response.usage);\n\n this.logger.debug(LOG_MODULE, \"response\", \"call to chat.completions succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n content: choice.message.content ?? \"\",\n finishReason,\n usage,\n toolCalls: this.extractToolCalls(choice.message.tool_calls),\n };\n }\n\n /**\n * Incremental streaming completion. Yields neutral `ModelStreamChunk`s —\n * `delta` for text tokens, `tool-call` when the model requests a tool,\n * and a terminal `done` carrying the final finish reason + usage totals.\n * Callers consume it with `for await`.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting streaming call to chat.completions\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let stream: Awaited<ReturnType<typeof this.client.chat.completions.create>>;\n\n try {\n stream = await this.client.chat.completions.create(\n {\n model: this.name,\n messages: toOpenAIMessages(messages),\n temperature: options?.temperature ?? this.config.temperature,\n max_tokens: options?.maxTokens ?? this.config.maxTokens,\n tools: toOpenAITools(options?.tools),\n stream: true,\n stream_options: { include_usage: true },\n ...this.buildResponseFormat(options?.responseSchema),\n ...this.buildReasoningParams(options?.reasoning, Boolean(options?.tools?.length)),\n },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n const wrapped = wrapOpenAIError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n let rawFinishReason: string = \"stop\";\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const toolCallAccum = new Map<number, { id: string; name: string; arguments: string }>();\n\n try {\n for await (const chunk of stream as AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>) {\n const delta = chunk.choices[0]?.delta;\n const finish = chunk.choices[0]?.finish_reason;\n\n if (delta?.content) {\n yield { type: \"delta\", content: delta.content };\n }\n\n if (delta?.tool_calls) {\n for (const toolCall of delta.tool_calls) {\n const idx = toolCall.index ?? 0;\n if (!toolCallAccum.has(idx)) {\n toolCallAccum.set(idx, { id: \"\", name: \"\", arguments: \"\" });\n }\n const acc = toolCallAccum.get(idx)!;\n if (toolCall.id) acc.id = toolCall.id;\n if (toolCall.function?.name) acc.name = toolCall.function.name;\n if (toolCall.function?.arguments) acc.arguments += toolCall.function.arguments;\n }\n }\n\n if (finish) {\n rawFinishReason = finish;\n }\n\n if (chunk.usage) {\n usage.input = chunk.usage.prompt_tokens ?? 0;\n usage.output = chunk.usage.completion_tokens ?? 0;\n usage.total = chunk.usage.total_tokens ?? 0;\n const cached = chunk.usage.prompt_tokens_details?.cached_tokens;\n if (cached !== undefined && cached > 0) {\n usage.cachedTokens = cached;\n }\n const reasoning = chunk.usage.completion_tokens_details?.reasoning_tokens;\n if (reasoning !== undefined && reasoning > 0) {\n usage.reasoningTokens = reasoning;\n }\n }\n }\n\n for (const acc of toolCallAccum.values()) {\n // Skip accumulators that never received a function name — those\n // are partial fragments the model started but never identified\n // (e.g. arguments-only deltas with no originating `id`/`name`).\n // Yielding them produces nameless tool-calls the agent runtime\n // can't dispatch and would mis-attribute as a registered tool.\n if (!acc.name) continue;\n\n yield {\n type: \"tool-call\",\n id: acc.id,\n name: acc.name,\n input: safeJsonParse<Record<string, unknown>>(acc.arguments, {}),\n };\n }\n } catch (thrown) {\n const wrapped = wrapOpenAIError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const finishReason = mapFinishReason(rawFinishReason);\n\n this.logger.debug(LOG_MODULE, \"response\", \"Streaming call to chat.completions succeeded\", {\n finishReason,\n usage,\n });\n\n yield { type: \"done\", finishReason, usage };\n }\n\n /**\n * Translate the neutral `responseSchema` option into OpenAI's\n * `response_format` parameter.\n *\n * When `config.responseFormat` is set, it wins: `\"text\"` emits no\n * `response_format` at all, `\"json_object\"` always picks the loose\n * mode, and `\"json_schema\"` picks strict mode (with the same\n * `isStrictCompatible` safety check — a malformed schema still\n * degrades to `json_object` rather than 400). The override exists\n * because some targets (older OpenAI models, OpenRouter routes,\n * Ollama OpenAI-compat) reject strict `json_schema` outright.\n *\n * When the override is omitted, uses strict `json_schema` mode\n * (token-level enforcement) only when the schema is a proper\n * root-object JSON Schema (`{ type: \"object\", properties: ... }`).\n * For anything else — malformed extractor output, non-object\n * schemas, or future shapes we haven't tested — falls back to loose\n * `json_object` mode, which guarantees *some* valid JSON without\n * enforcing shape. The agent's soft instruction already embeds the\n * schema text in the system prompt when the model declares no\n * native structured-output capability, so shape validation still\n * runs client-side via the Standard Schema `validate()` call.\n *\n * Returns an empty spread when no schema was supplied, so the caller\n * can unconditionally `...buildResponseFormat(...)` into the request.\n */\n private buildResponseFormat(responseSchema: Record<string, unknown> | undefined): {\n response_format?: OpenAI.Chat.Completions.ChatCompletionCreateParams[\"response_format\"];\n } {\n if (!responseSchema) {\n return {};\n }\n\n const override = this.config.responseFormat;\n\n if (override === \"text\") {\n return {};\n }\n\n if (override === \"json_object\") {\n return { response_format: { type: \"json_object\" } };\n }\n\n // Either auto-select (no override) or explicit `\"json_schema\"`.\n // The strict-compat check still applies in the explicit case —\n // a malformed / non-object schema would 400 before sampling, so\n // we degrade to `json_object` rather than crash.\n if (this.isStrictCompatible(responseSchema)) {\n return {\n response_format: {\n type: \"json_schema\",\n json_schema: {\n name: \"response\",\n schema: responseSchema,\n strict: true,\n },\n },\n };\n }\n\n return { response_format: { type: \"json_object\" } };\n }\n\n /**\n * OpenAI strict `json_schema` mode requires the root to be a JSON\n * Schema object type (`{ type: \"object\", properties: ... }`). Anything\n * else (top-level arrays, primitives, unknown shapes) is rejected with\n * a 400 before a token is sampled. We check structurally here so the\n * first call doesn't crash on a malformed extraction — loose\n * `json_object` mode is a safe degradation.\n */\n private isStrictCompatible(schema: Record<string, unknown>): boolean {\n return (\n schema.type === \"object\" &&\n typeof schema.properties === \"object\" &&\n schema.properties !== null &&\n this.isStrictSafeNode(schema)\n );\n }\n\n /**\n * Recursively check the one strict-mode rule schemas most often trip on:\n * every object must list ALL of its `properties` in `required` (OpenAI\n * strict has no notion of optional — optional fields must be expressed\n * as nullable, e.g. `type: [\"string\", \"null\"]`, and still appear in\n * `required`). A schema that violates this anywhere in the tree is NOT\n * sent in strict `json_schema` mode — it degrades to loose\n * `json_object` so a hand-built or optional-bearing schema can't 400\n * the call (\"'required' ... must include every key in properties\").\n * Client-side `validate()` still enforces the full shape.\n */\n private isStrictSafeNode(node: unknown): boolean {\n if (!node || typeof node !== \"object\") {\n return true;\n }\n\n const record = node as Record<string, unknown>;\n\n if (record.type === \"object\" && record.properties && typeof record.properties === \"object\") {\n const properties = record.properties as Record<string, unknown>;\n const keys = Object.keys(properties);\n const required = Array.isArray(record.required) ? (record.required as unknown[]) : [];\n\n if (keys.some((key) => !required.includes(key))) {\n return false;\n }\n\n for (const key of keys) {\n if (!this.isStrictSafeNode(properties[key])) {\n return false;\n }\n }\n }\n\n if (record.items !== undefined && !this.isStrictSafeNode(record.items)) {\n return false;\n }\n\n for (const branch of [\"anyOf\", \"allOf\", \"oneOf\"] as const) {\n const value = record[branch];\n if (Array.isArray(value) && value.some((sub) => !this.isStrictSafeNode(sub))) {\n return false;\n }\n }\n\n return true;\n }\n\n /**\n * Normalize OpenAI's `usage` block (which may be absent on some responses\n * or partials) into the neutral `Usage` shape. Missing usage collapses to\n * zeros rather than propagating `undefined`, so downstream aggregation\n * math stays safe.\n *\n * `cachedTokens` mirrors `prompt_tokens_details.cached_tokens` (the\n * subset of the prompt served from OpenAI's automatic prompt cache);\n * `reasoningTokens` mirrors `completion_tokens_details.reasoning_tokens`\n * (the hidden reasoning channel on o-series / gpt-5 models, already\n * counted within `output`). Both are emitted only when the provider\n * reports a positive value, so non-reasoning / uncached calls keep the\n * lean `{ input, output, total }` shape.\n */\n private extractUsage(raw: OpenAI.Completions.CompletionUsage | undefined): Usage {\n if (!raw) {\n return { input: 0, output: 0, total: 0 };\n }\n\n const cachedTokens = raw.prompt_tokens_details?.cached_tokens;\n const reasoningTokens = raw.completion_tokens_details?.reasoning_tokens;\n\n return {\n input: raw.prompt_tokens,\n output: raw.completion_tokens,\n total: raw.total_tokens,\n ...(cachedTokens !== undefined && cachedTokens > 0 ? { cachedTokens } : {}),\n ...(reasoningTokens !== undefined && reasoningTokens > 0 ? { reasoningTokens } : {}),\n };\n }\n\n /**\n * Translate the neutral `ModelCallOptions.reasoning` hint into OpenAI's\n * `reasoning_effort` request param. Only `effort` maps — OpenAI's Chat\n * Completions API exposes a discrete effort knob, not a token budget,\n * so `reasoning.maxTokens` (the Anthropic extended-thinking cap) has no\n * wire equivalent here and is silently ignored.\n *\n * The neutral `ReasoningEffort` (`\"low\" | \"medium\" | \"high\" | \"none\"`)\n * is a subset of OpenAI's accepted values, so it forwards verbatim —\n * `\"none\"` included. `\"none\"` is load-bearing: gpt-5 / o-series models\n * **reject function tools** on Chat Completions while reasoning is\n * active, and the endpoint accepts tools only when `reasoning_effort`\n * is `\"none\"` (the alternative is the Responses API). This is why the\n * param is EMITTED for `\"none\"` rather than omitted — omitting it\n * leaves the model reasoning server-side by default, so tools would\n * still be rejected.\n *\n * No-ops when the model is not reasoning-capable\n * (`capabilities.reasoning` is false — e.g. `gpt-4o`, which 400s on any\n * `reasoning_effort`).\n *\n * When the caller gave no explicit `effort`: defaults to `\"none\"` IF\n * `hasTools` is true, else omits the param (provider default — the\n * pre-existing behavior for a tool-less call). The default exists\n * because, on a reasoning-capable model, there is no working alternative\n * to `\"none\"` when tools are attached — omitting `reasoning_effort`\n * leaves reasoning on server-side, which Chat Completions rejects tools\n * for (empty replies on some model generations, a hard 400 — \"Function\n * tools with reasoning_effort are not supported ... in\n * /v1/chat/completions\" — on newer ones). An explicit `effort` from the\n * caller always wins over this default, in either direction.\n *\n * Returns an empty spread when nothing applies, so the caller can\n * unconditionally `...buildReasoningParams(...)` into the request.\n */\n private buildReasoningParams(\n reasoning: ModelCallOptions[\"reasoning\"],\n hasTools: boolean,\n ): {\n reasoning_effort?: OpenAI.Chat.Completions.ChatCompletionCreateParams[\"reasoning_effort\"];\n } {\n if (!this.capabilities.reasoning) {\n return {};\n }\n\n if (!reasoning?.effort) {\n return hasTools ? { reasoning_effort: \"none\" } : {};\n }\n\n return { reasoning_effort: reasoning.effort };\n }\n\n /**\n * Reshape OpenAI's `tool_calls` array into the neutral\n * `ModelToolCallRequest[]`. The raw `arguments` field is a JSON string\n * per OpenAI's protocol — we parse it defensively via `safeJsonParse` so\n * malformed or empty arguments yield an empty object instead of crashing\n * the trip. Returns `undefined` when no tools were requested so callers\n * can branch on presence.\n */\n private extractToolCalls(\n rawToolCalls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[] | undefined,\n ): ModelToolCallRequest[] | undefined {\n if (!rawToolCalls || rawToolCalls.length === 0) {\n return undefined;\n }\n\n return rawToolCalls.map((toolCall) => ({\n id: toolCall.id,\n name: (toolCall as any).function.name,\n input: safeJsonParse<Record<string,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AO,aAAa,KAAK,cAAc;GACrC,IAAI,SAAS;GACb,MAAO,SAAiB,SAAS;GACjC,OAAO,cAAwC,SAAiB,SAAS,WAAW,CAAC,CAAC;EACxF,EAAE;CACJ;AACF"}
1
+ {"version":3,"file":"model.mjs","names":[],"sources":["../../../../../../ai-openai/src/model.ts"],"sourcesContent":["import {\n InvalidRequestError,\n ProviderError,\n safeJsonParse,\n type Message,\n type ModelCallOptions,\n type ModelCapabilities,\n type ModelContract,\n type ModelPricing,\n type ModelResponse,\n type ModelStreamChunk,\n type ModelToolCallRequest,\n type Usage,\n} from \"@warlock.js/ai\";\nimport { log, type Logger } from \"@warlock.js/logger\";\nimport type OpenAI from \"openai\";\nimport type { OpenAIModelConfig } from \"./config.type\";\nimport { inferReasoningCapability } from \"./known-reasoning-models\";\nimport { inferVisionCapability } from \"./known-vision-models\";\nimport {\n buildPromptCacheParams,\n buildUsage,\n inferStructuredOutput,\n mapFinishReason,\n planStructuredOutput,\n toOpenAIMessages,\n toOpenAIReasoningEffort,\n toOpenAITools,\n wrapOpenAIError,\n} from \"./utils\";\n\nconst LOG_MODULE = \"ai.openai\";\n\n/**\n * OpenAI-backed implementation of `ModelContract`.\n *\n * **Role.** The provider-facing bridge between the vendor-neutral\n * `@warlock.js/ai` agent runtime and the official `openai` SDK. Agents,\n * workflows, and supervisors never talk to OpenAI directly — they hold a\n * `ModelContract`, and this class is what makes that contract concrete for\n * any OpenAI-compatible endpoint (OpenAI, Azure OpenAI, OpenRouter, local\n * gateways that speak the Chat Completions protocol).\n *\n * **Responsibility.**\n * - Owns: a long-lived `OpenAI` client + frozen `ModelConfig` (name,\n * temperature, maxTokens) used as defaults for every call.\n * - Owns: translating vendor-neutral `Message[]` and\n * `ToolContract[]` into OpenAI wire shapes on the way out, and\n * translating OpenAI's response (content, finish reason, tool calls,\n * usage) back into the neutral shapes on the way in.\n * - Does NOT own: dispatching tools, deciding whether to loop, tracking\n * conversation history, or retrying on failure — those are agent\n * concerns. The model is a stateless (per-call) protocol adapter.\n *\n * Because it holds a live client and shared defaults, it is modeled as a\n * class (see §4.2 of code-style.md — \"long-lived state across calls\").\n *\n * @example\n * import OpenAI from \"openai\";\n * const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });\n * const model = new OpenAIModel(client, { name: \"gpt-4o\", temperature: 0.3 });\n *\n * const myAgent = agent({\n * model,\n * systemPrompt: \"You are a helpful assistant.\",\n * tools: [searchTool],\n * });\n *\n * const result = await myAgent.execute(\"Summarize today's news.\");\n */\nexport class OpenAIModel implements ModelContract {\n public readonly name: string;\n public readonly provider: string;\n public readonly capabilities: ModelCapabilities;\n public readonly pricing?: ModelPricing;\n\n private readonly client: OpenAI;\n private readonly config: OpenAIModelConfig;\n private readonly logger: Logger = log;\n\n public constructor(client: OpenAI, config: OpenAIModelConfig, provider: string = \"openai\") {\n // `api: \"responses\"` belongs to `OpenAIResponsesModel`, which only the\n // `OpenAISDK.model()` factory constructs. This class speaks Chat\n // Completions only; accepting the flag here would silently ignore it.\n if (config.api === \"responses\") {\n throw new InvalidRequestError(\n 'OpenAIModel speaks Chat Completions only and cannot serve api: \"responses\". Use OpenAISDK.model({ api: \"responses\" }) on the direct OpenAI provider.',\n { context: { provider, model: config.name, api: config.api } },\n );\n }\n\n this.client = client;\n this.config = config;\n this.name = config.name;\n this.provider = provider;\n this.pricing = config.pricing;\n this.capabilities = {\n structuredOutput: config.structuredOutput ?? inferStructuredOutput(config.responseFormat),\n vision: config.vision ?? inferVisionCapability(config.name),\n // o-series + gpt-5 / gpt-6 models surface a reasoning channel and accept\n // the `reasoning_effort` param. Explicit config wins over the\n // name-prefix inference.\n reasoning: config.reasoning ?? inferReasoningCapability(config.name),\n // Direct OpenAI requests support automatic cache hits and explicit\n // content-part breakpoints. Wrapper endpoints have distinct provider\n // labels and may reject these OpenAI-only request fields.\n promptCaching: this.provider === \"openai\",\n // PDF + audio INPUT are off by default — OpenAI accepts `file`\n // (PDF) and `input_audio` parts only on specific models, so the\n // flags are conservative/honest and opt-in via config rather than\n // name-inferred. When set, the agent admits the attachments and\n // `toOpenAIMessages` maps them to the real wire parts.\n pdf: config.pdf ?? false,\n audio: config.audio ?? false,\n };\n }\n\n /**\n * Single-shot completion. Sends the full message list to the Chat\n * Completions endpoint, waits for the terminal response, and reshapes it\n * into a vendor-neutral `ModelResponse`. Per-call `options` override the\n * instance's `ModelConfig` defaults for this call only.\n */\n public async complete(messages: Message[], options?: ModelCallOptions): Promise<ModelResponse> {\n // Per-call request/response logs are hot-path in production agents\n // — keep them at `debug` so `info` stays reserved for lifecycle\n // events (agent starting/completed, etc.). Operators who need to\n // audit every LLM call can raise log-level at runtime.\n this.logger.debug(LOG_MODULE, \"request\", \"Starting call to chat.completions\", {\n model: this.name,\n messageCount: messages.length,\n streaming: false,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let response: OpenAI.Chat.Completions.ChatCompletion;\n\n try {\n response = await this.client.chat.completions.create(\n {\n model: this.name,\n messages: toOpenAIMessages(messages, this.promptCacheBreakpoints(options)),\n ...this.buildSamplingParams(options),\n ...buildPromptCacheParams(this.provider, this.config),\n tools: toOpenAITools(options?.tools),\n ...this.buildResponseFormat(options?.responseSchema),\n ...this.buildReasoningParams(options?.reasoning, Boolean(options?.tools?.length)),\n },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n const wrapped = wrapOpenAIError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const [choice] = response.choices;\n\n if (choice === undefined) {\n // A 200 carrying an empty `choices` array. Every line below reads off\n // `choice`, so without this the caller got a bare `TypeError: Cannot\n // read properties of undefined` — no provider, no model, no hint that\n // the response itself was the problem rather than our handling of it.\n //\n // `ProviderError` because that is what this is: the provider answered\n // successfully and returned nothing usable. Callers already branch on\n // `instanceof ProviderError` for any provider-side failure.\n throw new ProviderError(\"OpenAI returned a response with no choices.\", {\n context: { provider: \"openai\", model: this.name, choices: response.choices.length },\n });\n }\n\n const finishReason = mapFinishReason(choice.finish_reason);\n const usage = this.extractUsage(response.usage);\n\n this.logger.debug(LOG_MODULE, \"response\", \"call to chat.completions succeeded\", {\n finishReason,\n usage,\n });\n\n return {\n content: choice.message.content ?? \"\",\n finishReason,\n usage,\n toolCalls: this.extractToolCalls(choice.message.tool_calls),\n };\n }\n\n /**\n * Incremental streaming completion. Yields neutral `ModelStreamChunk`s —\n * `delta` for text tokens, `tool-call` when the model requests a tool,\n * and a terminal `done` carrying the final finish reason + usage totals.\n * Callers consume it with `for await`.\n */\n public async *stream(\n messages: Message[],\n options?: ModelCallOptions,\n ): AsyncIterable<ModelStreamChunk> {\n this.logger.debug(LOG_MODULE, \"request\", \"Starting streaming call to chat.completions\", {\n model: this.name,\n messageCount: messages.length,\n streaming: true,\n toolCount: options?.tools?.length ?? 0,\n });\n\n let stream: Awaited<ReturnType<typeof this.client.chat.completions.create>>;\n\n try {\n stream = await this.client.chat.completions.create(\n {\n model: this.name,\n messages: toOpenAIMessages(messages, this.promptCacheBreakpoints(options)),\n ...this.buildSamplingParams(options),\n ...buildPromptCacheParams(this.provider, this.config),\n tools: toOpenAITools(options?.tools),\n stream: true,\n stream_options: { include_usage: true },\n ...this.buildResponseFormat(options?.responseSchema),\n ...this.buildReasoningParams(options?.reasoning, Boolean(options?.tools?.length)),\n },\n options?.signal ? { signal: options.signal } : undefined,\n );\n } catch (thrown) {\n const wrapped = wrapOpenAIError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n let rawFinishReason: string = \"stop\";\n const usage: Usage = { input: 0, output: 0, total: 0 };\n const toolCallAccum = new Map<number, { id: string; name: string; arguments: string }>();\n\n try {\n for await (const chunk of stream as AsyncIterable<OpenAI.Chat.Completions.ChatCompletionChunk>) {\n const delta = chunk.choices[0]?.delta;\n const finish = chunk.choices[0]?.finish_reason;\n\n if (delta?.content) {\n yield { type: \"delta\", content: delta.content };\n }\n\n if (delta?.tool_calls) {\n for (const toolCall of delta.tool_calls) {\n const idx = toolCall.index ?? 0;\n if (!toolCallAccum.has(idx)) {\n toolCallAccum.set(idx, { id: \"\", name: \"\", arguments: \"\" });\n }\n const acc = toolCallAccum.get(idx)!;\n if (toolCall.id) acc.id = toolCall.id;\n if (toolCall.function?.name) acc.name = toolCall.function.name;\n if (toolCall.function?.arguments) acc.arguments += toolCall.function.arguments;\n }\n }\n\n if (finish) {\n rawFinishReason = finish;\n }\n\n if (chunk.usage) {\n usage.input = chunk.usage.prompt_tokens ?? 0;\n usage.output = chunk.usage.completion_tokens ?? 0;\n usage.total = chunk.usage.total_tokens ?? 0;\n const cached = chunk.usage.prompt_tokens_details?.cached_tokens;\n if (cached !== undefined && cached > 0) {\n usage.cachedTokens = cached;\n }\n const reasoning = chunk.usage.completion_tokens_details?.reasoning_tokens;\n if (reasoning !== undefined && reasoning > 0) {\n usage.reasoningTokens = reasoning;\n }\n }\n }\n\n for (const acc of toolCallAccum.values()) {\n // Skip accumulators that never received a function name — those\n // are partial fragments the model started but never identified\n // (e.g. arguments-only deltas with no originating `id`/`name`).\n // Yielding them produces nameless tool-calls the agent runtime\n // can't dispatch and would mis-attribute as a registered tool.\n if (!acc.name) continue;\n\n yield {\n type: \"tool-call\",\n id: acc.id,\n name: acc.name,\n input: safeJsonParse<Record<string, unknown>>(acc.arguments, {}),\n };\n }\n } catch (thrown) {\n const wrapped = wrapOpenAIError(thrown);\n\n this.logger.error(LOG_MODULE, \"error\", wrapped.message, {\n code: wrapped.code,\n context: wrapped.context,\n });\n\n throw wrapped;\n }\n\n const finishReason = mapFinishReason(rawFinishReason);\n\n this.logger.debug(LOG_MODULE, \"response\", \"Streaming call to chat.completions succeeded\", {\n finishReason,\n usage,\n });\n\n yield { type: \"done\", finishReason, usage };\n }\n\n /** Map the neutral cache hint only for the direct OpenAI provider. */\n private promptCacheBreakpoints(options: ModelCallOptions | undefined): number {\n return this.provider === \"openai\" ? (options?.cacheControl?.breakpoints ?? 0) : 0;\n }\n\n /**\n * Translate the neutral `responseSchema` option into OpenAI's\n * `response_format` parameter. The mode decision (override handling,\n * strict-compatibility downgrade to `json_object`) lives in\n * `planStructuredOutput`, shared with the Responses adapter.\n *\n * Returns an empty spread when no schema was supplied, so the caller\n * can unconditionally `...buildResponseFormat(...)` into the request.\n */\n private buildResponseFormat(responseSchema: Record<string, unknown> | undefined): {\n response_format?: OpenAI.Chat.Completions.ChatCompletionCreateParams[\"response_format\"];\n } {\n const plan = planStructuredOutput(responseSchema, this.config.responseFormat);\n\n if (plan.mode === \"json_schema\") {\n return {\n response_format: {\n type: \"json_schema\",\n json_schema: { name: \"response\", schema: plan.schema, strict: true },\n },\n };\n }\n\n if (plan.mode === \"json_object\") {\n return { response_format: { type: \"json_object\" } };\n }\n\n return {};\n }\n\n /**\n * Normalize OpenAI's `usage` block (which may be absent on some responses\n * or partials) into the neutral `Usage` shape. Missing usage collapses to\n * zeros rather than propagating `undefined`, so downstream aggregation\n * math stays safe.\n *\n * `cachedTokens` mirrors `prompt_tokens_details.cached_tokens` (the\n * subset of the prompt served from OpenAI's automatic prompt cache);\n * `reasoningTokens` mirrors `completion_tokens_details.reasoning_tokens`\n * (the hidden reasoning channel on o-series / gpt-5 models, already\n * counted within `output`). Both are emitted only when the provider\n * reports a positive value, so non-reasoning / uncached calls keep the\n * lean `{ input, output, total }` shape.\n */\n private extractUsage(raw: OpenAI.Completions.CompletionUsage | undefined): Usage {\n if (!raw) {\n return { input: 0, output: 0, total: 0 };\n }\n\n return buildUsage({\n input: raw.prompt_tokens,\n output: raw.completion_tokens,\n total: raw.total_tokens,\n cachedTokens: raw.prompt_tokens_details?.cached_tokens,\n reasoningTokens: raw.completion_tokens_details?.reasoning_tokens,\n });\n }\n\n /**\n * Translate the neutral `ModelCallOptions.reasoning` hint into OpenAI's\n * `reasoning_effort` request param. Only `effort` maps — OpenAI's Chat\n * Completions API exposes a discrete effort knob, not a token budget,\n * so `reasoning.maxTokens` (the Anthropic extended-thinking cap) has no\n * wire equivalent here and is silently ignored.\n *\n * The neutral `ReasoningEffort` (`\"low\" | \"medium\" | \"high\" | \"none\"`)\n * is a subset of OpenAI's accepted values, so it forwards verbatim —\n * `\"none\"` included. `\"none\"` is load-bearing: gpt-5 / o-series models\n * **reject function tools** on Chat Completions while reasoning is\n * active, and the endpoint accepts tools only when `reasoning_effort`\n * is `\"none\"` (the alternative is the Responses API). This is why the\n * param is EMITTED for `\"none\"` rather than omitted — omitting it\n * leaves the model reasoning server-side by default, so tools would\n * still be rejected.\n *\n * No-ops when the model is not reasoning-capable\n * (`capabilities.reasoning` is false — e.g. `gpt-4o`, which 400s on any\n * `reasoning_effort`).\n *\n * When the caller gave no explicit `effort`: defaults to `\"none\"` IF\n * `hasTools` is true, else omits the param (provider default — the\n * pre-existing behavior for a tool-less call). The default exists\n * because, on a reasoning-capable model, there is no working alternative\n * to `\"none\"` when tools are attached — omitting `reasoning_effort`\n * leaves reasoning on server-side, which Chat Completions rejects tools\n * for (empty replies on some model generations, a hard 400 — \"Function\n * tools with reasoning_effort are not supported ... in\n * /v1/chat/completions\" — on newer ones). An explicit `effort` from the\n * caller always wins over this default, in either direction.\n *\n * Returns an empty spread when nothing applies, so the caller can\n * unconditionally `...buildReasoningParams(...)` into the request.\n */\n /**\n * Output-length and sampling params, in current Chat Completions shapes only.\n *\n * - `max_completion_tokens`, never the legacy `max_tokens`: current\n * models (the gpt-5 family and later) answer `400 unsupported_parameter`\n * to `max_tokens`, and `max_completion_tokens` is accepted everywhere.\n * - No `temperature` for a reasoning-capable model: those accept only the\n * default value and reject any other with a 400.\n */\n private buildSamplingParams(options: ModelCallOptions | undefined): {\n max_completion_tokens?: number;\n temperature?: number;\n } {\n const maxTokens = options?.maxTokens ?? this.config.maxTokens;\n const temperature = options?.temperature ?? this.config.temperature;\n\n return {\n max_completion_tokens: maxTokens,\n ...(!this.capabilities.reasoning && { temperature }),\n };\n }\n\n private buildReasoningParams(\n reasoning: ModelCallOptions[\"reasoning\"],\n hasTools: boolean,\n ): {\n reasoning_effort?: OpenAI.Chat.Completions.ChatCompletionCreateParams[\"reasoning_effort\"];\n } {\n if (!this.capabilities.reasoning) {\n return {};\n }\n\n if (!reasoning?.effort) {\n return hasTools ? { reasoning_effort: \"none\" } : {};\n }\n\n return { reasoning_effort: toOpenAIReasoningEffort(reasoning.effort) };\n }\n\n /**\n * Reshape OpenAI's `tool_calls` array into the neutral\n * `ModelToolCallRequest[]`. The raw `arguments` field is a JSON string\n * per OpenAI's protocol — we parse it defensively via `safeJsonParse` so\n * malformed or empty arguments yield an empty object instead of crashing\n * the trip. Returns `undefined` when no tools were requested so callers\n * can branch on presence.\n */\n private extractToolCalls(\n rawToolCalls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[] | undefined,\n ): ModelToolCallRequest[] | undefined {\n if (!rawToolCalls || rawToolCalls.length === 0) {\n return undefined;\n }\n\n return rawToolCalls.map((toolCall) => ({\n id: toolCall.id,\n name: (toolCall as any).function.name,\n input: safeJsonParse<Record<string, unknown>>((toolCall as any).function.arguments, {}),\n }));\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;AA+BA,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuCnB,IAAa,cAAb,MAAkD;CAUhD,AAAO,YAAY,QAAgB,QAA2B,WAAmB,UAAU;gBAFzD;EAMhC,IAAI,OAAO,QAAQ,aACjB,MAAM,IAAI,oBACR,4JACA,EAAE,SAAS;GAAE;GAAU,OAAO,OAAO;GAAM,KAAK,OAAO;EAAI,EAAE,CAC/D;EAGF,KAAK,SAAS;EACd,KAAK,SAAS;EACd,KAAK,OAAO,OAAO;EACnB,KAAK,WAAW;EAChB,KAAK,UAAU,OAAO;EACtB,KAAK,eAAe;GAClB,kBAAkB,OAAO,oBAAoB,sBAAsB,OAAO,cAAc;GACxF,QAAQ,OAAO,UAAU,sBAAsB,OAAO,IAAI;GAI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