@ai-sdk/google 2.0.0-canary.0 → 2.0.0-canary.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 +110 -0
- package/dist/index.d.mts +108 -94
- package/dist/index.d.ts +108 -94
- package/dist/index.js +331 -344
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +336 -344
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +33 -12
- package/{internal/dist → dist/internal}/index.d.ts +33 -12
- package/{internal/dist → dist/internal}/index.js +138 -177
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +141 -178
- 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.js
CHANGED
@@ -26,15 +26,118 @@ __export(src_exports, {
|
|
26
26
|
module.exports = __toCommonJS(src_exports);
|
27
27
|
|
28
28
|
// src/google-provider.ts
|
29
|
+
var import_provider4 = require("@ai-sdk/provider");
|
29
30
|
var import_provider_utils5 = require("@ai-sdk/provider-utils");
|
30
31
|
|
31
|
-
// src/google-generative-ai-
|
32
|
-
var
|
32
|
+
// src/google-generative-ai-embedding-model.ts
|
33
|
+
var import_provider = require("@ai-sdk/provider");
|
34
|
+
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
35
|
+
var import_zod3 = require("zod");
|
36
|
+
|
37
|
+
// src/google-error.ts
|
38
|
+
var import_provider_utils = require("@ai-sdk/provider-utils");
|
39
|
+
var import_zod = require("zod");
|
40
|
+
var googleErrorDataSchema = import_zod.z.object({
|
41
|
+
error: import_zod.z.object({
|
42
|
+
code: import_zod.z.number().nullable(),
|
43
|
+
message: import_zod.z.string(),
|
44
|
+
status: import_zod.z.string()
|
45
|
+
})
|
46
|
+
});
|
47
|
+
var googleFailedResponseHandler = (0, import_provider_utils.createJsonErrorResponseHandler)({
|
48
|
+
errorSchema: googleErrorDataSchema,
|
49
|
+
errorToMessage: (data) => data.error.message
|
50
|
+
});
|
51
|
+
|
52
|
+
// src/google-generative-ai-embedding-options.ts
|
33
53
|
var import_zod2 = require("zod");
|
54
|
+
var googleGenerativeAIEmbeddingProviderOptions = import_zod2.z.object({
|
55
|
+
/**
|
56
|
+
* Optional. Optional reduced dimension for the output embedding.
|
57
|
+
* If set, excessive values in the output embedding are truncated from the end.
|
58
|
+
*/
|
59
|
+
outputDimensionality: import_zod2.z.number().optional()
|
60
|
+
});
|
61
|
+
|
62
|
+
// src/google-generative-ai-embedding-model.ts
|
63
|
+
var GoogleGenerativeAIEmbeddingModel = class {
|
64
|
+
constructor(modelId, config) {
|
65
|
+
this.specificationVersion = "v2";
|
66
|
+
this.modelId = modelId;
|
67
|
+
this.config = config;
|
68
|
+
}
|
69
|
+
get provider() {
|
70
|
+
return this.config.provider;
|
71
|
+
}
|
72
|
+
get maxEmbeddingsPerCall() {
|
73
|
+
return 2048;
|
74
|
+
}
|
75
|
+
get supportsParallelCalls() {
|
76
|
+
return true;
|
77
|
+
}
|
78
|
+
async doEmbed({
|
79
|
+
values,
|
80
|
+
headers,
|
81
|
+
abortSignal,
|
82
|
+
providerOptions
|
83
|
+
}) {
|
84
|
+
var _a;
|
85
|
+
const googleOptions = (_a = (0, import_provider_utils2.parseProviderOptions)({
|
86
|
+
provider: "google",
|
87
|
+
providerOptions,
|
88
|
+
schema: googleGenerativeAIEmbeddingProviderOptions
|
89
|
+
})) != null ? _a : {};
|
90
|
+
if (values.length > this.maxEmbeddingsPerCall) {
|
91
|
+
throw new import_provider.TooManyEmbeddingValuesForCallError({
|
92
|
+
provider: this.provider,
|
93
|
+
modelId: this.modelId,
|
94
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
95
|
+
values
|
96
|
+
});
|
97
|
+
}
|
98
|
+
const mergedHeaders = (0, import_provider_utils2.combineHeaders)(
|
99
|
+
await (0, import_provider_utils2.resolve)(this.config.headers),
|
100
|
+
headers
|
101
|
+
);
|
102
|
+
const {
|
103
|
+
responseHeaders,
|
104
|
+
value: response,
|
105
|
+
rawValue
|
106
|
+
} = await (0, import_provider_utils2.postJsonToApi)({
|
107
|
+
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
108
|
+
headers: mergedHeaders,
|
109
|
+
body: {
|
110
|
+
requests: values.map((value) => ({
|
111
|
+
model: `models/${this.modelId}`,
|
112
|
+
content: { role: "user", parts: [{ text: value }] },
|
113
|
+
outputDimensionality: googleOptions.outputDimensionality
|
114
|
+
}))
|
115
|
+
},
|
116
|
+
failedResponseHandler: googleFailedResponseHandler,
|
117
|
+
successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
|
118
|
+
googleGenerativeAITextEmbeddingResponseSchema
|
119
|
+
),
|
120
|
+
abortSignal,
|
121
|
+
fetch: this.config.fetch
|
122
|
+
});
|
123
|
+
return {
|
124
|
+
embeddings: response.embeddings.map((item) => item.values),
|
125
|
+
usage: void 0,
|
126
|
+
response: { headers: responseHeaders, body: rawValue }
|
127
|
+
};
|
128
|
+
}
|
129
|
+
};
|
130
|
+
var googleGenerativeAITextEmbeddingResponseSchema = import_zod3.z.object({
|
131
|
+
embeddings: import_zod3.z.array(import_zod3.z.object({ values: import_zod3.z.array(import_zod3.z.number()) }))
|
132
|
+
});
|
133
|
+
|
134
|
+
// src/google-generative-ai-language-model.ts
|
135
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
136
|
+
var import_zod4 = require("zod");
|
34
137
|
|
35
138
|
// src/convert-json-schema-to-openapi-schema.ts
|
36
139
|
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
37
|
-
if (isEmptyObjectSchema(jsonSchema)) {
|
140
|
+
if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
|
38
141
|
return void 0;
|
39
142
|
}
|
40
143
|
if (typeof jsonSchema === "boolean") {
|
@@ -130,10 +233,9 @@ function isEmptyObjectSchema(jsonSchema) {
|
|
130
233
|
}
|
131
234
|
|
132
235
|
// src/convert-to-google-generative-ai-messages.ts
|
133
|
-
var
|
134
|
-
var
|
236
|
+
var import_provider2 = require("@ai-sdk/provider");
|
237
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
135
238
|
function convertToGoogleGenerativeAIMessages(prompt) {
|
136
|
-
var _a, _b;
|
137
239
|
const systemInstructionParts = [];
|
138
240
|
const contents = [];
|
139
241
|
let systemMessagesAllowed = true;
|
@@ -141,7 +243,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
141
243
|
switch (role) {
|
142
244
|
case "system": {
|
143
245
|
if (!systemMessagesAllowed) {
|
144
|
-
throw new
|
246
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
145
247
|
functionality: "system messages are only supported at the beginning of the conversation"
|
146
248
|
});
|
147
249
|
}
|
@@ -157,33 +259,18 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
157
259
|
parts.push({ text: part.text });
|
158
260
|
break;
|
159
261
|
}
|
160
|
-
case "image": {
|
161
|
-
parts.push(
|
162
|
-
part.image instanceof URL ? {
|
163
|
-
fileData: {
|
164
|
-
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
165
|
-
fileUri: part.image.toString()
|
166
|
-
}
|
167
|
-
} : {
|
168
|
-
inlineData: {
|
169
|
-
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
170
|
-
data: (0, import_provider_utils.convertUint8ArrayToBase64)(part.image)
|
171
|
-
}
|
172
|
-
}
|
173
|
-
);
|
174
|
-
break;
|
175
|
-
}
|
176
262
|
case "file": {
|
263
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
177
264
|
parts.push(
|
178
265
|
part.data instanceof URL ? {
|
179
266
|
fileData: {
|
180
|
-
mimeType:
|
267
|
+
mimeType: mediaType,
|
181
268
|
fileUri: part.data.toString()
|
182
269
|
}
|
183
270
|
} : {
|
184
271
|
inlineData: {
|
185
|
-
mimeType:
|
186
|
-
data: part.data
|
272
|
+
mimeType: mediaType,
|
273
|
+
data: (0, import_provider_utils3.convertToBase64)(part.data)
|
187
274
|
}
|
188
275
|
}
|
189
276
|
);
|
@@ -204,20 +291,20 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
204
291
|
return part.text.length === 0 ? void 0 : { text: part.text };
|
205
292
|
}
|
206
293
|
case "file": {
|
207
|
-
if (part.
|
208
|
-
throw new
|
294
|
+
if (part.mediaType !== "image/png") {
|
295
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
209
296
|
functionality: "Only PNG images are supported in assistant messages"
|
210
297
|
});
|
211
298
|
}
|
212
299
|
if (part.data instanceof URL) {
|
213
|
-
throw new
|
300
|
+
throw new import_provider2.UnsupportedFunctionalityError({
|
214
301
|
functionality: "File data URLs in assistant messages are not supported"
|
215
302
|
});
|
216
303
|
}
|
217
304
|
return {
|
218
305
|
inlineData: {
|
219
|
-
mimeType: part.
|
220
|
-
data: part.data
|
306
|
+
mimeType: part.mediaType,
|
307
|
+
data: (0, import_provider_utils3.convertToBase64)(part.data)
|
221
308
|
}
|
222
309
|
};
|
223
310
|
}
|
@@ -263,26 +350,17 @@ function getModelPath(modelId) {
|
|
263
350
|
return modelId.includes("/") ? modelId : `models/${modelId}`;
|
264
351
|
}
|
265
352
|
|
266
|
-
// src/google-error.ts
|
267
|
-
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
268
|
-
var import_zod = require("zod");
|
269
|
-
var googleErrorDataSchema = import_zod.z.object({
|
270
|
-
error: import_zod.z.object({
|
271
|
-
code: import_zod.z.number().nullable(),
|
272
|
-
message: import_zod.z.string(),
|
273
|
-
status: import_zod.z.string()
|
274
|
-
})
|
275
|
-
});
|
276
|
-
var googleFailedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
|
277
|
-
errorSchema: googleErrorDataSchema,
|
278
|
-
errorToMessage: (data) => data.error.message
|
279
|
-
});
|
280
|
-
|
281
353
|
// src/google-prepare-tools.ts
|
282
|
-
var
|
283
|
-
function prepareTools(
|
284
|
-
|
285
|
-
|
354
|
+
var import_provider3 = require("@ai-sdk/provider");
|
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;
|
286
364
|
const toolWarnings = [];
|
287
365
|
const isGemini2 = modelId.includes("gemini-2");
|
288
366
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
@@ -305,12 +383,11 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
305
383
|
} else {
|
306
384
|
functionDeclarations.push({
|
307
385
|
name: tool.name,
|
308
|
-
description: (
|
386
|
+
description: (_a = tool.description) != null ? _a : "",
|
309
387
|
parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
|
310
388
|
});
|
311
389
|
}
|
312
390
|
}
|
313
|
-
const toolChoice = mode.toolChoice;
|
314
391
|
if (toolChoice == null) {
|
315
392
|
return {
|
316
393
|
tools: { functionDeclarations },
|
@@ -351,8 +428,8 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
351
428
|
};
|
352
429
|
default: {
|
353
430
|
const _exhaustiveCheck = type;
|
354
|
-
throw new
|
355
|
-
functionality: `
|
431
|
+
throw new import_provider3.UnsupportedFunctionalityError({
|
432
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
356
433
|
});
|
357
434
|
}
|
358
435
|
}
|
@@ -388,24 +465,21 @@ function mapGoogleGenerativeAIFinishReason({
|
|
388
465
|
// src/google-generative-ai-language-model.ts
|
389
466
|
var GoogleGenerativeAILanguageModel = class {
|
390
467
|
constructor(modelId, settings, config) {
|
391
|
-
this.specificationVersion = "
|
392
|
-
this.defaultObjectGenerationMode = "json";
|
393
|
-
this.supportsImageUrls = false;
|
468
|
+
this.specificationVersion = "v2";
|
394
469
|
this.modelId = modelId;
|
395
470
|
this.settings = settings;
|
396
471
|
this.config = config;
|
397
472
|
}
|
398
|
-
get supportsStructuredOutputs() {
|
399
|
-
var _a;
|
400
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : true;
|
401
|
-
}
|
402
473
|
get provider() {
|
403
474
|
return this.config.provider;
|
404
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
|
+
}
|
405
480
|
async getArgs({
|
406
|
-
mode,
|
407
481
|
prompt,
|
408
|
-
|
482
|
+
maxOutputTokens,
|
409
483
|
temperature,
|
410
484
|
topP,
|
411
485
|
topK,
|
@@ -414,196 +488,164 @@ var GoogleGenerativeAILanguageModel = class {
|
|
414
488
|
stopSequences,
|
415
489
|
responseFormat,
|
416
490
|
seed,
|
417
|
-
|
491
|
+
tools,
|
492
|
+
toolChoice,
|
493
|
+
providerOptions
|
418
494
|
}) {
|
419
495
|
var _a, _b;
|
420
|
-
const type = mode.type;
|
421
496
|
const warnings = [];
|
422
|
-
const googleOptions = (0,
|
497
|
+
const googleOptions = (0, import_provider_utils4.parseProviderOptions)({
|
423
498
|
provider: "google",
|
424
|
-
providerOptions
|
425
|
-
schema:
|
426
|
-
responseModalities: import_zod2.z.array(import_zod2.z.enum(["TEXT", "IMAGE"])).nullish()
|
427
|
-
})
|
499
|
+
providerOptions,
|
500
|
+
schema: googleGenerativeAIProviderOptionsSchema
|
428
501
|
});
|
429
|
-
const generationConfig = {
|
430
|
-
// standardized settings:
|
431
|
-
maxOutputTokens: maxTokens,
|
432
|
-
temperature,
|
433
|
-
topK,
|
434
|
-
topP,
|
435
|
-
frequencyPenalty,
|
436
|
-
presencePenalty,
|
437
|
-
stopSequences,
|
438
|
-
seed,
|
439
|
-
// response format:
|
440
|
-
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
441
|
-
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
442
|
-
// so this is needed as an escape hatch:
|
443
|
-
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
444
|
-
...this.settings.audioTimestamp && {
|
445
|
-
audioTimestamp: this.settings.audioTimestamp
|
446
|
-
},
|
447
|
-
// provider options:
|
448
|
-
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
|
449
|
-
};
|
450
502
|
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
|
474
|
-
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
contents,
|
483
|
-
systemInstruction,
|
484
|
-
safetySettings: this.settings.safetySettings,
|
485
|
-
cachedContent: this.settings.cachedContent
|
486
|
-
},
|
487
|
-
warnings
|
488
|
-
};
|
489
|
-
}
|
490
|
-
case "object-tool": {
|
491
|
-
return {
|
492
|
-
args: {
|
493
|
-
generationConfig,
|
494
|
-
contents,
|
495
|
-
tools: {
|
496
|
-
functionDeclarations: [
|
497
|
-
{
|
498
|
-
name: mode.tool.name,
|
499
|
-
description: (_b = mode.tool.description) != null ? _b : "",
|
500
|
-
parameters: convertJSONSchemaToOpenAPISchema(
|
501
|
-
mode.tool.parameters
|
502
|
-
)
|
503
|
-
}
|
504
|
-
]
|
505
|
-
},
|
506
|
-
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
507
|
-
safetySettings: this.settings.safetySettings,
|
508
|
-
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
|
509
534
|
},
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
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
|
+
};
|
521
548
|
}
|
522
549
|
async doGenerate(options) {
|
523
|
-
var _a, _b, _c, _d, _e;
|
550
|
+
var _a, _b, _c, _d, _e, _f;
|
524
551
|
const { args, warnings } = await this.getArgs(options);
|
525
552
|
const body = JSON.stringify(args);
|
526
|
-
const mergedHeaders = (0,
|
527
|
-
await (0,
|
553
|
+
const mergedHeaders = (0, import_provider_utils4.combineHeaders)(
|
554
|
+
await (0, import_provider_utils4.resolve)(this.config.headers),
|
528
555
|
options.headers
|
529
556
|
);
|
530
557
|
const {
|
531
558
|
responseHeaders,
|
532
559
|
value: response,
|
533
560
|
rawValue: rawResponse
|
534
|
-
} = await (0,
|
561
|
+
} = await (0, import_provider_utils4.postJsonToApi)({
|
535
562
|
url: `${this.config.baseURL}/${getModelPath(
|
536
563
|
this.modelId
|
537
564
|
)}:generateContent`,
|
538
565
|
headers: mergedHeaders,
|
539
566
|
body: args,
|
540
567
|
failedResponseHandler: googleFailedResponseHandler,
|
541
|
-
successfulResponseHandler: (0,
|
568
|
+
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(responseSchema),
|
542
569
|
abortSignal: options.abortSignal,
|
543
570
|
fetch: this.config.fetch
|
544
571
|
});
|
545
|
-
const { contents: rawPrompt, ...rawSettings } = args;
|
546
572
|
const candidate = response.candidates[0];
|
547
|
-
const
|
548
|
-
const
|
549
|
-
|
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,
|
550
596
|
generateId: this.config.generateId
|
551
|
-
});
|
597
|
+
})) != null ? _b : [];
|
598
|
+
for (const source of sources) {
|
599
|
+
content.push(source);
|
600
|
+
}
|
552
601
|
const usageMetadata = response.usageMetadata;
|
553
602
|
return {
|
554
|
-
|
555
|
-
files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
|
556
|
-
data: part.inlineData.data,
|
557
|
-
mimeType: part.inlineData.mimeType
|
558
|
-
})),
|
559
|
-
toolCalls,
|
603
|
+
content,
|
560
604
|
finishReason: mapGoogleGenerativeAIFinishReason({
|
561
605
|
finishReason: candidate.finishReason,
|
562
|
-
hasToolCalls:
|
606
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
563
607
|
}),
|
564
608
|
usage: {
|
565
|
-
|
566
|
-
|
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
|
567
611
|
},
|
568
|
-
rawCall: { rawPrompt, rawSettings },
|
569
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
570
612
|
warnings,
|
571
613
|
providerMetadata: {
|
572
614
|
google: {
|
573
|
-
groundingMetadata: (
|
574
|
-
safetyRatings: (
|
615
|
+
groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
|
616
|
+
safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
|
575
617
|
}
|
576
618
|
},
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
619
|
+
request: { body },
|
620
|
+
response: {
|
621
|
+
// TODO timestamp, model id, id
|
622
|
+
headers: responseHeaders,
|
623
|
+
body: rawResponse
|
624
|
+
}
|
582
625
|
};
|
583
626
|
}
|
584
627
|
async doStream(options) {
|
585
628
|
const { args, warnings } = await this.getArgs(options);
|
586
629
|
const body = JSON.stringify(args);
|
587
|
-
const headers = (0,
|
588
|
-
await (0,
|
630
|
+
const headers = (0, import_provider_utils4.combineHeaders)(
|
631
|
+
await (0, import_provider_utils4.resolve)(this.config.headers),
|
589
632
|
options.headers
|
590
633
|
);
|
591
|
-
const { responseHeaders, value: response } = await (0,
|
634
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
592
635
|
url: `${this.config.baseURL}/${getModelPath(
|
593
636
|
this.modelId
|
594
637
|
)}:streamGenerateContent?alt=sse`,
|
595
638
|
headers,
|
596
639
|
body: args,
|
597
640
|
failedResponseHandler: googleFailedResponseHandler,
|
598
|
-
successfulResponseHandler: (0,
|
641
|
+
successfulResponseHandler: (0, import_provider_utils4.createEventSourceResponseHandler)(chunkSchema),
|
599
642
|
abortSignal: options.abortSignal,
|
600
643
|
fetch: this.config.fetch
|
601
644
|
});
|
602
|
-
const { contents: rawPrompt, ...rawSettings } = args;
|
603
645
|
let finishReason = "unknown";
|
604
|
-
|
605
|
-
|
606
|
-
|
646
|
+
const usage = {
|
647
|
+
inputTokens: void 0,
|
648
|
+
outputTokens: void 0
|
607
649
|
};
|
608
650
|
let providerMetadata = void 0;
|
609
651
|
const generateId2 = this.config.generateId;
|
@@ -611,6 +653,9 @@ var GoogleGenerativeAILanguageModel = class {
|
|
611
653
|
return {
|
612
654
|
stream: response.pipeThrough(
|
613
655
|
new TransformStream({
|
656
|
+
start(controller) {
|
657
|
+
controller.enqueue({ type: "stream-start", warnings });
|
658
|
+
},
|
614
659
|
transform(chunk, controller) {
|
615
660
|
var _a, _b, _c, _d, _e, _f;
|
616
661
|
if (!chunk.success) {
|
@@ -620,10 +665,8 @@ var GoogleGenerativeAILanguageModel = class {
|
|
620
665
|
const value = chunk.value;
|
621
666
|
const usageMetadata = value.usageMetadata;
|
622
667
|
if (usageMetadata != null) {
|
623
|
-
usage =
|
624
|
-
|
625
|
-
completionTokens: (_b = usageMetadata.candidatesTokenCount) != null ? _b : NaN
|
626
|
-
};
|
668
|
+
usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
|
669
|
+
usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
|
627
670
|
}
|
628
671
|
const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
|
629
672
|
if (candidate == null) {
|
@@ -633,17 +676,14 @@ var GoogleGenerativeAILanguageModel = class {
|
|
633
676
|
if (content != null) {
|
634
677
|
const deltaText = getTextFromParts(content.parts);
|
635
678
|
if (deltaText != null) {
|
636
|
-
controller.enqueue(
|
637
|
-
type: "text-delta",
|
638
|
-
textDelta: deltaText
|
639
|
-
});
|
679
|
+
controller.enqueue(deltaText);
|
640
680
|
}
|
641
681
|
const inlineDataParts = getInlineDataParts(content.parts);
|
642
682
|
if (inlineDataParts != null) {
|
643
683
|
for (const part of inlineDataParts) {
|
644
684
|
controller.enqueue({
|
645
685
|
type: "file",
|
646
|
-
|
686
|
+
mediaType: part.inlineData.mimeType,
|
647
687
|
data: part.inlineData.data
|
648
688
|
});
|
649
689
|
}
|
@@ -682,7 +722,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
682
722
|
generateId: generateId2
|
683
723
|
})) != null ? _d : [];
|
684
724
|
for (const source of sources) {
|
685
|
-
controller.enqueue(
|
725
|
+
controller.enqueue(source);
|
686
726
|
}
|
687
727
|
providerMetadata = {
|
688
728
|
google: {
|
@@ -702,9 +742,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
702
742
|
}
|
703
743
|
})
|
704
744
|
),
|
705
|
-
|
706
|
-
rawResponse: { headers: responseHeaders },
|
707
|
-
warnings,
|
745
|
+
response: { headers: responseHeaders },
|
708
746
|
request: { body }
|
709
747
|
};
|
710
748
|
}
|
@@ -717,6 +755,7 @@ function getToolCallsFromParts({
|
|
717
755
|
(part) => "functionCall" in part
|
718
756
|
);
|
719
757
|
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
758
|
+
type: "tool-call",
|
720
759
|
toolCallType: "function",
|
721
760
|
toolCallId: generateId2(),
|
722
761
|
toolName: part.functionCall.name,
|
@@ -725,7 +764,10 @@ function getToolCallsFromParts({
|
|
725
764
|
}
|
726
765
|
function getTextFromParts(parts) {
|
727
766
|
const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
|
728
|
-
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
|
+
};
|
729
771
|
}
|
730
772
|
function getInlineDataParts(parts) {
|
731
773
|
return parts == null ? void 0 : parts.filter(
|
@@ -740,173 +782,110 @@ function extractSources({
|
|
740
782
|
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
741
783
|
(chunk) => chunk.web != null
|
742
784
|
).map((chunk) => ({
|
785
|
+
type: "source",
|
743
786
|
sourceType: "url",
|
744
787
|
id: generateId2(),
|
745
788
|
url: chunk.web.uri,
|
746
789
|
title: chunk.web.title
|
747
790
|
}));
|
748
791
|
}
|
749
|
-
var contentSchema =
|
750
|
-
role:
|
751
|
-
parts:
|
752
|
-
|
753
|
-
|
754
|
-
text:
|
792
|
+
var contentSchema = import_zod4.z.object({
|
793
|
+
role: import_zod4.z.string(),
|
794
|
+
parts: import_zod4.z.array(
|
795
|
+
import_zod4.z.union([
|
796
|
+
import_zod4.z.object({
|
797
|
+
text: import_zod4.z.string()
|
755
798
|
}),
|
756
|
-
|
757
|
-
functionCall:
|
758
|
-
name:
|
759
|
-
args:
|
799
|
+
import_zod4.z.object({
|
800
|
+
functionCall: import_zod4.z.object({
|
801
|
+
name: import_zod4.z.string(),
|
802
|
+
args: import_zod4.z.unknown()
|
760
803
|
})
|
761
804
|
}),
|
762
|
-
|
763
|
-
inlineData:
|
764
|
-
mimeType:
|
765
|
-
data:
|
805
|
+
import_zod4.z.object({
|
806
|
+
inlineData: import_zod4.z.object({
|
807
|
+
mimeType: import_zod4.z.string(),
|
808
|
+
data: import_zod4.z.string()
|
766
809
|
})
|
767
810
|
})
|
768
811
|
])
|
769
812
|
).nullish()
|
770
813
|
});
|
771
|
-
var groundingChunkSchema =
|
772
|
-
web:
|
773
|
-
retrievedContext:
|
814
|
+
var groundingChunkSchema = import_zod4.z.object({
|
815
|
+
web: import_zod4.z.object({ uri: import_zod4.z.string(), title: import_zod4.z.string() }).nullish(),
|
816
|
+
retrievedContext: import_zod4.z.object({ uri: import_zod4.z.string(), title: import_zod4.z.string() }).nullish()
|
774
817
|
});
|
775
|
-
var groundingMetadataSchema =
|
776
|
-
webSearchQueries:
|
777
|
-
retrievalQueries:
|
778
|
-
searchEntryPoint:
|
779
|
-
groundingChunks:
|
780
|
-
groundingSupports:
|
781
|
-
|
782
|
-
segment:
|
783
|
-
startIndex:
|
784
|
-
endIndex:
|
785
|
-
text:
|
818
|
+
var groundingMetadataSchema = import_zod4.z.object({
|
819
|
+
webSearchQueries: import_zod4.z.array(import_zod4.z.string()).nullish(),
|
820
|
+
retrievalQueries: import_zod4.z.array(import_zod4.z.string()).nullish(),
|
821
|
+
searchEntryPoint: import_zod4.z.object({ renderedContent: import_zod4.z.string() }).nullish(),
|
822
|
+
groundingChunks: import_zod4.z.array(groundingChunkSchema).nullish(),
|
823
|
+
groundingSupports: import_zod4.z.array(
|
824
|
+
import_zod4.z.object({
|
825
|
+
segment: import_zod4.z.object({
|
826
|
+
startIndex: import_zod4.z.number().nullish(),
|
827
|
+
endIndex: import_zod4.z.number().nullish(),
|
828
|
+
text: import_zod4.z.string().nullish()
|
786
829
|
}),
|
787
|
-
segment_text:
|
788
|
-
groundingChunkIndices:
|
789
|
-
supportChunkIndices:
|
790
|
-
confidenceScores:
|
791
|
-
confidenceScore:
|
830
|
+
segment_text: import_zod4.z.string().nullish(),
|
831
|
+
groundingChunkIndices: import_zod4.z.array(import_zod4.z.number()).nullish(),
|
832
|
+
supportChunkIndices: import_zod4.z.array(import_zod4.z.number()).nullish(),
|
833
|
+
confidenceScores: import_zod4.z.array(import_zod4.z.number()).nullish(),
|
834
|
+
confidenceScore: import_zod4.z.array(import_zod4.z.number()).nullish()
|
792
835
|
})
|
793
836
|
).nullish(),
|
794
|
-
retrievalMetadata:
|
795
|
-
|
796
|
-
webDynamicRetrievalScore:
|
837
|
+
retrievalMetadata: import_zod4.z.union([
|
838
|
+
import_zod4.z.object({
|
839
|
+
webDynamicRetrievalScore: import_zod4.z.number()
|
797
840
|
}),
|
798
|
-
|
841
|
+
import_zod4.z.object({})
|
799
842
|
]).nullish()
|
800
843
|
});
|
801
|
-
var safetyRatingSchema =
|
802
|
-
category:
|
803
|
-
probability:
|
804
|
-
probabilityScore:
|
805
|
-
severity:
|
806
|
-
severityScore:
|
807
|
-
blocked:
|
844
|
+
var safetyRatingSchema = import_zod4.z.object({
|
845
|
+
category: import_zod4.z.string(),
|
846
|
+
probability: import_zod4.z.string(),
|
847
|
+
probabilityScore: import_zod4.z.number().nullish(),
|
848
|
+
severity: import_zod4.z.string().nullish(),
|
849
|
+
severityScore: import_zod4.z.number().nullish(),
|
850
|
+
blocked: import_zod4.z.boolean().nullish()
|
808
851
|
});
|
809
|
-
var responseSchema =
|
810
|
-
candidates:
|
811
|
-
|
812
|
-
content: contentSchema.nullish().or(
|
813
|
-
finishReason:
|
814
|
-
safetyRatings:
|
852
|
+
var responseSchema = import_zod4.z.object({
|
853
|
+
candidates: import_zod4.z.array(
|
854
|
+
import_zod4.z.object({
|
855
|
+
content: contentSchema.nullish().or(import_zod4.z.object({}).strict()),
|
856
|
+
finishReason: import_zod4.z.string().nullish(),
|
857
|
+
safetyRatings: import_zod4.z.array(safetyRatingSchema).nullish(),
|
815
858
|
groundingMetadata: groundingMetadataSchema.nullish()
|
816
859
|
})
|
817
860
|
),
|
818
|
-
usageMetadata:
|
819
|
-
promptTokenCount:
|
820
|
-
candidatesTokenCount:
|
821
|
-
totalTokenCount:
|
861
|
+
usageMetadata: import_zod4.z.object({
|
862
|
+
promptTokenCount: import_zod4.z.number().nullish(),
|
863
|
+
candidatesTokenCount: import_zod4.z.number().nullish(),
|
864
|
+
totalTokenCount: import_zod4.z.number().nullish()
|
822
865
|
}).nullish()
|
823
866
|
});
|
824
|
-
var chunkSchema =
|
825
|
-
candidates:
|
826
|
-
|
867
|
+
var chunkSchema = import_zod4.z.object({
|
868
|
+
candidates: import_zod4.z.array(
|
869
|
+
import_zod4.z.object({
|
827
870
|
content: contentSchema.nullish(),
|
828
|
-
finishReason:
|
829
|
-
safetyRatings:
|
871
|
+
finishReason: import_zod4.z.string().nullish(),
|
872
|
+
safetyRatings: import_zod4.z.array(safetyRatingSchema).nullish(),
|
830
873
|
groundingMetadata: groundingMetadataSchema.nullish()
|
831
874
|
})
|
832
875
|
).nullish(),
|
833
|
-
usageMetadata:
|
834
|
-
promptTokenCount:
|
835
|
-
candidatesTokenCount:
|
836
|
-
totalTokenCount:
|
876
|
+
usageMetadata: import_zod4.z.object({
|
877
|
+
promptTokenCount: import_zod4.z.number().nullish(),
|
878
|
+
candidatesTokenCount: import_zod4.z.number().nullish(),
|
879
|
+
totalTokenCount: import_zod4.z.number().nullish()
|
837
880
|
}).nullish()
|
838
881
|
});
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
var GoogleGenerativeAIEmbeddingModel = class {
|
845
|
-
constructor(modelId, settings, config) {
|
846
|
-
this.specificationVersion = "v1";
|
847
|
-
this.modelId = modelId;
|
848
|
-
this.settings = settings;
|
849
|
-
this.config = config;
|
850
|
-
}
|
851
|
-
get provider() {
|
852
|
-
return this.config.provider;
|
853
|
-
}
|
854
|
-
get maxEmbeddingsPerCall() {
|
855
|
-
return 2048;
|
856
|
-
}
|
857
|
-
get supportsParallelCalls() {
|
858
|
-
return true;
|
859
|
-
}
|
860
|
-
async doEmbed({
|
861
|
-
values,
|
862
|
-
headers,
|
863
|
-
abortSignal
|
864
|
-
}) {
|
865
|
-
if (values.length > this.maxEmbeddingsPerCall) {
|
866
|
-
throw new import_provider3.TooManyEmbeddingValuesForCallError({
|
867
|
-
provider: this.provider,
|
868
|
-
modelId: this.modelId,
|
869
|
-
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
870
|
-
values
|
871
|
-
});
|
872
|
-
}
|
873
|
-
const mergedHeaders = (0, import_provider_utils4.combineHeaders)(
|
874
|
-
await (0, import_provider_utils4.resolve)(this.config.headers),
|
875
|
-
headers
|
876
|
-
);
|
877
|
-
const { responseHeaders, value: response } = await (0, import_provider_utils4.postJsonToApi)({
|
878
|
-
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
879
|
-
headers: mergedHeaders,
|
880
|
-
body: {
|
881
|
-
requests: values.map((value) => ({
|
882
|
-
model: `models/${this.modelId}`,
|
883
|
-
content: { role: "user", parts: [{ text: value }] },
|
884
|
-
outputDimensionality: this.settings.outputDimensionality
|
885
|
-
}))
|
886
|
-
},
|
887
|
-
failedResponseHandler: googleFailedResponseHandler,
|
888
|
-
successfulResponseHandler: (0, import_provider_utils4.createJsonResponseHandler)(
|
889
|
-
googleGenerativeAITextEmbeddingResponseSchema
|
890
|
-
),
|
891
|
-
abortSignal,
|
892
|
-
fetch: this.config.fetch
|
893
|
-
});
|
894
|
-
return {
|
895
|
-
embeddings: response.embeddings.map((item) => item.values),
|
896
|
-
usage: void 0,
|
897
|
-
rawResponse: { headers: responseHeaders }
|
898
|
-
};
|
899
|
-
}
|
900
|
-
};
|
901
|
-
var googleGenerativeAITextEmbeddingResponseSchema = import_zod3.z.object({
|
902
|
-
embeddings: import_zod3.z.array(import_zod3.z.object({ values: import_zod3.z.array(import_zod3.z.number()) }))
|
882
|
+
var googleGenerativeAIProviderOptionsSchema = import_zod4.z.object({
|
883
|
+
responseModalities: import_zod4.z.array(import_zod4.z.enum(["TEXT", "IMAGE"])).nullish(),
|
884
|
+
thinkingConfig: import_zod4.z.object({
|
885
|
+
thinkingBudget: import_zod4.z.number().nullish()
|
886
|
+
}).nullish()
|
903
887
|
});
|
904
888
|
|
905
|
-
// src/google-supported-file-url.ts
|
906
|
-
function isSupportedFileUrl(url) {
|
907
|
-
return url.toString().startsWith("https://generativelanguage.googleapis.com/v1beta/files/");
|
908
|
-
}
|
909
|
-
|
910
889
|
// src/google-provider.ts
|
911
890
|
function createGoogleGenerativeAI(options = {}) {
|
912
891
|
var _a;
|
@@ -926,11 +905,16 @@ function createGoogleGenerativeAI(options = {}) {
|
|
926
905
|
baseURL,
|
927
906
|
headers: getHeaders,
|
928
907
|
generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils5.generateId,
|
929
|
-
|
908
|
+
getSupportedUrls: async () => ({
|
909
|
+
"*": [
|
910
|
+
// HTTP URLs:
|
911
|
+
/^https?:\/\/.*$/
|
912
|
+
]
|
913
|
+
}),
|
930
914
|
fetch: options.fetch
|
931
915
|
});
|
932
916
|
};
|
933
|
-
const createEmbeddingModel = (modelId
|
917
|
+
const createEmbeddingModel = (modelId) => new GoogleGenerativeAIEmbeddingModel(modelId, {
|
934
918
|
provider: "google.generative-ai",
|
935
919
|
baseURL,
|
936
920
|
headers: getHeaders,
|
@@ -950,6 +934,9 @@ function createGoogleGenerativeAI(options = {}) {
|
|
950
934
|
provider.embedding = createEmbeddingModel;
|
951
935
|
provider.textEmbedding = createEmbeddingModel;
|
952
936
|
provider.textEmbeddingModel = createEmbeddingModel;
|
937
|
+
provider.imageModel = (modelId) => {
|
938
|
+
throw new import_provider4.NoSuchModelError({ modelId, modelType: "imageModel" });
|
939
|
+
};
|
953
940
|
return provider;
|
954
941
|
}
|
955
942
|
var google = createGoogleGenerativeAI();
|