|
| 1 | +"""ChannelRouter: turn a normalized InboundMessage into an agent turn. |
| 2 | +
|
| 3 | +Channel-agnostic — the same path serves webhook, Slack, or any future channel. |
| 4 | +Works for both ACP types: sync agents take the turn via message/send (reply returned |
| 5 | +inline); async/agentic agents via event/send (fire-and-forget, reply arrives on the |
| 6 | +task's message stream). Continuity is free: the InboundMessage's session_key is reused |
| 7 | +as the agentex task name (task/create is get-or-create on name). |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +from dataclasses import dataclass |
| 13 | + |
| 14 | +from src.domain.channels.base import ChannelBinding, InboundMessage |
| 15 | +from src.domain.entities.agents import ACPType |
| 16 | +from src.domain.entities.agents_rpc import ( |
| 17 | + AgentRPCMethod, |
| 18 | + CreateTaskRequestEntity, |
| 19 | + SendEventRequestEntity, |
| 20 | + SendMessageRequestEntity, |
| 21 | +) |
| 22 | +from src.domain.entities.task_messages import ( |
| 23 | + MessageAuthor, |
| 24 | + TaskMessageContentType, |
| 25 | + TextContentEntity, |
| 26 | +) |
| 27 | +from src.domain.use_cases.agents_acp_use_case import AgentsACPUseCase |
| 28 | +from src.utils.logging import make_logger |
| 29 | + |
| 30 | +logger = make_logger(__name__) |
| 31 | + |
| 32 | + |
| 33 | +@dataclass |
| 34 | +class DispatchResult: |
| 35 | + task_id: str |
| 36 | + # Sync agents return their reply inline; async agents reply later on the task |
| 37 | + # stream, so reply is None and the caller can poll /messages if it wants it. |
| 38 | + reply: str | None = None |
| 39 | + |
| 40 | + |
| 41 | +class ChannelRouter: |
| 42 | + def __init__(self, acp_use_case: AgentsACPUseCase): |
| 43 | + self._acp = acp_use_case |
| 44 | + |
| 45 | + async def dispatch( |
| 46 | + self, inbound: InboundMessage, binding: ChannelBinding, acp_type: ACPType |
| 47 | + ) -> DispatchResult: |
| 48 | + session_key = inbound.session_key(binding.agent_name) |
| 49 | + headers = binding.forward_headers or None |
| 50 | + |
| 51 | + task = await self._acp.handle_rpc_request( |
| 52 | + method=AgentRPCMethod.TASK_CREATE, |
| 53 | + params=CreateTaskRequestEntity( |
| 54 | + name=session_key, |
| 55 | + params=binding.params, |
| 56 | + task_metadata={ |
| 57 | + "channel": inbound.channel, |
| 58 | + "route_id": inbound.route_id, |
| 59 | + "peer_id": inbound.peer_id, |
| 60 | + "sender_id": inbound.sender_id, |
| 61 | + }, |
| 62 | + ), |
| 63 | + agent_name=binding.agent_name, |
| 64 | + request_headers=headers, |
| 65 | + ) |
| 66 | + task_id = task.id |
| 67 | + logger.info( |
| 68 | + "[channels] %s route=%s acp=%s -> task %s (session %s)", |
| 69 | + inbound.channel, |
| 70 | + inbound.route_id, |
| 71 | + acp_type.value, |
| 72 | + task_id, |
| 73 | + session_key, |
| 74 | + ) |
| 75 | + |
| 76 | + content = TextContentEntity(author=MessageAuthor.USER, content=inbound.text) |
| 77 | + |
| 78 | + if acp_type == ACPType.SYNC: |
| 79 | + # Sync: message/send carries the turn and returns the reply messages inline. |
| 80 | + messages = await self._acp.handle_rpc_request( |
| 81 | + method=AgentRPCMethod.MESSAGE_SEND, |
| 82 | + params=SendMessageRequestEntity( |
| 83 | + task_id=task_id, content=content, stream=False |
| 84 | + ), |
| 85 | + agent_name=binding.agent_name, |
| 86 | + request_headers=headers, |
| 87 | + ) |
| 88 | + return DispatchResult(task_id=task_id, reply=_agent_text(messages)) |
| 89 | + |
| 90 | + # Async / agentic: event/send delivers the turn; the reply lands on the stream. |
| 91 | + await self._acp.handle_rpc_request( |
| 92 | + method=AgentRPCMethod.EVENT_SEND, |
| 93 | + params=SendEventRequestEntity(task_id=task_id, content=content), |
| 94 | + agent_name=binding.agent_name, |
| 95 | + request_headers=headers, |
| 96 | + ) |
| 97 | + return DispatchResult(task_id=task_id, reply=None) |
| 98 | + |
| 99 | + |
| 100 | +def _agent_text(messages: object) -> str | None: |
| 101 | + """Join the agent-authored text from a sync message/send result.""" |
| 102 | + if not isinstance(messages, list): |
| 103 | + return None |
| 104 | + parts: list[str] = [] |
| 105 | + for m in messages: |
| 106 | + content = getattr(m, "content", None) |
| 107 | + if ( |
| 108 | + content is not None |
| 109 | + and getattr(content, "type", None) == TaskMessageContentType.TEXT |
| 110 | + and getattr(content, "author", None) == MessageAuthor.AGENT |
| 111 | + ): |
| 112 | + text = getattr(content, "content", "") or "" |
| 113 | + if text.strip(): |
| 114 | + parts.append(text.strip()) |
| 115 | + return "\n\n".join(parts) if parts else None |
0 commit comments