routing_service.py
3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from __future__ import annotations
from datetime import datetime, timezone
from typing import Optional
from sqlalchemy.orm import Session as OrmSession
from app.infrastructure.llm_client import LLMClient
from app.infrastructure.session_store import SessionStore
from app.model.agent import Agent
from app.schema.routing import (
ChatCompletionResponse,
HandoffInfo,
IntentInfo,
RoutingInfo,
)
_STEWARD_CODE = "steward"
class RoutingService:
def __init__(self, db: OrmSession, session_store: SessionStore, llm: LLMClient) -> None:
self.db = db
self.session_store = session_store
self.llm = llm
def route(
self,
session_id: str,
user_id: str,
channel: str,
query: str,
mentioned_agent_code: Optional[str] = None,
) -> ChatCompletionResponse:
now = datetime.now(timezone.utc)
target_agent: Optional[Agent] = None
routing_reason: str = ""
prev_agent_id: Optional[int] = None
intent_info: Optional[IntentInfo] = None
if mentioned_agent_code:
agent = self.db.query(Agent).filter(
Agent.code == mentioned_agent_code,
Agent.is_deleted == False,
).first()
if not agent:
raise ValueError(f"Agent不存在: {mentioned_agent_code}")
target_agent = agent
routing_reason = "mention"
existing = self.session_store.get(session_id)
if existing and existing.get("current_agent_id"):
prev_agent_id = existing["current_agent_id"]
else:
existing = self.session_store.get(session_id)
if existing and existing.get("current_agent_id"):
inherited_id = existing["current_agent_id"]
agent = self.db.query(Agent).filter(
Agent.id == inherited_id,
Agent.is_deleted == False,
).first()
if agent:
prev_agent_id = inherited_id
target_agent = agent
routing_reason = "inheritance"
if not target_agent:
steward = self.db.query(Agent).filter(
Agent.code == _STEWARD_CODE,
Agent.is_deleted == False,
).first()
if not steward:
raise ValueError("管家Agent未配置")
target_agent = steward
routing_reason = "default"
if routing_reason != "mention":
intent_str, confidence = self.llm.recognize_intent(query)
intent_info = IntentInfo(intent=intent_str, confidence=confidence)
handoff: Optional[HandoffInfo] = None
if prev_agent_id is not None and prev_agent_id != target_agent.id:
from_agent = self.db.query(Agent).filter(Agent.id == prev_agent_id).first()
from_name = from_agent.name if from_agent else "未知"
handoff = HandoffInfo(
from_agent_id=prev_agent_id,
from_agent_name=from_name,
to_agent_id=target_agent.id,
to_agent_name=target_agent.name,
handoff_message=f"收到,已转交给{target_agent.name}处理。",
)
routing = RoutingInfo(
agent_id=target_agent.id,
agent_code=target_agent.code,
agent_name=target_agent.name,
routing_reason=routing_reason,
)
return ChatCompletionResponse(
session_id=session_id,
routing=routing,
intent=intent_info,
handoff=handoff,
created_at=now,
)