ai 5.0.249 → 5.0.251

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
@@ -727,7 +727,7 @@ import {
727
727
  } from "@ai-sdk/provider-utils";
728
728
 
729
729
  // src/version.ts
730
- var VERSION = true ? "5.0.249" : "0.0.0-test";
730
+ var VERSION = true ? "5.0.251" : "0.0.0-test";
731
731
 
732
732
  // src/util/download/download.ts
733
733
  var download = async ({
@@ -1744,12 +1744,13 @@ function asArray(value) {
1744
1744
 
1745
1745
  // src/util/retry-with-exponential-backoff.ts
1746
1746
  import { APICallError as APICallError2 } from "@ai-sdk/provider";
1747
+ import { GatewayError } from "@ai-sdk/gateway";
1747
1748
  import { delay, getErrorMessage as getErrorMessage3, isAbortError } from "@ai-sdk/provider-utils";
1748
1749
  function getRetryDelayInMs({
1749
1750
  error,
1750
1751
  exponentialBackoffDelay
1751
1752
  }) {
1752
- const headers = error.responseHeaders;
1753
+ const headers = APICallError2.isInstance(error) ? error.responseHeaders : APICallError2.isInstance(error.cause) ? error.cause.responseHeaders : void 0;
1753
1754
  if (!headers)
1754
1755
  return exponentialBackoffDelay;
1755
1756
  let ms;
@@ -1810,7 +1811,7 @@ async function _retryWithExponentialBackoff(f, {
1810
1811
  errors: newErrors
1811
1812
  });
1812
1813
  }
1813
- if (error instanceof Error && APICallError2.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
1814
+ if (error instanceof Error && (APICallError2.isInstance(error) && error.isRetryable === true || GatewayError.isInstance(error) && error.isRetryable === true) && tryNumber <= maxRetries) {
1814
1815
  await delay(
1815
1816
  getRetryDelayInMs({
1816
1817
  error,
@@ -10081,10 +10082,38 @@ var TextStreamChatTransport = class extends HttpChatTransport {
10081
10082
  import { TypeValidationError as TypeValidationError4 } from "@ai-sdk/provider";
10082
10083
  import {
10083
10084
  lazyValidator as lazyValidator2,
10085
+ safeValidateTypes as safeValidateTypes5,
10084
10086
  validateTypes as validateTypes2,
10085
10087
  zodSchema as zodSchema2
10086
10088
  } from "@ai-sdk/provider-utils";
10087
10089
  import { z as z8 } from "zod/v4";
10090
+ function isEmptyObject(value) {
10091
+ return value != null && typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0;
10092
+ }
10093
+ function asDynamicToolPart(toolPart) {
10094
+ const common = {
10095
+ type: "dynamic-tool",
10096
+ toolName: toolPart.type.slice(5),
10097
+ toolCallId: toolPart.toolCallId,
10098
+ ...toolPart.providerExecuted === void 0 ? {} : { providerExecuted: toolPart.providerExecuted },
10099
+ ...toolPart.callProviderMetadata === void 0 ? {} : { callProviderMetadata: toolPart.callProviderMetadata }
10100
+ };
10101
+ if (toolPart.state === "output-available") {
10102
+ return {
10103
+ ...common,
10104
+ state: "output-available",
10105
+ input: toolPart.input,
10106
+ output: toolPart.output,
10107
+ ...toolPart.preliminary === void 0 ? {} : { preliminary: toolPart.preliminary }
10108
+ };
10109
+ }
10110
+ return {
10111
+ ...common,
10112
+ state: "output-error",
10113
+ input: toolPart.input,
10114
+ errorText: toolPart.errorText
10115
+ };
10116
+ }
10088
10117
  var uiMessagesSchema = lazyValidator2(
10089
10118
  () => zodSchema2(
10090
10119
  z8.array(
@@ -10175,7 +10204,8 @@ var uiMessagesSchema = lazyValidator2(
10175
10204
  toolName: z8.string(),
10176
10205
  toolCallId: z8.string(),
10177
10206
  state: z8.literal("output-error"),
10178
- input: z8.unknown(),
10207
+ input: z8.unknown().optional(),
10208
+ rawInput: z8.unknown().optional(),
10179
10209
  providerExecuted: z8.boolean().optional(),
10180
10210
  output: z8.never().optional(),
10181
10211
  errorText: z8.string(),
@@ -10253,7 +10283,8 @@ var uiMessagesSchema = lazyValidator2(
10253
10283
  toolCallId: z8.string(),
10254
10284
  state: z8.literal("output-error"),
10255
10285
  providerExecuted: z8.boolean().optional(),
10256
- input: z8.unknown(),
10286
+ input: z8.unknown().optional(),
10287
+ rawInput: z8.unknown().optional(),
10257
10288
  output: z8.never().optional(),
10258
10289
  errorText: z8.string(),
10259
10290
  callProviderMetadata: providerMetadataSchema.optional(),
@@ -10317,6 +10348,17 @@ async function safeValidateUIMessages({
10317
10348
  value: messages,
10318
10349
  schema: uiMessagesSchema
10319
10350
  });
10351
+ for (const message of validatedMessages) {
10352
+ for (const part of message.parts) {
10353
+ if (part.type !== "dynamic-tool" && !part.type.startsWith("tool-")) {
10354
+ continue;
10355
+ }
10356
+ const toolPart = part;
10357
+ if (toolPart.state === "output-error" && !("input" in toolPart)) {
10358
+ toolPart.input = void 0;
10359
+ }
10360
+ }
10361
+ }
10320
10362
  if (metadataSchema) {
10321
10363
  for (const message of validatedMessages) {
10322
10364
  await validateTypes2({
@@ -10351,12 +10393,17 @@ async function safeValidateUIMessages({
10351
10393
  }
10352
10394
  if (tools) {
10353
10395
  for (const message of validatedMessages) {
10354
- const toolParts = message.parts.filter(
10355
- (part) => part.type.startsWith("tool-")
10356
- );
10357
- for (const toolPart of toolParts) {
10396
+ for (const [partIdx, part] of message.parts.entries()) {
10397
+ if (!part.type.startsWith("tool-")) {
10398
+ continue;
10399
+ }
10400
+ const toolPart = part;
10358
10401
  const toolName = toolPart.type.slice(5);
10359
10402
  const tool2 = tools[toolName];
10403
+ if (!tool2 && (toolPart.state === "output-available" || toolPart.state === "output-error")) {
10404
+ message.parts[partIdx] = asDynamicToolPart(toolPart);
10405
+ continue;
10406
+ }
10360
10407
  if (!tool2) {
10361
10408
  return {
10362
10409
  success: false,
@@ -10366,7 +10413,30 @@ async function safeValidateUIMessages({
10366
10413
  })
10367
10414
  };
10368
10415
  }
10369
- if (toolPart.state === "input-available") {
10416
+ let dynamicToolPart;
10417
+ if (toolPart.state === "output-error") {
10418
+ if (toolPart.input !== void 0) {
10419
+ const result = await safeValidateTypes5({
10420
+ value: toolPart.input,
10421
+ schema: tool2.inputSchema
10422
+ });
10423
+ if (!result.success) {
10424
+ dynamicToolPart = asDynamicToolPart(toolPart);
10425
+ }
10426
+ }
10427
+ } else if (toolPart.state === "output-available") {
10428
+ const result = await safeValidateTypes5({
10429
+ value: toolPart.input,
10430
+ schema: tool2.inputSchema
10431
+ });
10432
+ if (!result.success) {
10433
+ if (isEmptyObject(toolPart.input)) {
10434
+ dynamicToolPart = asDynamicToolPart(toolPart);
10435
+ } else {
10436
+ throw result.error;
10437
+ }
10438
+ }
10439
+ } else if (toolPart.state === "input-available") {
10370
10440
  await validateTypes2({
10371
10441
  value: toolPart.input,
10372
10442
  schema: tool2.inputSchema
@@ -10378,6 +10448,9 @@ async function safeValidateUIMessages({
10378
10448
  schema: tool2.outputSchema
10379
10449
  });
10380
10450
  }
10451
+ if (dynamicToolPart) {
10452
+ message.parts[partIdx] = dynamicToolPart;
10453
+ }
10381
10454
  }
10382
10455
  }
10383
10456
  }
@@ -10393,18 +10466,8 @@ async function safeValidateUIMessages({
10393
10466
  };
10394
10467
  }
10395
10468
  }
10396
- async function validateUIMessages({
10397
- messages,
10398
- metadataSchema,
10399
- dataSchemas,
10400
- tools
10401
- }) {
10402
- const response = await safeValidateUIMessages({
10403
- messages,
10404
- metadataSchema,
10405
- dataSchemas,
10406
- tools
10407
- });
10469
+ async function validateUIMessages(options) {
10470
+ const response = await safeValidateUIMessages(options);
10408
10471
  if (!response.success)
10409
10472
  throw response.error;
10410
10473
  return response.data;