@memberjunction/ai-openrouter 6.2.0-edge.4 → 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.
package/README.md CHANGED
@@ -102,6 +102,16 @@ SiliconFlow) through the same Decisions API, so the `Kev-4B` model's OpenRouter
102
102
  `Default Decision` does not bind Kev-4B, but as an active Decision model it is one of the
103
103
  power-matched fallbacks, after Jev and `LLM Decision`, for any host that has this key.
104
104
 
105
+ **Mercury Decide.** OpenRouter also serves it through the same Decisions API, in the System One format,
106
+ so the model's OpenRouter row uses `OpenRouterDecision` and the same key, with a pinned slug:
107
+ `inception/mercury-decide-20260930`, served by Inception, 65,536-token context, text only. Its own
108
+ vendor route, `InceptionDecision` in [`@memberjunction/ai-inception`](../Inception/README.md), has the
109
+ higher priority. The free variant (`inception/mercury-decide:free`) is not in the catalog: it has its
110
+ own rate limits and data policy.
111
+
112
+ It is a `Default Decision` power-matched fallback for a host with this key, among the last, because the
113
+ fallbacks follow PowerRank distance from `Default Decision`'s target rank.
114
+
105
115
  **The wire format.** Jev and Kev speak the System One decisions format. `OpenRouterDecision` extends
106
116
  `BaseSystemOneDecision` (`@memberjunction/ai`), which owns the request and answer mapping; this class
107
117
  supplies only the endpoint and the default model. Cloudflare's Clef models speak the same format on
@@ -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;;;;OAIG;cACgB,wBAAwB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;CAG3F"}
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
- return { usage: { include: true } };
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,wBAAwB,CAAC;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAC;AAEtD,MAAM,eAAe,GAAW,8BAA8B,CAAA;AAE9D;;;;;;;;GAQG;AAEI,IAAM,aAAa,GAAnB,MAAM,aAAc,SAAQ,SAAS;IACxC,YAAY,MAAc;QACtB,KAAK,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACgB,wBAAwB,CAAC,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AAI,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",
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.4",
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.4",
20
- "@memberjunction/ai-openai": "6.2.0-edge.4",
21
- "@memberjunction/global": "6.2.0-edge.4"
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",