@ai-sdk/google 3.0.142 → 3.0.144
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 +19 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +98 -13
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -13
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +97 -12
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +97 -12
- package/dist/internal/index.mjs.map +1 -1
- package/docs/15-google-generative-ai.mdx +18 -0
- package/package.json +2 -2
- package/src/convert-json-schema-to-openapi-schema.ts +85 -0
- package/src/google-generative-ai-language-model.ts +8 -2
- package/src/google-json-accumulator.ts +49 -0
- package/src/interactions/google-interactions-language-model-options.ts +2 -0
- package/src/interactions/google-interactions-language-model.ts +1 -0
- package/src/interactions/google-interactions-prompt.ts +1 -0
package/dist/internal/index.mjs
CHANGED
|
@@ -162,25 +162,54 @@ function addTokenDetails(a, b) {
|
|
|
162
162
|
import {
|
|
163
163
|
UnsupportedFunctionalityError
|
|
164
164
|
} from "@ai-sdk/provider";
|
|
165
|
+
var maxSchemaDepth = 100;
|
|
166
|
+
var maxSchemaNodes = 1e4;
|
|
167
|
+
var maxSchemaSize = 1e6;
|
|
165
168
|
var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
|
|
166
169
|
function isRecursiveJSONSchemaReferenceError(error) {
|
|
167
170
|
return UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
|
|
168
171
|
}
|
|
169
172
|
function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
173
|
+
consumeSchemaBudget(jsonSchema, {
|
|
174
|
+
nodes: maxSchemaNodes,
|
|
175
|
+
size: maxSchemaSize
|
|
176
|
+
});
|
|
170
177
|
const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
|
|
171
178
|
return convertJSONSchemaDefinition(jsonSchema, isRoot, {
|
|
172
179
|
definitions: rootSchema == null ? void 0 : rootSchema.definitions,
|
|
173
180
|
dollarDefinitions: rootSchema == null ? void 0 : rootSchema.$defs,
|
|
174
|
-
resolvingReferences: /* @__PURE__ */ new Set()
|
|
181
|
+
resolvingReferences: /* @__PURE__ */ new Set(),
|
|
182
|
+
budget: { nodes: maxSchemaNodes, size: maxSchemaSize },
|
|
183
|
+
depth: 0
|
|
175
184
|
});
|
|
176
185
|
}
|
|
177
186
|
function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
178
187
|
if (jsonSchema == null) {
|
|
179
188
|
return void 0;
|
|
180
189
|
}
|
|
190
|
+
referenceContext.budget.nodes--;
|
|
191
|
+
referenceContext.budget.size -= 128;
|
|
192
|
+
checkSchemaBudget(referenceContext.budget, referenceContext.depth);
|
|
193
|
+
referenceContext = {
|
|
194
|
+
...referenceContext,
|
|
195
|
+
depth: referenceContext.depth + 1
|
|
196
|
+
};
|
|
181
197
|
if (typeof jsonSchema === "boolean") {
|
|
182
198
|
return { type: "boolean", properties: {} };
|
|
183
199
|
}
|
|
200
|
+
consumeSchemaBudget(
|
|
201
|
+
[
|
|
202
|
+
jsonSchema.$ref,
|
|
203
|
+
jsonSchema.type,
|
|
204
|
+
jsonSchema.description,
|
|
205
|
+
jsonSchema.required,
|
|
206
|
+
jsonSchema.format,
|
|
207
|
+
jsonSchema.const,
|
|
208
|
+
jsonSchema.enum,
|
|
209
|
+
jsonSchema.minLength
|
|
210
|
+
],
|
|
211
|
+
referenceContext.budget
|
|
212
|
+
);
|
|
184
213
|
if (jsonSchema.$ref != null) {
|
|
185
214
|
return convertJSONSchemaReference({
|
|
186
215
|
jsonSchema,
|
|
@@ -244,6 +273,7 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
|
244
273
|
if (properties != null) {
|
|
245
274
|
result.properties = Object.entries(properties).reduce(
|
|
246
275
|
(acc, [key, value]) => {
|
|
276
|
+
consumeSchemaBudget(key, referenceContext.budget);
|
|
247
277
|
acc[key] = convertJSONSchemaDefinition(value, false, referenceContext);
|
|
248
278
|
return acc;
|
|
249
279
|
},
|
|
@@ -299,6 +329,38 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
|
299
329
|
}
|
|
300
330
|
return result;
|
|
301
331
|
}
|
|
332
|
+
function checkSchemaBudget(budget, depth) {
|
|
333
|
+
if (budget.nodes < 0 || budget.size < 0 || depth > maxSchemaDepth) {
|
|
334
|
+
throw new UnsupportedFunctionalityError({
|
|
335
|
+
functionality: "JSON Schema conversion exceeding resource limits",
|
|
336
|
+
message: "Google schema conversion exceeded its node, size, or depth limit."
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
function consumeSchemaBudget(value, budget, depth = 0) {
|
|
341
|
+
if (value === void 0) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
344
|
+
budget.nodes--;
|
|
345
|
+
budget.size -= typeof value === "string" ? value.length * 6 + 2 : 32;
|
|
346
|
+
checkSchemaBudget(budget, depth);
|
|
347
|
+
if (Array.isArray(value)) {
|
|
348
|
+
for (const item of value) {
|
|
349
|
+
consumeSchemaBudget(item, budget, depth + 1);
|
|
350
|
+
}
|
|
351
|
+
} else if (value !== null && typeof value === "object") {
|
|
352
|
+
for (const key in value) {
|
|
353
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
354
|
+
consumeSchemaBudget(key, budget, depth + 1);
|
|
355
|
+
consumeSchemaBudget(
|
|
356
|
+
value[key],
|
|
357
|
+
budget,
|
|
358
|
+
depth + 1
|
|
359
|
+
);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
302
364
|
function addEnumToSchema({
|
|
303
365
|
values,
|
|
304
366
|
type,
|
|
@@ -1526,6 +1588,7 @@ var GoogleJSONAccumulator = class {
|
|
|
1526
1588
|
const rawPath = jsonPath.replace(/^\$\./, "");
|
|
1527
1589
|
if (!rawPath || rawPath === "$") continue;
|
|
1528
1590
|
const segments = parsePath(rawPath);
|
|
1591
|
+
if (!normalizeAndCheckIndexes(this.accumulatedArgs, segments)) continue;
|
|
1529
1592
|
const existingValue = getNestedValue(this.accumulatedArgs, segments);
|
|
1530
1593
|
const isStringContinuation = arg.stringValue != null && existingValue !== void 0;
|
|
1531
1594
|
if (isStringContinuation) {
|
|
@@ -1746,6 +1809,25 @@ function getNestedValue(obj, segments) {
|
|
|
1746
1809
|
}
|
|
1747
1810
|
return current;
|
|
1748
1811
|
}
|
|
1812
|
+
function normalizeAndCheckIndexes(obj, segments) {
|
|
1813
|
+
let current = obj;
|
|
1814
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1815
|
+
let pathSegment = segments[i];
|
|
1816
|
+
if (Array.isArray(current) && typeof pathSegment === "string") {
|
|
1817
|
+
const index = Number(pathSegment);
|
|
1818
|
+
if (Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === pathSegment) {
|
|
1819
|
+
pathSegment = index;
|
|
1820
|
+
segments[i] = index;
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
if (typeof pathSegment === "number") {
|
|
1824
|
+
const length = Array.isArray(current) ? current.length : current == null ? 0 : void 0;
|
|
1825
|
+
if (length !== void 0 && pathSegment > length) return false;
|
|
1826
|
+
}
|
|
1827
|
+
current = current != null && typeof current === "object" && hasOwnProperty(current, pathSegment) ? current[pathSegment] : void 0;
|
|
1828
|
+
}
|
|
1829
|
+
return true;
|
|
1830
|
+
}
|
|
1749
1831
|
function setNestedValue(obj, segments, value) {
|
|
1750
1832
|
let current = obj;
|
|
1751
1833
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
@@ -2020,7 +2102,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2020
2102
|
};
|
|
2021
2103
|
}
|
|
2022
2104
|
async doGenerate(options) {
|
|
2023
|
-
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, _B;
|
|
2105
|
+
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, _B, _C;
|
|
2024
2106
|
const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
|
|
2025
2107
|
const mergedHeaders = combineHeaders(
|
|
2026
2108
|
await resolve(this.config.headers),
|
|
@@ -2222,7 +2304,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2222
2304
|
safetyRatings: (_y = candidate == null ? void 0 : candidate.safetyRatings) != null ? _y : null,
|
|
2223
2305
|
usageMetadata: usageMetadata != null ? usageMetadata : null,
|
|
2224
2306
|
finishMessage: (_z = candidate == null ? void 0 : candidate.finishMessage) != null ? _z : null,
|
|
2225
|
-
serviceTier: (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : null,
|
|
2307
|
+
serviceTier: (_B = (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _B : null,
|
|
2226
2308
|
...(candidate == null ? void 0 : candidate.continuationToken) != null && rawFinishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
|
|
2227
2309
|
...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
|
|
2228
2310
|
}
|
|
@@ -2230,7 +2312,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2230
2312
|
request: { body: args },
|
|
2231
2313
|
response: {
|
|
2232
2314
|
// TODO timestamp, model id
|
|
2233
|
-
id: (
|
|
2315
|
+
id: (_C = response.responseId) != null ? _C : void 0,
|
|
2234
2316
|
headers: responseHeaders,
|
|
2235
2317
|
body: rawResponse
|
|
2236
2318
|
}
|
|
@@ -2706,7 +2788,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2706
2788
|
}
|
|
2707
2789
|
},
|
|
2708
2790
|
flush(controller) {
|
|
2709
|
-
var _a;
|
|
2791
|
+
var _a, _b;
|
|
2710
2792
|
usage = addGoogleGenerativeAIUsageMetadata(continuedUsage, usage);
|
|
2711
2793
|
if (currentTextBlockId !== null) {
|
|
2712
2794
|
controller.enqueue({
|
|
@@ -2738,7 +2820,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2738
2820
|
safetyRatings: lastSafetyRatings,
|
|
2739
2821
|
usageMetadata: usage != null ? usage : null,
|
|
2740
2822
|
finishMessage: lastFinishMessage,
|
|
2741
|
-
serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null,
|
|
2823
|
+
serviceTier: (_b = (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _b : null,
|
|
2742
2824
|
// Only surface the token when the response stopped early
|
|
2743
2825
|
// (budget used up, or continuing failed), not for the
|
|
2744
2826
|
// checkpoint tokens of a stream that finished normally.
|
|
@@ -4756,6 +4838,8 @@ import { z as z13 } from "zod/v4";
|
|
|
4756
4838
|
var googleInteractionsLanguageModelOptions = lazySchema10(
|
|
4757
4839
|
() => zodSchema10(
|
|
4758
4840
|
z13.object({
|
|
4841
|
+
/** User-defined request metadata, including `safety_identifier`. */
|
|
4842
|
+
labels: z13.record(z13.string(), z13.string()).nullish(),
|
|
4759
4843
|
previousInteractionId: z13.string().nullish(),
|
|
4760
4844
|
store: z13.boolean().nullish(),
|
|
4761
4845
|
agent: z13.string().nullish(),
|
|
@@ -5720,7 +5804,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5720
5804
|
};
|
|
5721
5805
|
}
|
|
5722
5806
|
async getArgs(options) {
|
|
5723
|
-
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, _B, _C, _D, _E, _F;
|
|
5807
|
+
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, _B, _C, _D, _E, _F, _G;
|
|
5724
5808
|
const warnings = [];
|
|
5725
5809
|
const opts = await parseProviderOptions2({
|
|
5726
5810
|
provider: "google",
|
|
@@ -5955,24 +6039,25 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5955
6039
|
const args = pruneUndefined({
|
|
5956
6040
|
...isAgent ? { agent: this.agent } : { model: this.modelId },
|
|
5957
6041
|
input,
|
|
6042
|
+
labels: (_B = opts == null ? void 0 : opts.labels) != null ? _B : void 0,
|
|
5958
6043
|
system_instruction: systemInstruction,
|
|
5959
6044
|
tools: toolsForBody,
|
|
5960
6045
|
response_format: responseFormatEntries.length > 0 ? responseFormatEntries : void 0,
|
|
5961
6046
|
response_modalities: (opts == null ? void 0 : opts.responseModalities) != null ? opts.responseModalities : void 0,
|
|
5962
|
-
previous_interaction_id: (
|
|
5963
|
-
service_tier: (
|
|
5964
|
-
store: (
|
|
6047
|
+
previous_interaction_id: (_C = opts == null ? void 0 : opts.previousInteractionId) != null ? _C : void 0,
|
|
6048
|
+
service_tier: (_D = opts == null ? void 0 : opts.serviceTier) != null ? _D : void 0,
|
|
6049
|
+
store: (_E = opts == null ? void 0 : opts.store) != null ? _E : void 0,
|
|
5965
6050
|
generation_config: generationConfig != null && Object.keys(generationConfig).length > 0 ? generationConfig : void 0,
|
|
5966
6051
|
agent_config: agentConfig,
|
|
5967
6052
|
environment,
|
|
5968
|
-
background: (
|
|
6053
|
+
background: (_F = opts == null ? void 0 : opts.background) != null ? _F : void 0
|
|
5969
6054
|
});
|
|
5970
6055
|
return {
|
|
5971
6056
|
args,
|
|
5972
6057
|
warnings,
|
|
5973
6058
|
isAgent,
|
|
5974
6059
|
isBackground: (opts == null ? void 0 : opts.background) === true,
|
|
5975
|
-
pollingTimeoutMs: (
|
|
6060
|
+
pollingTimeoutMs: (_G = opts == null ? void 0 : opts.pollingTimeoutMs) != null ? _G : void 0
|
|
5976
6061
|
};
|
|
5977
6062
|
}
|
|
5978
6063
|
async doGenerate(options) {
|