ai 6.0.0-beta.152 → 6.0.0-beta.154

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.0-beta.154
4
+
5
+ ### Patch Changes
6
+
7
+ - 32223c8: feat: add toolCallId arg to toModelOutput
8
+ - Updated dependencies [32223c8]
9
+ - @ai-sdk/provider-utils@4.0.0-beta.50
10
+ - @ai-sdk/gateway@2.0.0-beta.81
11
+
12
+ ## 6.0.0-beta.153
13
+
14
+ ### Patch Changes
15
+
16
+ - 83e5744: feat: support async Tool.toModelOutput
17
+ - Updated dependencies [83e5744]
18
+ - @ai-sdk/provider-utils@4.0.0-beta.49
19
+ - @ai-sdk/gateway@2.0.0-beta.80
20
+
3
21
  ## 6.0.0-beta.152
4
22
 
5
23
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -3317,7 +3317,7 @@ declare function convertToModelMessages<UI_MESSAGE extends UIMessage>(messages:
3317
3317
  tools?: ToolSet;
3318
3318
  ignoreIncompleteToolCalls?: boolean;
3319
3319
  convertDataPart?: (part: DataUIPart<InferUIMessageData<UI_MESSAGE>>) => TextPart | FilePart | undefined;
3320
- }): ModelMessage[];
3320
+ }): Promise<ModelMessage[]>;
3321
3321
 
3322
3322
  type PrepareSendMessagesRequest<UI_MESSAGE extends UIMessage> = (options: {
3323
3323
  id: string;
package/dist/index.d.ts CHANGED
@@ -3317,7 +3317,7 @@ declare function convertToModelMessages<UI_MESSAGE extends UIMessage>(messages:
3317
3317
  tools?: ToolSet;
3318
3318
  ignoreIncompleteToolCalls?: boolean;
3319
3319
  convertDataPart?: (part: DataUIPart<InferUIMessageData<UI_MESSAGE>>) => TextPart | FilePart | undefined;
3320
- }): ModelMessage[];
3320
+ }): Promise<ModelMessage[]>;
3321
3321
 
3322
3322
  type PrepareSendMessagesRequest<UI_MESSAGE extends UIMessage> = (options: {
3323
3323
  id: string;
package/dist/index.js CHANGED
@@ -964,7 +964,7 @@ function detectMediaType({
964
964
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
965
965
 
966
966
  // src/version.ts
967
- var VERSION = true ? "6.0.0-beta.152" : "0.0.0-test";
967
+ var VERSION = true ? "6.0.0-beta.154" : "0.0.0-test";
968
968
 
969
969
  // src/util/download/download.ts
970
970
  var download = async ({ url }) => {
@@ -1374,7 +1374,8 @@ function mapToolResultOutput(output) {
1374
1374
 
1375
1375
  // src/prompt/create-tool-model-output.ts
1376
1376
  var import_provider18 = require("@ai-sdk/provider");
1377
- function createToolModelOutput({
1377
+ async function createToolModelOutput({
1378
+ toolCallId,
1378
1379
  output,
1379
1380
  tool: tool2,
1380
1381
  errorMode
@@ -1385,7 +1386,7 @@ function createToolModelOutput({
1385
1386
  return { type: "error-json", value: toJSONValue(output) };
1386
1387
  }
1387
1388
  if (tool2 == null ? void 0 : tool2.toModelOutput) {
1388
- return tool2.toModelOutput({ output });
1389
+ return await tool2.toModelOutput({ toolCallId, output });
1389
1390
  }
1390
1391
  return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
1391
1392
  }
@@ -3352,93 +3353,115 @@ async function isStopConditionMet({
3352
3353
  }
3353
3354
 
3354
3355
  // src/generate-text/to-response-messages.ts
3355
- function toResponseMessages({
3356
+ async function toResponseMessages({
3356
3357
  content: inputContent,
3357
3358
  tools
3358
3359
  }) {
3359
3360
  const responseMessages = [];
3360
- const content = inputContent.filter((part) => part.type !== "source").filter(
3361
- (part) => (part.type !== "tool-result" || part.providerExecuted) && (part.type !== "tool-error" || part.providerExecuted)
3362
- ).filter((part) => part.type !== "text" || part.text.length > 0).map((part) => {
3361
+ const content = [];
3362
+ for (const part of inputContent) {
3363
+ if (part.type === "source" || (part.type === "tool-result" || part.type === "tool-error") && !part.providerExecuted || part.type === "text" && part.text.length === 0) {
3364
+ continue;
3365
+ }
3363
3366
  switch (part.type) {
3364
3367
  case "text":
3365
- return {
3368
+ content.push({
3366
3369
  type: "text",
3367
3370
  text: part.text,
3368
3371
  providerOptions: part.providerMetadata
3369
- };
3372
+ });
3373
+ break;
3370
3374
  case "reasoning":
3371
- return {
3375
+ content.push({
3372
3376
  type: "reasoning",
3373
3377
  text: part.text,
3374
3378
  providerOptions: part.providerMetadata
3375
- };
3379
+ });
3380
+ break;
3376
3381
  case "file":
3377
- return {
3382
+ content.push({
3378
3383
  type: "file",
3379
3384
  data: part.file.base64,
3380
3385
  mediaType: part.file.mediaType,
3381
3386
  providerOptions: part.providerMetadata
3382
- };
3387
+ });
3388
+ break;
3383
3389
  case "tool-call":
3384
- return {
3390
+ content.push({
3385
3391
  type: "tool-call",
3386
3392
  toolCallId: part.toolCallId,
3387
3393
  toolName: part.toolName,
3388
3394
  input: part.input,
3389
3395
  providerExecuted: part.providerExecuted,
3390
3396
  providerOptions: part.providerMetadata
3391
- };
3392
- case "tool-result":
3393
- return {
3397
+ });
3398
+ break;
3399
+ case "tool-result": {
3400
+ const output = await createToolModelOutput({
3401
+ toolCallId: part.toolCallId,
3402
+ tool: tools == null ? void 0 : tools[part.toolName],
3403
+ output: part.output,
3404
+ errorMode: "none"
3405
+ });
3406
+ content.push({
3394
3407
  type: "tool-result",
3395
3408
  toolCallId: part.toolCallId,
3396
3409
  toolName: part.toolName,
3397
- output: createToolModelOutput({
3398
- tool: tools == null ? void 0 : tools[part.toolName],
3399
- output: part.output,
3400
- errorMode: "none"
3401
- }),
3402
- providerExecuted: true,
3410
+ output,
3403
3411
  providerOptions: part.providerMetadata
3404
- };
3405
- case "tool-error":
3406
- return {
3412
+ });
3413
+ break;
3414
+ }
3415
+ case "tool-error": {
3416
+ const output = await createToolModelOutput({
3417
+ toolCallId: part.toolCallId,
3418
+ tool: tools == null ? void 0 : tools[part.toolName],
3419
+ output: part.error,
3420
+ errorMode: "json"
3421
+ });
3422
+ content.push({
3407
3423
  type: "tool-result",
3408
3424
  toolCallId: part.toolCallId,
3409
3425
  toolName: part.toolName,
3410
- output: createToolModelOutput({
3411
- tool: tools == null ? void 0 : tools[part.toolName],
3412
- output: part.error,
3413
- errorMode: "json"
3414
- }),
3426
+ output,
3415
3427
  providerOptions: part.providerMetadata
3416
- };
3428
+ });
3429
+ break;
3430
+ }
3417
3431
  case "tool-approval-request":
3418
- return {
3432
+ content.push({
3419
3433
  type: "tool-approval-request",
3420
3434
  approvalId: part.approvalId,
3421
3435
  toolCallId: part.toolCall.toolCallId
3422
- };
3436
+ });
3437
+ break;
3423
3438
  }
3424
- });
3439
+ }
3425
3440
  if (content.length > 0) {
3426
3441
  responseMessages.push({
3427
3442
  role: "assistant",
3428
3443
  content
3429
3444
  });
3430
3445
  }
3431
- const toolResultContent = inputContent.filter((part) => part.type === "tool-result" || part.type === "tool-error").filter((part) => !part.providerExecuted).map((toolResult) => ({
3432
- type: "tool-result",
3433
- toolCallId: toolResult.toolCallId,
3434
- toolName: toolResult.toolName,
3435
- output: createToolModelOutput({
3436
- tool: tools == null ? void 0 : tools[toolResult.toolName],
3437
- output: toolResult.type === "tool-result" ? toolResult.output : toolResult.error,
3438
- errorMode: toolResult.type === "tool-error" ? "text" : "none"
3439
- }),
3440
- ...toolResult.providerMetadata != null ? { providerOptions: toolResult.providerMetadata } : {}
3441
- }));
3446
+ const toolResultContent = [];
3447
+ for (const part of inputContent) {
3448
+ if (!(part.type === "tool-result" || part.type === "tool-error") || part.providerExecuted) {
3449
+ continue;
3450
+ }
3451
+ const output = await createToolModelOutput({
3452
+ toolCallId: part.toolCallId,
3453
+ tool: tools == null ? void 0 : tools[part.toolName],
3454
+ output: part.type === "tool-result" ? part.output : part.error,
3455
+ errorMode: part.type === "tool-error" ? "text" : "none"
3456
+ });
3457
+ toolResultContent.push({
3458
+ type: "tool-result",
3459
+ toolCallId: part.toolCallId,
3460
+ toolName: part.toolName,
3461
+ output,
3462
+ ...part.providerMetadata != null ? { providerOptions: part.providerMetadata } : {}
3463
+ });
3464
+ }
3442
3465
  if (toolResultContent.length > 0) {
3443
3466
  responseMessages.push({
3444
3467
  role: "tool",
@@ -3544,31 +3567,35 @@ async function generateText({
3544
3567
  abortSignal,
3545
3568
  experimental_context
3546
3569
  });
3570
+ const toolContent = [];
3571
+ for (const output2 of toolOutputs) {
3572
+ const modelOutput = await createToolModelOutput({
3573
+ toolCallId: output2.toolCallId,
3574
+ tool: tools == null ? void 0 : tools[output2.toolName],
3575
+ output: output2.type === "tool-result" ? output2.output : output2.error,
3576
+ errorMode: output2.type === "tool-error" ? "json" : "none"
3577
+ });
3578
+ toolContent.push({
3579
+ type: "tool-result",
3580
+ toolCallId: output2.toolCallId,
3581
+ toolName: output2.toolName,
3582
+ output: modelOutput
3583
+ });
3584
+ }
3585
+ for (const toolApproval of deniedToolApprovals) {
3586
+ toolContent.push({
3587
+ type: "tool-result",
3588
+ toolCallId: toolApproval.toolCall.toolCallId,
3589
+ toolName: toolApproval.toolCall.toolName,
3590
+ output: {
3591
+ type: "execution-denied",
3592
+ reason: toolApproval.approvalResponse.reason
3593
+ }
3594
+ });
3595
+ }
3547
3596
  responseMessages.push({
3548
3597
  role: "tool",
3549
- content: [
3550
- // add regular tool results for approved tool calls:
3551
- ...toolOutputs.map((output2) => ({
3552
- type: "tool-result",
3553
- toolCallId: output2.toolCallId,
3554
- toolName: output2.toolName,
3555
- output: createToolModelOutput({
3556
- tool: tools == null ? void 0 : tools[output2.toolName],
3557
- output: output2.type === "tool-result" ? output2.output : output2.error,
3558
- errorMode: output2.type === "tool-error" ? "json" : "none"
3559
- })
3560
- })),
3561
- // add execution denied tool results for denied tool approvals:
3562
- ...deniedToolApprovals.map((toolApproval) => ({
3563
- type: "tool-result",
3564
- toolCallId: toolApproval.toolCall.toolCallId,
3565
- toolName: toolApproval.toolCall.toolName,
3566
- output: {
3567
- type: "execution-denied",
3568
- reason: toolApproval.approvalResponse.reason
3569
- }
3570
- }))
3571
- ]
3598
+ content: toolContent
3572
3599
  });
3573
3600
  }
3574
3601
  const callSettings2 = prepareCallSettings(settings);
@@ -3781,7 +3808,7 @@ async function generateText({
3781
3808
  toolApprovalRequests: Object.values(toolApprovalRequests)
3782
3809
  });
3783
3810
  responseMessages.push(
3784
- ...toResponseMessages({
3811
+ ...await toResponseMessages({
3785
3812
  content: stepContent,
3786
3813
  tools
3787
3814
  })
@@ -5710,7 +5737,7 @@ var DefaultStreamTextResult = class {
5710
5737
  recordedWarnings = part.warnings;
5711
5738
  }
5712
5739
  if (part.type === "finish-step") {
5713
- const stepMessages = toResponseMessages({
5740
+ const stepMessages = await toResponseMessages({
5714
5741
  content: recordedContent,
5715
5742
  tools
5716
5743
  });
@@ -5934,31 +5961,34 @@ var DefaultStreamTextResult = class {
5934
5961
  }
5935
5962
  })
5936
5963
  );
5964
+ const content = [];
5965
+ for (const output2 of toolOutputs) {
5966
+ content.push({
5967
+ type: "tool-result",
5968
+ toolCallId: output2.toolCallId,
5969
+ toolName: output2.toolName,
5970
+ output: await createToolModelOutput({
5971
+ toolCallId: output2.toolCallId,
5972
+ tool: tools == null ? void 0 : tools[output2.toolName],
5973
+ output: output2.type === "tool-result" ? output2.output : output2.error,
5974
+ errorMode: output2.type === "tool-error" ? "json" : "none"
5975
+ })
5976
+ });
5977
+ }
5978
+ for (const toolApproval of deniedToolApprovals) {
5979
+ content.push({
5980
+ type: "tool-result",
5981
+ toolCallId: toolApproval.toolCall.toolCallId,
5982
+ toolName: toolApproval.toolCall.toolName,
5983
+ output: {
5984
+ type: "execution-denied",
5985
+ reason: toolApproval.approvalResponse.reason
5986
+ }
5987
+ });
5988
+ }
5937
5989
  initialResponseMessages.push({
5938
5990
  role: "tool",
5939
- content: [
5940
- // add regular tool results for approved tool calls:
5941
- ...toolOutputs.map((output2) => ({
5942
- type: "tool-result",
5943
- toolCallId: output2.toolCallId,
5944
- toolName: output2.toolName,
5945
- output: createToolModelOutput({
5946
- tool: tools == null ? void 0 : tools[output2.toolName],
5947
- output: output2.type === "tool-result" ? output2.output : output2.error,
5948
- errorMode: output2.type === "tool-error" ? "json" : "none"
5949
- })
5950
- })),
5951
- // add execution denied tool results for denied tool approvals:
5952
- ...deniedToolApprovals.map((toolApproval) => ({
5953
- type: "tool-result",
5954
- toolCallId: toolApproval.toolCall.toolCallId,
5955
- toolName: toolApproval.toolCall.toolName,
5956
- output: {
5957
- type: "execution-denied",
5958
- reason: toolApproval.approvalResponse.reason
5959
- }
5960
- }))
5961
- ]
5991
+ content
5962
5992
  });
5963
5993
  } finally {
5964
5994
  toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.close();
@@ -6306,7 +6336,7 @@ var DefaultStreamTextResult = class {
6306
6336
  steps: recordedSteps
6307
6337
  })) {
6308
6338
  responseMessages.push(
6309
- ...toResponseMessages({
6339
+ ...await toResponseMessages({
6310
6340
  content: (
6311
6341
  // use transformed content to create the messages for the next step:
6312
6342
  recordedSteps[recordedSteps.length - 1].content
@@ -7035,7 +7065,7 @@ function readUIMessageStream({
7035
7065
 
7036
7066
  // src/ui/convert-to-model-messages.ts
7037
7067
  var import_provider_utils19 = require("@ai-sdk/provider-utils");
7038
- function convertToModelMessages(messages, options) {
7068
+ async function convertToModelMessages(messages, options) {
7039
7069
  const modelMessages = [];
7040
7070
  if (options == null ? void 0 : options.ignoreIncompleteToolCalls) {
7041
7071
  messages = messages.map((message) => ({
@@ -7097,8 +7127,9 @@ function convertToModelMessages(messages, options) {
7097
7127
  }
7098
7128
  case "assistant": {
7099
7129
  if (message.parts != null) {
7100
- let processBlock2 = function() {
7101
- var _a15, _b, _c;
7130
+ let block = [];
7131
+ async function processBlock() {
7132
+ var _a15, _b, _c, _d, _e, _f;
7102
7133
  if (block.length === 0) {
7103
7134
  return;
7104
7135
  }
@@ -7146,7 +7177,8 @@ function convertToModelMessages(messages, options) {
7146
7177
  type: "tool-result",
7147
7178
  toolCallId: part.toolCallId,
7148
7179
  toolName,
7149
- output: createToolModelOutput({
7180
+ output: await createToolModelOutput({
7181
+ toolCallId: part.toolCallId,
7150
7182
  output: part.state === "output-error" ? part.errorText : part.output,
7151
7183
  tool: (_b = options == null ? void 0 : options.tools) == null ? void 0 : _b[toolName],
7152
7184
  errorMode: part.state === "output-error" ? "json" : "none"
@@ -7176,68 +7208,66 @@ function convertToModelMessages(messages, options) {
7176
7208
  (part) => isToolUIPart(part) && part.providerExecuted !== true
7177
7209
  );
7178
7210
  if (toolParts.length > 0) {
7179
- modelMessages.push({
7180
- role: "tool",
7181
- content: toolParts.flatMap(
7182
- (toolPart) => {
7183
- var _a16, _b2, _c2;
7184
- const outputs = [];
7185
- if (((_a16 = toolPart.approval) == null ? void 0 : _a16.approved) != null) {
7186
- outputs.push({
7187
- type: "tool-approval-response",
7188
- approvalId: toolPart.approval.id,
7189
- approved: toolPart.approval.approved,
7190
- reason: toolPart.approval.reason
7211
+ {
7212
+ const content2 = [];
7213
+ for (const toolPart of toolParts) {
7214
+ if (((_d = toolPart.approval) == null ? void 0 : _d.approved) != null) {
7215
+ content2.push({
7216
+ type: "tool-approval-response",
7217
+ approvalId: toolPart.approval.id,
7218
+ approved: toolPart.approval.approved,
7219
+ reason: toolPart.approval.reason
7220
+ });
7221
+ }
7222
+ switch (toolPart.state) {
7223
+ case "output-denied": {
7224
+ content2.push({
7225
+ type: "tool-result",
7226
+ toolCallId: toolPart.toolCallId,
7227
+ toolName: getToolName(toolPart),
7228
+ output: {
7229
+ type: "error-text",
7230
+ value: (_e = toolPart.approval.reason) != null ? _e : "Tool execution denied."
7231
+ },
7232
+ ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7191
7233
  });
7234
+ break;
7192
7235
  }
7193
- switch (toolPart.state) {
7194
- case "output-denied": {
7195
- outputs.push({
7196
- type: "tool-result",
7197
- toolCallId: toolPart.toolCallId,
7198
- toolName: getToolName(toolPart),
7199
- output: {
7200
- type: "error-text",
7201
- value: (_b2 = toolPart.approval.reason) != null ? _b2 : "Tool execution denied."
7202
- },
7203
- ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7204
- });
7205
- break;
7206
- }
7207
- case "output-error":
7208
- case "output-available": {
7209
- const toolName = getToolName(toolPart);
7210
- outputs.push({
7211
- type: "tool-result",
7236
+ case "output-error":
7237
+ case "output-available": {
7238
+ const toolName = getToolName(toolPart);
7239
+ content2.push({
7240
+ type: "tool-result",
7241
+ toolCallId: toolPart.toolCallId,
7242
+ toolName,
7243
+ output: await createToolModelOutput({
7212
7244
  toolCallId: toolPart.toolCallId,
7213
- toolName,
7214
- output: createToolModelOutput({
7215
- output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
7216
- tool: (_c2 = options == null ? void 0 : options.tools) == null ? void 0 : _c2[toolName],
7217
- errorMode: toolPart.state === "output-error" ? "text" : "none"
7218
- }),
7219
- ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7220
- });
7221
- break;
7222
- }
7245
+ output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
7246
+ tool: (_f = options == null ? void 0 : options.tools) == null ? void 0 : _f[toolName],
7247
+ errorMode: toolPart.state === "output-error" ? "text" : "none"
7248
+ }),
7249
+ ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7250
+ });
7251
+ break;
7223
7252
  }
7224
- return outputs;
7225
7253
  }
7226
- )
7227
- });
7254
+ }
7255
+ modelMessages.push({
7256
+ role: "tool",
7257
+ content: content2
7258
+ });
7259
+ }
7228
7260
  }
7229
7261
  block = [];
7230
- };
7231
- var processBlock = processBlock2;
7232
- let block = [];
7262
+ }
7233
7263
  for (const part of message.parts) {
7234
7264
  if (isTextUIPart(part) || isReasoningUIPart(part) || isFileUIPart(part) || isToolUIPart(part) || isDataUIPart(part)) {
7235
7265
  block.push(part);
7236
7266
  } else if (part.type === "step-start") {
7237
- processBlock2();
7267
+ await processBlock();
7238
7268
  }
7239
7269
  }
7240
- processBlock2();
7270
+ await processBlock();
7241
7271
  break;
7242
7272
  }
7243
7273
  break;
@@ -7643,7 +7673,7 @@ async function createAgentUIStream({
7643
7673
  messages,
7644
7674
  tools: agent.tools
7645
7675
  });
7646
- const modelMessages = convertToModelMessages(validatedMessages, {
7676
+ const modelMessages = await convertToModelMessages(validatedMessages, {
7647
7677
  tools: agent.tools
7648
7678
  });
7649
7679
  const result = await agent.stream({