ai 3.0.19 → 3.0.21

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.
Files changed (67) hide show
  1. package/anthropic/dist/index.d.mts +1 -319
  2. package/anthropic/dist/index.d.ts +1 -319
  3. package/anthropic/dist/index.js +44 -216
  4. package/anthropic/dist/index.js.map +1 -1
  5. package/anthropic/dist/index.mjs +32 -200
  6. package/anthropic/dist/index.mjs.map +1 -1
  7. package/dist/index.d.mts +60 -333
  8. package/dist/index.d.ts +60 -333
  9. package/dist/index.js +152 -441
  10. package/dist/index.js.map +1 -1
  11. package/dist/index.mjs +127 -412
  12. package/dist/index.mjs.map +1 -1
  13. package/google/dist/index.d.mts +1 -319
  14. package/google/dist/index.d.ts +1 -319
  15. package/google/dist/index.js +75 -229
  16. package/google/dist/index.js.map +1 -1
  17. package/google/dist/index.mjs +63 -213
  18. package/google/dist/index.mjs.map +1 -1
  19. package/mistral/dist/index.d.mts +1 -319
  20. package/mistral/dist/index.d.ts +1 -319
  21. package/mistral/dist/index.js +32 -205
  22. package/mistral/dist/index.js.map +1 -1
  23. package/mistral/dist/index.mjs +20 -189
  24. package/mistral/dist/index.mjs.map +1 -1
  25. package/openai/dist/index.d.mts +2 -320
  26. package/openai/dist/index.d.ts +2 -320
  27. package/openai/dist/index.js +69 -280
  28. package/openai/dist/index.js.map +1 -1
  29. package/openai/dist/index.mjs +57 -260
  30. package/openai/dist/index.mjs.map +1 -1
  31. package/package.json +4 -10
  32. package/react/dist/index.d.mts +8 -4
  33. package/react/dist/index.d.ts +12 -6
  34. package/react/dist/index.js +23 -105
  35. package/react/dist/index.js.map +1 -1
  36. package/react/dist/index.mjs +22 -105
  37. package/react/dist/index.mjs.map +1 -1
  38. package/react/dist/index.server.d.mts +4 -2
  39. package/react/dist/index.server.d.ts +4 -2
  40. package/react/dist/index.server.js.map +1 -1
  41. package/react/dist/index.server.mjs.map +1 -1
  42. package/rsc/dist/rsc-server.mjs +6 -16
  43. package/rsc/dist/rsc-server.mjs.map +1 -1
  44. package/solid/dist/index.d.mts +1 -1
  45. package/solid/dist/index.d.ts +1 -1
  46. package/solid/dist/index.js +19 -104
  47. package/solid/dist/index.js.map +1 -1
  48. package/solid/dist/index.mjs +19 -104
  49. package/solid/dist/index.mjs.map +1 -1
  50. package/svelte/dist/index.d.mts +1 -1
  51. package/svelte/dist/index.d.ts +1 -1
  52. package/svelte/dist/index.js +19 -104
  53. package/svelte/dist/index.js.map +1 -1
  54. package/svelte/dist/index.mjs +19 -104
  55. package/svelte/dist/index.mjs.map +1 -1
  56. package/vue/dist/index.d.mts +1 -1
  57. package/vue/dist/index.d.ts +1 -1
  58. package/vue/dist/index.js +19 -104
  59. package/vue/dist/index.js.map +1 -1
  60. package/vue/dist/index.mjs +19 -104
  61. package/vue/dist/index.mjs.map +1 -1
  62. package/spec/dist/index.d.mts +0 -752
  63. package/spec/dist/index.d.ts +0 -752
  64. package/spec/dist/index.js +0 -873
  65. package/spec/dist/index.js.map +0 -1
  66. package/spec/dist/index.mjs +0 -807
  67. package/spec/dist/index.mjs.map +0 -1
@@ -1,47 +1,3 @@
1
- // spec/errors/api-call-error.ts
2
- var APICallError = class extends Error {
3
- constructor({
4
- message,
5
- url,
6
- requestBodyValues,
7
- statusCode,
8
- responseBody,
9
- cause,
10
- isRetryable = statusCode != null && (statusCode === 408 || // request timeout
11
- statusCode === 409 || // conflict
12
- statusCode === 429 || // too many requests
13
- statusCode >= 500),
14
- // server error
15
- data
16
- }) {
17
- super(message);
18
- this.name = "AI_APICallError";
19
- this.url = url;
20
- this.requestBodyValues = requestBodyValues;
21
- this.statusCode = statusCode;
22
- this.responseBody = responseBody;
23
- this.cause = cause;
24
- this.isRetryable = isRetryable;
25
- this.data = data;
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
- }
30
- toJSON() {
31
- return {
32
- name: this.name,
33
- message: this.message,
34
- url: this.url,
35
- requestBodyValues: this.requestBodyValues,
36
- statusCode: this.statusCode,
37
- responseBody: this.responseBody,
38
- cause: this.cause,
39
- isRetryable: this.isRetryable,
40
- data: this.data
41
- };
42
- }
43
- };
44
-
45
1
  // spec/util/generate-id.ts
46
2
  import { customAlphabet } from "nanoid/non-secure";
47
3
  var generateId = customAlphabet(
@@ -49,38 +5,8 @@ var generateId = customAlphabet(
49
5
  7
50
6
  );
51
7
 
52
- // spec/util/get-error-message.ts
53
- function getErrorMessage(error) {
54
- if (error == null) {
55
- return "unknown error";
56
- }
57
- if (typeof error === "string") {
58
- return error;
59
- }
60
- if (error instanceof Error) {
61
- return error.message;
62
- }
63
- return JSON.stringify(error);
64
- }
65
-
66
- // spec/errors/load-api-key-error.ts
67
- var LoadAPIKeyError = class extends Error {
68
- constructor({ message }) {
69
- super(message);
70
- this.name = "AI_LoadAPIKeyError";
71
- }
72
- static isLoadAPIKeyError(error) {
73
- return error instanceof Error && error.name === "AI_LoadAPIKeyError";
74
- }
75
- toJSON() {
76
- return {
77
- name: this.name,
78
- message: this.message
79
- };
80
- }
81
- };
82
-
83
8
  // spec/util/load-api-key.ts
9
+ import { LoadAPIKeyError } from "@ai-sdk/provider";
84
10
  function loadApiKey({
85
11
  apiKey,
86
12
  environmentVariableName,
@@ -115,59 +41,11 @@ function loadApiKey({
115
41
  }
116
42
 
117
43
  // spec/util/parse-json.ts
44
+ import { JSONParseError, TypeValidationError as TypeValidationError2 } from "@ai-sdk/provider";
118
45
  import SecureJSON from "secure-json-parse";
119
46
 
120
- // spec/errors/json-parse-error.ts
121
- var JSONParseError = class extends Error {
122
- constructor({ text, cause }) {
123
- super(
124
- `JSON parsing failed: Text: ${text}.
125
- Error message: ${getErrorMessage(cause)}`
126
- );
127
- this.name = "AI_JSONParseError";
128
- this.cause = cause;
129
- this.text = text;
130
- }
131
- static isJSONParseError(error) {
132
- return error instanceof Error && error.name === "AI_JSONParseError" && typeof error.text === "string" && typeof error.cause === "string";
133
- }
134
- toJSON() {
135
- return {
136
- name: this.name,
137
- message: this.message,
138
- cause: this.cause,
139
- stack: this.stack,
140
- valueText: this.text
141
- };
142
- }
143
- };
144
-
145
- // spec/errors/type-validation-error.ts
146
- var TypeValidationError = class extends Error {
147
- constructor({ value, cause }) {
148
- super(
149
- `Type validation failed: Value: ${JSON.stringify(value)}.
150
- Error message: ${getErrorMessage(cause)}`
151
- );
152
- this.name = "AI_TypeValidationError";
153
- this.cause = cause;
154
- this.value = value;
155
- }
156
- static isTypeValidationError(error) {
157
- return error instanceof Error && error.name === "AI_TypeValidationError" && typeof error.value === "string" && typeof error.cause === "string";
158
- }
159
- toJSON() {
160
- return {
161
- name: this.name,
162
- message: this.message,
163
- cause: this.cause,
164
- stack: this.stack,
165
- value: this.value
166
- };
167
- }
168
- };
169
-
170
47
  // spec/util/validate-types.ts
48
+ import { TypeValidationError } from "@ai-sdk/provider";
171
49
  function validateTypes({
172
50
  value,
173
51
  schema
@@ -217,7 +95,7 @@ function parseJSON({
217
95
  }
218
96
  return validateTypes({ value, schema });
219
97
  } catch (error) {
220
- if (JSONParseError.isJSONParseError(error) || TypeValidationError.isTypeValidationError(error)) {
98
+ if (JSONParseError.isJSONParseError(error) || TypeValidationError2.isTypeValidationError(error)) {
221
99
  throw error;
222
100
  }
223
101
  throw new JSONParseError({ text, cause: error });
@@ -245,6 +123,7 @@ function safeParseJSON({
245
123
  }
246
124
 
247
125
  // spec/util/post-to-api.ts
126
+ import { APICallError } from "@ai-sdk/provider";
248
127
  var postJsonToApi = async ({
249
128
  url,
250
129
  headers,
@@ -350,29 +229,10 @@ var postToApi = async ({
350
229
  };
351
230
 
352
231
  // spec/util/response-handler.ts
232
+ import { APICallError as APICallError2, NoResponseBodyError } from "@ai-sdk/provider";
353
233
  import {
354
234
  EventSourceParserStream
355
235
  } from "eventsource-parser/stream";
356
-
357
- // spec/errors/no-response-body-error.ts
358
- var NoResponseBodyError = class extends Error {
359
- constructor({ message = "No response body" } = {}) {
360
- super(message);
361
- this.name = "AI_NoResponseBodyError";
362
- }
363
- static isNoResponseBodyError(error) {
364
- return error instanceof Error && error.name === "AI_NoResponseBodyError";
365
- }
366
- toJSON() {
367
- return {
368
- name: this.name,
369
- message: this.message,
370
- stack: this.stack
371
- };
372
- }
373
- };
374
-
375
- // spec/util/response-handler.ts
376
236
  var createJsonErrorResponseHandler = ({
377
237
  errorSchema,
378
238
  errorToMessage,
@@ -380,7 +240,7 @@ var createJsonErrorResponseHandler = ({
380
240
  }) => async ({ response, url, requestBodyValues }) => {
381
241
  const responseBody = await response.text();
382
242
  if (responseBody.trim() === "") {
383
- return new APICallError({
243
+ return new APICallError2({
384
244
  message: response.statusText,
385
245
  url,
386
246
  requestBodyValues,
@@ -394,7 +254,7 @@ var createJsonErrorResponseHandler = ({
394
254
  text: responseBody,
395
255
  schema: errorSchema
396
256
  });
397
- return new APICallError({
257
+ return new APICallError2({
398
258
  message: errorToMessage(parsedError),
399
259
  url,
400
260
  requestBodyValues,
@@ -404,7 +264,7 @@ var createJsonErrorResponseHandler = ({
404
264
  isRetryable: isRetryable == null ? void 0 : isRetryable(response, parsedError)
405
265
  });
406
266
  } catch (parseError) {
407
- return new APICallError({
267
+ return new APICallError2({
408
268
  message: response.statusText,
409
269
  url,
410
270
  requestBodyValues,
@@ -441,7 +301,7 @@ var createJsonResponseHandler = (responseSchema2) => async ({ response, url, req
441
301
  schema: responseSchema2
442
302
  });
443
303
  if (!parsedResult.success) {
444
- throw new APICallError({
304
+ throw new APICallError2({
445
305
  message: "Invalid JSON response",
446
306
  cause: parsedResult.error,
447
307
  statusCode: response.status,
@@ -462,41 +322,17 @@ function convertUint8ArrayToBase64(array) {
462
322
  return globalThis.btoa(latin1string);
463
323
  }
464
324
 
465
- // spec/errors/unsupported-functionality-error.ts
466
- var UnsupportedFunctionalityError = class extends Error {
467
- constructor({
468
- provider,
469
- functionality
470
- }) {
471
- super(
472
- `'${functionality}' functionality not supported by the '${provider}' provider.`
473
- );
474
- this.name = "AI_UnsupportedFunctionalityError";
475
- this.provider = provider;
476
- this.functionality = functionality;
477
- }
478
- static isUnsupportedFunctionalityError(error) {
479
- return error instanceof Error && error.name === "AI_UnsupportedFunctionalityError" && typeof error.provider === "string" && typeof error.functionality === "string";
480
- }
481
- toJSON() {
482
- return {
483
- name: this.name,
484
- message: this.message,
485
- stack: this.stack,
486
- provider: this.provider,
487
- functionality: this.functionality
488
- };
489
- }
490
- };
491
-
492
325
  // google/google-generative-ai-language-model.ts
326
+ import {
327
+ UnsupportedFunctionalityError as UnsupportedFunctionalityError2
328
+ } from "@ai-sdk/provider";
493
329
  import { z as z2 } from "zod";
494
330
 
495
331
  // google/convert-to-google-generative-ai-messages.ts
496
- function convertToGoogleGenerativeAIMessages({
497
- prompt,
498
- provider
499
- }) {
332
+ import {
333
+ UnsupportedFunctionalityError
334
+ } from "@ai-sdk/provider";
335
+ function convertToGoogleGenerativeAIMessages(prompt) {
500
336
  const messages = [];
501
337
  for (const { role, content } of prompt) {
502
338
  switch (role) {
@@ -517,7 +353,6 @@ function convertToGoogleGenerativeAIMessages({
517
353
  case "image": {
518
354
  if (part.image instanceof URL) {
519
355
  throw new UnsupportedFunctionalityError({
520
- provider,
521
356
  functionality: "URL image parts"
522
357
  });
523
358
  } else {
@@ -593,10 +428,13 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
593
428
  });
594
429
 
595
430
  // google/map-google-generative-ai-finish-reason.ts
596
- function mapGoogleGenerativeAIFinishReason(finishReason) {
431
+ function mapGoogleGenerativeAIFinishReason({
432
+ finishReason,
433
+ hasToolCalls
434
+ }) {
597
435
  switch (finishReason) {
598
436
  case "STOP":
599
- return "stop";
437
+ return hasToolCalls ? "tool-calls" : "stop";
600
438
  case "MAX_TOKENS":
601
439
  return "length";
602
440
  case "RECITATION":
@@ -662,10 +500,7 @@ var GoogleGenerativeAILanguageModel = class {
662
500
  topP
663
501
  },
664
502
  // prompt:
665
- contents: convertToGoogleGenerativeAIMessages({
666
- provider: this.provider,
667
- prompt
668
- })
503
+ contents: convertToGoogleGenerativeAIMessages(prompt)
669
504
  };
670
505
  switch (type) {
671
506
  case "regular": {
@@ -686,21 +521,18 @@ var GoogleGenerativeAILanguageModel = class {
686
521
  };
687
522
  }
688
523
  case "object-json": {
689
- throw new UnsupportedFunctionalityError({
690
- functionality: "object-json mode",
691
- provider: this.provider
524
+ throw new UnsupportedFunctionalityError2({
525
+ functionality: "object-json mode"
692
526
  });
693
527
  }
694
528
  case "object-tool": {
695
- throw new UnsupportedFunctionalityError({
696
- functionality: "object-tool mode",
697
- provider: this.provider
529
+ throw new UnsupportedFunctionalityError2({
530
+ functionality: "object-tool mode"
698
531
  });
699
532
  }
700
533
  case "object-grammar": {
701
- throw new UnsupportedFunctionalityError({
702
- functionality: "object-grammar mode",
703
- provider: this.provider
534
+ throw new UnsupportedFunctionalityError2({
535
+ functionality: "object-grammar mode"
704
536
  });
705
537
  }
706
538
  default: {
@@ -722,13 +554,17 @@ var GoogleGenerativeAILanguageModel = class {
722
554
  });
723
555
  const { contents: rawPrompt, ...rawSettings } = args;
724
556
  const candidate = response.candidates[0];
557
+ const toolCalls = getToolCallsFromParts({
558
+ parts: candidate.content.parts,
559
+ generateId: this.config.generateId
560
+ });
725
561
  return {
726
562
  text: getTextFromParts(candidate.content.parts),
727
- toolCalls: getToolCallsFromParts({
728
- parts: candidate.content.parts,
729
- generateId: this.config.generateId
563
+ toolCalls,
564
+ finishReason: mapGoogleGenerativeAIFinishReason({
565
+ finishReason: candidate.finishReason,
566
+ hasToolCalls: toolCalls != null && toolCalls.length > 0
730
567
  }),
731
- finishReason: mapGoogleGenerativeAIFinishReason(candidate.finishReason),
732
568
  usage: {
733
569
  promptTokens: NaN,
734
570
  completionTokens: (_a = candidate.tokenCount) != null ? _a : NaN
@@ -754,6 +590,7 @@ var GoogleGenerativeAILanguageModel = class {
754
590
  completionTokens: Number.NaN
755
591
  };
756
592
  const generateId2 = this.config.generateId;
593
+ let hasToolCalls = false;
757
594
  return {
758
595
  stream: response.pipeThrough(
759
596
  new TransformStream({
@@ -765,9 +602,10 @@ var GoogleGenerativeAILanguageModel = class {
765
602
  const value = chunk.value;
766
603
  const candidate = value.candidates[0];
767
604
  if ((candidate == null ? void 0 : candidate.finishReason) != null) {
768
- finishReason = mapGoogleGenerativeAIFinishReason(
769
- candidate.finishReason
770
- );
605
+ finishReason = mapGoogleGenerativeAIFinishReason({
606
+ finishReason: candidate.finishReason,
607
+ hasToolCalls
608
+ });
771
609
  }
772
610
  if (candidate.tokenCount != null) {
773
611
  usage = {
@@ -775,7 +613,11 @@ var GoogleGenerativeAILanguageModel = class {
775
613
  completionTokens: candidate.tokenCount
776
614
  };
777
615
  }
778
- const deltaText = getTextFromParts(candidate.content.parts);
616
+ const content = candidate.content;
617
+ if (content == null) {
618
+ return;
619
+ }
620
+ const deltaText = getTextFromParts(content.parts);
779
621
  if (deltaText != null) {
780
622
  controller.enqueue({
781
623
  type: "text-delta",
@@ -783,7 +625,7 @@ var GoogleGenerativeAILanguageModel = class {
783
625
  });
784
626
  }
785
627
  const toolCallDeltas = getToolCallsFromParts({
786
- parts: candidate.content.parts,
628
+ parts: content.parts,
787
629
  generateId: generateId2
788
630
  });
789
631
  if (toolCallDeltas != null) {
@@ -802,6 +644,7 @@ var GoogleGenerativeAILanguageModel = class {
802
644
  toolName: toolCall.toolName,
803
645
  args: toolCall.args
804
646
  });
647
+ hasToolCalls = true;
805
648
  }
806
649
  }
807
650
  },
@@ -865,16 +708,23 @@ var contentSchema = z2.object({
865
708
  ])
866
709
  )
867
710
  });
868
- var candidateSchema = z2.object({
869
- content: contentSchema,
870
- finishReason: z2.string().optional(),
871
- tokenCount: z2.number().optional()
872
- });
873
711
  var responseSchema = z2.object({
874
- candidates: z2.array(candidateSchema)
712
+ candidates: z2.array(
713
+ z2.object({
714
+ content: contentSchema,
715
+ finishReason: z2.string().optional(),
716
+ tokenCount: z2.number().optional()
717
+ })
718
+ )
875
719
  });
876
720
  var chunkSchema = z2.object({
877
- candidates: z2.array(candidateSchema)
721
+ candidates: z2.array(
722
+ z2.object({
723
+ content: contentSchema.optional(),
724
+ finishReason: z2.string().optional(),
725
+ tokenCount: z2.number().optional()
726
+ })
727
+ )
878
728
  });
879
729
 
880
730
  // google/google-facade.ts