feat(agent): 增强 Agent 配置,新增结构化字段与聊天功能支持
This commit is contained in:
1 parent
ac4262cd75
commit
c8ce77199e
183 files changed
+11690
-2300
No files matched your search
@@ -0,0 +1,193 @@
|
||||
"""
|
||||
backend/app/services/chat/intent_route_registry.py
|
||||
|
||||
用途:Chat Intent 快路径 handler 注册表(展示配置 + match/stream 执行器)。
|
||||
依赖:agent_registry.intent_routes。
|
||||
逻辑:各 intent_routes/*.py 由 load_builtin_intent_routes 调用 register;router 遍历本表;Admin 选项与 handler 同源。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
from typing import Any, Callable, Iterator
|
||||
|
||||
from app.core.errors import ValidationError
|
||||
from app.services.agent_registry.intent_routes import (
|
||||
IntentRouteDefinition,
|
||||
get_intent_route_definition,
|
||||
list_intent_routes,
|
||||
parse_intent_routes,
|
||||
register_intent_route,
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ChatIntentRouteContext:
|
||||
"""Intent Router 单次 dispatch 上下文。"""
|
||||
|
||||
user: Any
|
||||
agent: Any
|
||||
query: str
|
||||
agent_id: int | None
|
||||
session_id: str
|
||||
external_conversation_id: str | None
|
||||
normalized_files: list[dict] | None
|
||||
kb_type: str | None
|
||||
leave_supported: bool | None
|
||||
|
||||
@property
|
||||
def has_files(self) -> bool:
|
||||
return bool(self.normalized_files)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ChatIntentRouteHandler:
|
||||
"""可执行 Chat Intent 快路径定义。"""
|
||||
|
||||
key: str
|
||||
label: str
|
||||
description: str
|
||||
priority: int
|
||||
match: Callable[[ChatIntentRouteContext], bool]
|
||||
stream: Callable[[ChatIntentRouteContext], Iterator[str]]
|
||||
category: str = 'business'
|
||||
enabled: bool = True
|
||||
supports_files: bool = False
|
||||
ui_profile: str | None = None
|
||||
leave_feature: bool = False
|
||||
|
||||
|
||||
_CHAT_INTENT_ROUTE_REGISTRY: dict[str, ChatIntentRouteHandler] = {}
|
||||
_ROUTES_LOADED = False
|
||||
|
||||
|
||||
def register_chat_intent_route(handler: ChatIntentRouteHandler) -> ChatIntentRouteHandler:
|
||||
"""
|
||||
注册 Chat Intent handler,并同步写入 agent_registry.intent_routes。
|
||||
|
||||
Raises:
|
||||
ValidationError: key 为空或重复注册。
|
||||
"""
|
||||
key = str(handler.key or '').strip()
|
||||
if not key:
|
||||
raise ValidationError('chat intent route key 不能为空')
|
||||
if key in _CHAT_INTENT_ROUTE_REGISTRY:
|
||||
raise ValidationError(f'重复的 chat intent route: {key}')
|
||||
|
||||
register_intent_route(IntentRouteDefinition(
|
||||
key=handler.key,
|
||||
label=handler.label,
|
||||
description=handler.description,
|
||||
category=handler.category,
|
||||
enabled=handler.enabled,
|
||||
sort_order=handler.priority,
|
||||
ui_profile=handler.ui_profile,
|
||||
))
|
||||
_CHAT_INTENT_ROUTE_REGISTRY[key] = handler
|
||||
return handler
|
||||
|
||||
|
||||
def list_chat_intent_route_definitions(*, include_disabled: bool = False) -> list[dict[str, Any]]:
|
||||
"""返回 Admin 可选 route 列表(与 agent_registry.intent_routes 同源)。"""
|
||||
ensure_intent_routes_loaded()
|
||||
return list_intent_routes(include_disabled=include_disabled)
|
||||
|
||||
|
||||
def iter_enabled_chat_intent_routes() -> list[ChatIntentRouteHandler]:
|
||||
"""按 priority 升序返回已启用的 handler 列表。"""
|
||||
ensure_intent_routes_loaded()
|
||||
return sorted(
|
||||
[item for item in _CHAT_INTENT_ROUTE_REGISTRY.values() if item.enabled],
|
||||
key=lambda item: item.priority,
|
||||
)
|
||||
|
||||
|
||||
def get_chat_intent_route_handler(key: str) -> ChatIntentRouteHandler | None:
|
||||
"""按 key 读取 handler;未知 key 返回 None。"""
|
||||
ensure_intent_routes_loaded()
|
||||
normalized = str(key or '').strip()
|
||||
return _CHAT_INTENT_ROUTE_REGISTRY.get(normalized)
|
||||
|
||||
|
||||
def _resolve_content_profile_key(agent) -> str | None:
|
||||
"""只读 handler_config.content_profile;运行时不再从 intent route 组合推断。"""
|
||||
handler_config = getattr(agent, 'handler_config', None)
|
||||
if not isinstance(handler_config, dict):
|
||||
return None
|
||||
from app.services.agent_registry.content_profiles import VALID_CONTENT_PROFILES
|
||||
|
||||
profile = str(handler_config.get('content_profile') or '').strip()
|
||||
return profile if profile in VALID_CONTENT_PROFILES else None
|
||||
|
||||
|
||||
def resolve_agent_intent_metadata(agent) -> dict[str, Any]:
|
||||
"""
|
||||
根据 Agent 已声明 intent_routes 聚合 UI/功能元数据,供序列化与前端消费。
|
||||
|
||||
Returns:
|
||||
intent_ui_profiles、leave_feature_enabled、hr_focused_avatar、content_profile_key。
|
||||
"""
|
||||
ensure_intent_routes_loaded()
|
||||
content_profile_key = _resolve_content_profile_key(agent)
|
||||
declared = parse_intent_routes(
|
||||
getattr(agent, 'handler_config', None).get('intent_routes')
|
||||
if isinstance(getattr(agent, 'handler_config', None), dict)
|
||||
else None,
|
||||
)
|
||||
if not declared:
|
||||
return {
|
||||
'intent_ui_profiles': [],
|
||||
'leave_feature_enabled': False,
|
||||
'hr_focused_avatar': False,
|
||||
'content_profile_key': content_profile_key,
|
||||
}
|
||||
|
||||
ui_profiles: set[str] = set()
|
||||
leave_feature = False
|
||||
for route_key in declared:
|
||||
handler = get_chat_intent_route_handler(route_key)
|
||||
if handler:
|
||||
if handler.ui_profile:
|
||||
ui_profiles.add(handler.ui_profile)
|
||||
if handler.leave_feature:
|
||||
leave_feature = True
|
||||
continue
|
||||
definition = get_intent_route_definition(route_key)
|
||||
if definition and definition.ui_profile:
|
||||
ui_profiles.add(definition.ui_profile)
|
||||
|
||||
leave_route_keys = {
|
||||
key for key in declared
|
||||
if (handler := get_chat_intent_route_handler(key)) and handler.leave_feature
|
||||
}
|
||||
hr_focused_avatar = declared == leave_route_keys and len(leave_route_keys) == 1
|
||||
|
||||
return {
|
||||
'intent_ui_profiles': sorted(ui_profiles),
|
||||
'leave_feature_enabled': leave_feature,
|
||||
'hr_focused_avatar': hr_focused_avatar,
|
||||
'content_profile_key': content_profile_key,
|
||||
}
|
||||
|
||||
|
||||
def ensure_intent_routes_loaded() -> None:
|
||||
"""懒加载内置 Chat Intent route 模块(幂等)。"""
|
||||
global _ROUTES_LOADED
|
||||
if _ROUTES_LOADED:
|
||||
return
|
||||
from app.services.chat.intent_routes import load_builtin_intent_routes
|
||||
|
||||
load_builtin_intent_routes()
|
||||
_ROUTES_LOADED = True
|
||||
|
||||
|
||||
__all__ = [
|
||||
'ChatIntentRouteContext',
|
||||
'ChatIntentRouteHandler',
|
||||
'ensure_intent_routes_loaded',
|
||||
'get_chat_intent_route_handler',
|
||||
'iter_enabled_chat_intent_routes',
|
||||
'list_chat_intent_route_definitions',
|
||||
'register_chat_intent_route',
|
||||
'resolve_agent_intent_metadata',
|
||||
]
|
||||
Reference in new issue
Block a user