@ai-sdk/google 2.0.107 → 2.0.109

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,25 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 2.0.109
4
+
5
+ ### Patch Changes
6
+
7
+ - 5eaea4b: Omit Google provider-defined tools, including File Search and Vertex RAG Store, when `toolChoice` is `none` so disabled tools cannot retrieve data or execute server-side. Preserve function declarations needed by conversation history while disabling function calling.
8
+ - Updated dependencies [49c554d]
9
+ - Updated dependencies [b753c03]
10
+ - @ai-sdk/provider-utils@3.0.45
11
+
12
+ ## 2.0.108
13
+
14
+ ### Patch Changes
15
+
16
+ - 0a069bd: fix(google): handle cycles and BigInt in functionResponse and parse string functionCall arguments
17
+ - 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.
18
+ - Updated dependencies [0a069bd]
19
+ - Updated dependencies [0a069bd]
20
+ - Updated dependencies [0a069bd]
21
+ - @ai-sdk/provider-utils@3.0.44
22
+
3
23
  ## 2.0.107
4
24
 
5
25
  ### 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.109" : "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) {
@@ -1044,6 +1114,9 @@ function prepareTools({
1044
1114
  modelId,
1045
1115
  isVertexProvider = false
1046
1116
  }) {
1117
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
1118
+ tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
1119
+ }
1047
1120
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1048
1121
  const toolWarnings = [];
1049
1122
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -1059,7 +1132,9 @@ function prepareTools({
1059
1132
  toolWarnings.push({
1060
1133
  type: "unsupported-tool",
1061
1134
  tool: tools.find((tool) => tool.type === "function"),
1062
- details: `Cannot mix function tools with provider-defined tools in the same request. Falling back to provider-defined tools only. The following function tools will be ignored: ${functionTools.map((t) => t.name).join(", ")}. Please use either function tools or provider-defined tools, but not both.`
1135
+ details: `Cannot mix function tools with provider-defined tools in the same request. Falling back to provider-defined tools only. The following function tools will be ignored: ${functionTools.map((t) => t.name).join(
1136
+ ", "
1137
+ )}. Please use either function tools or provider-defined tools, but not both.`
1063
1138
  });
1064
1139
  }
1065
1140
  if (hasProviderDefinedTools) {
@@ -1648,10 +1723,11 @@ var GoogleGenerativeAILanguageModel = class {
1648
1723
  await (0, import_provider_utils6.resolve)(this.config.headers),
1649
1724
  options.headers
1650
1725
  );
1726
+ const url = `${this.config.baseURL}/${getModelPath(
1727
+ this.modelId
1728
+ )}:streamGenerateContent?alt=sse`;
1651
1729
  const streamGenerateContent = (body2) => (0, import_provider_utils6.postJsonToApi)({
1652
- url: `${this.config.baseURL}/${getModelPath(
1653
- this.modelId
1654
- )}:streamGenerateContent?alt=sse`,
1730
+ url,
1655
1731
  headers,
1656
1732
  body: body2,
1657
1733
  failedResponseHandler: googleFailedResponseHandler,
@@ -1710,7 +1786,7 @@ var GoogleGenerativeAILanguageModel = class {
1710
1786
  controller.enqueue({ type: "stream-start", warnings });
1711
1787
  },
1712
1788
  transform(chunk, controller) {
1713
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1789
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1714
1790
  if (options.includeRawChunks) {
1715
1791
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1716
1792
  }
@@ -1719,6 +1795,23 @@ var GoogleGenerativeAILanguageModel = class {
1719
1795
  return;
1720
1796
  }
1721
1797
  const value = chunk.value;
1798
+ if (value.error != null) {
1799
+ finishReason = "error";
1800
+ const error = value.error;
1801
+ controller.enqueue({
1802
+ type: "error",
1803
+ error: new import_provider5.APICallError({
1804
+ message: error.message,
1805
+ url,
1806
+ requestBodyValues: args,
1807
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
1808
+ responseHeaders,
1809
+ responseBody: JSON.stringify({ error }),
1810
+ data: error
1811
+ })
1812
+ });
1813
+ return;
1814
+ }
1722
1815
  if (value.usageMetadata != null) {
1723
1816
  requestUsageMetadata = value.usageMetadata;
1724
1817
  }
@@ -1727,16 +1820,16 @@ var GoogleGenerativeAILanguageModel = class {
1727
1820
  requestUsageMetadata
1728
1821
  );
1729
1822
  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;
1823
+ usage.inputTokens = (_b = usageMetadata.promptTokenCount) != null ? _b : void 0;
1824
+ usage.outputTokens = (_c = usageMetadata.candidatesTokenCount) != null ? _c : void 0;
1825
+ usage.totalTokens = (_d = usageMetadata.totalTokenCount) != null ? _d : void 0;
1826
+ usage.reasoningTokens = (_e = usageMetadata.thoughtsTokenCount) != null ? _e : void 0;
1827
+ usage.cachedInputTokens = (_f = usageMetadata.cachedContentTokenCount) != null ? _f : void 0;
1735
1828
  }
1736
1829
  if (value.serviceTier != null) {
1737
1830
  serviceTier = value.serviceTier;
1738
1831
  }
1739
- const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1832
+ const candidate = (_g = value.candidates) == null ? void 0 : _g[0];
1740
1833
  if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
1741
1834
  lastContinuationToken = candidate.continuationToken;
1742
1835
  }
@@ -1751,12 +1844,12 @@ var GoogleGenerativeAILanguageModel = class {
1751
1844
  requestUsageMetadata = void 0;
1752
1845
  }
1753
1846
  if (candidate == null) {
1754
- const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1847
+ const promptBlockReason2 = (_h = value.promptFeedback) == null ? void 0 : _h.blockReason;
1755
1848
  if (promptBlockReason2 != null) {
1756
1849
  finishReason = "content-filter";
1757
1850
  providerMetadata = {
1758
1851
  google: {
1759
- promptFeedback: (_h = value.promptFeedback) != null ? _h : null,
1852
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1760
1853
  groundingMetadata: lastGroundingMetadata,
1761
1854
  urlContextMetadata: lastUrlContextMetadata,
1762
1855
  safetyRatings: null,
@@ -1789,9 +1882,9 @@ var GoogleGenerativeAILanguageModel = class {
1789
1882
  }
1790
1883
  }
1791
1884
  if (content != null) {
1792
- const parts = (_i = content.parts) != null ? _i : [];
1885
+ const parts = (_j = content.parts) != null ? _j : [];
1793
1886
  for (const part of parts) {
1794
- if ("executableCode" in part && ((_j = part.executableCode) == null ? void 0 : _j.code)) {
1887
+ if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1795
1888
  const toolCallId = generateId3();
1796
1889
  lastCodeExecutionToolCallId = toolCallId;
1797
1890
  controller.enqueue({
@@ -1918,9 +2011,9 @@ var GoogleGenerativeAILanguageModel = class {
1918
2011
  }
1919
2012
  }
1920
2013
  }
1921
- const promptBlockReason = (_k = value.promptFeedback) == null ? void 0 : _k.blockReason;
2014
+ const promptBlockReason = (_l = value.promptFeedback) == null ? void 0 : _l.blockReason;
1922
2015
  const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
1923
- const rawFinishReason = (_m = (_l = candidate.finishReason) != null ? _l : promptBlockReason) != null ? _m : void 0;
2016
+ const rawFinishReason = (_n = (_m = candidate.finishReason) != null ? _m : promptBlockReason) != null ? _n : void 0;
1924
2017
  if (rawFinishReason != null) {
1925
2018
  finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1926
2019
  finishReason: rawFinishReason,
@@ -1928,10 +2021,10 @@ var GoogleGenerativeAILanguageModel = class {
1928
2021
  });
1929
2022
  providerMetadata = {
1930
2023
  google: {
1931
- promptFeedback: (_n = value.promptFeedback) != null ? _n : null,
2024
+ promptFeedback: (_o = value.promptFeedback) != null ? _o : null,
1932
2025
  groundingMetadata: lastGroundingMetadata,
1933
2026
  urlContextMetadata: lastUrlContextMetadata,
1934
- safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
2027
+ safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
1935
2028
  serviceTier,
1936
2029
  // Only surface the token when the response stopped early
1937
2030
  // (budget used up, or continuing failed), not for the
@@ -2238,6 +2331,7 @@ var responseSchema = (0, import_provider_utils6.lazySchema)(
2238
2331
  var chunkSchema = (0, import_provider_utils6.lazySchema)(
2239
2332
  () => (0, import_provider_utils6.zodSchema)(
2240
2333
  import_v45.z.object({
2334
+ error: getGoogleErrorSchema().nullish(),
2241
2335
  candidates: import_v45.z.array(
2242
2336
  import_v45.z.object({
2243
2337
  content: getContentSchema().nullish(),