ai 6.0.265 → 6.0.268

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
@@ -1193,7 +1193,7 @@ import {
1193
1193
  } from "@ai-sdk/provider-utils";
1194
1194
 
1195
1195
  // src/version.ts
1196
- var VERSION = true ? "6.0.265" : "0.0.0-test";
1196
+ var VERSION = true ? "6.0.268" : "0.0.0-test";
1197
1197
 
1198
1198
  // src/util/download/download.ts
1199
1199
  var download = async ({
@@ -2908,6 +2908,7 @@ async function executeToolCall({
2908
2908
  onToolCallStart,
2909
2909
  onToolCallFinish
2910
2910
  }) {
2911
+ var _a22;
2911
2912
  const { toolName, toolCallId, input } = toolCall;
2912
2913
  const tool2 = tools == null ? void 0 : tools[toolName];
2913
2914
  if ((tool2 == null ? void 0 : tool2.execute) == null) {
@@ -2932,6 +2933,12 @@ async function executeToolCall({
2932
2933
  operationId: "ai.toolCall",
2933
2934
  telemetry
2934
2935
  }),
2936
+ ...Object.fromEntries(
2937
+ Object.entries((_a22 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a22 : {}).map(([key, value]) => [
2938
+ `ai.telemetry.metadata.${key}`,
2939
+ value
2940
+ ])
2941
+ ),
2935
2942
  "ai.toolCall.name": toolName,
2936
2943
  "ai.toolCall.id": toolCallId,
2937
2944
  "ai.toolCall.args": {
@@ -3231,6 +3238,7 @@ async function validateApprovedToolApprovals({
3231
3238
  var _a22;
3232
3239
  const approved = [];
3233
3240
  const denied = [];
3241
+ const invalid = [];
3234
3242
  for (const approval of approvedToolApprovals) {
3235
3243
  const { toolCall, approvalRequest } = approval;
3236
3244
  const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
@@ -3264,11 +3272,15 @@ async function validateApprovedToolApprovals({
3264
3272
  schema: asSchema2(tool2.inputSchema)
3265
3273
  });
3266
3274
  if (!validation.success) {
3267
- throw new InvalidToolInputError({
3268
- toolName: toolCall.toolName,
3269
- toolInput: JSON.stringify(toolCall.input),
3270
- cause: validation.error
3275
+ invalid.push({
3276
+ ...approval,
3277
+ error: new InvalidToolInputError({
3278
+ toolName: toolCall.toolName,
3279
+ toolInput: JSON.stringify(toolCall.input),
3280
+ cause: validation.error
3281
+ })
3271
3282
  });
3283
+ continue;
3272
3284
  }
3273
3285
  }
3274
3286
  const approvalNeeded = tool2 != null && await isApprovalNeeded({
@@ -3290,7 +3302,11 @@ async function validateApprovedToolApprovals({
3290
3302
  });
3291
3303
  }
3292
3304
  }
3293
- return { approvedToolApprovals: approved, deniedToolApprovals: denied };
3305
+ return {
3306
+ approvedToolApprovals: approved,
3307
+ deniedToolApprovals: denied,
3308
+ invalidToolApprovals: invalid
3309
+ };
3294
3310
  }
3295
3311
 
3296
3312
  // src/generate-text/output.ts
@@ -4557,7 +4573,8 @@ async function generateText({
4557
4573
  } = collectToolApprovals({ messages: initialMessages });
4558
4574
  const {
4559
4575
  approvedToolApprovals: localApprovedToolApprovals,
4560
- deniedToolApprovals: revalidationDeniedToolApprovals
4576
+ deniedToolApprovals: revalidationDeniedToolApprovals,
4577
+ invalidToolApprovals
4561
4578
  } = await validateApprovedToolApprovals({
4562
4579
  approvedToolApprovals: approvedToolApprovals.filter(
4563
4580
  (toolApproval) => !toolApproval.toolCall.providerExecuted
@@ -4571,7 +4588,7 @@ async function generateText({
4571
4588
  ...collectedDeniedToolApprovals,
4572
4589
  ...revalidationDeniedToolApprovals
4573
4590
  ];
4574
- if (deniedToolApprovals.length > 0 || localApprovedToolApprovals.length > 0) {
4591
+ if (deniedToolApprovals.length > 0 || localApprovedToolApprovals.length > 0 || invalidToolApprovals.length > 0) {
4575
4592
  const toolOutputs = await executeTools({
4576
4593
  toolCalls: localApprovedToolApprovals.map(
4577
4594
  (toolApproval) => toolApproval.toolCall
@@ -4609,6 +4626,20 @@ async function generateText({
4609
4626
  output: modelOutput
4610
4627
  });
4611
4628
  }
4629
+ for (const toolApproval of invalidToolApprovals) {
4630
+ toolContent.push({
4631
+ type: "tool-result",
4632
+ toolCallId: toolApproval.toolCall.toolCallId,
4633
+ toolName: toolApproval.toolCall.toolName,
4634
+ output: await createToolModelOutput({
4635
+ toolCallId: toolApproval.toolCall.toolCallId,
4636
+ input: toolApproval.toolCall.input,
4637
+ tool: tools == null ? void 0 : tools[toolApproval.toolCall.toolName],
4638
+ output: toolApproval.error,
4639
+ errorMode: "text"
4640
+ })
4641
+ });
4642
+ }
4612
4643
  for (const toolApproval of deniedToolApprovals) {
4613
4644
  toolContent.push({
4614
4645
  type: "tool-result",
@@ -5097,7 +5128,7 @@ async function generateText({
5097
5128
  ]
5098
5129
  });
5099
5130
  let resolvedOutput;
5100
- if (lastStep.finishReason === "stop") {
5131
+ if (lastStep.finishReason === "stop" || lastStep.finishReason !== "tool-calls" && lastStep.text.length > 0) {
5101
5132
  const outputSpecification = output != null ? output : text();
5102
5133
  resolvedOutput = await outputSpecification.parseCompleteOutput(
5103
5134
  { text: lastStep.text },
@@ -6579,7 +6610,10 @@ function createStitchableStream() {
6579
6610
  const terminate = () => {
6580
6611
  isClosed = true;
6581
6612
  waitForNewStream.resolve();
6582
- innerStreams.forEach(({ reader }) => reader.cancel());
6613
+ innerStreams.forEach(({ reader, onCancel }) => {
6614
+ onCancel == null ? void 0 : onCancel();
6615
+ reader.cancel();
6616
+ });
6583
6617
  innerStreams = [];
6584
6618
  controller == null ? void 0 : controller.close();
6585
6619
  };
@@ -6620,7 +6654,8 @@ function createStitchableStream() {
6620
6654
  },
6621
6655
  pull: processPull,
6622
6656
  async cancel() {
6623
- for (const { reader } of innerStreams) {
6657
+ for (const { reader, onCancel } of innerStreams) {
6658
+ onCancel == null ? void 0 : onCancel();
6624
6659
  await reader.cancel();
6625
6660
  }
6626
6661
  innerStreams = [];
@@ -7625,7 +7660,8 @@ var DefaultStreamTextResult = class {
7625
7660
  if (deniedToolApprovals.length > 0 || approvedToolApprovals.length > 0) {
7626
7661
  const {
7627
7662
  approvedToolApprovals: localApprovedToolApprovals,
7628
- deniedToolApprovals: revalidationDeniedToolApprovals
7663
+ deniedToolApprovals: revalidationDeniedToolApprovals,
7664
+ invalidToolApprovals
7629
7665
  } = await validateApprovedToolApprovals({
7630
7666
  approvedToolApprovals: approvedToolApprovals.filter(
7631
7667
  (toolApproval) => !toolApproval.toolCall.providerExecuted
@@ -7662,6 +7698,18 @@ var DefaultStreamTextResult = class {
7662
7698
  toolName: toolApproval.toolCall.toolName
7663
7699
  });
7664
7700
  }
7701
+ for (const toolApproval of invalidToolApprovals) {
7702
+ toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue({
7703
+ type: "tool-error",
7704
+ toolCallId: toolApproval.toolCall.toolCallId,
7705
+ toolName: toolApproval.toolCall.toolName,
7706
+ input: toolApproval.toolCall.input,
7707
+ error: getErrorMessage6(toolApproval.error),
7708
+ title: toolApproval.toolCall.title,
7709
+ ...toolApproval.toolCall.dynamic === true ? { dynamic: true } : {},
7710
+ ...toolApproval.toolCall.toolMetadata != null ? { toolMetadata: toolApproval.toolCall.toolMetadata } : {}
7711
+ });
7712
+ }
7665
7713
  const toolOutputs = [];
7666
7714
  await Promise.all(
7667
7715
  localApprovedToolApprovals.map(async (toolApproval) => {
@@ -7693,7 +7741,7 @@ var DefaultStreamTextResult = class {
7693
7741
  }
7694
7742
  })
7695
7743
  );
7696
- if (toolOutputs.length > 0 || localDeniedToolApprovals.length > 0) {
7744
+ if (toolOutputs.length > 0 || localDeniedToolApprovals.length > 0 || invalidToolApprovals.length > 0) {
7697
7745
  const localToolContent = [];
7698
7746
  for (const output2 of toolOutputs) {
7699
7747
  localToolContent.push({
@@ -7709,6 +7757,20 @@ var DefaultStreamTextResult = class {
7709
7757
  })
7710
7758
  });
7711
7759
  }
7760
+ for (const toolApproval of invalidToolApprovals) {
7761
+ localToolContent.push({
7762
+ type: "tool-result",
7763
+ toolCallId: toolApproval.toolCall.toolCallId,
7764
+ toolName: toolApproval.toolCall.toolName,
7765
+ output: await createToolModelOutput({
7766
+ toolCallId: toolApproval.toolCall.toolCallId,
7767
+ input: toolApproval.toolCall.input,
7768
+ tool: tools == null ? void 0 : tools[toolApproval.toolCall.toolName],
7769
+ output: toolApproval.error,
7770
+ errorMode: "text"
7771
+ })
7772
+ });
7773
+ }
7712
7774
  for (const toolApproval of localDeniedToolApprovals) {
7713
7775
  localToolContent.push({
7714
7776
  type: "tool-result",
@@ -7764,8 +7826,17 @@ var DefaultStreamTextResult = class {
7764
7826
  clearTimeout(stepTimeoutId);
7765
7827
  }
7766
7828
  }
7767
- abortSignal == null ? void 0 : abortSignal.addEventListener("abort", clearStepTimeout);
7768
- abortSignal == null ? void 0 : abortSignal.addEventListener("abort", clearChunkTimeout);
7829
+ function clearStepTimeouts() {
7830
+ clearStepTimeout();
7831
+ clearChunkTimeout();
7832
+ }
7833
+ function cleanupStepTimeouts() {
7834
+ abortSignal == null ? void 0 : abortSignal.removeEventListener("abort", cleanupStepTimeouts);
7835
+ clearStepTimeouts();
7836
+ }
7837
+ abortSignal == null ? void 0 : abortSignal.addEventListener("abort", cleanupStepTimeouts, {
7838
+ once: true
7839
+ });
7769
7840
  try {
7770
7841
  stepFinish = new DelayedPromise();
7771
7842
  const stepInputMessages = [...initialMessages, ...responseMessages];
@@ -8142,8 +8213,7 @@ var DefaultStreamTextResult = class {
8142
8213
  })
8143
8214
  });
8144
8215
  doStreamSpan.end();
8145
- clearStepTimeout();
8146
- clearChunkTimeout();
8216
+ cleanupStepTimeouts();
8147
8217
  self.closeStream();
8148
8218
  return;
8149
8219
  }
@@ -8247,8 +8317,7 @@ var DefaultStreamTextResult = class {
8247
8317
  pendingDeferredToolCalls.delete(output2.toolCallId);
8248
8318
  }
8249
8319
  }
8250
- clearStepTimeout();
8251
- clearChunkTimeout();
8320
+ cleanupStepTimeouts();
8252
8321
  if (
8253
8322
  // Continue if:
8254
8323
  // 1. There are client tool calls that have all been executed, OR
@@ -8292,11 +8361,14 @@ var DefaultStreamTextResult = class {
8292
8361
  }
8293
8362
  }
8294
8363
  })
8295
- )
8364
+ ),
8365
+ {
8366
+ onError: cleanupStepTimeouts,
8367
+ onCancel: cleanupStepTimeouts
8368
+ }
8296
8369
  );
8297
8370
  } catch (error) {
8298
- clearStepTimeout();
8299
- clearChunkTimeout();
8371
+ cleanupStepTimeouts();
8300
8372
  throw error;
8301
8373
  }
8302
8374
  }