@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/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.107" : "0.0.0-test";
10
+ var VERSION = true ? "2.0.109" : "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) {
@@ -1057,6 +1133,9 @@ function prepareTools({
1057
1133
  modelId,
1058
1134
  isVertexProvider = false
1059
1135
  }) {
1136
+ if ((toolChoice == null ? void 0 : toolChoice.type) === "none") {
1137
+ tools = tools == null ? void 0 : tools.filter((tool) => tool.type === "function");
1138
+ }
1060
1139
  tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
1061
1140
  const toolWarnings = [];
1062
1141
  const { supportsGemini2Tools, supportsFileSearch, usesGemini3Features } = getGoogleModelCapabilities(modelId);
@@ -1072,7 +1151,9 @@ function prepareTools({
1072
1151
  toolWarnings.push({
1073
1152
  type: "unsupported-tool",
1074
1153
  tool: tools.find((tool) => tool.type === "function"),
1075
- 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.`
1154
+ 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(
1155
+ ", "
1156
+ )}. Please use either function tools or provider-defined tools, but not both.`
1076
1157
  });
1077
1158
  }
1078
1159
  if (hasProviderDefinedTools) {
@@ -1661,10 +1742,11 @@ var GoogleGenerativeAILanguageModel = class {
1661
1742
  await resolve2(this.config.headers),
1662
1743
  options.headers
1663
1744
  );
1745
+ const url = `${this.config.baseURL}/${getModelPath(
1746
+ this.modelId
1747
+ )}:streamGenerateContent?alt=sse`;
1664
1748
  const streamGenerateContent = (body2) => postJsonToApi2({
1665
- url: `${this.config.baseURL}/${getModelPath(
1666
- this.modelId
1667
- )}:streamGenerateContent?alt=sse`,
1749
+ url,
1668
1750
  headers,
1669
1751
  body: body2,
1670
1752
  failedResponseHandler: googleFailedResponseHandler,
@@ -1723,7 +1805,7 @@ var GoogleGenerativeAILanguageModel = class {
1723
1805
  controller.enqueue({ type: "stream-start", warnings });
1724
1806
  },
1725
1807
  transform(chunk, controller) {
1726
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1808
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1727
1809
  if (options.includeRawChunks) {
1728
1810
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1729
1811
  }
@@ -1732,6 +1814,23 @@ var GoogleGenerativeAILanguageModel = class {
1732
1814
  return;
1733
1815
  }
1734
1816
  const value = chunk.value;
1817
+ if (value.error != null) {
1818
+ finishReason = "error";
1819
+ const error = value.error;
1820
+ controller.enqueue({
1821
+ type: "error",
1822
+ error: new APICallError({
1823
+ message: error.message,
1824
+ url,
1825
+ requestBodyValues: args,
1826
+ statusCode: (_a = error.code) != null ? _a : mapGoogleErrorStatusToHttpStatusCode(error.status),
1827
+ responseHeaders,
1828
+ responseBody: JSON.stringify({ error }),
1829
+ data: error
1830
+ })
1831
+ });
1832
+ return;
1833
+ }
1735
1834
  if (value.usageMetadata != null) {
1736
1835
  requestUsageMetadata = value.usageMetadata;
1737
1836
  }
@@ -1740,16 +1839,16 @@ var GoogleGenerativeAILanguageModel = class {
1740
1839
  requestUsageMetadata
1741
1840
  );
1742
1841
  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;
1842
+ usage.inputTokens = (_b = usageMetadata.promptTokenCount) != null ? _b : void 0;
1843
+ usage.outputTokens = (_c = usageMetadata.candidatesTokenCount) != null ? _c : void 0;
1844
+ usage.totalTokens = (_d = usageMetadata.totalTokenCount) != null ? _d : void 0;
1845
+ usage.reasoningTokens = (_e = usageMetadata.thoughtsTokenCount) != null ? _e : void 0;
1846
+ usage.cachedInputTokens = (_f = usageMetadata.cachedContentTokenCount) != null ? _f : void 0;
1748
1847
  }
1749
1848
  if (value.serviceTier != null) {
1750
1849
  serviceTier = value.serviceTier;
1751
1850
  }
1752
- const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1851
+ const candidate = (_g = value.candidates) == null ? void 0 : _g[0];
1753
1852
  if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
1754
1853
  lastContinuationToken = candidate.continuationToken;
1755
1854
  }
@@ -1764,12 +1863,12 @@ var GoogleGenerativeAILanguageModel = class {
1764
1863
  requestUsageMetadata = void 0;
1765
1864
  }
1766
1865
  if (candidate == null) {
1767
- const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1866
+ const promptBlockReason2 = (_h = value.promptFeedback) == null ? void 0 : _h.blockReason;
1768
1867
  if (promptBlockReason2 != null) {
1769
1868
  finishReason = "content-filter";
1770
1869
  providerMetadata = {
1771
1870
  google: {
1772
- promptFeedback: (_h = value.promptFeedback) != null ? _h : null,
1871
+ promptFeedback: (_i = value.promptFeedback) != null ? _i : null,
1773
1872
  groundingMetadata: lastGroundingMetadata,
1774
1873
  urlContextMetadata: lastUrlContextMetadata,
1775
1874
  safetyRatings: null,
@@ -1802,9 +1901,9 @@ var GoogleGenerativeAILanguageModel = class {
1802
1901
  }
1803
1902
  }
1804
1903
  if (content != null) {
1805
- const parts = (_i = content.parts) != null ? _i : [];
1904
+ const parts = (_j = content.parts) != null ? _j : [];
1806
1905
  for (const part of parts) {
1807
- if ("executableCode" in part && ((_j = part.executableCode) == null ? void 0 : _j.code)) {
1906
+ if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1808
1907
  const toolCallId = generateId3();
1809
1908
  lastCodeExecutionToolCallId = toolCallId;
1810
1909
  controller.enqueue({
@@ -1931,9 +2030,9 @@ var GoogleGenerativeAILanguageModel = class {
1931
2030
  }
1932
2031
  }
1933
2032
  }
1934
- const promptBlockReason = (_k = value.promptFeedback) == null ? void 0 : _k.blockReason;
2033
+ const promptBlockReason = (_l = value.promptFeedback) == null ? void 0 : _l.blockReason;
1935
2034
  const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
1936
- const rawFinishReason = (_m = (_l = candidate.finishReason) != null ? _l : promptBlockReason) != null ? _m : void 0;
2035
+ const rawFinishReason = (_n = (_m = candidate.finishReason) != null ? _m : promptBlockReason) != null ? _n : void 0;
1937
2036
  if (rawFinishReason != null) {
1938
2037
  finishReason = isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
1939
2038
  finishReason: rawFinishReason,
@@ -1941,10 +2040,10 @@ var GoogleGenerativeAILanguageModel = class {
1941
2040
  });
1942
2041
  providerMetadata = {
1943
2042
  google: {
1944
- promptFeedback: (_n = value.promptFeedback) != null ? _n : null,
2043
+ promptFeedback: (_o = value.promptFeedback) != null ? _o : null,
1945
2044
  groundingMetadata: lastGroundingMetadata,
1946
2045
  urlContextMetadata: lastUrlContextMetadata,
1947
- safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
2046
+ safetyRatings: (_p = candidate.safetyRatings) != null ? _p : null,
1948
2047
  serviceTier,
1949
2048
  // Only surface the token when the response stopped early
1950
2049
  // (budget used up, or continuing failed), not for the
@@ -2251,6 +2350,7 @@ var responseSchema = lazySchema5(
2251
2350
  var chunkSchema = lazySchema5(
2252
2351
  () => zodSchema5(
2253
2352
  z5.object({
2353
+ error: getGoogleErrorSchema().nullish(),
2254
2354
  candidates: z5.array(
2255
2355
  z5.object({
2256
2356
  content: getContentSchema().nullish(),