ai 6.0.268 → 6.0.270

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.268" : "0.0.0-test";
1196
+ var VERSION = true ? "6.0.270" : "0.0.0-test";
1197
1197
 
1198
1198
  // src/util/download/download.ts
1199
1199
  var download = async ({
@@ -6606,8 +6606,12 @@ function createStitchableStream() {
6606
6606
  let innerStreams = [];
6607
6607
  let controller = null;
6608
6608
  let isClosed = false;
6609
+ let isCancelled = false;
6609
6610
  let waitForNewStream = createResolvablePromise();
6610
6611
  const terminate = () => {
6612
+ if (isCancelled) {
6613
+ return;
6614
+ }
6611
6615
  isClosed = true;
6612
6616
  waitForNewStream.resolve();
6613
6617
  innerStreams.forEach(({ reader, onCancel }) => {
@@ -6618,7 +6622,10 @@ function createStitchableStream() {
6618
6622
  controller == null ? void 0 : controller.close();
6619
6623
  };
6620
6624
  const processPull = async () => {
6621
- var _a22, _b;
6625
+ var _a22;
6626
+ if (isCancelled) {
6627
+ return;
6628
+ }
6622
6629
  if (isClosed && innerStreams.length === 0) {
6623
6630
  controller == null ? void 0 : controller.close();
6624
6631
  return;
@@ -6628,8 +6635,12 @@ function createStitchableStream() {
6628
6635
  await waitForNewStream.promise;
6629
6636
  return processPull();
6630
6637
  }
6638
+ const currentStream = innerStreams[0];
6631
6639
  try {
6632
- const { value, done } = await innerStreams[0].reader.read();
6640
+ const { value, done } = await currentStream.reader.read();
6641
+ if (isCancelled) {
6642
+ return;
6643
+ }
6633
6644
  if (done) {
6634
6645
  innerStreams.shift();
6635
6646
  if (innerStreams.length === 0 && isClosed) {
@@ -6641,7 +6652,10 @@ function createStitchableStream() {
6641
6652
  controller == null ? void 0 : controller.enqueue(value);
6642
6653
  }
6643
6654
  } catch (error) {
6644
- (_b = (_a22 = innerStreams[0]).onError) == null ? void 0 : _b.call(_a22, error);
6655
+ if (isCancelled) {
6656
+ return;
6657
+ }
6658
+ (_a22 = currentStream.onError) == null ? void 0 : _a22.call(currentStream, error);
6645
6659
  controller == null ? void 0 : controller.error(error);
6646
6660
  innerStreams.shift();
6647
6661
  terminate();
@@ -6654,15 +6668,24 @@ function createStitchableStream() {
6654
6668
  },
6655
6669
  pull: processPull,
6656
6670
  async cancel() {
6671
+ isCancelled = true;
6672
+ isClosed = true;
6673
+ waitForNewStream.resolve();
6657
6674
  for (const { reader, onCancel } of innerStreams) {
6658
6675
  onCancel == null ? void 0 : onCancel();
6659
6676
  await reader.cancel();
6660
6677
  }
6661
6678
  innerStreams = [];
6662
- isClosed = true;
6663
6679
  }
6664
6680
  }),
6665
6681
  addStream: (innerStream, callbacks) => {
6682
+ var _a22;
6683
+ if (isCancelled) {
6684
+ (_a22 = callbacks == null ? void 0 : callbacks.onCancel) == null ? void 0 : _a22.call(callbacks);
6685
+ void innerStream.cancel().catch(() => {
6686
+ });
6687
+ return;
6688
+ }
6666
6689
  if (isClosed) {
6667
6690
  throw new Error("Cannot add inner stream: outer stream is closed");
6668
6691
  }
@@ -6677,6 +6700,9 @@ function createStitchableStream() {
6677
6700
  * finish processing and then close the outer stream.
6678
6701
  */
6679
6702
  close: () => {
6703
+ if (isCancelled) {
6704
+ return;
6705
+ }
6680
6706
  isClosed = true;
6681
6707
  waitForNewStream.resolve();
6682
6708
  if (innerStreams.length === 0) {
@@ -10067,6 +10093,12 @@ import {
10067
10093
  withUserAgentSuffix as withUserAgentSuffix4
10068
10094
  } from "@ai-sdk/provider-utils";
10069
10095
 
10096
+ // src/model/get-embedding-model-max-input-bytes-per-call.ts
10097
+ import { EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL } from "@ai-sdk/provider-utils";
10098
+ function getEmbeddingModelMaxInputBytesPerCall(model) {
10099
+ return model[EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL];
10100
+ }
10101
+
10070
10102
  // src/util/split-array.ts
10071
10103
  function splitArray(array2, chunkSize) {
10072
10104
  if (chunkSize <= 0) {
@@ -10122,11 +10154,18 @@ async function embedMany({
10122
10154
  tracer,
10123
10155
  fn: async (span) => {
10124
10156
  var _a22;
10125
- const [maxEmbeddingsPerCall, supportsParallelCalls] = await Promise.all([
10157
+ const [
10158
+ maxEmbeddingsPerCall,
10159
+ maxInputBytesPerCall,
10160
+ supportsParallelCalls
10161
+ ] = await Promise.all([
10126
10162
  model.maxEmbeddingsPerCall,
10163
+ getEmbeddingModelMaxInputBytesPerCall(model),
10127
10164
  model.supportsParallelCalls
10128
10165
  ]);
10129
- if (maxEmbeddingsPerCall == null || maxEmbeddingsPerCall === Infinity) {
10166
+ const hasEmbeddingLimit = maxEmbeddingsPerCall != null && maxEmbeddingsPerCall !== Infinity;
10167
+ const hasInputByteLimit = maxInputBytesPerCall != null && maxInputBytesPerCall !== Infinity;
10168
+ if (!hasEmbeddingLimit && !hasInputByteLimit) {
10130
10169
  const { embeddings: embeddings2, usage, warnings: warnings2, response, providerMetadata: providerMetadata2 } = await retry(() => {
10131
10170
  return recordSpan({
10132
10171
  name: "ai.embedMany.doEmbed",
@@ -10203,7 +10242,11 @@ async function embedMany({
10203
10242
  responses: [response]
10204
10243
  });
10205
10244
  }
10206
- const valueChunks = splitArray(values, maxEmbeddingsPerCall);
10245
+ const valueChunks = splitByEmbeddingLimits({
10246
+ values,
10247
+ maxEmbeddingsPerCall: hasEmbeddingLimit ? maxEmbeddingsPerCall : Infinity,
10248
+ maxInputBytesPerCall: hasInputByteLimit ? maxInputBytesPerCall : Infinity
10249
+ });
10207
10250
  const embeddings = [];
10208
10251
  const warnings = [];
10209
10252
  const responses = [];
@@ -10317,6 +10360,37 @@ async function embedMany({
10317
10360
  }
10318
10361
  });
10319
10362
  }
10363
+ var textEncoder = new TextEncoder();
10364
+ function splitByEmbeddingLimits({
10365
+ values,
10366
+ maxEmbeddingsPerCall,
10367
+ maxInputBytesPerCall
10368
+ }) {
10369
+ if (maxEmbeddingsPerCall <= 0) {
10370
+ throw new Error("maxEmbeddingsPerCall must be greater than 0");
10371
+ }
10372
+ if (maxInputBytesPerCall <= 0) {
10373
+ throw new Error("maxInputBytesPerCall must be greater than 0");
10374
+ }
10375
+ if (values.length === 0) {
10376
+ return [];
10377
+ }
10378
+ const chunks = [];
10379
+ let currentChunk = [];
10380
+ let currentInputBytes = 0;
10381
+ for (const value of values) {
10382
+ const inputBytes = textEncoder.encode(value).length;
10383
+ if (currentChunk.length > 0 && (currentChunk.length >= maxEmbeddingsPerCall || currentInputBytes + inputBytes > maxInputBytesPerCall)) {
10384
+ chunks.push(currentChunk);
10385
+ currentChunk = [];
10386
+ currentInputBytes = 0;
10387
+ }
10388
+ currentChunk.push(value);
10389
+ currentInputBytes += inputBytes;
10390
+ }
10391
+ chunks.push(currentChunk);
10392
+ return chunks;
10393
+ }
10320
10394
  var DefaultEmbedManyResult = class {
10321
10395
  constructor(options) {
10322
10396
  this.values = options.values;
@@ -13014,6 +13088,7 @@ var doWrap = ({
13014
13088
  };
13015
13089
 
13016
13090
  // src/middleware/wrap-embedding-model.ts
13091
+ import { EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL as EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL2 } from "@ai-sdk/provider-utils";
13017
13092
  var wrapEmbeddingModel = ({
13018
13093
  model,
13019
13094
  middleware: middlewareArg,
@@ -13048,6 +13123,7 @@ var doWrap2 = ({
13048
13123
  provider: (_a22 = providerId != null ? providerId : overrideProvider == null ? void 0 : overrideProvider({ model })) != null ? _a22 : model.provider,
13049
13124
  modelId: (_b = modelId != null ? modelId : overrideModelId == null ? void 0 : overrideModelId({ model })) != null ? _b : model.modelId,
13050
13125
  maxEmbeddingsPerCall: (_c = overrideMaxEmbeddingsPerCall == null ? void 0 : overrideMaxEmbeddingsPerCall({ model })) != null ? _c : model.maxEmbeddingsPerCall,
13126
+ [EXPERIMENTAL_EMBEDDING_MODEL_MAX_INPUT_BYTES_PER_CALL2]: getEmbeddingModelMaxInputBytesPerCall(model),
13051
13127
  supportsParallelCalls: (_d = overrideSupportsParallelCalls == null ? void 0 : overrideSupportsParallelCalls({ model })) != null ? _d : model.supportsParallelCalls,
13052
13128
  async doEmbed(params) {
13053
13129
  const transformedParams = await doTransform({ params });
@@ -14437,7 +14513,7 @@ function lastAssistantMessageIsCompleteWithApprovalResponses({
14437
14513
  // has at least one tool approval response
14438
14514
  lastStepToolInvocations.filter((part) => part.state === "approval-responded").length > 0 && // all tool approvals must have a response
14439
14515
  lastStepToolInvocations.every(
14440
- (part) => part.state === "output-available" || part.state === "output-error" || part.state === "approval-responded"
14516
+ (part) => part.state === "output-available" || part.state === "output-error" || part.state === "output-denied" || part.state === "approval-responded"
14441
14517
  )
14442
14518
  );
14443
14519
  }