Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(tui): scope tool interactions by reply (#3282)
WuWenbo4203dca9559f
diff --git a/src/agentscope/tui/_ask_user.py b/src/agentscope/tui/_ask_user.pyindex 6b0b91e3b..62caf63d5 100644--- a/src/agentscope/tui/_ask_user.py+++ b/src/agentscope/tui/_ask_user.py@@ -193,8 +193,12 @@ class AskUserUI(Vertical):self,pending: list[tuple[str, str, ToolCallBlock]],) -> None:- previous_id = self._pending[0][2].id if self._pending else None- current_id = pending[0][2].id if pending else None+ previous_id = (+ (self._pending[0][0], self._pending[0][2].id)+ if self._pending+ else None+ )+ current_id = (pending[0][0], pending[0][2].id) if pending else Noneself._pending = pendingself.display = bool(pending)if previous_id != current_id:diff --git a/src/agentscope/tui/_chat.py b/src/agentscope/tui/_chat.pyindex 2864907bf..739f93093 100644--- a/src/agentscope/tui/_chat.py+++ b/src/agentscope/tui/_chat.py@@ -191,8 +191,12 @@ class HitlUI(Vertical):self,pending: list[tuple[str, str, ToolCallBlock]],) -> None:- previous = self._pending[0][2].id if self._pending else None- current = pending[0][2].id if pending else None+ previous = (+ (self._pending[0][0], self._pending[0][2].id)+ if self._pending+ else None+ )+ current = (pending[0][0], pending[0][2].id) if pending else Noneself._pending = pendingif previous != current:self._submitting = False@@ -478,7 +482,7 @@ class ChatUI(Widget):self.show_thinking = show_thinkingself.show_usage = show_usageself.input_enabled = input_enabled- self._dismissed_call_ids: set[str] = set()+ self._dismissed_calls: set[tuple[str, str]] = set()def compose(self) -> ComposeResult:yield MessagesUI(@@ -542,7 +546,7 @@ class ChatUI(Widget):def _pending_tools(self) -> list[tuple[str, str, ToolCallBlock]]:pending: list[tuple[str, str, ToolCallBlock]] = []- pending_ids: set[str] = set()+ pending_calls: set[tuple[str, str]] = set()for message in self._current_messages():if message.role != "assistant" or message.finished_at is not None:continue@@ -551,10 +555,11 @@ class ChatUI(Widget):ToolCallState.ASKING,ToolCallState.SUBMITTED,):- pending_ids.add(block.id)- if block.id not in self._dismissed_call_ids:+ call = (message.id, block.id)+ pending_calls.add(call)+ if call not in self._dismissed_calls:pending.append((message.id, message.name, block))- self._dismissed_call_ids.intersection_update(pending_ids)+ self._dismissed_calls.intersection_update(pending_calls)return pendingdef _latest_running_reply_id(self) -> str | None:@@ -621,8 +626,9 @@ class ChatUI(Widget):@on(HitlUI.Confirmed)def _on_hitl_confirmed(self, event: HitlUI.Confirmed) -> None:- self._dismissed_call_ids.update(- result.tool_call.id for result in event.value.confirm_results+ self._dismissed_calls.update(+ (event.value.reply_id, result.tool_call.id)+ for result in event.value.confirm_results)self._sync_interaction_area()self.post_message(self.Confirmed(event.value))@@ -630,7 +636,9 @@ class ChatUI(Widget):@on(AskUserUI.Submitted)def _on_ask_user_submitted(self, event: AskUserUI.Submitted) -> None:# Dismiss the form locally without changing authoritative messages.- self._dismissed_call_ids.add(event.tool_call_id)+ self._dismissed_calls.add(+ (event.value.reply_id, event.tool_call_id),+ )self._sync_interaction_area()self.post_message(self.ExternalExecutionSubmitted(event.value))diff --git a/tests/tui_test.py b/tests/tui_test.pyindex 927bc2dfb..e4f526117 100644--- a/tests/tui_test.py+++ b/tests/tui_test.py@@ -802,6 +802,55 @@ class ChatUITest(unittest.IsolatedAsyncioTestCase):str(app.query_one("#as-hitl-body", Static).render()),)+ async def test_reused_tool_call_id_stays_pending_in_new_reply(+ self,+ ) -> None:+ """Dismissing one reply must not hide a later call with the same+ provider-generated id."""+ first = AssistantMsg(+ name="agent",+ id="reply-1",+ content=[+ ToolCallBlock(+ id="call-1",+ name="Read",+ input="{}",+ state="asking",+ ),+ ],+ )+ app = _ChatApp([first])+ async with app.run_test() as pilot:+ await pilot.press("enter")+ await pilot.pause()++ chat = app.query_one(ChatUI)+ await chat.set_messages(+ [+ AssistantMsg(+ name="agent",+ id="reply-2",+ content=[+ ToolCallBlock(+ id="call-1",+ name="Bash",+ input="{}",+ state="asking",+ ),+ ],+ ),+ ],+ )+ await pilot.pause()++ hitl = app.query_one(HitlUI)+ self.assertTrue(hitl.display)+ self.assertFalse(app.query_one(OptionList).disabled)+ self.assertIn(+ "Bash",+ str(app.query_one("#as-hitl-body", Static).render()),+ )+async def test_permission_rules_are_embedded_in_always_option(self,) -> None:@@ -1051,6 +1100,64 @@ class ChatUITest(unittest.IsolatedAsyncioTestCase):)await AskUser().check_external_result(result)+ async def test_reused_ask_user_id_loads_the_new_reply(self) -> None:+ """AskUser state is scoped by reply as well as tool-call id."""++ def ask(reply_id: str, question: str) -> AssistantMsg:+ return AssistantMsg(+ name="agent",+ id=reply_id,+ content=[+ ToolCallBlock(+ id="ask-1",+ name="AskUser",+ input=json.dumps(+ {+ "questions": [+ {+ "header": "Choice",+ "question": question,+ "options": [+ {+ "label": "A",+ "description": "Choose A.",+ },+ {+ "label": "B",+ "description": "Choose B.",+ },+ ],+ },+ ],+ },+ ),+ state="submitted",+ ),+ ],+ )++ app = _ChatApp([ask("reply-1", "First question?")])+ async with app.run_test() as pilot:+ await pilot.press("enter")+ await pilot.pause()++ chat = app.query_one(ChatUI)+ await chat.set_messages([ask("reply-2", "Second question?")])+ await pilot.pause()++ ask_user = app.query_one(AskUserUI)+ self.assertTrue(ask_user.display)+ self.assertFalse(app.query_one(OptionList).disabled)+ self.assertIn(+ "Second question?",+ str(+ app.query_one(+ ".as-ask-user-question",+ Static,+ ).render(),+ ),+ )+async def test_invalid_ask_user_input_returns_error_result(self) -> None:observed: list[ChatUI.ExternalExecutionSubmitted] = []