fix: 修复AI询问时选项点击后选中状态丢失的问题

- 添加 answeredQuestions Map 存储已回答问题的状态
- 在重新渲染时恢复选中状态和 answered 类
- 已回答的问题自动隐藏输入框并禁用点击事件
- 确保用户选择在页面更新时保持显示
This commit is contained in:
Roe-xin
2025-12-24 10:01:53 +08:00
parent 5c2ea0f15c
commit 10f0877a5e
9 changed files with 1763 additions and 1258 deletions

View File

@ -37,6 +37,8 @@ export interface DialogCallbacks {
onToolError?: (toolName: string, error: string) => void;
/** 显示问题ask_user */
onQuestion?: (askId: string, question: string, options: string[]) => void;
/** 实时更新段落(流式过程中) */
onSegmentUpdate?: (segments: MessageSegment[]) => void;
/** 对话完成,返回所有段落 */
onComplete?: (segments: MessageSegment[]) => void;
/** 错误 */
@ -155,6 +157,8 @@ export class DialogSession {
this.appendText(data.text);
console.log('[DialogSession] onTextDelta, 累积文本长度:', this.accumulatedText.length);
callbacks.onText?.(this.accumulatedText, true);
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
},
onToolCall: async (data: ToolCallRequest) => {
@ -166,16 +170,22 @@ export class DialogSession {
console.log('[DialogSession] onToolCall: 跳过重复的工具段落:', toolName);
} else {
this.addToolSegment(toolName, 'running');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
}
// 注意:不在这里调用 callbacks.onToolStart避免与 onToolStart 事件重复
try {
await executeToolCall(data, this.toolContext);
this.updateToolSegment(toolName, 'success', '执行完成');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
// 也不调用 callbacks.onToolComplete避免重复
} catch (error) {
const errorMsg = error instanceof Error ? error.message : '未知错误';
this.updateToolSegment(toolName, 'error', errorMsg);
callbacks.onToolError?.(toolName, errorMsg);
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
}
},
@ -187,6 +197,8 @@ export class DialogSession {
console.log('[DialogSession] 跳过重复的工具段落:', data.tool_name);
} else {
this.addToolSegment(data.tool_name, 'running');
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
}
console.log('[DialogSession] segments 数量:', this.segments.length);
callbacks.onToolStart?.(data.tool_name);
@ -195,11 +207,15 @@ export class DialogSession {
onToolComplete: (data) => {
this.updateToolSegment(data.tool_name, 'success', data.result);
callbacks.onToolComplete?.(data.tool_name, data.result);
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
},
onToolError: (data) => {
this.updateToolSegment(data.tool_name, 'error', data.error);
callbacks.onToolError?.(data.tool_name, data.error);
// 实时发送段落更新
callbacks.onSegmentUpdate?.(this.segments);
},
onAskUser: async (data: AskUserEvent) => {
@ -210,6 +226,9 @@ export class DialogSession {
question: data.question,
options: data.options
});
// 实时发送段落更新(包含问题)
callbacks.onSegmentUpdate?.(this.segments);
// 同时调用 onQuestion 用于更新状态栏等
callbacks.onQuestion?.(data.askId, data.question, data.options);
try {
await userInteractionManager.handleAskUser(data, this.taskId);