Skip to content

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

fix(tui): hide realtime messages that fail to send (#2646)

WuWenbo0d0d86e2755f
diff --git a/src/agentscope/tui/_launcher.py b/src/agentscope/tui/_launcher.py
index 6f6286c57..2edc6f5b2 100644
--- a/src/agentscope/tui/_launcher.py
+++ b/src/agentscope/tui/_launcher.py
@@ -279,16 +279,18 @@ class _RealtimeTUI(_ChatAppBase):
async def _send(self, inputs: _RealtimeInput) -> None:
"""Hand the agent everything that is not audio."""
- # An interrupt leaves its mark on the reply it cuts off; the rest
- # is shown as it is submitted, since the agent does not echo it.
- if not isinstance(inputs, UserInterruptEvent):
- await self._publish(inputs)
try:
await self.agent.send(inputs)
# pylint: disable-next=broad-exception-caught
except Exception as error:
logger.exception("Realtime TUI input failed")
self.notify(str(error), title="Agent error", severity="error")
+ else:
+ # An interrupt leaves its mark on the reply it cuts off; the
+ # rest is shown after the agent accepts it, since the agent does
+ # not echo it.
+ if not isinstance(inputs, UserInterruptEvent):
+ await self._publish(inputs)
def _submit(self, msg: Msg) -> None:
self._spawn(self._send(msg))
diff --git a/tests/tui_test.py b/tests/tui_test.py
index 75ac20051..bd2658716 100644
--- a/tests/tui_test.py
+++ b/tests/tui_test.py
@@ -1584,6 +1584,24 @@ class RealtimeLauncherTest(unittest.IsolatedAsyncioTestCase):
expected,
)
+ async def test_failed_typed_turn_is_not_shown(self) -> None:
+ agent = _FakeRealtimeAgent()
+ app = _RealtimeTUI(agent, object(), [], "user")
+ msg = UserMsg(name="user", content="not delivered")
+ error = RuntimeError("send failed")
+ with patch.object(agent, "send", side_effect=error):
+ async with app.run_test(size=(80, 24)):
+ with patch.object(app, "notify") as notify:
+ await app._send(msg)
+
+ self.assertTupleEqual(app.query_one(ChatUI).messages, ())
+
+ notify.assert_called_once_with(
+ "send failed",
+ title="Agent error",
+ severity="error",
+ )
+
async def test_confirmation_and_interrupt_reach_the_agent(self) -> None:
agent = _FakeRealtimeAgent()
app = _RealtimeTUI(