@core-ai/google-genai 0.5.1 → 0.6.1
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 +201 -83
- 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 = {
|
|
@@ -24,6 +25,18 @@ var DEFAULT_CAPABILITIES = {
|
|
|
24
25
|
}
|
|
25
26
|
};
|
|
26
27
|
var MODEL_CAPABILITIES = {
|
|
28
|
+
"gemini-3.1-pro": {
|
|
29
|
+
reasoning: {
|
|
30
|
+
thinkingParam: "thinkingLevel",
|
|
31
|
+
canDisableThinking: false
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"gemini-3.1-flash-lite-preview": {
|
|
35
|
+
reasoning: {
|
|
36
|
+
thinkingParam: "thinkingLevel",
|
|
37
|
+
canDisableThinking: false
|
|
38
|
+
}
|
|
39
|
+
},
|
|
27
40
|
"gemini-3-pro": {
|
|
28
41
|
reasoning: {
|
|
29
42
|
thinkingParam: "thinkingLevel",
|
|
@@ -83,6 +96,67 @@ function asObject(value) {
|
|
|
83
96
|
return {};
|
|
84
97
|
}
|
|
85
98
|
|
|
99
|
+
// src/provider-options.ts
|
|
100
|
+
import { z } from "zod";
|
|
101
|
+
var googleGenerateProviderOptionsSchema = z.object({
|
|
102
|
+
stopSequences: z.array(z.string()).optional(),
|
|
103
|
+
frequencyPenalty: z.number().optional(),
|
|
104
|
+
presencePenalty: z.number().optional(),
|
|
105
|
+
seed: z.number().int().optional(),
|
|
106
|
+
topK: z.number().int().optional()
|
|
107
|
+
}).strict();
|
|
108
|
+
var googleEmbedProviderOptionsSchema = z.object({
|
|
109
|
+
taskType: z.string().optional(),
|
|
110
|
+
title: z.string().optional(),
|
|
111
|
+
mimeType: z.string().optional(),
|
|
112
|
+
autoTruncate: z.boolean().optional()
|
|
113
|
+
}).strict();
|
|
114
|
+
var googleImageProviderOptionsSchema = z.object({
|
|
115
|
+
outputGcsUri: z.string().optional(),
|
|
116
|
+
negativePrompt: z.string().optional(),
|
|
117
|
+
aspectRatio: z.string().optional(),
|
|
118
|
+
guidanceScale: z.number().optional(),
|
|
119
|
+
seed: z.number().int().optional(),
|
|
120
|
+
safetyFilterLevel: z.enum([
|
|
121
|
+
"BLOCK_LOW_AND_ABOVE",
|
|
122
|
+
"BLOCK_MEDIUM_AND_ABOVE",
|
|
123
|
+
"BLOCK_ONLY_HIGH",
|
|
124
|
+
"BLOCK_NONE"
|
|
125
|
+
]).optional(),
|
|
126
|
+
personGeneration: z.enum(["DONT_ALLOW", "ALLOW_ADULT", "ALLOW_ALL"]).optional(),
|
|
127
|
+
includeSafetyAttributes: z.boolean().optional(),
|
|
128
|
+
includeRaiReason: z.boolean().optional(),
|
|
129
|
+
language: z.string().optional(),
|
|
130
|
+
outputMimeType: z.string().optional(),
|
|
131
|
+
outputCompressionQuality: z.number().int().min(0).max(100).optional(),
|
|
132
|
+
addWatermark: z.boolean().optional(),
|
|
133
|
+
labels: z.record(z.string(), z.string()).optional(),
|
|
134
|
+
imageSize: z.string().optional(),
|
|
135
|
+
enhancePrompt: z.boolean().optional()
|
|
136
|
+
}).strict();
|
|
137
|
+
function parseGoogleGenerateProviderOptions(providerOptions) {
|
|
138
|
+
const rawOptions = providerOptions?.google;
|
|
139
|
+
if (rawOptions === void 0) {
|
|
140
|
+
return void 0;
|
|
141
|
+
}
|
|
142
|
+
return googleGenerateProviderOptionsSchema.parse(rawOptions);
|
|
143
|
+
}
|
|
144
|
+
function parseGoogleEmbedProviderOptions(providerOptions) {
|
|
145
|
+
const rawOptions = providerOptions?.google;
|
|
146
|
+
if (rawOptions === void 0) {
|
|
147
|
+
return void 0;
|
|
148
|
+
}
|
|
149
|
+
return googleEmbedProviderOptionsSchema.parse(rawOptions);
|
|
150
|
+
}
|
|
151
|
+
function parseGoogleImageProviderOptions(providerOptions) {
|
|
152
|
+
const rawOptions = providerOptions?.google;
|
|
153
|
+
if (rawOptions === void 0) {
|
|
154
|
+
return void 0;
|
|
155
|
+
}
|
|
156
|
+
return googleImageProviderOptionsSchema.parse(rawOptions);
|
|
157
|
+
}
|
|
158
|
+
var googleProviderOptionsSchema = googleGenerateProviderOptionsSchema;
|
|
159
|
+
|
|
86
160
|
// src/chat-adapter.ts
|
|
87
161
|
var DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME = "core_ai_generate_object";
|
|
88
162
|
var DEFAULT_STRUCTURED_OUTPUT_TOOL_DESCRIPTION = "Return a JSON object that matches the requested schema.";
|
|
@@ -121,13 +195,19 @@ function convertMessages(messages) {
|
|
|
121
195
|
});
|
|
122
196
|
continue;
|
|
123
197
|
}
|
|
198
|
+
const googleMeta = getProviderMetadata(
|
|
199
|
+
part.providerMetadata,
|
|
200
|
+
"google"
|
|
201
|
+
);
|
|
202
|
+
if (part.text.length === 0) {
|
|
203
|
+
continue;
|
|
204
|
+
}
|
|
124
205
|
const thoughtPart = {
|
|
125
206
|
text: part.text,
|
|
126
207
|
thought: true
|
|
127
208
|
};
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
thoughtPart["thoughtSignature"] = thoughtSignature;
|
|
209
|
+
if (typeof googleMeta?.thoughtSignature === "string") {
|
|
210
|
+
thoughtPart["thoughtSignature"] = googleMeta.thoughtSignature;
|
|
131
211
|
}
|
|
132
212
|
assistantParts.push(thoughtPart);
|
|
133
213
|
}
|
|
@@ -239,11 +319,7 @@ function convertToolChoice(choice) {
|
|
|
239
319
|
};
|
|
240
320
|
}
|
|
241
321
|
function getStructuredOutputToolName(options) {
|
|
242
|
-
|
|
243
|
-
if (trimmedName && trimmedName.length > 0) {
|
|
244
|
-
return trimmedName;
|
|
245
|
-
}
|
|
246
|
-
return DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME;
|
|
322
|
+
return options.schemaName?.trim() || DEFAULT_STRUCTURED_OUTPUT_TOOL_NAME;
|
|
247
323
|
}
|
|
248
324
|
function createStructuredOutputOptions(options) {
|
|
249
325
|
const toolName = getStructuredOutputToolName(options);
|
|
@@ -261,7 +337,9 @@ function createStructuredOutputOptions(options) {
|
|
|
261
337
|
type: "tool",
|
|
262
338
|
toolName
|
|
263
339
|
},
|
|
264
|
-
|
|
340
|
+
temperature: options.temperature,
|
|
341
|
+
maxTokens: options.maxTokens,
|
|
342
|
+
topP: options.topP,
|
|
265
343
|
providerOptions: options.providerOptions,
|
|
266
344
|
signal: options.signal
|
|
267
345
|
};
|
|
@@ -292,35 +370,39 @@ function inferMimeTypeFromUrl(url) {
|
|
|
292
370
|
return "application/octet-stream";
|
|
293
371
|
}
|
|
294
372
|
function createGenerateRequest(modelId, options) {
|
|
373
|
+
const googleOptions = parseGoogleGenerateProviderOptions(
|
|
374
|
+
options.providerOptions
|
|
375
|
+
);
|
|
295
376
|
const convertedMessages = convertMessages(options.messages);
|
|
296
|
-
const
|
|
377
|
+
const requestConfig = {
|
|
378
|
+
...convertedMessages.systemInstruction ? { systemInstruction: convertedMessages.systemInstruction } : {},
|
|
379
|
+
...options.tools && Object.keys(options.tools).length > 0 ? { tools: convertTools(options.tools) } : {},
|
|
380
|
+
...options.toolChoice ? { toolConfig: convertToolChoice(options.toolChoice) } : {},
|
|
381
|
+
...mapSamplingToConfig(options),
|
|
382
|
+
...mapReasoningToConfig(modelId, options, googleOptions),
|
|
383
|
+
...mapGoogleProviderOptionsToConfig(googleOptions),
|
|
384
|
+
...options.signal ? { abortSignal: options.signal } : {}
|
|
385
|
+
};
|
|
386
|
+
return {
|
|
297
387
|
model: modelId,
|
|
298
388
|
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
|
-
}
|
|
389
|
+
config: requestConfig
|
|
311
390
|
};
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
return baseRequest;
|
|
315
|
-
}
|
|
316
|
-
const providerConfig = asObject(providerOptions["config"]);
|
|
391
|
+
}
|
|
392
|
+
function mapSamplingToConfig(options) {
|
|
317
393
|
return {
|
|
318
|
-
...
|
|
319
|
-
...
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
394
|
+
...options.temperature !== void 0 ? { temperature: options.temperature } : {},
|
|
395
|
+
...options.maxTokens !== void 0 ? { maxOutputTokens: options.maxTokens } : {},
|
|
396
|
+
...options.topP !== void 0 ? { topP: options.topP } : {}
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
function mapGoogleProviderOptionsToConfig(options) {
|
|
400
|
+
return {
|
|
401
|
+
...options?.stopSequences ? { stopSequences: options.stopSequences } : {},
|
|
402
|
+
...options?.frequencyPenalty !== void 0 ? { frequencyPenalty: options.frequencyPenalty } : {},
|
|
403
|
+
...options?.presencePenalty !== void 0 ? { presencePenalty: options.presencePenalty } : {},
|
|
404
|
+
...options?.seed !== void 0 ? { seed: options.seed } : {},
|
|
405
|
+
...options?.topK !== void 0 ? { topK: options.topK } : {}
|
|
324
406
|
};
|
|
325
407
|
}
|
|
326
408
|
function mapGenerateResponse(response) {
|
|
@@ -333,16 +415,6 @@ function mapGenerateResponse(response) {
|
|
|
333
415
|
const finishReason = mapFinishReason(
|
|
334
416
|
response.candidates?.[0]?.finishReason ?? void 0
|
|
335
417
|
);
|
|
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
418
|
return {
|
|
347
419
|
parts,
|
|
348
420
|
content: content.length > 0 ? content : null,
|
|
@@ -385,6 +457,16 @@ async function* transformStream(stream) {
|
|
|
385
457
|
},
|
|
386
458
|
outputTokenDetails: {}
|
|
387
459
|
};
|
|
460
|
+
const closeReasoning = () => {
|
|
461
|
+
if (!reasoningOpen) {
|
|
462
|
+
return null;
|
|
463
|
+
}
|
|
464
|
+
reasoningOpen = false;
|
|
465
|
+
return {
|
|
466
|
+
type: "reasoning-end",
|
|
467
|
+
providerMetadata: { google: {} }
|
|
468
|
+
};
|
|
469
|
+
};
|
|
388
470
|
for await (const chunk of stream) {
|
|
389
471
|
usage = mapUsage(chunk, usage);
|
|
390
472
|
const reasoningDeltas = extractReasoningDeltas(chunk);
|
|
@@ -403,11 +485,9 @@ async function* transformStream(stream) {
|
|
|
403
485
|
}
|
|
404
486
|
}
|
|
405
487
|
if (chunk.text) {
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
yield
|
|
409
|
-
type: "reasoning-end"
|
|
410
|
-
};
|
|
488
|
+
const reasoningEnd2 = closeReasoning();
|
|
489
|
+
if (reasoningEnd2) {
|
|
490
|
+
yield reasoningEnd2;
|
|
411
491
|
}
|
|
412
492
|
yield {
|
|
413
493
|
type: "text-delta",
|
|
@@ -416,11 +496,9 @@ async function* transformStream(stream) {
|
|
|
416
496
|
}
|
|
417
497
|
const functionCalls = chunk.functionCalls ?? [];
|
|
418
498
|
if (functionCalls.length > 0) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
yield
|
|
422
|
-
type: "reasoning-end"
|
|
423
|
-
};
|
|
499
|
+
const reasoningEnd2 = closeReasoning();
|
|
500
|
+
if (reasoningEnd2) {
|
|
501
|
+
yield reasoningEnd2;
|
|
424
502
|
}
|
|
425
503
|
sawToolCalls = true;
|
|
426
504
|
for (const [index, functionCall] of functionCalls.entries()) {
|
|
@@ -464,10 +542,9 @@ async function* transformStream(stream) {
|
|
|
464
542
|
finishReason = candidateFinishReason;
|
|
465
543
|
}
|
|
466
544
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
};
|
|
545
|
+
const reasoningEnd = closeReasoning();
|
|
546
|
+
if (reasoningEnd) {
|
|
547
|
+
yield reasoningEnd;
|
|
471
548
|
}
|
|
472
549
|
for (const toolCall of bufferedToolCalls.values()) {
|
|
473
550
|
yield {
|
|
@@ -484,16 +561,11 @@ async function* transformStream(stream) {
|
|
|
484
561
|
usage
|
|
485
562
|
};
|
|
486
563
|
}
|
|
487
|
-
function mapReasoningToConfig(modelId, options) {
|
|
564
|
+
function mapReasoningToConfig(modelId, options, _googleProviderOptions) {
|
|
488
565
|
if (!options.reasoning) {
|
|
489
566
|
return {};
|
|
490
567
|
}
|
|
491
568
|
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
569
|
if (capabilities.reasoning.thinkingParam === "thinkingLevel") {
|
|
498
570
|
return {
|
|
499
571
|
thinkingConfig: {
|
|
@@ -523,11 +595,11 @@ function extractAssistantParts(response) {
|
|
|
523
595
|
parts.push({
|
|
524
596
|
type: "reasoning",
|
|
525
597
|
text: thoughtText,
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
thoughtSignature
|
|
598
|
+
providerMetadata: {
|
|
599
|
+
google: {
|
|
600
|
+
...thoughtSignature ? { thoughtSignature } : {}
|
|
529
601
|
}
|
|
530
|
-
}
|
|
602
|
+
}
|
|
531
603
|
});
|
|
532
604
|
continue;
|
|
533
605
|
}
|
|
@@ -637,8 +709,10 @@ function createGoogleGenAIChatModel(client, modelId) {
|
|
|
637
709
|
}
|
|
638
710
|
async function streamChat(options) {
|
|
639
711
|
const request = createGenerateRequest(modelId, options);
|
|
640
|
-
|
|
641
|
-
|
|
712
|
+
return createChatStream(
|
|
713
|
+
async () => transformStream(await callGenerateContentStreamApi(request)),
|
|
714
|
+
{ signal: options.signal }
|
|
715
|
+
);
|
|
642
716
|
}
|
|
643
717
|
return {
|
|
644
718
|
provider,
|
|
@@ -665,13 +739,16 @@ function createGoogleGenAIChatModel(client, modelId) {
|
|
|
665
739
|
const structuredOptions = createStructuredOutputOptions(options);
|
|
666
740
|
const stream = await streamChat(structuredOptions);
|
|
667
741
|
const toolName = getStructuredOutputToolName(options);
|
|
668
|
-
return
|
|
742
|
+
return createObjectStream(
|
|
669
743
|
transformStructuredOutputStream(
|
|
670
744
|
stream,
|
|
671
745
|
options.schema,
|
|
672
746
|
provider,
|
|
673
747
|
toolName
|
|
674
|
-
)
|
|
748
|
+
),
|
|
749
|
+
{
|
|
750
|
+
signal: options.signal
|
|
751
|
+
}
|
|
675
752
|
);
|
|
676
753
|
}
|
|
677
754
|
};
|
|
@@ -786,7 +863,12 @@ function validateStructuredToolArguments(schema, toolArguments, provider) {
|
|
|
786
863
|
}
|
|
787
864
|
function parseAndValidateStructuredPayload(schema, rawPayload, provider) {
|
|
788
865
|
const parsedPayload = parseJson(rawPayload, provider);
|
|
789
|
-
return validateStructuredObject(
|
|
866
|
+
return validateStructuredObject(
|
|
867
|
+
schema,
|
|
868
|
+
parsedPayload,
|
|
869
|
+
provider,
|
|
870
|
+
rawPayload
|
|
871
|
+
);
|
|
790
872
|
}
|
|
791
873
|
function parseJson(rawOutput, provider) {
|
|
792
874
|
try {
|
|
@@ -839,13 +921,15 @@ function createGoogleGenAIEmbeddingModel(client, modelId) {
|
|
|
839
921
|
}
|
|
840
922
|
} : {}
|
|
841
923
|
};
|
|
842
|
-
const
|
|
843
|
-
|
|
924
|
+
const googleOptions = parseGoogleEmbedProviderOptions(
|
|
925
|
+
options.providerOptions
|
|
926
|
+
);
|
|
927
|
+
const providerConfig = mapGoogleEmbedProviderOptionsToConfig(googleOptions);
|
|
928
|
+
const request = Object.keys(providerConfig).length > 0 ? {
|
|
844
929
|
...baseRequest,
|
|
845
|
-
...providerOptions,
|
|
846
930
|
config: {
|
|
847
931
|
...baseRequest.config,
|
|
848
|
-
...
|
|
932
|
+
...providerConfig
|
|
849
933
|
}
|
|
850
934
|
} : baseRequest;
|
|
851
935
|
const response = await client.models.embedContent(request);
|
|
@@ -870,6 +954,14 @@ function createGoogleGenAIEmbeddingModel(client, modelId) {
|
|
|
870
954
|
}
|
|
871
955
|
};
|
|
872
956
|
}
|
|
957
|
+
function mapGoogleEmbedProviderOptionsToConfig(options) {
|
|
958
|
+
return {
|
|
959
|
+
...options?.taskType !== void 0 ? { taskType: options.taskType } : {},
|
|
960
|
+
...options?.title !== void 0 ? { title: options.title } : {},
|
|
961
|
+
...options?.mimeType !== void 0 ? { mimeType: options.mimeType } : {},
|
|
962
|
+
...options?.autoTruncate !== void 0 ? { autoTruncate: options.autoTruncate } : {}
|
|
963
|
+
};
|
|
964
|
+
}
|
|
873
965
|
|
|
874
966
|
// src/image-model.ts
|
|
875
967
|
function createGoogleGenAIImageModel(client, modelId) {
|
|
@@ -886,13 +978,15 @@ function createGoogleGenAIImageModel(client, modelId) {
|
|
|
886
978
|
...mapSizeToImageConfig(options.size)
|
|
887
979
|
}
|
|
888
980
|
};
|
|
889
|
-
const
|
|
890
|
-
|
|
981
|
+
const googleOptions = parseGoogleImageProviderOptions(
|
|
982
|
+
options.providerOptions
|
|
983
|
+
);
|
|
984
|
+
const providerConfig = mapGoogleImageProviderOptionsToConfig(googleOptions);
|
|
985
|
+
const request = Object.keys(providerConfig).length > 0 ? {
|
|
891
986
|
...baseRequest,
|
|
892
|
-
...providerOptions,
|
|
893
987
|
config: {
|
|
894
988
|
...baseRequest.config,
|
|
895
|
-
...
|
|
989
|
+
...providerConfig
|
|
896
990
|
}
|
|
897
991
|
} : baseRequest;
|
|
898
992
|
const response = await client.models.generateImages(request);
|
|
@@ -909,6 +1003,26 @@ function createGoogleGenAIImageModel(client, modelId) {
|
|
|
909
1003
|
}
|
|
910
1004
|
};
|
|
911
1005
|
}
|
|
1006
|
+
function mapGoogleImageProviderOptionsToConfig(options) {
|
|
1007
|
+
return {
|
|
1008
|
+
...options?.outputGcsUri !== void 0 ? { outputGcsUri: options.outputGcsUri } : {},
|
|
1009
|
+
...options?.negativePrompt !== void 0 ? { negativePrompt: options.negativePrompt } : {},
|
|
1010
|
+
...options?.aspectRatio !== void 0 ? { aspectRatio: options.aspectRatio } : {},
|
|
1011
|
+
...options?.guidanceScale !== void 0 ? { guidanceScale: options.guidanceScale } : {},
|
|
1012
|
+
...options?.seed !== void 0 ? { seed: options.seed } : {},
|
|
1013
|
+
...options?.safetyFilterLevel !== void 0 ? { safetyFilterLevel: options.safetyFilterLevel } : {},
|
|
1014
|
+
...options?.personGeneration !== void 0 ? { personGeneration: options.personGeneration } : {},
|
|
1015
|
+
...options?.includeSafetyAttributes !== void 0 ? { includeSafetyAttributes: options.includeSafetyAttributes } : {},
|
|
1016
|
+
...options?.includeRaiReason !== void 0 ? { includeRaiReason: options.includeRaiReason } : {},
|
|
1017
|
+
...options?.language !== void 0 ? { language: options.language } : {},
|
|
1018
|
+
...options?.outputMimeType !== void 0 ? { outputMimeType: options.outputMimeType } : {},
|
|
1019
|
+
...options?.outputCompressionQuality !== void 0 ? { outputCompressionQuality: options.outputCompressionQuality } : {},
|
|
1020
|
+
...options?.addWatermark !== void 0 ? { addWatermark: options.addWatermark } : {},
|
|
1021
|
+
...options?.labels !== void 0 ? { labels: options.labels } : {},
|
|
1022
|
+
...options?.imageSize !== void 0 ? { imageSize: options.imageSize } : {},
|
|
1023
|
+
...options?.enhancePrompt !== void 0 ? { enhancePrompt: options.enhancePrompt } : {}
|
|
1024
|
+
};
|
|
1025
|
+
}
|
|
912
1026
|
function mapSizeToImageConfig(size) {
|
|
913
1027
|
if (!size) {
|
|
914
1028
|
return {};
|
|
@@ -962,5 +1076,9 @@ function createGoogleGenAI(options = {}) {
|
|
|
962
1076
|
};
|
|
963
1077
|
}
|
|
964
1078
|
export {
|
|
965
|
-
createGoogleGenAI
|
|
1079
|
+
createGoogleGenAI,
|
|
1080
|
+
googleEmbedProviderOptionsSchema,
|
|
1081
|
+
googleGenerateProviderOptionsSchema,
|
|
1082
|
+
googleImageProviderOptionsSchema,
|
|
1083
|
+
googleProviderOptionsSchema
|
|
966
1084
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@core-ai/google-genai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
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.1",
|
|
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
|
}
|