Skip to content

Commit 79149d2

Browse files
authored
fix(frontend): render interactive tools via registry instead of hardcoded ask_user (#66)
Replace scattered `=== 'ask_user'` checks in useAgentChat and TracePanel with an interactive-tools registry (interactiveTools.ts). Both tool_call and tool_result of interactive tools now render consistently, so the ask_user question/answer pair shows correctly when re-entering a historical session. Adding a new interactive tool is now a one-line registry entry.
1 parent 6461a47 commit 79149d2

3 files changed

Lines changed: 70 additions & 9 deletions

File tree

‎data-agent-frontend/src/composables/useAgentChat.ts‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { ref, shallowRef } from 'vue';
1919
import { streamChat, fetchSessionHistory, type ChatStreamEventType } from '@/api/agent';
20+
import { INTERACTIVE_TOOLS, isInteractiveTool } from '@/utils/interactiveTools';
2021

2122
export interface PendingQuestion {
2223
toolCallId: string;
@@ -178,11 +179,12 @@ export function useAgentChat(initialSessionId?: string) {
178179
},
179180
];
180181

181-
if (event.type === 'tool_call' && event.toolCall?.name === 'ask_user') {
182+
if (event.type === 'tool_call' && isInteractiveTool(event.toolCall?.name)) {
183+
const def = INTERACTIVE_TOOLS[event.toolCall!.name];
182184
pendingQuestion.value = {
183-
toolCallId: event.toolCall.id,
184-
toolName: event.toolCall.name,
185-
question: (event.toolCall.input.question as string) ?? '',
185+
toolCallId: event.toolCall!.id,
186+
toolName: event.toolCall!.name,
187+
question: (event.toolCall!.input[def.questionField] as string) ?? '',
186188
};
187189
msg.isStreaming = false;
188190
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2026 github.com/MaloneTalk
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* 交互工具定义 —— 对应后端一个 throw ToolSuspendException 的工具方法。
21+
* 新增交互表单只需在此注册表加一条配置,无需修改 TracePanel / useAgentChat。
22+
*/
23+
export interface InteractiveToolDef {
24+
/** tool_call 步骤的标签,如 "向用户提问" */
25+
label: string;
26+
/** 从 toolCall.input 中取展示文本的字段名 */
27+
questionField: string;
28+
/** tool_result 步骤的标签,如 "用户回答" */
29+
resultLabel: string;
30+
}
31+
32+
/** 所有交互工具的注册表,key 为工具名(与后端 @Tool.name 一致) */
33+
export const INTERACTIVE_TOOLS: Record<string, InteractiveToolDef> = {
34+
ask_user: {
35+
label: '向用户提问',
36+
questionField: 'question',
37+
resultLabel: '用户回答',
38+
},
39+
};
40+
41+
/** 判断工具名是否为已注册的交互工具 */
42+
export function isInteractiveTool(toolName: string | null | undefined): boolean {
43+
return toolName != null && toolName in INTERACTIVE_TOOLS;
44+
}

‎data-agent-frontend/src/views/chat/components/TracePanel.vue‎

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import { ref, computed } from 'vue';
2020
import type { TraceStep, ChatMessage } from '@/composables/useAgentChat';
2121
import type { ChatStreamEventType } from '@/api/agent';
22+
import { INTERACTIVE_TOOLS, isInteractiveTool } from '@/utils/interactiveTools';
2223
2324
const props = defineProps<{
2425
message: ChatMessage;
@@ -75,20 +76,27 @@
7576
}
7677
7778
function stepLabel(step: TraceStep): string {
78-
if (step.type === 'tool_call' && step.toolCall?.name === 'ask_user') {
79-
return '[向用户提问]';
79+
if (step.type === 'tool_call' && isInteractiveTool(step.toolCall?.name)) {
80+
return `[${INTERACTIVE_TOOLS[step.toolCall!.name].label}]`;
81+
}
82+
if (step.type === 'tool_result' && isInteractiveTool(step.toolResult?.name)) {
83+
return `[${INTERACTIVE_TOOLS[step.toolResult!.name].resultLabel}]`;
8084
}
8185
return `[${getStepRenderer(step.type).label}]`;
8286
}
8387
8488
function stepContent(step: TraceStep): string {
8589
if (step.type === 'tool_call' && step.toolCall) {
86-
if (step.toolCall.name === 'ask_user') {
87-
return (step.toolCall.input.question as string) ?? '等待用户输入...';
90+
if (isInteractiveTool(step.toolCall.name)) {
91+
const def = INTERACTIVE_TOOLS[step.toolCall.name];
92+
return (step.toolCall.input[def.questionField] as string) ?? '等待用户输入...';
8893
}
8994
return `调用工具: ${step.toolCall.name}`;
9095
}
9196
if (step.type === 'tool_result' && step.toolResult) {
97+
if (isInteractiveTool(step.toolResult.name)) {
98+
return step.toolResult.output || '(无内容)';
99+
}
92100
return `工具 ${step.toolResult.name} 返回结果`;
93101
}
94102
if (step.type === 'question') {
@@ -135,7 +143,14 @@
135143
<div class="code-label">Input:</div>
136144
<pre class="code-block">{{ displayMultiline(formatJson(step.toolCall.input)) }}</pre>
137145
</div>
138-
<div v-if="step.type === 'tool_result' && step.toolResult" class="trace-step__code">
146+
<div
147+
v-if="
148+
step.type === 'tool_result' &&
149+
step.toolResult &&
150+
!isInteractiveTool(step.toolResult.name)
151+
"
152+
class="trace-step__code"
153+
>
139154
<div class="code-label">Output:</div>
140155
<pre class="code-block">{{ displayMultiline(step.toolResult.output) }}</pre>
141156
</div>

0 commit comments

Comments
 (0)