Build and run agents you can see, understand and trust.
Python agentchatbotlarge-language-modelsllmllm-agentmcpmulti-agentmulti-modalreact-agentrealtime-agentvoice-agentvoice-assistant
fix(middleware): start a no-input reply on the agent's own model (#2847)
sxhe2d17d0571bd
diff --git a/src/agentscope/middleware/_model_router.py b/src/agentscope/middleware/_model_router.pyindex cd5adad20..cdd46664c 100644--- a/src/agentscope/middleware/_model_router.py+++ b/src/agentscope/middleware/_model_router.py@@ -7,7 +7,12 @@ from typing import AsyncGenerator, Callable, Sequence, TYPE_CHECKINGfrom ._base import MiddlewareBasefrom .._logging import loggerfrom ..classifier import ChoiceAnswer, ChoiceQuestion, ClassifierModelBase-from ..event import ReplyStartEvent+from ..event import (+ ExternalExecutionResultEvent,+ ReplyStartEvent,+ UserConfirmResultEvent,+ UserInterruptEvent,+)from ..message import Msg, SystemMsg, UserMsgfrom ..model import ChatModelBase@@ -90,8 +95,17 @@ class ModelRouterMiddleware(MiddlewareBase):"""Swap ``agent.model`` for the selected candidate during the reply."""key = await self.get_middleware_key()original_model = agent.model- # Before ReplyStartEvent, reply_id still names the previous reply- new_reply = isinstance(input_kwargs["inputs"], (Msg, list))+ # Mirror Agent._reply_impl, which treats only a HITL event as a+ # resumption: ``None`` starts a new reply, so it must not inherit the+ # previous reply's route while ``reply_id`` still names that reply.+ new_reply = not isinstance(+ input_kwargs["inputs"],+ (+ UserConfirmResultEvent,+ UserInterruptEvent,+ ExternalExecutionResultEvent,+ ),+ )routed = agent.state.middle_context.get(key, {})agent.model = self._models.get(None if new_reply else routed.get(agent.state.reply_id),diff --git a/tests/middleware_model_router_test.py b/tests/middleware_model_router_test.pyindex 6e6bc8b0b..84ab4fcd1 100644--- a/tests/middleware_model_router_test.py+++ b/tests/middleware_model_router_test.py@@ -16,7 +16,10 @@ from agentscope.classifier import (RefusalAnswer,)from agentscope.credential import CredentialBase-from agentscope.event import ModelCallStartEvent, ReplyStartEvent+from agentscope.event import (+ ModelCallStartEvent,+ UserConfirmResultEvent,+)from agentscope.formatter import OpenAIChatFormatterfrom agentscope.message import (Base64Source,@@ -25,8 +28,12 @@ from agentscope.message import (TextBlock,UserMsg,)-from agentscope.middleware import ChatModelCandidate, ModelRouterMiddleware-from agentscope.model import ChatResponse, StructuredResponse+from agentscope.middleware import (+ ChatModelCandidate,+ MiddlewareBase,+ ModelRouterMiddleware,+)+from agentscope.model import ChatModelBase, ChatResponse, StructuredResponseclass _MockClassifier(ClassifierModelBase):@@ -91,6 +98,27 @@ class _MockRoutingChatModel(MockModel):return StructuredResponse(content={"choice": self.outcome})+class _ModelSpy(MiddlewareBase):+ """A middleware nested inside the router that records which model the+ agent exposes each time a reply is entered."""++ def __init__(self) -> None:+ """Start with an empty log."""+ self.seen: list[ChatModelBase] = []++ async def on_reply(+ self,+ agent: Agent,+ input_kwargs: dict,+ next_handler: Any,+ ) -> Any:+ """Record ``agent.model`` before anything else runs, then pass+ through."""+ self.seen.append(agent.model)+ async for event in next_handler(**input_kwargs):+ yield event++class ModelRouterMiddlewareTest(IsolatedAsyncioTestCase):"""Test routing through a full agent reply."""@@ -197,7 +225,7 @@ class ModelRouterMiddlewareTest(IsolatedAsyncioTestCase):)async def test_resumed_reply_keeps_its_route(self) -> None:- """A reply resumed without a ReplyStartEvent reuses its route."""+ """A reply resumed with a HITL event reuses its parked route."""middleware = ModelRouterMiddleware(_MockClassifier(["reasoning"]),self.candidates,@@ -210,18 +238,58 @@ class ModelRouterMiddlewareTest(IsolatedAsyncioTestCase):async def resume(**_: Any) -> Any:active.append(agent.model)- yield ReplyStartEvent(- session_id=agent.state.session_id,- reply_id="another-reply",- name=agent.name,+ # A resumption never emits ReplyStartEvent: the reply it belongs+ # to has already started, so only the model call is streamed.+ yield ModelCallStartEvent(+ reply_id=agent.state.reply_id,+ model_name=agent.model.model,)active.append(agent.model)- async for _ in middleware.on_reply(agent, {"inputs": None}, resume):+ async for _ in middleware.on_reply(+ agent,+ {+ "inputs": UserConfirmResultEvent(+ reply_id=agent.state.reply_id,+ confirm_results=[],+ ),+ },+ resume,+ ):+ pass++ # The resumption keeps the route the parked reply was given+ self.assertListEqual(active, [self.reasoning, self.reasoning])+ self.assertIs(agent.model, self.primary)++ async def test_no_input_reply_does_not_inherit_the_route(+ self,+ ) -> None:+ """A reply started without input is a new reply, so a middleware+ nested inside the router must not see the previous reply's model."""+ middleware = ModelRouterMiddleware(+ _MockClassifier(["reasoning"]),+ self.candidates,+ )+ spy = _ModelSpy()+ agent = Agent(+ name="Friday",+ system_prompt="Help the user.",+ model=self.primary,+ middlewares=[middleware, spy],+ injection_config=InjectionConfig(inject_runtime_state=False),+ )++ async for _ in agent.reply_stream(+ UserMsg(name="user", content=[TextBlock(text="Prove this.")]),+ ):+ pass+ async for _ in agent.reply_stream():pass- # The resumed reply keeps the route, a new one is routed again- self.assertListEqual(active, [self.reasoning, self.primary])+ # Both replies are entered on the agent's own model; the routed model+ # only becomes visible once ReplyStartEvent has been handled.+ self.assertListEqual(spy.seen, [self.primary, self.primary])self.assertIs(agent.model, self.primary)async def test_new_reply_is_gated_by_its_own_routing(self) -> None: