@ai-sdk/google 3.0.135 → 3.0.137

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.
@@ -1887,7 +1887,7 @@ var GoogleGenerativeAILanguageModel = class {
1887
1887
  };
1888
1888
  }
1889
1889
  async doGenerate(options) {
1890
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A;
1890
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _A, _B;
1891
1891
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
1892
1892
  const mergedHeaders = (0, import_provider_utils5.combineHeaders)(
1893
1893
  await (0, import_provider_utils5.resolve)(this.config.headers),
@@ -1911,18 +1911,20 @@ var GoogleGenerativeAILanguageModel = class {
1911
1911
  rawValue: rawResponse
1912
1912
  } = await generateContent(args);
1913
1913
  const continuedParts = [];
1914
+ const groundingMetadataPerRequest = [];
1914
1915
  let continuedUsageMetadata;
1915
1916
  const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
1916
1917
  while (true) {
1917
1918
  const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
1919
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
1918
1920
  const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
1919
1921
  if (continuationToken == null) {
1920
1922
  break;
1921
1923
  }
1922
- continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
1924
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
1923
1925
  continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1924
1926
  continuedUsageMetadata,
1925
- (_e = response.usageMetadata) != null ? _e : void 0
1927
+ (_f = response.usageMetadata) != null ? _f : void 0
1926
1928
  );
1927
1929
  ({
1928
1930
  responseHeaders,
@@ -1930,23 +1932,23 @@ var GoogleGenerativeAILanguageModel = class {
1930
1932
  rawValue: rawResponse
1931
1933
  } = await generateContent({ ...args, continuationToken }));
1932
1934
  }
1933
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
1934
- const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
1935
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
1936
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
1935
1937
  const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
1936
1938
  promptBlockReason
1937
1939
  ) ? promptBlockReason : void 0;
1938
1940
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
1939
- const rawFinishReason = (_h = candidate == null ? void 0 : candidate.finishReason) != null ? _h : confirmedPromptBlockReason;
1941
+ const rawFinishReason = (_i = candidate == null ? void 0 : candidate.finishReason) != null ? _i : confirmedPromptBlockReason;
1940
1942
  const content = [];
1941
- const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
1943
+ const parts = [...continuedParts, ...(_k = (_j = candidate == null ? void 0 : candidate.content) == null ? void 0 : _j.parts) != null ? _k : []];
1942
1944
  const usageMetadata = addGoogleGenerativeAIUsageMetadata(
1943
1945
  continuedUsageMetadata,
1944
- (_k = response.usageMetadata) != null ? _k : void 0
1946
+ (_l = response.usageMetadata) != null ? _l : void 0
1945
1947
  );
1946
1948
  let lastCodeExecutionToolCallId;
1947
1949
  let lastServerToolCallId;
1948
1950
  for (const part of parts) {
1949
- if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
1951
+ if ("executableCode" in part && ((_m = part.executableCode) == null ? void 0 : _m.code)) {
1950
1952
  const toolCallId = this.config.generateId();
1951
1953
  lastCodeExecutionToolCallId = toolCallId;
1952
1954
  content.push({
@@ -1964,7 +1966,7 @@ var GoogleGenerativeAILanguageModel = class {
1964
1966
  toolName: "code_execution",
1965
1967
  result: {
1966
1968
  outcome: part.codeExecutionResult.outcome,
1967
- output: (_m = part.codeExecutionResult.output) != null ? _m : ""
1969
+ output: (_n = part.codeExecutionResult.output) != null ? _n : ""
1968
1970
  }
1969
1971
  });
1970
1972
  } else if ("text" in part && part.text != null) {
@@ -1988,9 +1990,9 @@ var GoogleGenerativeAILanguageModel = class {
1988
1990
  } else if ("functionCall" in part && part.functionCall.name != null) {
1989
1991
  content.push({
1990
1992
  type: "tool-call",
1991
- toolCallId: (_n = part.functionCall.id) != null ? _n : this.config.generateId(),
1993
+ toolCallId: (_o = part.functionCall.id) != null ? _o : this.config.generateId(),
1992
1994
  toolName: part.functionCall.name,
1993
- input: JSON.stringify((_o = part.functionCall.args) != null ? _o : {}),
1995
+ input: JSON.stringify((_p = part.functionCall.args) != null ? _p : {}),
1994
1996
  providerMetadata: part.thoughtSignature ? {
1995
1997
  [providerOptionsName]: {
1996
1998
  thoughtSignature: part.thoughtSignature
@@ -2012,13 +2014,13 @@ var GoogleGenerativeAILanguageModel = class {
2012
2014
  } : void 0
2013
2015
  });
2014
2016
  } else if ("toolCall" in part && part.toolCall) {
2015
- const toolCallId = (_p = part.toolCall.id) != null ? _p : this.config.generateId();
2017
+ const toolCallId = (_q = part.toolCall.id) != null ? _q : this.config.generateId();
2016
2018
  lastServerToolCallId = toolCallId;
2017
2019
  content.push({
2018
2020
  type: "tool-call",
2019
2021
  toolCallId,
2020
2022
  toolName: `server:${part.toolCall.toolType}`,
2021
- input: JSON.stringify((_q = part.toolCall.args) != null ? _q : {}),
2023
+ input: JSON.stringify((_r = part.toolCall.args) != null ? _r : {}),
2022
2024
  providerExecuted: true,
2023
2025
  dynamic: true,
2024
2026
  providerMetadata: part.thoughtSignature ? {
@@ -2035,12 +2037,12 @@ var GoogleGenerativeAILanguageModel = class {
2035
2037
  }
2036
2038
  });
2037
2039
  } else if ("toolResponse" in part && part.toolResponse) {
2038
- const responseToolCallId = (_r = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _r : this.config.generateId();
2040
+ const responseToolCallId = (_s = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _s : this.config.generateId();
2039
2041
  content.push({
2040
2042
  type: "tool-result",
2041
2043
  toolCallId: responseToolCallId,
2042
2044
  toolName: `server:${part.toolResponse.toolType}`,
2043
- result: (_s = part.toolResponse.response) != null ? _s : {},
2045
+ result: (_t = part.toolResponse.response) != null ? _t : {},
2044
2046
  providerMetadata: part.thoughtSignature ? {
2045
2047
  [providerOptionsName]: {
2046
2048
  thoughtSignature: part.thoughtSignature,
@@ -2057,10 +2059,10 @@ var GoogleGenerativeAILanguageModel = class {
2057
2059
  lastServerToolCallId = void 0;
2058
2060
  }
2059
2061
  }
2060
- const sources = (_t = extractSources({
2062
+ const sources = (_u = extractSources({
2061
2063
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
2062
2064
  generateId: this.config.generateId
2063
- })) != null ? _t : [];
2065
+ })) != null ? _u : [];
2064
2066
  for (const source of sources) {
2065
2067
  content.push(source);
2066
2068
  }
@@ -2080,13 +2082,14 @@ var GoogleGenerativeAILanguageModel = class {
2080
2082
  warnings,
2081
2083
  providerMetadata: {
2082
2084
  [providerOptionsName]: {
2083
- promptFeedback: (_u = response.promptFeedback) != null ? _u : null,
2084
- groundingMetadata: (_v = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _v : null,
2085
- urlContextMetadata: (_w = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _w : null,
2086
- safetyRatings: (_x = candidate == null ? void 0 : candidate.safetyRatings) != null ? _x : null,
2085
+ promptFeedback: (_v = response.promptFeedback) != null ? _v : null,
2086
+ groundingMetadata: (_w = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _w : null,
2087
+ ...groundingMetadataPerRequest.length > 1 ? { groundingMetadataPerRequest } : {},
2088
+ urlContextMetadata: (_x = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _x : null,
2089
+ safetyRatings: (_y = candidate == null ? void 0 : candidate.safetyRatings) != null ? _y : null,
2087
2090
  usageMetadata: usageMetadata != null ? usageMetadata : null,
2088
- finishMessage: (_y = candidate == null ? void 0 : candidate.finishMessage) != null ? _y : null,
2089
- serviceTier: (_z = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _z : null,
2091
+ finishMessage: (_z = candidate == null ? void 0 : candidate.finishMessage) != null ? _z : null,
2092
+ serviceTier: (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : null,
2090
2093
  ...(candidate == null ? void 0 : candidate.continuationToken) != null && rawFinishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
2091
2094
  ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
2092
2095
  }
@@ -2094,7 +2097,7 @@ var GoogleGenerativeAILanguageModel = class {
2094
2097
  request: { body: args },
2095
2098
  response: {
2096
2099
  // TODO timestamp, model id
2097
- id: (_A = response.responseId) != null ? _A : void 0,
2100
+ id: (_B = response.responseId) != null ? _B : void 0,
2098
2101
  headers: responseHeaders,
2099
2102
  body: rawResponse
2100
2103
  }
@@ -2119,14 +2122,27 @@ var GoogleGenerativeAILanguageModel = class {
2119
2122
  fetch: this.config.fetch
2120
2123
  });
2121
2124
  const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
2125
+ const groundingMetadataPerRequest = [];
2126
+ let requestGroundingMetadata = null;
2122
2127
  const response = continueGoogleStream({
2123
2128
  stream: firstResponse,
2124
2129
  getContinuationToken: (chunk) => {
2125
2130
  var _a, _b;
2126
2131
  const candidate = (_a = chunk.candidates) == null ? void 0 : _a[0];
2132
+ if ((candidate == null ? void 0 : candidate.groundingMetadata) != null) {
2133
+ requestGroundingMetadata = candidate.groundingMetadata;
2134
+ }
2127
2135
  return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
2128
2136
  },
2129
- continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
2137
+ continueStream: async (continuationToken) => {
2138
+ const { value } = await streamGenerateContent({
2139
+ ...args,
2140
+ continuationToken
2141
+ });
2142
+ groundingMetadataPerRequest.push(requestGroundingMetadata);
2143
+ requestGroundingMetadata = null;
2144
+ return value;
2145
+ }
2130
2146
  });
2131
2147
  let finishReason = {
2132
2148
  unified: "other",
@@ -2561,6 +2577,12 @@ var GoogleGenerativeAILanguageModel = class {
2561
2577
  [providerOptionsName]: {
2562
2578
  promptFeedback,
2563
2579
  groundingMetadata: lastGroundingMetadata,
2580
+ ...groundingMetadataPerRequest.length > 0 ? {
2581
+ groundingMetadataPerRequest: [
2582
+ ...groundingMetadataPerRequest,
2583
+ requestGroundingMetadata
2584
+ ]
2585
+ } : {},
2564
2586
  urlContextMetadata: lastUrlContextMetadata,
2565
2587
  safetyRatings: lastSafetyRatings,
2566
2588
  usageMetadata: usage != null ? usage : null,
@@ -2733,7 +2755,7 @@ var getGroundingMetadataSchema = () => import_v43.z.object({
2733
2755
  import_v43.z.object({})
2734
2756
  ]).nullish()
2735
2757
  });
2736
- var partialArgSchema = import_v43.z.object({
2758
+ var partialArgSchema = () => import_v43.z.object({
2737
2759
  jsonPath: import_v43.z.string(),
2738
2760
  stringValue: import_v43.z.string().nullish(),
2739
2761
  numberValue: import_v43.z.number().nullish(),
@@ -2750,7 +2772,7 @@ var getContentSchema = () => import_v43.z.object({
2750
2772
  id: import_v43.z.string().nullish(),
2751
2773
  name: import_v43.z.string().nullish(),
2752
2774
  args: import_v43.z.unknown().nullish(),
2753
- partialArgs: import_v43.z.array(partialArgSchema).nullish(),
2775
+ partialArgs: import_v43.z.array(partialArgSchema()).nullish(),
2754
2776
  willContinue: import_v43.z.boolean().nullish()
2755
2777
  }),
2756
2778
  thoughtSignature: import_v43.z.string().nullish()
@@ -2803,13 +2825,13 @@ var getSafetyRatingSchema = () => import_v43.z.object({
2803
2825
  severityScore: import_v43.z.number().nullish(),
2804
2826
  blocked: import_v43.z.boolean().nullish()
2805
2827
  });
2806
- var tokenDetailsSchema = import_v43.z.array(
2828
+ var tokenDetailsSchema = () => import_v43.z.array(
2807
2829
  import_v43.z.object({
2808
2830
  modality: import_v43.z.string(),
2809
2831
  tokenCount: import_v43.z.number()
2810
2832
  }).catchall(import_v43.z.json())
2811
2833
  ).nullish();
2812
- var usageSchema = import_v43.z.object({
2834
+ var usageSchema = () => import_v43.z.object({
2813
2835
  cachedContentTokenCount: import_v43.z.number().nullish(),
2814
2836
  thoughtsTokenCount: import_v43.z.number().nullish(),
2815
2837
  promptTokenCount: import_v43.z.number().nullish(),
@@ -2820,10 +2842,10 @@ var usageSchema = import_v43.z.object({
2820
2842
  trafficType: import_v43.z.string().nullish(),
2821
2843
  serviceTier: import_v43.z.string().nullish(),
2822
2844
  // https://ai.google.dev/api/generate-content#Modality
2823
- promptTokensDetails: tokenDetailsSchema,
2824
- cacheTokensDetails: tokenDetailsSchema,
2825
- candidatesTokensDetails: tokenDetailsSchema,
2826
- toolUsePromptTokensDetails: tokenDetailsSchema
2845
+ promptTokensDetails: tokenDetailsSchema(),
2846
+ cacheTokensDetails: tokenDetailsSchema(),
2847
+ candidatesTokensDetails: tokenDetailsSchema(),
2848
+ toolUsePromptTokensDetails: tokenDetailsSchema()
2827
2849
  }).catchall(import_v43.z.json());
2828
2850
  var getUrlContextMetadataSchema = () => import_v43.z.object({
2829
2851
  urlMetadata: import_v43.z.array(
@@ -2848,7 +2870,7 @@ var responseSchema = (0, import_provider_utils5.lazySchema)(
2848
2870
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
2849
2871
  })
2850
2872
  ).nullish(),
2851
- usageMetadata: usageSchema.nullish(),
2873
+ usageMetadata: usageSchema().nullish(),
2852
2874
  promptFeedback: import_v43.z.object({
2853
2875
  blockReason: import_v43.z.string().nullish(),
2854
2876
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish()
@@ -2871,7 +2893,7 @@ var chunkSchema = (0, import_provider_utils5.lazySchema)(
2871
2893
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
2872
2894
  })
2873
2895
  ).nullish(),
2874
- usageMetadata: usageSchema.nullish(),
2896
+ usageMetadata: usageSchema().nullish(),
2875
2897
  promptFeedback: import_v43.z.object({
2876
2898
  blockReason: import_v43.z.string().nullish(),
2877
2899
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish()
@@ -2909,7 +2931,7 @@ var enterpriseWebSearch = (0, import_provider_utils7.createProviderToolFactory)(
2909
2931
  // src/tool/file-search.ts
2910
2932
  var import_provider_utils8 = require("@ai-sdk/provider-utils");
2911
2933
  var import_v46 = require("zod/v4");
2912
- var fileSearchArgsBaseSchema = import_v46.z.object({
2934
+ var fileSearchArgsBaseSchema = () => import_v46.z.object({
2913
2935
  /** The names of the file_search_stores to retrieve from.
2914
2936
  * Example: `fileSearchStores/my-file-search-store-123`
2915
2937
  */
@@ -2926,7 +2948,7 @@ var fileSearchArgsBaseSchema = import_v46.z.object({
2926
2948
  ).optional()
2927
2949
  }).passthrough();
2928
2950
  var fileSearchArgsSchema = (0, import_provider_utils8.lazySchema)(
2929
- () => (0, import_provider_utils8.zodSchema)(fileSearchArgsBaseSchema)
2951
+ () => (0, import_provider_utils8.zodSchema)(fileSearchArgsBaseSchema())
2930
2952
  );
2931
2953
  var fileSearch = (0, import_provider_utils8.createProviderToolFactory)({
2932
2954
  id: "google.file_search",
@@ -2944,7 +2966,7 @@ var googleMaps = (0, import_provider_utils9.createProviderToolFactory)({
2944
2966
  // src/tool/google-search.ts
2945
2967
  var import_provider_utils10 = require("@ai-sdk/provider-utils");
2946
2968
  var import_v48 = require("zod/v4");
2947
- var googleSearchToolArgsBaseSchema = import_v48.z.object({
2969
+ var googleSearchToolArgsBaseSchema = () => import_v48.z.object({
2948
2970
  searchTypes: import_v48.z.object({
2949
2971
  webSearch: import_v48.z.object({}).optional(),
2950
2972
  imageSearch: import_v48.z.object({}).optional()
@@ -2955,7 +2977,7 @@ var googleSearchToolArgsBaseSchema = import_v48.z.object({
2955
2977
  }).optional()
2956
2978
  }).passthrough();
2957
2979
  var googleSearchToolArgsSchema = (0, import_provider_utils10.lazySchema)(
2958
- () => (0, import_provider_utils10.zodSchema)(googleSearchToolArgsBaseSchema)
2980
+ () => (0, import_provider_utils10.zodSchema)(googleSearchToolArgsBaseSchema())
2959
2981
  );
2960
2982
  var googleSearch = (0, import_provider_utils10.createProviderToolFactory)(
2961
2983
  {