ai 3.1.7 → 3.1.8

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
@@ -4,6 +4,96 @@ var __export = (target, all) => {
4
4
  __defProp(target, name, { get: all[name], enumerable: true });
5
5
  };
6
6
 
7
+ // core/util/retry-with-exponential-backoff.ts
8
+ import { APICallError, RetryError } from "@ai-sdk/provider";
9
+ import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
10
+
11
+ // core/util/delay.ts
12
+ async function delay(delayInMs) {
13
+ return new Promise((resolve) => setTimeout(resolve, delayInMs));
14
+ }
15
+
16
+ // core/util/retry-with-exponential-backoff.ts
17
+ var retryWithExponentialBackoff = ({
18
+ maxRetries = 2,
19
+ initialDelayInMs = 2e3,
20
+ backoffFactor = 2
21
+ } = {}) => async (f) => _retryWithExponentialBackoff(f, {
22
+ maxRetries,
23
+ delayInMs: initialDelayInMs,
24
+ backoffFactor
25
+ });
26
+ async function _retryWithExponentialBackoff(f, {
27
+ maxRetries,
28
+ delayInMs,
29
+ backoffFactor
30
+ }, errors = []) {
31
+ try {
32
+ return await f();
33
+ } catch (error) {
34
+ if (isAbortError(error)) {
35
+ throw error;
36
+ }
37
+ if (maxRetries === 0) {
38
+ throw error;
39
+ }
40
+ const errorMessage = getErrorMessage(error);
41
+ const newErrors = [...errors, error];
42
+ const tryNumber = newErrors.length;
43
+ if (tryNumber > maxRetries) {
44
+ throw new RetryError({
45
+ message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
46
+ reason: "maxRetriesExceeded",
47
+ errors: newErrors
48
+ });
49
+ }
50
+ if (error instanceof Error && APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
51
+ await delay(delayInMs);
52
+ return _retryWithExponentialBackoff(
53
+ f,
54
+ { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
55
+ newErrors
56
+ );
57
+ }
58
+ if (tryNumber === 1) {
59
+ throw error;
60
+ }
61
+ throw new RetryError({
62
+ message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
63
+ reason: "errorNotRetryable",
64
+ errors: newErrors
65
+ });
66
+ }
67
+ }
68
+
69
+ // core/embed/embed.ts
70
+ async function embed({
71
+ model,
72
+ value,
73
+ maxRetries,
74
+ abortSignal
75
+ }) {
76
+ const retry = retryWithExponentialBackoff({ maxRetries });
77
+ const modelResponse = await retry(
78
+ () => model.doEmbed({
79
+ values: [value],
80
+ abortSignal
81
+ })
82
+ );
83
+ return new EmbedResult({
84
+ value,
85
+ embedding: modelResponse.embeddings[0],
86
+ rawResponse: modelResponse.rawResponse
87
+ });
88
+ }
89
+ var EmbedResult = class {
90
+ constructor(options) {
91
+ this.value = options.value;
92
+ this.embedding = options.embedding;
93
+ this.rawResponse = options.rawResponse;
94
+ }
95
+ };
96
+
7
97
  // core/generate-object/generate-object.ts
8
98
  import { NoObjectGeneratedError } from "@ai-sdk/provider";
9
99
  import { safeParseJSON } from "@ai-sdk/provider-utils";
@@ -287,68 +377,6 @@ function convertZodToJSONSchema(zodSchema) {
287
377
  return zodToJsonSchema(zodSchema);
288
378
  }
289
379
 
290
- // core/util/retry-with-exponential-backoff.ts
291
- import { APICallError, RetryError } from "@ai-sdk/provider";
292
- import { getErrorMessage, isAbortError } from "@ai-sdk/provider-utils";
293
-
294
- // core/util/delay.ts
295
- async function delay(delayInMs) {
296
- return new Promise((resolve) => setTimeout(resolve, delayInMs));
297
- }
298
-
299
- // core/util/retry-with-exponential-backoff.ts
300
- var retryWithExponentialBackoff = ({
301
- maxRetries = 2,
302
- initialDelayInMs = 2e3,
303
- backoffFactor = 2
304
- } = {}) => async (f) => _retryWithExponentialBackoff(f, {
305
- maxRetries,
306
- delayInMs: initialDelayInMs,
307
- backoffFactor
308
- });
309
- async function _retryWithExponentialBackoff(f, {
310
- maxRetries,
311
- delayInMs,
312
- backoffFactor
313
- }, errors = []) {
314
- try {
315
- return await f();
316
- } catch (error) {
317
- if (isAbortError(error)) {
318
- throw error;
319
- }
320
- if (maxRetries === 0) {
321
- throw error;
322
- }
323
- const errorMessage = getErrorMessage(error);
324
- const newErrors = [...errors, error];
325
- const tryNumber = newErrors.length;
326
- if (tryNumber > maxRetries) {
327
- throw new RetryError({
328
- message: `Failed after ${tryNumber} attempts. Last error: ${errorMessage}`,
329
- reason: "maxRetriesExceeded",
330
- errors: newErrors
331
- });
332
- }
333
- if (error instanceof Error && APICallError.isAPICallError(error) && error.isRetryable === true && tryNumber <= maxRetries) {
334
- await delay(delayInMs);
335
- return _retryWithExponentialBackoff(
336
- f,
337
- { maxRetries, delayInMs: backoffFactor * delayInMs, backoffFactor },
338
- newErrors
339
- );
340
- }
341
- if (tryNumber === 1) {
342
- throw error;
343
- }
344
- throw new RetryError({
345
- message: `Failed after ${tryNumber} attempts with non-retryable error: '${errorMessage}'`,
346
- reason: "errorNotRetryable",
347
- errors: newErrors
348
- });
349
- }
350
- }
351
-
352
380
  // core/generate-object/inject-json-schema-into-system.ts
353
381
  var DEFAULT_SCHEMA_PREFIX = "JSON schema:";
354
382
  var DEFAULT_SCHEMA_SUFFIX = "You MUST answer with a JSON object that matches the JSON schema above.";
@@ -3220,6 +3248,7 @@ export {
3220
3248
  AnthropicStream,
3221
3249
  AssistantResponse,
3222
3250
  CohereStream,
3251
+ EmbedResult,
3223
3252
  EmptyResponseBodyError,
3224
3253
  GenerateObjectResult,
3225
3254
  GenerateTextResult,
@@ -3256,6 +3285,7 @@ export {
3256
3285
  createChunkDecoder,
3257
3286
  createEventStreamTransformer,
3258
3287
  createStreamDataTransformer,
3288
+ embed,
3259
3289
  experimental_AssistantResponse,
3260
3290
  experimental_StreamData,
3261
3291
  experimental_StreamingReactResponse,