@ai-sdk/google 3.0.131 → 3.0.132
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 +8 -0
- package/dist/index.d.mts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +216 -36
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +216 -36
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +3 -2
- package/dist/internal/index.d.ts +3 -2
- package/dist/internal/index.js +215 -35
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +215 -35
- package/dist/internal/index.mjs.map +1 -1
- package/docs/15-google-generative-ai.mdx +137 -0
- package/package.json +3 -3
- package/src/continue-google-stream.ts +75 -0
- package/src/convert-google-generative-ai-usage.ts +75 -0
- package/src/google-generative-ai-language-model.ts +138 -24
- package/src/google-generative-ai-options.ts +12 -0
- package/src/google-generative-ai-prompt.ts +18 -0
- package/src/interactions/google-interactions-language-model-options.ts +1 -0
- package/src/interactions/google-interactions-language-model.ts +6 -6
- package/src/map-google-generative-ai-finish-reason.ts +3 -0
package/dist/internal/index.mjs
CHANGED
|
@@ -12,6 +12,53 @@ import {
|
|
|
12
12
|
} from "@ai-sdk/provider-utils";
|
|
13
13
|
import { z as z3 } from "zod/v4";
|
|
14
14
|
|
|
15
|
+
// src/continue-google-stream.ts
|
|
16
|
+
function continueGoogleStream({
|
|
17
|
+
stream,
|
|
18
|
+
getContinuationToken,
|
|
19
|
+
continueStream
|
|
20
|
+
}) {
|
|
21
|
+
let reader = stream.getReader();
|
|
22
|
+
let pendingContinuationToken;
|
|
23
|
+
return new ReadableStream({
|
|
24
|
+
async pull(controller) {
|
|
25
|
+
while (true) {
|
|
26
|
+
const { done, value } = await reader.read();
|
|
27
|
+
if (!done) {
|
|
28
|
+
if (value.success) {
|
|
29
|
+
const token = getContinuationToken(value.value);
|
|
30
|
+
if (token != null) {
|
|
31
|
+
pendingContinuationToken = token;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
controller.enqueue(value);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (pendingContinuationToken == null) {
|
|
38
|
+
controller.close();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const continuationToken = pendingContinuationToken;
|
|
42
|
+
pendingContinuationToken = void 0;
|
|
43
|
+
try {
|
|
44
|
+
reader = (await continueStream(continuationToken)).getReader();
|
|
45
|
+
} catch (error) {
|
|
46
|
+
controller.enqueue({
|
|
47
|
+
success: false,
|
|
48
|
+
error,
|
|
49
|
+
rawValue: void 0
|
|
50
|
+
});
|
|
51
|
+
controller.close();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
async cancel(reason) {
|
|
57
|
+
await reader.cancel(reason);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
15
62
|
// src/convert-google-generative-ai-usage.ts
|
|
16
63
|
function convertGoogleGenerativeAIUsage(usage) {
|
|
17
64
|
var _a, _b, _c, _d;
|
|
@@ -50,6 +97,63 @@ function convertGoogleGenerativeAIUsage(usage) {
|
|
|
50
97
|
raw: usage
|
|
51
98
|
};
|
|
52
99
|
}
|
|
100
|
+
function addGoogleGenerativeAIUsageMetadata(a, b) {
|
|
101
|
+
if (a == null || b == null) {
|
|
102
|
+
return b != null ? b : a;
|
|
103
|
+
}
|
|
104
|
+
return {
|
|
105
|
+
...a,
|
|
106
|
+
...b,
|
|
107
|
+
promptTokenCount: addCounts(a.promptTokenCount, b.promptTokenCount),
|
|
108
|
+
candidatesTokenCount: addCounts(
|
|
109
|
+
a.candidatesTokenCount,
|
|
110
|
+
b.candidatesTokenCount
|
|
111
|
+
),
|
|
112
|
+
toolUsePromptTokenCount: addCounts(
|
|
113
|
+
a.toolUsePromptTokenCount,
|
|
114
|
+
b.toolUsePromptTokenCount
|
|
115
|
+
),
|
|
116
|
+
totalTokenCount: addCounts(a.totalTokenCount, b.totalTokenCount),
|
|
117
|
+
cachedContentTokenCount: addCounts(
|
|
118
|
+
a.cachedContentTokenCount,
|
|
119
|
+
b.cachedContentTokenCount
|
|
120
|
+
),
|
|
121
|
+
thoughtsTokenCount: addCounts(a.thoughtsTokenCount, b.thoughtsTokenCount),
|
|
122
|
+
promptTokensDetails: addTokenDetails(
|
|
123
|
+
a.promptTokensDetails,
|
|
124
|
+
b.promptTokensDetails
|
|
125
|
+
),
|
|
126
|
+
cacheTokensDetails: addTokenDetails(
|
|
127
|
+
a.cacheTokensDetails,
|
|
128
|
+
b.cacheTokensDetails
|
|
129
|
+
),
|
|
130
|
+
candidatesTokensDetails: addTokenDetails(
|
|
131
|
+
a.candidatesTokensDetails,
|
|
132
|
+
b.candidatesTokensDetails
|
|
133
|
+
),
|
|
134
|
+
toolUsePromptTokensDetails: addTokenDetails(
|
|
135
|
+
a.toolUsePromptTokensDetails,
|
|
136
|
+
b.toolUsePromptTokensDetails
|
|
137
|
+
)
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
function addCounts(a, b) {
|
|
141
|
+
return a == null && b == null ? b != null ? b : a : (a != null ? a : 0) + (b != null ? b : 0);
|
|
142
|
+
}
|
|
143
|
+
function addTokenDetails(a, b) {
|
|
144
|
+
var _a;
|
|
145
|
+
if (a == null || b == null) {
|
|
146
|
+
return b != null ? b : a;
|
|
147
|
+
}
|
|
148
|
+
const tokenCounts = /* @__PURE__ */ new Map();
|
|
149
|
+
for (const { modality, tokenCount } of [...a, ...b]) {
|
|
150
|
+
tokenCounts.set(modality, ((_a = tokenCounts.get(modality)) != null ? _a : 0) + tokenCount);
|
|
151
|
+
}
|
|
152
|
+
return Array.from(tokenCounts, ([modality, tokenCount]) => ({
|
|
153
|
+
modality,
|
|
154
|
+
tokenCount
|
|
155
|
+
}));
|
|
156
|
+
}
|
|
53
157
|
|
|
54
158
|
// src/convert-json-schema-to-openapi-schema.ts
|
|
55
159
|
import {
|
|
@@ -979,6 +1083,16 @@ var googleLanguageModelOptions = lazySchema2(
|
|
|
979
1083
|
* `serviceTier` body field. Gemini API only.
|
|
980
1084
|
*/
|
|
981
1085
|
serviceTier: z2.enum(["standard", "flex", "priority"]).optional(),
|
|
1086
|
+
/**
|
|
1087
|
+
* Optional. Resumes a response that stopped with `MAX_TOKENS` because it
|
|
1088
|
+
* used up its `maxOutputTokens` budget (Gemini 4 long decode). Pass the
|
|
1089
|
+
* `continuationToken` from the previous response's provider metadata
|
|
1090
|
+
* together with the original prompt and a higher `maxOutputTokens` (the
|
|
1091
|
+
* budget is cumulative across requests). Requests that reach the
|
|
1092
|
+
* per-request cap while budget remains (`CONTINUATION`) are continued
|
|
1093
|
+
* automatically. Sent as the `continuationToken` body field.
|
|
1094
|
+
*/
|
|
1095
|
+
continuationToken: z2.string().optional(),
|
|
982
1096
|
/**
|
|
983
1097
|
* Optional. Vertex AI only. Sent as the
|
|
984
1098
|
* `X-Vertex-AI-LLM-Shared-Request-Type` request header to select a
|
|
@@ -1529,6 +1643,9 @@ function mapGoogleGenerativeAIFinishReason({
|
|
|
1529
1643
|
case "STOP":
|
|
1530
1644
|
return hasToolCalls ? "tool-calls" : "stop";
|
|
1531
1645
|
case "MAX_TOKENS":
|
|
1646
|
+
// Gemini 4 long decode: the per-request cap was reached while budget
|
|
1647
|
+
// remains. Continued automatically; only surfaced if continuing fails.
|
|
1648
|
+
case "CONTINUATION":
|
|
1532
1649
|
return "length";
|
|
1533
1650
|
case "IMAGE_SAFETY":
|
|
1534
1651
|
case "RECITATION":
|
|
@@ -1760,7 +1877,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1760
1877
|
toolConfig,
|
|
1761
1878
|
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent,
|
|
1762
1879
|
labels: googleOptions == null ? void 0 : googleOptions.labels,
|
|
1763
|
-
serviceTier: bodyServiceTier
|
|
1880
|
+
serviceTier: bodyServiceTier,
|
|
1881
|
+
...(googleOptions == null ? void 0 : googleOptions.continuationToken) != null && {
|
|
1882
|
+
continuationToken: googleOptions.continuationToken
|
|
1883
|
+
}
|
|
1764
1884
|
},
|
|
1765
1885
|
warnings: [...warnings, ...toolWarnings],
|
|
1766
1886
|
providerOptionsName,
|
|
@@ -1768,42 +1888,66 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1768
1888
|
};
|
|
1769
1889
|
}
|
|
1770
1890
|
async doGenerate(options) {
|
|
1771
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q, _r, _s, _t, _u;
|
|
1891
|
+
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;
|
|
1772
1892
|
const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
|
|
1773
1893
|
const mergedHeaders = combineHeaders(
|
|
1774
1894
|
await resolve(this.config.headers),
|
|
1775
1895
|
options.headers,
|
|
1776
1896
|
extraHeaders
|
|
1777
1897
|
);
|
|
1778
|
-
const {
|
|
1779
|
-
responseHeaders,
|
|
1780
|
-
value: response,
|
|
1781
|
-
rawValue: rawResponse
|
|
1782
|
-
} = await postJsonToApi({
|
|
1898
|
+
const generateContent = (body) => postJsonToApi({
|
|
1783
1899
|
url: `${this.config.baseURL}/${getModelPath(
|
|
1784
1900
|
this.modelId
|
|
1785
1901
|
)}:generateContent`,
|
|
1786
1902
|
headers: mergedHeaders,
|
|
1787
|
-
body
|
|
1903
|
+
body,
|
|
1788
1904
|
failedResponseHandler: googleFailedResponseHandler,
|
|
1789
1905
|
successfulResponseHandler: createJsonResponseHandler(responseSchema),
|
|
1790
1906
|
abortSignal: options.abortSignal,
|
|
1791
1907
|
fetch: this.config.fetch
|
|
1792
1908
|
});
|
|
1793
|
-
|
|
1794
|
-
|
|
1909
|
+
let {
|
|
1910
|
+
responseHeaders,
|
|
1911
|
+
value: response,
|
|
1912
|
+
rawValue: rawResponse
|
|
1913
|
+
} = await generateContent(args);
|
|
1914
|
+
const continuedParts = [];
|
|
1915
|
+
let continuedUsageMetadata;
|
|
1916
|
+
const firstPromptTokenCount = (_a = response.usageMetadata) == null ? void 0 : _a.promptTokenCount;
|
|
1917
|
+
while (true) {
|
|
1918
|
+
const candidate2 = (_b = response.candidates) == null ? void 0 : _b[0];
|
|
1919
|
+
const continuationToken = (candidate2 == null ? void 0 : candidate2.finishReason) === "CONTINUATION" ? candidate2.continuationToken : void 0;
|
|
1920
|
+
if (continuationToken == null) {
|
|
1921
|
+
break;
|
|
1922
|
+
}
|
|
1923
|
+
continuedParts.push(...(_d = (_c = candidate2 == null ? void 0 : candidate2.content) == null ? void 0 : _c.parts) != null ? _d : []);
|
|
1924
|
+
continuedUsageMetadata = addGoogleGenerativeAIUsageMetadata(
|
|
1925
|
+
continuedUsageMetadata,
|
|
1926
|
+
(_e = response.usageMetadata) != null ? _e : void 0
|
|
1927
|
+
);
|
|
1928
|
+
({
|
|
1929
|
+
responseHeaders,
|
|
1930
|
+
value: response,
|
|
1931
|
+
rawValue: rawResponse
|
|
1932
|
+
} = await generateContent({ ...args, continuationToken }));
|
|
1933
|
+
}
|
|
1934
|
+
const candidate = (_f = response.candidates) == null ? void 0 : _f[0];
|
|
1935
|
+
const promptBlockReason = (_g = response.promptFeedback) == null ? void 0 : _g.blockReason;
|
|
1795
1936
|
const confirmedPromptBlockReason = isConfirmedPromptBlockReason(
|
|
1796
1937
|
promptBlockReason
|
|
1797
1938
|
) ? promptBlockReason : void 0;
|
|
1798
1939
|
const isPromptBlocked = (candidate == null ? void 0 : candidate.finishReason) == null && confirmedPromptBlockReason != null;
|
|
1799
|
-
const rawFinishReason = (
|
|
1940
|
+
const rawFinishReason = (_h = candidate == null ? void 0 : candidate.finishReason) != null ? _h : confirmedPromptBlockReason;
|
|
1800
1941
|
const content = [];
|
|
1801
|
-
const parts = (
|
|
1802
|
-
const usageMetadata =
|
|
1942
|
+
const parts = [...continuedParts, ...(_j = (_i = candidate == null ? void 0 : candidate.content) == null ? void 0 : _i.parts) != null ? _j : []];
|
|
1943
|
+
const usageMetadata = addGoogleGenerativeAIUsageMetadata(
|
|
1944
|
+
continuedUsageMetadata,
|
|
1945
|
+
(_k = response.usageMetadata) != null ? _k : void 0
|
|
1946
|
+
);
|
|
1803
1947
|
let lastCodeExecutionToolCallId;
|
|
1804
1948
|
let lastServerToolCallId;
|
|
1805
1949
|
for (const part of parts) {
|
|
1806
|
-
if ("executableCode" in part && ((
|
|
1950
|
+
if ("executableCode" in part && ((_l = part.executableCode) == null ? void 0 : _l.code)) {
|
|
1807
1951
|
const toolCallId = this.config.generateId();
|
|
1808
1952
|
lastCodeExecutionToolCallId = toolCallId;
|
|
1809
1953
|
content.push({
|
|
@@ -1821,7 +1965,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1821
1965
|
toolName: "code_execution",
|
|
1822
1966
|
result: {
|
|
1823
1967
|
outcome: part.codeExecutionResult.outcome,
|
|
1824
|
-
output: (
|
|
1968
|
+
output: (_m = part.codeExecutionResult.output) != null ? _m : ""
|
|
1825
1969
|
}
|
|
1826
1970
|
});
|
|
1827
1971
|
} else if ("text" in part && part.text != null) {
|
|
@@ -1845,9 +1989,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1845
1989
|
} else if ("functionCall" in part && part.functionCall.name != null) {
|
|
1846
1990
|
content.push({
|
|
1847
1991
|
type: "tool-call",
|
|
1848
|
-
toolCallId: (
|
|
1992
|
+
toolCallId: (_n = part.functionCall.id) != null ? _n : this.config.generateId(),
|
|
1849
1993
|
toolName: part.functionCall.name,
|
|
1850
|
-
input: JSON.stringify((
|
|
1994
|
+
input: JSON.stringify((_o = part.functionCall.args) != null ? _o : {}),
|
|
1851
1995
|
providerMetadata: part.thoughtSignature ? {
|
|
1852
1996
|
[providerOptionsName]: {
|
|
1853
1997
|
thoughtSignature: part.thoughtSignature
|
|
@@ -1869,13 +2013,13 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1869
2013
|
} : void 0
|
|
1870
2014
|
});
|
|
1871
2015
|
} else if ("toolCall" in part && part.toolCall) {
|
|
1872
|
-
const toolCallId = (
|
|
2016
|
+
const toolCallId = (_p = part.toolCall.id) != null ? _p : this.config.generateId();
|
|
1873
2017
|
lastServerToolCallId = toolCallId;
|
|
1874
2018
|
content.push({
|
|
1875
2019
|
type: "tool-call",
|
|
1876
2020
|
toolCallId,
|
|
1877
2021
|
toolName: `server:${part.toolCall.toolType}`,
|
|
1878
|
-
input: JSON.stringify((
|
|
2022
|
+
input: JSON.stringify((_q = part.toolCall.args) != null ? _q : {}),
|
|
1879
2023
|
providerExecuted: true,
|
|
1880
2024
|
dynamic: true,
|
|
1881
2025
|
providerMetadata: part.thoughtSignature ? {
|
|
@@ -1892,12 +2036,12 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1892
2036
|
}
|
|
1893
2037
|
});
|
|
1894
2038
|
} else if ("toolResponse" in part && part.toolResponse) {
|
|
1895
|
-
const responseToolCallId = (
|
|
2039
|
+
const responseToolCallId = (_r = lastServerToolCallId != null ? lastServerToolCallId : part.toolResponse.id) != null ? _r : this.config.generateId();
|
|
1896
2040
|
content.push({
|
|
1897
2041
|
type: "tool-result",
|
|
1898
2042
|
toolCallId: responseToolCallId,
|
|
1899
2043
|
toolName: `server:${part.toolResponse.toolType}`,
|
|
1900
|
-
result: (
|
|
2044
|
+
result: (_s = part.toolResponse.response) != null ? _s : {},
|
|
1901
2045
|
providerMetadata: part.thoughtSignature ? {
|
|
1902
2046
|
[providerOptionsName]: {
|
|
1903
2047
|
thoughtSignature: part.thoughtSignature,
|
|
@@ -1914,10 +2058,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1914
2058
|
lastServerToolCallId = void 0;
|
|
1915
2059
|
}
|
|
1916
2060
|
}
|
|
1917
|
-
const sources = (
|
|
2061
|
+
const sources = (_t = extractSources({
|
|
1918
2062
|
groundingMetadata: candidate == null ? void 0 : candidate.groundingMetadata,
|
|
1919
2063
|
generateId: this.config.generateId
|
|
1920
|
-
})) != null ?
|
|
2064
|
+
})) != null ? _t : [];
|
|
1921
2065
|
for (const source of sources) {
|
|
1922
2066
|
content.push(source);
|
|
1923
2067
|
}
|
|
@@ -1937,19 +2081,21 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1937
2081
|
warnings,
|
|
1938
2082
|
providerMetadata: {
|
|
1939
2083
|
[providerOptionsName]: {
|
|
1940
|
-
promptFeedback: (
|
|
1941
|
-
groundingMetadata: (
|
|
1942
|
-
urlContextMetadata: (
|
|
1943
|
-
safetyRatings: (
|
|
2084
|
+
promptFeedback: (_u = response.promptFeedback) != null ? _u : null,
|
|
2085
|
+
groundingMetadata: (_v = candidate == null ? void 0 : candidate.groundingMetadata) != null ? _v : null,
|
|
2086
|
+
urlContextMetadata: (_w = candidate == null ? void 0 : candidate.urlContextMetadata) != null ? _w : null,
|
|
2087
|
+
safetyRatings: (_x = candidate == null ? void 0 : candidate.safetyRatings) != null ? _x : null,
|
|
1944
2088
|
usageMetadata: usageMetadata != null ? usageMetadata : null,
|
|
1945
|
-
finishMessage: (
|
|
1946
|
-
serviceTier: (
|
|
2089
|
+
finishMessage: (_y = candidate == null ? void 0 : candidate.finishMessage) != null ? _y : null,
|
|
2090
|
+
serviceTier: (_z = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _z : null,
|
|
2091
|
+
...(candidate == null ? void 0 : candidate.continuationToken) != null ? { continuationToken: candidate.continuationToken } : {},
|
|
2092
|
+
...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
|
|
1947
2093
|
}
|
|
1948
2094
|
},
|
|
1949
2095
|
request: { body: args },
|
|
1950
2096
|
response: {
|
|
1951
2097
|
// TODO timestamp, model id
|
|
1952
|
-
id: (
|
|
2098
|
+
id: (_A = response.responseId) != null ? _A : void 0,
|
|
1953
2099
|
headers: responseHeaders,
|
|
1954
2100
|
body: rawResponse
|
|
1955
2101
|
}
|
|
@@ -1962,27 +2108,40 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
1962
2108
|
options.headers,
|
|
1963
2109
|
extraHeaders
|
|
1964
2110
|
);
|
|
1965
|
-
const
|
|
2111
|
+
const streamGenerateContent = (body) => postJsonToApi({
|
|
1966
2112
|
url: `${this.config.baseURL}/${getModelPath(
|
|
1967
2113
|
this.modelId
|
|
1968
2114
|
)}:streamGenerateContent?alt=sse`,
|
|
1969
2115
|
headers,
|
|
1970
|
-
body
|
|
2116
|
+
body,
|
|
1971
2117
|
failedResponseHandler: googleFailedResponseHandler,
|
|
1972
2118
|
successfulResponseHandler: createEventSourceResponseHandler(chunkSchema),
|
|
1973
2119
|
abortSignal: options.abortSignal,
|
|
1974
2120
|
fetch: this.config.fetch
|
|
1975
2121
|
});
|
|
2122
|
+
const { responseHeaders, value: firstResponse } = await streamGenerateContent(args);
|
|
2123
|
+
const response = continueGoogleStream({
|
|
2124
|
+
stream: firstResponse,
|
|
2125
|
+
getContinuationToken: (chunk) => {
|
|
2126
|
+
var _a, _b;
|
|
2127
|
+
const candidate = (_a = chunk.candidates) == null ? void 0 : _a[0];
|
|
2128
|
+
return (candidate == null ? void 0 : candidate.finishReason) === "CONTINUATION" ? (_b = candidate.continuationToken) != null ? _b : void 0 : void 0;
|
|
2129
|
+
},
|
|
2130
|
+
continueStream: async (continuationToken) => (await streamGenerateContent({ ...args, continuationToken })).value
|
|
2131
|
+
});
|
|
1976
2132
|
let finishReason = {
|
|
1977
2133
|
unified: "other",
|
|
1978
2134
|
raw: void 0
|
|
1979
2135
|
};
|
|
1980
2136
|
let usage = void 0;
|
|
2137
|
+
let continuedUsage = void 0;
|
|
2138
|
+
let initialPromptTokenCount = void 0;
|
|
1981
2139
|
let promptFeedback = null;
|
|
1982
2140
|
let lastGroundingMetadata = null;
|
|
1983
2141
|
let lastUrlContextMetadata = null;
|
|
1984
2142
|
let lastSafetyRatings = null;
|
|
1985
2143
|
let lastFinishMessage = null;
|
|
2144
|
+
let lastContinuationToken = null;
|
|
1986
2145
|
let confirmedPromptBlockReason;
|
|
1987
2146
|
const generateId2 = this.config.generateId;
|
|
1988
2147
|
let hasToolCalls = false;
|
|
@@ -2073,6 +2232,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2073
2232
|
if (candidate.finishMessage != null) {
|
|
2074
2233
|
lastFinishMessage = candidate.finishMessage;
|
|
2075
2234
|
}
|
|
2235
|
+
if (candidate.continuationToken != null) {
|
|
2236
|
+
lastContinuationToken = candidate.continuationToken;
|
|
2237
|
+
}
|
|
2076
2238
|
}
|
|
2077
2239
|
if (confirmedPromptBlockReason != null || candidate == null) {
|
|
2078
2240
|
return;
|
|
@@ -2357,6 +2519,16 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2357
2519
|
}
|
|
2358
2520
|
}
|
|
2359
2521
|
}
|
|
2522
|
+
if (candidate.finishReason === "CONTINUATION") {
|
|
2523
|
+
if (continuedUsage == null) {
|
|
2524
|
+
initialPromptTokenCount = usage == null ? void 0 : usage.promptTokenCount;
|
|
2525
|
+
}
|
|
2526
|
+
continuedUsage = addGoogleGenerativeAIUsageMetadata(
|
|
2527
|
+
continuedUsage,
|
|
2528
|
+
usage
|
|
2529
|
+
);
|
|
2530
|
+
usage = void 0;
|
|
2531
|
+
}
|
|
2360
2532
|
if (candidate.finishReason != null) {
|
|
2361
2533
|
finishReason = {
|
|
2362
2534
|
unified: mapGoogleGenerativeAIFinishReason({
|
|
@@ -2369,6 +2541,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2369
2541
|
},
|
|
2370
2542
|
flush(controller) {
|
|
2371
2543
|
var _a;
|
|
2544
|
+
usage = addGoogleGenerativeAIUsageMetadata(continuedUsage, usage);
|
|
2372
2545
|
if (currentTextBlockId !== null) {
|
|
2373
2546
|
controller.enqueue({
|
|
2374
2547
|
type: "text-end",
|
|
@@ -2393,7 +2566,12 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2393
2566
|
safetyRatings: lastSafetyRatings,
|
|
2394
2567
|
usageMetadata: usage != null ? usage : null,
|
|
2395
2568
|
finishMessage: lastFinishMessage,
|
|
2396
|
-
serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null
|
|
2569
|
+
serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null,
|
|
2570
|
+
// Only surface the token when the response stopped early
|
|
2571
|
+
// (budget used up, or continuing failed), not for the
|
|
2572
|
+
// checkpoint tokens of a stream that finished normally.
|
|
2573
|
+
...lastContinuationToken != null && (finishReason.raw === "MAX_TOKENS" || finishReason.raw === "CONTINUATION") ? { continuationToken: lastContinuationToken } : {},
|
|
2574
|
+
...initialPromptTokenCount != null ? { initialPromptTokenCount } : {}
|
|
2397
2575
|
}
|
|
2398
2576
|
}
|
|
2399
2577
|
});
|
|
@@ -2665,6 +2843,7 @@ var responseSchema = lazySchema3(
|
|
|
2665
2843
|
content: getContentSchema().nullish().or(z3.object({}).strict()),
|
|
2666
2844
|
finishReason: z3.string().nullish(),
|
|
2667
2845
|
finishMessage: z3.string().nullish(),
|
|
2846
|
+
continuationToken: z3.string().nullish(),
|
|
2668
2847
|
safetyRatings: z3.array(getSafetyRatingSchema()).nullish(),
|
|
2669
2848
|
groundingMetadata: getGroundingMetadataSchema().nullish(),
|
|
2670
2849
|
urlContextMetadata: getUrlContextMetadataSchema().nullish()
|
|
@@ -2687,6 +2866,7 @@ var chunkSchema = lazySchema3(
|
|
|
2687
2866
|
content: getContentSchema().nullish(),
|
|
2688
2867
|
finishReason: z3.string().nullish(),
|
|
2689
2868
|
finishMessage: z3.string().nullish(),
|
|
2869
|
+
continuationToken: z3.string().nullish(),
|
|
2690
2870
|
safetyRatings: z3.array(getSafetyRatingSchema()).nullish(),
|
|
2691
2871
|
groundingMetadata: getGroundingMetadataSchema().nullish(),
|
|
2692
2872
|
urlContextMetadata: getUrlContextMetadataSchema().nullish()
|
|
@@ -5624,7 +5804,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5624
5804
|
}
|
|
5625
5805
|
async doGenerate(options) {
|
|
5626
5806
|
var _a, _b, _c, _d, _e, _f;
|
|
5627
|
-
const { args, warnings, isAgent, pollingTimeoutMs } = await this.getArgs(options);
|
|
5807
|
+
const { args, warnings, isAgent, isBackground, pollingTimeoutMs } = await this.getArgs(options);
|
|
5628
5808
|
const url = `${this.config.baseURL}/interactions`;
|
|
5629
5809
|
const mergedHeaders = combineHeaders3(
|
|
5630
5810
|
INTERACTIONS_API_REVISION_HEADER,
|
|
@@ -5647,7 +5827,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5647
5827
|
value: response,
|
|
5648
5828
|
rawValue: rawResponse
|
|
5649
5829
|
} = postResult;
|
|
5650
|
-
if (isAgent && !isTerminalStatus(response.status)) {
|
|
5830
|
+
if ((isAgent || isBackground) && !isTerminalStatus(response.status)) {
|
|
5651
5831
|
const polled = await pollGoogleInteractionUntilTerminal({
|
|
5652
5832
|
baseURL: this.config.baseURL,
|
|
5653
5833
|
interactionId: response.id,
|