fix: 修复会话中止时 Promise 挂起问题
- 添加 completeCallback 实例变量保存完成回调 - abort() 中先标记 hasCompleted 防止 onClose 重复触发 - abort() 中主动调用完成回调以结束 Promise
This commit is contained in:
@ -96,6 +96,7 @@ export class DialogSession {
|
|||||||
private hasCompleted = false; // 标记是否已收到 complete 事件
|
private hasCompleted = false; // 标记是否已收到 complete 事件
|
||||||
private segments: MessageSegment[] = [];
|
private segments: MessageSegment[] = [];
|
||||||
private currentTextSegment: MessageSegment | null = null;
|
private currentTextSegment: MessageSegment | null = null;
|
||||||
|
private completeCallback: ((segments: MessageSegment[]) => void) | null = null; // 保存完成回调,用于 abort 时触发
|
||||||
|
|
||||||
constructor(extensionPath: string, existingTaskId?: string) {
|
constructor(extensionPath: string, existingTaskId?: string) {
|
||||||
// 支持复用现有 taskId(用于 Plan 模式确认后继续执行)
|
// 支持复用现有 taskId(用于 Plan 模式确认后继续执行)
|
||||||
@ -337,6 +338,7 @@ export class DialogSession {
|
|||||||
this.accumulatedText = '';
|
this.accumulatedText = '';
|
||||||
this.segments = [];
|
this.segments = [];
|
||||||
this.currentTextSegment = null;
|
this.currentTextSegment = null;
|
||||||
|
this.completeCallback = callbacks.onComplete || null; // 保存完成回调,用于 abort 时触发
|
||||||
|
|
||||||
const config = getConfig();
|
const config = getConfig();
|
||||||
|
|
||||||
@ -842,13 +844,25 @@ export class DialogSession {
|
|||||||
* 中止当前对话
|
* 中止当前对话
|
||||||
*/
|
*/
|
||||||
abort(): void {
|
abort(): void {
|
||||||
|
// 先标记完成,防止 onClose 重复触发
|
||||||
|
const wasActive = this.isActive;
|
||||||
|
this.hasCompleted = true;
|
||||||
|
this.isActive = false;
|
||||||
|
|
||||||
if (this.sseController) {
|
if (this.sseController) {
|
||||||
this.sseController.abort();
|
this.sseController.abort();
|
||||||
this.sseController = null;
|
this.sseController = null;
|
||||||
}
|
}
|
||||||
this.isActive = false;
|
|
||||||
userInteractionManager.cancelAll();
|
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 => {
|
stopDialog(this.taskId).catch(err => {
|
||||||
console.warn('[DialogSession] 停止对话请求失败:', err);
|
console.warn('[DialogSession] 停止对话请求失败:', err);
|
||||||
|
|||||||
Reference in New Issue
Block a user