@elizaos/plugin-openrouter 2.0.0-alpha.8 → 2.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +125 -0
- package/auto-enable.ts +17 -0
- package/dist/browser/index.browser.js +333 -137
- package/dist/browser/index.browser.js.map +10 -11
- package/dist/cjs/index.node.cjs +334 -134
- package/dist/cjs/index.node.cjs.map +10 -11
- package/dist/node/index.node.js +333 -137
- package/dist/node/index.node.js.map +10 -11
- package/package.json +29 -13
package/dist/cjs/index.node.cjs
CHANGED
|
@@ -2,37 +2,51 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
|
|
5
|
+
function __accessProp(key) {
|
|
6
|
+
return this[key];
|
|
7
|
+
}
|
|
6
8
|
var __toCommonJS = (from) => {
|
|
7
|
-
var entry = __moduleCache.get(from), desc;
|
|
9
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
8
10
|
if (entry)
|
|
9
11
|
return entry;
|
|
10
12
|
entry = __defProp({}, "__esModule", { value: true });
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (var key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(entry, key))
|
|
16
|
+
__defProp(entry, key, {
|
|
17
|
+
get: __accessProp.bind(from, key),
|
|
18
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
19
|
+
});
|
|
20
|
+
}
|
|
16
21
|
__moduleCache.set(from, entry);
|
|
17
22
|
return entry;
|
|
18
23
|
};
|
|
24
|
+
var __moduleCache;
|
|
25
|
+
var __returnValue = (v) => v;
|
|
26
|
+
function __exportSetter(name, newValue) {
|
|
27
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
28
|
+
}
|
|
19
29
|
var __export = (target, all) => {
|
|
20
30
|
for (var name in all)
|
|
21
31
|
__defProp(target, name, {
|
|
22
32
|
get: all[name],
|
|
23
33
|
enumerable: true,
|
|
24
34
|
configurable: true,
|
|
25
|
-
set: (
|
|
35
|
+
set: __exportSetter.bind(all, name)
|
|
26
36
|
});
|
|
27
37
|
};
|
|
28
38
|
|
|
29
39
|
// index.ts
|
|
30
|
-
var
|
|
31
|
-
__export(
|
|
40
|
+
var exports_plugin_openrouter = {};
|
|
41
|
+
__export(exports_plugin_openrouter, {
|
|
32
42
|
shouldAutoCleanupImages: () => shouldAutoCleanupImages,
|
|
33
|
-
openrouterPlugin: () =>
|
|
43
|
+
openrouterPlugin: () => openrouterPlugin2,
|
|
34
44
|
getSmallModel: () => getSmallModel,
|
|
35
45
|
getSetting: () => getSetting,
|
|
46
|
+
getResponseHandlerModel: () => getResponseHandlerModel,
|
|
47
|
+
getNanoModel: () => getNanoModel,
|
|
48
|
+
getMegaModel: () => getMegaModel,
|
|
49
|
+
getMediumModel: () => getMediumModel,
|
|
36
50
|
getLargeModel: () => getLargeModel,
|
|
37
51
|
getImageModel: () => getImageModel,
|
|
38
52
|
getImageGenerationModel: () => getImageGenerationModel,
|
|
@@ -40,7 +54,8 @@ __export(exports_typescript, {
|
|
|
40
54
|
getEmbeddingDimensions: () => getEmbeddingDimensions,
|
|
41
55
|
getBaseURL: () => getBaseURL,
|
|
42
56
|
getApiKey: () => getApiKey,
|
|
43
|
-
|
|
57
|
+
getActionPlannerModel: () => getActionPlannerModel,
|
|
58
|
+
default: () => openrouterPlugin2,
|
|
44
59
|
DEFAULT_SMALL_MODEL: () => DEFAULT_SMALL_MODEL,
|
|
45
60
|
DEFAULT_LARGE_MODEL: () => DEFAULT_LARGE_MODEL,
|
|
46
61
|
DEFAULT_IMAGE_MODEL: () => DEFAULT_IMAGE_MODEL,
|
|
@@ -49,10 +64,10 @@ __export(exports_typescript, {
|
|
|
49
64
|
DEFAULT_EMBEDDING_DIMENSIONS: () => DEFAULT_EMBEDDING_DIMENSIONS,
|
|
50
65
|
DEFAULT_BASE_URL: () => DEFAULT_BASE_URL
|
|
51
66
|
});
|
|
52
|
-
module.exports = __toCommonJS(
|
|
67
|
+
module.exports = __toCommonJS(exports_plugin_openrouter);
|
|
53
68
|
|
|
54
69
|
// plugin.ts
|
|
55
|
-
var
|
|
70
|
+
var import_core6 = require("@elizaos/core");
|
|
56
71
|
|
|
57
72
|
// init.ts
|
|
58
73
|
var import_core = require("@elizaos/core");
|
|
@@ -92,9 +107,24 @@ function getApiKey(runtime) {
|
|
|
92
107
|
function getSmallModel(runtime) {
|
|
93
108
|
return getSetting(runtime, "OPENROUTER_SMALL_MODEL") ?? getSetting(runtime, "SMALL_MODEL", DEFAULT_SMALL_MODEL) ?? DEFAULT_SMALL_MODEL;
|
|
94
109
|
}
|
|
110
|
+
function getNanoModel(runtime) {
|
|
111
|
+
return getSetting(runtime, "OPENROUTER_NANO_MODEL") ?? getSetting(runtime, "NANO_MODEL") ?? getSmallModel(runtime);
|
|
112
|
+
}
|
|
113
|
+
function getMediumModel(runtime) {
|
|
114
|
+
return getSetting(runtime, "OPENROUTER_MEDIUM_MODEL") ?? getSetting(runtime, "MEDIUM_MODEL") ?? getSmallModel(runtime);
|
|
115
|
+
}
|
|
95
116
|
function getLargeModel(runtime) {
|
|
96
117
|
return getSetting(runtime, "OPENROUTER_LARGE_MODEL") ?? getSetting(runtime, "LARGE_MODEL", DEFAULT_LARGE_MODEL) ?? DEFAULT_LARGE_MODEL;
|
|
97
118
|
}
|
|
119
|
+
function getMegaModel(runtime) {
|
|
120
|
+
return getSetting(runtime, "OPENROUTER_MEGA_MODEL") ?? getSetting(runtime, "MEGA_MODEL") ?? getLargeModel(runtime);
|
|
121
|
+
}
|
|
122
|
+
function getResponseHandlerModel(runtime) {
|
|
123
|
+
return getSetting(runtime, "OPENROUTER_RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "OPENROUTER_SHOULD_RESPOND_MODEL") ?? getSetting(runtime, "RESPONSE_HANDLER_MODEL") ?? getSetting(runtime, "SHOULD_RESPOND_MODEL") ?? getNanoModel(runtime);
|
|
124
|
+
}
|
|
125
|
+
function getActionPlannerModel(runtime) {
|
|
126
|
+
return getSetting(runtime, "OPENROUTER_ACTION_PLANNER_MODEL") ?? getSetting(runtime, "OPENROUTER_PLANNER_MODEL") ?? getSetting(runtime, "ACTION_PLANNER_MODEL") ?? getSetting(runtime, "PLANNER_MODEL") ?? getMediumModel(runtime);
|
|
127
|
+
}
|
|
98
128
|
function getImageModel(runtime) {
|
|
99
129
|
return getSetting(runtime, "OPENROUTER_IMAGE_MODEL") ?? getSetting(runtime, "IMAGE_MODEL", DEFAULT_IMAGE_MODEL) ?? DEFAULT_IMAGE_MODEL;
|
|
100
130
|
}
|
|
@@ -147,22 +177,30 @@ var import_core3 = require("@elizaos/core");
|
|
|
147
177
|
|
|
148
178
|
// utils/events.ts
|
|
149
179
|
var import_core2 = require("@elizaos/core");
|
|
150
|
-
function emitModelUsageEvent(
|
|
180
|
+
function emitModelUsageEvent(runtime, modelType, _prompt, usage, modelName, modelLabel) {
|
|
151
181
|
const inputTokens = usage.inputTokens ?? usage.promptTokens ?? 0;
|
|
152
182
|
const outputTokens = usage.outputTokens ?? usage.completionTokens ?? 0;
|
|
153
183
|
const totalTokens = usage.totalTokens ?? inputTokens + outputTokens;
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
184
|
+
const model = modelName?.trim() || modelLabel?.trim() || String(modelType);
|
|
185
|
+
runtime.emitEvent(import_core2.EventType.MODEL_USED, {
|
|
186
|
+
runtime,
|
|
187
|
+
source: "openrouter",
|
|
157
188
|
provider: "openrouter",
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
189
|
+
type: modelType,
|
|
190
|
+
model,
|
|
191
|
+
modelName: model,
|
|
192
|
+
modelLabel: modelLabel ?? String(modelType),
|
|
193
|
+
tokens: {
|
|
194
|
+
prompt: inputTokens,
|
|
195
|
+
completion: outputTokens,
|
|
196
|
+
total: totalTokens
|
|
197
|
+
}
|
|
165
198
|
});
|
|
199
|
+
return {
|
|
200
|
+
promptTokens: inputTokens,
|
|
201
|
+
completionTokens: outputTokens,
|
|
202
|
+
totalTokens
|
|
203
|
+
};
|
|
166
204
|
}
|
|
167
205
|
|
|
168
206
|
// models/embedding.ts
|
|
@@ -247,7 +285,7 @@ async function handleTextEmbedding(runtime, params) {
|
|
|
247
285
|
outputTokens: 0,
|
|
248
286
|
totalTokens: data.usage.total_tokens
|
|
249
287
|
};
|
|
250
|
-
emitModelUsageEvent(runtime, import_core3.ModelType.TEXT_EMBEDDING, text, usage);
|
|
288
|
+
emitModelUsageEvent(runtime, import_core3.ModelType.TEXT_EMBEDDING, text, usage, embeddingModelName);
|
|
251
289
|
}
|
|
252
290
|
return embedding;
|
|
253
291
|
} catch (error) {
|
|
@@ -293,7 +331,7 @@ async function handleImageDescription(runtime, params) {
|
|
|
293
331
|
};
|
|
294
332
|
const response = await import_ai.generateText(generateParams);
|
|
295
333
|
if (response.usage) {
|
|
296
|
-
emitModelUsageEvent(runtime, import_core4.ModelType.IMAGE_DESCRIPTION, prompt, response.usage);
|
|
334
|
+
emitModelUsageEvent(runtime, import_core4.ModelType.IMAGE_DESCRIPTION, prompt, response.usage, modelName);
|
|
297
335
|
}
|
|
298
336
|
return response.text;
|
|
299
337
|
} catch (error) {
|
|
@@ -312,7 +350,7 @@ async function handleImageGeneration(runtime, params) {
|
|
|
312
350
|
};
|
|
313
351
|
const response = await import_ai.generateText(generateParams);
|
|
314
352
|
if (response.usage) {
|
|
315
|
-
emitModelUsageEvent(runtime, import_core4.ModelType.IMAGE, params.prompt, response.usage);
|
|
353
|
+
emitModelUsageEvent(runtime, import_core4.ModelType.IMAGE, params.prompt, response.usage, modelName);
|
|
316
354
|
}
|
|
317
355
|
return {
|
|
318
356
|
imageUrl: response.text,
|
|
@@ -325,57 +363,28 @@ async function handleImageGeneration(runtime, params) {
|
|
|
325
363
|
}
|
|
326
364
|
}
|
|
327
365
|
|
|
328
|
-
// models/
|
|
329
|
-
var import_core6 = require("@elizaos/core");
|
|
330
|
-
var import_ai2 = require("ai");
|
|
331
|
-
|
|
332
|
-
// utils/helpers.ts
|
|
366
|
+
// models/text.ts
|
|
333
367
|
var import_core5 = require("@elizaos/core");
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
const
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
output: "object",
|
|
351
|
-
prompt: params.prompt,
|
|
352
|
-
temperature
|
|
353
|
-
}) : await import_ai2.generateObject({
|
|
354
|
-
model,
|
|
355
|
-
output: "no-schema",
|
|
356
|
-
prompt: params.prompt,
|
|
357
|
-
temperature
|
|
368
|
+
var import_ai2 = require("ai");
|
|
369
|
+
var RESPONSES_ROUTED_PREFIXES = ["openai/", "anthropic/"];
|
|
370
|
+
var NO_SAMPLING_MODEL_PATTERNS = ["o1", "o3", "o4", "gpt-5", "gpt-5-mini"];
|
|
371
|
+
var TEXT_NANO_MODEL_TYPE = import_core5.ModelType.TEXT_NANO ?? "TEXT_NANO";
|
|
372
|
+
var TEXT_MEDIUM_MODEL_TYPE = import_core5.ModelType.TEXT_MEDIUM ?? "TEXT_MEDIUM";
|
|
373
|
+
var TEXT_MEGA_MODEL_TYPE = import_core5.ModelType.TEXT_MEGA ?? "TEXT_MEGA";
|
|
374
|
+
var RESPONSE_HANDLER_MODEL_TYPE = import_core5.ModelType.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
|
|
375
|
+
var ACTION_PLANNER_MODEL_TYPE = import_core5.ModelType.ACTION_PLANNER ?? "ACTION_PLANNER";
|
|
376
|
+
function buildUserContent(params) {
|
|
377
|
+
const content = [{ type: "text", text: params.prompt }];
|
|
378
|
+
for (const attachment of params.attachments ?? []) {
|
|
379
|
+
content.push({
|
|
380
|
+
type: "file",
|
|
381
|
+
data: attachment.data,
|
|
382
|
+
mediaType: attachment.mediaType,
|
|
383
|
+
...attachment.filename ? { filename: attachment.filename } : {}
|
|
358
384
|
});
|
|
359
|
-
if (usage) {
|
|
360
|
-
emitModelUsageEvent(runtime, modelType, params.prompt, usage);
|
|
361
|
-
}
|
|
362
|
-
return object;
|
|
363
|
-
} catch (error) {
|
|
364
|
-
return handleObjectGenerationError(error);
|
|
365
385
|
}
|
|
386
|
+
return content;
|
|
366
387
|
}
|
|
367
|
-
async function handleObjectSmall(runtime, params) {
|
|
368
|
-
return generateObjectWithModel(runtime, import_core6.ModelType.OBJECT_SMALL, params);
|
|
369
|
-
}
|
|
370
|
-
async function handleObjectLarge(runtime, params) {
|
|
371
|
-
return generateObjectWithModel(runtime, import_core6.ModelType.OBJECT_LARGE, params);
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
// models/text.ts
|
|
375
|
-
var import_core7 = require("@elizaos/core");
|
|
376
|
-
var import_ai3 = require("ai");
|
|
377
|
-
var RESPONSES_ROUTED_PREFIXES = ["openai/", "anthropic/"];
|
|
378
|
-
var NO_SAMPLING_MODEL_PATTERNS = ["o1", "o3", "o4", "gpt-5", "gpt-5-mini"];
|
|
379
388
|
function supportsSamplingParameters(modelName) {
|
|
380
389
|
const lowerModelName = modelName.toLowerCase();
|
|
381
390
|
if (RESPONSES_ROUTED_PREFIXES.some((prefix) => lowerModelName.startsWith(prefix))) {
|
|
@@ -383,74 +392,222 @@ function supportsSamplingParameters(modelName) {
|
|
|
383
392
|
}
|
|
384
393
|
return !NO_SAMPLING_MODEL_PATTERNS.some((pattern) => lowerModelName.includes(pattern));
|
|
385
394
|
}
|
|
395
|
+
function buildStructuredOutput(responseSchema) {
|
|
396
|
+
if (responseSchema && typeof responseSchema === "object" && "responseFormat" in responseSchema && "parseCompleteOutput" in responseSchema) {
|
|
397
|
+
return responseSchema;
|
|
398
|
+
}
|
|
399
|
+
const schemaOptions = responseSchema && typeof responseSchema === "object" && "schema" in responseSchema ? responseSchema : { schema: responseSchema };
|
|
400
|
+
return {
|
|
401
|
+
name: "object",
|
|
402
|
+
responseFormat: Promise.resolve({
|
|
403
|
+
type: "json",
|
|
404
|
+
schema: schemaOptions.schema,
|
|
405
|
+
...schemaOptions.name ? { name: schemaOptions.name } : {},
|
|
406
|
+
...schemaOptions.description ? { description: schemaOptions.description } : {}
|
|
407
|
+
}),
|
|
408
|
+
async parseCompleteOutput({ text }) {
|
|
409
|
+
return JSON.parse(text);
|
|
410
|
+
},
|
|
411
|
+
async parsePartialOutput() {
|
|
412
|
+
return;
|
|
413
|
+
},
|
|
414
|
+
createElementStreamTransform() {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
function usesNativeTextResult(params) {
|
|
420
|
+
return Boolean(params.messages || params.tools || params.toolChoice || params.responseSchema);
|
|
421
|
+
}
|
|
422
|
+
function getModelNameForType(runtime, modelType) {
|
|
423
|
+
switch (modelType) {
|
|
424
|
+
case TEXT_NANO_MODEL_TYPE:
|
|
425
|
+
return getNanoModel(runtime);
|
|
426
|
+
case TEXT_MEDIUM_MODEL_TYPE:
|
|
427
|
+
return getMediumModel(runtime);
|
|
428
|
+
case import_core5.ModelType.TEXT_SMALL:
|
|
429
|
+
return getSmallModel(runtime);
|
|
430
|
+
case import_core5.ModelType.TEXT_LARGE:
|
|
431
|
+
return getLargeModel(runtime);
|
|
432
|
+
case TEXT_MEGA_MODEL_TYPE:
|
|
433
|
+
return getMegaModel(runtime);
|
|
434
|
+
case RESPONSE_HANDLER_MODEL_TYPE:
|
|
435
|
+
return getResponseHandlerModel(runtime);
|
|
436
|
+
case ACTION_PLANNER_MODEL_TYPE:
|
|
437
|
+
return getActionPlannerModel(runtime);
|
|
438
|
+
default:
|
|
439
|
+
return getLargeModel(runtime);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
function getModelLabelForType(modelType) {
|
|
443
|
+
switch (modelType) {
|
|
444
|
+
case TEXT_NANO_MODEL_TYPE:
|
|
445
|
+
return "TEXT_NANO";
|
|
446
|
+
case TEXT_MEDIUM_MODEL_TYPE:
|
|
447
|
+
return "TEXT_MEDIUM";
|
|
448
|
+
case import_core5.ModelType.TEXT_SMALL:
|
|
449
|
+
return "TEXT_SMALL";
|
|
450
|
+
case import_core5.ModelType.TEXT_LARGE:
|
|
451
|
+
return "TEXT_LARGE";
|
|
452
|
+
case TEXT_MEGA_MODEL_TYPE:
|
|
453
|
+
return "TEXT_MEGA";
|
|
454
|
+
case RESPONSE_HANDLER_MODEL_TYPE:
|
|
455
|
+
return "RESPONSE_HANDLER";
|
|
456
|
+
case ACTION_PLANNER_MODEL_TYPE:
|
|
457
|
+
return "ACTION_PLANNER";
|
|
458
|
+
default:
|
|
459
|
+
return String(modelType);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
386
462
|
function buildGenerateParams(runtime, modelType, params) {
|
|
463
|
+
const paramsWithAttachments = params;
|
|
387
464
|
const { prompt } = params;
|
|
388
465
|
const paramsWithMax = params;
|
|
389
466
|
const resolvedMaxOutput = paramsWithMax.maxOutputTokens ?? paramsWithMax.maxTokens ?? 8192;
|
|
390
467
|
const openrouter = createOpenRouterProvider(runtime);
|
|
391
|
-
const modelName =
|
|
392
|
-
const modelLabel = modelType
|
|
468
|
+
const modelName = getModelNameForType(runtime, modelType);
|
|
469
|
+
const modelLabel = getModelLabelForType(modelType);
|
|
393
470
|
const supportsSampling = supportsSamplingParameters(modelName);
|
|
394
471
|
const stopSequences = Array.isArray(params.stopSequences) && params.stopSequences.length > 0 ? params.stopSequences : undefined;
|
|
472
|
+
const userContent = (paramsWithAttachments.attachments?.length ?? 0) > 0 ? buildUserContent(paramsWithAttachments) : undefined;
|
|
395
473
|
const temperature = params.temperature ?? 0.7;
|
|
396
474
|
const frequencyPenalty = params.frequencyPenalty ?? 0.7;
|
|
397
475
|
const presencePenalty = params.presencePenalty ?? 0.7;
|
|
476
|
+
const systemPrompt = import_core5.resolveEffectiveSystemPrompt({
|
|
477
|
+
params: paramsWithAttachments,
|
|
478
|
+
fallback: import_core5.buildCanonicalSystemPrompt({ character: runtime.character })
|
|
479
|
+
});
|
|
480
|
+
const wireMessages = import_core5.dropDuplicateLeadingSystemMessage(paramsWithAttachments.messages, systemPrompt);
|
|
481
|
+
const promptOrMessages = paramsWithAttachments.messages ? wireMessages && wireMessages.length > 0 ? { messages: wireMessages } : userContent ? { messages: [{ role: "user", content: userContent }] } : { prompt } : userContent ? { messages: [{ role: "user", content: userContent }] } : { prompt };
|
|
482
|
+
const rawProviderOptions = paramsWithAttachments.providerOptions;
|
|
483
|
+
const { openrouter: rawOpenrouterOptions, ...restProviderOptions } = rawProviderOptions ?? {};
|
|
484
|
+
const openrouterOptions = {
|
|
485
|
+
...rawOpenrouterOptions ?? {}
|
|
486
|
+
};
|
|
487
|
+
const mergedProviderOptions = {
|
|
488
|
+
...restProviderOptions,
|
|
489
|
+
...Object.keys(openrouterOptions).length > 0 ? { openrouter: openrouterOptions } : {}
|
|
490
|
+
};
|
|
491
|
+
const resolvedProviderOptions = Object.keys(mergedProviderOptions).length > 0 ? mergedProviderOptions : undefined;
|
|
398
492
|
const generateParams = {
|
|
399
493
|
model: openrouter.chat(modelName),
|
|
400
|
-
|
|
401
|
-
system:
|
|
494
|
+
...promptOrMessages,
|
|
495
|
+
system: systemPrompt,
|
|
402
496
|
...supportsSampling ? {
|
|
403
497
|
temperature,
|
|
404
498
|
frequencyPenalty,
|
|
405
499
|
presencePenalty,
|
|
406
500
|
...stopSequences ? { stopSequences } : {}
|
|
407
501
|
} : {},
|
|
408
|
-
maxOutputTokens: resolvedMaxOutput
|
|
502
|
+
maxOutputTokens: resolvedMaxOutput,
|
|
503
|
+
...paramsWithAttachments.tools ? { tools: paramsWithAttachments.tools } : {},
|
|
504
|
+
...paramsWithAttachments.toolChoice ? { toolChoice: paramsWithAttachments.toolChoice } : {},
|
|
505
|
+
...paramsWithAttachments.responseSchema ? { output: buildStructuredOutput(paramsWithAttachments.responseSchema) } : {},
|
|
506
|
+
...resolvedProviderOptions ? { providerOptions: resolvedProviderOptions } : {}
|
|
409
507
|
};
|
|
410
|
-
return { generateParams, modelName, modelLabel, prompt };
|
|
411
|
-
}
|
|
412
|
-
function handleStreamingGeneration(runtime, modelType, generateParams, prompt, _modelLabel) {
|
|
413
|
-
const streamResult = import_ai3.streamText(generateParams);
|
|
414
508
|
return {
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
};
|
|
427
|
-
}
|
|
509
|
+
generateParams,
|
|
510
|
+
modelName,
|
|
511
|
+
modelLabel,
|
|
512
|
+
prompt,
|
|
513
|
+
shouldReturnNativeResult: usesNativeTextResult(paramsWithAttachments)
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
function handleStreamingGeneration(runtime, modelType, generateParams, prompt, modelName, modelLabel, shouldReturnNativeResult) {
|
|
517
|
+
const streamResult = import_ai2.streamText(generateParams);
|
|
518
|
+
const usagePromise = Promise.resolve(streamResult.usage).then((usage) => {
|
|
519
|
+
if (!usage) {
|
|
428
520
|
return;
|
|
521
|
+
}
|
|
522
|
+
return emitModelUsageEvent(runtime, modelType, prompt, usage, modelName, modelLabel);
|
|
523
|
+
});
|
|
524
|
+
const ignoreUsageError = () => {
|
|
525
|
+
return;
|
|
526
|
+
};
|
|
527
|
+
async function* textStreamWithUsage() {
|
|
528
|
+
let completed = false;
|
|
529
|
+
try {
|
|
530
|
+
for await (const chunk of streamResult.textStream) {
|
|
531
|
+
yield chunk;
|
|
532
|
+
}
|
|
533
|
+
completed = true;
|
|
534
|
+
} finally {
|
|
535
|
+
if (completed) {
|
|
536
|
+
await usagePromise.catch(ignoreUsageError);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
return {
|
|
541
|
+
textStream: textStreamWithUsage(),
|
|
542
|
+
text: Promise.resolve(streamResult.text).then(async (text) => {
|
|
543
|
+
await usagePromise.catch(ignoreUsageError);
|
|
544
|
+
return text;
|
|
429
545
|
}),
|
|
546
|
+
...shouldReturnNativeResult ? { toolCalls: Promise.resolve(streamResult.toolCalls) } : {},
|
|
547
|
+
usage: usagePromise,
|
|
430
548
|
finishReason: Promise.resolve(streamResult.finishReason)
|
|
431
549
|
};
|
|
432
550
|
}
|
|
551
|
+
function buildNativeTextResult(result) {
|
|
552
|
+
const inputTokens = result.usage?.inputTokens ?? result.usage?.promptTokens ?? 0;
|
|
553
|
+
const outputTokens = result.usage?.outputTokens ?? result.usage?.completionTokens ?? 0;
|
|
554
|
+
if (!result.usage) {
|
|
555
|
+
return {
|
|
556
|
+
text: result.text,
|
|
557
|
+
toolCalls: result.toolCalls ?? [],
|
|
558
|
+
finishReason: result.finishReason
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
const cacheRead = result.usage.cacheReadInputTokens ?? result.usage.cachedInputTokens;
|
|
562
|
+
const cacheCreation = result.usage.cacheCreationInputTokens;
|
|
563
|
+
const usage = {
|
|
564
|
+
promptTokens: inputTokens,
|
|
565
|
+
completionTokens: outputTokens,
|
|
566
|
+
totalTokens: result.usage.totalTokens ?? inputTokens + outputTokens,
|
|
567
|
+
...typeof cacheRead === "number" ? { cacheReadInputTokens: cacheRead } : {},
|
|
568
|
+
...typeof cacheCreation === "number" ? { cacheCreationInputTokens: cacheCreation } : {}
|
|
569
|
+
};
|
|
570
|
+
return {
|
|
571
|
+
text: result.text,
|
|
572
|
+
toolCalls: result.toolCalls ?? [],
|
|
573
|
+
finishReason: result.finishReason,
|
|
574
|
+
usage
|
|
575
|
+
};
|
|
576
|
+
}
|
|
433
577
|
async function generateTextWithModel(runtime, modelType, params) {
|
|
434
|
-
const {
|
|
435
|
-
generateParams,
|
|
436
|
-
modelName: _modelName,
|
|
437
|
-
modelLabel,
|
|
438
|
-
prompt
|
|
439
|
-
} = buildGenerateParams(runtime, modelType, params);
|
|
578
|
+
const { generateParams, modelName, modelLabel, prompt, shouldReturnNativeResult } = buildGenerateParams(runtime, modelType, params);
|
|
440
579
|
if (params.stream) {
|
|
441
|
-
return handleStreamingGeneration(runtime, modelType, generateParams, prompt, modelLabel);
|
|
580
|
+
return handleStreamingGeneration(runtime, modelType, generateParams, prompt, modelName, modelLabel, shouldReturnNativeResult);
|
|
442
581
|
}
|
|
443
|
-
const response = await
|
|
582
|
+
const response = await import_ai2.generateText(generateParams);
|
|
444
583
|
if (response.usage) {
|
|
445
|
-
emitModelUsageEvent(runtime, modelType, prompt, response.usage);
|
|
584
|
+
emitModelUsageEvent(runtime, modelType, prompt, response.usage, modelName, modelLabel);
|
|
585
|
+
}
|
|
586
|
+
if (shouldReturnNativeResult) {
|
|
587
|
+
return buildNativeTextResult(response);
|
|
446
588
|
}
|
|
447
589
|
return response.text;
|
|
448
590
|
}
|
|
449
591
|
async function handleTextSmall(runtime, params) {
|
|
450
|
-
return generateTextWithModel(runtime,
|
|
592
|
+
return generateTextWithModel(runtime, import_core5.ModelType.TEXT_SMALL, params);
|
|
593
|
+
}
|
|
594
|
+
async function handleTextNano(runtime, params) {
|
|
595
|
+
return generateTextWithModel(runtime, TEXT_NANO_MODEL_TYPE, params);
|
|
596
|
+
}
|
|
597
|
+
async function handleTextMedium(runtime, params) {
|
|
598
|
+
return generateTextWithModel(runtime, TEXT_MEDIUM_MODEL_TYPE, params);
|
|
451
599
|
}
|
|
452
600
|
async function handleTextLarge(runtime, params) {
|
|
453
|
-
return generateTextWithModel(runtime,
|
|
601
|
+
return generateTextWithModel(runtime, import_core5.ModelType.TEXT_LARGE, params);
|
|
602
|
+
}
|
|
603
|
+
async function handleTextMega(runtime, params) {
|
|
604
|
+
return generateTextWithModel(runtime, TEXT_MEGA_MODEL_TYPE, params);
|
|
605
|
+
}
|
|
606
|
+
async function handleResponseHandler(runtime, params) {
|
|
607
|
+
return generateTextWithModel(runtime, RESPONSE_HANDLER_MODEL_TYPE, params);
|
|
608
|
+
}
|
|
609
|
+
async function handleActionPlanner(runtime, params) {
|
|
610
|
+
return generateTextWithModel(runtime, ACTION_PLANNER_MODEL_TYPE, params);
|
|
454
611
|
}
|
|
455
612
|
|
|
456
613
|
// plugin.ts
|
|
@@ -461,21 +618,43 @@ function getProcessEnv() {
|
|
|
461
618
|
return process.env;
|
|
462
619
|
}
|
|
463
620
|
var env = getProcessEnv();
|
|
621
|
+
var TEXT_NANO_MODEL_TYPE2 = import_core6.ModelType.TEXT_NANO ?? "TEXT_NANO";
|
|
622
|
+
var TEXT_MEDIUM_MODEL_TYPE2 = import_core6.ModelType.TEXT_MEDIUM ?? "TEXT_MEDIUM";
|
|
623
|
+
var TEXT_MEGA_MODEL_TYPE2 = import_core6.ModelType.TEXT_MEGA ?? "TEXT_MEGA";
|
|
624
|
+
var RESPONSE_HANDLER_MODEL_TYPE2 = import_core6.ModelType.RESPONSE_HANDLER ?? "RESPONSE_HANDLER";
|
|
625
|
+
var ACTION_PLANNER_MODEL_TYPE2 = import_core6.ModelType.ACTION_PLANNER ?? "ACTION_PLANNER";
|
|
464
626
|
var openrouterPlugin = {
|
|
465
627
|
name: "openrouter",
|
|
466
628
|
description: "OpenRouter multi-model AI gateway plugin",
|
|
629
|
+
autoEnable: {
|
|
630
|
+
envKeys: ["OPENROUTER_API_KEY"]
|
|
631
|
+
},
|
|
467
632
|
config: {
|
|
468
633
|
OPENROUTER_API_KEY: env.OPENROUTER_API_KEY ?? null,
|
|
469
634
|
OPENROUTER_BASE_URL: env.OPENROUTER_BASE_URL ?? null,
|
|
635
|
+
OPENROUTER_NANO_MODEL: env.OPENROUTER_NANO_MODEL ?? null,
|
|
636
|
+
OPENROUTER_MEDIUM_MODEL: env.OPENROUTER_MEDIUM_MODEL ?? null,
|
|
470
637
|
OPENROUTER_SMALL_MODEL: env.OPENROUTER_SMALL_MODEL ?? null,
|
|
471
638
|
OPENROUTER_LARGE_MODEL: env.OPENROUTER_LARGE_MODEL ?? null,
|
|
639
|
+
OPENROUTER_MEGA_MODEL: env.OPENROUTER_MEGA_MODEL ?? null,
|
|
640
|
+
OPENROUTER_RESPONSE_HANDLER_MODEL: env.OPENROUTER_RESPONSE_HANDLER_MODEL ?? null,
|
|
641
|
+
OPENROUTER_SHOULD_RESPOND_MODEL: env.OPENROUTER_SHOULD_RESPOND_MODEL ?? null,
|
|
642
|
+
OPENROUTER_ACTION_PLANNER_MODEL: env.OPENROUTER_ACTION_PLANNER_MODEL ?? null,
|
|
643
|
+
OPENROUTER_PLANNER_MODEL: env.OPENROUTER_PLANNER_MODEL ?? null,
|
|
472
644
|
OPENROUTER_IMAGE_MODEL: env.OPENROUTER_IMAGE_MODEL ?? null,
|
|
473
645
|
OPENROUTER_IMAGE_GENERATION_MODEL: env.OPENROUTER_IMAGE_GENERATION_MODEL ?? null,
|
|
474
646
|
OPENROUTER_EMBEDDING_MODEL: env.OPENROUTER_EMBEDDING_MODEL ?? null,
|
|
475
647
|
OPENROUTER_EMBEDDING_DIMENSIONS: env.OPENROUTER_EMBEDDING_DIMENSIONS ?? null,
|
|
476
648
|
OPENROUTER_AUTO_CLEANUP_IMAGES: env.OPENROUTER_AUTO_CLEANUP_IMAGES ?? null,
|
|
649
|
+
NANO_MODEL: env.NANO_MODEL ?? null,
|
|
650
|
+
MEDIUM_MODEL: env.MEDIUM_MODEL ?? null,
|
|
477
651
|
SMALL_MODEL: env.SMALL_MODEL ?? null,
|
|
478
652
|
LARGE_MODEL: env.LARGE_MODEL ?? null,
|
|
653
|
+
MEGA_MODEL: env.MEGA_MODEL ?? null,
|
|
654
|
+
RESPONSE_HANDLER_MODEL: env.RESPONSE_HANDLER_MODEL ?? null,
|
|
655
|
+
SHOULD_RESPOND_MODEL: env.SHOULD_RESPOND_MODEL ?? null,
|
|
656
|
+
ACTION_PLANNER_MODEL: env.ACTION_PLANNER_MODEL ?? null,
|
|
657
|
+
PLANNER_MODEL: env.PLANNER_MODEL ?? null,
|
|
479
658
|
IMAGE_MODEL: env.IMAGE_MODEL ?? null,
|
|
480
659
|
IMAGE_GENERATION_MODEL: env.IMAGE_GENERATION_MODEL ?? null,
|
|
481
660
|
EMBEDDING_MODEL: env.EMBEDDING_MODEL ?? null,
|
|
@@ -485,27 +664,36 @@ var openrouterPlugin = {
|
|
|
485
664
|
initializeOpenRouter(config, runtime);
|
|
486
665
|
},
|
|
487
666
|
models: {
|
|
488
|
-
[
|
|
667
|
+
[TEXT_NANO_MODEL_TYPE2]: async (runtime, params) => {
|
|
668
|
+
return handleTextNano(runtime, params);
|
|
669
|
+
},
|
|
670
|
+
[import_core6.ModelType.TEXT_SMALL]: async (runtime, params) => {
|
|
489
671
|
return handleTextSmall(runtime, params);
|
|
490
672
|
},
|
|
491
|
-
[
|
|
673
|
+
[TEXT_MEDIUM_MODEL_TYPE2]: async (runtime, params) => {
|
|
674
|
+
return handleTextMedium(runtime, params);
|
|
675
|
+
},
|
|
676
|
+
[import_core6.ModelType.TEXT_LARGE]: async (runtime, params) => {
|
|
492
677
|
return handleTextLarge(runtime, params);
|
|
493
678
|
},
|
|
494
|
-
[
|
|
495
|
-
return
|
|
679
|
+
[TEXT_MEGA_MODEL_TYPE2]: async (runtime, params) => {
|
|
680
|
+
return handleTextMega(runtime, params);
|
|
681
|
+
},
|
|
682
|
+
[RESPONSE_HANDLER_MODEL_TYPE2]: async (runtime, params) => {
|
|
683
|
+
return handleResponseHandler(runtime, params);
|
|
496
684
|
},
|
|
497
|
-
[
|
|
498
|
-
return
|
|
685
|
+
[ACTION_PLANNER_MODEL_TYPE2]: async (runtime, params) => {
|
|
686
|
+
return handleActionPlanner(runtime, params);
|
|
499
687
|
},
|
|
500
|
-
[
|
|
688
|
+
[import_core6.ModelType.IMAGE_DESCRIPTION]: async (runtime, params) => {
|
|
501
689
|
const description = await handleImageDescription(runtime, params);
|
|
502
690
|
return { title: "", description };
|
|
503
691
|
},
|
|
504
|
-
[
|
|
692
|
+
[import_core6.ModelType.IMAGE]: async (runtime, params) => {
|
|
505
693
|
const result = await handleImageGeneration(runtime, params);
|
|
506
694
|
return [{ url: result.imageUrl }];
|
|
507
695
|
},
|
|
508
|
-
[
|
|
696
|
+
[import_core6.ModelType.TEXT_EMBEDDING]: async (runtime, params) => {
|
|
509
697
|
return handleTextEmbedding(runtime, params);
|
|
510
698
|
}
|
|
511
699
|
},
|
|
@@ -517,16 +705,17 @@ var openrouterPlugin = {
|
|
|
517
705
|
name: "openrouter_test_text_small",
|
|
518
706
|
fn: async (runtime) => {
|
|
519
707
|
try {
|
|
520
|
-
const
|
|
708
|
+
const runModel = runtime.useModel.bind(runtime);
|
|
709
|
+
const text = await runModel(import_core6.ModelType.TEXT_SMALL, {
|
|
521
710
|
prompt: "What is the nature of reality in 10 words?"
|
|
522
711
|
});
|
|
523
712
|
if (text.length === 0) {
|
|
524
713
|
throw new Error("Failed to generate text");
|
|
525
714
|
}
|
|
526
|
-
|
|
715
|
+
import_core6.logger.log({ text }, "generated with test_text_small");
|
|
527
716
|
} catch (error) {
|
|
528
717
|
const message = error instanceof Error ? error.message : String(error);
|
|
529
|
-
|
|
718
|
+
import_core6.logger.error(`Error in test_text_small: ${message}`);
|
|
530
719
|
throw error;
|
|
531
720
|
}
|
|
532
721
|
}
|
|
@@ -535,35 +724,41 @@ var openrouterPlugin = {
|
|
|
535
724
|
name: "openrouter_test_text_large",
|
|
536
725
|
fn: async (runtime) => {
|
|
537
726
|
try {
|
|
538
|
-
const
|
|
727
|
+
const runModel = runtime.useModel.bind(runtime);
|
|
728
|
+
const text = await runModel(import_core6.ModelType.TEXT_LARGE, {
|
|
539
729
|
prompt: "What is the nature of reality in 10 words?"
|
|
540
730
|
});
|
|
541
731
|
if (text.length === 0) {
|
|
542
732
|
throw new Error("Failed to generate text");
|
|
543
733
|
}
|
|
544
|
-
|
|
734
|
+
import_core6.logger.log({ text }, "generated with test_text_large");
|
|
545
735
|
} catch (error) {
|
|
546
736
|
const message = error instanceof Error ? error.message : String(error);
|
|
547
|
-
|
|
737
|
+
import_core6.logger.error(`Error in test_text_large: ${message}`);
|
|
548
738
|
throw error;
|
|
549
739
|
}
|
|
550
740
|
}
|
|
551
741
|
},
|
|
552
742
|
{
|
|
553
|
-
name: "
|
|
743
|
+
name: "openrouter_test_structured_output_via_text_large",
|
|
554
744
|
fn: async (runtime) => {
|
|
555
745
|
try {
|
|
556
|
-
const
|
|
746
|
+
const runModel = runtime.useModel.bind(runtime);
|
|
747
|
+
const result = await runModel(import_core6.ModelType.TEXT_LARGE, {
|
|
557
748
|
prompt: "Create a simple JSON object with a message field saying hello",
|
|
558
|
-
|
|
749
|
+
responseSchema: {
|
|
750
|
+
type: "object",
|
|
751
|
+
properties: { message: { type: "string" } },
|
|
752
|
+
required: ["message"]
|
|
753
|
+
}
|
|
559
754
|
});
|
|
560
|
-
|
|
561
|
-
if (!result
|
|
562
|
-
throw new Error("Failed to generate
|
|
755
|
+
import_core6.logger.log({ result }, "Generated structured output via TEXT_LARGE");
|
|
756
|
+
if (!result) {
|
|
757
|
+
throw new Error("Failed to generate structured output");
|
|
563
758
|
}
|
|
564
759
|
} catch (error) {
|
|
565
760
|
const message = error instanceof Error ? error.message : String(error);
|
|
566
|
-
|
|
761
|
+
import_core6.logger.error(`Error in test_structured_output_via_text_large: ${message}`);
|
|
567
762
|
throw error;
|
|
568
763
|
}
|
|
569
764
|
}
|
|
@@ -572,13 +767,14 @@ var openrouterPlugin = {
|
|
|
572
767
|
name: "openrouter_test_text_embedding",
|
|
573
768
|
fn: async (runtime) => {
|
|
574
769
|
try {
|
|
575
|
-
const
|
|
770
|
+
const runModel = runtime.useModel.bind(runtime);
|
|
771
|
+
const embedding = await runModel(import_core6.ModelType.TEXT_EMBEDDING, {
|
|
576
772
|
text: "Hello, world!"
|
|
577
773
|
});
|
|
578
|
-
|
|
774
|
+
import_core6.logger.log({ embedding }, "embedding");
|
|
579
775
|
} catch (error) {
|
|
580
776
|
const message = error instanceof Error ? error.message : String(error);
|
|
581
|
-
|
|
777
|
+
import_core6.logger.error(`Error in test_text_embedding: ${message}`);
|
|
582
778
|
throw error;
|
|
583
779
|
}
|
|
584
780
|
}
|
|
@@ -587,6 +783,10 @@ var openrouterPlugin = {
|
|
|
587
783
|
}
|
|
588
784
|
]
|
|
589
785
|
};
|
|
786
|
+
var plugin_default = openrouterPlugin;
|
|
787
|
+
|
|
788
|
+
// index.ts
|
|
789
|
+
var openrouterPlugin2 = plugin_default;
|
|
590
790
|
|
|
591
|
-
//# debugId=
|
|
791
|
+
//# debugId=9F505D89FE343F3864756E2164756E21
|
|
592
792
|
//# sourceMappingURL=index.node.cjs.map
|