feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持
This commit is contained in:
1 parent
ac4262cd75
commit
c8ce77199e
183 files changed
+11690
-2300
No files matched your search
@@ -1,27 +1,132 @@
|
||||
"""
|
||||
backend/app/services/chat/router.py
|
||||
|
||||
用途:Chat Intent Router,按意图分流至 PLM/HR/Text2SQL/Dify。
|
||||
依赖:integrations/* stream_chat 实现。
|
||||
逻辑:api/chat/completions 调用本模块 select_stream_generator。
|
||||
设计参考:docs/tec.md §4.1 Chat Intent Router。
|
||||
用途: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
|
||||
|
||||
def select_stream_generator(*, user: Any, agent: Any, query: str, **kwargs: Any) -> Iterator[str]:
|
||||
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:
|
||||
"""
|
||||
根据 agent 配置与用户问题选择流式 generator。
|
||||
核心逻辑:Wave 3 从 api/chat.py /completions 分支迁入。
|
||||
尝试 Intent 快路径;命中则返回 SSE 迭代器,未命中返回 None。
|
||||
|
||||
核心逻辑:FAQ 仍优先;其余快路径按 intent_route_registry 的 priority 遍历,
|
||||
仅当 Agent 声明对应 route 且 match() 命中时 dispatch。
|
||||
|
||||
Args:
|
||||
user: 当前 User 模型实例。
|
||||
agent: 当前 Agent 模型实例。
|
||||
query: 用户问题文本。
|
||||
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:
|
||||
SSE 事件字符串迭代器。
|
||||
Raises:
|
||||
NotImplementedError: Wave 0 占位。
|
||||
FastRouteResult 或 None。
|
||||
"""
|
||||
raise NotImplementedError('ChatIntentRouter pending Wave 3 migration from api/chat.py')
|
||||
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',
|
||||
]
|
||||
Reference in new issue
Block a user