@ai-sdk/google 2.0.0-canary.2 → 2.0.0-canary.4
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 +21 -0
- package/dist/index.js +79 -141
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -127
- package/dist/index.mjs.map +1 -1
- package/internal/dist/index.js +77 -139
- package/internal/dist/index.js.map +1 -1
- package/internal/dist/index.mjs +65 -127
- package/internal/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,26 @@
|
|
1
1
|
# @ai-sdk/google
|
2
2
|
|
3
|
+
## 2.0.0-canary.4
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- Updated dependencies [a166433]
|
8
|
+
- Updated dependencies [abf9a79]
|
9
|
+
- Updated dependencies [9f95b35]
|
10
|
+
- Updated dependencies [0a87932]
|
11
|
+
- Updated dependencies [6dc848c]
|
12
|
+
- @ai-sdk/provider-utils@3.0.0-canary.3
|
13
|
+
- @ai-sdk/provider@2.0.0-canary.2
|
14
|
+
|
15
|
+
## 2.0.0-canary.3
|
16
|
+
|
17
|
+
### Patch Changes
|
18
|
+
|
19
|
+
- Updated dependencies [c57e248]
|
20
|
+
- Updated dependencies [33f4a6a]
|
21
|
+
- @ai-sdk/provider@2.0.0-canary.1
|
22
|
+
- @ai-sdk/provider-utils@3.0.0-canary.2
|
23
|
+
|
3
24
|
## 2.0.0-canary.2
|
4
25
|
|
5
26
|
### Patch Changes
|
package/dist/index.js
CHANGED
@@ -27,7 +27,7 @@ module.exports = __toCommonJS(src_exports);
|
|
27
27
|
|
28
28
|
// src/google-provider.ts
|
29
29
|
var import_provider4 = require("@ai-sdk/provider");
|
30
|
-
var
|
30
|
+
var import_provider_utils4 = require("@ai-sdk/provider-utils");
|
31
31
|
|
32
32
|
// src/google-generative-ai-embedding-model.ts
|
33
33
|
var import_provider = require("@ai-sdk/provider");
|
@@ -112,7 +112,7 @@ var googleGenerativeAITextEmbeddingResponseSchema = import_zod2.z.object({
|
|
112
112
|
});
|
113
113
|
|
114
114
|
// src/google-generative-ai-language-model.ts
|
115
|
-
var
|
115
|
+
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
116
116
|
var import_zod3 = require("zod");
|
117
117
|
|
118
118
|
// src/convert-json-schema-to-openapi-schema.ts
|
@@ -214,9 +214,7 @@ function isEmptyObjectSchema(jsonSchema) {
|
|
214
214
|
|
215
215
|
// src/convert-to-google-generative-ai-messages.ts
|
216
216
|
var import_provider2 = require("@ai-sdk/provider");
|
217
|
-
var import_provider_utils3 = require("@ai-sdk/provider-utils");
|
218
217
|
function convertToGoogleGenerativeAIMessages(prompt) {
|
219
|
-
var _a, _b;
|
220
218
|
const systemInstructionParts = [];
|
221
219
|
const contents = [];
|
222
220
|
let systemMessagesAllowed = true;
|
@@ -240,35 +238,15 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
240
238
|
parts.push({ text: part.text });
|
241
239
|
break;
|
242
240
|
}
|
243
|
-
case "image": {
|
244
|
-
parts.push(
|
245
|
-
part.image instanceof URL ? {
|
246
|
-
fileData: {
|
247
|
-
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
248
|
-
fileUri: part.image.toString()
|
249
|
-
}
|
250
|
-
} : {
|
251
|
-
inlineData: {
|
252
|
-
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
253
|
-
data: (0, import_provider_utils3.convertUint8ArrayToBase64)(part.image)
|
254
|
-
}
|
255
|
-
}
|
256
|
-
);
|
257
|
-
break;
|
258
|
-
}
|
259
241
|
case "file": {
|
242
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
260
243
|
parts.push(
|
261
244
|
part.data instanceof URL ? {
|
262
245
|
fileData: {
|
263
|
-
mimeType:
|
246
|
+
mimeType: mediaType,
|
264
247
|
fileUri: part.data.toString()
|
265
248
|
}
|
266
|
-
} : {
|
267
|
-
inlineData: {
|
268
|
-
mimeType: part.mimeType,
|
269
|
-
data: part.data
|
270
|
-
}
|
271
|
-
}
|
249
|
+
} : { inlineData: { mimeType: mediaType, data: part.data } }
|
272
250
|
);
|
273
251
|
break;
|
274
252
|
}
|
@@ -287,7 +265,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
287
265
|
return part.text.length === 0 ? void 0 : { text: part.text };
|
288
266
|
}
|
289
267
|
case "file": {
|
290
|
-
if (part.
|
268
|
+
if (part.mediaType !== "image/png") {
|
291
269
|
throw new import_provider2.UnsupportedFunctionalityError({
|
292
270
|
functionality: "Only PNG images are supported in assistant messages"
|
293
271
|
});
|
@@ -299,7 +277,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
299
277
|
}
|
300
278
|
return {
|
301
279
|
inlineData: {
|
302
|
-
mimeType: part.
|
280
|
+
mimeType: part.mediaType,
|
303
281
|
data: part.data
|
304
282
|
}
|
305
283
|
};
|
@@ -348,9 +326,15 @@ function getModelPath(modelId) {
|
|
348
326
|
|
349
327
|
// src/google-prepare-tools.ts
|
350
328
|
var import_provider3 = require("@ai-sdk/provider");
|
351
|
-
function prepareTools(
|
352
|
-
|
353
|
-
|
329
|
+
function prepareTools({
|
330
|
+
tools,
|
331
|
+
toolChoice,
|
332
|
+
useSearchGrounding,
|
333
|
+
dynamicRetrievalConfig,
|
334
|
+
modelId
|
335
|
+
}) {
|
336
|
+
var _a;
|
337
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
354
338
|
const toolWarnings = [];
|
355
339
|
const isGemini2 = modelId.includes("gemini-2");
|
356
340
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
@@ -373,12 +357,11 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
373
357
|
} else {
|
374
358
|
functionDeclarations.push({
|
375
359
|
name: tool.name,
|
376
|
-
description: (
|
360
|
+
description: (_a = tool.description) != null ? _a : "",
|
377
361
|
parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
|
378
362
|
});
|
379
363
|
}
|
380
364
|
}
|
381
|
-
const toolChoice = mode.toolChoice;
|
382
365
|
if (toolChoice == null) {
|
383
366
|
return {
|
384
367
|
tools: { functionDeclarations },
|
@@ -420,7 +403,7 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
420
403
|
default: {
|
421
404
|
const _exhaustiveCheck = type;
|
422
405
|
throw new import_provider3.UnsupportedFunctionalityError({
|
423
|
-
functionality: `
|
406
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
424
407
|
});
|
425
408
|
}
|
426
409
|
}
|
@@ -471,7 +454,6 @@ var GoogleGenerativeAILanguageModel = class {
|
|
471
454
|
return this.config.provider;
|
472
455
|
}
|
473
456
|
async getArgs({
|
474
|
-
mode,
|
475
457
|
prompt,
|
476
458
|
maxTokens,
|
477
459
|
temperature,
|
@@ -482,105 +464,61 @@ var GoogleGenerativeAILanguageModel = class {
|
|
482
464
|
stopSequences,
|
483
465
|
responseFormat,
|
484
466
|
seed,
|
485
|
-
|
467
|
+
tools,
|
468
|
+
toolChoice,
|
469
|
+
providerOptions
|
486
470
|
}) {
|
487
|
-
var _a
|
488
|
-
const type = mode.type;
|
471
|
+
var _a;
|
489
472
|
const warnings = [];
|
490
|
-
const googleOptions = (0,
|
473
|
+
const googleOptions = (0, import_provider_utils3.parseProviderOptions)({
|
491
474
|
provider: "google",
|
492
|
-
providerOptions
|
475
|
+
providerOptions,
|
493
476
|
schema: googleGenerativeAIProviderOptionsSchema
|
494
477
|
});
|
495
|
-
const generationConfig = {
|
496
|
-
// standardized settings:
|
497
|
-
maxOutputTokens: maxTokens,
|
498
|
-
temperature,
|
499
|
-
topK,
|
500
|
-
topP,
|
501
|
-
frequencyPenalty,
|
502
|
-
presencePenalty,
|
503
|
-
stopSequences,
|
504
|
-
seed,
|
505
|
-
// response format:
|
506
|
-
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
507
|
-
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
508
|
-
// so this is needed as an escape hatch:
|
509
|
-
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
510
|
-
...this.settings.audioTimestamp && {
|
511
|
-
audioTimestamp: this.settings.audioTimestamp
|
512
|
-
},
|
513
|
-
// provider options:
|
514
|
-
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
|
515
|
-
};
|
516
478
|
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
|
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
|
-
|
546
|
-
|
547
|
-
},
|
548
|
-
contents,
|
549
|
-
systemInstruction,
|
550
|
-
safetySettings: this.settings.safetySettings,
|
551
|
-
cachedContent: this.settings.cachedContent
|
552
|
-
},
|
553
|
-
warnings
|
554
|
-
};
|
555
|
-
}
|
556
|
-
case "object-tool": {
|
557
|
-
return {
|
558
|
-
args: {
|
559
|
-
generationConfig,
|
560
|
-
contents,
|
561
|
-
tools: {
|
562
|
-
functionDeclarations: [
|
563
|
-
{
|
564
|
-
name: mode.tool.name,
|
565
|
-
description: (_b = mode.tool.description) != null ? _b : "",
|
566
|
-
parameters: convertJSONSchemaToOpenAPISchema(
|
567
|
-
mode.tool.parameters
|
568
|
-
)
|
569
|
-
}
|
570
|
-
]
|
571
|
-
},
|
572
|
-
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
573
|
-
safetySettings: this.settings.safetySettings,
|
574
|
-
cachedContent: this.settings.cachedContent
|
479
|
+
const {
|
480
|
+
tools: googleTools,
|
481
|
+
toolConfig: googleToolConfig,
|
482
|
+
toolWarnings
|
483
|
+
} = prepareTools({
|
484
|
+
tools,
|
485
|
+
toolChoice,
|
486
|
+
useSearchGrounding: (_a = this.settings.useSearchGrounding) != null ? _a : false,
|
487
|
+
dynamicRetrievalConfig: this.settings.dynamicRetrievalConfig,
|
488
|
+
modelId: this.modelId
|
489
|
+
});
|
490
|
+
return {
|
491
|
+
args: {
|
492
|
+
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
|
575
509
|
},
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
510
|
+
// provider options:
|
511
|
+
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
|
512
|
+
},
|
513
|
+
contents,
|
514
|
+
systemInstruction,
|
515
|
+
safetySettings: this.settings.safetySettings,
|
516
|
+
tools: googleTools,
|
517
|
+
toolConfig: googleToolConfig,
|
518
|
+
cachedContent: this.settings.cachedContent
|
519
|
+
},
|
520
|
+
warnings: [...warnings, ...toolWarnings]
|
521
|
+
};
|
584
522
|
}
|
585
523
|
supportsUrl(url) {
|
586
524
|
return this.config.isSupportedUrl(url);
|
@@ -589,22 +527,22 @@ var GoogleGenerativeAILanguageModel = class {
|
|
589
527
|
var _a, _b, _c, _d, _e;
|
590
528
|
const { args, warnings } = await this.getArgs(options);
|
591
529
|
const body = JSON.stringify(args);
|
592
|
-
const mergedHeaders = (0,
|
593
|
-
await (0,
|
530
|
+
const mergedHeaders = (0, import_provider_utils3.combineHeaders)(
|
531
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
594
532
|
options.headers
|
595
533
|
);
|
596
534
|
const {
|
597
535
|
responseHeaders,
|
598
536
|
value: response,
|
599
537
|
rawValue: rawResponse
|
600
|
-
} = await (0,
|
538
|
+
} = await (0, import_provider_utils3.postJsonToApi)({
|
601
539
|
url: `${this.config.baseURL}/${getModelPath(
|
602
540
|
this.modelId
|
603
541
|
)}:generateContent`,
|
604
542
|
headers: mergedHeaders,
|
605
543
|
body: args,
|
606
544
|
failedResponseHandler: googleFailedResponseHandler,
|
607
|
-
successfulResponseHandler: (0,
|
545
|
+
successfulResponseHandler: (0, import_provider_utils3.createJsonResponseHandler)(responseSchema),
|
608
546
|
abortSignal: options.abortSignal,
|
609
547
|
fetch: this.config.fetch
|
610
548
|
});
|
@@ -620,7 +558,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
620
558
|
text: getTextFromParts(parts),
|
621
559
|
files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
|
622
560
|
data: part.inlineData.data,
|
623
|
-
|
561
|
+
mediaType: part.inlineData.mimeType
|
624
562
|
})),
|
625
563
|
toolCalls,
|
626
564
|
finishReason: mapGoogleGenerativeAIFinishReason({
|
@@ -650,18 +588,18 @@ var GoogleGenerativeAILanguageModel = class {
|
|
650
588
|
async doStream(options) {
|
651
589
|
const { args, warnings } = await this.getArgs(options);
|
652
590
|
const body = JSON.stringify(args);
|
653
|
-
const headers = (0,
|
654
|
-
await (0,
|
591
|
+
const headers = (0, import_provider_utils3.combineHeaders)(
|
592
|
+
await (0, import_provider_utils3.resolve)(this.config.headers),
|
655
593
|
options.headers
|
656
594
|
);
|
657
|
-
const { responseHeaders, value: response } = await (0,
|
595
|
+
const { responseHeaders, value: response } = await (0, import_provider_utils3.postJsonToApi)({
|
658
596
|
url: `${this.config.baseURL}/${getModelPath(
|
659
597
|
this.modelId
|
660
598
|
)}:streamGenerateContent?alt=sse`,
|
661
599
|
headers,
|
662
600
|
body: args,
|
663
601
|
failedResponseHandler: googleFailedResponseHandler,
|
664
|
-
successfulResponseHandler: (0,
|
602
|
+
successfulResponseHandler: (0, import_provider_utils3.createEventSourceResponseHandler)(chunkSchema),
|
665
603
|
abortSignal: options.abortSignal,
|
666
604
|
fetch: this.config.fetch
|
667
605
|
});
|
@@ -709,7 +647,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
709
647
|
for (const part of inlineDataParts) {
|
710
648
|
controller.enqueue({
|
711
649
|
type: "file",
|
712
|
-
|
650
|
+
mediaType: part.inlineData.mimeType,
|
713
651
|
data: part.inlineData.data
|
714
652
|
});
|
715
653
|
}
|
@@ -914,9 +852,9 @@ function isSupportedFileUrl(url) {
|
|
914
852
|
// src/google-provider.ts
|
915
853
|
function createGoogleGenerativeAI(options = {}) {
|
916
854
|
var _a;
|
917
|
-
const baseURL = (_a = (0,
|
855
|
+
const baseURL = (_a = (0, import_provider_utils4.withoutTrailingSlash)(options.baseURL)) != null ? _a : "https://generativelanguage.googleapis.com/v1beta";
|
918
856
|
const getHeaders = () => ({
|
919
|
-
"x-goog-api-key": (0,
|
857
|
+
"x-goog-api-key": (0, import_provider_utils4.loadApiKey)({
|
920
858
|
apiKey: options.apiKey,
|
921
859
|
environmentVariableName: "GOOGLE_GENERATIVE_AI_API_KEY",
|
922
860
|
description: "Google Generative AI"
|
@@ -929,7 +867,7 @@ function createGoogleGenerativeAI(options = {}) {
|
|
929
867
|
provider: "google.generative-ai",
|
930
868
|
baseURL,
|
931
869
|
headers: getHeaders,
|
932
|
-
generateId: (_a2 = options.generateId) != null ? _a2 :
|
870
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : import_provider_utils4.generateId,
|
933
871
|
isSupportedUrl: isSupportedFileUrl,
|
934
872
|
fetch: options.fetch
|
935
873
|
});
|