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/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.268
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [ad28ecb]
8
+ - @ai-sdk/gateway@3.0.183
9
+
10
+ ## 6.0.267
11
+
12
+ ### Patch Changes
13
+
14
+ - cd29175: Include telemetry metadata attributes on tool call spans.
15
+ - e88c835: Expose structured output parsing diagnostics from `generateText` when generation stops because of the output token limit.
16
+ - Updated dependencies [845ad6d]
17
+ - Updated dependencies [fe80a3c]
18
+ - @ai-sdk/gateway@3.0.182
19
+
20
+ ## 6.0.266
21
+
22
+ ### Patch Changes
23
+
24
+ - a1bafb1: Continue approved `generateText` and `streamText` turns with a model-visible tool error when revalidated tool input is invalid.
25
+ - 3cb50fc: Prevent aborted, errored, or cancelled `streamText` calls from retaining per-call state through abort cleanup listeners.
26
+ - Updated dependencies [5642849]
27
+ - @ai-sdk/provider-utils@4.0.48
28
+ - @ai-sdk/gateway@3.0.181
29
+
3
30
  ## 6.0.265
4
31
 
5
32
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -1173,7 +1173,8 @@ interface GenerateTextResult<TOOLS extends ToolSet, OUTPUT extends Output> {
1173
1173
  * The generated output according to the `output` specification.
1174
1174
  *
1175
1175
  * @throws {NoOutputGeneratedError} When no output is available, for example
1176
- * when the final step does not finish with a `stop` reason.
1176
+ * when the final step finishes with a `tool-calls` reason, or when it
1177
+ * contains no text and does not finish with a `stop` reason.
1177
1178
  */
1178
1179
  readonly output: InferCompleteOutput<OUTPUT>;
1179
1180
  }
package/dist/index.d.ts CHANGED
@@ -1173,7 +1173,8 @@ interface GenerateTextResult<TOOLS extends ToolSet, OUTPUT extends Output> {
1173
1173
  * The generated output according to the `output` specification.
1174
1174
  *
1175
1175
  * @throws {NoOutputGeneratedError} When no output is available, for example
1176
- * when the final step does not finish with a `stop` reason.
1176
+ * when the final step finishes with a `tool-calls` reason, or when it
1177
+ * contains no text and does not finish with a `stop` reason.
1177
1178
  */
1178
1179
  readonly output: InferCompleteOutput<OUTPUT>;
1179
1180
  }
package/dist/index.js CHANGED
@@ -1304,7 +1304,7 @@ function detectMediaType({
1304
1304
  var import_provider_utils3 = require("@ai-sdk/provider-utils");
1305
1305
 
1306
1306
  // src/version.ts
1307
- var VERSION = true ? "6.0.265" : "0.0.0-test";
1307
+ var VERSION = true ? "6.0.268" : "0.0.0-test";
1308
1308
 
1309
1309
  // src/util/download/download.ts
1310
1310
  var download = async ({
@@ -3009,6 +3009,7 @@ async function executeToolCall({
3009
3009
  onToolCallStart,
3010
3010
  onToolCallFinish
3011
3011
  }) {
3012
+ var _a22;
3012
3013
  const { toolName, toolCallId, input } = toolCall;
3013
3014
  const tool2 = tools == null ? void 0 : tools[toolName];
3014
3015
  if ((tool2 == null ? void 0 : tool2.execute) == null) {
@@ -3033,6 +3034,12 @@ async function executeToolCall({
3033
3034
  operationId: "ai.toolCall",
3034
3035
  telemetry
3035
3036
  }),
3037
+ ...Object.fromEntries(
3038
+ Object.entries((_a22 = telemetry == null ? void 0 : telemetry.metadata) != null ? _a22 : {}).map(([key, value]) => [
3039
+ `ai.telemetry.metadata.${key}`,
3040
+ value
3041
+ ])
3042
+ ),
3036
3043
  "ai.toolCall.name": toolName,
3037
3044
  "ai.toolCall.id": toolCallId,
3038
3045
  "ai.toolCall.args": {
@@ -3323,6 +3330,7 @@ async function validateApprovedToolApprovals({
3323
3330
  var _a22;
3324
3331
  const approved = [];
3325
3332
  const denied = [];
3333
+ const invalid = [];
3326
3334
  for (const approval of approvedToolApprovals) {
3327
3335
  const { toolCall, approvalRequest } = approval;
3328
3336
  const tool2 = tools == null ? void 0 : tools[toolCall.toolName];
@@ -3356,11 +3364,15 @@ async function validateApprovedToolApprovals({
3356
3364
  schema: (0, import_provider_utils12.asSchema)(tool2.inputSchema)
3357
3365
  });
3358
3366
  if (!validation.success) {
3359
- throw new InvalidToolInputError({
3360
- toolName: toolCall.toolName,
3361
- toolInput: JSON.stringify(toolCall.input),
3362
- cause: validation.error
3367
+ invalid.push({
3368
+ ...approval,
3369
+ error: new InvalidToolInputError({
3370
+ toolName: toolCall.toolName,
3371
+ toolInput: JSON.stringify(toolCall.input),
3372
+ cause: validation.error
3373
+ })
3363
3374
  });
3375
+ continue;
3364
3376
  }
3365
3377
  }
3366
3378
  const approvalNeeded = tool2 != null && await isApprovalNeeded({
@@ -3382,7 +3394,11 @@ async function validateApprovedToolApprovals({
3382
3394
  });
3383
3395
  }
3384
3396
  }
3385
- return { approvedToolApprovals: approved, deniedToolApprovals: denied };
3397
+ return {
3398
+ approvedToolApprovals: approved,
3399
+ deniedToolApprovals: denied,
3400
+ invalidToolApprovals: invalid
3401
+ };
3386
3402
  }
3387
3403
 
3388
3404
  // src/generate-text/output.ts
@@ -4638,7 +4654,8 @@ async function generateText({
4638
4654
  } = collectToolApprovals({ messages: initialMessages });
4639
4655
  const {
4640
4656
  approvedToolApprovals: localApprovedToolApprovals,
4641
- deniedToolApprovals: revalidationDeniedToolApprovals
4657
+ deniedToolApprovals: revalidationDeniedToolApprovals,
4658
+ invalidToolApprovals
4642
4659
  } = await validateApprovedToolApprovals({
4643
4660
  approvedToolApprovals: approvedToolApprovals.filter(
4644
4661
  (toolApproval) => !toolApproval.toolCall.providerExecuted
@@ -4652,7 +4669,7 @@ async function generateText({
4652
4669
  ...collectedDeniedToolApprovals,
4653
4670
  ...revalidationDeniedToolApprovals
4654
4671
  ];
4655
- if (deniedToolApprovals.length > 0 || localApprovedToolApprovals.length > 0) {
4672
+ if (deniedToolApprovals.length > 0 || localApprovedToolApprovals.length > 0 || invalidToolApprovals.length > 0) {
4656
4673
  const toolOutputs = await executeTools({
4657
4674
  toolCalls: localApprovedToolApprovals.map(
4658
4675
  (toolApproval) => toolApproval.toolCall
@@ -4690,6 +4707,20 @@ async function generateText({
4690
4707
  output: modelOutput
4691
4708
  });
4692
4709
  }
4710
+ for (const toolApproval of invalidToolApprovals) {
4711
+ toolContent.push({
4712
+ type: "tool-result",
4713
+ toolCallId: toolApproval.toolCall.toolCallId,
4714
+ toolName: toolApproval.toolCall.toolName,
4715
+ output: await createToolModelOutput({
4716
+ toolCallId: toolApproval.toolCall.toolCallId,
4717
+ input: toolApproval.toolCall.input,
4718
+ tool: tools == null ? void 0 : tools[toolApproval.toolCall.toolName],
4719
+ output: toolApproval.error,
4720
+ errorMode: "text"
4721
+ })
4722
+ });
4723
+ }
4693
4724
  for (const toolApproval of deniedToolApprovals) {
4694
4725
  toolContent.push({
4695
4726
  type: "tool-result",
@@ -5178,7 +5209,7 @@ async function generateText({
5178
5209
  ]
5179
5210
  });
5180
5211
  let resolvedOutput;
5181
- if (lastStep.finishReason === "stop") {
5212
+ if (lastStep.finishReason === "stop" || lastStep.finishReason !== "tool-calls" && lastStep.text.length > 0) {
5182
5213
  const outputSpecification = output != null ? output : text();
5183
5214
  resolvedOutput = await outputSpecification.parseCompleteOutput(
5184
5215
  { text: lastStep.text },
@@ -6653,7 +6684,10 @@ function createStitchableStream() {
6653
6684
  const terminate = () => {
6654
6685
  isClosed = true;
6655
6686
  waitForNewStream.resolve();
6656
- innerStreams.forEach(({ reader }) => reader.cancel());
6687
+ innerStreams.forEach(({ reader, onCancel }) => {
6688
+ onCancel == null ? void 0 : onCancel();
6689
+ reader.cancel();
6690
+ });
6657
6691
  innerStreams = [];
6658
6692
  controller == null ? void 0 : controller.close();
6659
6693
  };
@@ -6694,7 +6728,8 @@ function createStitchableStream() {
6694
6728
  },
6695
6729
  pull: processPull,
6696
6730
  async cancel() {
6697
- for (const { reader } of innerStreams) {
6731
+ for (const { reader, onCancel } of innerStreams) {
6732
+ onCancel == null ? void 0 : onCancel();
6698
6733
  await reader.cancel();
6699
6734
  }
6700
6735
  innerStreams = [];
@@ -7697,7 +7732,8 @@ var DefaultStreamTextResult = class {
7697
7732
  if (deniedToolApprovals.length > 0 || approvedToolApprovals.length > 0) {
7698
7733
  const {
7699
7734
  approvedToolApprovals: localApprovedToolApprovals,
7700
- deniedToolApprovals: revalidationDeniedToolApprovals
7735
+ deniedToolApprovals: revalidationDeniedToolApprovals,
7736
+ invalidToolApprovals
7701
7737
  } = await validateApprovedToolApprovals({
7702
7738
  approvedToolApprovals: approvedToolApprovals.filter(
7703
7739
  (toolApproval) => !toolApproval.toolCall.providerExecuted
@@ -7734,6 +7770,18 @@ var DefaultStreamTextResult = class {
7734
7770
  toolName: toolApproval.toolCall.toolName
7735
7771
  });
7736
7772
  }
7773
+ for (const toolApproval of invalidToolApprovals) {
7774
+ toolExecutionStepStreamController == null ? void 0 : toolExecutionStepStreamController.enqueue({
7775
+ type: "tool-error",
7776
+ toolCallId: toolApproval.toolCall.toolCallId,
7777
+ toolName: toolApproval.toolCall.toolName,
7778
+ input: toolApproval.toolCall.input,
7779
+ error: (0, import_provider29.getErrorMessage)(toolApproval.error),
7780
+ title: toolApproval.toolCall.title,
7781
+ ...toolApproval.toolCall.dynamic === true ? { dynamic: true } : {},
7782
+ ...toolApproval.toolCall.toolMetadata != null ? { toolMetadata: toolApproval.toolCall.toolMetadata } : {}
7783
+ });
7784
+ }
7737
7785
  const toolOutputs = [];
7738
7786
  await Promise.all(
7739
7787
  localApprovedToolApprovals.map(async (toolApproval) => {
@@ -7765,7 +7813,7 @@ var DefaultStreamTextResult = class {
7765
7813
  }
7766
7814
  })
7767
7815
  );
7768
- if (toolOutputs.length > 0 || localDeniedToolApprovals.length > 0) {
7816
+ if (toolOutputs.length > 0 || localDeniedToolApprovals.length > 0 || invalidToolApprovals.length > 0) {
7769
7817
  const localToolContent = [];
7770
7818
  for (const output2 of toolOutputs) {
7771
7819
  localToolContent.push({
@@ -7781,6 +7829,20 @@ var DefaultStreamTextResult = class {
7781
7829
  })
7782
7830
  });
7783
7831
  }
7832
+ for (const toolApproval of invalidToolApprovals) {
7833
+ localToolContent.push({
7834
+ type: "tool-result",
7835
+ toolCallId: toolApproval.toolCall.toolCallId,
7836
+ toolName: toolApproval.toolCall.toolName,
7837
+ output: await createToolModelOutput({
7838
+ toolCallId: toolApproval.toolCall.toolCallId,
7839
+ input: toolApproval.toolCall.input,
7840
+ tool: tools == null ? void 0 : tools[toolApproval.toolCall.toolName],
7841
+ output: toolApproval.error,
7842
+ errorMode: "text"
7843
+ })
7844
+ });
7845
+ }
7784
7846
  for (const toolApproval of localDeniedToolApprovals) {
7785
7847
  localToolContent.push({
7786
7848
  type: "tool-result",
@@ -7836,8 +7898,17 @@ var DefaultStreamTextResult = class {
7836
7898
  clearTimeout(stepTimeoutId);
7837
7899
  }
7838
7900
  }
7839
- abortSignal == null ? void 0 : abortSignal.addEventListener("abort", clearStepTimeout);
7840
- abortSignal == null ? void 0 : abortSignal.addEventListener("abort", clearChunkTimeout);
7901
+ function clearStepTimeouts() {
7902
+ clearStepTimeout();
7903
+ clearChunkTimeout();
7904
+ }
7905
+ function cleanupStepTimeouts() {
7906
+ abortSignal == null ? void 0 : abortSignal.removeEventListener("abort", cleanupStepTimeouts);
7907
+ clearStepTimeouts();
7908
+ }
7909
+ abortSignal == null ? void 0 : abortSignal.addEventListener("abort", cleanupStepTimeouts, {
7910
+ once: true
7911
+ });
7841
7912
  try {
7842
7913
  stepFinish = new import_provider_utils20.DelayedPromise();
7843
7914
  const stepInputMessages = [...initialMessages, ...responseMessages];
@@ -8214,8 +8285,7 @@ var DefaultStreamTextResult = class {
8214
8285
  })
8215
8286
  });
8216
8287
  doStreamSpan.end();
8217
- clearStepTimeout();
8218
- clearChunkTimeout();
8288
+ cleanupStepTimeouts();
8219
8289
  self.closeStream();
8220
8290
  return;
8221
8291
  }
@@ -8319,8 +8389,7 @@ var DefaultStreamTextResult = class {
8319
8389
  pendingDeferredToolCalls.delete(output2.toolCallId);
8320
8390
  }
8321
8391
  }
8322
- clearStepTimeout();
8323
- clearChunkTimeout();
8392
+ cleanupStepTimeouts();
8324
8393
  if (
8325
8394
  // Continue if:
8326
8395
  // 1. There are client tool calls that have all been executed, OR
@@ -8364,11 +8433,14 @@ var DefaultStreamTextResult = class {
8364
8433
  }
8365
8434
  }
8366
8435
  })
8367
- )
8436
+ ),
8437
+ {
8438
+ onError: cleanupStepTimeouts,
8439
+ onCancel: cleanupStepTimeouts
8440
+ }
8368
8441
  );
8369
8442
  } catch (error) {
8370
- clearStepTimeout();
8371
- clearChunkTimeout();
8443
+ cleanupStepTimeouts();
8372
8444
  throw error;
8373
8445
  }
8374
8446
  }