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);