fix: 修复会话中止时 Promise 挂起问题

- 添加 completeCallback 实例变量保存完成回调
- abort() 中先标记 hasCompleted 防止 onClose 重复触发
- abort() 中主动调用完成回调以结束 Promise
This commit is contained in:
XiaoFeng
2026-01-12 09:43:12 +08:00
parent 772b067202
commit a02027e7c9

View File

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