ai 7.0.92 → 7.0.94

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.
Files changed (44) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/dist/index.d.ts +239 -214
  3. package/dist/index.js +391 -138
  4. package/dist/index.js.map +1 -1
  5. package/dist/internal/index.d.ts +3 -2
  6. package/dist/internal/index.js +8 -5
  7. package/dist/internal/index.js.map +1 -1
  8. package/docs/02-foundations/02-providers-and-models.mdx +1 -1
  9. package/docs/03-agents/04-loop-control.mdx +5 -3
  10. package/docs/03-agents/07-workflow-agent.mdx +27 -6
  11. package/docs/03-ai-sdk-core/10-generating-structured-data.mdx +15 -3
  12. package/docs/03-ai-sdk-core/16-mcp-tools.mdx +64 -1
  13. package/docs/03-ai-sdk-core/35-image-generation.mdx +9 -0
  14. package/docs/03-ai-sdk-core/36-transcription.mdx +36 -35
  15. package/docs/03-ai-sdk-core/37-speech.mdx +0 -17
  16. package/docs/03-ai-sdk-harnesses/02-harness-agent.mdx +36 -0
  17. package/docs/04-ai-sdk-ui/20-streaming-data.mdx +11 -6
  18. package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +14 -0
  19. package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +15 -1
  20. package/docs/07-reference/01-ai-sdk-core/10-generate-image.mdx +2 -1
  21. package/docs/07-reference/01-ai-sdk-core/28-output.mdx +27 -1
  22. package/docs/07-reference/02-ai-sdk-ui/01-use-chat.mdx +1 -1
  23. package/docs/07-reference/02-ai-sdk-ui/40-create-ui-message-stream.mdx +4 -0
  24. package/docs/07-reference/02-ai-sdk-ui/41-create-ui-message-stream-response.mdx +6 -1
  25. package/docs/07-reference/04-ai-sdk-workflow/01-workflow-agent.mdx +42 -28
  26. package/docs/07-reference/05-ai-sdk-errors/ai-no-image-generated-error.mdx +7 -0
  27. package/package.json +12 -12
  28. package/src/agent/tool-loop-agent-settings.ts +15 -0
  29. package/src/batch/batch-types.ts +76 -68
  30. package/src/batch/batch.ts +169 -101
  31. package/src/batch/index.ts +9 -7
  32. package/src/embed/embed-many.ts +27 -2
  33. package/src/error/no-image-generated-error.ts +9 -0
  34. package/src/generate-image/generate-image.ts +51 -10
  35. package/src/generate-text/output.ts +111 -1
  36. package/src/generate-text/stream-language-model-call.ts +82 -0
  37. package/src/generate-text/stream-text.ts +5 -1
  38. package/src/ui/chat.ts +1 -1
  39. package/src/ui/convert-to-model-messages.ts +8 -2
  40. package/src/ui/validate-ui-messages.ts +14 -0
  41. package/src/util/data-url.ts +1 -1
  42. package/src/util/merge-abort-signals.ts +1 -1
  43. package/src/util/prepare-retries.ts +7 -1
  44. package/src/util/retry-with-exponential-backoff.ts +9 -4
package/dist/index.js CHANGED
@@ -7,7 +7,7 @@ var __export = (target, all) => {
7
7
  // src/index.ts
8
8
  import {
9
9
  createGateway,
10
- gateway as gateway2
10
+ gateway as gateway3
11
11
  } from "@ai-sdk/gateway";
12
12
  import {
13
13
  asSchema as asSchema8,
@@ -222,10 +222,12 @@ var NoImageGeneratedError = class extends (_b8 = AISDKError8, _a8 = symbol8, _b8
222
222
  constructor({
223
223
  message = "No image generated.",
224
224
  cause,
225
+ calls,
225
226
  responses
226
227
  }) {
227
228
  super({ name: name8, message, cause });
228
229
  this[_a8] = true;
230
+ this.calls = calls;
229
231
  this.responses = responses;
230
232
  }
231
233
  static isInstance(error) {
@@ -1176,7 +1178,7 @@ import {
1176
1178
  } from "@ai-sdk/provider-utils";
1177
1179
 
1178
1180
  // src/version.ts
1179
- var VERSION = true ? "7.0.92" : "0.0.0-test";
1181
+ var VERSION = true ? "7.0.94" : "0.0.0-test";
1180
1182
 
1181
1183
  // src/util/download/download.ts
1182
1184
  var download = async ({
@@ -2749,7 +2751,7 @@ function getOwn(obj, key) {
2749
2751
  import { filterNullable } from "@ai-sdk/provider-utils";
2750
2752
  function mergeAbortSignals(...signals) {
2751
2753
  const validSignals = filterNullable(...signals).map(
2752
- (signal) => signal instanceof AbortSignal ? signal : AbortSignal.timeout(signal)
2754
+ (signal) => typeof signal === "number" ? AbortSignal.timeout(signal) : signal
2753
2755
  );
2754
2756
  return validSignals.length === 0 ? void 0 : validSignals.length === 1 ? validSignals[0] : AbortSignal.any(validSignals);
2755
2757
  }
@@ -2811,13 +2813,14 @@ var retryWithExponentialBackoffRespectingRetryHeaders = ({
2811
2813
  maxRetries = 2,
2812
2814
  initialDelayInMs = 2e3,
2813
2815
  backoffFactor = 2,
2814
- abortSignal
2816
+ abortSignal,
2817
+ additionalRetryableError
2815
2818
  } = {}) => retryWithExponentialBackoff({
2816
2819
  maxRetries,
2817
2820
  initialDelayInMs,
2818
2821
  backoffFactor,
2819
2822
  abortSignal,
2820
- shouldRetry: (error) => error instanceof Error && (APICallError2.isInstance(error) && error.isRetryable === true || GatewayError.isInstance(error) && error.isRetryable === true),
2823
+ shouldRetry: async (error) => error instanceof Error && (APICallError2.isInstance(error) && error.isRetryable === true || GatewayError.isInstance(error) && error.isRetryable === true) || additionalRetryableError != null && await additionalRetryableError(error),
2821
2824
  getDelayInMs: ({ error, exponentialBackoffDelay }) => getRetryDelayInMs({
2822
2825
  error,
2823
2826
  exponentialBackoffDelay
@@ -2829,6 +2832,7 @@ var retryWithExponentialBackoffRespectingRetryHeaders = ({
2829
2832
  function prepareRetries({
2830
2833
  maxRetries,
2831
2834
  abortSignal,
2835
+ additionalRetryableError,
2832
2836
  parameter = "maxRetries",
2833
2837
  defaultMaxRetries = 2
2834
2838
  }) {
@@ -2853,7 +2857,8 @@ function prepareRetries({
2853
2857
  maxRetries: maxRetriesResult,
2854
2858
  retry: retryWithExponentialBackoffRespectingRetryHeaders({
2855
2859
  maxRetries: maxRetriesResult,
2856
- abortSignal
2860
+ abortSignal,
2861
+ additionalRetryableError
2857
2862
  })
2858
2863
  };
2859
2864
  }
@@ -3780,9 +3785,20 @@ var object2 = ({
3780
3785
  };
3781
3786
  var array2 = ({
3782
3787
  element: inputElementSchema,
3788
+ minItems,
3789
+ maxItems,
3783
3790
  name: name25,
3784
3791
  description
3785
3792
  }) => {
3793
+ validateArrayBound({ name: "minItems", value: minItems });
3794
+ validateArrayBound({ name: "maxItems", value: maxItems });
3795
+ if (minItems != null && maxItems != null && minItems > maxItems) {
3796
+ throw new InvalidArgumentError({
3797
+ parameter: "minItems",
3798
+ value: minItems,
3799
+ message: "minItems must be less than or equal to maxItems"
3800
+ });
3801
+ }
3786
3802
  const elementSchema = asSchema2(inputElementSchema);
3787
3803
  return {
3788
3804
  name: "array",
@@ -3802,7 +3818,12 @@ var array2 = ({
3802
3818
  ...$defs != null && { $defs },
3803
3819
  type: "object",
3804
3820
  properties: {
3805
- elements: { type: "array", items: itemSchema }
3821
+ elements: {
3822
+ type: "array",
3823
+ items: itemSchema,
3824
+ ...minItems != null && { minItems },
3825
+ ...maxItems != null && { maxItems }
3826
+ }
3806
3827
  },
3807
3828
  required: ["elements"],
3808
3829
  additionalProperties: false
@@ -3837,6 +3858,21 @@ var array2 = ({
3837
3858
  finishReason: context.finishReason
3838
3859
  });
3839
3860
  }
3861
+ const lengthValidationError = getArrayLengthValidationError({
3862
+ value: outerValue.elements,
3863
+ minItems,
3864
+ maxItems
3865
+ });
3866
+ if (lengthValidationError != null) {
3867
+ throw new NoObjectGeneratedError({
3868
+ message: "No object generated: response did not match schema.",
3869
+ cause: lengthValidationError,
3870
+ text: text2,
3871
+ response: context.response,
3872
+ usage: context.usage,
3873
+ finishReason: context.finishReason
3874
+ });
3875
+ }
3840
3876
  const validatedElements = [];
3841
3877
  for (const element of outerValue.elements) {
3842
3878
  const validationResult = await safeValidateTypes2({
@@ -3891,6 +3927,15 @@ var array2 = ({
3891
3927
  transform({ partialOutput }, controller) {
3892
3928
  if (partialOutput != null) {
3893
3929
  for (; publishedElements < partialOutput.length; publishedElements++) {
3930
+ if (maxItems != null && publishedElements >= maxItems) {
3931
+ controller.error(
3932
+ getArrayLengthValidationError({
3933
+ value: partialOutput,
3934
+ maxItems
3935
+ })
3936
+ );
3937
+ return;
3938
+ }
3894
3939
  controller.enqueue(partialOutput[publishedElements]);
3895
3940
  }
3896
3941
  }
@@ -3899,6 +3944,47 @@ var array2 = ({
3899
3944
  }
3900
3945
  };
3901
3946
  };
3947
+ function validateArrayBound({
3948
+ name: name25,
3949
+ value
3950
+ }) {
3951
+ if (value == null) {
3952
+ return;
3953
+ }
3954
+ if (!Number.isInteger(value)) {
3955
+ throw new InvalidArgumentError({
3956
+ parameter: name25,
3957
+ value,
3958
+ message: `${name25} must be an integer`
3959
+ });
3960
+ }
3961
+ if (value < 0) {
3962
+ throw new InvalidArgumentError({
3963
+ parameter: name25,
3964
+ value,
3965
+ message: `${name25} must be greater than or equal to 0`
3966
+ });
3967
+ }
3968
+ }
3969
+ function getArrayLengthValidationError({
3970
+ value,
3971
+ minItems,
3972
+ maxItems
3973
+ }) {
3974
+ if (minItems != null && value.length < minItems) {
3975
+ return new TypeValidationError2({
3976
+ value,
3977
+ cause: `elements array must contain at least ${minItems} items`
3978
+ });
3979
+ }
3980
+ if (maxItems != null && value.length > maxItems) {
3981
+ return new TypeValidationError2({
3982
+ value,
3983
+ cause: `elements array must contain at most ${maxItems} items`
3984
+ });
3985
+ }
3986
+ return void 0;
3987
+ }
3902
3988
  var choice = ({
3903
3989
  options: choiceOptions,
3904
3990
  name: name25,
@@ -8709,6 +8795,7 @@ async function streamLanguageModelCall({
8709
8795
  callId: effectiveCallId,
8710
8796
  provider: resolvedModel.provider,
8711
8797
  modelId: resolvedModel.modelId,
8798
+ toolChoice: stepToolChoice,
8712
8799
  generateId: generateId4,
8713
8800
  now: now2,
8714
8801
  callStartTimestampMs,
@@ -8730,6 +8817,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8730
8817
  callId,
8731
8818
  provider,
8732
8819
  modelId,
8820
+ toolChoice,
8733
8821
  generateId: generateId4,
8734
8822
  now: now2,
8735
8823
  callStartTimestampMs,
@@ -8737,8 +8825,11 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8737
8825
  }) {
8738
8826
  const toolCallsByToolCallId = /* @__PURE__ */ new Map();
8739
8827
  const modelCallContent = [];
8828
+ const rawModelCallContent = [];
8740
8829
  const textPartIndexes = /* @__PURE__ */ new Map();
8741
8830
  const reasoningPartIndexes = /* @__PURE__ */ new Map();
8831
+ const rawTextPartIndexes = /* @__PURE__ */ new Map();
8832
+ const rawReasoningPartIndexes = /* @__PURE__ */ new Map();
8742
8833
  let responseId = generateId4();
8743
8834
  let responseModelId = modelId;
8744
8835
  let timeToFirstOutputMs;
@@ -8768,7 +8859,9 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8768
8859
  case "text-start":
8769
8860
  upsertTextContentPart({
8770
8861
  content: modelCallContent,
8862
+ rawContent: rawModelCallContent,
8771
8863
  partIndexes: textPartIndexes,
8864
+ rawPartIndexes: rawTextPartIndexes,
8772
8865
  id: chunk.id,
8773
8866
  type: "text",
8774
8867
  providerMetadata: chunk.providerMetadata
@@ -8778,7 +8871,9 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8778
8871
  case "text-delta":
8779
8872
  upsertTextContentPart({
8780
8873
  content: modelCallContent,
8874
+ rawContent: rawModelCallContent,
8781
8875
  partIndexes: textPartIndexes,
8876
+ rawPartIndexes: rawTextPartIndexes,
8782
8877
  id: chunk.id,
8783
8878
  type: "text",
8784
8879
  textDelta: chunk.delta,
@@ -8794,18 +8889,23 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8794
8889
  case "text-end":
8795
8890
  upsertTextContentPart({
8796
8891
  content: modelCallContent,
8892
+ rawContent: rawModelCallContent,
8797
8893
  partIndexes: textPartIndexes,
8894
+ rawPartIndexes: rawTextPartIndexes,
8798
8895
  id: chunk.id,
8799
8896
  type: "text",
8800
8897
  providerMetadata: chunk.providerMetadata
8801
8898
  });
8802
8899
  textPartIndexes.delete(chunk.id);
8900
+ rawTextPartIndexes.delete(chunk.id);
8803
8901
  controller.enqueue(chunk);
8804
8902
  break;
8805
8903
  case "reasoning-start":
8806
8904
  upsertTextContentPart({
8807
8905
  content: modelCallContent,
8906
+ rawContent: rawModelCallContent,
8808
8907
  partIndexes: reasoningPartIndexes,
8908
+ rawPartIndexes: rawReasoningPartIndexes,
8809
8909
  id: chunk.id,
8810
8910
  type: "reasoning",
8811
8911
  providerMetadata: chunk.providerMetadata
@@ -8815,7 +8915,9 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8815
8915
  case "reasoning-delta":
8816
8916
  upsertTextContentPart({
8817
8917
  content: modelCallContent,
8918
+ rawContent: rawModelCallContent,
8818
8919
  partIndexes: reasoningPartIndexes,
8920
+ rawPartIndexes: rawReasoningPartIndexes,
8819
8921
  id: chunk.id,
8820
8922
  type: "reasoning",
8821
8923
  textDelta: chunk.delta,
@@ -8831,12 +8933,15 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8831
8933
  case "reasoning-end":
8832
8934
  upsertTextContentPart({
8833
8935
  content: modelCallContent,
8936
+ rawContent: rawModelCallContent,
8834
8937
  partIndexes: reasoningPartIndexes,
8938
+ rawPartIndexes: rawReasoningPartIndexes,
8835
8939
  id: chunk.id,
8836
8940
  type: "reasoning",
8837
8941
  providerMetadata: chunk.providerMetadata
8838
8942
  });
8839
8943
  reasoningPartIndexes.delete(chunk.id);
8944
+ rawReasoningPartIndexes.delete(chunk.id);
8840
8945
  controller.enqueue(chunk);
8841
8946
  break;
8842
8947
  case "file":
@@ -8850,6 +8955,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8850
8955
  file,
8851
8956
  ...chunk.providerMetadata != null ? { providerMetadata: chunk.providerMetadata } : {}
8852
8957
  });
8958
+ rawModelCallContent.push(chunk);
8853
8959
  controller.enqueue({
8854
8960
  type: chunk.type,
8855
8961
  file,
@@ -8903,9 +9009,26 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8903
9009
  providerMetadata: chunk.providerMetadata,
8904
9010
  performance
8905
9011
  });
9012
+ const enforcedToolChoice = toolChoice.type === "required" || toolChoice.type === "tool" ? toolChoice : void 0;
9013
+ if (enforcedToolChoice != null && ![...toolCallsByToolCallId.values()].some(
9014
+ (toolCall) => enforcedToolChoice.type === "required" || toolCall.toolName === enforcedToolChoice.toolName
9015
+ )) {
9016
+ controller.enqueue({
9017
+ type: "error",
9018
+ error: new ToolChoiceViolationError({
9019
+ toolChoice: enforcedToolChoice,
9020
+ finishReason: chunk.finishReason.unified,
9021
+ provider,
9022
+ modelId,
9023
+ content: rawModelCallContent
9024
+ })
9025
+ });
9026
+ break;
9027
+ }
8906
9028
  break;
8907
9029
  }
8908
9030
  case "tool-call": {
9031
+ rawModelCallContent.push(chunk);
8909
9032
  try {
8910
9033
  const toolCall = await parseToolCall({
8911
9034
  toolCall: chunk,
@@ -8939,6 +9062,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8939
9062
  break;
8940
9063
  }
8941
9064
  case "tool-approval-request": {
9065
+ rawModelCallContent.push(chunk);
8942
9066
  const toolCall = toolCallsByToolCallId.get(chunk.toolCallId);
8943
9067
  if (toolCall == null) {
8944
9068
  controller.enqueue({
@@ -8960,6 +9084,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
8960
9084
  break;
8961
9085
  }
8962
9086
  case "tool-result": {
9087
+ rawModelCallContent.push(chunk);
8963
9088
  const toolName = chunk.toolName;
8964
9089
  const toolCall = toolCallsByToolCallId.get(chunk.toolCallId);
8965
9090
  const toolResultPart = chunk.isError ? {
@@ -9018,6 +9143,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform({
9018
9143
  default:
9019
9144
  if (chunk.type === "custom" || chunk.type === "source") {
9020
9145
  modelCallContent.push(chunk);
9146
+ rawModelCallContent.push(chunk);
9021
9147
  }
9022
9148
  controller.enqueue(chunk);
9023
9149
  break;
@@ -9045,7 +9171,9 @@ function calculateNearestRankPercentile(sortedValues, percentile) {
9045
9171
  }
9046
9172
  function upsertTextContentPart({
9047
9173
  content,
9174
+ rawContent,
9048
9175
  partIndexes,
9176
+ rawPartIndexes,
9049
9177
  id,
9050
9178
  type,
9051
9179
  textDelta,
@@ -9060,12 +9188,24 @@ function upsertTextContentPart({
9060
9188
  }) - 1;
9061
9189
  partIndexes.set(id, partIndex);
9062
9190
  }
9191
+ let rawPartIndex = rawPartIndexes.get(id);
9192
+ if (rawPartIndex == null) {
9193
+ rawPartIndex = rawContent.push({
9194
+ type,
9195
+ text: "",
9196
+ ...providerMetadata != null ? { providerMetadata } : {}
9197
+ }) - 1;
9198
+ rawPartIndexes.set(id, rawPartIndex);
9199
+ }
9063
9200
  const part = content[partIndex];
9201
+ const rawPart = rawContent[rawPartIndex];
9064
9202
  if (textDelta != null) {
9065
9203
  part.text += textDelta;
9204
+ rawPart.text += textDelta;
9066
9205
  }
9067
9206
  if (providerMetadata != null) {
9068
9207
  part.providerMetadata = providerMetadata;
9208
+ rawPart.providerMetadata = providerMetadata;
9069
9209
  }
9070
9210
  }
9071
9211
 
@@ -10409,14 +10549,15 @@ var DefaultStreamTextResult = class {
10409
10549
  callbacks: onChunk
10410
10550
  });
10411
10551
  const error = wrapGatewayError(value.error);
10552
+ const isToolChoiceViolation = ToolChoiceViolationError.isInstance(error);
10412
10553
  let onErrorResult;
10413
10554
  try {
10414
10555
  onErrorResult = await onError({ error });
10415
10556
  } catch (e) {
10416
10557
  }
10417
10558
  const callbackRequestedRetry = canRetryStreamViaOnError && typeof onErrorResult === "object" && onErrorResult != null && "retry" in onErrorResult && onErrorResult.retry === true;
10418
- const automaticRetry = automaticStreamRetryCount < streamRetries;
10419
- const callbackRetry = !automaticRetry && callbackRequestedRetry && callbackStreamRetryCount < 1;
10559
+ const automaticRetry = !isToolChoiceViolation && automaticStreamRetryCount < streamRetries;
10560
+ const callbackRetry = !isToolChoiceViolation && !automaticRetry && callbackRequestedRetry && callbackStreamRetryCount < 1;
10420
10561
  if (!automaticRetry && !callbackRetry) {
10421
10562
  flushBufferedAttemptParts();
10422
10563
  errorsHandledForStreamRetry.add(value.error);
@@ -11589,7 +11730,7 @@ async function convertToModelMessages(messages, options) {
11589
11730
  if (message.parts != null) {
11590
11731
  let block = [];
11591
11732
  async function processBlock() {
11592
- var _a25, _b25, _c, _d, _e, _f, _g;
11733
+ var _a25, _b25, _c, _d, _e, _f, _g, _h;
11593
11734
  if (block.length === 0) {
11594
11735
  return;
11595
11736
  }
@@ -11634,13 +11775,14 @@ async function convertToModelMessages(messages, options) {
11634
11775
  } else if (isToolUIPart(part)) {
11635
11776
  const toolName = getToolName(part);
11636
11777
  if (part.state !== "input-streaming") {
11778
+ const callProviderMetadata = (_a25 = part.callProviderMetadata) != null ? _a25 : part.state === "output-error" ? part.resultProviderMetadata : void 0;
11637
11779
  content.push({
11638
11780
  type: "tool-call",
11639
11781
  toolCallId: part.toolCallId,
11640
11782
  toolName,
11641
- input: part.state === "output-error" ? (_a25 = part.input) != null ? _a25 : "rawInput" in part ? part.rawInput : void 0 : part.input,
11783
+ input: part.state === "output-error" ? (_b25 = part.input) != null ? _b25 : "rawInput" in part ? part.rawInput : void 0 : part.input,
11642
11784
  providerExecuted: part.providerExecuted,
11643
- ...part.callProviderMetadata != null ? { providerOptions: part.callProviderMetadata } : {}
11785
+ ...callProviderMetadata != null ? { providerOptions: callProviderMetadata } : {}
11644
11786
  });
11645
11787
  if (part.approval != null) {
11646
11788
  content.push({
@@ -11653,7 +11795,7 @@ async function convertToModelMessages(messages, options) {
11653
11795
  });
11654
11796
  }
11655
11797
  if (part.providerExecuted === true && part.state !== "approval-responded" && (part.state === "output-available" || part.state === "output-error")) {
11656
- const resultProviderMetadata = (_b25 = part.resultProviderMetadata) != null ? _b25 : part.callProviderMetadata;
11798
+ const resultProviderMetadata = (_c = part.resultProviderMetadata) != null ? _c : part.callProviderMetadata;
11657
11799
  content.push({
11658
11800
  type: "tool-result",
11659
11801
  toolCallId: part.toolCallId,
@@ -11670,7 +11812,7 @@ async function convertToModelMessages(messages, options) {
11670
11812
  }
11671
11813
  }
11672
11814
  } else if (isDataUIPart(part)) {
11673
- const dataPart = (_c = options == null ? void 0 : options.convertDataPart) == null ? void 0 : _c.call(
11815
+ const dataPart = (_d = options == null ? void 0 : options.convertDataPart) == null ? void 0 : _d.call(
11674
11816
  options,
11675
11817
  part
11676
11818
  );
@@ -11698,7 +11840,7 @@ async function convertToModelMessages(messages, options) {
11698
11840
  {
11699
11841
  const content2 = [];
11700
11842
  for (const toolPart of toolParts) {
11701
- if (((_d = toolPart.approval) == null ? void 0 : _d.approved) != null) {
11843
+ if (((_e = toolPart.approval) == null ? void 0 : _e.approved) != null) {
11702
11844
  content2.push({
11703
11845
  type: "tool-approval-response",
11704
11846
  approvalId: toolPart.approval.id,
@@ -11707,7 +11849,7 @@ async function convertToModelMessages(messages, options) {
11707
11849
  providerExecuted: toolPart.providerExecuted
11708
11850
  });
11709
11851
  }
11710
- if (toolPart.state === "approval-responded" && ((_e = toolPart.approval) == null ? void 0 : _e.approved) === false) {
11852
+ if (toolPart.state === "approval-responded" && ((_f = toolPart.approval) == null ? void 0 : _f.approved) === false) {
11711
11853
  content2.push({
11712
11854
  type: "tool-result",
11713
11855
  toolCallId: toolPart.toolCallId,
@@ -11730,7 +11872,7 @@ async function convertToModelMessages(messages, options) {
11730
11872
  toolName: getToolName(toolPart),
11731
11873
  output: {
11732
11874
  type: "error-text",
11733
- value: (_g = (_f = toolPart.approval) == null ? void 0 : _f.reason) != null ? _g : "Tool call execution denied."
11875
+ value: (_h = (_g = toolPart.approval) == null ? void 0 : _g.reason) != null ? _h : "Tool call execution denied."
11734
11876
  },
11735
11877
  ...toolPart.callProviderMetadata != null ? { providerOptions: toolPart.callProviderMetadata } : {}
11736
11878
  });
@@ -11882,6 +12024,7 @@ var uiMessagesSchema = lazySchema2(
11882
12024
  type: z.literal("dynamic-tool"),
11883
12025
  toolName: z.string(),
11884
12026
  toolCallId: z.string(),
12027
+ title: z.string().optional(),
11885
12028
  toolMetadata: toolMetadataSchema2.optional(),
11886
12029
  state: z.literal("input-streaming"),
11887
12030
  input: z.unknown().optional(),
@@ -11895,6 +12038,7 @@ var uiMessagesSchema = lazySchema2(
11895
12038
  type: z.literal("dynamic-tool"),
11896
12039
  toolName: z.string(),
11897
12040
  toolCallId: z.string(),
12041
+ title: z.string().optional(),
11898
12042
  toolMetadata: toolMetadataSchema2.optional(),
11899
12043
  state: z.literal("input-available"),
11900
12044
  input: z.unknown(),
@@ -11908,6 +12052,7 @@ var uiMessagesSchema = lazySchema2(
11908
12052
  type: z.literal("dynamic-tool"),
11909
12053
  toolName: z.string(),
11910
12054
  toolCallId: z.string(),
12055
+ title: z.string().optional(),
11911
12056
  toolMetadata: toolMetadataSchema2.optional(),
11912
12057
  state: z.literal("approval-requested"),
11913
12058
  input: z.unknown(),
@@ -11929,6 +12074,7 @@ var uiMessagesSchema = lazySchema2(
11929
12074
  type: z.literal("dynamic-tool"),
11930
12075
  toolName: z.string(),
11931
12076
  toolCallId: z.string(),
12077
+ title: z.string().optional(),
11932
12078
  toolMetadata: toolMetadataSchema2.optional(),
11933
12079
  state: z.literal("approval-responded"),
11934
12080
  input: z.unknown(),
@@ -11950,6 +12096,7 @@ var uiMessagesSchema = lazySchema2(
11950
12096
  type: z.literal("dynamic-tool"),
11951
12097
  toolName: z.string(),
11952
12098
  toolCallId: z.string(),
12099
+ title: z.string().optional(),
11953
12100
  toolMetadata: toolMetadataSchema2.optional(),
11954
12101
  state: z.literal("output-available"),
11955
12102
  input: z.unknown(),
@@ -11973,6 +12120,7 @@ var uiMessagesSchema = lazySchema2(
11973
12120
  type: z.literal("dynamic-tool"),
11974
12121
  toolName: z.string(),
11975
12122
  toolCallId: z.string(),
12123
+ title: z.string().optional(),
11976
12124
  toolMetadata: toolMetadataSchema2.optional(),
11977
12125
  state: z.literal("output-error"),
11978
12126
  input: z.unknown().optional(),
@@ -11996,6 +12144,7 @@ var uiMessagesSchema = lazySchema2(
11996
12144
  type: z.literal("dynamic-tool"),
11997
12145
  toolName: z.string(),
11998
12146
  toolCallId: z.string(),
12147
+ title: z.string().optional(),
11999
12148
  toolMetadata: toolMetadataSchema2.optional(),
12000
12149
  state: z.literal("output-denied"),
12001
12150
  input: z.unknown(),
@@ -12016,6 +12165,7 @@ var uiMessagesSchema = lazySchema2(
12016
12165
  z.object({
12017
12166
  type: z.string().startsWith("tool-"),
12018
12167
  toolCallId: z.string(),
12168
+ title: z.string().optional(),
12019
12169
  toolMetadata: toolMetadataSchema2.optional(),
12020
12170
  state: z.literal("input-streaming"),
12021
12171
  providerExecuted: z.boolean().optional(),
@@ -12028,6 +12178,7 @@ var uiMessagesSchema = lazySchema2(
12028
12178
  z.object({
12029
12179
  type: z.string().startsWith("tool-"),
12030
12180
  toolCallId: z.string(),
12181
+ title: z.string().optional(),
12031
12182
  toolMetadata: toolMetadataSchema2.optional(),
12032
12183
  state: z.literal("input-available"),
12033
12184
  providerExecuted: z.boolean().optional(),
@@ -12040,6 +12191,7 @@ var uiMessagesSchema = lazySchema2(
12040
12191
  z.object({
12041
12192
  type: z.string().startsWith("tool-"),
12042
12193
  toolCallId: z.string(),
12194
+ title: z.string().optional(),
12043
12195
  toolMetadata: toolMetadataSchema2.optional(),
12044
12196
  state: z.literal("approval-requested"),
12045
12197
  input: z.unknown(),
@@ -12060,6 +12212,7 @@ var uiMessagesSchema = lazySchema2(
12060
12212
  z.object({
12061
12213
  type: z.string().startsWith("tool-"),
12062
12214
  toolCallId: z.string(),
12215
+ title: z.string().optional(),
12063
12216
  toolMetadata: toolMetadataSchema2.optional(),
12064
12217
  state: z.literal("approval-responded"),
12065
12218
  input: z.unknown(),
@@ -12080,6 +12233,7 @@ var uiMessagesSchema = lazySchema2(
12080
12233
  z.object({
12081
12234
  type: z.string().startsWith("tool-"),
12082
12235
  toolCallId: z.string(),
12236
+ title: z.string().optional(),
12083
12237
  toolMetadata: toolMetadataSchema2.optional(),
12084
12238
  state: z.literal("output-available"),
12085
12239
  providerExecuted: z.boolean().optional(),
@@ -12102,6 +12256,7 @@ var uiMessagesSchema = lazySchema2(
12102
12256
  z.object({
12103
12257
  type: z.string().startsWith("tool-"),
12104
12258
  toolCallId: z.string(),
12259
+ title: z.string().optional(),
12105
12260
  toolMetadata: toolMetadataSchema2.optional(),
12106
12261
  state: z.literal("output-error"),
12107
12262
  providerExecuted: z.boolean().optional(),
@@ -12124,6 +12279,7 @@ var uiMessagesSchema = lazySchema2(
12124
12279
  z.object({
12125
12280
  type: z.string().startsWith("tool-"),
12126
12281
  toolCallId: z.string(),
12282
+ title: z.string().optional(),
12127
12283
  toolMetadata: toolMetadataSchema2.optional(),
12128
12284
  state: z.literal("output-denied"),
12129
12285
  providerExecuted: z.boolean().optional(),
@@ -12424,14 +12580,40 @@ async function pipeAgentUIStreamToResponse({
12424
12580
  import {
12425
12581
  UnsupportedFunctionalityError as UnsupportedFunctionalityError3
12426
12582
  } from "@ai-sdk/provider";
12583
+ import { gateway as gateway2 } from "@ai-sdk/gateway";
12427
12584
  import { withUserAgentSuffix as withUserAgentSuffix4 } from "@ai-sdk/provider-utils";
12428
- async function startTextBatch({
12429
- model: modelArg,
12585
+
12586
+ // src/util/is-deep-equal-data.ts
12587
+ function isDeepEqualData(obj1, obj2) {
12588
+ if (obj1 === obj2) return true;
12589
+ if (obj1 == null || obj2 == null) return false;
12590
+ if (typeof obj1 !== "object" && typeof obj2 !== "object")
12591
+ return obj1 === obj2;
12592
+ if (obj1.constructor !== obj2.constructor) return false;
12593
+ if (obj1 instanceof Date && obj2 instanceof Date) {
12594
+ return obj1.getTime() === obj2.getTime();
12595
+ }
12596
+ if (Array.isArray(obj1)) {
12597
+ if (obj1.length !== obj2.length) return false;
12598
+ for (let i = 0; i < obj1.length; i++) {
12599
+ if (!isDeepEqualData(obj1[i], obj2[i])) return false;
12600
+ }
12601
+ return true;
12602
+ }
12603
+ const keys1 = Object.keys(obj1);
12604
+ const keys2 = Object.keys(obj2);
12605
+ if (keys1.length !== keys2.length) return false;
12606
+ for (const key of keys1) {
12607
+ if (!keys2.includes(key)) return false;
12608
+ if (!isDeepEqualData(obj1[key], obj2[key])) return false;
12609
+ }
12610
+ return true;
12611
+ }
12612
+
12613
+ // src/batch/batch.ts
12614
+ async function startBatch({
12615
+ provider,
12430
12616
  requests,
12431
- tools,
12432
- toolChoice,
12433
- toolOrder,
12434
- toolsContext,
12435
12617
  providerOptions,
12436
12618
  webhookUrl,
12437
12619
  abortSignal,
@@ -12439,37 +12621,49 @@ async function startTextBatch({
12439
12621
  timeout
12440
12622
  }) {
12441
12623
  validateRequests(requests);
12442
- const model = resolveBatchLanguageModel(modelArg);
12624
+ const batchApi = resolveBatchApi(provider);
12443
12625
  const operationAbortSignal = mergeAbortSignals(
12444
12626
  abortSignal,
12445
12627
  getTotalTimeoutMs(timeout)
12446
12628
  );
12447
- const supportedUrls = await model.supportedUrls;
12448
- const preparedTools = await prepareTools({
12449
- tools,
12450
- toolOrder,
12451
- toolsContext
12452
- });
12453
- const preparedToolChoice = prepareToolChoice({ toolChoice });
12629
+ const supportedUrls = await batchApi.supportedUrls;
12454
12630
  operationAbortSignal == null ? void 0 : operationAbortSignal.throwIfAborted();
12455
12631
  const normalizedRequests = [];
12632
+ const toolsByName = /* @__PURE__ */ new Map();
12456
12633
  for (const request of requests) {
12457
- const standardizedPrompt = await standardizePrompt(request);
12458
- normalizedRequests.push({
12459
- id: request.id,
12460
- options: {
12461
- ...prepareLanguageModelCallOptions(request),
12462
- prompt: await convertToLanguageModelPrompt({
12463
- prompt: standardizedPrompt,
12464
- supportedUrls,
12465
- download: void 0,
12466
- provider: model.provider.split(".")[0]
12467
- }),
12468
- tools: preparedTools,
12469
- toolChoice: preparedToolChoice,
12470
- providerOptions: request.providerOptions
12634
+ switch (request.type) {
12635
+ case "text": {
12636
+ const standardizedPrompt = await standardizePrompt(request);
12637
+ const preparedTools = await prepareTools({
12638
+ tools: request.tools,
12639
+ toolOrder: request.toolOrder,
12640
+ toolsContext: request.toolsContext
12641
+ });
12642
+ validateCompatibleTools({
12643
+ requestId: request.id,
12644
+ tools: preparedTools,
12645
+ toolsByName
12646
+ });
12647
+ normalizedRequests.push({
12648
+ id: request.id,
12649
+ type: request.type,
12650
+ modelId: request.model,
12651
+ options: {
12652
+ ...prepareLanguageModelCallOptions(request),
12653
+ prompt: await convertToLanguageModelPrompt({
12654
+ prompt: standardizedPrompt,
12655
+ supportedUrls,
12656
+ download: void 0,
12657
+ provider: batchApi.provider.split(".")[0]
12658
+ }),
12659
+ tools: preparedTools,
12660
+ toolChoice: prepareToolChoice({ toolChoice: request.toolChoice }),
12661
+ providerOptions: request.providerOptions
12662
+ }
12663
+ });
12664
+ break;
12471
12665
  }
12472
- });
12666
+ }
12473
12667
  operationAbortSignal == null ? void 0 : operationAbortSignal.throwIfAborted();
12474
12668
  }
12475
12669
  const headersWithUserAgent = withUserAgentSuffix4(
@@ -12477,7 +12671,7 @@ async function startTextBatch({
12477
12671
  `ai/${VERSION}`
12478
12672
  );
12479
12673
  try {
12480
- const result = await model.experimental_doStartBatch({
12674
+ const result = await batchApi.doStartBatch({
12481
12675
  requests: normalizedRequests,
12482
12676
  providerOptions,
12483
12677
  abortSignal: operationAbortSignal,
@@ -12485,17 +12679,20 @@ async function startTextBatch({
12485
12679
  ...webhookUrl != null && { webhookUrl }
12486
12680
  });
12487
12681
  const { batchId, warnings, ...status } = result;
12488
- logWarnings({
12489
- warnings: warnings.map(({ warning }) => warning),
12490
- provider: model.provider,
12491
- model: model.modelId
12492
- });
12682
+ const modelByRequestId = new Map(
12683
+ normalizedRequests.map((request) => [request.id, request.modelId])
12684
+ );
12685
+ for (const { requestId, warning } of warnings) {
12686
+ logWarnings({
12687
+ warnings: [warning],
12688
+ provider: batchApi.provider,
12689
+ model: requestId == null ? void 0 : modelByRequestId.get(requestId)
12690
+ });
12691
+ }
12493
12692
  return {
12494
- version: 1,
12495
- type: "text",
12693
+ version: 2,
12496
12694
  id: batchId,
12497
- provider: model.provider,
12498
- modelId: model.modelId,
12695
+ provider: batchApi.provider,
12499
12696
  ...status,
12500
12697
  warnings
12501
12698
  };
@@ -12503,8 +12700,25 @@ async function startTextBatch({
12503
12700
  throw wrapGatewayError(error);
12504
12701
  }
12505
12702
  }
12703
+ function validateCompatibleTools({
12704
+ requestId,
12705
+ tools,
12706
+ toolsByName
12707
+ }) {
12708
+ for (const tool2 of tools != null ? tools : []) {
12709
+ const previousTool = toolsByName.get(tool2.name);
12710
+ if (previousTool != null && !isDeepEqualData(previousTool, tool2)) {
12711
+ throw new InvalidArgumentError({
12712
+ parameter: "requests",
12713
+ value: requestId,
12714
+ message: `tool "${tool2.name}" must have the same definition in every batch request`
12715
+ });
12716
+ }
12717
+ toolsByName.set(tool2.name, tool2);
12718
+ }
12719
+ }
12506
12720
  async function getBatchStatus({
12507
- model: modelArg,
12721
+ provider,
12508
12722
  batch,
12509
12723
  providerOptions,
12510
12724
  maxRetries,
@@ -12512,8 +12726,8 @@ async function getBatchStatus({
12512
12726
  headers,
12513
12727
  timeout
12514
12728
  }) {
12515
- const model = resolveBatchLanguageModel(modelArg);
12516
- validateBatchReference({ model, batch });
12729
+ const batchApi = resolveBatchApi(provider);
12730
+ validateBatchReference({ batchApi, batch });
12517
12731
  const operationAbortSignal = mergeAbortSignals(
12518
12732
  abortSignal,
12519
12733
  getTotalTimeoutMs(timeout)
@@ -12524,7 +12738,7 @@ async function getBatchStatus({
12524
12738
  });
12525
12739
  try {
12526
12740
  const status = await retry(
12527
- () => model.experimental_doGetBatchStatus({
12741
+ () => batchApi.doGetBatchStatus({
12528
12742
  batchId: batch.id,
12529
12743
  providerOptions,
12530
12744
  abortSignal: operationAbortSignal,
@@ -12537,7 +12751,7 @@ async function getBatchStatus({
12537
12751
  }
12538
12752
  }
12539
12753
  function getBatchResults({
12540
- model: modelArg,
12754
+ provider,
12541
12755
  batch,
12542
12756
  tools,
12543
12757
  providerOptions,
@@ -12546,8 +12760,8 @@ function getBatchResults({
12546
12760
  headers,
12547
12761
  timeout
12548
12762
  }) {
12549
- const model = resolveBatchLanguageModel(modelArg);
12550
- validateBatchReference({ model, batch });
12763
+ const batchApi = resolveBatchApi(provider);
12764
+ validateBatchReference({ batchApi, batch });
12551
12765
  const streamAbortController = new AbortController();
12552
12766
  const operationAbortSignal = mergeAbortSignals(
12553
12767
  abortSignal,
@@ -12572,7 +12786,7 @@ function getBatchResults({
12572
12786
  void (async () => {
12573
12787
  try {
12574
12788
  const stream = await retry(
12575
- () => model.experimental_doGetBatchResults({
12789
+ () => batchApi.doGetBatchResults({
12576
12790
  batchId: batch.id,
12577
12791
  providerOptions,
12578
12792
  abortSignal: operationAbortSignal,
@@ -12589,19 +12803,26 @@ function getBatchResults({
12589
12803
  })();
12590
12804
  return asAsyncIterableStream(transform.readable);
12591
12805
  }
12592
- function resolveBatchLanguageModel(modelArg) {
12593
- const model = resolveLanguageModel(modelArg);
12594
- if (!isBatchLanguageModel(model)) {
12806
+ function resolveBatchApi(provider) {
12807
+ var _a25;
12808
+ provider != null ? provider : provider = asProviderV4((_a25 = globalThis.AI_SDK_DEFAULT_PROVIDER) != null ? _a25 : gateway2);
12809
+ if (isBatchApi(provider)) {
12810
+ return provider;
12811
+ }
12812
+ if (!hasBatchFactory(provider)) {
12595
12813
  throw new UnsupportedFunctionalityError3({
12596
12814
  functionality: "batch processing",
12597
- message: `The ${model.provider} model "${model.modelId}" does not support batch processing.`
12815
+ message: "The provider does not support batch processing. Make sure it exposes an experimental_batch() method."
12598
12816
  });
12599
12817
  }
12600
- return model;
12818
+ return provider.experimental_batch();
12819
+ }
12820
+ function hasBatchFactory(provider) {
12821
+ return typeof provider.experimental_batch === "function";
12601
12822
  }
12602
- function isBatchLanguageModel(model) {
12603
- const candidate = model;
12604
- return typeof candidate.experimental_doStartBatch === "function" && typeof candidate.experimental_doGetBatchStatus === "function" && typeof candidate.experimental_doGetBatchResults === "function";
12823
+ function isBatchApi(provider) {
12824
+ const candidate = provider;
12825
+ return typeof candidate.doStartBatch === "function" && typeof candidate.doGetBatchStatus === "function" && typeof candidate.doGetBatchResults === "function";
12605
12826
  }
12606
12827
  function validateRequests(requests) {
12607
12828
  if (requests.length === 0) {
@@ -12631,21 +12852,21 @@ function validateRequests(requests) {
12631
12852
  }
12632
12853
  }
12633
12854
  function validateBatchReference({
12634
- model,
12855
+ batchApi,
12635
12856
  batch
12636
12857
  }) {
12637
- if (batch.version !== 1 || batch.type !== "text") {
12858
+ if (batch.version !== 2) {
12638
12859
  throw new InvalidArgumentError({
12639
12860
  parameter: "batch",
12640
12861
  value: batch,
12641
- message: "batch must be a supported text batch reference"
12862
+ message: "batch must be a supported batch reference"
12642
12863
  });
12643
12864
  }
12644
- if (batch.provider !== model.provider || batch.modelId !== model.modelId) {
12865
+ if (batch.provider !== batchApi.provider) {
12645
12866
  throw new InvalidArgumentError({
12646
- parameter: "model",
12647
- value: model,
12648
- message: `model ${model.provider}:${model.modelId} is not compatible with batch ${batch.provider}:${batch.modelId}`
12867
+ parameter: "provider",
12868
+ value: batchApi,
12869
+ message: `provider ${batchApi.provider} is not compatible with batch provider ${batch.provider}`
12649
12870
  });
12650
12871
  }
12651
12872
  }
@@ -12653,14 +12874,32 @@ async function convertBatchItemResult({
12653
12874
  item,
12654
12875
  tools
12655
12876
  }) {
12656
- if (item.status !== "succeeded") {
12657
- return item;
12877
+ switch (item.type) {
12878
+ case "text":
12879
+ switch (item.status) {
12880
+ case "succeeded":
12881
+ return {
12882
+ id: item.id,
12883
+ status: item.status,
12884
+ ...await convertGenerateResult({ result: item.result, tools })
12885
+ };
12886
+ case "failed":
12887
+ return {
12888
+ id: item.id,
12889
+ status: item.status,
12890
+ error: item.error,
12891
+ providerMetadata: item.providerMetadata
12892
+ };
12893
+ case "cancelled":
12894
+ case "expired":
12895
+ return {
12896
+ id: item.id,
12897
+ status: item.status,
12898
+ error: item.error,
12899
+ providerMetadata: item.providerMetadata
12900
+ };
12901
+ }
12658
12902
  }
12659
- return {
12660
- id: item.id,
12661
- status: "succeeded",
12662
- ...await convertGenerateResult({ result: item.result, tools })
12663
- };
12664
12903
  }
12665
12904
  async function convertGenerateResult({
12666
12905
  result,
@@ -12859,6 +13098,7 @@ var DefaultEmbedResult = class {
12859
13098
  };
12860
13099
 
12861
13100
  // src/embed/embed-many.ts
13101
+ import { InvalidResponseDataError as InvalidResponseDataError2 } from "@ai-sdk/provider";
12862
13102
  import {
12863
13103
  createIdGenerator as createIdGenerator5,
12864
13104
  withUserAgentSuffix as withUserAgentSuffix6
@@ -12995,6 +13235,7 @@ async function embedMany({
12995
13235
  response: modelResponse.response
12996
13236
  };
12997
13237
  });
13238
+ validateEmbeddingCount({ embeddings: embeddings2, values });
12998
13239
  logWarnings({
12999
13240
  warnings: warnings2,
13000
13241
  provider: model.provider,
@@ -13040,8 +13281,8 @@ async function embedMany({
13040
13281
  );
13041
13282
  for (const parallelChunk of parallelChunks) {
13042
13283
  const results = await Promise.all(
13043
- parallelChunk.map((chunk) => {
13044
- return retry(async () => {
13284
+ parallelChunk.map(async (chunk) => {
13285
+ const result = await retry(async () => {
13045
13286
  var _a27, _b26;
13046
13287
  const embedCallId = generateCallId();
13047
13288
  await notify({
@@ -13084,6 +13325,11 @@ async function embedMany({
13084
13325
  response: modelResponse.response
13085
13326
  };
13086
13327
  });
13328
+ validateEmbeddingCount({
13329
+ embeddings: result.embeddings,
13330
+ values: chunk
13331
+ });
13332
+ return result;
13087
13333
  })
13088
13334
  );
13089
13335
  for (const result of results) {
@@ -13142,6 +13388,17 @@ async function embedMany({
13142
13388
  }
13143
13389
  });
13144
13390
  }
13391
+ function validateEmbeddingCount({
13392
+ embeddings,
13393
+ values
13394
+ }) {
13395
+ if (embeddings.length !== values.length) {
13396
+ throw new InvalidResponseDataError2({
13397
+ data: embeddings,
13398
+ message: `Expected ${values.length} embeddings, but received ${embeddings.length}.`
13399
+ });
13400
+ }
13401
+ }
13145
13402
  var textEncoder = new TextEncoder();
13146
13403
  function splitByEmbeddingLimits({
13147
13404
  values,
@@ -13239,6 +13496,12 @@ var gatewayCostMetadataKeys = [
13239
13496
  "outputInferenceCost",
13240
13497
  "surchargeCost"
13241
13498
  ];
13499
+ var RetryableNoImageResultError = class extends Error {
13500
+ constructor() {
13501
+ super("No image generated.");
13502
+ this.name = "RetryableNoImageResultError";
13503
+ }
13504
+ };
13242
13505
  async function generateImage({
13243
13506
  model: modelArg,
13244
13507
  prompt: promptArg,
@@ -13260,7 +13523,8 @@ async function generateImage({
13260
13523
  );
13261
13524
  const { retry } = prepareRetries({
13262
13525
  maxRetries: maxRetriesArg,
13263
- abortSignal
13526
+ abortSignal,
13527
+ additionalRetryableError: (error) => error instanceof RetryableNoImageResultError
13264
13528
  });
13265
13529
  const maxImagesPerCallWithDefault = (_a25 = maxImagesPerCall != null ? maxImagesPerCall : await invokeModelMaxImagesPerCall(model)) != null ? _a25 : 1;
13266
13530
  const callCount = Math.ceil(n / maxImagesPerCallWithDefault);
@@ -13271,25 +13535,41 @@ async function generateImage({
13271
13535
  const remainder = n % maxImagesPerCallWithDefault;
13272
13536
  return remainder === 0 ? maxImagesPerCallWithDefault : remainder;
13273
13537
  });
13274
- const results = await Promise.all(
13275
- callImageCounts.map(
13276
- async (callImageCount) => await retry(() => {
13277
- const { prompt, files, mask } = normalizePrompt(promptArg);
13278
- return model.doGenerate({
13279
- prompt,
13280
- files,
13281
- mask,
13282
- n: callImageCount,
13283
- abortSignal,
13284
- headers: headersWithUserAgent,
13285
- size,
13286
- aspectRatio,
13287
- seed,
13288
- providerOptions: providerOptions != null ? providerOptions : {}
13538
+ const resultGroups = await Promise.all(
13539
+ callImageCounts.map(async (callImageCount) => {
13540
+ const callResults = [];
13541
+ try {
13542
+ await retry(async () => {
13543
+ const { prompt, files, mask } = normalizePrompt(promptArg);
13544
+ const result = await model.doGenerate({
13545
+ prompt,
13546
+ files,
13547
+ mask,
13548
+ n: callImageCount,
13549
+ abortSignal,
13550
+ headers: headersWithUserAgent,
13551
+ size,
13552
+ aspectRatio,
13553
+ seed,
13554
+ providerOptions: providerOptions != null ? providerOptions : {}
13555
+ });
13556
+ callResults.push(result);
13557
+ if (result.images.length === 0 && result.isRetryable !== false) {
13558
+ throw new RetryableNoImageResultError();
13559
+ }
13560
+ return result;
13289
13561
  });
13290
- })
13291
- )
13562
+ return callResults;
13563
+ } catch (error) {
13564
+ const noImageResultError = error instanceof RetryableNoImageResultError ? error : RetryError.isInstance(error) && error.lastError instanceof RetryableNoImageResultError ? error.lastError : void 0;
13565
+ if (noImageResultError != null) {
13566
+ return callResults;
13567
+ }
13568
+ throw error;
13569
+ }
13570
+ })
13292
13571
  );
13572
+ const results = resultGroups.flat();
13293
13573
  const images = [];
13294
13574
  const calls = [];
13295
13575
  const warnings = [];
@@ -13370,7 +13650,7 @@ async function generateImage({
13370
13650
  }
13371
13651
  logWarnings({ warnings, provider: model.provider, model: model.modelId });
13372
13652
  if (!images.length) {
13373
- throw new NoImageGeneratedError({ responses });
13653
+ throw new NoImageGeneratedError({ calls, responses });
13374
13654
  }
13375
13655
  return new DefaultGenerateImageResult({
13376
13656
  images,
@@ -14240,39 +14520,12 @@ function getTextFromDataUrl(dataUrl) {
14240
14520
  throw new Error("Invalid data URL format");
14241
14521
  }
14242
14522
  try {
14243
- return window.atob(base64Content);
14523
+ return globalThis.atob(base64Content);
14244
14524
  } catch (e) {
14245
14525
  throw new Error(`Error decoding data URL`);
14246
14526
  }
14247
14527
  }
14248
14528
 
14249
- // src/util/is-deep-equal-data.ts
14250
- function isDeepEqualData(obj1, obj2) {
14251
- if (obj1 === obj2) return true;
14252
- if (obj1 == null || obj2 == null) return false;
14253
- if (typeof obj1 !== "object" && typeof obj2 !== "object")
14254
- return obj1 === obj2;
14255
- if (obj1.constructor !== obj2.constructor) return false;
14256
- if (obj1 instanceof Date && obj2 instanceof Date) {
14257
- return obj1.getTime() === obj2.getTime();
14258
- }
14259
- if (Array.isArray(obj1)) {
14260
- if (obj1.length !== obj2.length) return false;
14261
- for (let i = 0; i < obj1.length; i++) {
14262
- if (!isDeepEqualData(obj1[i], obj2[i])) return false;
14263
- }
14264
- return true;
14265
- }
14266
- const keys1 = Object.keys(obj1);
14267
- const keys2 = Object.keys(obj2);
14268
- if (keys1.length !== keys2.length) return false;
14269
- for (const key of keys1) {
14270
- if (!keys2.includes(key)) return false;
14271
- if (!isDeepEqualData(obj1[key], obj2[key])) return false;
14272
- }
14273
- return true;
14274
- }
14275
-
14276
14529
  // src/util/serial-job-executor.ts
14277
14530
  var SerialJobExecutor = class {
14278
14531
  constructor() {
@@ -18815,8 +19068,8 @@ var AbstractChat = class {
18815
19068
  }
18816
19069
  this.state.messages = this.state.messages.slice(0, messageIndex + 1);
18817
19070
  this.state.replaceMessage(messageIndex, {
18818
- ...uiMessage,
18819
19071
  id: message.messageId,
19072
+ ...uiMessage,
18820
19073
  role: (_b25 = uiMessage.role) != null ? _b25 : "user",
18821
19074
  metadata: message.metadata
18822
19075
  });
@@ -19523,7 +19776,7 @@ export {
19523
19776
  getRealtimeToolDefinitions as experimental_getRealtimeToolDefinitions,
19524
19777
  experimental_getVideoStatus,
19525
19778
  resampleAudio as experimental_resampleAudio,
19526
- startTextBatch as experimental_startTextBatch,
19779
+ startBatch as experimental_startBatch,
19527
19780
  experimental_startVideo,
19528
19781
  streamLanguageModelCall as experimental_streamLanguageModelCall,
19529
19782
  streamTranscribe as experimental_streamTranscribe,
@@ -19533,7 +19786,7 @@ export {
19533
19786
  extractJsonMiddleware,
19534
19787
  extractReasoningMiddleware,
19535
19788
  fingerprintTools,
19536
- gateway2 as gateway,
19789
+ gateway3 as gateway,
19537
19790
  generateId3 as generateId,
19538
19791
  generateImage,
19539
19792
  generateObject,