@ai-sdk/google 2.0.0-canary.8 → 2.0.0
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 +570 -0
- package/README.md +2 -2
- package/dist/index.d.mts +157 -267
- package/dist/index.d.ts +157 -267
- package/dist/index.js +757 -230
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +751 -220
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.d.mts +47 -254
- package/dist/internal/index.d.ts +47 -254
- package/dist/internal/index.js +554 -201
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +541 -189
- package/dist/internal/index.mjs.map +1 -1
- package/internal.d.ts +1 -0
- package/package.json +11 -9
package/dist/index.mjs
CHANGED
@@ -1,9 +1,6 @@
|
|
1
1
|
// src/google-provider.ts
|
2
2
|
import {
|
3
|
-
|
4
|
-
} from "@ai-sdk/provider";
|
5
|
-
import {
|
6
|
-
generateId,
|
3
|
+
generateId as generateId2,
|
7
4
|
loadApiKey,
|
8
5
|
withoutTrailingSlash
|
9
6
|
} from "@ai-sdk/provider-utils";
|
@@ -15,14 +12,15 @@ import {
|
|
15
12
|
import {
|
16
13
|
combineHeaders,
|
17
14
|
createJsonResponseHandler,
|
15
|
+
parseProviderOptions,
|
18
16
|
postJsonToApi,
|
19
17
|
resolve
|
20
18
|
} from "@ai-sdk/provider-utils";
|
21
|
-
import { z as
|
19
|
+
import { z as z3 } from "zod/v4";
|
22
20
|
|
23
21
|
// src/google-error.ts
|
24
22
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
25
|
-
import { z } from "zod";
|
23
|
+
import { z } from "zod/v4";
|
26
24
|
var googleErrorDataSchema = z.object({
|
27
25
|
error: z.object({
|
28
26
|
code: z.number().nullable(),
|
@@ -35,28 +33,61 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
35
33
|
errorToMessage: (data) => data.error.message
|
36
34
|
});
|
37
35
|
|
36
|
+
// src/google-generative-ai-embedding-options.ts
|
37
|
+
import { z as z2 } from "zod/v4";
|
38
|
+
var googleGenerativeAIEmbeddingProviderOptions = z2.object({
|
39
|
+
/**
|
40
|
+
* Optional. Optional reduced dimension for the output embedding.
|
41
|
+
* If set, excessive values in the output embedding are truncated from the end.
|
42
|
+
*/
|
43
|
+
outputDimensionality: z2.number().optional(),
|
44
|
+
/**
|
45
|
+
* Optional. Specifies the task type for generating embeddings.
|
46
|
+
* Supported task types:
|
47
|
+
* - SEMANTIC_SIMILARITY: Optimized for text similarity.
|
48
|
+
* - CLASSIFICATION: Optimized for text classification.
|
49
|
+
* - CLUSTERING: Optimized for clustering texts based on similarity.
|
50
|
+
* - RETRIEVAL_DOCUMENT: Optimized for document retrieval.
|
51
|
+
* - RETRIEVAL_QUERY: Optimized for query-based retrieval.
|
52
|
+
* - QUESTION_ANSWERING: Optimized for answering questions.
|
53
|
+
* - FACT_VERIFICATION: Optimized for verifying factual information.
|
54
|
+
* - CODE_RETRIEVAL_QUERY: Optimized for retrieving code blocks based on natural language queries.
|
55
|
+
*/
|
56
|
+
taskType: z2.enum([
|
57
|
+
"SEMANTIC_SIMILARITY",
|
58
|
+
"CLASSIFICATION",
|
59
|
+
"CLUSTERING",
|
60
|
+
"RETRIEVAL_DOCUMENT",
|
61
|
+
"RETRIEVAL_QUERY",
|
62
|
+
"QUESTION_ANSWERING",
|
63
|
+
"FACT_VERIFICATION",
|
64
|
+
"CODE_RETRIEVAL_QUERY"
|
65
|
+
]).optional()
|
66
|
+
});
|
67
|
+
|
38
68
|
// src/google-generative-ai-embedding-model.ts
|
39
69
|
var GoogleGenerativeAIEmbeddingModel = class {
|
40
|
-
constructor(modelId,
|
70
|
+
constructor(modelId, config) {
|
41
71
|
this.specificationVersion = "v2";
|
72
|
+
this.maxEmbeddingsPerCall = 2048;
|
73
|
+
this.supportsParallelCalls = true;
|
42
74
|
this.modelId = modelId;
|
43
|
-
this.settings = settings;
|
44
75
|
this.config = config;
|
45
76
|
}
|
46
77
|
get provider() {
|
47
78
|
return this.config.provider;
|
48
79
|
}
|
49
|
-
get maxEmbeddingsPerCall() {
|
50
|
-
return 2048;
|
51
|
-
}
|
52
|
-
get supportsParallelCalls() {
|
53
|
-
return true;
|
54
|
-
}
|
55
80
|
async doEmbed({
|
56
81
|
values,
|
57
82
|
headers,
|
58
|
-
abortSignal
|
83
|
+
abortSignal,
|
84
|
+
providerOptions
|
59
85
|
}) {
|
86
|
+
const googleOptions = await parseProviderOptions({
|
87
|
+
provider: "google",
|
88
|
+
providerOptions,
|
89
|
+
schema: googleGenerativeAIEmbeddingProviderOptions
|
90
|
+
});
|
60
91
|
if (values.length > this.maxEmbeddingsPerCall) {
|
61
92
|
throw new TooManyEmbeddingValuesForCallError({
|
62
93
|
provider: this.provider,
|
@@ -69,6 +100,35 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
69
100
|
await resolve(this.config.headers),
|
70
101
|
headers
|
71
102
|
);
|
103
|
+
if (values.length === 1) {
|
104
|
+
const {
|
105
|
+
responseHeaders: responseHeaders2,
|
106
|
+
value: response2,
|
107
|
+
rawValue: rawValue2
|
108
|
+
} = await postJsonToApi({
|
109
|
+
url: `${this.config.baseURL}/models/${this.modelId}:embedContent`,
|
110
|
+
headers: mergedHeaders,
|
111
|
+
body: {
|
112
|
+
model: `models/${this.modelId}`,
|
113
|
+
content: {
|
114
|
+
parts: [{ text: values[0] }]
|
115
|
+
},
|
116
|
+
outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
|
117
|
+
taskType: googleOptions == null ? void 0 : googleOptions.taskType
|
118
|
+
},
|
119
|
+
failedResponseHandler: googleFailedResponseHandler,
|
120
|
+
successfulResponseHandler: createJsonResponseHandler(
|
121
|
+
googleGenerativeAISingleEmbeddingResponseSchema
|
122
|
+
),
|
123
|
+
abortSignal,
|
124
|
+
fetch: this.config.fetch
|
125
|
+
});
|
126
|
+
return {
|
127
|
+
embeddings: [response2.embedding.values],
|
128
|
+
usage: void 0,
|
129
|
+
response: { headers: responseHeaders2, body: rawValue2 }
|
130
|
+
};
|
131
|
+
}
|
72
132
|
const {
|
73
133
|
responseHeaders,
|
74
134
|
value: response,
|
@@ -80,7 +140,8 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
80
140
|
requests: values.map((value) => ({
|
81
141
|
model: `models/${this.modelId}`,
|
82
142
|
content: { role: "user", parts: [{ text: value }] },
|
83
|
-
outputDimensionality:
|
143
|
+
outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
|
144
|
+
taskType: googleOptions == null ? void 0 : googleOptions.taskType
|
84
145
|
}))
|
85
146
|
},
|
86
147
|
failedResponseHandler: googleFailedResponseHandler,
|
@@ -97,8 +158,11 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
97
158
|
};
|
98
159
|
}
|
99
160
|
};
|
100
|
-
var googleGenerativeAITextEmbeddingResponseSchema =
|
101
|
-
embeddings:
|
161
|
+
var googleGenerativeAITextEmbeddingResponseSchema = z3.object({
|
162
|
+
embeddings: z3.array(z3.object({ values: z3.array(z3.number()) }))
|
163
|
+
});
|
164
|
+
var googleGenerativeAISingleEmbeddingResponseSchema = z3.object({
|
165
|
+
embedding: z3.object({ values: z3.array(z3.number()) })
|
102
166
|
});
|
103
167
|
|
104
168
|
// src/google-generative-ai-language-model.ts
|
@@ -106,11 +170,12 @@ import {
|
|
106
170
|
combineHeaders as combineHeaders2,
|
107
171
|
createEventSourceResponseHandler,
|
108
172
|
createJsonResponseHandler as createJsonResponseHandler2,
|
109
|
-
|
173
|
+
generateId,
|
174
|
+
parseProviderOptions as parseProviderOptions2,
|
110
175
|
postJsonToApi as postJsonToApi2,
|
111
176
|
resolve as resolve2
|
112
177
|
} from "@ai-sdk/provider-utils";
|
113
|
-
import { z as
|
178
|
+
import { z as z7 } from "zod/v4";
|
114
179
|
|
115
180
|
// src/convert-json-schema-to-openapi-schema.ts
|
116
181
|
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
@@ -206,20 +271,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
|
206
271
|
return result;
|
207
272
|
}
|
208
273
|
function isEmptyObjectSchema(jsonSchema) {
|
209
|
-
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0);
|
274
|
+
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
|
210
275
|
}
|
211
276
|
|
212
277
|
// src/convert-to-google-generative-ai-messages.ts
|
213
278
|
import {
|
214
279
|
UnsupportedFunctionalityError
|
215
280
|
} from "@ai-sdk/provider";
|
216
|
-
import {
|
217
|
-
|
218
|
-
|
219
|
-
function convertToGoogleGenerativeAIMessages(prompt) {
|
281
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
282
|
+
function convertToGoogleGenerativeAIMessages(prompt, options) {
|
283
|
+
var _a;
|
220
284
|
const systemInstructionParts = [];
|
221
285
|
const contents = [];
|
222
286
|
let systemMessagesAllowed = true;
|
287
|
+
const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
|
223
288
|
for (const { role, content } of prompt) {
|
224
289
|
switch (role) {
|
225
290
|
case "system": {
|
@@ -293,7 +358,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
293
358
|
return {
|
294
359
|
functionCall: {
|
295
360
|
name: part.toolName,
|
296
|
-
args: part.
|
361
|
+
args: part.input
|
297
362
|
}
|
298
363
|
};
|
299
364
|
}
|
@@ -311,7 +376,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
311
376
|
name: part.toolName,
|
312
377
|
response: {
|
313
378
|
name: part.toolName,
|
314
|
-
content: part.
|
379
|
+
content: part.output.value
|
315
380
|
}
|
316
381
|
}
|
317
382
|
}))
|
@@ -320,8 +385,12 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
320
385
|
}
|
321
386
|
}
|
322
387
|
}
|
388
|
+
if (isGemmaModel && systemInstructionParts.length > 0 && contents.length > 0 && contents[0].role === "user") {
|
389
|
+
const systemText = systemInstructionParts.map((part) => part.text).join("\n\n");
|
390
|
+
contents[0].parts.unshift({ text: systemText + "\n\n" });
|
391
|
+
}
|
323
392
|
return {
|
324
|
-
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0,
|
393
|
+
systemInstruction: systemInstructionParts.length > 0 && !isGemmaModel ? { parts: systemInstructionParts } : void 0,
|
325
394
|
contents
|
326
395
|
};
|
327
396
|
}
|
@@ -331,6 +400,68 @@ function getModelPath(modelId) {
|
|
331
400
|
return modelId.includes("/") ? modelId : `models/${modelId}`;
|
332
401
|
}
|
333
402
|
|
403
|
+
// src/google-generative-ai-options.ts
|
404
|
+
import { z as z4 } from "zod/v4";
|
405
|
+
var googleGenerativeAIProviderOptions = z4.object({
|
406
|
+
responseModalities: z4.array(z4.enum(["TEXT", "IMAGE"])).optional(),
|
407
|
+
thinkingConfig: z4.object({
|
408
|
+
thinkingBudget: z4.number().optional(),
|
409
|
+
includeThoughts: z4.boolean().optional()
|
410
|
+
}).optional(),
|
411
|
+
/**
|
412
|
+
Optional.
|
413
|
+
The name of the cached content used as context to serve the prediction.
|
414
|
+
Format: cachedContents/{cachedContent}
|
415
|
+
*/
|
416
|
+
cachedContent: z4.string().optional(),
|
417
|
+
/**
|
418
|
+
* Optional. Enable structured output. Default is true.
|
419
|
+
*
|
420
|
+
* This is useful when the JSON Schema contains elements that are
|
421
|
+
* not supported by the OpenAPI schema version that
|
422
|
+
* Google Generative AI uses. You can use this to disable
|
423
|
+
* structured outputs if you need to.
|
424
|
+
*/
|
425
|
+
structuredOutputs: z4.boolean().optional(),
|
426
|
+
/**
|
427
|
+
Optional. A list of unique safety settings for blocking unsafe content.
|
428
|
+
*/
|
429
|
+
safetySettings: z4.array(
|
430
|
+
z4.object({
|
431
|
+
category: z4.enum([
|
432
|
+
"HARM_CATEGORY_UNSPECIFIED",
|
433
|
+
"HARM_CATEGORY_HATE_SPEECH",
|
434
|
+
"HARM_CATEGORY_DANGEROUS_CONTENT",
|
435
|
+
"HARM_CATEGORY_HARASSMENT",
|
436
|
+
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
437
|
+
"HARM_CATEGORY_CIVIC_INTEGRITY"
|
438
|
+
]),
|
439
|
+
threshold: z4.enum([
|
440
|
+
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
441
|
+
"BLOCK_LOW_AND_ABOVE",
|
442
|
+
"BLOCK_MEDIUM_AND_ABOVE",
|
443
|
+
"BLOCK_ONLY_HIGH",
|
444
|
+
"BLOCK_NONE",
|
445
|
+
"OFF"
|
446
|
+
])
|
447
|
+
})
|
448
|
+
).optional(),
|
449
|
+
threshold: z4.enum([
|
450
|
+
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
451
|
+
"BLOCK_LOW_AND_ABOVE",
|
452
|
+
"BLOCK_MEDIUM_AND_ABOVE",
|
453
|
+
"BLOCK_ONLY_HIGH",
|
454
|
+
"BLOCK_NONE",
|
455
|
+
"OFF"
|
456
|
+
]).optional(),
|
457
|
+
/**
|
458
|
+
* Optional. Enables timestamp understanding for audio-only files.
|
459
|
+
*
|
460
|
+
* https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
|
461
|
+
*/
|
462
|
+
audioTimestamp: z4.boolean().optional()
|
463
|
+
});
|
464
|
+
|
334
465
|
// src/google-prepare-tools.ts
|
335
466
|
import {
|
336
467
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
@@ -338,8 +469,6 @@ import {
|
|
338
469
|
function prepareTools({
|
339
470
|
tools,
|
340
471
|
toolChoice,
|
341
|
-
useSearchGrounding,
|
342
|
-
dynamicRetrievalConfig,
|
343
472
|
modelId
|
344
473
|
}) {
|
345
474
|
var _a;
|
@@ -347,28 +476,87 @@ function prepareTools({
|
|
347
476
|
const toolWarnings = [];
|
348
477
|
const isGemini2 = modelId.includes("gemini-2");
|
349
478
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
350
|
-
if (
|
479
|
+
if (tools == null) {
|
480
|
+
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
481
|
+
}
|
482
|
+
const hasFunctionTools = tools.some((tool) => tool.type === "function");
|
483
|
+
const hasProviderDefinedTools = tools.some(
|
484
|
+
(tool) => tool.type === "provider-defined"
|
485
|
+
);
|
486
|
+
if (hasFunctionTools && hasProviderDefinedTools) {
|
487
|
+
toolWarnings.push({
|
488
|
+
type: "unsupported-tool",
|
489
|
+
tool: tools.find((tool) => tool.type === "function"),
|
490
|
+
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."
|
491
|
+
});
|
492
|
+
}
|
493
|
+
if (hasProviderDefinedTools) {
|
494
|
+
const googleTools2 = {};
|
495
|
+
const providerDefinedTools = tools.filter(
|
496
|
+
(tool) => tool.type === "provider-defined"
|
497
|
+
);
|
498
|
+
providerDefinedTools.forEach((tool) => {
|
499
|
+
switch (tool.id) {
|
500
|
+
case "google.google_search":
|
501
|
+
if (isGemini2) {
|
502
|
+
googleTools2.googleSearch = {};
|
503
|
+
} else if (supportsDynamicRetrieval) {
|
504
|
+
googleTools2.googleSearchRetrieval = {
|
505
|
+
dynamicRetrievalConfig: {
|
506
|
+
mode: tool.args.mode,
|
507
|
+
dynamicThreshold: tool.args.dynamicThreshold
|
508
|
+
}
|
509
|
+
};
|
510
|
+
} else {
|
511
|
+
googleTools2.googleSearchRetrieval = {};
|
512
|
+
}
|
513
|
+
break;
|
514
|
+
case "google.url_context":
|
515
|
+
if (isGemini2) {
|
516
|
+
googleTools2.urlContext = {};
|
517
|
+
} else {
|
518
|
+
toolWarnings.push({
|
519
|
+
type: "unsupported-tool",
|
520
|
+
tool,
|
521
|
+
details: "The URL context tool is not supported with other Gemini models than Gemini 2."
|
522
|
+
});
|
523
|
+
}
|
524
|
+
break;
|
525
|
+
case "google.code_execution":
|
526
|
+
if (isGemini2) {
|
527
|
+
googleTools2.codeExecution = {};
|
528
|
+
} else {
|
529
|
+
toolWarnings.push({
|
530
|
+
type: "unsupported-tool",
|
531
|
+
tool,
|
532
|
+
details: "The code execution tools is not supported with other Gemini models than Gemini 2."
|
533
|
+
});
|
534
|
+
}
|
535
|
+
break;
|
536
|
+
default:
|
537
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
538
|
+
break;
|
539
|
+
}
|
540
|
+
});
|
351
541
|
return {
|
352
|
-
tools:
|
353
|
-
googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig ? {} : { dynamicRetrievalConfig }
|
354
|
-
},
|
542
|
+
tools: Object.keys(googleTools2).length > 0 ? googleTools2 : void 0,
|
355
543
|
toolConfig: void 0,
|
356
544
|
toolWarnings
|
357
545
|
};
|
358
546
|
}
|
359
|
-
if (tools == null) {
|
360
|
-
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
361
|
-
}
|
362
547
|
const functionDeclarations = [];
|
363
548
|
for (const tool of tools) {
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
549
|
+
switch (tool.type) {
|
550
|
+
case "function":
|
551
|
+
functionDeclarations.push({
|
552
|
+
name: tool.name,
|
553
|
+
description: (_a = tool.description) != null ? _a : "",
|
554
|
+
parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
|
555
|
+
});
|
556
|
+
break;
|
557
|
+
default:
|
558
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
559
|
+
break;
|
372
560
|
}
|
373
561
|
}
|
374
562
|
if (toolChoice == null) {
|
@@ -445,23 +633,80 @@ function mapGoogleGenerativeAIFinishReason({
|
|
445
633
|
}
|
446
634
|
}
|
447
635
|
|
636
|
+
// src/tool/google-search.ts
|
637
|
+
import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
|
638
|
+
import { z as z5 } from "zod/v4";
|
639
|
+
var groundingChunkSchema = z5.object({
|
640
|
+
web: z5.object({ uri: z5.string(), title: z5.string() }).nullish(),
|
641
|
+
retrievedContext: z5.object({ uri: z5.string(), title: z5.string() }).nullish()
|
642
|
+
});
|
643
|
+
var groundingMetadataSchema = z5.object({
|
644
|
+
webSearchQueries: z5.array(z5.string()).nullish(),
|
645
|
+
retrievalQueries: z5.array(z5.string()).nullish(),
|
646
|
+
searchEntryPoint: z5.object({ renderedContent: z5.string() }).nullish(),
|
647
|
+
groundingChunks: z5.array(groundingChunkSchema).nullish(),
|
648
|
+
groundingSupports: z5.array(
|
649
|
+
z5.object({
|
650
|
+
segment: z5.object({
|
651
|
+
startIndex: z5.number().nullish(),
|
652
|
+
endIndex: z5.number().nullish(),
|
653
|
+
text: z5.string().nullish()
|
654
|
+
}),
|
655
|
+
segment_text: z5.string().nullish(),
|
656
|
+
groundingChunkIndices: z5.array(z5.number()).nullish(),
|
657
|
+
supportChunkIndices: z5.array(z5.number()).nullish(),
|
658
|
+
confidenceScores: z5.array(z5.number()).nullish(),
|
659
|
+
confidenceScore: z5.array(z5.number()).nullish()
|
660
|
+
})
|
661
|
+
).nullish(),
|
662
|
+
retrievalMetadata: z5.union([
|
663
|
+
z5.object({
|
664
|
+
webDynamicRetrievalScore: z5.number()
|
665
|
+
}),
|
666
|
+
z5.object({})
|
667
|
+
]).nullish()
|
668
|
+
});
|
669
|
+
var googleSearch = createProviderDefinedToolFactory({
|
670
|
+
id: "google.google_search",
|
671
|
+
name: "google_search",
|
672
|
+
inputSchema: z5.object({
|
673
|
+
mode: z5.enum(["MODE_DYNAMIC", "MODE_UNSPECIFIED"]).default("MODE_UNSPECIFIED"),
|
674
|
+
dynamicThreshold: z5.number().default(1)
|
675
|
+
})
|
676
|
+
});
|
677
|
+
|
678
|
+
// src/tool/url-context.ts
|
679
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
|
680
|
+
import { z as z6 } from "zod/v4";
|
681
|
+
var urlMetadataSchema = z6.object({
|
682
|
+
retrievedUrl: z6.string(),
|
683
|
+
urlRetrievalStatus: z6.string()
|
684
|
+
});
|
685
|
+
var urlContextMetadataSchema = z6.object({
|
686
|
+
urlMetadata: z6.array(urlMetadataSchema)
|
687
|
+
});
|
688
|
+
var urlContext = createProviderDefinedToolFactory2({
|
689
|
+
id: "google.url_context",
|
690
|
+
name: "url_context",
|
691
|
+
inputSchema: z6.object({})
|
692
|
+
});
|
693
|
+
|
448
694
|
// src/google-generative-ai-language-model.ts
|
449
695
|
var GoogleGenerativeAILanguageModel = class {
|
450
|
-
constructor(modelId,
|
696
|
+
constructor(modelId, config) {
|
451
697
|
this.specificationVersion = "v2";
|
452
|
-
|
453
|
-
this.supportsImageUrls = false;
|
698
|
+
var _a;
|
454
699
|
this.modelId = modelId;
|
455
|
-
this.settings = settings;
|
456
700
|
this.config = config;
|
457
|
-
|
458
|
-
get supportsStructuredOutputs() {
|
459
|
-
var _a;
|
460
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : true;
|
701
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
461
702
|
}
|
462
703
|
get provider() {
|
463
704
|
return this.config.provider;
|
464
705
|
}
|
706
|
+
get supportedUrls() {
|
707
|
+
var _a, _b, _c;
|
708
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
709
|
+
}
|
465
710
|
async getArgs({
|
466
711
|
prompt,
|
467
712
|
maxOutputTokens,
|
@@ -477,23 +722,31 @@ var GoogleGenerativeAILanguageModel = class {
|
|
477
722
|
toolChoice,
|
478
723
|
providerOptions
|
479
724
|
}) {
|
480
|
-
var _a;
|
725
|
+
var _a, _b;
|
481
726
|
const warnings = [];
|
482
|
-
const googleOptions =
|
727
|
+
const googleOptions = await parseProviderOptions2({
|
483
728
|
provider: "google",
|
484
729
|
providerOptions,
|
485
|
-
schema:
|
730
|
+
schema: googleGenerativeAIProviderOptions
|
486
731
|
});
|
487
|
-
|
732
|
+
if (((_a = googleOptions == null ? void 0 : googleOptions.thinkingConfig) == null ? void 0 : _a.includeThoughts) === true && !this.config.provider.startsWith("google.vertex.")) {
|
733
|
+
warnings.push({
|
734
|
+
type: "other",
|
735
|
+
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}).`
|
736
|
+
});
|
737
|
+
}
|
738
|
+
const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
|
739
|
+
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
|
740
|
+
prompt,
|
741
|
+
{ isGemmaModel }
|
742
|
+
);
|
488
743
|
const {
|
489
|
-
tools:
|
744
|
+
tools: googleTools2,
|
490
745
|
toolConfig: googleToolConfig,
|
491
746
|
toolWarnings
|
492
747
|
} = prepareTools({
|
493
748
|
tools,
|
494
749
|
toolChoice,
|
495
|
-
useSearchGrounding: (_a = this.settings.useSearchGrounding) != null ? _a : false,
|
496
|
-
dynamicRetrievalConfig: this.settings.dynamicRetrievalConfig,
|
497
750
|
modelId: this.modelId
|
498
751
|
});
|
499
752
|
return {
|
@@ -512,28 +765,27 @@ var GoogleGenerativeAILanguageModel = class {
|
|
512
765
|
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
513
766
|
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
514
767
|
// so this is needed as an escape hatch:
|
515
|
-
|
516
|
-
|
517
|
-
|
768
|
+
// TODO convert into provider option
|
769
|
+
((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
770
|
+
...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
|
771
|
+
audioTimestamp: googleOptions.audioTimestamp
|
518
772
|
},
|
519
773
|
// provider options:
|
520
|
-
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
|
774
|
+
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
|
775
|
+
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
521
776
|
},
|
522
777
|
contents,
|
523
|
-
systemInstruction,
|
524
|
-
safetySettings:
|
525
|
-
tools:
|
778
|
+
systemInstruction: isGemmaModel ? void 0 : systemInstruction,
|
779
|
+
safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
|
780
|
+
tools: googleTools2,
|
526
781
|
toolConfig: googleToolConfig,
|
527
|
-
cachedContent:
|
782
|
+
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
|
528
783
|
},
|
529
784
|
warnings: [...warnings, ...toolWarnings]
|
530
785
|
};
|
531
786
|
}
|
532
|
-
supportsUrl(url) {
|
533
|
-
return this.config.isSupportedUrl(url);
|
534
|
-
}
|
535
787
|
async doGenerate(options) {
|
536
|
-
var _a, _b, _c, _d, _e;
|
788
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
537
789
|
const { args, warnings } = await this.getArgs(options);
|
538
790
|
const body = JSON.stringify(args);
|
539
791
|
const mergedHeaders = combineHeaders2(
|
@@ -556,39 +808,84 @@ var GoogleGenerativeAILanguageModel = class {
|
|
556
808
|
fetch: this.config.fetch
|
557
809
|
});
|
558
810
|
const candidate = response.candidates[0];
|
559
|
-
const
|
560
|
-
const
|
561
|
-
parts,
|
562
|
-
generateId: this.config.generateId
|
563
|
-
});
|
811
|
+
const content = [];
|
812
|
+
const parts = (_b = (_a = candidate.content) == null ? void 0 : _a.parts) != null ? _b : [];
|
564
813
|
const usageMetadata = response.usageMetadata;
|
814
|
+
let lastCodeExecutionToolCallId;
|
815
|
+
for (const part of parts) {
|
816
|
+
if ("executableCode" in part && ((_c = part.executableCode) == null ? void 0 : _c.code)) {
|
817
|
+
const toolCallId = this.config.generateId();
|
818
|
+
lastCodeExecutionToolCallId = toolCallId;
|
819
|
+
content.push({
|
820
|
+
type: "tool-call",
|
821
|
+
toolCallId,
|
822
|
+
toolName: "code_execution",
|
823
|
+
input: JSON.stringify(part.executableCode),
|
824
|
+
providerExecuted: true
|
825
|
+
});
|
826
|
+
} else if ("codeExecutionResult" in part && part.codeExecutionResult) {
|
827
|
+
content.push({
|
828
|
+
type: "tool-result",
|
829
|
+
// Assumes a result directly follows its corresponding call part.
|
830
|
+
toolCallId: lastCodeExecutionToolCallId,
|
831
|
+
toolName: "code_execution",
|
832
|
+
result: {
|
833
|
+
outcome: part.codeExecutionResult.outcome,
|
834
|
+
output: part.codeExecutionResult.output
|
835
|
+
},
|
836
|
+
providerExecuted: true
|
837
|
+
});
|
838
|
+
lastCodeExecutionToolCallId = void 0;
|
839
|
+
} else if ("text" in part && part.text != null && part.text.length > 0) {
|
840
|
+
if (part.thought === true) {
|
841
|
+
content.push({ type: "reasoning", text: part.text });
|
842
|
+
} else {
|
843
|
+
content.push({ type: "text", text: part.text });
|
844
|
+
}
|
845
|
+
} else if ("functionCall" in part) {
|
846
|
+
content.push({
|
847
|
+
type: "tool-call",
|
848
|
+
toolCallId: this.config.generateId(),
|
849
|
+
toolName: part.functionCall.name,
|
850
|
+
input: JSON.stringify(part.functionCall.args)
|
851
|
+
});
|
852
|
+
} else if ("inlineData" in part) {
|
853
|
+
content.push({
|
854
|
+
type: "file",
|
855
|
+
data: part.inlineData.data,
|
856
|
+
mediaType: part.inlineData.mimeType
|
857
|
+
});
|
858
|
+
}
|
859
|
+
}
|
860
|
+
const sources = (_d = extractSources({
|
861
|
+
groundingMetadata: candidate.groundingMetadata,
|
862
|
+
generateId: this.config.generateId
|
863
|
+
})) != null ? _d : [];
|
864
|
+
for (const source of sources) {
|
865
|
+
content.push(source);
|
866
|
+
}
|
565
867
|
return {
|
566
|
-
|
567
|
-
files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
|
568
|
-
type: "file",
|
569
|
-
data: part.inlineData.data,
|
570
|
-
mediaType: part.inlineData.mimeType
|
571
|
-
})),
|
572
|
-
toolCalls,
|
868
|
+
content,
|
573
869
|
finishReason: mapGoogleGenerativeAIFinishReason({
|
574
870
|
finishReason: candidate.finishReason,
|
575
|
-
hasToolCalls:
|
871
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
576
872
|
}),
|
577
873
|
usage: {
|
578
|
-
inputTokens: (
|
579
|
-
outputTokens: (
|
874
|
+
inputTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _e : void 0,
|
875
|
+
outputTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _f : void 0,
|
876
|
+
totalTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _g : void 0,
|
877
|
+
reasoningTokens: (_h = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _h : void 0,
|
878
|
+
cachedInputTokens: (_i = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _i : void 0
|
580
879
|
},
|
581
880
|
warnings,
|
582
881
|
providerMetadata: {
|
583
882
|
google: {
|
584
|
-
groundingMetadata: (
|
585
|
-
|
883
|
+
groundingMetadata: (_j = candidate.groundingMetadata) != null ? _j : null,
|
884
|
+
urlContextMetadata: (_k = candidate.urlContextMetadata) != null ? _k : null,
|
885
|
+
safetyRatings: (_l = candidate.safetyRatings) != null ? _l : null,
|
886
|
+
usageMetadata: usageMetadata != null ? usageMetadata : null
|
586
887
|
}
|
587
888
|
},
|
588
|
-
sources: extractSources({
|
589
|
-
groundingMetadata: candidate.groundingMetadata,
|
590
|
-
generateId: this.config.generateId
|
591
|
-
}),
|
592
889
|
request: { body },
|
593
890
|
response: {
|
594
891
|
// TODO timestamp, model id, id
|
@@ -618,16 +915,28 @@ var GoogleGenerativeAILanguageModel = class {
|
|
618
915
|
let finishReason = "unknown";
|
619
916
|
const usage = {
|
620
917
|
inputTokens: void 0,
|
621
|
-
outputTokens: void 0
|
918
|
+
outputTokens: void 0,
|
919
|
+
totalTokens: void 0
|
622
920
|
};
|
623
921
|
let providerMetadata = void 0;
|
624
|
-
const
|
922
|
+
const generateId3 = this.config.generateId;
|
625
923
|
let hasToolCalls = false;
|
924
|
+
let currentTextBlockId = null;
|
925
|
+
let currentReasoningBlockId = null;
|
926
|
+
let blockCounter = 0;
|
927
|
+
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
928
|
+
let lastCodeExecutionToolCallId;
|
626
929
|
return {
|
627
930
|
stream: response.pipeThrough(
|
628
931
|
new TransformStream({
|
932
|
+
start(controller) {
|
933
|
+
controller.enqueue({ type: "stream-start", warnings });
|
934
|
+
},
|
629
935
|
transform(chunk, controller) {
|
630
|
-
var _a, _b, _c, _d, _e, _f;
|
936
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
937
|
+
if (options.includeRawChunks) {
|
938
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
939
|
+
}
|
631
940
|
if (!chunk.success) {
|
632
941
|
controller.enqueue({ type: "error", error: chunk.error });
|
633
942
|
return;
|
@@ -637,16 +946,99 @@ var GoogleGenerativeAILanguageModel = class {
|
|
637
946
|
if (usageMetadata != null) {
|
638
947
|
usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
|
639
948
|
usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
|
949
|
+
usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
|
950
|
+
usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
|
951
|
+
usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
|
640
952
|
}
|
641
|
-
const candidate = (
|
953
|
+
const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
|
642
954
|
if (candidate == null) {
|
643
955
|
return;
|
644
956
|
}
|
645
957
|
const content = candidate.content;
|
958
|
+
const sources = extractSources({
|
959
|
+
groundingMetadata: candidate.groundingMetadata,
|
960
|
+
generateId: generateId3
|
961
|
+
});
|
962
|
+
if (sources != null) {
|
963
|
+
for (const source of sources) {
|
964
|
+
if (source.sourceType === "url" && !emittedSourceUrls.has(source.url)) {
|
965
|
+
emittedSourceUrls.add(source.url);
|
966
|
+
controller.enqueue(source);
|
967
|
+
}
|
968
|
+
}
|
969
|
+
}
|
646
970
|
if (content != null) {
|
647
|
-
const
|
648
|
-
|
649
|
-
|
971
|
+
const parts = (_g = content.parts) != null ? _g : [];
|
972
|
+
for (const part of parts) {
|
973
|
+
if ("executableCode" in part && ((_h = part.executableCode) == null ? void 0 : _h.code)) {
|
974
|
+
const toolCallId = generateId3();
|
975
|
+
lastCodeExecutionToolCallId = toolCallId;
|
976
|
+
controller.enqueue({
|
977
|
+
type: "tool-call",
|
978
|
+
toolCallId,
|
979
|
+
toolName: "code_execution",
|
980
|
+
input: JSON.stringify(part.executableCode),
|
981
|
+
providerExecuted: true
|
982
|
+
});
|
983
|
+
hasToolCalls = true;
|
984
|
+
} else if ("codeExecutionResult" in part && part.codeExecutionResult) {
|
985
|
+
const toolCallId = lastCodeExecutionToolCallId;
|
986
|
+
if (toolCallId) {
|
987
|
+
controller.enqueue({
|
988
|
+
type: "tool-result",
|
989
|
+
toolCallId,
|
990
|
+
toolName: "code_execution",
|
991
|
+
result: {
|
992
|
+
outcome: part.codeExecutionResult.outcome,
|
993
|
+
output: part.codeExecutionResult.output
|
994
|
+
},
|
995
|
+
providerExecuted: true
|
996
|
+
});
|
997
|
+
lastCodeExecutionToolCallId = void 0;
|
998
|
+
}
|
999
|
+
} else if ("text" in part && part.text != null && part.text.length > 0) {
|
1000
|
+
if (part.thought === true) {
|
1001
|
+
if (currentTextBlockId !== null) {
|
1002
|
+
controller.enqueue({
|
1003
|
+
type: "text-end",
|
1004
|
+
id: currentTextBlockId
|
1005
|
+
});
|
1006
|
+
currentTextBlockId = null;
|
1007
|
+
}
|
1008
|
+
if (currentReasoningBlockId === null) {
|
1009
|
+
currentReasoningBlockId = String(blockCounter++);
|
1010
|
+
controller.enqueue({
|
1011
|
+
type: "reasoning-start",
|
1012
|
+
id: currentReasoningBlockId
|
1013
|
+
});
|
1014
|
+
}
|
1015
|
+
controller.enqueue({
|
1016
|
+
type: "reasoning-delta",
|
1017
|
+
id: currentReasoningBlockId,
|
1018
|
+
delta: part.text
|
1019
|
+
});
|
1020
|
+
} else {
|
1021
|
+
if (currentReasoningBlockId !== null) {
|
1022
|
+
controller.enqueue({
|
1023
|
+
type: "reasoning-end",
|
1024
|
+
id: currentReasoningBlockId
|
1025
|
+
});
|
1026
|
+
currentReasoningBlockId = null;
|
1027
|
+
}
|
1028
|
+
if (currentTextBlockId === null) {
|
1029
|
+
currentTextBlockId = String(blockCounter++);
|
1030
|
+
controller.enqueue({
|
1031
|
+
type: "text-start",
|
1032
|
+
id: currentTextBlockId
|
1033
|
+
});
|
1034
|
+
}
|
1035
|
+
controller.enqueue({
|
1036
|
+
type: "text-delta",
|
1037
|
+
id: currentTextBlockId,
|
1038
|
+
delta: part.text
|
1039
|
+
});
|
1040
|
+
}
|
1041
|
+
}
|
650
1042
|
}
|
651
1043
|
const inlineDataParts = getInlineDataParts(content.parts);
|
652
1044
|
if (inlineDataParts != null) {
|
@@ -660,23 +1052,29 @@ var GoogleGenerativeAILanguageModel = class {
|
|
660
1052
|
}
|
661
1053
|
const toolCallDeltas = getToolCallsFromParts({
|
662
1054
|
parts: content.parts,
|
663
|
-
generateId:
|
1055
|
+
generateId: generateId3
|
664
1056
|
});
|
665
1057
|
if (toolCallDeltas != null) {
|
666
1058
|
for (const toolCall of toolCallDeltas) {
|
667
1059
|
controller.enqueue({
|
668
|
-
type: "tool-
|
669
|
-
|
670
|
-
|
671
|
-
|
672
|
-
|
1060
|
+
type: "tool-input-start",
|
1061
|
+
id: toolCall.toolCallId,
|
1062
|
+
toolName: toolCall.toolName
|
1063
|
+
});
|
1064
|
+
controller.enqueue({
|
1065
|
+
type: "tool-input-delta",
|
1066
|
+
id: toolCall.toolCallId,
|
1067
|
+
delta: toolCall.args
|
1068
|
+
});
|
1069
|
+
controller.enqueue({
|
1070
|
+
type: "tool-input-end",
|
1071
|
+
id: toolCall.toolCallId
|
673
1072
|
});
|
674
1073
|
controller.enqueue({
|
675
1074
|
type: "tool-call",
|
676
|
-
toolCallType: "function",
|
677
1075
|
toolCallId: toolCall.toolCallId,
|
678
1076
|
toolName: toolCall.toolName,
|
679
|
-
|
1077
|
+
input: toolCall.args
|
680
1078
|
});
|
681
1079
|
hasToolCalls = true;
|
682
1080
|
}
|
@@ -687,22 +1085,31 @@ var GoogleGenerativeAILanguageModel = class {
|
|
687
1085
|
finishReason: candidate.finishReason,
|
688
1086
|
hasToolCalls
|
689
1087
|
});
|
690
|
-
const sources = (_d = extractSources({
|
691
|
-
groundingMetadata: candidate.groundingMetadata,
|
692
|
-
generateId: generateId2
|
693
|
-
})) != null ? _d : [];
|
694
|
-
for (const source of sources) {
|
695
|
-
controller.enqueue(source);
|
696
|
-
}
|
697
1088
|
providerMetadata = {
|
698
1089
|
google: {
|
699
|
-
groundingMetadata: (
|
700
|
-
|
1090
|
+
groundingMetadata: (_i = candidate.groundingMetadata) != null ? _i : null,
|
1091
|
+
urlContextMetadata: (_j = candidate.urlContextMetadata) != null ? _j : null,
|
1092
|
+
safetyRatings: (_k = candidate.safetyRatings) != null ? _k : null
|
701
1093
|
}
|
702
1094
|
};
|
1095
|
+
if (usageMetadata != null) {
|
1096
|
+
providerMetadata.google.usageMetadata = usageMetadata;
|
1097
|
+
}
|
703
1098
|
}
|
704
1099
|
},
|
705
1100
|
flush(controller) {
|
1101
|
+
if (currentTextBlockId !== null) {
|
1102
|
+
controller.enqueue({
|
1103
|
+
type: "text-end",
|
1104
|
+
id: currentTextBlockId
|
1105
|
+
});
|
1106
|
+
}
|
1107
|
+
if (currentReasoningBlockId !== null) {
|
1108
|
+
controller.enqueue({
|
1109
|
+
type: "reasoning-end",
|
1110
|
+
id: currentReasoningBlockId
|
1111
|
+
});
|
1112
|
+
}
|
706
1113
|
controller.enqueue({
|
707
1114
|
type: "finish",
|
708
1115
|
finishReason,
|
@@ -713,33 +1120,24 @@ var GoogleGenerativeAILanguageModel = class {
|
|
713
1120
|
})
|
714
1121
|
),
|
715
1122
|
response: { headers: responseHeaders },
|
716
|
-
warnings,
|
717
1123
|
request: { body }
|
718
1124
|
};
|
719
1125
|
}
|
720
1126
|
};
|
721
1127
|
function getToolCallsFromParts({
|
722
1128
|
parts,
|
723
|
-
generateId:
|
1129
|
+
generateId: generateId3
|
724
1130
|
}) {
|
725
1131
|
const functionCallParts = parts == null ? void 0 : parts.filter(
|
726
1132
|
(part) => "functionCall" in part
|
727
1133
|
);
|
728
1134
|
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
729
1135
|
type: "tool-call",
|
730
|
-
|
731
|
-
toolCallId: generateId2(),
|
1136
|
+
toolCallId: generateId3(),
|
732
1137
|
toolName: part.functionCall.name,
|
733
1138
|
args: JSON.stringify(part.functionCall.args)
|
734
1139
|
}));
|
735
1140
|
}
|
736
|
-
function getTextFromParts(parts) {
|
737
|
-
const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
|
738
|
-
return textParts == null || textParts.length === 0 ? void 0 : {
|
739
|
-
type: "text",
|
740
|
-
text: textParts.map((part) => part.text).join("")
|
741
|
-
};
|
742
|
-
}
|
743
1141
|
function getInlineDataParts(parts) {
|
744
1142
|
return parts == null ? void 0 : parts.filter(
|
745
1143
|
(part) => "inlineData" in part
|
@@ -747,7 +1145,7 @@ function getInlineDataParts(parts) {
|
|
747
1145
|
}
|
748
1146
|
function extractSources({
|
749
1147
|
groundingMetadata,
|
750
|
-
generateId:
|
1148
|
+
generateId: generateId3
|
751
1149
|
}) {
|
752
1150
|
var _a;
|
753
1151
|
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
@@ -755,109 +1153,230 @@ function extractSources({
|
|
755
1153
|
).map((chunk) => ({
|
756
1154
|
type: "source",
|
757
1155
|
sourceType: "url",
|
758
|
-
id:
|
1156
|
+
id: generateId3(),
|
759
1157
|
url: chunk.web.uri,
|
760
1158
|
title: chunk.web.title
|
761
1159
|
}));
|
762
1160
|
}
|
763
|
-
var contentSchema =
|
764
|
-
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
770
|
-
|
771
|
-
functionCall: z3.object({
|
772
|
-
name: z3.string(),
|
773
|
-
args: z3.unknown()
|
1161
|
+
var contentSchema = z7.object({
|
1162
|
+
parts: z7.array(
|
1163
|
+
z7.union([
|
1164
|
+
// note: order matters since text can be fully empty
|
1165
|
+
z7.object({
|
1166
|
+
functionCall: z7.object({
|
1167
|
+
name: z7.string(),
|
1168
|
+
args: z7.unknown()
|
774
1169
|
})
|
775
1170
|
}),
|
776
|
-
|
777
|
-
inlineData:
|
778
|
-
mimeType:
|
779
|
-
data:
|
1171
|
+
z7.object({
|
1172
|
+
inlineData: z7.object({
|
1173
|
+
mimeType: z7.string(),
|
1174
|
+
data: z7.string()
|
780
1175
|
})
|
1176
|
+
}),
|
1177
|
+
z7.object({
|
1178
|
+
executableCode: z7.object({
|
1179
|
+
language: z7.string(),
|
1180
|
+
code: z7.string()
|
1181
|
+
}).nullish(),
|
1182
|
+
codeExecutionResult: z7.object({
|
1183
|
+
outcome: z7.string(),
|
1184
|
+
output: z7.string()
|
1185
|
+
}).nullish(),
|
1186
|
+
text: z7.string().nullish(),
|
1187
|
+
thought: z7.boolean().nullish()
|
781
1188
|
})
|
782
1189
|
])
|
783
1190
|
).nullish()
|
784
1191
|
});
|
785
|
-
var
|
786
|
-
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
|
793
|
-
groundingChunks: z3.array(groundingChunkSchema).nullish(),
|
794
|
-
groundingSupports: z3.array(
|
795
|
-
z3.object({
|
796
|
-
segment: z3.object({
|
797
|
-
startIndex: z3.number().nullish(),
|
798
|
-
endIndex: z3.number().nullish(),
|
799
|
-
text: z3.string().nullish()
|
800
|
-
}),
|
801
|
-
segment_text: z3.string().nullish(),
|
802
|
-
groundingChunkIndices: z3.array(z3.number()).nullish(),
|
803
|
-
supportChunkIndices: z3.array(z3.number()).nullish(),
|
804
|
-
confidenceScores: z3.array(z3.number()).nullish(),
|
805
|
-
confidenceScore: z3.array(z3.number()).nullish()
|
806
|
-
})
|
807
|
-
).nullish(),
|
808
|
-
retrievalMetadata: z3.union([
|
809
|
-
z3.object({
|
810
|
-
webDynamicRetrievalScore: z3.number()
|
811
|
-
}),
|
812
|
-
z3.object({})
|
813
|
-
]).nullish()
|
1192
|
+
var safetyRatingSchema = z7.object({
|
1193
|
+
category: z7.string().nullish(),
|
1194
|
+
probability: z7.string().nullish(),
|
1195
|
+
probabilityScore: z7.number().nullish(),
|
1196
|
+
severity: z7.string().nullish(),
|
1197
|
+
severityScore: z7.number().nullish(),
|
1198
|
+
blocked: z7.boolean().nullish()
|
814
1199
|
});
|
815
|
-
var
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
|
820
|
-
|
821
|
-
blocked: z3.boolean().nullish()
|
1200
|
+
var usageSchema = z7.object({
|
1201
|
+
cachedContentTokenCount: z7.number().nullish(),
|
1202
|
+
thoughtsTokenCount: z7.number().nullish(),
|
1203
|
+
promptTokenCount: z7.number().nullish(),
|
1204
|
+
candidatesTokenCount: z7.number().nullish(),
|
1205
|
+
totalTokenCount: z7.number().nullish()
|
822
1206
|
});
|
823
|
-
var responseSchema =
|
824
|
-
candidates:
|
825
|
-
|
826
|
-
content: contentSchema.nullish().or(
|
827
|
-
finishReason:
|
828
|
-
safetyRatings:
|
829
|
-
groundingMetadata: groundingMetadataSchema.nullish()
|
1207
|
+
var responseSchema = z7.object({
|
1208
|
+
candidates: z7.array(
|
1209
|
+
z7.object({
|
1210
|
+
content: contentSchema.nullish().or(z7.object({}).strict()),
|
1211
|
+
finishReason: z7.string().nullish(),
|
1212
|
+
safetyRatings: z7.array(safetyRatingSchema).nullish(),
|
1213
|
+
groundingMetadata: groundingMetadataSchema.nullish(),
|
1214
|
+
urlContextMetadata: urlContextMetadataSchema.nullish()
|
830
1215
|
})
|
831
1216
|
),
|
832
|
-
usageMetadata:
|
833
|
-
promptTokenCount: z3.number().nullish(),
|
834
|
-
candidatesTokenCount: z3.number().nullish(),
|
835
|
-
totalTokenCount: z3.number().nullish()
|
836
|
-
}).nullish()
|
1217
|
+
usageMetadata: usageSchema.nullish()
|
837
1218
|
});
|
838
|
-
var chunkSchema =
|
839
|
-
candidates:
|
840
|
-
|
1219
|
+
var chunkSchema = z7.object({
|
1220
|
+
candidates: z7.array(
|
1221
|
+
z7.object({
|
841
1222
|
content: contentSchema.nullish(),
|
842
|
-
finishReason:
|
843
|
-
safetyRatings:
|
844
|
-
groundingMetadata: groundingMetadataSchema.nullish()
|
1223
|
+
finishReason: z7.string().nullish(),
|
1224
|
+
safetyRatings: z7.array(safetyRatingSchema).nullish(),
|
1225
|
+
groundingMetadata: groundingMetadataSchema.nullish(),
|
1226
|
+
urlContextMetadata: urlContextMetadataSchema.nullish()
|
845
1227
|
})
|
846
1228
|
).nullish(),
|
847
|
-
usageMetadata:
|
848
|
-
promptTokenCount: z3.number().nullish(),
|
849
|
-
candidatesTokenCount: z3.number().nullish(),
|
850
|
-
totalTokenCount: z3.number().nullish()
|
851
|
-
}).nullish()
|
1229
|
+
usageMetadata: usageSchema.nullish()
|
852
1230
|
});
|
853
|
-
|
854
|
-
|
1231
|
+
|
1232
|
+
// src/tool/code-execution.ts
|
1233
|
+
import { createProviderDefinedToolFactoryWithOutputSchema } from "@ai-sdk/provider-utils";
|
1234
|
+
import { z as z8 } from "zod/v4";
|
1235
|
+
var codeExecution = createProviderDefinedToolFactoryWithOutputSchema({
|
1236
|
+
id: "google.code_execution",
|
1237
|
+
name: "code_execution",
|
1238
|
+
inputSchema: z8.object({
|
1239
|
+
language: z8.string().describe("The programming language of the code."),
|
1240
|
+
code: z8.string().describe("The code to be executed.")
|
1241
|
+
}),
|
1242
|
+
outputSchema: z8.object({
|
1243
|
+
outcome: z8.string().describe('The outcome of the execution (e.g., "OUTCOME_OK").'),
|
1244
|
+
output: z8.string().describe("The output from the code execution.")
|
1245
|
+
})
|
855
1246
|
});
|
856
1247
|
|
857
|
-
// src/google-
|
858
|
-
|
859
|
-
|
860
|
-
|
1248
|
+
// src/google-tools.ts
|
1249
|
+
var googleTools = {
|
1250
|
+
/**
|
1251
|
+
* Creates a Google search tool that gives Google direct access to real-time web content.
|
1252
|
+
* Must have name "google_search".
|
1253
|
+
*/
|
1254
|
+
googleSearch,
|
1255
|
+
/**
|
1256
|
+
* Creates a URL context tool that gives Google direct access to real-time web content.
|
1257
|
+
* Must have name "url_context".
|
1258
|
+
*/
|
1259
|
+
urlContext,
|
1260
|
+
/**
|
1261
|
+
* A tool that enables the model to generate and run Python code.
|
1262
|
+
* Must have name "code_execution".
|
1263
|
+
*
|
1264
|
+
* @note Ensure the selected model supports Code Execution.
|
1265
|
+
* Multi-tool usage with the code execution tool is typically compatible with Gemini >=2 models.
|
1266
|
+
*
|
1267
|
+
* @see https://ai.google.dev/gemini-api/docs/code-execution (Google AI)
|
1268
|
+
* @see https://cloud.google.com/vertex-ai/generative-ai/docs/model-reference/code-execution-api (Vertex AI)
|
1269
|
+
*/
|
1270
|
+
codeExecution
|
1271
|
+
};
|
1272
|
+
|
1273
|
+
// src/google-generative-ai-image-model.ts
|
1274
|
+
import {
|
1275
|
+
combineHeaders as combineHeaders3,
|
1276
|
+
createJsonResponseHandler as createJsonResponseHandler3,
|
1277
|
+
parseProviderOptions as parseProviderOptions3,
|
1278
|
+
postJsonToApi as postJsonToApi3,
|
1279
|
+
resolve as resolve3
|
1280
|
+
} from "@ai-sdk/provider-utils";
|
1281
|
+
import { z as z9 } from "zod/v4";
|
1282
|
+
var GoogleGenerativeAIImageModel = class {
|
1283
|
+
constructor(modelId, settings, config) {
|
1284
|
+
this.modelId = modelId;
|
1285
|
+
this.settings = settings;
|
1286
|
+
this.config = config;
|
1287
|
+
this.specificationVersion = "v2";
|
1288
|
+
}
|
1289
|
+
get maxImagesPerCall() {
|
1290
|
+
var _a;
|
1291
|
+
return (_a = this.settings.maxImagesPerCall) != null ? _a : 4;
|
1292
|
+
}
|
1293
|
+
get provider() {
|
1294
|
+
return this.config.provider;
|
1295
|
+
}
|
1296
|
+
async doGenerate(options) {
|
1297
|
+
var _a, _b, _c;
|
1298
|
+
const {
|
1299
|
+
prompt,
|
1300
|
+
n = 1,
|
1301
|
+
size = "1024x1024",
|
1302
|
+
aspectRatio = "1:1",
|
1303
|
+
seed,
|
1304
|
+
providerOptions,
|
1305
|
+
headers,
|
1306
|
+
abortSignal
|
1307
|
+
} = options;
|
1308
|
+
const warnings = [];
|
1309
|
+
if (size != null) {
|
1310
|
+
warnings.push({
|
1311
|
+
type: "unsupported-setting",
|
1312
|
+
setting: "size",
|
1313
|
+
details: "This model does not support the `size` option. Use `aspectRatio` instead."
|
1314
|
+
});
|
1315
|
+
}
|
1316
|
+
if (seed != null) {
|
1317
|
+
warnings.push({
|
1318
|
+
type: "unsupported-setting",
|
1319
|
+
setting: "seed",
|
1320
|
+
details: "This model does not support the `seed` option through this provider."
|
1321
|
+
});
|
1322
|
+
}
|
1323
|
+
const googleOptions = await parseProviderOptions3({
|
1324
|
+
provider: "google",
|
1325
|
+
providerOptions,
|
1326
|
+
schema: googleImageProviderOptionsSchema
|
1327
|
+
});
|
1328
|
+
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
1329
|
+
const parameters = {
|
1330
|
+
sampleCount: n
|
1331
|
+
};
|
1332
|
+
if (aspectRatio != null) {
|
1333
|
+
parameters.aspectRatio = aspectRatio;
|
1334
|
+
}
|
1335
|
+
if (googleOptions) {
|
1336
|
+
Object.assign(parameters, googleOptions);
|
1337
|
+
}
|
1338
|
+
const body = {
|
1339
|
+
instances: [{ prompt }],
|
1340
|
+
parameters
|
1341
|
+
};
|
1342
|
+
const { responseHeaders, value: response } = await postJsonToApi3({
|
1343
|
+
url: `${this.config.baseURL}/models/${this.modelId}:predict`,
|
1344
|
+
headers: combineHeaders3(await resolve3(this.config.headers), headers),
|
1345
|
+
body,
|
1346
|
+
failedResponseHandler: googleFailedResponseHandler,
|
1347
|
+
successfulResponseHandler: createJsonResponseHandler3(
|
1348
|
+
googleImageResponseSchema
|
1349
|
+
),
|
1350
|
+
abortSignal,
|
1351
|
+
fetch: this.config.fetch
|
1352
|
+
});
|
1353
|
+
return {
|
1354
|
+
images: response.predictions.map(
|
1355
|
+
(p) => p.bytesBase64Encoded
|
1356
|
+
),
|
1357
|
+
warnings: warnings != null ? warnings : [],
|
1358
|
+
providerMetadata: {
|
1359
|
+
google: {
|
1360
|
+
images: response.predictions.map((prediction) => ({
|
1361
|
+
// Add any prediction-specific metadata here
|
1362
|
+
}))
|
1363
|
+
}
|
1364
|
+
},
|
1365
|
+
response: {
|
1366
|
+
timestamp: currentDate,
|
1367
|
+
modelId: this.modelId,
|
1368
|
+
headers: responseHeaders
|
1369
|
+
}
|
1370
|
+
};
|
1371
|
+
}
|
1372
|
+
};
|
1373
|
+
var googleImageResponseSchema = z9.object({
|
1374
|
+
predictions: z9.array(z9.object({ bytesBase64Encoded: z9.string() })).default([])
|
1375
|
+
});
|
1376
|
+
var googleImageProviderOptionsSchema = z9.object({
|
1377
|
+
personGeneration: z9.enum(["dont_allow", "allow_adult", "allow_all"]).nullish(),
|
1378
|
+
aspectRatio: z9.enum(["1:1", "3:4", "4:3", "9:16", "16:9"]).nullish()
|
1379
|
+
});
|
861
1380
|
|
862
1381
|
// src/google-provider.ts
|
863
1382
|
function createGoogleGenerativeAI(options = {}) {
|
@@ -871,30 +1390,42 @@ function createGoogleGenerativeAI(options = {}) {
|
|
871
1390
|
}),
|
872
1391
|
...options.headers
|
873
1392
|
});
|
874
|
-
const createChatModel = (modelId
|
1393
|
+
const createChatModel = (modelId) => {
|
875
1394
|
var _a2;
|
876
|
-
return new GoogleGenerativeAILanguageModel(modelId,
|
1395
|
+
return new GoogleGenerativeAILanguageModel(modelId, {
|
877
1396
|
provider: "google.generative-ai",
|
878
1397
|
baseURL,
|
879
1398
|
headers: getHeaders,
|
880
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
881
|
-
|
1399
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId2,
|
1400
|
+
supportedUrls: () => ({
|
1401
|
+
"*": [
|
1402
|
+
// Only allow requests to the Google Generative Language "files" endpoint
|
1403
|
+
// e.g. https://generativelanguage.googleapis.com/v1beta/files/...
|
1404
|
+
new RegExp(`^${baseURL}/files/.*$`)
|
1405
|
+
]
|
1406
|
+
}),
|
882
1407
|
fetch: options.fetch
|
883
1408
|
});
|
884
1409
|
};
|
885
|
-
const createEmbeddingModel = (modelId
|
1410
|
+
const createEmbeddingModel = (modelId) => new GoogleGenerativeAIEmbeddingModel(modelId, {
|
886
1411
|
provider: "google.generative-ai",
|
887
1412
|
baseURL,
|
888
1413
|
headers: getHeaders,
|
889
1414
|
fetch: options.fetch
|
890
1415
|
});
|
891
|
-
const
|
1416
|
+
const createImageModel = (modelId, settings = {}) => new GoogleGenerativeAIImageModel(modelId, settings, {
|
1417
|
+
provider: "google.generative-ai",
|
1418
|
+
baseURL,
|
1419
|
+
headers: getHeaders,
|
1420
|
+
fetch: options.fetch
|
1421
|
+
});
|
1422
|
+
const provider = function(modelId) {
|
892
1423
|
if (new.target) {
|
893
1424
|
throw new Error(
|
894
1425
|
"The Google Generative AI model function cannot be called with the new keyword."
|
895
1426
|
);
|
896
1427
|
}
|
897
|
-
return createChatModel(modelId
|
1428
|
+
return createChatModel(modelId);
|
898
1429
|
};
|
899
1430
|
provider.languageModel = createChatModel;
|
900
1431
|
provider.chat = createChatModel;
|
@@ -902,9 +1433,9 @@ function createGoogleGenerativeAI(options = {}) {
|
|
902
1433
|
provider.embedding = createEmbeddingModel;
|
903
1434
|
provider.textEmbedding = createEmbeddingModel;
|
904
1435
|
provider.textEmbeddingModel = createEmbeddingModel;
|
905
|
-
provider.
|
906
|
-
|
907
|
-
|
1436
|
+
provider.image = createImageModel;
|
1437
|
+
provider.imageModel = createImageModel;
|
1438
|
+
provider.tools = googleTools;
|
908
1439
|
return provider;
|
909
1440
|
}
|
910
1441
|
var google = createGoogleGenerativeAI();
|