route-claudecode
Version:
Advanced routing and transformation system for Claude Code outputs to multiple AI providers
268 lines • 11.6 kB
JavaScript
;
/**
* 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.CodeWhispererConfigMigrator = void 0;
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const logger_1 = require("@/utils/logger");
const enhanced_auth_config_1 = require("./enhanced-auth-config");
class CodeWhispererConfigMigrator {
/**
* 从传统配置迁移到增强配置
*/
static migrateFromLegacy(legacyConfig) {
if (!legacyConfig) {
return this.getDefaultEnhancedConfig();
}
logger_1.logger.info('Migrating CodeWhisperer configuration from legacy format', {
hasTokenPath: !!legacyConfig.tokenPath,
region: legacyConfig.region,
timeout: legacyConfig.timeout,
maxRetries: legacyConfig.maxRetries
});
// 构建增强配置
const enhancedConfig = {
credentials: {
...enhanced_auth_config_1.DEFAULT_CREDENTIAL_CONFIG,
// 如果指定了 tokenPath,优先使用文件路径源
credsFilePath: legacyConfig.tokenPath,
priorityOrder: legacyConfig.tokenPath
? [enhanced_auth_config_1.CredentialSource.FILE_PATH, enhanced_auth_config_1.CredentialSource.DEFAULT_PATH, enhanced_auth_config_1.CredentialSource.DIRECTORY_SCAN]
: enhanced_auth_config_1.DEFAULT_CREDENTIAL_CONFIG.priorityOrder
},
region: {
...enhanced_auth_config_1.DEFAULT_REGION_CONFIG,
region: legacyConfig.region || enhanced_auth_config_1.DEFAULT_REGION_CONFIG.region,
refreshUrl: legacyConfig.refreshUrl || enhanced_auth_config_1.DEFAULT_REGION_CONFIG.refreshUrl
},
retry: {
...enhanced_auth_config_1.DEFAULT_RETRY_CONFIG,
maxRetries: legacyConfig.maxRetries || enhanced_auth_config_1.DEFAULT_RETRY_CONFIG.maxRetries,
timeoutMs: legacyConfig.timeout || enhanced_auth_config_1.DEFAULT_RETRY_CONFIG.timeoutMs
},
authMethod: enhanced_auth_config_1.AuthMethod.SOCIAL, // 默认使用 Social 认证
enableDebugLog: legacyConfig.enableDebug || false,
userAgent: 'CodeWhisperer-Router/2.7.0'
};
logger_1.logger.info('Configuration migration completed', {
credentialSources: enhancedConfig.credentials.priorityOrder,
region: enhancedConfig.region?.region,
retryConfig: enhancedConfig.retry,
authMethod: enhancedConfig.authMethod
});
return enhancedConfig;
}
/**
* 自动检测现有配置并迁移
*/
static autoMigrateFromEnvironment() {
logger_1.logger.info('Auto-detecting existing CodeWhisperer configuration');
const detectedConfig = {};
// 检测环境变量
if (process.env.CODEWHISPERER_TOKEN_PATH) {
detectedConfig.tokenPath = process.env.CODEWHISPERER_TOKEN_PATH;
}
if (process.env.CODEWHISPERER_REGION) {
detectedConfig.region = process.env.CODEWHISPERER_REGION;
}
if (process.env.CODEWHISPERER_TIMEOUT) {
detectedConfig.timeout = parseInt(process.env.CODEWHISPERER_TIMEOUT, 10);
}
if (process.env.CODEWHISPERER_MAX_RETRIES) {
detectedConfig.maxRetries = parseInt(process.env.CODEWHISPERER_MAX_RETRIES, 10);
}
if (process.env.CODEWHISPERER_DEBUG) {
detectedConfig.enableDebug = process.env.CODEWHISPERER_DEBUG === 'true';
}
// 检测默认 token 文件位置
const defaultTokenPath = path.join(os.homedir(), '.aws', 'sso', 'cache', enhanced_auth_config_1.KIRO_AUTH_TOKEN_FILE);
if (!detectedConfig.tokenPath) {
detectedConfig.tokenPath = defaultTokenPath;
}
logger_1.logger.info('Environment detection completed', {
detectedTokenPath: detectedConfig.tokenPath,
detectedRegion: detectedConfig.region,
detectedTimeout: detectedConfig.timeout,
detectedMaxRetries: detectedConfig.maxRetries,
detectedDebug: detectedConfig.enableDebug
});
return this.migrateFromLegacy(detectedConfig);
}
/**
* 创建基于多源凭据的配置
*/
static createMultiSourceConfig(options) {
logger_1.logger.info('Creating multi-source CodeWhisperer configuration', {
hasBase64Creds: !!options.base64Creds,
hasCredsFilePath: !!options.credsFilePath,
hasCredsDirPath: !!options.credsDirPath,
region: options.region,
authMethod: options.authMethod
});
// 根据提供的选项确定优先级顺序
const priorityOrder = [];
if (options.base64Creds) {
priorityOrder.push(enhanced_auth_config_1.CredentialSource.BASE64);
}
if (options.credsFilePath) {
priorityOrder.push(enhanced_auth_config_1.CredentialSource.FILE_PATH);
}
if (options.credsDirPath) {
priorityOrder.push(enhanced_auth_config_1.CredentialSource.DIRECTORY_SCAN);
}
// 始终包含环境变量和默认路径作为后备
priorityOrder.push(enhanced_auth_config_1.CredentialSource.ENVIRONMENT, enhanced_auth_config_1.CredentialSource.DEFAULT_PATH);
const config = {
credentials: {
...enhanced_auth_config_1.DEFAULT_CREDENTIAL_CONFIG,
base64Creds: options.base64Creds,
credsFilePath: options.credsFilePath,
credsDirPath: options.credsDirPath,
priorityOrder
},
region: {
...enhanced_auth_config_1.DEFAULT_REGION_CONFIG,
region: options.region || enhanced_auth_config_1.DEFAULT_REGION_CONFIG.region
},
retry: enhanced_auth_config_1.DEFAULT_RETRY_CONFIG,
authMethod: options.authMethod || enhanced_auth_config_1.AuthMethod.SOCIAL,
enableDebugLog: options.enableDebugLog || false,
userAgent: 'CodeWhisperer-Router/2.7.0'
};
logger_1.logger.info('Multi-source configuration created', {
priorityOrder: config.credentials.priorityOrder,
region: config.region?.region,
authMethod: config.authMethod
});
return config;
}
/**
* 获取默认增强配置
*/
static getDefaultEnhancedConfig() {
logger_1.logger.info('Creating default enhanced CodeWhisperer configuration');
return {
credentials: enhanced_auth_config_1.DEFAULT_CREDENTIAL_CONFIG,
region: enhanced_auth_config_1.DEFAULT_REGION_CONFIG,
retry: enhanced_auth_config_1.DEFAULT_RETRY_CONFIG,
authMethod: enhanced_auth_config_1.AuthMethod.SOCIAL,
enableDebugLog: false,
userAgent: 'CodeWhisperer-Router/2.7.0'
};
}
/**
* 验证配置有效性
*/
static validateConfig(config) {
const errors = [];
// 验证凭据配置
if (!config.credentials) {
errors.push('Credentials configuration is required');
}
else {
if (!config.credentials.priorityOrder || config.credentials.priorityOrder.length === 0) {
errors.push('At least one credential source must be specified');
}
}
// 验证区域配置
if (!config.region?.region) {
errors.push('Region is required');
}
// 验证重试配置
if (config.retry) {
if (config.retry.maxRetries < 0) {
errors.push('Max retries cannot be negative');
}
if (config.retry.baseDelay < 0) {
errors.push('Base delay cannot be negative');
}
if (config.retry.timeoutMs < 1000) {
errors.push('Timeout must be at least 1000ms');
}
}
// 验证认证方法
if (config.authMethod && !Object.values(enhanced_auth_config_1.AuthMethod).includes(config.authMethod)) {
errors.push('Invalid authentication method');
}
const isValid = errors.length === 0;
if (isValid) {
logger_1.logger.info('Configuration validation passed');
}
else {
logger_1.logger.error('Configuration validation failed', { errors });
}
return { isValid, errors };
}
/**
* 比较两个配置的差异
*/
static compareConfigs(oldConfig, newConfig) {
const changes = [];
// 比较凭据配置
if (JSON.stringify(oldConfig.credentials) !== JSON.stringify(newConfig.credentials)) {
changes.push('credentials configuration changed');
}
// 比较区域配置
if (oldConfig.region?.region !== newConfig.region?.region) {
changes.push(`region changed from ${oldConfig.region?.region} to ${newConfig.region?.region}`);
}
// 比较重试配置
if (JSON.stringify(oldConfig.retry) !== JSON.stringify(newConfig.retry)) {
changes.push('retry configuration changed');
}
// 比较认证方法
if (oldConfig.authMethod !== newConfig.authMethod) {
changes.push(`auth method changed from ${oldConfig.authMethod} to ${newConfig.authMethod}`);
}
// 比较调试设置
if (oldConfig.enableDebugLog !== newConfig.enableDebugLog) {
changes.push(`debug logging ${newConfig.enableDebugLog ? 'enabled' : 'disabled'}`);
}
const hasChanges = changes.length > 0;
logger_1.logger.info('Configuration comparison completed', {
hasChanges,
changeCount: changes.length,
changes: hasChanges ? changes : 'no changes detected'
});
return { hasChanges, changes };
}
}
exports.CodeWhispererConfigMigrator = CodeWhispererConfigMigrator;
//# sourceMappingURL=config-migration.js.map