@ai-sdk/google 2.0.0-alpha.9 → 2.0.0-beta.10
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 +142 -0
- package/dist/index.d.mts +110 -290
- package/dist/index.d.ts +110 -290
- package/dist/index.js +472 -209
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +448 -182
- 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 +352 -196
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +328 -173
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/internal/index.mjs
CHANGED
@@ -3,11 +3,12 @@ import {
|
|
3
3
|
combineHeaders,
|
4
4
|
createEventSourceResponseHandler,
|
5
5
|
createJsonResponseHandler,
|
6
|
+
generateId,
|
6
7
|
parseProviderOptions,
|
7
8
|
postJsonToApi,
|
8
9
|
resolve
|
9
10
|
} from "@ai-sdk/provider-utils";
|
10
|
-
import { z as
|
11
|
+
import { z as z5 } from "zod/v4";
|
11
12
|
|
12
13
|
// src/convert-json-schema-to-openapi-schema.ts
|
13
14
|
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
@@ -103,20 +104,20 @@ function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
|
103
104
|
return result;
|
104
105
|
}
|
105
106
|
function isEmptyObjectSchema(jsonSchema) {
|
106
|
-
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0);
|
107
|
+
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
|
107
108
|
}
|
108
109
|
|
109
110
|
// src/convert-to-google-generative-ai-messages.ts
|
110
111
|
import {
|
111
112
|
UnsupportedFunctionalityError
|
112
113
|
} from "@ai-sdk/provider";
|
113
|
-
import {
|
114
|
-
|
115
|
-
|
116
|
-
function convertToGoogleGenerativeAIMessages(prompt) {
|
114
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
115
|
+
function convertToGoogleGenerativeAIMessages(prompt, options) {
|
116
|
+
var _a;
|
117
117
|
const systemInstructionParts = [];
|
118
118
|
const contents = [];
|
119
119
|
let systemMessagesAllowed = true;
|
120
|
+
const isGemmaModel = (_a = options == null ? void 0 : options.isGemmaModel) != null ? _a : false;
|
120
121
|
for (const { role, content } of prompt) {
|
121
122
|
switch (role) {
|
122
123
|
case "system": {
|
@@ -190,7 +191,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
190
191
|
return {
|
191
192
|
functionCall: {
|
192
193
|
name: part.toolName,
|
193
|
-
args: part.
|
194
|
+
args: part.input
|
194
195
|
}
|
195
196
|
};
|
196
197
|
}
|
@@ -208,7 +209,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
208
209
|
name: part.toolName,
|
209
210
|
response: {
|
210
211
|
name: part.toolName,
|
211
|
-
content: part.
|
212
|
+
content: part.output.value
|
212
213
|
}
|
213
214
|
}
|
214
215
|
}))
|
@@ -217,8 +218,12 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
217
218
|
}
|
218
219
|
}
|
219
220
|
}
|
221
|
+
if (isGemmaModel && systemInstructionParts.length > 0 && contents.length > 0 && contents[0].role === "user") {
|
222
|
+
const systemText = systemInstructionParts.map((part) => part.text).join("\n\n");
|
223
|
+
contents[0].parts.unshift({ text: systemText + "\n\n" });
|
224
|
+
}
|
220
225
|
return {
|
221
|
-
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0,
|
226
|
+
systemInstruction: systemInstructionParts.length > 0 && !isGemmaModel ? { parts: systemInstructionParts } : void 0,
|
222
227
|
contents
|
223
228
|
};
|
224
229
|
}
|
@@ -230,7 +235,7 @@ function getModelPath(modelId) {
|
|
230
235
|
|
231
236
|
// src/google-error.ts
|
232
237
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
233
|
-
import { z } from "zod";
|
238
|
+
import { z } from "zod/v4";
|
234
239
|
var googleErrorDataSchema = z.object({
|
235
240
|
error: z.object({
|
236
241
|
code: z.number().nullable(),
|
@@ -244,22 +249,12 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
244
249
|
});
|
245
250
|
|
246
251
|
// src/google-generative-ai-options.ts
|
247
|
-
import { z as z2 } from "zod";
|
248
|
-
var dynamicRetrievalConfig = z2.object({
|
249
|
-
/**
|
250
|
-
* The mode of the predictor to be used in dynamic retrieval.
|
251
|
-
*/
|
252
|
-
mode: z2.enum(["MODE_UNSPECIFIED", "MODE_DYNAMIC"]).optional(),
|
253
|
-
/**
|
254
|
-
* The threshold to be used in dynamic retrieval. If not set, a system default
|
255
|
-
* value is used.
|
256
|
-
*/
|
257
|
-
dynamicThreshold: z2.number().optional()
|
258
|
-
});
|
252
|
+
import { z as z2 } from "zod/v4";
|
259
253
|
var googleGenerativeAIProviderOptions = z2.object({
|
260
254
|
responseModalities: z2.array(z2.enum(["TEXT", "IMAGE"])).optional(),
|
261
255
|
thinkingConfig: z2.object({
|
262
|
-
thinkingBudget: z2.number().optional()
|
256
|
+
thinkingBudget: z2.number().optional(),
|
257
|
+
includeThoughts: z2.boolean().optional()
|
263
258
|
}).optional(),
|
264
259
|
/**
|
265
260
|
Optional.
|
@@ -312,21 +307,7 @@ var googleGenerativeAIProviderOptions = z2.object({
|
|
312
307
|
*
|
313
308
|
* https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
|
314
309
|
*/
|
315
|
-
audioTimestamp: z2.boolean().optional()
|
316
|
-
/**
|
317
|
-
Optional. When enabled, the model will use Google search to ground the response.
|
318
|
-
|
319
|
-
@see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
|
320
|
-
*/
|
321
|
-
useSearchGrounding: z2.boolean().optional(),
|
322
|
-
/**
|
323
|
-
Optional. Specifies the dynamic retrieval configuration.
|
324
|
-
|
325
|
-
@note Dynamic retrieval is only compatible with Gemini 1.5 Flash.
|
326
|
-
|
327
|
-
@see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
|
328
|
-
*/
|
329
|
-
dynamicRetrievalConfig: dynamicRetrievalConfig.optional()
|
310
|
+
audioTimestamp: z2.boolean().optional()
|
330
311
|
});
|
331
312
|
|
332
313
|
// src/google-prepare-tools.ts
|
@@ -336,8 +317,6 @@ import {
|
|
336
317
|
function prepareTools({
|
337
318
|
tools,
|
338
319
|
toolChoice,
|
339
|
-
useSearchGrounding,
|
340
|
-
dynamicRetrievalConfig: dynamicRetrievalConfig2,
|
341
320
|
modelId
|
342
321
|
}) {
|
343
322
|
var _a;
|
@@ -345,28 +324,76 @@ function prepareTools({
|
|
345
324
|
const toolWarnings = [];
|
346
325
|
const isGemini2 = modelId.includes("gemini-2");
|
347
326
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
348
|
-
if (
|
327
|
+
if (tools == null) {
|
328
|
+
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
329
|
+
}
|
330
|
+
const hasFunctionTools = tools.some((tool) => tool.type === "function");
|
331
|
+
const hasProviderDefinedTools = tools.some(
|
332
|
+
(tool) => tool.type === "provider-defined"
|
333
|
+
);
|
334
|
+
if (hasFunctionTools && hasProviderDefinedTools) {
|
335
|
+
toolWarnings.push({
|
336
|
+
type: "unsupported-tool",
|
337
|
+
tool: tools.find((tool) => tool.type === "function"),
|
338
|
+
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."
|
339
|
+
});
|
340
|
+
}
|
341
|
+
if (hasProviderDefinedTools) {
|
342
|
+
const googleTools2 = {};
|
343
|
+
const providerDefinedTools = tools.filter(
|
344
|
+
(tool) => tool.type === "provider-defined"
|
345
|
+
);
|
346
|
+
providerDefinedTools.forEach((tool) => {
|
347
|
+
switch (tool.id) {
|
348
|
+
case "google.google_search":
|
349
|
+
if (isGemini2) {
|
350
|
+
googleTools2.googleSearch = {};
|
351
|
+
} else if (supportsDynamicRetrieval) {
|
352
|
+
googleTools2.googleSearchRetrieval = {
|
353
|
+
dynamicRetrievalConfig: {
|
354
|
+
mode: tool.args.mode,
|
355
|
+
dynamicThreshold: tool.args.dynamicThreshold
|
356
|
+
}
|
357
|
+
};
|
358
|
+
} else {
|
359
|
+
googleTools2.googleSearchRetrieval = {};
|
360
|
+
}
|
361
|
+
break;
|
362
|
+
case "google.url_context":
|
363
|
+
if (isGemini2) {
|
364
|
+
googleTools2.urlContext = {};
|
365
|
+
} else {
|
366
|
+
toolWarnings.push({
|
367
|
+
type: "unsupported-tool",
|
368
|
+
tool,
|
369
|
+
details: "The URL context tool is not supported with other Gemini models than Gemini 2."
|
370
|
+
});
|
371
|
+
}
|
372
|
+
break;
|
373
|
+
default:
|
374
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
375
|
+
break;
|
376
|
+
}
|
377
|
+
});
|
349
378
|
return {
|
350
|
-
tools:
|
351
|
-
googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig2 ? {} : { dynamicRetrievalConfig: dynamicRetrievalConfig2 }
|
352
|
-
},
|
379
|
+
tools: Object.keys(googleTools2).length > 0 ? googleTools2 : void 0,
|
353
380
|
toolConfig: void 0,
|
354
381
|
toolWarnings
|
355
382
|
};
|
356
383
|
}
|
357
|
-
if (tools == null) {
|
358
|
-
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
359
|
-
}
|
360
384
|
const functionDeclarations = [];
|
361
385
|
for (const tool of tools) {
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
386
|
+
switch (tool.type) {
|
387
|
+
case "function":
|
388
|
+
functionDeclarations.push({
|
389
|
+
name: tool.name,
|
390
|
+
description: (_a = tool.description) != null ? _a : "",
|
391
|
+
parameters: convertJSONSchemaToOpenAPISchema(tool.inputSchema)
|
392
|
+
});
|
393
|
+
break;
|
394
|
+
default:
|
395
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
396
|
+
break;
|
370
397
|
}
|
371
398
|
}
|
372
399
|
if (toolChoice == null) {
|
@@ -443,12 +470,72 @@ function mapGoogleGenerativeAIFinishReason({
|
|
443
470
|
}
|
444
471
|
}
|
445
472
|
|
473
|
+
// src/tool/google-search.ts
|
474
|
+
import { createProviderDefinedToolFactory } from "@ai-sdk/provider-utils";
|
475
|
+
import { z as z3 } from "zod/v4";
|
476
|
+
var groundingChunkSchema = z3.object({
|
477
|
+
web: z3.object({ uri: z3.string(), title: z3.string() }).nullish(),
|
478
|
+
retrievedContext: z3.object({ uri: z3.string(), title: z3.string() }).nullish()
|
479
|
+
});
|
480
|
+
var groundingMetadataSchema = z3.object({
|
481
|
+
webSearchQueries: z3.array(z3.string()).nullish(),
|
482
|
+
retrievalQueries: z3.array(z3.string()).nullish(),
|
483
|
+
searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
|
484
|
+
groundingChunks: z3.array(groundingChunkSchema).nullish(),
|
485
|
+
groundingSupports: z3.array(
|
486
|
+
z3.object({
|
487
|
+
segment: z3.object({
|
488
|
+
startIndex: z3.number().nullish(),
|
489
|
+
endIndex: z3.number().nullish(),
|
490
|
+
text: z3.string().nullish()
|
491
|
+
}),
|
492
|
+
segment_text: z3.string().nullish(),
|
493
|
+
groundingChunkIndices: z3.array(z3.number()).nullish(),
|
494
|
+
supportChunkIndices: z3.array(z3.number()).nullish(),
|
495
|
+
confidenceScores: z3.array(z3.number()).nullish(),
|
496
|
+
confidenceScore: z3.array(z3.number()).nullish()
|
497
|
+
})
|
498
|
+
).nullish(),
|
499
|
+
retrievalMetadata: z3.union([
|
500
|
+
z3.object({
|
501
|
+
webDynamicRetrievalScore: z3.number()
|
502
|
+
}),
|
503
|
+
z3.object({})
|
504
|
+
]).nullish()
|
505
|
+
});
|
506
|
+
var googleSearch = createProviderDefinedToolFactory({
|
507
|
+
id: "google.google_search",
|
508
|
+
name: "google_search",
|
509
|
+
inputSchema: z3.object({
|
510
|
+
mode: z3.enum(["MODE_DYNAMIC", "MODE_UNSPECIFIED"]).default("MODE_UNSPECIFIED"),
|
511
|
+
dynamicThreshold: z3.number().default(1)
|
512
|
+
})
|
513
|
+
});
|
514
|
+
|
515
|
+
// src/tool/url-context.ts
|
516
|
+
import { createProviderDefinedToolFactory as createProviderDefinedToolFactory2 } from "@ai-sdk/provider-utils";
|
517
|
+
import { z as z4 } from "zod/v4";
|
518
|
+
var urlMetadataSchema = z4.object({
|
519
|
+
retrievedUrl: z4.string(),
|
520
|
+
urlRetrievalStatus: z4.string()
|
521
|
+
});
|
522
|
+
var urlContextMetadataSchema = z4.object({
|
523
|
+
urlMetadata: z4.array(urlMetadataSchema)
|
524
|
+
});
|
525
|
+
var urlContext = createProviderDefinedToolFactory2({
|
526
|
+
id: "google.url_context",
|
527
|
+
name: "url_context",
|
528
|
+
inputSchema: z4.object({})
|
529
|
+
});
|
530
|
+
|
446
531
|
// src/google-generative-ai-language-model.ts
|
447
532
|
var GoogleGenerativeAILanguageModel = class {
|
448
533
|
constructor(modelId, config) {
|
449
534
|
this.specificationVersion = "v2";
|
535
|
+
var _a;
|
450
536
|
this.modelId = modelId;
|
451
537
|
this.config = config;
|
538
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
452
539
|
}
|
453
540
|
get provider() {
|
454
541
|
return this.config.provider;
|
@@ -479,16 +566,24 @@ var GoogleGenerativeAILanguageModel = class {
|
|
479
566
|
providerOptions,
|
480
567
|
schema: googleGenerativeAIProviderOptions
|
481
568
|
});
|
482
|
-
|
569
|
+
if (((_a = googleOptions == null ? void 0 : googleOptions.thinkingConfig) == null ? void 0 : _a.includeThoughts) === true && !this.config.provider.startsWith("google.vertex.")) {
|
570
|
+
warnings.push({
|
571
|
+
type: "other",
|
572
|
+
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}).`
|
573
|
+
});
|
574
|
+
}
|
575
|
+
const isGemmaModel = this.modelId.toLowerCase().startsWith("gemma-");
|
576
|
+
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
|
577
|
+
prompt,
|
578
|
+
{ isGemmaModel }
|
579
|
+
);
|
483
580
|
const {
|
484
|
-
tools:
|
581
|
+
tools: googleTools2,
|
485
582
|
toolConfig: googleToolConfig,
|
486
583
|
toolWarnings
|
487
584
|
} = prepareTools({
|
488
585
|
tools,
|
489
586
|
toolChoice,
|
490
|
-
useSearchGrounding: (_a = googleOptions == null ? void 0 : googleOptions.useSearchGrounding) != null ? _a : false,
|
491
|
-
dynamicRetrievalConfig: googleOptions == null ? void 0 : googleOptions.dynamicRetrievalConfig,
|
492
587
|
modelId: this.modelId
|
493
588
|
});
|
494
589
|
return {
|
@@ -517,9 +612,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
517
612
|
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
518
613
|
},
|
519
614
|
contents,
|
520
|
-
systemInstruction,
|
615
|
+
systemInstruction: isGemmaModel ? void 0 : systemInstruction,
|
521
616
|
safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
|
522
|
-
tools:
|
617
|
+
tools: googleTools2,
|
523
618
|
toolConfig: googleToolConfig,
|
524
619
|
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
|
525
620
|
},
|
@@ -527,7 +622,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
527
622
|
};
|
528
623
|
}
|
529
624
|
async doGenerate(options) {
|
530
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
625
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
531
626
|
const { args, warnings } = await this.getArgs(options);
|
532
627
|
const body = JSON.stringify(args);
|
533
628
|
const mergedHeaders = combineHeaders(
|
@@ -552,16 +647,20 @@ var GoogleGenerativeAILanguageModel = class {
|
|
552
647
|
const candidate = response.candidates[0];
|
553
648
|
const content = [];
|
554
649
|
const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : (_a = candidate.content.parts) != null ? _a : [];
|
650
|
+
const usageMetadata = response.usageMetadata;
|
555
651
|
for (const part of parts) {
|
556
|
-
if ("text" in part && part.text.length > 0) {
|
557
|
-
|
652
|
+
if ("text" in part && part.text != null && part.text.length > 0) {
|
653
|
+
if (part.thought === true) {
|
654
|
+
content.push({ type: "reasoning", text: part.text });
|
655
|
+
} else {
|
656
|
+
content.push({ type: "text", text: part.text });
|
657
|
+
}
|
558
658
|
} else if ("functionCall" in part) {
|
559
659
|
content.push({
|
560
660
|
type: "tool-call",
|
561
|
-
toolCallType: "function",
|
562
661
|
toolCallId: this.config.generateId(),
|
563
662
|
toolName: part.functionCall.name,
|
564
|
-
|
663
|
+
input: JSON.stringify(part.functionCall.args)
|
565
664
|
});
|
566
665
|
} else if ("inlineData" in part) {
|
567
666
|
content.push({
|
@@ -578,7 +677,6 @@ var GoogleGenerativeAILanguageModel = class {
|
|
578
677
|
for (const source of sources) {
|
579
678
|
content.push(source);
|
580
679
|
}
|
581
|
-
const usageMetadata = response.usageMetadata;
|
582
680
|
return {
|
583
681
|
content,
|
584
682
|
finishReason: mapGoogleGenerativeAIFinishReason({
|
@@ -596,7 +694,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
596
694
|
providerMetadata: {
|
597
695
|
google: {
|
598
696
|
groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
|
599
|
-
|
697
|
+
urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
|
698
|
+
safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null,
|
699
|
+
usageMetadata: usageMetadata != null ? usageMetadata : null
|
600
700
|
}
|
601
701
|
},
|
602
702
|
request: { body },
|
@@ -632,8 +732,12 @@ var GoogleGenerativeAILanguageModel = class {
|
|
632
732
|
totalTokens: void 0
|
633
733
|
};
|
634
734
|
let providerMetadata = void 0;
|
635
|
-
const
|
735
|
+
const generateId2 = this.config.generateId;
|
636
736
|
let hasToolCalls = false;
|
737
|
+
let currentTextBlockId = null;
|
738
|
+
let currentReasoningBlockId = null;
|
739
|
+
let blockCounter = 0;
|
740
|
+
const emittedSourceUrls = /* @__PURE__ */ new Set();
|
637
741
|
return {
|
638
742
|
stream: response.pipeThrough(
|
639
743
|
new TransformStream({
|
@@ -641,7 +745,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
641
745
|
controller.enqueue({ type: "stream-start", warnings });
|
642
746
|
},
|
643
747
|
transform(chunk, controller) {
|
644
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
748
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
749
|
+
if (options.includeRawChunks) {
|
750
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
751
|
+
}
|
645
752
|
if (!chunk.success) {
|
646
753
|
controller.enqueue({ type: "error", error: chunk.error });
|
647
754
|
return;
|
@@ -660,10 +767,64 @@ var GoogleGenerativeAILanguageModel = class {
|
|
660
767
|
return;
|
661
768
|
}
|
662
769
|
const content = candidate.content;
|
770
|
+
const sources = extractSources({
|
771
|
+
groundingMetadata: candidate.groundingMetadata,
|
772
|
+
generateId: generateId2
|
773
|
+
});
|
774
|
+
if (sources != null) {
|
775
|
+
for (const source of sources) {
|
776
|
+
if (source.sourceType === "url" && !emittedSourceUrls.has(source.url)) {
|
777
|
+
emittedSourceUrls.add(source.url);
|
778
|
+
controller.enqueue(source);
|
779
|
+
}
|
780
|
+
}
|
781
|
+
}
|
663
782
|
if (content != null) {
|
664
|
-
const
|
665
|
-
|
666
|
-
|
783
|
+
const parts = (_g = content.parts) != null ? _g : [];
|
784
|
+
for (const part of parts) {
|
785
|
+
if ("text" in part && part.text != null && part.text.length > 0) {
|
786
|
+
if (part.thought === true) {
|
787
|
+
if (currentTextBlockId !== null) {
|
788
|
+
controller.enqueue({
|
789
|
+
type: "text-end",
|
790
|
+
id: currentTextBlockId
|
791
|
+
});
|
792
|
+
currentTextBlockId = null;
|
793
|
+
}
|
794
|
+
if (currentReasoningBlockId === null) {
|
795
|
+
currentReasoningBlockId = String(blockCounter++);
|
796
|
+
controller.enqueue({
|
797
|
+
type: "reasoning-start",
|
798
|
+
id: currentReasoningBlockId
|
799
|
+
});
|
800
|
+
}
|
801
|
+
controller.enqueue({
|
802
|
+
type: "reasoning-delta",
|
803
|
+
id: currentReasoningBlockId,
|
804
|
+
delta: part.text
|
805
|
+
});
|
806
|
+
} else {
|
807
|
+
if (currentReasoningBlockId !== null) {
|
808
|
+
controller.enqueue({
|
809
|
+
type: "reasoning-end",
|
810
|
+
id: currentReasoningBlockId
|
811
|
+
});
|
812
|
+
currentReasoningBlockId = null;
|
813
|
+
}
|
814
|
+
if (currentTextBlockId === null) {
|
815
|
+
currentTextBlockId = String(blockCounter++);
|
816
|
+
controller.enqueue({
|
817
|
+
type: "text-start",
|
818
|
+
id: currentTextBlockId
|
819
|
+
});
|
820
|
+
}
|
821
|
+
controller.enqueue({
|
822
|
+
type: "text-delta",
|
823
|
+
id: currentTextBlockId,
|
824
|
+
delta: part.text
|
825
|
+
});
|
826
|
+
}
|
827
|
+
}
|
667
828
|
}
|
668
829
|
const inlineDataParts = getInlineDataParts(content.parts);
|
669
830
|
if (inlineDataParts != null) {
|
@@ -677,23 +838,29 @@ var GoogleGenerativeAILanguageModel = class {
|
|
677
838
|
}
|
678
839
|
const toolCallDeltas = getToolCallsFromParts({
|
679
840
|
parts: content.parts,
|
680
|
-
generateId
|
841
|
+
generateId: generateId2
|
681
842
|
});
|
682
843
|
if (toolCallDeltas != null) {
|
683
844
|
for (const toolCall of toolCallDeltas) {
|
684
845
|
controller.enqueue({
|
685
|
-
type: "tool-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
846
|
+
type: "tool-input-start",
|
847
|
+
id: toolCall.toolCallId,
|
848
|
+
toolName: toolCall.toolName
|
849
|
+
});
|
850
|
+
controller.enqueue({
|
851
|
+
type: "tool-input-delta",
|
852
|
+
id: toolCall.toolCallId,
|
853
|
+
delta: toolCall.args
|
854
|
+
});
|
855
|
+
controller.enqueue({
|
856
|
+
type: "tool-input-end",
|
857
|
+
id: toolCall.toolCallId
|
690
858
|
});
|
691
859
|
controller.enqueue({
|
692
860
|
type: "tool-call",
|
693
|
-
toolCallType: "function",
|
694
861
|
toolCallId: toolCall.toolCallId,
|
695
862
|
toolName: toolCall.toolName,
|
696
|
-
|
863
|
+
input: toolCall.args
|
697
864
|
});
|
698
865
|
hasToolCalls = true;
|
699
866
|
}
|
@@ -704,22 +871,31 @@ var GoogleGenerativeAILanguageModel = class {
|
|
704
871
|
finishReason: candidate.finishReason,
|
705
872
|
hasToolCalls
|
706
873
|
});
|
707
|
-
const sources = (_g = extractSources({
|
708
|
-
groundingMetadata: candidate.groundingMetadata,
|
709
|
-
generateId
|
710
|
-
})) != null ? _g : [];
|
711
|
-
for (const source of sources) {
|
712
|
-
controller.enqueue(source);
|
713
|
-
}
|
714
874
|
providerMetadata = {
|
715
875
|
google: {
|
716
876
|
groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
|
717
|
-
|
877
|
+
urlContextMetadata: (_i = candidate.urlContextMetadata) != null ? _i : null,
|
878
|
+
safetyRatings: (_j = candidate.safetyRatings) != null ? _j : null
|
718
879
|
}
|
719
880
|
};
|
881
|
+
if (usageMetadata != null) {
|
882
|
+
providerMetadata.google.usageMetadata = usageMetadata;
|
883
|
+
}
|
720
884
|
}
|
721
885
|
},
|
722
886
|
flush(controller) {
|
887
|
+
if (currentTextBlockId !== null) {
|
888
|
+
controller.enqueue({
|
889
|
+
type: "text-end",
|
890
|
+
id: currentTextBlockId
|
891
|
+
});
|
892
|
+
}
|
893
|
+
if (currentReasoningBlockId !== null) {
|
894
|
+
controller.enqueue({
|
895
|
+
type: "reasoning-end",
|
896
|
+
id: currentReasoningBlockId
|
897
|
+
});
|
898
|
+
}
|
723
899
|
controller.enqueue({
|
724
900
|
type: "finish",
|
725
901
|
finishReason,
|
@@ -736,26 +912,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
736
912
|
};
|
737
913
|
function getToolCallsFromParts({
|
738
914
|
parts,
|
739
|
-
generateId
|
915
|
+
generateId: generateId2
|
740
916
|
}) {
|
741
917
|
const functionCallParts = parts == null ? void 0 : parts.filter(
|
742
918
|
(part) => "functionCall" in part
|
743
919
|
);
|
744
920
|
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
745
921
|
type: "tool-call",
|
746
|
-
|
747
|
-
toolCallId: generateId(),
|
922
|
+
toolCallId: generateId2(),
|
748
923
|
toolName: part.functionCall.name,
|
749
924
|
args: JSON.stringify(part.functionCall.args)
|
750
925
|
}));
|
751
926
|
}
|
752
|
-
function getTextFromParts(parts) {
|
753
|
-
const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
|
754
|
-
return textParts == null || textParts.length === 0 ? void 0 : {
|
755
|
-
type: "text",
|
756
|
-
text: textParts.map((part) => part.text).join("")
|
757
|
-
};
|
758
|
-
}
|
759
927
|
function getInlineDataParts(parts) {
|
760
928
|
return parts == null ? void 0 : parts.filter(
|
761
929
|
(part) => "inlineData" in part
|
@@ -763,7 +931,7 @@ function getInlineDataParts(parts) {
|
|
763
931
|
}
|
764
932
|
function extractSources({
|
765
933
|
groundingMetadata,
|
766
|
-
generateId
|
934
|
+
generateId: generateId2
|
767
935
|
}) {
|
768
936
|
var _a;
|
769
937
|
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
@@ -771,103 +939,90 @@ function extractSources({
|
|
771
939
|
).map((chunk) => ({
|
772
940
|
type: "source",
|
773
941
|
sourceType: "url",
|
774
|
-
id:
|
942
|
+
id: generateId2(),
|
775
943
|
url: chunk.web.uri,
|
776
944
|
title: chunk.web.title
|
777
945
|
}));
|
778
946
|
}
|
779
|
-
var contentSchema =
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
784
|
-
|
785
|
-
|
786
|
-
|
787
|
-
functionCall: z3.object({
|
788
|
-
name: z3.string(),
|
789
|
-
args: z3.unknown()
|
947
|
+
var contentSchema = z5.object({
|
948
|
+
parts: z5.array(
|
949
|
+
z5.union([
|
950
|
+
// note: order matters since text can be fully empty
|
951
|
+
z5.object({
|
952
|
+
functionCall: z5.object({
|
953
|
+
name: z5.string(),
|
954
|
+
args: z5.unknown()
|
790
955
|
})
|
791
956
|
}),
|
792
|
-
|
793
|
-
inlineData:
|
794
|
-
mimeType:
|
795
|
-
data:
|
957
|
+
z5.object({
|
958
|
+
inlineData: z5.object({
|
959
|
+
mimeType: z5.string(),
|
960
|
+
data: z5.string()
|
796
961
|
})
|
962
|
+
}),
|
963
|
+
z5.object({
|
964
|
+
text: z5.string().nullish(),
|
965
|
+
thought: z5.boolean().nullish()
|
797
966
|
})
|
798
967
|
])
|
799
968
|
).nullish()
|
800
969
|
});
|
801
|
-
var
|
802
|
-
|
803
|
-
|
804
|
-
|
805
|
-
|
806
|
-
|
807
|
-
|
808
|
-
searchEntryPoint: z3.object({ renderedContent: z3.string() }).nullish(),
|
809
|
-
groundingChunks: z3.array(groundingChunkSchema).nullish(),
|
810
|
-
groundingSupports: z3.array(
|
811
|
-
z3.object({
|
812
|
-
segment: z3.object({
|
813
|
-
startIndex: z3.number().nullish(),
|
814
|
-
endIndex: z3.number().nullish(),
|
815
|
-
text: z3.string().nullish()
|
816
|
-
}),
|
817
|
-
segment_text: z3.string().nullish(),
|
818
|
-
groundingChunkIndices: z3.array(z3.number()).nullish(),
|
819
|
-
supportChunkIndices: z3.array(z3.number()).nullish(),
|
820
|
-
confidenceScores: z3.array(z3.number()).nullish(),
|
821
|
-
confidenceScore: z3.array(z3.number()).nullish()
|
822
|
-
})
|
823
|
-
).nullish(),
|
824
|
-
retrievalMetadata: z3.union([
|
825
|
-
z3.object({
|
826
|
-
webDynamicRetrievalScore: z3.number()
|
827
|
-
}),
|
828
|
-
z3.object({})
|
829
|
-
]).nullish()
|
830
|
-
});
|
831
|
-
var safetyRatingSchema = z3.object({
|
832
|
-
category: z3.string().nullish(),
|
833
|
-
probability: z3.string().nullish(),
|
834
|
-
probabilityScore: z3.number().nullish(),
|
835
|
-
severity: z3.string().nullish(),
|
836
|
-
severityScore: z3.number().nullish(),
|
837
|
-
blocked: z3.boolean().nullish()
|
970
|
+
var safetyRatingSchema = z5.object({
|
971
|
+
category: z5.string().nullish(),
|
972
|
+
probability: z5.string().nullish(),
|
973
|
+
probabilityScore: z5.number().nullish(),
|
974
|
+
severity: z5.string().nullish(),
|
975
|
+
severityScore: z5.number().nullish(),
|
976
|
+
blocked: z5.boolean().nullish()
|
838
977
|
});
|
839
|
-
var usageSchema =
|
840
|
-
cachedContentTokenCount:
|
841
|
-
thoughtsTokenCount:
|
842
|
-
promptTokenCount:
|
843
|
-
candidatesTokenCount:
|
844
|
-
totalTokenCount:
|
978
|
+
var usageSchema = z5.object({
|
979
|
+
cachedContentTokenCount: z5.number().nullish(),
|
980
|
+
thoughtsTokenCount: z5.number().nullish(),
|
981
|
+
promptTokenCount: z5.number().nullish(),
|
982
|
+
candidatesTokenCount: z5.number().nullish(),
|
983
|
+
totalTokenCount: z5.number().nullish()
|
845
984
|
});
|
846
|
-
var responseSchema =
|
847
|
-
candidates:
|
848
|
-
|
849
|
-
content: contentSchema.nullish().or(
|
850
|
-
finishReason:
|
851
|
-
safetyRatings:
|
852
|
-
groundingMetadata: groundingMetadataSchema.nullish()
|
985
|
+
var responseSchema = z5.object({
|
986
|
+
candidates: z5.array(
|
987
|
+
z5.object({
|
988
|
+
content: contentSchema.nullish().or(z5.object({}).strict()),
|
989
|
+
finishReason: z5.string().nullish(),
|
990
|
+
safetyRatings: z5.array(safetyRatingSchema).nullish(),
|
991
|
+
groundingMetadata: groundingMetadataSchema.nullish(),
|
992
|
+
urlContextMetadata: urlContextMetadataSchema.nullish()
|
853
993
|
})
|
854
994
|
),
|
855
995
|
usageMetadata: usageSchema.nullish()
|
856
996
|
});
|
857
|
-
var chunkSchema =
|
858
|
-
candidates:
|
859
|
-
|
997
|
+
var chunkSchema = z5.object({
|
998
|
+
candidates: z5.array(
|
999
|
+
z5.object({
|
860
1000
|
content: contentSchema.nullish(),
|
861
|
-
finishReason:
|
862
|
-
safetyRatings:
|
863
|
-
groundingMetadata: groundingMetadataSchema.nullish()
|
1001
|
+
finishReason: z5.string().nullish(),
|
1002
|
+
safetyRatings: z5.array(safetyRatingSchema).nullish(),
|
1003
|
+
groundingMetadata: groundingMetadataSchema.nullish(),
|
1004
|
+
urlContextMetadata: urlContextMetadataSchema.nullish()
|
864
1005
|
})
|
865
1006
|
).nullish(),
|
866
1007
|
usageMetadata: usageSchema.nullish()
|
867
1008
|
});
|
1009
|
+
|
1010
|
+
// src/google-tools.ts
|
1011
|
+
var googleTools = {
|
1012
|
+
/**
|
1013
|
+
* Creates a Google search tool that gives Google direct access to real-time web content.
|
1014
|
+
* Must have name "google_search".
|
1015
|
+
*/
|
1016
|
+
googleSearch,
|
1017
|
+
/**
|
1018
|
+
* Creates a URL context tool that gives Google direct access to real-time web content.
|
1019
|
+
* Must have name "url_context".
|
1020
|
+
*/
|
1021
|
+
urlContext
|
1022
|
+
};
|
868
1023
|
export {
|
869
1024
|
GoogleGenerativeAILanguageModel,
|
870
|
-
|
1025
|
+
googleTools,
|
871
1026
|
safetyRatingSchema
|
872
1027
|
};
|
873
1028
|
//# sourceMappingURL=index.mjs.map
|