ai 6.0.0-beta.152 → 6.0.0-beta.153

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,14 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.0-beta.153
4
+
5
+ ### Patch Changes
6
+
7
+ - 83e5744: feat: support async Tool.toModelOutput
8
+ - Updated dependencies [83e5744]
9
+ - @ai-sdk/provider-utils@4.0.0-beta.49
10
+ - @ai-sdk/gateway@2.0.0-beta.80
11
+
3
12
  ## 6.0.0-beta.152
4
13
 
5
14
  ### 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.153" : "0.0.0-test";
968
968
 
969
969
  // src/util/download/download.ts
970
970
  var download = async ({ url }) => {
@@ -1374,7 +1374,7 @@ 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
1378
  output,
1379
1379
  tool: tool2,
1380
1380
  errorMode
@@ -1385,7 +1385,7 @@ function createToolModelOutput({
1385
1385
  return { type: "error-json", value: toJSONValue(output) };
1386
1386
  }
1387
1387
  if (tool2 == null ? void 0 : tool2.toModelOutput) {
1388
- return tool2.toModelOutput({ output });
1388
+ return await tool2.toModelOutput({ output });
1389
1389
  }
1390
1390
  return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
1391
1391
  }
@@ -3352,93 +3352,112 @@ async function isStopConditionMet({
3352
3352
  }
3353
3353
 
3354
3354
  // src/generate-text/to-response-messages.ts
3355
- function toResponseMessages({
3355
+ async function toResponseMessages({
3356
3356
  content: inputContent,
3357
3357
  tools
3358
3358
  }) {
3359
3359
  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) => {
3360
+ const content = [];
3361
+ for (const part of inputContent) {
3362
+ if (part.type === "source" || (part.type === "tool-result" || part.type === "tool-error") && !part.providerExecuted || part.type === "text" && part.text.length === 0) {
3363
+ continue;
3364
+ }
3363
3365
  switch (part.type) {
3364
3366
  case "text":
3365
- return {
3367
+ content.push({
3366
3368
  type: "text",
3367
3369
  text: part.text,
3368
3370
  providerOptions: part.providerMetadata
3369
- };
3371
+ });
3372
+ break;
3370
3373
  case "reasoning":
3371
- return {
3374
+ content.push({
3372
3375
  type: "reasoning",
3373
3376
  text: part.text,
3374
3377
  providerOptions: part.providerMetadata
3375
- };
3378
+ });
3379
+ break;
3376
3380
  case "file":
3377
- return {
3381
+ content.push({
3378
3382
  type: "file",
3379
3383
  data: part.file.base64,
3380
3384
  mediaType: part.file.mediaType,
3381
3385
  providerOptions: part.providerMetadata
3382
- };
3386
+ });
3387
+ break;
3383
3388
  case "tool-call":
3384
- return {
3389
+ content.push({
3385
3390
  type: "tool-call",
3386
3391
  toolCallId: part.toolCallId,
3387
3392
  toolName: part.toolName,
3388
3393
  input: part.input,
3389
3394
  providerExecuted: part.providerExecuted,
3390
3395
  providerOptions: part.providerMetadata
3391
- };
3392
- case "tool-result":
3393
- return {
3396
+ });
3397
+ break;
3398
+ case "tool-result": {
3399
+ const output = await createToolModelOutput({
3400
+ tool: tools == null ? void 0 : tools[part.toolName],
3401
+ output: part.output,
3402
+ errorMode: "none"
3403
+ });
3404
+ content.push({
3394
3405
  type: "tool-result",
3395
3406
  toolCallId: part.toolCallId,
3396
3407
  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,
3408
+ output,
3403
3409
  providerOptions: part.providerMetadata
3404
- };
3405
- case "tool-error":
3406
- return {
3410
+ });
3411
+ break;
3412
+ }
3413
+ case "tool-error": {
3414
+ const output = await createToolModelOutput({
3415
+ tool: tools == null ? void 0 : tools[part.toolName],
3416
+ output: part.error,
3417
+ errorMode: "json"
3418
+ });
3419
+ content.push({
3407
3420
  type: "tool-result",
3408
3421
  toolCallId: part.toolCallId,
3409
3422
  toolName: part.toolName,
3410
- output: createToolModelOutput({
3411
- tool: tools == null ? void 0 : tools[part.toolName],
3412
- output: part.error,
3413
- errorMode: "json"
3414
- }),
3423
+ output,
3415
3424
  providerOptions: part.providerMetadata
3416
- };
3425
+ });
3426
+ break;
3427
+ }
3417
3428
  case "tool-approval-request":
3418
- return {
3429
+ content.push({
3419
3430
  type: "tool-approval-request",
3420
3431
  approvalId: part.approvalId,
3421
3432
  toolCallId: part.toolCall.toolCallId
3422
- };
3433
+ });
3434
+ break;
3423
3435
  }
3424
- });
3436
+ }
3425
3437
  if (content.length > 0) {
3426
3438
  responseMessages.push({
3427
3439
  role: "assistant",
3428
3440
  content
3429
3441
  });
3430
3442
  }
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
- }));
3443
+ const toolResultContent = [];
3444
+ for (const part of inputContent) {
3445
+ if (!(part.type === "tool-result" || part.type === "tool-error") || part.providerExecuted) {
3446
+ continue;
3447
+ }
3448
+ const output = await createToolModelOutput({
3449
+ tool: tools == null ? void 0 : tools[part.toolName],
3450
+ output: part.type === "tool-result" ? part.output : part.error,
3451
+ errorMode: part.type === "tool-error" ? "text" : "none"
3452
+ });
3453
+ toolResultContent.push({
3454
+ type: "tool-result",
3455
+ toolCallId: part.toolCallId,
3456
+ toolName: part.toolName,
3457
+ output,
3458
+ ...part.providerMetadata != null ? { providerOptions: part.providerMetadata } : {}
3459
+ });
3460
+ }
3442
3461
  if (toolResultContent.length > 0) {
3443
3462
  responseMessages.push({
3444
3463
  role: "tool",
@@ -3544,31 +3563,34 @@ async function generateText({
3544
3563
  abortSignal,
3545
3564
  experimental_context
3546
3565
  });
3566
+ const toolContent = [];
3567
+ for (const output2 of toolOutputs) {
3568
+ const modelOutput = await createToolModelOutput({
3569
+ tool: tools == null ? void 0 : tools[output2.toolName],
3570
+ output: output2.type === "tool-result" ? output2.output : output2.error,
3571
+ errorMode: output2.type === "tool-error" ? "json" : "none"
3572
+ });
3573
+ toolContent.push({
3574
+ type: "tool-result",
3575
+ toolCallId: output2.toolCallId,
3576
+ toolName: output2.toolName,
3577
+ output: modelOutput
3578
+ });
3579
+ }
3580
+ for (const toolApproval of deniedToolApprovals) {
3581
+ toolContent.push({
3582
+ type: "tool-result",
3583
+ toolCallId: toolApproval.toolCall.toolCallId,
3584
+ toolName: toolApproval.toolCall.toolName,
3585
+ output: {
3586
+ type: "execution-denied",
3587
+ reason: toolApproval.approvalResponse.reason
3588
+ }
3589
+ });
3590
+ }
3547
3591
  responseMessages.push({
3548
3592
  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
- ]
3593
+ content: toolContent
3572
3594
  });
3573
3595
  }
3574
3596
  const callSettings2 = prepareCallSettings(settings);
@@ -3781,7 +3803,7 @@ async function generateText({
3781
3803
  toolApprovalRequests: Object.values(toolApprovalRequests)
3782
3804
  });
3783
3805
  responseMessages.push(
3784
- ...toResponseMessages({
3806
+ ...await toResponseMessages({
3785
3807
  content: stepContent,
3786
3808
  tools
3787
3809
  })
@@ -5710,7 +5732,7 @@ var DefaultStreamTextResult = class {
5710
5732
  recordedWarnings = part.warnings;
5711
5733
  }
5712
5734
  if (part.type === "finish-step") {
5713
- const stepMessages = toResponseMessages({
5735
+ const stepMessages = await toResponseMessages({
5714
5736
  content: recordedContent,
5715
5737
  tools
5716
5738
  });
@@ -5934,31 +5956,33 @@ var DefaultStreamTextResult = class {
5934
5956
  }
5935
5957
  })
5936
5958
  );
5959
+ const content = [];
5960
+ for (const output2 of toolOutputs) {
5961
+ content.push({
5962
+ type: "tool-result",
5963
+ toolCallId: output2.toolCallId,
5964
+ toolName: output2.toolName,
5965
+ output: await createToolModelOutput({
5966
+ tool: tools == null ? void 0 : tools[output2.toolName],
5967
+ output: output2.type === "tool-result" ? output2.output : output2.error,
5968
+ errorMode: output2.type === "tool-error" ? "json" : "none"
5969
+ })
5970
+ });
5971
+ }
5972
+ for (const toolApproval of deniedToolApprovals) {
5973
+ content.push({
5974
+ type: "tool-result",
5975
+ toolCallId: toolApproval.toolCall.toolCallId,
5976
+ toolName: toolApproval.toolCall.toolName,
5977
+ output: {
5978
+ type: "execution-denied",
5979
+ reason: toolApproval.approvalResponse.reason
5980
+ }
5981
+ });
5982
+ }
5937
5983
  initialResponseMessages.push({
5938
5984
  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
- ]
5985
+ content
5962
5986
  });
5963
5987
  } finally {
5964
5988
  toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.close();
@@ -6306,7 +6330,7 @@ var DefaultStreamTextResult = class {
6306
6330
  steps: recordedSteps
6307
6331
  })) {
6308
6332
  responseMessages.push(
6309
- ...toResponseMessages({
6333
+ ...await toResponseMessages({
6310
6334
  content: (
6311
6335
  // use transformed content to create the messages for the next step:
6312
6336
  recordedSteps[recordedSteps.length - 1].content
@@ -7035,7 +7059,7 @@ function readUIMessageStream({
7035
7059
 
7036
7060
  // src/ui/convert-to-model-messages.ts
7037
7061
  var import_provider_utils19 = require("@ai-sdk/provider-utils");
7038
- function convertToModelMessages(messages, options) {
7062
+ async function convertToModelMessages(messages, options) {
7039
7063
  const modelMessages = [];
7040
7064
  if (options == null ? void 0 : options.ignoreIncompleteToolCalls) {
7041
7065
  messages = messages.map((message) => ({
@@ -7097,8 +7121,9 @@ function convertToModelMessages(messages, options) {
7097
7121
  }
7098
7122
  case "assistant": {
7099
7123
  if (message.parts != null) {
7100
- let processBlock2 = function() {
7101
- var _a15, _b, _c;
7124
+ let block = [];
7125
+ async function processBlock() {
7126
+ var _a15, _b, _c, _d, _e, _f;
7102
7127
  if (block.length === 0) {
7103
7128
  return;
7104
7129
  }
@@ -7146,7 +7171,7 @@ function convertToModelMessages(messages, options) {
7146
7171
  type: "tool-result",
7147
7172
  toolCallId: part.toolCallId,
7148
7173
  toolName,
7149
- output: createToolModelOutput({
7174
+ output: await createToolModelOutput({
7150
7175
  output: part.state === "output-error" ? part.errorText : part.output,
7151
7176
  tool: (_b = options == null ? void 0 : options.tools) == null ? void 0 : _b[toolName],
7152
7177
  errorMode: part.state === "output-error" ? "json" : "none"
@@ -7176,68 +7201,65 @@ function convertToModelMessages(messages, options) {
7176
7201
  (part) => isToolUIPart(part) && part.providerExecuted !== true
7177
7202
  );
7178
7203
  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
7204
+ {
7205
+ const content2 = [];
7206
+ for (const toolPart of toolParts) {
7207
+ if (((_d = toolPart.approval) == null ? void 0 : _d.approved) != null) {
7208
+ content2.push({
7209
+ type: "tool-approval-response",
7210
+ approvalId: toolPart.approval.id,
7211
+ approved: toolPart.approval.approved,
7212
+ reason: toolPart.approval.reason
7213
+ });
7214
+ }
7215
+ switch (toolPart.state) {
7216
+ case "output-denied": {
7217
+ content2.push({
7218
+ type: "tool-result",
7219
+ toolCallId: toolPart.toolCallId,
7220
+ toolName: getToolName(toolPart),
7221
+ output: {
7222
+ type: "error-text",
7223
+ value: (_e = toolPart.approval.reason) != null ? _e : "Tool execution denied."
7224
+ },
7225
+ ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7191
7226
  });
7227
+ break;
7192
7228
  }
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",
7212
- 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
- }
7229
+ case "output-error":
7230
+ case "output-available": {
7231
+ const toolName = getToolName(toolPart);
7232
+ content2.push({
7233
+ type: "tool-result",
7234
+ toolCallId: toolPart.toolCallId,
7235
+ toolName,
7236
+ output: await createToolModelOutput({
7237
+ output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
7238
+ tool: (_f = options == null ? void 0 : options.tools) == null ? void 0 : _f[toolName],
7239
+ errorMode: toolPart.state === "output-error" ? "text" : "none"
7240
+ }),
7241
+ ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7242
+ });
7243
+ break;
7223
7244
  }
7224
- return outputs;
7225
7245
  }
7226
- )
7227
- });
7246
+ }
7247
+ modelMessages.push({
7248
+ role: "tool",
7249
+ content: content2
7250
+ });
7251
+ }
7228
7252
  }
7229
7253
  block = [];
7230
- };
7231
- var processBlock = processBlock2;
7232
- let block = [];
7254
+ }
7233
7255
  for (const part of message.parts) {
7234
7256
  if (isTextUIPart(part) || isReasoningUIPart(part) || isFileUIPart(part) || isToolUIPart(part) || isDataUIPart(part)) {
7235
7257
  block.push(part);
7236
7258
  } else if (part.type === "step-start") {
7237
- processBlock2();
7259
+ await processBlock();
7238
7260
  }
7239
7261
  }
7240
- processBlock2();
7262
+ await processBlock();
7241
7263
  break;
7242
7264
  }
7243
7265
  break;
@@ -7643,7 +7665,7 @@ async function createAgentUIStream({
7643
7665
  messages,
7644
7666
  tools: agent.tools
7645
7667
  });
7646
- const modelMessages = convertToModelMessages(validatedMessages, {
7668
+ const modelMessages = await convertToModelMessages(validatedMessages, {
7647
7669
  tools: agent.tools
7648
7670
  });
7649
7671
  const result = await agent.stream({