@x12i/ai-providers-router 4.8.9 → 4.9.0

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/dist/factory.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { applyPackageLogLevelsFromEnv } from '@x12i/logxer';
2
2
  import { LLMProviderRouter } from './router/RouterWrapper.js';
3
3
  import dns from 'node:dns';
4
- import { resolveOpenRouterApiKey, OPENROUTER_API_KEY_ERC, resolveUseOpenRouter } from './utils/openrouterEnv.js';
4
+ import { resolveOpenRouterApiKey, OPENROUTER_API_KEY_ERC, resolvePreferOpenRouterPolicy } from './utils/openrouterEnv.js';
5
5
  import dotenv from 'dotenv';
6
6
  // Fix IPv6-first DNS resolution issue on Windows (forces IPv4-first to avoid connect timeouts)
7
7
  // This prevents undici from trying IPv6 first on networks that silently drop IPv6 traffic
@@ -121,7 +121,7 @@ export async function createRouter(config) {
121
121
  apiKey: OPENROUTER_API_KEY_ERC,
122
122
  httpReferer: 'ENV.OPENROUTER_HTTP_REFERER||ENV.OPEN_ROUTER_HTTP_REFERER',
123
123
  xTitle: 'ENV.OPENROUTER_X_TITLE||ENV.OPEN_ROUTER_X_TITLE',
124
- useOpenRouter: 'ENV.USE_OPENROUTER',
124
+ preferOpenRouter: 'ENV.PREFER_OPENROUTER||ENV.USE_OPENROUTER',
125
125
  },
126
126
  },
127
127
  };
@@ -134,19 +134,19 @@ export async function createRouter(config) {
134
134
  ercResult.config.providers.openrouter.apiKey = resolvedOpenRouterKey;
135
135
  }
136
136
  // Use explicit config if provided (Advanced Mode), otherwise use ERC auto-discovered config (Zero-Config Mode)
137
- const useOpenRouter = resolveUseOpenRouter({
138
- userValue: config?.useOpenRouter,
137
+ const preferOpenRouter = resolvePreferOpenRouterPolicy({
138
+ userValue: config?.preferOpenRouter ?? config?.useOpenRouter,
139
139
  });
140
140
  const finalConfig = Object.keys(config || {}).length > 0 ? {
141
141
  ...config,
142
- useOpenRouter: config?.useOpenRouter ?? useOpenRouter,
142
+ preferOpenRouter: config?.preferOpenRouter ?? config?.useOpenRouter ?? preferOpenRouter,
143
143
  } : {
144
144
  logLevel: (ercResult.config.router.logsLevel ||
145
145
  ercResult.config.router.logLevel),
146
146
  verbose: ercResult.config.router.verbose === 'true' || ercResult.config.router.verbose === true,
147
147
  timeoutMs: ercResult.config.router.timeoutMs ? parseInt(String(ercResult.config.router.timeoutMs), 10) : undefined,
148
148
  defaultTimeoutMs: ercResult.config.router.timeoutMs ? parseInt(String(ercResult.config.router.timeoutMs), 10) : undefined,
149
- useOpenRouter,
149
+ preferOpenRouter,
150
150
  };
151
151
  const router = new LLMProviderRouter(finalConfig);
152
152
  // Configure providers from ERC auto-detected config OR explicit config
@@ -16,7 +16,7 @@ export declare class AIRouter {
16
16
  private resolveProviderNameForCandidate;
17
17
  /**
18
18
  * Resolve provider name from request.
19
- * Prefers OpenRouter when USE_OPENROUTER is true (default); falls back to OpenRouter when
19
+ * Prefers OpenRouter when PREFER_OPENROUTER is true (default) and a key is present; falls back to OpenRouter when
20
20
  * the requested vendor has no direct provider registered but an OpenRouter key exists.
21
21
  */
22
22
  private resolveProviderName;
@@ -33,7 +33,7 @@ export class AIRouter {
33
33
  }
34
34
  /**
35
35
  * Resolve provider name from request.
36
- * Prefers OpenRouter when USE_OPENROUTER is true (default); falls back to OpenRouter when
36
+ * Prefers OpenRouter when PREFER_OPENROUTER is true (default) and a key is present; falls back to OpenRouter when
37
37
  * the requested vendor has no direct provider registered but an OpenRouter key exists.
38
38
  */
39
39
  resolveProviderName(input) {
@@ -104,9 +104,11 @@ export interface RouterConfig {
104
104
  defaultTimeoutMs?: number;
105
105
  /**
106
106
  * Prefer OpenRouter for vendor calls when OPENROUTER_API_KEY is set (default: true).
107
- * Maps to USE_OPENROUTER env. When false, direct providers are used when keys exist;
107
+ * Maps to PREFER_OPENROUTER env. When false, direct providers are used when keys exist;
108
108
  * OpenRouter is still used as fallback for providers without a direct key.
109
109
  */
110
+ preferOpenRouter?: boolean;
111
+ /** @deprecated Use preferOpenRouter (maps to USE_OPENROUTER env as legacy fallback). */
110
112
  useOpenRouter?: boolean;
111
113
  /** OpenRouter provider config (e.g. from gateway); used when env OPEN_ROUTER_KEY is not visible */
112
114
  openrouter?: {
@@ -6,21 +6,32 @@ export declare function resolveOpenRouterApiKey(): string | undefined;
6
6
  export declare function hasOpenRouterApiKey(): boolean;
7
7
  /** ERC placeholder resolution: prefer canonical name, fall back to legacy. */
8
8
  export declare const OPENROUTER_API_KEY_ERC = "ENV.OPENROUTER_API_KEY||ENV.OPEN_ROUTER_KEY";
9
- export interface ResolveUseOpenRouterOptions {
10
- /** Programmatic override from RouterConfig.useOpenRouter — wins over environment. */
9
+ export interface ResolvePreferOpenRouterOptions {
10
+ /** Programmatic override from RouterConfig.preferOpenRouter — wins over environment. */
11
11
  userValue?: boolean;
12
12
  /** For tests; defaults to `process.env`. */
13
13
  env?: NodeJS.ProcessEnv;
14
14
  }
15
+ /** @deprecated Use ResolvePreferOpenRouterOptions */
16
+ export type ResolveUseOpenRouterOptions = ResolvePreferOpenRouterOptions;
17
+ export interface PreferOpenRouterConfig {
18
+ preferOpenRouter?: boolean;
19
+ /** @deprecated Use preferOpenRouter */
20
+ useOpenRouter?: boolean;
21
+ }
15
22
  /**
16
- * Whether to prefer OpenRouter for vendor calls when an OpenRouter key is available.
23
+ * Read operator preference from env. Prefers PREFER_OPENROUTER; USE_OPENROUTER is legacy fallback.
24
+ * Default when both are unset: `true`.
25
+ */
26
+ export declare function readPreferOpenRouterFromEnv(env?: NodeJS.ProcessEnv): boolean;
27
+ /**
28
+ * Whether the operator prefers OpenRouter for vendor calls (before key availability is applied).
17
29
  *
18
- * Default: `true` (route through OpenRouter even when direct provider keys exist).
19
- * Set `USE_OPENROUTER=false` to use direct providers when keys exist; OpenRouter remains
20
- * available as fallback for providers without a direct key.
30
+ * Default: `true`. Set `PREFER_OPENROUTER=false` to force vendor-direct when keys exist;
31
+ * OpenRouter remains available as fallback for providers without a direct key.
21
32
  */
22
- export declare function resolveUseOpenRouter(options?: ResolveUseOpenRouterOptions): boolean;
33
+ export declare function resolvePreferOpenRouterPolicy(options?: ResolvePreferOpenRouterOptions): boolean;
34
+ /** @deprecated Use resolvePreferOpenRouterPolicy */
35
+ export declare function resolveUseOpenRouter(options?: ResolvePreferOpenRouterOptions): boolean;
23
36
  /** True when OpenRouter should be the primary transport (not merely fallback). */
24
- export declare function shouldPreferOpenRouter(config: {
25
- useOpenRouter?: boolean;
26
- } | undefined, env?: NodeJS.ProcessEnv): boolean;
37
+ export declare function shouldPreferOpenRouter(config: PreferOpenRouterConfig | undefined, env?: NodeJS.ProcessEnv): boolean;
@@ -16,31 +16,56 @@ export function hasOpenRouterApiKey() {
16
16
  }
17
17
  /** ERC placeholder resolution: prefer canonical name, fall back to legacy. */
18
18
  export const OPENROUTER_API_KEY_ERC = 'ENV.OPENROUTER_API_KEY||ENV.OPEN_ROUTER_KEY';
19
+ function parsePreferOpenRouterEnvValue(raw) {
20
+ if (raw === undefined || raw === '')
21
+ return undefined;
22
+ if (raw === 'false')
23
+ return false;
24
+ if (raw === 'true')
25
+ return true;
26
+ return true;
27
+ }
19
28
  /**
20
- * Whether to prefer OpenRouter for vendor calls when an OpenRouter key is available.
29
+ * Read operator preference from env. Prefers PREFER_OPENROUTER; USE_OPENROUTER is legacy fallback.
30
+ * Default when both are unset: `true`.
31
+ */
32
+ export function readPreferOpenRouterFromEnv(env = process.env) {
33
+ const prefer = parsePreferOpenRouterEnvValue(env.PREFER_OPENROUTER);
34
+ if (prefer !== undefined)
35
+ return prefer;
36
+ const legacy = parsePreferOpenRouterEnvValue(env.USE_OPENROUTER);
37
+ if (legacy !== undefined)
38
+ return legacy;
39
+ return true;
40
+ }
41
+ function resolveConfigPreferOpenRouter(config) {
42
+ if (config?.preferOpenRouter !== undefined)
43
+ return config.preferOpenRouter;
44
+ if (config?.useOpenRouter !== undefined)
45
+ return config.useOpenRouter;
46
+ return undefined;
47
+ }
48
+ /**
49
+ * Whether the operator prefers OpenRouter for vendor calls (before key availability is applied).
21
50
  *
22
- * Default: `true` (route through OpenRouter even when direct provider keys exist).
23
- * Set `USE_OPENROUTER=false` to use direct providers when keys exist; OpenRouter remains
24
- * available as fallback for providers without a direct key.
51
+ * Default: `true`. Set `PREFER_OPENROUTER=false` to force vendor-direct when keys exist;
52
+ * OpenRouter remains available as fallback for providers without a direct key.
25
53
  */
26
- export function resolveUseOpenRouter(options = {}) {
54
+ export function resolvePreferOpenRouterPolicy(options = {}) {
27
55
  const { userValue, env = process.env } = options;
28
56
  if (userValue === false)
29
57
  return false;
30
58
  if (userValue === true)
31
59
  return true;
32
- const raw = env.USE_OPENROUTER;
33
- if (raw === undefined || raw === '')
34
- return true;
35
- if (raw === 'false')
36
- return false;
37
- if (raw === 'true')
38
- return true;
39
- return true;
60
+ return readPreferOpenRouterFromEnv(env);
61
+ }
62
+ /** @deprecated Use resolvePreferOpenRouterPolicy */
63
+ export function resolveUseOpenRouter(options = {}) {
64
+ return resolvePreferOpenRouterPolicy(options);
40
65
  }
41
66
  /** True when OpenRouter should be the primary transport (not merely fallback). */
42
67
  export function shouldPreferOpenRouter(config, env) {
43
68
  if (!hasOpenRouterApiKey())
44
69
  return false;
45
- return resolveUseOpenRouter({ userValue: config?.useOpenRouter, env });
70
+ return resolvePreferOpenRouterPolicy({ userValue: resolveConfigPreferOpenRouter(config), env });
46
71
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x12i/ai-providers-router",
3
- "version": "4.8.9",
3
+ "version": "4.9.0",
4
4
  "description": "Unified router for all LLM provider implementations",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -46,7 +46,7 @@
46
46
  "@x12i/ai-provider-grok": "^3.2.0",
47
47
  "@x12i/ai-provider-interface": "^3.2.1",
48
48
  "@x12i/ai-provider-openai": "^3.2.1",
49
- "@x12i/logxer": "^4.5.1",
49
+ "@x12i/logxer": "^4.6.0",
50
50
  "ai-io-normalizer": "^6.0.3"
51
51
  },
52
52
  "devDependencies": {