From b662d25c9c3dc7bc0b61e75dac7e6201dd8c198b Mon Sep 17 00:00:00 2001 From: Roe-xin Date: Wed, 31 Dec 2025 10:18:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=B0=86=20Ask=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E7=9A=84=E5=B7=A5=E5=85=B7=E7=A1=AE=E8=AE=A4=E4=BB=8E?= =?UTF-8?q?=E5=BC=B9=E7=AA=97=E6=94=B9=E4=B8=BA=E5=86=85=E5=B5=8C=E8=81=8A?= =?UTF-8?q?=E5=A4=A9=E5=8D=A1=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 主要修改 ### dialogService.ts - 移除 `vscode.window.showWarningMessage` 弹窗 - 将工具确认改为添加 question 类型的 segment - 使用 `userInteractionManager.handleAskUser` 等待用户回答 - 生成唯一的 askId: `tool_confirm_{confirmId}` ### userInteraction.ts - 导入 `submitToolConfirm` 方法 - 在 `submitUserAnswer` 中识别工具确认类型的 askId - 根据用户选择("确认执行" / "取消")调用对应的 API ## 用户体验改进 - 工具确认问题自然融入对话流程 - 用户可以看到历史确认记录 - 非阻塞式交互,体验更流畅 --- src/services/dialogService.ts | 74 ++++++++++++++++++++++++--------- src/services/userInteraction.ts | 53 ++++++++++++++++------- 2 files changed, 93 insertions(+), 34 deletions(-) diff --git a/src/services/dialogService.ts b/src/services/dialogService.ts index ec4551a..4d304f7 100644 --- a/src/services/dialogService.ts +++ b/src/services/dialogService.ts @@ -301,30 +301,64 @@ export class DialogSession { onToolConfirm: async (data: ToolConfirmEvent) => { console.log('[DialogSession] onToolConfirm:', data.toolName, data.confirmId); - // 调用回调通知 UI 显示确认对话框 + // 结束当前文本段落 + this.finalizeTextSegment(); + + // 生成工具描述 + const toolDescription = this.getToolDescription(data.toolName, data.toolInput); + + // 构建问题文本 + const toolNameMap: Record = { + 'file_write': '写入文件', + 'file_delete': '删除文件', + 'syntax_check': '语法检查', + 'simulation': '运行仿真' + }; + const toolDisplayName = toolNameMap[data.toolName] || data.toolName; + const question = `确认执行操作:${toolDisplayName}\n\n${toolDescription}`; + + // 生成唯一的 askId + const askId = `tool_confirm_${data.confirmId}`; + + // 添加问题段落到聊天界面 + this.segments.push({ + type: 'question', + askId: askId, + question: question, + options: ['确认执行', '取消'] + }); + + // 实时发送段落更新 + callbacks.onSegmentUpdate?.(this.segments); + + // 调用回调通知 UI callbacks.onToolConfirm?.(data.confirmId, data.toolName, data.toolInput); - // 使用 VSCode 快速选择框显示确认对话框 - const toolDescription = this.getToolDescription(data.toolName, data.toolInput); - const result = await vscode.window.showWarningMessage( - `确认执行操作: ${data.toolName}`, - { modal: true, detail: toolDescription }, - '确认执行', - '取消' - ); - - const approved = result === '确认执行'; - console.log('[DialogSession] 用户确认结果:', approved); - - // 发送确认响应到后端 + // 使用 userInteractionManager 等待用户回答 try { - await submitToolConfirm({ - confirmId: data.confirmId, - taskId: this.taskId, - approved - }); + await userInteractionManager.handleAskUser( + { + askId: askId, + question: question, + options: ['确认执行', '取消'] + } as AskUserEvent, + this.taskId + ); + + // 注意:用户回答后,需要在 receiveAnswer 中处理 tool_confirm 类型的 askId + // 这里不直接调用 submitToolConfirm,而是在 userInteractionManager 中统一处理 } catch (error) { - console.error('[DialogSession] 发送确认响应失败:', error); + console.error('[DialogSession] 处理工具确认失败:', error); + // 如果出错,默认取消执行 + try { + await submitToolConfirm({ + confirmId: data.confirmId, + taskId: this.taskId, + approved: false + }); + } catch (submitError) { + console.error('[DialogSession] 发送取消响应失败:', submitError); + } } }, diff --git a/src/services/userInteraction.ts b/src/services/userInteraction.ts index f5e9a21..0879224 100644 --- a/src/services/userInteraction.ts +++ b/src/services/userInteraction.ts @@ -3,7 +3,7 @@ * 处理 ask_user 事件,通过 WebView 显示问题并收集用户回答 */ import * as vscode from 'vscode'; -import { submitAnswer } from './apiClient'; +import { submitAnswer, submitToolConfirm } from './apiClient'; import type { AskUserEvent, AnswerRequest } from '../types/api'; /** @@ -107,21 +107,46 @@ export class UserInteractionManager { taskId: string, answer: string ): Promise { - const request: AnswerRequest = { - askId, - taskId, - customInput: answer - }; + // 检查是否是工具确认类型的问题 + if (askId.startsWith('tool_confirm_')) { + // 提取 confirmId + const confirmId = parseInt(askId.replace('tool_confirm_', '')); + const approved = answer === '确认执行'; - try { - const response = await submitAnswer(request); - if (!response.success) { - throw new Error(response.error || '提交回答失败'); + console.log(`[UserInteraction] 提交工具确认: confirmId=${confirmId}, approved=${approved}`); + + try { + const response = await submitToolConfirm({ + confirmId, + taskId, + approved + }); + if (!response.success) { + throw new Error(response.error || '提交工具确认失败'); + } + console.log(`[UserInteraction] 工具确认已提交: confirmId=${confirmId}`); + } catch (error) { + console.error(`[UserInteraction] 提交工具确认失败: confirmId=${confirmId}`, error); + throw error; + } + } else { + // 普通问题回答 + const request: AnswerRequest = { + askId, + taskId, + customInput: answer + }; + + try { + const response = await submitAnswer(request); + if (!response.success) { + throw new Error(response.error || '提交回答失败'); + } + console.log(`[UserInteraction] 回答已提交: askId=${askId}`); + } catch (error) { + console.error(`[UserInteraction] 提交回答失败: askId=${askId}`, error); + throw error; } - console.log(`[UserInteraction] 回答已提交: askId=${askId}`); - } catch (error) { - console.error(`[UserInteraction] 提交回答失败: askId=${askId}`, error); - throw error; } }