@ai-sdk/google 2.0.103 → 2.0.105

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.
@@ -1294,7 +1294,7 @@ var GoogleGenerativeAILanguageModel = class {
1294
1294
  };
1295
1295
  }
1296
1296
  async doGenerate(options) {
1297
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
1297
+ 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;
1298
1298
  const { args, warnings } = await this.getArgs(options);
1299
1299
  const body = JSON.stringify(args);
1300
1300
  const mergedHeaders = (0, import_provider_utils4.combineHeaders)(
@@ -1318,18 +1318,20 @@ var GoogleGenerativeAILanguageModel = class {
1318
1318
  rawValue: rawResponse
1319
1319
  } = await generateContent(args);
1320
1320
  const continuedParts = [];
1321
+ const groundingMetadataPerRequest = [];
1321
1322
  let continuedUsageMetadata;
1322
1323
  const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
1323
1324
  while (true) {
1324
1325
  const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
1326
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
1325
1327
  const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
1326
1328
  if (continuationToken == null) {
1327
1329
  break;
1328
1330
  }
1329
- continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
1331
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
1330
1332
  continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1331
1333
  continuedUsageMetadata,
1332
- (_e = response.usageMetadata) != null ? _e : void 0
1334
+ (_f = response.usageMetadata) != null ? _f : void 0
1333
1335
  );
1334
1336
  ({
1335
1337
  responseHeaders,
@@ -1337,18 +1339,18 @@ var GoogleGenerativeAILanguageModel = class {
1337
1339
  rawValue: rawResponse
1338
1340
  } = await generateContent({ ...args, continuationToken }));
1339
1341
  }
1340
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
1341
- const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
1342
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
1343
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
1342
1344
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
1343
1345
  const content = [];
1344
- const parts = [...continuedParts, ...(_i = (_h = candidate == null ? void 0 : candidate.content) == null ? void 0 : _h.parts) != null ? _i : []];
1346
+ const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
1345
1347
  const usageMetadata = addGoogleGenerativeAIUsageMetadata(
1346
1348
  continuedUsageMetadata,
1347
- (_j = response.usageMetadata) != null ? _j : void 0
1349
+ (_k = response.usageMetadata) != null ? _k : void 0
1348
1350
  );
1349
1351
  let lastCodeExecutionToolCallId;
1350
1352
  for (const part of parts) {
1351
- if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1353
+ if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
1352
1354
  const toolCallId = this.config.generateId();
1353
1355
  lastCodeExecutionToolCallId = toolCallId;
1354
1356
  content.push({
@@ -1389,7 +1391,7 @@ var GoogleGenerativeAILanguageModel = class {
1389
1391
  } else if ("functionCall" in part) {
1390
1392
  content.push({
1391
1393
  type: "tool-call",
1392
- toolCallId: (_l = part.functionCall.id) != null ? _l : this.config.generateId(),
1394
+ toolCallId: (_m = part.functionCall.id) != null ? _m : this.config.generateId(),
1393
1395
  toolName: part.functionCall.name,
1394
1396
  input: JSON.stringify(part.functionCall.args),
1395
1397
  providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
@@ -1402,10 +1404,10 @@ var GoogleGenerativeAILanguageModel = class {
1402
1404
  });
1403
1405
  }
1404
1406
  }
1405
- const sources = (_m = extractSources({
1407
+ const sources = (_n = extractSources({
1406
1408
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1407
1409
  generateId: this.config.generateId
1408
- })) != null ? _m : [];
1410
+ })) != null ? _n : [];
1409
1411
  for (const source of sources) {
1410
1412
  content.push(source);
1411
1413
  }
@@ -1416,21 +1418,22 @@ var GoogleGenerativeAILanguageModel = class {
1416
1418
  hasToolCalls: content.some((part) => part.type === "tool-call")
1417
1419
  }),
1418
1420
  usage: {
1419
- inputTokens: (_n = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _n : void 0,
1420
- outputTokens: (_o = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _o : void 0,
1421
- totalTokens: (_p = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _p : void 0,
1422
- reasoningTokens: (_q = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _q : void 0,
1423
- cachedInputTokens: (_r = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _r : void 0
1421
+ inputTokens: (_o = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _o : void 0,
1422
+ outputTokens: (_p = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _p : void 0,
1423
+ totalTokens: (_q = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _q : void 0,
1424
+ reasoningTokens: (_r = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _r : void 0,
1425
+ cachedInputTokens: (_s = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _s : void 0
1424
1426
  },
1425
1427
  warnings,
1426
1428
  providerMetadata: {
1427
1429
  google: {
1428
- promptFeedback: (_s = response.promptFeedback) != null ? _s : null,
1429
- groundingMetadata: (_t = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _t : null,
1430
- urlContextMetadata: (_u = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _u : null,
1431
- safetyRatings: (_v = candidate == null ? void 0 : candidate.safetyRatings) != null ? _v : null,
1432
- serviceTier: (_w = response.serviceTier) != null ? _w : null,
1433
- ...(candidate == null ? void 0 : candidate.continuationToken) != null ? { continuationToken: candidate.continuationToken } : {},
1430
+ promptFeedback: (_t = response.promptFeedback) != null ? _t : null,
1431
+ groundingMetadata: (_u = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _u : null,
1432
+ ...groundingMetadataPerRequest.length > 1 ? { groundingMetadataPerRequest } : {},
1433
+ urlContextMetadata: (_v = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _v : null,
1434
+ safetyRatings: (_w = candidate == null ? void 0 : candidate.safetyRatings) != null ? _w : null,
1435
+ serviceTier: (_x = response.serviceTier) != null ? _x : null,
1436
+ ...(candidate == null ? void 0 : candidate.continuationToken) != null && candidate.finishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
1434
1437
  ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {},
1435
1438
  usageMetadata: usageMetadata != null ? usageMetadata : null
1436
1439
  }
@@ -1462,14 +1465,27 @@ var GoogleGenerativeAILanguageModel = class {
1462
1465
  fetch: this.config.fetch
1463
1466
  });
1464
1467
  const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
1468
+ const groundingMetadataPerRequest = [];
1469
+ let requestGroundingMetadata = null;
1465
1470
  const response = continueGoogleStream({
1466
1471
  stream: firstResponse,
1467
1472
  getContinuationToken: (chunk) => {
1468
1473
  var _a, _b;
1469
1474
  const candidate = (_a = chunk.candidates) == null ? void 0 : _a[0];
1475
+ if ((candidate == null ? void 0 : candidate.groundingMetadata) != null) {
1476
+ requestGroundingMetadata = candidate.groundingMetadata;
1477
+ }
1470
1478
  return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
1471
1479
  },
1472
- continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
1480
+ continueStream: async (continuationToken) => {
1481
+ const { value } = await streamGenerateContent({
1482
+ ...args,
1483
+ continuationToken
1484
+ });
1485
+ groundingMetadataPerRequest.push(requestGroundingMetadata);
1486
+ requestGroundingMetadata = null;
1487
+ return value;
1488
+ }
1473
1489
  });
1474
1490
  let finishReason = "unknown";
1475
1491
  const usage = {
@@ -1747,6 +1763,18 @@ var GoogleGenerativeAILanguageModel = class {
1747
1763
  id: currentReasoningBlockId
1748
1764
  });
1749
1765
  }
1766
+ if (groundingMetadataPerRequest.length > 0) {
1767
+ providerMetadata = {
1768
+ ...providerMetadata,
1769
+ google: {
1770
+ ...providerMetadata == null ? void 0 : providerMetadata.google,
1771
+ groundingMetadataPerRequest: [
1772
+ ...groundingMetadataPerRequest,
1773
+ requestGroundingMetadata
1774
+ ]
1775
+ }
1776
+ };
1777
+ }
1750
1778
  controller.enqueue({
1751
1779
  type: "finish",
1752
1780
  finishReason,
@@ -1964,13 +1992,13 @@ var getSafetyRatingSchema = () => import_v43.z.object({
1964
1992
  severityScore: import_v43.z.number().nullish(),
1965
1993
  blocked: import_v43.z.boolean().nullish()
1966
1994
  });
1967
- var tokenDetailsSchema = import_v43.z.array(
1995
+ var tokenDetailsSchema = () => import_v43.z.array(
1968
1996
  import_v43.z.object({
1969
1997
  modality: import_v43.z.string(),
1970
1998
  tokenCount: import_v43.z.number()
1971
1999
  })
1972
2000
  ).nullish();
1973
- var usageSchema = import_v43.z.object({
2001
+ var usageSchema = () => import_v43.z.object({
1974
2002
  cachedContentTokenCount: import_v43.z.number().nullish(),
1975
2003
  thoughtsTokenCount: import_v43.z.number().nullish(),
1976
2004
  promptTokenCount: import_v43.z.number().nullish(),
@@ -1979,8 +2007,8 @@ var usageSchema = import_v43.z.object({
1979
2007
  // https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/GenerateContentResponse#TrafficType
1980
2008
  trafficType: import_v43.z.string().nullish(),
1981
2009
  // https://ai.google.dev/api/generate-content#Modality
1982
- promptTokensDetails: tokenDetailsSchema,
1983
- candidatesTokensDetails: tokenDetailsSchema
2010
+ promptTokensDetails: tokenDetailsSchema(),
2011
+ candidatesTokensDetails: tokenDetailsSchema()
1984
2012
  });
1985
2013
  var getUrlContextMetadataSchema = () => import_v43.z.object({
1986
2014
  urlMetadata: import_v43.z.array(
@@ -2003,7 +2031,7 @@ var responseSchema = (0, import_provider_utils4.lazySchema)(
2003
2031
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
2004
2032
  })
2005
2033
  ).nullish(),
2006
- usageMetadata: usageSchema.nullish(),
2034
+ usageMetadata: usageSchema().nullish(),
2007
2035
  promptFeedback: import_v43.z.object({
2008
2036
  blockReason: import_v43.z.string().nullish(),
2009
2037
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish()
@@ -2025,7 +2053,7 @@ var chunkSchema = (0, import_provider_utils4.lazySchema)(
2025
2053
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
2026
2054
  })
2027
2055
  ).nullish(),
2028
- usageMetadata: usageSchema.nullish(),
2056
+ usageMetadata: usageSchema().nullish(),
2029
2057
  promptFeedback: import_v43.z.object({
2030
2058
  blockReason: import_v43.z.string().nullish(),
2031
2059
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish()
@@ -2063,7 +2091,7 @@ var enterpriseWebSearch = (0, import_provider_utils6.createProviderDefinedToolFa
2063
2091
  // src/tool/file-search.ts
2064
2092
  var import_provider_utils7 = require("@ai-sdk/provider-utils");
2065
2093
  var import_v46 = require("zod/v4");
2066
- var fileSearchArgsBaseSchema = import_v46.z.object({
2094
+ var fileSearchArgsBaseSchema = () => import_v46.z.object({
2067
2095
  /** The names of the file_search_stores to retrieve from.
2068
2096
  * Example: `fileSearchStores/my-file-search-store-123`
2069
2097
  */
@@ -2080,7 +2108,7 @@ var fileSearchArgsBaseSchema = import_v46.z.object({
2080
2108
  ).optional()
2081
2109
  }).passthrough();
2082
2110
  var fileSearchArgsSchema = (0, import_provider_utils7.lazySchema)(
2083
- () => (0, import_provider_utils7.zodSchema)(fileSearchArgsBaseSchema)
2111
+ () => (0, import_provider_utils7.zodSchema)(fileSearchArgsBaseSchema())
2084
2112
  );
2085
2113
  var fileSearch = (0, import_provider_utils7.createProviderDefinedToolFactory)({
2086
2114
  id: "google.file_search",
@@ -2100,7 +2128,7 @@ var googleMaps = (0, import_provider_utils8.createProviderDefinedToolFactory)({
2100
2128
  // src/tool/google-search.ts
2101
2129
  var import_provider_utils9 = require("@ai-sdk/provider-utils");
2102
2130
  var import_v48 = require("zod/v4");
2103
- var googleSearchToolArgsBaseSchema = import_v48.z.object({
2131
+ var googleSearchToolArgsBaseSchema = () => import_v48.z.object({
2104
2132
  searchTypes: import_v48.z.object({
2105
2133
  webSearch: import_v48.z.object({}).optional(),
2106
2134
  imageSearch: import_v48.z.object({}).optional()
@@ -2111,7 +2139,7 @@ var googleSearchToolArgsBaseSchema = import_v48.z.object({
2111
2139
  }).optional()
2112
2140
  }).passthrough();
2113
2141
  var googleSearchToolArgsSchema = (0, import_provider_utils9.lazySchema)(
2114
- () => (0, import_provider_utils9.zodSchema)(googleSearchToolArgsBaseSchema)
2142
+ () => (0, import_provider_utils9.zodSchema)(googleSearchToolArgsBaseSchema())
2115
2143
  );
2116
2144
  var googleSearch = (0, import_provider_utils9.createProviderDefinedToolFactory)({
2117
2145
  id: "google.google_search",