@ai-sdk/google 2.0.106 → 2.0.108

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
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "2.0.106" : "0.0.0-test";
10
+ var VERSION = true ? "2.0.108" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -31,15 +31,16 @@ import {
31
31
  zodSchema
32
32
  } from "@ai-sdk/provider-utils";
33
33
  import { z } from "zod/v4";
34
+ var getGoogleErrorSchema = () => z.object({
35
+ code: z.number().nullable(),
36
+ message: z.string(),
37
+ status: z.string(),
38
+ details: z.array(z.unknown()).nullish()
39
+ });
34
40
  var googleErrorDataSchema = lazySchema(
35
41
  () => zodSchema(
36
42
  z.object({
37
- error: z.object({
38
- code: z.number().nullable(),
39
- message: z.string(),
40
- status: z.string(),
41
- details: z.array(z.unknown()).nullish()
42
- })
43
+ error: getGoogleErrorSchema()
43
44
  })
44
45
  )
45
46
  );
@@ -47,6 +48,39 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
47
48
  errorSchema: googleErrorDataSchema,
48
49
  errorToMessage: (data) => data.error.message
49
50
  });
51
+ function mapGoogleErrorStatusToHttpStatusCode(status) {
52
+ switch (status) {
53
+ case "CANCELLED":
54
+ return 499;
55
+ case "INVALID_ARGUMENT":
56
+ case "FAILED_PRECONDITION":
57
+ case "OUT_OF_RANGE":
58
+ return 400;
59
+ case "DEADLINE_EXCEEDED":
60
+ return 504;
61
+ case "NOT_FOUND":
62
+ return 404;
63
+ case "ALREADY_EXISTS":
64
+ case "ABORTED":
65
+ return 409;
66
+ case "PERMISSION_DENIED":
67
+ return 403;
68
+ case "UNAUTHENTICATED":
69
+ return 401;
70
+ case "RESOURCE_EXHAUSTED":
71
+ return 429;
72
+ case "UNIMPLEMENTED":
73
+ return 501;
74
+ case "UNKNOWN":
75
+ case "INTERNAL":
76
+ case "DATA_LOSS":
77
+ return 500;
78
+ case "UNAVAILABLE":
79
+ return 503;
80
+ default:
81
+ return void 0;
82
+ }
83
+ }
50
84
 
51
85
  // src/google-generative-ai-embedding-options.ts
52
86
  import {
@@ -232,6 +266,9 @@ var googleGenerativeAISingleEmbeddingResponseSchema = lazySchema3(
232
266
  );
233
267
 
234
268
  // src/google-generative-ai-language-model.ts
269
+ import {
270
+ APICallError
271
+ } from "@ai-sdk/provider";
235
272
  import {
236
273
  combineHeaders as combineHeaders2,
237
274
  createEventSourceResponseHandler,
@@ -620,7 +657,11 @@ function isEmptyObjectSchema(jsonSchema) {
620
657
  import {
621
658
  UnsupportedFunctionalityError as UnsupportedFunctionalityError2
622
659
  } from "@ai-sdk/provider";
623
- import { convertToBase64 } from "@ai-sdk/provider-utils";
660
+ import {
661
+ stringifyWithBigIntAndCycles,
662
+ secureJsonParse,
663
+ convertToBase64
664
+ } from "@ai-sdk/provider-utils";
624
665
  var SKIP_THOUGHT_SIGNATURE_VALIDATOR = "skip_thought_signature_validator";
625
666
  function convertToGoogleGenerativeAIMessages(prompt, options) {
626
667
  var _a, _b, _c;
@@ -721,7 +762,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
721
762
  functionCall: {
722
763
  ...part.toolCallId != null ? { id: part.toolCallId } : {},
723
764
  name: part.toolName,
724
- args: part.input
765
+ args: parseToolCallInput(part.input)
725
766
  },
726
767
  thoughtSignature: effectiveThoughtSignature
727
768
  };
@@ -749,7 +790,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
749
790
  name: part.toolName,
750
791
  response: {
751
792
  name: part.toolName,
752
- content: output.value
793
+ content: serializeFunctionResponseContent(output.value)
753
794
  }
754
795
  }
755
796
  });
@@ -840,11 +881,46 @@ function appendLegacyToolResultParts({
840
881
  );
841
882
  break;
842
883
  default:
843
- parts.push({ text: JSON.stringify(contentPart) });
884
+ parts.push({ text: stringifyWithBigIntAndCycles(contentPart) });
844
885
  break;
845
886
  }
846
887
  }
847
888
  }
889
+ function serializeFunctionResponseContent(value) {
890
+ return containsNonJsonValue(value) ? secureJsonParse(stringifyWithBigIntAndCycles(value)) : value;
891
+ }
892
+ function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
893
+ if (typeof value === "bigint") {
894
+ return true;
895
+ }
896
+ if (typeof value !== "object" || value === null) {
897
+ return false;
898
+ }
899
+ if (typeof value.toJSON === "function") {
900
+ return true;
901
+ }
902
+ if (ancestors.has(value)) {
903
+ return true;
904
+ }
905
+ ancestors.add(value);
906
+ try {
907
+ return Object.values(value).some(
908
+ (item) => containsNonJsonValue(item, ancestors)
909
+ );
910
+ } finally {
911
+ ancestors.delete(value);
912
+ }
913
+ }
914
+ function parseToolCallInput(input) {
915
+ if (typeof input !== "string") {
916
+ return input;
917
+ }
918
+ try {
919
+ return secureJsonParse(input);
920
+ } catch (e) {
921
+ return input;
922
+ }
923
+ }
848
924
 
849
925
  // src/get-model-path.ts
850
926
  function getModelPath(modelId) {
@@ -1661,10 +1737,11 @@ var GoogleGenerativeAILanguageModel = class {
1661
1737
  await resolve2(this.config.headers),
1662
1738
  options.headers
1663
1739
  );
1740
+ const url = `${this.config.baseURL}/${getModelPath(
1741
+ this.modelId
1742
+ )}:streamGenerateContent?alt=sse`;
1664
1743
  const streamGenerateContent = (body2) => postJsonToApi2({
1665
- url: `${this.config.baseURL}/${getModelPath(
1666
- this.modelId
1667
- )}:streamGenerateContent?alt=sse`,
1744
+ url,
1668
1745
  headers,
1669
1746
  body: body2,
1670
1747
  failedResponseHandler: googleFailedResponseHandler,
@@ -1723,7 +1800,7 @@ var GoogleGenerativeAILanguageModel = class {
1723
1800
  controller.enqueue({ type: "stream-start", warnings });
1724
1801
  },
1725
1802
  transform(chunk, controller) {
1726
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1803
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1727
1804
  if (options.includeRawChunks) {
1728
1805
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1729
1806
  }
@@ -1732,6 +1809,23 @@ var GoogleGenerativeAILanguageModel = class {
1732
1809
  return;
1733
1810
  }
1734
1811
  const value = chunk.value;
1812
+ if (value.error != null) {
1813
+ finishReason = "error";
1814
+ const error = value.error;
1815
+ controller.enqueue({
1816
+ type: "error",
1817
+ error: new APICallError({
1818
+ message: error.message,
1819
+ url,
1820
+ requestBodyValues: args,
1821
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
1822
+ responseHeaders,
1823
+ responseBody: JSON.stringify({ error }),
1824
+ data: error
1825
+ })
1826
+ });
1827
+ return;
1828
+ }
1735
1829
  if (value.usageMetadata != null) {
1736
1830
  requestUsageMetadata = value.usageMetadata;
1737
1831
  }
@@ -1740,16 +1834,16 @@ var GoogleGenerativeAILanguageModel = class {
1740
1834
  requestUsageMetadata
1741
1835
  );
1742
1836
  if (usageMetadata != null) {
1743
- usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
1744
- usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
1745
- usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
1746
- usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
1747
- usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
1837
+ usage.inputTokens = (_b = usageMetadata.promptTokenCount) != null ? _b : void 0;
1838
+ usage.outputTokens = (_c = usageMetadata.candidatesTokenCount) != null ? _c : void 0;
1839
+ usage.totalTokens = (_d = usageMetadata.totalTokenCount) != null ? _d : void 0;
1840
+ usage.reasoningTokens = (_e = usageMetadata.thoughtsTokenCount) != null ? _e : void 0;
1841
+ usage.cachedInputTokens = (_f = usageMetadata.cachedContentTokenCount) != null ? _f : void 0;
1748
1842
  }
1749
1843
  if (value.serviceTier != null) {
1750
1844
  serviceTier = value.serviceTier;
1751
1845
  }
1752
- const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1846
+ const candidate = (_g = value.candidates) == null ? void 0 : _g[0];
1753
1847
  if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
1754
1848
  lastContinuationToken = candidate.continuationToken;
1755
1849
  }
@@ -1764,12 +1858,12 @@ var GoogleGenerativeAILanguageModel = class {
1764
1858
  requestUsageMetadata = void 0;
1765
1859
  }
1766
1860
  if (candidate == null) {
1767
- const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1861
+ const promptBlockReason2 = (_h = value.promptFeedback) == null ? void 0 : _h.blockReason;
1768
1862
  if (promptBlockReason2 != null) {
1769
1863
  finishReason = "content-filter";
1770
1864
  providerMetadata = {
1771
1865
  google: {
1772
- promptFeedback: (_h = value.promptFeedback) != null ? _h : null,
1866
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1773
1867
  groundingMetadata: lastGroundingMetadata,
1774
1868
  urlContextMetadata: lastUrlContextMetadata,
1775
1869
  safetyRatings: null,
@@ -1802,9 +1896,9 @@ var GoogleGenerativeAILanguageModel = class {
1802
1896
  }
1803
1897
  }
1804
1898
  if (content != null) {
1805
- const parts = (_i = content.parts) != null ? _i : [];
1899
+ const parts = (_j = content.parts) != null ? _j : [];
1806
1900
  for (const part of parts) {
1807
- if ("executableCode" in part && ((_j = part.executableCode) == null ? void 0 : _j.code)) {
1901
+ if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1808
1902
  const toolCallId = generateId3();
1809
1903
  lastCodeExecutionToolCallId = toolCallId;
1810
1904
  controller.enqueue({
@@ -1931,9 +2025,9 @@ var GoogleGenerativeAILanguageModel = class {
1931
2025
  }
1932
2026
  }
1933
2027
  }
1934
- const promptBlockReason = (_k = value.promptFeedback) == null ? void 0 : _k.blockReason;
2028
+ const promptBlockReason = (_l = value.promptFeedback) == null ? void 0 : _l.blockReason;
1935
2029
  const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
1936
- const rawFinishReason = (_m = (_l = candidate.finishReason) != null ? _l : promptBlockReason) != null ? _m : void 0;
2030
+ const rawFinishReason = (_n = (_m = candidate.finishReason) != null ? _m : promptBlockReason) != null ? _n : void 0;
1937
2031
  if (rawFinishReason != null) {
1938
2032
  finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1939
2033
  finishReason: rawFinishReason,
@@ -1941,10 +2035,10 @@ var GoogleGenerativeAILanguageModel = class {
1941
2035
  });
1942
2036
  providerMetadata = {
1943
2037
  google: {
1944
- promptFeedback: (_n = value.promptFeedback) != null ? _n : null,
2038
+ promptFeedback: (_o = value.promptFeedback) != null ? _o : null,
1945
2039
  groundingMetadata: lastGroundingMetadata,
1946
2040
  urlContextMetadata: lastUrlContextMetadata,
1947
- safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
2041
+ safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
1948
2042
  serviceTier,
1949
2043
  // Only surface the token when the response stopped early
1950
2044
  // (budget used up, or continuing failed), not for the
@@ -2251,6 +2345,7 @@ var responseSchema = lazySchema5(
2251
2345
  var chunkSchema = lazySchema5(
2252
2346
  () => zodSchema5(
2253
2347
  z5.object({
2348
+ error: getGoogleErrorSchema().nullish(),
2254
2349
  candidates: z5.array(
2255
2350
  z5.object({
2256
2351
  content: getContentSchema().nullish(),