route-claudecode
Version:
Advanced routing and transformation system for Claude Code outputs to multiple AI providers
109 lines • 3.08 kB
TypeScript
/**
* OpenAI Client Factory - 统一客户端管理器
* 基于项目记忆中的成功重构经验,消除重复实现
* 项目所有者: Jason Zhang
*
* 设计目标:
* 1. 统一的客户端创建接口
* 2. 消除Pure Client和SDK Client的重复代码
* 3. 遵循零硬编码、零Fallback、零跨节点耦合原则
* 4. 向后兼容性保证
*/
import { Provider, ProviderConfig } from '@/types';
export interface OpenAIClientConfig extends ProviderConfig {
clientType?: 'pure' | 'sdk' | 'unified' | 'auto';
sdkOptions?: {
timeout?: number;
maxRetries?: number;
defaultHeaders?: Record<string, string>;
};
switchingStrategy?: {
autoSwitch?: boolean;
errorThreshold?: number;
performanceThreshold?: number;
switchCooldown?: number;
};
}
/**
* 客户端类型枚举 - 推荐使用UNIFIED
*/
export declare enum ClientType {
UNIFIED = "unified",// 推荐:统一客户端实现
PURE = "pure",// 废弃:保留向后兼容
SDK = "sdk"
}
/**
* 客户端性能统计
*/
interface ClientStats {
requestCount: number;
errorCount: number;
consecutiveErrors: number;
averageResponseTime: number;
lastErrorTime?: Date;
lastSwitchTime?: Date;
}
/**
* OpenAI客户端工厂
*/
export declare class OpenAIClientFactory {
private static clientStats;
private static activeClients;
/**
* 创建OpenAI客户端 - 优先使用统一实现
*/
static createClient(config: OpenAIClientConfig, providerId: string, globalConfig?: any): Provider;
/**
* 确定客户端类型 - 优先使用统一客户端
* 基于项目记忆中的成功重构经验
*/
private static determineClientType;
/**
* 实例化客户端 - 优先使用统一实现
*/
private static instantiateClient;
/**
* 包装客户端以添加监控和自动切换功能
*/
private static wrapClientWithMonitoring;
/**
* 更新成功统计
*/
private static updateSuccessStats;
/**
* 更新错误统计
*/
private static updateErrorStats;
/**
* 检查并切换客户端
*/
private static checkAndSwitchClient;
/**
* 获取客户端统计信息
*/
static getClientStats(providerId: string): ClientStats | undefined;
/**
* 获取活跃客户端信息
*/
static getActiveClientInfo(providerId: string): {
type: ClientType;
stats: ClientStats;
} | undefined;
/**
* 手动切换客户端
*/
static switchClient(providerId: string, targetType: ClientType, config: OpenAIClientConfig): Promise<boolean>;
/**
* 获取所有客户端状态
*/
static getAllClientStatus(): Record<string, {
type: ClientType;
stats: ClientStats;
}>;
}
/**
* 便捷函数:创建OpenAI客户端
*/
export declare function createOpenAIClient(config: OpenAIClientConfig, providerId: string, globalConfig?: any): Provider;
export {};
//# sourceMappingURL=client-factory.d.ts.map