聊天记录服务端+本地混合加载逻辑
This commit is contained in:
@@ -9,7 +9,7 @@ import { EmotionAIService } from "./EmotionAIService";
|
||||
import { ChatService } from "db://assets/Scripts/chat18x/network/services/ChatService";
|
||||
import { DataManager, DataId } from "../data/DataManager";
|
||||
import { GirlData } from "../data/GirlData";
|
||||
import proto from 'db://assets/Scripts/proto/proto.pb.js';
|
||||
import proto from "db://assets/Scripts/proto/proto.pb.js";
|
||||
|
||||
/**
|
||||
* AI聊天服务类
|
||||
@@ -85,20 +85,9 @@ export class ChatAIService {
|
||||
const systemInstruction = RoleConfigLoader.getRoleInstruction(roleId);
|
||||
const config = ApiConfig.Instance.getAIConfig();
|
||||
|
||||
// // 获取聊天数据
|
||||
// await this.reqGetChatMsg(roleId, 1, 20);
|
||||
// const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
// const category = girlData.getGrilCategoryById(roleId);
|
||||
// const ids = girlData.getChatRecordIds(category.toString(), roleId);
|
||||
// for (let id of ids) {
|
||||
// let isAi = girlData.getChatRecordIsAi(category.toString(), roleId, id);
|
||||
// let mgs = girlData.getChatRecordMsg(category.toString(), roleId, id);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
// 从本地加载历史记录
|
||||
const savedHistory = ChatHistoryManager.Instance.loadHistory(roleId);
|
||||
// 使用 ChatHistoryManager 的统一加载方法(优先本地,无则获取服务端)
|
||||
const savedHistory = await ChatHistoryManager.Instance.loadChatHistory(roleId);
|
||||
|
||||
let chat;
|
||||
if (savedHistory && savedHistory.length > 0) {
|
||||
chat = this.ai.chats.create({
|
||||
@@ -115,6 +104,9 @@ export class ChatAIService {
|
||||
},
|
||||
history: savedHistory,
|
||||
});
|
||||
console.log(
|
||||
`Loaded ${savedHistory.length} history messages for role ${roleId}`
|
||||
);
|
||||
} else {
|
||||
chat = this.ai.chats.create({
|
||||
model: config.model,
|
||||
@@ -129,17 +121,10 @@ export class ChatAIService {
|
||||
],
|
||||
},
|
||||
});
|
||||
console.log(`Created new chat instance for role ${roleId}`);
|
||||
}
|
||||
|
||||
this.chatInstances.set(roleId, chat);
|
||||
|
||||
if (savedHistory.length > 0) {
|
||||
console.log(
|
||||
`Loaded ${savedHistory.length} history messages for role ${roleId}`
|
||||
);
|
||||
} else {
|
||||
console.log(`Created new chat instance for role ${roleId}`);
|
||||
}
|
||||
}
|
||||
return this.chatInstances.get(roleId);
|
||||
}
|
||||
@@ -148,10 +133,10 @@ export class ChatAIService {
|
||||
* 设置当前活动的角色ID
|
||||
* @param roleId 角色ID
|
||||
*/
|
||||
public setCurrentRole(roleId: number): void {
|
||||
public async setCurrentRole(roleId: number): Promise<void> {
|
||||
this.currentRoleId = roleId;
|
||||
// 预创建聊天实例
|
||||
this.createOrGetChat(roleId);
|
||||
await this.createOrGetChat(roleId);
|
||||
// 预创建情绪聊天实例
|
||||
EmotionAIService.Instance.ensureEmotionChatExists(roleId);
|
||||
}
|
||||
@@ -197,7 +182,7 @@ export class ChatAIService {
|
||||
}
|
||||
|
||||
try {
|
||||
const chat = this.createOrGetChat(roleId);
|
||||
const chat = await this.createOrGetChat(roleId);
|
||||
const response = await chat.sendMessage({
|
||||
message: message.trim(),
|
||||
});
|
||||
@@ -228,7 +213,7 @@ export class ChatAIService {
|
||||
let aiData = {
|
||||
isAi: true,
|
||||
msg: response.text,
|
||||
};
|
||||
};
|
||||
msgList.push(aiData);
|
||||
this.reqChatMsg(roleId, msgList);
|
||||
|
||||
@@ -237,7 +222,7 @@ export class ChatAIService {
|
||||
roleId,
|
||||
message,
|
||||
response.text
|
||||
).catch(emotionError => {
|
||||
).catch((emotionError) => {
|
||||
console.warn(
|
||||
`Failed to update emotion for role ${roleId}:`,
|
||||
emotionError
|
||||
@@ -337,8 +322,16 @@ export class ChatAIService {
|
||||
// 保存数据
|
||||
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
const category = girlData.getGrilCategoryById(girlId);
|
||||
girlData.setChatTotalCount(category.toString(), girlId, resData.chatTotalCount);
|
||||
girlData.setChatRemainCount(category.toString(), girlId, resData.chatRemainCount);
|
||||
girlData.setChatTotalCount(
|
||||
category.toString(),
|
||||
girlId,
|
||||
resData.chatTotalCount
|
||||
);
|
||||
girlData.setChatRemainCount(
|
||||
category.toString(),
|
||||
girlId,
|
||||
resData.chatRemainCount
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -361,10 +354,18 @@ export class ChatAIService {
|
||||
const girlData = DataManager.I.getDataById<GirlData>(DataId.Girl);
|
||||
const category = girlData.getGrilCategoryById(girlId);
|
||||
girlData.setChatRecord(category.toString(), girlId, resData.msgs);
|
||||
girlData.setChatTotalCount(category.toString(), girlId, resData.chatTotalCount);
|
||||
girlData.setChatRemainCount(category.toString(), girlId, resData.chatRemainCount);
|
||||
girlData.setChatTotalCount(
|
||||
category.toString(),
|
||||
girlId,
|
||||
resData.chatTotalCount
|
||||
);
|
||||
girlData.setChatRemainCount(
|
||||
category.toString(),
|
||||
girlId,
|
||||
resData.chatRemainCount
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 请求数据类型定义
|
||||
|
||||
Reference in New Issue
Block a user