From a02027e7c9d2a14c5fcadb0ad5184f43a434db08 Mon Sep 17 00:00:00 2001 From: XiaoFeng <117837368+Fzhiyu1@users.noreply.github.com> Date: Mon, 12 Jan 2026 09:43:12 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E4=B8=AD=E6=AD=A2=E6=97=B6=20Promise=20=E6=8C=82=E8=B5=B7?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 completeCallback 实例变量保存完成回调 - abort() 中先标记 hasCompleted 防止 onClose 重复触发 - abort() 中主动调用完成回调以结束 Promise --- src/services/dialogService.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/services/dialogService.ts b/src/services/dialogService.ts index 515fa4b..8fb1414 100644 --- a/src/services/dialogService.ts +++ b/src/services/dialogService.ts @@ -96,6 +96,7 @@ export class DialogSession { private hasCompleted = false; // 标记是否已收到 complete 事件 private segments: MessageSegment[] = []; private currentTextSegment: MessageSegment | null = null; + private completeCallback: ((segments: MessageSegment[]) => void) | null = null; // 保存完成回调,用于 abort 时触发 constructor(extensionPath: string, existingTaskId?: string) { // 支持复用现有 taskId(用于 Plan 模式确认后继续执行) @@ -337,6 +338,7 @@ export class DialogSession { this.accumulatedText = ''; this.segments = []; this.currentTextSegment = null; + this.completeCallback = callbacks.onComplete || null; // 保存完成回调,用于 abort 时触发 const config = getConfig(); @@ -842,13 +844,25 @@ export class DialogSession { * 中止当前对话 */ abort(): void { + // 先标记完成,防止 onClose 重复触发 + const wasActive = this.isActive; + this.hasCompleted = true; + this.isActive = false; + if (this.sseController) { this.sseController.abort(); this.sseController = null; } - this.isActive = false; userInteractionManager.cancelAll(); + // 如果之前是活跃状态,触发完成回调以结束 Promise + if (wasActive && this.completeCallback) { + this.finalizeTextSegment(); + console.log('[DialogSession] abort 触发完成回调'); + this.completeCallback(this.segments); + this.completeCallback = null; + } + // 通知后端停止处理 stopDialog(this.taskId).catch(err => { console.warn('[DialogSession] 停止对话请求失败:', err);