@ai-sdk/google 2.0.107 → 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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 2.0.108
4
+
5
+ ### Patch Changes
6
+
7
+ - 0a069bd: fix(google): handle cycles and BigInt in functionResponse and parse string functionCall arguments
8
+ - 0a069bd: Map Google RPC status names to HTTP status codes when streamed errors omit a numeric code, so transient failures such as `UNAVAILABLE` and `RESOURCE_EXHAUSTED` are marked retryable.
9
+ - Updated dependencies [0a069bd]
10
+ - Updated dependencies [0a069bd]
11
+ - Updated dependencies [0a069bd]
12
+ - @ai-sdk/provider-utils@3.0.44
13
+
3
14
  ## 2.0.107
4
15
 
5
16
  ### Patch Changes
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(index_exports);
30
30
  var import_provider_utils15 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/version.ts
33
- var VERSION = true ? "2.0.107" : "0.0.0-test";
33
+ var VERSION = true ? "2.0.108" : "0.0.0-test";
34
34
 
35
35
  // src/google-generative-ai-embedding-model.ts
36
36
  var import_provider = require("@ai-sdk/provider");
@@ -40,15 +40,16 @@ var import_v43 = require("zod/v4");
40
40
  // src/google-error.ts
41
41
  var import_provider_utils = require("@ai-sdk/provider-utils");
42
42
  var import_v4 = require("zod/v4");
43
+ var getGoogleErrorSchema = () => import_v4.z.object({
44
+ code: import_v4.z.number().nullable(),
45
+ message: import_v4.z.string(),
46
+ status: import_v4.z.string(),
47
+ details: import_v4.z.array(import_v4.z.unknown()).nullish()
48
+ });
43
49
  var googleErrorDataSchema = (0, import_provider_utils.lazySchema)(
44
50
  () => (0, import_provider_utils.zodSchema)(
45
51
  import_v4.z.object({
46
- error: import_v4.z.object({
47
- code: import_v4.z.number().nullable(),
48
- message: import_v4.z.string(),
49
- status: import_v4.z.string(),
50
- details: import_v4.z.array(import_v4.z.unknown()).nullish()
51
- })
52
+ error: getGoogleErrorSchema()
52
53
  })
53
54
  )
54
55
  );
@@ -56,6 +57,39 @@ var googleFailedResponseHandler = (0, import_provider_utils.createJsonErrorRespo
56
57
  errorSchema: googleErrorDataSchema,
57
58
  errorToMessage: (data) => data.error.message
58
59
  });
60
+ function mapGoogleErrorStatusToHttpStatusCode(status) {
61
+ switch (status) {
62
+ case "CANCELLED":
63
+ return 499;
64
+ case "INVALID_ARGUMENT":
65
+ case "FAILED_PRECONDITION":
66
+ case "OUT_OF_RANGE":
67
+ return 400;
68
+ case "DEADLINE_EXCEEDED":
69
+ return 504;
70
+ case "NOT_FOUND":
71
+ return 404;
72
+ case "ALREADY_EXISTS":
73
+ case "ABORTED":
74
+ return 409;
75
+ case "PERMISSION_DENIED":
76
+ return 403;
77
+ case "UNAUTHENTICATED":
78
+ return 401;
79
+ case "RESOURCE_EXHAUSTED":
80
+ return 429;
81
+ case "UNIMPLEMENTED":
82
+ return 501;
83
+ case "UNKNOWN":
84
+ case "INTERNAL":
85
+ case "DATA_LOSS":
86
+ return 500;
87
+ case "UNAVAILABLE":
88
+ return 503;
89
+ default:
90
+ return void 0;
91
+ }
92
+ }
59
93
 
60
94
  // src/google-generative-ai-embedding-options.ts
61
95
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
@@ -238,6 +272,7 @@ var googleGenerativeAISingleEmbeddingResponseSchema = (0, import_provider_utils3
238
272
  );
239
273
 
240
274
  // src/google-generative-ai-language-model.ts
275
+ var import_provider5 = require("@ai-sdk/provider");
241
276
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
242
277
  var import_v45 = require("zod/v4");
243
278
 
@@ -713,7 +748,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
713
748
  functionCall: {
714
749
  ...part.toolCallId != null ? { id: part.toolCallId } : {},
715
750
  name: part.toolName,
716
- args: part.input
751
+ args: parseToolCallInput(part.input)
717
752
  },
718
753
  thoughtSignature: effectiveThoughtSignature
719
754
  };
@@ -741,7 +776,7 @@ function convertToGoogleGenerativeAIMessages(prompt, options) {
741
776
  name: part.toolName,
742
777
  response: {
743
778
  name: part.toolName,
744
- content: output.value
779
+ content: serializeFunctionResponseContent(output.value)
745
780
  }
746
781
  }
747
782
  });
@@ -832,11 +867,46 @@ function appendLegacyToolResultParts({
832
867
  );
833
868
  break;
834
869
  default:
835
- parts.push({ text: JSON.stringify(contentPart) });
870
+ parts.push({ text: (0, import_provider_utils4.stringifyWithBigIntAndCycles)(contentPart) });
836
871
  break;
837
872
  }
838
873
  }
839
874
  }
875
+ function serializeFunctionResponseContent(value) {
876
+ return containsNonJsonValue(value) ? (0, import_provider_utils4.secureJsonParse)((0, import_provider_utils4.stringifyWithBigIntAndCycles)(value)) : value;
877
+ }
878
+ function containsNonJsonValue(value, ancestors = /* @__PURE__ */ new Set()) {
879
+ if (typeof value === "bigint") {
880
+ return true;
881
+ }
882
+ if (typeof value !== "object" || value === null) {
883
+ return false;
884
+ }
885
+ if (typeof value.toJSON === "function") {
886
+ return true;
887
+ }
888
+ if (ancestors.has(value)) {
889
+ return true;
890
+ }
891
+ ancestors.add(value);
892
+ try {
893
+ return Object.values(value).some(
894
+ (item) => containsNonJsonValue(item, ancestors)
895
+ );
896
+ } finally {
897
+ ancestors.delete(value);
898
+ }
899
+ }
900
+ function parseToolCallInput(input) {
901
+ if (typeof input !== "string") {
902
+ return input;
903
+ }
904
+ try {
905
+ return (0, import_provider_utils4.secureJsonParse)(input);
906
+ } catch (e) {
907
+ return input;
908
+ }
909
+ }
840
910
 
841
911
  // src/get-model-path.ts
842
912
  function getModelPath(modelId) {
@@ -1648,10 +1718,11 @@ var GoogleGenerativeAILanguageModel = class {
1648
1718
  await (0, import_provider_utils6.resolve)(this.config.headers),
1649
1719
  options.headers
1650
1720
  );
1721
+ const url = `${this.config.baseURL}/${getModelPath(
1722
+ this.modelId
1723
+ )}:streamGenerateContent?alt=sse`;
1651
1724
  const streamGenerateContent = (body2) => (0, import_provider_utils6.postJsonToApi)({
1652
- url: `${this.config.baseURL}/${getModelPath(
1653
- this.modelId
1654
- )}:streamGenerateContent?alt=sse`,
1725
+ url,
1655
1726
  headers,
1656
1727
  body: body2,
1657
1728
  failedResponseHandler: googleFailedResponseHandler,
@@ -1710,7 +1781,7 @@ var GoogleGenerativeAILanguageModel = class {
1710
1781
  controller.enqueue({ type: "stream-start", warnings });
1711
1782
  },
1712
1783
  transform(chunk, controller) {
1713
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1784
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1714
1785
  if (options.includeRawChunks) {
1715
1786
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1716
1787
  }
@@ -1719,6 +1790,23 @@ var GoogleGenerativeAILanguageModel = class {
1719
1790
  return;
1720
1791
  }
1721
1792
  const value = chunk.value;
1793
+ if (value.error != null) {
1794
+ finishReason = "error";
1795
+ const error = value.error;
1796
+ controller.enqueue({
1797
+ type: "error",
1798
+ error: new import_provider5.APICallError({
1799
+ message: error.message,
1800
+ url,
1801
+ requestBodyValues: args,
1802
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
1803
+ responseHeaders,
1804
+ responseBody: JSON.stringify({ error }),
1805
+ data: error
1806
+ })
1807
+ });
1808
+ return;
1809
+ }
1722
1810
  if (value.usageMetadata != null) {
1723
1811
  requestUsageMetadata = value.usageMetadata;
1724
1812
  }
@@ -1727,16 +1815,16 @@ var GoogleGenerativeAILanguageModel = class {
1727
1815
  requestUsageMetadata
1728
1816
  );
1729
1817
  if (usageMetadata != null) {
1730
- usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
1731
- usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
1732
- usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
1733
- usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
1734
- usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
1818
+ usage.inputTokens = (_b = usageMetadata.promptTokenCount) != null ? _b : void 0;
1819
+ usage.outputTokens = (_c = usageMetadata.candidatesTokenCount) != null ? _c : void 0;
1820
+ usage.totalTokens = (_d = usageMetadata.totalTokenCount) != null ? _d : void 0;
1821
+ usage.reasoningTokens = (_e = usageMetadata.thoughtsTokenCount) != null ? _e : void 0;
1822
+ usage.cachedInputTokens = (_f = usageMetadata.cachedContentTokenCount) != null ? _f : void 0;
1735
1823
  }
1736
1824
  if (value.serviceTier != null) {
1737
1825
  serviceTier = value.serviceTier;
1738
1826
  }
1739
- const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1827
+ const candidate = (_g = value.candidates) == null ? void 0 : _g[0];
1740
1828
  if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
1741
1829
  lastContinuationToken = candidate.continuationToken;
1742
1830
  }
@@ -1751,12 +1839,12 @@ var GoogleGenerativeAILanguageModel = class {
1751
1839
  requestUsageMetadata = void 0;
1752
1840
  }
1753
1841
  if (candidate == null) {
1754
- const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1842
+ const promptBlockReason2 = (_h = value.promptFeedback) == null ? void 0 : _h.blockReason;
1755
1843
  if (promptBlockReason2 != null) {
1756
1844
  finishReason = "content-filter";
1757
1845
  providerMetadata = {
1758
1846
  google: {
1759
- promptFeedback: (_h = value.promptFeedback) != null ? _h : null,
1847
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1760
1848
  groundingMetadata: lastGroundingMetadata,
1761
1849
  urlContextMetadata: lastUrlContextMetadata,
1762
1850
  safetyRatings: null,
@@ -1789,9 +1877,9 @@ var GoogleGenerativeAILanguageModel = class {
1789
1877
  }
1790
1878
  }
1791
1879
  if (content != null) {
1792
- const parts = (_i = content.parts) != null ? _i : [];
1880
+ const parts = (_j = content.parts) != null ? _j : [];
1793
1881
  for (const part of parts) {
1794
- if ("executableCode" in part && ((_j = part.executableCode) == null ? void 0 : _j.code)) {
1882
+ if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1795
1883
  const toolCallId = generateId3();
1796
1884
  lastCodeExecutionToolCallId = toolCallId;
1797
1885
  controller.enqueue({
@@ -1918,9 +2006,9 @@ var GoogleGenerativeAILanguageModel = class {
1918
2006
  }
1919
2007
  }
1920
2008
  }
1921
- const promptBlockReason = (_k = value.promptFeedback) == null ? void 0 : _k.blockReason;
2009
+ const promptBlockReason = (_l = value.promptFeedback) == null ? void 0 : _l.blockReason;
1922
2010
  const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
1923
- const rawFinishReason = (_m = (_l = candidate.finishReason) != null ? _l : promptBlockReason) != null ? _m : void 0;
2011
+ const rawFinishReason = (_n = (_m = candidate.finishReason) != null ? _m : promptBlockReason) != null ? _n : void 0;
1924
2012
  if (rawFinishReason != null) {
1925
2013
  finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1926
2014
  finishReason: rawFinishReason,
@@ -1928,10 +2016,10 @@ var GoogleGenerativeAILanguageModel = class {
1928
2016
  });
1929
2017
  providerMetadata = {
1930
2018
  google: {
1931
- promptFeedback: (_n = value.promptFeedback) != null ? _n : null,
2019
+ promptFeedback: (_o = value.promptFeedback) != null ? _o : null,
1932
2020
  groundingMetadata: lastGroundingMetadata,
1933
2021
  urlContextMetadata: lastUrlContextMetadata,
1934
- safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
2022
+ safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
1935
2023
  serviceTier,
1936
2024
  // Only surface the token when the response stopped early
1937
2025
  // (budget used up, or continuing failed), not for the
@@ -2238,6 +2326,7 @@ var responseSchema = (0, import_provider_utils6.lazySchema)(
2238
2326
  var chunkSchema = (0, import_provider_utils6.lazySchema)(
2239
2327
  () => (0, import_provider_utils6.zodSchema)(
2240
2328
  import_v45.z.object({
2329
+ error: getGoogleErrorSchema().nullish(),
2241
2330
  candidates: import_v45.z.array(
2242
2331
  import_v45.z.object({
2243
2332
  content: getContentSchema().nullish(),