fix:修复手动暂停导致历史记录重复的bug

- 统一对话历史保存入口,移除手动暂停时的重复写入逻辑。手动暂停仅保留一条历史记录,并附带中止标记。
This commit is contained in:
Roe-xin
2026-03-20 18:54:21 +08:00
parent 4e06d08106
commit 394faa4328
2 changed files with 15 additions and 3 deletions

View File

@ -127,6 +127,7 @@ export class DialogSession {
private toolContext: ToolExecutorContext; private toolContext: ToolExecutorContext;
private accumulatedText = ""; private accumulatedText = "";
private isActive = false; private isActive = false;
private wasAbortedByUser = false;
private hasCompleted = false; // 标记是否已收到 complete 事件 private hasCompleted = false; // 标记是否已收到 complete 事件
private segments: MessageSegment[] = []; private segments: MessageSegment[] = [];
private currentTextSegment: MessageSegment | null = null; private currentTextSegment: MessageSegment | null = null;
@ -218,6 +219,10 @@ export class DialogSession {
return this.isActive; return this.isActive;
} }
get abortedByUser(): boolean {
return this.wasAbortedByUser;
}
/** /**
* 加载知识图谱数据 * 加载知识图谱数据
* 从 .iccoder/knowledge.json 读取 * 从 .iccoder/knowledge.json 读取
@ -1078,6 +1083,7 @@ export class DialogSession {
abort(): void { abort(): void {
// 先标记完成,防止 onClose 重复触发 // 先标记完成,防止 onClose 重复触发
const wasActive = this.isActive; const wasActive = this.isActive;
this.wasAbortedByUser = true;
this.hasCompleted = true; this.hasCompleted = true;
this.isActive = false; this.isActive = false;

View File

@ -362,7 +362,11 @@ async function handleUserMessageWithBackend(
.map((s) => s.content) .map((s) => s.content)
.join("\n"); .join("\n");
await historyManager.addAiMessage(textContent, undefined, segments); const finalText = currentSession?.abortedByUser
? `${textContent}\n\n[对话已被用户中止]`
: textContent;
await historyManager.addAiMessage(finalText, undefined, segments);
console.log("[MessageHandler] AI响应已保存到历史记录"); console.log("[MessageHandler] AI响应已保存到历史记录");
} catch (error) { } catch (error) {
console.error("[MessageHandler] 保存AI响应历史失败:", error); console.error("[MessageHandler] 保存AI响应历史失败:", error);
@ -506,9 +510,11 @@ export async function handleUserAnswer(
* 中止当前对话 * 中止当前对话
*/ */
export async function abortCurrentDialog(): Promise<void> { export async function abortCurrentDialog(): Promise<void> {
if (currentSession) { const session = currentSession;
if (false && session) {
// 历史保存统一走 onComplete避免手动中止时重复写入
// 保存当前已有的对话内容 // 保存当前已有的对话内容
const segments = currentSession.getSegments(); const segments = session!.getSegments();
if (segments && segments.length > 0) { if (segments && segments.length > 0) {
try { try {
const historyManager = ChatHistoryManager.getInstance(); const historyManager = ChatHistoryManager.getInstance();