@elizaos/plugin-google-genai 2.0.3-beta.5 → 2.0.3-beta.7

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.
@@ -0,0 +1,795 @@
1
+ // index.ts
2
+ import { logger as logger6, ModelType as ModelType3 } from "@elizaos/core";
3
+ import { GoogleGenAI as GoogleGenAI3 } from "@google/genai";
4
+
5
+ // init.ts
6
+ import { logger as logger2 } from "@elizaos/core";
7
+ import { GoogleGenAI as GoogleGenAI2 } from "@google/genai";
8
+
9
+ // utils/config.ts
10
+ import { logger } from "@elizaos/core";
11
+ import { GoogleGenAI, HarmBlockThreshold, HarmCategory } from "@google/genai";
12
+ function getEnvValue(key) {
13
+ if (typeof process === "undefined") {
14
+ return;
15
+ }
16
+ const value = process.env[key];
17
+ return normalizeSettingValue(value);
18
+ }
19
+ function normalizeSettingValue(value) {
20
+ if (value === undefined || value === null) {
21
+ return;
22
+ }
23
+ const trimmed = String(value).trim();
24
+ return trimmed.length > 0 ? trimmed : undefined;
25
+ }
26
+ function getSetting(runtime, key, defaultValue) {
27
+ const runtimeValue = normalizeSettingValue(runtime.getSetting(key));
28
+ if (runtimeValue !== undefined) {
29
+ return runtimeValue;
30
+ }
31
+ return getEnvValue(key) ?? defaultValue;
32
+ }
33
+ function getApiKey(runtime) {
34
+ return getSetting(runtime, "GOOGLE_GENERATIVE_AI_API_KEY");
35
+ }
36
+ function getSmallModel(runtime) {
37
+ return getSetting(runtime, "GOOGLE_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", "gemini-2.0-flash-001") ?? "gemini-2.0-flash-001";
38
+ }
39
+ function getNanoModel(runtime) {
40
+ return getSetting(runtime, "GOOGLE_NANO_MODEL") ?? getSetting(runtime, "NANO_MODEL") ?? getSmallModel(runtime);
41
+ }
42
+ function getMediumModel(runtime) {
43
+ return getSetting(runtime, "GOOGLE_MEDIUM_MODEL") ?? getSetting(runtime, "MEDIUM_MODEL") ?? getSmallModel(runtime);
44
+ }
45
+ function getLargeModel(runtime) {
46
+ return getSetting(runtime, "GOOGLE_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", "gemini-2.5-pro-preview-03-25") ?? "gemini-2.5-pro-preview-03-25";
47
+ }
48
+ function getMegaModel(runtime) {
49
+ return getSetting(runtime, "GOOGLE_MEGA_MODEL") ?? getSetting(runtime, "MEGA_MODEL") ?? getLargeModel(runtime);
50
+ }
51
+ function getResponseHandlerModel(runtime) {
52
+ return getSetting(runtime, "GOOGLE_RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "GOOGLE_SHOULD_RESPOND_MODEL") ?? getSetting(runtime, "RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "SHOULD_RESPOND_MODEL") ?? getNanoModel(runtime);
53
+ }
54
+ function getActionPlannerModel(runtime) {
55
+ return getSetting(runtime, "GOOGLE_ACTION_PLANNER_MODEL") ?? getSetting(runtime, "GOOGLE_PLANNER_MODEL") ?? getSetting(runtime, "ACTION_PLANNER_MODEL") ?? getSetting(runtime, "PLANNER_MODEL") ?? getMediumModel(runtime);
56
+ }
57
+ function getImageModel(runtime) {
58
+ return getSetting(runtime, "GOOGLE_IMAGE_MODEL") ?? getSetting(runtime, "IMAGE_MODEL", "gemini-2.5-pro-preview-03-25") ?? "gemini-2.5-pro-preview-03-25";
59
+ }
60
+ function getEmbeddingModel(runtime) {
61
+ return getSetting(runtime, "GOOGLE_EMBEDDING_MODEL", "text-embedding-004") ?? "text-embedding-004";
62
+ }
63
+ function createGoogleGenAI(runtime) {
64
+ const apiKey = getApiKey(runtime);
65
+ if (!apiKey) {
66
+ logger.error("Google Generative AI API Key is missing");
67
+ return null;
68
+ }
69
+ return new GoogleGenAI({ apiKey });
70
+ }
71
+ function getSafetySettings() {
72
+ return [
73
+ {
74
+ category: HarmCategory.HARM_CATEGORY_HARASSMENT,
75
+ threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
76
+ },
77
+ {
78
+ category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
79
+ threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
80
+ },
81
+ {
82
+ category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
83
+ threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
84
+ },
85
+ {
86
+ category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
87
+ threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE
88
+ }
89
+ ];
90
+ }
91
+
92
+ // init.ts
93
+ function initializeGoogleGenAI(_config, runtime) {
94
+ (async () => {
95
+ try {
96
+ const apiKey = getApiKey(runtime);
97
+ if (!apiKey) {
98
+ logger2.warn("GOOGLE_GENERATIVE_AI_API_KEY is not set");
99
+ return;
100
+ }
101
+ const genAI = new GoogleGenAI2({ apiKey });
102
+ const modelList = await genAI.models.list();
103
+ const models = [];
104
+ for await (const model of modelList) {
105
+ models.push(model);
106
+ }
107
+ logger2.log(`Google AI API key validated. Available models: ${models.length}`);
108
+ } catch (error) {
109
+ logger2.warn(`Google AI configuration error: ${error instanceof Error ? error.message : String(error)}`);
110
+ }
111
+ })();
112
+ }
113
+
114
+ // models/embedding.ts
115
+ import * as ElizaCore from "@elizaos/core";
116
+ import { logger as logger3 } from "@elizaos/core";
117
+
118
+ // utils/events.ts
119
+ var MODEL_USED_EVENT = "MODEL_USED";
120
+ function emitModelUsageEvent(runtime, type, _prompt, usage) {
121
+ runtime.emitEvent(MODEL_USED_EVENT, {
122
+ runtime,
123
+ source: "plugin-google-genai",
124
+ type,
125
+ tokens: {
126
+ prompt: usage.promptTokens,
127
+ completion: usage.completionTokens,
128
+ total: usage.totalTokens
129
+ }
130
+ });
131
+ }
132
+
133
+ // utils/tokenization.ts
134
+ async function countTokens(text) {
135
+ return Math.ceil(text.length / 4);
136
+ }
137
+
138
+ // models/embedding.ts
139
+ var TEXT_EMBEDDING_MODEL_TYPE = ElizaCore.ModelType?.TEXT_EMBEDDING ?? "TEXT_EMBEDDING";
140
+ function createInitProbeVector() {
141
+ const vector = Array(768).fill(0);
142
+ vector[0] = 0.1;
143
+ return vector;
144
+ }
145
+ function extractText(params) {
146
+ if (params === null) {
147
+ return null;
148
+ }
149
+ if (typeof params === "string") {
150
+ return params;
151
+ }
152
+ if (typeof params === "object" && typeof params.text === "string") {
153
+ return params.text;
154
+ }
155
+ throw new Error("Invalid input format for embedding: expected string or { text: string }");
156
+ }
157
+ async function handleTextEmbedding(runtime, params) {
158
+ if (params === null) {
159
+ return createInitProbeVector();
160
+ }
161
+ let text = extractText(params);
162
+ if (text === null) {
163
+ return createInitProbeVector();
164
+ }
165
+ if (!text.trim()) {
166
+ throw new Error("Cannot generate embedding for empty text");
167
+ }
168
+ const genAI = createGoogleGenAI(runtime);
169
+ if (!genAI) {
170
+ throw new Error("Google Generative AI client not initialized");
171
+ }
172
+ const embeddingModelName = getEmbeddingModel(runtime);
173
+ logger3.debug(`[TEXT_EMBEDDING] Using model: ${embeddingModelName}`);
174
+ const maxChars = 8192 * 4;
175
+ if (text.length > maxChars) {
176
+ logger3.warn(`[Google GenAI] Embedding input too long (~${Math.ceil(text.length / 4)} tokens), truncating to ~8192 tokens`);
177
+ text = text.slice(0, maxChars);
178
+ }
179
+ try {
180
+ const response = await genAI.models.embedContent({
181
+ model: embeddingModelName,
182
+ contents: text
183
+ });
184
+ const embedding = response.embeddings?.[0]?.values || [];
185
+ if (embedding.length === 0) {
186
+ throw new Error("Google GenAI API returned no embedding");
187
+ }
188
+ const promptTokens = await countTokens(text);
189
+ emitModelUsageEvent(runtime, TEXT_EMBEDDING_MODEL_TYPE, text, {
190
+ promptTokens,
191
+ completionTokens: 0,
192
+ totalTokens: promptTokens
193
+ });
194
+ logger3.log(`Got embedding with length ${embedding.length}`);
195
+ return embedding;
196
+ } catch (error) {
197
+ logger3.error(`Error generating embedding: ${error instanceof Error ? error.message : String(error)}`);
198
+ throw error instanceof Error ? error : new Error(String(error));
199
+ }
200
+ }
201
+ // models/image.ts
202
+ import { logger as logger4, recordLlmCall } from "@elizaos/core";
203
+ async function handleImageDescription(runtime, params) {
204
+ const genAI = createGoogleGenAI(runtime);
205
+ if (!genAI) {
206
+ throw new Error("Google Generative AI client not initialized");
207
+ }
208
+ let imageUrl;
209
+ let promptText;
210
+ const modelName = getImageModel(runtime);
211
+ logger4.log(`[IMAGE_DESCRIPTION] Using model: ${modelName}`);
212
+ if (typeof params === "string") {
213
+ imageUrl = params;
214
+ promptText = "Please analyze this image and provide a title and detailed description.";
215
+ } else {
216
+ imageUrl = params.imageUrl;
217
+ promptText = params.prompt || "Please analyze this image and provide a title and detailed description.";
218
+ }
219
+ try {
220
+ const imageResponse = await fetch(imageUrl);
221
+ if (!imageResponse.ok) {
222
+ throw new Error(`Failed to fetch image: ${imageResponse.statusText}`);
223
+ }
224
+ const imageData = await imageResponse.arrayBuffer();
225
+ const base64Image = Buffer.from(imageData).toString("base64");
226
+ const contentType = imageResponse.headers.get("content-type") || "image/jpeg";
227
+ const details = {
228
+ model: modelName,
229
+ systemPrompt: "",
230
+ userPrompt: promptText,
231
+ temperature: 0.7,
232
+ maxTokens: 8192,
233
+ purpose: "external_llm",
234
+ actionType: "google-genai.IMAGE_DESCRIPTION.generateContent"
235
+ };
236
+ const response = await recordLlmCall(runtime, details, async () => {
237
+ const result = await genAI.models.generateContent({
238
+ model: modelName,
239
+ contents: [
240
+ {
241
+ role: "user",
242
+ parts: [
243
+ { text: promptText },
244
+ {
245
+ inlineData: {
246
+ mimeType: contentType,
247
+ data: base64Image
248
+ }
249
+ }
250
+ ]
251
+ }
252
+ ],
253
+ config: {
254
+ temperature: 0.7,
255
+ topK: 40,
256
+ topP: 0.95,
257
+ maxOutputTokens: 8192,
258
+ safetySettings: getSafetySettings()
259
+ }
260
+ });
261
+ const responseText2 = result.text || "";
262
+ details.response = responseText2;
263
+ details.promptTokens = await countTokens(promptText);
264
+ details.completionTokens = await countTokens(responseText2);
265
+ return result;
266
+ });
267
+ const responseText = response.text || "";
268
+ try {
269
+ const jsonResponse = JSON.parse(responseText);
270
+ if (typeof jsonResponse.title === "string" && typeof jsonResponse.description === "string") {
271
+ return {
272
+ title: jsonResponse.title,
273
+ description: jsonResponse.description
274
+ };
275
+ }
276
+ } catch {}
277
+ const titleMatch = responseText.match(/title[:\s]+(.+?)(?:\n|$)/i);
278
+ const title = titleMatch?.[1]?.trim() || "Image Analysis";
279
+ const description = titleMatch ? responseText.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim() : responseText.trim();
280
+ return { title, description };
281
+ } catch (error) {
282
+ const message = error instanceof Error ? error.message : String(error);
283
+ logger4.error(`Error analyzing image: ${message}`);
284
+ return {
285
+ title: "Failed to analyze image",
286
+ description: `Error: ${message}`
287
+ };
288
+ }
289
+ }
290
+ // models/text.ts
291
+ import {
292
+ buildCanonicalSystemPrompt,
293
+ logger as logger5,
294
+ ModelType as ModelType2,
295
+ recordLlmCall as recordLlmCall2,
296
+ renderChatMessagesForPrompt,
297
+ resolveEffectiveSystemPrompt
298
+ } from "@elizaos/core";
299
+ var TEXT_NANO_MODEL_TYPE = ModelType2.TEXT_NANO;
300
+ var TEXT_MEDIUM_MODEL_TYPE = ModelType2.TEXT_MEDIUM;
301
+ var TEXT_SMALL_MODEL_TYPE = ModelType2.TEXT_SMALL;
302
+ var TEXT_LARGE_MODEL_TYPE = ModelType2.TEXT_LARGE;
303
+ var TEXT_MEGA_MODEL_TYPE = ModelType2.TEXT_MEGA;
304
+ var RESPONSE_HANDLER_MODEL_TYPE = ModelType2.RESPONSE_HANDLER;
305
+ var ACTION_PLANNER_MODEL_TYPE = ModelType2.ACTION_PLANNER;
306
+ function normalizeToolsForGoogle(tools) {
307
+ if (!tools)
308
+ return;
309
+ if (Array.isArray(tools) && tools.length > 0 && typeof tools[0] === "object" && tools[0] !== null && "functionDeclarations" in tools[0]) {
310
+ return tools;
311
+ }
312
+ const flat = Array.isArray(tools) ? tools : Object.entries(tools).map(([name, value]) => ({ name, ...value }));
313
+ const declarations = [];
314
+ for (const tool of flat) {
315
+ const name = tool.name ?? tool.function?.name;
316
+ if (!name) {
317
+ throw new Error("[GoogleGenAI] Tool definition is missing a name.");
318
+ }
319
+ const description = tool.description ?? tool.function?.description;
320
+ const parameters = tool.parameters ?? tool.inputSchema ?? tool.function?.parameters ?? {
321
+ type: "object",
322
+ properties: {}
323
+ };
324
+ declarations.push({
325
+ name,
326
+ ...description ? { description } : {},
327
+ parameters
328
+ });
329
+ }
330
+ return declarations.length > 0 ? [{ functionDeclarations: declarations }] : undefined;
331
+ }
332
+ function normalizeToolConfigForGoogle(toolChoice) {
333
+ if (!toolChoice)
334
+ return;
335
+ if (toolChoice === "auto") {
336
+ return { functionCallingConfig: { mode: "AUTO" } };
337
+ }
338
+ if (toolChoice === "required") {
339
+ return { functionCallingConfig: { mode: "ANY" } };
340
+ }
341
+ if (toolChoice === "none") {
342
+ return { functionCallingConfig: { mode: "NONE" } };
343
+ }
344
+ let toolName;
345
+ if ("type" in toolChoice) {
346
+ toolName = toolChoice.type === "function" ? toolChoice.function.name : toolChoice.toolName ?? toolChoice.name;
347
+ } else {
348
+ toolName = toolChoice.name;
349
+ }
350
+ if (toolName) {
351
+ return {
352
+ functionCallingConfig: {
353
+ mode: "ANY",
354
+ allowedFunctionNames: [toolName]
355
+ }
356
+ };
357
+ }
358
+ return;
359
+ }
360
+ function resolveResponseJsonSchema(responseSchema) {
361
+ if (!responseSchema)
362
+ return;
363
+ if ("schema" in responseSchema && responseSchema.schema) {
364
+ return responseSchema.schema;
365
+ }
366
+ return responseSchema;
367
+ }
368
+ function buildPromptParts(prompt, attachments) {
369
+ const parts = [{ text: prompt }];
370
+ for (const attachment of attachments ?? []) {
371
+ if (attachment.data instanceof URL) {
372
+ parts.push({
373
+ fileData: {
374
+ mimeType: attachment.mediaType,
375
+ fileUri: attachment.data.toString()
376
+ }
377
+ });
378
+ continue;
379
+ }
380
+ if (typeof attachment.data === "string" && /^https?:\/\//i.test(attachment.data)) {
381
+ parts.push({
382
+ fileData: {
383
+ mimeType: attachment.mediaType,
384
+ fileUri: attachment.data
385
+ }
386
+ });
387
+ continue;
388
+ }
389
+ if (typeof attachment.data === "string") {
390
+ const dataUrlMatch = attachment.data.match(/^data:([^;,]+);base64,(.+)$/i);
391
+ parts.push({
392
+ inlineData: {
393
+ mimeType: dataUrlMatch?.[1] ?? attachment.mediaType,
394
+ data: dataUrlMatch?.[2] ?? attachment.data
395
+ }
396
+ });
397
+ continue;
398
+ }
399
+ parts.push({
400
+ inlineData: {
401
+ mimeType: attachment.mediaType,
402
+ data: Buffer.from(attachment.data).toString("base64")
403
+ }
404
+ });
405
+ }
406
+ return parts;
407
+ }
408
+ function resolveGoogleSystemInstruction(runtime, params) {
409
+ return resolveEffectiveSystemPrompt({
410
+ params,
411
+ fallback: buildCanonicalSystemPrompt({ character: runtime.character })
412
+ });
413
+ }
414
+ function resolveGooglePrompt(params, systemInstruction) {
415
+ return renderChatMessagesForPrompt(params.messages, {
416
+ omitDuplicateSystem: systemInstruction
417
+ }) ?? params.prompt ?? "";
418
+ }
419
+ function getModelNameForType(runtime, modelType) {
420
+ switch (modelType) {
421
+ case TEXT_NANO_MODEL_TYPE:
422
+ return getNanoModel(runtime);
423
+ case TEXT_MEDIUM_MODEL_TYPE:
424
+ return getMediumModel(runtime);
425
+ case TEXT_SMALL_MODEL_TYPE:
426
+ return getSmallModel(runtime);
427
+ case TEXT_LARGE_MODEL_TYPE:
428
+ return getLargeModel(runtime);
429
+ case TEXT_MEGA_MODEL_TYPE:
430
+ return getMegaModel(runtime);
431
+ case RESPONSE_HANDLER_MODEL_TYPE:
432
+ return getResponseHandlerModel(runtime);
433
+ case ACTION_PLANNER_MODEL_TYPE:
434
+ return getActionPlannerModel(runtime);
435
+ default:
436
+ return getLargeModel(runtime);
437
+ }
438
+ }
439
+ function buildGoogleGenerationConfig(params, systemInstruction, temperature, maxTokens, stopSequences) {
440
+ const tools = normalizeToolsForGoogle(params.tools);
441
+ const toolConfig = normalizeToolConfigForGoogle(params.toolChoice);
442
+ const responseJsonSchema = resolveResponseJsonSchema(params.responseSchema);
443
+ const baseConfig = {
444
+ temperature,
445
+ topK: 40,
446
+ topP: 0.95,
447
+ stopSequences,
448
+ safetySettings: getSafetySettings(),
449
+ ...typeof maxTokens === "number" ? { maxOutputTokens: maxTokens } : {},
450
+ ...systemInstruction && { systemInstruction },
451
+ ...tools ? { tools } : {},
452
+ ...toolConfig ? { toolConfig } : {},
453
+ ...responseJsonSchema ? {
454
+ responseMimeType: "application/json",
455
+ responseJsonSchema
456
+ } : {}
457
+ };
458
+ return baseConfig;
459
+ }
460
+ function createLlmCallDetails(modelName, modelType, prompt, systemInstruction, temperature, maxTokens, maxTokensOmitted) {
461
+ return {
462
+ model: modelName,
463
+ systemPrompt: systemInstruction ?? "",
464
+ userPrompt: prompt,
465
+ temperature,
466
+ maxTokens: maxTokens ?? 0,
467
+ maxTokensOmitted: maxTokensOmitted ? true : undefined,
468
+ purpose: "external_llm",
469
+ actionType: `google-genai.${modelType}.generateContent`
470
+ };
471
+ }
472
+ async function generateContentWithTrajectory(runtime, genAI, modelName, modelType, prompt, systemInstruction, temperature, maxTokens, maxTokensOmitted, request) {
473
+ const details = createLlmCallDetails(modelName, modelType, prompt, systemInstruction, temperature, maxTokens, maxTokensOmitted);
474
+ const response = await recordLlmCall2(runtime, details, async () => {
475
+ const result = await genAI.models.generateContent(request);
476
+ const text2 = result.text || "";
477
+ details.response = text2;
478
+ details.promptTokens = await countTokens(prompt);
479
+ details.completionTokens = await countTokens(text2);
480
+ return result;
481
+ });
482
+ const text = response.text || "";
483
+ const promptTokens = details.promptTokens ?? await countTokens(prompt);
484
+ const completionTokens = details.completionTokens ?? await countTokens(text);
485
+ emitModelUsageEvent(runtime, modelType, prompt, {
486
+ promptTokens,
487
+ completionTokens,
488
+ totalTokens: promptTokens + completionTokens
489
+ });
490
+ return text;
491
+ }
492
+ async function handleTextSmall(runtime, params) {
493
+ const { stopSequences = [], temperature = 0.7, attachments } = params;
494
+ const maxTokens = params.omitMaxTokens ? undefined : params.maxTokens ?? 8192;
495
+ const genAI = createGoogleGenAI(runtime);
496
+ if (!genAI) {
497
+ throw new Error("Google Generative AI client not initialized");
498
+ }
499
+ const modelName = getModelNameForType(runtime, TEXT_SMALL_MODEL_TYPE);
500
+ logger5.log(`[TEXT_SMALL] Using model: ${modelName}`);
501
+ try {
502
+ const systemInstruction = resolveGoogleSystemInstruction(runtime, params);
503
+ const promptText = resolveGooglePrompt(params, systemInstruction);
504
+ return await generateContentWithTrajectory(runtime, genAI, modelName, TEXT_SMALL_MODEL_TYPE, promptText, systemInstruction, temperature, maxTokens, params.omitMaxTokens, {
505
+ model: modelName,
506
+ contents: (attachments?.length ?? 0) > 0 ? [
507
+ {
508
+ role: "user",
509
+ parts: buildPromptParts(promptText, attachments)
510
+ }
511
+ ] : promptText,
512
+ config: buildGoogleGenerationConfig(params, systemInstruction, temperature, maxTokens, stopSequences)
513
+ });
514
+ } catch (error) {
515
+ logger5.error(`[TEXT_SMALL] Error: ${error instanceof Error ? error.message : String(error)}`);
516
+ throw error;
517
+ }
518
+ }
519
+ async function handleTextLarge(runtime, params) {
520
+ const { stopSequences = [], temperature = 0.7, attachments } = params;
521
+ const maxTokens = params.omitMaxTokens ? undefined : params.maxTokens ?? 8192;
522
+ const genAI = createGoogleGenAI(runtime);
523
+ if (!genAI) {
524
+ throw new Error("Google Generative AI client not initialized");
525
+ }
526
+ const modelName = getModelNameForType(runtime, TEXT_LARGE_MODEL_TYPE);
527
+ logger5.log(`[TEXT_LARGE] Using model: ${modelName}`);
528
+ try {
529
+ const systemInstruction = resolveGoogleSystemInstruction(runtime, params);
530
+ const promptText = resolveGooglePrompt(params, systemInstruction);
531
+ return await generateContentWithTrajectory(runtime, genAI, modelName, TEXT_LARGE_MODEL_TYPE, promptText, systemInstruction, temperature, maxTokens, params.omitMaxTokens, {
532
+ model: modelName,
533
+ contents: (attachments?.length ?? 0) > 0 ? [
534
+ {
535
+ role: "user",
536
+ parts: buildPromptParts(promptText, attachments)
537
+ }
538
+ ] : promptText,
539
+ config: buildGoogleGenerationConfig(params, systemInstruction, temperature, maxTokens, stopSequences)
540
+ });
541
+ } catch (error) {
542
+ logger5.error(`[TEXT_LARGE] Error: ${error instanceof Error ? error.message : String(error)}`);
543
+ throw error;
544
+ }
545
+ }
546
+ async function handleTextNano(runtime, params) {
547
+ return handleTextWithType(runtime, TEXT_NANO_MODEL_TYPE, params);
548
+ }
549
+ async function handleTextMedium(runtime, params) {
550
+ return handleTextWithType(runtime, TEXT_MEDIUM_MODEL_TYPE, params);
551
+ }
552
+ async function handleTextMega(runtime, params) {
553
+ return handleTextWithType(runtime, TEXT_MEGA_MODEL_TYPE, params);
554
+ }
555
+ async function handleResponseHandler(runtime, params) {
556
+ return handleTextWithType(runtime, RESPONSE_HANDLER_MODEL_TYPE, params);
557
+ }
558
+ async function handleActionPlanner(runtime, params) {
559
+ return handleTextWithType(runtime, ACTION_PLANNER_MODEL_TYPE, params);
560
+ }
561
+ async function handleTextWithType(runtime, modelType, params) {
562
+ const { stopSequences = [], temperature = 0.7, attachments } = params;
563
+ const maxTokens = params.omitMaxTokens ? undefined : params.maxTokens ?? 8192;
564
+ const genAI = createGoogleGenAI(runtime);
565
+ if (!genAI) {
566
+ throw new Error("Google Generative AI client not initialized");
567
+ }
568
+ const modelName = getModelNameForType(runtime, modelType);
569
+ logger5.log(`[${modelType}] Using model: ${modelName}`);
570
+ try {
571
+ const systemInstruction = resolveGoogleSystemInstruction(runtime, params);
572
+ const promptText = resolveGooglePrompt(params, systemInstruction);
573
+ return await generateContentWithTrajectory(runtime, genAI, modelName, modelType, promptText, systemInstruction, temperature, maxTokens, params.omitMaxTokens, {
574
+ model: modelName,
575
+ contents: (attachments?.length ?? 0) > 0 ? [
576
+ {
577
+ role: "user",
578
+ parts: buildPromptParts(promptText, attachments)
579
+ }
580
+ ] : promptText,
581
+ config: buildGoogleGenerationConfig(params, systemInstruction, temperature, maxTokens, stopSequences)
582
+ });
583
+ } catch (error) {
584
+ logger5.error(`[${modelType}] Error: ${error instanceof Error ? error.message : String(error)}`);
585
+ throw error;
586
+ }
587
+ }
588
+ // index.ts
589
+ var TEXT_NANO_MODEL_TYPE2 = ModelType3.TEXT_NANO;
590
+ var TEXT_MEDIUM_MODEL_TYPE2 = ModelType3.TEXT_MEDIUM;
591
+ var TEXT_SMALL_MODEL_TYPE2 = ModelType3.TEXT_SMALL;
592
+ var TEXT_LARGE_MODEL_TYPE2 = ModelType3.TEXT_LARGE;
593
+ var TEXT_EMBEDDING_MODEL_TYPE2 = ModelType3.TEXT_EMBEDDING;
594
+ var IMAGE_DESCRIPTION_MODEL_TYPE = ModelType3.IMAGE_DESCRIPTION;
595
+ var TEXT_MEGA_MODEL_TYPE2 = ModelType3.TEXT_MEGA;
596
+ var RESPONSE_HANDLER_MODEL_TYPE2 = ModelType3.RESPONSE_HANDLER;
597
+ var ACTION_PLANNER_MODEL_TYPE2 = ModelType3.ACTION_PLANNER;
598
+ var pluginTests = [
599
+ {
600
+ name: "google_genai_plugin_tests",
601
+ tests: [
602
+ {
603
+ name: "google_test_api_key_validation",
604
+ fn: async (runtime) => {
605
+ const apiKey = getApiKey(runtime);
606
+ if (!apiKey) {
607
+ throw new Error("GOOGLE_GENERATIVE_AI_API_KEY not set");
608
+ }
609
+ const genAI = new GoogleGenAI3({ apiKey });
610
+ const modelList = await genAI.models.list();
611
+ const models = [];
612
+ for await (const model of modelList) {
613
+ models.push(model);
614
+ }
615
+ logger6.log(`Available models: ${models.length}`);
616
+ }
617
+ },
618
+ {
619
+ name: "google_test_text_embedding",
620
+ fn: async (runtime) => {
621
+ try {
622
+ const embedding = await runtime.useModel(ModelType3.TEXT_EMBEDDING, {
623
+ text: "Hello, world!"
624
+ });
625
+ logger6.log(`Embedding dimension: ${embedding.length}`);
626
+ if (embedding.length === 0) {
627
+ throw new Error("Failed to generate embedding");
628
+ }
629
+ } catch (error) {
630
+ logger6.error(`Error in test_text_embedding: ${error instanceof Error ? error.message : String(error)}`);
631
+ throw error;
632
+ }
633
+ }
634
+ },
635
+ {
636
+ name: "google_test_text_small",
637
+ fn: async (runtime) => {
638
+ try {
639
+ const text = await runtime.useModel(ModelType3.TEXT_SMALL, {
640
+ prompt: "What is the nature of reality in 10 words?"
641
+ });
642
+ if (text.length === 0) {
643
+ throw new Error("Failed to generate text");
644
+ }
645
+ logger6.log("Generated with TEXT_SMALL:", text);
646
+ } catch (error) {
647
+ logger6.error(`Error in test_text_small: ${error instanceof Error ? error.message : String(error)}`);
648
+ throw error;
649
+ }
650
+ }
651
+ },
652
+ {
653
+ name: "google_test_text_large",
654
+ fn: async (runtime) => {
655
+ try {
656
+ const text = await runtime.useModel(ModelType3.TEXT_LARGE, {
657
+ prompt: "Explain quantum mechanics in simple terms."
658
+ });
659
+ if (text.length === 0) {
660
+ throw new Error("Failed to generate text");
661
+ }
662
+ logger6.log("Generated with TEXT_LARGE:", `${text.substring(0, 100)}...`);
663
+ } catch (error) {
664
+ logger6.error(`Error in test_text_large: ${error instanceof Error ? error.message : String(error)}`);
665
+ throw error;
666
+ }
667
+ }
668
+ },
669
+ {
670
+ name: "google_test_image_description",
671
+ fn: async (runtime) => {
672
+ try {
673
+ const result = await runtime.useModel(ModelType3.IMAGE_DESCRIPTION, "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Vitalik_Buterin_TechCrunch_London_2015_%28cropped%29.jpg/537px-Vitalik_Buterin_TechCrunch_London_2015_%28cropped%29.jpg");
674
+ if (result != null && typeof result === "object" && "title" in result && "description" in result) {
675
+ logger6.log("Image description:", JSON.stringify(result));
676
+ } else {
677
+ logger6.error(`Invalid image description result format: ${JSON.stringify(result)}`);
678
+ }
679
+ } catch (error) {
680
+ logger6.error(`Error in test_image_description: ${error instanceof Error ? error.message : String(error)}`);
681
+ throw error;
682
+ }
683
+ }
684
+ },
685
+ {
686
+ name: "google_test_structured_output_via_text_large",
687
+ fn: async (runtime) => {
688
+ try {
689
+ const schema = {
690
+ type: "object",
691
+ properties: {
692
+ name: { type: "string" },
693
+ age: { type: "number" },
694
+ hobbies: { type: "array", items: { type: "string" } }
695
+ },
696
+ required: ["name", "age", "hobbies"]
697
+ };
698
+ const result = await runtime.useModel(ModelType3.TEXT_LARGE, {
699
+ prompt: "Generate a person profile with name, age, and hobbies.",
700
+ responseSchema: schema
701
+ });
702
+ logger6.log("Generated structured output:", JSON.stringify(result));
703
+ if (!result) {
704
+ throw new Error("Generated structured output is empty");
705
+ }
706
+ } catch (error) {
707
+ logger6.error(`Error in test_structured_output_via_text_large: ${error instanceof Error ? error.message : String(error)}`);
708
+ throw error;
709
+ }
710
+ }
711
+ }
712
+ ]
713
+ }
714
+ ];
715
+ function getProcessEnv() {
716
+ if (typeof process === "undefined") {
717
+ return {};
718
+ }
719
+ return process.env;
720
+ }
721
+ var env = getProcessEnv();
722
+ var googleGenAIPlugin = {
723
+ name: "google-genai",
724
+ description: "Google Generative AI plugin for Gemini models",
725
+ autoEnable: {
726
+ envKeys: ["GOOGLE_API_KEY", "GOOGLE_GENERATIVE_AI_API_KEY"]
727
+ },
728
+ config: {
729
+ GOOGLE_GENERATIVE_AI_API_KEY: env.GOOGLE_GENERATIVE_AI_API_KEY ?? null,
730
+ GOOGLE_NANO_MODEL: env.GOOGLE_NANO_MODEL ?? null,
731
+ GOOGLE_MEDIUM_MODEL: env.GOOGLE_MEDIUM_MODEL ?? null,
732
+ GOOGLE_SMALL_MODEL: env.GOOGLE_SMALL_MODEL ?? null,
733
+ GOOGLE_LARGE_MODEL: env.GOOGLE_LARGE_MODEL ?? null,
734
+ GOOGLE_MEGA_MODEL: env.GOOGLE_MEGA_MODEL ?? null,
735
+ GOOGLE_RESPONSE_HANDLER_MODEL: env.GOOGLE_RESPONSE_HANDLER_MODEL ?? null,
736
+ GOOGLE_SHOULD_RESPOND_MODEL: env.GOOGLE_SHOULD_RESPOND_MODEL ?? null,
737
+ GOOGLE_ACTION_PLANNER_MODEL: env.GOOGLE_ACTION_PLANNER_MODEL ?? null,
738
+ GOOGLE_PLANNER_MODEL: env.GOOGLE_PLANNER_MODEL ?? null,
739
+ GOOGLE_IMAGE_MODEL: env.GOOGLE_IMAGE_MODEL ?? null,
740
+ GOOGLE_EMBEDDING_MODEL: env.GOOGLE_EMBEDDING_MODEL ?? null,
741
+ NANO_MODEL: env.NANO_MODEL ?? null,
742
+ MEDIUM_MODEL: env.MEDIUM_MODEL ?? null,
743
+ SMALL_MODEL: env.SMALL_MODEL ?? null,
744
+ LARGE_MODEL: env.LARGE_MODEL ?? null,
745
+ MEGA_MODEL: env.MEGA_MODEL ?? null,
746
+ RESPONSE_HANDLER_MODEL: env.RESPONSE_HANDLER_MODEL ?? null,
747
+ SHOULD_RESPOND_MODEL: env.SHOULD_RESPOND_MODEL ?? null,
748
+ ACTION_PLANNER_MODEL: env.ACTION_PLANNER_MODEL ?? null,
749
+ PLANNER_MODEL: env.PLANNER_MODEL ?? null,
750
+ IMAGE_MODEL: env.IMAGE_MODEL ?? null
751
+ },
752
+ async init(config, runtime) {
753
+ initializeGoogleGenAI(config, runtime);
754
+ },
755
+ models: {
756
+ [TEXT_NANO_MODEL_TYPE2]: async (runtime, params) => {
757
+ return handleTextNano(runtime, params);
758
+ },
759
+ [TEXT_MEDIUM_MODEL_TYPE2]: async (runtime, params) => {
760
+ return handleTextMedium(runtime, params);
761
+ },
762
+ [TEXT_SMALL_MODEL_TYPE2]: async (runtime, params) => {
763
+ return handleTextSmall(runtime, params);
764
+ },
765
+ [TEXT_LARGE_MODEL_TYPE2]: async (runtime, params) => {
766
+ return handleTextLarge(runtime, params);
767
+ },
768
+ [TEXT_MEGA_MODEL_TYPE2]: async (runtime, params) => {
769
+ return handleTextMega(runtime, params);
770
+ },
771
+ [RESPONSE_HANDLER_MODEL_TYPE2]: async (runtime, params) => {
772
+ return handleResponseHandler(runtime, params);
773
+ },
774
+ [ACTION_PLANNER_MODEL_TYPE2]: async (runtime, params) => {
775
+ return handleActionPlanner(runtime, params);
776
+ },
777
+ [TEXT_EMBEDDING_MODEL_TYPE2]: async (runtime, params) => {
778
+ return handleTextEmbedding(runtime, params);
779
+ },
780
+ [IMAGE_DESCRIPTION_MODEL_TYPE]: async (runtime, params) => {
781
+ return handleImageDescription(runtime, params);
782
+ }
783
+ },
784
+ tests: pluginTests
785
+ };
786
+ var plugin_google_genai_default = googleGenAIPlugin;
787
+
788
+ // index.node.ts
789
+ var index_node_default = plugin_google_genai_default;
790
+ export {
791
+ googleGenAIPlugin,
792
+ index_node_default as default
793
+ };
794
+
795
+ //# debugId=1AF6D4C9F5965CAB64756E2164756E21