@ai-sdk/openai 3.0.0-beta.109 → 3.0.0-beta.111

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.
@@ -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) {
@@ -2710,6 +2713,9 @@ async function convertToOpenAIResponsesInput({
2710
2713
  }
2711
2714
  case "tool": {
2712
2715
  for (const part of content) {
2716
+ if (part.type === "tool-approval-response") {
2717
+ continue;
2718
+ }
2713
2719
  const output = part.output;
2714
2720
  const resolvedToolName = toolNameMapping.toProviderToolName(
2715
2721
  part.toolName
@@ -3236,6 +3242,19 @@ var openaiResponsesChunkSchema = lazySchema14(
3236
3242
  item_id: z16.string(),
3237
3243
  summary_index: z16.number()
3238
3244
  }),
3245
+ z16.object({
3246
+ type: z16.literal("response.apply_patch_call_operation_diff.delta"),
3247
+ item_id: z16.string(),
3248
+ output_index: z16.number(),
3249
+ delta: z16.string(),
3250
+ obfuscation: z16.string().nullish()
3251
+ }),
3252
+ z16.object({
3253
+ type: z16.literal("response.apply_patch_call_operation_diff.done"),
3254
+ item_id: z16.string(),
3255
+ output_index: z16.number(),
3256
+ diff: z16.string()
3257
+ }),
3239
3258
  z16.object({
3240
3259
  type: z16.literal("error"),
3241
3260
  sequence_number: z16.number(),
@@ -3917,16 +3936,14 @@ var mcpArgsSchema = lazySchema19(
3917
3936
  authorization: z21.string().optional(),
3918
3937
  connectorId: z21.string().optional(),
3919
3938
  headers: z21.record(z21.string(), z21.string()).optional(),
3920
- // TODO: Integrate this MCP tool approval with our SDK's existing tool approval architecture
3921
- // requireApproval: z
3922
- // .union([
3923
- // z.enum(['always', 'never']),
3924
- // z.object({
3925
- // readOnly: z.boolean().optional(),
3926
- // toolNames: z.array(z.string()).optional(),
3927
- // }),
3928
- // ])
3929
- // .optional(),
3939
+ requireApproval: z21.union([
3940
+ z21.enum(["always", "never"]),
3941
+ z21.object({
3942
+ never: z21.object({
3943
+ toolNames: z21.array(z21.string()).optional()
3944
+ }).optional()
3945
+ })
3946
+ ]).optional(),
3930
3947
  serverDescription: z21.string().optional(),
3931
3948
  serverUrl: z21.string().optional()
3932
3949
  }).refine(
@@ -3938,36 +3955,14 @@ var mcpArgsSchema = lazySchema19(
3938
3955
  var mcpInputSchema = lazySchema19(() => zodSchema19(z21.object({})));
3939
3956
  var mcpOutputSchema = lazySchema19(
3940
3957
  () => zodSchema19(
3941
- z21.discriminatedUnion("type", [
3942
- z21.object({
3943
- type: z21.literal("call"),
3944
- serverLabel: z21.string(),
3945
- name: z21.string(),
3946
- arguments: z21.string(),
3947
- output: z21.string().nullable().optional(),
3948
- error: z21.union([z21.string(), jsonValueSchema]).optional()
3949
- }),
3950
- z21.object({
3951
- type: z21.literal("listTools"),
3952
- serverLabel: z21.string(),
3953
- tools: z21.array(
3954
- z21.object({
3955
- name: z21.string(),
3956
- description: z21.string().optional(),
3957
- inputSchema: jsonValueSchema,
3958
- annotations: z21.record(z21.string(), jsonValueSchema).optional()
3959
- })
3960
- ),
3961
- error: z21.union([z21.string(), jsonValueSchema]).optional()
3962
- }),
3963
- z21.object({
3964
- type: z21.literal("approvalRequest"),
3965
- serverLabel: z21.string(),
3966
- name: z21.string(),
3967
- arguments: z21.string(),
3968
- approvalRequestId: z21.string()
3969
- })
3970
- ])
3958
+ z21.object({
3959
+ type: z21.literal("call"),
3960
+ serverLabel: z21.string(),
3961
+ name: z21.string(),
3962
+ arguments: z21.string(),
3963
+ output: z21.string().nullable().optional(),
3964
+ error: z21.union([z21.string(), jsonValueSchema]).optional()
3965
+ })
3971
3966
  )
3972
3967
  );
3973
3968
  var mcpToolFactory = createProviderToolFactoryWithOutputSchema7({
@@ -4746,54 +4741,9 @@ var OpenAIResponsesLanguageModel = class {
4746
4741
  break;
4747
4742
  }
4748
4743
  case "mcp_list_tools": {
4749
- content.push({
4750
- type: "tool-call",
4751
- toolCallId: part.id,
4752
- toolName: toolNameMapping.toCustomToolName("mcp"),
4753
- input: JSON.stringify({}),
4754
- providerExecuted: true
4755
- });
4756
- content.push({
4757
- type: "tool-result",
4758
- toolCallId: part.id,
4759
- toolName: toolNameMapping.toCustomToolName("mcp"),
4760
- result: {
4761
- type: "listTools",
4762
- serverLabel: part.server_label,
4763
- tools: part.tools.map((t) => {
4764
- var _a2, _b2;
4765
- return {
4766
- name: t.name,
4767
- description: (_a2 = t.description) != null ? _a2 : void 0,
4768
- inputSchema: t.input_schema,
4769
- annotations: (_b2 = t.annotations) != null ? _b2 : void 0
4770
- };
4771
- }),
4772
- ...part.error != null ? { error: part.error } : {}
4773
- }
4774
- });
4775
4744
  break;
4776
4745
  }
4777
4746
  case "mcp_approval_request": {
4778
- content.push({
4779
- type: "tool-call",
4780
- toolCallId: part.id,
4781
- toolName: toolNameMapping.toCustomToolName("mcp"),
4782
- input: JSON.stringify({}),
4783
- providerExecuted: true
4784
- });
4785
- content.push({
4786
- type: "tool-result",
4787
- toolCallId: part.id,
4788
- toolName: toolNameMapping.toCustomToolName("mcp"),
4789
- result: {
4790
- type: "approvalRequest",
4791
- serverLabel: part.server_label,
4792
- name: part.name,
4793
- arguments: part.arguments,
4794
- approvalRequestId: part.approval_request_id
4795
- }
4796
- });
4797
4747
  break;
4798
4748
  }
4799
4749
  case "computer_call": {
@@ -5062,24 +5012,40 @@ var OpenAIResponsesLanguageModel = class {
5062
5012
  providerExecuted: true
5063
5013
  });
5064
5014
  } else if (value.item.type === "apply_patch_call") {
5015
+ const { call_id: callId, operation } = value.item;
5065
5016
  ongoingToolCalls[value.output_index] = {
5066
5017
  toolName: toolNameMapping.toCustomToolName("apply_patch"),
5067
- toolCallId: value.item.call_id
5018
+ toolCallId: callId,
5019
+ applyPatch: {
5020
+ // delete_file doesn't have diff
5021
+ hasDiff: operation.type === "delete_file",
5022
+ endEmitted: operation.type === "delete_file"
5023
+ }
5068
5024
  };
5069
- if (value.item.status === "completed") {
5025
+ controller.enqueue({
5026
+ type: "tool-input-start",
5027
+ id: callId,
5028
+ toolName: toolNameMapping.toCustomToolName("apply_patch")
5029
+ });
5030
+ if (operation.type === "delete_file") {
5031
+ const inputString = JSON.stringify({
5032
+ callId,
5033
+ operation
5034
+ });
5070
5035
  controller.enqueue({
5071
- type: "tool-call",
5072
- toolCallId: value.item.call_id,
5073
- toolName: toolNameMapping.toCustomToolName("apply_patch"),
5074
- input: JSON.stringify({
5075
- callId: value.item.call_id,
5076
- operation: value.item.operation
5077
- }),
5078
- providerMetadata: {
5079
- [providerKey]: {
5080
- itemId: value.item.id
5081
- }
5082
- }
5036
+ type: "tool-input-delta",
5037
+ id: callId,
5038
+ delta: inputString
5039
+ });
5040
+ controller.enqueue({
5041
+ type: "tool-input-end",
5042
+ id: callId
5043
+ });
5044
+ } else {
5045
+ controller.enqueue({
5046
+ type: "tool-input-delta",
5047
+ id: callId,
5048
+ delta: `{"callId":"${escapeJSONDelta(callId)}","operation":{"type":"${escapeJSONDelta(operation.type)}","path":"${escapeJSONDelta(operation.path)}","diff":"`
5083
5049
  });
5084
5050
  }
5085
5051
  } else if (value.item.type === "shell_call") {
@@ -5231,31 +5197,31 @@ var OpenAIResponsesLanguageModel = class {
5231
5197
  });
5232
5198
  } else if (value.item.type === "mcp_list_tools") {
5233
5199
  ongoingToolCalls[value.output_index] = void 0;
5234
- controller.enqueue({
5235
- type: "tool-result",
5236
- toolCallId: value.item.id,
5237
- toolName: toolNameMapping.toCustomToolName("mcp"),
5238
- result: {
5239
- type: "listTools",
5240
- serverLabel: value.item.server_label,
5241
- tools: value.item.tools.map((t) => {
5242
- var _a2, _b2;
5243
- return {
5244
- name: t.name,
5245
- description: (_a2 = t.description) != null ? _a2 : void 0,
5246
- inputSchema: t.input_schema,
5247
- annotations: (_b2 = t.annotations) != null ? _b2 : void 0
5248
- };
5249
- }),
5250
- ...value.item.error != null ? { error: value.item.error } : {}
5251
- }
5252
- });
5253
5200
  } else if (value.item.type === "apply_patch_call") {
5254
- ongoingToolCalls[value.output_index] = void 0;
5255
- if (value.item.status === "completed") {
5201
+ const toolCall = ongoingToolCalls[value.output_index];
5202
+ if ((toolCall == null ? void 0 : toolCall.applyPatch) && !toolCall.applyPatch.endEmitted && value.item.operation.type !== "delete_file") {
5203
+ if (!toolCall.applyPatch.hasDiff) {
5204
+ controller.enqueue({
5205
+ type: "tool-input-delta",
5206
+ id: toolCall.toolCallId,
5207
+ delta: escapeJSONDelta(value.item.operation.diff)
5208
+ });
5209
+ }
5210
+ controller.enqueue({
5211
+ type: "tool-input-delta",
5212
+ id: toolCall.toolCallId,
5213
+ delta: '"}}'
5214
+ });
5215
+ controller.enqueue({
5216
+ type: "tool-input-end",
5217
+ id: toolCall.toolCallId
5218
+ });
5219
+ toolCall.applyPatch.endEmitted = true;
5220
+ }
5221
+ if (toolCall && value.item.status === "completed") {
5256
5222
  controller.enqueue({
5257
5223
  type: "tool-call",
5258
- toolCallId: value.item.call_id,
5224
+ toolCallId: toolCall.toolCallId,
5259
5225
  toolName: toolNameMapping.toCustomToolName("apply_patch"),
5260
5226
  input: JSON.stringify({
5261
5227
  callId: value.item.call_id,
@@ -5268,20 +5234,9 @@ var OpenAIResponsesLanguageModel = class {
5268
5234
  }
5269
5235
  });
5270
5236
  }
5237
+ ongoingToolCalls[value.output_index] = void 0;
5271
5238
  } else if (value.item.type === "mcp_approval_request") {
5272
5239
  ongoingToolCalls[value.output_index] = void 0;
5273
- controller.enqueue({
5274
- type: "tool-result",
5275
- toolCallId: value.item.id,
5276
- toolName: toolNameMapping.toCustomToolName("mcp"),
5277
- result: {
5278
- type: "approvalRequest",
5279
- serverLabel: value.item.server_label,
5280
- name: value.item.name,
5281
- arguments: value.item.arguments,
5282
- approvalRequestId: value.item.approval_request_id
5283
- }
5284
- });
5285
5240
  } else if (value.item.type === "local_shell_call") {
5286
5241
  ongoingToolCalls[value.output_index] = void 0;
5287
5242
  controller.enqueue({
@@ -5347,6 +5302,38 @@ var OpenAIResponsesLanguageModel = class {
5347
5302
  delta: value.delta
5348
5303
  });
5349
5304
  }
5305
+ } else if (isResponseApplyPatchCallOperationDiffDeltaChunk(value)) {
5306
+ const toolCall = ongoingToolCalls[value.output_index];
5307
+ if (toolCall == null ? void 0 : toolCall.applyPatch) {
5308
+ controller.enqueue({
5309
+ type: "tool-input-delta",
5310
+ id: toolCall.toolCallId,
5311
+ delta: escapeJSONDelta(value.delta)
5312
+ });
5313
+ toolCall.applyPatch.hasDiff = true;
5314
+ }
5315
+ } else if (isResponseApplyPatchCallOperationDiffDoneChunk(value)) {
5316
+ const toolCall = ongoingToolCalls[value.output_index];
5317
+ if ((toolCall == null ? void 0 : toolCall.applyPatch) && !toolCall.applyPatch.endEmitted) {
5318
+ if (!toolCall.applyPatch.hasDiff) {
5319
+ controller.enqueue({
5320
+ type: "tool-input-delta",
5321
+ id: toolCall.toolCallId,
5322
+ delta: escapeJSONDelta(value.diff)
5323
+ });
5324
+ toolCall.applyPatch.hasDiff = true;
5325
+ }
5326
+ controller.enqueue({
5327
+ type: "tool-input-delta",
5328
+ id: toolCall.toolCallId,
5329
+ delta: '"}}'
5330
+ });
5331
+ controller.enqueue({
5332
+ type: "tool-input-end",
5333
+ id: toolCall.toolCallId
5334
+ });
5335
+ toolCall.applyPatch.endEmitted = true;
5336
+ }
5350
5337
  } else if (isResponseImageGenerationCallPartialImageChunk(value)) {
5351
5338
  controller.enqueue({
5352
5339
  type: "tool-result",
@@ -5363,9 +5350,7 @@ var OpenAIResponsesLanguageModel = class {
5363
5350
  controller.enqueue({
5364
5351
  type: "tool-input-delta",
5365
5352
  id: toolCall.toolCallId,
5366
- // The delta is code, which is embedding in a JSON string.
5367
- // To escape it, we use JSON.stringify and slice to remove the outer quotes.
5368
- delta: JSON.stringify(value.delta).slice(1, -1)
5353
+ delta: escapeJSONDelta(value.delta)
5369
5354
  });
5370
5355
  }
5371
5356
  } else if (isResponseCodeInterpreterCallCodeDoneChunk(value)) {
@@ -5585,6 +5570,12 @@ function isResponseCodeInterpreterCallCodeDeltaChunk(chunk) {
5585
5570
  function isResponseCodeInterpreterCallCodeDoneChunk(chunk) {
5586
5571
  return chunk.type === "response.code_interpreter_call_code.done";
5587
5572
  }
5573
+ function isResponseApplyPatchCallOperationDiffDeltaChunk(chunk) {
5574
+ return chunk.type === "response.apply_patch_call_operation_diff.delta";
5575
+ }
5576
+ function isResponseApplyPatchCallOperationDiffDoneChunk(chunk) {
5577
+ return chunk.type === "response.apply_patch_call_operation_diff.done";
5578
+ }
5588
5579
  function isResponseOutputItemAddedChunk(chunk) {
5589
5580
  return chunk.type === "response.output_item.added";
5590
5581
  }
@@ -5615,6 +5606,9 @@ function mapWebSearchOutput(action) {
5615
5606
  };
5616
5607
  }
5617
5608
  }
5609
+ function escapeJSONDelta(delta) {
5610
+ return JSON.stringify(delta).slice(1, -1);
5611
+ }
5618
5612
  export {
5619
5613
  OpenAIChatLanguageModel,
5620
5614
  OpenAICompletionLanguageModel,