ai 4.0.5 → 4.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai",
3
- "version": "4.0.5",
3
+ "version": "4.0.6",
4
4
  "description": "AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -620,8 +620,7 @@ function prepareCallSettings({
620
620
  presencePenalty,
621
621
  frequencyPenalty,
622
622
  stopSequences,
623
- seed,
624
- maxRetries
623
+ seed
625
624
  }) {
626
625
  if (maxTokens != null) {
627
626
  if (!Number.isInteger(maxTokens)) {
@@ -693,6 +692,108 @@ function prepareCallSettings({
693
692
  });
694
693
  }
695
694
  }
695
+ return {
696
+ maxTokens,
697
+ temperature: temperature != null ? temperature : 0,
698
+ topP,
699
+ topK,
700
+ presencePenalty,
701
+ frequencyPenalty,
702
+ stopSequences: stopSequences != null && stopSequences.length > 0 ? stopSequences : void 0,
703
+ seed
704
+ };
705
+ }
706
+
707
+ // util/retry-with-exponential-backoff.ts
708
+ import { APICallError } from "@ai-sdk/provider";
709
+ import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
710
+
711
+ // util/delay.ts
712
+ async function delay(delayInMs) {
713
+ return delayInMs === void 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delayInMs));
714
+ }
715
+
716
+ // util/retry-error.ts
717
+ import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
718
+ var name5 = "AI_RetryError";
719
+ var marker5 = `vercel.ai.error.${name5}`;
720
+ var symbol5 = Symbol.for(marker5);
721
+ var _a5;
722
+ var RetryError = class extends AISDKError5 {
723
+ constructor({
724
+ message,
725
+ reason,
726
+ errors
727
+ }) {
728
+ super({ name: name5, message });
729
+ this[_a5] = true;
730
+ this.reason = reason;
731
+ this.errors = errors;
732
+ this.lastError = errors[errors.length - 1];
733
+ }
734
+ static isInstance(error) {
735
+ return AISDKError5.hasMarker(error, marker5);
736
+ }
737
+ };
738
+ _a5 = symbol5;
739
+
740
+ // util/retry-with-exponential-backoff.ts
741
+ var retryWithExponentialBackoff = ({
742
+ maxRetries = 2,
743
+ initialDelayInMs = 2e3,
744
+ backoffFactor = 2
745
+ } = {}) => async (f) => _retryWithExponentialBackoff(f, {
746
+ maxRetries,
747
+ delayInMs: initialDelayInMs,
748
+ backoffFactor
749
+ });
750
+ async function _retryWithExponentialBackoff(f, {
751
+ maxRetries,
752
+ delayInMs,
753
+ backoffFactor
754
+ }, errors = []) {
755
+ try {
756
+ return await f();
757
+ } catch (error) {
758
+ if (isAbortError(error)) {
759
+ throw error;
760
+ }
761
+ if (maxRetries === 0) {
762
+ throw error;
763
+ }
764
+ const errorMessage = getErrorMessage(error);
765
+ const newErrors = [...errors, error];
766
+ const tryNumber = newErrors.length;
767
+ if (tryNumber > maxRetries) {
768
+ throw new RetryError({
769
+ message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
770
+ reason: "maxRetriesExceeded",
771
+ errors: newErrors
772
+ });
773
+ }
774
+ if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
775
+ await delay(delayInMs);
776
+ return _retryWithExponentialBackoff(
777
+ f,
778
+ { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
779
+ newErrors
780
+ );
781
+ }
782
+ if (tryNumber === 1) {
783
+ throw error;
784
+ }
785
+ throw new RetryError({
786
+ message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
787
+ reason: "errorNotRetryable",
788
+ errors: newErrors
789
+ });
790
+ }
791
+ }
792
+
793
+ // core/prompt/prepare-retries.ts
794
+ function prepareRetries({
795
+ maxRetries
796
+ }) {
696
797
  if (maxRetries != null) {
697
798
  if (!Number.isInteger(maxRetries)) {
698
799
  throw new InvalidArgumentError({
@@ -709,16 +810,10 @@ function prepareCallSettings({
709
810
  });
710
811
  }
711
812
  }
813
+ const maxRetriesResult = maxRetries != null ? maxRetries : 2;
712
814
  return {
713
- maxTokens,
714
- temperature: temperature != null ? temperature : 0,
715
- topP,
716
- topK,
717
- presencePenalty,
718
- frequencyPenalty,
719
- stopSequences: stopSequences != null && stopSequences.length > 0 ? stopSequences : void 0,
720
- seed,
721
- maxRetries: maxRetries != null ? maxRetries : 2
815
+ maxRetries: maxRetriesResult,
816
+ retry: retryWithExponentialBackoff({ maxRetries: maxRetriesResult })
722
817
  };
723
818
  }
724
819
 
@@ -1001,25 +1096,25 @@ function attachmentsToParts(attachments) {
1001
1096
  }
1002
1097
 
1003
1098
  // core/prompt/message-conversion-error.ts
1004
- import { AISDKError as AISDKError5 } from "@ai-sdk/provider";
1005
- var name5 = "AI_MessageConversionError";
1006
- var marker5 = `vercel.ai.error.${name5}`;
1007
- var symbol5 = Symbol.for(marker5);
1008
- var _a5;
1009
- var MessageConversionError = class extends AISDKError5 {
1099
+ import { AISDKError as AISDKError6 } from "@ai-sdk/provider";
1100
+ var name6 = "AI_MessageConversionError";
1101
+ var marker6 = `vercel.ai.error.${name6}`;
1102
+ var symbol6 = Symbol.for(marker6);
1103
+ var _a6;
1104
+ var MessageConversionError = class extends AISDKError6 {
1010
1105
  constructor({
1011
1106
  originalMessage,
1012
1107
  message
1013
1108
  }) {
1014
- super({ name: name5, message });
1015
- this[_a5] = true;
1109
+ super({ name: name6, message });
1110
+ this[_a6] = true;
1016
1111
  this.originalMessage = originalMessage;
1017
1112
  }
1018
1113
  static isInstance(error) {
1019
- return AISDKError5.hasMarker(error, marker5);
1114
+ return AISDKError6.hasMarker(error, marker6);
1020
1115
  }
1021
1116
  };
1022
- _a5 = symbol5;
1117
+ _a6 = symbol6;
1023
1118
 
1024
1119
  // core/prompt/convert-to-core-messages.ts
1025
1120
  function convertToCoreMessages(messages, options) {
@@ -1192,53 +1287,53 @@ function calculateLanguageModelUsage({
1192
1287
  }
1193
1288
 
1194
1289
  // errors/invalid-tool-arguments-error.ts
1195
- import { AISDKError as AISDKError6, getErrorMessage } from "@ai-sdk/provider";
1196
- var name6 = "AI_InvalidToolArgumentsError";
1197
- var marker6 = `vercel.ai.error.${name6}`;
1198
- var symbol6 = Symbol.for(marker6);
1199
- var _a6;
1200
- var InvalidToolArgumentsError = class extends AISDKError6 {
1290
+ import { AISDKError as AISDKError7, getErrorMessage as getErrorMessage2 } from "@ai-sdk/provider";
1291
+ var name7 = "AI_InvalidToolArgumentsError";
1292
+ var marker7 = `vercel.ai.error.${name7}`;
1293
+ var symbol7 = Symbol.for(marker7);
1294
+ var _a7;
1295
+ var InvalidToolArgumentsError = class extends AISDKError7 {
1201
1296
  constructor({
1202
1297
  toolArgs,
1203
1298
  toolName,
1204
1299
  cause,
1205
- message = `Invalid arguments for tool ${toolName}: ${getErrorMessage(
1300
+ message = `Invalid arguments for tool ${toolName}: ${getErrorMessage2(
1206
1301
  cause
1207
1302
  )}`
1208
1303
  }) {
1209
- super({ name: name6, message, cause });
1210
- this[_a6] = true;
1304
+ super({ name: name7, message, cause });
1305
+ this[_a7] = true;
1211
1306
  this.toolArgs = toolArgs;
1212
1307
  this.toolName = toolName;
1213
1308
  }
1214
1309
  static isInstance(error) {
1215
- return AISDKError6.hasMarker(error, marker6);
1310
+ return AISDKError7.hasMarker(error, marker7);
1216
1311
  }
1217
1312
  };
1218
- _a6 = symbol6;
1313
+ _a7 = symbol7;
1219
1314
 
1220
1315
  // errors/no-such-tool-error.ts
1221
- import { AISDKError as AISDKError7 } from "@ai-sdk/provider";
1222
- var name7 = "AI_NoSuchToolError";
1223
- var marker7 = `vercel.ai.error.${name7}`;
1224
- var symbol7 = Symbol.for(marker7);
1225
- var _a7;
1226
- var NoSuchToolError = class extends AISDKError7 {
1316
+ import { AISDKError as AISDKError8 } from "@ai-sdk/provider";
1317
+ var name8 = "AI_NoSuchToolError";
1318
+ var marker8 = `vercel.ai.error.${name8}`;
1319
+ var symbol8 = Symbol.for(marker8);
1320
+ var _a8;
1321
+ var NoSuchToolError = class extends AISDKError8 {
1227
1322
  constructor({
1228
1323
  toolName,
1229
1324
  availableTools = void 0,
1230
1325
  message = `Model tried to call unavailable tool '${toolName}'. ${availableTools === void 0 ? "No tools are available." : `Available tools: ${availableTools.join(", ")}.`}`
1231
1326
  }) {
1232
- super({ name: name7, message });
1233
- this[_a7] = true;
1327
+ super({ name: name8, message });
1328
+ this[_a8] = true;
1234
1329
  this.toolName = toolName;
1235
1330
  this.availableTools = availableTools;
1236
1331
  }
1237
1332
  static isInstance(error) {
1238
- return AISDKError7.hasMarker(error, marker7);
1333
+ return AISDKError8.hasMarker(error, marker8);
1239
1334
  }
1240
1335
  };
1241
- _a7 = symbol7;
1336
+ _a8 = symbol8;
1242
1337
 
1243
1338
  // util/is-async-generator.ts
1244
1339
  function isAsyncGenerator(value) {
@@ -1250,92 +1345,6 @@ function isGenerator(value) {
1250
1345
  return value != null && typeof value === "object" && Symbol.iterator in value;
1251
1346
  }
1252
1347
 
1253
- // util/retry-with-exponential-backoff.ts
1254
- import { APICallError } from "@ai-sdk/provider";
1255
- import { getErrorMessage as getErrorMessage2, isAbortError } from "@ai-sdk/provider-utils";
1256
-
1257
- // util/delay.ts
1258
- async function delay(delayInMs) {
1259
- return delayInMs === void 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delayInMs));
1260
- }
1261
-
1262
- // util/retry-error.ts
1263
- import { AISDKError as AISDKError8 } from "@ai-sdk/provider";
1264
- var name8 = "AI_RetryError";
1265
- var marker8 = `vercel.ai.error.${name8}`;
1266
- var symbol8 = Symbol.for(marker8);
1267
- var _a8;
1268
- var RetryError = class extends AISDKError8 {
1269
- constructor({
1270
- message,
1271
- reason,
1272
- errors
1273
- }) {
1274
- super({ name: name8, message });
1275
- this[_a8] = true;
1276
- this.reason = reason;
1277
- this.errors = errors;
1278
- this.lastError = errors[errors.length - 1];
1279
- }
1280
- static isInstance(error) {
1281
- return AISDKError8.hasMarker(error, marker8);
1282
- }
1283
- };
1284
- _a8 = symbol8;
1285
-
1286
- // util/retry-with-exponential-backoff.ts
1287
- var retryWithExponentialBackoff = ({
1288
- maxRetries = 2,
1289
- initialDelayInMs = 2e3,
1290
- backoffFactor = 2
1291
- } = {}) => async (f) => _retryWithExponentialBackoff(f, {
1292
- maxRetries,
1293
- delayInMs: initialDelayInMs,
1294
- backoffFactor
1295
- });
1296
- async function _retryWithExponentialBackoff(f, {
1297
- maxRetries,
1298
- delayInMs,
1299
- backoffFactor
1300
- }, errors = []) {
1301
- try {
1302
- return await f();
1303
- } catch (error) {
1304
- if (isAbortError(error)) {
1305
- throw error;
1306
- }
1307
- if (maxRetries === 0) {
1308
- throw error;
1309
- }
1310
- const errorMessage = getErrorMessage2(error);
1311
- const newErrors = [...errors, error];
1312
- const tryNumber = newErrors.length;
1313
- if (tryNumber > maxRetries) {
1314
- throw new RetryError({
1315
- message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
1316
- reason: "maxRetriesExceeded",
1317
- errors: newErrors
1318
- });
1319
- }
1320
- if (error instanceof Error && APICallError.isInstance(error) && error.isRetryable === true && tryNumber <= maxRetries) {
1321
- await delay(delayInMs);
1322
- return _retryWithExponentialBackoff(
1323
- f,
1324
- { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
1325
- newErrors
1326
- );
1327
- }
1328
- if (tryNumber === 1) {
1329
- throw error;
1330
- }
1331
- throw new RetryError({
1332
- message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
1333
- reason: "errorNotRetryable",
1334
- errors: newErrors
1335
- });
1336
- }
1337
- }
1338
-
1339
1348
  // util/constants.ts
1340
1349
  var HANGING_STREAM_WARNING_TIME_MS = 15 * 1e3;
1341
1350
 
@@ -1524,7 +1533,7 @@ async function streamUI({
1524
1533
  }
1525
1534
  renderFinished.resolve(void 0);
1526
1535
  }
1527
- const retry = retryWithExponentialBackoff({ maxRetries });
1536
+ const { retry } = prepareRetries({ maxRetries });
1528
1537
  const validatedPrompt = standardizePrompt({
1529
1538
  prompt: { system, prompt, messages },
1530
1539
  tools: void 0