Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(realtime): restart the downlink pump after it crashes (#3732)
konlue9d65f4e11a7f
diff --git a/src/agentscope/agent/_realtime/_agent.py b/src/agentscope/agent/_realtime/_agent.pyindex 1bc8d48b7..d850c39ec 100644--- a/src/agentscope/agent/_realtime/_agent.py+++ b/src/agentscope/agent/_realtime/_agent.py@@ -459,6 +459,7 @@ class RealtimeAgent:"""Surface a crashed downlink instead of a silently dead session."""if task.cancelled() or task.exception() is None:return+ self._downlink = Noneself._connected = Falseself._connected_event.clear()logger.error(diff --git a/tests/realtime_agent_test.py b/tests/realtime_agent_test.pyindex 0b5a5dffc..68c378a3a 100644--- a/tests/realtime_agent_test.py+++ b/tests/realtime_agent_test.py@@ -274,6 +274,26 @@ class QueueSessionModel(ScriptedModel):self.iterator_finished[index].set()+class CrashingSessionModel(ScriptedModel):+ """The first sessions' event streams raise, like a websocket that+ dies mid-frame; later sessions behave and play their script."""++ def __init__(+ self,+ scripts: list[list[Any]],+ crash_sessions: int = 1,+ ) -> None:+ super().__init__(scripts)+ self.crash_sessions = crash_sessions++ async def events(self) -> AsyncIterator[me.ModelEvent]:+ """Crash the stream for the first sessions, then play the script."""+ if self.sessions <= self.crash_sessions:+ raise RuntimeError("provider event stream exploded")+ async for event in super().events():+ yield event++class FakeTransport(TransportBase):"""Emit ``frames`` chunks of silence and record transport calls."""@@ -864,6 +884,45 @@ class RealtimeAgentTest(IsolatedAsyncioTestCase):],)+ async def test_crashed_downlink_pump_is_replaced_on_reconnect(+ self,+ ) -> None:+ """A downlink pump that dies from a provider stream error must be+ replaced: after the crash the next audio reconnects, and the new+ session's events must be pumped instead of silently dropped."""+ model = CrashingSessionModel([[], REPLY_R2])+ agent = RealtimeAgent("Friday", "be brief", model)++ async with agent:+ # The first session's event stream raises and kills the pump.+ for _ in range(500):+ if agent._connected:+ await asyncio.sleep(0.01)+ continue+ if agent._downlink is None or agent._downlink.done():+ break+ await asyncio.sleep(0.01)+ self.assertFalse(agent._connected) # pylint: disable=W0212+ # The crashed task is no longer the live pump.+ self.assertTrue(+ agent._downlink is None or agent._downlink.done(),+ ) # pylint: disable=W0212++ summary = await self._collect(agent, FakeTransport(frames=2))++ # The pump must be live again: not the finished, crashed task.+ self.assertIsNotNone(agent._downlink) # pylint: disable=W0212+ self.assertFalse(agent._downlink.done()) # pylint: disable=W0212++ self.assertEqual(model.sessions, 2)+ self.assertListEqual(+ summary,+ [+ ("user", "Hello"),+ ("reply_end", "completed"),+ ],+ )+async def test_structured_history_is_replayed_on_reconnect(self) -> None:"""A replay-capable provider gets roles, summary, and no fallback."""state = AgentState(