@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.
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.105" : "0.0.0-test";
11
11
 
12
12
  // src/google-generative-ai-embedding-model.ts
13
13
  import {
@@ -54,7 +54,7 @@ import {
54
54
  zodSchema as zodSchema2
55
55
  } from "@ai-sdk/provider-utils";
56
56
  import { z as z2 } from "zod/v4";
57
- var googleEmbeddingContentPartSchema = z2.union([
57
+ var googleEmbeddingContentPartSchema = () => z2.union([
58
58
  z2.object({ text: z2.string() }),
59
59
  z2.object({
60
60
  inlineData: z2.object({
@@ -102,7 +102,7 @@ var googleGenerativeAIEmbeddingProviderOptions = lazySchema2(
102
102
  * The array length must match the number of values being embedded. In
103
103
  * the case of a single embedding, the array length must be 1.
104
104
  */
105
- content: z2.array(z2.array(googleEmbeddingContentPartSchema).min(1).nullable()).optional()
105
+ content: z2.array(z2.array(googleEmbeddingContentPartSchema()).min(1).nullable()).optional()
106
106
  })
107
107
  )
108
108
  );
@@ -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,
@@ -2167,13 +2195,13 @@ var getSafetyRatingSchema = () => z5.object({
2167
2195
  severityScore: z5.number().nullish(),
2168
2196
  blocked: z5.boolean().nullish()
2169
2197
  });
2170
- var tokenDetailsSchema = z5.array(
2198
+ var tokenDetailsSchema = () => z5.array(
2171
2199
  z5.object({
2172
2200
  modality: z5.string(),
2173
2201
  tokenCount: z5.number()
2174
2202
  })
2175
2203
  ).nullish();
2176
- var usageSchema = z5.object({
2204
+ var usageSchema = () => z5.object({
2177
2205
  cachedContentTokenCount: z5.number().nullish(),
2178
2206
  thoughtsTokenCount: z5.number().nullish(),
2179
2207
  promptTokenCount: z5.number().nullish(),
@@ -2182,8 +2210,8 @@ var usageSchema = z5.object({
2182
2210
  // https://cloud.google.com/vertex-ai/generative-ai/docs/reference/rest/v1/GenerateContentResponse#TrafficType
2183
2211
  trafficType: z5.string().nullish(),
2184
2212
  // https://ai.google.dev/api/generate-content#Modality
2185
- promptTokensDetails: tokenDetailsSchema,
2186
- candidatesTokensDetails: tokenDetailsSchema
2213
+ promptTokensDetails: tokenDetailsSchema(),
2214
+ candidatesTokensDetails: tokenDetailsSchema()
2187
2215
  });
2188
2216
  var getUrlContextMetadataSchema = () => z5.object({
2189
2217
  urlMetadata: z5.array(
@@ -2206,7 +2234,7 @@ var responseSchema = lazySchema5(
2206
2234
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
2207
2235
  })
2208
2236
  ).nullish(),
2209
- usageMetadata: usageSchema.nullish(),
2237
+ usageMetadata: usageSchema().nullish(),
2210
2238
  promptFeedback: z5.object({
2211
2239
  blockReason: z5.string().nullish(),
2212
2240
  safetyRatings: z5.array(getSafetyRatingSchema()).nullish()
@@ -2228,7 +2256,7 @@ var chunkSchema = lazySchema5(
2228
2256
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
2229
2257
  })
2230
2258
  ).nullish(),
2231
- usageMetadata: usageSchema.nullish(),
2259
+ usageMetadata: usageSchema().nullish(),
2232
2260
  promptFeedback: z5.object({
2233
2261
  blockReason: z5.string().nullish(),
2234
2262
  safetyRatings: z5.array(getSafetyRatingSchema()).nullish()
@@ -2274,7 +2302,7 @@ import {
2274
2302
  zodSchema as zodSchema7
2275
2303
  } from "@ai-sdk/provider-utils";
2276
2304
  import { z as z8 } from "zod/v4";
2277
- var fileSearchArgsBaseSchema = z8.object({
2305
+ var fileSearchArgsBaseSchema = () => z8.object({
2278
2306
  /** The names of the file_search_stores to retrieve from.
2279
2307
  * Example: `fileSearchStores/my-file-search-store-123`
2280
2308
  */
@@ -2291,7 +2319,7 @@ var fileSearchArgsBaseSchema = z8.object({
2291
2319
  ).optional()
2292
2320
  }).passthrough();
2293
2321
  var fileSearchArgsSchema = lazySchema7(
2294
- () => zodSchema7(fileSearchArgsBaseSchema)
2322
+ () => zodSchema7(fileSearchArgsBaseSchema())
2295
2323
  );
2296
2324
  var fileSearch = createProviderDefinedToolFactory2({
2297
2325
  id: "google.file_search",
@@ -2319,7 +2347,7 @@ import {
2319
2347
  zodSchema as zodSchema9
2320
2348
  } from "@ai-sdk/provider-utils";
2321
2349
  import { z as z10 } from "zod/v4";
2322
- var googleSearchToolArgsBaseSchema = z10.object({
2350
+ var googleSearchToolArgsBaseSchema = () => z10.object({
2323
2351
  searchTypes: z10.object({
2324
2352
  webSearch: z10.object({}).optional(),
2325
2353
  imageSearch: z10.object({}).optional()
@@ -2330,7 +2358,7 @@ var googleSearchToolArgsBaseSchema = z10.object({
2330
2358
  }).optional()
2331
2359
  }).passthrough();
2332
2360
  var googleSearchToolArgsSchema = lazySchema9(
2333
- () => zodSchema9(googleSearchToolArgsBaseSchema)
2361
+ () => zodSchema9(googleSearchToolArgsBaseSchema())
2334
2362
  );
2335
2363
  var googleSearch = createProviderDefinedToolFactory4({
2336
2364
  id: "google.google_search",