🎨 refactor(aiCaller): improve logging for user OpenAI instance retrieval

🔧 fix(userConfigManager): update user config path and improve error handling during load/save operations
This commit is contained in:
Jerry 2025-12-06 01:30:11 +08:00
parent 71f956d418
commit fcbb10d2f5
2 changed files with 18 additions and 7 deletions

View File

@ -265,10 +265,12 @@ class AiCaller {
*/ */
async getUserOpenaiInstance(userId, config) { async getUserOpenaiInstance(userId, config) {
if (config.apiKey === this.config.apiKey && config.baseApi === this.config.baseApi) { if (config.apiKey === this.config.apiKey && config.baseApi === this.config.baseApi) {
logger.info(`[crystelf-ai] 用户 ${userId} 使用全局OpenAI实例`);
return this.openaiChat; return this.openaiChat;
} }
const cacheKey = `${userId}_${config.apiKey}_${config.baseApi}`; const cacheKey = `${userId}_${config.apiKey}_${config.baseApi}`;
if (this.userOpenaiInstances.has(cacheKey)) { if (this.userOpenaiInstances.has(cacheKey)) {
logger.info(`[crystelf-ai] 用户 ${userId} 使用缓存的OpenAI实例`);
return this.userOpenaiInstances.get(cacheKey); return this.userOpenaiInstances.get(cacheKey);
} }
const userOpenaiChat = new OpenaiChat(); const userOpenaiChat = new OpenaiChat();

View File

@ -8,7 +8,7 @@ import ConfigControl from '../config/configControl.js';
*/ */
class UserConfigManager { class UserConfigManager {
constructor() { constructor() {
this.basePath = path.join(process.cwd(), 'data', 'crystelf', 'ai'); this.basePath = path.join(process.cwd(), 'data', 'crystelf');
this.userConfigs = new Map(); this.userConfigs = new Map();
this.globalConfig = null; this.globalConfig = null;
} }
@ -30,20 +30,25 @@ class UserConfigManager {
async getUserConfig(userId) { async getUserConfig(userId) {
try { try {
if (this.userConfigs.has(userId)) { if (this.userConfigs.has(userId)) {
return this.userConfigs.get(userId); const cachedConfig = this.userConfigs.get(userId);
logger.info(`[crystelf-ai] 使用缓存的用户配置 ${userId}: apiKey=${!!cachedConfig.apiKey}, model=${cachedConfig.modelType}`);
return cachedConfig;
} }
const userConfigPath = path.join(this.basePath, `${userId}.json`); const userConfigPath = path.join(this.basePath, 'ai', userId, 'ai.json');
logger.info(`[crystelf-ai] 尝试加载用户配置: ${userConfigPath}`);
let userConfig = {}; let userConfig = {};
try { try {
const configData = await fs.readFile(userConfigPath, 'utf-8'); const configData = await fs.readFile(userConfigPath, 'utf-8');
userConfig = JSON.parse(configData); userConfig = JSON.parse(configData);
} catch (error) { } catch (error) {
if (error.code !== 'ENOENT') { if (error.code === 'ENOENT') {
} else {
logger.warn(`[crystelf-ai] 用户 ${userId} 的配置文件解析失败,使用默认配置: ${error.message}`); logger.warn(`[crystelf-ai] 用户 ${userId} 的配置文件解析失败,使用默认配置: ${error.message}`);
} }
} }
const mergedConfig = this.mergeConfigs(this.globalConfig, userConfig); const mergedConfig = this.mergeConfigs(this.globalConfig, userConfig);
this.userConfigs.set(userId, mergedConfig); this.userConfigs.set(userId, mergedConfig);
@ -61,12 +66,16 @@ class UserConfigManager {
*/ */
async saveUserConfig(userId, config) { async saveUserConfig(userId, config) {
try { try {
const userConfigPath = path.join(this.basePath, `${userId}.json`); const userConfigDir = path.join(this.basePath, 'ai', userId);
const userConfigPath = path.join(userConfigDir, 'ai.json');
const filteredConfig = this.filterUserConfig(config); const filteredConfig = this.filterUserConfig(config);
await fs.mkdir(userConfigDir, { recursive: true });
await fs.writeFile(userConfigPath, JSON.stringify(filteredConfig, null, 2)); await fs.writeFile(userConfigPath, JSON.stringify(filteredConfig, null, 2));
const mergedConfig = this.mergeConfigs(this.globalConfig, filteredConfig); const mergedConfig = this.mergeConfigs(this.globalConfig, filteredConfig);
this.userConfigs.set(userId, mergedConfig); this.userConfigs.set(userId, mergedConfig);
} catch (error) { } catch (error) {
logger.error(`[crystelf-ai] 保存用户 ${userId} 配置失败: ${error.message}`); logger.error(`[crystelf-ai] 保存用户 ${userId} 配置失败: ${error.message}`);
throw error; throw error;
@ -158,7 +167,7 @@ class UserConfigManager {
*/ */
async hasUserConfig(userId) { async hasUserConfig(userId) {
try { try {
const userConfigPath = path.join(this.basePath, `${userId}.json`); const userConfigPath = path.join(this.basePath, 'ai', userId, 'ai.json');
await fs.access(userConfigPath); await fs.access(userConfigPath);
return true; return true;
} catch { } catch {