@memberjunction/ai-openrouter 6.2.0-edge.5 → 6.2.0-edge.6
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.
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
import { ChatParams } from "@memberjunction/ai";
|
|
2
2
|
import { OpenAILLM } from "@memberjunction/ai-openai";
|
|
3
|
+
/**
|
|
4
|
+
* The environment variable naming an ordered model preference for OpenRouter.
|
|
5
|
+
*
|
|
6
|
+
* Comma-separated, highest preference first, e.g.
|
|
7
|
+
* MJ_OPENROUTER_MODEL_PREFERENCE="google/gemini-3.8-flash,anthropic/claude-3-5-haiku"
|
|
8
|
+
*
|
|
9
|
+
* WHY AN ENVIRONMENT VARIABLE. This preference has to reach three consumers that
|
|
10
|
+
* configure themselves in three different ways — a long-running server, a CodeGen
|
|
11
|
+
* run, and the standalone `db-auto-doc` CLI, which builds this very class through
|
|
12
|
+
* MJGlobal's ClassFactory and takes its own configuration from a JSON file it is
|
|
13
|
+
* handed. The environment is the only channel all three share.
|
|
14
|
+
*
|
|
15
|
+
* Unset changes nothing: the request body is byte-for-byte what it was before.
|
|
16
|
+
*/
|
|
17
|
+
export declare const OPENROUTER_MODEL_PREFERENCE_ENV = "MJ_OPENROUTER_MODEL_PREFERENCE";
|
|
3
18
|
/**
|
|
4
19
|
* OpenRouter implementation is just a sub-class of the OpenAILLM that overrides the base url.
|
|
5
20
|
*
|
|
@@ -11,10 +26,37 @@ import { OpenAILLM } from "@memberjunction/ai-openai";
|
|
|
11
26
|
*/
|
|
12
27
|
export declare class OpenRouterLLM extends OpenAILLM {
|
|
13
28
|
constructor(apiKey: string);
|
|
29
|
+
/**
|
|
30
|
+
* The configured preference order, or an empty array when none is set.
|
|
31
|
+
*
|
|
32
|
+
* Read at CALL time rather than construction: a long-running server is not
|
|
33
|
+
* the only consumer, and a process that configures its environment after
|
|
34
|
+
* importing this module must still see the preference.
|
|
35
|
+
*/
|
|
36
|
+
protected getModelPreference(env?: NodeJS.ProcessEnv): string[];
|
|
14
37
|
/**
|
|
15
38
|
* Request OpenRouter's usage accounting so the response includes `usage.cost` and
|
|
16
39
|
* `usage.cost_details`. The OpenAI SDK forwards this extra body param unchanged.
|
|
17
40
|
* @see https://openrouter.ai/docs/use-cases/usage-accounting
|
|
41
|
+
*
|
|
42
|
+
* ORDERED MODEL PREFERENCE. When {@link OPENROUTER_MODEL_PREFERENCE_ENV} is set,
|
|
43
|
+
* the request also carries OpenRouter's `models` array: an ordered list it walks
|
|
44
|
+
* on ANY error from the one before — downtime, rate limiting, moderation, and
|
|
45
|
+
* context-length validation. Billing follows the model that actually answered,
|
|
46
|
+
* not the ones that failed.
|
|
47
|
+
*
|
|
48
|
+
* The caller's own model is always appended last, so a preference can never make
|
|
49
|
+
* a deliberately chosen model unreachable — it only gets tried after the
|
|
50
|
+
* preferred ones decline. That also makes the context-length case degrade
|
|
51
|
+
* correctly: a prompt sized for a large-context model overflows the preferred
|
|
52
|
+
* small one, OpenRouter treats that as an error, and the fall-through lands on
|
|
53
|
+
* the model the caller asked for in the first place.
|
|
54
|
+
*
|
|
55
|
+
* `model` IS SET AS WELL, to the head of the same list, and that is deliberate
|
|
56
|
+
* rather than redundant. OpenRouter documents both fields but never states which
|
|
57
|
+
* wins when both are present. Setting them consistently means the same model
|
|
58
|
+
* leads under either reading, instead of the behaviour depending on an ambiguity
|
|
59
|
+
* in someone else's documentation.
|
|
18
60
|
*/
|
|
19
61
|
protected getProviderRequestExtras(params: ChatParams): Record<string, unknown>;
|
|
20
62
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openRouter.d.ts","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAItD;;;;;;;;GAQG;AACH,qBACa,aAAc,SAAQ,SAAS;gBAC5B,MAAM,EAAE,MAAM;IAI1B
|
|
1
|
+
{"version":3,"file":"openRouter.d.ts","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAEzD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAItD;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,+BAA+B,mCAAmC,CAAC;AAEhF;;;;;;;;GAQG;AACH,qBACa,aAAc,SAAQ,SAAS;gBAC5B,MAAM,EAAE,MAAM;IAI1B;;;;;;OAMG;IACH,SAAS,CAAC,kBAAkB,CAAC,GAAG,GAAE,MAAM,CAAC,UAAwB,GAAG,MAAM,EAAE;IAW5E;;;;;;;;;;;;;;;;;;;;;;;OAuBG;cACgB,wBAAwB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAwB3F"}
|
|
@@ -11,6 +11,21 @@ import { BaseLLM } from "@memberjunction/ai";
|
|
|
11
11
|
import { RegisterClass } from '@memberjunction/global';
|
|
12
12
|
import { OpenAILLM } from "@memberjunction/ai-openai";
|
|
13
13
|
const __openRouterURL = 'https://openrouter.ai/api/v1';
|
|
14
|
+
/**
|
|
15
|
+
* The environment variable naming an ordered model preference for OpenRouter.
|
|
16
|
+
*
|
|
17
|
+
* Comma-separated, highest preference first, e.g.
|
|
18
|
+
* MJ_OPENROUTER_MODEL_PREFERENCE="google/gemini-3.8-flash,anthropic/claude-3-5-haiku"
|
|
19
|
+
*
|
|
20
|
+
* WHY AN ENVIRONMENT VARIABLE. This preference has to reach three consumers that
|
|
21
|
+
* configure themselves in three different ways — a long-running server, a CodeGen
|
|
22
|
+
* run, and the standalone `db-auto-doc` CLI, which builds this very class through
|
|
23
|
+
* MJGlobal's ClassFactory and takes its own configuration from a JSON file it is
|
|
24
|
+
* handed. The environment is the only channel all three share.
|
|
25
|
+
*
|
|
26
|
+
* Unset changes nothing: the request body is byte-for-byte what it was before.
|
|
27
|
+
*/
|
|
28
|
+
export const OPENROUTER_MODEL_PREFERENCE_ENV = 'MJ_OPENROUTER_MODEL_PREFERENCE';
|
|
14
29
|
/**
|
|
15
30
|
* OpenRouter implementation is just a sub-class of the OpenAILLM that overrides the base url.
|
|
16
31
|
*
|
|
@@ -24,13 +39,67 @@ let OpenRouterLLM = class OpenRouterLLM extends OpenAILLM {
|
|
|
24
39
|
constructor(apiKey) {
|
|
25
40
|
super(apiKey, __openRouterURL);
|
|
26
41
|
}
|
|
42
|
+
/**
|
|
43
|
+
* The configured preference order, or an empty array when none is set.
|
|
44
|
+
*
|
|
45
|
+
* Read at CALL time rather than construction: a long-running server is not
|
|
46
|
+
* the only consumer, and a process that configures its environment after
|
|
47
|
+
* importing this module must still see the preference.
|
|
48
|
+
*/
|
|
49
|
+
getModelPreference(env = process.env) {
|
|
50
|
+
const raw = env[OPENROUTER_MODEL_PREFERENCE_ENV];
|
|
51
|
+
if (typeof raw !== 'string') {
|
|
52
|
+
return [];
|
|
53
|
+
}
|
|
54
|
+
return raw
|
|
55
|
+
.split(',')
|
|
56
|
+
.map(m => m.trim())
|
|
57
|
+
.filter(m => m.length > 0);
|
|
58
|
+
}
|
|
27
59
|
/**
|
|
28
60
|
* Request OpenRouter's usage accounting so the response includes `usage.cost` and
|
|
29
61
|
* `usage.cost_details`. The OpenAI SDK forwards this extra body param unchanged.
|
|
30
62
|
* @see https://openrouter.ai/docs/use-cases/usage-accounting
|
|
63
|
+
*
|
|
64
|
+
* ORDERED MODEL PREFERENCE. When {@link OPENROUTER_MODEL_PREFERENCE_ENV} is set,
|
|
65
|
+
* the request also carries OpenRouter's `models` array: an ordered list it walks
|
|
66
|
+
* on ANY error from the one before — downtime, rate limiting, moderation, and
|
|
67
|
+
* context-length validation. Billing follows the model that actually answered,
|
|
68
|
+
* not the ones that failed.
|
|
69
|
+
*
|
|
70
|
+
* The caller's own model is always appended last, so a preference can never make
|
|
71
|
+
* a deliberately chosen model unreachable — it only gets tried after the
|
|
72
|
+
* preferred ones decline. That also makes the context-length case degrade
|
|
73
|
+
* correctly: a prompt sized for a large-context model overflows the preferred
|
|
74
|
+
* small one, OpenRouter treats that as an error, and the fall-through lands on
|
|
75
|
+
* the model the caller asked for in the first place.
|
|
76
|
+
*
|
|
77
|
+
* `model` IS SET AS WELL, to the head of the same list, and that is deliberate
|
|
78
|
+
* rather than redundant. OpenRouter documents both fields but never states which
|
|
79
|
+
* wins when both are present. Setting them consistently means the same model
|
|
80
|
+
* leads under either reading, instead of the behaviour depending on an ambiguity
|
|
81
|
+
* in someone else's documentation.
|
|
31
82
|
*/
|
|
32
83
|
getProviderRequestExtras(params) {
|
|
33
|
-
|
|
84
|
+
const extras = { usage: { include: true } };
|
|
85
|
+
const preference = this.getModelPreference();
|
|
86
|
+
if (preference.length === 0) {
|
|
87
|
+
// Byte-identical to the request this class has always sent.
|
|
88
|
+
return extras;
|
|
89
|
+
}
|
|
90
|
+
// Preferred models first, the caller's model last. De-duplicated, because a
|
|
91
|
+
// caller whose model is already in the preference list must not make the
|
|
92
|
+
// list walk the same model twice — a repeat costs a whole failed attempt.
|
|
93
|
+
const callerModel = typeof params?.model === 'string' ? params.model.trim() : '';
|
|
94
|
+
const ordered = [];
|
|
95
|
+
for (const m of [...preference, callerModel]) {
|
|
96
|
+
if (m && !ordered.includes(m)) {
|
|
97
|
+
ordered.push(m);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
extras.models = ordered;
|
|
101
|
+
extras.model = ordered[0];
|
|
102
|
+
return extras;
|
|
34
103
|
}
|
|
35
104
|
};
|
|
36
105
|
OpenRouterLLM = __decorate([
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openRouter.js","sourceRoot":"","sources":["../../src/models/openRouter.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAc,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,wB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AAwB,CAAC,MAAkB;QAC1D,MAAM,MAAM,GAA4B,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC;QAErE,MAAM,UAAU,GAAG,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAC7C,IAAI,UAAU,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAC1B,4DAA4D;YAC5D,OAAO,MAAM,CAAC;QAClB,CAAC;QAED,4EAA4E;QAC5E,yEAAyE;QACzE,0EAA0E;QAC1E,MAAM,WAAW,GAAG,OAAO,MAAM,EAAE,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QACjF,MAAM,OAAO,GAAa,EAAE,CAAC;QAC7B,KAAK,MAAM,CAAC,IAAI,CAAC,GAAG,UAAU,EAAE,WAAW,CAAC,EAAE,CAAC;YAC3C,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC;gBAC5B,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;YACpB,CAAC;QACL,CAAC;QAED,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC;QACxB,MAAM,CAAC,KAAK,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;QAC1B,OAAO,MAAM,CAAC;IAClB,CAAC;CACJ,CAAA;AAvEY,aAAa;IADzB,aAAa,CAAC,OAAO,EAAE,eAAe,CAAC;;GAC3B,aAAa,CAuEzB"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/ai-openrouter",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.2.0-edge.
|
|
4
|
+
"version": "6.2.0-edge.6",
|
|
5
5
|
"description": "MemberJunction Wrapper for OpenRouter AI inference services",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -11,14 +11,14 @@
|
|
|
11
11
|
"author": "MemberJunction.com",
|
|
12
12
|
"license": "BUSL-1.1",
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@memberjunction/unit-testing": "6.2.0-edge.
|
|
14
|
+
"@memberjunction/unit-testing": "6.2.0-edge.6",
|
|
15
15
|
"ts-node-dev": "^2.0.0",
|
|
16
16
|
"typescript": "^5.9.3"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@memberjunction/ai": "6.2.0-edge.
|
|
20
|
-
"@memberjunction/ai-openai": "6.2.0-edge.
|
|
21
|
-
"@memberjunction/global": "6.2.0-edge.
|
|
19
|
+
"@memberjunction/ai": "6.2.0-edge.6",
|
|
20
|
+
"@memberjunction/ai-openai": "6.2.0-edge.6",
|
|
21
|
+
"@memberjunction/global": "6.2.0-edge.6"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|
|
24
24
|
"type": "git",
|