@core-ai/google-genai 0.5.1 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +128 -1
- package/dist/index.js +188 -77
- package/package.json +5 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { GoogleGenAI } from '@google/genai';
|
|
2
2
|
import { ChatModel, EmbeddingModel, ImageModel } from '@core-ai/core-ai';
|
|
3
|
+
import { z } from 'zod';
|
|
3
4
|
|
|
4
5
|
type GoogleGenAIProviderOptions = {
|
|
5
6
|
apiKey?: string;
|
|
@@ -14,4 +15,130 @@ type GoogleGenAIProvider = {
|
|
|
14
15
|
};
|
|
15
16
|
declare function createGoogleGenAI(options?: GoogleGenAIProviderOptions): GoogleGenAIProvider;
|
|
16
17
|
|
|
17
|
-
|
|
18
|
+
type GoogleReasoningMetadata = {
|
|
19
|
+
thoughtSignature?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
declare const googleGenerateProviderOptionsSchema: z.ZodObject<{
|
|
23
|
+
stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
24
|
+
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
25
|
+
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
26
|
+
seed: z.ZodOptional<z.ZodNumber>;
|
|
27
|
+
topK: z.ZodOptional<z.ZodNumber>;
|
|
28
|
+
}, "strict", z.ZodTypeAny, {
|
|
29
|
+
stopSequences?: string[] | undefined;
|
|
30
|
+
frequencyPenalty?: number | undefined;
|
|
31
|
+
presencePenalty?: number | undefined;
|
|
32
|
+
seed?: number | undefined;
|
|
33
|
+
topK?: number | undefined;
|
|
34
|
+
}, {
|
|
35
|
+
stopSequences?: string[] | undefined;
|
|
36
|
+
frequencyPenalty?: number | undefined;
|
|
37
|
+
presencePenalty?: number | undefined;
|
|
38
|
+
seed?: number | undefined;
|
|
39
|
+
topK?: number | undefined;
|
|
40
|
+
}>;
|
|
41
|
+
type GoogleGenerateProviderOptions = z.infer<typeof googleGenerateProviderOptionsSchema>;
|
|
42
|
+
declare const googleEmbedProviderOptionsSchema: z.ZodObject<{
|
|
43
|
+
taskType: z.ZodOptional<z.ZodString>;
|
|
44
|
+
title: z.ZodOptional<z.ZodString>;
|
|
45
|
+
mimeType: z.ZodOptional<z.ZodString>;
|
|
46
|
+
autoTruncate: z.ZodOptional<z.ZodBoolean>;
|
|
47
|
+
}, "strict", z.ZodTypeAny, {
|
|
48
|
+
taskType?: string | undefined;
|
|
49
|
+
title?: string | undefined;
|
|
50
|
+
mimeType?: string | undefined;
|
|
51
|
+
autoTruncate?: boolean | undefined;
|
|
52
|
+
}, {
|
|
53
|
+
taskType?: string | undefined;
|
|
54
|
+
title?: string | undefined;
|
|
55
|
+
mimeType?: string | undefined;
|
|
56
|
+
autoTruncate?: boolean | undefined;
|
|
57
|
+
}>;
|
|
58
|
+
type GoogleEmbedProviderOptions = z.infer<typeof googleEmbedProviderOptionsSchema>;
|
|
59
|
+
declare const googleImageProviderOptionsSchema: z.ZodObject<{
|
|
60
|
+
outputGcsUri: z.ZodOptional<z.ZodString>;
|
|
61
|
+
negativePrompt: z.ZodOptional<z.ZodString>;
|
|
62
|
+
aspectRatio: z.ZodOptional<z.ZodString>;
|
|
63
|
+
guidanceScale: z.ZodOptional<z.ZodNumber>;
|
|
64
|
+
seed: z.ZodOptional<z.ZodNumber>;
|
|
65
|
+
safetyFilterLevel: z.ZodOptional<z.ZodEnum<["BLOCK_LOW_AND_ABOVE", "BLOCK_MEDIUM_AND_ABOVE", "BLOCK_ONLY_HIGH", "BLOCK_NONE"]>>;
|
|
66
|
+
personGeneration: z.ZodOptional<z.ZodEnum<["DONT_ALLOW", "ALLOW_ADULT", "ALLOW_ALL"]>>;
|
|
67
|
+
includeSafetyAttributes: z.ZodOptional<z.ZodBoolean>;
|
|
68
|
+
includeRaiReason: z.ZodOptional<z.ZodBoolean>;
|
|
69
|
+
language: z.ZodOptional<z.ZodString>;
|
|
70
|
+
outputMimeType: z.ZodOptional<z.ZodString>;
|
|
71
|
+
outputCompressionQuality: z.ZodOptional<z.ZodNumber>;
|
|
72
|
+
addWatermark: z.ZodOptional<z.ZodBoolean>;
|
|
73
|
+
labels: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
74
|
+
imageSize: z.ZodOptional<z.ZodString>;
|
|
75
|
+
enhancePrompt: z.ZodOptional<z.ZodBoolean>;
|
|
76
|
+
}, "strict", z.ZodTypeAny, {
|
|
77
|
+
seed?: number | undefined;
|
|
78
|
+
outputGcsUri?: string | undefined;
|
|
79
|
+
negativePrompt?: string | undefined;
|
|
80
|
+
aspectRatio?: string | undefined;
|
|
81
|
+
guidanceScale?: number | undefined;
|
|
82
|
+
safetyFilterLevel?: "BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_ONLY_HIGH" | "BLOCK_NONE" | undefined;
|
|
83
|
+
personGeneration?: "DONT_ALLOW" | "ALLOW_ADULT" | "ALLOW_ALL" | undefined;
|
|
84
|
+
includeSafetyAttributes?: boolean | undefined;
|
|
85
|
+
includeRaiReason?: boolean | undefined;
|
|
86
|
+
language?: string | undefined;
|
|
87
|
+
outputMimeType?: string | undefined;
|
|
88
|
+
outputCompressionQuality?: number | undefined;
|
|
89
|
+
addWatermark?: boolean | undefined;
|
|
90
|
+
labels?: Record<string, string> | undefined;
|
|
91
|
+
imageSize?: string | undefined;
|
|
92
|
+
enhancePrompt?: boolean | undefined;
|
|
93
|
+
}, {
|
|
94
|
+
seed?: number | undefined;
|
|
95
|
+
outputGcsUri?: string | undefined;
|
|
96
|
+
negativePrompt?: string | undefined;
|
|
97
|
+
aspectRatio?: string | undefined;
|
|
98
|
+
guidanceScale?: number | undefined;
|
|
99
|
+
safetyFilterLevel?: "BLOCK_LOW_AND_ABOVE" | "BLOCK_MEDIUM_AND_ABOVE" | "BLOCK_ONLY_HIGH" | "BLOCK_NONE" | undefined;
|
|
100
|
+
personGeneration?: "DONT_ALLOW" | "ALLOW_ADULT" | "ALLOW_ALL" | undefined;
|
|
101
|
+
includeSafetyAttributes?: boolean | undefined;
|
|
102
|
+
includeRaiReason?: boolean | undefined;
|
|
103
|
+
language?: string | undefined;
|
|
104
|
+
outputMimeType?: string | undefined;
|
|
105
|
+
outputCompressionQuality?: number | undefined;
|
|
106
|
+
addWatermark?: boolean | undefined;
|
|
107
|
+
labels?: Record<string, string> | undefined;
|
|
108
|
+
imageSize?: string | undefined;
|
|
109
|
+
enhancePrompt?: boolean | undefined;
|
|
110
|
+
}>;
|
|
111
|
+
type GoogleImageProviderOptions = z.infer<typeof googleImageProviderOptionsSchema>;
|
|
112
|
+
declare module '@core-ai/core-ai' {
|
|
113
|
+
interface GenerateProviderOptions {
|
|
114
|
+
google?: GoogleGenerateProviderOptions;
|
|
115
|
+
}
|
|
116
|
+
interface EmbedProviderOptions {
|
|
117
|
+
google?: GoogleEmbedProviderOptions;
|
|
118
|
+
}
|
|
119
|
+
interface ImageProviderOptions {
|
|
120
|
+
google?: GoogleImageProviderOptions;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
declare const googleProviderOptionsSchema: z.ZodObject<{
|
|
124
|
+
stopSequences: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
|
|
125
|
+
frequencyPenalty: z.ZodOptional<z.ZodNumber>;
|
|
126
|
+
presencePenalty: z.ZodOptional<z.ZodNumber>;
|
|
127
|
+
seed: z.ZodOptional<z.ZodNumber>;
|
|
128
|
+
topK: z.ZodOptional<z.ZodNumber>;
|
|
129
|
+
}, "strict", z.ZodTypeAny, {
|
|
130
|
+
stopSequences?: string[] | undefined;
|
|
131
|
+
frequencyPenalty?: number | undefined;
|
|
132
|
+
presencePenalty?: number | undefined;
|
|
133
|
+
seed?: number | undefined;
|
|
134
|
+
topK?: number | undefined;
|
|
135
|
+
}, {
|
|
136
|
+
stopSequences?: string[] | undefined;
|
|
137
|
+
frequencyPenalty?: number | undefined;
|
|
138
|
+
presencePenalty?: number | undefined;
|
|
139
|
+
seed?: number | undefined;
|
|
140
|
+
topK?: number | undefined;
|
|
141
|
+
}>;
|
|
142
|
+
type GoogleProviderOptions = GoogleGenerateProviderOptions;
|
|
143
|
+
|
|
144
|
+
export { type GoogleEmbedProviderOptions, type GoogleGenAIProvider, type GoogleGenAIProviderOptions, type GoogleGenerateProviderOptions, type GoogleImageProviderOptions, type GoogleProviderOptions as GoogleModelProviderOptions, type GoogleReasoningMetadata, createGoogleGenAI, googleEmbedProviderOptionsSchema, googleGenerateProviderOptionsSchema, googleImageProviderOptionsSchema, googleProviderOptionsSchema };
|
package/dist/index.js
CHANGED
|
@@ -6,8 +6,8 @@ import {
|
|
|
6
6
|
StructuredOutputNoObjectGeneratedError,
|
|
7
7
|
StructuredOutputParseError,
|
|
8
8
|
StructuredOutputValidationError,
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
createObjectStream,
|
|
10
|
+
createChatStream
|
|
11
11
|
} from "@core-ai/core-ai";
|
|
12
12
|
|
|
13
13
|
// src/chat-adapter.ts
|
|
@@ -15,6 +15,7 @@ import {
|
|
|
15
15
|
FunctionCallingConfigMode
|
|
16
16
|
} from "@google/genai";
|
|
17
17
|
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
18
|
+
import { getProviderMetadata } from "@core-ai/core-ai";
|
|
18
19
|
|
|
19
20
|
// src/model-capabilities.ts
|
|
20
21
|
var DEFAULT_CAPABILITIES = {
|
|
@@ -83,6 +84,67 @@ function asObject(value) {
|
|
|
83
84
|
return {};
|
|
84
85
|
}
|
|
85
86
|
|
|
87
|
+
// src/provider-options.ts
|
|
88
|
+
import { z } from "zod";
|
|
89
|
+
var googleGenerateProviderOptionsSchema = z.object({
|
|
90
|
+
stopSequences: z.array(z.string()).optional(),
|
|
91
|
+
frequencyPenalty: z.number().optional(),
|
|
92
|
+
presencePenalty: z.number().optional(),
|
|
93
|
+
seed: z.number().int().optional(),
|
|
94
|
+
topK: z.number().int().optional()
|
|
95
|
+
}).strict();
|
|
96
|
+
var googleEmbedProviderOptionsSchema = z.object({
|
|
97
|
+
taskType: z.string().optional(),
|
|
98
|
+
title: z.string().optional(),
|
|
99
|
+
mimeType: z.string().optional(),
|
|
100
|
+
autoTruncate: z.boolean().optional()
|
|
101
|
+
}).strict();
|
|
102
|
+
var googleImageProviderOptionsSchema = z.object({
|
|
103
|
+
outputGcsUri: z.string().optional(),
|
|
104
|
+
negativePrompt: z.string().optional(),
|
|
105
|
+
aspectRatio: z.string().optional(),
|
|
106
|
+
guidanceScale: z.number().optional(),
|
|
107
|
+
seed: z.number().int().optional(),
|
|
108
|
+
safetyFilterLevel: z.enum([
|
|
109
|
+
"BLOCK_LOW_AND_ABOVE",
|
|
110
|
+
"BLOCK_MEDIUM_AND_ABOVE",
|
|
111
|
+
"BLOCK_ONLY_HIGH",
|
|
112
|
+
"BLOCK_NONE"
|
|
113
|
+
]).optional(),
|
|
114
|
+
personGeneration: z.enum(["DONT_ALLOW", "ALLOW_ADULT", "ALLOW_ALL"]).optional(),
|
|
115
|
+
includeSafetyAttributes: z.boolean().optional(),
|
|
116
|
+
includeRaiReason: z.boolean().optional(),
|
|
117
|
+
language: z.string().optional(),
|
|
118
|
+
outputMimeType: z.string().optional(),
|
|
119
|
+
outputCompressionQuality: z.number().int().min(0).max(100).optional(),
|
|
120
|
+
addWatermark: z.boolean().optional(),
|
|
121
|
+
labels: z.record(z.string(), z.string()).optional(),
|
|
122
|
+
imageSize: z.string().optional(),
|
|
123
|
+
enhancePrompt: z.boolean().optional()
|
|
124
|
+
}).strict();
|
|
125
|
+
function parseGoogleGenerateProviderOptions(providerOptions) {
|
|
126
|
+
const rawOptions = providerOptions?.google;
|
|
127
|
+
if (rawOptions === void 0) {
|
|
128
|
+
return void 0;
|
|
129
|
+
}
|
|
130
|
+
return googleGenerateProviderOptionsSchema.parse(rawOptions);
|
|
131
|
+
}
|
|
132
|
+
function parseGoogleEmbedProviderOptions(providerOptions) {
|
|
133
|
+
const rawOptions = providerOptions?.google;
|
|
134
|
+
if (rawOptions === void 0) {
|
|
135
|
+
return void 0;
|
|
136
|
+
}
|
|
137
|
+
return googleEmbedProviderOptionsSchema.parse(rawOptions);
|
|
138
|
+
}
|
|
139
|
+
function parseGoogleImageProviderOptions(providerOptions) {
|
|
140
|
+
const rawOptions = providerOptions?.google;
|
|
141
|
+
if (rawOptions === void 0) {
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
return googleImageProviderOptionsSchema.parse(rawOptions);
|
|
145
|
+
}
|
|
146
|
+
var googleProviderOptionsSchema = googleGenerateProviderOptionsSchema;
|
|
147
|
+
|
|
86
148
|
// src/chat-adapter.ts
|
|
87
149
|
var DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME = "core_ai_generate_object";
|
|
88
150
|
var DEFAULT_STRUCTURED_OUTPUT_TOOL_DESCRIPTION = "Return a JSON object that matches the requested schema.";
|
|
@@ -121,13 +183,19 @@ function convertMessages(messages) {
|
|
|
121
183
|
});
|
|
122
184
|
continue;
|
|
123
185
|
}
|
|
186
|
+
const googleMeta = getProviderMetadata(
|
|
187
|
+
part.providerMetadata,
|
|
188
|
+
"google"
|
|
189
|
+
);
|
|
190
|
+
if (part.text.length === 0) {
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
124
193
|
const thoughtPart = {
|
|
125
194
|
text: part.text,
|
|
126
195
|
thought: true
|
|
127
196
|
};
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
thoughtPart["thoughtSignature"] = thoughtSignature;
|
|
197
|
+
if (typeof googleMeta?.thoughtSignature === "string") {
|
|
198
|
+
thoughtPart["thoughtSignature"] = googleMeta.thoughtSignature;
|
|
131
199
|
}
|
|
132
200
|
assistantParts.push(thoughtPart);
|
|
133
201
|
}
|
|
@@ -261,7 +329,9 @@ function createStructuredOutputOptions(options) {
|
|
|
261
329
|
type: "tool",
|
|
262
330
|
toolName
|
|
263
331
|
},
|
|
264
|
-
|
|
332
|
+
temperature: options.temperature,
|
|
333
|
+
maxTokens: options.maxTokens,
|
|
334
|
+
topP: options.topP,
|
|
265
335
|
providerOptions: options.providerOptions,
|
|
266
336
|
signal: options.signal
|
|
267
337
|
};
|
|
@@ -292,35 +362,40 @@ function inferMimeTypeFromUrl(url) {
|
|
|
292
362
|
return "application/octet-stream";
|
|
293
363
|
}
|
|
294
364
|
function createGenerateRequest(modelId, options) {
|
|
365
|
+
const googleOptions = parseGoogleGenerateProviderOptions(
|
|
366
|
+
options.providerOptions
|
|
367
|
+
);
|
|
295
368
|
const convertedMessages = convertMessages(options.messages);
|
|
369
|
+
const requestConfig = {
|
|
370
|
+
...convertedMessages.systemInstruction ? { systemInstruction: convertedMessages.systemInstruction } : {},
|
|
371
|
+
...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
|
|
372
|
+
...options.toolChoice ? { toolConfig: convertToolChoice(options.toolChoice) } : {},
|
|
373
|
+
...mapSamplingToConfig(options),
|
|
374
|
+
...mapReasoningToConfig(modelId, options, googleOptions),
|
|
375
|
+
...mapGoogleProviderOptionsToConfig(googleOptions),
|
|
376
|
+
...options.signal ? { abortSignal: options.signal } : {}
|
|
377
|
+
};
|
|
296
378
|
const baseRequest = {
|
|
297
379
|
model: modelId,
|
|
298
380
|
contents: convertedMessages.contents,
|
|
299
|
-
config:
|
|
300
|
-
...convertedMessages.systemInstruction ? { systemInstruction: convertedMessages.systemInstruction } : {},
|
|
301
|
-
...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
|
|
302
|
-
...options.toolChoice ? { toolConfig: convertToolChoice(options.toolChoice) } : {},
|
|
303
|
-
...options.config?.temperature !== void 0 ? { temperature: options.config.temperature } : {},
|
|
304
|
-
...options.config?.maxTokens !== void 0 ? { maxOutputTokens: options.config.maxTokens } : {},
|
|
305
|
-
...options.config?.topP !== void 0 ? { topP: options.config.topP } : {},
|
|
306
|
-
...options.config?.stopSequences ? { stopSequences: options.config.stopSequences } : {},
|
|
307
|
-
...options.config?.frequencyPenalty !== void 0 ? { frequencyPenalty: options.config.frequencyPenalty } : {},
|
|
308
|
-
...options.config?.presencePenalty !== void 0 ? { presencePenalty: options.config.presencePenalty } : {},
|
|
309
|
-
...mapReasoningToConfig(modelId, options)
|
|
310
|
-
}
|
|
381
|
+
config: requestConfig
|
|
311
382
|
};
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
}
|
|
316
|
-
const providerConfig = asObject(providerOptions["config"]);
|
|
383
|
+
return baseRequest;
|
|
384
|
+
}
|
|
385
|
+
function mapSamplingToConfig(options) {
|
|
317
386
|
return {
|
|
318
|
-
...
|
|
319
|
-
...
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
387
|
+
...options.temperature !== void 0 ? { temperature: options.temperature } : {},
|
|
388
|
+
...options.maxTokens !== void 0 ? { maxOutputTokens: options.maxTokens } : {},
|
|
389
|
+
...options.topP !== void 0 ? { topP: options.topP } : {}
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
function mapGoogleProviderOptionsToConfig(options) {
|
|
393
|
+
return {
|
|
394
|
+
...options?.stopSequences ? { stopSequences: options.stopSequences } : {},
|
|
395
|
+
...options?.frequencyPenalty !== void 0 ? { frequencyPenalty: options.frequencyPenalty } : {},
|
|
396
|
+
...options?.presencePenalty !== void 0 ? { presencePenalty: options.presencePenalty } : {},
|
|
397
|
+
...options?.seed !== void 0 ? { seed: options.seed } : {},
|
|
398
|
+
...options?.topK !== void 0 ? { topK: options.topK } : {}
|
|
324
399
|
};
|
|
325
400
|
}
|
|
326
401
|
function mapGenerateResponse(response) {
|
|
@@ -333,16 +408,6 @@ function mapGenerateResponse(response) {
|
|
|
333
408
|
const finishReason = mapFinishReason(
|
|
334
409
|
response.candidates?.[0]?.finishReason ?? void 0
|
|
335
410
|
);
|
|
336
|
-
if (!response.candidates?.[0]) {
|
|
337
|
-
return {
|
|
338
|
-
parts,
|
|
339
|
-
content: content.length > 0 ? content : null,
|
|
340
|
-
reasoning: reasoning.length > 0 ? reasoning : null,
|
|
341
|
-
toolCalls,
|
|
342
|
-
finishReason: toolCalls.length > 0 ? "tool-calls" : finishReason,
|
|
343
|
-
usage: mapUsage(response)
|
|
344
|
-
};
|
|
345
|
-
}
|
|
346
411
|
return {
|
|
347
412
|
parts,
|
|
348
413
|
content: content.length > 0 ? content : null,
|
|
@@ -385,6 +450,16 @@ async function* transformStream(stream) {
|
|
|
385
450
|
},
|
|
386
451
|
outputTokenDetails: {}
|
|
387
452
|
};
|
|
453
|
+
const closeReasoning = () => {
|
|
454
|
+
if (!reasoningOpen) {
|
|
455
|
+
return null;
|
|
456
|
+
}
|
|
457
|
+
reasoningOpen = false;
|
|
458
|
+
return {
|
|
459
|
+
type: "reasoning-end",
|
|
460
|
+
providerMetadata: { google: {} }
|
|
461
|
+
};
|
|
462
|
+
};
|
|
388
463
|
for await (const chunk of stream) {
|
|
389
464
|
usage = mapUsage(chunk, usage);
|
|
390
465
|
const reasoningDeltas = extractReasoningDeltas(chunk);
|
|
@@ -403,11 +478,9 @@ async function* transformStream(stream) {
|
|
|
403
478
|
}
|
|
404
479
|
}
|
|
405
480
|
if (chunk.text) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
yield
|
|
409
|
-
type: "reasoning-end"
|
|
410
|
-
};
|
|
481
|
+
const reasoningEnd2 = closeReasoning();
|
|
482
|
+
if (reasoningEnd2) {
|
|
483
|
+
yield reasoningEnd2;
|
|
411
484
|
}
|
|
412
485
|
yield {
|
|
413
486
|
type: "text-delta",
|
|
@@ -416,11 +489,9 @@ async function* transformStream(stream) {
|
|
|
416
489
|
}
|
|
417
490
|
const functionCalls = chunk.functionCalls ?? [];
|
|
418
491
|
if (functionCalls.length > 0) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
yield
|
|
422
|
-
type: "reasoning-end"
|
|
423
|
-
};
|
|
492
|
+
const reasoningEnd2 = closeReasoning();
|
|
493
|
+
if (reasoningEnd2) {
|
|
494
|
+
yield reasoningEnd2;
|
|
424
495
|
}
|
|
425
496
|
sawToolCalls = true;
|
|
426
497
|
for (const [index, functionCall] of functionCalls.entries()) {
|
|
@@ -464,10 +535,9 @@ async function* transformStream(stream) {
|
|
|
464
535
|
finishReason = candidateFinishReason;
|
|
465
536
|
}
|
|
466
537
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
};
|
|
538
|
+
const reasoningEnd = closeReasoning();
|
|
539
|
+
if (reasoningEnd) {
|
|
540
|
+
yield reasoningEnd;
|
|
471
541
|
}
|
|
472
542
|
for (const toolCall of bufferedToolCalls.values()) {
|
|
473
543
|
yield {
|
|
@@ -484,16 +554,11 @@ async function* transformStream(stream) {
|
|
|
484
554
|
usage
|
|
485
555
|
};
|
|
486
556
|
}
|
|
487
|
-
function mapReasoningToConfig(modelId, options) {
|
|
557
|
+
function mapReasoningToConfig(modelId, options, _googleProviderOptions) {
|
|
488
558
|
if (!options.reasoning) {
|
|
489
559
|
return {};
|
|
490
560
|
}
|
|
491
561
|
const capabilities = getGoogleModelCapabilities(modelId);
|
|
492
|
-
const providerConfig = asObject(options.providerOptions?.["config"]);
|
|
493
|
-
const providerThinkingConfig = asObject(providerConfig["thinkingConfig"]);
|
|
494
|
-
if (Object.keys(providerThinkingConfig).length > 0) {
|
|
495
|
-
return {};
|
|
496
|
-
}
|
|
497
562
|
if (capabilities.reasoning.thinkingParam === "thinkingLevel") {
|
|
498
563
|
return {
|
|
499
564
|
thinkingConfig: {
|
|
@@ -523,11 +588,11 @@ function extractAssistantParts(response) {
|
|
|
523
588
|
parts.push({
|
|
524
589
|
type: "reasoning",
|
|
525
590
|
text: thoughtText,
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
thoughtSignature
|
|
591
|
+
providerMetadata: {
|
|
592
|
+
google: {
|
|
593
|
+
...thoughtSignature ? { thoughtSignature } : {}
|
|
529
594
|
}
|
|
530
|
-
}
|
|
595
|
+
}
|
|
531
596
|
});
|
|
532
597
|
continue;
|
|
533
598
|
}
|
|
@@ -637,8 +702,10 @@ function createGoogleGenAIChatModel(client, modelId) {
|
|
|
637
702
|
}
|
|
638
703
|
async function streamChat(options) {
|
|
639
704
|
const request = createGenerateRequest(modelId, options);
|
|
640
|
-
|
|
641
|
-
|
|
705
|
+
return createChatStream(
|
|
706
|
+
async () => transformStream(await callGenerateContentStreamApi(request)),
|
|
707
|
+
{ signal: options.signal }
|
|
708
|
+
);
|
|
642
709
|
}
|
|
643
710
|
return {
|
|
644
711
|
provider,
|
|
@@ -665,13 +732,16 @@ function createGoogleGenAIChatModel(client, modelId) {
|
|
|
665
732
|
const structuredOptions = createStructuredOutputOptions(options);
|
|
666
733
|
const stream = await streamChat(structuredOptions);
|
|
667
734
|
const toolName = getStructuredOutputToolName(options);
|
|
668
|
-
return
|
|
735
|
+
return createObjectStream(
|
|
669
736
|
transformStructuredOutputStream(
|
|
670
737
|
stream,
|
|
671
738
|
options.schema,
|
|
672
739
|
provider,
|
|
673
740
|
toolName
|
|
674
|
-
)
|
|
741
|
+
),
|
|
742
|
+
{
|
|
743
|
+
signal: options.signal
|
|
744
|
+
}
|
|
675
745
|
);
|
|
676
746
|
}
|
|
677
747
|
};
|
|
@@ -786,7 +856,12 @@ function validateStructuredToolArguments(schema, toolArguments, provider) {
|
|
|
786
856
|
}
|
|
787
857
|
function parseAndValidateStructuredPayload(schema, rawPayload, provider) {
|
|
788
858
|
const parsedPayload = parseJson(rawPayload, provider);
|
|
789
|
-
return validateStructuredObject(
|
|
859
|
+
return validateStructuredObject(
|
|
860
|
+
schema,
|
|
861
|
+
parsedPayload,
|
|
862
|
+
provider,
|
|
863
|
+
rawPayload
|
|
864
|
+
);
|
|
790
865
|
}
|
|
791
866
|
function parseJson(rawOutput, provider) {
|
|
792
867
|
try {
|
|
@@ -839,13 +914,15 @@ function createGoogleGenAIEmbeddingModel(client, modelId) {
|
|
|
839
914
|
}
|
|
840
915
|
} : {}
|
|
841
916
|
};
|
|
842
|
-
const
|
|
843
|
-
|
|
917
|
+
const googleOptions = parseGoogleEmbedProviderOptions(
|
|
918
|
+
options.providerOptions
|
|
919
|
+
);
|
|
920
|
+
const providerConfig = mapGoogleEmbedProviderOptionsToConfig(googleOptions);
|
|
921
|
+
const request = Object.keys(providerConfig).length > 0 ? {
|
|
844
922
|
...baseRequest,
|
|
845
|
-
...providerOptions,
|
|
846
923
|
config: {
|
|
847
924
|
...baseRequest.config,
|
|
848
|
-
...
|
|
925
|
+
...providerConfig
|
|
849
926
|
}
|
|
850
927
|
} : baseRequest;
|
|
851
928
|
const response = await client.models.embedContent(request);
|
|
@@ -870,6 +947,14 @@ function createGoogleGenAIEmbeddingModel(client, modelId) {
|
|
|
870
947
|
}
|
|
871
948
|
};
|
|
872
949
|
}
|
|
950
|
+
function mapGoogleEmbedProviderOptionsToConfig(options) {
|
|
951
|
+
return {
|
|
952
|
+
...options?.taskType !== void 0 ? { taskType: options.taskType } : {},
|
|
953
|
+
...options?.title !== void 0 ? { title: options.title } : {},
|
|
954
|
+
...options?.mimeType !== void 0 ? { mimeType: options.mimeType } : {},
|
|
955
|
+
...options?.autoTruncate !== void 0 ? { autoTruncate: options.autoTruncate } : {}
|
|
956
|
+
};
|
|
957
|
+
}
|
|
873
958
|
|
|
874
959
|
// src/image-model.ts
|
|
875
960
|
function createGoogleGenAIImageModel(client, modelId) {
|
|
@@ -886,13 +971,15 @@ function createGoogleGenAIImageModel(client, modelId) {
|
|
|
886
971
|
...mapSizeToImageConfig(options.size)
|
|
887
972
|
}
|
|
888
973
|
};
|
|
889
|
-
const
|
|
890
|
-
|
|
974
|
+
const googleOptions = parseGoogleImageProviderOptions(
|
|
975
|
+
options.providerOptions
|
|
976
|
+
);
|
|
977
|
+
const providerConfig = mapGoogleImageProviderOptionsToConfig(googleOptions);
|
|
978
|
+
const request = Object.keys(providerConfig).length > 0 ? {
|
|
891
979
|
...baseRequest,
|
|
892
|
-
...providerOptions,
|
|
893
980
|
config: {
|
|
894
981
|
...baseRequest.config,
|
|
895
|
-
...
|
|
982
|
+
...providerConfig
|
|
896
983
|
}
|
|
897
984
|
} : baseRequest;
|
|
898
985
|
const response = await client.models.generateImages(request);
|
|
@@ -909,6 +996,26 @@ function createGoogleGenAIImageModel(client, modelId) {
|
|
|
909
996
|
}
|
|
910
997
|
};
|
|
911
998
|
}
|
|
999
|
+
function mapGoogleImageProviderOptionsToConfig(options) {
|
|
1000
|
+
return {
|
|
1001
|
+
...options?.outputGcsUri !== void 0 ? { outputGcsUri: options.outputGcsUri } : {},
|
|
1002
|
+
...options?.negativePrompt !== void 0 ? { negativePrompt: options.negativePrompt } : {},
|
|
1003
|
+
...options?.aspectRatio !== void 0 ? { aspectRatio: options.aspectRatio } : {},
|
|
1004
|
+
...options?.guidanceScale !== void 0 ? { guidanceScale: options.guidanceScale } : {},
|
|
1005
|
+
...options?.seed !== void 0 ? { seed: options.seed } : {},
|
|
1006
|
+
...options?.safetyFilterLevel !== void 0 ? { safetyFilterLevel: options.safetyFilterLevel } : {},
|
|
1007
|
+
...options?.personGeneration !== void 0 ? { personGeneration: options.personGeneration } : {},
|
|
1008
|
+
...options?.includeSafetyAttributes !== void 0 ? { includeSafetyAttributes: options.includeSafetyAttributes } : {},
|
|
1009
|
+
...options?.includeRaiReason !== void 0 ? { includeRaiReason: options.includeRaiReason } : {},
|
|
1010
|
+
...options?.language !== void 0 ? { language: options.language } : {},
|
|
1011
|
+
...options?.outputMimeType !== void 0 ? { outputMimeType: options.outputMimeType } : {},
|
|
1012
|
+
...options?.outputCompressionQuality !== void 0 ? { outputCompressionQuality: options.outputCompressionQuality } : {},
|
|
1013
|
+
...options?.addWatermark !== void 0 ? { addWatermark: options.addWatermark } : {},
|
|
1014
|
+
...options?.labels !== void 0 ? { labels: options.labels } : {},
|
|
1015
|
+
...options?.imageSize !== void 0 ? { imageSize: options.imageSize } : {},
|
|
1016
|
+
...options?.enhancePrompt !== void 0 ? { enhancePrompt: options.enhancePrompt } : {}
|
|
1017
|
+
};
|
|
1018
|
+
}
|
|
912
1019
|
function mapSizeToImageConfig(size) {
|
|
913
1020
|
if (!size) {
|
|
914
1021
|
return {};
|
|
@@ -962,5 +1069,9 @@ function createGoogleGenAI(options = {}) {
|
|
|
962
1069
|
};
|
|
963
1070
|
}
|
|
964
1071
|
export {
|
|
965
|
-
createGoogleGenAI
|
|
1072
|
+
createGoogleGenAI,
|
|
1073
|
+
googleEmbedProviderOptionsSchema,
|
|
1074
|
+
googleGenerateProviderOptionsSchema,
|
|
1075
|
+
googleImageProviderOptionsSchema,
|
|
1076
|
+
googleProviderOptionsSchema
|
|
966
1077
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@core-ai/google-genai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "Google GenAI provider package for @core-ai/core-ai",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Omnifact (https://omnifact.ai)",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"test:watch": "vitest"
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
|
-
"@core-ai/core-ai": "^0.
|
|
46
|
+
"@core-ai/core-ai": "^0.6.0",
|
|
47
47
|
"@google/genai": "^1.42.0",
|
|
48
48
|
"zod-to-json-schema": "^3.25.1"
|
|
49
49
|
},
|
|
@@ -51,8 +51,9 @@
|
|
|
51
51
|
"zod": "^3.25.0 || ^4.0.0"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
|
-
"@core-ai/eslint-config": "
|
|
55
|
-
"@core-ai/
|
|
54
|
+
"@core-ai/eslint-config": "*",
|
|
55
|
+
"@core-ai/testing": "*",
|
|
56
|
+
"@core-ai/typescript-config": "*",
|
|
56
57
|
"typescript": "^5.7.3",
|
|
57
58
|
"vitest": "^3.2.4"
|
|
58
59
|
}
|