""" backend/app/services/agent_registry/intent_routes.py 用途:Intent Router 快路径 route 纯注册表(定义/注册/查询/校验,不含具体业务)。 依赖:app.core.errors.ValidationError。 逻辑:chat.intent_route_registry 注册 handler 时同步写入本模块;Admin 选项同源。 """ from __future__ import annotations from dataclasses import dataclass from typing import Any from app.core.errors import ValidationError @dataclass(frozen=True) class IntentRouteDefinition: """Intent 快路径 Admin/校验用元数据。""" key: str label: str description: str category: str = 'business' enabled: bool = True sort_order: int = 0 ui_profile: str | None = None _INTENT_ROUTE_REGISTRY: dict[str, IntentRouteDefinition] = {} def register_intent_route(definition: IntentRouteDefinition) -> IntentRouteDefinition: """ 注册 Intent route 定义。 Raises: ValidationError: key 为空或重复注册。 """ key = str(definition.key or '').strip() if not key: raise ValidationError('intent_route key 不能为空') if key in _INTENT_ROUTE_REGISTRY: raise ValidationError(f'重复的 intent_route: {key}') _INTENT_ROUTE_REGISTRY[key] = definition return definition def get_intent_route_definition(key: str) -> IntentRouteDefinition | None: """按 key 读取已注册 route 定义;未知 key 返回 None。""" normalized = str(key or '').strip() return _INTENT_ROUTE_REGISTRY.get(normalized) def list_intent_routes(*, include_disabled: bool = False) -> list[dict[str, Any]]: """返回 Admin 可选 Intent route 列表(默认仅 enabled)。""" items = sorted(_INTENT_ROUTE_REGISTRY.values(), key=lambda item: item.sort_order) if not include_disabled: items = [item for item in items if item.enabled] return [ { 'key': item.key, 'label': item.label, 'description': item.description, 'category': item.category, 'enabled': item.enabled, 'sort_order': item.sort_order, 'ui_profile': item.ui_profile, } for item in items ] def get_intent_route_keys() -> frozenset[str]: """返回已注册 route key 集合。""" return frozenset(_INTENT_ROUTE_REGISTRY.keys()) def is_known_intent_route(route: str) -> bool: """判断 route key 是否已在注册表中。""" return str(route or '').strip() in _INTENT_ROUTE_REGISTRY def normalize_intent_routes_list(raw: list[Any] | None) -> list[str]: """将 intent_routes 归一化为去重合法 route 名;非法项静默跳过(回填场景)。""" if not isinstance(raw, list): return [] seen: set[str] = set() result: list[str] = [] for item in raw: route = str(item or '').strip() if not route or not is_known_intent_route(route) or route in seen: continue seen.add(route) result.append(route) return result def validate_intent_routes(raw: Any) -> list[str]: """ Admin 写入时校验 intent_routes。 Raises: ValidationError: 非数组或含未知 route。 """ if raw is None: return [] if not isinstance(raw, list): raise ValidationError('handler_config.intent_routes 必须为数组') result: list[str] = [] for item in raw: route = str(item or '').strip() if not route: continue if not is_known_intent_route(route): raise ValidationError(f'无效的 intent_route: {route}') if route not in result: result.append(route) return result def parse_intent_routes(raw: Any) -> frozenset[str]: """从 handler_config.intent_routes 解析为 frozenset。""" if isinstance(raw, (list, tuple, set, frozenset)): return frozenset(normalize_intent_routes_list(list(raw))) return frozenset() __all__ = [ 'IntentRouteDefinition', 'get_intent_route_definition', 'get_intent_route_keys', 'is_known_intent_route', 'list_intent_routes', 'normalize_intent_routes_list', 'parse_intent_routes', 'register_intent_route', 'validate_intent_routes', ]