ai 7.0.54 → 7.0.56

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.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  createIdGenerator as createIdGenerator9,
12
12
  dynamicTool,
13
13
  experimental_toolCaller,
14
- generateId,
14
+ generateId as generateId2,
15
15
  jsonSchema,
16
16
  parseJsonEventStream as parseJsonEventStream3,
17
17
  tool,
@@ -1143,7 +1143,7 @@ import {
1143
1143
  } from "@ai-sdk/provider-utils";
1144
1144
 
1145
1145
  // src/version.ts
1146
- var VERSION = true ? "7.0.54" : "0.0.0-test";
1146
+ var VERSION = true ? "7.0.56" : "0.0.0-test";
1147
1147
 
1148
1148
  // src/util/download/download.ts
1149
1149
  var download = async ({
@@ -5227,7 +5227,7 @@ async function generateText({
5227
5227
  experimental_include,
5228
5228
  include = experimental_include,
5229
5229
  _internal: {
5230
- generateId: generateId2 = originalGenerateId,
5230
+ generateId: generateId3 = originalGenerateId,
5231
5231
  generateCallId = originalGenerateCallId,
5232
5232
  now: now2 = now
5233
5233
  } = {},
@@ -5612,7 +5612,7 @@ async function generateText({
5612
5612
  }
5613
5613
  );
5614
5614
  const responseData = {
5615
- id: (_b3 = (_a26 = result.response) == null ? void 0 : _a26.id) != null ? _b3 : generateId2(),
5615
+ id: (_b3 = (_a26 = result.response) == null ? void 0 : _a26.id) != null ? _b3 : generateId3(),
5616
5616
  timestamp: (_d3 = (_c3 = result.response) == null ? void 0 : _c3.timestamp) != null ? _d3 : /* @__PURE__ */ new Date(),
5617
5617
  modelId: (_f2 = (_e2 = result.response) == null ? void 0 : _e2.modelId) != null ? _f2 : stepModel.modelId,
5618
5618
  headers: (_g2 = result.response) == null ? void 0 : _g2.headers,
@@ -5658,6 +5658,9 @@ async function generateText({
5658
5658
  usage: stepUsage,
5659
5659
  content: modelCallContent,
5660
5660
  responseId: currentModelResponse.response.id,
5661
+ ...currentModelResponse.providerMetadata != null ? {
5662
+ providerMetadata: currentModelResponse.providerMetadata
5663
+ } : {},
5661
5664
  performance: {
5662
5665
  responseTimeMs,
5663
5666
  effectiveOutputTokensPerSecond: calculateTokensPerSecond({
@@ -5717,7 +5720,7 @@ async function generateText({
5717
5720
  if (toolApprovalStatus.type === "not-applicable") {
5718
5721
  continue;
5719
5722
  }
5720
- const approvalId = generateId2();
5723
+ const approvalId = generateId3();
5721
5724
  const signature = await maybeSignApproval({
5722
5725
  secret: experimental_toolApprovalSecret,
5723
5726
  approvalId,
@@ -7886,9 +7889,19 @@ function asAsyncIterableStream(stream) {
7886
7889
  // src/util/consume-stream.ts
7887
7890
  async function consumeStream({
7888
7891
  stream,
7889
- onError
7892
+ onError,
7893
+ abortSignal
7890
7894
  }) {
7891
7895
  const reader = stream.getReader();
7896
+ const cancelOnAbort = () => {
7897
+ reader.cancel().catch(() => {
7898
+ });
7899
+ };
7900
+ if (abortSignal == null ? void 0 : abortSignal.aborted) {
7901
+ cancelOnAbort();
7902
+ } else {
7903
+ abortSignal == null ? void 0 : abortSignal.addEventListener("abort", cancelOnAbort, { once: true });
7904
+ }
7892
7905
  try {
7893
7906
  while (true) {
7894
7907
  const { done } = await reader.read();
@@ -7898,6 +7911,7 @@ async function consumeStream({
7898
7911
  } catch (error) {
7899
7912
  onError == null ? void 0 : onError(error);
7900
7913
  } finally {
7914
+ abortSignal == null ? void 0 : abortSignal.removeEventListener("abort", cancelOnAbort);
7901
7915
  reader.releaseLock();
7902
7916
  }
7903
7917
  }
@@ -8021,7 +8035,7 @@ function executeToolsFromStream({
8021
8035
  toolApproval,
8022
8036
  runtimeContext,
8023
8037
  toolApprovalSecret,
8024
- generateId: generateId2,
8038
+ generateId: generateId3,
8025
8039
  onToolExecutionStart,
8026
8040
  onToolExecutionEnd,
8027
8041
  executeToolInTelemetryContext,
@@ -8056,7 +8070,7 @@ function executeToolsFromStream({
8056
8070
  }
8057
8071
  return;
8058
8072
  }
8059
- const approvalId = generateId2();
8073
+ const approvalId = generateId3();
8060
8074
  const signature = await maybeSignApproval({
8061
8075
  secret: toolApprovalSecret,
8062
8076
  approvalId,
@@ -8262,7 +8276,7 @@ async function streamLanguageModelCall({
8262
8276
  toolsContext,
8263
8277
  experimental_sandbox: sandbox,
8264
8278
  _internal: {
8265
- generateId: generateId2 = originalGenerateId2,
8279
+ generateId: generateId3 = originalGenerateId2,
8266
8280
  generateCallId = originalGenerateCallId2,
8267
8281
  now: now2 = now
8268
8282
  } = {},
@@ -8347,7 +8361,7 @@ async function streamLanguageModelCall({
8347
8361
  callId: effectiveCallId,
8348
8362
  provider: resolvedModel.provider,
8349
8363
  modelId: resolvedModel.modelId,
8350
- generateId: generateId2,
8364
+ generateId: generateId3,
8351
8365
  now: now2,
8352
8366
  callStartTimestampMs,
8353
8367
  onLanguageModelCallEnd
@@ -8368,7 +8382,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8368
8382
  callId,
8369
8383
  provider,
8370
8384
  modelId,
8371
- generateId: generateId2,
8385
+ generateId: generateId3,
8372
8386
  now: now2,
8373
8387
  callStartTimestampMs,
8374
8388
  onLanguageModelCallEnd
@@ -8377,7 +8391,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8377
8391
  const modelCallContent = [];
8378
8392
  const textPartIndexes = /* @__PURE__ */ new Map();
8379
8393
  const reasoningPartIndexes = /* @__PURE__ */ new Map();
8380
- let responseId = generateId2();
8394
+ let responseId = generateId3();
8381
8395
  let responseModelId = modelId;
8382
8396
  let timeToFirstOutputMs;
8383
8397
  let previousOutputChunkTimestampMs;
@@ -8522,6 +8536,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8522
8536
  usage,
8523
8537
  content: modelCallContent,
8524
8538
  responseId,
8539
+ ...chunk.providerMetadata != null ? { providerMetadata: chunk.providerMetadata } : {},
8525
8540
  performance
8526
8541
  },
8527
8542
  callbacks: onLanguageModelCallEnd
@@ -8811,7 +8826,7 @@ function streamText({
8811
8826
  include = experimental_include,
8812
8827
  _internal: {
8813
8828
  now: now2 = now,
8814
- generateId: generateId2 = originalGenerateId3,
8829
+ generateId: generateId3 = originalGenerateId3,
8815
8830
  generateCallId = originalGenerateCallId3
8816
8831
  } = {},
8817
8832
  ...settings
@@ -8885,7 +8900,7 @@ function streamText({
8885
8900
  onToolExecutionStart: resolvedOnToolExecutionStart,
8886
8901
  onToolExecutionEnd: resolvedOnToolExecutionEnd,
8887
8902
  now: now2,
8888
- generateId: generateId2,
8903
+ generateId: generateId3,
8889
8904
  generateCallId,
8890
8905
  download: download2,
8891
8906
  // assign default values to include:
@@ -9003,7 +9018,7 @@ var DefaultStreamTextResult = class {
9003
9018
  providerOptions,
9004
9019
  prepareStep,
9005
9020
  now: now2,
9006
- generateId: generateId2,
9021
+ generateId: generateId3,
9007
9022
  generateCallId,
9008
9023
  timeout,
9009
9024
  onChunk,
@@ -9771,7 +9786,7 @@ var DefaultStreamTextResult = class {
9771
9786
  toolApproval,
9772
9787
  runtimeContext,
9773
9788
  toolApprovalSecret: experimental_toolApprovalSecret,
9774
- generateId: generateId2,
9789
+ generateId: generateId3,
9775
9790
  // the callbacks need to be passed down and handled by executeToolCall
9776
9791
  // to guarantee that the onToolExecutionStart callback is invoked before the tool execute function
9777
9792
  onToolExecutionStart: filterNullable2(
@@ -9813,7 +9828,7 @@ var DefaultStreamTextResult = class {
9813
9828
  };
9814
9829
  const toolExecutionMs = {};
9815
9830
  let stepResponse = {
9816
- id: generateId2(),
9831
+ id: generateId3(),
9817
9832
  timestamp: /* @__PURE__ */ new Date(),
9818
9833
  modelId: model.modelId
9819
9834
  };
@@ -10577,7 +10592,7 @@ function createUIMessageStream({
10577
10592
  onStepFinish,
10578
10593
  onEnd,
10579
10594
  onFinish,
10580
- generateId: generateId2 = generateIdFunc
10595
+ generateId: generateId3 = generateIdFunc
10581
10596
  }) {
10582
10597
  let controller;
10583
10598
  const ongoingStreamPromises = [];
@@ -10649,7 +10664,7 @@ function createUIMessageStream({
10649
10664
  });
10650
10665
  return handleUIMessageStreamFinish({
10651
10666
  stream,
10652
- messageId: generateId2(),
10667
+ messageId: generateId3(),
10653
10668
  originalMessages,
10654
10669
  onStepEnd: onStepEnd != null ? onStepEnd : onStepFinish,
10655
10670
  onEnd: onEnd != null ? onEnd : onFinish,
@@ -11532,10 +11547,254 @@ async function pipeAgentUIStreamToResponse({
11532
11547
  });
11533
11548
  }
11534
11549
 
11550
+ // src/batch/batch.ts
11551
+ import {
11552
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError3
11553
+ } from "@ai-sdk/provider";
11554
+ import { withUserAgentSuffix as withUserAgentSuffix4 } from "@ai-sdk/provider-utils";
11555
+ async function startTextBatch({
11556
+ model: modelArg,
11557
+ requests,
11558
+ providerOptions,
11559
+ abortSignal,
11560
+ headers,
11561
+ timeout
11562
+ }) {
11563
+ validateRequests(requests);
11564
+ const model = resolveBatchLanguageModel(modelArg);
11565
+ const operationAbortSignal = mergeAbortSignals(
11566
+ abortSignal,
11567
+ getTotalTimeoutMs(timeout)
11568
+ );
11569
+ const supportedUrls = await model.supportedUrls;
11570
+ operationAbortSignal == null ? void 0 : operationAbortSignal.throwIfAborted();
11571
+ const normalizedRequests = [];
11572
+ for (const request of requests) {
11573
+ const standardizedPrompt = await standardizePrompt(request);
11574
+ normalizedRequests.push({
11575
+ id: request.id,
11576
+ options: {
11577
+ ...prepareLanguageModelCallOptions(request),
11578
+ prompt: await convertToLanguageModelPrompt({
11579
+ prompt: standardizedPrompt,
11580
+ supportedUrls,
11581
+ download: void 0,
11582
+ provider: model.provider.split(".")[0]
11583
+ }),
11584
+ providerOptions: request.providerOptions
11585
+ }
11586
+ });
11587
+ operationAbortSignal == null ? void 0 : operationAbortSignal.throwIfAborted();
11588
+ }
11589
+ const headersWithUserAgent = withUserAgentSuffix4(
11590
+ headers != null ? headers : {},
11591
+ `ai/${VERSION}`
11592
+ );
11593
+ try {
11594
+ const result = await model.experimental_doStartBatch({
11595
+ requests: normalizedRequests,
11596
+ providerOptions,
11597
+ abortSignal: operationAbortSignal,
11598
+ headers: headersWithUserAgent
11599
+ });
11600
+ const { batchId, warnings, ...status } = result;
11601
+ logWarnings({
11602
+ warnings: warnings.map(({ warning }) => warning),
11603
+ provider: model.provider,
11604
+ model: model.modelId
11605
+ });
11606
+ return {
11607
+ version: 1,
11608
+ type: "text",
11609
+ id: batchId,
11610
+ provider: model.provider,
11611
+ modelId: model.modelId,
11612
+ ...status,
11613
+ warnings
11614
+ };
11615
+ } catch (error) {
11616
+ throw wrapGatewayError(error);
11617
+ }
11618
+ }
11619
+ async function getBatchStatus({
11620
+ model: modelArg,
11621
+ batch,
11622
+ providerOptions,
11623
+ maxRetries,
11624
+ abortSignal,
11625
+ headers,
11626
+ timeout
11627
+ }) {
11628
+ const model = resolveBatchLanguageModel(modelArg);
11629
+ validateBatchReference({ model, batch });
11630
+ const operationAbortSignal = mergeAbortSignals(
11631
+ abortSignal,
11632
+ getTotalTimeoutMs(timeout)
11633
+ );
11634
+ const { retry } = prepareRetries({
11635
+ maxRetries,
11636
+ abortSignal: operationAbortSignal
11637
+ });
11638
+ try {
11639
+ const status = await retry(
11640
+ () => model.experimental_doGetBatchStatus({
11641
+ batchId: batch.id,
11642
+ providerOptions,
11643
+ abortSignal: operationAbortSignal,
11644
+ headers: withUserAgentSuffix4(headers != null ? headers : {}, `ai/${VERSION}`)
11645
+ })
11646
+ );
11647
+ return status;
11648
+ } catch (error) {
11649
+ throw wrapGatewayError(error);
11650
+ }
11651
+ }
11652
+ function getBatchResults({
11653
+ model: modelArg,
11654
+ batch,
11655
+ providerOptions,
11656
+ maxRetries,
11657
+ abortSignal,
11658
+ headers,
11659
+ timeout
11660
+ }) {
11661
+ const model = resolveBatchLanguageModel(modelArg);
11662
+ validateBatchReference({ model, batch });
11663
+ const streamAbortController = new AbortController();
11664
+ const operationAbortSignal = mergeAbortSignals(
11665
+ abortSignal,
11666
+ getTotalTimeoutMs(timeout),
11667
+ streamAbortController.signal
11668
+ );
11669
+ const { retry } = prepareRetries({
11670
+ maxRetries,
11671
+ abortSignal: operationAbortSignal
11672
+ });
11673
+ const transformer = {
11674
+ transform(item, controller) {
11675
+ controller.enqueue(convertBatchItemResult(item));
11676
+ },
11677
+ cancel(reason) {
11678
+ streamAbortController.abort(
11679
+ reason != null ? reason : new Error("Batch results stream was cancelled.")
11680
+ );
11681
+ }
11682
+ };
11683
+ const transform = new TransformStream(transformer);
11684
+ void (async () => {
11685
+ try {
11686
+ const stream = await retry(
11687
+ () => model.experimental_doGetBatchResults({
11688
+ batchId: batch.id,
11689
+ providerOptions,
11690
+ abortSignal: operationAbortSignal,
11691
+ headers: withUserAgentSuffix4(headers != null ? headers : {}, `ai/${VERSION}`)
11692
+ })
11693
+ );
11694
+ await stream.pipeTo(transform.writable, {
11695
+ signal: operationAbortSignal
11696
+ });
11697
+ } catch (error) {
11698
+ await transform.writable.abort(wrapGatewayError(error)).catch(() => {
11699
+ });
11700
+ }
11701
+ })();
11702
+ return asAsyncIterableStream(transform.readable);
11703
+ }
11704
+ function resolveBatchLanguageModel(modelArg) {
11705
+ const model = resolveLanguageModel(modelArg);
11706
+ if (!isBatchLanguageModel(model)) {
11707
+ throw new UnsupportedFunctionalityError3({
11708
+ functionality: "batch processing",
11709
+ message: `The ${model.provider} model "${model.modelId}" does not support batch processing.`
11710
+ });
11711
+ }
11712
+ return model;
11713
+ }
11714
+ function isBatchLanguageModel(model) {
11715
+ const candidate = model;
11716
+ return typeof candidate.experimental_doStartBatch === "function" && typeof candidate.experimental_doGetBatchStatus === "function" && typeof candidate.experimental_doGetBatchResults === "function";
11717
+ }
11718
+ function validateRequests(requests) {
11719
+ if (requests.length === 0) {
11720
+ throw new InvalidArgumentError({
11721
+ parameter: "requests",
11722
+ value: requests,
11723
+ message: "requests must not be empty"
11724
+ });
11725
+ }
11726
+ const ids = /* @__PURE__ */ new Set();
11727
+ for (const request of requests) {
11728
+ if (request.id.trim().length === 0) {
11729
+ throw new InvalidArgumentError({
11730
+ parameter: "requests",
11731
+ value: requests,
11732
+ message: "request IDs must not be empty"
11733
+ });
11734
+ }
11735
+ if (ids.has(request.id)) {
11736
+ throw new InvalidArgumentError({
11737
+ parameter: "requests",
11738
+ value: requests,
11739
+ message: `request IDs must be unique; duplicate ID "${request.id}"`
11740
+ });
11741
+ }
11742
+ ids.add(request.id);
11743
+ }
11744
+ }
11745
+ function validateBatchReference({
11746
+ model,
11747
+ batch
11748
+ }) {
11749
+ if (batch.version !== 1 || batch.type !== "text") {
11750
+ throw new InvalidArgumentError({
11751
+ parameter: "batch",
11752
+ value: batch,
11753
+ message: "batch must be a supported text batch reference"
11754
+ });
11755
+ }
11756
+ if (batch.provider !== model.provider || batch.modelId !== model.modelId) {
11757
+ throw new InvalidArgumentError({
11758
+ parameter: "model",
11759
+ value: model,
11760
+ message: `model ${model.provider}:${model.modelId} is not compatible with batch ${batch.provider}:${batch.modelId}`
11761
+ });
11762
+ }
11763
+ }
11764
+ function convertBatchItemResult(item) {
11765
+ if (item.status !== "succeeded") {
11766
+ return item;
11767
+ }
11768
+ return {
11769
+ id: item.id,
11770
+ status: "succeeded",
11771
+ ...convertGenerateResult(item.result)
11772
+ };
11773
+ }
11774
+ function convertGenerateResult(result) {
11775
+ var _a23;
11776
+ return {
11777
+ text: result.content.filter(
11778
+ (part) => part.type === "text"
11779
+ ).map((part) => part.text).join(""),
11780
+ finishReason: result.finishReason.unified,
11781
+ rawFinishReason: result.finishReason.raw,
11782
+ usage: asLanguageModelUsage(result.usage),
11783
+ ...result.response != null ? {
11784
+ response: {
11785
+ id: result.response.id,
11786
+ timestamp: (_a23 = result.response.timestamp) == null ? void 0 : _a23.toISOString(),
11787
+ modelId: result.response.modelId
11788
+ }
11789
+ } : {},
11790
+ providerMetadata: result.providerMetadata
11791
+ };
11792
+ }
11793
+
11535
11794
  // src/embed/embed.ts
11536
11795
  import {
11537
11796
  createIdGenerator as createIdGenerator4,
11538
- withUserAgentSuffix as withUserAgentSuffix4
11797
+ withUserAgentSuffix as withUserAgentSuffix5
11539
11798
  } from "@ai-sdk/provider-utils";
11540
11799
  var originalGenerateCallId4 = createIdGenerator4({
11541
11800
  prefix: "call",
@@ -11564,7 +11823,7 @@ async function embed({
11564
11823
  });
11565
11824
  const resolvedOnStart = onStart != null ? onStart : experimental_onStart;
11566
11825
  const resolvedOnEnd = onEnd != null ? onEnd : experimental_onEnd;
11567
- const headersWithUserAgent = withUserAgentSuffix4(
11826
+ const headersWithUserAgent = withUserAgentSuffix5(
11568
11827
  headers != null ? headers : {},
11569
11828
  `ai/${VERSION}`
11570
11829
  );
@@ -11685,7 +11944,7 @@ var DefaultEmbedResult = class {
11685
11944
  // src/embed/embed-many.ts
11686
11945
  import {
11687
11946
  createIdGenerator as createIdGenerator5,
11688
- withUserAgentSuffix as withUserAgentSuffix5
11947
+ withUserAgentSuffix as withUserAgentSuffix6
11689
11948
  } from "@ai-sdk/provider-utils";
11690
11949
 
11691
11950
  // src/util/split-array.ts
@@ -11729,7 +11988,7 @@ async function embedMany({
11729
11988
  });
11730
11989
  const resolvedOnStart = onStart != null ? onStart : experimental_onStart;
11731
11990
  const resolvedOnEnd = onEnd != null ? onEnd : experimental_onEnd;
11732
- const headersWithUserAgent = withUserAgentSuffix5(
11991
+ const headersWithUserAgent = withUserAgentSuffix6(
11733
11992
  headers != null ? headers : {},
11734
11993
  `ai/${VERSION}`
11735
11994
  );
@@ -11963,7 +12222,7 @@ var DefaultEmbedManyResult = class {
11963
12222
  import {
11964
12223
  convertBase64ToUint8Array as convertBase64ToUint8Array4,
11965
12224
  detectMediaType as detectMediaType2,
11966
- withUserAgentSuffix as withUserAgentSuffix6
12225
+ withUserAgentSuffix as withUserAgentSuffix7
11967
12226
  } from "@ai-sdk/provider-utils";
11968
12227
 
11969
12228
  // src/prompt/data-content.ts
@@ -12017,7 +12276,7 @@ async function generateImage({
12017
12276
  }) {
12018
12277
  var _a23, _b;
12019
12278
  const model = resolveImageModel(modelArg);
12020
- const headersWithUserAgent = withUserAgentSuffix6(
12279
+ const headersWithUserAgent = withUserAgentSuffix7(
12021
12280
  headers != null ? headers : {},
12022
12281
  `ai/${VERSION}`
12023
12282
  );
@@ -12185,7 +12444,7 @@ function toImageModelV4File(dataContent) {
12185
12444
  // src/generate-object/generate-object.ts
12186
12445
  import {
12187
12446
  createIdGenerator as createIdGenerator6,
12188
- withUserAgentSuffix as withUserAgentSuffix7
12447
+ withUserAgentSuffix as withUserAgentSuffix8
12189
12448
  } from "@ai-sdk/provider-utils";
12190
12449
 
12191
12450
  // src/generate-text/extract-reasoning-content.ts
@@ -12212,7 +12471,7 @@ import {
12212
12471
  isJSONArray,
12213
12472
  isJSONObject,
12214
12473
  TypeValidationError as TypeValidationError4,
12215
- UnsupportedFunctionalityError as UnsupportedFunctionalityError3
12474
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError4
12216
12475
  } from "@ai-sdk/provider";
12217
12476
  import {
12218
12477
  asSchema as asSchema5,
@@ -12237,7 +12496,7 @@ var noSchemaOutputStrategy = {
12237
12496
  } : { success: true, value };
12238
12497
  },
12239
12498
  createElementStream() {
12240
- throw new UnsupportedFunctionalityError3({
12499
+ throw new UnsupportedFunctionalityError4({
12241
12500
  functionality: "element streams in no-schema mode"
12242
12501
  });
12243
12502
  }
@@ -12259,7 +12518,7 @@ var objectOutputStrategy = (schema) => ({
12259
12518
  return safeValidateTypes5({ value, schema });
12260
12519
  },
12261
12520
  createElementStream() {
12262
- throw new UnsupportedFunctionalityError3({
12521
+ throw new UnsupportedFunctionalityError4({
12263
12522
  functionality: "element streams in object mode"
12264
12523
  });
12265
12524
  }
@@ -12450,7 +12709,7 @@ var enumOutputStrategy = (enumValues) => {
12450
12709
  };
12451
12710
  },
12452
12711
  createElementStream() {
12453
- throw new UnsupportedFunctionalityError3({
12712
+ throw new UnsupportedFunctionalityError4({
12454
12713
  functionality: "element streams in enum mode"
12455
12714
  });
12456
12715
  }
@@ -12681,7 +12940,7 @@ async function generateObject(options) {
12681
12940
  onStepFinish,
12682
12941
  onFinish,
12683
12942
  _internal: {
12684
- generateId: generateId2 = originalGenerateId4,
12943
+ generateId: generateId3 = originalGenerateId4,
12685
12944
  currentDate = () => /* @__PURE__ */ new Date()
12686
12945
  } = {},
12687
12946
  ...settings
@@ -12710,7 +12969,7 @@ async function generateObject(options) {
12710
12969
  enumValues
12711
12970
  });
12712
12971
  const callSettings = prepareLanguageModelCallOptions(settings);
12713
- const headersWithUserAgent = withUserAgentSuffix7(
12972
+ const headersWithUserAgent = withUserAgentSuffix8(
12714
12973
  headers != null ? headers : {},
12715
12974
  `ai/${VERSION}`
12716
12975
  );
@@ -12721,7 +12980,7 @@ async function generateObject(options) {
12721
12980
  const resolvedOnStepStart = onStepStart != null ? onStepStart : experimental_onStepStart;
12722
12981
  const resolvedOnStepEnd = onStepEnd != null ? onStepEnd : onStepFinish;
12723
12982
  const jsonSchema2 = await outputStrategy.jsonSchema();
12724
- const callId = generateId2();
12983
+ const callId = generateId3();
12725
12984
  await notify({
12726
12985
  event: {
12727
12986
  callId,
@@ -12790,7 +13049,7 @@ async function generateObject(options) {
12790
13049
  })
12791
13050
  );
12792
13051
  const responseData = {
12793
- id: (_b = (_a23 = generateResult.response) == null ? void 0 : _a23.id) != null ? _b : generateId2(),
13052
+ id: (_b = (_a23 = generateResult.response) == null ? void 0 : _a23.id) != null ? _b : generateId3(),
12794
13053
  timestamp: (_d = (_c = generateResult.response) == null ? void 0 : _c.timestamp) != null ? _d : currentDate(),
12795
13054
  modelId: (_f = (_e = generateResult.response) == null ? void 0 : _e.modelId) != null ? _f : model.modelId,
12796
13055
  headers: (_g = generateResult.response) == null ? void 0 : _g.headers,
@@ -13070,7 +13329,7 @@ function streamObject(options) {
13070
13329
  },
13071
13330
  onFinish,
13072
13331
  _internal: {
13073
- generateId: generateId2 = originalGenerateId5,
13332
+ generateId: generateId3 = originalGenerateId5,
13074
13333
  currentDate = () => /* @__PURE__ */ new Date(),
13075
13334
  now: now2 = now
13076
13335
  } = {},
@@ -13117,7 +13376,7 @@ function streamObject(options) {
13117
13376
  onError,
13118
13377
  onFinish,
13119
13378
  download: download2,
13120
- generateId: generateId2,
13379
+ generateId: generateId3,
13121
13380
  currentDate,
13122
13381
  now: now2
13123
13382
  });
@@ -13146,7 +13405,7 @@ var DefaultStreamObjectResult = class {
13146
13405
  onError,
13147
13406
  onFinish,
13148
13407
  download: download2,
13149
- generateId: generateId2,
13408
+ generateId: generateId3,
13150
13409
  currentDate,
13151
13410
  now: now2
13152
13411
  }) {
@@ -13177,7 +13436,7 @@ var DefaultStreamObjectResult = class {
13177
13436
  }
13178
13437
  });
13179
13438
  this.baseStream = stitchableStream.stream.pipeThrough(eventProcessor);
13180
- const callId = generateId2();
13439
+ const callId = generateId3();
13181
13440
  (async () => {
13182
13441
  const jsonSchema2 = await outputStrategy.jsonSchema();
13183
13442
  await notify({
@@ -13274,7 +13533,7 @@ var DefaultStreamObjectResult = class {
13274
13533
  let accumulatedText = "";
13275
13534
  let textDelta = "";
13276
13535
  let fullResponse = {
13277
- id: generateId2(),
13536
+ id: generateId3(),
13278
13537
  timestamp: currentDate(),
13279
13538
  modelId: model.modelId
13280
13539
  };
@@ -13548,7 +13807,7 @@ var DefaultStreamObjectResult = class {
13548
13807
  // src/generate-speech/generate-speech.ts
13549
13808
  import {
13550
13809
  detectMediaType as detectMediaType3,
13551
- withUserAgentSuffix as withUserAgentSuffix8
13810
+ withUserAgentSuffix as withUserAgentSuffix9
13552
13811
  } from "@ai-sdk/provider-utils";
13553
13812
 
13554
13813
  // src/generate-speech/generated-audio-file.ts
@@ -13595,7 +13854,7 @@ async function generateSpeech({
13595
13854
  if (!resolvedModel) {
13596
13855
  throw new Error("Model could not be resolved");
13597
13856
  }
13598
- const headersWithUserAgent = withUserAgentSuffix8(
13857
+ const headersWithUserAgent = withUserAgentSuffix9(
13599
13858
  headers != null ? headers : {},
13600
13859
  `ai/${VERSION}`
13601
13860
  );
@@ -13892,7 +14151,8 @@ function detectToolDrift(current, baseline) {
13892
14151
  import {
13893
14152
  convertBase64ToUint8Array as convertBase64ToUint8Array5,
13894
14153
  delay as defaultDelay,
13895
- withUserAgentSuffix as withUserAgentSuffix9,
14154
+ generateId,
14155
+ withUserAgentSuffix as withUserAgentSuffix10,
13896
14156
  detectMediaType as detectMediaType4
13897
14157
  } from "@ai-sdk/provider-utils";
13898
14158
  var defaultDownload = createDownload();
@@ -13919,7 +14179,7 @@ async function experimental_generateVideo({
13919
14179
  }) {
13920
14180
  var _a23, _b;
13921
14181
  const model = resolveVideoModel(modelArg);
13922
- const headersWithUserAgent = withUserAgentSuffix9(
14182
+ const headersWithUserAgent = withUserAgentSuffix10(
13923
14183
  headers != null ? headers : {},
13924
14184
  `ai/${VERSION}`
13925
14185
  );
@@ -14093,7 +14353,7 @@ async function executeStartStatusFlow({
14093
14353
  webhook: webhookFactory,
14094
14354
  retry
14095
14355
  }) {
14096
- var _a23, _b, _c;
14356
+ var _a23, _b, _c, _d;
14097
14357
  const earlyWarnings = [];
14098
14358
  let webhookUrl;
14099
14359
  let webhookReceived;
@@ -14112,17 +14372,23 @@ async function executeStartStatusFlow({
14112
14372
  });
14113
14373
  }
14114
14374
  }
14115
- const startResult = await retry(
14116
- () => model.doStart({
14117
- ...callOptions,
14118
- webhookUrl
14119
- })
14375
+ const callerIdempotencyKey = Object.entries((_a23 = callOptions.headers) != null ? _a23 : {}).find(
14376
+ ([key, value]) => key.toLowerCase() === "idempotency-key" && value !== void 0
14120
14377
  );
14378
+ const startCallOptions = {
14379
+ ...callOptions,
14380
+ headers: {
14381
+ ...callOptions.headers,
14382
+ ...callerIdempotencyKey ? {} : { "idempotency-key": `aisdk_vid_${generateId()}` }
14383
+ },
14384
+ webhookUrl
14385
+ };
14386
+ const startResult = await retry(() => model.doStart(startCallOptions));
14121
14387
  const allWarnings = [...earlyWarnings, ...startResult.warnings];
14122
14388
  let operationProviderMetadata = startResult.providerMetadata == null ? void 0 : { ...startResult.providerMetadata };
14123
- const intervalMs = (_a23 = pollConfig == null ? void 0 : pollConfig.intervalMs) != null ? _a23 : 5e3;
14124
- const timeoutMs = (_b = pollConfig == null ? void 0 : pollConfig.timeoutMs) != null ? _b : 6e5;
14125
- const delay = (_c = pollConfig == null ? void 0 : pollConfig.delay) != null ? _c : defaultDelay;
14389
+ const intervalMs = (_b = pollConfig == null ? void 0 : pollConfig.intervalMs) != null ? _b : 5e3;
14390
+ const timeoutMs = (_c = pollConfig == null ? void 0 : pollConfig.timeoutMs) != null ? _c : 6e5;
14391
+ const delay = (_d = pollConfig == null ? void 0 : pollConfig.delay) != null ? _d : defaultDelay;
14126
14392
  const startTime = Date.now();
14127
14393
  if (webhookReceived != null) {
14128
14394
  await waitForWebhook({
@@ -16454,7 +16720,7 @@ var DefaultRerankResult = class {
16454
16720
  // src/transcribe/transcribe.ts
16455
16721
  import {
16456
16722
  detectMediaType as detectMediaType5,
16457
- withUserAgentSuffix as withUserAgentSuffix10
16723
+ withUserAgentSuffix as withUserAgentSuffix11
16458
16724
  } from "@ai-sdk/provider-utils";
16459
16725
  var defaultDownload2 = createDownload();
16460
16726
  async function transcribe({
@@ -16474,7 +16740,7 @@ async function transcribe({
16474
16740
  maxRetries: maxRetriesArg,
16475
16741
  abortSignal
16476
16742
  });
16477
- const headersWithUserAgent = withUserAgentSuffix10(
16743
+ const headersWithUserAgent = withUserAgentSuffix11(
16478
16744
  headers != null ? headers : {},
16479
16745
  `ai/${VERSION}`
16480
16746
  );
@@ -16527,11 +16793,11 @@ var DefaultTranscriptionResult = class {
16527
16793
 
16528
16794
  // src/transcribe/stream-transcribe.ts
16529
16795
  import {
16530
- UnsupportedFunctionalityError as UnsupportedFunctionalityError4
16796
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError5
16531
16797
  } from "@ai-sdk/provider";
16532
16798
  import {
16533
16799
  DelayedPromise as DelayedPromise3,
16534
- withUserAgentSuffix as withUserAgentSuffix11
16800
+ withUserAgentSuffix as withUserAgentSuffix12
16535
16801
  } from "@ai-sdk/provider-utils";
16536
16802
  function streamTranscribe({
16537
16803
  model,
@@ -16550,12 +16816,12 @@ function streamTranscribe({
16550
16816
  }
16551
16817
  const doStream = (_a23 = resolvedModel.doStream) == null ? void 0 : _a23.bind(resolvedModel);
16552
16818
  if (doStream == null) {
16553
- throw new UnsupportedFunctionalityError4({
16819
+ throw new UnsupportedFunctionalityError5({
16554
16820
  functionality: "streaming transcription",
16555
16821
  message: `The ${resolvedModel.provider} model "${resolvedModel.modelId}" does not support streaming transcription.` + (typeof model === "string" ? " String model IDs resolve through the global provider (AI Gateway by default). If that provider does not support streaming transcription, pass a provider model instance instead (e.g. openai.transcription('gpt-realtime-whisper')) or upgrade @ai-sdk/gateway to a version with streaming transcription support." : "")
16556
16822
  });
16557
16823
  }
16558
- const headersWithUserAgent = withUserAgentSuffix11(
16824
+ const headersWithUserAgent = withUserAgentSuffix12(
16559
16825
  headers != null ? headers : {},
16560
16826
  `ai/${VERSION}`
16561
16827
  );
@@ -16740,7 +17006,7 @@ var experimental_transcribe = transcribe;
16740
17006
  // src/translate/stream-translate.ts
16741
17007
  import {
16742
17008
  DelayedPromise as DelayedPromise4,
16743
- withUserAgentSuffix as withUserAgentSuffix12
17009
+ withUserAgentSuffix as withUserAgentSuffix13
16744
17010
  } from "@ai-sdk/provider-utils";
16745
17011
  function streamTranslate({
16746
17012
  model,
@@ -16757,7 +17023,7 @@ function streamTranslate({
16757
17023
  }) {
16758
17024
  const resolvedModel = resolveSpeechTranslationModel(model);
16759
17025
  const doStream = resolvedModel.doStream.bind(resolvedModel);
16760
- const headersWithUserAgent = withUserAgentSuffix12(
17026
+ const headersWithUserAgent = withUserAgentSuffix13(
16761
17027
  headers != null ? headers : {},
16762
17028
  `ai/${VERSION}`
16763
17029
  );
@@ -16954,7 +17220,7 @@ function streamTranslate({
16954
17220
  // src/ui/call-completion-api.ts
16955
17221
  import {
16956
17222
  parseJsonEventStream,
16957
- withUserAgentSuffix as withUserAgentSuffix13,
17223
+ withUserAgentSuffix as withUserAgentSuffix14,
16958
17224
  getRuntimeEnvironmentUserAgent as getRuntimeEnvironmentUserAgent2
16959
17225
  } from "@ai-sdk/provider-utils";
16960
17226
 
@@ -17004,7 +17270,7 @@ async function callCompletionApi({
17004
17270
  ...body
17005
17271
  }),
17006
17272
  credentials,
17007
- headers: withUserAgentSuffix13(
17273
+ headers: withUserAgentSuffix14(
17008
17274
  {
17009
17275
  "Content-Type": "application/json",
17010
17276
  ...headers
@@ -17229,7 +17495,8 @@ var HttpChatTransport = class {
17229
17495
  const response = await fetch2(api, {
17230
17496
  method: "GET",
17231
17497
  headers,
17232
- credentials
17498
+ credentials,
17499
+ signal: options.abortSignal
17233
17500
  });
17234
17501
  if (response.status === 204) {
17235
17502
  return null;
@@ -17271,8 +17538,8 @@ var DefaultChatTransport = class extends HttpChatTransport {
17271
17538
  // src/ui/chat.ts
17272
17539
  var AbstractChat = class {
17273
17540
  constructor({
17274
- generateId: generateId2 = generateIdFunc2,
17275
- id = generateId2(),
17541
+ generateId: generateId3 = generateIdFunc2,
17542
+ id = generateId3(),
17276
17543
  transport = new DefaultChatTransport(),
17277
17544
  messageMetadataSchema,
17278
17545
  dataPartSchemas,
@@ -17284,6 +17551,7 @@ var AbstractChat = class {
17284
17551
  sendAutomaticallyWhen
17285
17552
  }) {
17286
17553
  this.activeResponse = void 0;
17554
+ this.activeResumeRequest = void 0;
17287
17555
  this.jobExecutor = new SerialJobExecutor();
17288
17556
  /**
17289
17557
  * Appends or replaces a user message to the chat list. This triggers the API call to fetch
@@ -17453,16 +17721,13 @@ var AbstractChat = class {
17453
17721
  * Abort the current request immediately, keep the generated tokens if any.
17454
17722
  */
17455
17723
  this.stop = async () => {
17456
- var _a23;
17457
- if (this.status !== "streaming" && this.status !== "submitted")
17458
- return;
17459
- if ((_a23 = this.activeResponse) == null ? void 0 : _a23.abortController) {
17460
- this.activeResponse.abortController.abort();
17461
- }
17724
+ var _a23, _b;
17725
+ (_a23 = this.activeResumeRequest) == null ? void 0 : _a23.abortController.abort();
17726
+ (_b = this.activeResponse) == null ? void 0 : _b.abortController.abort();
17462
17727
  };
17463
17728
  this.id = id;
17464
17729
  this.transport = transport;
17465
- this.generateId = generateId2;
17730
+ this.generateId = generateId3;
17466
17731
  this.messageMetadataSchema = messageMetadataSchema;
17467
17732
  this.dataPartSchemas = dataPartSchemas;
17468
17733
  this.state = state;
@@ -17522,25 +17787,60 @@ var AbstractChat = class {
17522
17787
  body,
17523
17788
  messageId
17524
17789
  }) {
17525
- var _a23, _b;
17790
+ var _a23, _b, _c;
17791
+ const abortController = new AbortController();
17792
+ const activeResumeRequest = trigger === "resume-stream" ? { abortController } : void 0;
17793
+ if (activeResumeRequest) {
17794
+ (_a23 = this.activeResumeRequest) == null ? void 0 : _a23.abortController.abort();
17795
+ this.activeResumeRequest = activeResumeRequest;
17796
+ }
17797
+ const isCurrentRequest = () => activeResumeRequest == null || this.activeResumeRequest === activeResumeRequest;
17798
+ const clearActiveResumeRequest = () => {
17799
+ if (this.activeResumeRequest === activeResumeRequest) {
17800
+ this.activeResumeRequest = void 0;
17801
+ }
17802
+ };
17526
17803
  let resumeStream;
17527
17804
  if (trigger === "resume-stream") {
17528
17805
  try {
17529
17806
  const reconnect = await this.transport.reconnectToStream({
17530
17807
  chatId: this.id,
17808
+ abortSignal: abortController.signal,
17531
17809
  metadata,
17532
17810
  headers,
17533
17811
  body
17534
17812
  });
17813
+ if (abortController.signal.aborted || !isCurrentRequest()) {
17814
+ await (reconnect == null ? void 0 : reconnect.cancel().catch(() => {
17815
+ }));
17816
+ if (isCurrentRequest()) {
17817
+ this.setStatus({ status: "ready" });
17818
+ }
17819
+ clearActiveResumeRequest();
17820
+ return;
17821
+ }
17535
17822
  if (reconnect == null) {
17823
+ this.setStatus({ status: "ready" });
17824
+ clearActiveResumeRequest();
17536
17825
  return;
17537
17826
  }
17538
17827
  resumeStream = reconnect;
17539
17828
  } catch (err) {
17829
+ if (abortController.signal.aborted || err.name === "AbortError") {
17830
+ if (isCurrentRequest()) {
17831
+ this.setStatus({ status: "ready" });
17832
+ }
17833
+ clearActiveResumeRequest();
17834
+ return;
17835
+ }
17836
+ if (!isCurrentRequest()) {
17837
+ return;
17838
+ }
17540
17839
  if (this.onError && err instanceof Error) {
17541
17840
  this.onError(err);
17542
17841
  }
17543
17842
  this.setStatus({ status: "error", error: err });
17843
+ clearActiveResumeRequest();
17544
17844
  return;
17545
17845
  }
17546
17846
  }
@@ -17556,7 +17856,7 @@ var AbstractChat = class {
17556
17856
  lastMessage: trigger === "regenerate-message" ? void 0 : this.state.snapshot(lastMessage),
17557
17857
  messageId: this.generateId()
17558
17858
  }),
17559
- abortController: new AbortController()
17859
+ abortController
17560
17860
  };
17561
17861
  activeResponse = response;
17562
17862
  response.abortController.signal.addEventListener("abort", () => {
@@ -17580,11 +17880,17 @@ var AbstractChat = class {
17580
17880
  }
17581
17881
  const runUpdateMessageJob = (job) => (
17582
17882
  // serialize the job execution to avoid race conditions:
17583
- this.jobExecutor.run(
17584
- () => job({
17883
+ this.jobExecutor.run(() => {
17884
+ if (response.abortController.signal.aborted) {
17885
+ return Promise.resolve();
17886
+ }
17887
+ return job({
17585
17888
  state: response.state,
17586
17889
  write: () => {
17587
17890
  var _a24;
17891
+ if (response.abortController.signal.aborted) {
17892
+ return;
17893
+ }
17588
17894
  this.setStatus({ status: "streaming" });
17589
17895
  const replaceLastMessage = response.state.message.id === ((_a24 = this.lastMessage) == null ? void 0 : _a24.id);
17590
17896
  if (replaceLastMessage) {
@@ -17596,8 +17902,8 @@ var AbstractChat = class {
17596
17902
  this.state.pushMessage(response.state.message);
17597
17903
  }
17598
17904
  }
17599
- })
17600
- )
17905
+ });
17906
+ })
17601
17907
  );
17602
17908
  await consumeStream({
17603
17909
  stream: processUIMessageStream({
@@ -17611,15 +17917,29 @@ var AbstractChat = class {
17611
17917
  throw error;
17612
17918
  }
17613
17919
  }),
17920
+ abortSignal: response.abortController.signal,
17614
17921
  onError: (error) => {
17615
17922
  throw error;
17616
17923
  }
17617
17924
  });
17618
- this.setStatus({ status: "ready" });
17925
+ if (isAbort) {
17926
+ if (isCurrentRequest()) {
17927
+ this.setStatus({ status: "ready" });
17928
+ }
17929
+ return null;
17930
+ }
17931
+ if (isCurrentRequest()) {
17932
+ this.setStatus({ status: "ready" });
17933
+ }
17619
17934
  } catch (err) {
17620
17935
  if (isAbort || err.name === "AbortError") {
17621
17936
  isAbort = true;
17622
- this.setStatus({ status: "ready" });
17937
+ if (isCurrentRequest()) {
17938
+ this.setStatus({ status: "ready" });
17939
+ }
17940
+ return null;
17941
+ }
17942
+ if (!isCurrentRequest()) {
17623
17943
  return null;
17624
17944
  }
17625
17945
  isError = true;
@@ -17633,7 +17953,7 @@ var AbstractChat = class {
17633
17953
  } finally {
17634
17954
  try {
17635
17955
  if (activeResponse) {
17636
- (_a23 = this.onFinish) == null ? void 0 : _a23.call(this, {
17956
+ (_b = this.onFinish) == null ? void 0 : _b.call(this, {
17637
17957
  message: activeResponse.state.message,
17638
17958
  messages: this.state.messages,
17639
17959
  isAbort,
@@ -17648,11 +17968,12 @@ var AbstractChat = class {
17648
17968
  if (this.activeResponse === activeResponse) {
17649
17969
  this.activeResponse = void 0;
17650
17970
  }
17971
+ clearActiveResumeRequest();
17651
17972
  }
17652
17973
  if (!isError && await this.shouldSendAutomatically()) {
17653
17974
  await this.makeRequest({
17654
17975
  trigger: "submit-message",
17655
- messageId: (_b = this.lastMessage) == null ? void 0 : _b.id,
17976
+ messageId: (_c = this.lastMessage) == null ? void 0 : _c.id,
17656
17977
  metadata,
17657
17978
  headers,
17658
17979
  body
@@ -17955,8 +18276,11 @@ export {
17955
18276
  filterActiveTools as experimental_filterActiveTools,
17956
18277
  experimental_generateSpeech,
17957
18278
  experimental_generateVideo,
18279
+ getBatchResults as experimental_getBatchResults,
18280
+ getBatchStatus as experimental_getBatchStatus,
17958
18281
  getRealtimeToolDefinitions as experimental_getRealtimeToolDefinitions,
17959
18282
  resampleAudio as experimental_resampleAudio,
18283
+ startTextBatch as experimental_startTextBatch,
17960
18284
  streamLanguageModelCall as experimental_streamLanguageModelCall,
17961
18285
  streamTranscribe as experimental_streamTranscribe,
17962
18286
  streamTranslate as experimental_streamTranslate,
@@ -17966,7 +18290,7 @@ export {
17966
18290
  extractReasoningMiddleware,
17967
18291
  fingerprintTools,
17968
18292
  gateway2 as gateway,
17969
- generateId,
18293
+ generateId2 as generateId,
17970
18294
  generateImage,
17971
18295
  generateObject,
17972
18296
  generateSpeech,