@ai-sdk/google 3.0.122 → 3.0.124

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.
@@ -1667,7 +1667,7 @@ var GoogleGenerativeAILanguageModel = class {
1667
1667
  };
1668
1668
  }
1669
1669
  async doGenerate(options) {
1670
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
1670
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
1671
1671
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
1672
1672
  const mergedHeaders = combineHeaders(
1673
1673
  await resolve(this.config.headers),
@@ -1691,15 +1691,18 @@ var GoogleGenerativeAILanguageModel = class {
1691
1691
  });
1692
1692
  const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
1693
1693
  const promptBlockReason = (_b = response.promptFeedback) == null ? void 0 : _b.blockReason;
1694
- const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
1695
- const rawFinishReason = (_d = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : promptBlockReason) != null ? _d : void 0;
1694
+ const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
1695
+ promptBlockReason
1696
+ ) ? promptBlockReason : void 0;
1697
+ const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
1698
+ const rawFinishReason = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : confirmedPromptBlockReason;
1696
1699
  const content = [];
1697
- const parts = (_f = (_e = candidate == null ? void 0 : candidate.content) == null ? void 0 : _e.parts) != null ? _f : [];
1700
+ const parts = (_e = (_d = candidate == null ? void 0 : candidate.content) == null ? void 0 : _d.parts) != null ? _e : [];
1698
1701
  const usageMetadata = response.usageMetadata;
1699
1702
  let lastCodeExecutionToolCallId;
1700
1703
  let lastServerToolCallId;
1701
1704
  for (const part of parts) {
1702
- if ("executableCode" in part && ((_g = part.executableCode) == null ? void 0 : _g.code)) {
1705
+ if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
1703
1706
  const toolCallId = this.config.generateId();
1704
1707
  lastCodeExecutionToolCallId = toolCallId;
1705
1708
  content.push({
@@ -1717,7 +1720,7 @@ var GoogleGenerativeAILanguageModel = class {
1717
1720
  toolName: "code_execution",
1718
1721
  result: {
1719
1722
  outcome: part.codeExecutionResult.outcome,
1720
- output: (_h = part.codeExecutionResult.output) != null ? _h : ""
1723
+ output: (_g = part.codeExecutionResult.output) != null ? _g : ""
1721
1724
  }
1722
1725
  });
1723
1726
  } else if ("text" in part && part.text != null) {
@@ -1741,9 +1744,9 @@ var GoogleGenerativeAILanguageModel = class {
1741
1744
  } else if ("functionCall" in part && part.functionCall.name != null) {
1742
1745
  content.push({
1743
1746
  type: "tool-call",
1744
- toolCallId: (_i = part.functionCall.id) != null ? _i : this.config.generateId(),
1747
+ toolCallId: (_h = part.functionCall.id) != null ? _h : this.config.generateId(),
1745
1748
  toolName: part.functionCall.name,
1746
- input: JSON.stringify((_j = part.functionCall.args) != null ? _j : {}),
1749
+ input: JSON.stringify((_i = part.functionCall.args) != null ? _i : {}),
1747
1750
  providerMetadata: part.thoughtSignature ? {
1748
1751
  [providerOptionsName]: {
1749
1752
  thoughtSignature: part.thoughtSignature
@@ -1765,13 +1768,13 @@ var GoogleGenerativeAILanguageModel = class {
1765
1768
  } : void 0
1766
1769
  });
1767
1770
  } else if ("toolCall" in part && part.toolCall) {
1768
- const toolCallId = (_k = part.toolCall.id) != null ? _k : this.config.generateId();
1771
+ const toolCallId = (_j = part.toolCall.id) != null ? _j : this.config.generateId();
1769
1772
  lastServerToolCallId = toolCallId;
1770
1773
  content.push({
1771
1774
  type: "tool-call",
1772
1775
  toolCallId,
1773
1776
  toolName: `server:${part.toolCall.toolType}`,
1774
- input: JSON.stringify((_l = part.toolCall.args) != null ? _l : {}),
1777
+ input: JSON.stringify((_k = part.toolCall.args) != null ? _k : {}),
1775
1778
  providerExecuted: true,
1776
1779
  dynamic: true,
1777
1780
  providerMetadata: part.thoughtSignature ? {
@@ -1788,12 +1791,12 @@ var GoogleGenerativeAILanguageModel = class {
1788
1791
  }
1789
1792
  });
1790
1793
  } else if ("toolResponse" in part && part.toolResponse) {
1791
- const responseToolCallId = (_m = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _m : this.config.generateId();
1794
+ const responseToolCallId = (_l = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _l : this.config.generateId();
1792
1795
  content.push({
1793
1796
  type: "tool-result",
1794
1797
  toolCallId: responseToolCallId,
1795
1798
  toolName: `server:${part.toolResponse.toolType}`,
1796
- result: (_n = part.toolResponse.response) != null ? _n : {},
1799
+ result: (_m = part.toolResponse.response) != null ? _m : {},
1797
1800
  providerMetadata: part.thoughtSignature ? {
1798
1801
  [providerOptionsName]: {
1799
1802
  thoughtSignature: part.thoughtSignature,
@@ -1810,10 +1813,10 @@ var GoogleGenerativeAILanguageModel = class {
1810
1813
  lastServerToolCallId = void 0;
1811
1814
  }
1812
1815
  }
1813
- const sources = (_o = extractSources({
1816
+ const sources = (_n = extractSources({
1814
1817
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1815
1818
  generateId: this.config.generateId
1816
- })) != null ? _o : [];
1819
+ })) != null ? _n : [];
1817
1820
  for (const source of sources) {
1818
1821
  content.push(source);
1819
1822
  }
@@ -1833,19 +1836,19 @@ var GoogleGenerativeAILanguageModel = class {
1833
1836
  warnings,
1834
1837
  providerMetadata: {
1835
1838
  [providerOptionsName]: {
1836
- promptFeedback: (_p = response.promptFeedback) != null ? _p : null,
1837
- groundingMetadata: (_q = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _q : null,
1838
- urlContextMetadata: (_r = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _r : null,
1839
- safetyRatings: (_s = candidate == null ? void 0 : candidate.safetyRatings) != null ? _s : null,
1839
+ promptFeedback: (_o = response.promptFeedback) != null ? _o : null,
1840
+ groundingMetadata: (_p = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _p : null,
1841
+ urlContextMetadata: (_q = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _q : null,
1842
+ safetyRatings: (_r = candidate == null ? void 0 : candidate.safetyRatings) != null ? _r : null,
1840
1843
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1841
- finishMessage: (_t = candidate == null ? void 0 : candidate.finishMessage) != null ? _t : null,
1842
- serviceTier: (_u = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _u : null
1844
+ finishMessage: (_s = candidate == null ? void 0 : candidate.finishMessage) != null ? _s : null,
1845
+ serviceTier: (_t = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _t : null
1843
1846
  }
1844
1847
  },
1845
1848
  request: { body: args },
1846
1849
  response: {
1847
1850
  // TODO timestamp, model id
1848
- id: (_v = response.responseId) != null ? _v : void 0,
1851
+ id: (_u = response.responseId) != null ? _u : void 0,
1849
1852
  headers: responseHeaders,
1850
1853
  body: rawResponse
1851
1854
  }
@@ -1874,9 +1877,12 @@ var GoogleGenerativeAILanguageModel = class {
1874
1877
  raw: void 0
1875
1878
  };
1876
1879
  let usage = void 0;
1877
- let providerMetadata = void 0;
1880
+ let promptFeedback = null;
1878
1881
  let lastGroundingMetadata = null;
1879
1882
  let lastUrlContextMetadata = null;
1883
+ let lastSafetyRatings = null;
1884
+ let lastFinishMessage = null;
1885
+ let confirmedPromptBlockReason;
1880
1886
  const generateId2 = this.config.generateId;
1881
1887
  let hasToolCalls = false;
1882
1888
  let hasEmittedResponseMetadata = false;
@@ -1922,7 +1928,7 @@ var GoogleGenerativeAILanguageModel = class {
1922
1928
  controller.enqueue({ type: "stream-start", warnings });
1923
1929
  },
1924
1930
  transform(chunk, controller) {
1925
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v;
1931
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1926
1932
  if (options.includeRawChunks) {
1927
1933
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
1928
1934
  }
@@ -1942,35 +1948,35 @@ var GoogleGenerativeAILanguageModel = class {
1942
1948
  if (usageMetadata != null) {
1943
1949
  usage = usageMetadata;
1944
1950
  }
1945
- const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
1946
- if (candidate == null) {
1947
- const promptBlockReason2 = (_b = value.promptFeedback) == null ? void 0 : _b.blockReason;
1948
- if (promptBlockReason2 != null) {
1951
+ if (value.promptFeedback != null && confirmedPromptBlockReason == null) {
1952
+ promptFeedback = value.promptFeedback;
1953
+ if (isConfirmedPromptBlockReason(value.promptFeedback.blockReason)) {
1954
+ confirmedPromptBlockReason = value.promptFeedback.blockReason;
1949
1955
  finishReason = {
1950
1956
  unified: "content-filter",
1951
- raw: promptBlockReason2
1952
- };
1953
- providerMetadata = {
1954
- [providerOptionsName]: {
1955
- promptFeedback: (_c = value.promptFeedback) != null ? _c : null,
1956
- groundingMetadata: lastGroundingMetadata,
1957
- urlContextMetadata: lastUrlContextMetadata,
1958
- safetyRatings: null,
1959
- usageMetadata: usageMetadata != null ? usageMetadata : null,
1960
- finishMessage: null,
1961
- serviceTier: (_d = usage == null ? void 0 : usage.serviceTier) != null ? _d : null
1962
- }
1957
+ raw: confirmedPromptBlockReason
1963
1958
  };
1964
1959
  }
1965
- return;
1966
1960
  }
1967
- const content = candidate.content;
1968
- if (candidate.groundingMetadata != null) {
1969
- lastGroundingMetadata = candidate.groundingMetadata;
1961
+ const candidate = (_a = value.candidates) == null ? void 0 : _a[0];
1962
+ if (candidate != null) {
1963
+ if (candidate.groundingMetadata != null) {
1964
+ lastGroundingMetadata = candidate.groundingMetadata;
1965
+ }
1966
+ if (candidate.urlContextMetadata != null) {
1967
+ lastUrlContextMetadata = candidate.urlContextMetadata;
1968
+ }
1969
+ if (candidate.safetyRatings != null) {
1970
+ lastSafetyRatings = candidate.safetyRatings;
1971
+ }
1972
+ if (candidate.finishMessage != null) {
1973
+ lastFinishMessage = candidate.finishMessage;
1974
+ }
1970
1975
  }
1971
- if (candidate.urlContextMetadata != null) {
1972
- lastUrlContextMetadata = candidate.urlContextMetadata;
1976
+ if (confirmedPromptBlockReason != null || candidate == null) {
1977
+ return;
1973
1978
  }
1979
+ const content = candidate.content;
1974
1980
  const sources = extractSources({
1975
1981
  groundingMetadata: candidate.groundingMetadata,
1976
1982
  generateId: generateId2
@@ -1984,9 +1990,9 @@ var GoogleGenerativeAILanguageModel = class {
1984
1990
  }
1985
1991
  }
1986
1992
  if (content != null) {
1987
- const parts = (_e = content.parts) != null ? _e : [];
1993
+ const parts = (_b = content.parts) != null ? _b : [];
1988
1994
  for (const part of parts) {
1989
- if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
1995
+ if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
1990
1996
  const toolCallId = generateId2();
1991
1997
  lastCodeExecutionToolCallId = toolCallId;
1992
1998
  controller.enqueue({
@@ -2005,7 +2011,7 @@ var GoogleGenerativeAILanguageModel = class {
2005
2011
  toolName: "code_execution",
2006
2012
  result: {
2007
2013
  outcome: part.codeExecutionResult.outcome,
2008
- output: (_g = part.codeExecutionResult.output) != null ? _g : ""
2014
+ output: (_d = part.codeExecutionResult.output) != null ? _d : ""
2009
2015
  }
2010
2016
  });
2011
2017
  }
@@ -2099,7 +2105,7 @@ var GoogleGenerativeAILanguageModel = class {
2099
2105
  providerMetadata: fileMeta
2100
2106
  });
2101
2107
  } else if ("toolCall" in part && part.toolCall) {
2102
- const toolCallId = (_h = part.toolCall.id) != null ? _h : generateId2();
2108
+ const toolCallId = (_e = part.toolCall.id) != null ? _e : generateId2();
2103
2109
  lastServerToolCallId = toolCallId;
2104
2110
  const serverMeta = {
2105
2111
  [providerOptionsName]: {
@@ -2112,13 +2118,13 @@ var GoogleGenerativeAILanguageModel = class {
2112
2118
  type: "tool-call",
2113
2119
  toolCallId,
2114
2120
  toolName: `server:${part.toolCall.toolType}`,
2115
- input: JSON.stringify((_i = part.toolCall.args) != null ? _i : {}),
2121
+ input: JSON.stringify((_f = part.toolCall.args) != null ? _f : {}),
2116
2122
  providerExecuted: true,
2117
2123
  dynamic: true,
2118
2124
  providerMetadata: serverMeta
2119
2125
  });
2120
2126
  } else if ("toolResponse" in part && part.toolResponse) {
2121
- const responseToolCallId = (_j = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _j : generateId2();
2127
+ const responseToolCallId = (_g = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _g : generateId2();
2122
2128
  const serverMeta = {
2123
2129
  [providerOptionsName]: {
2124
2130
  ...part.thoughtSignature ? { thoughtSignature: part.thoughtSignature } : {},
@@ -2130,7 +2136,7 @@ var GoogleGenerativeAILanguageModel = class {
2130
2136
  type: "tool-result",
2131
2137
  toolCallId: responseToolCallId,
2132
2138
  toolName: `server:${part.toolResponse.toolType}`,
2133
- result: (_k = part.toolResponse.response) != null ? _k : {},
2139
+ result: (_h = part.toolResponse.response) != null ? _h : {},
2134
2140
  providerMetadata: serverMeta
2135
2141
  });
2136
2142
  lastServerToolCallId = void 0;
@@ -2149,7 +2155,7 @@ var GoogleGenerativeAILanguageModel = class {
2149
2155
  const isNoArgsCompleteCall = part.functionCall.name != null && part.functionCall.args == null && part.functionCall.partialArgs == null && part.functionCall.willContinue !== true;
2150
2156
  if (isStreamingChunk) {
2151
2157
  if (part.functionCall.name != null) {
2152
- const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId2();
2158
+ const toolCallId = (_i = part.functionCall.id) != null ? _i : generateId2();
2153
2159
  const accumulator = new GoogleJSONAccumulator();
2154
2160
  activeStreamingToolCalls.push({
2155
2161
  toolCallId,
@@ -2197,9 +2203,9 @@ var GoogleGenerativeAILanguageModel = class {
2197
2203
  } else if (isTerminalChunk && activeStreamingToolCalls.length > 0) {
2198
2204
  finishActiveStreamingToolCall(controller);
2199
2205
  } else if (isCompleteCall) {
2200
- const toolCallId = (_m = part.functionCall.id) != null ? _m : generateId2();
2206
+ const toolCallId = (_j = part.functionCall.id) != null ? _j : generateId2();
2201
2207
  const toolName = part.functionCall.name;
2202
- const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_n = part.functionCall.args) != null ? _n : {});
2208
+ const args2 = typeof part.functionCall.args === "string" ? part.functionCall.args : JSON.stringify((_k = part.functionCall.args) != null ? _k : {});
2203
2209
  controller.enqueue({
2204
2210
  type: "tool-input-start",
2205
2211
  id: toolCallId,
@@ -2226,7 +2232,7 @@ var GoogleGenerativeAILanguageModel = class {
2226
2232
  });
2227
2233
  hasToolCalls = true;
2228
2234
  } else if (isNoArgsCompleteCall) {
2229
- const toolCallId = (_o = part.functionCall.id) != null ? _o : generateId2();
2235
+ const toolCallId = (_l = part.functionCall.id) != null ? _l : generateId2();
2230
2236
  const toolName = part.functionCall.name;
2231
2237
  controller.enqueue({
2232
2238
  type: "tool-input-start",
@@ -2250,31 +2256,18 @@ var GoogleGenerativeAILanguageModel = class {
2250
2256
  }
2251
2257
  }
2252
2258
  }
2253
- const promptBlockReason = (_p = value.promptFeedback) == null ? void 0 : _p.blockReason;
2254
- const isPromptBlocked = candidate.finishReason == null && promptBlockReason != null;
2255
- const rawFinishReason = (_r = (_q = candidate.finishReason) != null ? _q : promptBlockReason) != null ? _r : void 0;
2256
- if (rawFinishReason != null) {
2259
+ if (candidate.finishReason != null) {
2257
2260
  finishReason = {
2258
- unified: isPromptBlocked ? "content-filter" : mapGoogleGenerativeAIFinishReason({
2259
- finishReason: rawFinishReason,
2261
+ unified: mapGoogleGenerativeAIFinishReason({
2262
+ finishReason: candidate.finishReason,
2260
2263
  hasToolCalls
2261
2264
  }),
2262
- raw: rawFinishReason
2263
- };
2264
- providerMetadata = {
2265
- [providerOptionsName]: {
2266
- promptFeedback: (_s = value.promptFeedback) != null ? _s : null,
2267
- groundingMetadata: lastGroundingMetadata,
2268
- urlContextMetadata: lastUrlContextMetadata,
2269
- safetyRatings: (_t = candidate.safetyRatings) != null ? _t : null,
2270
- usageMetadata: usageMetadata != null ? usageMetadata : null,
2271
- finishMessage: (_u = candidate.finishMessage) != null ? _u : null,
2272
- serviceTier: (_v = usage == null ? void 0 : usage.serviceTier) != null ? _v : null
2273
- }
2265
+ raw: candidate.finishReason
2274
2266
  };
2275
2267
  }
2276
2268
  },
2277
2269
  flush(controller) {
2270
+ var _a;
2278
2271
  if (currentTextBlockId !== null) {
2279
2272
  controller.enqueue({
2280
2273
  type: "text-end",
@@ -2291,7 +2284,17 @@ var GoogleGenerativeAILanguageModel = class {
2291
2284
  type: "finish",
2292
2285
  finishReason,
2293
2286
  usage: convertGoogleGenerativeAIUsage(usage),
2294
- providerMetadata
2287
+ providerMetadata: {
2288
+ [providerOptionsName]: {
2289
+ promptFeedback,
2290
+ groundingMetadata: lastGroundingMetadata,
2291
+ urlContextMetadata: lastUrlContextMetadata,
2292
+ safetyRatings: lastSafetyRatings,
2293
+ usageMetadata: usage != null ? usage : null,
2294
+ finishMessage: lastFinishMessage,
2295
+ serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null
2296
+ }
2297
+ }
2295
2298
  });
2296
2299
  }
2297
2300
  })
@@ -2588,6 +2591,9 @@ var chunkSchema = lazySchema3(
2588
2591
  })
2589
2592
  )
2590
2593
  );
2594
+ function isConfirmedPromptBlockReason(blockReason) {
2595
+ return blockReason != null && blockReason !== "" && blockReason !== "BLOCK_REASON_UNSPECIFIED" && blockReason !== "BLOCKED_REASON_UNSPECIFIED";
2596
+ }
2591
2597
 
2592
2598
  // src/tool/code-execution.ts
2593
2599
  import { createProviderToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";