Skip to content

Build and run agents you can see, understand and trust.

fix(webui): reset confirmation state between pending tool calls (#2243)

qbc3d2fca31bd48
diff --git a/examples/web_ui/frontend/src/components/chat/ChatContent.tsx b/examples/web_ui/frontend/src/components/chat/ChatContent.tsx
index 40061e19f..4ed92bc4d 100644
--- a/examples/web_ui/frontend/src/components/chat/ChatContent.tsx
+++ b/examples/web_ui/frontend/src/components/chat/ChatContent.tsx
@@ -52,7 +52,7 @@ interface ChatContentProps {
confirm: boolean,
replyId: string,
rules?: ToolCallBlock['suggested_rules'],
- ) => void;
+ ) => Promise<void>;
autoComplete?: (input: string) => string | null;
className?: string;
/** Called when the user clicks the stop button. */
@@ -179,15 +179,16 @@ const ChatContentComponent: React.FC<ChatContentProps> = ({
>
{toConfirmedToolCalls.length > 0 ? (
<ConfirmCard
+ key={`${toConfirmedToolCalls[0].replyId}:${toConfirmedToolCalls[0].toolCall.id}`}
toolCall={toConfirmedToolCalls[0].toolCall}
- onUserConfirm={async (confirm, rules) => {
+ onUserConfirm={(confirm, rules) =>
onUserConfirm(
toConfirmedToolCalls[0].toolCall,
confirm,
toConfirmedToolCalls[0].replyId,
rules,
- );
- }}
+ )
+ }
/>
) : (
footerSlot
diff --git a/examples/web_ui/frontend/src/components/chat/ConfirmCard.tsx b/examples/web_ui/frontend/src/components/chat/ConfirmCard.tsx
index 4b201eedd..c434a9953 100644
--- a/examples/web_ui/frontend/src/components/chat/ConfirmCard.tsx
+++ b/examples/web_ui/frontend/src/components/chat/ConfirmCard.tsx
@@ -1,6 +1,6 @@
import type { ToolCallBlock } from '@agentscope-ai/agentscope/message';
import { ChevronRight } from 'lucide-react';
-import { useEffect, useState } from 'react';
+import { useCallback, useEffect, useRef, useState } from 'react';
import { getDisplayName, renderConfirmBody } from './tool-renderers';
import { Button } from '@/components/ui/button';
@@ -11,6 +11,9 @@ import { cn } from '@/lib/utils';
type SelectOption = 'yes' | 'yes_with_rule' | 'no';
+const OPTIONS_WITH_SUGGESTED_RULES: SelectOption[] = ['yes', 'yes_with_rule', 'no'];
+const OPTIONS_WITHOUT_SUGGESTED_RULES: SelectOption[] = ['yes', 'no'];
+
export function ConfirmCard({
toolCall,
onUserConfirm,
@@ -20,19 +23,27 @@ export function ConfirmCard({
}) {
const { t } = useTranslation();
const hasSuggestedRules = !!toolCall.suggested_rules?.length;
- const options: SelectOption[] = hasSuggestedRules
- ? ['yes', 'yes_with_rule', 'no']
- : ['yes', 'no'];
+ const options = hasSuggestedRules
+ ? OPTIONS_WITH_SUGGESTED_RULES
+ : OPTIONS_WITHOUT_SUGGESTED_RULES;
const [selected, setSelected] = useState<SelectOption>('yes');
- const [hasConfirmed, sethasConfirmed] = useState<boolean>(false);
+ const [hasConfirmed, setHasConfirmed] = useState<boolean>(false);
+ const submittingRef = useRef(false);
- const handleConfirm = async (confirm: boolean, rules?: ToolCallBlock['suggested_rules']) => {
- if (hasConfirmed) return;
- sethasConfirmed(true);
- onUserConfirm(confirm, rules).catch(() => {
- sethasConfirmed(false);
- });
- };
+ const handleConfirm = useCallback(
+ async (confirm: boolean, rules?: ToolCallBlock['suggested_rules']) => {
+ if (submittingRef.current) return;
+ submittingRef.current = true;
+ setHasConfirmed(true);
+ try {
+ await onUserConfirm(confirm, rules);
+ } catch {
+ submittingRef.current = false;
+ setHasConfirmed(false);
+ }
+ },
+ [onUserConfirm],
+ );
useEffect(() => {
const handleKeyDown = async (e: KeyboardEvent) => {
@@ -59,7 +70,7 @@ export function ConfirmCard({
window.addEventListener('keydown', handleKeyDown);
return () => window.removeEventListener('keydown', handleKeyDown);
- }, [onUserConfirm, selected, options]);
+ }, [handleConfirm, selected, options, toolCall.suggested_rules]);
return (
<div className="bg-muted ring ring-border rounded-[28px] w-full px-6 py-5 space-y-4 text-sm overflow-hidden">
diff --git a/examples/web_ui/frontend/src/components/chat/SubagentHitlCard.tsx b/examples/web_ui/frontend/src/components/chat/SubagentHitlCard.tsx
index 0b0e09aa4..bb345a301 100644
--- a/examples/web_ui/frontend/src/components/chat/SubagentHitlCard.tsx
+++ b/examples/web_ui/frontend/src/components/chat/SubagentHitlCard.tsx
@@ -29,7 +29,7 @@ export function SubagentHitlCard({
toolCall: ToolCallBlock,
confirm: boolean,
rules?: ToolCallBlock['suggested_rules'],
- ) => void;
+ ) => Promise<void>;
}) {
const { t } = useTranslation();
const toolCalls = entry.event.tool_calls ?? [];
@@ -47,9 +47,7 @@ export function SubagentHitlCard({
<ConfirmCard
key={toolCall.id}
toolCall={toolCall}
- onUserConfirm={async (confirm, rules) =>
- onConfirm(toolCall, confirm, rules)
- }
+ onUserConfirm={(confirm, rules) => onConfirm(toolCall, confirm, rules)}
/>
))}
</div>
diff --git a/examples/web_ui/frontend/src/hooks/useMessages.ts b/examples/web_ui/frontend/src/hooks/useMessages.ts
index 781107d23..a816f9089 100644
--- a/examples/web_ui/frontend/src/hooks/useMessages.ts
+++ b/examples/web_ui/frontend/src/hooks/useMessages.ts
@@ -195,11 +195,24 @@ export function useMessages(
return;
}
if (event.type === EventType.REPLY_START) {
- audioManager?.stopAllPlayback();
const e = event as ReplyStartEvent;
- const msg = AssistantMsg({ id: e.reply_id, name: e.name, content: [] });
- msgsRef.current = [...msgsRef.current, msg];
- currentReplyRef.current = msg;
+ // A continuation (the run resuming after a confirmation or an
+ // external execution result) re-emits REPLY_START with the
+ // *same* reply_id. Re-point at the existing reply instead of
+ // appending a second msg under that id: a duplicate would
+ // collide on React keys, strand the original bubble in its
+ // running state, and hide any still-pending confirmation card
+ // — those are read off the tail msg, which would be the empty
+ // duplicate.
+ const existing = msgsRef.current.find((m) => m.id === e.reply_id);
+ if (existing) {
+ currentReplyRef.current = existing;
+ } else {
+ audioManager?.stopAllPlayback();
+ const msg = AssistantMsg({ id: e.reply_id, name: e.name, content: [] });
+ msgsRef.current = [...msgsRef.current, msg];
+ currentReplyRef.current = msg;
+ }
clearInterruptTimer();
setPhase('streaming');
} else if (event.type === EventType.REPLY_END) {
@@ -399,6 +412,7 @@ export function useMessages(
});
} catch (e) {
setError(e as Error);
+ throw e;
}
},
[agentId, sessionId],
@@ -480,9 +494,6 @@ export function useMessages(
],
};
- // Optimistically clear; the backend's clear event re-confirms.
- setSubagentHitl((prev) => prev.filter((x) => hitlKey(x) !== hitlKey(entry)));
-
try {
// Post to the leader front door — backend routes to the
// worker session (§3.6). Do NOT address the worker here.
@@ -493,7 +504,25 @@ export function useMessages(
});
} catch (e) {
setError(e as Error);
+ // Rethrow so the card can re-enable itself and be retried.
+ throw e;
}
+
+ // Drop only the call just answered — an entry can carry several
+ // pending tool calls, and clearing the whole entry would take the
+ // unanswered siblings' cards down with it. The backend's clear
+ // event removes whatever is left.
+ setSubagentHitl((prev) =>
+ prev.flatMap((x) => {
+ if (hitlKey(x) !== hitlKey(entry)) return [x];
+ const remaining = (x.event.tool_calls ?? []).filter(
+ (tc) => tc.id !== toolCall.id,
+ );
+ return remaining.length > 0
+ ? [{ ...x, event: { ...x.event, tool_calls: remaining } }]
+ : [];
+ }),
+ );
},
[agentId, sessionId],
);