@ai-sdk/google 3.0.131 → 3.0.133

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.
@@ -3,7 +3,7 @@ import { Resolvable, FetchFunction, InferSchema } from '@ai-sdk/provider-utils';
3
3
  import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult } from '@ai-sdk/provider';
4
4
  import { z } from 'zod/v4';
5
5
 
6
- type GoogleGenerativeAIModelId = 'gemini-2.0-flash' | 'gemini-2.0-flash-001' | 'gemini-2.0-flash-lite' | 'gemini-2.0-flash-lite-001' | 'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro-preview-tts' | 'gemini-2.5-flash-native-audio-latest' | 'gemini-2.5-flash-native-audio-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-3-pro-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-flash-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-pro-preview-customtools' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-nano-banana-2.1' | 'gemini-pro-latest' | 'gemini-flash-latest' | 'gemini-flash-lite-latest' | 'deep-research-pro-preview-12-2025' | 'nano-banana-pro-preview' | 'aqa' | 'gemini-robotics-er-1.5-preview' | 'gemma-3-1b-it' | 'gemma-3-4b-it' | 'gemma-3n-e4b-it' | 'gemma-3n-e2b-it' | 'gemma-3-12b-it' | 'gemma-3-27b-it' | (string & {});
6
+ type GoogleGenerativeAIModelId = 'gemini-2.0-flash' | 'gemini-2.0-flash-001' | 'gemini-2.0-flash-lite' | 'gemini-2.0-flash-lite-001' | 'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro-preview-tts' | 'gemini-2.5-flash-native-audio-latest' | 'gemini-2.5-flash-native-audio-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-3-pro-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-flash-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-pro-preview-customtools' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-4-argon' | 'gemini-nano-banana-2.1' | 'gemini-pro-latest' | 'gemini-flash-latest' | 'gemini-flash-lite-latest' | 'deep-research-pro-preview-12-2025' | 'nano-banana-pro-preview' | 'aqa' | 'gemini-robotics-er-1.5-preview' | 'gemma-3-1b-it' | 'gemma-3-4b-it' | 'gemma-3n-e4b-it' | 'gemma-3n-e2b-it' | 'gemma-3-12b-it' | 'gemma-3-27b-it' | (string & {});
7
7
 
8
8
  type GoogleGenerativeAIConfig = {
9
9
  provider: string;
@@ -146,6 +146,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
146
146
  } | null | undefined;
147
147
  finishReason?: string | null | undefined;
148
148
  finishMessage?: string | null | undefined;
149
+ continuationToken?: string | null | undefined;
149
150
  safetyRatings?: {
150
151
  category?: string | null | undefined;
151
152
  probability?: string | null | undefined;
@@ -358,7 +359,7 @@ declare const googleTools: {
358
359
  * the two surfaces (`:generateContent` vs `/interactions`) are independent and
359
360
  * may diverge over time.
360
361
  */
361
- type GoogleInteractionsModelId = 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-lite-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-flash-preview-09-2025' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro' | 'gemini-2.5-pro-preview-tts' | 'gemini-3-flash-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-pro-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-nano-banana-2.1' | 'lyria-3-clip-preview' | 'lyria-3-pro-preview' | (string & {});
362
+ type GoogleInteractionsModelId = 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-lite-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-flash-preview-09-2025' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro' | 'gemini-2.5-pro-preview-tts' | 'gemini-3-flash-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-pro-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-4-argon' | 'gemini-nano-banana-2.1' | 'lyria-3-clip-preview' | 'lyria-3-pro-preview' | (string & {});
362
363
 
363
364
  type GoogleInteractionsConfig = {
364
365
  provider: string;
@@ -3,7 +3,7 @@ import { Resolvable, FetchFunction, InferSchema } from '@ai-sdk/provider-utils';
3
3
  import { LanguageModelV3, LanguageModelV3CallOptions, LanguageModelV3GenerateResult, LanguageModelV3StreamResult } from '@ai-sdk/provider';
4
4
  import { z } from 'zod/v4';
5
5
 
6
- type GoogleGenerativeAIModelId = 'gemini-2.0-flash' | 'gemini-2.0-flash-001' | 'gemini-2.0-flash-lite' | 'gemini-2.0-flash-lite-001' | 'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro-preview-tts' | 'gemini-2.5-flash-native-audio-latest' | 'gemini-2.5-flash-native-audio-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-3-pro-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-flash-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-pro-preview-customtools' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-nano-banana-2.1' | 'gemini-pro-latest' | 'gemini-flash-latest' | 'gemini-flash-lite-latest' | 'deep-research-pro-preview-12-2025' | 'nano-banana-pro-preview' | 'aqa' | 'gemini-robotics-er-1.5-preview' | 'gemma-3-1b-it' | 'gemma-3-4b-it' | 'gemma-3n-e4b-it' | 'gemma-3n-e2b-it' | 'gemma-3-12b-it' | 'gemma-3-27b-it' | (string & {});
6
+ type GoogleGenerativeAIModelId = 'gemini-2.0-flash' | 'gemini-2.0-flash-001' | 'gemini-2.0-flash-lite' | 'gemini-2.0-flash-lite-001' | 'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro-preview-tts' | 'gemini-2.5-flash-native-audio-latest' | 'gemini-2.5-flash-native-audio-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-3-pro-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-flash-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-pro-preview-customtools' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-4-argon' | 'gemini-nano-banana-2.1' | 'gemini-pro-latest' | 'gemini-flash-latest' | 'gemini-flash-lite-latest' | 'deep-research-pro-preview-12-2025' | 'nano-banana-pro-preview' | 'aqa' | 'gemini-robotics-er-1.5-preview' | 'gemma-3-1b-it' | 'gemma-3-4b-it' | 'gemma-3n-e4b-it' | 'gemma-3n-e2b-it' | 'gemma-3-12b-it' | 'gemma-3-27b-it' | (string & {});
7
7
 
8
8
  type GoogleGenerativeAIConfig = {
9
9
  provider: string;
@@ -146,6 +146,7 @@ declare const responseSchema: _ai_sdk_provider_utils.LazySchema<{
146
146
  } | null | undefined;
147
147
  finishReason?: string | null | undefined;
148
148
  finishMessage?: string | null | undefined;
149
+ continuationToken?: string | null | undefined;
149
150
  safetyRatings?: {
150
151
  category?: string | null | undefined;
151
152
  probability?: string | null | undefined;
@@ -358,7 +359,7 @@ declare const googleTools: {
358
359
  * the two surfaces (`:generateContent` vs `/interactions`) are independent and
359
360
  * may diverge over time.
360
361
  */
361
- type GoogleInteractionsModelId = 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-lite-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-flash-preview-09-2025' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro' | 'gemini-2.5-pro-preview-tts' | 'gemini-3-flash-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-pro-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-nano-banana-2.1' | 'lyria-3-clip-preview' | 'lyria-3-pro-preview' | (string & {});
362
+ type GoogleInteractionsModelId = 'gemini-2.5-computer-use-preview-10-2025' | 'gemini-2.5-flash' | 'gemini-2.5-flash-image' | 'gemini-2.5-flash-lite' | 'gemini-2.5-flash-lite-preview-09-2025' | 'gemini-2.5-flash-native-audio-preview-12-2025' | 'gemini-2.5-flash-preview-09-2025' | 'gemini-2.5-flash-preview-tts' | 'gemini-2.5-pro' | 'gemini-2.5-pro-preview-tts' | 'gemini-3-flash-preview' | 'gemini-3-pro-image-preview' | 'gemini-3-pro-preview' | 'gemini-3.1-pro-preview' | 'gemini-3.1-flash-image-preview' | 'gemini-3.1-flash-lite-preview' | 'gemini-3.1-flash-tts-preview' | 'gemini-3.5-flash' | 'gemini-3.5-flash-lite' | 'gemini-3.6-flash' | 'gemini-3.7-flash' | 'gemini-3.8-flash' | 'gemini-4-argon' | 'gemini-nano-banana-2.1' | 'lyria-3-clip-preview' | 'lyria-3-pro-preview' | (string & {});
362
363
 
363
364
  type GoogleInteractionsConfig = {
364
365
  provider: string;
@@ -32,6 +32,53 @@ module.exports = __toCommonJS(index_exports);
32
32
  var import_provider_utils5 = require("@ai-sdk/provider-utils");
33
33
  var import_v43 = require("zod/v4");
34
34
 
35
+ // src/continue-google-stream.ts
36
+ function continueGoogleStream({
37
+ stream,
38
+ getContinuationToken,
39
+ continueStream
40
+ }) {
41
+ let reader = stream.getReader();
42
+ let pendingContinuationToken;
43
+ return new ReadableStream({
44
+ async pull(controller) {
45
+ while (true) {
46
+ const { done, value } = await reader.read();
47
+ if (!done) {
48
+ if (value.success) {
49
+ const token = getContinuationToken(value.value);
50
+ if (token != null) {
51
+ pendingContinuationToken = token;
52
+ }
53
+ }
54
+ controller.enqueue(value);
55
+ return;
56
+ }
57
+ if (pendingContinuationToken == null) {
58
+ controller.close();
59
+ return;
60
+ }
61
+ const continuationToken = pendingContinuationToken;
62
+ pendingContinuationToken = void 0;
63
+ try {
64
+ reader = (await continueStream(continuationToken)).getReader();
65
+ } catch (error) {
66
+ controller.enqueue({
67
+ success: false,
68
+ error,
69
+ rawValue: void 0
70
+ });
71
+ controller.close();
72
+ return;
73
+ }
74
+ }
75
+ },
76
+ async cancel(reason) {
77
+ await reader.cancel(reason);
78
+ }
79
+ });
80
+ }
81
+
35
82
  // src/convert-google-generative-ai-usage.ts
36
83
  function convertGoogleGenerativeAIUsage(usage) {
37
84
  var _a, _b, _c, _d;
@@ -70,6 +117,63 @@ function convertGoogleGenerativeAIUsage(usage) {
70
117
  raw: usage
71
118
  };
72
119
  }
120
+ function addGoogleGenerativeAIUsageMetadata(a, b) {
121
+ if (a == null || b == null) {
122
+ return b != null ? b : a;
123
+ }
124
+ return {
125
+ ...a,
126
+ ...b,
127
+ promptTokenCount: addCounts(a.promptTokenCount, b.promptTokenCount),
128
+ candidatesTokenCount: addCounts(
129
+ a.candidatesTokenCount,
130
+ b.candidatesTokenCount
131
+ ),
132
+ toolUsePromptTokenCount: addCounts(
133
+ a.toolUsePromptTokenCount,
134
+ b.toolUsePromptTokenCount
135
+ ),
136
+ totalTokenCount: addCounts(a.totalTokenCount, b.totalTokenCount),
137
+ cachedContentTokenCount: addCounts(
138
+ a.cachedContentTokenCount,
139
+ b.cachedContentTokenCount
140
+ ),
141
+ thoughtsTokenCount: addCounts(a.thoughtsTokenCount, b.thoughtsTokenCount),
142
+ promptTokensDetails: addTokenDetails(
143
+ a.promptTokensDetails,
144
+ b.promptTokensDetails
145
+ ),
146
+ cacheTokensDetails: addTokenDetails(
147
+ a.cacheTokensDetails,
148
+ b.cacheTokensDetails
149
+ ),
150
+ candidatesTokensDetails: addTokenDetails(
151
+ a.candidatesTokensDetails,
152
+ b.candidatesTokensDetails
153
+ ),
154
+ toolUsePromptTokensDetails: addTokenDetails(
155
+ a.toolUsePromptTokensDetails,
156
+ b.toolUsePromptTokensDetails
157
+ )
158
+ };
159
+ }
160
+ function addCounts(a, b) {
161
+ return a == null && b == null ? b != null ? b : a : (a != null ? a : 0) + (b != null ? b : 0);
162
+ }
163
+ function addTokenDetails(a, b) {
164
+ var _a;
165
+ if (a == null || b == null) {
166
+ return b != null ? b : a;
167
+ }
168
+ const tokenCounts = /* @__PURE__ */ new Map();
169
+ for (const { modality, tokenCount } of [...a, ...b]) {
170
+ tokenCounts.set(modality, ((_a = tokenCounts.get(modality)) != null ? _a : 0) + tokenCount);
171
+ }
172
+ return Array.from(tokenCounts, ([modality, tokenCount]) => ({
173
+ modality,
174
+ tokenCount
175
+ }));
176
+ }
73
177
 
74
178
  // src/convert-json-schema-to-openapi-schema.ts
75
179
  var import_provider = require("@ai-sdk/provider");
@@ -980,6 +1084,16 @@ var googleLanguageModelOptions = (0, import_provider_utils4.lazySchema)(
980
1084
  * `serviceTier` body field. Gemini API only.
981
1085
  */
982
1086
  serviceTier: import_v42.z.enum(["standard", "flex", "priority"]).optional(),
1087
+ /**
1088
+ * Optional. Resumes a response that stopped with `MAX_TOKENS` because it
1089
+ * used up its `maxOutputTokens` budget (Gemini 4 long decode). Pass the
1090
+ * `continuationToken` from the previous response's provider metadata
1091
+ * together with the original prompt and a higher `maxOutputTokens` (the
1092
+ * budget is cumulative across requests). Requests that reach the
1093
+ * per-request cap while budget remains (`CONTINUATION`) are continued
1094
+ * automatically. Sent as the `continuationToken` body field.
1095
+ */
1096
+ continuationToken: import_v42.z.string().optional(),
983
1097
  /**
984
1098
  * Optional. Vertex AI only. Sent as the
985
1099
  * `X-Vertex-AI-LLM-Shared-Request-Type` request header to select a
@@ -1528,6 +1642,9 @@ function mapGoogleGenerativeAIFinishReason({
1528
1642
  case "STOP":
1529
1643
  return hasToolCalls ? "tool-calls" : "stop";
1530
1644
  case "MAX_TOKENS":
1645
+ // Gemini 4 long decode: the per-request cap was reached while budget
1646
+ // remains. Continued automatically; only surfaced if continuing fails.
1647
+ case "CONTINUATION":
1531
1648
  return "length";
1532
1649
  case "IMAGE_SAFETY":
1533
1650
  case "RECITATION":
@@ -1759,7 +1876,10 @@ var GoogleGenerativeAILanguageModel = class {
1759
1876
  toolConfig,
1760
1877
  cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
1761
1878
  labels: googleOptions == null ? void 0 : googleOptions.labels,
1762
- serviceTier: bodyServiceTier
1879
+ serviceTier: bodyServiceTier,
1880
+ ...(googleOptions == null ? void 0 : googleOptions.continuationToken) != null && {
1881
+ continuationToken: googleOptions.continuationToken
1882
+ }
1763
1883
  },
1764
1884
  warnings: [...warnings, ...toolWarnings],
1765
1885
  providerOptionsName,
@@ -1767,42 +1887,66 @@ var GoogleGenerativeAILanguageModel = class {
1767
1887
  };
1768
1888
  }
1769
1889
  async doGenerate(options) {
1770
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
1890
+ 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, _y, _z, _A;
1771
1891
  const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
1772
1892
  const mergedHeaders = (0, import_provider_utils5.combineHeaders)(
1773
1893
  await (0, import_provider_utils5.resolve)(this.config.headers),
1774
1894
  options.headers,
1775
1895
  extraHeaders
1776
1896
  );
1777
- const {
1778
- responseHeaders,
1779
- value: response,
1780
- rawValue: rawResponse
1781
- } = await (0, import_provider_utils5.postJsonToApi)({
1897
+ const generateContent = (body) => (0, import_provider_utils5.postJsonToApi)({
1782
1898
  url: `${this.config.baseURL}/${getModelPath(
1783
1899
  this.modelId
1784
1900
  )}:generateContent`,
1785
1901
  headers: mergedHeaders,
1786
- body: args,
1902
+ body,
1787
1903
  failedResponseHandler: googleFailedResponseHandler,
1788
1904
  successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(responseSchema),
1789
1905
  abortSignal: options.abortSignal,
1790
1906
  fetch: this.config.fetch
1791
1907
  });
1792
- const candidate = (_a = response.candidates) == null ? void 0 : _a[0];
1793
- const promptBlockReason = (_b = response.promptFeedback) == null ? void 0 : _b.blockReason;
1908
+ let {
1909
+ responseHeaders,
1910
+ value: response,
1911
+ rawValue: rawResponse
1912
+ } = await generateContent(args);
1913
+ const continuedParts = [];
1914
+ let continuedUsageMetadata;
1915
+ const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
1916
+ while (true) {
1917
+ const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
1918
+ const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
1919
+ if (continuationToken == null) {
1920
+ break;
1921
+ }
1922
+ continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
1923
+ continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
1924
+ continuedUsageMetadata,
1925
+ (_e = response.usageMetadata) != null ? _e : void 0
1926
+ );
1927
+ ({
1928
+ responseHeaders,
1929
+ value: response,
1930
+ rawValue: rawResponse
1931
+ } = await generateContent({ ...args, continuationToken }));
1932
+ }
1933
+ const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
1934
+ const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
1794
1935
  const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
1795
1936
  promptBlockReason
1796
1937
  ) ? promptBlockReason : void 0;
1797
1938
  const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
1798
- const rawFinishReason = (_c = candidate == null ? void 0 : candidate.finishReason) != null ? _c : confirmedPromptBlockReason;
1939
+ const rawFinishReason = (_h = candidate == null ? void 0 : candidate.finishReason) != null ? _h : confirmedPromptBlockReason;
1799
1940
  const content = [];
1800
- const parts = (_e = (_d = candidate == null ? void 0 : candidate.content) == null ? void 0 : _d.parts) != null ? _e : [];
1801
- const usageMetadata = response.usageMetadata;
1941
+ const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
1942
+ const usageMetadata = addGoogleGenerativeAIUsageMetadata(
1943
+ continuedUsageMetadata,
1944
+ (_k = response.usageMetadata) != null ? _k : void 0
1945
+ );
1802
1946
  let lastCodeExecutionToolCallId;
1803
1947
  let lastServerToolCallId;
1804
1948
  for (const part of parts) {
1805
- if ("executableCode" in part && ((_f = part.executableCode) == null ? void 0 : _f.code)) {
1949
+ if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
1806
1950
  const toolCallId = this.config.generateId();
1807
1951
  lastCodeExecutionToolCallId = toolCallId;
1808
1952
  content.push({
@@ -1820,7 +1964,7 @@ var GoogleGenerativeAILanguageModel = class {
1820
1964
  toolName: "code_execution",
1821
1965
  result: {
1822
1966
  outcome: part.codeExecutionResult.outcome,
1823
- output: (_g = part.codeExecutionResult.output) != null ? _g : ""
1967
+ output: (_m = part.codeExecutionResult.output) != null ? _m : ""
1824
1968
  }
1825
1969
  });
1826
1970
  } else if ("text" in part && part.text != null) {
@@ -1844,9 +1988,9 @@ var GoogleGenerativeAILanguageModel = class {
1844
1988
  } else if ("functionCall" in part && part.functionCall.name != null) {
1845
1989
  content.push({
1846
1990
  type: "tool-call",
1847
- toolCallId: (_h = part.functionCall.id) != null ? _h : this.config.generateId(),
1991
+ toolCallId: (_n = part.functionCall.id) != null ? _n : this.config.generateId(),
1848
1992
  toolName: part.functionCall.name,
1849
- input: JSON.stringify((_i = part.functionCall.args) != null ? _i : {}),
1993
+ input: JSON.stringify((_o = part.functionCall.args) != null ? _o : {}),
1850
1994
  providerMetadata: part.thoughtSignature ? {
1851
1995
  [providerOptionsName]: {
1852
1996
  thoughtSignature: part.thoughtSignature
@@ -1868,13 +2012,13 @@ var GoogleGenerativeAILanguageModel = class {
1868
2012
  } : void 0
1869
2013
  });
1870
2014
  } else if ("toolCall" in part && part.toolCall) {
1871
- const toolCallId = (_j = part.toolCall.id) != null ? _j : this.config.generateId();
2015
+ const toolCallId = (_p = part.toolCall.id) != null ? _p : this.config.generateId();
1872
2016
  lastServerToolCallId = toolCallId;
1873
2017
  content.push({
1874
2018
  type: "tool-call",
1875
2019
  toolCallId,
1876
2020
  toolName: `server:${part.toolCall.toolType}`,
1877
- input: JSON.stringify((_k = part.toolCall.args) != null ? _k : {}),
2021
+ input: JSON.stringify((_q = part.toolCall.args) != null ? _q : {}),
1878
2022
  providerExecuted: true,
1879
2023
  dynamic: true,
1880
2024
  providerMetadata: part.thoughtSignature ? {
@@ -1891,12 +2035,12 @@ var GoogleGenerativeAILanguageModel = class {
1891
2035
  }
1892
2036
  });
1893
2037
  } else if ("toolResponse" in part && part.toolResponse) {
1894
- const responseToolCallId = (_l = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _l : this.config.generateId();
2038
+ const responseToolCallId = (_r = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _r : this.config.generateId();
1895
2039
  content.push({
1896
2040
  type: "tool-result",
1897
2041
  toolCallId: responseToolCallId,
1898
2042
  toolName: `server:${part.toolResponse.toolType}`,
1899
- result: (_m = part.toolResponse.response) != null ? _m : {},
2043
+ result: (_s = part.toolResponse.response) != null ? _s : {},
1900
2044
  providerMetadata: part.thoughtSignature ? {
1901
2045
  [providerOptionsName]: {
1902
2046
  thoughtSignature: part.thoughtSignature,
@@ -1913,10 +2057,10 @@ var GoogleGenerativeAILanguageModel = class {
1913
2057
  lastServerToolCallId = void 0;
1914
2058
  }
1915
2059
  }
1916
- const sources = (_n = extractSources({
2060
+ const sources = (_t = extractSources({
1917
2061
  groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
1918
2062
  generateId: this.config.generateId
1919
- })) != null ? _n : [];
2063
+ })) != null ? _t : [];
1920
2064
  for (const source of sources) {
1921
2065
  content.push(source);
1922
2066
  }
@@ -1936,19 +2080,21 @@ var GoogleGenerativeAILanguageModel = class {
1936
2080
  warnings,
1937
2081
  providerMetadata: {
1938
2082
  [providerOptionsName]: {
1939
- promptFeedback: (_o = response.promptFeedback) != null ? _o : null,
1940
- groundingMetadata: (_p = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _p : null,
1941
- urlContextMetadata: (_q = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _q : null,
1942
- safetyRatings: (_r = candidate == null ? void 0 : candidate.safetyRatings) != null ? _r : null,
2083
+ promptFeedback: (_u = response.promptFeedback) != null ? _u : null,
2084
+ groundingMetadata: (_v = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _v : null,
2085
+ urlContextMetadata: (_w = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _w : null,
2086
+ safetyRatings: (_x = candidate == null ? void 0 : candidate.safetyRatings) != null ? _x : null,
1943
2087
  usageMetadata: usageMetadata != null ? usageMetadata : null,
1944
- finishMessage: (_s = candidate == null ? void 0 : candidate.finishMessage) != null ? _s : null,
1945
- serviceTier: (_t = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _t : null
2088
+ finishMessage: (_y = candidate == null ? void 0 : candidate.finishMessage) != null ? _y : null,
2089
+ serviceTier: (_z = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _z : null,
2090
+ ...(candidate == null ? void 0 : candidate.continuationToken) != null && rawFinishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
2091
+ ...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
1946
2092
  }
1947
2093
  },
1948
2094
  request: { body: args },
1949
2095
  response: {
1950
2096
  // TODO timestamp, model id
1951
- id: (_u = response.responseId) != null ? _u : void 0,
2097
+ id: (_A = response.responseId) != null ? _A : void 0,
1952
2098
  headers: responseHeaders,
1953
2099
  body: rawResponse
1954
2100
  }
@@ -1961,27 +2107,40 @@ var GoogleGenerativeAILanguageModel = class {
1961
2107
  options.headers,
1962
2108
  extraHeaders
1963
2109
  );
1964
- const { responseHeaders, value: response } = await (0, import_provider_utils5.postJsonToApi)({
2110
+ const streamGenerateContent = (body) => (0, import_provider_utils5.postJsonToApi)({
1965
2111
  url: `${this.config.baseURL}/${getModelPath(
1966
2112
  this.modelId
1967
2113
  )}:streamGenerateContent?alt=sse`,
1968
2114
  headers,
1969
- body: args,
2115
+ body,
1970
2116
  failedResponseHandler: googleFailedResponseHandler,
1971
2117
  successfulResponseHandler: (0, import_provider_utils5.createEventSourceResponseHandler)(chunkSchema),
1972
2118
  abortSignal: options.abortSignal,
1973
2119
  fetch: this.config.fetch
1974
2120
  });
2121
+ const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
2122
+ const response = continueGoogleStream({
2123
+ stream: firstResponse,
2124
+ getContinuationToken: (chunk) => {
2125
+ var _a, _b;
2126
+ const candidate = (_a = chunk.candidates) == null ? void 0 : _a[0];
2127
+ return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
2128
+ },
2129
+ continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
2130
+ });
1975
2131
  let finishReason = {
1976
2132
  unified: "other",
1977
2133
  raw: void 0
1978
2134
  };
1979
2135
  let usage = void 0;
2136
+ let continuedUsage = void 0;
2137
+ let initialPromptTokenCount = void 0;
1980
2138
  let promptFeedback = null;
1981
2139
  let lastGroundingMetadata = null;
1982
2140
  let lastUrlContextMetadata = null;
1983
2141
  let lastSafetyRatings = null;
1984
2142
  let lastFinishMessage = null;
2143
+ let lastContinuationToken = null;
1985
2144
  let confirmedPromptBlockReason;
1986
2145
  const generateId2 = this.config.generateId;
1987
2146
  let hasToolCalls = false;
@@ -2072,6 +2231,9 @@ var GoogleGenerativeAILanguageModel = class {
2072
2231
  if (candidate.finishMessage != null) {
2073
2232
  lastFinishMessage = candidate.finishMessage;
2074
2233
  }
2234
+ if (candidate.continuationToken != null) {
2235
+ lastContinuationToken = candidate.continuationToken;
2236
+ }
2075
2237
  }
2076
2238
  if (confirmedPromptBlockReason != null || candidate == null) {
2077
2239
  return;
@@ -2356,6 +2518,16 @@ var GoogleGenerativeAILanguageModel = class {
2356
2518
  }
2357
2519
  }
2358
2520
  }
2521
+ if (candidate.finishReason === "CONTINUATION") {
2522
+ if (continuedUsage == null) {
2523
+ initialPromptTokenCount = usage == null ? void 0 : usage.promptTokenCount;
2524
+ }
2525
+ continuedUsage = addGoogleGenerativeAIUsageMetadata(
2526
+ continuedUsage,
2527
+ usage
2528
+ );
2529
+ usage = void 0;
2530
+ }
2359
2531
  if (candidate.finishReason != null) {
2360
2532
  finishReason = {
2361
2533
  unified: mapGoogleGenerativeAIFinishReason({
@@ -2368,6 +2540,7 @@ var GoogleGenerativeAILanguageModel = class {
2368
2540
  },
2369
2541
  flush(controller) {
2370
2542
  var _a;
2543
+ usage = addGoogleGenerativeAIUsageMetadata(continuedUsage, usage);
2371
2544
  if (currentTextBlockId !== null) {
2372
2545
  controller.enqueue({
2373
2546
  type: "text-end",
@@ -2392,7 +2565,12 @@ var GoogleGenerativeAILanguageModel = class {
2392
2565
  safetyRatings: lastSafetyRatings,
2393
2566
  usageMetadata: usage != null ? usage : null,
2394
2567
  finishMessage: lastFinishMessage,
2395
- serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null
2568
+ serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null,
2569
+ // Only surface the token when the response stopped early
2570
+ // (budget used up, or continuing failed), not for the
2571
+ // checkpoint tokens of a stream that finished normally.
2572
+ ...lastContinuationToken != null && (finishReason.raw === "MAX_TOKENS" || finishReason.raw === "CONTINUATION") ? { continuationToken: lastContinuationToken } : {},
2573
+ ...initialPromptTokenCount != null ? { initialPromptTokenCount } : {}
2396
2574
  }
2397
2575
  }
2398
2576
  });
@@ -2664,6 +2842,7 @@ var responseSchema = (0, import_provider_utils5.lazySchema)(
2664
2842
  content: getContentSchema().nullish().or(import_v43.z.object({}).strict()),
2665
2843
  finishReason: import_v43.z.string().nullish(),
2666
2844
  finishMessage: import_v43.z.string().nullish(),
2845
+ continuationToken: import_v43.z.string().nullish(),
2667
2846
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish(),
2668
2847
  groundingMetadata: getGroundingMetadataSchema().nullish(),
2669
2848
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
@@ -2686,6 +2865,7 @@ var chunkSchema = (0, import_provider_utils5.lazySchema)(
2686
2865
  content: getContentSchema().nullish(),
2687
2866
  finishReason: import_v43.z.string().nullish(),
2688
2867
  finishMessage: import_v43.z.string().nullish(),
2868
+ continuationToken: import_v43.z.string().nullish(),
2689
2869
  safetyRatings: import_v43.z.array(getSafetyRatingSchema()).nullish(),
2690
2870
  groundingMetadata: getGroundingMetadataSchema().nullish(),
2691
2871
  urlContextMetadata: getUrlContextMetadataSchema().nullish()
@@ -5575,7 +5755,7 @@ var GoogleInteractionsLanguageModel = class {
5575
5755
  }
5576
5756
  async doGenerate(options) {
5577
5757
  var _a, _b, _c, _d, _e, _f;
5578
- const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
5758
+ const { args, warnings, isAgent, isBackground, pollingTimeoutMs } = await this.getArgs(options);
5579
5759
  const url = `${this.config.baseURL}/interactions`;
5580
5760
  const mergedHeaders = (0, import_provider_utils19.combineHeaders)(
5581
5761
  INTERACTIONS_API_REVISION_HEADER,
@@ -5598,7 +5778,7 @@ var GoogleInteractionsLanguageModel = class {
5598
5778
  value: response,
5599
5779
  rawValue: rawResponse
5600
5780
  } = postResult;
5601
- if (isAgent && !isTerminalStatus(response.status)) {
5781
+ if ((isAgent || isBackground) && !isTerminalStatus(response.status)) {
5602
5782
  const polled = await pollGoogleInteractionUntilTerminal({
5603
5783
  baseURL: this.config.baseURL,
5604
5784
  interactionId: response.id,