UNPKG

route-claudecode

Version:

Advanced routing and transformation system for Claude Code outputs to multiple AI providers

342 lines 9.04 kB
/** * Core types and interfaces for Claude Code Router */ export interface BaseRequest { model: string; messages: Array<{ role: string; content: any; }>; stream?: boolean; max_tokens?: number; temperature?: number; system?: any; tools?: any[]; tool_choice?: any; contents?: any[]; systemInstruction?: any; metadata?: { requestId: string; sessionId?: string; conversationId?: string; [key: string]: any; }; } export interface AnthropicRequest extends BaseRequest { system?: Array<{ type: string; text: string; }>; tools?: Array<{ name: string; description: string; input_schema: Record<string, any>; }>; thinking?: boolean; } export interface OpenAIRequest extends BaseRequest { system?: string; tools?: Array<{ type: string; function: { name: string; description: string; parameters: Record<string, any>; }; }>; } export interface GeminiApiRequest { model?: string; contents: Array<{ role: 'user' | 'model'; parts: Array<{ text?: string; functionCall?: { name: string; args: Record<string, any>; }; functionResponse?: { name: string; response: { name: string; content: any; }; }; }>; }>; tools?: Array<{ functionDeclarations: Array<{ name: string; description: string; parameters: Record<string, any>; }>; }>; toolConfig?: { functionCallingConfig: { mode: 'AUTO' | 'ANY' | 'NONE'; allowedFunctionNames?: string[]; }; }; generationConfig?: { temperature?: number; maxOutputTokens?: number; topP?: number; topK?: number; }; safetySettings?: Array<{ category: string; threshold: string; }>; } export interface GeminiApiResponse { candidates: Array<{ content: { parts: Array<{ text?: string; functionCall?: { name: string; args: Record<string, any>; }; }>; }; finishReason: string; index: number; safetyRatings?: Array<{ category: string; probability: string; }>; }>; usageMetadata?: { promptTokenCount: number; candidatesTokenCount: number; totalTokenCount: number; }; } export interface BaseResponse { id?: string; type?: string; model: string; role: string; content: any[]; stop_reason?: string; stop_sequence?: string | null; usage?: { input_tokens: number; output_tokens: number; total_tokens?: number; }; choices?: any[]; metadata?: { [key: string]: any; }; } export interface AnthropicResponse extends BaseResponse { type: "message"; stop_sequence?: string; } export interface OpenAIResponse extends BaseResponse { object: "chat.completion"; created: number; choices: Array<{ index: number; message: { role: string; content: string; }; finish_reason: string; }>; } export type RoutingCategory = 'default' | 'background' | 'thinking' | 'longcontext' | 'search'; export interface BackupProvider { provider: string; model: string; weight?: number; } export interface ProviderEntry { provider: string; model: string; weight?: number; } export interface LoadBalancingConfig { enabled: boolean; strategy: 'round_robin' | 'weighted' | 'health_based'; healthCheckInterval?: number; } export interface FailoverTrigger { type: 'consecutive_errors' | 'http_error' | 'network_timeout' | 'auth_failed'; threshold: number; timeWindow?: number; httpCodes?: number[]; } export interface FailoverConfig { enabled: boolean; triggers: FailoverTrigger[]; cooldown: number; } export interface ProviderHealth { providerId: string; isHealthy: boolean; consecutiveErrors: number; errorHistory: Array<{ timestamp: Date; errorType: string; errorMessage: string; httpCode?: number; }>; lastSuccessTime?: Date; lastFailureTime?: Date; totalRequests: number; successCount: number; failureCount: number; inCooldown: boolean; cooldownUntil?: Date; isPermanentlyBlacklisted: boolean; blacklistReason?: string; temporaryBackoffLevel: number; nextRetryTime?: Date; authFailureCount: number; networkFailureCount: number; gatewayFailureCount: number; lastAuthFailure?: Date; lastNetworkFailure?: Date; lastGatewayFailure?: Date; isTemporarilyBlacklisted: boolean; temporaryBlacklistUntil?: Date; rateLimitFailureCount: number; lastRateLimitFailure?: Date; isTemporarilyDisabledByUser?: boolean; } export interface CategoryRouting { providers?: ProviderEntry[]; loadBalancing?: LoadBalancingConfig; failover?: FailoverConfig; provider?: string; model?: string; backup?: BackupProvider[]; } export interface ProviderConfig { type: 'codewhisperer' | 'shuaihong' | 'openai' | 'anthropic' | 'gemini' | 'lmstudio'; endpoint: string; port?: number; authentication: { type: 'bearer' | 'api_key' | 'none'; credentials?: Record<string, string | string[]>; }; settings: Record<string, any>; models?: string[]; defaultModel?: string; healthCheck?: { endpoint: string; interval: number; }; keyRotation?: { enabled: boolean; strategy: 'round_robin' | 'health_based' | 'rate_limit_aware'; cooldownMs?: number; maxRetriesPerKey?: number; rateLimitCooldownMs?: number; }; tokenRotation?: { strategy: 'round_robin' | 'health_based' | 'least_used'; cooldownMs?: number; maxRetriesPerToken?: number; tempDisableCooldownMs?: number; maxRefreshFailures?: number; refreshRetryIntervalMs?: number; }; } export interface ProviderInstance { id: string; config: ProviderConfig; status: 'active' | 'inactive' | 'error'; lastCheck: Date; errorCount: number; } export interface InputProcessor { name: string; canProcess(request: any): boolean; process(request: any): Promise<BaseRequest>; validate(request: any): boolean; } export interface OutputProcessor { name: string; canProcess(response: any, format: string): boolean; process(response: any, originalRequest: BaseRequest): Promise<BaseResponse>; } export interface Provider { name: string; type: string; config: ProviderConfig; isHealthy(): Promise<boolean>; sendRequest(request: BaseRequest): Promise<BaseResponse>; sendStreamRequest(request: BaseRequest): AsyncIterable<any>; } export interface HookContext { requestId: string; stage: 'input' | 'routing' | 'provider' | 'output'; timestamp: Date; data: any; metadata: Record<string, any>; } export interface Hook { name: string; stage: HookContext['stage']; priority: number; execute(context: HookContext): Promise<HookContext>; } export interface RouterConfig { server: { port: number; host: string; }; providers: Record<string, ProviderConfig>; routing: { [category: string]: { provider: string; model: string; }; }; debug: { enabled: boolean; logLevel: 'error' | 'warn' | 'info' | 'debug'; traceRequests: boolean; saveRequests: boolean; logDir: string; }; concurrency?: { enabled: boolean; queueManagement: boolean; sequenceTracking: boolean; }; hooks: Hook[]; } export interface RequestMetrics { requestId: string; startTime: Date; endTime?: Date; duration?: number; inputTokens: number; outputTokens: number; provider: string; success: boolean; error?: string; } export interface LoadBalancerState { providers: Record<string, ProviderInstance[]>; roundRobinIndex: Record<string, number>; healthStatus: Record<string, boolean>; } export declare class RouterError extends Error { code: string; statusCode: number; details?: any | undefined; constructor(message: string, code: string, statusCode?: number, details?: any | undefined); } export declare class ProviderError extends RouterError { provider: string; constructor(message: string, provider: string, statusCode?: number, details?: any); } export declare class ValidationError extends RouterError { constructor(message: string, details?: any); } //# sourceMappingURL=index.d.ts.map