聊天相关fix

This commit is contained in:
2025-09-20 20:37:31 +08:00
parent 86282eec31
commit b8a417632a
22 changed files with 2423 additions and 398 deletions
@@ -101,6 +101,10 @@ export class ApiConfig {
}
}
public getTimeout() {
return this.config.timeout;
}
/**
* 验证配置是否有效
*/
+44 -2
View File
@@ -11,6 +11,7 @@ import { DataManager, DataId } from "../data/DataManager";
import { GirlData } from "../data/GirlData";
import proto from "db://assets/Scripts/proto/proto.pb.js";
import { TipsPanel } from "../ui/panels/TipsPanel";
import LanguageUtils from "../../Main/Common/LanguageUtils";
/**
* AI聊天服务类
@@ -48,12 +49,18 @@ export class ChatAIService {
{ config },
true
);
TipsPanel.show(LanguageUtils.getText("chat_error_code_2002"));
throw new Error("AI服务初始化失败:配置无效");
}
try {
this.ai = new GoogleGenAI({ apiKey: config.apiKey });
this.ai = new GoogleGenAI({
apiKey: config.apiKey,
httpOptions: { timeout: ApiConfig.Instance.getTimeout() },
});
} catch (error) {
TipsPanel.show(LanguageUtils.getText("chat_error_code_2001"));
ErrorHandler.Instance.handleError(
error as Error,
ErrorType.API_ERROR,
@@ -102,6 +109,23 @@ export class ChatAIService {
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
],
},
history: savedHistory,
@@ -120,6 +144,23 @@ export class ChatAIService {
category: HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_CIVIC_INTEGRITY,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_NONE,
},
],
},
});
@@ -245,6 +286,7 @@ export class ChatAIService {
return response.text;
} else {
TipsPanel.show(response.promptFeedback.blockReason);
const warningMsg = `AI返回了空响应 (角色ID: ${roleId})`;
ErrorHandler.Instance.handleError(
new Error(warningMsg),
@@ -257,7 +299,7 @@ export class ChatAIService {
} catch (error) {
ErrorHandler.Instance.handleApiError(error, "sendMessage", {
roleId,
message: message.substring(0, 100) + "...",
message: message,
});
TipsPanel.show("api调用失败,请使用vpn并重新启动");
return null;
@@ -7,6 +7,8 @@ import { ChatModel } from "../data/ChatModel";
import { ChatHistoryManager } from "../manager/ChatHistoryManager";
import Utils from "../../Main/Common/Utils";
import { InnerMsgCode } from "../../Main/Config/InnerMsgCode";
import { TipsPanel } from "../ui/panels/TipsPanel";
import LanguageUtils from "../../Main/Common/LanguageUtils";
/**
* 聊天控制器接口 - 定义Panel和Controller之间的通信协议
@@ -188,7 +190,8 @@ export class ChatController {
try {
const roleId = this.chatModel.getCurrentRoleId();
if (!roleId) {
throw new Error("Role ID is not available in ChatModel");
console.error("Role ID is not available in ChatModel");
TipsPanel.show(LanguageUtils.getText("chat_error_code_1004"));
}
// 添加用户消息到模型
@@ -40,7 +40,10 @@ export class EmotionAIService {
const emotionConfig = ApiConfig.Instance.getEmotionAIConfig();
try {
this.emotionAI = new GoogleGenAI({ apiKey: emotionConfig.apiKey });
this.emotionAI = new GoogleGenAI({
apiKey: emotionConfig.apiKey,
httpOptions: { timeout: emotionConfig.timeout },
});
} catch (error) {
ErrorHandler.Instance.handleError(
error as Error,
@@ -109,7 +112,9 @@ export class EmotionAIService {
// 分析历史情绪状态
emotionalState = await this.analyzeEmotionalState(roleId);
if (emotionalState == null) {
emotionalState = VideoEmotion.calm_down;
}
console.log(
`Initial emotional state for role ${roleId}: ${VideoEmotion[emotionalState]}`
);
@@ -185,7 +190,7 @@ export class EmotionAIService {
"角色ID必须是正整数",
roleId
);
return VideoEmotion.calm_down;
return null;
}
if (!message || message.trim() === "") {
@@ -194,7 +199,7 @@ export class EmotionAIService {
"消息内容不能为空",
message
);
return VideoEmotion.calm_down;
return null;
}
try {
@@ -214,14 +219,14 @@ export class EmotionAIService {
{ roleId, message, response },
true
);
return VideoEmotion.calm_down;
return null;
}
} catch (error) {
ErrorHandler.Instance.handleApiError(error, "sendEmotionMessage", {
roleId,
message: message.substring(0, 100) + "...",
});
return VideoEmotion.calm_down;
return null;
}
}
@@ -251,7 +256,7 @@ export class EmotionAIService {
{ roleId },
false
);
return VideoEmotion.calm_down;
return null;
}
}
@@ -376,8 +381,11 @@ export class EmotionAIService {
);
// 发送给情绪AI进行分析
const newEmotion = await this.sendEmotionMessage(roleId, analysisPrompt);
let newEmotion = await this.sendEmotionMessage(roleId, analysisPrompt);
if (newEmotion == null) {
newEmotion = currentEmotion;
}
// 更新并保存情绪状态
this.setCurrentEmotion(roleId, newEmotion);