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.
@@ -30,11 +30,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
30
30
  // ai-model-specification/index.ts
31
31
  var ai_model_specification_exports = {};
32
32
  __export(ai_model_specification_exports, {
33
- AI_InvalidArgumentError: () => AI_InvalidArgumentError,
34
- ApiCallError: () => ApiCallError,
33
+ APICallError: () => APICallError,
34
+ InvalidArgumentError: () => InvalidArgumentError,
35
+ InvalidPromptError: () => InvalidPromptError,
36
+ InvalidToolArgumentsError: () => InvalidToolArgumentsError,
35
37
  JSONParseError: () => JSONParseError,
36
38
  LoadAPIKeyError: () => LoadAPIKeyError,
39
+ NoSuchToolError: () => NoSuchToolError,
37
40
  NoTextGeneratedError: () => NoTextGeneratedError,
41
+ RetryError: () => RetryError,
38
42
  TypeValidationError: () => TypeValidationError,
39
43
  UnsupportedFunctionalityError: () => UnsupportedFunctionalityError,
40
44
  convertBase64ToUint8Array: () => convertBase64ToUint8Array,
@@ -42,6 +46,7 @@ __export(ai_model_specification_exports, {
42
46
  createEventSourceResponseHandler: () => createEventSourceResponseHandler,
43
47
  createJsonErrorResponseHandler: () => createJsonErrorResponseHandler,
44
48
  createJsonResponseHandler: () => createJsonResponseHandler,
49
+ getErrorMessage: () => getErrorMessage,
45
50
  isParseableJson: () => isParseableJson,
46
51
  loadApiKey: () => loadApiKey,
47
52
  parseJSON: () => parseJSON,
@@ -54,33 +59,8 @@ __export(ai_model_specification_exports, {
54
59
  });
55
60
  module.exports = __toCommonJS(ai_model_specification_exports);
56
61
 
57
- // ai-model-specification/errors/ai-invalid-argument-error.ts
58
- var AI_InvalidArgumentError = class extends Error {
59
- // readonly learnMore =
60
- // 'https://sdk.vercel.com/docs/ai/errors/ai_invalid_argument_error';
61
- constructor({
62
- parameter,
63
- value,
64
- message
65
- }) {
66
- super(`Invalid argument for parameter ${parameter}: ${message}`);
67
- this.name = "AI_InvalidArgumentError";
68
- this.parameter = parameter;
69
- this.value = value;
70
- }
71
- toJSON() {
72
- return {
73
- name: this.name,
74
- message: this.message,
75
- stack: this.stack,
76
- parameter: this.parameter,
77
- value: this.value
78
- };
79
- }
80
- };
81
-
82
62
  // ai-model-specification/errors/api-call-error.ts
83
- var ApiCallError = class extends Error {
63
+ var APICallError = class extends Error {
84
64
  constructor({
85
65
  message,
86
66
  url,
@@ -88,11 +68,15 @@ var ApiCallError = class extends Error {
88
68
  statusCode,
89
69
  responseBody,
90
70
  cause,
91
- isRetryable = statusCode != null && (statusCode === 429 || statusCode >= 500),
71
+ isRetryable = statusCode != null && (statusCode === 408 || // request timeout
72
+ statusCode === 409 || // conflict
73
+ statusCode === 429 || // too many requests
74
+ statusCode >= 500),
75
+ // server error
92
76
  data
93
77
  }) {
94
78
  super(message);
95
- this.name = "ApiCallError";
79
+ this.name = "AI_APICallError";
96
80
  this.url = url;
97
81
  this.requestBodyValues = requestBodyValues;
98
82
  this.statusCode = statusCode;
@@ -101,6 +85,9 @@ var ApiCallError = class extends Error {
101
85
  this.isRetryable = isRetryable;
102
86
  this.data = data;
103
87
  }
88
+ static isAPICallError(error) {
89
+ 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");
90
+ }
104
91
  toJSON() {
105
92
  return {
106
93
  name: this.name,
@@ -116,115 +103,79 @@ var ApiCallError = class extends Error {
116
103
  }
117
104
  };
118
105
 
119
- // ai-model-specification/util/get-error-message.ts
120
- function getErrorMessage(error) {
121
- if (error == null) {
122
- return "unknown error";
123
- }
124
- if (typeof error === "string") {
125
- return error;
126
- }
127
- if (error instanceof Error) {
128
- return error.message;
106
+ // ai-model-specification/errors/invalid-argument-error.ts
107
+ var InvalidArgumentError = class extends Error {
108
+ constructor({
109
+ parameter,
110
+ value,
111
+ message
112
+ }) {
113
+ super(`Invalid argument for parameter ${parameter}: ${message}`);
114
+ this.name = "AI_InvalidArgumentError";
115
+ this.parameter = parameter;
116
+ this.value = value;
129
117
  }
130
- return JSON.stringify(error);
131
- }
132
-
133
- // ai-model-specification/errors/json-parse-error.ts
134
- var JSONParseError = class extends Error {
135
- constructor({ text, cause }) {
136
- super(
137
- `JSON parsing failed: Text: ${text}.
138
- Error message: ${getErrorMessage(cause)}`
139
- );
140
- this.name = "JSONParseError";
141
- this.cause = cause;
142
- this.text = text;
118
+ static isInvalidArgumentError(error) {
119
+ return error instanceof Error && error.name === "AI_InvalidArgumentError" && typeof error.parameter === "string" && typeof error.value === "string";
143
120
  }
144
121
  toJSON() {
145
122
  return {
146
123
  name: this.name,
147
124
  message: this.message,
148
- cause: this.cause,
149
125
  stack: this.stack,
150
- valueText: this.text
126
+ parameter: this.parameter,
127
+ value: this.value
151
128
  };
152
129
  }
153
130
  };
154
131
 
155
- // ai-model-specification/errors/load-api-key-error.ts
156
- var LoadAPIKeyError = class extends Error {
157
- constructor({ message }) {
158
- super(message);
159
- this.name = "LoadAPIKeyError";
160
- }
161
- toJSON() {
162
- return {
163
- name: this.name,
164
- message: this.message
165
- };
132
+ // ai-model-specification/errors/invalid-prompt-error.ts
133
+ var InvalidPromptError = class extends Error {
134
+ constructor({ prompt: prompt2, message }) {
135
+ super(`Invalid prompt: ${message}`);
136
+ this.name = "AI_InvalidPromptError";
137
+ this.prompt = prompt2;
166
138
  }
167
- };
168
-
169
- // ai-model-specification/errors/no-object-generated-error.ts
170
- var NoTextGeneratedError = class extends Error {
171
- constructor() {
172
- super(`No text generated.`);
173
- this.name = "NoTextGeneratedError";
139
+ static isInvalidPromptError(error) {
140
+ return error instanceof Error && error.name === "AI_InvalidPromptError" && prompt != null;
174
141
  }
175
142
  toJSON() {
176
143
  return {
177
144
  name: this.name,
178
- cause: this.cause,
179
145
  message: this.message,
180
- stack: this.stack
146
+ stack: this.stack,
147
+ prompt: this.prompt
181
148
  };
182
149
  }
183
150
  };
184
151
 
185
- // ai-model-specification/errors/type-validation-error.ts
186
- var TypeValidationError = class extends Error {
187
- constructor({ value, cause }) {
188
- super(
189
- `Type validation failed: Value: ${JSON.stringify(value)}.
190
- Error message: ${getErrorMessage(cause)}`
191
- );
192
- this.name = "TypeValidationError";
193
- this.cause = cause;
194
- this.value = value;
152
+ // ai-model-specification/util/get-error-message.ts
153
+ function getErrorMessage(error) {
154
+ if (error == null) {
155
+ return "unknown error";
195
156
  }
196
- toJSON() {
197
- return {
198
- name: this.name,
199
- message: this.message,
200
- cause: this.cause,
201
- stack: this.stack,
202
- value: this.value
203
- };
157
+ if (typeof error === "string") {
158
+ return error;
204
159
  }
205
- };
160
+ if (error instanceof Error) {
161
+ return error.message;
162
+ }
163
+ return JSON.stringify(error);
164
+ }
206
165
 
207
- // ai-model-specification/errors/unsupported-functionality-error.ts
208
- var UnsupportedFunctionalityError = class extends Error {
209
- constructor({
210
- provider,
211
- functionality
212
- }) {
213
- super(
214
- `Functionality not supported by the provider. Provider: ${provider}.
215
- Functionality: ${functionality}`
216
- );
217
- this.name = "UnsupportedFunctionalityError";
218
- this.provider = provider;
219
- this.functionality = functionality;
166
+ // ai-model-specification/errors/load-api-key-error.ts
167
+ var LoadAPIKeyError = class extends Error {
168
+ constructor({ message }) {
169
+ super(message);
170
+ this.name = "AI_LoadAPIKeyError";
171
+ }
172
+ static isLoadAPIKeyError(error) {
173
+ return error instanceof Error && error.name === "AI_LoadAPIKeyError";
220
174
  }
221
175
  toJSON() {
222
176
  return {
223
177
  name: this.name,
224
- message: this.message,
225
- stack: this.stack,
226
- provider: this.provider,
227
- functionality: this.functionality
178
+ message: this.message
228
179
  };
229
180
  }
230
181
  };
@@ -256,6 +207,56 @@ function loadApiKey({
256
207
  // ai-model-specification/util/parse-json.ts
257
208
  var import_secure_json_parse = __toESM(require("secure-json-parse"));
258
209
 
210
+ // ai-model-specification/errors/json-parse-error.ts
211
+ var JSONParseError = class extends Error {
212
+ constructor({ text, cause }) {
213
+ super(
214
+ `JSON parsing failed: Text: ${text}.
215
+ Error message: ${getErrorMessage(cause)}`
216
+ );
217
+ this.name = "AI_JSONParseError";
218
+ this.cause = cause;
219
+ this.text = text;
220
+ }
221
+ static isJSONParseError(error) {
222
+ return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
223
+ }
224
+ toJSON() {
225
+ return {
226
+ name: this.name,
227
+ message: this.message,
228
+ cause: this.cause,
229
+ stack: this.stack,
230
+ valueText: this.text
231
+ };
232
+ }
233
+ };
234
+
235
+ // ai-model-specification/errors/type-validation-error.ts
236
+ var TypeValidationError = class extends Error {
237
+ constructor({ value, cause }) {
238
+ super(
239
+ `Type validation failed: Value: ${JSON.stringify(value)}.
240
+ Error message: ${getErrorMessage(cause)}`
241
+ );
242
+ this.name = "AI_TypeValidationError";
243
+ this.cause = cause;
244
+ this.value = value;
245
+ }
246
+ static isTypeValidationError(error) {
247
+ return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
248
+ }
249
+ toJSON() {
250
+ return {
251
+ name: this.name,
252
+ message: this.message,
253
+ cause: this.cause,
254
+ stack: this.stack,
255
+ value: this.value
256
+ };
257
+ }
258
+ };
259
+
259
260
  // ai-model-specification/util/validate-types.ts
260
261
  function validateTypes({
261
262
  value,
@@ -289,7 +290,7 @@ function safeValidateTypes({
289
290
  } catch (error) {
290
291
  return {
291
292
  success: false,
292
- error: error instanceof TypeValidationError ? error : new TypeValidationError({ value, cause: error })
293
+ error: TypeValidationError.isTypeValidationError(error) ? error : new TypeValidationError({ value, cause: error })
293
294
  };
294
295
  }
295
296
  }
@@ -306,7 +307,7 @@ function parseJSON({
306
307
  }
307
308
  return validateTypes({ value, schema });
308
309
  } catch (error) {
309
- if (error instanceof JSONParseError || error instanceof TypeValidationError) {
310
+ if (JSONParseError.isJSONParseError(error) || TypeValidationError.isTypeValidationError(error)) {
310
311
  throw error;
311
312
  }
312
313
  throw new JSONParseError({ text, cause: error });
@@ -328,7 +329,7 @@ function safeParseJSON({
328
329
  } catch (error) {
329
330
  return {
330
331
  success: false,
331
- error: error instanceof JSONParseError ? error : new JSONParseError({ text, cause: error })
332
+ error: JSONParseError.isJSONParseError(error) ? error : new JSONParseError({ text, cause: error })
332
333
  };
333
334
  }
334
335
  }
@@ -352,8 +353,8 @@ var postJsonToApi = async ({
352
353
  }) => postToApi({
353
354
  url,
354
355
  headers: {
355
- "Content-Type": "application/json",
356
- ...headers
356
+ ...headers,
357
+ "Content-Type": "application/json"
357
358
  },
358
359
  body: {
359
360
  content: JSON.stringify(body),
@@ -372,9 +373,12 @@ var postToApi = async ({
372
373
  abortSignal
373
374
  }) => {
374
375
  try {
376
+ const definedHeaders = Object.fromEntries(
377
+ Object.entries(headers).filter(([_key, value]) => value != null)
378
+ );
375
379
  const response = await fetch(url, {
376
380
  method: "POST",
377
- headers,
381
+ headers: definedHeaders,
378
382
  body: body.content,
379
383
  signal: abortSignal
380
384
  });
@@ -387,11 +391,11 @@ var postToApi = async ({
387
391
  });
388
392
  } catch (error) {
389
393
  if (error instanceof Error) {
390
- if (error.name === "AbortError" || error instanceof ApiCallError) {
394
+ if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
391
395
  throw error;
392
396
  }
393
397
  }
394
- throw new ApiCallError({
398
+ throw new APICallError({
395
399
  message: "Failed to process error response",
396
400
  cause: error,
397
401
  statusCode: response.status,
@@ -408,11 +412,11 @@ var postToApi = async ({
408
412
  });
409
413
  } catch (error) {
410
414
  if (error instanceof Error) {
411
- if (error.name === "AbortError" || error instanceof ApiCallError) {
415
+ if (error.name === "AbortError" || APICallError.isAPICallError(error)) {
412
416
  throw error;
413
417
  }
414
418
  }
415
- throw new ApiCallError({
419
+ throw new APICallError({
416
420
  message: "Failed to process successful response",
417
421
  cause: error,
418
422
  statusCode: response.status,
@@ -429,12 +433,13 @@ var postToApi = async ({
429
433
  if (error instanceof TypeError && error.message === "fetch failed") {
430
434
  const cause = error.cause;
431
435
  if (cause != null) {
432
- throw new ApiCallError({
436
+ throw new APICallError({
433
437
  message: `Cannot connect to API: ${cause.message}`,
434
438
  cause,
435
439
  url,
436
440
  requestBodyValues: body.values,
437
441
  isRetryable: true
442
+ // retry when network error
438
443
  });
439
444
  }
440
445
  }
@@ -451,7 +456,7 @@ var createJsonErrorResponseHandler = ({
451
456
  }) => async ({ response, url, requestBodyValues }) => {
452
457
  const responseBody = await response.text();
453
458
  if (responseBody.trim() === "") {
454
- return new ApiCallError({
459
+ return new APICallError({
455
460
  message: response.statusText,
456
461
  url,
457
462
  requestBodyValues,
@@ -465,7 +470,7 @@ var createJsonErrorResponseHandler = ({
465
470
  text: responseBody,
466
471
  schema: errorSchema
467
472
  });
468
- return new ApiCallError({
473
+ return new APICallError({
469
474
  message: errorToMessage(parsedError),
470
475
  url,
471
476
  requestBodyValues,
@@ -475,7 +480,7 @@ var createJsonErrorResponseHandler = ({
475
480
  isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
476
481
  });
477
482
  } catch (parseError) {
478
- return new ApiCallError({
483
+ return new APICallError({
479
484
  message: response.statusText,
480
485
  url,
481
486
  requestBodyValues,
@@ -513,7 +518,7 @@ var createJsonResponseHandler = (responseSchema) => async ({ response, url, requ
513
518
  schema: responseSchema
514
519
  });
515
520
  if (!parsedResult.success) {
516
- throw new ApiCallError({
521
+ throw new APICallError({
517
522
  message: "Invalid JSON response",
518
523
  cause: parsedResult.error,
519
524
  statusCode: response.status,
@@ -554,13 +559,142 @@ function convertUint8ArrayToBase64(array) {
554
559
  }
555
560
  return globalThis.btoa(latin1string);
556
561
  }
562
+
563
+ // ai-model-specification/errors/invalid-tool-arguments-error.ts
564
+ var InvalidToolArgumentsError = class extends Error {
565
+ constructor({
566
+ toolArgs,
567
+ toolName,
568
+ cause,
569
+ message = `Invalid arguments for tool ${toolName}: ${getErrorMessage(
570
+ cause
571
+ )}`
572
+ }) {
573
+ super(message);
574
+ this.name = "AI_InvalidToolArgumentsError";
575
+ this.toolArgs = toolArgs;
576
+ this.toolName = toolName;
577
+ this.cause = cause;
578
+ }
579
+ static isInvalidToolArgumentsError(error) {
580
+ return error instanceof Error && error.name === "AI_InvalidToolArgumentsError" && typeof error.toolName === "string" && typeof error.toolArgs === "string";
581
+ }
582
+ toJSON() {
583
+ return {
584
+ name: this.name,
585
+ message: this.message,
586
+ cause: this.cause,
587
+ stack: this.stack,
588
+ toolName: this.toolName,
589
+ toolArgs: this.toolArgs
590
+ };
591
+ }
592
+ };
593
+
594
+ // ai-model-specification/errors/no-object-generated-error.ts
595
+ var NoTextGeneratedError = class extends Error {
596
+ constructor() {
597
+ super(`No text generated.`);
598
+ this.name = "AI_NoTextGeneratedError";
599
+ }
600
+ static isNoTextGeneratedError(error) {
601
+ return error instanceof Error && error.name === "AI_NoTextGeneratedError";
602
+ }
603
+ toJSON() {
604
+ return {
605
+ name: this.name,
606
+ cause: this.cause,
607
+ message: this.message,
608
+ stack: this.stack
609
+ };
610
+ }
611
+ };
612
+
613
+ // ai-model-specification/errors/no-such-tool-error.ts
614
+ var NoSuchToolError = class extends Error {
615
+ constructor({ message, toolName }) {
616
+ super(message);
617
+ this.name = "AI_NoSuchToolError";
618
+ this.toolName = toolName;
619
+ }
620
+ static isNoSuchToolError(error) {
621
+ return error instanceof Error && error.name === "AI_NoSuchToolError" && typeof error.toolName === "string";
622
+ }
623
+ toJSON() {
624
+ return {
625
+ name: this.name,
626
+ message: this.message,
627
+ stack: this.stack,
628
+ toolName: this.toolName
629
+ };
630
+ }
631
+ };
632
+
633
+ // ai-model-specification/errors/retry-error.ts
634
+ var RetryError = class extends Error {
635
+ constructor({
636
+ message,
637
+ reason,
638
+ errors
639
+ }) {
640
+ super(message);
641
+ this.name = "AI_RetryError";
642
+ this.reason = reason;
643
+ this.errors = errors;
644
+ this.lastError = errors[errors.length - 1];
645
+ }
646
+ static isRetryError(error) {
647
+ return error instanceof Error && error.name === "AI_RetryError" && typeof error.reason === "string" && Array.isArray(error.errors);
648
+ }
649
+ toJSON() {
650
+ return {
651
+ name: this.name,
652
+ message: this.message,
653
+ reason: this.reason,
654
+ lastError: this.lastError,
655
+ errors: this.errors
656
+ };
657
+ }
658
+ };
659
+
660
+ // ai-model-specification/errors/unsupported-functionality-error.ts
661
+ var UnsupportedFunctionalityError = class extends Error {
662
+ constructor({
663
+ provider,
664
+ functionality
665
+ }) {
666
+ super(
667
+ `Functionality not supported by the provider. Provider: ${provider}.
668
+ Functionality: ${functionality}`
669
+ );
670
+ this.name = "AI_UnsupportedFunctionalityError";
671
+ this.provider = provider;
672
+ this.functionality = functionality;
673
+ }
674
+ static isUnsupportedFunctionalityError(error) {
675
+ return error instanceof Error && error.name === "AI_UnsupportedFunctionalityError" && typeof error.provider === "string" && typeof error.functionality === "string";
676
+ }
677
+ toJSON() {
678
+ return {
679
+ name: this.name,
680
+ message: this.message,
681
+ stack: this.stack,
682
+ provider: this.provider,
683
+ functionality: this.functionality
684
+ };
685
+ }
686
+ };
557
687
  // Annotate the CommonJS export names for ESM import in node:
558
688
  0 && (module.exports = {
559
- AI_InvalidArgumentError,
560
- ApiCallError,
689
+ APICallError,
690
+ InvalidArgumentError,
691
+ InvalidPromptError,
692
+ InvalidToolArgumentsError,
561
693
  JSONParseError,
562
694
  LoadAPIKeyError,
695
+ NoSuchToolError,
563
696
  NoTextGeneratedError,
697
+ RetryError,
564
698
  TypeValidationError,
565
699
  UnsupportedFunctionalityError,
566
700
  convertBase64ToUint8Array,
@@ -568,6 +702,7 @@ function convertUint8ArrayToBase64(array) {
568
702
  createEventSourceResponseHandler,
569
703
  createJsonErrorResponseHandler,
570
704
  createJsonResponseHandler,
705
+ getErrorMessage,
571
706
  isParseableJson,
572
707
  loadApiKey,
573
708
  parseJSON,