实现聊天记录本地保存
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { GoogleGenAI } from "@google/genai";
|
||||
import { RoleConfig } from "./RoleConfig";
|
||||
import { ChatHistoryManager } from "./ChatHistoryManager";
|
||||
|
||||
// API配置
|
||||
const API_CONFIG = {
|
||||
@@ -39,15 +40,37 @@ export class ChatAIService {
|
||||
private createOrGetChat(roleId: number): any {
|
||||
if (!this.chatInstances.has(roleId)) {
|
||||
const systemInstruction = RoleConfig.getRoleInstruction(roleId);
|
||||
const chat = this.ai.chats.create({
|
||||
model: API_CONFIG.model,
|
||||
config: {
|
||||
temperature: API_CONFIG.temperature,
|
||||
systemInstruction: systemInstruction
|
||||
},
|
||||
});
|
||||
|
||||
// 从本地加载历史记录
|
||||
const savedHistory = ChatHistoryManager.Instance.loadHistory(roleId);
|
||||
let chat;
|
||||
if(savedHistory && savedHistory.length > 0) {
|
||||
chat = this.ai.chats.create({
|
||||
model: API_CONFIG.model,
|
||||
config: {
|
||||
temperature: API_CONFIG.temperature,
|
||||
systemInstruction: systemInstruction
|
||||
},
|
||||
history: savedHistory
|
||||
});
|
||||
}else{
|
||||
chat = this.ai.chats.create({
|
||||
model: API_CONFIG.model,
|
||||
config: {
|
||||
temperature: API_CONFIG.temperature,
|
||||
systemInstruction: systemInstruction
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
this.chatInstances.set(roleId, chat);
|
||||
console.log(`Created new chat instance for role ${roleId}`);
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -83,6 +106,19 @@ export class ChatAIService {
|
||||
|
||||
if (response && response.text) {
|
||||
console.log(`Response from role ${roleId}:`, response.text);
|
||||
|
||||
// 保存用户消息
|
||||
ChatHistoryManager.Instance.appendMessage(roleId, {
|
||||
role: "user",
|
||||
parts: [{ text: message }]
|
||||
});
|
||||
|
||||
// 保存AI回复
|
||||
ChatHistoryManager.Instance.appendMessage(roleId, {
|
||||
role: "model",
|
||||
parts: [{ text: response.text }]
|
||||
});
|
||||
|
||||
return response.text;
|
||||
} else {
|
||||
console.warn(`Empty response from role ${roleId}`);
|
||||
@@ -101,8 +137,10 @@ export class ChatAIService {
|
||||
public clearChatHistory(roleId: number): void {
|
||||
if (this.chatInstances.has(roleId)) {
|
||||
this.chatInstances.delete(roleId);
|
||||
console.log(`Cleared chat history for role ${roleId}`);
|
||||
}
|
||||
// 清除本地存储的历史
|
||||
ChatHistoryManager.Instance.clearHistory(roleId);
|
||||
console.log(`Cleared chat history for role ${roleId}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,6 +148,8 @@ export class ChatAIService {
|
||||
*/
|
||||
public clearAllChatHistory(): void {
|
||||
this.chatInstances.clear();
|
||||
// 清除本地存储的所有历史
|
||||
ChatHistoryManager.Instance.clearAllHistory();
|
||||
console.log("Cleared all chat histories");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user