日志控制

This commit is contained in:
chen wei bo
2025-09-21 20:18:30 +08:00
parent 23c038fd14
commit 8f90c1d7cf
23 changed files with 207 additions and 178 deletions
@@ -3,6 +3,7 @@ import { DataManager, DataId } from "../data/DataManager";
import { GirlData } from "../data/GirlData";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import { ChatService } from "../network/services/ChatService";
import { logger } from "db://assets/Scripts/Main/Common/Logger";
export interface ChatMessage {
role: "user" | "model";
@@ -49,7 +50,7 @@ export class ChatHistoryManager {
const existingHistory: ChatHistory = JSON.parse(existingData);
createdAt = existingHistory.createdAt;
} catch (parseError) {
console.warn(`Failed to parse existing history for role ${roleId}, using current time as createdAt`);
logger.warn(`Failed to parse existing history for role ${roleId}, using current time as createdAt`);
}
}
@@ -61,11 +62,11 @@ export class ChatHistoryManager {
};
sys.localStorage.setItem(key, JSON.stringify(history));
console.log(
logger.log(
`Saved chat history for role ${roleId}, ${messages.length} messages`
);
} catch (error) {
console.error(`Failed to save chat history for role ${roleId}:`, error);
logger.error(`Failed to save chat history for role ${roleId}:`, error);
}
}
@@ -80,13 +81,13 @@ export class ChatHistoryManager {
const data = sys.localStorage.getItem(key);
if (data) {
const history: ChatHistory = JSON.parse(data);
console.log(
logger.log(
`Loaded chat history for role ${roleId}: ${history.messages.length} messages`
);
return history.messages;
}
} catch (error) {
console.error(`Failed to load chat history for role ${roleId}:`, error);
logger.error(`Failed to load chat history for role ${roleId}:`, error);
// 如果数据损坏,清除错误的数据
this.clearHistory(roleId);
}
@@ -100,7 +101,7 @@ export class ChatHistoryManager {
public clearHistory(roleId: number): void {
const key = `chat_history_${roleId}`;
sys.localStorage.removeItem(key);
console.log(`Cleared chat history for role ${roleId}`);
logger.log(`Cleared chat history for role ${roleId}`);
}
/**
@@ -118,7 +119,7 @@ export class ChatHistoryManager {
// 限制历史长度,保留最近100条消息
if (history.length > 100) {
history.splice(0, history.length - 100);
console.log(`Trimmed chat history for role ${roleId} to 100 messages`);
logger.log(`Trimmed chat history for role ${roleId} to 100 messages`);
}
this.saveHistory(roleId, history);
@@ -163,7 +164,7 @@ export class ChatHistoryManager {
sys.localStorage.removeItem(key);
});
console.log(
logger.log(
`Cleared all chat histories, ${keysToRemove.length} records removed`
);
}
@@ -193,7 +194,7 @@ export class ChatHistoryManager {
public async syncToRemote(roleId: number): Promise<void> {
const history = this.loadHistory(roleId);
if (history.length === 0) {
console.log(`No history to sync for role ${roleId}`);
logger.log(`No history to sync for role ${roleId}`);
return;
}
@@ -204,11 +205,11 @@ export class ChatHistoryManager {
// messages: history
// }, "POST");
console.log(
logger.log(
`Ready to sync ${history.length} messages for role ${roleId} to remote server`
);
} catch (error) {
console.error(`Failed to sync history for role ${roleId}:`, error);
logger.error(`Failed to sync history for role ${roleId}:`, error);
}
}
@@ -232,7 +233,7 @@ export class ChatHistoryManager {
return history.createdAt;
}
} catch (error) {
console.error(`Failed to get last chat time for role ${roleId}:`, error);
logger.error(`Failed to get last chat time for role ${roleId}:`, error);
}
return null;
}
@@ -250,14 +251,14 @@ export class ChatHistoryManager {
// if (response && response.messages) {
// this.saveHistory(roleId, response.messages);
// console.log(`Synced ${response.messages.length} messages from remote for role ${roleId}`);
// logger.log(`Synced ${response.messages.length} messages from remote for role ${roleId}`);
// }
console.log(
logger.log(
`Ready to sync history from remote server for role ${roleId}`
);
} catch (error) {
console.error(`Failed to sync from remote for role ${roleId}:`, error);
logger.error(`Failed to sync from remote for role ${roleId}:`, error);
}
}
@@ -288,7 +289,7 @@ export class ChatHistoryManager {
// 按时间戳排序,确保消息顺序正确
messages.sort((a, b) => (a.timestamp || 0) - (b.timestamp || 0));
console.log(
logger.log(
`Converted ${serverMessages.length} server messages to ${messages.length} local messages`
);
return messages;
@@ -313,7 +314,7 @@ export class ChatHistoryManager {
limit,
};
console.log("获取服务端聊天记录:", reqData);
logger.log("获取服务端聊天记录:", reqData);
const res = await ChatService.I.reqGetChatMsg(reqData);
if (res && res.code === proto.cs.EnmRetCode.SUCCESS) {
@@ -333,20 +334,20 @@ export class ChatHistoryManager {
resData.chatRemainCount
);
console.log(
logger.log(
`成功从服务端获取聊天记录,roleId: ${roleId}, 消息数: ${
resData.msgs?.length || 0
}`
);
return true;
} else {
console.warn(
logger.warn(
`从服务端获取聊天记录失败,roleId: ${roleId}, code: ${res?.code}`
);
return false;
}
} catch (error) {
console.error(`获取服务端聊天记录异常,roleId: ${roleId}:`, error);
logger.error(`获取服务端聊天记录异常,roleId: ${roleId}:`, error);
return false;
}
}
@@ -390,7 +391,7 @@ export class ChatHistoryManager {
const mergedMessages = Array.from(messageMap.values());
mergedMessages.sort((a, b) => (a.timestamp || 0) - (b.timestamp || 0));
console.log(
logger.log(
`合并聊天记录: 本地 ${localMessages.length} 条, 服务端 ${serverMessages.length} 条, 合并后 ${mergedMessages.length}`
);
return mergedMessages;
@@ -407,18 +408,18 @@ export class ChatHistoryManager {
// 2. 如果本地有数据,直接返回
if (localHistory && localHistory.length > 0) {
console.log(
logger.log(
`使用本地聊天记录,roleId: ${roleId}, 消息数: ${localHistory.length}`
);
return localHistory;
}
// 3. 本地没有数据,尝试从服务端获取
console.log(`本地无聊天记录,尝试从服务端获取,roleId: ${roleId}`);
logger.log(`本地无聊天记录,尝试从服务端获取,roleId: ${roleId}`);
const serverSuccess = await this.fetchServerChatHistory(roleId);
if (!serverSuccess) {
console.log(`服务端获取失败,返回空记录,roleId: ${roleId}`);
logger.log(`服务端获取失败,返回空记录,roleId: ${roleId}`);
return [];
}
@@ -447,7 +448,7 @@ export class ChatHistoryManager {
// 6. 保存到本地存储
if (convertedMessages.length > 0) {
this.saveHistory(roleId, convertedMessages);
console.log(
logger.log(
`从服务端获取并保存聊天记录,roleId: ${roleId}, 消息数: ${convertedMessages.length}`
);
}