ai 3.1.0-canary.2 → 3.1.0-canary.4

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.
@@ -1,30 +1,5 @@
1
- // ai-model-specification/errors/ai-invalid-argument-error.ts
2
- var AI_InvalidArgumentError = class extends Error {
3
- // readonly learnMore =
4
- // 'https://sdk.vercel.com/docs/ai/errors/ai_invalid_argument_error';
5
- constructor({
6
- parameter,
7
- value,
8
- message
9
- }) {
10
- super(`Invalid argument for parameter ${parameter}: ${message}`);
11
- this.name = "AI_InvalidArgumentError";
12
- this.parameter = parameter;
13
- this.value = value;
14
- }
15
- toJSON() {
16
- return {
17
- name: this.name,
18
- message: this.message,
19
- stack: this.stack,
20
- parameter: this.parameter,
21
- value: this.value
22
- };
23
- }
24
- };
25
-
26
1
  // ai-model-specification/errors/api-call-error.ts
27
- var ApiCallError = class extends Error {
2
+ var APICallError = class extends Error {
28
3
  constructor({
29
4
  message,
30
5
  url,
@@ -32,11 +7,15 @@ var ApiCallError = class extends Error {
32
7
  statusCode,
33
8
  responseBody,
34
9
  cause,
35
- isRetryable = statusCode != null && (statusCode === 429 || statusCode >= 500),
10
+ isRetryable = statusCode != null && (statusCode === 408 || // request timeout
11
+ statusCode === 409 || // conflict
12
+ statusCode === 429 || // too many requests
13
+ statusCode >= 500),
14
+ // server error
36
15
  data
37
16
  }) {
38
17
  super(message);
39
- this.name = "ApiCallError";
18
+ this.name = "AI_APICallError";
40
19
  this.url = url;
41
20
  this.requestBodyValues = requestBodyValues;
42
21
  this.statusCode = statusCode;
@@ -45,6 +24,9 @@ var ApiCallError = class extends Error {
45
24
  this.isRetryable = isRetryable;
46
25
  this.data = data;
47
26
  }
27
+ static isAPICallError(error) {
28
+ return error instanceof Error && error.name === "AI_APICallError" && typeof error.url === "string" && typeof error.requestBodyValues === "object" && (error.statusCode == null || typeof error.statusCode === "number") && (error.responseBody == null || typeof error.responseBody === "string") && (error.cause == null || typeof error.cause === "object") && typeof error.isRetryable === "boolean" && (error.data == null || typeof error.data === "object");
29
+ }
48
30
  toJSON() {
49
31
  return {
50
32
  name: this.name,
@@ -60,115 +42,79 @@ var ApiCallError = class extends Error {
60
42
  }
61
43
  };
62
44
 
63
- // ai-model-specification/util/get-error-message.ts
64
- function getErrorMessage(error) {
65
- if (error == null) {
66
- return "unknown error";
67
- }
68
- if (typeof error === "string") {
69
- return error;
70
- }
71
- if (error instanceof Error) {
72
- return error.message;
45
+ // ai-model-specification/errors/invalid-argument-error.ts
46
+ var InvalidArgumentError = class extends Error {
47
+ constructor({
48
+ parameter,
49
+ value,
50
+ message
51
+ }) {
52
+ super(`Invalid argument for parameter ${parameter}: ${message}`);
53
+ this.name = "AI_InvalidArgumentError";
54
+ this.parameter = parameter;
55
+ this.value = value;
73
56
  }
74
- return JSON.stringify(error);
75
- }
76
-
77
- // ai-model-specification/errors/json-parse-error.ts
78
- var JSONParseError = class extends Error {
79
- constructor({ text, cause }) {
80
- super(
81
- `JSON parsing failed: Text: ${text}.
82
- Error message: ${getErrorMessage(cause)}`
83
- );
84
- this.name = "JSONParseError";
85
- this.cause = cause;
86
- this.text = text;
57
+ static isInvalidArgumentError(error) {
58
+ return error instanceof Error && error.name === "AI_InvalidArgumentError" && typeof error.parameter === "string" && typeof error.value === "string";
87
59
  }
88
60
  toJSON() {
89
61
  return {
90
62
  name: this.name,
91
63
  message: this.message,
92
- cause: this.cause,
93
64
  stack: this.stack,
94
- valueText: this.text
65
+ parameter: this.parameter,
66
+ value: this.value
95
67
  };
96
68
  }
97
69
  };
98
70
 
99
- // ai-model-specification/errors/load-api-key-error.ts
100
- var LoadAPIKeyError = class extends Error {
101
- constructor({ message }) {
102
- super(message);
103
- this.name = "LoadAPIKeyError";
104
- }
105
- toJSON() {
106
- return {
107
- name: this.name,
108
- message: this.message
109
- };
71
+ // ai-model-specification/errors/invalid-prompt-error.ts
72
+ var InvalidPromptError = class extends Error {
73
+ constructor({ prompt: prompt2, message }) {
74
+ super(`Invalid prompt: ${message}`);
75
+ this.name = "AI_InvalidPromptError";
76
+ this.prompt = prompt2;
110
77
  }
111
- };
112
-
113
- // ai-model-specification/errors/no-object-generated-error.ts
114
- var NoTextGeneratedError = class extends Error {
115
- constructor() {
116
- super(`No text generated.`);
117
- this.name = "NoTextGeneratedError";
78
+ static isInvalidPromptError(error) {
79
+ return error instanceof Error && error.name === "AI_InvalidPromptError" && prompt != null;
118
80
  }
119
81
  toJSON() {
120
82
  return {
121
83
  name: this.name,
122
- cause: this.cause,
123
84
  message: this.message,
124
- stack: this.stack
85
+ stack: this.stack,
86
+ prompt: this.prompt
125
87
  };
126
88
  }
127
89
  };
128
90
 
129
- // ai-model-specification/errors/type-validation-error.ts
130
- var TypeValidationError = class extends Error {
131
- constructor({ value, cause }) {
132
- super(
133
- `Type validation failed: Value: ${JSON.stringify(value)}.
134
- Error message: ${getErrorMessage(cause)}`
135
- );
136
- this.name = "TypeValidationError";
137
- this.cause = cause;
138
- this.value = value;
91
+ // ai-model-specification/util/get-error-message.ts
92
+ function getErrorMessage(error) {
93
+ if (error == null) {
94
+ return "unknown error";
139
95
  }
140
- toJSON() {
141
- return {
142
- name: this.name,
143
- message: this.message,
144
- cause: this.cause,
145
- stack: this.stack,
146
- value: this.value
147
- };
96
+ if (typeof error === "string") {
97
+ return error;
148
98
  }
149
- };
99
+ if (error instanceof Error) {
100
+ return error.message;
101
+ }
102
+ return JSON.stringify(error);
103
+ }
150
104
 
151
- // ai-model-specification/errors/unsupported-functionality-error.ts
152
- var UnsupportedFunctionalityError = class extends Error {
153
- constructor({
154
- provider,
155
- functionality
156
- }) {
157
- super(
158
- `Functionality not supported by the provider. Provider: ${provider}.
159
- Functionality: ${functionality}`
160
- );
161
- this.name = "UnsupportedFunctionalityError";
162
- this.provider = provider;
163
- this.functionality = functionality;
105
+ // ai-model-specification/errors/load-api-key-error.ts
106
+ var LoadAPIKeyError = class extends Error {
107
+ constructor({ message }) {
108
+ super(message);
109
+ this.name = "AI_LoadAPIKeyError";
110
+ }
111
+ static isLoadAPIKeyError(error) {
112
+ return error instanceof Error && error.name === "AI_LoadAPIKeyError";
164
113
  }
165
114
  toJSON() {
166
115
  return {
167
116
  name: this.name,
168
- message: this.message,
169
- stack: this.stack,
170
- provider: this.provider,
171
- functionality: this.functionality
117
+ message: this.message
172
118
  };
173
119
  }
174
120
  };
@@ -200,6 +146,56 @@ function loadApiKey({
200
146
  // ai-model-specification/util/parse-json.ts
201
147
  import SecureJSON from "secure-json-parse";
202
148
 
149
+ // ai-model-specification/errors/json-parse-error.ts
150
+ var JSONParseError = class extends Error {
151
+ constructor({ text, cause }) {
152
+ super(
153
+ `JSON parsing failed: Text: ${text}.
154
+ Error message: ${getErrorMessage(cause)}`
155
+ );
156
+ this.name = "AI_JSONParseError";
157
+ this.cause = cause;
158
+ this.text = text;
159
+ }
160
+ static isJSONParseError(error) {
161
+ return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
162
+ }
163
+ toJSON() {
164
+ return {
165
+ name: this.name,
166
+ message: this.message,
167
+ cause: this.cause,
168
+ stack: this.stack,
169
+ valueText: this.text
170
+ };
171
+ }
172
+ };
173
+
174
+ // ai-model-specification/errors/type-validation-error.ts
175
+ var TypeValidationError = class extends Error {
176
+ constructor({ value, cause }) {
177
+ super(
178
+ `Type validation failed: Value: ${JSON.stringify(value)}.
179
+ Error message: ${getErrorMessage(cause)}`
180
+ );
181
+ this.name = "AI_TypeValidationError";
182
+ this.cause = cause;
183
+ this.value = value;
184
+ }
185
+ static isTypeValidationError(error) {
186
+ return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
187
+ }
188
+ toJSON() {
189
+ return {
190
+ name: this.name,
191
+ message: this.message,
192
+ cause: this.cause,
193
+ stack: this.stack,
194
+ value: this.value
195
+ };
196
+ }
197
+ };
198
+
203
199
  // ai-model-specification/util/validate-types.ts
204
200
  function validateTypes({
205
201
  value,
@@ -233,7 +229,7 @@ function safeValidateTypes({
233
229
  } catch (error) {
234
230
  return {
235
231
  success: false,
236
- error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
232
+ error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
237
233
  };
238
234
  }
239
235
  }
@@ -250,7 +246,7 @@ function parseJSON({
250
246
  }
251
247
  return validateTypes({ value, schema });
252
248
  } catch (error) {
253
- if (error instanceof JSONParseError || error instanceof TypeValidationError) {
249
+ if (JSONParseError.isJSONParseError(error) || TypeValidationError.isTypeValidationError(error)) {
254
250
  throw error;
255
251
  }
256
252
  throw new JSONParseError({ text, cause: error });
@@ -272,7 +268,7 @@ function safeParseJSON({
272
268
  } catch (error) {
273
269
  return {
274
270
  success: false,
275
- error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
271
+ error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
276
272
  };
277
273
  }
278
274
  }
@@ -296,8 +292,8 @@ var postJsonToApi = async ({
296
292
  }) => postToApi({
297
293
  url,
298
294
  headers: {
299
- "Content-Type": "application/json",
300
- ...headers
295
+ ...headers,
296
+ "Content-Type": "application/json"
301
297
  },
302
298
  body: {
303
299
  content: JSON.stringify(body),
@@ -316,9 +312,12 @@ var postToApi = async ({
316
312
  abortSignal
317
313
  }) => {
318
314
  try {
315
+ const definedHeaders = Object.fromEntries(
316
+ Object.entries(headers).filter(([_key, value]) => value != null)
317
+ );
319
318
  const response = await fetch(url, {
320
319
  method: "POST",
321
- headers,
320
+ headers: definedHeaders,
322
321
  body: body.content,
323
322
  signal: abortSignal
324
323
  });
@@ -331,11 +330,11 @@ var postToApi = async ({
331
330
  });
332
331
  } catch (error) {
333
332
  if (error instanceof Error) {
334
- if (error.name === "AbortError" || error instanceof ApiCallError) {
333
+ if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
335
334
  throw error;
336
335
  }
337
336
  }
338
- throw new ApiCallError({
337
+ throw new APICallError({
339
338
  message: "Failed to process error response",
340
339
  cause: error,
341
340
  statusCode: response.status,
@@ -352,11 +351,11 @@ var postToApi = async ({
352
351
  });
353
352
  } catch (error) {
354
353
  if (error instanceof Error) {
355
- if (error.name === "AbortError" || error instanceof ApiCallError) {
354
+ if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
356
355
  throw error;
357
356
  }
358
357
  }
359
- throw new ApiCallError({
358
+ throw new APICallError({
360
359
  message: "Failed to process successful response",
361
360
  cause: error,
362
361
  statusCode: response.status,
@@ -373,12 +372,13 @@ var postToApi = async ({
373
372
  if (error instanceof TypeError && error.message === "fetch failed") {
374
373
  const cause = error.cause;
375
374
  if (cause != null) {
376
- throw new ApiCallError({
375
+ throw new APICallError({
377
376
  message: `Cannot connect to API: ${cause.message}`,
378
377
  cause,
379
378
  url,
380
379
  requestBodyValues: body.values,
381
380
  isRetryable: true
381
+ // retry when network error
382
382
  });
383
383
  }
384
384
  }
@@ -397,7 +397,7 @@ var createJsonErrorResponseHandler = ({
397
397
  }) => async ({ response, url, requestBodyValues }) => {
398
398
  const responseBody = await response.text();
399
399
  if (responseBody.trim() === "") {
400
- return new ApiCallError({
400
+ return new APICallError({
401
401
  message: response.statusText,
402
402
  url,
403
403
  requestBodyValues,
@@ -411,7 +411,7 @@ var createJsonErrorResponseHandler = ({
411
411
  text: responseBody,
412
412
  schema: errorSchema
413
413
  });
414
- return new ApiCallError({
414
+ return new APICallError({
415
415
  message: errorToMessage(parsedError),
416
416
  url,
417
417
  requestBodyValues,
@@ -421,7 +421,7 @@ var createJsonErrorResponseHandler = ({
421
421
  isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
422
422
  });
423
423
  } catch (parseError) {
424
- return new ApiCallError({
424
+ return new APICallError({
425
425
  message: response.statusText,
426
426
  url,
427
427
  requestBodyValues,
@@ -459,7 +459,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
459
459
  schema: responseSchema
460
460
  });
461
461
  if (!parsedResult.success) {
462
- throw new ApiCallError({
462
+ throw new APICallError({
463
463
  message: "Invalid JSON response",
464
464
  cause: parsedResult.error,
465
465
  statusCode: response.status,
@@ -500,12 +500,141 @@ function convertUint8ArrayToBase64(array) {
500
500
  }
501
501
  return globalThis.btoa(latin1string);
502
502
  }
503
+
504
+ // ai-model-specification/errors/invalid-tool-arguments-error.ts
505
+ var InvalidToolArgumentsError = class extends Error {
506
+ constructor({
507
+ toolArgs,
508
+ toolName,
509
+ cause,
510
+ message = `Invalid arguments for tool ${toolName}: ${getErrorMessage(
511
+ cause
512
+ )}`
513
+ }) {
514
+ super(message);
515
+ this.name = "AI_InvalidToolArgumentsError";
516
+ this.toolArgs = toolArgs;
517
+ this.toolName = toolName;
518
+ this.cause = cause;
519
+ }
520
+ static isInvalidToolArgumentsError(error) {
521
+ return error instanceof Error && error.name === "AI_InvalidToolArgumentsError" && typeof error.toolName === "string" && typeof error.toolArgs === "string";
522
+ }
523
+ toJSON() {
524
+ return {
525
+ name: this.name,
526
+ message: this.message,
527
+ cause: this.cause,
528
+ stack: this.stack,
529
+ toolName: this.toolName,
530
+ toolArgs: this.toolArgs
531
+ };
532
+ }
533
+ };
534
+
535
+ // ai-model-specification/errors/no-object-generated-error.ts
536
+ var NoTextGeneratedError = class extends Error {
537
+ constructor() {
538
+ super(`No text generated.`);
539
+ this.name = "AI_NoTextGeneratedError";
540
+ }
541
+ static isNoTextGeneratedError(error) {
542
+ return error instanceof Error && error.name === "AI_NoTextGeneratedError";
543
+ }
544
+ toJSON() {
545
+ return {
546
+ name: this.name,
547
+ cause: this.cause,
548
+ message: this.message,
549
+ stack: this.stack
550
+ };
551
+ }
552
+ };
553
+
554
+ // ai-model-specification/errors/no-such-tool-error.ts
555
+ var NoSuchToolError = class extends Error {
556
+ constructor({ message, toolName }) {
557
+ super(message);
558
+ this.name = "AI_NoSuchToolError";
559
+ this.toolName = toolName;
560
+ }
561
+ static isNoSuchToolError(error) {
562
+ return error instanceof Error && error.name === "AI_NoSuchToolError" && typeof error.toolName === "string";
563
+ }
564
+ toJSON() {
565
+ return {
566
+ name: this.name,
567
+ message: this.message,
568
+ stack: this.stack,
569
+ toolName: this.toolName
570
+ };
571
+ }
572
+ };
573
+
574
+ // ai-model-specification/errors/retry-error.ts
575
+ var RetryError = class extends Error {
576
+ constructor({
577
+ message,
578
+ reason,
579
+ errors
580
+ }) {
581
+ super(message);
582
+ this.name = "AI_RetryError";
583
+ this.reason = reason;
584
+ this.errors = errors;
585
+ this.lastError = errors[errors.length - 1];
586
+ }
587
+ static isRetryError(error) {
588
+ return error instanceof Error && error.name === "AI_RetryError" && typeof error.reason === "string" && Array.isArray(error.errors);
589
+ }
590
+ toJSON() {
591
+ return {
592
+ name: this.name,
593
+ message: this.message,
594
+ reason: this.reason,
595
+ lastError: this.lastError,
596
+ errors: this.errors
597
+ };
598
+ }
599
+ };
600
+
601
+ // ai-model-specification/errors/unsupported-functionality-error.ts
602
+ var UnsupportedFunctionalityError = class extends Error {
603
+ constructor({
604
+ provider,
605
+ functionality
606
+ }) {
607
+ super(
608
+ `Functionality not supported by the provider. Provider: ${provider}.
609
+ Functionality: ${functionality}`
610
+ );
611
+ this.name = "AI_UnsupportedFunctionalityError";
612
+ this.provider = provider;
613
+ this.functionality = functionality;
614
+ }
615
+ static isUnsupportedFunctionalityError(error) {
616
+ return error instanceof Error && error.name === "AI_UnsupportedFunctionalityError" && typeof error.provider === "string" && typeof error.functionality === "string";
617
+ }
618
+ toJSON() {
619
+ return {
620
+ name: this.name,
621
+ message: this.message,
622
+ stack: this.stack,
623
+ provider: this.provider,
624
+ functionality: this.functionality
625
+ };
626
+ }
627
+ };
503
628
  export {
504
- AI_InvalidArgumentError,
505
- ApiCallError,
629
+ APICallError,
630
+ InvalidArgumentError,
631
+ InvalidPromptError,
632
+ InvalidToolArgumentsError,
506
633
  JSONParseError,
507
634
  LoadAPIKeyError,
635
+ NoSuchToolError,
508
636
  NoTextGeneratedError,
637
+ RetryError,
509
638
  TypeValidationError,
510
639
  UnsupportedFunctionalityError,
511
640
  convertBase64ToUint8Array,
@@ -513,6 +642,7 @@ export {
513
642
  createEventSourceResponseHandler,
514
643
  createJsonErrorResponseHandler,
515
644
  createJsonResponseHandler,
645
+ getErrorMessage,
516
646
  isParseableJson,
517
647
  loadApiKey,
518
648
  parseJSON,