@ai-sdk/google 2.0.102 → 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,17 @@
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
+
9
+ ## 2.0.103
10
+
11
+ ### Patch Changes
12
+
13
+ - 7c28815: feat(google): add `gemini-4-argon` model id and long decode support: requests that reach the per-request cap (`CONTINUATION`) are continued automatically with usage summed across requests and the first request's prompt token count exposed as `providerMetadata.google.initialPromptTokenCount`, and the `continuationToken` provider option / provider metadata resume responses that used up their `maxOutputTokens` budget
14
+
3
15
  ## 2.0.102
4
16
 
5
17
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -47,6 +47,7 @@ declare const googleGenerativeAIProviderOptions: _ai_sdk_provider_utils.LazySche
47
47
  } | undefined;
48
48
  } | undefined;
49
49
  serviceTier?: "standard" | "flex" | "priority" | undefined;
50
+ continuationToken?: string | undefined;
50
51
  }>;
51
52
  type GoogleGenerativeAIProviderOptions = InferValidator<typeof googleGenerativeAIProviderOptions>;
52
53
 
@@ -80,6 +81,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
80
81
  })[] | null | undefined;
81
82
  } | null | undefined;
82
83
  finishReason?: string | null | undefined;
84
+ continuationToken?: string | null | undefined;
83
85
  safetyRatings?: {
84
86
  category?: string | null | undefined;
85
87
  probability?: string | null | undefined;
@@ -181,9 +183,27 @@ type GoogleGenerativeAIUrlContextMetadata = UrlContextMetadataSchema;
181
183
  type GoogleGenerativeAISafetyRating = SafetyRatingSchema;
182
184
  interface GoogleGenerativeAIProviderMetadata {
183
185
  groundingMetadata: GoogleGenerativeAIGroundingMetadata | null;
186
+ /** Grounding from each automatically continued request, in request order. */
187
+ groundingMetadataPerRequest?: Array<GoogleGenerativeAIGroundingMetadata | null>;
184
188
  urlContextMetadata: GoogleGenerativeAIUrlContextMetadata | null;
185
189
  safetyRatings: GoogleGenerativeAISafetyRating[] | null;
186
190
  serviceTier: string | null;
191
+ /**
192
+ * Opaque token returned when a response stops with `MAX_TOKENS` because it
193
+ * used up its `maxOutputTokens` budget. Pass it back as
194
+ * `providerOptions.google.continuationToken` together with a higher
195
+ * `maxOutputTokens` and the original prompt to resume generation.
196
+ */
197
+ continuationToken?: string | null;
198
+ /**
199
+ * Prompt token count of the first request of a long decode response that
200
+ * was continued automatically (Gemini 4). Usage is summed across requests
201
+ * to match billing, and each continued request re-sends the prompt plus all
202
+ * output so far as input. Use this value instead of the summed input tokens
203
+ * for context window checks and compaction. Only present when the response
204
+ * spanned more than one request.
205
+ */
206
+ initialPromptTokenCount?: number | null;
187
207
  }
188
208
 
189
209
  type GoogleGenerativeAIImageModelId = 'imagen-4.0-generate-001' | 'imagen-4.0-ultra-generate-001' | 'imagen-4.0-fast-generate-001' | (string & {});
package/dist/index.d.ts CHANGED
@@ -47,6 +47,7 @@ declare const googleGenerativeAIProviderOptions: _ai_sdk_provider_utils.LazySche
47
47
  } | undefined;
48
48
  } | undefined;
49
49
  serviceTier?: "standard" | "flex" | "priority" | undefined;
50
+ continuationToken?: string | undefined;
50
51
  }>;
51
52
  type GoogleGenerativeAIProviderOptions = InferValidator<typeof googleGenerativeAIProviderOptions>;
52
53
 
@@ -80,6 +81,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
80
81
  })[] | null | undefined;
81
82
  } | null | undefined;
82
83
  finishReason?: string | null | undefined;
84
+ continuationToken?: string | null | undefined;
83
85
  safetyRatings?: {
84
86
  category?: string | null | undefined;
85
87
  probability?: string | null | undefined;
@@ -181,9 +183,27 @@ type GoogleGenerativeAIUrlContextMetadata = UrlContextMetadataSchema;
181
183
  type GoogleGenerativeAISafetyRating = SafetyRatingSchema;
182
184
  interface GoogleGenerativeAIProviderMetadata {
183
185
  groundingMetadata: GoogleGenerativeAIGroundingMetadata | null;
186
+ /** Grounding from each automatically continued request, in request order. */
187
+ groundingMetadataPerRequest?: Array<GoogleGenerativeAIGroundingMetadata | null>;
184
188
  urlContextMetadata: GoogleGenerativeAIUrlContextMetadata | null;
185
189
  safetyRatings: GoogleGenerativeAISafetyRating[] | null;
186
190
  serviceTier: string | null;
191
+ /**
192
+ * Opaque token returned when a response stops with `MAX_TOKENS` because it
193
+ * used up its `maxOutputTokens` budget. Pass it back as
194
+ * `providerOptions.google.continuationToken` together with a higher
195
+ * `maxOutputTokens` and the original prompt to resume generation.
196
+ */
197
+ continuationToken?: string | null;
198
+ /**
199
+ * Prompt token count of the first request of a long decode response that
200
+ * was continued automatically (Gemini 4). Usage is summed across requests
201
+ * to match billing, and each continued request re-sends the prompt plus all
202
+ * output so far as input. Use this value instead of the summed input tokens
203
+ * for context window checks and compaction. Only present when the response
204
+ * spanned more than one request.
205
+ */
206
+ initialPromptTokenCount?: number | null;
187
207
  }
188
208
 
189
209
  type GoogleGenerativeAIImageModelId = 'imagen-4.0-generate-001' | 'imagen-4.0-ultra-generate-001' | 'imagen-4.0-fast-generate-001' | (string & {});
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.102" : "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");
@@ -241,6 +241,100 @@ var googleGenerativeAISingleEmbeddingResponseSchema = (0, import_provider_utils3
241
241
  var import_provider_utils6 = require("@ai-sdk/provider-utils");
242
242
  var import_v45 = require("zod/v4");
243
243
 
244
+ // src/add-google-generative-ai-usage-metadata.ts
245
+ function addGoogleGenerativeAIUsageMetadata(a, b) {
246
+ if (a == null || b == null) {
247
+ return b != null ? b : a;
248
+ }
249
+ return {
250
+ ...a,
251
+ ...b,
252
+ promptTokenCount: addCounts(a.promptTokenCount, b.promptTokenCount),
253
+ candidatesTokenCount: addCounts(
254
+ a.candidatesTokenCount,
255
+ b.candidatesTokenCount
256
+ ),
257
+ totalTokenCount: addCounts(a.totalTokenCount, b.totalTokenCount),
258
+ cachedContentTokenCount: addCounts(
259
+ a.cachedContentTokenCount,
260
+ b.cachedContentTokenCount
261
+ ),
262
+ thoughtsTokenCount: addCounts(a.thoughtsTokenCount, b.thoughtsTokenCount),
263
+ promptTokensDetails: addTokenDetails(
264
+ a.promptTokensDetails,
265
+ b.promptTokensDetails
266
+ ),
267
+ candidatesTokensDetails: addTokenDetails(
268
+ a.candidatesTokensDetails,
269
+ b.candidatesTokensDetails
270
+ )
271
+ };
272
+ }
273
+ function addCounts(a, b) {
274
+ return a == null && b == null ? b != null ? b : a : (a != null ? a : 0) + (b != null ? b : 0);
275
+ }
276
+ function addTokenDetails(a, b) {
277
+ var _a;
278
+ if (a == null || b == null) {
279
+ return b != null ? b : a;
280
+ }
281
+ const tokenCounts = /* @__PURE__ */ new Map();
282
+ for (const { modality, tokenCount } of [...a, ...b]) {
283
+ tokenCounts.set(modality, ((_a = tokenCounts.get(modality)) != null ? _a : 0) + tokenCount);
284
+ }
285
+ return Array.from(tokenCounts, ([modality, tokenCount]) => ({
286
+ modality,
287
+ tokenCount
288
+ }));
289
+ }
290
+
291
+ // src/continue-google-stream.ts
292
+ function continueGoogleStream({
293
+ stream,
294
+ getContinuationToken,
295
+ continueStream
296
+ }) {
297
+ let reader = stream.getReader();
298
+ let pendingContinuationToken;
299
+ return new ReadableStream({
300
+ async pull(controller) {
301
+ while (true) {
302
+ const { done, value } = await reader.read();
303
+ if (!done) {
304
+ if (value.success) {
305
+ const token = getContinuationToken(value.value);
306
+ if (token != null) {
307
+ pendingContinuationToken = token;
308
+ }
309
+ }
310
+ controller.enqueue(value);
311
+ return;
312
+ }
313
+ if (pendingContinuationToken == null) {
314
+ controller.close();
315
+ return;
316
+ }
317
+ const continuationToken = pendingContinuationToken;
318
+ pendingContinuationToken = void 0;
319
+ try {
320
+ reader = (await continueStream(continuationToken)).getReader();
321
+ } catch (error) {
322
+ controller.enqueue({
323
+ success: false,
324
+ error,
325
+ rawValue: void 0
326
+ });
327
+ controller.close();
328
+ return;
329
+ }
330
+ }
331
+ },
332
+ async cancel(reason) {
333
+ await reader.cancel(reason);
334
+ }
335
+ });
336
+ }
337
+
244
338
  // src/convert-json-schema-to-openapi-schema.ts
245
339
  var import_provider2 = require("@ai-sdk/provider");
246
340
  var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
@@ -896,7 +990,17 @@ var googleGenerativeAIProviderOptions = (0, import_provider_utils5.lazySchema)(
896
990
  /**
897
991
  * Optional. The service tier to use for the request.
898
992
  */
899
- serviceTier: import_v44.z.enum(["standard", "flex", "priority"]).optional()
993
+ serviceTier: import_v44.z.enum(["standard", "flex", "priority"]).optional(),
994
+ /**
995
+ * Optional. Resumes a response that stopped with `MAX_TOKENS` because it
996
+ * used up its `maxOutputTokens` budget (Gemini 4 long decode). Pass the
997
+ * `continuationToken` from the previous response's provider metadata
998
+ * together with the original prompt and a higher `maxOutputTokens` (the
999
+ * budget is cumulative across requests). Requests that reach the
1000
+ * per-request cap while budget remains (`CONTINUATION`) are continued
1001
+ * automatically. Sent as the `continuationToken` body field.
1002
+ */
1003
+ continuationToken: import_v44.z.string().optional()
900
1004
  })
901
1005
  )
902
1006
  );
@@ -1194,6 +1298,9 @@ function mapGoogleGenerativeAIFinishReason({
1194
1298
  case "STOP":
1195
1299
  return hasToolCalls ? "tool-calls" : "stop";
1196
1300
  case "MAX_TOKENS":
1301
+ // Gemini 4 long decode: the per-request cap was reached while budget
1302
+ // remains. Continued automatically; only surfaced if continuing fails.
1303
+ case "CONTINUATION":
1197
1304
  return "length";
1198
1305
  case "IMAGE_SAFETY":
1199
1306
  case "RECITATION":
@@ -1368,43 +1475,72 @@ var GoogleGenerativeAILanguageModel = class {
1368
1475
  } : googleToolConfig,
1369
1476
  cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
1370
1477
  labels: googleOptions == null ? void 0 : googleOptions.labels,
1371
- serviceTier: sanitizedServiceTier
1478
+ serviceTier: sanitizedServiceTier,
1479
+ ...(googleOptions == null ? void 0 : googleOptions.continuationToken) != null && {
1480
+ continuationToken: googleOptions.continuationToken
1481
+ }
1372
1482
  },
1373
1483
  warnings: [...warnings, ...toolWarnings]
1374
1484
  };
1375
1485
  }
1376
1486
  async doGenerate(options) {
1377
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
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;
1378
1488
  const { args, warnings } = await this.getArgs(options);
1379
1489
  const body = JSON.stringify(args);
1380
1490
  const mergedHeaders = (0, import_provider_utils6.combineHeaders)(
1381
1491
  await (0, import_provider_utils6.resolve)(this.config.headers),
1382
1492
  options.headers
1383
1493
  );
1384
- const {
1385
- responseHeaders,
1386
- value: response,
1387
- rawValue: rawResponse
1388
- } = await (0, import_provider_utils6.postJsonToApi)({
1494
+ const generateContent = (body2) => (0, import_provider_utils6.postJsonToApi)({
1389
1495
  url: `${this.config.baseURL}/${getModelPath(
1390
1496
  this.modelId
1391
1497
  )}:generateContent`,
1392
1498
  headers: mergedHeaders,
1393
- body: args,
1499
+ body: body2,
1394
1500
  failedResponseHandler: googleFailedResponseHandler,
1395
1501
  successfulResponseHandler: (0, import_provider_utils6.createJsonResponseHandler)(responseSchema),
1396
1502
  abortSignal: options.abortSignal,
1397
1503
  fetch: this.config.fetch
1398
1504
  });
1399
- const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
1400
- const promptBlockReason = (_b = response.promptFeedback) == null ? void 0 : _b.blockReason;
1505
+ let {
1506
+ responseHeaders,
1507
+ value: response,
1508
+ rawValue: rawResponse
1509
+ } = await generateContent(args);
1510
+ const continuedParts = [];
1511
+ const groundingMetadataPerRequest = [];
1512
+ let continuedUsageMetadata;
1513
+ const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
1514
+ while (true) {
1515
+ const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
1516
+ groundingMetadataPerRequest.push((_c = candidate2 == null ? void 0 : candidate2.groundingMetadata) != null ? _c : null);
1517
+ const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
1518
+ if (continuationToken == null) {
1519
+ break;
1520
+ }
1521
+ continuedParts.push(...(_e = (_d = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _d.parts) != null ? _e : []);
1522
+ continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1523
+ continuedUsageMetadata,
1524
+ (_f = response.usageMetadata) != null ? _f : void 0
1525
+ );
1526
+ ({
1527
+ responseHeaders,
1528
+ value: response,
1529
+ rawValue: rawResponse
1530
+ } = await generateContent({ ...args, continuationToken }));
1531
+ }
1532
+ const candidate = (_g = response.candidates) == null ? void 0 : _g[0];
1533
+ const promptBlockReason = (_h = response.promptFeedback) == null ? void 0 : _h.blockReason;
1401
1534
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && promptBlockReason != null;
1402
1535
  const content = [];
1403
- const parts = (_d = (_c = candidate == null ? void 0 : candidate.content) == null ? void 0 : _c.parts) != null ? _d : [];
1404
- const usageMetadata = response.usageMetadata;
1536
+ const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
1537
+ const usageMetadata = addGoogleGenerativeAIUsageMetadata(
1538
+ continuedUsageMetadata,
1539
+ (_k = response.usageMetadata) != null ? _k : void 0
1540
+ );
1405
1541
  let lastCodeExecutionToolCallId;
1406
1542
  for (const part of parts) {
1407
- if ("executableCode" in part && ((_e = part.executableCode) == null ? void 0 : _e.code)) {
1543
+ if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
1408
1544
  const toolCallId = this.config.generateId();
1409
1545
  lastCodeExecutionToolCallId = toolCallId;
1410
1546
  content.push({
@@ -1445,7 +1581,7 @@ var GoogleGenerativeAILanguageModel = class {
1445
1581
  } else if ("functionCall" in part) {
1446
1582
  content.push({
1447
1583
  type: "tool-call",
1448
- toolCallId: (_f = part.functionCall.id) != null ? _f : this.config.generateId(),
1584
+ toolCallId: (_m = part.functionCall.id) != null ? _m : this.config.generateId(),
1449
1585
  toolName: part.functionCall.name,
1450
1586
  input: JSON.stringify(part.functionCall.args),
1451
1587
  providerMetadata: part.thoughtSignature ? { google: { thoughtSignature: part.thoughtSignature } } : void 0
@@ -1458,10 +1594,10 @@ var GoogleGenerativeAILanguageModel = class {
1458
1594
  });
1459
1595
  }
1460
1596
  }
1461
- const sources = (_g = extractSources({
1597
+ const sources = (_n = extractSources({
1462
1598
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1463
1599
  generateId: this.config.generateId
1464
- })) != null ? _g : [];
1600
+ })) != null ? _n : [];
1465
1601
  for (const source of sources) {
1466
1602
  content.push(source);
1467
1603
  }
@@ -1472,20 +1608,23 @@ var GoogleGenerativeAILanguageModel = class {
1472
1608
  hasToolCalls: content.some((part) => part.type === "tool-call")
1473
1609
  }),
1474
1610
  usage: {
1475
- inputTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _h : void 0,
1476
- outputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _i : void 0,
1477
- totalTokens: (_j = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _j : void 0,
1478
- reasoningTokens: (_k = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _k : void 0,
1479
- cachedInputTokens: (_l = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _l : 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
1480
1616
  },
1481
1617
  warnings,
1482
1618
  providerMetadata: {
1483
1619
  google: {
1484
- promptFeedback: (_m = response.promptFeedback) != null ? _m : null,
1485
- groundingMetadata: (_n = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _n : null,
1486
- urlContextMetadata: (_o = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _o : null,
1487
- safetyRatings: (_p = candidate == null ? void 0 : candidate.safetyRatings) != null ? _p : null,
1488
- serviceTier: (_q = response.serviceTier) != null ? _q : null,
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 } : {},
1627
+ ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {},
1489
1628
  usageMetadata: usageMetadata != null ? usageMetadata : null
1490
1629
  }
1491
1630
  },
@@ -1504,17 +1643,40 @@ var GoogleGenerativeAILanguageModel = class {
1504
1643
  await (0, import_provider_utils6.resolve)(this.config.headers),
1505
1644
  options.headers
1506
1645
  );
1507
- const { responseHeaders, value: response } = await (0, import_provider_utils6.postJsonToApi)({
1646
+ const streamGenerateContent = (body2) => (0, import_provider_utils6.postJsonToApi)({
1508
1647
  url: `${this.config.baseURL}/${getModelPath(
1509
1648
  this.modelId
1510
1649
  )}:streamGenerateContent?alt=sse`,
1511
1650
  headers,
1512
- body: args,
1651
+ body: body2,
1513
1652
  failedResponseHandler: googleFailedResponseHandler,
1514
1653
  successfulResponseHandler: (0, import_provider_utils6.createEventSourceResponseHandler)(chunkSchema),
1515
1654
  abortSignal: options.abortSignal,
1516
1655
  fetch: this.config.fetch
1517
1656
  });
1657
+ const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
1658
+ const groundingMetadataPerRequest = [];
1659
+ let requestGroundingMetadata = null;
1660
+ const response = continueGoogleStream({
1661
+ stream: firstResponse,
1662
+ getContinuationToken: (chunk) => {
1663
+ var _a, _b;
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
+ }
1668
+ return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
1669
+ },
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
+ }
1679
+ });
1518
1680
  let finishReason = "unknown";
1519
1681
  const usage = {
1520
1682
  inputTokens: void 0,
@@ -1525,6 +1687,10 @@ var GoogleGenerativeAILanguageModel = class {
1525
1687
  let lastGroundingMetadata = null;
1526
1688
  let lastUrlContextMetadata = null;
1527
1689
  let serviceTier = null;
1690
+ let requestUsageMetadata;
1691
+ let continuedUsageMetadata;
1692
+ let lastContinuationToken = null;
1693
+ let initialPromptTokenCount = void 0;
1528
1694
  const generateId3 = this.config.generateId;
1529
1695
  let hasToolCalls = false;
1530
1696
  let currentTextBlockId = null;
@@ -1548,7 +1714,13 @@ var GoogleGenerativeAILanguageModel = class {
1548
1714
  return;
1549
1715
  }
1550
1716
  const value = chunk.value;
1551
- const usageMetadata = value.usageMetadata;
1717
+ if (value.usageMetadata != null) {
1718
+ requestUsageMetadata = value.usageMetadata;
1719
+ }
1720
+ const usageMetadata = value.usageMetadata == null ? void 0 : addGoogleGenerativeAIUsageMetadata(
1721
+ continuedUsageMetadata,
1722
+ requestUsageMetadata
1723
+ );
1552
1724
  if (usageMetadata != null) {
1553
1725
  usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
1554
1726
  usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
@@ -1560,6 +1732,19 @@ var GoogleGenerativeAILanguageModel = class {
1560
1732
  serviceTier = value.serviceTier;
1561
1733
  }
1562
1734
  const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
1735
+ if ((candidate == null ? void 0 : candidate.continuationToken) != null) {
1736
+ lastContinuationToken = candidate.continuationToken;
1737
+ }
1738
+ if ((candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION") {
1739
+ if (continuedUsageMetadata == null) {
1740
+ initialPromptTokenCount = requestUsageMetadata == null ? void 0 : requestUsageMetadata.promptTokenCount;
1741
+ }
1742
+ continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1743
+ continuedUsageMetadata,
1744
+ requestUsageMetadata
1745
+ );
1746
+ requestUsageMetadata = void 0;
1747
+ }
1563
1748
  if (candidate == null) {
1564
1749
  const promptBlockReason2 = (_g = value.promptFeedback) == null ? void 0 : _g.blockReason;
1565
1750
  if (promptBlockReason2 != null) {
@@ -1742,7 +1927,12 @@ var GoogleGenerativeAILanguageModel = class {
1742
1927
  groundingMetadata: lastGroundingMetadata,
1743
1928
  urlContextMetadata: lastUrlContextMetadata,
1744
1929
  safetyRatings: (_o = candidate.safetyRatings) != null ? _o : null,
1745
- serviceTier
1930
+ serviceTier,
1931
+ // Only surface the token when the response stopped early
1932
+ // (budget used up, or continuing failed), not for the
1933
+ // checkpoint tokens of a stream that finished normally.
1934
+ ...lastContinuationToken != null && (rawFinishReason === "MAX_TOKENS" || rawFinishReason === "CONTINUATION") ? { continuationToken: lastContinuationToken } : {},
1935
+ ...initialPromptTokenCount != null ? { initialPromptTokenCount } : {}
1746
1936
  }
1747
1937
  };
1748
1938
  if (usageMetadata != null) {
@@ -1763,6 +1953,18 @@ var GoogleGenerativeAILanguageModel = class {
1763
1953
  id: currentReasoningBlockId
1764
1954
  });
1765
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
+ }
1766
1968
  controller.enqueue({
1767
1969
  type: "finish",
1768
1970
  finishReason,
@@ -2013,6 +2215,7 @@ var responseSchema = (0, import_provider_utils6.lazySchema)(
2013
2215
  import_v45.z.object({
2014
2216
  content: getContentSchema().nullish().or(import_v45.z.object({}).strict()),
2015
2217
  finishReason: import_v45.z.string().nullish(),
2218
+ continuationToken: import_v45.z.string().nullish(),
2016
2219
  safetyRatings: import_v45.z.array(getSafetyRatingSchema()).nullish(),
2017
2220
  groundingMetadata: getGroundingMetadataSchema().nullish(),
2018
2221
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
@@ -2034,6 +2237,7 @@ var chunkSchema = (0, import_provider_utils6.lazySchema)(
2034
2237
  import_v45.z.object({
2035
2238
  content: getContentSchema().nullish(),
2036
2239
  finishReason: import_v45.z.string().nullish(),
2240
+ continuationToken: import_v45.z.string().nullish(),
2037
2241
  safetyRatings: import_v45.z.array(getSafetyRatingSchema()).nullish(),
2038
2242
  groundingMetadata: getGroundingMetadataSchema().nullish(),
2039
2243
  urlContextMetadata: getUrlContextMetadataSchema().nullish()