JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
Refactor A2A Manager and Enhance Protocol Definitions
NickFuryXXX133a5f6aedf8
diff --git a/jiuwenswarm/agents/harness/common/tools/a2a_outbound_tools.py b/jiuwenswarm/agents/harness/common/tools/a2a_outbound_tools.pyindex d816f2c05b..09146c761f 100644--- a/jiuwenswarm/agents/harness/common/tools/a2a_outbound_tools.py+++ b/jiuwenswarm/agents/harness/common/tools/a2a_outbound_tools.py@@ -183,7 +183,10 @@ class A2AOutboundToolkit:"properties": {"query": {"type": "string",- "description": "Optional task keywords for ranking. Use an empty string to list the callable catalog.",+ "description": (+ "Optional task keywords for ranking. Use an empty "+ "string to list the callable catalog."+ ),"default": "",},"required_skills": {diff --git a/jiuwenswarm/gateway/a2a_manager/manager.py b/jiuwenswarm/gateway/a2a_manager/manager.pyindex cec2331c93..c2178550cf 100644--- a/jiuwenswarm/gateway/a2a_manager/manager.py+++ b/jiuwenswarm/gateway/a2a_manager/manager.py@@ -464,7 +464,7 @@ class A2AManager:):propagate_cancellation = Trueexcept Exception: # noqa: BLE001- pass+ logger.debug("a2a.ingress start task cleanup failed", exc_info=True)try:await channel.stop()finally:diff --git a/jiuwenswarm/gateway/a2a_manager/outbound/dispatcher.py b/jiuwenswarm/gateway/a2a_manager/outbound/dispatcher.pyindex 03dc7f6e2c..1a111d4307 100644--- a/jiuwenswarm/gateway/a2a_manager/outbound/dispatcher.py+++ b/jiuwenswarm/gateway/a2a_manager/outbound/dispatcher.py@@ -236,7 +236,7 @@ class A2AOutboundDispatcher:):terms.add(token)if all("\u3400" <= char <= "\u9fff" for char in token) and len(token) > 2:- terms.update(token[index : index + 2] for index in range(len(token) - 1))+ terms.update(token[index:index + 2] for index in range(len(token) - 1))return termsasync def dispatch(diff --git a/jiuwenswarm/gateway/channel_manager/channel_manager.py b/jiuwenswarm/gateway/channel_manager/channel_manager.pyindex e1137fc525..ff57ee9754 100644--- a/jiuwenswarm/gateway/channel_manager/channel_manager.py+++ b/jiuwenswarm/gateway/channel_manager/channel_manager.py@@ -102,7 +102,7 @@ class ChannelManager(ABC):)if not self._get_channel_by_id(msg.channel_id):logger.info(f"[ChannelManager] Channel: {msg.channel_id} closed, cancel this user message.")- return+ return Nonefrom jiuwenswarm.common.schema.message import ReqMethoddiff --git a/jiuwenswarm/gateway/message_handler/message_handler.py b/jiuwenswarm/gateway/message_handler/message_handler.pyindex 674cf3d2b9..93c9a0fc47 100644--- a/jiuwenswarm/gateway/message_handler/message_handler.py+++ b/jiuwenswarm/gateway/message_handler/message_handler.py@@ -439,6 +439,16 @@ class MessageHandler(ABC):else:waiter.set_exception(error)+ @staticmethod+ def _is_external_channel_cancel(msg: "Message | None") -> bool:+ from jiuwenswarm.common.schema.message import ReqMethod++ return (+ msg is not None+ and msg.channel_id == "a2a"+ and msg.req_method == ReqMethod.CHAT_CANCEL+ )+def _schedule_external_channel_cancel(self, msg: "Message") -> None:async def cancel_and_finish() -> None:try:@@ -4581,11 +4591,7 @@ class MessageHandler(ABC):breakexcept Exception as exc:external_cancel_error = exc- if (- msg is not None- and msg.channel_id == "a2a"- and msg.req_method == ReqMethod.CHAT_CANCEL- ):+ if self._is_external_channel_cancel(msg):logger.exception("[MessageHandler] A2A cancel preprocessing failed: id=%s",msg.id,@@ -4593,12 +4599,9 @@ class MessageHandler(ABC):continueraisefinally:- if (- msg is not None- and msg.channel_id == "a2a"- and msg.req_method == ReqMethod.CHAT_CANCEL- and not external_cancel_handed_off- ):+ if self._is_external_channel_cancel(+ msg+ ) and not external_cancel_handed_off:self._finish_external_channel_cancel(msg.id,external_cancel_errordiff --git a/tests/unit_tests/gateway/test_a2a_outbound_dispatcher.py b/tests/unit_tests/gateway/test_a2a_outbound_dispatcher.pyindex b8407ad9fe..a936e3689e 100644--- a/tests/unit_tests/gateway/test_a2a_outbound_dispatcher.py+++ b/tests/unit_tests/gateway/test_a2a_outbound_dispatcher.py@@ -1275,9 +1275,12 @@ async def test_reverse_rpc_emits_cancel_notification_when_tool_is_canceled(manager = get_acp_output_manager()manager.reset_state()pushes = []+ dispatch_sent = asyncio.Event()async def send_push(wire):pushes.append(wire)+ if wire["body"]["method"] == A2A_TOOL_DISPATCH_TASK:+ dispatch_sent.set()if wire["body"]["method"] == A2A_TOOL_CANCEL_CALL:rpc_id = wire["body"]["id"]manager.complete_jsonrpc_response(@@ -1290,22 +1293,30 @@ async def test_reverse_rpc_emits_cancel_notification_when_tool_is_canceled(A2A_TOOL_DISPATCH_TASK,{"task": "secret"},session_id="s1",+ timeout=1.0,log_params=False,cancel_method=A2A_TOOL_CANCEL_CALL,))- while not pushes:- await asyncio.sleep(0)- pending.cancel()+ try:+ await asyncio.wait_for(dispatch_sent.wait(), timeout=0.5)+ pending.cancel()- with pytest.raises(asyncio.CancelledError):- await pending- assert [item["body"]["method"] for item in pushes] == [- A2A_TOOL_DISPATCH_TASK,- A2A_TOOL_CANCEL_CALL,- ]- assert pushes[1]["body"]["params"]["jsonrpc_id"] == pushes[0]["body"]["id"]- manager.reset_state()+ with pytest.raises(asyncio.CancelledError):+ await pending+ assert [item["body"]["method"] for item in pushes] == [+ A2A_TOOL_DISPATCH_TASK,+ A2A_TOOL_CANCEL_CALL,+ ]+ assert (+ pushes[1]["body"]["params"]["jsonrpc_id"]+ == pushes[0]["body"]["id"]+ )+ finally:+ if not pending.done():+ pending.cancel()+ await asyncio.gather(pending, return_exceptions=True)+ manager.reset_state()@pytest.mark.asyncio