Commit a8a7598d by sunqi02 Committed by 胡悦

feat(message-gate): implement actionable dispatch to AgentHub

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent ee998e28
...@@ -319,7 +319,44 @@ class MessageGate: ...@@ -319,7 +319,44 @@ class MessageGate:
bot_id_mapping: Dict[str, str], bot_id_mapping: Dict[str, str],
) -> None: ) -> None:
"""actionable 分支:分发到 AgentHub。""" """actionable 分支:分发到 AgentHub。"""
pass query = decision.rewritten_query or ""
if not query:
logger.warning("[MessageGate] session=%s actionable 但无 query,跳过", session_id)
return
# 提取 file_url(兼容 image 类型)
file_url = None
for m in pending:
if m.get("msg_type") == "image" and m.get("msg_content"):
file_url = m["msg_content"]
break
async def _run():
try:
await self.agent_hub.process_message(
session_id=session_id,
room_id=room_id,
user_id=user_id,
user_query=query,
file_url=file_url,
allowed_agent_codes=None, # Phase 1 简化:不传
bot_id_mapping=bot_id_mapping,
)
except asyncio.CancelledError:
logger.info(
"[MessageGate] session=%s 任务被取消",
session_id,
)
raise
except Exception:
logger.exception(
"[MessageGate] session=%s AgentHub.process_message 失败",
session_id,
)
task = asyncio.create_task(_run())
self._running_tasks[session_id] = task
task.add_done_callback(lambda t: self._running_tasks.pop(session_id, None))
async def close(self) -> None: async def close(self) -> None:
"""清理资源(Phase 1 简化版)。""" """清理资源(Phase 1 简化版)。"""
......
...@@ -147,4 +147,39 @@ async def test_classify_actionable_with_rewrite(message_gate_with_deps): ...@@ -147,4 +147,39 @@ async def test_classify_actionable_with_rewrite(message_gate_with_deps):
agent_codes=["11"], agent_codes=["11"],
) )
assert decision.intent == "actionable" assert decision.intent == "actionable"
assert decision.rewritten_query == "请总结这个文件"
\ No newline at end of file \ No newline at end of file
assert decision.rewritten_query == "请总结这个文件"
@pytest.mark.asyncio
async def test_actionable_dispatches_to_agent_hub(message_gate_with_deps):
"""actionable 分支应创建 asyncio.Task 调 AgentHub。"""
gate = message_gate_with_deps
# 让 process_message 慢一点,避免 task 在 sleep(0.1) 内完成被 done_callback 清理
async def _slow_process(*args, **kwargs):
await asyncio.sleep(0.5)
gate.agent_hub.process_message = AsyncMock(side_effect=_slow_process)
from app.infrastructure.session_store import SessionStore
from app.infrastructure.session_store import clear_default_store_for_tests
clear_default_store_for_tests()
store = SessionStore()
await gate._handle_actionable(
session_id="sess_001",
room_id="r1",
user_id="u1",
decision=GateDecision(intent="actionable", rewritten_query="请分析文件"),
pending=[{"msg_id": "m1", "msg_type": "file", "msg_content": "url",
"sender_id": "u1", "msg_time": "t"}],
bot_id_mapping={"main": "bot-001"},
)
# 等待 task 启动
await asyncio.sleep(0.1)
assert "sess_001" in gate._running_tasks
# 等 task 完成
task = gate._running_tasks["sess_001"]
await task
gate.agent_hub.process_message.assert_awaited_once()
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!