Commit 21f3061b by sunqi02

feat(message-gate): scaffold MessageGate service class

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent a0b0fc70
"""MessageGate 服务 - API 层和 AgentHub 之间的意图门控层。
详见设计文档: docs/superpowers/specs/2026-06-01-message-gate-design.md
"""
from __future__ import annotations
import asyncio
import logging
from dataclasses import dataclass, field
from typing import Any, Dict, List, Optional
from app.config.loader import ConfigLoader
from app.hooks.post_reply import PostReplyHook
from app.service.active_session_service import ActiveSessionService
logger = logging.getLogger(__name__)
@dataclass
class GateDecision:
"""MessageGate 评估后输出的决策结果。"""
intent: str # actionable | casual | incomplete | ignore
rewritten_query: Optional[str] = None
reply: Optional[str] = None
cancel: bool = False # 暂未使用,Phase 2 启用
class MessageGate:
"""消息门控层 - 等待/分类/重写/调度。"""
def __init__(
self,
config_loader: ConfigLoader,
session_service: ActiveSessionService,
post_reply_hook: PostReplyHook,
agent_hub: Any, # AgentHub 实例,避免循环 import
main_agent_key: str,
) -> None:
self.config_loader = config_loader
self.session_service = session_service
self.post_reply_hook = post_reply_hook
self.agent_hub = agent_hub
self.main_agent_key = main_agent_key
# 运行时状态
self._session_locks: Dict[str, asyncio.Lock] = {}
self._running_tasks: Dict[str, asyncio.Task] = {}
# 每次 evaluate 调用时由 endpoint 注入的 bot_id_mapping
self._current_bot_id_mapping: Dict[str, str] = {}
def _get_lock(self, session_id: str) -> asyncio.Lock:
if session_id not in self._session_locks:
self._session_locks[session_id] = asyncio.Lock()
return self._session_locks[session_id]
async def evaluate(
self,
session_id: str,
room_id: str,
user_id: str,
bot_id_mapping: Dict[str, str],
messages: List[Dict[str, Any]] = None,
wait_seconds: float = 6.0,
) -> None:
"""评估入口:等待、分类、重写、分发。"""
raise NotImplementedError
\ No newline at end of file \ No newline at end of file
"""MessageGate 服务的单元测试。"""
import pytest
from unittest.mock import MagicMock, AsyncMock
from app.service.message_gate import MessageGate, GateDecision
def test_message_gate_can_be_constructed():
"""MessageGate 应能通过依赖注入的方式构造。"""
config_loader = MagicMock()
session_service = MagicMock()
post_reply_hook = MagicMock()
agent_hub = MagicMock()
main_agent_key = "main"
gate = MessageGate(
config_loader=config_loader,
session_service=session_service,
post_reply_hook=post_reply_hook,
agent_hub=agent_hub,
main_agent_key=main_agent_key,
)
assert gate is not None
assert gate.config_loader is config_loader
assert gate.session_service is session_service
assert gate.agent_hub is agent_hub
def test_gate_decision_dataclass_defaults():
"""GateDecision 默认字段正确。"""
d = GateDecision(intent="ignore")
assert d.intent == "ignore"
assert d.rewritten_query is None
assert d.reply is None
\ 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!