UNPKG

route-claudecode

Version:

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

290 lines 10 kB
"use strict"; /** * CodeWhisperer 实时流式配置管理 * 支持动态切换缓冲式和实时流式实现 * 项目所有者: Jason Zhang */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.CodeWhispererStreamingConfigManager = exports.defaultStreamingConfig = void 0; const logger_1 = require("@/utils/logger"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); exports.defaultStreamingConfig = { implementation: 'buffered', // 默认使用现有实现 realtimeOptions: { enableZeroDelay: true, maxConcurrentStreams: 100, binaryFrameSize: 1024 * 1024, // 1MB toolCallStrategy: 'immediate', enableCompression: false, }, performanceMetrics: { enableProfiling: false, collectLatencyData: false, memoryUsageTracking: false, metricsIntervalMs: 5000, }, fallback: { enableFallback: true, fallbackToBuffered: true, maxFailuresBeforeFallback: 3, }, }; class CodeWhispererStreamingConfigManager { static instance; config; configPath; listeners = []; constructor() { this.configPath = this.getConfigPathInternal(); this.config = this.loadConfig(); } static getInstance() { if (!CodeWhispererStreamingConfigManager.instance) { CodeWhispererStreamingConfigManager.instance = new CodeWhispererStreamingConfigManager(); } return CodeWhispererStreamingConfigManager.instance; } /** * 获取配置文件路径 */ /** * 获取配置文件路径 */ getConfigPathInternal() { // 优先使用环境变量指定的路径 const envPath = process.env.CODEWHISPERER_STREAMING_CONFIG; if (envPath && fs.existsSync(envPath)) { return envPath; } // 其次使用用户目录下的配置 const homeDir = process.env.HOME || process.env.USERPROFILE || ''; const userConfigPath = path.join(homeDir, '.route-claude-code', 'config', 'codewhisperer-streaming.json'); if (fs.existsSync(userConfigPath)) { return userConfigPath; } // 最后使用项目目录下的配置 const projectConfigPath = path.join(process.cwd(), 'config', 'codewhisperer-streaming.json'); return projectConfigPath; } /** * 加载配置 */ loadConfig() { try { if (fs.existsSync(this.configPath)) { const configData = JSON.parse(fs.readFileSync(this.configPath, 'utf8')); const mergedConfig = { ...exports.defaultStreamingConfig, ...configData }; logger_1.logger.info('成功加载CodeWhisperer流式配置', { path: this.configPath, implementation: mergedConfig.implementation, realtimeOptions: mergedConfig.realtimeOptions, }); return mergedConfig; } } catch (error) { logger_1.logger.warn('无法加载CodeWhisperer流式配置,使用默认配置', { path: this.configPath, error: error instanceof Error ? error.message : String(error), }); } logger_1.logger.info('使用默认CodeWhisperer流式配置'); return { ...exports.defaultStreamingConfig }; } /** * 保存配置到文件 */ saveConfig() { try { // 确保目录存在 const configDir = path.dirname(this.configPath); if (!fs.existsSync(configDir)) { fs.mkdirSync(configDir, { recursive: true }); } fs.writeFileSync(this.configPath, JSON.stringify(this.config, null, 2)); logger_1.logger.info('CodeWhisperer流式配置已保存', { path: this.configPath }); } catch (error) { logger_1.logger.error('保存CodeWhisperer流式配置失败', { path: this.configPath, error: error instanceof Error ? error.message : String(error), }); } } /** * 获取当前配置 */ getConfig() { return { ...this.config }; } /** * 更新配置 */ updateConfig(newConfig) { const oldConfig = { ...this.config }; this.config = { ...this.config, ...newConfig }; logger_1.logger.info('CodeWhisperer流式配置已更新', { changes: this.getConfigChanges(oldConfig, this.config), newConfig: this.config, }); // 保存配置 this.saveConfig(); // 通知监听器 this.notifyListeners(this.config); } /** * 切换实现类型 */ switchImplementation(implementation) { const oldImplementation = this.config.implementation; this.config.implementation = implementation; logger_1.logger.info(`CodeWhisperer实现已切换`, { from: oldImplementation, to: implementation, configPath: this.configPath, }); // 保存配置 this.saveConfig(); // 通知监听器 this.notifyListeners(this.config); } /** * 添加配置变更监听器 */ addConfigChangeListener(listener) { this.listeners.push(listener); } /** * 移除配置变更监听器 */ removeConfigChangeListener(listener) { const index = this.listeners.indexOf(listener); if (index > -1) { this.listeners.splice(index, 1); } } /** * 通知监听器 */ notifyListeners(config) { this.listeners.forEach(listener => { try { listener(config); } catch (error) { logger_1.logger.error('配置变更监听器执行失败', { error: error instanceof Error ? error.message : String(error), }); } }); } /** * 获取配置变更内容 */ getConfigChanges(oldConfig, newConfig) { const changes = {}; if (oldConfig.implementation !== newConfig.implementation) { changes.implementation = { from: oldConfig.implementation, to: newConfig.implementation, }; } // 检查realtimeOptions的变更 const realtimeChanges = {}; for (const key in oldConfig.realtimeOptions) { const oldValue = oldConfig.realtimeOptions[key]; const newValue = newConfig.realtimeOptions[key]; if (oldValue !== newValue) { realtimeChanges[key] = { from: oldValue, to: newValue }; } } if (Object.keys(realtimeChanges).length > 0) { changes.realtimeOptions = realtimeChanges; } return changes; } /** * 获取配置文件路径 */ getConfigPath() { return this.configPath; } /** * 重置为默认配置 */ resetToDefault() { this.config = { ...exports.defaultStreamingConfig }; this.saveConfig(); this.notifyListeners(this.config); logger_1.logger.info('CodeWhisperer流式配置已重置为默认配置'); } /** * 验证配置 */ validateConfig(config) { const errors = []; // 验证implementation if (!['buffered', 'realtime'].includes(config.implementation)) { errors.push('implementation必须是buffered或realtime'); } // 验证realtimeOptions if (config.realtimeOptions.maxConcurrentStreams < 1) { errors.push('maxConcurrentStreams必须大于0'); } if (config.realtimeOptions.binaryFrameSize < 1024) { errors.push('binaryFrameSize必须至少为1024字节'); } if (!['immediate', 'buffered'].includes(config.realtimeOptions.toolCallStrategy)) { errors.push('toolCallStrategy必须是immediate或buffered'); } // 验证performanceMetrics if (config.performanceMetrics.metricsIntervalMs < 100) { errors.push('metricsIntervalMs必须至少为100ms'); } // 验证fallback if (config.fallback.maxFailuresBeforeFallback < 1) { errors.push('maxFailuresBeforeFallback必须大于0'); } return { valid: errors.length === 0, errors, }; } } exports.CodeWhispererStreamingConfigManager = CodeWhispererStreamingConfigManager; //# sourceMappingURL=streaming-config.js.map