@ai-sdk/google 2.0.103 → 2.0.104

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,