@wengine-ai/llms 2.3.231 → 2.3.234
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 +10 -5
- package/dist/api/middleware.d.ts +9 -0
- package/dist/api/routes.d.ts +24 -0
- package/dist/ccr/admin-routes.d.ts +6 -0
- package/dist/ccr/agents/image.agent.d.ts +10 -0
- package/dist/ccr/agents/index.d.ts +28 -0
- package/dist/ccr/agents/type.d.ts +13 -0
- package/dist/ccr/auth.d.ts +8 -0
- package/dist/ccr/claude-config-init.d.ts +1 -0
- package/dist/ccr/codex-accounts.d.ts +9 -0
- package/dist/ccr/codex-usage-cache.d.ts +2 -0
- package/dist/ccr/config.d.ts +6 -0
- package/dist/ccr/create-server.d.ts +6 -0
- package/dist/ccr/health-reconcile.d.ts +22 -0
- package/dist/ccr/models.d.ts +26 -0
- package/dist/ccr/pino-retention.d.ts +1 -0
- package/dist/ccr/plugin-registration.d.ts +6 -0
- package/dist/ccr/request-pipeline.d.ts +37 -0
- package/dist/ccr/usage-merge.d.ts +32 -0
- package/dist/ccr/usage-store.d.ts +109 -0
- package/dist/cjs/server.cjs +337 -350
- package/dist/cjs/server.cjs.map +4 -4
- package/dist/clients/adapters.d.ts +22 -0
- package/dist/esm/server.mjs +338 -350
- package/dist/esm/server.mjs.map +4 -4
- package/dist/index.html +227 -0
- package/dist/plugins/index.d.ts +4 -0
- package/dist/plugins/output/console-handler.d.ts +23 -0
- package/dist/plugins/output/index.d.ts +29 -0
- package/dist/plugins/output/output-manager.d.ts +108 -0
- package/dist/plugins/output/temp-file-handler.d.ts +48 -0
- package/dist/plugins/output/types.d.ts +157 -0
- package/dist/plugins/output/webhook-handler.d.ts +35 -0
- package/dist/plugins/plugin-manager.d.ts +62 -0
- package/dist/plugins/token-speed.d.ts +5 -0
- package/dist/plugins/types.d.ts +25 -0
- package/dist/server.d.ts +110 -0
- package/dist/services/active-probe.d.ts +96 -0
- package/dist/services/config.d.ts +30 -0
- package/dist/services/provider-health.d.ts +135 -0
- package/dist/services/provider.d.ts +32 -0
- package/dist/services/proxy.d.ts +58 -0
- package/dist/services/quota-adapters.d.ts +16 -0
- package/dist/services/quota-store.d.ts +12 -0
- package/dist/services/rate-limit.d.ts +26 -0
- package/dist/services/tokenizer.d.ts +45 -0
- package/dist/services/transformer.d.ts +28 -0
- package/dist/tiktoken_bg.wasm +0 -0
- package/dist/tokenizer/api-tokenizer.d.ts +46 -0
- package/dist/tokenizer/huggingface-tokenizer.d.ts +52 -0
- package/dist/tokenizer/tiktoken-tokenizer.d.ts +20 -0
- package/dist/transformer/anthropic.transformer.d.ts +18 -0
- package/dist/transformer/cerebras.transformer.d.ts +16 -0
- package/dist/transformer/cleancache.transformer.d.ts +6 -0
- package/dist/transformer/customparams.transformer.d.ts +30 -0
- package/dist/transformer/deepseek.transformer.d.ts +7 -0
- package/dist/transformer/enhancetool.transformer.d.ts +5 -0
- package/dist/transformer/forcereasoning.transformer.d.ts +7 -0
- package/dist/transformer/gemini.transformer.d.ts +10 -0
- package/dist/transformer/groq.transformer.d.ts +7 -0
- package/dist/transformer/index.d.ts +49 -0
- package/dist/transformer/kimi.transformer.d.ts +17 -0
- package/dist/transformer/maxcompletiontokens.transformer.d.ts +6 -0
- package/dist/transformer/maxtoken.transformer.d.ts +9 -0
- package/dist/transformer/openai.responses.transformer.d.ts +28 -0
- package/dist/transformer/openai.transformer.d.ts +5 -0
- package/dist/transformer/opencode.transformer.d.ts +29 -0
- package/dist/transformer/openrouter.transformer.d.ts +9 -0
- package/dist/transformer/reasoning.transformer.d.ts +10 -0
- package/dist/transformer/sampling.transformer.d.ts +13 -0
- package/dist/transformer/streamoptions.transformer.d.ts +6 -0
- package/dist/transformer/tooluse.transformer.d.ts +7 -0
- package/dist/transformer/vercel.transformer.d.ts +10 -0
- package/dist/transformer/vertex-claude.transformer.d.ts +8 -0
- package/dist/transformer/vertex-gemini.transformer.d.ts +8 -0
- package/dist/types/llm.d.ts +233 -0
- package/dist/types/transformer.d.ts +23 -0
- package/dist/utils/cache.d.ts +17 -0
- package/dist/utils/converter.d.ts +12 -0
- package/dist/utils/debug-log.d.ts +24 -0
- package/dist/utils/fallback-promotion.d.ts +65 -0
- package/dist/utils/gemini.util.d.ts +11 -0
- package/dist/utils/image.d.ts +1 -0
- package/dist/utils/request.d.ts +2 -0
- package/dist/utils/router.d.ts +84 -0
- package/dist/utils/session-id.d.ts +2 -0
- package/dist/utils/sse/SSEParser.transform.d.ts +6 -0
- package/dist/utils/sse/SSESerializer.transform.d.ts +3 -0
- package/dist/utils/sse/index.d.ts +3 -0
- package/dist/utils/sse/rewriteStream.d.ts +9 -0
- package/dist/utils/thinking.d.ts +2 -0
- package/dist/utils/toolArgumentsParser.d.ts +8 -0
- package/dist/utils/vertex-claude.util.d.ts +42 -0
- package/package.json +13 -6
- package/vitest.config.ts +1 -0
- package/dist/index.d.ts +0 -175
- package/dist/plugins.d.ts +0 -42
- package/dist/provider-health.d.ts +0 -39
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Active health/quota probing service for providers
|
|
3
|
+
* Runs periodic probes to refresh health and quota info without waiting for real traffic
|
|
4
|
+
*/
|
|
5
|
+
import type { LLMProvider } from '../types/llm';
|
|
6
|
+
/**
|
|
7
|
+
* Configuration for active probing behavior
|
|
8
|
+
*/
|
|
9
|
+
export interface ActiveProbeConfig {
|
|
10
|
+
/** Enable active probing (default: true) */
|
|
11
|
+
enabled?: boolean;
|
|
12
|
+
/** Interval in minutes for periodic quota probes (default: 10) */
|
|
13
|
+
quotaProbeIntervalMinutes?: number;
|
|
14
|
+
/** Timeout in milliseconds for probe requests (default: 15000) */
|
|
15
|
+
probeTimeoutMs?: number;
|
|
16
|
+
/** Initial delay before starting first probe (default: 5000) */
|
|
17
|
+
initialDelayMs?: number;
|
|
18
|
+
/** Providers to exclude from active probing (e.g., providers without models endpoint) */
|
|
19
|
+
excludeProviders?: string[];
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Active probe scheduler
|
|
23
|
+
*/
|
|
24
|
+
export declare class ActiveProbeService {
|
|
25
|
+
private config;
|
|
26
|
+
private quotaProbeTimer?;
|
|
27
|
+
private healthProbeTimer?;
|
|
28
|
+
private wakeupTimer?;
|
|
29
|
+
private rateLimitProbeTimer?;
|
|
30
|
+
private lastWakeupDate;
|
|
31
|
+
private getProviders;
|
|
32
|
+
private resolveProxyUrl?;
|
|
33
|
+
private logger?;
|
|
34
|
+
private getConfig?;
|
|
35
|
+
private running;
|
|
36
|
+
constructor(getProviders: () => LLMProvider[], config?: ActiveProbeConfig, resolveProxyUrl?: (provider: LLMProvider) => string | undefined, logger?: any, getConfig?: (key: string) => any);
|
|
37
|
+
/**
|
|
38
|
+
* Start the active probe loop
|
|
39
|
+
*/
|
|
40
|
+
start(): void;
|
|
41
|
+
/**
|
|
42
|
+
* Stop all probe timers
|
|
43
|
+
*/
|
|
44
|
+
stop(): void;
|
|
45
|
+
/**
|
|
46
|
+
* Run quota probes for configured providers.
|
|
47
|
+
* Provider-specific adapters actively query official quota APIs, while the
|
|
48
|
+
* Kimi/Moonshot models probe continues to capture header-based limits.
|
|
49
|
+
* When quota is exhausted, marks provider as unhealthy in health store.
|
|
50
|
+
*/
|
|
51
|
+
private runQuotaProbe;
|
|
52
|
+
/**
|
|
53
|
+
* Run health probe for all providers
|
|
54
|
+
* Probes every configured provider to detect reachability issues early,
|
|
55
|
+
* not just providers already in the fail pool.
|
|
56
|
+
*/
|
|
57
|
+
private runHealthProbe;
|
|
58
|
+
/**
|
|
59
|
+
* Run independent probe for rate-limited models.
|
|
60
|
+
* Sends real chat requests (max_tokens:1) to test if the rate limit has cleared.
|
|
61
|
+
* Rate-limited models are NOT probed by runHealthProbe — they have a separate timer
|
|
62
|
+
* so they can be checked more frequently (every 2 minutes) without affecting normal probes.
|
|
63
|
+
*/
|
|
64
|
+
private runRateLimitProbe;
|
|
65
|
+
/**
|
|
66
|
+
* Run scheduled wake-up for configured providers
|
|
67
|
+
*/
|
|
68
|
+
private runScheduledWakeup;
|
|
69
|
+
private isWakeupGloballyEnabled;
|
|
70
|
+
/**
|
|
71
|
+
* Check if the wake-up has already fired for this provider today.
|
|
72
|
+
* Returns true if already fired, so the caller can skip.
|
|
73
|
+
* If not yet fired, atomically marks it as fired.
|
|
74
|
+
*/
|
|
75
|
+
private hasWakeupFiredToday;
|
|
76
|
+
/**
|
|
77
|
+
* Trigger a manual probe for a specific provider
|
|
78
|
+
*/
|
|
79
|
+
probeProviderManually(providerName: string): Promise<boolean>;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Initialize and get the active probe service
|
|
83
|
+
*/
|
|
84
|
+
export declare function getActiveProbeService(getProviders: () => LLMProvider[], config?: ActiveProbeConfig, resolveProxyUrl?: (provider: LLMProvider) => string | undefined, logger?: any, getConfig?: (key: string) => any): ActiveProbeService;
|
|
85
|
+
/**
|
|
86
|
+
* Start the active probe service (convenience function)
|
|
87
|
+
*/
|
|
88
|
+
export declare function startActiveProbe(getProviders: () => LLMProvider[], config?: ActiveProbeConfig, resolveProxyUrl?: (provider: LLMProvider) => string | undefined, logger?: any, getConfig?: (key: string) => any): ActiveProbeService;
|
|
89
|
+
/**
|
|
90
|
+
* Stop the active probe service
|
|
91
|
+
*/
|
|
92
|
+
export declare function stopActiveProbe(): void;
|
|
93
|
+
/**
|
|
94
|
+
* Reset the active probe service (for testing)
|
|
95
|
+
*/
|
|
96
|
+
export declare function resetActiveProbeService(): void;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export interface ConfigOptions {
|
|
2
|
+
envPath?: string;
|
|
3
|
+
jsonPath?: string;
|
|
4
|
+
useEnvFile?: boolean;
|
|
5
|
+
useJsonFile?: boolean;
|
|
6
|
+
useEnvironmentVariables?: boolean;
|
|
7
|
+
initialConfig?: AppConfig;
|
|
8
|
+
}
|
|
9
|
+
export interface AppConfig {
|
|
10
|
+
[key: string]: any;
|
|
11
|
+
}
|
|
12
|
+
export declare class ConfigService {
|
|
13
|
+
private config;
|
|
14
|
+
private options;
|
|
15
|
+
constructor(options?: ConfigOptions);
|
|
16
|
+
private loadConfig;
|
|
17
|
+
private loadJsonConfig;
|
|
18
|
+
private loadEnvConfig;
|
|
19
|
+
private loadEnvironmentVariables;
|
|
20
|
+
private parseEnvConfig;
|
|
21
|
+
private isAbsolutePath;
|
|
22
|
+
get<T = any>(key: keyof AppConfig): T | undefined;
|
|
23
|
+
get<T = any>(key: keyof AppConfig, defaultValue: T): T;
|
|
24
|
+
getAll(): AppConfig;
|
|
25
|
+
getHttpsProxy(): string | undefined;
|
|
26
|
+
has(key: keyof AppConfig): boolean;
|
|
27
|
+
set(key: keyof AppConfig, value: any): void;
|
|
28
|
+
reload(): void;
|
|
29
|
+
getConfigSummary(): string;
|
|
30
|
+
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider health state for circuit breaker pattern
|
|
3
|
+
*/
|
|
4
|
+
export interface ProviderHealthState {
|
|
5
|
+
provider: string;
|
|
6
|
+
model: string;
|
|
7
|
+
status: 'closed' | 'open' | 'half-open';
|
|
8
|
+
failureCount: number;
|
|
9
|
+
successCount: number;
|
|
10
|
+
lastFailureTime: number;
|
|
11
|
+
lastProbeTime: number;
|
|
12
|
+
lastError?: string;
|
|
13
|
+
/** Epoch ms before which a rate-limited model should NOT be probed or used.
|
|
14
|
+
* Set by markRateLimited(); cleared on expiry (auto-recover) or on explicit recovery. */
|
|
15
|
+
rateLimitUntil?: number;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Configuration for health pool behavior
|
|
19
|
+
*/
|
|
20
|
+
export interface HealthPoolConfig {
|
|
21
|
+
enabled?: boolean;
|
|
22
|
+
failureThreshold?: number;
|
|
23
|
+
probeIntervalMinutes?: number;
|
|
24
|
+
halfOpenSuccessThreshold?: number;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Provider health store implementing circuit breaker pattern
|
|
28
|
+
* Tracks provider/model health and manages state transitions
|
|
29
|
+
*/
|
|
30
|
+
export declare class ProviderHealthStore {
|
|
31
|
+
private states;
|
|
32
|
+
private config;
|
|
33
|
+
private probeTimer?;
|
|
34
|
+
constructor(config?: HealthPoolConfig);
|
|
35
|
+
/**
|
|
36
|
+
* Build the composite key used to index provider/model state.
|
|
37
|
+
* Returns null when either part is empty, which signals the caller to
|
|
38
|
+
* short-circuit (skip the operation) instead of creating a bogus entry like
|
|
39
|
+
* ",model" or "provider," that would pollute the health pool.
|
|
40
|
+
*/
|
|
41
|
+
private getKey;
|
|
42
|
+
/**
|
|
43
|
+
* Record a successful request
|
|
44
|
+
* Transitions: open -> half-open (if probe), half-open -> closed (if threshold reached)
|
|
45
|
+
*/
|
|
46
|
+
recordSuccess(provider: string, model: string): void;
|
|
47
|
+
/**
|
|
48
|
+
* Clear fallback promotions when a primary model recovers to healthy state
|
|
49
|
+
* Uses dynamic import to avoid circular dependency
|
|
50
|
+
*/
|
|
51
|
+
private clearPromotionsForPrimary;
|
|
52
|
+
/**
|
|
53
|
+
* Record a failed request
|
|
54
|
+
* Transitions: closed -> open (if threshold), half-open -> open (immediate)
|
|
55
|
+
*/
|
|
56
|
+
recordFailure(provider: string, model: string, error?: string): void;
|
|
57
|
+
/**
|
|
58
|
+
* Get current health state for a provider/model
|
|
59
|
+
*/
|
|
60
|
+
getState(provider: string, model: string): ProviderHealthState | undefined;
|
|
61
|
+
/**
|
|
62
|
+
* Check if a provider/model is available for routing
|
|
63
|
+
* closed = available, half-open = available (lower priority), open = unavailable
|
|
64
|
+
*/
|
|
65
|
+
isAvailable(provider: string, model: string): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Get health status priority for sorting
|
|
68
|
+
* Returns: 0 = closed (best), 1 = half-open (ok), 2 = open (worst)
|
|
69
|
+
*/
|
|
70
|
+
getPriority(provider: string, model: string): number;
|
|
71
|
+
/**
|
|
72
|
+
* Get all models that are healthy (closed status)
|
|
73
|
+
*/
|
|
74
|
+
getHealthyModels(): string[];
|
|
75
|
+
/**
|
|
76
|
+
* Get all models in fail pool (open status)
|
|
77
|
+
*/
|
|
78
|
+
getFailPoolModels(): string[];
|
|
79
|
+
/**
|
|
80
|
+
* Get all models in half-open state
|
|
81
|
+
*/
|
|
82
|
+
getHalfOpenModels(): string[];
|
|
83
|
+
/**
|
|
84
|
+
* Immediately mark a provider/model as open (unavailable)
|
|
85
|
+
* Used when fallback succeeds to prevent further requests to a failing primary
|
|
86
|
+
*/
|
|
87
|
+
forceOpen(provider: string, model: string, error?: string): void;
|
|
88
|
+
/**
|
|
89
|
+
* Immediately mark a provider/model as rate-limited (unavailable).
|
|
90
|
+
* Sets a rateLimitUntil timestamp after which the model auto-recovers.
|
|
91
|
+
* Does NOT increment failureCount so the generic circuit-breaker threshold
|
|
92
|
+
* (used for network / unreachable failures) is preserved separately.
|
|
93
|
+
*/
|
|
94
|
+
markRateLimited(provider: string, model: string, retryAfterSeconds?: number, error?: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* Get all models currently in rate-limited state.
|
|
97
|
+
* Returns keys in "provider,model" format.
|
|
98
|
+
*/
|
|
99
|
+
getRateLimitedModels(): string[];
|
|
100
|
+
/**
|
|
101
|
+
* Check if probe is needed for an open state
|
|
102
|
+
* Returns true if probeInterval has elapsed since last probe
|
|
103
|
+
*/
|
|
104
|
+
needsProbe(state: ProviderHealthState): boolean;
|
|
105
|
+
/**
|
|
106
|
+
* Mark that a probe attempt has been made
|
|
107
|
+
*/
|
|
108
|
+
markProbeAttempt(provider: string, model: string): void;
|
|
109
|
+
/**
|
|
110
|
+
* Clear all health state (for testing or manual reset)
|
|
111
|
+
*/
|
|
112
|
+
clear(): void;
|
|
113
|
+
/**
|
|
114
|
+
* Recover a provider/model from health fail pool
|
|
115
|
+
*/
|
|
116
|
+
recover(provider: string, model: string): void;
|
|
117
|
+
/**
|
|
118
|
+
* Get all states for debugging/monitoring
|
|
119
|
+
*/
|
|
120
|
+
getAllStates(): ProviderHealthState[];
|
|
121
|
+
/**
|
|
122
|
+
* Restore a previously persisted health state.
|
|
123
|
+
*/
|
|
124
|
+
restore(state: ProviderHealthState): void;
|
|
125
|
+
/**
|
|
126
|
+
* Stop probe timer
|
|
127
|
+
*/
|
|
128
|
+
stopProbeTimer(): void;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Initialize persistence: load from disk and set up periodic save.
|
|
132
|
+
*/
|
|
133
|
+
export declare function initProviderHealthPersistence(): void;
|
|
134
|
+
export declare function getHealthStore(config?: HealthPoolConfig): ProviderHealthStore;
|
|
135
|
+
export declare function resetHealthStore(): void;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { LLMProvider, RegisterProviderRequest, ModelRoute, RequestRouteInfo } from "../types/llm";
|
|
2
|
+
import { ConfigService } from "./config";
|
|
3
|
+
import { TransformerService } from "./transformer";
|
|
4
|
+
export declare class ProviderService {
|
|
5
|
+
private readonly configService;
|
|
6
|
+
private readonly transformerService;
|
|
7
|
+
private readonly logger;
|
|
8
|
+
private providers;
|
|
9
|
+
private modelRoutes;
|
|
10
|
+
constructor(configService: ConfigService, transformerService: TransformerService, logger: any);
|
|
11
|
+
private initializeCustomProviders;
|
|
12
|
+
private initializeFromProvidersArray;
|
|
13
|
+
registerProvider(request: RegisterProviderRequest): LLMProvider;
|
|
14
|
+
getProviders(): LLMProvider[];
|
|
15
|
+
getProvider(name: string): LLMProvider | undefined;
|
|
16
|
+
updateProvider(id: string, updates: Partial<LLMProvider>): LLMProvider | null;
|
|
17
|
+
deleteProvider(id: string): boolean;
|
|
18
|
+
toggleProvider(name: string, enabled: boolean): boolean;
|
|
19
|
+
resolveModelRoute(modelName: string): RequestRouteInfo | null;
|
|
20
|
+
getAvailableModelNames(): string[];
|
|
21
|
+
getModelRoutes(): ModelRoute[];
|
|
22
|
+
private parseTransformerConfig;
|
|
23
|
+
getAvailableModels(): Promise<{
|
|
24
|
+
object: string;
|
|
25
|
+
data: Array<{
|
|
26
|
+
id: string;
|
|
27
|
+
object: string;
|
|
28
|
+
owned_by: string;
|
|
29
|
+
provider: string;
|
|
30
|
+
}>;
|
|
31
|
+
}>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { ProxyAgent } from "undici";
|
|
2
|
+
import type { ConfigService } from "./config";
|
|
3
|
+
export interface ProxyAwareProvider {
|
|
4
|
+
proxyEnabled?: boolean;
|
|
5
|
+
proxy_enabled?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare const PROXY_URL_KEYS: readonly ["HTTPS_PROXY", "https_proxy", "httpsProxy", "PROXY_URL"];
|
|
8
|
+
export type ProxyUrlValidation = {
|
|
9
|
+
ok: true;
|
|
10
|
+
} | {
|
|
11
|
+
ok: false;
|
|
12
|
+
error: string;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Validate a proxy URL config value *before* it is persisted.
|
|
16
|
+
*
|
|
17
|
+
* Acceptance rules:
|
|
18
|
+
* 1. Empty / whitespace-only value is allowed (means "no proxy").
|
|
19
|
+
* 2. Values containing an env-var placeholder ($VAR / ${VAR}) are allowed —
|
|
20
|
+
* they are raw config that gets interpolated at runtime; the final URL is
|
|
21
|
+
* still subject to normalizeProxyUrl's http(s)-only check at request time.
|
|
22
|
+
* 3. Otherwise the value must parse as a URL with protocol http: or https:.
|
|
23
|
+
*
|
|
24
|
+
* This does NOT replace the runtime contract: getProxyDispatcher still throws
|
|
25
|
+
* TypeError for unsupported protocols when a request actually uses the proxy.
|
|
26
|
+
*/
|
|
27
|
+
export declare function validateProxyUrl(rawUrl: unknown): ProxyUrlValidation;
|
|
28
|
+
/**
|
|
29
|
+
* Scan a config object for any proxy-related keys (PROXY_URL plus the
|
|
30
|
+
* compatibility aliases) and return a list of human-readable validation
|
|
31
|
+
* errors. Returns an empty array when every value is valid.
|
|
32
|
+
*/
|
|
33
|
+
export declare function findInvalidProxyUrls(config: Record<string, unknown>): Array<{
|
|
34
|
+
key: string;
|
|
35
|
+
error: string;
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* Resolve the configured proxy URL using the compatibility key priority.
|
|
39
|
+
*/
|
|
40
|
+
export declare function getConfiguredProxyUrl(configService: Pick<ConfigService, "get">): string | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Interpret the global proxy policy. Missing values keep the legacy behavior
|
|
43
|
+
* where a configured proxy applies to every provider. Unknown values use
|
|
44
|
+
* normal JavaScript truthiness after strings are trimmed.
|
|
45
|
+
*/
|
|
46
|
+
export declare function isGlobalProxyEnabled(value: unknown): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Resolve whether a provider-specific request should use the configured proxy.
|
|
49
|
+
*/
|
|
50
|
+
export declare function resolveProviderProxyUrl(configService: Pick<ConfigService, "get">, provider?: ProxyAwareProvider): string | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Return a cached dispatcher for a normalized HTTP(S) proxy URL.
|
|
53
|
+
*/
|
|
54
|
+
export declare function getProxyDispatcher(proxyUrl: string): ProxyAgent;
|
|
55
|
+
/**
|
|
56
|
+
* Close and clear all cached proxy dispatchers.
|
|
57
|
+
*/
|
|
58
|
+
export declare function closeProxyDispatchers(): Promise<void>;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { LLMProvider } from "../types/llm";
|
|
2
|
+
export interface ProviderQuotaResult {
|
|
3
|
+
totalBalance?: number;
|
|
4
|
+
usedBalance?: number;
|
|
5
|
+
remainingBalance?: number;
|
|
6
|
+
usedDailyBalance?: number;
|
|
7
|
+
/** Explicit limit for the short-window (5h) rate limit, if available */
|
|
8
|
+
limitDaily?: number;
|
|
9
|
+
currency?: string;
|
|
10
|
+
resetTime?: string;
|
|
11
|
+
resetTime7d?: string;
|
|
12
|
+
}
|
|
13
|
+
export interface QuotaAdapter {
|
|
14
|
+
queryQuota(provider: LLMProvider, timeoutMs: number, proxyUrl?: string): Promise<ProviderQuotaResult | null>;
|
|
15
|
+
}
|
|
16
|
+
export declare function getQuotaAdapter(baseUrl: string): QuotaAdapter | null;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ProviderQuotaResult } from "./quota-adapters";
|
|
2
|
+
export interface StoredQuotaResult extends ProviderQuotaResult {
|
|
3
|
+
provider: string;
|
|
4
|
+
capturedAt: number;
|
|
5
|
+
}
|
|
6
|
+
export declare function storeQuotaResult(providerName: string, result: ProviderQuotaResult): void;
|
|
7
|
+
export declare function getQuotaResult(providerName: string): StoredQuotaResult | undefined;
|
|
8
|
+
export declare function getAllQuotaResults(): StoredQuotaResult[];
|
|
9
|
+
/**
|
|
10
|
+
* Initialize persistence: load from disk and set up periodic save.
|
|
11
|
+
*/
|
|
12
|
+
export declare function initQuotaStorePersistence(): void;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rate limit info captured from upstream provider response headers
|
|
3
|
+
*/
|
|
4
|
+
export interface RateLimitInfo {
|
|
5
|
+
provider: string;
|
|
6
|
+
remaining: number | null;
|
|
7
|
+
limit: number | null;
|
|
8
|
+
reset: number | null;
|
|
9
|
+
capturedAt: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Capture rate limit headers from an upstream response.
|
|
13
|
+
*/
|
|
14
|
+
export declare function captureRateLimitHeaders(providerName: string, apiBaseUrl: string, headers: Headers | Record<string, string>): void;
|
|
15
|
+
/**
|
|
16
|
+
* Get cached rate limit info for a provider
|
|
17
|
+
*/
|
|
18
|
+
export declare function getRateLimitInfo(providerName: string): RateLimitInfo | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Get all cached rate limit info
|
|
21
|
+
*/
|
|
22
|
+
export declare function getAllRateLimitInfo(): RateLimitInfo[];
|
|
23
|
+
/**
|
|
24
|
+
* Initialize persistence: load from disk and set up periodic save.
|
|
25
|
+
*/
|
|
26
|
+
export declare function initRateLimitPersistence(): void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ConfigService } from "./config";
|
|
2
|
+
import { ITokenizer, TokenizeRequest, TokenizerConfig, TokenizerResult, TokenizerOptions } from "../types/tokenizer";
|
|
3
|
+
/**
|
|
4
|
+
* TokenizerService - Manages tokenization for different model types
|
|
5
|
+
*
|
|
6
|
+
* Supports three types of tokenizers:
|
|
7
|
+
* - tiktoken: Fast, OpenAI-compatible tokenizer (default)
|
|
8
|
+
* - huggingface: Local model-based tokenizer for open-source models
|
|
9
|
+
* - api: API-based tokenizer for closed-source models
|
|
10
|
+
*
|
|
11
|
+
* Features:
|
|
12
|
+
* - Automatic fallback to tiktoken on errors
|
|
13
|
+
* - Config-driven tokenizer selection
|
|
14
|
+
* - Per-provider and per-model configuration
|
|
15
|
+
*/
|
|
16
|
+
export declare class TokenizerService {
|
|
17
|
+
private tokenizers;
|
|
18
|
+
private configService;
|
|
19
|
+
private logger;
|
|
20
|
+
private options;
|
|
21
|
+
private fallbackTokenizer?;
|
|
22
|
+
constructor(configService: ConfigService, logger: any, options?: TokenizerOptions);
|
|
23
|
+
initialize(): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Get or create a tokenizer for specific configuration
|
|
26
|
+
*/
|
|
27
|
+
getTokenizer(config: TokenizerConfig, providerName?: string): Promise<ITokenizer>;
|
|
28
|
+
/**
|
|
29
|
+
* Count tokens for a request using the specified tokenizer configuration
|
|
30
|
+
*/
|
|
31
|
+
countTokens(request: TokenizeRequest, config?: TokenizerConfig, providerName?: string): Promise<TokenizerResult>;
|
|
32
|
+
/**
|
|
33
|
+
* Get tokenizer configuration for a specific model/provider
|
|
34
|
+
*/
|
|
35
|
+
getTokenizerConfigForModel(providerName: string, modelName: string): TokenizerConfig | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Dispose all tokenizers
|
|
38
|
+
*/
|
|
39
|
+
dispose(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Generate cache key from tokenizer config
|
|
42
|
+
*/
|
|
43
|
+
private getCacheKey;
|
|
44
|
+
private getRawProvider;
|
|
45
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { Transformer, TransformerConstructor } from "../types/transformer";
|
|
2
|
+
import { ConfigService } from "./config";
|
|
3
|
+
export declare class TransformerService {
|
|
4
|
+
private readonly configService;
|
|
5
|
+
private readonly logger;
|
|
6
|
+
private transformers;
|
|
7
|
+
constructor(configService: ConfigService, logger: any);
|
|
8
|
+
registerTransformer(name: string, transformer: Transformer): void;
|
|
9
|
+
getTransformer(name: string): Transformer | TransformerConstructor | undefined;
|
|
10
|
+
getAllTransformers(): Map<string, Transformer | TransformerConstructor>;
|
|
11
|
+
getTransformersWithEndpoint(): {
|
|
12
|
+
name: string;
|
|
13
|
+
transformer: Transformer;
|
|
14
|
+
}[];
|
|
15
|
+
getTransformersWithoutEndpoint(): {
|
|
16
|
+
name: string;
|
|
17
|
+
transformer: Transformer;
|
|
18
|
+
}[];
|
|
19
|
+
removeTransformer(name: string): boolean;
|
|
20
|
+
hasTransformer(name: string): boolean;
|
|
21
|
+
registerTransformerFromConfig(config: {
|
|
22
|
+
path?: string;
|
|
23
|
+
options?: any;
|
|
24
|
+
}): Promise<boolean>;
|
|
25
|
+
initialize(): Promise<void>;
|
|
26
|
+
private registerDefaultTransformersInternal;
|
|
27
|
+
private loadFromConfig;
|
|
28
|
+
}
|
|
Binary file
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { ITokenizer, TokenizeRequest, TokenizerConfig } from "../types/tokenizer";
|
|
2
|
+
/**
|
|
3
|
+
* Options for API tokenizer
|
|
4
|
+
*/
|
|
5
|
+
interface ApiTokenizerOptions {
|
|
6
|
+
timeout?: number;
|
|
7
|
+
resolveProxyUrl?: () => string | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* API-based tokenizer implementation
|
|
11
|
+
* Calls provider's tokenization API to get token counts
|
|
12
|
+
* Supports flexible configuration for different API formats
|
|
13
|
+
*/
|
|
14
|
+
export declare class ApiTokenizer implements ITokenizer {
|
|
15
|
+
readonly type = "api";
|
|
16
|
+
readonly name: string;
|
|
17
|
+
private config;
|
|
18
|
+
private logger;
|
|
19
|
+
private options;
|
|
20
|
+
constructor(config: TokenizerConfig, logger: any, options?: ApiTokenizerOptions);
|
|
21
|
+
initialize(): Promise<void>;
|
|
22
|
+
countTokens(request: TokenizeRequest): Promise<number>;
|
|
23
|
+
isInitialized(): boolean;
|
|
24
|
+
dispose(): void;
|
|
25
|
+
/**
|
|
26
|
+
* Format request body based on requestFormat
|
|
27
|
+
*/
|
|
28
|
+
private formatRequestBody;
|
|
29
|
+
/**
|
|
30
|
+
* Extract messages in OpenAI format
|
|
31
|
+
*/
|
|
32
|
+
private extractMessagesAsOpenAIFormat;
|
|
33
|
+
/**
|
|
34
|
+
* Extract text from a message
|
|
35
|
+
*/
|
|
36
|
+
private extractTextFromMessage;
|
|
37
|
+
/**
|
|
38
|
+
* Extract all text from request
|
|
39
|
+
*/
|
|
40
|
+
private extractConcatenatedText;
|
|
41
|
+
/**
|
|
42
|
+
* Extract token count from response using the configured field path
|
|
43
|
+
*/
|
|
44
|
+
private extractTokenCount;
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ITokenizer, TokenizeRequest } from "../types/tokenizer";
|
|
2
|
+
/**
|
|
3
|
+
* Options for HuggingFace tokenizer
|
|
4
|
+
*/
|
|
5
|
+
interface HFTokenizerOptions {
|
|
6
|
+
timeout?: number;
|
|
7
|
+
cacheDir?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* HuggingFace tokenizer implementation
|
|
11
|
+
* Uses @huggingface/tokenizers library for lightweight tokenization
|
|
12
|
+
*/
|
|
13
|
+
export declare class HuggingFaceTokenizer implements ITokenizer {
|
|
14
|
+
readonly type = "huggingface";
|
|
15
|
+
readonly name: string;
|
|
16
|
+
private readonly modelId;
|
|
17
|
+
private readonly logger;
|
|
18
|
+
private readonly options;
|
|
19
|
+
private tokenizer;
|
|
20
|
+
private readonly cacheDir;
|
|
21
|
+
private readonly safeModelName;
|
|
22
|
+
constructor(modelId: string, logger: any, options?: HFTokenizerOptions);
|
|
23
|
+
/**
|
|
24
|
+
* Get cache file paths for tokenizer files
|
|
25
|
+
*/
|
|
26
|
+
private getCachePaths;
|
|
27
|
+
/**
|
|
28
|
+
* Ensure directory exists
|
|
29
|
+
*/
|
|
30
|
+
private ensureDir;
|
|
31
|
+
/**
|
|
32
|
+
* Load tokenizer files from local cache
|
|
33
|
+
*/
|
|
34
|
+
private loadFromCache;
|
|
35
|
+
/**
|
|
36
|
+
* Download tokenizer files from Hugging Face Hub and save to cache
|
|
37
|
+
*/
|
|
38
|
+
private downloadAndCache;
|
|
39
|
+
initialize(): Promise<void>;
|
|
40
|
+
countTokens(request: TokenizeRequest): Promise<number>;
|
|
41
|
+
isInitialized(): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Encode text to tokens (for simple text tokenization)
|
|
44
|
+
*/
|
|
45
|
+
encodeText(text: string): number[];
|
|
46
|
+
dispose(): void;
|
|
47
|
+
/**
|
|
48
|
+
* Extract text from tokenize request
|
|
49
|
+
*/
|
|
50
|
+
private extractTextFromRequest;
|
|
51
|
+
}
|
|
52
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { TiktokenEncoding } from "tiktoken";
|
|
2
|
+
import type { ITokenizer, TokenizeRequest } from "../types/tokenizer";
|
|
3
|
+
/**
|
|
4
|
+
* Tiktoken-based tokenizer implementation
|
|
5
|
+
* Uses tiktoken library for fast token counting (OpenAI compatible)
|
|
6
|
+
*/
|
|
7
|
+
export declare class TiktokenTokenizer implements ITokenizer {
|
|
8
|
+
readonly type = "tiktoken";
|
|
9
|
+
readonly name: string;
|
|
10
|
+
private encoding?;
|
|
11
|
+
constructor(encodingName?: TiktokenEncoding);
|
|
12
|
+
initialize(): Promise<void>;
|
|
13
|
+
countTokens(request: TokenizeRequest): Promise<number>;
|
|
14
|
+
isInitialized(): boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Encode text to tokens (for simple text tokenization)
|
|
17
|
+
*/
|
|
18
|
+
encodeText(text: string): number[];
|
|
19
|
+
dispose(): void;
|
|
20
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { LLMProvider, UnifiedChatRequest } from "../types/llm";
|
|
2
|
+
import { Transformer, TransformerContext, TransformerOptions } from "../types/transformer";
|
|
3
|
+
export declare class AnthropicTransformer implements Transformer {
|
|
4
|
+
private readonly options?;
|
|
5
|
+
name: string;
|
|
6
|
+
endPoint: string;
|
|
7
|
+
private useBearer;
|
|
8
|
+
logger?: any;
|
|
9
|
+
constructor(options?: TransformerOptions | undefined);
|
|
10
|
+
auth(request: any, provider: LLMProvider): Promise<any>;
|
|
11
|
+
transformRequestOut(request: Record<string, any>): Promise<UnifiedChatRequest>;
|
|
12
|
+
transformRequestIn(request: UnifiedChatRequest, provider: LLMProvider, context: TransformerContext): Promise<any>;
|
|
13
|
+
private isAnthropicMessagesEndpoint;
|
|
14
|
+
transformResponseIn(response: Response, context?: TransformerContext): Promise<Response>;
|
|
15
|
+
private convertAnthropicToolsToUnified;
|
|
16
|
+
private convertOpenAIStreamToAnthropic;
|
|
17
|
+
private convertOpenAIResponseToAnthropic;
|
|
18
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { LLMProvider, UnifiedChatRequest } from "../types/llm";
|
|
2
|
+
import { Transformer } from "../types/transformer";
|
|
3
|
+
/**
|
|
4
|
+
* Transformer class for Cerebras
|
|
5
|
+
*/
|
|
6
|
+
export declare class CerebrasTransformer implements Transformer {
|
|
7
|
+
name: string;
|
|
8
|
+
/**
|
|
9
|
+
* Transform the request from Claude Code format to Cerebras format
|
|
10
|
+
* @param request - The incoming request
|
|
11
|
+
* @param provider - The LLM provider information
|
|
12
|
+
* @returns The transformed request
|
|
13
|
+
*/
|
|
14
|
+
transformRequestIn(request: UnifiedChatRequest, provider: LLMProvider): Promise<Record<string, unknown>>;
|
|
15
|
+
transformResponseOut(response: Response): Promise<Response>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { UnifiedChatRequest } from "../types/llm";
|
|
2
|
+
import { Transformer } from "../types/transformer";
|
|
3
|
+
export declare class CleancacheTransformer implements Transformer {
|
|
4
|
+
name: string;
|
|
5
|
+
transformRequestIn(request: UnifiedChatRequest): Promise<UnifiedChatRequest>;
|
|
6
|
+
}
|