@elizaos/plugin-google-genai 2.0.0-beta.1 → 2.0.3-beta.2

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