@ai-sdk/openai 3.0.0-beta.99 → 3.0.1

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.
@@ -35,8 +35,8 @@ var openaiFailedResponseHandler = createJsonErrorResponseHandler({
35
35
  function getOpenAILanguageModelCapabilities(modelId) {
36
36
  const supportsFlexProcessing = modelId.startsWith("o3") || modelId.startsWith("o4-mini") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat");
37
37
  const supportsPriorityProcessing = modelId.startsWith("gpt-4") || modelId.startsWith("gpt-5-mini") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-nano") && !modelId.startsWith("gpt-5-chat") || modelId.startsWith("o3") || modelId.startsWith("o4-mini");
38
- const isReasoningModel = !(modelId.startsWith("gpt-3") || modelId.startsWith("gpt-4") || modelId.startsWith("chatgpt-4o") || modelId.startsWith("gpt-5-chat"));
39
- const supportsNonReasoningParameters = modelId.startsWith("gpt-5.1");
38
+ const isReasoningModel = modelId.startsWith("o1") || modelId.startsWith("o3") || modelId.startsWith("o4-mini") || modelId.startsWith("codex-mini") || modelId.startsWith("computer-use-preview") || modelId.startsWith("gpt-5") && !modelId.startsWith("gpt-5-chat");
39
+ const supportsNonReasoningParameters = modelId.startsWith("gpt-5.1") || modelId.startsWith("gpt-5.2");
40
40
  const systemMessageMode = isReasoningModel ? "developer" : "system";
41
41
  return {
42
42
  supportsFlexProcessing,
@@ -237,6 +237,9 @@ function convertToOpenAIChatMessages({
237
237
  }
238
238
  case "tool": {
239
239
  for (const toolResponse of content) {
240
+ if (toolResponse.type === "tool-approval-response") {
241
+ continue;
242
+ }
240
243
  const output = toolResponse.output;
241
244
  let contentValue;
242
245
  switch (output.type) {
@@ -296,7 +299,7 @@ function mapOpenAIFinishReason(finishReason) {
296
299
  case "tool_calls":
297
300
  return "tool-calls";
298
301
  default:
299
- return "unknown";
302
+ return "other";
300
303
  }
301
304
  }
302
305
 
@@ -537,7 +540,26 @@ var openaiChatLanguageModelOptions = lazySchema2(
537
540
  * username or email address, in order to avoid sending us any identifying
538
541
  * information.
539
542
  */
540
- safetyIdentifier: z3.string().optional()
543
+ safetyIdentifier: z3.string().optional(),
544
+ /**
545
+ * Override the system message mode for this model.
546
+ * - 'system': Use the 'system' role for system messages (default for most models)
547
+ * - 'developer': Use the 'developer' role for system messages (used by reasoning models)
548
+ * - 'remove': Remove system messages entirely
549
+ *
550
+ * If not specified, the mode is automatically determined based on the model.
551
+ */
552
+ systemMessageMode: z3.enum(["system", "developer", "remove"]).optional(),
553
+ /**
554
+ * Force treating this model as a reasoning model.
555
+ *
556
+ * This is useful for "stealth" reasoning models (e.g. via a custom baseURL)
557
+ * where the model ID is not recognized by the SDK's allowlist.
558
+ *
559
+ * When enabled, the SDK applies reasoning-model parameter compatibility rules
560
+ * and defaults `systemMessageMode` to `developer` unless overridden.
561
+ */
562
+ forceReasoning: z3.boolean().optional()
541
563
  })
542
564
  )
543
565
  );
@@ -634,7 +656,7 @@ var OpenAIChatLanguageModel = class {
634
656
  toolChoice,
635
657
  providerOptions
636
658
  }) {
637
- var _a, _b, _c;
659
+ var _a, _b, _c, _d, _e;
638
660
  const warnings = [];
639
661
  const openaiOptions = (_a = await parseProviderOptions({
640
662
  provider: "openai",
@@ -642,17 +664,18 @@ var OpenAIChatLanguageModel = class {
642
664
  schema: openaiChatLanguageModelOptions
643
665
  })) != null ? _a : {};
644
666
  const modelCapabilities = getOpenAILanguageModelCapabilities(this.modelId);
667
+ const isReasoningModel = (_b = openaiOptions.forceReasoning) != null ? _b : modelCapabilities.isReasoningModel;
645
668
  if (topK != null) {
646
669
  warnings.push({ type: "unsupported", feature: "topK" });
647
670
  }
648
671
  const { messages, warnings: messageWarnings } = convertToOpenAIChatMessages(
649
672
  {
650
673
  prompt,
651
- systemMessageMode: modelCapabilities.systemMessageMode
674
+ systemMessageMode: (_c = openaiOptions.systemMessageMode) != null ? _c : isReasoningModel ? "developer" : modelCapabilities.systemMessageMode
652
675
  }
653
676
  );
654
677
  warnings.push(...messageWarnings);
655
- const strictJsonSchema = (_b = openaiOptions.strictJsonSchema) != null ? _b : true;
678
+ const strictJsonSchema = (_d = openaiOptions.strictJsonSchema) != null ? _d : true;
656
679
  const baseArgs = {
657
680
  // model id:
658
681
  model: this.modelId,
@@ -673,7 +696,7 @@ var OpenAIChatLanguageModel = class {
673
696
  json_schema: {
674
697
  schema: responseFormat.schema,
675
698
  strict: strictJsonSchema,
676
- name: (_c = responseFormat.name) != null ? _c : "response",
699
+ name: (_e = responseFormat.name) != null ? _e : "response",
677
700
  description: responseFormat.description
678
701
  }
679
702
  } : { type: "json_object" } : void 0,
@@ -694,7 +717,7 @@ var OpenAIChatLanguageModel = class {
694
717
  // messages:
695
718
  messages
696
719
  };
697
- if (modelCapabilities.isReasoningModel) {
720
+ if (isReasoningModel) {
698
721
  if (openaiOptions.reasoningEffort !== "none" || !modelCapabilities.supportsNonReasoningParameters) {
699
722
  if (baseArgs.temperature != null) {
700
723
  baseArgs.temperature = void 0;
@@ -800,7 +823,7 @@ var OpenAIChatLanguageModel = class {
800
823
  };
801
824
  }
802
825
  async doGenerate(options) {
803
- var _a, _b, _c, _d, _e, _f;
826
+ var _a, _b, _c, _d, _e, _f, _g;
804
827
  const { args: body, warnings } = await this.getArgs(options);
805
828
  const {
806
829
  responseHeaders,
@@ -857,7 +880,10 @@ var OpenAIChatLanguageModel = class {
857
880
  }
858
881
  return {
859
882
  content,
860
- finishReason: mapOpenAIFinishReason(choice.finish_reason),
883
+ finishReason: {
884
+ unified: mapOpenAIFinishReason(choice.finish_reason),
885
+ raw: (_g = choice.finish_reason) != null ? _g : void 0
886
+ },
861
887
  usage: convertOpenAIChatUsage(response.usage),
862
888
  request: { body },
863
889
  response: {
@@ -893,7 +919,10 @@ var OpenAIChatLanguageModel = class {
893
919
  fetch: this.config.fetch
894
920
  });
895
921
  const toolCalls = [];
896
- let finishReason = "unknown";
922
+ let finishReason = {
923
+ unified: "other",
924
+ raw: void 0
925
+ };
897
926
  let usage = void 0;
898
927
  let metadataExtracted = false;
899
928
  let isActiveText = false;
@@ -910,13 +939,13 @@ var OpenAIChatLanguageModel = class {
910
939
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
911
940
  }
912
941
  if (!chunk.success) {
913
- finishReason = "error";
942
+ finishReason = { unified: "error", raw: void 0 };
914
943
  controller.enqueue({ type: "error", error: chunk.error });
915
944
  return;
916
945
  }
917
946
  const value = chunk.value;
918
947
  if ("error" in value) {
919
- finishReason = "error";
948
+ finishReason = { unified: "error", raw: void 0 };
920
949
  controller.enqueue({ type: "error", error: value.error });
921
950
  return;
922
951
  }
@@ -941,7 +970,10 @@ var OpenAIChatLanguageModel = class {
941
970
  }
942
971
  const choice = value.choices[0];
943
972
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
944
- finishReason = mapOpenAIFinishReason(choice.finish_reason);
973
+ finishReason = {
974
+ unified: mapOpenAIFinishReason(choice.finish_reason),
975
+ raw: choice.finish_reason
976
+ };
945
977
  }
946
978
  if (((_e = choice == null ? void 0 : choice.logprobs) == null ? void 0 : _e.content) != null) {
947
979
  providerMetadata.openai.logprobs = choice.logprobs.content;
@@ -1230,7 +1262,7 @@ function mapOpenAIFinishReason2(finishReason) {
1230
1262
  case "tool_calls":
1231
1263
  return "tool-calls";
1232
1264
  default:
1233
- return "unknown";
1265
+ return "other";
1234
1266
  }
1235
1267
  }
1236
1268
 
@@ -1428,6 +1460,7 @@ var OpenAICompletionLanguageModel = class {
1428
1460
  };
1429
1461
  }
1430
1462
  async doGenerate(options) {
1463
+ var _a;
1431
1464
  const { args, warnings } = await this.getArgs(options);
1432
1465
  const {
1433
1466
  responseHeaders,
@@ -1455,7 +1488,10 @@ var OpenAICompletionLanguageModel = class {
1455
1488
  return {
1456
1489
  content: [{ type: "text", text: choice.text }],
1457
1490
  usage: convertOpenAICompletionUsage(response.usage),
1458
- finishReason: mapOpenAIFinishReason2(choice.finish_reason),
1491
+ finishReason: {
1492
+ unified: mapOpenAIFinishReason2(choice.finish_reason),
1493
+ raw: (_a = choice.finish_reason) != null ? _a : void 0
1494
+ },
1459
1495
  request: { body: args },
1460
1496
  response: {
1461
1497
  ...getResponseMetadata2(response),
@@ -1489,7 +1525,10 @@ var OpenAICompletionLanguageModel = class {
1489
1525
  abortSignal: options.abortSignal,
1490
1526
  fetch: this.config.fetch
1491
1527
  });
1492
- let finishReason = "unknown";
1528
+ let finishReason = {
1529
+ unified: "other",
1530
+ raw: void 0
1531
+ };
1493
1532
  const providerMetadata = { openai: {} };
1494
1533
  let usage = void 0;
1495
1534
  let isFirstChunk = true;
@@ -1504,13 +1543,13 @@ var OpenAICompletionLanguageModel = class {
1504
1543
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1505
1544
  }
1506
1545
  if (!chunk.success) {
1507
- finishReason = "error";
1546
+ finishReason = { unified: "error", raw: void 0 };
1508
1547
  controller.enqueue({ type: "error", error: chunk.error });
1509
1548
  return;
1510
1549
  }
1511
1550
  const value = chunk.value;
1512
1551
  if ("error" in value) {
1513
- finishReason = "error";
1552
+ finishReason = { unified: "error", raw: void 0 };
1514
1553
  controller.enqueue({ type: "error", error: value.error });
1515
1554
  return;
1516
1555
  }
@@ -1527,7 +1566,10 @@ var OpenAICompletionLanguageModel = class {
1527
1566
  }
1528
1567
  const choice = value.choices[0];
1529
1568
  if ((choice == null ? void 0 : choice.finish_reason) != null) {
1530
- finishReason = mapOpenAIFinishReason2(choice.finish_reason);
1569
+ finishReason = {
1570
+ unified: mapOpenAIFinishReason2(choice.finish_reason),
1571
+ raw: choice.finish_reason
1572
+ };
1531
1573
  }
1532
1574
  if ((choice == null ? void 0 : choice.logprobs) != null) {
1533
1575
  providerMetadata.openai.logprobs = choice.logprobs;
@@ -1670,7 +1712,11 @@ var OpenAIEmbeddingModel = class {
1670
1712
  // src/image/openai-image-model.ts
1671
1713
  import {
1672
1714
  combineHeaders as combineHeaders4,
1715
+ convertBase64ToUint8Array,
1716
+ convertToFormData,
1673
1717
  createJsonResponseHandler as createJsonResponseHandler4,
1718
+ downloadBlob,
1719
+ postFormDataToApi,
1674
1720
  postJsonToApi as postJsonToApi4
1675
1721
  } from "@ai-sdk/provider-utils";
1676
1722
 
@@ -1709,11 +1755,13 @@ var modelMaxImagesPerCall = {
1709
1755
  "dall-e-3": 1,
1710
1756
  "dall-e-2": 10,
1711
1757
  "gpt-image-1": 10,
1712
- "gpt-image-1-mini": 10
1758
+ "gpt-image-1-mini": 10,
1759
+ "gpt-image-1.5": 10
1713
1760
  };
1714
1761
  var hasDefaultResponseFormat = /* @__PURE__ */ new Set([
1715
1762
  "gpt-image-1",
1716
- "gpt-image-1-mini"
1763
+ "gpt-image-1-mini",
1764
+ "gpt-image-1.5"
1717
1765
  ]);
1718
1766
 
1719
1767
  // src/image/openai-image-model.ts
@@ -1732,6 +1780,8 @@ var OpenAIImageModel = class {
1732
1780
  }
1733
1781
  async doGenerate({
1734
1782
  prompt,
1783
+ files,
1784
+ mask,
1735
1785
  n,
1736
1786
  size,
1737
1787
  aspectRatio,
@@ -1740,7 +1790,7 @@ var OpenAIImageModel = class {
1740
1790
  headers,
1741
1791
  abortSignal
1742
1792
  }) {
1743
- var _a, _b, _c, _d, _e, _f, _g;
1793
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
1744
1794
  const warnings = [];
1745
1795
  if (aspectRatio != null) {
1746
1796
  warnings.push({
@@ -1753,6 +1803,72 @@ var OpenAIImageModel = class {
1753
1803
  warnings.push({ type: "unsupported", feature: "seed" });
1754
1804
  }
1755
1805
  const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
1806
+ if (files != null) {
1807
+ const { value: response2, responseHeaders: responseHeaders2 } = await postFormDataToApi({
1808
+ url: this.config.url({
1809
+ path: "/images/edits",
1810
+ modelId: this.modelId
1811
+ }),
1812
+ headers: combineHeaders4(this.config.headers(), headers),
1813
+ formData: convertToFormData({
1814
+ model: this.modelId,
1815
+ prompt,
1816
+ image: await Promise.all(
1817
+ files.map(
1818
+ (file) => file.type === "file" ? new Blob(
1819
+ [
1820
+ file.data instanceof Uint8Array ? new Blob([file.data], {
1821
+ type: file.mediaType
1822
+ }) : new Blob([convertBase64ToUint8Array(file.data)], {
1823
+ type: file.mediaType
1824
+ })
1825
+ ],
1826
+ { type: file.mediaType }
1827
+ ) : downloadBlob(file.url)
1828
+ )
1829
+ ),
1830
+ mask: mask != null ? await fileToBlob(mask) : void 0,
1831
+ n,
1832
+ size,
1833
+ ...(_d = providerOptions.openai) != null ? _d : {}
1834
+ }),
1835
+ failedResponseHandler: openaiFailedResponseHandler,
1836
+ successfulResponseHandler: createJsonResponseHandler4(
1837
+ openaiImageResponseSchema
1838
+ ),
1839
+ abortSignal,
1840
+ fetch: this.config.fetch
1841
+ });
1842
+ return {
1843
+ images: response2.data.map((item) => item.b64_json),
1844
+ warnings,
1845
+ usage: response2.usage != null ? {
1846
+ inputTokens: (_e = response2.usage.input_tokens) != null ? _e : void 0,
1847
+ outputTokens: (_f = response2.usage.output_tokens) != null ? _f : void 0,
1848
+ totalTokens: (_g = response2.usage.total_tokens) != null ? _g : void 0
1849
+ } : void 0,
1850
+ response: {
1851
+ timestamp: currentDate,
1852
+ modelId: this.modelId,
1853
+ headers: responseHeaders2
1854
+ },
1855
+ providerMetadata: {
1856
+ openai: {
1857
+ images: response2.data.map((item) => {
1858
+ var _a2, _b2, _c2, _d2, _e2;
1859
+ return {
1860
+ ...item.revised_prompt ? { revisedPrompt: item.revised_prompt } : {},
1861
+ created: (_a2 = response2.created) != null ? _a2 : void 0,
1862
+ size: (_b2 = response2.size) != null ? _b2 : void 0,
1863
+ quality: (_c2 = response2.quality) != null ? _c2 : void 0,
1864
+ background: (_d2 = response2.background) != null ? _d2 : void 0,
1865
+ outputFormat: (_e2 = response2.output_format) != null ? _e2 : void 0
1866
+ };
1867
+ })
1868
+ }
1869
+ }
1870
+ };
1871
+ }
1756
1872
  const { value: response, responseHeaders } = await postJsonToApi4({
1757
1873
  url: this.config.url({
1758
1874
  path: "/images/generations",
@@ -1764,7 +1880,7 @@ var OpenAIImageModel = class {
1764
1880
  prompt,
1765
1881
  n,
1766
1882
  size,
1767
- ...(_d = providerOptions.openai) != null ? _d : {},
1883
+ ...(_h = providerOptions.openai) != null ? _h : {},
1768
1884
  ...!hasDefaultResponseFormat.has(this.modelId) ? { response_format: "b64_json" } : {}
1769
1885
  },
1770
1886
  failedResponseHandler: openaiFailedResponseHandler,
@@ -1778,9 +1894,9 @@ var OpenAIImageModel = class {
1778
1894
  images: response.data.map((item) => item.b64_json),
1779
1895
  warnings,
1780
1896
  usage: response.usage != null ? {
1781
- inputTokens: (_e = response.usage.input_tokens) != null ? _e : void 0,
1782
- outputTokens: (_f = response.usage.output_tokens) != null ? _f : void 0,
1783
- totalTokens: (_g = response.usage.total_tokens) != null ? _g : void 0
1897
+ inputTokens: (_i = response.usage.input_tokens) != null ? _i : void 0,
1898
+ outputTokens: (_j = response.usage.output_tokens) != null ? _j : void 0,
1899
+ totalTokens: (_k = response.usage.total_tokens) != null ? _k : void 0
1784
1900
  } : void 0,
1785
1901
  response: {
1786
1902
  timestamp: currentDate,
@@ -1805,15 +1921,23 @@ var OpenAIImageModel = class {
1805
1921
  };
1806
1922
  }
1807
1923
  };
1924
+ async function fileToBlob(file) {
1925
+ if (!file) return void 0;
1926
+ if (file.type === "url") {
1927
+ return downloadBlob(file.url);
1928
+ }
1929
+ const data = file.data instanceof Uint8Array ? file.data : convertBase64ToUint8Array(file.data);
1930
+ return new Blob([data], { type: file.mediaType });
1931
+ }
1808
1932
 
1809
1933
  // src/transcription/openai-transcription-model.ts
1810
1934
  import {
1811
1935
  combineHeaders as combineHeaders5,
1812
- convertBase64ToUint8Array,
1936
+ convertBase64ToUint8Array as convertBase64ToUint8Array2,
1813
1937
  createJsonResponseHandler as createJsonResponseHandler5,
1814
1938
  mediaTypeToExtension,
1815
1939
  parseProviderOptions as parseProviderOptions4,
1816
- postFormDataToApi
1940
+ postFormDataToApi as postFormDataToApi2
1817
1941
  } from "@ai-sdk/provider-utils";
1818
1942
 
1819
1943
  // src/transcription/openai-transcription-api.ts
@@ -1963,7 +2087,7 @@ var OpenAITranscriptionModel = class {
1963
2087
  schema: openAITranscriptionProviderOptions
1964
2088
  });
1965
2089
  const formData = new FormData();
1966
- const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
2090
+ const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array2(audio)]);
1967
2091
  formData.append("model", this.modelId);
1968
2092
  const fileExtension = mediaTypeToExtension(mediaType);
1969
2093
  formData.append(
@@ -2010,7 +2134,7 @@ var OpenAITranscriptionModel = class {
2010
2134
  value: response,
2011
2135
  responseHeaders,
2012
2136
  rawValue: rawResponse
2013
- } = await postFormDataToApi({
2137
+ } = await postFormDataToApi2({
2014
2138
  url: this.config.url({
2015
2139
  path: "/audio/transcriptions",
2016
2140
  modelId: this.modelId
@@ -2366,9 +2490,10 @@ async function convertToOpenAIResponsesInput({
2366
2490
  hasShellTool = false,
2367
2491
  hasApplyPatchTool = false
2368
2492
  }) {
2369
- var _a, _b, _c, _d, _e;
2493
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
2370
2494
  const input = [];
2371
2495
  const warnings = [];
2496
+ const processedApprovalIds = /* @__PURE__ */ new Set();
2372
2497
  for (const { role, content } of prompt) {
2373
2498
  switch (role) {
2374
2499
  case "system": {
@@ -2459,10 +2584,13 @@ async function convertToOpenAIResponsesInput({
2459
2584
  break;
2460
2585
  }
2461
2586
  case "tool-call": {
2587
+ const id = (_g = (_d = (_c = part.providerOptions) == null ? void 0 : _c.openai) == null ? void 0 : _d.itemId) != null ? _g : (_f = (_e = part.providerMetadata) == null ? void 0 : _e.openai) == null ? void 0 : _f.itemId;
2462
2588
  if (part.providerExecuted) {
2589
+ if (store && id != null) {
2590
+ input.push({ type: "item_reference", id });
2591
+ }
2463
2592
  break;
2464
2593
  }
2465
- const id = (_d = (_c = part.providerOptions) == null ? void 0 : _c.openai) == null ? void 0 : _d.itemId;
2466
2594
  if (store && id != null) {
2467
2595
  input.push({ type: "item_reference", id });
2468
2596
  break;
@@ -2519,8 +2647,12 @@ async function convertToOpenAIResponsesInput({
2519
2647
  }
2520
2648
  // assistant tool result parts are from provider-executed tools:
2521
2649
  case "tool-result": {
2650
+ if (part.output.type === "execution-denied" || part.output.type === "json" && typeof part.output.value === "object" && part.output.value != null && "type" in part.output.value && part.output.value.type === "execution-denied") {
2651
+ break;
2652
+ }
2522
2653
  if (store) {
2523
- input.push({ type: "item_reference", id: part.toolCallId });
2654
+ const itemId = (_j = (_i = (_h = part.providerMetadata) == null ? void 0 : _h.openai) == null ? void 0 : _i.itemId) != null ? _j : part.toolCallId;
2655
+ input.push({ type: "item_reference", id: itemId });
2524
2656
  } else {
2525
2657
  warnings.push({
2526
2658
  type: "other",
@@ -2589,7 +2721,32 @@ async function convertToOpenAIResponsesInput({
2589
2721
  }
2590
2722
  case "tool": {
2591
2723
  for (const part of content) {
2724
+ if (part.type === "tool-approval-response") {
2725
+ const approvalResponse = part;
2726
+ if (processedApprovalIds.has(approvalResponse.approvalId)) {
2727
+ continue;
2728
+ }
2729
+ processedApprovalIds.add(approvalResponse.approvalId);
2730
+ if (store) {
2731
+ input.push({
2732
+ type: "item_reference",
2733
+ id: approvalResponse.approvalId
2734
+ });
2735
+ }
2736
+ input.push({
2737
+ type: "mcp_approval_response",
2738
+ approval_request_id: approvalResponse.approvalId,
2739
+ approve: approvalResponse.approved
2740
+ });
2741
+ continue;
2742
+ }
2592
2743
  const output = part.output;
2744
+ if (output.type === "execution-denied") {
2745
+ const approvalId = (_l = (_k = output.providerOptions) == null ? void 0 : _k.openai) == null ? void 0 : _l.approvalId;
2746
+ if (approvalId) {
2747
+ continue;
2748
+ }
2749
+ }
2593
2750
  const resolvedToolName = toolNameMapping.toProviderToolName(
2594
2751
  part.toolName
2595
2752
  );
@@ -2603,7 +2760,7 @@ async function convertToOpenAIResponsesInput({
2603
2760
  call_id: part.toolCallId,
2604
2761
  output: parsedOutput.output
2605
2762
  });
2606
- break;
2763
+ continue;
2607
2764
  }
2608
2765
  if (hasShellTool && resolvedToolName === "shell" && output.type === "json") {
2609
2766
  const parsedOutput = await validateTypes({
@@ -2622,7 +2779,7 @@ async function convertToOpenAIResponsesInput({
2622
2779
  }
2623
2780
  }))
2624
2781
  });
2625
- break;
2782
+ continue;
2626
2783
  }
2627
2784
  if (hasApplyPatchTool && part.toolName === "apply_patch" && output.type === "json") {
2628
2785
  const parsedOutput = await validateTypes({
@@ -2635,7 +2792,7 @@ async function convertToOpenAIResponsesInput({
2635
2792
  status: parsedOutput.status,
2636
2793
  output: parsedOutput.output
2637
2794
  });
2638
- break;
2795
+ continue;
2639
2796
  }
2640
2797
  let contentValue;
2641
2798
  switch (output.type) {
@@ -2644,7 +2801,7 @@ async function convertToOpenAIResponsesInput({
2644
2801
  contentValue = output.value;
2645
2802
  break;
2646
2803
  case "execution-denied":
2647
- contentValue = (_e = output.reason) != null ? _e : "Tool execution denied.";
2804
+ contentValue = (_m = output.reason) != null ? _m : "Tool execution denied.";
2648
2805
  break;
2649
2806
  case "json":
2650
2807
  case "error-json":
@@ -2716,7 +2873,7 @@ function mapOpenAIResponseFinishReason({
2716
2873
  case "content_filter":
2717
2874
  return "content-filter";
2718
2875
  default:
2719
- return hasFunctionCall ? "tool-calls" : "unknown";
2876
+ return hasFunctionCall ? "tool-calls" : "other";
2720
2877
  }
2721
2878
  }
2722
2879
 
@@ -2819,7 +2976,8 @@ var openaiResponsesChunkSchema = lazySchema14(
2819
2976
  z16.object({
2820
2977
  type: z16.literal("mcp_call"),
2821
2978
  id: z16.string(),
2822
- status: z16.string()
2979
+ status: z16.string(),
2980
+ approval_request_id: z16.string().nullish()
2823
2981
  }),
2824
2982
  z16.object({
2825
2983
  type: z16.literal("mcp_list_tools"),
@@ -2976,7 +3134,8 @@ var openaiResponsesChunkSchema = lazySchema14(
2976
3134
  code: z16.union([z16.number(), z16.string()]).optional(),
2977
3135
  message: z16.string().optional()
2978
3136
  }).loose()
2979
- ]).nullish()
3137
+ ]).nullish(),
3138
+ approval_request_id: z16.string().nullish()
2980
3139
  }),
2981
3140
  z16.object({
2982
3141
  type: z16.literal("mcp_list_tools"),
@@ -3005,7 +3164,7 @@ var openaiResponsesChunkSchema = lazySchema14(
3005
3164
  server_label: z16.string(),
3006
3165
  name: z16.string(),
3007
3166
  arguments: z16.string(),
3008
- approval_request_id: z16.string()
3167
+ approval_request_id: z16.string().optional()
3009
3168
  }),
3010
3169
  z16.object({
3011
3170
  type: z16.literal("apply_patch_call"),
@@ -3115,6 +3274,19 @@ var openaiResponsesChunkSchema = lazySchema14(
3115
3274
  item_id: z16.string(),
3116
3275
  summary_index: z16.number()
3117
3276
  }),
3277
+ z16.object({
3278
+ type: z16.literal("response.apply_patch_call_operation_diff.delta"),
3279
+ item_id: z16.string(),
3280
+ output_index: z16.number(),
3281
+ delta: z16.string(),
3282
+ obfuscation: z16.string().nullish()
3283
+ }),
3284
+ z16.object({
3285
+ type: z16.literal("response.apply_patch_call_operation_diff.done"),
3286
+ item_id: z16.string(),
3287
+ output_index: z16.number(),
3288
+ diff: z16.string()
3289
+ }),
3118
3290
  z16.object({
3119
3291
  type: z16.literal("error"),
3120
3292
  sequence_number: z16.number(),
@@ -3315,7 +3487,8 @@ var openaiResponsesResponseSchema = lazySchema14(
3315
3487
  code: z16.union([z16.number(), z16.string()]).optional(),
3316
3488
  message: z16.string().optional()
3317
3489
  }).loose()
3318
- ]).nullish()
3490
+ ]).nullish(),
3491
+ approval_request_id: z16.string().nullish()
3319
3492
  }),
3320
3493
  z16.object({
3321
3494
  type: z16.literal("mcp_list_tools"),
@@ -3344,7 +3517,7 @@ var openaiResponsesResponseSchema = lazySchema14(
3344
3517
  server_label: z16.string(),
3345
3518
  name: z16.string(),
3346
3519
  arguments: z16.string(),
3347
- approval_request_id: z16.string()
3520
+ approval_request_id: z16.string().optional()
3348
3521
  }),
3349
3522
  z16.object({
3350
3523
  type: z16.literal("apply_patch_call"),
@@ -3595,7 +3768,26 @@ var openaiResponsesProviderOptionsSchema = lazySchema15(
3595
3768
  * Defaults to `undefined`.
3596
3769
  * @see https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids
3597
3770
  */
3598
- user: z17.string().nullish()
3771
+ user: z17.string().nullish(),
3772
+ /**
3773
+ * Override the system message mode for this model.
3774
+ * - 'system': Use the 'system' role for system messages (default for most models)
3775
+ * - 'developer': Use the 'developer' role for system messages (used by reasoning models)
3776
+ * - 'remove': Remove system messages entirely
3777
+ *
3778
+ * If not specified, the mode is automatically determined based on the model.
3779
+ */
3780
+ systemMessageMode: z17.enum(["system", "developer", "remove"]).optional(),
3781
+ /**
3782
+ * Force treating this model as a reasoning model.
3783
+ *
3784
+ * This is useful for "stealth" reasoning models (e.g. via a custom baseURL)
3785
+ * where the model ID is not recognized by the SDK's allowlist.
3786
+ *
3787
+ * When enabled, the SDK applies reasoning-model parameter compatibility rules
3788
+ * and defaults `systemMessageMode` to `developer` unless overridden.
3789
+ */
3790
+ forceReasoning: z17.boolean().optional()
3599
3791
  })
3600
3792
  )
3601
3793
  );
@@ -3777,16 +3969,14 @@ var mcpArgsSchema = lazySchema19(
3777
3969
  authorization: z21.string().optional(),
3778
3970
  connectorId: z21.string().optional(),
3779
3971
  headers: z21.record(z21.string(), z21.string()).optional(),
3780
- // TODO: Integrate this MCP tool approval with our SDK's existing tool approval architecture
3781
- // requireApproval: z
3782
- // .union([
3783
- // z.enum(['always', 'never']),
3784
- // z.object({
3785
- // readOnly: z.boolean().optional(),
3786
- // toolNames: z.array(z.string()).optional(),
3787
- // }),
3788
- // ])
3789
- // .optional(),
3972
+ requireApproval: z21.union([
3973
+ z21.enum(["always", "never"]),
3974
+ z21.object({
3975
+ never: z21.object({
3976
+ toolNames: z21.array(z21.string()).optional()
3977
+ }).optional()
3978
+ })
3979
+ ]).optional(),
3790
3980
  serverDescription: z21.string().optional(),
3791
3981
  serverUrl: z21.string().optional()
3792
3982
  }).refine(
@@ -3798,36 +3988,14 @@ var mcpArgsSchema = lazySchema19(
3798
3988
  var mcpInputSchema = lazySchema19(() => zodSchema19(z21.object({})));
3799
3989
  var mcpOutputSchema = lazySchema19(
3800
3990
  () => zodSchema19(
3801
- z21.discriminatedUnion("type", [
3802
- z21.object({
3803
- type: z21.literal("call"),
3804
- serverLabel: z21.string(),
3805
- name: z21.string(),
3806
- arguments: z21.string(),
3807
- output: z21.string().nullable().optional(),
3808
- error: z21.union([z21.string(), jsonValueSchema]).optional()
3809
- }),
3810
- z21.object({
3811
- type: z21.literal("listTools"),
3812
- serverLabel: z21.string(),
3813
- tools: z21.array(
3814
- z21.object({
3815
- name: z21.string(),
3816
- description: z21.string().optional(),
3817
- inputSchema: jsonValueSchema,
3818
- annotations: z21.record(z21.string(), jsonValueSchema).optional()
3819
- })
3820
- ),
3821
- error: z21.union([z21.string(), jsonValueSchema]).optional()
3822
- }),
3823
- z21.object({
3824
- type: z21.literal("approvalRequest"),
3825
- serverLabel: z21.string(),
3826
- name: z21.string(),
3827
- arguments: z21.string(),
3828
- approvalRequestId: z21.string()
3829
- })
3830
- ])
3991
+ z21.object({
3992
+ type: z21.literal("call"),
3993
+ serverLabel: z21.string(),
3994
+ name: z21.string(),
3995
+ arguments: z21.string(),
3996
+ output: z21.string().nullish(),
3997
+ error: z21.union([z21.string(), jsonValueSchema]).optional()
3998
+ })
3831
3999
  )
3832
4000
  );
3833
4001
  var mcpToolFactory = createProviderToolFactoryWithOutputSchema7({
@@ -4068,6 +4236,11 @@ async function prepareResponsesTools({
4068
4236
  value: tool.args,
4069
4237
  schema: mcpArgsSchema
4070
4238
  });
4239
+ const mapApprovalFilter = (filter) => ({
4240
+ tool_names: filter.toolNames
4241
+ });
4242
+ const requireApproval = args.requireApproval;
4243
+ const requireApprovalParam = requireApproval == null ? void 0 : typeof requireApproval === "string" ? requireApproval : requireApproval.never != null ? { never: mapApprovalFilter(requireApproval.never) } : void 0;
4071
4244
  openaiTools.push({
4072
4245
  type: "mcp",
4073
4246
  server_label: args.serverLabel,
@@ -4078,7 +4251,7 @@ async function prepareResponsesTools({
4078
4251
  authorization: args.authorization,
4079
4252
  connector_id: args.connectorId,
4080
4253
  headers: args.headers,
4081
- require_approval: "never",
4254
+ require_approval: requireApprovalParam != null ? requireApprovalParam : "never",
4082
4255
  server_description: args.serverDescription,
4083
4256
  server_url: args.serverUrl
4084
4257
  });
@@ -4120,6 +4293,21 @@ async function prepareResponsesTools({
4120
4293
  }
4121
4294
 
4122
4295
  // src/responses/openai-responses-language-model.ts
4296
+ function extractApprovalRequestIdToToolCallIdMapping(prompt) {
4297
+ var _a, _b;
4298
+ const mapping = {};
4299
+ for (const message of prompt) {
4300
+ if (message.role !== "assistant") continue;
4301
+ for (const part of message.content) {
4302
+ if (part.type !== "tool-call") continue;
4303
+ const approvalRequestId = (_b = (_a = part.providerOptions) == null ? void 0 : _a.openai) == null ? void 0 : _b.approvalRequestId;
4304
+ if (approvalRequestId != null) {
4305
+ mapping[approvalRequestId] = part.toolCallId;
4306
+ }
4307
+ }
4308
+ }
4309
+ return mapping;
4310
+ }
4123
4311
  var OpenAIResponsesLanguageModel = class {
4124
4312
  constructor(modelId, config) {
4125
4313
  this.specificationVersion = "v3";
@@ -4148,7 +4336,7 @@ var OpenAIResponsesLanguageModel = class {
4148
4336
  toolChoice,
4149
4337
  responseFormat
4150
4338
  }) {
4151
- var _a, _b, _c, _d;
4339
+ var _a, _b, _c, _d, _e, _f;
4152
4340
  const warnings = [];
4153
4341
  const modelCapabilities = getOpenAILanguageModelCapabilities(this.modelId);
4154
4342
  if (topK != null) {
@@ -4171,6 +4359,7 @@ var OpenAIResponsesLanguageModel = class {
4171
4359
  providerOptions,
4172
4360
  schema: openaiResponsesProviderOptionsSchema
4173
4361
  });
4362
+ const isReasoningModel = (_a = openaiOptions == null ? void 0 : openaiOptions.forceReasoning) != null ? _a : modelCapabilities.isReasoningModel;
4174
4363
  if ((openaiOptions == null ? void 0 : openaiOptions.conversation) && (openaiOptions == null ? void 0 : openaiOptions.previousResponseId)) {
4175
4364
  warnings.push({
4176
4365
  type: "unsupported",
@@ -4195,15 +4384,15 @@ var OpenAIResponsesLanguageModel = class {
4195
4384
  const { input, warnings: inputWarnings } = await convertToOpenAIResponsesInput({
4196
4385
  prompt,
4197
4386
  toolNameMapping,
4198
- systemMessageMode: modelCapabilities.systemMessageMode,
4387
+ systemMessageMode: (_b = openaiOptions == null ? void 0 : openaiOptions.systemMessageMode) != null ? _b : isReasoningModel ? "developer" : modelCapabilities.systemMessageMode,
4199
4388
  fileIdPrefixes: this.config.fileIdPrefixes,
4200
- store: (_a = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _a : true,
4389
+ store: (_c = openaiOptions == null ? void 0 : openaiOptions.store) != null ? _c : true,
4201
4390
  hasLocalShellTool: hasOpenAITool("openai.local_shell"),
4202
4391
  hasShellTool: hasOpenAITool("openai.shell"),
4203
4392
  hasApplyPatchTool: hasOpenAITool("openai.apply_patch")
4204
4393
  });
4205
4394
  warnings.push(...inputWarnings);
4206
- const strictJsonSchema = (_b = openaiOptions == null ? void 0 : openaiOptions.strictJsonSchema) != null ? _b : true;
4395
+ const strictJsonSchema = (_d = openaiOptions == null ? void 0 : openaiOptions.strictJsonSchema) != null ? _d : true;
4207
4396
  let include = openaiOptions == null ? void 0 : openaiOptions.include;
4208
4397
  function addInclude(key) {
4209
4398
  if (include == null) {
@@ -4219,9 +4408,9 @@ var OpenAIResponsesLanguageModel = class {
4219
4408
  if (topLogprobs) {
4220
4409
  addInclude("message.output_text.logprobs");
4221
4410
  }
4222
- const webSearchToolName = (_c = tools == null ? void 0 : tools.find(
4411
+ const webSearchToolName = (_e = tools == null ? void 0 : tools.find(
4223
4412
  (tool) => tool.type === "provider" && (tool.id === "openai.web_search" || tool.id === "openai.web_search_preview")
4224
- )) == null ? void 0 : _c.name;
4413
+ )) == null ? void 0 : _e.name;
4225
4414
  if (webSearchToolName) {
4226
4415
  addInclude("web_search_call.action.sources");
4227
4416
  }
@@ -4229,7 +4418,7 @@ var OpenAIResponsesLanguageModel = class {
4229
4418
  addInclude("code_interpreter_call.outputs");
4230
4419
  }
4231
4420
  const store = openaiOptions == null ? void 0 : openaiOptions.store;
4232
- if (store === false && modelCapabilities.isReasoningModel) {
4421
+ if (store === false && isReasoningModel) {
4233
4422
  addInclude("reasoning.encrypted_content");
4234
4423
  }
4235
4424
  const baseArgs = {
@@ -4244,7 +4433,7 @@ var OpenAIResponsesLanguageModel = class {
4244
4433
  format: responseFormat.schema != null ? {
4245
4434
  type: "json_schema",
4246
4435
  strict: strictJsonSchema,
4247
- name: (_d = responseFormat.name) != null ? _d : "response",
4436
+ name: (_f = responseFormat.name) != null ? _f : "response",
4248
4437
  description: responseFormat.description,
4249
4438
  schema: responseFormat.schema
4250
4439
  } : { type: "json_object" }
@@ -4271,7 +4460,7 @@ var OpenAIResponsesLanguageModel = class {
4271
4460
  top_logprobs: topLogprobs,
4272
4461
  truncation: openaiOptions == null ? void 0 : openaiOptions.truncation,
4273
4462
  // model-specific settings:
4274
- ...modelCapabilities.isReasoningModel && ((openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null || (openaiOptions == null ? void 0 : openaiOptions.reasoningSummary) != null) && {
4463
+ ...isReasoningModel && ((openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null || (openaiOptions == null ? void 0 : openaiOptions.reasoningSummary) != null) && {
4275
4464
  reasoning: {
4276
4465
  ...(openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) != null && {
4277
4466
  effort: openaiOptions.reasoningEffort
@@ -4282,7 +4471,7 @@ var OpenAIResponsesLanguageModel = class {
4282
4471
  }
4283
4472
  }
4284
4473
  };
4285
- if (modelCapabilities.isReasoningModel) {
4474
+ if (isReasoningModel) {
4286
4475
  if (!((openaiOptions == null ? void 0 : openaiOptions.reasoningEffort) === "none" && modelCapabilities.supportsNonReasoningParameters)) {
4287
4476
  if (baseArgs.temperature != null) {
4288
4477
  baseArgs.temperature = void 0;
@@ -4354,7 +4543,7 @@ var OpenAIResponsesLanguageModel = class {
4354
4543
  };
4355
4544
  }
4356
4545
  async doGenerate(options) {
4357
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x;
4546
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E;
4358
4547
  const {
4359
4548
  args: body,
4360
4549
  warnings,
@@ -4366,6 +4555,7 @@ var OpenAIResponsesLanguageModel = class {
4366
4555
  modelId: this.modelId
4367
4556
  });
4368
4557
  const providerKey = this.config.provider.replace(".responses", "");
4558
+ const approvalRequestIdToDummyToolCallIdFromPrompt = extractApprovalRequestIdToToolCallIdMapping(options.prompt);
4369
4559
  const {
4370
4560
  responseHeaders,
4371
4561
  value: response,
@@ -4582,17 +4772,20 @@ var OpenAIResponsesLanguageModel = class {
4582
4772
  break;
4583
4773
  }
4584
4774
  case "mcp_call": {
4775
+ const toolCallId = part.approval_request_id != null ? (_v = approvalRequestIdToDummyToolCallIdFromPrompt[part.approval_request_id]) != null ? _v : part.id : part.id;
4776
+ const toolName = `mcp.${part.name}`;
4585
4777
  content.push({
4586
4778
  type: "tool-call",
4587
- toolCallId: part.id,
4588
- toolName: toolNameMapping.toCustomToolName("mcp"),
4589
- input: JSON.stringify({}),
4590
- providerExecuted: true
4779
+ toolCallId,
4780
+ toolName,
4781
+ input: part.arguments,
4782
+ providerExecuted: true,
4783
+ dynamic: true
4591
4784
  });
4592
4785
  content.push({
4593
4786
  type: "tool-result",
4594
- toolCallId: part.id,
4595
- toolName: toolNameMapping.toCustomToolName("mcp"),
4787
+ toolCallId,
4788
+ toolName,
4596
4789
  result: {
4597
4790
  type: "call",
4598
4791
  serverLabel: part.server_label,
@@ -4600,58 +4793,34 @@ var OpenAIResponsesLanguageModel = class {
4600
4793
  arguments: part.arguments,
4601
4794
  ...part.output != null ? { output: part.output } : {},
4602
4795
  ...part.error != null ? { error: part.error } : {}
4796
+ },
4797
+ providerMetadata: {
4798
+ [providerKey]: {
4799
+ itemId: part.id
4800
+ }
4603
4801
  }
4604
4802
  });
4605
4803
  break;
4606
4804
  }
4607
4805
  case "mcp_list_tools": {
4608
- content.push({
4609
- type: "tool-call",
4610
- toolCallId: part.id,
4611
- toolName: toolNameMapping.toCustomToolName("mcp"),
4612
- input: JSON.stringify({}),
4613
- providerExecuted: true
4614
- });
4615
- content.push({
4616
- type: "tool-result",
4617
- toolCallId: part.id,
4618
- toolName: toolNameMapping.toCustomToolName("mcp"),
4619
- result: {
4620
- type: "listTools",
4621
- serverLabel: part.server_label,
4622
- tools: part.tools.map((t) => {
4623
- var _a2, _b2;
4624
- return {
4625
- name: t.name,
4626
- description: (_a2 = t.description) != null ? _a2 : void 0,
4627
- inputSchema: t.input_schema,
4628
- annotations: (_b2 = t.annotations) != null ? _b2 : void 0
4629
- };
4630
- }),
4631
- ...part.error != null ? { error: part.error } : {}
4632
- }
4633
- });
4634
4806
  break;
4635
4807
  }
4636
4808
  case "mcp_approval_request": {
4809
+ const approvalRequestId = (_w = part.approval_request_id) != null ? _w : part.id;
4810
+ const dummyToolCallId = (_z = (_y = (_x = this.config).generateId) == null ? void 0 : _y.call(_x)) != null ? _z : generateId2();
4811
+ const toolName = `mcp.${part.name}`;
4637
4812
  content.push({
4638
4813
  type: "tool-call",
4639
- toolCallId: part.id,
4640
- toolName: toolNameMapping.toCustomToolName("mcp"),
4641
- input: JSON.stringify({}),
4642
- providerExecuted: true
4814
+ toolCallId: dummyToolCallId,
4815
+ toolName,
4816
+ input: part.arguments,
4817
+ providerExecuted: true,
4818
+ dynamic: true
4643
4819
  });
4644
4820
  content.push({
4645
- type: "tool-result",
4646
- toolCallId: part.id,
4647
- toolName: toolNameMapping.toCustomToolName("mcp"),
4648
- result: {
4649
- type: "approvalRequest",
4650
- serverLabel: part.server_label,
4651
- name: part.name,
4652
- arguments: part.arguments,
4653
- approvalRequestId: part.approval_request_id
4654
- }
4821
+ type: "tool-approval-request",
4822
+ approvalId: approvalRequestId,
4823
+ toolCallId: dummyToolCallId
4655
4824
  });
4656
4825
  break;
4657
4826
  }
@@ -4688,13 +4857,13 @@ var OpenAIResponsesLanguageModel = class {
4688
4857
  toolName: toolNameMapping.toCustomToolName("file_search"),
4689
4858
  result: {
4690
4859
  queries: part.queries,
4691
- results: (_w = (_v = part.results) == null ? void 0 : _v.map((result) => ({
4860
+ results: (_B = (_A = part.results) == null ? void 0 : _A.map((result) => ({
4692
4861
  attributes: result.attributes,
4693
4862
  fileId: result.file_id,
4694
4863
  filename: result.filename,
4695
4864
  score: result.score,
4696
4865
  text: result.text
4697
- }))) != null ? _w : null
4866
+ }))) != null ? _B : null
4698
4867
  }
4699
4868
  });
4700
4869
  break;
@@ -4751,10 +4920,13 @@ var OpenAIResponsesLanguageModel = class {
4751
4920
  const usage = response.usage;
4752
4921
  return {
4753
4922
  content,
4754
- finishReason: mapOpenAIResponseFinishReason({
4755
- finishReason: (_x = response.incomplete_details) == null ? void 0 : _x.reason,
4756
- hasFunctionCall
4757
- }),
4923
+ finishReason: {
4924
+ unified: mapOpenAIResponseFinishReason({
4925
+ finishReason: (_C = response.incomplete_details) == null ? void 0 : _C.reason,
4926
+ hasFunctionCall
4927
+ }),
4928
+ raw: (_E = (_D = response.incomplete_details) == null ? void 0 : _D.reason) != null ? _E : void 0
4929
+ },
4758
4930
  usage: convertOpenAIResponsesUsage(usage),
4759
4931
  request: { body },
4760
4932
  response: {
@@ -4795,7 +4967,12 @@ var OpenAIResponsesLanguageModel = class {
4795
4967
  });
4796
4968
  const self = this;
4797
4969
  const providerKey = this.config.provider.replace(".responses", "");
4798
- let finishReason = "unknown";
4970
+ const approvalRequestIdToDummyToolCallIdFromPrompt = extractApprovalRequestIdToToolCallIdMapping(options.prompt);
4971
+ const approvalRequestIdToDummyToolCallIdFromStream = /* @__PURE__ */ new Map();
4972
+ let finishReason = {
4973
+ unified: "other",
4974
+ raw: void 0
4975
+ };
4799
4976
  let usage = void 0;
4800
4977
  const logprobs = [];
4801
4978
  let responseId = null;
@@ -4811,12 +4988,12 @@ var OpenAIResponsesLanguageModel = class {
4811
4988
  controller.enqueue({ type: "stream-start", warnings });
4812
4989
  },
4813
4990
  transform(chunk, controller) {
4814
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A;
4991
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B, _C, _D, _E, _F, _G, _H, _I, _J;
4815
4992
  if (options.includeRawChunks) {
4816
4993
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
4817
4994
  }
4818
4995
  if (!chunk.success) {
4819
- finishReason = "error";
4996
+ finishReason = { unified: "error", raw: void 0 };
4820
4997
  controller.enqueue({ type: "error", error: chunk.error });
4821
4998
  return;
4822
4999
  }
@@ -4907,32 +5084,41 @@ var OpenAIResponsesLanguageModel = class {
4907
5084
  providerExecuted: true
4908
5085
  });
4909
5086
  } else if (value.item.type === "mcp_call" || value.item.type === "mcp_list_tools" || value.item.type === "mcp_approval_request") {
4910
- controller.enqueue({
4911
- type: "tool-call",
4912
- toolCallId: value.item.id,
4913
- toolName: toolNameMapping.toCustomToolName("mcp"),
4914
- input: "{}",
4915
- providerExecuted: true
4916
- });
4917
5087
  } else if (value.item.type === "apply_patch_call") {
5088
+ const { call_id: callId, operation } = value.item;
4918
5089
  ongoingToolCalls[value.output_index] = {
4919
5090
  toolName: toolNameMapping.toCustomToolName("apply_patch"),
4920
- toolCallId: value.item.call_id
5091
+ toolCallId: callId,
5092
+ applyPatch: {
5093
+ // delete_file doesn't have diff
5094
+ hasDiff: operation.type === "delete_file",
5095
+ endEmitted: operation.type === "delete_file"
5096
+ }
4921
5097
  };
4922
- if (value.item.status === "completed") {
5098
+ controller.enqueue({
5099
+ type: "tool-input-start",
5100
+ id: callId,
5101
+ toolName: toolNameMapping.toCustomToolName("apply_patch")
5102
+ });
5103
+ if (operation.type === "delete_file") {
5104
+ const inputString = JSON.stringify({
5105
+ callId,
5106
+ operation
5107
+ });
4923
5108
  controller.enqueue({
4924
- type: "tool-call",
4925
- toolCallId: value.item.call_id,
4926
- toolName: toolNameMapping.toCustomToolName("apply_patch"),
4927
- input: JSON.stringify({
4928
- callId: value.item.call_id,
4929
- operation: value.item.operation
4930
- }),
4931
- providerMetadata: {
4932
- [providerKey]: {
4933
- itemId: value.item.id
4934
- }
4935
- }
5109
+ type: "tool-input-delta",
5110
+ id: callId,
5111
+ delta: inputString
5112
+ });
5113
+ controller.enqueue({
5114
+ type: "tool-input-end",
5115
+ id: callId
5116
+ });
5117
+ } else {
5118
+ controller.enqueue({
5119
+ type: "tool-input-delta",
5120
+ id: callId,
5121
+ delta: `{"callId":"${escapeJSONDelta(callId)}","operation":{"type":"${escapeJSONDelta(operation.type)}","path":"${escapeJSONDelta(operation.path)}","diff":"`
4936
5122
  });
4937
5123
  }
4938
5124
  } else if (value.item.type === "shell_call") {
@@ -5069,10 +5255,23 @@ var OpenAIResponsesLanguageModel = class {
5069
5255
  });
5070
5256
  } else if (value.item.type === "mcp_call") {
5071
5257
  ongoingToolCalls[value.output_index] = void 0;
5258
+ const approvalRequestId = (_d = value.item.approval_request_id) != null ? _d : void 0;
5259
+ const aliasedToolCallId = approvalRequestId != null ? (_f = (_e = approvalRequestIdToDummyToolCallIdFromStream.get(
5260
+ approvalRequestId
5261
+ )) != null ? _e : approvalRequestIdToDummyToolCallIdFromPrompt[approvalRequestId]) != null ? _f : value.item.id : value.item.id;
5262
+ const toolName = `mcp.${value.item.name}`;
5263
+ controller.enqueue({
5264
+ type: "tool-call",
5265
+ toolCallId: aliasedToolCallId,
5266
+ toolName,
5267
+ input: value.item.arguments,
5268
+ providerExecuted: true,
5269
+ dynamic: true
5270
+ });
5072
5271
  controller.enqueue({
5073
5272
  type: "tool-result",
5074
- toolCallId: value.item.id,
5075
- toolName: toolNameMapping.toCustomToolName("mcp"),
5273
+ toolCallId: aliasedToolCallId,
5274
+ toolName,
5076
5275
  result: {
5077
5276
  type: "call",
5078
5277
  serverLabel: value.item.server_label,
@@ -5080,35 +5279,40 @@ var OpenAIResponsesLanguageModel = class {
5080
5279
  arguments: value.item.arguments,
5081
5280
  ...value.item.output != null ? { output: value.item.output } : {},
5082
5281
  ...value.item.error != null ? { error: value.item.error } : {}
5282
+ },
5283
+ providerMetadata: {
5284
+ [providerKey]: {
5285
+ itemId: value.item.id
5286
+ }
5083
5287
  }
5084
5288
  });
5085
5289
  } else if (value.item.type === "mcp_list_tools") {
5086
5290
  ongoingToolCalls[value.output_index] = void 0;
5087
- controller.enqueue({
5088
- type: "tool-result",
5089
- toolCallId: value.item.id,
5090
- toolName: toolNameMapping.toCustomToolName("mcp"),
5091
- result: {
5092
- type: "listTools",
5093
- serverLabel: value.item.server_label,
5094
- tools: value.item.tools.map((t) => {
5095
- var _a2, _b2;
5096
- return {
5097
- name: t.name,
5098
- description: (_a2 = t.description) != null ? _a2 : void 0,
5099
- inputSchema: t.input_schema,
5100
- annotations: (_b2 = t.annotations) != null ? _b2 : void 0
5101
- };
5102
- }),
5103
- ...value.item.error != null ? { error: value.item.error } : {}
5104
- }
5105
- });
5106
5291
  } else if (value.item.type === "apply_patch_call") {
5107
- ongoingToolCalls[value.output_index] = void 0;
5108
- if (value.item.status === "completed") {
5292
+ const toolCall = ongoingToolCalls[value.output_index];
5293
+ if ((toolCall == null ? void 0 : toolCall.applyPatch) && !toolCall.applyPatch.endEmitted && value.item.operation.type !== "delete_file") {
5294
+ if (!toolCall.applyPatch.hasDiff) {
5295
+ controller.enqueue({
5296
+ type: "tool-input-delta",
5297
+ id: toolCall.toolCallId,
5298
+ delta: escapeJSONDelta(value.item.operation.diff)
5299
+ });
5300
+ }
5301
+ controller.enqueue({
5302
+ type: "tool-input-delta",
5303
+ id: toolCall.toolCallId,
5304
+ delta: '"}}'
5305
+ });
5306
+ controller.enqueue({
5307
+ type: "tool-input-end",
5308
+ id: toolCall.toolCallId
5309
+ });
5310
+ toolCall.applyPatch.endEmitted = true;
5311
+ }
5312
+ if (toolCall && value.item.status === "completed") {
5109
5313
  controller.enqueue({
5110
5314
  type: "tool-call",
5111
- toolCallId: value.item.call_id,
5315
+ toolCallId: toolCall.toolCallId,
5112
5316
  toolName: toolNameMapping.toCustomToolName("apply_patch"),
5113
5317
  input: JSON.stringify({
5114
5318
  callId: value.item.call_id,
@@ -5121,19 +5325,28 @@ var OpenAIResponsesLanguageModel = class {
5121
5325
  }
5122
5326
  });
5123
5327
  }
5328
+ ongoingToolCalls[value.output_index] = void 0;
5124
5329
  } else if (value.item.type === "mcp_approval_request") {
5125
5330
  ongoingToolCalls[value.output_index] = void 0;
5331
+ const dummyToolCallId = (_i = (_h = (_g = self.config).generateId) == null ? void 0 : _h.call(_g)) != null ? _i : generateId2();
5332
+ const approvalRequestId = (_j = value.item.approval_request_id) != null ? _j : value.item.id;
5333
+ approvalRequestIdToDummyToolCallIdFromStream.set(
5334
+ approvalRequestId,
5335
+ dummyToolCallId
5336
+ );
5337
+ const toolName = `mcp.${value.item.name}`;
5126
5338
  controller.enqueue({
5127
- type: "tool-result",
5128
- toolCallId: value.item.id,
5129
- toolName: toolNameMapping.toCustomToolName("mcp"),
5130
- result: {
5131
- type: "approvalRequest",
5132
- serverLabel: value.item.server_label,
5133
- name: value.item.name,
5134
- arguments: value.item.arguments,
5135
- approvalRequestId: value.item.approval_request_id
5136
- }
5339
+ type: "tool-call",
5340
+ toolCallId: dummyToolCallId,
5341
+ toolName,
5342
+ input: value.item.arguments,
5343
+ providerExecuted: true,
5344
+ dynamic: true
5345
+ });
5346
+ controller.enqueue({
5347
+ type: "tool-approval-request",
5348
+ approvalId: approvalRequestId,
5349
+ toolCallId: dummyToolCallId
5137
5350
  });
5138
5351
  } else if (value.item.type === "local_shell_call") {
5139
5352
  ongoingToolCalls[value.output_index] = void 0;
@@ -5184,7 +5397,7 @@ var OpenAIResponsesLanguageModel = class {
5184
5397
  providerMetadata: {
5185
5398
  [providerKey]: {
5186
5399
  itemId: value.item.id,
5187
- reasoningEncryptedContent: (_d = value.item.encrypted_content) != null ? _d : null
5400
+ reasoningEncryptedContent: (_k = value.item.encrypted_content) != null ? _k : null
5188
5401
  }
5189
5402
  }
5190
5403
  });
@@ -5200,6 +5413,38 @@ var OpenAIResponsesLanguageModel = class {
5200
5413
  delta: value.delta
5201
5414
  });
5202
5415
  }
5416
+ } else if (isResponseApplyPatchCallOperationDiffDeltaChunk(value)) {
5417
+ const toolCall = ongoingToolCalls[value.output_index];
5418
+ if (toolCall == null ? void 0 : toolCall.applyPatch) {
5419
+ controller.enqueue({
5420
+ type: "tool-input-delta",
5421
+ id: toolCall.toolCallId,
5422
+ delta: escapeJSONDelta(value.delta)
5423
+ });
5424
+ toolCall.applyPatch.hasDiff = true;
5425
+ }
5426
+ } else if (isResponseApplyPatchCallOperationDiffDoneChunk(value)) {
5427
+ const toolCall = ongoingToolCalls[value.output_index];
5428
+ if ((toolCall == null ? void 0 : toolCall.applyPatch) && !toolCall.applyPatch.endEmitted) {
5429
+ if (!toolCall.applyPatch.hasDiff) {
5430
+ controller.enqueue({
5431
+ type: "tool-input-delta",
5432
+ id: toolCall.toolCallId,
5433
+ delta: escapeJSONDelta(value.diff)
5434
+ });
5435
+ toolCall.applyPatch.hasDiff = true;
5436
+ }
5437
+ controller.enqueue({
5438
+ type: "tool-input-delta",
5439
+ id: toolCall.toolCallId,
5440
+ delta: '"}}'
5441
+ });
5442
+ controller.enqueue({
5443
+ type: "tool-input-end",
5444
+ id: toolCall.toolCallId
5445
+ });
5446
+ toolCall.applyPatch.endEmitted = true;
5447
+ }
5203
5448
  } else if (isResponseImageGenerationCallPartialImageChunk(value)) {
5204
5449
  controller.enqueue({
5205
5450
  type: "tool-result",
@@ -5216,9 +5461,7 @@ var OpenAIResponsesLanguageModel = class {
5216
5461
  controller.enqueue({
5217
5462
  type: "tool-input-delta",
5218
5463
  id: toolCall.toolCallId,
5219
- // The delta is code, which is embedding in a JSON string.
5220
- // To escape it, we use JSON.stringify and slice to remove the outer quotes.
5221
- delta: JSON.stringify(value.delta).slice(1, -1)
5464
+ delta: escapeJSONDelta(value.delta)
5222
5465
  });
5223
5466
  }
5224
5467
  } else if (isResponseCodeInterpreterCallCodeDoneChunk(value)) {
@@ -5258,7 +5501,7 @@ var OpenAIResponsesLanguageModel = class {
5258
5501
  id: value.item_id,
5259
5502
  delta: value.delta
5260
5503
  });
5261
- if (((_f = (_e = options.providerOptions) == null ? void 0 : _e.openai) == null ? void 0 : _f.logprobs) && value.logprobs) {
5504
+ if (((_m = (_l = options.providerOptions) == null ? void 0 : _l.openai) == null ? void 0 : _m.logprobs) && value.logprobs) {
5262
5505
  logprobs.push(value.logprobs);
5263
5506
  }
5264
5507
  } else if (value.type === "response.reasoning_summary_part.added") {
@@ -5285,7 +5528,7 @@ var OpenAIResponsesLanguageModel = class {
5285
5528
  providerMetadata: {
5286
5529
  [providerKey]: {
5287
5530
  itemId: value.item_id,
5288
- reasoningEncryptedContent: (_h = (_g = activeReasoning[value.item_id]) == null ? void 0 : _g.encryptedContent) != null ? _h : null
5531
+ reasoningEncryptedContent: (_o = (_n = activeReasoning[value.item_id]) == null ? void 0 : _n.encryptedContent) != null ? _o : null
5289
5532
  }
5290
5533
  }
5291
5534
  });
@@ -5315,10 +5558,13 @@ var OpenAIResponsesLanguageModel = class {
5315
5558
  activeReasoning[value.item_id].summaryParts[value.summary_index] = "can-conclude";
5316
5559
  }
5317
5560
  } else if (isResponseFinishedChunk(value)) {
5318
- finishReason = mapOpenAIResponseFinishReason({
5319
- finishReason: (_i = value.response.incomplete_details) == null ? void 0 : _i.reason,
5320
- hasFunctionCall
5321
- });
5561
+ finishReason = {
5562
+ unified: mapOpenAIResponseFinishReason({
5563
+ finishReason: (_p = value.response.incomplete_details) == null ? void 0 : _p.reason,
5564
+ hasFunctionCall
5565
+ }),
5566
+ raw: (_r = (_q = value.response.incomplete_details) == null ? void 0 : _q.reason) != null ? _r : void 0
5567
+ };
5322
5568
  usage = value.response.usage;
5323
5569
  if (typeof value.response.service_tier === "string") {
5324
5570
  serviceTier = value.response.service_tier;
@@ -5329,7 +5575,7 @@ var OpenAIResponsesLanguageModel = class {
5329
5575
  controller.enqueue({
5330
5576
  type: "source",
5331
5577
  sourceType: "url",
5332
- id: (_l = (_k = (_j = self.config).generateId) == null ? void 0 : _k.call(_j)) != null ? _l : generateId2(),
5578
+ id: (_u = (_t = (_s = self.config).generateId) == null ? void 0 : _t.call(_s)) != null ? _u : generateId2(),
5333
5579
  url: value.annotation.url,
5334
5580
  title: value.annotation.title
5335
5581
  });
@@ -5337,10 +5583,10 @@ var OpenAIResponsesLanguageModel = class {
5337
5583
  controller.enqueue({
5338
5584
  type: "source",
5339
5585
  sourceType: "document",
5340
- id: (_o = (_n = (_m = self.config).generateId) == null ? void 0 : _n.call(_m)) != null ? _o : generateId2(),
5586
+ id: (_x = (_w = (_v = self.config).generateId) == null ? void 0 : _w.call(_v)) != null ? _x : generateId2(),
5341
5587
  mediaType: "text/plain",
5342
- title: (_q = (_p = value.annotation.quote) != null ? _p : value.annotation.filename) != null ? _q : "Document",
5343
- filename: (_r = value.annotation.filename) != null ? _r : value.annotation.file_id,
5588
+ title: (_z = (_y = value.annotation.quote) != null ? _y : value.annotation.filename) != null ? _z : "Document",
5589
+ filename: (_A = value.annotation.filename) != null ? _A : value.annotation.file_id,
5344
5590
  ...value.annotation.file_id ? {
5345
5591
  providerMetadata: {
5346
5592
  [providerKey]: {
@@ -5353,10 +5599,10 @@ var OpenAIResponsesLanguageModel = class {
5353
5599
  controller.enqueue({
5354
5600
  type: "source",
5355
5601
  sourceType: "document",
5356
- id: (_u = (_t = (_s = self.config).generateId) == null ? void 0 : _t.call(_s)) != null ? _u : generateId2(),
5602
+ id: (_D = (_C = (_B = self.config).generateId) == null ? void 0 : _C.call(_B)) != null ? _D : generateId2(),
5357
5603
  mediaType: "text/plain",
5358
- title: (_w = (_v = value.annotation.filename) != null ? _v : value.annotation.file_id) != null ? _w : "Document",
5359
- filename: (_x = value.annotation.filename) != null ? _x : value.annotation.file_id,
5604
+ title: (_F = (_E = value.annotation.filename) != null ? _E : value.annotation.file_id) != null ? _F : "Document",
5605
+ filename: (_G = value.annotation.filename) != null ? _G : value.annotation.file_id,
5360
5606
  providerMetadata: {
5361
5607
  [providerKey]: {
5362
5608
  fileId: value.annotation.file_id,
@@ -5369,7 +5615,7 @@ var OpenAIResponsesLanguageModel = class {
5369
5615
  controller.enqueue({
5370
5616
  type: "source",
5371
5617
  sourceType: "document",
5372
- id: (_A = (_z = (_y = self.config).generateId) == null ? void 0 : _z.call(_y)) != null ? _A : generateId2(),
5618
+ id: (_J = (_I = (_H = self.config).generateId) == null ? void 0 : _I.call(_H)) != null ? _J : generateId2(),
5373
5619
  mediaType: "application/octet-stream",
5374
5620
  title: value.annotation.file_id,
5375
5621
  filename: value.annotation.file_id,
@@ -5435,6 +5681,12 @@ function isResponseCodeInterpreterCallCodeDeltaChunk(chunk) {
5435
5681
  function isResponseCodeInterpreterCallCodeDoneChunk(chunk) {
5436
5682
  return chunk.type === "response.code_interpreter_call_code.done";
5437
5683
  }
5684
+ function isResponseApplyPatchCallOperationDiffDeltaChunk(chunk) {
5685
+ return chunk.type === "response.apply_patch_call_operation_diff.delta";
5686
+ }
5687
+ function isResponseApplyPatchCallOperationDiffDoneChunk(chunk) {
5688
+ return chunk.type === "response.apply_patch_call_operation_diff.done";
5689
+ }
5438
5690
  function isResponseOutputItemAddedChunk(chunk) {
5439
5691
  return chunk.type === "response.output_item.added";
5440
5692
  }
@@ -5465,6 +5717,9 @@ function mapWebSearchOutput(action) {
5465
5717
  };
5466
5718
  }
5467
5719
  }
5720
+ function escapeJSONDelta(delta) {
5721
+ return JSON.stringify(delta).slice(1, -1);
5722
+ }
5468
5723
  export {
5469
5724
  OpenAIChatLanguageModel,
5470
5725
  OpenAICompletionLanguageModel,