@ai-sdk/google 3.0.135 → 3.0.136

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,