@x12i/ai-gateway 10.0.6 → 10.1.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/README.md +17 -17
- package/dist/activity-manager.d.ts +2 -2
- package/dist/activity-manager.js +2 -2
- package/dist/ai-tools-client.d.ts +4 -2
- package/dist/ai-tools-client.js +8 -5
- package/dist/gateway-config.d.ts +1 -1
- package/dist/gateway-config.js +5 -5
- package/dist/gateway-utils.d.ts +1 -1
- package/dist/gateway-utils.js +10 -3
- package/dist/gateway.d.ts +1 -1
- package/dist/gateway.js +4 -4
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/openrouter-routing.d.ts +11 -3
- package/dist/openrouter-routing.js +18 -5
- package/dist-cjs/activity-manager.cjs +2 -2
- package/dist-cjs/activity-manager.d.ts +2 -2
- package/dist-cjs/ai-tools-client.cjs +8 -5
- package/dist-cjs/ai-tools-client.d.ts +4 -2
- package/dist-cjs/gateway-config.cjs +5 -5
- package/dist-cjs/gateway-config.d.ts +1 -1
- package/dist-cjs/gateway-utils.cjs +10 -3
- package/dist-cjs/gateway-utils.d.ts +1 -1
- package/dist-cjs/gateway.cjs +4 -4
- package/dist-cjs/gateway.d.ts +1 -1
- package/dist-cjs/index.cjs +2 -2
- package/dist-cjs/index.d.ts +2 -2
- package/dist-cjs/openrouter-routing.cjs +18 -5
- package/dist-cjs/openrouter-routing.d.ts +11 -3
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @x12i/ai-gateway
|
|
2
2
|
|
|
3
|
-
Unified gateway for LLM provider routing, structured logging, optional Activix activity persistence, and cost/model resolution via **@x12i/ai-tools**
|
|
3
|
+
Unified gateway for LLM provider routing, structured logging, optional Activix activity persistence, and cost/model resolution via **@x12i/ai-tools** v3. Built on **@x12i/ai-providers-router**, **@x12i/logxer**, **@x12i/rendrix** (templates), and **@x12i/flex-md** (output-format hints and max-token lookup).
|
|
4
4
|
|
|
5
5
|
## What this package does
|
|
6
6
|
|
|
@@ -13,7 +13,7 @@ Unified gateway for LLM provider routing, structured logging, optional Activix a
|
|
|
13
13
|
| **Activix** | Optional Mongo-backed activity rows; billing written from gateway-computed slice on **`completeRecord`** (`outer.cost` + root fields). No Activix **`autoCost`** re-pricing. |
|
|
14
14
|
| **Trace mode** | `diagnostics.mode === 'trace'` adds `metadata.attempts[]`, `metadata.usage`, and per-attempt **`costUsd`** / **`costStatus`**. |
|
|
15
15
|
|
|
16
|
-
Pinned dependency versions are in `package.json` (currently **Activix ^8.
|
|
16
|
+
Pinned dependency versions are in `package.json` (currently **Activix ^8.6**, **ai-tools ^3.0**, **ai-providers-router ^4.9**).
|
|
17
17
|
|
|
18
18
|
---
|
|
19
19
|
|
|
@@ -148,7 +148,7 @@ import {
|
|
|
148
148
|
|
|
149
149
|
**Required on every invoke:** `config.model` (or `modelConfig.model`) and `maxTokens` (`request.config`, `modelConfig`, `GatewayConfig`, or `internalSystemActions`). Missing model → `ModelRequiredError` (`code: 'MODEL_REQUIRED'`). Missing `maxTokens` → `MaxTokensRequiredError` (`code: 'MAX_TOKENS_REQUIRED'`). There is **no** packaged default model, **no** flex-md / Optimixer auto-fill, and **no** `GATEWAY_DEFAULT_MAX_TOKENS`. Use [@x12i/optimixer](https://www.npmjs.com/package/@x12i/optimixer) in the **client** that wraps this gateway if you want adaptive completion budgets.
|
|
150
150
|
|
|
151
|
-
**Rate limiting:** removed from the gateway. See [
|
|
151
|
+
**Rate limiting:** removed from the gateway. See [upstream rate-limit spec](./docs/upstream-reports/AI_PROVIDER_ROUTER_RATE_LIMITING.md) — implement in `@x12i/ai-providers-router`.
|
|
152
152
|
|
|
153
153
|
### Template rendering (`defaults/template-rendering.json`)
|
|
154
154
|
|
|
@@ -184,7 +184,8 @@ Instructions must be **complete caller text** — the gateway no longer injects
|
|
|
184
184
|
|
|
185
185
|
| Variable | Role |
|
|
186
186
|
|----------|------|
|
|
187
|
-
| `MONGO_URI`, `MONGO_LOGS_DB` / `MONGO_DB` | Activix when no custom tracker |
|
|
187
|
+
| `MONGO_URI`, `MONGO_LOGS_DB` / `MONGO_DB` / `MONGO_AI_LOGS_DB` | Activix when no custom tracker |
|
|
188
|
+
| `ACTIVIX_DB_NAME` | Activix database override (falls back to `MONGO_AI_LOGS_DB` → `MONGO_LOGS_DB` → `MONGO_DB`) |
|
|
188
189
|
| `mode` / `MODE` | Operational mode (`dev`, `debug`, `prod`) — expose to downstream clients |
|
|
189
190
|
| `AI_GATEWAY_LOGS_LEVEL` | Log threshold for gateway diagnostics (`AI_GATEWAY` prefix): `error` … `verbose` |
|
|
190
191
|
| `AI_GATEWAY_VERBOSE` | Full payload lines (still requires `AI_GATEWAY_LOGS_LEVEL=verbose`) |
|
|
@@ -241,13 +242,13 @@ Implemented in **`resolveCostCompletionWithAiTools`** only ( **`CostCalculator.c
|
|
|
241
242
|
|
|
242
243
|
- **No Catalox / Firestore** — catalogs come from ai-tools open-assets JSON (optional **`bundledOnly`**).
|
|
243
244
|
|
|
244
|
-
Gateway exports the model orchestrator from `@x12i/ai-tools` ≥ **
|
|
245
|
+
Gateway exports the model orchestrator from `@x12i/ai-tools` ≥ **3.0.0** (`resolveInvokeModel`, `useOpenRouter`, …). v3 requires **`profile/choice`** keys (e.g. `cheap/default`) or concrete catalog ids — shortcuts like `cheapest` / bare `cheap` are rejected.
|
|
245
246
|
|
|
246
247
|
Gateway billing helpers (exported for tests/integrators): `resolveCostCompletionWithAiTools`, `buildGatewayPricingRecord`, `catalogPricingSucceeded`, `buildTraceUsageSummary`, `enrichTraceAttemptsWithBilling`.
|
|
247
248
|
|
|
248
249
|
---
|
|
249
250
|
|
|
250
|
-
## Activity tracking (@x12i/activix 8.
|
|
251
|
+
## Activity tracking (@x12i/activix 8.6)
|
|
251
252
|
|
|
252
253
|
When tracking is enabled and no custom tracker is supplied, the gateway constructs Activix with fixed collection names (see `src/config/activity-tracking-config.ts`):
|
|
253
254
|
|
|
@@ -261,12 +262,14 @@ When tracking is enabled and no custom tracker is supplied, the gateway construc
|
|
|
261
262
|
|
|
262
263
|
**Successful completion** (no duplicate billing on `outer.metadata`):
|
|
263
264
|
|
|
264
|
-
- Root: `cost`, `costUsd`, `costStatus`, **`metadata`** (routing + billing mirror for
|
|
265
|
+
- Root: `cost`, `costUsd`, `costStatus`, **`metadata`** (routing + billing mirror; **`metadata.modelUsed`** for studio)
|
|
265
266
|
- `outer.metadata`: routing only (`modelUsed`, `provider`, …)
|
|
266
267
|
- `outer.cost`: Activix cost shape (`usd`, `tokens`, `provider`, `model`, `details`)
|
|
267
268
|
- `response.metadata`: same billing slice as returned to callers
|
|
268
269
|
|
|
269
|
-
Gateway resolves billing **before** `completeRecord` and sets **`outer.cost`** from that slice.
|
|
270
|
+
Activix **8.6** runs **`materializeRecordRoutingAndBilling`** on persist (root **`config`** mirror from routing metadata). Gateway resolves billing **before** `completeRecord` and sets **`outer.cost`** from that slice.
|
|
271
|
+
|
|
272
|
+
**`autoCost`:** Activix 8.6 default is **`false`**. The gateway keeps **`autoCost: false`** on the default activity manager so billing is not recomputed via ai-tools at persist time (no second pricing path). Custom **`activityTracker`** instances may opt in to Activix **`autoCost`** (uses **@x12i/ai-tools** v3 **`calculateFromRecord`** when enabled).
|
|
270
273
|
|
|
271
274
|
Mongo env: `MONGO_URI` + `MONGO_LOGS_DB` or `MONGO_DB`.
|
|
272
275
|
|
|
@@ -335,20 +338,17 @@ Live tests use `LIVE_TEST_PROVIDER` / `LIVE_TEST_MODEL` (default `openrouter` +
|
|
|
335
338
|
|
|
336
339
|
## Documentation index
|
|
337
340
|
|
|
341
|
+
Full index: **[docs/README.md](./docs/README.md)**. Upstream gaps: **[docs/upstream-reports/](./docs/upstream-reports/README.md)**.
|
|
342
|
+
|
|
338
343
|
| Document | Topic |
|
|
339
344
|
|----------|--------|
|
|
340
|
-
| [IDENTITY_OBJECT_CONTRACT.md](./docs/IDENTITY_OBJECT_CONTRACT.md) | Identity / `runContext` |
|
|
341
345
|
| [AI_GATEWAY_INVOKE_EXECUTION_METADATA.md](./docs/AI_GATEWAY_INVOKE_EXECUTION_METADATA.md) | Metadata, cost, trace, Activix completion |
|
|
342
|
-
| [
|
|
343
|
-
| [
|
|
344
|
-
| [
|
|
345
|
-
| [OPENROUTER_ENV.md](./docs/OPENROUTER_ENV.md) | `OPENROUTER_API_KEY` and `USE_OPENROUTER` semantics |
|
|
346
|
-
| [UPSTREAM_PROFILE_RESOLUTION_AND_OPENROUTER_FALLBACK.md](./docs/UPSTREAM_PROFILE_RESOLUTION_AND_OPENROUTER_FALLBACK.md) | Profile routing and OpenRouter fallback checklist |
|
|
347
|
-
| [upstream-reports/README.md](./docs/upstream-reports/README.md) | Upstream issues (one file per package/gap) |
|
|
348
|
-
| [AI_PROVIDER_ROUTER_RATE_LIMITING_FEATURE_REQUEST.md](./docs/AI_PROVIDER_ROUTER_RATE_LIMITING_FEATURE_REQUEST.md) | Router rate-limit FR (gateway no longer sleeps between calls) |
|
|
349
|
-
| [RUNTIME_OBJECTS_OBSERVABILITY.md](./docs/RUNTIME_OBJECTS_OBSERVABILITY.md) | Runtime object keys |
|
|
346
|
+
| [IDENTITY_OBJECT_CONTRACT.md](./docs/IDENTITY_OBJECT_CONTRACT.md) | Identity / `runContext` |
|
|
347
|
+
| [OPENROUTER_ENV.md](./docs/OPENROUTER_ENV.md) | `OPENROUTER_API_KEY` and `USE_OPENROUTER` |
|
|
348
|
+
| [UPSTREAM_PROFILE_RESOLUTION_AND_OPENROUTER_FALLBACK.md](./docs/UPSTREAM_PROFILE_RESOLUTION_AND_OPENROUTER_FALLBACK.md) | Profile routing + OpenRouter fallback |
|
|
350
349
|
| [UPSTREAM_TEMPLATE_RENDERING_AND_PARSER_V4.md](./docs/UPSTREAM_TEMPLATE_RENDERING_AND_PARSER_V4.md) | Parser v4 + `template-rendering.json` |
|
|
351
350
|
| [GRAPH_EXECUTION_SUPPORT.md](./docs/GRAPH_EXECUTION_SUPPORT.md) | Graph / node identity |
|
|
351
|
+
| [LOGGER_INITIALIZATION.md](./docs/LOGGER_INITIALIZATION.md) | Logxer setup |
|
|
352
352
|
| [DUAL_PACKAGE_SETUP_GUIDE.md](./docs/DUAL_PACKAGE_SETUP_GUIDE.md) | ESM + CJS publish layout |
|
|
353
353
|
|
|
354
354
|
---
|
|
@@ -32,8 +32,8 @@ export interface ActivityManagerConfig {
|
|
|
32
32
|
customTracker?: Activix;
|
|
33
33
|
logger: Logxer;
|
|
34
34
|
/**
|
|
35
|
-
* Activix
|
|
36
|
-
*
|
|
35
|
+
* Activix 8.6+ {@link ActivixAutoCostOptions}: optional persist-time cost via @x12i/ai-tools v3.
|
|
36
|
+
* Default gateway manager leaves this off — billing is set before completeRecord.
|
|
37
37
|
*/
|
|
38
38
|
autoCost?: boolean | ActivixAutoCostOptions;
|
|
39
39
|
}
|
package/dist/activity-manager.js
CHANGED
|
@@ -165,7 +165,7 @@ function pickActivixUsageTokens(response) {
|
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
167
|
/**
|
|
168
|
-
* Activix
|
|
168
|
+
* Activix 8.x `outer.cost` from gateway billing + routing (Run Analysis G8).
|
|
169
169
|
* Uses Activix {@link normalizeToActivixCostShape} so the shape matches package validators.
|
|
170
170
|
*/
|
|
171
171
|
function buildActivixOuterCost(routingMeta, billing, response) {
|
|
@@ -193,7 +193,7 @@ function buildActivixOuterCost(routingMeta, billing, response) {
|
|
|
193
193
|
};
|
|
194
194
|
return normalizeToActivixCostShape(candidate, 'gateway.outer.cost') ?? undefined;
|
|
195
195
|
}
|
|
196
|
-
/** Run-level record metadata (Activix
|
|
196
|
+
/** Run-level record metadata (Activix 8.x top-level `metadata`, sibling to `outer`). */
|
|
197
197
|
function buildActivixRecordMetadata(response, billing) {
|
|
198
198
|
const out = {
|
|
199
199
|
...pickActivixCompletionRoutingMetadata(response)
|
|
@@ -6,8 +6,10 @@ import { type AiToolsInvokeClient, type ModelResolutionSuccess, type OpenRouterR
|
|
|
6
6
|
import type { Logxer } from '@x12i/logxer';
|
|
7
7
|
import type { GatewayConfig } from './types.js';
|
|
8
8
|
export type AiToolsClientBundle = AiToolsInvokeClient;
|
|
9
|
-
export { resolveInvokeModel,
|
|
10
|
-
|
|
9
|
+
export { resolveInvokeModel, applyOpenRouterInvokeRouting, buildInvokeModelResolverOptions, enrichModelResolutionError, mapResolutionToRouterConfig, ModelProfileUnroutableError, ModelProfileInputRejectedError, MODEL_PROFILE_UNROUTABLE, getAiToolsInvokeClient, resetAiToolsInvokeClientForTests as resetAiToolsInvokeClientForTestsUpstream, createAiToolsInvokeClient, } from '@x12i/ai-tools';
|
|
10
|
+
/** @deprecated Use `applyOpenRouterInvokeRouting` (ai-tools v3). */
|
|
11
|
+
export { applyOpenRouterInvokeRouting as applyOpenRouterInvokePolicy } from '@x12i/ai-tools';
|
|
12
|
+
export { resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, } from './openrouter-routing.js';
|
|
11
13
|
export type { InvokeModelResolutionDiagnostics, InvokeModelResolutionInput, InvokeModelResolutionOptions, InvokeModelResolutionResult, InvokeRouterConfigSlice, AiToolsInvokeClient, } from '@x12i/ai-tools';
|
|
12
14
|
/** @deprecated Use buildInvokeModelResolverOptions */
|
|
13
15
|
export declare function buildModelResolverOptions(config: GatewayConfig, routingEnv?: OpenRouterRoutingConfig): import("@x12i/ai-tools").ModelResolverOptions;
|
package/dist/ai-tools-client.js
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { getAiToolsInvokeClient, resetAiToolsInvokeClientForTests as resetAiToolsInvokeClientForTestsUpstream, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, CostCalculator, } from '@x12i/ai-tools';
|
|
6
6
|
import { gatewayLogDebug, withActivityIdentity } from './gateway-log-meta.js';
|
|
7
|
-
import {
|
|
8
|
-
export { resolveInvokeModel,
|
|
9
|
-
|
|
7
|
+
import { resolveUseOpenRouter } from './openrouter-routing.js';
|
|
8
|
+
export { resolveInvokeModel, applyOpenRouterInvokeRouting, buildInvokeModelResolverOptions, enrichModelResolutionError, mapResolutionToRouterConfig, ModelProfileUnroutableError, ModelProfileInputRejectedError, MODEL_PROFILE_UNROUTABLE, getAiToolsInvokeClient, resetAiToolsInvokeClientForTests as resetAiToolsInvokeClientForTestsUpstream, createAiToolsInvokeClient, } from '@x12i/ai-tools';
|
|
9
|
+
/** @deprecated Use `applyOpenRouterInvokeRouting` (ai-tools v3). */
|
|
10
|
+
export { applyOpenRouterInvokeRouting as applyOpenRouterInvokePolicy } from '@x12i/ai-tools';
|
|
11
|
+
export { resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, } from './openrouter-routing.js';
|
|
10
12
|
let bootstrapFailedLogged = false;
|
|
11
13
|
function invokeClientOptions(config) {
|
|
12
14
|
return {
|
|
@@ -26,7 +28,8 @@ function withCatalogLaneCalculator(client, config) {
|
|
|
26
28
|
...(config.aiTools?.costIncludeBreakdown ? { includeBreakdown: true } : {}),
|
|
27
29
|
resolverOptions: buildInvokeModelResolverOptions({
|
|
28
30
|
routingEnv: client.routingEnv,
|
|
29
|
-
catalogLane: lane
|
|
31
|
+
catalogLane: lane,
|
|
32
|
+
useOpenRouter: resolveUseOpenRouter(config),
|
|
30
33
|
})
|
|
31
34
|
})
|
|
32
35
|
};
|
|
@@ -35,7 +38,7 @@ function withCatalogLaneCalculator(client, config) {
|
|
|
35
38
|
export function buildModelResolverOptions(config, routingEnv) {
|
|
36
39
|
return buildInvokeModelResolverOptions({
|
|
37
40
|
routingEnv,
|
|
38
|
-
|
|
41
|
+
useOpenRouter: resolveUseOpenRouter(config),
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
44
|
/**
|
package/dist/gateway-config.d.ts
CHANGED
|
@@ -37,6 +37,6 @@ export declare function initializeGatewayComponents(config: GatewayConfig): {
|
|
|
37
37
|
activityManager: ActivityManager;
|
|
38
38
|
usageTracker: UsageTracker;
|
|
39
39
|
messageBuilderConfig: MessageBuilderConfig;
|
|
40
|
-
|
|
40
|
+
useOpenRouter: boolean;
|
|
41
41
|
openRouterApiKey?: string;
|
|
42
42
|
};
|
package/dist/gateway-config.js
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import * as fs from 'fs';
|
|
6
6
|
import * as path from 'path';
|
|
7
7
|
import { fileURLToPath } from 'url';
|
|
8
|
-
import { resolveOpenRouterApiKey,
|
|
8
|
+
import { resolveOpenRouterApiKey, resolveUseOpenRouter, } from './openrouter-routing.js';
|
|
9
9
|
import { LLMProviderRouter } from '@x12i/ai-providers-router';
|
|
10
10
|
import { createGatewayLogger } from './logger-factory.js';
|
|
11
11
|
import { ActivityManager } from './activity-manager.js';
|
|
@@ -154,14 +154,14 @@ export function initializeGatewayComponents(config) {
|
|
|
154
154
|
if (config.logging !== undefined)
|
|
155
155
|
routerConfig.logging = config.logging;
|
|
156
156
|
const openRouterKey = resolveOpenRouterApiKey(config);
|
|
157
|
-
const
|
|
157
|
+
const useOpenRouter = resolveUseOpenRouter(config);
|
|
158
158
|
if (openRouterKey) {
|
|
159
159
|
routerConfig.openrouter = { apiKey: openRouterKey };
|
|
160
160
|
routerConfig.openRouter = {
|
|
161
161
|
enabled: true,
|
|
162
|
-
prefer:
|
|
162
|
+
prefer: useOpenRouter,
|
|
163
163
|
};
|
|
164
|
-
if (
|
|
164
|
+
if (useOpenRouter) {
|
|
165
165
|
routerConfig.defaultMode = 'openrouter';
|
|
166
166
|
}
|
|
167
167
|
}
|
|
@@ -190,7 +190,7 @@ export function initializeGatewayComponents(config) {
|
|
|
190
190
|
activityManager,
|
|
191
191
|
usageTracker,
|
|
192
192
|
messageBuilderConfig,
|
|
193
|
-
|
|
193
|
+
useOpenRouter,
|
|
194
194
|
openRouterApiKey: openRouterKey,
|
|
195
195
|
};
|
|
196
196
|
}
|
package/dist/gateway-utils.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export type MergeConfigOptions = {
|
|
|
17
17
|
catalog?: AiModelsCatalogClient | null;
|
|
18
18
|
routingEnv?: OpenRouterRoutingConfig;
|
|
19
19
|
openRouterApiKey?: string;
|
|
20
|
-
|
|
20
|
+
useOpenRouter?: boolean;
|
|
21
21
|
};
|
|
22
22
|
export { MODEL_PROFILE_UNROUTABLE, ModelProfileUnroutableError, ModelProfileInputRejectedError, } from '@x12i/ai-tools';
|
|
23
23
|
/**
|
package/dist/gateway-utils.js
CHANGED
|
@@ -104,7 +104,7 @@ export async function mergeConfig(request, config, logger, mergeOptions) {
|
|
|
104
104
|
catalog: mergeOptions.catalog,
|
|
105
105
|
routingEnv: mergeOptions.routingEnv,
|
|
106
106
|
openRouterApiKey: mergeOptions.openRouterApiKey,
|
|
107
|
-
|
|
107
|
+
useOpenRouter: mergeOptions.useOpenRouter,
|
|
108
108
|
defaultProvider: config.defaultEngine,
|
|
109
109
|
resolveModels: true,
|
|
110
110
|
modelsOnly: config.aiTools?.modelsOnly !== false,
|
|
@@ -146,7 +146,7 @@ export async function mergeConfig(request, config, logger, mergeOptions) {
|
|
|
146
146
|
resolveModels: false,
|
|
147
147
|
routingEnv: mergeOptions.routingEnv,
|
|
148
148
|
openRouterApiKey: mergeOptions.openRouterApiKey,
|
|
149
|
-
|
|
149
|
+
useOpenRouter: mergeOptions.useOpenRouter,
|
|
150
150
|
defaultProvider: config.defaultEngine,
|
|
151
151
|
});
|
|
152
152
|
merged.provider = resolved.router.provider;
|
|
@@ -512,7 +512,14 @@ export async function resolveCostCompletionWithAiTools(routerResponse, tokens, o
|
|
|
512
512
|
options?.calculator &&
|
|
513
513
|
hasNonZeroTokenUsage(tokens)) {
|
|
514
514
|
try {
|
|
515
|
-
const record =
|
|
515
|
+
const record = routerResponse != null && typeof routerResponse === 'object'
|
|
516
|
+
? {
|
|
517
|
+
...routerResponse,
|
|
518
|
+
...(options.mergedConfig != null ? { config: options.mergedConfig } : {})
|
|
519
|
+
}
|
|
520
|
+
: options.mergedConfig != null
|
|
521
|
+
? { config: options.mergedConfig, metadata: { tokens } }
|
|
522
|
+
: { metadata: { tokens } };
|
|
516
523
|
const result = await options.calculator.calculateFromRecord(record);
|
|
517
524
|
billing = mapAiCostResultToResolvedActivityCost(billing, result);
|
|
518
525
|
}
|
package/dist/gateway.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare class AIGateway {
|
|
|
18
18
|
private messageBuilderConfig?;
|
|
19
19
|
private _autoRegisterDone;
|
|
20
20
|
private _aiToolsClient;
|
|
21
|
-
private readonly
|
|
21
|
+
private readonly useOpenRouter;
|
|
22
22
|
private readonly openRouterApiKey?;
|
|
23
23
|
constructor(config?: GatewayConfig, activityManager?: ActivityManager);
|
|
24
24
|
/**
|
package/dist/gateway.js
CHANGED
|
@@ -59,7 +59,7 @@ export class AIGateway {
|
|
|
59
59
|
messageBuilderConfig;
|
|
60
60
|
_autoRegisterDone = false;
|
|
61
61
|
_aiToolsClient = null;
|
|
62
|
-
|
|
62
|
+
useOpenRouter;
|
|
63
63
|
openRouterApiKey;
|
|
64
64
|
constructor(config = {}, activityManager) {
|
|
65
65
|
this.config = config;
|
|
@@ -69,7 +69,7 @@ export class AIGateway {
|
|
|
69
69
|
this.router = components.router;
|
|
70
70
|
this.activityManager = components.activityManager;
|
|
71
71
|
this.messageBuilderConfig = components.messageBuilderConfig;
|
|
72
|
-
this.
|
|
72
|
+
this.useOpenRouter = components.useOpenRouter;
|
|
73
73
|
this.openRouterApiKey = components.openRouterApiKey;
|
|
74
74
|
setGatewayRuntimeClients({
|
|
75
75
|
activix: this.activityManager?.getTracker(),
|
|
@@ -100,7 +100,7 @@ export class AIGateway {
|
|
|
100
100
|
catalog: aiTools?.catalog ?? null,
|
|
101
101
|
routingEnv: aiTools?.routingEnv,
|
|
102
102
|
openRouterApiKey: this.openRouterApiKey,
|
|
103
|
-
|
|
103
|
+
useOpenRouter: this.useOpenRouter,
|
|
104
104
|
});
|
|
105
105
|
// Activix start snapshot must match what the router receives (modelConfig-only callers omit request.config.model).
|
|
106
106
|
request._mergedRouterConfig = mergedConfig;
|
|
@@ -288,7 +288,7 @@ export class AIGateway {
|
|
|
288
288
|
catalog: aiTools?.catalog ?? null,
|
|
289
289
|
routingEnv: aiTools?.routingEnv,
|
|
290
290
|
openRouterApiKey: this.openRouterApiKey,
|
|
291
|
-
|
|
291
|
+
useOpenRouter: this.useOpenRouter,
|
|
292
292
|
});
|
|
293
293
|
request._mergedRouterConfig = mergedConfig;
|
|
294
294
|
logResolvedModelRouting(this.logger, request, mergedConfig);
|
package/dist/index.d.ts
CHANGED
|
@@ -40,8 +40,8 @@ export type { GatewayLoggerConfig } from './logger-factory.js';
|
|
|
40
40
|
export { GATEWAY_LOG_ENV_PREFIX, GATEWAY_LOGXER_PACKAGE, GATEWAY_STACK_LOG_PREFIXES, initializeGatewayPackageLogLevels, resetGatewayPackageLogLevelsInit } from './gateway-log-levels.js';
|
|
41
41
|
export { GatewayLogCode, gatewayErrorCode, gatewayInfoCode, gatewayWarnCode, gatewayAnomalyMeta, resolveLogDiagnosticsCatalogPath, exceptionEvidence, fieldEvidence } from './gateway-log-diagnostics.js';
|
|
42
42
|
export type { GatewayLogCode as GatewayDiagnosticCode } from './gateway-log-diagnostics.js';
|
|
43
|
-
/** Re-export @x12i/ai-tools invoke orchestrator (≥
|
|
44
|
-
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolvePreferOpenRouter, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
43
|
+
/** Re-export @x12i/ai-tools invoke orchestrator (≥ 3.0.0) for engine callers. */
|
|
44
|
+
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokeRouting, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
45
45
|
export type { AiToolsClientBundle, AiToolsInvokeClient, InvokeModelResolutionDiagnostics, InvokeModelResolutionInput, InvokeModelResolutionOptions, InvokeModelResolutionResult, InvokeRouterConfigSlice, } from './ai-tools-client.js';
|
|
46
46
|
export { createLogxer, DebugLogAbstract, runWithLogContext, getStationRuntimeIdentity, mergeRuntimeIdentity, conditionEvidence, sourceEvidence, logReferenceEvidence, readAgentLoggingInstructions, resolveAgentLoggingInstructionsPath, applyPackageLogLevelsFromEnv, configurePackageLogLevels, mergePackageLogLevelsConfig, setPackageLogLevel, resolveStackLogLevelForPrefix, resolvePackageLogsLevel, parseLogxerPackageLevelsEnv, LOGXER_PACKAGE_LEVELS_ENV, LOGXER_PACKAGE_LOGS_DEFAULT_ENV } from '@x12i/logxer';
|
|
47
47
|
export { ROUTER_LOG_ENV_PREFIX } from '@x12i/ai-providers-router';
|
package/dist/index.js
CHANGED
|
@@ -33,8 +33,8 @@ export { activityIdentityToLogContext, activityIdentityToLogMeta, withActivityId
|
|
|
33
33
|
export { createGatewayLogger, resolveGatewayVerboseEnabled } from './logger-factory.js';
|
|
34
34
|
export { GATEWAY_LOG_ENV_PREFIX, GATEWAY_LOGXER_PACKAGE, GATEWAY_STACK_LOG_PREFIXES, initializeGatewayPackageLogLevels, resetGatewayPackageLogLevelsInit } from './gateway-log-levels.js';
|
|
35
35
|
export { GatewayLogCode, gatewayErrorCode, gatewayInfoCode, gatewayWarnCode, gatewayAnomalyMeta, resolveLogDiagnosticsCatalogPath, exceptionEvidence, fieldEvidence } from './gateway-log-diagnostics.js';
|
|
36
|
-
/** Re-export @x12i/ai-tools invoke orchestrator (≥
|
|
37
|
-
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolvePreferOpenRouter, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
36
|
+
/** Re-export @x12i/ai-tools invoke orchestrator (≥ 3.0.0) for engine callers. */
|
|
37
|
+
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokeRouting, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
38
38
|
// Re-export logging (@x12i/logxer)
|
|
39
39
|
export { createLogxer, DebugLogAbstract, runWithLogContext, getStationRuntimeIdentity, mergeRuntimeIdentity, conditionEvidence, sourceEvidence, logReferenceEvidence, readAgentLoggingInstructions, resolveAgentLoggingInstructionsPath, applyPackageLogLevelsFromEnv, configurePackageLogLevels, mergePackageLogLevelsConfig, setPackageLogLevel, resolveStackLogLevelForPrefix, resolvePackageLogsLevel, parseLogxerPackageLevelsEnv, LOGXER_PACKAGE_LEVELS_ENV, LOGXER_PACKAGE_LOGS_DEFAULT_ENV } from '@x12i/logxer';
|
|
40
40
|
export { ROUTER_LOG_ENV_PREFIX } from '@x12i/ai-providers-router';
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gateway-level OpenRouter key +
|
|
2
|
+
* Gateway-level OpenRouter key + USE_OPENROUTER flags mapped to @x12i/ai-tools v3 invoke helpers.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { readUseOpenRouterFromEnv } from '@x12i/ai-profiles';
|
|
5
5
|
import type { GatewayConfig } from './types.js';
|
|
6
|
-
export {
|
|
6
|
+
export { readUseOpenRouterFromEnv };
|
|
7
7
|
export declare function resolveOpenRouterApiKey(config?: GatewayConfig): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Effective OpenRouter preference for invoke-time routing (ai-tools `useOpenRouter`).
|
|
10
|
+
* Constructor `openRouter.prefer` / `openRouter.enabled` override env `USE_OPENROUTER`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveUseOpenRouter(config?: GatewayConfig): boolean;
|
|
13
|
+
/** @deprecated Use {@link resolveUseOpenRouter} (ai-tools v3 renamed prefer → useOpenRouter). */
|
|
8
14
|
export declare function resolvePreferOpenRouter(config?: GatewayConfig): boolean;
|
|
15
|
+
/** @deprecated Use {@link readUseOpenRouterFromEnv} from `@x12i/ai-profiles`. */
|
|
16
|
+
export declare function readPreferOpenRouterFromEnv(): boolean;
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gateway-level OpenRouter key +
|
|
2
|
+
* Gateway-level OpenRouter key + USE_OPENROUTER flags mapped to @x12i/ai-tools v3 invoke helpers.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import { resolveOpenRouterApiKey as resolveOpenRouterApiKeyFromTools, resolveUseOpenRouterPreference, } from '@x12i/ai-tools';
|
|
5
|
+
import { readUseOpenRouterFromEnv } from '@x12i/ai-profiles';
|
|
6
|
+
export { readUseOpenRouterFromEnv };
|
|
6
7
|
export function resolveOpenRouterApiKey(config = {}) {
|
|
7
8
|
return resolveOpenRouterApiKeyFromTools(config.openrouter?.apiKey);
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Effective OpenRouter preference for invoke-time routing (ai-tools `useOpenRouter`).
|
|
12
|
+
* Constructor `openRouter.prefer` / `openRouter.enabled` override env `USE_OPENROUTER`.
|
|
13
|
+
*/
|
|
14
|
+
export function resolveUseOpenRouter(config = {}) {
|
|
10
15
|
if (config.openRouter?.prefer === true)
|
|
11
16
|
return true;
|
|
12
17
|
if (config.openRouter?.prefer === false)
|
|
@@ -15,5 +20,13 @@ export function resolvePreferOpenRouter(config = {}) {
|
|
|
15
20
|
return true;
|
|
16
21
|
if (config.openRouter?.enabled === false)
|
|
17
22
|
return false;
|
|
18
|
-
return
|
|
23
|
+
return resolveUseOpenRouterPreference();
|
|
24
|
+
}
|
|
25
|
+
/** @deprecated Use {@link resolveUseOpenRouter} (ai-tools v3 renamed prefer → useOpenRouter). */
|
|
26
|
+
export function resolvePreferOpenRouter(config = {}) {
|
|
27
|
+
return resolveUseOpenRouter(config);
|
|
28
|
+
}
|
|
29
|
+
/** @deprecated Use {@link readUseOpenRouterFromEnv} from `@x12i/ai-profiles`. */
|
|
30
|
+
export function readPreferOpenRouterFromEnv() {
|
|
31
|
+
return readUseOpenRouterFromEnv() ?? true;
|
|
19
32
|
}
|
|
@@ -165,7 +165,7 @@ function pickActivixUsageTokens(response) {
|
|
|
165
165
|
};
|
|
166
166
|
}
|
|
167
167
|
/**
|
|
168
|
-
* Activix
|
|
168
|
+
* Activix 8.x `outer.cost` from gateway billing + routing (Run Analysis G8).
|
|
169
169
|
* Uses Activix {@link normalizeToActivixCostShape} so the shape matches package validators.
|
|
170
170
|
*/
|
|
171
171
|
function buildActivixOuterCost(routingMeta, billing, response) {
|
|
@@ -193,7 +193,7 @@ function buildActivixOuterCost(routingMeta, billing, response) {
|
|
|
193
193
|
};
|
|
194
194
|
return normalizeToActivixCostShape(candidate, 'gateway.outer.cost') ?? undefined;
|
|
195
195
|
}
|
|
196
|
-
/** Run-level record metadata (Activix
|
|
196
|
+
/** Run-level record metadata (Activix 8.x top-level `metadata`, sibling to `outer`). */
|
|
197
197
|
function buildActivixRecordMetadata(response, billing) {
|
|
198
198
|
const out = {
|
|
199
199
|
...pickActivixCompletionRoutingMetadata(response)
|
|
@@ -32,8 +32,8 @@ export interface ActivityManagerConfig {
|
|
|
32
32
|
customTracker?: Activix;
|
|
33
33
|
logger: Logxer;
|
|
34
34
|
/**
|
|
35
|
-
* Activix
|
|
36
|
-
*
|
|
35
|
+
* Activix 8.6+ {@link ActivixAutoCostOptions}: optional persist-time cost via @x12i/ai-tools v3.
|
|
36
|
+
* Default gateway manager leaves this off — billing is set before completeRecord.
|
|
37
37
|
*/
|
|
38
38
|
autoCost?: boolean | ActivixAutoCostOptions;
|
|
39
39
|
}
|
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { getAiToolsInvokeClient, resetAiToolsInvokeClientForTests as resetAiToolsInvokeClientForTestsUpstream, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, CostCalculator, } from '@x12i/ai-tools';
|
|
6
6
|
import { gatewayLogDebug, withActivityIdentity } from './gateway-log-meta.js';
|
|
7
|
-
import {
|
|
8
|
-
export { resolveInvokeModel,
|
|
9
|
-
|
|
7
|
+
import { resolveUseOpenRouter } from './openrouter-routing.js';
|
|
8
|
+
export { resolveInvokeModel, applyOpenRouterInvokeRouting, buildInvokeModelResolverOptions, enrichModelResolutionError, mapResolutionToRouterConfig, ModelProfileUnroutableError, ModelProfileInputRejectedError, MODEL_PROFILE_UNROUTABLE, getAiToolsInvokeClient, resetAiToolsInvokeClientForTests as resetAiToolsInvokeClientForTestsUpstream, createAiToolsInvokeClient, } from '@x12i/ai-tools';
|
|
9
|
+
/** @deprecated Use `applyOpenRouterInvokeRouting` (ai-tools v3). */
|
|
10
|
+
export { applyOpenRouterInvokeRouting as applyOpenRouterInvokePolicy } from '@x12i/ai-tools';
|
|
11
|
+
export { resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, } from './openrouter-routing.js';
|
|
10
12
|
let bootstrapFailedLogged = false;
|
|
11
13
|
function invokeClientOptions(config) {
|
|
12
14
|
return {
|
|
@@ -26,7 +28,8 @@ function withCatalogLaneCalculator(client, config) {
|
|
|
26
28
|
...(config.aiTools?.costIncludeBreakdown ? { includeBreakdown: true } : {}),
|
|
27
29
|
resolverOptions: buildInvokeModelResolverOptions({
|
|
28
30
|
routingEnv: client.routingEnv,
|
|
29
|
-
catalogLane: lane
|
|
31
|
+
catalogLane: lane,
|
|
32
|
+
useOpenRouter: resolveUseOpenRouter(config),
|
|
30
33
|
})
|
|
31
34
|
})
|
|
32
35
|
};
|
|
@@ -35,7 +38,7 @@ function withCatalogLaneCalculator(client, config) {
|
|
|
35
38
|
export function buildModelResolverOptions(config, routingEnv) {
|
|
36
39
|
return buildInvokeModelResolverOptions({
|
|
37
40
|
routingEnv,
|
|
38
|
-
|
|
41
|
+
useOpenRouter: resolveUseOpenRouter(config),
|
|
39
42
|
});
|
|
40
43
|
}
|
|
41
44
|
/**
|
|
@@ -6,8 +6,10 @@ import { type AiToolsInvokeClient, type ModelResolutionSuccess, type OpenRouterR
|
|
|
6
6
|
import type { Logxer } from '@x12i/logxer';
|
|
7
7
|
import type { GatewayConfig } from './types.js';
|
|
8
8
|
export type AiToolsClientBundle = AiToolsInvokeClient;
|
|
9
|
-
export { resolveInvokeModel,
|
|
10
|
-
|
|
9
|
+
export { resolveInvokeModel, applyOpenRouterInvokeRouting, buildInvokeModelResolverOptions, enrichModelResolutionError, mapResolutionToRouterConfig, ModelProfileUnroutableError, ModelProfileInputRejectedError, MODEL_PROFILE_UNROUTABLE, getAiToolsInvokeClient, resetAiToolsInvokeClientForTests as resetAiToolsInvokeClientForTestsUpstream, createAiToolsInvokeClient, } from '@x12i/ai-tools';
|
|
10
|
+
/** @deprecated Use `applyOpenRouterInvokeRouting` (ai-tools v3). */
|
|
11
|
+
export { applyOpenRouterInvokeRouting as applyOpenRouterInvokePolicy } from '@x12i/ai-tools';
|
|
12
|
+
export { resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, } from './openrouter-routing.js';
|
|
11
13
|
export type { InvokeModelResolutionDiagnostics, InvokeModelResolutionInput, InvokeModelResolutionOptions, InvokeModelResolutionResult, InvokeRouterConfigSlice, AiToolsInvokeClient, } from '@x12i/ai-tools';
|
|
12
14
|
/** @deprecated Use buildInvokeModelResolverOptions */
|
|
13
15
|
export declare function buildModelResolverOptions(config: GatewayConfig, routingEnv?: OpenRouterRoutingConfig): import("@x12i/ai-tools").ModelResolverOptions;
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import * as fs from 'fs';
|
|
6
6
|
import * as path from 'path';
|
|
7
7
|
import { fileURLToPath } from 'url';
|
|
8
|
-
import { resolveOpenRouterApiKey,
|
|
8
|
+
import { resolveOpenRouterApiKey, resolveUseOpenRouter, } from './openrouter-routing.js';
|
|
9
9
|
import { LLMProviderRouter } from '@x12i/ai-providers-router';
|
|
10
10
|
import { createGatewayLogger } from './logger-factory.js';
|
|
11
11
|
import { ActivityManager } from './activity-manager.js';
|
|
@@ -154,14 +154,14 @@ export function initializeGatewayComponents(config) {
|
|
|
154
154
|
if (config.logging !== undefined)
|
|
155
155
|
routerConfig.logging = config.logging;
|
|
156
156
|
const openRouterKey = resolveOpenRouterApiKey(config);
|
|
157
|
-
const
|
|
157
|
+
const useOpenRouter = resolveUseOpenRouter(config);
|
|
158
158
|
if (openRouterKey) {
|
|
159
159
|
routerConfig.openrouter = { apiKey: openRouterKey };
|
|
160
160
|
routerConfig.openRouter = {
|
|
161
161
|
enabled: true,
|
|
162
|
-
prefer:
|
|
162
|
+
prefer: useOpenRouter,
|
|
163
163
|
};
|
|
164
|
-
if (
|
|
164
|
+
if (useOpenRouter) {
|
|
165
165
|
routerConfig.defaultMode = 'openrouter';
|
|
166
166
|
}
|
|
167
167
|
}
|
|
@@ -190,7 +190,7 @@ export function initializeGatewayComponents(config) {
|
|
|
190
190
|
activityManager,
|
|
191
191
|
usageTracker,
|
|
192
192
|
messageBuilderConfig,
|
|
193
|
-
|
|
193
|
+
useOpenRouter,
|
|
194
194
|
openRouterApiKey: openRouterKey,
|
|
195
195
|
};
|
|
196
196
|
}
|
|
@@ -37,6 +37,6 @@ export declare function initializeGatewayComponents(config: GatewayConfig): {
|
|
|
37
37
|
activityManager: ActivityManager;
|
|
38
38
|
usageTracker: UsageTracker;
|
|
39
39
|
messageBuilderConfig: MessageBuilderConfig;
|
|
40
|
-
|
|
40
|
+
useOpenRouter: boolean;
|
|
41
41
|
openRouterApiKey?: string;
|
|
42
42
|
};
|
|
@@ -104,7 +104,7 @@ export async function mergeConfig(request, config, logger, mergeOptions) {
|
|
|
104
104
|
catalog: mergeOptions.catalog,
|
|
105
105
|
routingEnv: mergeOptions.routingEnv,
|
|
106
106
|
openRouterApiKey: mergeOptions.openRouterApiKey,
|
|
107
|
-
|
|
107
|
+
useOpenRouter: mergeOptions.useOpenRouter,
|
|
108
108
|
defaultProvider: config.defaultEngine,
|
|
109
109
|
resolveModels: true,
|
|
110
110
|
modelsOnly: config.aiTools?.modelsOnly !== false,
|
|
@@ -146,7 +146,7 @@ export async function mergeConfig(request, config, logger, mergeOptions) {
|
|
|
146
146
|
resolveModels: false,
|
|
147
147
|
routingEnv: mergeOptions.routingEnv,
|
|
148
148
|
openRouterApiKey: mergeOptions.openRouterApiKey,
|
|
149
|
-
|
|
149
|
+
useOpenRouter: mergeOptions.useOpenRouter,
|
|
150
150
|
defaultProvider: config.defaultEngine,
|
|
151
151
|
});
|
|
152
152
|
merged.provider = resolved.router.provider;
|
|
@@ -512,7 +512,14 @@ export async function resolveCostCompletionWithAiTools(routerResponse, tokens, o
|
|
|
512
512
|
options?.calculator &&
|
|
513
513
|
hasNonZeroTokenUsage(tokens)) {
|
|
514
514
|
try {
|
|
515
|
-
const record =
|
|
515
|
+
const record = routerResponse != null && typeof routerResponse === 'object'
|
|
516
|
+
? {
|
|
517
|
+
...routerResponse,
|
|
518
|
+
...(options.mergedConfig != null ? { config: options.mergedConfig } : {})
|
|
519
|
+
}
|
|
520
|
+
: options.mergedConfig != null
|
|
521
|
+
? { config: options.mergedConfig, metadata: { tokens } }
|
|
522
|
+
: { metadata: { tokens } };
|
|
516
523
|
const result = await options.calculator.calculateFromRecord(record);
|
|
517
524
|
billing = mapAiCostResultToResolvedActivityCost(billing, result);
|
|
518
525
|
}
|
|
@@ -17,7 +17,7 @@ export type MergeConfigOptions = {
|
|
|
17
17
|
catalog?: AiModelsCatalogClient | null;
|
|
18
18
|
routingEnv?: OpenRouterRoutingConfig;
|
|
19
19
|
openRouterApiKey?: string;
|
|
20
|
-
|
|
20
|
+
useOpenRouter?: boolean;
|
|
21
21
|
};
|
|
22
22
|
export { MODEL_PROFILE_UNROUTABLE, ModelProfileUnroutableError, ModelProfileInputRejectedError, } from '@x12i/ai-tools';
|
|
23
23
|
/**
|
package/dist-cjs/gateway.cjs
CHANGED
|
@@ -59,7 +59,7 @@ export class AIGateway {
|
|
|
59
59
|
messageBuilderConfig;
|
|
60
60
|
_autoRegisterDone = false;
|
|
61
61
|
_aiToolsClient = null;
|
|
62
|
-
|
|
62
|
+
useOpenRouter;
|
|
63
63
|
openRouterApiKey;
|
|
64
64
|
constructor(config = {}, activityManager) {
|
|
65
65
|
this.config = config;
|
|
@@ -69,7 +69,7 @@ export class AIGateway {
|
|
|
69
69
|
this.router = components.router;
|
|
70
70
|
this.activityManager = components.activityManager;
|
|
71
71
|
this.messageBuilderConfig = components.messageBuilderConfig;
|
|
72
|
-
this.
|
|
72
|
+
this.useOpenRouter = components.useOpenRouter;
|
|
73
73
|
this.openRouterApiKey = components.openRouterApiKey;
|
|
74
74
|
setGatewayRuntimeClients({
|
|
75
75
|
activix: this.activityManager?.getTracker(),
|
|
@@ -100,7 +100,7 @@ export class AIGateway {
|
|
|
100
100
|
catalog: aiTools?.catalog ?? null,
|
|
101
101
|
routingEnv: aiTools?.routingEnv,
|
|
102
102
|
openRouterApiKey: this.openRouterApiKey,
|
|
103
|
-
|
|
103
|
+
useOpenRouter: this.useOpenRouter,
|
|
104
104
|
});
|
|
105
105
|
// Activix start snapshot must match what the router receives (modelConfig-only callers omit request.config.model).
|
|
106
106
|
request._mergedRouterConfig = mergedConfig;
|
|
@@ -288,7 +288,7 @@ export class AIGateway {
|
|
|
288
288
|
catalog: aiTools?.catalog ?? null,
|
|
289
289
|
routingEnv: aiTools?.routingEnv,
|
|
290
290
|
openRouterApiKey: this.openRouterApiKey,
|
|
291
|
-
|
|
291
|
+
useOpenRouter: this.useOpenRouter,
|
|
292
292
|
});
|
|
293
293
|
request._mergedRouterConfig = mergedConfig;
|
|
294
294
|
logResolvedModelRouting(this.logger, request, mergedConfig);
|
package/dist-cjs/gateway.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare class AIGateway {
|
|
|
18
18
|
private messageBuilderConfig?;
|
|
19
19
|
private _autoRegisterDone;
|
|
20
20
|
private _aiToolsClient;
|
|
21
|
-
private readonly
|
|
21
|
+
private readonly useOpenRouter;
|
|
22
22
|
private readonly openRouterApiKey?;
|
|
23
23
|
constructor(config?: GatewayConfig, activityManager?: ActivityManager);
|
|
24
24
|
/**
|
package/dist-cjs/index.cjs
CHANGED
|
@@ -33,8 +33,8 @@ export { activityIdentityToLogContext, activityIdentityToLogMeta, withActivityId
|
|
|
33
33
|
export { createGatewayLogger, resolveGatewayVerboseEnabled } from './logger-factory.js';
|
|
34
34
|
export { GATEWAY_LOG_ENV_PREFIX, GATEWAY_LOGXER_PACKAGE, GATEWAY_STACK_LOG_PREFIXES, initializeGatewayPackageLogLevels, resetGatewayPackageLogLevelsInit } from './gateway-log-levels.js';
|
|
35
35
|
export { GatewayLogCode, gatewayErrorCode, gatewayInfoCode, gatewayWarnCode, gatewayAnomalyMeta, resolveLogDiagnosticsCatalogPath, exceptionEvidence, fieldEvidence } from './gateway-log-diagnostics.js';
|
|
36
|
-
/** Re-export @x12i/ai-tools invoke orchestrator (≥
|
|
37
|
-
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolvePreferOpenRouter, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
36
|
+
/** Re-export @x12i/ai-tools invoke orchestrator (≥ 3.0.0) for engine callers. */
|
|
37
|
+
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokeRouting, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
38
38
|
// Re-export logging (@x12i/logxer)
|
|
39
39
|
export { createLogxer, DebugLogAbstract, runWithLogContext, getStationRuntimeIdentity, mergeRuntimeIdentity, conditionEvidence, sourceEvidence, logReferenceEvidence, readAgentLoggingInstructions, resolveAgentLoggingInstructionsPath, applyPackageLogLevelsFromEnv, configurePackageLogLevels, mergePackageLogLevelsConfig, setPackageLogLevel, resolveStackLogLevelForPrefix, resolvePackageLogsLevel, parseLogxerPackageLevelsEnv, LOGXER_PACKAGE_LEVELS_ENV, LOGXER_PACKAGE_LOGS_DEFAULT_ENV } from '@x12i/logxer';
|
|
40
40
|
export { ROUTER_LOG_ENV_PREFIX } from '@x12i/ai-providers-router';
|
package/dist-cjs/index.d.ts
CHANGED
|
@@ -40,8 +40,8 @@ export type { GatewayLoggerConfig } from './logger-factory.js';
|
|
|
40
40
|
export { GATEWAY_LOG_ENV_PREFIX, GATEWAY_LOGXER_PACKAGE, GATEWAY_STACK_LOG_PREFIXES, initializeGatewayPackageLogLevels, resetGatewayPackageLogLevelsInit } from './gateway-log-levels.js';
|
|
41
41
|
export { GatewayLogCode, gatewayErrorCode, gatewayInfoCode, gatewayWarnCode, gatewayAnomalyMeta, resolveLogDiagnosticsCatalogPath, exceptionEvidence, fieldEvidence } from './gateway-log-diagnostics.js';
|
|
42
42
|
export type { GatewayLogCode as GatewayDiagnosticCode } from './gateway-log-diagnostics.js';
|
|
43
|
-
/** Re-export @x12i/ai-tools invoke orchestrator (≥
|
|
44
|
-
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolvePreferOpenRouter, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
43
|
+
/** Re-export @x12i/ai-tools invoke orchestrator (≥ 3.0.0) for engine callers. */
|
|
44
|
+
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokeRouting, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolveUseOpenRouter, resolvePreferOpenRouter, readUseOpenRouterFromEnv, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
45
45
|
export type { AiToolsClientBundle, AiToolsInvokeClient, InvokeModelResolutionDiagnostics, InvokeModelResolutionInput, InvokeModelResolutionOptions, InvokeModelResolutionResult, InvokeRouterConfigSlice, } from './ai-tools-client.js';
|
|
46
46
|
export { createLogxer, DebugLogAbstract, runWithLogContext, getStationRuntimeIdentity, mergeRuntimeIdentity, conditionEvidence, sourceEvidence, logReferenceEvidence, readAgentLoggingInstructions, resolveAgentLoggingInstructionsPath, applyPackageLogLevelsFromEnv, configurePackageLogLevels, mergePackageLogLevelsConfig, setPackageLogLevel, resolveStackLogLevelForPrefix, resolvePackageLogsLevel, parseLogxerPackageLevelsEnv, LOGXER_PACKAGE_LEVELS_ENV, LOGXER_PACKAGE_LOGS_DEFAULT_ENV } from '@x12i/logxer';
|
|
47
47
|
export { ROUTER_LOG_ENV_PREFIX } from '@x12i/ai-providers-router';
|
|
@@ -1,12 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gateway-level OpenRouter key +
|
|
2
|
+
* Gateway-level OpenRouter key + USE_OPENROUTER flags mapped to @x12i/ai-tools v3 invoke helpers.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
5
|
-
|
|
4
|
+
import { resolveOpenRouterApiKey as resolveOpenRouterApiKeyFromTools, resolveUseOpenRouterPreference, } from '@x12i/ai-tools';
|
|
5
|
+
import { readUseOpenRouterFromEnv } from '@x12i/ai-profiles';
|
|
6
|
+
export { readUseOpenRouterFromEnv };
|
|
6
7
|
export function resolveOpenRouterApiKey(config = {}) {
|
|
7
8
|
return resolveOpenRouterApiKeyFromTools(config.openrouter?.apiKey);
|
|
8
9
|
}
|
|
9
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Effective OpenRouter preference for invoke-time routing (ai-tools `useOpenRouter`).
|
|
12
|
+
* Constructor `openRouter.prefer` / `openRouter.enabled` override env `USE_OPENROUTER`.
|
|
13
|
+
*/
|
|
14
|
+
export function resolveUseOpenRouter(config = {}) {
|
|
10
15
|
if (config.openRouter?.prefer === true)
|
|
11
16
|
return true;
|
|
12
17
|
if (config.openRouter?.prefer === false)
|
|
@@ -15,5 +20,13 @@ export function resolvePreferOpenRouter(config = {}) {
|
|
|
15
20
|
return true;
|
|
16
21
|
if (config.openRouter?.enabled === false)
|
|
17
22
|
return false;
|
|
18
|
-
return
|
|
23
|
+
return resolveUseOpenRouterPreference();
|
|
24
|
+
}
|
|
25
|
+
/** @deprecated Use {@link resolveUseOpenRouter} (ai-tools v3 renamed prefer → useOpenRouter). */
|
|
26
|
+
export function resolvePreferOpenRouter(config = {}) {
|
|
27
|
+
return resolveUseOpenRouter(config);
|
|
28
|
+
}
|
|
29
|
+
/** @deprecated Use {@link readUseOpenRouterFromEnv} from `@x12i/ai-profiles`. */
|
|
30
|
+
export function readPreferOpenRouterFromEnv() {
|
|
31
|
+
return readUseOpenRouterFromEnv() ?? true;
|
|
19
32
|
}
|
|
@@ -1,8 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Gateway-level OpenRouter key +
|
|
2
|
+
* Gateway-level OpenRouter key + USE_OPENROUTER flags mapped to @x12i/ai-tools v3 invoke helpers.
|
|
3
3
|
*/
|
|
4
|
-
import {
|
|
4
|
+
import { readUseOpenRouterFromEnv } from '@x12i/ai-profiles';
|
|
5
5
|
import type { GatewayConfig } from './types.js';
|
|
6
|
-
export {
|
|
6
|
+
export { readUseOpenRouterFromEnv };
|
|
7
7
|
export declare function resolveOpenRouterApiKey(config?: GatewayConfig): string | undefined;
|
|
8
|
+
/**
|
|
9
|
+
* Effective OpenRouter preference for invoke-time routing (ai-tools `useOpenRouter`).
|
|
10
|
+
* Constructor `openRouter.prefer` / `openRouter.enabled` override env `USE_OPENROUTER`.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveUseOpenRouter(config?: GatewayConfig): boolean;
|
|
13
|
+
/** @deprecated Use {@link resolveUseOpenRouter} (ai-tools v3 renamed prefer → useOpenRouter). */
|
|
8
14
|
export declare function resolvePreferOpenRouter(config?: GatewayConfig): boolean;
|
|
15
|
+
/** @deprecated Use {@link readUseOpenRouterFromEnv} from `@x12i/ai-profiles`. */
|
|
16
|
+
export declare function readPreferOpenRouterFromEnv(): boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@x12i/ai-gateway",
|
|
3
|
-
"version": "10.0
|
|
3
|
+
"version": "10.1.0",
|
|
4
4
|
"description": "AI Gateway - Unified interface for LLM provider routing and management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -41,9 +41,9 @@
|
|
|
41
41
|
"author": "x12i",
|
|
42
42
|
"license": "mit",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@x12i/activix": "^8.
|
|
44
|
+
"@x12i/activix": "^8.6.0",
|
|
45
45
|
"@x12i/ai-providers-router": "^4.9.2",
|
|
46
|
-
"@x12i/ai-tools": "^
|
|
46
|
+
"@x12i/ai-tools": "^3.0.0",
|
|
47
47
|
"@x12i/flex-md": "^4.8.0",
|
|
48
48
|
"@x12i/logxer": "^4.6.0",
|
|
49
49
|
"@x12i/rendrix": "^4.3.0"
|