Commit e26aacb6 by sunqi02

refactor(session): simplify routing to always main, integrate save_to_db

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent cfa9c727
......@@ -7,7 +7,7 @@ from fastapi.responses import JSONResponse, Response
from app.api.v1.router import api_router
from app.config.loader import ConfigLoader
from app.infrastructure.database import engine
from app.infrastructure.database import engine, SessionLocal
from app.infrastructure.response_format import format_response_datetimes
from app.infrastructure.schema_patch import ensure_runtime_schema
from app.infrastructure.database import Base
......@@ -41,7 +41,8 @@ async def lifespan(app: FastAPI):
config_loader = ConfigLoader()
settings = config_loader.load_settings()
session_service = ActiveSessionService()
db = SessionLocal()
session_service = ActiveSessionService(db=db)
use_console_im = os.getenv("USE_CONSOLE_IM")
if use_console_im is None:
......@@ -67,6 +68,7 @@ async def lifespan(app: FastAPI):
if agent_hub_instance:
await agent_hub_instance.close()
db.close()
app = FastAPI(
......
......@@ -306,7 +306,6 @@ class AgentHub:
session = self.session_service.get_or_create_session(
session_id=session_id,
room_id=room_id,
user_id=user_id,
channel=channel
)
......@@ -318,26 +317,9 @@ class AgentHub:
messages = memory.get_messages(max_rounds=10)
# Determine target agent and routing reason
position_restricted = allowed_agent_codes is not None
if mentioned_agent_code and mentioned_agent_code in self.agents:
if position_restricted and mentioned_agent_code not in allowed_agent_codes:
target_agent = "main"
routing_reason = "position_restricted"
else:
target_agent = mentioned_agent_code
routing_reason = "mention"
elif session.current_agent_code and session.current_agent_code in self.agents:
if position_restricted and session.current_agent_code not in allowed_agent_codes:
target_agent = "main"
routing_reason = "position_restricted"
else:
target_agent = session.current_agent_code
routing_reason = "inheritance"
else:
target_agent = "main"
routing_reason = "default"
# 始终路由到 main agent
target_agent = "main"
routing_reason = "default"
context = {
"user_query": user_query,
......@@ -390,11 +372,11 @@ class AgentHub:
response_content = response_msg.get_text_content()
# Store messages via SessionMemory
memory.add("user", user_query, agent_code=mentioned_agent_code or "main")
memory.add("user", user_query, agent_code="main")
memory.add("assistant", response_content, agent_code=target_agent)
if target_agent != "main":
self.session_service.update_current_agent(session.id, target_agent)
# 持久化到 DB
self.session_service.save_to_db(session_id)
# Send via hook
await self.post_reply_hook.execute({
......
......@@ -491,8 +491,8 @@ class TestAgentHubRouting:
assert len(result["response_content"]) > 0
@pytest.mark.asyncio
async def test_mention_routing_to_sub_agent(self):
"""mentioned_agent_code='meeting' → 直接路由到 meeting"""
async def test_mention_routing_always_main(self):
"""即使传入 mentioned_agent_code,也始终路由到 main"""
with patch.object(test_hub, 'model', new=_make_mock_model_reply("已为你整理会议纪要")):
with patch.object(test_hub.formatter, 'format', new=AsyncMock(side_effect=_noop_format)):
result = await test_hub.process_message(
......@@ -503,12 +503,12 @@ class TestAgentHubRouting:
mentioned_agent_code="meeting",
)
assert result["response_agent_code"] == "meeting"
assert result["routing_reason"] == "mention"
assert result["response_agent_code"] == "main"
assert result["routing_reason"] == "default"
@pytest.mark.asyncio
async def test_inheritance_routing(self):
"""首次 meeting,第二次不 mention → 继承 meeting"""
async def test_no_sticky_routing(self):
"""不再粘性继承 agent,每次始终路由 main"""
with patch.object(test_hub, 'model', new=_make_mock_model_reply("会议纪要内容")):
with patch.object(test_hub.formatter, 'format', new=AsyncMock(side_effect=_noop_format)):
sid = "sess_route_003"
......@@ -519,7 +519,7 @@ class TestAgentHubRouting:
user_query="查会议纪要",
mentioned_agent_code="meeting",
)
assert r1["response_agent_code"] == "meeting"
assert r1["response_agent_code"] == "main"
r2 = await test_hub.process_message(
session_id=sid,
......@@ -527,8 +527,8 @@ class TestAgentHubRouting:
user_id="user_001",
user_query="再查一个",
)
assert r2["response_agent_code"] == "meeting"
assert r2["routing_reason"] == "inheritance"
assert r2["response_agent_code"] == "main"
assert r2["routing_reason"] == "default"
@pytest.mark.asyncio
async def test_messages_accumulate(self):
......@@ -579,22 +579,21 @@ class TestAgentHubRouting:
assert result["response_content"] is not None
@pytest.mark.asyncio
async def test_allowed_agent_codes_restricts_routing(self):
"""allowed_agent_codes 限制路由范围"""
async def test_allowed_agent_codes_still_routes_main(self):
"""allowed_agent_codes 不影响路由(始终 main),仅影响 prompt 中 advertised 的子 agent"""
with patch.object(test_hub, 'model', new=_make_mock_model_reply("回复")):
with patch.object(test_hub.formatter, 'format', new=AsyncMock(side_effect=_noop_format)):
# allowed 中不包含 meeting,mention meeting 应回退到 main
result = await test_hub.process_message(
session_id="sess_restricted",
room_id="room_001",
user_id="user_001",
user_query="整理会议纪要",
mentioned_agent_code="meeting",
allowed_agent_codes=[], # 空列表,不允许任何子 agent
allowed_agent_codes=[],
)
assert result["response_agent_code"] == "main"
assert result["routing_reason"] == "position_restricted"
assert result["routing_reason"] == "default"
# ═══════════════════════════════════════════════════════════════════
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!