Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(realtime): include reply ID in interrupt events (#2692)
Weng Qiang7cf31bcb4941
diff --git a/src/agentscope/agent/_realtime/_agent.py b/src/agentscope/agent/_realtime/_agent.pyindex 58dec6712..34c8ce2c9 100644--- a/src/agentscope/agent/_realtime/_agent.py+++ b/src/agentscope/agent/_realtime/_agent.py@@ -480,7 +480,7 @@ class RealtimeAgent:async def interrupt(self) -> None:"""Stop the active reply, as when the user presses stop."""- await self.send(UserInterruptEvent())+ await self.send(UserInterruptEvent(reply_id=self._reply_id))@propertydef last_turn_metrics(self) -> TurnMetrics:@@ -559,7 +559,7 @@ class RealtimeAgent:case ControlFrameType.USER_CONFIRM:await self.send(UserConfirmResultEvent(**frame.data))case ControlFrameType.INTERRUPT:- await self.send(UserInterruptEvent())+ await self.send(UserInterruptEvent(reply_id=self._reply_id))case _:logger.debug("RealtimeAgent: ignoring %s frame", frame.type)diff --git a/tests/realtime_agent_test.py b/tests/realtime_agent_test.pyindex c87173212..c10a78687 100644--- a/tests/realtime_agent_test.py+++ b/tests/realtime_agent_test.py@@ -214,6 +214,15 @@ class FakeTransport(TransportBase):)+class InterruptingTransport(FakeTransport):+ """Sends one INTERRUPT frame once the reply is playing."""++ async def incoming(self) -> AsyncIterator[ControlFrame]:+ """Emit the interrupt after the scripted reply has streamed."""+ await asyncio.sleep(0.02)+ yield ControlFrame(type=ControlFrameType.INTERRUPT)++class EndOnSecondFrameVAD(VADBase):"""Reports the user starting on the first chunk and stopping on thesecond."""@@ -323,6 +332,53 @@ class RealtimeAgentTest(IsolatedAsyncioTestCase):)self.assertEqual(agent.last_turn_metrics.first_audio_played_at, 1.0)+ async def test_interrupt_stops_active_reply(self) -> None:+ """``interrupt()`` cuts the reply in flight to what was heard."""+ model = ScriptedModel([REPLY_R1])+ agent = RealtimeAgent("Friday", "be brief", model)+ transport = FakeTransport(frames=3)++ reply_ends = []+ async with agent, transport:+ async for event in agent.reply_stream(transport):+ if isinstance(event, TextBlockDeltaEvent):+ if event.delta == "老和尚":+ await agent.interrupt()+ elif isinstance(event, ReplyEndEvent):+ reply_ends.append((event.reply_id, event.finished_reason))++ self.assertListEqual(+ reply_ends,+ [("u1", "completed"), ("r1", "interrupted")],+ )+ self.assertEqual(transport.cleared, 1)+ self.assertListEqual(+ [(m.role, m.get_text_content()) for m in agent.state.context],+ [("user", "讲个故事"), ("assistant", "从前有座山山里有座庙")],+ )++ async def test_interrupt_frame_stops_active_reply(self) -> None:+ """An INTERRUPT control frame cuts the reply in flight."""+ model = ScriptedModel([REPLY_R1])+ agent = RealtimeAgent("Friday", "be brief", model)++ async with agent:+ summary = await self._collect(agent, InterruptingTransport(0))++ self.assertListEqual(+ summary,+ [("user", "讲个故事"), ("reply_end", "interrupted")],+ )+ self.assertListEqual(+ model.calls,+ [+ "connect(session=1,td_off=False)",+ "truncate(r1,320ms,'从前有座山山里有座庙')",+ "cancel",+ "close",+ ],+ )+async def test_provider_timeout_reconnects_on_next_audio(self) -> None:"""When the provider closes the session, nothing reconnects untilthe next user audio, which reconnects with the current context."""