ai 6.0.220 → 6.0.222

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
@@ -1171,7 +1171,7 @@ import {
1171
1171
  } from "@ai-sdk/provider-utils";
1172
1172
 
1173
1173
  // src/version.ts
1174
- var VERSION = true ? "6.0.220" : "0.0.0-test";
1174
+ var VERSION = true ? "6.0.222" : "0.0.0-test";
1175
1175
 
1176
1176
  // src/util/download/download.ts
1177
1177
  var download = async ({
@@ -2756,6 +2756,26 @@ function prepareRetries({
2756
2756
  };
2757
2757
  }
2758
2758
 
2759
+ // src/util/set-abort-timeout.ts
2760
+ function setAbortTimeout({
2761
+ abortController,
2762
+ label,
2763
+ timeoutMs
2764
+ }) {
2765
+ if (abortController == null || timeoutMs == null) {
2766
+ return void 0;
2767
+ }
2768
+ return setTimeout(
2769
+ () => abortController.abort(
2770
+ new DOMException(
2771
+ `${label} timeout of ${timeoutMs}ms exceeded`,
2772
+ "TimeoutError"
2773
+ )
2774
+ ),
2775
+ timeoutMs
2776
+ );
2777
+ }
2778
+
2759
2779
  // src/generate-text/collect-tool-approvals.ts
2760
2780
  function collectToolApprovals({
2761
2781
  messages
@@ -3724,6 +3744,7 @@ var array = ({
3724
3744
  finishReason: context2.finishReason
3725
3745
  });
3726
3746
  }
3747
+ const validatedElements = [];
3727
3748
  for (const element of outerValue.elements) {
3728
3749
  const validationResult = await safeValidateTypes3({
3729
3750
  value: element,
@@ -3739,8 +3760,9 @@ var array = ({
3739
3760
  finishReason: context2.finishReason
3740
3761
  });
3741
3762
  }
3763
+ validatedElements.push(validationResult.value);
3742
3764
  }
3743
- return outerValue.elements;
3765
+ return validatedElements;
3744
3766
  },
3745
3767
  async parsePartialOutput({ text: text2 }) {
3746
3768
  const result = await parsePartialJson(text2);
@@ -4547,7 +4569,11 @@ async function generateText({
4547
4569
  const steps = [];
4548
4570
  const pendingDeferredToolCalls = /* @__PURE__ */ new Map();
4549
4571
  do {
4550
- const stepTimeoutId = stepTimeoutMs != null ? setTimeout(() => stepAbortController.abort(), stepTimeoutMs) : void 0;
4572
+ const stepTimeoutId = setAbortTimeout({
4573
+ abortController: stepAbortController,
4574
+ label: "Step",
4575
+ timeoutMs: stepTimeoutMs
4576
+ });
4551
4577
  try {
4552
4578
  const stepInputMessages = [...initialMessages, ...responseMessages];
4553
4579
  const prepareStepResult = await (prepareStep == null ? void 0 : prepareStep({
@@ -6519,7 +6545,6 @@ function runToolsTransformation({
6519
6545
  }
6520
6546
  });
6521
6547
  const outstandingToolResults = /* @__PURE__ */ new Set();
6522
- const toolInputs = /* @__PURE__ */ new Map();
6523
6548
  const toolCallsByToolCallId = /* @__PURE__ */ new Map();
6524
6549
  let canClose = false;
6525
6550
  let finishChunk = void 0;
@@ -6651,7 +6676,6 @@ function runToolsTransformation({
6651
6676
  });
6652
6677
  break;
6653
6678
  }
6654
- toolInputs.set(toolCall.toolCallId, toolCall.input);
6655
6679
  if (tool2.execute != null && toolCall.providerExecuted !== true) {
6656
6680
  const toolExecutionId = generateId2();
6657
6681
  outstandingToolResults.add(toolExecutionId);
@@ -6695,7 +6719,7 @@ function runToolsTransformation({
6695
6719
  type: "tool-error",
6696
6720
  toolCallId: chunk.toolCallId,
6697
6721
  toolName,
6698
- input: toolInputs.get(chunk.toolCallId),
6722
+ input: toolCall == null ? void 0 : toolCall.input,
6699
6723
  providerExecuted: true,
6700
6724
  error: chunk.result,
6701
6725
  dynamic: chunk.dynamic,
@@ -6707,7 +6731,7 @@ function runToolsTransformation({
6707
6731
  type: "tool-result",
6708
6732
  toolCallId: chunk.toolCallId,
6709
6733
  toolName,
6710
- input: toolInputs.get(chunk.toolCallId),
6734
+ input: toolCall == null ? void 0 : toolCall.input,
6711
6735
  output: chunk.result,
6712
6736
  providerExecuted: true,
6713
6737
  dynamic: chunk.dynamic,
@@ -7507,18 +7531,21 @@ var DefaultStreamTextResult = class {
7507
7531
  }) {
7508
7532
  var _a22, _b, _c, _d, _e, _f, _g, _h, _i;
7509
7533
  const includeRawChunks2 = self.includeRawChunks;
7510
- const stepTimeoutId = stepTimeoutMs != null ? setTimeout(() => stepAbortController.abort(), stepTimeoutMs) : void 0;
7534
+ const stepTimeoutId = setAbortTimeout({
7535
+ abortController: stepAbortController,
7536
+ label: "Step",
7537
+ timeoutMs: stepTimeoutMs
7538
+ });
7511
7539
  let chunkTimeoutId = void 0;
7512
7540
  function resetChunkTimeout() {
7513
- if (chunkTimeoutMs != null) {
7514
- if (chunkTimeoutId != null) {
7515
- clearTimeout(chunkTimeoutId);
7516
- }
7517
- chunkTimeoutId = setTimeout(
7518
- () => chunkAbortController.abort(),
7519
- chunkTimeoutMs
7520
- );
7541
+ if (chunkTimeoutId != null) {
7542
+ clearTimeout(chunkTimeoutId);
7521
7543
  }
7544
+ chunkTimeoutId = setAbortTimeout({
7545
+ abortController: chunkAbortController,
7546
+ label: "Chunk",
7547
+ timeoutMs: chunkTimeoutMs
7548
+ });
7522
7549
  }
7523
7550
  function clearChunkTimeout() {
7524
7551
  if (chunkTimeoutId != null) {
@@ -7531,6 +7558,8 @@ var DefaultStreamTextResult = class {
7531
7558
  clearTimeout(stepTimeoutId);
7532
7559
  }
7533
7560
  }
7561
+ abortSignal == null ? void 0 : abortSignal.addEventListener("abort", clearStepTimeout);
7562
+ abortSignal == null ? void 0 : abortSignal.addEventListener("abort", clearChunkTimeout);
7534
7563
  try {
7535
7564
  stepFinish = new DelayedPromise();
7536
7565
  const stepInputMessages = [...initialMessages, ...responseMessages];
@@ -8026,9 +8055,10 @@ var DefaultStreamTextResult = class {
8026
8055
  })
8027
8056
  )
8028
8057
  );
8029
- } finally {
8058
+ } catch (error) {
8030
8059
  clearStepTimeout();
8031
8060
  clearChunkTimeout();
8061
+ throw error;
8032
8062
  }
8033
8063
  }
8034
8064
  await streamStep({
@@ -8950,10 +8980,12 @@ async function convertToModelMessages(messages, options) {
8950
8980
  throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
8951
8981
  }
8952
8982
  }
8953
- modelMessages.push({
8954
- role: "assistant",
8955
- content
8956
- });
8983
+ if (content.length > 0) {
8984
+ modelMessages.push({
8985
+ role: "assistant",
8986
+ content
8987
+ });
8988
+ }
8957
8989
  const toolParts = block.filter(
8958
8990
  (part) => {
8959
8991
  var _a23;
@@ -8984,7 +9016,7 @@ async function convertToModelMessages(messages, options) {
8984
9016
  toolName: getToolName(toolPart),
8985
9017
  output: {
8986
9018
  type: "error-text",
8987
- value: (_g = (_f = toolPart.approval) == null ? void 0 : _f.reason) != null ? _g : "Tool execution denied."
9019
+ value: (_g = (_f = toolPart.approval) == null ? void 0 : _f.reason) != null ? _g : "Tool call execution denied."
8988
9020
  },
8989
9021
  ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
8990
9022
  });
@@ -11861,13 +11893,14 @@ async function experimental_generateVideo({
11861
11893
  abortSignal
11862
11894
  });
11863
11895
  const { prompt, image } = normalizePrompt2(promptArg);
11864
- const normalizedFrameImages = frameImages == null ? void 0 : frameImages.map((frame) => ({
11865
- image: normalizeImageData(frame.image),
11866
- frameType: frame.frameType
11867
- }));
11868
- const normalizedInputReferences = inputReferences == null ? void 0 : inputReferences.map(
11869
- (reference) => normalizeImageData(reference)
11870
- );
11896
+ const normalizedFrameImages = frameImages == null ? void 0 : frameImages.flatMap((frame) => {
11897
+ const normalizedImage = normalizeImageData(frame.image);
11898
+ return normalizedImage != null ? [{ image: normalizedImage, frameType: frame.frameType }] : [];
11899
+ });
11900
+ const normalizedInputReferences = inputReferences == null ? void 0 : inputReferences.flatMap((reference) => {
11901
+ const normalized = normalizeReferenceData(reference);
11902
+ return normalized != null ? [normalized] : [];
11903
+ });
11871
11904
  const effectiveInputReferences = normalizedFrameImages != null && normalizedFrameImages.length > 0 ? void 0 : normalizedInputReferences;
11872
11905
  const warnings = [];
11873
11906
  if (normalizedFrameImages != null && normalizedFrameImages.length > 0 && normalizedInputReferences != null && normalizedInputReferences.length > 0) {
@@ -12021,8 +12054,12 @@ function normalizePrompt2(promptArg) {
12021
12054
  image: promptArg.image != null ? normalizeImageData(promptArg.image) : void 0
12022
12055
  };
12023
12056
  }
12024
- function normalizeImageData(dataContent) {
12025
- var _a22, _b;
12057
+ function detectFileMediaType(data, restrictToImages) {
12058
+ var _a22;
12059
+ const detected = restrictToImages ? detectMediaType({ data, signatures: imageMediaTypeSignatures }) : (_a22 = detectMediaType({ data, signatures: imageMediaTypeSignatures })) != null ? _a22 : detectMediaType({ data, signatures: videoMediaTypeSignatures });
12060
+ return detected != null ? detected : "image/png";
12061
+ }
12062
+ function normalizeImageData(dataContent, { restrictToImages = true } = {}) {
12026
12063
  if (typeof dataContent === "string") {
12027
12064
  if (dataContent.startsWith("http://") || dataContent.startsWith("https://")) {
12028
12065
  return {
@@ -12032,27 +12069,46 @@ function normalizeImageData(dataContent) {
12032
12069
  }
12033
12070
  if (dataContent.startsWith("data:")) {
12034
12071
  const { mediaType, base64Content } = splitDataUrl(dataContent);
12072
+ const data = convertBase64ToUint8Array6(base64Content != null ? base64Content : "");
12035
12073
  return {
12036
12074
  type: "file",
12037
- mediaType: mediaType != null ? mediaType : "image/png",
12038
- data: convertBase64ToUint8Array6(base64Content != null ? base64Content : "")
12075
+ mediaType: mediaType != null ? mediaType : detectFileMediaType(data, restrictToImages),
12076
+ data
12039
12077
  };
12040
12078
  }
12041
- const bytes2 = convertBase64ToUint8Array6(dataContent);
12079
+ const bytes = convertBase64ToUint8Array6(dataContent);
12042
12080
  return {
12043
12081
  type: "file",
12044
- mediaType: (_a22 = detectMediaType({
12045
- data: bytes2,
12046
- signatures: imageMediaTypeSignatures
12047
- })) != null ? _a22 : "image/png",
12048
- data: bytes2
12082
+ mediaType: detectFileMediaType(bytes, restrictToImages),
12083
+ data: bytes
12084
+ };
12085
+ }
12086
+ if (dataContent instanceof Uint8Array || dataContent instanceof ArrayBuffer) {
12087
+ const bytes = dataContent instanceof Uint8Array ? dataContent : new Uint8Array(dataContent);
12088
+ return {
12089
+ type: "file",
12090
+ mediaType: detectFileMediaType(bytes, restrictToImages),
12091
+ data: bytes
12049
12092
  };
12050
12093
  }
12051
- const bytes = convertDataContentToUint8Array(dataContent);
12094
+ return void 0;
12095
+ }
12096
+ function normalizeReferenceData(reference) {
12097
+ const isObjectForm = typeof reference === "object" && reference != null && !(reference instanceof Uint8Array) && !(reference instanceof ArrayBuffer) && "data" in reference;
12098
+ if (!isObjectForm) {
12099
+ return normalizeImageData(reference, {
12100
+ restrictToImages: false
12101
+ });
12102
+ }
12103
+ const normalized = normalizeImageData(reference.data, {
12104
+ restrictToImages: false
12105
+ });
12106
+ if (normalized == null) {
12107
+ return normalized;
12108
+ }
12052
12109
  return {
12053
- type: "file",
12054
- mediaType: (_b = detectMediaType({ data: bytes, signatures: imageMediaTypeSignatures })) != null ? _b : "image/png",
12055
- data: bytes
12110
+ ...normalized,
12111
+ ...reference.mediaType != null ? { mediaType: reference.mediaType } : {}
12056
12112
  };
12057
12113
  }
12058
12114
  async function invokeModelMaxVideosPerCall(model) {
@@ -13599,7 +13655,7 @@ var AbstractChat = class {
13599
13655
  const updatePart = (part) => isToolUIPart(part) && part.state === "approval-requested" && part.approval.id === id ? {
13600
13656
  ...part,
13601
13657
  state: "approval-responded",
13602
- approval: { id, approved, reason }
13658
+ approval: { ...part.approval, id, approved, reason }
13603
13659
  } : part;
13604
13660
  this.state.replaceMessage(messages.length - 1, {
13605
13661
  ...lastMessage,
@@ -14070,6 +14126,7 @@ export {
14070
14126
  hasToolCall,
14071
14127
  isDataUIPart,
14072
14128
  isDeepEqualData,
14129
+ isDynamicToolUIPart,
14073
14130
  isFileUIPart,
14074
14131
  isLoopFinished,
14075
14132
  isReasoningUIPart,