@ai-sdk/google 2.0.0-canary.1 → 2.0.0-canary.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 +108 -0
- package/dist/index.d.mts +18 -12
- package/dist/index.d.ts +18 -12
- package/dist/index.js +236 -256
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +242 -259
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +19 -6
- package/{internal/dist → dist/internal}/index.d.ts +19 -6
- package/{internal/dist → dist/internal}/index.js +134 -174
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +137 -175
- package/dist/internal/index.mjs.map +1 -0
- package/package.json +15 -15
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
@@ -15,10 +15,11 @@ import {
|
|
15
15
|
import {
|
16
16
|
combineHeaders,
|
17
17
|
createJsonResponseHandler,
|
18
|
+
parseProviderOptions,
|
18
19
|
postJsonToApi,
|
19
20
|
resolve
|
20
21
|
} from "@ai-sdk/provider-utils";
|
21
|
-
import { z as
|
22
|
+
import { z as z3 } from "zod";
|
22
23
|
|
23
24
|
// src/google-error.ts
|
24
25
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
@@ -35,12 +36,21 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
35
36
|
errorToMessage: (data) => data.error.message
|
36
37
|
});
|
37
38
|
|
39
|
+
// src/google-generative-ai-embedding-options.ts
|
40
|
+
import { z as z2 } from "zod";
|
41
|
+
var googleGenerativeAIEmbeddingProviderOptions = z2.object({
|
42
|
+
/**
|
43
|
+
* Optional. Optional reduced dimension for the output embedding.
|
44
|
+
* If set, excessive values in the output embedding are truncated from the end.
|
45
|
+
*/
|
46
|
+
outputDimensionality: z2.number().optional()
|
47
|
+
});
|
48
|
+
|
38
49
|
// src/google-generative-ai-embedding-model.ts
|
39
50
|
var GoogleGenerativeAIEmbeddingModel = class {
|
40
|
-
constructor(modelId,
|
41
|
-
this.specificationVersion = "
|
51
|
+
constructor(modelId, config) {
|
52
|
+
this.specificationVersion = "v2";
|
42
53
|
this.modelId = modelId;
|
43
|
-
this.settings = settings;
|
44
54
|
this.config = config;
|
45
55
|
}
|
46
56
|
get provider() {
|
@@ -55,8 +65,15 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
55
65
|
async doEmbed({
|
56
66
|
values,
|
57
67
|
headers,
|
58
|
-
abortSignal
|
68
|
+
abortSignal,
|
69
|
+
providerOptions
|
59
70
|
}) {
|
71
|
+
var _a;
|
72
|
+
const googleOptions = (_a = parseProviderOptions({
|
73
|
+
provider: "google",
|
74
|
+
providerOptions,
|
75
|
+
schema: googleGenerativeAIEmbeddingProviderOptions
|
76
|
+
})) != null ? _a : {};
|
60
77
|
if (values.length > this.maxEmbeddingsPerCall) {
|
61
78
|
throw new TooManyEmbeddingValuesForCallError({
|
62
79
|
provider: this.provider,
|
@@ -69,14 +86,18 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
69
86
|
await resolve(this.config.headers),
|
70
87
|
headers
|
71
88
|
);
|
72
|
-
const {
|
89
|
+
const {
|
90
|
+
responseHeaders,
|
91
|
+
value: response,
|
92
|
+
rawValue
|
93
|
+
} = await postJsonToApi({
|
73
94
|
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
74
95
|
headers: mergedHeaders,
|
75
96
|
body: {
|
76
97
|
requests: values.map((value) => ({
|
77
98
|
model: `models/${this.modelId}`,
|
78
99
|
content: { role: "user", parts: [{ text: value }] },
|
79
|
-
outputDimensionality:
|
100
|
+
outputDimensionality: googleOptions.outputDimensionality
|
80
101
|
}))
|
81
102
|
},
|
82
103
|
failedResponseHandler: googleFailedResponseHandler,
|
@@ -89,12 +110,12 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
89
110
|
return {
|
90
111
|
embeddings: response.embeddings.map((item) => item.values),
|
91
112
|
usage: void 0,
|
92
|
-
|
113
|
+
response: { headers: responseHeaders, body: rawValue }
|
93
114
|
};
|
94
115
|
}
|
95
116
|
};
|
96
|
-
var googleGenerativeAITextEmbeddingResponseSchema =
|
97
|
-
embeddings:
|
117
|
+
var googleGenerativeAITextEmbeddingResponseSchema = z3.object({
|
118
|
+
embeddings: z3.array(z3.object({ values: z3.array(z3.number()) }))
|
98
119
|
});
|
99
120
|
|
100
121
|
// src/google-generative-ai-language-model.ts
|
@@ -102,15 +123,15 @@ import {
|
|
102
123
|
combineHeaders as combineHeaders2,
|
103
124
|
createEventSourceResponseHandler,
|
104
125
|
createJsonResponseHandler as createJsonResponseHandler2,
|
105
|
-
parseProviderOptions,
|
126
|
+
parseProviderOptions as parseProviderOptions2,
|
106
127
|
postJsonToApi as postJsonToApi2,
|
107
128
|
resolve as resolve2
|
108
129
|
} from "@ai-sdk/provider-utils";
|
109
|
-
import { z as
|
130
|
+
import { z as z4 } from "zod";
|
110
131
|
|
111
132
|
// src/convert-json-schema-to-openapi-schema.ts
|
112
133
|
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
113
|
-
if (isEmptyObjectSchema(jsonSchema)) {
|
134
|
+
if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
|
114
135
|
return void 0;
|
115
136
|
}
|
116
137
|
if (typeof jsonSchema === "boolean") {
|
@@ -209,9 +230,10 @@ function isEmptyObjectSchema(jsonSchema) {
|
|
209
230
|
import {
|
210
231
|
UnsupportedFunctionalityError
|
211
232
|
} from "@ai-sdk/provider";
|
212
|
-
import {
|
233
|
+
import {
|
234
|
+
convertToBase64
|
235
|
+
} from "@ai-sdk/provider-utils";
|
213
236
|
function convertToGoogleGenerativeAIMessages(prompt) {
|
214
|
-
var _a, _b;
|
215
237
|
const systemInstructionParts = [];
|
216
238
|
const contents = [];
|
217
239
|
let systemMessagesAllowed = true;
|
@@ -235,33 +257,18 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
235
257
|
parts.push({ text: part.text });
|
236
258
|
break;
|
237
259
|
}
|
238
|
-
case "image": {
|
239
|
-
parts.push(
|
240
|
-
part.image instanceof URL ? {
|
241
|
-
fileData: {
|
242
|
-
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
243
|
-
fileUri: part.image.toString()
|
244
|
-
}
|
245
|
-
} : {
|
246
|
-
inlineData: {
|
247
|
-
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
248
|
-
data: convertUint8ArrayToBase64(part.image)
|
249
|
-
}
|
250
|
-
}
|
251
|
-
);
|
252
|
-
break;
|
253
|
-
}
|
254
260
|
case "file": {
|
261
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
255
262
|
parts.push(
|
256
263
|
part.data instanceof URL ? {
|
257
264
|
fileData: {
|
258
|
-
mimeType:
|
265
|
+
mimeType: mediaType,
|
259
266
|
fileUri: part.data.toString()
|
260
267
|
}
|
261
268
|
} : {
|
262
269
|
inlineData: {
|
263
|
-
mimeType:
|
264
|
-
data: part.data
|
270
|
+
mimeType: mediaType,
|
271
|
+
data: convertToBase64(part.data)
|
265
272
|
}
|
266
273
|
}
|
267
274
|
);
|
@@ -282,7 +289,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
282
289
|
return part.text.length === 0 ? void 0 : { text: part.text };
|
283
290
|
}
|
284
291
|
case "file": {
|
285
|
-
if (part.
|
292
|
+
if (part.mediaType !== "image/png") {
|
286
293
|
throw new UnsupportedFunctionalityError({
|
287
294
|
functionality: "Only PNG images are supported in assistant messages"
|
288
295
|
});
|
@@ -294,8 +301,8 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
294
301
|
}
|
295
302
|
return {
|
296
303
|
inlineData: {
|
297
|
-
mimeType: part.
|
298
|
-
data: part.data
|
304
|
+
mimeType: part.mediaType,
|
305
|
+
data: convertToBase64(part.data)
|
299
306
|
}
|
300
307
|
};
|
301
308
|
}
|
@@ -345,9 +352,15 @@ function getModelPath(modelId) {
|
|
345
352
|
import {
|
346
353
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
347
354
|
} from "@ai-sdk/provider";
|
348
|
-
function prepareTools(
|
349
|
-
|
350
|
-
|
355
|
+
function prepareTools({
|
356
|
+
tools,
|
357
|
+
toolChoice,
|
358
|
+
useSearchGrounding,
|
359
|
+
dynamicRetrievalConfig,
|
360
|
+
modelId
|
361
|
+
}) {
|
362
|
+
var _a;
|
363
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
351
364
|
const toolWarnings = [];
|
352
365
|
const isGemini2 = modelId.includes("gemini-2");
|
353
366
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
@@ -370,12 +383,11 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
370
383
|
} else {
|
371
384
|
functionDeclarations.push({
|
372
385
|
name: tool.name,
|
373
|
-
description: (
|
386
|
+
description: (_a = tool.description) != null ? _a : "",
|
374
387
|
parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
|
375
388
|
});
|
376
389
|
}
|
377
390
|
}
|
378
|
-
const toolChoice = mode.toolChoice;
|
379
391
|
if (toolChoice == null) {
|
380
392
|
return {
|
381
393
|
tools: { functionDeclarations },
|
@@ -417,7 +429,7 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
417
429
|
default: {
|
418
430
|
const _exhaustiveCheck = type;
|
419
431
|
throw new UnsupportedFunctionalityError2({
|
420
|
-
functionality: `
|
432
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
421
433
|
});
|
422
434
|
}
|
423
435
|
}
|
@@ -454,23 +466,20 @@ function mapGoogleGenerativeAIFinishReason({
|
|
454
466
|
var GoogleGenerativeAILanguageModel = class {
|
455
467
|
constructor(modelId, settings, config) {
|
456
468
|
this.specificationVersion = "v2";
|
457
|
-
this.defaultObjectGenerationMode = "json";
|
458
|
-
this.supportsImageUrls = false;
|
459
469
|
this.modelId = modelId;
|
460
470
|
this.settings = settings;
|
461
471
|
this.config = config;
|
462
472
|
}
|
463
|
-
get supportsStructuredOutputs() {
|
464
|
-
var _a;
|
465
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : true;
|
466
|
-
}
|
467
473
|
get provider() {
|
468
474
|
return this.config.provider;
|
469
475
|
}
|
476
|
+
async getSupportedUrls() {
|
477
|
+
var _a, _b, _c;
|
478
|
+
return (_c = (_b = (_a = this.config).getSupportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
479
|
+
}
|
470
480
|
async getArgs({
|
471
|
-
mode,
|
472
481
|
prompt,
|
473
|
-
|
482
|
+
maxOutputTokens,
|
474
483
|
temperature,
|
475
484
|
topP,
|
476
485
|
topK,
|
@@ -479,111 +488,66 @@ var GoogleGenerativeAILanguageModel = class {
|
|
479
488
|
stopSequences,
|
480
489
|
responseFormat,
|
481
490
|
seed,
|
482
|
-
|
491
|
+
tools,
|
492
|
+
toolChoice,
|
493
|
+
providerOptions
|
483
494
|
}) {
|
484
495
|
var _a, _b;
|
485
|
-
const type = mode.type;
|
486
496
|
const warnings = [];
|
487
|
-
const googleOptions =
|
497
|
+
const googleOptions = parseProviderOptions2({
|
488
498
|
provider: "google",
|
489
|
-
providerOptions
|
499
|
+
providerOptions,
|
490
500
|
schema: googleGenerativeAIProviderOptionsSchema
|
491
501
|
});
|
492
|
-
const generationConfig = {
|
493
|
-
// standardized settings:
|
494
|
-
maxOutputTokens: maxTokens,
|
495
|
-
temperature,
|
496
|
-
topK,
|
497
|
-
topP,
|
498
|
-
frequencyPenalty,
|
499
|
-
presencePenalty,
|
500
|
-
stopSequences,
|
501
|
-
seed,
|
502
|
-
// response format:
|
503
|
-
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
504
|
-
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
505
|
-
// so this is needed as an escape hatch:
|
506
|
-
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
507
|
-
...this.settings.audioTimestamp && {
|
508
|
-
audioTimestamp: this.settings.audioTimestamp
|
509
|
-
},
|
510
|
-
// provider options:
|
511
|
-
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
|
512
|
-
};
|
513
502
|
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
contents,
|
546
|
-
systemInstruction,
|
547
|
-
safetySettings: this.settings.safetySettings,
|
548
|
-
cachedContent: this.settings.cachedContent
|
549
|
-
},
|
550
|
-
warnings
|
551
|
-
};
|
552
|
-
}
|
553
|
-
case "object-tool": {
|
554
|
-
return {
|
555
|
-
args: {
|
556
|
-
generationConfig,
|
557
|
-
contents,
|
558
|
-
tools: {
|
559
|
-
functionDeclarations: [
|
560
|
-
{
|
561
|
-
name: mode.tool.name,
|
562
|
-
description: (_b = mode.tool.description) != null ? _b : "",
|
563
|
-
parameters: convertJSONSchemaToOpenAPISchema(
|
564
|
-
mode.tool.parameters
|
565
|
-
)
|
566
|
-
}
|
567
|
-
]
|
568
|
-
},
|
569
|
-
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
570
|
-
safetySettings: this.settings.safetySettings,
|
571
|
-
cachedContent: this.settings.cachedContent
|
503
|
+
const {
|
504
|
+
tools: googleTools,
|
505
|
+
toolConfig: googleToolConfig,
|
506
|
+
toolWarnings
|
507
|
+
} = prepareTools({
|
508
|
+
tools,
|
509
|
+
toolChoice,
|
510
|
+
useSearchGrounding: (_a = this.settings.useSearchGrounding) != null ? _a : false,
|
511
|
+
dynamicRetrievalConfig: this.settings.dynamicRetrievalConfig,
|
512
|
+
modelId: this.modelId
|
513
|
+
});
|
514
|
+
return {
|
515
|
+
args: {
|
516
|
+
generationConfig: {
|
517
|
+
// standardized settings:
|
518
|
+
maxOutputTokens,
|
519
|
+
temperature,
|
520
|
+
topK,
|
521
|
+
topP,
|
522
|
+
frequencyPenalty,
|
523
|
+
presencePenalty,
|
524
|
+
stopSequences,
|
525
|
+
seed,
|
526
|
+
// response format:
|
527
|
+
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
528
|
+
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
529
|
+
// so this is needed as an escape hatch:
|
530
|
+
// TODO convert into provider option
|
531
|
+
((_b = this.settings.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
532
|
+
...this.settings.audioTimestamp && {
|
533
|
+
audioTimestamp: this.settings.audioTimestamp
|
572
534
|
},
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
535
|
+
// provider options:
|
536
|
+
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
|
537
|
+
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
538
|
+
},
|
539
|
+
contents,
|
540
|
+
systemInstruction,
|
541
|
+
safetySettings: this.settings.safetySettings,
|
542
|
+
tools: googleTools,
|
543
|
+
toolConfig: googleToolConfig,
|
544
|
+
cachedContent: this.settings.cachedContent
|
545
|
+
},
|
546
|
+
warnings: [...warnings, ...toolWarnings]
|
547
|
+
};
|
584
548
|
}
|
585
549
|
async doGenerate(options) {
|
586
|
-
var _a, _b, _c, _d, _e;
|
550
|
+
var _a, _b, _c, _d, _e, _f;
|
587
551
|
const { args, warnings } = await this.getArgs(options);
|
588
552
|
const body = JSON.stringify(args);
|
589
553
|
const mergedHeaders = combineHeaders2(
|
@@ -605,43 +569,59 @@ var GoogleGenerativeAILanguageModel = class {
|
|
605
569
|
abortSignal: options.abortSignal,
|
606
570
|
fetch: this.config.fetch
|
607
571
|
});
|
608
|
-
const { contents: rawPrompt, ...rawSettings } = args;
|
609
572
|
const candidate = response.candidates[0];
|
610
|
-
const
|
611
|
-
const
|
612
|
-
|
573
|
+
const content = [];
|
574
|
+
const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : (_a = candidate.content.parts) != null ? _a : [];
|
575
|
+
for (const part of parts) {
|
576
|
+
if ("text" in part && part.text.length > 0) {
|
577
|
+
content.push({ type: "text", text: part.text });
|
578
|
+
} else if ("functionCall" in part) {
|
579
|
+
content.push({
|
580
|
+
type: "tool-call",
|
581
|
+
toolCallType: "function",
|
582
|
+
toolCallId: this.config.generateId(),
|
583
|
+
toolName: part.functionCall.name,
|
584
|
+
args: JSON.stringify(part.functionCall.args)
|
585
|
+
});
|
586
|
+
} else if ("inlineData" in part) {
|
587
|
+
content.push({
|
588
|
+
type: "file",
|
589
|
+
data: part.inlineData.data,
|
590
|
+
mediaType: part.inlineData.mimeType
|
591
|
+
});
|
592
|
+
}
|
593
|
+
}
|
594
|
+
const sources = (_b = extractSources({
|
595
|
+
groundingMetadata: candidate.groundingMetadata,
|
613
596
|
generateId: this.config.generateId
|
614
|
-
});
|
597
|
+
})) != null ? _b : [];
|
598
|
+
for (const source of sources) {
|
599
|
+
content.push(source);
|
600
|
+
}
|
615
601
|
const usageMetadata = response.usageMetadata;
|
616
602
|
return {
|
617
|
-
|
618
|
-
files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
|
619
|
-
data: part.inlineData.data,
|
620
|
-
mimeType: part.inlineData.mimeType
|
621
|
-
})),
|
622
|
-
toolCalls,
|
603
|
+
content,
|
623
604
|
finishReason: mapGoogleGenerativeAIFinishReason({
|
624
605
|
finishReason: candidate.finishReason,
|
625
|
-
hasToolCalls:
|
606
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
626
607
|
}),
|
627
608
|
usage: {
|
628
|
-
|
629
|
-
|
609
|
+
inputTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _c : void 0,
|
610
|
+
outputTokens: (_d = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _d : void 0
|
630
611
|
},
|
631
|
-
rawCall: { rawPrompt, rawSettings },
|
632
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
633
612
|
warnings,
|
634
613
|
providerMetadata: {
|
635
614
|
google: {
|
636
|
-
groundingMetadata: (
|
637
|
-
safetyRatings: (
|
615
|
+
groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
|
616
|
+
safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
|
638
617
|
}
|
639
618
|
},
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
619
|
+
request: { body },
|
620
|
+
response: {
|
621
|
+
// TODO timestamp, model id, id
|
622
|
+
headers: responseHeaders,
|
623
|
+
body: rawResponse
|
624
|
+
}
|
645
625
|
};
|
646
626
|
}
|
647
627
|
async doStream(options) {
|
@@ -662,11 +642,10 @@ var GoogleGenerativeAILanguageModel = class {
|
|
662
642
|
abortSignal: options.abortSignal,
|
663
643
|
fetch: this.config.fetch
|
664
644
|
});
|
665
|
-
const { contents: rawPrompt, ...rawSettings } = args;
|
666
645
|
let finishReason = "unknown";
|
667
|
-
|
668
|
-
|
669
|
-
|
646
|
+
const usage = {
|
647
|
+
inputTokens: void 0,
|
648
|
+
outputTokens: void 0
|
670
649
|
};
|
671
650
|
let providerMetadata = void 0;
|
672
651
|
const generateId2 = this.config.generateId;
|
@@ -674,6 +653,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
674
653
|
return {
|
675
654
|
stream: response.pipeThrough(
|
676
655
|
new TransformStream({
|
656
|
+
start(controller) {
|
657
|
+
controller.enqueue({ type: "stream-start", warnings });
|
658
|
+
},
|
677
659
|
transform(chunk, controller) {
|
678
660
|
var _a, _b, _c, _d, _e, _f;
|
679
661
|
if (!chunk.success) {
|
@@ -683,10 +665,8 @@ var GoogleGenerativeAILanguageModel = class {
|
|
683
665
|
const value = chunk.value;
|
684
666
|
const usageMetadata = value.usageMetadata;
|
685
667
|
if (usageMetadata != null) {
|
686
|
-
usage =
|
687
|
-
|
688
|
-
completionTokens: (_b = usageMetadata.candidatesTokenCount) != null ? _b : NaN
|
689
|
-
};
|
668
|
+
usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
|
669
|
+
usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
|
690
670
|
}
|
691
671
|
const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
|
692
672
|
if (candidate == null) {
|
@@ -696,17 +676,14 @@ var GoogleGenerativeAILanguageModel = class {
|
|
696
676
|
if (content != null) {
|
697
677
|
const deltaText = getTextFromParts(content.parts);
|
698
678
|
if (deltaText != null) {
|
699
|
-
controller.enqueue(
|
700
|
-
type: "text-delta",
|
701
|
-
textDelta: deltaText
|
702
|
-
});
|
679
|
+
controller.enqueue(deltaText);
|
703
680
|
}
|
704
681
|
const inlineDataParts = getInlineDataParts(content.parts);
|
705
682
|
if (inlineDataParts != null) {
|
706
683
|
for (const part of inlineDataParts) {
|
707
684
|
controller.enqueue({
|
708
685
|
type: "file",
|
709
|
-
|
686
|
+
mediaType: part.inlineData.mimeType,
|
710
687
|
data: part.inlineData.data
|
711
688
|
});
|
712
689
|
}
|
@@ -745,7 +722,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
745
722
|
generateId: generateId2
|
746
723
|
})) != null ? _d : [];
|
747
724
|
for (const source of sources) {
|
748
|
-
controller.enqueue(
|
725
|
+
controller.enqueue(source);
|
749
726
|
}
|
750
727
|
providerMetadata = {
|
751
728
|
google: {
|
@@ -765,9 +742,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
765
742
|
}
|
766
743
|
})
|
767
744
|
),
|
768
|
-
|
769
|
-
rawResponse: { headers: responseHeaders },
|
770
|
-
warnings,
|
745
|
+
response: { headers: responseHeaders },
|
771
746
|
request: { body }
|
772
747
|
};
|
773
748
|
}
|
@@ -780,6 +755,7 @@ function getToolCallsFromParts({
|
|
780
755
|
(part) => "functionCall" in part
|
781
756
|
);
|
782
757
|
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
758
|
+
type: "tool-call",
|
783
759
|
toolCallType: "function",
|
784
760
|
toolCallId: generateId2(),
|
785
761
|
toolName: part.functionCall.name,
|
@@ -788,7 +764,10 @@ function getToolCallsFromParts({
|
|
788
764
|
}
|
789
765
|
function getTextFromParts(parts) {
|
790
766
|
const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
|
791
|
-
return textParts == null || textParts.length === 0 ? void 0 :
|
767
|
+
return textParts == null || textParts.length === 0 ? void 0 : {
|
768
|
+
type: "text",
|
769
|
+
text: textParts.map((part) => part.text).join("")
|
770
|
+
};
|
792
771
|
}
|
793
772
|
function getInlineDataParts(parts) {
|
794
773
|
return parts == null ? void 0 : parts.filter(
|
@@ -803,111 +782,110 @@ function extractSources({
|
|
803
782
|
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
804
783
|
(chunk) => chunk.web != null
|
805
784
|
).map((chunk) => ({
|
785
|
+
type: "source",
|
806
786
|
sourceType: "url",
|
807
787
|
id: generateId2(),
|
808
788
|
url: chunk.web.uri,
|
809
789
|
title: chunk.web.title
|
810
790
|
}));
|
811
791
|
}
|
812
|
-
var contentSchema =
|
813
|
-
role:
|
814
|
-
parts:
|
815
|
-
|
816
|
-
|
817
|
-
text:
|
792
|
+
var contentSchema = z4.object({
|
793
|
+
role: z4.string(),
|
794
|
+
parts: z4.array(
|
795
|
+
z4.union([
|
796
|
+
z4.object({
|
797
|
+
text: z4.string()
|
818
798
|
}),
|
819
|
-
|
820
|
-
functionCall:
|
821
|
-
name:
|
822
|
-
args:
|
799
|
+
z4.object({
|
800
|
+
functionCall: z4.object({
|
801
|
+
name: z4.string(),
|
802
|
+
args: z4.unknown()
|
823
803
|
})
|
824
804
|
}),
|
825
|
-
|
826
|
-
inlineData:
|
827
|
-
mimeType:
|
828
|
-
data:
|
805
|
+
z4.object({
|
806
|
+
inlineData: z4.object({
|
807
|
+
mimeType: z4.string(),
|
808
|
+
data: z4.string()
|
829
809
|
})
|
830
810
|
})
|
831
811
|
])
|
832
812
|
).nullish()
|
833
813
|
});
|
834
|
-
var groundingChunkSchema =
|
835
|
-
web:
|
836
|
-
retrievedContext:
|
814
|
+
var groundingChunkSchema = z4.object({
|
815
|
+
web: z4.object({ uri: z4.string(), title: z4.string() }).nullish(),
|
816
|
+
retrievedContext: z4.object({ uri: z4.string(), title: z4.string() }).nullish()
|
837
817
|
});
|
838
|
-
var groundingMetadataSchema =
|
839
|
-
webSearchQueries:
|
840
|
-
retrievalQueries:
|
841
|
-
searchEntryPoint:
|
842
|
-
groundingChunks:
|
843
|
-
groundingSupports:
|
844
|
-
|
845
|
-
segment:
|
846
|
-
startIndex:
|
847
|
-
endIndex:
|
848
|
-
text:
|
818
|
+
var groundingMetadataSchema = z4.object({
|
819
|
+
webSearchQueries: z4.array(z4.string()).nullish(),
|
820
|
+
retrievalQueries: z4.array(z4.string()).nullish(),
|
821
|
+
searchEntryPoint: z4.object({ renderedContent: z4.string() }).nullish(),
|
822
|
+
groundingChunks: z4.array(groundingChunkSchema).nullish(),
|
823
|
+
groundingSupports: z4.array(
|
824
|
+
z4.object({
|
825
|
+
segment: z4.object({
|
826
|
+
startIndex: z4.number().nullish(),
|
827
|
+
endIndex: z4.number().nullish(),
|
828
|
+
text: z4.string().nullish()
|
849
829
|
}),
|
850
|
-
segment_text:
|
851
|
-
groundingChunkIndices:
|
852
|
-
supportChunkIndices:
|
853
|
-
confidenceScores:
|
854
|
-
confidenceScore:
|
830
|
+
segment_text: z4.string().nullish(),
|
831
|
+
groundingChunkIndices: z4.array(z4.number()).nullish(),
|
832
|
+
supportChunkIndices: z4.array(z4.number()).nullish(),
|
833
|
+
confidenceScores: z4.array(z4.number()).nullish(),
|
834
|
+
confidenceScore: z4.array(z4.number()).nullish()
|
855
835
|
})
|
856
836
|
).nullish(),
|
857
|
-
retrievalMetadata:
|
858
|
-
|
859
|
-
webDynamicRetrievalScore:
|
837
|
+
retrievalMetadata: z4.union([
|
838
|
+
z4.object({
|
839
|
+
webDynamicRetrievalScore: z4.number()
|
860
840
|
}),
|
861
|
-
|
841
|
+
z4.object({})
|
862
842
|
]).nullish()
|
863
843
|
});
|
864
|
-
var safetyRatingSchema =
|
865
|
-
category:
|
866
|
-
probability:
|
867
|
-
probabilityScore:
|
868
|
-
severity:
|
869
|
-
severityScore:
|
870
|
-
blocked:
|
844
|
+
var safetyRatingSchema = z4.object({
|
845
|
+
category: z4.string(),
|
846
|
+
probability: z4.string(),
|
847
|
+
probabilityScore: z4.number().nullish(),
|
848
|
+
severity: z4.string().nullish(),
|
849
|
+
severityScore: z4.number().nullish(),
|
850
|
+
blocked: z4.boolean().nullish()
|
871
851
|
});
|
872
|
-
var responseSchema =
|
873
|
-
candidates:
|
874
|
-
|
875
|
-
content: contentSchema.nullish().or(
|
876
|
-
finishReason:
|
877
|
-
safetyRatings:
|
852
|
+
var responseSchema = z4.object({
|
853
|
+
candidates: z4.array(
|
854
|
+
z4.object({
|
855
|
+
content: contentSchema.nullish().or(z4.object({}).strict()),
|
856
|
+
finishReason: z4.string().nullish(),
|
857
|
+
safetyRatings: z4.array(safetyRatingSchema).nullish(),
|
878
858
|
groundingMetadata: groundingMetadataSchema.nullish()
|
879
859
|
})
|
880
860
|
),
|
881
|
-
usageMetadata:
|
882
|
-
promptTokenCount:
|
883
|
-
candidatesTokenCount:
|
884
|
-
totalTokenCount:
|
861
|
+
usageMetadata: z4.object({
|
862
|
+
promptTokenCount: z4.number().nullish(),
|
863
|
+
candidatesTokenCount: z4.number().nullish(),
|
864
|
+
totalTokenCount: z4.number().nullish()
|
885
865
|
}).nullish()
|
886
866
|
});
|
887
|
-
var chunkSchema =
|
888
|
-
candidates:
|
889
|
-
|
867
|
+
var chunkSchema = z4.object({
|
868
|
+
candidates: z4.array(
|
869
|
+
z4.object({
|
890
870
|
content: contentSchema.nullish(),
|
891
|
-
finishReason:
|
892
|
-
safetyRatings:
|
871
|
+
finishReason: z4.string().nullish(),
|
872
|
+
safetyRatings: z4.array(safetyRatingSchema).nullish(),
|
893
873
|
groundingMetadata: groundingMetadataSchema.nullish()
|
894
874
|
})
|
895
875
|
).nullish(),
|
896
|
-
usageMetadata:
|
897
|
-
promptTokenCount:
|
898
|
-
candidatesTokenCount:
|
899
|
-
totalTokenCount:
|
876
|
+
usageMetadata: z4.object({
|
877
|
+
promptTokenCount: z4.number().nullish(),
|
878
|
+
candidatesTokenCount: z4.number().nullish(),
|
879
|
+
totalTokenCount: z4.number().nullish()
|
900
880
|
}).nullish()
|
901
881
|
});
|
902
|
-
var googleGenerativeAIProviderOptionsSchema =
|
903
|
-
responseModalities:
|
882
|
+
var googleGenerativeAIProviderOptionsSchema = z4.object({
|
883
|
+
responseModalities: z4.array(z4.enum(["TEXT", "IMAGE"])).nullish(),
|
884
|
+
thinkingConfig: z4.object({
|
885
|
+
thinkingBudget: z4.number().nullish()
|
886
|
+
}).nullish()
|
904
887
|
});
|
905
888
|
|
906
|
-
// src/google-supported-file-url.ts
|
907
|
-
function isSupportedFileUrl(url) {
|
908
|
-
return url.toString().startsWith("https://generativelanguage.googleapis.com/v1beta/files/");
|
909
|
-
}
|
910
|
-
|
911
889
|
// src/google-provider.ts
|
912
890
|
function createGoogleGenerativeAI(options = {}) {
|
913
891
|
var _a;
|
@@ -927,11 +905,16 @@ function createGoogleGenerativeAI(options = {}) {
|
|
927
905
|
baseURL,
|
928
906
|
headers: getHeaders,
|
929
907
|
generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
|
930
|
-
|
908
|
+
getSupportedUrls: async () => ({
|
909
|
+
"*": [
|
910
|
+
// HTTP URLs:
|
911
|
+
/^https?:\/\/.*$/
|
912
|
+
]
|
913
|
+
}),
|
931
914
|
fetch: options.fetch
|
932
915
|
});
|
933
916
|
};
|
934
|
-
const createEmbeddingModel = (modelId
|
917
|
+
const createEmbeddingModel = (modelId) => new GoogleGenerativeAIEmbeddingModel(modelId, {
|
935
918
|
provider: "google.generative-ai",
|
936
919
|
baseURL,
|
937
920
|
headers: getHeaders,
|