@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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ai-sdk/google
2
2
 
3
+ ## 2.0.104
4
+
5
+ ### Patch Changes
6
+
7
+ - 1275533: Preserve grounding metadata from each automatically continued Google response in `groundingMetadataPerRequest`. Previously, a later response without grounding could leave earlier Search queries absent from the final provider metadata even though their sources were returned. The new array preserves request order and includes `null` for requests without grounding.
8
+
3
9
  ## 2.0.103
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -183,6 +183,8 @@ type GoogleGenerativeAIUrlContextMetadata = UrlContextMetadataSchema;
183
183
  type GoogleGenerativeAISafetyRating = SafetyRatingSchema;
184
184
  interface GoogleGenerativeAIProviderMetadata {
185
185
  groundingMetadata: GoogleGenerativeAIGroundingMetadata | null;
186
+ /** Grounding from each automatically continued request, in request order. */
187
+ groundingMetadataPerRequest?: Array<GoogleGenerativeAIGroundingMetadata | null>;
186
188
  urlContextMetadata: GoogleGenerativeAIUrlContextMetadata | null;
187
189
  safetyRatings: GoogleGenerativeAISafetyRating[] | null;
188
190
  serviceTier: string | null;
package/dist/index.d.ts CHANGED
@@ -183,6 +183,8 @@ type GoogleGenerativeAIUrlContextMetadata = UrlContextMetadataSchema;
183
183
  type GoogleGenerativeAISafetyRating = SafetyRatingSchema;
184
184
  interface GoogleGenerativeAIProviderMetadata {
185
185
  groundingMetadata: GoogleGenerativeAIGroundingMetadata | null;
186
+ /** Grounding from each automatically continued request, in request order. */
187
+ groundingMetadataPerRequest?: Array<GoogleGenerativeAIGroundingMetadata | null>;
186
188
  urlContextMetadata: GoogleGenerativeAIUrlContextMetadata | null;
187
189
  safetyRatings: GoogleGenerativeAISafetyRating[] | null;
188
190
  serviceTier: string | null;
package/dist/index.js CHANGED
@@ -30,7 +30,7 @@ module.exports = __toCommonJS(index_exports);
30
30
  var import_provider_utils15 = require("@ai-sdk/provider-utils");
31
31
 
32
32
  // src/version.ts
33
- var VERSION = true ? "2.0.103" : "0.0.0-test";
33
+ var VERSION = true ? "2.0.104" : "0.0.0-test";
34
34
 
35
35
  // src/google-generative-ai-embedding-model.ts
36
36
  var import_provider = require("@ai-sdk/provider");
@@ -1484,7 +1484,7 @@ var GoogleGenerativeAILanguageModel = class {
1484
1484
  };
1485
1485
  }
1486
1486
  async doGenerate(options) {
1487
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
1487
+ 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;
1488
1488
  const { args, warnings } = await this.getArgs(options);
1489
1489
  const body = JSON.stringify(args);
1490
1490
  const mergedHeaders = (0, import_provider_utils6.combineHeaders)(
@@ -1508,18 +1508,20 @@ var GoogleGenerativeAILanguageModel = class {
1508
1508
  rawValue: rawResponse
1509
1509
  } = await generateContent(args);
1510
1510
  const continuedParts = [];
1511
+ const groundingMetadataPerRequest = [];
1511
1512
  let continuedUsageMetadata;
1512
1513
  const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
1513
1514
  while (true) {
1514
1515
  const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
1516
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
1515
1517
  const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
1516
1518
  if (continuationToken == null) {
1517
1519
  break;
1518
1520
  }
1519
- continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
1521
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
1520
1522
  continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1521
1523
  continuedUsageMetadata,
1522
- (_e = response.usageMetadata) != null ? _e : void 0
1524
+ (_f = response.usageMetadata) != null ? _f : void 0
1523
1525
  );
1524
1526
  ({
1525
1527
  responseHeaders,
@@ -1527,18 +1529,18 @@ var GoogleGenerativeAILanguageModel = class {
1527
1529
  rawValue: rawResponse
1528
1530
  } = await generateContent({ ...args, continuationToken }));
1529
1531
  }
1530
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
1531
- const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
1532
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
1533
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
1532
1534
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
1533
1535
  const content = [];
1534
- const parts = [...continuedParts, ...(_i = (_h = candidate == null ? void 0 : candidate.content) == null ? void 0 : _h.parts) != null ? _i : []];
1536
+ const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
1535
1537
  const usageMetadata = addGoogleGenerativeAIUsageMetadata(
1536
1538
  continuedUsageMetadata,
1537
- (_j = response.usageMetadata) != null ? _j : void 0
1539
+ (_k = response.usageMetadata) != null ? _k : void 0
1538
1540
  );
1539
1541
  let lastCodeExecutionToolCallId;
1540
1542
  for (const part of parts) {
1541
- if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1543
+ if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
1542
1544
  const toolCallId = this.config.generateId();
1543
1545
  lastCodeExecutionToolCallId = toolCallId;
1544
1546
  content.push({
@@ -1579,7 +1581,7 @@ var GoogleGenerativeAILanguageModel = class {
1579
1581
  } else if ("functionCall" in part) {
1580
1582
  content.push({
1581
1583
  type: "tool-call",
1582
- toolCallId: (_l = part.functionCall.id) != null ? _l : this.config.generateId(),
1584
+ toolCallId: (_m = part.functionCall.id) != null ? _m : this.config.generateId(),
1583
1585
  toolName: part.functionCall.name,
1584
1586
  input: JSON.stringify(part.functionCall.args),
1585
1587
  providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
@@ -1592,10 +1594,10 @@ var GoogleGenerativeAILanguageModel = class {
1592
1594
  });
1593
1595
  }
1594
1596
  }
1595
- const sources = (_m = extractSources({
1597
+ const sources = (_n = extractSources({
1596
1598
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1597
1599
  generateId: this.config.generateId
1598
- })) != null ? _m : [];
1600
+ })) != null ? _n : [];
1599
1601
  for (const source of sources) {
1600
1602
  content.push(source);
1601
1603
  }
@@ -1606,21 +1608,22 @@ var GoogleGenerativeAILanguageModel = class {
1606
1608
  hasToolCalls: content.some((part) => part.type === "tool-call")
1607
1609
  }),
1608
1610
  usage: {
1609
- inputTokens: (_n = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _n : void 0,
1610
- outputTokens: (_o = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _o : void 0,
1611
- totalTokens: (_p = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _p : void 0,
1612
- reasoningTokens: (_q = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _q : void 0,
1613
- cachedInputTokens: (_r = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _r : void 0
1611
+ inputTokens: (_o = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _o : void 0,
1612
+ outputTokens: (_p = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _p : void 0,
1613
+ totalTokens: (_q = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _q : void 0,
1614
+ reasoningTokens: (_r = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _r : void 0,
1615
+ cachedInputTokens: (_s = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _s : void 0
1614
1616
  },
1615
1617
  warnings,
1616
1618
  providerMetadata: {
1617
1619
  google: {
1618
- promptFeedback: (_s = response.promptFeedback) != null ? _s : null,
1619
- groundingMetadata: (_t = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _t : null,
1620
- urlContextMetadata: (_u = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _u : null,
1621
- safetyRatings: (_v = candidate == null ? void 0 : candidate.safetyRatings) != null ? _v : null,
1622
- serviceTier: (_w = response.serviceTier) != null ? _w : null,
1623
- ...(candidate == null ? void 0 : candidate.continuationToken) != null ? { continuationToken: candidate.continuationToken } : {},
1620
+ promptFeedback: (_t = response.promptFeedback) != null ? _t : null,
1621
+ groundingMetadata: (_u = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _u : null,
1622
+ ...groundingMetadataPerRequest.length > 1 ? { groundingMetadataPerRequest } : {},
1623
+ urlContextMetadata: (_v = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _v : null,
1624
+ safetyRatings: (_w = candidate == null ? void 0 : candidate.safetyRatings) != null ? _w : null,
1625
+ serviceTier: (_x = response.serviceTier) != null ? _x : null,
1626
+ ...(candidate == null ? void 0 : candidate.continuationToken) != null && candidate.finishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
1624
1627
  ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {},
1625
1628
  usageMetadata: usageMetadata != null ? usageMetadata : null
1626
1629
  }
@@ -1652,14 +1655,27 @@ var GoogleGenerativeAILanguageModel = class {
1652
1655
  fetch: this.config.fetch
1653
1656
  });
1654
1657
  const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
1658
+ const groundingMetadataPerRequest = [];
1659
+ let requestGroundingMetadata = null;
1655
1660
  const response = continueGoogleStream({
1656
1661
  stream: firstResponse,
1657
1662
  getContinuationToken: (chunk) => {
1658
1663
  var _a, _b;
1659
1664
  const candidate = (_a = chunk.candidates) == null ? void 0 : _a[0];
1665
+ if ((candidate == null ? void 0 : candidate.groundingMetadata) != null) {
1666
+ requestGroundingMetadata = candidate.groundingMetadata;
1667
+ }
1660
1668
  return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
1661
1669
  },
1662
- continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
1670
+ continueStream: async (continuationToken) => {
1671
+ const { value } = await streamGenerateContent({
1672
+ ...args,
1673
+ continuationToken
1674
+ });
1675
+ groundingMetadataPerRequest.push(requestGroundingMetadata);
1676
+ requestGroundingMetadata = null;
1677
+ return value;
1678
+ }
1663
1679
  });
1664
1680
  let finishReason = "unknown";
1665
1681
  const usage = {
@@ -1937,6 +1953,18 @@ var GoogleGenerativeAILanguageModel = class {
1937
1953
  id: currentReasoningBlockId
1938
1954
  });
1939
1955
  }
1956
+ if (groundingMetadataPerRequest.length > 0) {
1957
+ providerMetadata = {
1958
+ ...providerMetadata,
1959
+ google: {
1960
+ ...providerMetadata == null ? void 0 : providerMetadata.google,
1961
+ groundingMetadataPerRequest: [
1962
+ ...groundingMetadataPerRequest,
1963
+ requestGroundingMetadata
1964
+ ]
1965
+ }
1966
+ };
1967
+ }
1940
1968
  controller.enqueue({
1941
1969
  type: "finish",
1942
1970
  finishReason,