@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.js
CHANGED
|
@@ -178,25 +178,54 @@ function addTokenDetails(a, b) {
|
|
|
178
178
|
|
|
179
179
|
// src/convert-json-schema-to-openapi-schema.ts
|
|
180
180
|
var import_provider = require("@ai-sdk/provider");
|
|
181
|
+
var maxSchemaDepth = 100;
|
|
182
|
+
var maxSchemaNodes = 1e4;
|
|
183
|
+
var maxSchemaSize = 1e6;
|
|
181
184
|
var recursiveReferenceFunctionalityPrefix = "recursive JSON Schema reference:";
|
|
182
185
|
function isRecursiveJSONSchemaReferenceError(error) {
|
|
183
186
|
return import_provider.UnsupportedFunctionalityError.isInstance(error) && error.functionality.startsWith(recursiveReferenceFunctionalityPrefix);
|
|
184
187
|
}
|
|
185
188
|
function convertJSONSchemaToOpenAPISchema(jsonSchema, isRoot = true) {
|
|
189
|
+
consumeSchemaBudget(jsonSchema, {
|
|
190
|
+
nodes: maxSchemaNodes,
|
|
191
|
+
size: maxSchemaSize
|
|
192
|
+
});
|
|
186
193
|
const rootSchema = typeof jsonSchema === "object" ? jsonSchema : void 0;
|
|
187
194
|
return convertJSONSchemaDefinition(jsonSchema, isRoot, {
|
|
188
195
|
definitions: rootSchema == null ? void 0 : rootSchema.definitions,
|
|
189
196
|
dollarDefinitions: rootSchema == null ? void 0 : rootSchema.$defs,
|
|
190
|
-
resolvingReferences: /* @__PURE__ */ new Set()
|
|
197
|
+
resolvingReferences: /* @__PURE__ */ new Set(),
|
|
198
|
+
budget: { nodes: maxSchemaNodes, size: maxSchemaSize },
|
|
199
|
+
depth: 0
|
|
191
200
|
});
|
|
192
201
|
}
|
|
193
202
|
function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
194
203
|
if (jsonSchema == null) {
|
|
195
204
|
return void 0;
|
|
196
205
|
}
|
|
206
|
+
referenceContext.budget.nodes--;
|
|
207
|
+
referenceContext.budget.size -= 128;
|
|
208
|
+
checkSchemaBudget(referenceContext.budget, referenceContext.depth);
|
|
209
|
+
referenceContext = {
|
|
210
|
+
...referenceContext,
|
|
211
|
+
depth: referenceContext.depth + 1
|
|
212
|
+
};
|
|
197
213
|
if (typeof jsonSchema === "boolean") {
|
|
198
214
|
return { type: "boolean", properties: {} };
|
|
199
215
|
}
|
|
216
|
+
consumeSchemaBudget(
|
|
217
|
+
[
|
|
218
|
+
jsonSchema.$ref,
|
|
219
|
+
jsonSchema.type,
|
|
220
|
+
jsonSchema.description,
|
|
221
|
+
jsonSchema.required,
|
|
222
|
+
jsonSchema.format,
|
|
223
|
+
jsonSchema.const,
|
|
224
|
+
jsonSchema.enum,
|
|
225
|
+
jsonSchema.minLength
|
|
226
|
+
],
|
|
227
|
+
referenceContext.budget
|
|
228
|
+
);
|
|
200
229
|
if (jsonSchema.$ref != null) {
|
|
201
230
|
return convertJSONSchemaReference({
|
|
202
231
|
jsonSchema,
|
|
@@ -260,6 +289,7 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
|
260
289
|
if (properties != null) {
|
|
261
290
|
result.properties = Object.entries(properties).reduce(
|
|
262
291
|
(acc, [key, value]) => {
|
|
292
|
+
consumeSchemaBudget(key, referenceContext.budget);
|
|
263
293
|
acc[key] = convertJSONSchemaDefinition(value, false, referenceContext);
|
|
264
294
|
return acc;
|
|
265
295
|
},
|
|
@@ -315,6 +345,38 @@ function convertJSONSchemaDefinition(jsonSchema, isRoot, referenceContext) {
|
|
|
315
345
|
}
|
|
316
346
|
return result;
|
|
317
347
|
}
|
|
348
|
+
function checkSchemaBudget(budget, depth) {
|
|
349
|
+
if (budget.nodes < 0 || budget.size < 0 || depth > maxSchemaDepth) {
|
|
350
|
+
throw new import_provider.UnsupportedFunctionalityError({
|
|
351
|
+
functionality: "JSON Schema conversion exceeding resource limits",
|
|
352
|
+
message: "Google schema conversion exceeded its node, size, or depth limit."
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
function consumeSchemaBudget(value, budget, depth = 0) {
|
|
357
|
+
if (value === void 0) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
budget.nodes--;
|
|
361
|
+
budget.size -= typeof value === "string" ? value.length * 6 + 2 : 32;
|
|
362
|
+
checkSchemaBudget(budget, depth);
|
|
363
|
+
if (Array.isArray(value)) {
|
|
364
|
+
for (const item of value) {
|
|
365
|
+
consumeSchemaBudget(item, budget, depth + 1);
|
|
366
|
+
}
|
|
367
|
+
} else if (value !== null && typeof value === "object") {
|
|
368
|
+
for (const key in value) {
|
|
369
|
+
if (Object.prototype.hasOwnProperty.call(value, key)) {
|
|
370
|
+
consumeSchemaBudget(key, budget, depth + 1);
|
|
371
|
+
consumeSchemaBudget(
|
|
372
|
+
value[key],
|
|
373
|
+
budget,
|
|
374
|
+
depth + 1
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
318
380
|
function addEnumToSchema({
|
|
319
381
|
values,
|
|
320
382
|
type,
|
|
@@ -1522,6 +1584,7 @@ var GoogleJSONAccumulator = class {
|
|
|
1522
1584
|
const rawPath = jsonPath.replace(/^\$\./, "");
|
|
1523
1585
|
if (!rawPath || rawPath === "$") continue;
|
|
1524
1586
|
const segments = parsePath(rawPath);
|
|
1587
|
+
if (!normalizeAndCheckIndexes(this.accumulatedArgs, segments)) continue;
|
|
1525
1588
|
const existingValue = getNestedValue(this.accumulatedArgs, segments);
|
|
1526
1589
|
const isStringContinuation = arg.stringValue != null && existingValue !== void 0;
|
|
1527
1590
|
if (isStringContinuation) {
|
|
@@ -1742,6 +1805,25 @@ function getNestedValue(obj, segments) {
|
|
|
1742
1805
|
}
|
|
1743
1806
|
return current;
|
|
1744
1807
|
}
|
|
1808
|
+
function normalizeAndCheckIndexes(obj, segments) {
|
|
1809
|
+
let current = obj;
|
|
1810
|
+
for (let i = 0; i < segments.length; i++) {
|
|
1811
|
+
let pathSegment = segments[i];
|
|
1812
|
+
if (Array.isArray(current) && typeof pathSegment === "string") {
|
|
1813
|
+
const index = Number(pathSegment);
|
|
1814
|
+
if (Number.isInteger(index) && index >= 0 && index < 2 ** 32 - 1 && String(index) === pathSegment) {
|
|
1815
|
+
pathSegment = index;
|
|
1816
|
+
segments[i] = index;
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
if (typeof pathSegment === "number") {
|
|
1820
|
+
const length = Array.isArray(current) ? current.length : current == null ? 0 : void 0;
|
|
1821
|
+
if (length !== void 0 && pathSegment > length) return false;
|
|
1822
|
+
}
|
|
1823
|
+
current = current != null && typeof current === "object" && hasOwnProperty(current, pathSegment) ? current[pathSegment] : void 0;
|
|
1824
|
+
}
|
|
1825
|
+
return true;
|
|
1826
|
+
}
|
|
1745
1827
|
function setNestedValue(obj, segments, value) {
|
|
1746
1828
|
let current = obj;
|
|
1747
1829
|
for (let i = 0; i < segments.length - 1; i++) {
|
|
@@ -2016,7 +2098,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2016
2098
|
};
|
|
2017
2099
|
}
|
|
2018
2100
|
async doGenerate(options) {
|
|
2019
|
-
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;
|
|
2101
|
+
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;
|
|
2020
2102
|
const { args, warnings, providerOptionsName, extraHeaders } = await this.getArgs(options);
|
|
2021
2103
|
const mergedHeaders = (0, import_provider_utils5.combineHeaders)(
|
|
2022
2104
|
await (0, import_provider_utils5.resolve)(this.config.headers),
|
|
@@ -2218,7 +2300,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2218
2300
|
safetyRatings: (_y = candidate == null ? void 0 : candidate.safetyRatings) != null ? _y : null,
|
|
2219
2301
|
usageMetadata: usageMetadata != null ? usageMetadata : null,
|
|
2220
2302
|
finishMessage: (_z = candidate == null ? void 0 : candidate.finishMessage) != null ? _z : null,
|
|
2221
|
-
serviceTier: (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : null,
|
|
2303
|
+
serviceTier: (_B = (_A = usageMetadata == null ? void 0 : usageMetadata.serviceTier) != null ? _A : responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _B : null,
|
|
2222
2304
|
...(candidate == null ? void 0 : candidate.continuationToken) != null && rawFinishReason === "MAX_TOKENS" ? { continuationToken: candidate.continuationToken } : {},
|
|
2223
2305
|
...continuedUsageMetadata != null && firstPromptTokenCount != null ? { initialPromptTokenCount: firstPromptTokenCount } : {}
|
|
2224
2306
|
}
|
|
@@ -2226,7 +2308,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2226
2308
|
request: { body: args },
|
|
2227
2309
|
response: {
|
|
2228
2310
|
// TODO timestamp, model id
|
|
2229
|
-
id: (
|
|
2311
|
+
id: (_C = response.responseId) != null ? _C : void 0,
|
|
2230
2312
|
headers: responseHeaders,
|
|
2231
2313
|
body: rawResponse
|
|
2232
2314
|
}
|
|
@@ -2702,7 +2784,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2702
2784
|
}
|
|
2703
2785
|
},
|
|
2704
2786
|
flush(controller) {
|
|
2705
|
-
var _a;
|
|
2787
|
+
var _a, _b;
|
|
2706
2788
|
usage = addGoogleGenerativeAIUsageMetadata(continuedUsage, usage);
|
|
2707
2789
|
if (currentTextBlockId !== null) {
|
|
2708
2790
|
controller.enqueue({
|
|
@@ -2734,7 +2816,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
|
2734
2816
|
safetyRatings: lastSafetyRatings,
|
|
2735
2817
|
usageMetadata: usage != null ? usage : null,
|
|
2736
2818
|
finishMessage: lastFinishMessage,
|
|
2737
|
-
serviceTier: (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : null,
|
|
2819
|
+
serviceTier: (_b = (_a = usage == null ? void 0 : usage.serviceTier) != null ? _a : responseHeaders == null ? void 0 : responseHeaders["x-gemini-service-tier"]) != null ? _b : null,
|
|
2738
2820
|
// Only surface the token when the response stopped early
|
|
2739
2821
|
// (budget used up, or continuing failed), not for the
|
|
2740
2822
|
// checkpoint tokens of a stream that finished normally.
|
|
@@ -4718,6 +4800,8 @@ var import_v413 = require("zod/v4");
|
|
|
4718
4800
|
var googleInteractionsLanguageModelOptions = (0, import_provider_utils15.lazySchema)(
|
|
4719
4801
|
() => (0, import_provider_utils15.zodSchema)(
|
|
4720
4802
|
import_v413.z.object({
|
|
4803
|
+
/** User-defined request metadata, including `safety_identifier`. */
|
|
4804
|
+
labels: import_v413.z.record(import_v413.z.string(), import_v413.z.string()).nullish(),
|
|
4721
4805
|
previousInteractionId: import_v413.z.string().nullish(),
|
|
4722
4806
|
store: import_v413.z.boolean().nullish(),
|
|
4723
4807
|
agent: import_v413.z.string().nullish(),
|
|
@@ -5668,7 +5752,7 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5668
5752
|
};
|
|
5669
5753
|
}
|
|
5670
5754
|
async getArgs(options) {
|
|
5671
|
-
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;
|
|
5755
|
+
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;
|
|
5672
5756
|
const warnings = [];
|
|
5673
5757
|
const opts = await (0, import_provider_utils19.parseProviderOptions)({
|
|
5674
5758
|
provider: "google",
|
|
@@ -5903,24 +5987,25 @@ var GoogleInteractionsLanguageModel = class {
|
|
|
5903
5987
|
const args = pruneUndefined({
|
|
5904
5988
|
...isAgent ? { agent: this.agent } : { model: this.modelId },
|
|
5905
5989
|
input,
|
|
5990
|
+
labels: (_B = opts == null ? void 0 : opts.labels) != null ? _B : void 0,
|
|
5906
5991
|
system_instruction: systemInstruction,
|
|
5907
5992
|
tools: toolsForBody,
|
|
5908
5993
|
response_format: responseFormatEntries.length > 0 ? responseFormatEntries : void 0,
|
|
5909
5994
|
response_modalities: (opts == null ? void 0 : opts.responseModalities) != null ? opts.responseModalities : void 0,
|
|
5910
|
-
previous_interaction_id: (
|
|
5911
|
-
service_tier: (
|
|
5912
|
-
store: (
|
|
5995
|
+
previous_interaction_id: (_C = opts == null ? void 0 : opts.previousInteractionId) != null ? _C : void 0,
|
|
5996
|
+
service_tier: (_D = opts == null ? void 0 : opts.serviceTier) != null ? _D : void 0,
|
|
5997
|
+
store: (_E = opts == null ? void 0 : opts.store) != null ? _E : void 0,
|
|
5913
5998
|
generation_config: generationConfig != null && Object.keys(generationConfig).length > 0 ? generationConfig : void 0,
|
|
5914
5999
|
agent_config: agentConfig,
|
|
5915
6000
|
environment,
|
|
5916
|
-
background: (
|
|
6001
|
+
background: (_F = opts == null ? void 0 : opts.background) != null ? _F : void 0
|
|
5917
6002
|
});
|
|
5918
6003
|
return {
|
|
5919
6004
|
args,
|
|
5920
6005
|
warnings,
|
|
5921
6006
|
isAgent,
|
|
5922
6007
|
isBackground: (opts == null ? void 0 : opts.background) === true,
|
|
5923
|
-
pollingTimeoutMs: (
|
|
6008
|
+
pollingTimeoutMs: (_G = opts == null ? void 0 : opts.pollingTimeoutMs) != null ? _G : void 0
|
|
5924
6009
|
};
|
|
5925
6010
|
}
|
|
5926
6011
|
async doGenerate(options) {
|