ccgo
Version:
Simple Claude Code launcher with config management and environment variable injection
254 lines • 7.5 kB
JavaScript
"use strict";
/**
* 配置管理器
* 负责读取、保存、验证 API 配置信息
*/
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;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigManager = void 0;
const conf_1 = __importDefault(require("conf"));
const path = __importStar(require("path"));
const os = __importStar(require("os"));
const constants_1 = require("./constants");
/**
* 配置管理器类
*/
class ConfigManager {
constructor() {
this.config = new conf_1.default({
projectName: constants_1.CONFIG_NAME,
cwd: path.join(os.homedir(), '.config', constants_1.CONFIG_NAME)
});
// 迁移旧配置(如果存在)
this.migrateOldConfig();
}
/**
* 迁移旧版本的单配置到新的多配置结构
*/
migrateOldConfig() {
const oldApi = this.config.get('api');
// 如果存在旧配置但没有 profiles,进行迁移
if (oldApi && !this.config.has('profiles')) {
const profileName = 'default';
this.config.set('profiles', {
[profileName]: {
apiKey: oldApi.apiKey,
baseUrl: oldApi.baseUrl,
model: oldApi.model,
smallFastModel: oldApi.smallFastModel,
configuredAt: oldApi.configuredAt,
version: oldApi.version
}
});
this.config.delete('api');
}
// 清理旧的 defaultProfile 字段
if (this.config.has('defaultProfile')) {
this.config.delete('defaultProfile');
}
}
/**
* 检查是否有任何配置
*/
hasAnyProfile() {
const profiles = this.config.get('profiles', {});
return Object.keys(profiles).length > 0;
}
/**
* 检查是否已配置 API Key(向后兼容)
*/
hasApiKey() {
return this.hasAnyProfile();
}
/**
* 获取所有配置名称
*/
getProfileNames() {
const profiles = this.config.get('profiles', {});
return Object.keys(profiles);
}
/**
* 获取配置数量
*/
getProfileCount() {
return this.getProfileNames().length;
}
/**
* 检查配置是否存在
*/
hasProfile(name) {
return this.config.has(`profiles.${name}`);
}
/**
* 获取指定配置
*/
getProfile(name) {
return this.config.get(`profiles.${name}`);
}
/**
* 获取当前激活的配置(仅当只有一个配置时返回)
*/
getCurrentProfile() {
const names = this.getProfileNames();
if (names.length === 0) {
return undefined;
}
// 只有一个配置时,直接使用
if (names.length === 1) {
const name = names[0];
const config = this.getProfile(name);
return config ? { name, config } : undefined;
}
// 多个配置时,返回 undefined(需要用户选择)
return undefined;
}
/**
* 获取 API Key(向后兼容)
*/
getApiKey() {
const current = this.getCurrentProfile();
return current?.config.apiKey;
}
/**
* 获取 Base URL(向后兼容)
*/
getBaseUrl() {
const current = this.getCurrentProfile();
return current?.config.baseUrl;
}
/**
* 获取模型名称(可选)
*/
getModel() {
const current = this.getCurrentProfile();
return current?.config.model;
}
/**
* 获取小快速模型(可选)
*/
getSmallFastModel() {
const current = this.getCurrentProfile();
return current?.config.smallFastModel;
}
/**
* 添加或更新配置
*/
saveProfile(name, config) {
const fullConfig = {
...config,
configuredAt: new Date().toISOString(),
version: '1.0.0'
};
const profiles = this.config.get('profiles', {});
profiles[name] = fullConfig;
this.config.set('profiles', profiles);
}
/**
* 删除配置
*/
removeProfile(name) {
if (!this.hasProfile(name)) {
throw new Error(`配置 "${name}" 不存在`);
}
const profiles = this.config.get('profiles', {});
delete profiles[name];
this.config.set('profiles', profiles);
}
/**
* 保存 API 配置(向后兼容,保存为 default)
*/
saveApiConfig(config, name = 'default') {
this.saveProfile(name, config);
}
/**
* 获取所有配置(含配置名称)
*/
getAllProfiles() {
const profiles = this.config.get('profiles', {});
return Object.entries(profiles).map(([name, config]) => ({
name,
...config
}));
}
/**
* 重置所有配置
*/
reset() {
this.config.clear();
}
/**
* 获取所有配置
*/
getAll() {
return this.config.store;
}
/**
* 获取配置文件路径
*/
getConfigPath() {
return this.config.path;
}
/**
* 获取安全的配置信息(隐藏 API Key)
*/
getSafeApiConfig(name) {
let apiConfig;
if (name) {
apiConfig = this.getProfile(name);
}
else {
const current = this.getCurrentProfile();
apiConfig = current?.config;
}
if (!apiConfig)
return undefined;
const safeConfig = { ...apiConfig };
if (safeConfig.apiKey) {
const key = safeConfig.apiKey;
if (key.length > 12) {
safeConfig.apiKey = `${key.substring(0, 8)}...${key.substring(key.length - 4)}`;
}
else {
safeConfig.apiKey = '***';
}
}
return safeConfig;
}
}
exports.ConfigManager = ConfigManager;
//# sourceMappingURL=manager.js.map