ai 6.0.0-beta.151 → 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/dist/index.mjs CHANGED
@@ -866,7 +866,7 @@ import {
866
866
  } from "@ai-sdk/provider-utils";
867
867
 
868
868
  // src/version.ts
869
- var VERSION = true ? "6.0.0-beta.151" : "0.0.0-test";
869
+ var VERSION = true ? "6.0.0-beta.153" : "0.0.0-test";
870
870
 
871
871
  // src/util/download/download.ts
872
872
  var download = async ({ url }) => {
@@ -1279,7 +1279,7 @@ function mapToolResultOutput(output) {
1279
1279
 
1280
1280
  // src/prompt/create-tool-model-output.ts
1281
1281
  import { getErrorMessage as getErrorMessage3 } from "@ai-sdk/provider";
1282
- function createToolModelOutput({
1282
+ async function createToolModelOutput({
1283
1283
  output,
1284
1284
  tool: tool2,
1285
1285
  errorMode
@@ -1290,7 +1290,7 @@ function createToolModelOutput({
1290
1290
  return { type: "error-json", value: toJSONValue(output) };
1291
1291
  }
1292
1292
  if (tool2 == null ? void 0 : tool2.toModelOutput) {
1293
- return tool2.toModelOutput(output);
1293
+ return await tool2.toModelOutput({ output });
1294
1294
  }
1295
1295
  return typeof output === "string" ? { type: "text", value: output } : { type: "json", value: toJSONValue(output) };
1296
1296
  }
@@ -3273,93 +3273,112 @@ async function isStopConditionMet({
3273
3273
  }
3274
3274
 
3275
3275
  // src/generate-text/to-response-messages.ts
3276
- function toResponseMessages({
3276
+ async function toResponseMessages({
3277
3277
  content: inputContent,
3278
3278
  tools
3279
3279
  }) {
3280
3280
  const responseMessages = [];
3281
- const content = inputContent.filter((part) => part.type !== "source").filter(
3282
- (part) => (part.type !== "tool-result" || part.providerExecuted) && (part.type !== "tool-error" || part.providerExecuted)
3283
- ).filter((part) => part.type !== "text" || part.text.length > 0).map((part) => {
3281
+ const content = [];
3282
+ for (const part of inputContent) {
3283
+ if (part.type === "source" || (part.type === "tool-result" || part.type === "tool-error") && !part.providerExecuted || part.type === "text" && part.text.length === 0) {
3284
+ continue;
3285
+ }
3284
3286
  switch (part.type) {
3285
3287
  case "text":
3286
- return {
3288
+ content.push({
3287
3289
  type: "text",
3288
3290
  text: part.text,
3289
3291
  providerOptions: part.providerMetadata
3290
- };
3292
+ });
3293
+ break;
3291
3294
  case "reasoning":
3292
- return {
3295
+ content.push({
3293
3296
  type: "reasoning",
3294
3297
  text: part.text,
3295
3298
  providerOptions: part.providerMetadata
3296
- };
3299
+ });
3300
+ break;
3297
3301
  case "file":
3298
- return {
3302
+ content.push({
3299
3303
  type: "file",
3300
3304
  data: part.file.base64,
3301
3305
  mediaType: part.file.mediaType,
3302
3306
  providerOptions: part.providerMetadata
3303
- };
3307
+ });
3308
+ break;
3304
3309
  case "tool-call":
3305
- return {
3310
+ content.push({
3306
3311
  type: "tool-call",
3307
3312
  toolCallId: part.toolCallId,
3308
3313
  toolName: part.toolName,
3309
3314
  input: part.input,
3310
3315
  providerExecuted: part.providerExecuted,
3311
3316
  providerOptions: part.providerMetadata
3312
- };
3313
- case "tool-result":
3314
- return {
3317
+ });
3318
+ break;
3319
+ case "tool-result": {
3320
+ const output = await createToolModelOutput({
3321
+ tool: tools == null ? void 0 : tools[part.toolName],
3322
+ output: part.output,
3323
+ errorMode: "none"
3324
+ });
3325
+ content.push({
3315
3326
  type: "tool-result",
3316
3327
  toolCallId: part.toolCallId,
3317
3328
  toolName: part.toolName,
3318
- output: createToolModelOutput({
3319
- tool: tools == null ? void 0 : tools[part.toolName],
3320
- output: part.output,
3321
- errorMode: "none"
3322
- }),
3323
- providerExecuted: true,
3329
+ output,
3324
3330
  providerOptions: part.providerMetadata
3325
- };
3326
- case "tool-error":
3327
- return {
3331
+ });
3332
+ break;
3333
+ }
3334
+ case "tool-error": {
3335
+ const output = await createToolModelOutput({
3336
+ tool: tools == null ? void 0 : tools[part.toolName],
3337
+ output: part.error,
3338
+ errorMode: "json"
3339
+ });
3340
+ content.push({
3328
3341
  type: "tool-result",
3329
3342
  toolCallId: part.toolCallId,
3330
3343
  toolName: part.toolName,
3331
- output: createToolModelOutput({
3332
- tool: tools == null ? void 0 : tools[part.toolName],
3333
- output: part.error,
3334
- errorMode: "json"
3335
- }),
3344
+ output,
3336
3345
  providerOptions: part.providerMetadata
3337
- };
3346
+ });
3347
+ break;
3348
+ }
3338
3349
  case "tool-approval-request":
3339
- return {
3350
+ content.push({
3340
3351
  type: "tool-approval-request",
3341
3352
  approvalId: part.approvalId,
3342
3353
  toolCallId: part.toolCall.toolCallId
3343
- };
3354
+ });
3355
+ break;
3344
3356
  }
3345
- });
3357
+ }
3346
3358
  if (content.length > 0) {
3347
3359
  responseMessages.push({
3348
3360
  role: "assistant",
3349
3361
  content
3350
3362
  });
3351
3363
  }
3352
- const toolResultContent = inputContent.filter((part) => part.type === "tool-result" || part.type === "tool-error").filter((part) => !part.providerExecuted).map((toolResult) => ({
3353
- type: "tool-result",
3354
- toolCallId: toolResult.toolCallId,
3355
- toolName: toolResult.toolName,
3356
- output: createToolModelOutput({
3357
- tool: tools == null ? void 0 : tools[toolResult.toolName],
3358
- output: toolResult.type === "tool-result" ? toolResult.output : toolResult.error,
3359
- errorMode: toolResult.type === "tool-error" ? "text" : "none"
3360
- }),
3361
- ...toolResult.providerMetadata != null ? { providerOptions: toolResult.providerMetadata } : {}
3362
- }));
3364
+ const toolResultContent = [];
3365
+ for (const part of inputContent) {
3366
+ if (!(part.type === "tool-result" || part.type === "tool-error") || part.providerExecuted) {
3367
+ continue;
3368
+ }
3369
+ const output = await createToolModelOutput({
3370
+ tool: tools == null ? void 0 : tools[part.toolName],
3371
+ output: part.type === "tool-result" ? part.output : part.error,
3372
+ errorMode: part.type === "tool-error" ? "text" : "none"
3373
+ });
3374
+ toolResultContent.push({
3375
+ type: "tool-result",
3376
+ toolCallId: part.toolCallId,
3377
+ toolName: part.toolName,
3378
+ output,
3379
+ ...part.providerMetadata != null ? { providerOptions: part.providerMetadata } : {}
3380
+ });
3381
+ }
3363
3382
  if (toolResultContent.length > 0) {
3364
3383
  responseMessages.push({
3365
3384
  role: "tool",
@@ -3465,31 +3484,34 @@ async function generateText({
3465
3484
  abortSignal,
3466
3485
  experimental_context
3467
3486
  });
3487
+ const toolContent = [];
3488
+ for (const output2 of toolOutputs) {
3489
+ const modelOutput = await createToolModelOutput({
3490
+ tool: tools == null ? void 0 : tools[output2.toolName],
3491
+ output: output2.type === "tool-result" ? output2.output : output2.error,
3492
+ errorMode: output2.type === "tool-error" ? "json" : "none"
3493
+ });
3494
+ toolContent.push({
3495
+ type: "tool-result",
3496
+ toolCallId: output2.toolCallId,
3497
+ toolName: output2.toolName,
3498
+ output: modelOutput
3499
+ });
3500
+ }
3501
+ for (const toolApproval of deniedToolApprovals) {
3502
+ toolContent.push({
3503
+ type: "tool-result",
3504
+ toolCallId: toolApproval.toolCall.toolCallId,
3505
+ toolName: toolApproval.toolCall.toolName,
3506
+ output: {
3507
+ type: "execution-denied",
3508
+ reason: toolApproval.approvalResponse.reason
3509
+ }
3510
+ });
3511
+ }
3468
3512
  responseMessages.push({
3469
3513
  role: "tool",
3470
- content: [
3471
- // add regular tool results for approved tool calls:
3472
- ...toolOutputs.map((output2) => ({
3473
- type: "tool-result",
3474
- toolCallId: output2.toolCallId,
3475
- toolName: output2.toolName,
3476
- output: createToolModelOutput({
3477
- tool: tools == null ? void 0 : tools[output2.toolName],
3478
- output: output2.type === "tool-result" ? output2.output : output2.error,
3479
- errorMode: output2.type === "tool-error" ? "json" : "none"
3480
- })
3481
- })),
3482
- // add execution denied tool results for denied tool approvals:
3483
- ...deniedToolApprovals.map((toolApproval) => ({
3484
- type: "tool-result",
3485
- toolCallId: toolApproval.toolCall.toolCallId,
3486
- toolName: toolApproval.toolCall.toolName,
3487
- output: {
3488
- type: "execution-denied",
3489
- reason: toolApproval.approvalResponse.reason
3490
- }
3491
- }))
3492
- ]
3514
+ content: toolContent
3493
3515
  });
3494
3516
  }
3495
3517
  const callSettings2 = prepareCallSettings(settings);
@@ -3702,7 +3724,7 @@ async function generateText({
3702
3724
  toolApprovalRequests: Object.values(toolApprovalRequests)
3703
3725
  });
3704
3726
  responseMessages.push(
3705
- ...toResponseMessages({
3727
+ ...await toResponseMessages({
3706
3728
  content: stepContent,
3707
3729
  tools
3708
3730
  })
@@ -5639,7 +5661,7 @@ var DefaultStreamTextResult = class {
5639
5661
  recordedWarnings = part.warnings;
5640
5662
  }
5641
5663
  if (part.type === "finish-step") {
5642
- const stepMessages = toResponseMessages({
5664
+ const stepMessages = await toResponseMessages({
5643
5665
  content: recordedContent,
5644
5666
  tools
5645
5667
  });
@@ -5863,31 +5885,33 @@ var DefaultStreamTextResult = class {
5863
5885
  }
5864
5886
  })
5865
5887
  );
5888
+ const content = [];
5889
+ for (const output2 of toolOutputs) {
5890
+ content.push({
5891
+ type: "tool-result",
5892
+ toolCallId: output2.toolCallId,
5893
+ toolName: output2.toolName,
5894
+ output: await createToolModelOutput({
5895
+ tool: tools == null ? void 0 : tools[output2.toolName],
5896
+ output: output2.type === "tool-result" ? output2.output : output2.error,
5897
+ errorMode: output2.type === "tool-error" ? "json" : "none"
5898
+ })
5899
+ });
5900
+ }
5901
+ for (const toolApproval of deniedToolApprovals) {
5902
+ content.push({
5903
+ type: "tool-result",
5904
+ toolCallId: toolApproval.toolCall.toolCallId,
5905
+ toolName: toolApproval.toolCall.toolName,
5906
+ output: {
5907
+ type: "execution-denied",
5908
+ reason: toolApproval.approvalResponse.reason
5909
+ }
5910
+ });
5911
+ }
5866
5912
  initialResponseMessages.push({
5867
5913
  role: "tool",
5868
- content: [
5869
- // add regular tool results for approved tool calls:
5870
- ...toolOutputs.map((output2) => ({
5871
- type: "tool-result",
5872
- toolCallId: output2.toolCallId,
5873
- toolName: output2.toolName,
5874
- output: createToolModelOutput({
5875
- tool: tools == null ? void 0 : tools[output2.toolName],
5876
- output: output2.type === "tool-result" ? output2.output : output2.error,
5877
- errorMode: output2.type === "tool-error" ? "json" : "none"
5878
- })
5879
- })),
5880
- // add execution denied tool results for denied tool approvals:
5881
- ...deniedToolApprovals.map((toolApproval) => ({
5882
- type: "tool-result",
5883
- toolCallId: toolApproval.toolCall.toolCallId,
5884
- toolName: toolApproval.toolCall.toolName,
5885
- output: {
5886
- type: "execution-denied",
5887
- reason: toolApproval.approvalResponse.reason
5888
- }
5889
- }))
5890
- ]
5914
+ content
5891
5915
  });
5892
5916
  } finally {
5893
5917
  toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.close();
@@ -6235,7 +6259,7 @@ var DefaultStreamTextResult = class {
6235
6259
  steps: recordedSteps
6236
6260
  })) {
6237
6261
  responseMessages.push(
6238
- ...toResponseMessages({
6262
+ ...await toResponseMessages({
6239
6263
  content: (
6240
6264
  // use transformed content to create the messages for the next step:
6241
6265
  recordedSteps[recordedSteps.length - 1].content
@@ -6969,7 +6993,7 @@ function readUIMessageStream({
6969
6993
  import {
6970
6994
  isNonNullable
6971
6995
  } from "@ai-sdk/provider-utils";
6972
- function convertToModelMessages(messages, options) {
6996
+ async function convertToModelMessages(messages, options) {
6973
6997
  const modelMessages = [];
6974
6998
  if (options == null ? void 0 : options.ignoreIncompleteToolCalls) {
6975
6999
  messages = messages.map((message) => ({
@@ -7031,8 +7055,9 @@ function convertToModelMessages(messages, options) {
7031
7055
  }
7032
7056
  case "assistant": {
7033
7057
  if (message.parts != null) {
7034
- let processBlock2 = function() {
7035
- var _a15, _b, _c;
7058
+ let block = [];
7059
+ async function processBlock() {
7060
+ var _a15, _b, _c, _d, _e, _f;
7036
7061
  if (block.length === 0) {
7037
7062
  return;
7038
7063
  }
@@ -7080,7 +7105,7 @@ function convertToModelMessages(messages, options) {
7080
7105
  type: "tool-result",
7081
7106
  toolCallId: part.toolCallId,
7082
7107
  toolName,
7083
- output: createToolModelOutput({
7108
+ output: await createToolModelOutput({
7084
7109
  output: part.state === "output-error" ? part.errorText : part.output,
7085
7110
  tool: (_b = options == null ? void 0 : options.tools) == null ? void 0 : _b[toolName],
7086
7111
  errorMode: part.state === "output-error" ? "json" : "none"
@@ -7110,68 +7135,65 @@ function convertToModelMessages(messages, options) {
7110
7135
  (part) => isToolUIPart(part) && part.providerExecuted !== true
7111
7136
  );
7112
7137
  if (toolParts.length > 0) {
7113
- modelMessages.push({
7114
- role: "tool",
7115
- content: toolParts.flatMap(
7116
- (toolPart) => {
7117
- var _a16, _b2, _c2;
7118
- const outputs = [];
7119
- if (((_a16 = toolPart.approval) == null ? void 0 : _a16.approved) != null) {
7120
- outputs.push({
7121
- type: "tool-approval-response",
7122
- approvalId: toolPart.approval.id,
7123
- approved: toolPart.approval.approved,
7124
- reason: toolPart.approval.reason
7138
+ {
7139
+ const content2 = [];
7140
+ for (const toolPart of toolParts) {
7141
+ if (((_d = toolPart.approval) == null ? void 0 : _d.approved) != null) {
7142
+ content2.push({
7143
+ type: "tool-approval-response",
7144
+ approvalId: toolPart.approval.id,
7145
+ approved: toolPart.approval.approved,
7146
+ reason: toolPart.approval.reason
7147
+ });
7148
+ }
7149
+ switch (toolPart.state) {
7150
+ case "output-denied": {
7151
+ content2.push({
7152
+ type: "tool-result",
7153
+ toolCallId: toolPart.toolCallId,
7154
+ toolName: getToolName(toolPart),
7155
+ output: {
7156
+ type: "error-text",
7157
+ value: (_e = toolPart.approval.reason) != null ? _e : "Tool execution denied."
7158
+ },
7159
+ ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7125
7160
  });
7161
+ break;
7126
7162
  }
7127
- switch (toolPart.state) {
7128
- case "output-denied": {
7129
- outputs.push({
7130
- type: "tool-result",
7131
- toolCallId: toolPart.toolCallId,
7132
- toolName: getToolName(toolPart),
7133
- output: {
7134
- type: "error-text",
7135
- value: (_b2 = toolPart.approval.reason) != null ? _b2 : "Tool execution denied."
7136
- },
7137
- ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7138
- });
7139
- break;
7140
- }
7141
- case "output-error":
7142
- case "output-available": {
7143
- const toolName = getToolName(toolPart);
7144
- outputs.push({
7145
- type: "tool-result",
7146
- toolCallId: toolPart.toolCallId,
7147
- toolName,
7148
- output: createToolModelOutput({
7149
- output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
7150
- tool: (_c2 = options == null ? void 0 : options.tools) == null ? void 0 : _c2[toolName],
7151
- errorMode: toolPart.state === "output-error" ? "text" : "none"
7152
- }),
7153
- ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7154
- });
7155
- break;
7156
- }
7163
+ case "output-error":
7164
+ case "output-available": {
7165
+ const toolName = getToolName(toolPart);
7166
+ content2.push({
7167
+ type: "tool-result",
7168
+ toolCallId: toolPart.toolCallId,
7169
+ toolName,
7170
+ output: await createToolModelOutput({
7171
+ output: toolPart.state === "output-error" ? toolPart.errorText : toolPart.output,
7172
+ tool: (_f = options == null ? void 0 : options.tools) == null ? void 0 : _f[toolName],
7173
+ errorMode: toolPart.state === "output-error" ? "text" : "none"
7174
+ }),
7175
+ ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
7176
+ });
7177
+ break;
7157
7178
  }
7158
- return outputs;
7159
7179
  }
7160
- )
7161
- });
7180
+ }
7181
+ modelMessages.push({
7182
+ role: "tool",
7183
+ content: content2
7184
+ });
7185
+ }
7162
7186
  }
7163
7187
  block = [];
7164
- };
7165
- var processBlock = processBlock2;
7166
- let block = [];
7188
+ }
7167
7189
  for (const part of message.parts) {
7168
7190
  if (isTextUIPart(part) || isReasoningUIPart(part) || isFileUIPart(part) || isToolUIPart(part) || isDataUIPart(part)) {
7169
7191
  block.push(part);
7170
7192
  } else if (part.type === "step-start") {
7171
- processBlock2();
7193
+ await processBlock();
7172
7194
  }
7173
7195
  }
7174
- processBlock2();
7196
+ await processBlock();
7175
7197
  break;
7176
7198
  }
7177
7199
  break;
@@ -7581,7 +7603,7 @@ async function createAgentUIStream({
7581
7603
  messages,
7582
7604
  tools: agent.tools
7583
7605
  });
7584
- const modelMessages = convertToModelMessages(validatedMessages, {
7606
+ const modelMessages = await convertToModelMessages(validatedMessages, {
7585
7607
  tools: agent.tools
7586
7608
  });
7587
7609
  const result = await agent.stream({