@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/dist/index.mjs CHANGED
@@ -7,7 +7,7 @@ import {
7
7
  } from "@ai-sdk/provider-utils";
8
8
 
9
9
  // src/version.ts
10
- var VERSION = true ? "2.0.103" : "0.0.0-test";
10
+ var VERSION = true ? "2.0.104" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -1497,7 +1497,7 @@ var GoogleGenerativeAILanguageModel = class {
1497
1497
  };
1498
1498
  }
1499
1499
  async doGenerate(options) {
1500
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u, _v, _w;
1500
+ 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;
1501
1501
  const { args, warnings } = await this.getArgs(options);
1502
1502
  const body = JSON.stringify(args);
1503
1503
  const mergedHeaders = combineHeaders2(
@@ -1521,18 +1521,20 @@ var GoogleGenerativeAILanguageModel = class {
1521
1521
  rawValue: rawResponse
1522
1522
  } = await generateContent(args);
1523
1523
  const continuedParts = [];
1524
+ const groundingMetadataPerRequest = [];
1524
1525
  let continuedUsageMetadata;
1525
1526
  const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
1526
1527
  while (true) {
1527
1528
  const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
1529
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
1528
1530
  const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
1529
1531
  if (continuationToken == null) {
1530
1532
  break;
1531
1533
  }
1532
- continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
1534
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
1533
1535
  continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1534
1536
  continuedUsageMetadata,
1535
- (_e = response.usageMetadata) != null ? _e : void 0
1537
+ (_f = response.usageMetadata) != null ? _f : void 0
1536
1538
  );
1537
1539
  ({
1538
1540
  responseHeaders,
@@ -1540,18 +1542,18 @@ var GoogleGenerativeAILanguageModel = class {
1540
1542
  rawValue: rawResponse
1541
1543
  } = await generateContent({ ...args, continuationToken }));
1542
1544
  }
1543
- const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
1544
- const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
1545
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
1546
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
1545
1547
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
1546
1548
  const content = [];
1547
- const parts = [...continuedParts, ...(_i = (_h = candidate == null ? void 0 : candidate.content) == null ? void 0 : _h.parts) != null ? _i : []];
1549
+ const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
1548
1550
  const usageMetadata = addGoogleGenerativeAIUsageMetadata(
1549
1551
  continuedUsageMetadata,
1550
- (_j = response.usageMetadata) != null ? _j : void 0
1552
+ (_k = response.usageMetadata) != null ? _k : void 0
1551
1553
  );
1552
1554
  let lastCodeExecutionToolCallId;
1553
1555
  for (const part of parts) {
1554
- if ("executableCode" in part && ((_k = part.executableCode) == null ? void 0 : _k.code)) {
1556
+ if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
1555
1557
  const toolCallId = this.config.generateId();
1556
1558
  lastCodeExecutionToolCallId = toolCallId;
1557
1559
  content.push({
@@ -1592,7 +1594,7 @@ var GoogleGenerativeAILanguageModel = class {
1592
1594
  } else if ("functionCall" in part) {
1593
1595
  content.push({
1594
1596
  type: "tool-call",
1595
- toolCallId: (_l = part.functionCall.id) != null ? _l : this.config.generateId(),
1597
+ toolCallId: (_m = part.functionCall.id) != null ? _m : this.config.generateId(),
1596
1598
  toolName: part.functionCall.name,
1597
1599
  input: JSON.stringify(part.functionCall.args),
1598
1600
  providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
@@ -1605,10 +1607,10 @@ var GoogleGenerativeAILanguageModel = class {
1605
1607
  });
1606
1608
  }
1607
1609
  }
1608
- const sources = (_m = extractSources({
1610
+ const sources = (_n = extractSources({
1609
1611
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1610
1612
  generateId: this.config.generateId
1611
- })) != null ? _m : [];
1613
+ })) != null ? _n : [];
1612
1614
  for (const source of sources) {
1613
1615
  content.push(source);
1614
1616
  }
@@ -1619,21 +1621,22 @@ var GoogleGenerativeAILanguageModel = class {
1619
1621
  hasToolCalls: content.some((part) => part.type === "tool-call")
1620
1622
  }),
1621
1623
  usage: {
1622
- inputTokens: (_n = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _n : void 0,
1623
- outputTokens: (_o = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _o : void 0,
1624
- totalTokens: (_p = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _p : void 0,
1625
- reasoningTokens: (_q = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _q : void 0,
1626
- cachedInputTokens: (_r = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _r : void 0
1624
+ inputTokens: (_o = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _o : void 0,
1625
+ outputTokens: (_p = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _p : void 0,
1626
+ totalTokens: (_q = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _q : void 0,
1627
+ reasoningTokens: (_r = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _r : void 0,
1628
+ cachedInputTokens: (_s = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _s : void 0
1627
1629
  },
1628
1630
  warnings,
1629
1631
  providerMetadata: {
1630
1632
  google: {
1631
- promptFeedback: (_s = response.promptFeedback) != null ? _s : null,
1632
- groundingMetadata: (_t = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _t : null,
1633
- urlContextMetadata: (_u = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _u : null,
1634
- safetyRatings: (_v = candidate == null ? void 0 : candidate.safetyRatings) != null ? _v : null,
1635
- serviceTier: (_w = response.serviceTier) != null ? _w : null,
1636
- ...(candidate == null ? void 0 : candidate.continuationToken) != null ? { continuationToken: candidate.continuationToken } : {},
1633
+ promptFeedback: (_t = response.promptFeedback) != null ? _t : null,
1634
+ groundingMetadata: (_u = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _u : null,
1635
+ ...groundingMetadataPerRequest.length > 1 ? { groundingMetadataPerRequest } : {},
1636
+ urlContextMetadata: (_v = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _v : null,
1637
+ safetyRatings: (_w = candidate == null ? void 0 : candidate.safetyRatings) != null ? _w : null,
1638
+ serviceTier: (_x = response.serviceTier) != null ? _x : null,
1639
+ ...(candidate == null ? void 0 : candidate.continuationToken) != null && candidate.finishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
1637
1640
  ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {},
1638
1641
  usageMetadata: usageMetadata != null ? usageMetadata : null
1639
1642
  }
@@ -1665,14 +1668,27 @@ var GoogleGenerativeAILanguageModel = class {
1665
1668
  fetch: this.config.fetch
1666
1669
  });
1667
1670
  const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
1671
+ const groundingMetadataPerRequest = [];
1672
+ let requestGroundingMetadata = null;
1668
1673
  const response = continueGoogleStream({
1669
1674
  stream: firstResponse,
1670
1675
  getContinuationToken: (chunk) => {
1671
1676
  var _a, _b;
1672
1677
  const candidate = (_a = chunk.candidates) == null ? void 0 : _a[0];
1678
+ if ((candidate == null ? void 0 : candidate.groundingMetadata) != null) {
1679
+ requestGroundingMetadata = candidate.groundingMetadata;
1680
+ }
1673
1681
  return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
1674
1682
  },
1675
- continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
1683
+ continueStream: async (continuationToken) => {
1684
+ const { value } = await streamGenerateContent({
1685
+ ...args,
1686
+ continuationToken
1687
+ });
1688
+ groundingMetadataPerRequest.push(requestGroundingMetadata);
1689
+ requestGroundingMetadata = null;
1690
+ return value;
1691
+ }
1676
1692
  });
1677
1693
  let finishReason = "unknown";
1678
1694
  const usage = {
@@ -1950,6 +1966,18 @@ var GoogleGenerativeAILanguageModel = class {
1950
1966
  id: currentReasoningBlockId
1951
1967
  });
1952
1968
  }
1969
+ if (groundingMetadataPerRequest.length > 0) {
1970
+ providerMetadata = {
1971
+ ...providerMetadata,
1972
+ google: {
1973
+ ...providerMetadata == null ? void 0 : providerMetadata.google,
1974
+ groundingMetadataPerRequest: [
1975
+ ...groundingMetadataPerRequest,
1976
+ requestGroundingMetadata
1977
+ ]
1978
+ }
1979
+ };
1980
+ }
1953
1981
  controller.enqueue({
1954
1982
  type: "finish",
1955
1983
  finishReason,