133 lines
4.0 KiB
Python
133 lines
4.0 KiB
Python
"""
|
|
backend/app/services/chat/router.py
|
|
|
|
用途:Chat Intent Router — completions 内先于 Executor 的 FAQ 与注册式快路径 dispatch。
|
|
依赖:faq_cache、chat.intent_route_registry、agent_registry.resolver。
|
|
逻辑:api/chat.completions 调用 try_fast_route;未命中时由 chat.py 走 Executor/Dify。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from dataclasses import dataclass
|
|
from typing import Any, Iterator
|
|
|
|
from app.services.agent_registry.resolver import agent_has_intent_route
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class FastRouteResult:
|
|
"""Intent Router 命中结果。"""
|
|
|
|
generator: Iterator[str]
|
|
route_name: str
|
|
|
|
|
|
def try_fast_route(
|
|
*,
|
|
user: Any,
|
|
agent: Any,
|
|
query: str,
|
|
agent_id: int | None,
|
|
session_id: str,
|
|
external_conversation_id: str | None = None,
|
|
normalized_files: list[dict] | None = None,
|
|
kb_type: str | None = None,
|
|
leave_supported: bool | None = None,
|
|
) -> FastRouteResult | None:
|
|
"""
|
|
尝试 Intent 快路径;命中则返回 SSE 迭代器,未命中返回 None。
|
|
|
|
核心逻辑:FAQ 仍优先;其余快路径按 intent_route_registry 的 priority 遍历,
|
|
仅当 Agent 声明对应 route 且 match() 命中时 dispatch。
|
|
|
|
Args:
|
|
user: 当前用户代理对象。
|
|
agent: 当前 Agent 代理对象。
|
|
query: 用户问题。
|
|
agent_id: Agent 主键,FAQ 匹配使用。
|
|
session_id: HXOPS ChatSession.session_id。
|
|
external_conversation_id: Dify 侧会话 ID。
|
|
normalized_files: 规范化附件列表。
|
|
kb_type: 知识库类型(如 ERP知识库)。
|
|
leave_supported: 预计算的 HRLeaveSQLServerService.supports;为 None 时由 route match 计算。
|
|
|
|
Returns:
|
|
FastRouteResult 或 None。
|
|
"""
|
|
from app.services.agent_registry.resolver import resolve_agent_code
|
|
from app.services.chat.intent_route_registry import (
|
|
ChatIntentRouteContext,
|
|
ensure_intent_routes_loaded,
|
|
iter_enabled_chat_intent_routes,
|
|
)
|
|
from app.services.faq_cache import FaqCacheService
|
|
|
|
ensure_intent_routes_loaded()
|
|
|
|
agent_code = resolve_agent_code(agent)
|
|
has_files = bool(normalized_files)
|
|
|
|
if not has_files:
|
|
faq_hit = FaqCacheService.try_match(query, agent_id) if agent_id else None
|
|
if faq_hit:
|
|
_log_fast_route('faq_cache', agent_code=agent_code, agent_id=agent_id, session_id=session_id)
|
|
return FastRouteResult(
|
|
generator=FaqCacheService.stream_chat(
|
|
faq_hit,
|
|
query,
|
|
user,
|
|
agent,
|
|
external_conversation_id,
|
|
session_id=session_id,
|
|
),
|
|
route_name='faq_cache',
|
|
)
|
|
|
|
ctx = ChatIntentRouteContext(
|
|
user=user,
|
|
agent=agent,
|
|
query=query,
|
|
agent_id=agent_id,
|
|
session_id=session_id,
|
|
external_conversation_id=external_conversation_id,
|
|
normalized_files=normalized_files,
|
|
kb_type=kb_type,
|
|
leave_supported=leave_supported,
|
|
)
|
|
|
|
for route in iter_enabled_chat_intent_routes():
|
|
if not agent_has_intent_route(agent, route.key):
|
|
continue
|
|
if has_files and not route.supports_files:
|
|
continue
|
|
if not route.match(ctx):
|
|
continue
|
|
|
|
_log_fast_route(route.key, agent_code=agent_code, agent_id=agent_id, session_id=session_id)
|
|
return FastRouteResult(
|
|
generator=route.stream(ctx),
|
|
route_name=route.key,
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
def _log_fast_route(route_name: str, *, agent_code: str, agent_id: int | None, session_id: str) -> None:
|
|
"""记录 Intent Router 命中日志,便于联调与排错。"""
|
|
logger.info(
|
|
'[chat-router] route=%s agent_code=%s agent_id=%s session_id=%s',
|
|
route_name,
|
|
agent_code,
|
|
agent_id,
|
|
session_id,
|
|
)
|
|
|
|
|
|
__all__ = [
|
|
'FastRouteResult',
|
|
'try_fast_route',
|
|
]
|