@x12i/ai-gateway 11.0.7 → 11.1.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/dist/gateway-config.d.ts +4 -4
- package/dist/gateway-config.js +18 -53
- package/dist/gateway-log-levels.d.ts +3 -1
- package/dist/gateway-log-levels.js +2 -1
- package/dist/gateway-retry.d.ts +2 -2
- package/dist/gateway-router.d.ts +42 -0
- package/dist/gateway-router.js +118 -0
- package/dist/gateway-utils.d.ts +2 -2
- package/dist/gateway-utils.js +1 -1
- package/dist/gateway.d.ts +2 -3
- package/dist/gateway.js +4 -16
- package/dist/index.d.ts +9 -13
- package/dist/index.js +7 -13
- package/dist/openrouter-runtime-adapter/index.d.ts +1 -3
- package/dist/openrouter-runtime-adapter/index.js +1 -3
- package/dist/openrouter-runtime-adapter/map-gateway-request.d.ts +1 -2
- package/dist/openrouter-runtime-adapter/map-gateway-request.js +0 -1
- package/dist/openrouter-runtime-adapter/map-runtime-errors.d.ts +14 -3
- package/dist/openrouter-runtime-adapter/map-runtime-errors.js +13 -3
- package/dist/openrouter-runtime-adapter/map-runtime-response.d.ts +2 -2
- package/dist/openrouter-runtime-adapter/map-server-tools.d.ts +1 -1
- package/dist/openrouter-runtime-adapter/should-use-openrouter-runtime.d.ts +1 -6
- package/dist/openrouter-runtime-adapter/should-use-openrouter-runtime.js +1 -6
- package/dist/router-errors.d.ts +52 -0
- package/dist/router-errors.js +115 -0
- package/dist/router-types.d.ts +147 -0
- package/dist/router-types.js +4 -0
- package/dist/types.d.ts +11 -16
- package/dist-cjs/gateway-config.cjs +18 -53
- package/dist-cjs/gateway-config.d.ts +4 -4
- package/dist-cjs/gateway-log-levels.cjs +2 -1
- package/dist-cjs/gateway-log-levels.d.ts +3 -1
- package/dist-cjs/gateway-retry.d.ts +2 -2
- package/dist-cjs/gateway-router.cjs +118 -0
- package/dist-cjs/gateway-router.d.ts +42 -0
- package/dist-cjs/gateway-utils.cjs +1 -1
- package/dist-cjs/gateway-utils.d.ts +2 -2
- package/dist-cjs/gateway.cjs +4 -16
- package/dist-cjs/gateway.d.ts +2 -3
- package/dist-cjs/index.cjs +7 -13
- package/dist-cjs/index.d.ts +9 -13
- package/dist-cjs/openrouter-runtime-adapter/index.cjs +1 -3
- package/dist-cjs/openrouter-runtime-adapter/index.d.ts +1 -3
- package/dist-cjs/openrouter-runtime-adapter/map-gateway-request.cjs +0 -1
- package/dist-cjs/openrouter-runtime-adapter/map-gateway-request.d.ts +1 -2
- package/dist-cjs/openrouter-runtime-adapter/map-runtime-errors.cjs +13 -3
- package/dist-cjs/openrouter-runtime-adapter/map-runtime-errors.d.ts +14 -3
- package/dist-cjs/openrouter-runtime-adapter/map-runtime-response.d.ts +2 -2
- package/dist-cjs/openrouter-runtime-adapter/map-server-tools.d.ts +1 -1
- package/dist-cjs/openrouter-runtime-adapter/should-use-openrouter-runtime.cjs +1 -6
- package/dist-cjs/openrouter-runtime-adapter/should-use-openrouter-runtime.d.ts +1 -6
- package/dist-cjs/router-errors.cjs +115 -0
- package/dist-cjs/router-errors.d.ts +52 -0
- package/dist-cjs/router-types.cjs +4 -0
- package/dist-cjs/router-types.d.ts +147 -0
- package/dist-cjs/types.d.ts +11 -16
- package/package.json +5 -6
- package/dist/gateway-provider-auto-register.d.ts +0 -17
- package/dist/gateway-provider-auto-register.js +0 -159
- package/dist/gateway-provider.d.ts +0 -54
- package/dist/gateway-provider.js +0 -202
- package/dist/openrouter-runtime-adapter/create-openrouter-runtime-adapter.d.ts +0 -23
- package/dist/openrouter-runtime-adapter/create-openrouter-runtime-adapter.js +0 -31
- package/dist/openrouter-runtime-adapter/create-openrouter-runtime-provider.d.ts +0 -10
- package/dist/openrouter-runtime-adapter/create-openrouter-runtime-provider.js +0 -152
- package/dist/openrouter-runtime-adapter/register-openrouter-runtime.d.ts +0 -10
- package/dist/openrouter-runtime-adapter/register-openrouter-runtime.js +0 -19
- package/dist-cjs/gateway-provider-auto-register.cjs +0 -159
- package/dist-cjs/gateway-provider-auto-register.d.ts +0 -17
- package/dist-cjs/gateway-provider.cjs +0 -202
- package/dist-cjs/gateway-provider.d.ts +0 -54
- package/dist-cjs/openrouter-runtime-adapter/create-openrouter-runtime-adapter.cjs +0 -31
- package/dist-cjs/openrouter-runtime-adapter/create-openrouter-runtime-adapter.d.ts +0 -23
- package/dist-cjs/openrouter-runtime-adapter/create-openrouter-runtime-provider.cjs +0 -152
- package/dist-cjs/openrouter-runtime-adapter/create-openrouter-runtime-provider.d.ts +0 -10
- package/dist-cjs/openrouter-runtime-adapter/register-openrouter-runtime.cjs +0 -19
- package/dist-cjs/openrouter-runtime-adapter/register-openrouter-runtime.d.ts +0 -10
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
* Between-calls rate limiting is handled separately in gateway-rate-limiter.ts (smart).
|
|
7
7
|
*/
|
|
8
8
|
import type { RetryConfig } from './types.js';
|
|
9
|
-
import type {
|
|
9
|
+
import type { GatewayRouter } from './gateway-router.js';
|
|
10
10
|
import type { Logxer } from '@x12i/logxer';
|
|
11
11
|
/**
|
|
12
12
|
* Determines if an error is a network error (fetch failed, DNS, connectivity)
|
|
@@ -34,7 +34,7 @@ export declare function sleep(ms: number): Promise<void>;
|
|
|
34
34
|
* Invokes router with retry logic for network and server errors
|
|
35
35
|
* Returns response and retry metadata
|
|
36
36
|
*/
|
|
37
|
-
export declare function invokeWithRetry(routerRequest: any, retryConfig: RetryConfig, jobId: string, router:
|
|
37
|
+
export declare function invokeWithRetry(routerRequest: any, retryConfig: RetryConfig, jobId: string, router: GatewayRouter, logger: Logxer, hooks?: {
|
|
38
38
|
/**
|
|
39
39
|
* Called immediately before each provider call attempt (including the first try).
|
|
40
40
|
* `retryIndex` is 0-based within this invokeWithRetry call.
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway router backed by @x12i/ai-router-runtime.
|
|
3
|
+
*/
|
|
4
|
+
import { createAiRouterRuntime, AiRouterError, } from '@x12i/ai-router-runtime';
|
|
5
|
+
import { resolveOpenRouterRuntimeDefaults } from './openrouter-runtime-adapter/should-use-openrouter-runtime.js';
|
|
6
|
+
import { mapGatewayRequestToRuntimeRequest, } from './openrouter-runtime-adapter/map-gateway-request.js';
|
|
7
|
+
import { parseRuntimeResponseToAIResponse } from './openrouter-runtime-adapter/map-runtime-response.js';
|
|
8
|
+
import { mapRuntimeErrorToGatewayError, throwMappedRuntimeResponseErrors, } from './openrouter-runtime-adapter/map-runtime-errors.js';
|
|
9
|
+
import { OpenRouterApiKeyMissingError } from './router-errors.js';
|
|
10
|
+
/** Stable ERC 2.0 env prefix for router runtime logging. */
|
|
11
|
+
export const ROUTER_LOG_ENV_PREFIX = 'AI_ROUTER_RUNTIME';
|
|
12
|
+
function mapLogxerToRuntimeLogger(logger) {
|
|
13
|
+
return {
|
|
14
|
+
debug: (event, data) => logger.debug(event, data),
|
|
15
|
+
info: (event, data) => logger.info(event, data),
|
|
16
|
+
warn: (event, data) => logger.warn(event, data),
|
|
17
|
+
error: (event, data) => logger.error(event, data),
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function buildRuntimeOptions(config, apiKey) {
|
|
21
|
+
const runtimeDefaults = resolveOpenRouterRuntimeDefaults({ openrouterRuntime: config.openrouterRuntime });
|
|
22
|
+
const timeoutMs = config.timeoutMs ?? config.defaultTimeoutMs;
|
|
23
|
+
return {
|
|
24
|
+
provider: 'openrouter',
|
|
25
|
+
logger: config.logger ? mapLogxerToRuntimeLogger(config.logger) : undefined,
|
|
26
|
+
openrouter: {
|
|
27
|
+
apiKey,
|
|
28
|
+
defaults: {
|
|
29
|
+
apiMode: runtimeDefaults.apiMode,
|
|
30
|
+
retry: { enabled: false },
|
|
31
|
+
onPolicyViolation: 'throw',
|
|
32
|
+
...(timeoutMs ? { timeoutMs } : {}),
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export class GatewayRouter {
|
|
38
|
+
runtime;
|
|
39
|
+
logger;
|
|
40
|
+
apiKey;
|
|
41
|
+
requestInterceptors = [];
|
|
42
|
+
constructor(config = {}) {
|
|
43
|
+
this.logger = config.logger;
|
|
44
|
+
this.apiKey = config.openrouter?.apiKey;
|
|
45
|
+
this.runtime = createAiRouterRuntime(buildRuntimeOptions(config, this.apiKey));
|
|
46
|
+
}
|
|
47
|
+
addRequestInterceptor(interceptor) {
|
|
48
|
+
this.requestInterceptors.push(interceptor);
|
|
49
|
+
}
|
|
50
|
+
addResponseInterceptor(_interceptor) {
|
|
51
|
+
this.logger?.debug('addResponseInterceptor is not supported with @x12i/ai-router-runtime');
|
|
52
|
+
}
|
|
53
|
+
async invoke(input) {
|
|
54
|
+
if (!this.apiKey) {
|
|
55
|
+
throw new OpenRouterApiKeyMissingError();
|
|
56
|
+
}
|
|
57
|
+
let request = { ...(input.request ?? {}) };
|
|
58
|
+
for (const interceptor of this.requestInterceptors) {
|
|
59
|
+
request = (await interceptor(request, { getProviderName: () => 'openrouter' }));
|
|
60
|
+
}
|
|
61
|
+
const runtimeRequest = mapGatewayRequestToRuntimeRequest(request, {
|
|
62
|
+
timeoutMs: input.exec?.timeoutMs,
|
|
63
|
+
});
|
|
64
|
+
try {
|
|
65
|
+
const response = await this.runtime.run(runtimeRequest);
|
|
66
|
+
if (response.status !== 'completed') {
|
|
67
|
+
throwMappedRuntimeResponseErrors(response);
|
|
68
|
+
}
|
|
69
|
+
return parseRuntimeResponseToAIResponse({
|
|
70
|
+
requestId: input.requestId ?? runtimeRequest.id ?? 'unknown',
|
|
71
|
+
execResult: { rawResponse: response },
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
catch (err) {
|
|
75
|
+
if (err instanceof AiRouterError && err.code === 'OPENROUTER_API_KEY_MISSING') {
|
|
76
|
+
throw new OpenRouterApiKeyMissingError();
|
|
77
|
+
}
|
|
78
|
+
throw mapRuntimeErrorToGatewayError(err);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
registerProvider(_provider) {
|
|
82
|
+
this.logger?.warn('registerProvider is not supported with @x12i/ai-router-runtime; only openrouter is available');
|
|
83
|
+
}
|
|
84
|
+
listProviders() {
|
|
85
|
+
return this.apiKey ? ['openrouter'] : [];
|
|
86
|
+
}
|
|
87
|
+
getAdapterRegistry() {
|
|
88
|
+
return { register: () => undefined };
|
|
89
|
+
}
|
|
90
|
+
getProviderRegistry() {
|
|
91
|
+
return {
|
|
92
|
+
register: () => undefined,
|
|
93
|
+
unregister: () => undefined,
|
|
94
|
+
get: () => undefined,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
async checkHealth(provider) {
|
|
98
|
+
if (provider !== 'openrouter') {
|
|
99
|
+
return { provider, healthy: false, error: `Provider "${provider}" is not supported` };
|
|
100
|
+
}
|
|
101
|
+
if (!this.apiKey) {
|
|
102
|
+
return { provider, healthy: false, error: 'OPENROUTER_API_KEY is missing' };
|
|
103
|
+
}
|
|
104
|
+
return { provider, healthy: true, latencyMs: 0 };
|
|
105
|
+
}
|
|
106
|
+
/** Access the underlying @x12i/ai-router-runtime instance. */
|
|
107
|
+
getRuntime() {
|
|
108
|
+
return this.runtime;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
/** @deprecated Use {@link GatewayRouter}. */
|
|
112
|
+
export const LLMProviderRouter = GatewayRouter;
|
|
113
|
+
export async function createRouter(config) {
|
|
114
|
+
return new GatewayRouter(config ?? {});
|
|
115
|
+
}
|
|
116
|
+
export async function createRouterFromConfig(_configPath) {
|
|
117
|
+
return createRouter();
|
|
118
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway router backed by @x12i/ai-router-runtime.
|
|
3
|
+
*/
|
|
4
|
+
import { type AiRouterRuntime } from '@x12i/ai-router-runtime';
|
|
5
|
+
import type { AIResponse, AIRouterRequest, GatewayRouterConfig, HealthCheckResult } from './router-types.js';
|
|
6
|
+
import type { GatewayOpenRouterRuntimeConfig } from './types.js';
|
|
7
|
+
/** Stable ERC 2.0 env prefix for router runtime logging. */
|
|
8
|
+
export declare const ROUTER_LOG_ENV_PREFIX = "AI_ROUTER_RUNTIME";
|
|
9
|
+
export type CreateRouterConfig = GatewayRouterConfig & {
|
|
10
|
+
openrouterRuntime?: GatewayOpenRouterRuntimeConfig;
|
|
11
|
+
};
|
|
12
|
+
type RequestInterceptor = (request: Record<string, unknown>, provider?: {
|
|
13
|
+
getProviderName?: () => string;
|
|
14
|
+
}) => Promise<Record<string, unknown>> | Record<string, unknown>;
|
|
15
|
+
export declare class GatewayRouter {
|
|
16
|
+
private readonly runtime;
|
|
17
|
+
private readonly logger?;
|
|
18
|
+
private readonly apiKey?;
|
|
19
|
+
private readonly requestInterceptors;
|
|
20
|
+
constructor(config?: CreateRouterConfig);
|
|
21
|
+
addRequestInterceptor(interceptor: RequestInterceptor): void;
|
|
22
|
+
addResponseInterceptor(_interceptor: unknown): void;
|
|
23
|
+
invoke(input: AIRouterRequest): Promise<AIResponse>;
|
|
24
|
+
registerProvider(_provider: unknown): void;
|
|
25
|
+
listProviders(): string[];
|
|
26
|
+
getAdapterRegistry(): {
|
|
27
|
+
register: (_adapter: unknown) => void;
|
|
28
|
+
};
|
|
29
|
+
getProviderRegistry(): {
|
|
30
|
+
register: (_provider: unknown) => void;
|
|
31
|
+
unregister: (_name: string) => void;
|
|
32
|
+
get: (_name: string) => undefined;
|
|
33
|
+
};
|
|
34
|
+
checkHealth(provider: string): Promise<HealthCheckResult>;
|
|
35
|
+
/** Access the underlying @x12i/ai-router-runtime instance. */
|
|
36
|
+
getRuntime(): AiRouterRuntime;
|
|
37
|
+
}
|
|
38
|
+
/** @deprecated Use {@link GatewayRouter}. */
|
|
39
|
+
export declare const LLMProviderRouter: typeof GatewayRouter;
|
|
40
|
+
export declare function createRouter(config?: CreateRouterConfig): Promise<GatewayRouter>;
|
|
41
|
+
export declare function createRouterFromConfig(_configPath: string): Promise<GatewayRouter>;
|
|
42
|
+
export {};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* Handles utility functions
|
|
4
4
|
*/
|
|
5
5
|
import * as crypto from 'crypto';
|
|
6
|
-
import { FallbackExhaustedError, enrichRunTaskError, providerAttemptsFromFallbackAttempts } from '
|
|
6
|
+
import { FallbackExhaustedError, enrichRunTaskError, providerAttemptsFromFallbackAttempts } from './router-errors.js';
|
|
7
7
|
import { ModelResolutionError, ModelProfileInputRejectedError, ModelProfileUnroutableError, resolveInvokeModel, } from '@x12i/ai-tools';
|
|
8
8
|
import { extractHttpStatusCode } from './gateway-retry.js';
|
|
9
9
|
import { gatewayLogDebug, withActivityIdentity } from './gateway-log-meta.js';
|
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
* Handles utility functions
|
|
4
4
|
*/
|
|
5
5
|
import type { AIInvokeRequest, ChatRequest, GatewayConfig, GatewayFallbackAttempt, GatewayInvokeRejectionMetadata, GatewayTraceAttempt, GatewayTraceMergedConfig, GatewayTraceRequestIds, GatewayTraceUsageSummary, ModelConfig } from './types.js';
|
|
6
|
-
import { FallbackExhaustedError } from '
|
|
7
|
-
import type { ProviderFailureCode } from '
|
|
6
|
+
import { FallbackExhaustedError } from './router-errors.js';
|
|
7
|
+
import type { ProviderFailureCode } from './router-errors.js';
|
|
8
8
|
import type { Logxer } from '@x12i/logxer';
|
|
9
9
|
import { type AiCostResult, type AiModelsCatalogClient, type CostCalculator, type OpenRouterRoutingConfig } from '@x12i/ai-tools';
|
|
10
10
|
import { pickOpenRouterRuntimeMetadataSlice, enrichTraceOpenRouterRuntimeMetadata } from './openrouter-runtime-adapter/index.js';
|
package/dist-cjs/gateway.cjs
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Simplified AI Gateway - Clean proxy implementation
|
|
5
5
|
*/
|
|
6
|
-
import { FallbackExhaustedError } from '
|
|
6
|
+
import { FallbackExhaustedError } from './router-errors.js';
|
|
7
7
|
import { validateChatRequest, validateAIRequest } from './gateway-validation.js';
|
|
8
8
|
import { ensureGatewayRequestIdentity } from './activity-manager.js';
|
|
9
9
|
import { initializeGatewayComponents } from './gateway-config.js';
|
|
@@ -14,14 +14,13 @@ import { enrichParsedContentForOutputContract, resolveOutputContractFieldKeys }
|
|
|
14
14
|
import { attachGatewayInvokeRejectionMetadata, buildGatewayFallbackAttemptsFromTrace, buildInvokeRejectionMetadata, capActivityFullResponsePayload, formatFallbackExhaustionMessage, logResolvedModelRouting, enrichGatewayFallbackExhaustedError, mapGatewayFallbackAttemptsToRouter, DEFAULT_ACTIVITY_FULL_RESPONSE_MAX_CHARS, extractCostUsdFromRouterResponse, extractTokenUsageFromRouterResponse, mergeConfig, pickEffectiveModelConfigForMetadata, pickInvokeRoutingMetadataSlice, pickTraceMergedRouterConfig, pickEnhancedOpenRouterMetadata, resolveCostCompletionWithAiTools, buildTraceUsageSummary, enrichTraceAttemptsWithBilling, tryExtractRouterLikePayloadFromErrorChain, tryExtractOpenRouterMetadataFromErrorChain, } from './gateway-utils.js';
|
|
15
15
|
import { buildTraceAttemptOpenRouterRuntimeSlice } from './openrouter-runtime-adapter/index.js';
|
|
16
16
|
import { getAiToolsClient } from './ai-tools-client.js';
|
|
17
|
-
import { autoRegisterProviders } from './gateway-provider-auto-register.js';
|
|
18
17
|
import { setGatewayLastJobId, setGatewayRuntimeClients } from './runtime-objects.js';
|
|
19
18
|
import { gatewayLogDebug, withActivityIdentity, withGatewayLogContext } from './gateway-log-meta.js';
|
|
20
19
|
import { exceptionEvidence, fieldEvidence, GatewayLogCode, gatewayErrorCode, gatewayWarnCode } from './gateway-log-diagnostics.js';
|
|
21
20
|
import { invokeWithRetry } from './gateway-retry.js';
|
|
22
|
-
/** Error message
|
|
23
|
-
const NO_PROVIDER_ERROR = '
|
|
24
|
-
const NO_PROVIDER_HINT = ' Set OPENROUTER_API_KEY in the environment to use OpenRouter
|
|
21
|
+
/** Error message when OpenRouter is not configured */
|
|
22
|
+
const NO_PROVIDER_ERROR = 'OPENROUTER_API_KEY is required';
|
|
23
|
+
const NO_PROVIDER_HINT = ' Set OPENROUTER_API_KEY in the environment to use OpenRouter.';
|
|
25
24
|
/** Warn when a successful call reports no tokens and/or explicit zero cost (often missing adapter metadata). */
|
|
26
25
|
function warnIfSuccessfulInvokeReportsZeroUsageOrCost(logger, identity, meta, invokeKind) {
|
|
27
26
|
const { tokens, costUsd, cost } = meta;
|
|
@@ -58,7 +57,6 @@ export class AIGateway {
|
|
|
58
57
|
logger;
|
|
59
58
|
activityManager;
|
|
60
59
|
messageBuilderConfig;
|
|
61
|
-
_autoRegisterDone = false;
|
|
62
60
|
_aiToolsClient = null;
|
|
63
61
|
preferOpenRouter;
|
|
64
62
|
openRouterApiKey;
|
|
@@ -151,11 +149,6 @@ export class AIGateway {
|
|
|
151
149
|
});
|
|
152
150
|
// Activix start snapshot must match what the router receives (modelConfig-only callers omit request.config.model).
|
|
153
151
|
request._mergedRouterConfig = mergedConfig;
|
|
154
|
-
// Lazy auto-register providers from env (OPENAI_API_KEY, etc.) so consumers don't have to call init
|
|
155
|
-
if (!this._autoRegisterDone) {
|
|
156
|
-
await autoRegisterProviders(this.router, this.logger);
|
|
157
|
-
this._autoRegisterDone = true;
|
|
158
|
-
}
|
|
159
152
|
// Start activity tracking if available
|
|
160
153
|
let activity = undefined;
|
|
161
154
|
if (this.activityManager) {
|
|
@@ -346,11 +339,6 @@ export class AIGateway {
|
|
|
346
339
|
const diagnosticsMode = request.diagnostics?.mode;
|
|
347
340
|
const traceEnabled = diagnosticsMode === 'trace';
|
|
348
341
|
const includeRawProviderPayload = request.diagnostics?.includeRawProviderPayload === true;
|
|
349
|
-
// Lazy auto-register providers from env (OPENAI_API_KEY, etc.) so consumers don't have to call init
|
|
350
|
-
if (!this._autoRegisterDone) {
|
|
351
|
-
await autoRegisterProviders(this.router, this.logger);
|
|
352
|
-
this._autoRegisterDone = true;
|
|
353
|
-
}
|
|
354
342
|
// Start activity tracking if available
|
|
355
343
|
let activity = undefined;
|
|
356
344
|
if (this.activityManager) {
|
package/dist-cjs/gateway.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
*
|
|
4
4
|
* Simplified AI Gateway - Clean proxy implementation
|
|
5
5
|
*/
|
|
6
|
-
import {
|
|
6
|
+
import { GatewayRouter } from './gateway-router.js';
|
|
7
7
|
import type { GatewayConfig, ChatRequest, AIInvokeRequest, EnhancedLLMResponse } from './types.js';
|
|
8
8
|
import type { Logxer } from '@x12i/logxer';
|
|
9
9
|
import { ActivityManager } from './activity-manager.js';
|
|
@@ -16,7 +16,6 @@ export declare class AIGateway {
|
|
|
16
16
|
private logger;
|
|
17
17
|
private activityManager?;
|
|
18
18
|
private messageBuilderConfig?;
|
|
19
|
-
private _autoRegisterDone;
|
|
20
19
|
private _aiToolsClient;
|
|
21
20
|
private readonly preferOpenRouter;
|
|
22
21
|
private readonly openRouterApiKey?;
|
|
@@ -31,7 +30,7 @@ export declare class AIGateway {
|
|
|
31
30
|
invoke<TContent = unknown>(request: AIInvokeRequest): Promise<EnhancedLLMResponse<TContent>>;
|
|
32
31
|
register(provider: any): void;
|
|
33
32
|
listProviders(): string[];
|
|
34
|
-
getRouter():
|
|
33
|
+
getRouter(): GatewayRouter;
|
|
35
34
|
getLogger(): Logxer;
|
|
36
35
|
getActivityManager(): ActivityManager | undefined;
|
|
37
36
|
setActivityManager(activityManager: ActivityManager): void;
|
package/dist-cjs/index.cjs
CHANGED
|
@@ -2,23 +2,18 @@
|
|
|
2
2
|
* @x12i/ai-gateway
|
|
3
3
|
*
|
|
4
4
|
* Unified gateway for LLM provider routing and management.
|
|
5
|
-
*
|
|
6
|
-
* with enhanced features: context propagation, usage tier tracking, and comprehensive metadata.
|
|
5
|
+
* Router execution is backed by @x12i/ai-router-runtime with enhanced gateway features.
|
|
7
6
|
*/
|
|
8
|
-
//
|
|
9
|
-
export {
|
|
10
|
-
//
|
|
11
|
-
export { createRouter, createRouterFromConfig } from '
|
|
12
|
-
|
|
13
|
-
export { ProviderNotFoundError, FallbackExhaustedError, OpenRouterApiKeyMissingError, } from '@x12i/ai-providers-router';
|
|
14
|
-
// Re-export all from router (includes everything it exports)
|
|
15
|
-
export * from '@x12i/ai-providers-router';
|
|
7
|
+
// Router runtime (OpenRouter protocol)
|
|
8
|
+
export { createAiRouterRuntime, AiRouterError, IMPLEMENTED_AI_PROVIDERS, isImplementedAiProvider, providerNotImplemented, resolveProvider, } from '@x12i/ai-router-runtime';
|
|
9
|
+
// Gateway router (invoke-compatible wrapper)
|
|
10
|
+
export { GatewayRouter, LLMProviderRouter, createRouter, createRouterFromConfig, ROUTER_LOG_ENV_PREFIX, } from './gateway-router.js';
|
|
11
|
+
export { FallbackExhaustedError, ProviderNotFoundError, OpenRouterApiKeyMissingError, PROVIDER_FAILURE_CODES, operatorHintForProviderFailureCode, enrichRunTaskError, providerAttemptsFromFallbackAttempts, buildProviderAttemptDetail, } from './router-errors.js';
|
|
16
12
|
// Export enhanced gateway
|
|
17
13
|
export { AIGateway } from './gateway.js';
|
|
18
14
|
export { InstructionNotFoundError, InstructionBackendError, ModelRequiredError, MaxTokensRequiredError, GatewayAliasModelRejectedError, GatewayValidationError, ProviderConfigError, ProviderInvokeError, GatewayPolicyViolationError, } from './instruction-errors.js';
|
|
19
15
|
export { normalizeInvokeModelAtIngress } from './invoke-model-ingress.js';
|
|
20
|
-
export {
|
|
21
|
-
export { registerOpenRouterRuntime, shouldUseOpenRouterRuntime, mapGatewayServerTools, } from './openrouter-runtime-adapter/index.js';
|
|
16
|
+
export { shouldUseOpenRouterRuntime, mapGatewayServerTools, } from './openrouter-runtime-adapter/index.js';
|
|
22
17
|
export { attachGatewayInvokeRejectionMetadata, buildInvokeRejectionMetadata, tryExtractRouterLikePayloadFromErrorChain, tryExtractOpenRouterMetadataFromErrorChain, tryExtractFallbackAttemptsFromErrorChain, pickRequestIdsFromRouterLike, resolveActivityCostCompletion, resolveCostCompletionForActivity, resolveCostCompletionWithAiTools, buildGatewayPricingRecord, mapAiCostResultToResolvedActivityCost, catalogPricingSucceeded, extractUsageExtrasFromRouterResponse, buildTraceUsageSummary, enrichTraceAttemptsWithBilling, pickEnhancedOpenRouterMetadata, hasNonZeroTokenUsage, MODEL_PROFILE_UNROUTABLE, ModelProfileUnroutableError, ModelProfileInputRejectedError, buildGatewayFallbackAttemptsFromTrace, formatFallbackExhaustionMessage, enrichGatewayFallbackExhaustedError, logResolvedModelRouting, mapGatewayFallbackAttemptsToRouter } from './gateway-utils.js';
|
|
23
18
|
export { getGatewayOperationalMode, isProdGatewayMode, parseModelProviderSpec } from './gateway-mode.js';
|
|
24
19
|
export { DEFAULT_ACTIVITY_FULL_RESPONSE_MAX_CHARS, GATEWAY_DEFAULT_FREQUENCY_PENALTY, GATEWAY_DEFAULT_PRESENCE_PENALTY, GATEWAY_DEFAULT_RETRY, GATEWAY_DEFAULT_TEMPERATURE, GATEWAY_DEFAULT_TOP_P, resolveRetryConfig } from './gateway-defaults.js';
|
|
@@ -39,7 +34,6 @@ export { GatewayLogCode, gatewayErrorCode, gatewayInfoCode, gatewayWarnCode, gat
|
|
|
39
34
|
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokeRouting, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolvePreferOpenRouter, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
40
35
|
// Re-export logging (@x12i/logxer)
|
|
41
36
|
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';
|
|
42
|
-
export { ROUTER_LOG_ENV_PREFIX } from '@x12i/ai-providers-router';
|
|
43
37
|
// Runtime observability surface (leaf package: no downstream runtime objects)
|
|
44
38
|
export { runtimeObjects } from './runtime-objects.js';
|
|
45
39
|
// Runtime observability surface (leaf package: no downstream runtime objects)
|
package/dist-cjs/index.d.ts
CHANGED
|
@@ -2,22 +2,19 @@
|
|
|
2
2
|
* @x12i/ai-gateway
|
|
3
3
|
*
|
|
4
4
|
* Unified gateway for LLM provider routing and management.
|
|
5
|
-
*
|
|
6
|
-
* with enhanced features: context propagation, usage tier tracking, and comprehensive metadata.
|
|
5
|
+
* Router execution is backed by @x12i/ai-router-runtime with enhanced gateway features.
|
|
7
6
|
*/
|
|
8
|
-
export {
|
|
9
|
-
export type {
|
|
10
|
-
export { createRouter, createRouterFromConfig } from '
|
|
11
|
-
export type { CreateRouterConfig } from '
|
|
12
|
-
export {
|
|
13
|
-
export
|
|
14
|
-
export type {
|
|
15
|
-
export * from '@x12i/ai-providers-router';
|
|
7
|
+
export { createAiRouterRuntime, AiRouterError, IMPLEMENTED_AI_PROVIDERS, isImplementedAiProvider, providerNotImplemented, resolveProvider, } from '@x12i/ai-router-runtime';
|
|
8
|
+
export type { AiProviderId, AiRouterRequest, AiRouterRuntime, AiRouterRuntimeOptions, ImplementedAiProvider, } from '@x12i/ai-router-runtime';
|
|
9
|
+
export { GatewayRouter, LLMProviderRouter, createRouter, createRouterFromConfig, ROUTER_LOG_ENV_PREFIX, } from './gateway-router.js';
|
|
10
|
+
export type { CreateRouterConfig } from './gateway-router.js';
|
|
11
|
+
export type { AIResponse, AIRouterRequest, GatewayRouterConfig, RouterConfig, HealthCheckResult, RouterMode, NormalizedUsage, ProviderModelRef as RouterProviderModelRef, } from './router-types.js';
|
|
12
|
+
export { FallbackExhaustedError, ProviderNotFoundError, OpenRouterApiKeyMissingError, PROVIDER_FAILURE_CODES, operatorHintForProviderFailureCode, enrichRunTaskError, providerAttemptsFromFallbackAttempts, buildProviderAttemptDetail, } from './router-errors.js';
|
|
13
|
+
export type { FallbackAttempt, ProviderFailureCode, ProviderAttemptDetail, } from './router-errors.js';
|
|
16
14
|
export { AIGateway } from './gateway.js';
|
|
17
15
|
export { InstructionNotFoundError, InstructionBackendError, ModelRequiredError, MaxTokensRequiredError, GatewayAliasModelRejectedError, GatewayValidationError, ProviderConfigError, ProviderInvokeError, GatewayPolicyViolationError, } from './instruction-errors.js';
|
|
18
16
|
export { normalizeInvokeModelAtIngress } from './invoke-model-ingress.js';
|
|
19
|
-
export {
|
|
20
|
-
export { registerOpenRouterRuntime, shouldUseOpenRouterRuntime, mapGatewayServerTools, } from './openrouter-runtime-adapter/index.js';
|
|
17
|
+
export { shouldUseOpenRouterRuntime, mapGatewayServerTools, } from './openrouter-runtime-adapter/index.js';
|
|
21
18
|
export type { GatewayConfig, ProviderModelRef, ModelConfig, GatewayModelConfig, GatewayServerToolsConfig, GatewayServerToolMode, GatewayServerToolUsage, GatewayNestedServerTool, GatewayWebSearchToolConfig, GatewayWebFetchToolConfig, GatewayDatetimeToolConfig, GatewayImageGenerationToolConfig, GatewayApplyPatchToolConfig, GatewayFusionToolConfig, GatewayAdvisorToolConfig, GatewaySubagentToolConfig, GatewayOpenRouterErrorMetadata, GatewayOpenRouterConfig, GatewayOpenRouterRuntimeConfig, GatewayServerToolUsageMap, GatewayCitation, GatewayGeneratedImage, GatewayPatchProposal, GatewayOpenRouterRuntimeMetadata, RetryConfig, ChatRequest, AIInvokeRequest, AIRequest, GatewayActionType, GatewayInvokeRejectionMetadata, GatewayFallbackAttempt, GatewayTraceRequestIds, GatewayTraceAttempt, GatewayTraceUsageSummary, GatewayTraceMergedConfig, EnhancedLLMResponse, InstructionMetadata, ValidationRule, TemplateRenderOptions, SmartInputConfig, SmartInputRenderOptions } from './types.js';
|
|
22
19
|
export { attachGatewayInvokeRejectionMetadata, buildInvokeRejectionMetadata, tryExtractRouterLikePayloadFromErrorChain, tryExtractOpenRouterMetadataFromErrorChain, tryExtractFallbackAttemptsFromErrorChain, pickRequestIdsFromRouterLike, resolveActivityCostCompletion, resolveCostCompletionForActivity, resolveCostCompletionWithAiTools, buildGatewayPricingRecord, mapAiCostResultToResolvedActivityCost, catalogPricingSucceeded, extractUsageExtrasFromRouterResponse, buildTraceUsageSummary, enrichTraceAttemptsWithBilling, pickEnhancedOpenRouterMetadata, hasNonZeroTokenUsage, MODEL_PROFILE_UNROUTABLE, ModelProfileUnroutableError, ModelProfileInputRejectedError, buildGatewayFallbackAttemptsFromTrace, formatFallbackExhaustionMessage, enrichGatewayFallbackExhaustedError, logResolvedModelRouting, mapGatewayFallbackAttemptsToRouter } from './gateway-utils.js';
|
|
23
20
|
export { getGatewayOperationalMode, isProdGatewayMode, parseModelProviderSpec } from './gateway-mode.js';
|
|
@@ -46,7 +43,6 @@ export type { GatewayLogCode as GatewayDiagnosticCode } from './gateway-log-diag
|
|
|
46
43
|
export { resolveInvokeModel, applyModelResolution, applyOpenRouterInvokeRouting, applyOpenRouterInvokePolicy, mapResolutionToRouterConfig, buildInvokeModelResolverOptions, buildModelResolverOptions, enrichModelResolutionError, getAiToolsClient, resetAiToolsClientForTests, resolveOpenRouterApiKey, resolvePreferOpenRouter, readPreferOpenRouterFromEnv, getAiToolsInvokeClient, createAiToolsInvokeClient, } from './ai-tools-client.js';
|
|
47
44
|
export type { AiToolsClientBundle, AiToolsInvokeClient, InvokeModelResolutionDiagnostics, InvokeModelResolutionInput, InvokeModelResolutionOptions, InvokeModelResolutionResult, InvokeRouterConfigSlice, } from './ai-tools-client.js';
|
|
48
45
|
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';
|
|
49
|
-
export { ROUTER_LOG_ENV_PREFIX } from '@x12i/ai-providers-router';
|
|
50
46
|
export type { Logxer, LogMeta, RuntimeIdentity, LogRuntimeContext, GetJobLogsInput, GetJobLogsResult, QueryableLogLine, LogDiagnostics, DiagnosticEvidence, ScopeCriteria, ScopeLogsResult, StackLoggingOptions, PackageLogLevelsConfig, PackageLogLevelSetting } from '@x12i/logxer';
|
|
51
47
|
export { runtimeObjects } from './runtime-objects.js';
|
|
52
48
|
export type { ActivixQueryableClient, LogxerQueryableClient, PackageRuntimeObjects, RuntimeObjects } from './runtime-objects.js';
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { createOpenRouterRuntimeProvider } from './create-openrouter-runtime-provider.js';
|
|
3
|
-
export { createOpenRouterRuntimeAdapter } from './create-openrouter-runtime-adapter.js';
|
|
1
|
+
export { shouldUseOpenRouterRuntime, resolveOpenRouterRuntimeDefaults } from './should-use-openrouter-runtime.js';
|
|
4
2
|
export { mapGatewayServerTools } from './map-server-tools.js';
|
|
5
3
|
export { mapGatewayRequestToRuntimeRequest } from './map-gateway-request.js';
|
|
6
4
|
export { buildPartialMetadataFromRuntimeResponse, buildRouterLikeEnvelopeFromRuntimeResponse, pickOpenRouterRuntimeMetadataSlice, extractOpenRouterRuntimeRouterMetadata, parseRuntimeResponseToAIResponse, } from './map-runtime-response.js';
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { createOpenRouterRuntimeProvider } from './create-openrouter-runtime-provider.js';
|
|
3
|
-
export { createOpenRouterRuntimeAdapter } from './create-openrouter-runtime-adapter.js';
|
|
1
|
+
export { shouldUseOpenRouterRuntime, resolveOpenRouterRuntimeDefaults } from './should-use-openrouter-runtime.js';
|
|
4
2
|
export { mapGatewayServerTools } from './map-server-tools.js';
|
|
5
3
|
export { mapGatewayRequestToRuntimeRequest } from './map-gateway-request.js';
|
|
6
4
|
export { buildPartialMetadataFromRuntimeResponse, buildRouterLikeEnvelopeFromRuntimeResponse, pickOpenRouterRuntimeMetadataSlice, extractOpenRouterRuntimeRouterMetadata, parseRuntimeResponseToAIResponse, } from './map-runtime-response.js';
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { RuntimeRequest } from '@x12i/
|
|
2
|
-
export declare const OPENROUTER_RUNTIME_OPERATION = "openrouter-runtime.run";
|
|
1
|
+
import type { RuntimeRequest } from '@x12i/ai-router-runtime';
|
|
3
2
|
export type GatewayRouterInvokeRequest = {
|
|
4
3
|
messages?: Array<{
|
|
5
4
|
role: string;
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
-
import { OpenRouterHttpError, RuntimeConfigError, } from '@x12i/openrouter-runtime';
|
|
2
1
|
import { GatewayPolicyViolationError, GatewayValidationError, ProviderConfigError, ProviderInvokeError, } from '../instruction-errors.js';
|
|
3
2
|
import { buildPartialMetadataFromRuntimeResponse, buildRouterLikeEnvelopeFromRuntimeResponse, } from './map-runtime-response.js';
|
|
3
|
+
function isRuntimeConfigError(err) {
|
|
4
|
+
return (err instanceof Error &&
|
|
5
|
+
err.name === 'RuntimeConfigError' &&
|
|
6
|
+
typeof err.code === 'string');
|
|
7
|
+
}
|
|
8
|
+
function isOpenRouterHttpError(err) {
|
|
9
|
+
return (err instanceof Error &&
|
|
10
|
+
err.name === 'OpenRouterHttpError' &&
|
|
11
|
+
typeof err.status === 'number' &&
|
|
12
|
+
typeof err.code === 'string');
|
|
13
|
+
}
|
|
4
14
|
function attachRouterEnvelopeToError(err, response) {
|
|
5
15
|
const envelope = buildRouterLikeEnvelopeFromRuntimeResponse(response);
|
|
6
16
|
err.response = envelope;
|
|
@@ -52,7 +62,7 @@ export function throwMappedRuntimeResponseErrors(response) {
|
|
|
52
62
|
throw err;
|
|
53
63
|
}
|
|
54
64
|
export function mapRuntimeErrorToGatewayError(err) {
|
|
55
|
-
if (err
|
|
65
|
+
if (isRuntimeConfigError(err)) {
|
|
56
66
|
try {
|
|
57
67
|
throwMappedRuntimeConfigError(err);
|
|
58
68
|
}
|
|
@@ -60,7 +70,7 @@ export function mapRuntimeErrorToGatewayError(err) {
|
|
|
60
70
|
return mapped;
|
|
61
71
|
}
|
|
62
72
|
}
|
|
63
|
-
if (err
|
|
73
|
+
if (isOpenRouterHttpError(err)) {
|
|
64
74
|
try {
|
|
65
75
|
throwMappedOpenRouterHttpError(err);
|
|
66
76
|
}
|
|
@@ -1,5 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import type { RuntimeResponse } from '@x12i/ai-router-runtime';
|
|
2
|
+
type RuntimeConfigErrorLike = Error & {
|
|
3
|
+
code: string;
|
|
4
|
+
details?: unknown;
|
|
5
|
+
};
|
|
6
|
+
type OpenRouterHttpErrorLike = Error & {
|
|
7
|
+
status: number;
|
|
8
|
+
code: string;
|
|
9
|
+
retryable: boolean;
|
|
10
|
+
body?: unknown;
|
|
11
|
+
};
|
|
12
|
+
export declare function throwMappedRuntimeConfigError(err: RuntimeConfigErrorLike): never;
|
|
13
|
+
export declare function throwMappedOpenRouterHttpError(err: OpenRouterHttpErrorLike): never;
|
|
4
14
|
export declare function throwMappedRuntimeResponseErrors(response: RuntimeResponse): void;
|
|
5
15
|
export declare function mapRuntimeErrorToGatewayError(err: unknown): Error;
|
|
16
|
+
export {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { AIResponse } from '
|
|
2
|
-
import type { RuntimeResponse } from '@x12i/
|
|
1
|
+
import type { AIResponse } from '../router-types.js';
|
|
2
|
+
import type { RuntimeResponse } from '@x12i/ai-router-runtime';
|
|
3
3
|
import type { GatewayCitation, GatewayGeneratedImage, GatewayOpenRouterRuntimeMetadata, GatewayPatchProposal, GatewayServerToolUsageMap } from '../types.js';
|
|
4
4
|
export declare function mapRuntimeToolUsage(toolUsage: RuntimeResponse['toolUsage']): GatewayServerToolUsageMap;
|
|
5
5
|
export declare function mapRuntimeCitations(citations: RuntimeResponse['citations']): GatewayCitation[];
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { GatewayServerToolsConfig } from '../types.js';
|
|
2
|
-
import type { RuntimeServerToolsPolicy } from '@x12i/
|
|
2
|
+
import type { RuntimeServerToolsPolicy } from '@x12i/ai-router-runtime';
|
|
3
3
|
export declare function mapGatewayServerTools(config?: GatewayServerToolsConfig): RuntimeServerToolsPolicy | undefined;
|
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Whether
|
|
3
|
-
*
|
|
4
|
-
* - AI_GATEWAY_USE_LEGACY_OPENROUTER=1 → legacy router OpenRouter
|
|
5
|
-
* - AI_GATEWAY_USE_OPENROUTER_RUNTIME=0 → legacy
|
|
6
|
-
* - GatewayConfig.openrouterRuntime.enabled=false → legacy
|
|
7
|
-
* - default → runtime enabled
|
|
2
|
+
* Whether OpenRouter runtime path is enabled (always true with @x12i/ai-router-runtime).
|
|
8
3
|
*/
|
|
9
4
|
export function shouldUseOpenRouterRuntime(config) {
|
|
10
5
|
const legacyEnv = process.env.AI_GATEWAY_USE_LEGACY_OPENROUTER;
|
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import type { GatewayConfig } from '../types.js';
|
|
2
2
|
/**
|
|
3
|
-
* Whether
|
|
4
|
-
*
|
|
5
|
-
* - AI_GATEWAY_USE_LEGACY_OPENROUTER=1 → legacy router OpenRouter
|
|
6
|
-
* - AI_GATEWAY_USE_OPENROUTER_RUNTIME=0 → legacy
|
|
7
|
-
* - GatewayConfig.openrouterRuntime.enabled=false → legacy
|
|
8
|
-
* - default → runtime enabled
|
|
3
|
+
* Whether OpenRouter runtime path is enabled (always true with @x12i/ai-router-runtime).
|
|
9
4
|
*/
|
|
10
5
|
export declare function shouldUseOpenRouterRuntime(config?: GatewayConfig): boolean;
|
|
11
6
|
export declare function resolveOpenRouterRuntimeDefaults(config?: GatewayConfig): {
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Router error types and enrichment helpers for gateway consumers.
|
|
3
|
+
*/
|
|
4
|
+
export const PROVIDER_FAILURE_CODES = [
|
|
5
|
+
'OPENROUTER_API_KEY_MISSING',
|
|
6
|
+
'PROVIDER_AUTH_FAILED',
|
|
7
|
+
'PROVIDER_RATE_LIMITED',
|
|
8
|
+
'PROVIDER_QUOTA_EXCEEDED',
|
|
9
|
+
'PROVIDER_MODEL_NOT_FOUND',
|
|
10
|
+
'PROVIDER_GATEWAY_UNREACHABLE',
|
|
11
|
+
'PROVIDER_NOT_REGISTERED',
|
|
12
|
+
'PROVIDER_REQUEST_FAILED',
|
|
13
|
+
'PROVIDER_INVOKE_FAILED',
|
|
14
|
+
];
|
|
15
|
+
const OPERATOR_HINTS = {
|
|
16
|
+
OPENROUTER_API_KEY_MISSING: 'Set OPENROUTER_API_KEY or OPEN_ROUTER_KEY, or pass apiKey when creating the runtime.',
|
|
17
|
+
PROVIDER_AUTH_FAILED: 'Check that the OpenRouter API key is present, valid, and authorized for this route.',
|
|
18
|
+
PROVIDER_RATE_LIMITED: 'Retry after the provider rate limit resets or reduce request concurrency.',
|
|
19
|
+
PROVIDER_QUOTA_EXCEEDED: 'Check provider billing, credits, quota, and project limits.',
|
|
20
|
+
PROVIDER_MODEL_NOT_FOUND: 'Verify the model id and whether it is available through OpenRouter for this account.',
|
|
21
|
+
PROVIDER_GATEWAY_UNREACHABLE: 'Retry later; the provider gateway was unreachable or timed out.',
|
|
22
|
+
PROVIDER_NOT_REGISTERED: 'Provider module is not registered; install and configure the provider package.',
|
|
23
|
+
PROVIDER_REQUEST_FAILED: 'Provider call failed; inspect providerAttempts in error.details for per-attempt diagnostics.',
|
|
24
|
+
PROVIDER_INVOKE_FAILED: 'Provider call failed; inspect providerAttempts in error.details for per-attempt diagnostics.',
|
|
25
|
+
};
|
|
26
|
+
export function operatorHintForProviderFailureCode(code) {
|
|
27
|
+
return OPERATOR_HINTS[code];
|
|
28
|
+
}
|
|
29
|
+
export class ProviderNotFoundError extends Error {
|
|
30
|
+
constructor(providerName) {
|
|
31
|
+
super(`Provider not found: ${providerName}`);
|
|
32
|
+
this.name = 'ProviderNotFoundError';
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export class OpenRouterApiKeyMissingError extends Error {
|
|
36
|
+
code = 'OPENROUTER_API_KEY_MISSING';
|
|
37
|
+
constructor() {
|
|
38
|
+
super('OPENROUTER_API_KEY is required for OpenRouter routing');
|
|
39
|
+
this.name = 'OpenRouterApiKeyMissingError';
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class FallbackExhaustedError extends Error {
|
|
43
|
+
attempts;
|
|
44
|
+
code;
|
|
45
|
+
operatorHint;
|
|
46
|
+
details;
|
|
47
|
+
constructor(attempts) {
|
|
48
|
+
super('All providers in the fallback chain failed');
|
|
49
|
+
this.name = 'FallbackExhaustedError';
|
|
50
|
+
this.attempts = attempts;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function classifyProviderFailure(err) {
|
|
54
|
+
const message = err instanceof Error ? err.message : typeof err === 'string' ? err : String(err ?? '');
|
|
55
|
+
const lower = message.toLowerCase();
|
|
56
|
+
if (lower.includes('openrouter_api_key') || lower.includes('api key missing')) {
|
|
57
|
+
return 'OPENROUTER_API_KEY_MISSING';
|
|
58
|
+
}
|
|
59
|
+
if (lower.includes('rate limit') || lower.includes('429'))
|
|
60
|
+
return 'PROVIDER_RATE_LIMITED';
|
|
61
|
+
if (lower.includes('quota') || lower.includes('insufficient'))
|
|
62
|
+
return 'PROVIDER_QUOTA_EXCEEDED';
|
|
63
|
+
if (lower.includes('model not found') || lower.includes('404'))
|
|
64
|
+
return 'PROVIDER_MODEL_NOT_FOUND';
|
|
65
|
+
if (lower.includes('unauthorized') || lower.includes('401') || lower.includes('403')) {
|
|
66
|
+
return 'PROVIDER_AUTH_FAILED';
|
|
67
|
+
}
|
|
68
|
+
if (lower.includes('fetch failed') ||
|
|
69
|
+
lower.includes('econnrefused') ||
|
|
70
|
+
lower.includes('etimedout') ||
|
|
71
|
+
lower.includes('network')) {
|
|
72
|
+
return 'PROVIDER_GATEWAY_UNREACHABLE';
|
|
73
|
+
}
|
|
74
|
+
return 'PROVIDER_REQUEST_FAILED';
|
|
75
|
+
}
|
|
76
|
+
export function buildProviderAttemptDetail(input) {
|
|
77
|
+
return {
|
|
78
|
+
provider: input.provider,
|
|
79
|
+
...(input.modelId !== undefined ? { modelId: input.modelId } : {}),
|
|
80
|
+
...(input.httpStatus !== undefined ? { httpStatus: input.httpStatus } : {}),
|
|
81
|
+
message: input.message,
|
|
82
|
+
code: input.code ?? classifyProviderFailure(input.err ?? input.message),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
export function providerAttemptsFromFallbackAttempts(attempts) {
|
|
86
|
+
return attempts.map((a) => buildProviderAttemptDetail({
|
|
87
|
+
provider: String(a.provider),
|
|
88
|
+
modelId: a.model,
|
|
89
|
+
httpStatus: a.httpStatus,
|
|
90
|
+
message: a.message ?? a.error.message,
|
|
91
|
+
code: a.code,
|
|
92
|
+
err: a.error,
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
function resolveExhaustedFailureCode(providerAttempts) {
|
|
96
|
+
if (providerAttempts.length === 0)
|
|
97
|
+
return 'PROVIDER_REQUEST_FAILED';
|
|
98
|
+
return providerAttempts[providerAttempts.length - 1].code;
|
|
99
|
+
}
|
|
100
|
+
export function enrichRunTaskError(err, input) {
|
|
101
|
+
const existingCode = err.code;
|
|
102
|
+
const providerAttempts = input?.providerAttempts ??
|
|
103
|
+
(err instanceof FallbackExhaustedError ? providerAttemptsFromFallbackAttempts(err.attempts) : []);
|
|
104
|
+
const code = input?.code ??
|
|
105
|
+
existingCode ??
|
|
106
|
+
(providerAttempts.length > 0 ? resolveExhaustedFailureCode(providerAttempts) : undefined) ??
|
|
107
|
+
classifyProviderFailure(err);
|
|
108
|
+
const enriched = err;
|
|
109
|
+
enriched.code = code;
|
|
110
|
+
enriched.operatorHint = operatorHintForProviderFailureCode(code);
|
|
111
|
+
if (providerAttempts.length > 0) {
|
|
112
|
+
enriched.details = { providerAttempts };
|
|
113
|
+
}
|
|
114
|
+
return enriched;
|
|
115
|
+
}
|