@elizaos/plugin-openrouter 1.5.17 → 2.0.0-alpha.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.
- package/LICENSE +1 -1
- package/dist/browser/index.browser.js +319 -919
- package/dist/browser/index.browser.js.map +13 -15
- package/dist/cjs/index.node.cjs +320 -554
- package/dist/cjs/index.node.cjs.map +19 -0
- package/dist/index.d.ts +4 -6
- package/dist/node/index.node.js +319 -564
- package/dist/node/index.node.js.map +13 -14
- package/package.json +39 -22
- package/README.md +0 -122
- package/dist/cjs/index.node.js.map +0 -20
- package/dist/index.browser.d.ts +0 -2
- package/dist/index.node.d.ts +0 -2
- package/dist/init.d.ts +0 -6
- package/dist/models/embedding.d.ts +0 -5
- package/dist/models/image.d.ts +0 -14
- package/dist/models/index.d.ts +0 -4
- package/dist/models/object.d.ts +0 -9
- package/dist/models/text.d.ts +0 -17
- package/dist/providers/index.d.ts +0 -1
- package/dist/providers/openrouter.d.ts +0 -8
- package/dist/types/index.d.ts +0 -28
- package/dist/utils/config.d.ts +0 -64
- package/dist/utils/events.d.ts +0 -6
- package/dist/utils/helpers.d.ts +0 -21
- package/dist/utils/image-storage.d.ts +0 -8
- package/dist/utils/index.d.ts +0 -2
package/dist/cjs/index.node.cjs
CHANGED
|
@@ -1,20 +1,7 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
3
1
|
var __defProp = Object.defineProperty;
|
|
4
2
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
-
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
-
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
-
for (let key of __getOwnPropNames(mod))
|
|
11
|
-
if (!__hasOwnProp.call(to, key))
|
|
12
|
-
__defProp(to, key, {
|
|
13
|
-
get: () => mod[key],
|
|
14
|
-
enumerable: true
|
|
15
|
-
});
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
5
|
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
19
6
|
var __toCommonJS = (from) => {
|
|
20
7
|
var entry = __moduleCache.get(from), desc;
|
|
@@ -39,60 +26,95 @@ var __export = (target, all) => {
|
|
|
39
26
|
});
|
|
40
27
|
};
|
|
41
28
|
|
|
42
|
-
//
|
|
43
|
-
var
|
|
44
|
-
__export(
|
|
29
|
+
// index.ts
|
|
30
|
+
var exports_typescript = {};
|
|
31
|
+
__export(exports_typescript, {
|
|
32
|
+
shouldAutoCleanupImages: () => shouldAutoCleanupImages,
|
|
45
33
|
openrouterPlugin: () => openrouterPlugin,
|
|
46
|
-
|
|
34
|
+
getSmallModel: () => getSmallModel,
|
|
35
|
+
getSetting: () => getSetting,
|
|
36
|
+
getLargeModel: () => getLargeModel,
|
|
37
|
+
getImageModel: () => getImageModel,
|
|
38
|
+
getImageGenerationModel: () => getImageGenerationModel,
|
|
39
|
+
getEmbeddingModel: () => getEmbeddingModel,
|
|
40
|
+
getEmbeddingDimensions: () => getEmbeddingDimensions,
|
|
41
|
+
getBaseURL: () => getBaseURL,
|
|
42
|
+
getApiKey: () => getApiKey,
|
|
43
|
+
default: () => openrouterPlugin,
|
|
44
|
+
DEFAULT_SMALL_MODEL: () => DEFAULT_SMALL_MODEL,
|
|
45
|
+
DEFAULT_LARGE_MODEL: () => DEFAULT_LARGE_MODEL,
|
|
46
|
+
DEFAULT_IMAGE_MODEL: () => DEFAULT_IMAGE_MODEL,
|
|
47
|
+
DEFAULT_IMAGE_GENERATION_MODEL: () => DEFAULT_IMAGE_GENERATION_MODEL,
|
|
48
|
+
DEFAULT_EMBEDDING_MODEL: () => DEFAULT_EMBEDDING_MODEL,
|
|
49
|
+
DEFAULT_EMBEDDING_DIMENSIONS: () => DEFAULT_EMBEDDING_DIMENSIONS,
|
|
50
|
+
DEFAULT_BASE_URL: () => DEFAULT_BASE_URL
|
|
47
51
|
});
|
|
48
|
-
module.exports = __toCommonJS(
|
|
52
|
+
module.exports = __toCommonJS(exports_typescript);
|
|
49
53
|
|
|
50
|
-
//
|
|
51
|
-
var
|
|
54
|
+
// plugin.ts
|
|
55
|
+
var import_core8 = require("@elizaos/core");
|
|
52
56
|
|
|
53
|
-
//
|
|
57
|
+
// init.ts
|
|
54
58
|
var import_core = require("@elizaos/core");
|
|
55
|
-
var import_undici = require("undici");
|
|
56
59
|
|
|
57
|
-
//
|
|
60
|
+
// utils/config.ts
|
|
61
|
+
var DEFAULT_BASE_URL = "https://openrouter.ai/api/v1";
|
|
62
|
+
var DEFAULT_SMALL_MODEL = "google/gemini-2.0-flash-001";
|
|
63
|
+
var DEFAULT_LARGE_MODEL = "google/gemini-2.5-flash";
|
|
64
|
+
var DEFAULT_IMAGE_MODEL = "x-ai/grok-2-vision-1212";
|
|
65
|
+
var DEFAULT_IMAGE_GENERATION_MODEL = "google/gemini-2.5-flash-image-preview";
|
|
66
|
+
var DEFAULT_EMBEDDING_MODEL = "openai/text-embedding-3-small";
|
|
67
|
+
var DEFAULT_EMBEDDING_DIMENSIONS = 1536;
|
|
68
|
+
function getEnvValue(key) {
|
|
69
|
+
if (typeof process === "undefined" || !process.env) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const value = process.env[key];
|
|
73
|
+
return value === undefined ? undefined : String(value);
|
|
74
|
+
}
|
|
58
75
|
function getSetting(runtime, key, defaultValue) {
|
|
59
76
|
const value = runtime.getSetting(key);
|
|
60
77
|
if (value !== undefined && value !== null) {
|
|
61
78
|
return String(value);
|
|
62
79
|
}
|
|
63
|
-
return
|
|
80
|
+
return getEnvValue(key) ?? defaultValue;
|
|
64
81
|
}
|
|
65
82
|
function getBaseURL(runtime) {
|
|
66
83
|
const browserURL = getSetting(runtime, "OPENROUTER_BROWSER_BASE_URL");
|
|
67
84
|
if (typeof globalThis !== "undefined" && globalThis.document && browserURL) {
|
|
68
85
|
return browserURL;
|
|
69
86
|
}
|
|
70
|
-
return getSetting(runtime, "OPENROUTER_BASE_URL",
|
|
87
|
+
return getSetting(runtime, "OPENROUTER_BASE_URL", DEFAULT_BASE_URL) || DEFAULT_BASE_URL;
|
|
71
88
|
}
|
|
72
89
|
function getApiKey(runtime) {
|
|
73
90
|
return getSetting(runtime, "OPENROUTER_API_KEY");
|
|
74
91
|
}
|
|
75
92
|
function getSmallModel(runtime) {
|
|
76
|
-
return getSetting(runtime, "OPENROUTER_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL",
|
|
93
|
+
return getSetting(runtime, "OPENROUTER_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", DEFAULT_SMALL_MODEL) ?? DEFAULT_SMALL_MODEL;
|
|
77
94
|
}
|
|
78
95
|
function getLargeModel(runtime) {
|
|
79
|
-
return getSetting(runtime, "OPENROUTER_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL",
|
|
96
|
+
return getSetting(runtime, "OPENROUTER_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", DEFAULT_LARGE_MODEL) ?? DEFAULT_LARGE_MODEL;
|
|
80
97
|
}
|
|
81
98
|
function getImageModel(runtime) {
|
|
82
|
-
return getSetting(runtime, "OPENROUTER_IMAGE_MODEL") ?? getSetting(runtime, "IMAGE_MODEL",
|
|
99
|
+
return getSetting(runtime, "OPENROUTER_IMAGE_MODEL") ?? getSetting(runtime, "IMAGE_MODEL", DEFAULT_IMAGE_MODEL) ?? DEFAULT_IMAGE_MODEL;
|
|
83
100
|
}
|
|
84
101
|
function getImageGenerationModel(runtime) {
|
|
85
|
-
return getSetting(runtime, "OPENROUTER_IMAGE_GENERATION_MODEL") ?? getSetting(runtime, "IMAGE_GENERATION_MODEL",
|
|
102
|
+
return getSetting(runtime, "OPENROUTER_IMAGE_GENERATION_MODEL") ?? getSetting(runtime, "IMAGE_GENERATION_MODEL", DEFAULT_IMAGE_GENERATION_MODEL) ?? DEFAULT_IMAGE_GENERATION_MODEL;
|
|
86
103
|
}
|
|
87
104
|
function getEmbeddingModel(runtime) {
|
|
88
|
-
return getSetting(runtime, "OPENROUTER_EMBEDDING_MODEL") ?? getSetting(runtime, "EMBEDDING_MODEL",
|
|
105
|
+
return getSetting(runtime, "OPENROUTER_EMBEDDING_MODEL") ?? getSetting(runtime, "EMBEDDING_MODEL", DEFAULT_EMBEDDING_MODEL) ?? DEFAULT_EMBEDDING_MODEL;
|
|
106
|
+
}
|
|
107
|
+
function getEmbeddingDimensions(runtime) {
|
|
108
|
+
const setting = getSetting(runtime, "OPENROUTER_EMBEDDING_DIMENSIONS") ?? getSetting(runtime, "EMBEDDING_DIMENSIONS");
|
|
109
|
+
return setting ? parseInt(setting, 10) : DEFAULT_EMBEDDING_DIMENSIONS;
|
|
89
110
|
}
|
|
90
111
|
function shouldAutoCleanupImages(runtime) {
|
|
91
112
|
const setting = getSetting(runtime, "OPENROUTER_AUTO_CLEANUP_IMAGES", "false");
|
|
92
113
|
return setting?.toLowerCase() === "true";
|
|
93
114
|
}
|
|
94
115
|
|
|
95
|
-
//
|
|
116
|
+
// init.ts
|
|
117
|
+
globalThis.AI_SDK_LOG_WARNINGS ??= false;
|
|
96
118
|
function initializeOpenRouter(_config, runtime) {
|
|
97
119
|
(async () => {
|
|
98
120
|
try {
|
|
@@ -104,456 +126,55 @@ function initializeOpenRouter(_config, runtime) {
|
|
|
104
126
|
import_core.logger.warn("OPENROUTER_API_KEY is not set in environment - OpenRouter functionality will be limited");
|
|
105
127
|
return;
|
|
106
128
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
} else {
|
|
116
|
-
import_core.logger.log("OpenRouter API key validated successfully");
|
|
117
|
-
}
|
|
118
|
-
} catch (fetchError) {
|
|
119
|
-
const message = fetchError instanceof Error ? fetchError.message : String(fetchError);
|
|
120
|
-
import_core.logger.warn(`Error validating OpenRouter API key: ${message}`);
|
|
121
|
-
import_core.logger.warn("OpenRouter functionality will be limited until a valid API key is provided");
|
|
129
|
+
const baseURL = getBaseURL(runtime);
|
|
130
|
+
const response = await fetch(`${baseURL}/models`, {
|
|
131
|
+
headers: { Authorization: `Bearer ${getApiKey(runtime)}` }
|
|
132
|
+
});
|
|
133
|
+
if (!response.ok) {
|
|
134
|
+
import_core.logger.warn(`OpenRouter API key validation failed: ${response.statusText}`);
|
|
135
|
+
} else {
|
|
136
|
+
import_core.logger.log("OpenRouter API key validated successfully");
|
|
122
137
|
}
|
|
123
138
|
} catch (error) {
|
|
124
|
-
const message = error
|
|
125
|
-
import_core.logger.warn(`OpenRouter
|
|
139
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
140
|
+
import_core.logger.warn(`Error validating OpenRouter API key: ${message}`);
|
|
126
141
|
}
|
|
127
142
|
})();
|
|
128
|
-
return;
|
|
129
143
|
}
|
|
130
144
|
|
|
131
|
-
//
|
|
145
|
+
// models/embedding.ts
|
|
132
146
|
var import_core3 = require("@elizaos/core");
|
|
133
|
-
var import_ai = require("ai");
|
|
134
147
|
|
|
135
|
-
//
|
|
136
|
-
var import_ai_sdk_provider = require("@openrouter/ai-sdk-provider");
|
|
137
|
-
function createOpenRouterProvider(runtime) {
|
|
138
|
-
const apiKey = getApiKey(runtime);
|
|
139
|
-
const isBrowser = typeof globalThis !== "undefined" && globalThis.document;
|
|
140
|
-
const baseURL = getBaseURL(runtime);
|
|
141
|
-
return import_ai_sdk_provider.createOpenRouter({
|
|
142
|
-
apiKey: isBrowser ? undefined : apiKey,
|
|
143
|
-
baseURL
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
// src/utils/events.ts
|
|
148
|
+
// utils/events.ts
|
|
147
149
|
var import_core2 = require("@elizaos/core");
|
|
148
|
-
function emitModelUsageEvent(
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
const
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
source: "openrouter",
|
|
150
|
+
function emitModelUsageEvent(_runtime, modelType, prompt, usage) {
|
|
151
|
+
const inputTokens = usage.inputTokens ?? usage.promptTokens ?? 0;
|
|
152
|
+
const outputTokens = usage.outputTokens ?? usage.completionTokens ?? 0;
|
|
153
|
+
const totalTokens = usage.totalTokens ?? inputTokens + outputTokens;
|
|
154
|
+
import_core2.logger.debug({
|
|
155
|
+
event: "model:usage",
|
|
156
|
+
modelType,
|
|
156
157
|
provider: "openrouter",
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
158
|
+
prompt: prompt.substring(0, 100),
|
|
159
|
+
usage: {
|
|
160
|
+
promptTokens: inputTokens,
|
|
161
|
+
completionTokens: outputTokens,
|
|
162
|
+
totalTokens
|
|
163
|
+
},
|
|
164
|
+
timestamp: Date.now()
|
|
164
165
|
});
|
|
165
166
|
}
|
|
166
167
|
|
|
167
|
-
//
|
|
168
|
-
function buildGenerateParams(runtime, modelType, params) {
|
|
169
|
-
const { prompt, stopSequences = [] } = params;
|
|
170
|
-
const temperature = params.temperature ?? 0.7;
|
|
171
|
-
const frequencyPenalty = params.frequencyPenalty ?? 0.7;
|
|
172
|
-
const presencePenalty = params.presencePenalty ?? 0.7;
|
|
173
|
-
const resolvedMaxOutput = params.maxOutputTokens ?? params.maxTokens ?? 8192;
|
|
174
|
-
const openrouter = createOpenRouterProvider(runtime);
|
|
175
|
-
const modelName = modelType === import_core3.ModelType.TEXT_SMALL ? getSmallModel(runtime) : getLargeModel(runtime);
|
|
176
|
-
const modelLabel = modelType === import_core3.ModelType.TEXT_SMALL ? "TEXT_SMALL" : "TEXT_LARGE";
|
|
177
|
-
const generateParams = {
|
|
178
|
-
model: openrouter.chat(modelName),
|
|
179
|
-
prompt,
|
|
180
|
-
system: runtime.character.system ?? undefined,
|
|
181
|
-
temperature,
|
|
182
|
-
frequencyPenalty,
|
|
183
|
-
presencePenalty,
|
|
184
|
-
stopSequences
|
|
185
|
-
};
|
|
186
|
-
generateParams.maxOutputTokens = resolvedMaxOutput;
|
|
187
|
-
return { generateParams, modelName, modelLabel, prompt };
|
|
188
|
-
}
|
|
189
|
-
function handleStreamingGeneration(runtime, modelType, generateParams, prompt, modelLabel) {
|
|
190
|
-
import_core3.logger.debug(`[OpenRouter] Streaming text with ${modelLabel} model`);
|
|
191
|
-
const streamResult = import_ai.streamText(generateParams);
|
|
192
|
-
return {
|
|
193
|
-
textStream: streamResult.textStream,
|
|
194
|
-
text: streamResult.text,
|
|
195
|
-
usage: streamResult.usage.then((usage) => {
|
|
196
|
-
if (usage) {
|
|
197
|
-
emitModelUsageEvent(runtime, modelType, prompt, usage);
|
|
198
|
-
const inputTokens = usage.inputTokens ?? 0;
|
|
199
|
-
const outputTokens = usage.outputTokens ?? 0;
|
|
200
|
-
return {
|
|
201
|
-
promptTokens: inputTokens,
|
|
202
|
-
completionTokens: outputTokens,
|
|
203
|
-
totalTokens: inputTokens + outputTokens
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
return;
|
|
207
|
-
}),
|
|
208
|
-
finishReason: streamResult.finishReason
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
async function generateTextWithModel(runtime, modelType, params) {
|
|
212
|
-
const { generateParams, modelName, modelLabel, prompt } = buildGenerateParams(runtime, modelType, params);
|
|
213
|
-
import_core3.logger.debug(`[OpenRouter] Generating text with ${modelLabel} model: ${modelName}`);
|
|
214
|
-
if (params.stream) {
|
|
215
|
-
return handleStreamingGeneration(runtime, modelType, generateParams, prompt, modelLabel);
|
|
216
|
-
}
|
|
217
|
-
const response = await import_ai.generateText(generateParams);
|
|
218
|
-
if (response.usage) {
|
|
219
|
-
emitModelUsageEvent(runtime, modelType, prompt, response.usage);
|
|
220
|
-
}
|
|
221
|
-
return response.text;
|
|
222
|
-
}
|
|
223
|
-
async function handleTextSmall(runtime, params) {
|
|
224
|
-
return generateTextWithModel(runtime, import_core3.ModelType.TEXT_SMALL, params);
|
|
225
|
-
}
|
|
226
|
-
async function handleTextLarge(runtime, params) {
|
|
227
|
-
return generateTextWithModel(runtime, import_core3.ModelType.TEXT_LARGE, params);
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
// src/models/object.ts
|
|
231
|
-
var import_core5 = require("@elizaos/core");
|
|
232
|
-
var import_ai3 = require("ai");
|
|
233
|
-
|
|
234
|
-
// src/utils/helpers.ts
|
|
235
|
-
var import_core4 = require("@elizaos/core");
|
|
236
|
-
var import_ai2 = require("ai");
|
|
237
|
-
function getJsonRepairFunction() {
|
|
238
|
-
return async ({ text, error }) => {
|
|
239
|
-
try {
|
|
240
|
-
if (error instanceof import_ai2.JSONParseError) {
|
|
241
|
-
const cleanedText = text.replace(/```json\n|\n```|```/g, "");
|
|
242
|
-
JSON.parse(cleanedText);
|
|
243
|
-
return cleanedText;
|
|
244
|
-
}
|
|
245
|
-
return null;
|
|
246
|
-
} catch (jsonError) {
|
|
247
|
-
const message = jsonError instanceof Error ? jsonError.message : String(jsonError);
|
|
248
|
-
import_core4.logger.warn(`Failed to repair JSON text: ${message}`);
|
|
249
|
-
return null;
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
}
|
|
253
|
-
function parseImageDescriptionResponse(responseText) {
|
|
254
|
-
try {
|
|
255
|
-
const jsonResponse = JSON.parse(responseText);
|
|
256
|
-
if (jsonResponse.title && jsonResponse.description) {
|
|
257
|
-
return jsonResponse;
|
|
258
|
-
}
|
|
259
|
-
} catch (e) {
|
|
260
|
-
import_core4.logger.debug(`Parsing as JSON failed, processing as text: ${e}`);
|
|
261
|
-
}
|
|
262
|
-
const titleMatch = responseText.match(/title[:\s]+(.+?)(?:\n|$)/i);
|
|
263
|
-
const title = titleMatch?.[1]?.trim() || "Image Analysis";
|
|
264
|
-
const description = responseText.replace(/title[:\s]+(.+?)(?:\n|$)/i, "").trim();
|
|
265
|
-
return { title, description };
|
|
266
|
-
}
|
|
267
|
-
async function handleObjectGenerationError(error) {
|
|
268
|
-
if (error instanceof import_ai2.JSONParseError) {
|
|
269
|
-
import_core4.logger.error(`[generateObject] Failed to parse JSON: ${error.message}`);
|
|
270
|
-
const repairFunction = getJsonRepairFunction();
|
|
271
|
-
const repairedJsonString = await repairFunction({
|
|
272
|
-
text: error.text,
|
|
273
|
-
error
|
|
274
|
-
});
|
|
275
|
-
if (repairedJsonString) {
|
|
276
|
-
try {
|
|
277
|
-
const repairedObject = JSON.parse(repairedJsonString);
|
|
278
|
-
import_core4.logger.log("[generateObject] Successfully repaired JSON.");
|
|
279
|
-
return repairedObject;
|
|
280
|
-
} catch (repairParseError) {
|
|
281
|
-
const message = repairParseError instanceof Error ? repairParseError.message : String(repairParseError);
|
|
282
|
-
import_core4.logger.error(`[generateObject] Failed to parse repaired JSON: ${message}`);
|
|
283
|
-
if (repairParseError instanceof Error)
|
|
284
|
-
throw repairParseError;
|
|
285
|
-
throw Object.assign(new Error(message), { cause: repairParseError });
|
|
286
|
-
}
|
|
287
|
-
} else {
|
|
288
|
-
import_core4.logger.error("[generateObject] JSON repair failed.");
|
|
289
|
-
throw error;
|
|
290
|
-
}
|
|
291
|
-
} else {
|
|
292
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
293
|
-
import_core4.logger.error(`[generateObject] Unknown error: ${message}`);
|
|
294
|
-
if (error instanceof Error)
|
|
295
|
-
throw error;
|
|
296
|
-
throw Object.assign(new Error(message), { cause: error });
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
// src/models/object.ts
|
|
301
|
-
async function generateObjectWithModel(runtime, modelType, params) {
|
|
302
|
-
const openrouter = createOpenRouterProvider(runtime);
|
|
303
|
-
const modelName = modelType === import_core5.ModelType.OBJECT_SMALL ? getSmallModel(runtime) : getLargeModel(runtime);
|
|
304
|
-
const modelLabel = modelType === import_core5.ModelType.OBJECT_SMALL ? "OBJECT_SMALL" : "OBJECT_LARGE";
|
|
305
|
-
import_core5.logger.log(`[OpenRouter] Using ${modelLabel} model: ${modelName}`);
|
|
306
|
-
const temperature = params.temperature ?? 0.7;
|
|
307
|
-
try {
|
|
308
|
-
const { object, usage } = await import_ai3.generateObject({
|
|
309
|
-
model: openrouter.chat(modelName),
|
|
310
|
-
...params.schema && { schema: import_ai3.jsonSchema(params.schema) },
|
|
311
|
-
output: params.schema ? "object" : "no-schema",
|
|
312
|
-
prompt: params.prompt,
|
|
313
|
-
temperature,
|
|
314
|
-
experimental_repairText: getJsonRepairFunction()
|
|
315
|
-
});
|
|
316
|
-
if (usage) {
|
|
317
|
-
emitModelUsageEvent(runtime, modelType, params.prompt, usage);
|
|
318
|
-
}
|
|
319
|
-
return object;
|
|
320
|
-
} catch (error) {
|
|
321
|
-
return handleObjectGenerationError(error);
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
async function handleObjectSmall(runtime, params) {
|
|
325
|
-
return generateObjectWithModel(runtime, import_core5.ModelType.OBJECT_SMALL, params);
|
|
326
|
-
}
|
|
327
|
-
async function handleObjectLarge(runtime, params) {
|
|
328
|
-
return generateObjectWithModel(runtime, import_core5.ModelType.OBJECT_LARGE, params);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
// src/models/image.ts
|
|
332
|
-
var import_core7 = require("@elizaos/core");
|
|
333
|
-
var import_ai4 = require("ai");
|
|
334
|
-
|
|
335
|
-
// src/utils/image-storage.ts
|
|
336
|
-
var import_core6 = require("@elizaos/core");
|
|
337
|
-
function isBrowser() {
|
|
338
|
-
return typeof globalThis !== "undefined" && globalThis.document;
|
|
339
|
-
}
|
|
340
|
-
function sanitizeId(id) {
|
|
341
|
-
const src = (id ?? "").toString();
|
|
342
|
-
const normalized = src.normalize("NFKC");
|
|
343
|
-
let safe = normalized.replace(/[^a-zA-Z0-9_-]/g, "_");
|
|
344
|
-
safe = safe.replace(/_+/g, "_");
|
|
345
|
-
safe = safe.slice(0, 64);
|
|
346
|
-
safe = safe.replace(/^_+|_+$/g, "");
|
|
347
|
-
return safe || "agent";
|
|
348
|
-
}
|
|
349
|
-
function base64ToBytes(base64) {
|
|
350
|
-
const cleaned = base64.replace(/\s+/g, "");
|
|
351
|
-
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
352
|
-
const lookup = new Array(256).fill(-1);
|
|
353
|
-
for (let i = 0;i < chars.length; i++)
|
|
354
|
-
lookup[chars.charCodeAt(i)] = i;
|
|
355
|
-
const len = cleaned.length;
|
|
356
|
-
let pad = 0;
|
|
357
|
-
if (len >= 2 && cleaned[len - 1] === "=")
|
|
358
|
-
pad++;
|
|
359
|
-
if (len >= 2 && cleaned[len - 2] === "=")
|
|
360
|
-
pad++;
|
|
361
|
-
const outLen = (len * 3 >> 2) - pad;
|
|
362
|
-
const out = new Uint8Array(outLen);
|
|
363
|
-
let o = 0;
|
|
364
|
-
for (let i = 0;i < len; i += 4) {
|
|
365
|
-
const c0 = lookup[cleaned.charCodeAt(i)];
|
|
366
|
-
const c1 = lookup[cleaned.charCodeAt(i + 1)];
|
|
367
|
-
const c2 = lookup[cleaned.charCodeAt(i + 2)];
|
|
368
|
-
const c3 = lookup[cleaned.charCodeAt(i + 3)];
|
|
369
|
-
const n = c0 << 18 | c1 << 12 | (c2 & 63) << 6 | c3 & 63;
|
|
370
|
-
if (o < outLen)
|
|
371
|
-
out[o++] = n >> 16 & 255;
|
|
372
|
-
if (o < outLen)
|
|
373
|
-
out[o++] = n >> 8 & 255;
|
|
374
|
-
if (o < outLen)
|
|
375
|
-
out[o++] = n & 255;
|
|
376
|
-
}
|
|
377
|
-
return out;
|
|
378
|
-
}
|
|
379
|
-
async function saveBase64Image(base64Url, agentId, index = 0) {
|
|
380
|
-
if (isBrowser()) {
|
|
381
|
-
return null;
|
|
382
|
-
}
|
|
383
|
-
const m = base64Url.match(/^data:(image\/[a-zA-Z0-9.+-]+);base64,([A-Za-z0-9+/=]+)$/);
|
|
384
|
-
if (!m)
|
|
385
|
-
return null;
|
|
386
|
-
const mime = m[1];
|
|
387
|
-
const base64Data = m[2];
|
|
388
|
-
const extMap = {
|
|
389
|
-
"image/png": "png",
|
|
390
|
-
"image/jpeg": "jpg",
|
|
391
|
-
"image/jpg": "jpg",
|
|
392
|
-
"image/webp": "webp",
|
|
393
|
-
"image/gif": "gif",
|
|
394
|
-
"image/bmp": "bmp",
|
|
395
|
-
"image/tiff": "tiff"
|
|
396
|
-
};
|
|
397
|
-
const extension = extMap[mime];
|
|
398
|
-
if (!extension)
|
|
399
|
-
return null;
|
|
400
|
-
const { join } = await import("node:path");
|
|
401
|
-
const safeAgentId = sanitizeId(agentId);
|
|
402
|
-
const baseDir = join(import_core6.getGeneratedDir(), safeAgentId);
|
|
403
|
-
const { existsSync } = await import("node:fs");
|
|
404
|
-
if (!existsSync(baseDir)) {
|
|
405
|
-
const { mkdir } = await import("node:fs/promises");
|
|
406
|
-
await mkdir(baseDir, { recursive: true });
|
|
407
|
-
}
|
|
408
|
-
const timestamp = Date.now();
|
|
409
|
-
const filename = `image_${timestamp}_${index}.${extension}`;
|
|
410
|
-
const filepath = join(baseDir, filename);
|
|
411
|
-
const buffer = base64ToBytes(base64Data);
|
|
412
|
-
const { writeFile } = await import("node:fs/promises");
|
|
413
|
-
await writeFile(filepath, buffer);
|
|
414
|
-
import_core6.logger.info(`[OpenRouter] Saved generated image to ${filepath}`);
|
|
415
|
-
return filepath;
|
|
416
|
-
}
|
|
417
|
-
function deleteImage(filepath) {
|
|
418
|
-
if (isBrowser()) {
|
|
419
|
-
return;
|
|
420
|
-
}
|
|
421
|
-
try {
|
|
422
|
-
(async () => {
|
|
423
|
-
const { existsSync, unlinkSync } = await import("node:fs");
|
|
424
|
-
if (existsSync(filepath)) {
|
|
425
|
-
unlinkSync(filepath);
|
|
426
|
-
import_core6.logger.debug(`[OpenRouter] Deleted image: ${filepath}`);
|
|
427
|
-
}
|
|
428
|
-
})().catch((error) => {
|
|
429
|
-
import_core6.logger.warn(`[OpenRouter] Failed to delete image ${filepath}:`, String(error));
|
|
430
|
-
});
|
|
431
|
-
} catch (error) {
|
|
432
|
-
import_core6.logger.warn(`[OpenRouter] Failed to delete image ${filepath}:`, String(error));
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
|
|
436
|
-
// src/models/image.ts
|
|
437
|
-
async function handleImageDescription(runtime, params) {
|
|
438
|
-
let imageUrl;
|
|
439
|
-
let promptText;
|
|
440
|
-
const modelName = getImageModel(runtime);
|
|
441
|
-
import_core7.logger.log(`[OpenRouter] Using IMAGE_DESCRIPTION model: ${modelName}`);
|
|
442
|
-
const maxOutputTokens = 300;
|
|
443
|
-
if (typeof params === "string") {
|
|
444
|
-
imageUrl = params;
|
|
445
|
-
promptText = "Please analyze this image and provide a title and detailed description.";
|
|
446
|
-
} else {
|
|
447
|
-
imageUrl = params.imageUrl;
|
|
448
|
-
promptText = params.prompt || "Please analyze this image and provide a title and detailed description.";
|
|
449
|
-
}
|
|
450
|
-
const openrouter = createOpenRouterProvider(runtime);
|
|
451
|
-
const messages = [
|
|
452
|
-
{
|
|
453
|
-
role: "user",
|
|
454
|
-
content: [
|
|
455
|
-
{ type: "text", text: promptText },
|
|
456
|
-
{ type: "image", image: imageUrl }
|
|
457
|
-
]
|
|
458
|
-
}
|
|
459
|
-
];
|
|
460
|
-
try {
|
|
461
|
-
const model = openrouter.chat(modelName);
|
|
462
|
-
const { text: responseText } = await import_ai4.generateText({
|
|
463
|
-
model,
|
|
464
|
-
messages,
|
|
465
|
-
maxOutputTokens
|
|
466
|
-
});
|
|
467
|
-
return parseImageDescriptionResponse(responseText);
|
|
468
|
-
} catch (error) {
|
|
469
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
470
|
-
import_core7.logger.error(`Error analyzing image: ${message}`);
|
|
471
|
-
return {
|
|
472
|
-
title: "Failed to analyze image",
|
|
473
|
-
description: `Error: ${message}`
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
async function handleImageGeneration(runtime, params) {
|
|
478
|
-
const modelName = getImageGenerationModel(runtime);
|
|
479
|
-
import_core7.logger.log(`[OpenRouter] Using IMAGE_GENERATION model: ${modelName}`);
|
|
480
|
-
const apiKey = getApiKey(runtime);
|
|
481
|
-
try {
|
|
482
|
-
const baseUrl = getBaseURL(runtime);
|
|
483
|
-
const isBrowser2 = typeof globalThis !== "undefined" && globalThis.document;
|
|
484
|
-
const response = await fetch(`${baseUrl}/chat/completions`, {
|
|
485
|
-
method: "POST",
|
|
486
|
-
headers: {
|
|
487
|
-
...isBrowser2 ? {} : { Authorization: `Bearer ${apiKey}` },
|
|
488
|
-
"Content-Type": "application/json"
|
|
489
|
-
},
|
|
490
|
-
body: JSON.stringify({
|
|
491
|
-
model: modelName,
|
|
492
|
-
messages: [
|
|
493
|
-
{
|
|
494
|
-
role: "user",
|
|
495
|
-
content: params.prompt
|
|
496
|
-
}
|
|
497
|
-
],
|
|
498
|
-
modalities: ["image", "text"]
|
|
499
|
-
}),
|
|
500
|
-
signal: AbortSignal.timeout ? AbortSignal.timeout(60000) : undefined
|
|
501
|
-
});
|
|
502
|
-
if (!response.ok) {
|
|
503
|
-
const errorText = await response.text().catch(() => "");
|
|
504
|
-
throw new Error(`HTTP ${response.status} ${response.statusText} ${errorText}`);
|
|
505
|
-
}
|
|
506
|
-
const result = await response.json();
|
|
507
|
-
const images = [];
|
|
508
|
-
const savedPaths = [];
|
|
509
|
-
if (result.choices?.[0]?.message?.images) {
|
|
510
|
-
for (const [index, image] of result.choices[0].message.images.entries()) {
|
|
511
|
-
const base64Url = image.image_url.url;
|
|
512
|
-
const filepath = await saveBase64Image(base64Url, runtime.agentId, index);
|
|
513
|
-
if (filepath) {
|
|
514
|
-
import_core7.logger.log(`[OpenRouter] Returning image with filepath: ${filepath}`);
|
|
515
|
-
images.push({
|
|
516
|
-
url: filepath
|
|
517
|
-
});
|
|
518
|
-
savedPaths.push(filepath);
|
|
519
|
-
} else if (!base64Url.startsWith("data:")) {
|
|
520
|
-
images.push({ url: base64Url });
|
|
521
|
-
} else {
|
|
522
|
-
import_core7.logger.warn(`[OpenRouter] Failed to save image ${index + 1}, skipping`);
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
if (savedPaths.length > 0 && shouldAutoCleanupImages(runtime)) {
|
|
527
|
-
setTimeout(() => {
|
|
528
|
-
savedPaths.forEach((path) => {
|
|
529
|
-
deleteImage(path);
|
|
530
|
-
});
|
|
531
|
-
}, 30000);
|
|
532
|
-
}
|
|
533
|
-
if (images.length === 0) {
|
|
534
|
-
throw new Error("No images generated in response");
|
|
535
|
-
}
|
|
536
|
-
import_core7.logger.log(`[OpenRouter] Generated ${images.length} image(s)`);
|
|
537
|
-
return images;
|
|
538
|
-
} catch (error) {
|
|
539
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
540
|
-
import_core7.logger.error(`[OpenRouter] Error generating image: ${message}`);
|
|
541
|
-
return [];
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
// src/models/embedding.ts
|
|
546
|
-
var import_core8 = require("@elizaos/core");
|
|
168
|
+
// models/embedding.ts
|
|
547
169
|
async function handleTextEmbedding(runtime, params) {
|
|
548
170
|
const embeddingModelName = getEmbeddingModel(runtime);
|
|
549
171
|
const embeddingDimension = Number.parseInt(getSetting(runtime, "OPENROUTER_EMBEDDING_DIMENSIONS") ?? getSetting(runtime, "EMBEDDING_DIMENSIONS") ?? "1536", 10);
|
|
550
|
-
if (!Object.values(
|
|
551
|
-
const errorMsg = `Invalid embedding dimension: ${embeddingDimension}. Must be one of: ${Object.values(
|
|
552
|
-
|
|
172
|
+
if (!Object.values(import_core3.VECTOR_DIMS).includes(embeddingDimension)) {
|
|
173
|
+
const errorMsg = `Invalid embedding dimension: ${embeddingDimension}. Must be one of: ${Object.values(import_core3.VECTOR_DIMS).join(", ")}`;
|
|
174
|
+
import_core3.logger.error(errorMsg);
|
|
553
175
|
throw new Error(errorMsg);
|
|
554
176
|
}
|
|
555
177
|
if (params === null) {
|
|
556
|
-
import_core8.logger.debug("Creating test embedding for initialization");
|
|
557
178
|
const testVector = Array(embeddingDimension).fill(0);
|
|
558
179
|
testVector[0] = 0.1;
|
|
559
180
|
return testVector;
|
|
@@ -565,14 +186,14 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
565
186
|
text = params.text;
|
|
566
187
|
} else {
|
|
567
188
|
const errorMsg = "Invalid input format for embedding";
|
|
568
|
-
|
|
189
|
+
import_core3.logger.warn(errorMsg);
|
|
569
190
|
const fallbackVector = Array(embeddingDimension).fill(0);
|
|
570
191
|
fallbackVector[0] = 0.2;
|
|
571
192
|
return fallbackVector;
|
|
572
193
|
}
|
|
573
194
|
if (!text.trim()) {
|
|
574
195
|
const errorMsg = "Empty text for embedding";
|
|
575
|
-
|
|
196
|
+
import_core3.logger.warn(errorMsg);
|
|
576
197
|
const fallbackVector = Array(embeddingDimension).fill(0);
|
|
577
198
|
fallbackVector[0] = 0.3;
|
|
578
199
|
return fallbackVector;
|
|
@@ -580,7 +201,7 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
580
201
|
const apiKey = getApiKey(runtime);
|
|
581
202
|
if (!apiKey) {
|
|
582
203
|
const errorMsg = "OPENROUTER_API_KEY is not set";
|
|
583
|
-
|
|
204
|
+
import_core3.logger.error(errorMsg);
|
|
584
205
|
throw new Error(errorMsg);
|
|
585
206
|
}
|
|
586
207
|
const baseURL = getBaseURL(runtime);
|
|
@@ -599,18 +220,18 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
599
220
|
})
|
|
600
221
|
});
|
|
601
222
|
if (!response.ok) {
|
|
602
|
-
|
|
223
|
+
import_core3.logger.error(`OpenRouter API error: ${response.status} - ${response.statusText}`);
|
|
603
224
|
throw new Error(`OpenRouter API error: ${response.status} - ${response.statusText}`);
|
|
604
225
|
}
|
|
605
226
|
const data = await response.json();
|
|
606
227
|
if (!data?.data?.[0]?.embedding) {
|
|
607
|
-
|
|
228
|
+
import_core3.logger.error("API returned invalid structure");
|
|
608
229
|
throw new Error("API returned invalid structure");
|
|
609
230
|
}
|
|
610
231
|
const embedding = data.data[0].embedding;
|
|
611
232
|
if (!Array.isArray(embedding) || embedding.length !== embeddingDimension) {
|
|
612
233
|
const errorMsg = `Embedding length ${embedding?.length ?? 0} does not match configured dimension ${embeddingDimension}`;
|
|
613
|
-
|
|
234
|
+
import_core3.logger.error(errorMsg);
|
|
614
235
|
const fallbackVector = Array(embeddingDimension).fill(0);
|
|
615
236
|
fallbackVector[0] = 0.4;
|
|
616
237
|
return fallbackVector;
|
|
@@ -621,61 +242,249 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
621
242
|
outputTokens: 0,
|
|
622
243
|
totalTokens: data.usage.total_tokens
|
|
623
244
|
};
|
|
624
|
-
emitModelUsageEvent(runtime,
|
|
245
|
+
emitModelUsageEvent(runtime, import_core3.ModelType.TEXT_EMBEDDING, text, usage);
|
|
625
246
|
}
|
|
626
|
-
import_core8.logger.log(`Got valid embedding with length ${embedding.length}`);
|
|
627
247
|
return embedding;
|
|
628
248
|
} catch (error) {
|
|
629
249
|
const message = error instanceof Error ? error.message : String(error);
|
|
630
|
-
|
|
250
|
+
import_core3.logger.error(`Error generating embedding: ${message}`);
|
|
251
|
+
throw error instanceof Error ? error : new Error(message);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// models/image.ts
|
|
256
|
+
var import_core4 = require("@elizaos/core");
|
|
257
|
+
var import_ai = require("ai");
|
|
258
|
+
|
|
259
|
+
// providers/openrouter.ts
|
|
260
|
+
var import_ai_sdk_provider = require("@openrouter/ai-sdk-provider");
|
|
261
|
+
function createOpenRouterProvider(runtime) {
|
|
262
|
+
const apiKey = getApiKey(runtime);
|
|
263
|
+
const isBrowser = typeof globalThis !== "undefined" && globalThis.document;
|
|
264
|
+
const baseURL = getBaseURL(runtime);
|
|
265
|
+
return import_ai_sdk_provider.createOpenRouter({
|
|
266
|
+
apiKey: isBrowser ? undefined : apiKey,
|
|
267
|
+
baseURL
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
// models/image.ts
|
|
271
|
+
async function handleImageDescription(runtime, params) {
|
|
272
|
+
const openrouter = createOpenRouterProvider(runtime);
|
|
273
|
+
const modelName = getImageModel(runtime);
|
|
274
|
+
const imageUrl = typeof params === "string" ? params : params.imageUrl;
|
|
275
|
+
const prompt = typeof params === "string" ? "Describe this image" : params.prompt || "Describe this image";
|
|
276
|
+
try {
|
|
277
|
+
const generateParams = {
|
|
278
|
+
model: openrouter.chat(modelName),
|
|
279
|
+
messages: [
|
|
280
|
+
{
|
|
281
|
+
role: "user",
|
|
282
|
+
content: [
|
|
283
|
+
{ type: "text", text: prompt },
|
|
284
|
+
{ type: "image", image: imageUrl }
|
|
285
|
+
]
|
|
286
|
+
}
|
|
287
|
+
]
|
|
288
|
+
};
|
|
289
|
+
const response = await import_ai.generateText(generateParams);
|
|
290
|
+
if (response.usage) {
|
|
291
|
+
emitModelUsageEvent(runtime, import_core4.ModelType.IMAGE_DESCRIPTION, prompt, response.usage);
|
|
292
|
+
}
|
|
293
|
+
return response.text;
|
|
294
|
+
} catch (error) {
|
|
295
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
296
|
+
import_core4.logger.error(`Error describing image: ${message}`);
|
|
297
|
+
throw error instanceof Error ? error : new Error(message);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
async function handleImageGeneration(runtime, params) {
|
|
301
|
+
const openrouter = createOpenRouterProvider(runtime);
|
|
302
|
+
const modelName = getImageGenerationModel(runtime);
|
|
303
|
+
try {
|
|
304
|
+
const generateParams = {
|
|
305
|
+
model: openrouter.chat(modelName),
|
|
306
|
+
prompt: `Generate an image: ${params.prompt}`
|
|
307
|
+
};
|
|
308
|
+
const response = await import_ai.generateText(generateParams);
|
|
309
|
+
if (response.usage) {
|
|
310
|
+
emitModelUsageEvent(runtime, import_core4.ModelType.IMAGE, params.prompt, response.usage);
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
imageUrl: response.text,
|
|
314
|
+
caption: params.prompt
|
|
315
|
+
};
|
|
316
|
+
} catch (error) {
|
|
317
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
318
|
+
import_core4.logger.error(`Error generating image: ${message}`);
|
|
631
319
|
throw error instanceof Error ? error : new Error(message);
|
|
632
320
|
}
|
|
633
321
|
}
|
|
634
322
|
|
|
635
|
-
//
|
|
323
|
+
// models/object.ts
|
|
324
|
+
var import_core6 = require("@elizaos/core");
|
|
325
|
+
var import_ai2 = require("ai");
|
|
326
|
+
|
|
327
|
+
// utils/helpers.ts
|
|
328
|
+
var import_core5 = require("@elizaos/core");
|
|
329
|
+
function handleObjectGenerationError(error) {
|
|
330
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
331
|
+
import_core5.logger.error(`Error generating object: ${message}`);
|
|
332
|
+
return { error: message };
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// models/object.ts
|
|
336
|
+
async function generateObjectWithModel(runtime, modelType, params) {
|
|
337
|
+
const openrouter = createOpenRouterProvider(runtime);
|
|
338
|
+
const modelName = modelType === import_core6.ModelType.OBJECT_SMALL ? getSmallModel(runtime) : getLargeModel(runtime);
|
|
339
|
+
const temperature = params.temperature ?? 0.7;
|
|
340
|
+
try {
|
|
341
|
+
const generateParams = {
|
|
342
|
+
model: openrouter.chat(modelName),
|
|
343
|
+
...params.schema && {
|
|
344
|
+
schema: import_ai2.jsonSchema(params.schema)
|
|
345
|
+
},
|
|
346
|
+
output: params.schema ? "object" : "no-schema",
|
|
347
|
+
prompt: params.prompt,
|
|
348
|
+
temperature
|
|
349
|
+
};
|
|
350
|
+
const { object, usage } = await import_ai2.generateObject(generateParams);
|
|
351
|
+
if (usage) {
|
|
352
|
+
emitModelUsageEvent(runtime, modelType, params.prompt, usage);
|
|
353
|
+
}
|
|
354
|
+
return object;
|
|
355
|
+
} catch (error) {
|
|
356
|
+
return handleObjectGenerationError(error);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
async function handleObjectSmall(runtime, params) {
|
|
360
|
+
return generateObjectWithModel(runtime, import_core6.ModelType.OBJECT_SMALL, params);
|
|
361
|
+
}
|
|
362
|
+
async function handleObjectLarge(runtime, params) {
|
|
363
|
+
return generateObjectWithModel(runtime, import_core6.ModelType.OBJECT_LARGE, params);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// models/text.ts
|
|
367
|
+
var import_core7 = require("@elizaos/core");
|
|
368
|
+
var import_ai3 = require("ai");
|
|
369
|
+
function buildGenerateParams(runtime, modelType, params) {
|
|
370
|
+
const { prompt, stopSequences = [] } = params;
|
|
371
|
+
const temperature = params.temperature ?? 0.7;
|
|
372
|
+
const frequencyPenalty = params.frequencyPenalty ?? 0.7;
|
|
373
|
+
const presencePenalty = params.presencePenalty ?? 0.7;
|
|
374
|
+
const paramsWithMax = params;
|
|
375
|
+
const resolvedMaxOutput = paramsWithMax.maxOutputTokens ?? paramsWithMax.maxTokens ?? 8192;
|
|
376
|
+
const openrouter = createOpenRouterProvider(runtime);
|
|
377
|
+
const modelName = modelType === import_core7.ModelType.TEXT_SMALL ? getSmallModel(runtime) : getLargeModel(runtime);
|
|
378
|
+
const modelLabel = modelType === import_core7.ModelType.TEXT_SMALL ? "TEXT_SMALL" : "TEXT_LARGE";
|
|
379
|
+
const generateParams = {
|
|
380
|
+
model: openrouter.chat(modelName),
|
|
381
|
+
prompt,
|
|
382
|
+
system: runtime.character?.system ?? undefined,
|
|
383
|
+
temperature,
|
|
384
|
+
frequencyPenalty,
|
|
385
|
+
presencePenalty,
|
|
386
|
+
stopSequences,
|
|
387
|
+
maxOutputTokens: resolvedMaxOutput
|
|
388
|
+
};
|
|
389
|
+
return { generateParams, modelName, modelLabel, prompt };
|
|
390
|
+
}
|
|
391
|
+
function handleStreamingGeneration(runtime, modelType, generateParams, prompt, _modelLabel) {
|
|
392
|
+
const streamResult = import_ai3.streamText(generateParams);
|
|
393
|
+
return {
|
|
394
|
+
textStream: streamResult.textStream,
|
|
395
|
+
text: Promise.resolve(streamResult.text),
|
|
396
|
+
usage: Promise.resolve(streamResult.usage).then((usage) => {
|
|
397
|
+
if (usage) {
|
|
398
|
+
emitModelUsageEvent(runtime, modelType, prompt, usage);
|
|
399
|
+
const inputTokens = usage.inputTokens ?? 0;
|
|
400
|
+
const outputTokens = usage.outputTokens ?? 0;
|
|
401
|
+
return {
|
|
402
|
+
promptTokens: inputTokens,
|
|
403
|
+
completionTokens: outputTokens,
|
|
404
|
+
totalTokens: inputTokens + outputTokens
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
return;
|
|
408
|
+
}),
|
|
409
|
+
finishReason: Promise.resolve(streamResult.finishReason)
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
async function generateTextWithModel(runtime, modelType, params) {
|
|
413
|
+
const {
|
|
414
|
+
generateParams,
|
|
415
|
+
modelName: _modelName,
|
|
416
|
+
modelLabel,
|
|
417
|
+
prompt
|
|
418
|
+
} = buildGenerateParams(runtime, modelType, params);
|
|
419
|
+
if (params.stream) {
|
|
420
|
+
return handleStreamingGeneration(runtime, modelType, generateParams, prompt, modelLabel);
|
|
421
|
+
}
|
|
422
|
+
const response = await import_ai3.generateText(generateParams);
|
|
423
|
+
if (response.usage) {
|
|
424
|
+
emitModelUsageEvent(runtime, modelType, prompt, response.usage);
|
|
425
|
+
}
|
|
426
|
+
return response.text;
|
|
427
|
+
}
|
|
428
|
+
async function handleTextSmall(runtime, params) {
|
|
429
|
+
return generateTextWithModel(runtime, import_core7.ModelType.TEXT_SMALL, params);
|
|
430
|
+
}
|
|
431
|
+
async function handleTextLarge(runtime, params) {
|
|
432
|
+
return generateTextWithModel(runtime, import_core7.ModelType.TEXT_LARGE, params);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// plugin.ts
|
|
436
|
+
function getProcessEnv() {
|
|
437
|
+
if (typeof process === "undefined" || !process.env) {
|
|
438
|
+
return {};
|
|
439
|
+
}
|
|
440
|
+
return process.env;
|
|
441
|
+
}
|
|
442
|
+
var env = getProcessEnv();
|
|
636
443
|
var openrouterPlugin = {
|
|
637
444
|
name: "openrouter",
|
|
638
|
-
description: "OpenRouter plugin",
|
|
445
|
+
description: "OpenRouter multi-model AI gateway plugin",
|
|
639
446
|
config: {
|
|
640
|
-
OPENROUTER_API_KEY:
|
|
641
|
-
OPENROUTER_BASE_URL:
|
|
642
|
-
OPENROUTER_SMALL_MODEL:
|
|
643
|
-
OPENROUTER_LARGE_MODEL:
|
|
644
|
-
OPENROUTER_IMAGE_MODEL:
|
|
645
|
-
OPENROUTER_IMAGE_GENERATION_MODEL:
|
|
646
|
-
OPENROUTER_EMBEDDING_MODEL:
|
|
647
|
-
OPENROUTER_EMBEDDING_DIMENSIONS:
|
|
648
|
-
OPENROUTER_AUTO_CLEANUP_IMAGES:
|
|
649
|
-
SMALL_MODEL:
|
|
650
|
-
LARGE_MODEL:
|
|
651
|
-
IMAGE_MODEL:
|
|
652
|
-
IMAGE_GENERATION_MODEL:
|
|
653
|
-
EMBEDDING_MODEL:
|
|
654
|
-
EMBEDDING_DIMENSIONS:
|
|
447
|
+
OPENROUTER_API_KEY: env.OPENROUTER_API_KEY ?? null,
|
|
448
|
+
OPENROUTER_BASE_URL: env.OPENROUTER_BASE_URL ?? null,
|
|
449
|
+
OPENROUTER_SMALL_MODEL: env.OPENROUTER_SMALL_MODEL ?? null,
|
|
450
|
+
OPENROUTER_LARGE_MODEL: env.OPENROUTER_LARGE_MODEL ?? null,
|
|
451
|
+
OPENROUTER_IMAGE_MODEL: env.OPENROUTER_IMAGE_MODEL ?? null,
|
|
452
|
+
OPENROUTER_IMAGE_GENERATION_MODEL: env.OPENROUTER_IMAGE_GENERATION_MODEL ?? null,
|
|
453
|
+
OPENROUTER_EMBEDDING_MODEL: env.OPENROUTER_EMBEDDING_MODEL ?? null,
|
|
454
|
+
OPENROUTER_EMBEDDING_DIMENSIONS: env.OPENROUTER_EMBEDDING_DIMENSIONS ?? null,
|
|
455
|
+
OPENROUTER_AUTO_CLEANUP_IMAGES: env.OPENROUTER_AUTO_CLEANUP_IMAGES ?? null,
|
|
456
|
+
SMALL_MODEL: env.SMALL_MODEL ?? null,
|
|
457
|
+
LARGE_MODEL: env.LARGE_MODEL ?? null,
|
|
458
|
+
IMAGE_MODEL: env.IMAGE_MODEL ?? null,
|
|
459
|
+
IMAGE_GENERATION_MODEL: env.IMAGE_GENERATION_MODEL ?? null,
|
|
460
|
+
EMBEDDING_MODEL: env.EMBEDDING_MODEL ?? null,
|
|
461
|
+
EMBEDDING_DIMENSIONS: env.EMBEDDING_DIMENSIONS ?? null
|
|
655
462
|
},
|
|
656
463
|
async init(config, runtime) {
|
|
657
464
|
initializeOpenRouter(config, runtime);
|
|
658
465
|
},
|
|
659
466
|
models: {
|
|
660
|
-
[
|
|
467
|
+
[import_core8.ModelType.TEXT_SMALL]: async (runtime, params) => {
|
|
661
468
|
return handleTextSmall(runtime, params);
|
|
662
469
|
},
|
|
663
|
-
[
|
|
470
|
+
[import_core8.ModelType.TEXT_LARGE]: async (runtime, params) => {
|
|
664
471
|
return handleTextLarge(runtime, params);
|
|
665
472
|
},
|
|
666
|
-
[
|
|
473
|
+
[import_core8.ModelType.OBJECT_SMALL]: async (runtime, params) => {
|
|
667
474
|
return handleObjectSmall(runtime, params);
|
|
668
475
|
},
|
|
669
|
-
[
|
|
476
|
+
[import_core8.ModelType.OBJECT_LARGE]: async (runtime, params) => {
|
|
670
477
|
return handleObjectLarge(runtime, params);
|
|
671
478
|
},
|
|
672
|
-
[
|
|
673
|
-
|
|
479
|
+
[import_core8.ModelType.IMAGE_DESCRIPTION]: async (runtime, params) => {
|
|
480
|
+
const description = await handleImageDescription(runtime, params);
|
|
481
|
+
return { title: "", description };
|
|
674
482
|
},
|
|
675
|
-
[
|
|
676
|
-
|
|
483
|
+
[import_core8.ModelType.IMAGE]: async (runtime, params) => {
|
|
484
|
+
const result = await handleImageGeneration(runtime, params);
|
|
485
|
+
return [{ url: result.imageUrl }];
|
|
677
486
|
},
|
|
678
|
-
[
|
|
487
|
+
[import_core8.ModelType.TEXT_EMBEDDING]: async (runtime, params) => {
|
|
679
488
|
return handleTextEmbedding(runtime, params);
|
|
680
489
|
}
|
|
681
490
|
},
|
|
@@ -687,16 +496,16 @@ var openrouterPlugin = {
|
|
|
687
496
|
name: "openrouter_test_text_small",
|
|
688
497
|
fn: async (runtime) => {
|
|
689
498
|
try {
|
|
690
|
-
const text = await runtime.useModel(
|
|
499
|
+
const text = await runtime.useModel(import_core8.ModelType.TEXT_SMALL, {
|
|
691
500
|
prompt: "What is the nature of reality in 10 words?"
|
|
692
501
|
});
|
|
693
502
|
if (text.length === 0) {
|
|
694
503
|
throw new Error("Failed to generate text");
|
|
695
504
|
}
|
|
696
|
-
|
|
505
|
+
import_core8.logger.log({ text }, "generated with test_text_small");
|
|
697
506
|
} catch (error) {
|
|
698
507
|
const message = error instanceof Error ? error.message : String(error);
|
|
699
|
-
|
|
508
|
+
import_core8.logger.error(`Error in test_text_small: ${message}`);
|
|
700
509
|
throw error;
|
|
701
510
|
}
|
|
702
511
|
}
|
|
@@ -705,59 +514,16 @@ var openrouterPlugin = {
|
|
|
705
514
|
name: "openrouter_test_text_large",
|
|
706
515
|
fn: async (runtime) => {
|
|
707
516
|
try {
|
|
708
|
-
const text = await runtime.useModel(
|
|
517
|
+
const text = await runtime.useModel(import_core8.ModelType.TEXT_LARGE, {
|
|
709
518
|
prompt: "What is the nature of reality in 10 words?"
|
|
710
519
|
});
|
|
711
520
|
if (text.length === 0) {
|
|
712
521
|
throw new Error("Failed to generate text");
|
|
713
522
|
}
|
|
714
|
-
|
|
715
|
-
} catch (error) {
|
|
716
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
717
|
-
import_core9.logger.error(`Error in test_text_large: ${message}`);
|
|
718
|
-
throw error;
|
|
719
|
-
}
|
|
720
|
-
}
|
|
721
|
-
},
|
|
722
|
-
{
|
|
723
|
-
name: "openrouter_test_text_generation_large",
|
|
724
|
-
fn: async (runtime) => {
|
|
725
|
-
try {
|
|
726
|
-
const result = await runtime.useModel(import_core9.ModelType.TEXT_LARGE, {
|
|
727
|
-
prompt: "Say hello in 5 words."
|
|
728
|
-
});
|
|
729
|
-
if (!result || result.length === 0) {
|
|
730
|
-
throw new Error("Text generation returned empty result");
|
|
731
|
-
}
|
|
732
|
-
import_core9.logger.log({ result }, "Text generation test completed");
|
|
733
|
-
} catch (error) {
|
|
734
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
735
|
-
import_core9.logger.error(`Error in openrouter_test_text_generation_large: ${message}`);
|
|
736
|
-
throw error;
|
|
737
|
-
}
|
|
738
|
-
}
|
|
739
|
-
},
|
|
740
|
-
{
|
|
741
|
-
name: "openrouter_test_streaming",
|
|
742
|
-
fn: async (runtime) => {
|
|
743
|
-
try {
|
|
744
|
-
const chunks = [];
|
|
745
|
-
const result = await runtime.useModel(import_core9.ModelType.TEXT_LARGE, {
|
|
746
|
-
prompt: "Count from 1 to 5.",
|
|
747
|
-
onStreamChunk: (chunk) => {
|
|
748
|
-
chunks.push(chunk);
|
|
749
|
-
}
|
|
750
|
-
});
|
|
751
|
-
if (!result || result.length === 0) {
|
|
752
|
-
throw new Error("Streaming returned empty result");
|
|
753
|
-
}
|
|
754
|
-
if (chunks.length === 0) {
|
|
755
|
-
throw new Error("No streaming chunks received");
|
|
756
|
-
}
|
|
757
|
-
import_core9.logger.log({ chunks: chunks.length, result: result.substring(0, 50) }, "Streaming test completed");
|
|
523
|
+
import_core8.logger.log({ text }, "generated with test_text_large");
|
|
758
524
|
} catch (error) {
|
|
759
525
|
const message = error instanceof Error ? error.message : String(error);
|
|
760
|
-
|
|
526
|
+
import_core8.logger.error(`Error in test_text_large: ${message}`);
|
|
761
527
|
throw error;
|
|
762
528
|
}
|
|
763
529
|
}
|
|
@@ -766,17 +532,17 @@ var openrouterPlugin = {
|
|
|
766
532
|
name: "openrouter_test_object_small",
|
|
767
533
|
fn: async (runtime) => {
|
|
768
534
|
try {
|
|
769
|
-
const result = await runtime.useModel(
|
|
535
|
+
const result = await runtime.useModel(import_core8.ModelType.OBJECT_SMALL, {
|
|
770
536
|
prompt: "Create a simple JSON object with a message field saying hello",
|
|
771
537
|
schema: { type: "object" }
|
|
772
538
|
});
|
|
773
|
-
|
|
539
|
+
import_core8.logger.log({ result }, "Generated object with test_object_small");
|
|
774
540
|
if (!result || typeof result === "object" && "error" in result) {
|
|
775
541
|
throw new Error("Failed to generate object");
|
|
776
542
|
}
|
|
777
543
|
} catch (error) {
|
|
778
544
|
const message = error instanceof Error ? error.message : String(error);
|
|
779
|
-
|
|
545
|
+
import_core8.logger.error(`Error in test_object_small: ${message}`);
|
|
780
546
|
throw error;
|
|
781
547
|
}
|
|
782
548
|
}
|
|
@@ -785,13 +551,13 @@ var openrouterPlugin = {
|
|
|
785
551
|
name: "openrouter_test_text_embedding",
|
|
786
552
|
fn: async (runtime) => {
|
|
787
553
|
try {
|
|
788
|
-
const embedding = await runtime.useModel(
|
|
554
|
+
const embedding = await runtime.useModel(import_core8.ModelType.TEXT_EMBEDDING, {
|
|
789
555
|
text: "Hello, world!"
|
|
790
556
|
});
|
|
791
|
-
|
|
557
|
+
import_core8.logger.log({ embedding }, "embedding");
|
|
792
558
|
} catch (error) {
|
|
793
559
|
const message = error instanceof Error ? error.message : String(error);
|
|
794
|
-
|
|
560
|
+
import_core8.logger.error(`Error in test_text_embedding: ${message}`);
|
|
795
561
|
throw error;
|
|
796
562
|
}
|
|
797
563
|
}
|
|
@@ -800,6 +566,6 @@ var openrouterPlugin = {
|
|
|
800
566
|
}
|
|
801
567
|
]
|
|
802
568
|
};
|
|
803
|
-
var src_default = openrouterPlugin;
|
|
804
569
|
|
|
805
|
-
//# debugId=
|
|
570
|
+
//# debugId=04D8684535B07F9764756E2164756E21
|
|
571
|
+
//# sourceMappingURL=index.node.cjs.map
|