""" backend/app/services/agent_registry/backfill_mapping.py 用途:存量 Agent 一次性回填映射(extra_config / type / 名称 → Registry 结构化列)。 依赖:agent_registry.catalog、agent_registry.resolver.normalize_capabilities。 逻辑:仅 backfill_agent_registry.py 调用;运行时 resolver 不做名称/能力启发式。 """ from __future__ import annotations import re from typing import Any # 回填专用:历史 Agent 名称 → handler_config.intent_routes key(迁移映射,非运行时 dispatch)。 LEGACY_INTENT_ROUTE_KEYS = { 'hr_leave': 'hr_leave_sql', 'plm': 'plm_oracle', 'maintenance': 'maintenance_chat', 'text2sql': 'text2sql', } from app.services.agent_registry.content_profiles import ( infer_content_profile_from_intent_routes, merge_profile_defaults_into_fields, ) from app.services.agent_registry.resolver import normalize_capabilities TRAINING_CONTENT_PROFILE = 'training' def _extra_dict(agent) -> dict[str, Any]: extra = getattr(agent, 'extra_config', None) return extra if isinstance(extra, dict) else {} def _strip_str(value: Any) -> str: return str(value or '').strip() def _legacy_capabilities_list(extra: dict[str, Any]) -> list[str]: caps = extra.get('capabilities') if isinstance(caps, list): return [str(item).strip() for item in caps if str(item).strip()] return [] def _declares_tool_run_progress(extra: dict[str, Any]) -> bool: if 'tool_run_progress' in _legacy_capabilities_list(extra): return True return isinstance(extra.get('tool_run_progress'), dict) def _name_implies_shipping_notice(agent) -> bool: """回填专用:历史货代 Agent 名称 → agent_code=shipping_notice。""" agent_name = _strip_str(getattr(agent, 'name', None)) if agent_name == '货代发货通知 Agent': return True normalized_name = agent_name.replace(' ', '') return '发货通知' in normalized_name and 'Agent' in normalized_name def _name_implies_maintenance_chat(agent) -> bool: agent_name = _strip_str(getattr(agent, 'name', None)) return agent_name == '设备维保助手' or ('设备维保' in agent_name and '助手' in agent_name) def _infer_intent_routes_from_name(agent) -> list[str]: """回填专用:历史 Agent 名称 → handler_config.intent_routes。""" name = _strip_str(getattr(agent, 'name', None)) routes: list[str] = [] if name == '通用入口': routes.extend([LEGACY_INTENT_ROUTE_KEYS['hr_leave'], LEGACY_INTENT_ROUTE_KEYS['plm']]) elif name == '人事规章': routes.append(LEGACY_INTENT_ROUTE_KEYS['hr_leave']) elif name == '生产sop': routes.append(LEGACY_INTENT_ROUTE_KEYS['plm']) elif _name_implies_maintenance_chat(agent): routes.append(LEGACY_INTENT_ROUTE_KEYS['maintenance']) elif name == '固资管理': routes.append(LEGACY_INTENT_ROUTE_KEYS['text2sql']) from app.services.agent_registry.intent_routes import normalize_intent_routes_list return normalize_intent_routes_list(routes) def _infer_content_profile_from_name(agent) -> str | None: """回填专用:历史培训 Agent 名称 / extra 开关 → handler_config.content_profile。""" extra = _extra_dict(agent) if extra.get('training_workspace_enabled') in (True, 'true', 'True', 1): return TRAINING_CONTENT_PROFILE name = _strip_str(getattr(agent, 'name', None)) if name and re.search(r'(培训|考试|入职)', name): return TRAINING_CONTENT_PROFILE return None def _capabilities_from_legacy(extra: dict[str, Any], agent_code: str | None = None) -> dict[str, bool]: caps = normalize_capabilities(extra.get('capabilities') if isinstance(extra.get('capabilities'), dict) else None) if _declares_tool_run_progress(extra) or agent_code == 'file_check': caps['task_progress'] = True if isinstance(extra.get('file_upload'), bool): caps['file_upload'] = extra['file_upload'] return caps def _apply_name_based_capabilities(agent, caps: dict[str, bool]) -> dict[str, bool]: """回填专用:固资管理 Agent 启用 erp_kb capability。""" if _strip_str(getattr(agent, 'name', None)) == '固资管理': caps['erp_kb'] = True return caps def infer_registry_fields_from_legacy(agent) -> dict[str, Any]: """ 按旧字段与运维映射规则推断 Registry 结构化值(仅回填脚本使用)。 核心逻辑:type=iframe → generic/iframe/embedded;名称含发货通知 → shipping_notice; capabilities 含 tool_run_progress → file_check;其余 type=api → generic/dify/chat。 Returns: 含 agent_code、runtime_provider、conversation_mode、capabilities、bound_schedule_task_id 等键的字典。 """ extra = _extra_dict(agent) agent_type = _strip_str(getattr(agent, 'type', None)).lower() result: dict[str, Any] = {} if agent_type == 'iframe': result['agent_code'] = 'generic' result['runtime_provider'] = 'iframe' result['conversation_mode'] = 'embedded' elif _name_implies_shipping_notice(agent): result['agent_code'] = 'shipping_notice' result['runtime_provider'] = 'hybrid' result['conversation_mode'] = 'task_chat' elif _declares_tool_run_progress(extra): result['agent_code'] = 'file_check' result['runtime_provider'] = 'dify' result['conversation_mode'] = 'task_chat' else: extra_code = _strip_str(extra.get('agent_code')) result['agent_code'] = extra_code or 'generic' result['runtime_provider'] = 'iframe' if agent_type == 'iframe' else 'dify' result['conversation_mode'] = 'embedded' if agent_type == 'iframe' else 'chat' extra_code = _strip_str(extra.get('agent_code')) if extra_code and result.get('agent_code') == 'generic': result['agent_code'] = extra_code extra_provider = _strip_str(extra.get('runtime_provider')) if extra_provider: result['runtime_provider'] = extra_provider extra_mode = _strip_str(extra.get('conversation_mode')) if extra_mode: result['conversation_mode'] = extra_mode if _strip_str(extra.get('dify_app_type')): result['dify_app_type'] = _strip_str(extra.get('dify_app_type')) if _strip_str(extra.get('iframe_url')): result['iframe_url'] = _strip_str(extra.get('iframe_url')) if _strip_str(extra.get('category')): result['category'] = _strip_str(extra.get('category')) if _strip_str(extra.get('sub_category')): result['sub_category'] = _strip_str(extra.get('sub_category')) if extra.get('sort_order') is not None: try: result['sort_order'] = int(extra.get('sort_order')) except (TypeError, ValueError): pass if _strip_str(extra.get('welcome_message')): result['welcome_message'] = _strip_str(extra.get('welcome_message')) if _strip_str(extra.get('suggested_questions_style')): result['suggested_questions_style'] = _strip_str(extra.get('suggested_questions_style')) if isinstance(extra.get('suggested_questions'), list): result['suggested_questions'] = extra.get('suggested_questions') tool_cfg = extra.get('tool_run_progress') if isinstance(tool_cfg, dict): raw_task_id = tool_cfg.get('schedule_task_id', tool_cfg.get('task_id')) try: task_id = int(raw_task_id) if task_id > 0: result['bound_schedule_task_id'] = task_id except (TypeError, ValueError): pass result['handler_config'] = { key: tool_cfg[key] for key in ('poll_interval_seconds', 'lock_input_when_running', 'type') if key in tool_cfg } result['capabilities'] = _apply_name_based_capabilities( agent, _capabilities_from_legacy(extra, result.get('agent_code')), ) intent_routes = _infer_intent_routes_from_name(agent) content_profile = _infer_content_profile_from_name(agent) if not content_profile and intent_routes: content_profile = infer_content_profile_from_intent_routes(intent_routes) if intent_routes or content_profile: handler_config = dict(result.get('handler_config') or {}) if intent_routes: handler_config['intent_routes'] = intent_routes if content_profile: handler_config['content_profile'] = content_profile result['handler_config'] = handler_config if content_profile: merge_profile_defaults_into_fields(result, content_profile) return result