@ai-sdk/google 2.0.0-beta.1 → 2.0.0-beta.11
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 +87 -0
- package/dist/index.d.mts +109 -294
- package/dist/index.d.ts +109 -294
- package/dist/index.js +411 -185
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +384 -153
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +22 -180
- package/dist/internal/index.d.ts +22 -180
- package/dist/internal/index.js +259 -172
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +232 -144
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/internal/index.js
CHANGED
@@ -21,14 +21,14 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
21
21
|
var internal_exports = {};
|
22
22
|
__export(internal_exports, {
|
23
23
|
GoogleGenerativeAILanguageModel: () => GoogleGenerativeAILanguageModel,
|
24
|
-
|
24
|
+
googleTools: () => googleTools,
|
25
25
|
safetyRatingSchema: () => safetyRatingSchema
|
26
26
|
});
|
27
27
|
module.exports = __toCommonJS(internal_exports);
|
28
28
|
|
29
29
|
// src/google-generative-ai-language-model.ts
|
30
|
-
var
|
31
|
-
var
|
30
|
+
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
31
|
+
var import_v45 = require("zod/v4");
|
32
32
|
|
33
33
|
// src/convert-json-schema-to-openapi-schema.ts
|
34
34
|
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
@@ -124,16 +124,18 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
|
124
124
|
return result;
|
125
125
|
}
|
126
126
|
function isEmptyObjectSchema(jsonSchema) {
|
127
|
-
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0);
|
127
|
+
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
|
128
128
|
}
|
129
129
|
|
130
130
|
// src/convert-to-google-generative-ai-messages.ts
|
131
131
|
var import_provider = require("@ai-sdk/provider");
|
132
132
|
var import_provider_utils = require("@ai-sdk/provider-utils");
|
133
|
-
function convertToGoogleGenerativeAIMessages(prompt) {
|
133
|
+
function convertToGoogleGenerativeAIMessages(prompt, options) {
|
134
|
+
var _a;
|
134
135
|
const systemInstructionParts = [];
|
135
136
|
const contents = [];
|
136
137
|
let systemMessagesAllowed = true;
|
138
|
+
const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
|
137
139
|
for (const { role, content } of prompt) {
|
138
140
|
switch (role) {
|
139
141
|
case "system": {
|
@@ -234,8 +236,12 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
234
236
|
}
|
235
237
|
}
|
236
238
|
}
|
239
|
+
if (isGemmaModel && systemInstructionParts.length > 0 && contents.length > 0 && contents[0].role === "user") {
|
240
|
+
const systemText = systemInstructionParts.map((part) => part.text).join("\n\n");
|
241
|
+
contents[0].parts.unshift({ text: systemText + "\n\n" });
|
242
|
+
}
|
237
243
|
return {
|
238
|
-
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0,
|
244
|
+
systemInstruction: systemInstructionParts.length > 0 && !isGemmaModel ? { parts: systemInstructionParts } : void 0,
|
239
245
|
contents
|
240
246
|
};
|
241
247
|
}
|
@@ -247,12 +253,12 @@ function getModelPath(modelId) {
|
|
247
253
|
|
248
254
|
// src/google-error.ts
|
249
255
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
250
|
-
var
|
251
|
-
var googleErrorDataSchema =
|
252
|
-
error:
|
253
|
-
code:
|
254
|
-
message:
|
255
|
-
status:
|
256
|
+
var import_v4 = require("zod/v4");
|
257
|
+
var googleErrorDataSchema = import_v4.z.object({
|
258
|
+
error: import_v4.z.object({
|
259
|
+
code: import_v4.z.number().nullable(),
|
260
|
+
message: import_v4.z.string(),
|
261
|
+
status: import_v4.z.string()
|
256
262
|
})
|
257
263
|
});
|
258
264
|
var googleFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
@@ -261,30 +267,19 @@ var googleFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResp
|
|
261
267
|
});
|
262
268
|
|
263
269
|
// src/google-generative-ai-options.ts
|
264
|
-
var
|
265
|
-
var
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
/**
|
271
|
-
* The threshold to be used in dynamic retrieval. If not set, a system default
|
272
|
-
* value is used.
|
273
|
-
*/
|
274
|
-
dynamicThreshold: import_zod2.z.number().optional()
|
275
|
-
});
|
276
|
-
var googleGenerativeAIProviderOptions = import_zod2.z.object({
|
277
|
-
responseModalities: import_zod2.z.array(import_zod2.z.enum(["TEXT", "IMAGE"])).optional(),
|
278
|
-
thinkingConfig: import_zod2.z.object({
|
279
|
-
thinkingBudget: import_zod2.z.number().optional(),
|
280
|
-
includeThoughts: import_zod2.z.boolean().optional()
|
270
|
+
var import_v42 = require("zod/v4");
|
271
|
+
var googleGenerativeAIProviderOptions = import_v42.z.object({
|
272
|
+
responseModalities: import_v42.z.array(import_v42.z.enum(["TEXT", "IMAGE"])).optional(),
|
273
|
+
thinkingConfig: import_v42.z.object({
|
274
|
+
thinkingBudget: import_v42.z.number().optional(),
|
275
|
+
includeThoughts: import_v42.z.boolean().optional()
|
281
276
|
}).optional(),
|
282
277
|
/**
|
283
278
|
Optional.
|
284
279
|
The name of the cached content used as context to serve the prediction.
|
285
280
|
Format: cachedContents/{cachedContent}
|
286
281
|
*/
|
287
|
-
cachedContent:
|
282
|
+
cachedContent: import_v42.z.string().optional(),
|
288
283
|
/**
|
289
284
|
* Optional. Enable structured output. Default is true.
|
290
285
|
*
|
@@ -293,13 +288,13 @@ var googleGenerativeAIProviderOptions = import_zod2.z.object({
|
|
293
288
|
* Google Generative AI uses. You can use this to disable
|
294
289
|
* structured outputs if you need to.
|
295
290
|
*/
|
296
|
-
structuredOutputs:
|
291
|
+
structuredOutputs: import_v42.z.boolean().optional(),
|
297
292
|
/**
|
298
293
|
Optional. A list of unique safety settings for blocking unsafe content.
|
299
294
|
*/
|
300
|
-
safetySettings:
|
301
|
-
|
302
|
-
category:
|
295
|
+
safetySettings: import_v42.z.array(
|
296
|
+
import_v42.z.object({
|
297
|
+
category: import_v42.z.enum([
|
303
298
|
"HARM_CATEGORY_UNSPECIFIED",
|
304
299
|
"HARM_CATEGORY_HATE_SPEECH",
|
305
300
|
"HARM_CATEGORY_DANGEROUS_CONTENT",
|
@@ -307,7 +302,7 @@ var googleGenerativeAIProviderOptions = import_zod2.z.object({
|
|
307
302
|
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
308
303
|
"HARM_CATEGORY_CIVIC_INTEGRITY"
|
309
304
|
]),
|
310
|
-
threshold:
|
305
|
+
threshold: import_v42.z.enum([
|
311
306
|
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
312
307
|
"BLOCK_LOW_AND_ABOVE",
|
313
308
|
"BLOCK_MEDIUM_AND_ABOVE",
|
@@ -317,7 +312,7 @@ var googleGenerativeAIProviderOptions = import_zod2.z.object({
|
|
317
312
|
])
|
318
313
|
})
|
319
314
|
).optional(),
|
320
|
-
threshold:
|
315
|
+
threshold: import_v42.z.enum([
|
321
316
|
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
322
317
|
"BLOCK_LOW_AND_ABOVE",
|
323
318
|
"BLOCK_MEDIUM_AND_ABOVE",
|
@@ -330,21 +325,7 @@ var googleGenerativeAIProviderOptions = import_zod2.z.object({
|
|
330
325
|
*
|
331
326
|
* https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
|
332
327
|
*/
|
333
|
-
audioTimestamp:
|
334
|
-
/**
|
335
|
-
Optional. When enabled, the model will use Google search to ground the response.
|
336
|
-
|
337
|
-
@see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
|
338
|
-
*/
|
339
|
-
useSearchGrounding: import_zod2.z.boolean().optional(),
|
340
|
-
/**
|
341
|
-
Optional. Specifies the dynamic retrieval configuration.
|
342
|
-
|
343
|
-
@note Dynamic retrieval is only compatible with Gemini 1.5 Flash.
|
344
|
-
|
345
|
-
@see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
|
346
|
-
*/
|
347
|
-
dynamicRetrievalConfig: dynamicRetrievalConfig.optional()
|
328
|
+
audioTimestamp: import_v42.z.boolean().optional()
|
348
329
|
});
|
349
330
|
|
350
331
|
// src/google-prepare-tools.ts
|
@@ -352,8 +333,6 @@ var import_provider2 = require("@ai-sdk/provider");
|
|
352
333
|
function prepareTools({
|
353
334
|
tools,
|
354
335
|
toolChoice,
|
355
|
-
useSearchGrounding,
|
356
|
-
dynamicRetrievalConfig: dynamicRetrievalConfig2,
|
357
336
|
modelId
|
358
337
|
}) {
|
359
338
|
var _a;
|
@@ -361,28 +340,76 @@ function prepareTools({
|
|
361
340
|
const toolWarnings = [];
|
362
341
|
const isGemini2 = modelId.includes("gemini-2");
|
363
342
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
364
|
-
if (
|
343
|
+
if (tools == null) {
|
344
|
+
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
345
|
+
}
|
346
|
+
const hasFunctionTools = tools.some((tool) => tool.type === "function");
|
347
|
+
const hasProviderDefinedTools = tools.some(
|
348
|
+
(tool) => tool.type === "provider-defined"
|
349
|
+
);
|
350
|
+
if (hasFunctionTools && hasProviderDefinedTools) {
|
351
|
+
toolWarnings.push({
|
352
|
+
type: "unsupported-tool",
|
353
|
+
tool: tools.find((tool) => tool.type === "function"),
|
354
|
+
details: "Cannot mix function tools with provider-defined tools in the same request. Please use either function tools or provider-defined tools, but not both."
|
355
|
+
});
|
356
|
+
}
|
357
|
+
if (hasProviderDefinedTools) {
|
358
|
+
const googleTools2 = {};
|
359
|
+
const providerDefinedTools = tools.filter(
|
360
|
+
(tool) => tool.type === "provider-defined"
|
361
|
+
);
|
362
|
+
providerDefinedTools.forEach((tool) => {
|
363
|
+
switch (tool.id) {
|
364
|
+
case "google.google_search":
|
365
|
+
if (isGemini2) {
|
366
|
+
googleTools2.googleSearch = {};
|
367
|
+
} else if (supportsDynamicRetrieval) {
|
368
|
+
googleTools2.googleSearchRetrieval = {
|
369
|
+
dynamicRetrievalConfig: {
|
370
|
+
mode: tool.args.mode,
|
371
|
+
dynamicThreshold: tool.args.dynamicThreshold
|
372
|
+
}
|
373
|
+
};
|
374
|
+
} else {
|
375
|
+
googleTools2.googleSearchRetrieval = {};
|
376
|
+
}
|
377
|
+
break;
|
378
|
+
case "google.url_context":
|
379
|
+
if (isGemini2) {
|
380
|
+
googleTools2.urlContext = {};
|
381
|
+
} else {
|
382
|
+
toolWarnings.push({
|
383
|
+
type: "unsupported-tool",
|
384
|
+
tool,
|
385
|
+
details: "The URL context tool is not supported with other Gemini models than Gemini 2."
|
386
|
+
});
|
387
|
+
}
|
388
|
+
break;
|
389
|
+
default:
|
390
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
391
|
+
break;
|
392
|
+
}
|
393
|
+
});
|
365
394
|
return {
|
366
|
-
tools:
|
367
|
-
googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig2 ? {} : { dynamicRetrievalConfig: dynamicRetrievalConfig2 }
|
368
|
-
},
|
395
|
+
tools: Object.keys(googleTools2).length > 0 ? googleTools2 : void 0,
|
369
396
|
toolConfig: void 0,
|
370
397
|
toolWarnings
|
371
398
|
};
|
372
399
|
}
|
373
|
-
if (tools == null) {
|
374
|
-
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
375
|
-
}
|
376
400
|
const functionDeclarations = [];
|
377
401
|
for (const tool of tools) {
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
402
|
+
switch (tool.type) {
|
403
|
+
case "function":
|
404
|
+
functionDeclarations.push({
|
405
|
+
name: tool.name,
|
406
|
+
description: (_a = tool.description) != null ? _a : "",
|
407
|
+
parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
|
408
|
+
});
|
409
|
+
break;
|
410
|
+
default:
|
411
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
412
|
+
break;
|
386
413
|
}
|
387
414
|
}
|
388
415
|
if (toolChoice == null) {
|
@@ -459,12 +486,72 @@ function mapGoogleGenerativeAIFinishReason({
|
|
459
486
|
}
|
460
487
|
}
|
461
488
|
|
489
|
+
// src/tool/google-search.ts
|
490
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
491
|
+
var import_v43 = require("zod/v4");
|
492
|
+
var groundingChunkSchema = import_v43.z.object({
|
493
|
+
web: import_v43.z.object({ uri: import_v43.z.string(), title: import_v43.z.string() }).nullish(),
|
494
|
+
retrievedContext: import_v43.z.object({ uri: import_v43.z.string(), title: import_v43.z.string() }).nullish()
|
495
|
+
});
|
496
|
+
var groundingMetadataSchema = import_v43.z.object({
|
497
|
+
webSearchQueries: import_v43.z.array(import_v43.z.string()).nullish(),
|
498
|
+
retrievalQueries: import_v43.z.array(import_v43.z.string()).nullish(),
|
499
|
+
searchEntryPoint: import_v43.z.object({ renderedContent: import_v43.z.string() }).nullish(),
|
500
|
+
groundingChunks: import_v43.z.array(groundingChunkSchema).nullish(),
|
501
|
+
groundingSupports: import_v43.z.array(
|
502
|
+
import_v43.z.object({
|
503
|
+
segment: import_v43.z.object({
|
504
|
+
startIndex: import_v43.z.number().nullish(),
|
505
|
+
endIndex: import_v43.z.number().nullish(),
|
506
|
+
text: import_v43.z.string().nullish()
|
507
|
+
}),
|
508
|
+
segment_text: import_v43.z.string().nullish(),
|
509
|
+
groundingChunkIndices: import_v43.z.array(import_v43.z.number()).nullish(),
|
510
|
+
supportChunkIndices: import_v43.z.array(import_v43.z.number()).nullish(),
|
511
|
+
confidenceScores: import_v43.z.array(import_v43.z.number()).nullish(),
|
512
|
+
confidenceScore: import_v43.z.array(import_v43.z.number()).nullish()
|
513
|
+
})
|
514
|
+
).nullish(),
|
515
|
+
retrievalMetadata: import_v43.z.union([
|
516
|
+
import_v43.z.object({
|
517
|
+
webDynamicRetrievalScore: import_v43.z.number()
|
518
|
+
}),
|
519
|
+
import_v43.z.object({})
|
520
|
+
]).nullish()
|
521
|
+
});
|
522
|
+
var googleSearch = (0, import_provider_utils3.createProviderDefinedToolFactory)({
|
523
|
+
id: "google.google_search",
|
524
|
+
name: "google_search",
|
525
|
+
inputSchema: import_v43.z.object({
|
526
|
+
mode: import_v43.z.enum(["MODE_DYNAMIC", "MODE_UNSPECIFIED"]).default("MODE_UNSPECIFIED"),
|
527
|
+
dynamicThreshold: import_v43.z.number().default(1)
|
528
|
+
})
|
529
|
+
});
|
530
|
+
|
531
|
+
// src/tool/url-context.ts
|
532
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
533
|
+
var import_v44 = require("zod/v4");
|
534
|
+
var urlMetadataSchema = import_v44.z.object({
|
535
|
+
retrievedUrl: import_v44.z.string(),
|
536
|
+
urlRetrievalStatus: import_v44.z.string()
|
537
|
+
});
|
538
|
+
var urlContextMetadataSchema = import_v44.z.object({
|
539
|
+
urlMetadata: import_v44.z.array(urlMetadataSchema)
|
540
|
+
});
|
541
|
+
var urlContext = (0, import_provider_utils4.createProviderDefinedToolFactory)({
|
542
|
+
id: "google.url_context",
|
543
|
+
name: "url_context",
|
544
|
+
inputSchema: import_v44.z.object({})
|
545
|
+
});
|
546
|
+
|
462
547
|
// src/google-generative-ai-language-model.ts
|
463
548
|
var GoogleGenerativeAILanguageModel = class {
|
464
549
|
constructor(modelId, config) {
|
465
550
|
this.specificationVersion = "v2";
|
551
|
+
var _a;
|
466
552
|
this.modelId = modelId;
|
467
553
|
this.config = config;
|
554
|
+
this.generateId = (_a = config.generateId) != null ? _a : import_provider_utils5.generateId;
|
468
555
|
}
|
469
556
|
get provider() {
|
470
557
|
return this.config.provider;
|
@@ -488,9 +575,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
488
575
|
toolChoice,
|
489
576
|
providerOptions
|
490
577
|
}) {
|
491
|
-
var _a, _b
|
578
|
+
var _a, _b;
|
492
579
|
const warnings = [];
|
493
|
-
const googleOptions = await (0,
|
580
|
+
const googleOptions = await (0, import_provider_utils5.parseProviderOptions)({
|
494
581
|
provider: "google",
|
495
582
|
providerOptions,
|
496
583
|
schema: googleGenerativeAIProviderOptions
|
@@ -501,16 +588,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
501
588
|
message: `The 'includeThoughts' option is only supported with the Google Vertex provider and might not be supported or could behave unexpectedly with the current Google provider (${this.config.provider}).`
|
502
589
|
});
|
503
590
|
}
|
504
|
-
const
|
591
|
+
const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
|
592
|
+
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
|
593
|
+
prompt,
|
594
|
+
{ isGemmaModel }
|
595
|
+
);
|
505
596
|
const {
|
506
|
-
tools:
|
597
|
+
tools: googleTools2,
|
507
598
|
toolConfig: googleToolConfig,
|
508
599
|
toolWarnings
|
509
600
|
} = prepareTools({
|
510
601
|
tools,
|
511
602
|
toolChoice,
|
512
|
-
useSearchGrounding: (_b = googleOptions == null ? void 0 : googleOptions.useSearchGrounding) != null ? _b : false,
|
513
|
-
dynamicRetrievalConfig: googleOptions == null ? void 0 : googleOptions.dynamicRetrievalConfig,
|
514
603
|
modelId: this.modelId
|
515
604
|
});
|
516
605
|
return {
|
@@ -530,7 +619,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
530
619
|
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
531
620
|
// so this is needed as an escape hatch:
|
532
621
|
// TODO convert into provider option
|
533
|
-
((
|
622
|
+
((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
534
623
|
...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
|
535
624
|
audioTimestamp: googleOptions.audioTimestamp
|
536
625
|
},
|
@@ -539,9 +628,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
539
628
|
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
540
629
|
},
|
541
630
|
contents,
|
542
|
-
systemInstruction,
|
631
|
+
systemInstruction: isGemmaModel ? void 0 : systemInstruction,
|
543
632
|
safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
|
544
|
-
tools:
|
633
|
+
tools: googleTools2,
|
545
634
|
toolConfig: googleToolConfig,
|
546
635
|
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
|
547
636
|
},
|
@@ -549,25 +638,25 @@ var GoogleGenerativeAILanguageModel = class {
|
|
549
638
|
};
|
550
639
|
}
|
551
640
|
async doGenerate(options) {
|
552
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
641
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
553
642
|
const { args, warnings } = await this.getArgs(options);
|
554
643
|
const body = JSON.stringify(args);
|
555
|
-
const mergedHeaders = (0,
|
556
|
-
await (0,
|
644
|
+
const mergedHeaders = (0, import_provider_utils5.combineHeaders)(
|
645
|
+
await (0, import_provider_utils5.resolve)(this.config.headers),
|
557
646
|
options.headers
|
558
647
|
);
|
559
648
|
const {
|
560
649
|
responseHeaders,
|
561
650
|
value: response,
|
562
651
|
rawValue: rawResponse
|
563
|
-
} = await (0,
|
652
|
+
} = await (0, import_provider_utils5.postJsonToApi)({
|
564
653
|
url: `${this.config.baseURL}/${getModelPath(
|
565
654
|
this.modelId
|
566
655
|
)}:generateContent`,
|
567
656
|
headers: mergedHeaders,
|
568
657
|
body: args,
|
569
658
|
failedResponseHandler: googleFailedResponseHandler,
|
570
|
-
successfulResponseHandler: (0,
|
659
|
+
successfulResponseHandler: (0, import_provider_utils5.createJsonResponseHandler)(responseSchema),
|
571
660
|
abortSignal: options.abortSignal,
|
572
661
|
fetch: this.config.fetch
|
573
662
|
});
|
@@ -621,7 +710,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
621
710
|
providerMetadata: {
|
622
711
|
google: {
|
623
712
|
groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
|
624
|
-
|
713
|
+
urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
|
714
|
+
safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
|
715
|
+
usageMetadata: usageMetadata != null ? usageMetadata : null
|
625
716
|
}
|
626
717
|
},
|
627
718
|
request: { body },
|
@@ -635,18 +726,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
635
726
|
async doStream(options) {
|
636
727
|
const { args, warnings } = await this.getArgs(options);
|
637
728
|
const body = JSON.stringify(args);
|
638
|
-
const headers = (0,
|
639
|
-
await (0,
|
729
|
+
const headers = (0, import_provider_utils5.combineHeaders)(
|
730
|
+
await (0, import_provider_utils5.resolve)(this.config.headers),
|
640
731
|
options.headers
|
641
732
|
);
|
642
|
-
const { responseHeaders, value: response } = await (0,
|
733
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils5.postJsonToApi)({
|
643
734
|
url: `${this.config.baseURL}/${getModelPath(
|
644
735
|
this.modelId
|
645
736
|
)}:streamGenerateContent?alt=sse`,
|
646
737
|
headers,
|
647
738
|
body: args,
|
648
739
|
failedResponseHandler: googleFailedResponseHandler,
|
649
|
-
successfulResponseHandler: (0,
|
740
|
+
successfulResponseHandler: (0, import_provider_utils5.createEventSourceResponseHandler)(chunkSchema),
|
650
741
|
abortSignal: options.abortSignal,
|
651
742
|
fetch: this.config.fetch
|
652
743
|
});
|
@@ -657,11 +748,12 @@ var GoogleGenerativeAILanguageModel = class {
|
|
657
748
|
totalTokens: void 0
|
658
749
|
};
|
659
750
|
let providerMetadata = void 0;
|
660
|
-
const
|
751
|
+
const generateId2 = this.config.generateId;
|
661
752
|
let hasToolCalls = false;
|
662
753
|
let currentTextBlockId = null;
|
663
754
|
let currentReasoningBlockId = null;
|
664
755
|
let blockCounter = 0;
|
756
|
+
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
665
757
|
return {
|
666
758
|
stream: response.pipeThrough(
|
667
759
|
new TransformStream({
|
@@ -691,6 +783,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
691
783
|
return;
|
692
784
|
}
|
693
785
|
const content = candidate.content;
|
786
|
+
const sources = extractSources({
|
787
|
+
groundingMetadata: candidate.groundingMetadata,
|
788
|
+
generateId: generateId2
|
789
|
+
});
|
790
|
+
if (sources != null) {
|
791
|
+
for (const source of sources) {
|
792
|
+
if (source.sourceType === "url" && !emittedSourceUrls.has(source.url)) {
|
793
|
+
emittedSourceUrls.add(source.url);
|
794
|
+
controller.enqueue(source);
|
795
|
+
}
|
796
|
+
}
|
797
|
+
}
|
694
798
|
if (content != null) {
|
695
799
|
const parts = (_g = content.parts) != null ? _g : [];
|
696
800
|
for (const part of parts) {
|
@@ -750,7 +854,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
750
854
|
}
|
751
855
|
const toolCallDeltas = getToolCallsFromParts({
|
752
856
|
parts: content.parts,
|
753
|
-
generateId
|
857
|
+
generateId: generateId2
|
754
858
|
});
|
755
859
|
if (toolCallDeltas != null) {
|
756
860
|
for (const toolCall of toolCallDeltas) {
|
@@ -783,19 +887,16 @@ var GoogleGenerativeAILanguageModel = class {
|
|
783
887
|
finishReason: candidate.finishReason,
|
784
888
|
hasToolCalls
|
785
889
|
});
|
786
|
-
const sources = (_h = extractSources({
|
787
|
-
groundingMetadata: candidate.groundingMetadata,
|
788
|
-
generateId
|
789
|
-
})) != null ? _h : [];
|
790
|
-
for (const source of sources) {
|
791
|
-
controller.enqueue(source);
|
792
|
-
}
|
793
890
|
providerMetadata = {
|
794
891
|
google: {
|
795
|
-
groundingMetadata: (
|
892
|
+
groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
|
893
|
+
urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
|
796
894
|
safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null
|
797
895
|
}
|
798
896
|
};
|
897
|
+
if (usageMetadata != null) {
|
898
|
+
providerMetadata.google.usageMetadata = usageMetadata;
|
899
|
+
}
|
799
900
|
}
|
800
901
|
},
|
801
902
|
flush(controller) {
|
@@ -827,14 +928,14 @@ var GoogleGenerativeAILanguageModel = class {
|
|
827
928
|
};
|
828
929
|
function getToolCallsFromParts({
|
829
930
|
parts,
|
830
|
-
generateId
|
931
|
+
generateId: generateId2
|
831
932
|
}) {
|
832
933
|
const functionCallParts = parts == null ? void 0 : parts.filter(
|
833
934
|
(part) => "functionCall" in part
|
834
935
|
);
|
835
936
|
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
836
937
|
type: "tool-call",
|
837
|
-
toolCallId:
|
938
|
+
toolCallId: generateId2(),
|
838
939
|
toolName: part.functionCall.name,
|
839
940
|
args: JSON.stringify(part.functionCall.args)
|
840
941
|
}));
|
@@ -846,7 +947,7 @@ function getInlineDataParts(parts) {
|
|
846
947
|
}
|
847
948
|
function extractSources({
|
848
949
|
groundingMetadata,
|
849
|
-
generateId
|
950
|
+
generateId: generateId2
|
850
951
|
}) {
|
851
952
|
var _a;
|
852
953
|
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
@@ -854,105 +955,91 @@ function extractSources({
|
|
854
955
|
).map((chunk) => ({
|
855
956
|
type: "source",
|
856
957
|
sourceType: "url",
|
857
|
-
id:
|
958
|
+
id: generateId2(),
|
858
959
|
url: chunk.web.uri,
|
859
960
|
title: chunk.web.title
|
860
961
|
}));
|
861
962
|
}
|
862
|
-
var contentSchema =
|
863
|
-
parts:
|
864
|
-
|
963
|
+
var contentSchema = import_v45.z.object({
|
964
|
+
parts: import_v45.z.array(
|
965
|
+
import_v45.z.union([
|
865
966
|
// note: order matters since text can be fully empty
|
866
|
-
|
867
|
-
functionCall:
|
868
|
-
name:
|
869
|
-
args:
|
967
|
+
import_v45.z.object({
|
968
|
+
functionCall: import_v45.z.object({
|
969
|
+
name: import_v45.z.string(),
|
970
|
+
args: import_v45.z.unknown()
|
870
971
|
})
|
871
972
|
}),
|
872
|
-
|
873
|
-
inlineData:
|
874
|
-
mimeType:
|
875
|
-
data:
|
973
|
+
import_v45.z.object({
|
974
|
+
inlineData: import_v45.z.object({
|
975
|
+
mimeType: import_v45.z.string(),
|
976
|
+
data: import_v45.z.string()
|
876
977
|
})
|
877
978
|
}),
|
878
|
-
|
879
|
-
text:
|
880
|
-
thought:
|
979
|
+
import_v45.z.object({
|
980
|
+
text: import_v45.z.string().nullish(),
|
981
|
+
thought: import_v45.z.boolean().nullish()
|
881
982
|
})
|
882
983
|
])
|
883
984
|
).nullish()
|
884
985
|
});
|
885
|
-
var
|
886
|
-
|
887
|
-
|
888
|
-
|
889
|
-
|
890
|
-
|
891
|
-
|
892
|
-
searchEntryPoint: import_zod3.z.object({ renderedContent: import_zod3.z.string() }).nullish(),
|
893
|
-
groundingChunks: import_zod3.z.array(groundingChunkSchema).nullish(),
|
894
|
-
groundingSupports: import_zod3.z.array(
|
895
|
-
import_zod3.z.object({
|
896
|
-
segment: import_zod3.z.object({
|
897
|
-
startIndex: import_zod3.z.number().nullish(),
|
898
|
-
endIndex: import_zod3.z.number().nullish(),
|
899
|
-
text: import_zod3.z.string().nullish()
|
900
|
-
}),
|
901
|
-
segment_text: import_zod3.z.string().nullish(),
|
902
|
-
groundingChunkIndices: import_zod3.z.array(import_zod3.z.number()).nullish(),
|
903
|
-
supportChunkIndices: import_zod3.z.array(import_zod3.z.number()).nullish(),
|
904
|
-
confidenceScores: import_zod3.z.array(import_zod3.z.number()).nullish(),
|
905
|
-
confidenceScore: import_zod3.z.array(import_zod3.z.number()).nullish()
|
906
|
-
})
|
907
|
-
).nullish(),
|
908
|
-
retrievalMetadata: import_zod3.z.union([
|
909
|
-
import_zod3.z.object({
|
910
|
-
webDynamicRetrievalScore: import_zod3.z.number()
|
911
|
-
}),
|
912
|
-
import_zod3.z.object({})
|
913
|
-
]).nullish()
|
914
|
-
});
|
915
|
-
var safetyRatingSchema = import_zod3.z.object({
|
916
|
-
category: import_zod3.z.string().nullish(),
|
917
|
-
probability: import_zod3.z.string().nullish(),
|
918
|
-
probabilityScore: import_zod3.z.number().nullish(),
|
919
|
-
severity: import_zod3.z.string().nullish(),
|
920
|
-
severityScore: import_zod3.z.number().nullish(),
|
921
|
-
blocked: import_zod3.z.boolean().nullish()
|
986
|
+
var safetyRatingSchema = import_v45.z.object({
|
987
|
+
category: import_v45.z.string().nullish(),
|
988
|
+
probability: import_v45.z.string().nullish(),
|
989
|
+
probabilityScore: import_v45.z.number().nullish(),
|
990
|
+
severity: import_v45.z.string().nullish(),
|
991
|
+
severityScore: import_v45.z.number().nullish(),
|
992
|
+
blocked: import_v45.z.boolean().nullish()
|
922
993
|
});
|
923
|
-
var usageSchema =
|
924
|
-
cachedContentTokenCount:
|
925
|
-
thoughtsTokenCount:
|
926
|
-
promptTokenCount:
|
927
|
-
candidatesTokenCount:
|
928
|
-
totalTokenCount:
|
994
|
+
var usageSchema = import_v45.z.object({
|
995
|
+
cachedContentTokenCount: import_v45.z.number().nullish(),
|
996
|
+
thoughtsTokenCount: import_v45.z.number().nullish(),
|
997
|
+
promptTokenCount: import_v45.z.number().nullish(),
|
998
|
+
candidatesTokenCount: import_v45.z.number().nullish(),
|
999
|
+
totalTokenCount: import_v45.z.number().nullish()
|
929
1000
|
});
|
930
|
-
var responseSchema =
|
931
|
-
candidates:
|
932
|
-
|
933
|
-
content: contentSchema.nullish().or(
|
934
|
-
finishReason:
|
935
|
-
safetyRatings:
|
936
|
-
groundingMetadata: groundingMetadataSchema.nullish()
|
1001
|
+
var responseSchema = import_v45.z.object({
|
1002
|
+
candidates: import_v45.z.array(
|
1003
|
+
import_v45.z.object({
|
1004
|
+
content: contentSchema.nullish().or(import_v45.z.object({}).strict()),
|
1005
|
+
finishReason: import_v45.z.string().nullish(),
|
1006
|
+
safetyRatings: import_v45.z.array(safetyRatingSchema).nullish(),
|
1007
|
+
groundingMetadata: groundingMetadataSchema.nullish(),
|
1008
|
+
urlContextMetadata: urlContextMetadataSchema.nullish()
|
937
1009
|
})
|
938
1010
|
),
|
939
1011
|
usageMetadata: usageSchema.nullish()
|
940
1012
|
});
|
941
|
-
var chunkSchema =
|
942
|
-
candidates:
|
943
|
-
|
1013
|
+
var chunkSchema = import_v45.z.object({
|
1014
|
+
candidates: import_v45.z.array(
|
1015
|
+
import_v45.z.object({
|
944
1016
|
content: contentSchema.nullish(),
|
945
|
-
finishReason:
|
946
|
-
safetyRatings:
|
947
|
-
groundingMetadata: groundingMetadataSchema.nullish()
|
1017
|
+
finishReason: import_v45.z.string().nullish(),
|
1018
|
+
safetyRatings: import_v45.z.array(safetyRatingSchema).nullish(),
|
1019
|
+
groundingMetadata: groundingMetadataSchema.nullish(),
|
1020
|
+
urlContextMetadata: urlContextMetadataSchema.nullish()
|
948
1021
|
})
|
949
1022
|
).nullish(),
|
950
1023
|
usageMetadata: usageSchema.nullish()
|
951
1024
|
});
|
1025
|
+
|
1026
|
+
// src/google-tools.ts
|
1027
|
+
var googleTools = {
|
1028
|
+
/**
|
1029
|
+
* Creates a Google search tool that gives Google direct access to real-time web content.
|
1030
|
+
* Must have name "google_search".
|
1031
|
+
*/
|
1032
|
+
googleSearch,
|
1033
|
+
/**
|
1034
|
+
* Creates a URL context tool that gives Google direct access to real-time web content.
|
1035
|
+
* Must have name "url_context".
|
1036
|
+
*/
|
1037
|
+
urlContext
|
1038
|
+
};
|
952
1039
|
// Annotate the CommonJS export names for ESM import in node:
|
953
1040
|
0 && (module.exports = {
|
954
1041
|
GoogleGenerativeAILanguageModel,
|
955
|
-
|
1042
|
+
googleTools,
|
956
1043
|
safetyRatingSchema
|
957
1044
|
});
|
958
1045
|
//# sourceMappingURL=index.js.map
|