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,157 @@
|
||||
"""
|
||||
backend/app/services/agent_registry/serialization.py
|
||||
|
||||
用途:Agent Registry 字段序列化,供 Admin / 用户侧 API 输出。
|
||||
依赖:agent_registry.resolver、agent_registry.catalog。
|
||||
逻辑:api/admin/agents、api/user、api/agents、agent_category 调用本模块合并结构化列。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
|
||||
from app.models import Agent
|
||||
from app.services.agent_registry.resolver import (
|
||||
normalize_capabilities,
|
||||
resolve_agent_code,
|
||||
resolve_capabilities,
|
||||
resolve_conversation_mode,
|
||||
resolve_runtime_provider,
|
||||
)
|
||||
|
||||
|
||||
def mask_dify_api_key(value: str | None) -> str:
|
||||
"""管理端列表脱敏 Dify API Key。"""
|
||||
text = str(value or '').strip()
|
||||
if not text:
|
||||
return ''
|
||||
if len(text) <= 8:
|
||||
return '****'
|
||||
return f'{text[:4]}****{text[-4:]}'
|
||||
|
||||
|
||||
def serialize_registry_fields(agent: Agent | Any) -> dict[str, Any]:
|
||||
"""
|
||||
输出 Agent Registry 结构化字段(列 + resolver 归一化 capabilities)。
|
||||
|
||||
Returns:
|
||||
agent_code、runtime_provider、conversation_mode、capabilities 等 API 公共字段。
|
||||
"""
|
||||
from app.services.chat.intent_route_registry import resolve_agent_intent_metadata
|
||||
|
||||
capabilities = resolve_capabilities(agent)
|
||||
handler_config = getattr(agent, 'handler_config', None)
|
||||
intent_meta = resolve_agent_intent_metadata(agent)
|
||||
tool_run_progress = build_tool_run_progress_view(agent)
|
||||
return {
|
||||
'agent_code': resolve_agent_code(agent),
|
||||
'runtime_provider': resolve_runtime_provider(agent),
|
||||
'conversation_mode': resolve_conversation_mode(agent),
|
||||
'dify_app_type': getattr(agent, 'dify_app_type', None) or 'chatflow',
|
||||
'iframe_url': getattr(agent, 'iframe_url', None),
|
||||
'category': getattr(agent, 'category', None) or None,
|
||||
'sub_category': getattr(agent, 'sub_category', None) or None,
|
||||
'sort_order': int(getattr(agent, 'sort_order', 0) or 0),
|
||||
'welcome_message': getattr(agent, 'welcome_message', None),
|
||||
'suggested_questions_style': getattr(agent, 'suggested_questions_style', None) or 'plain',
|
||||
'suggested_questions': getattr(agent, 'suggested_questions', None) or [],
|
||||
'capabilities': capabilities,
|
||||
'file_upload_policy': getattr(agent, 'file_upload_policy', None),
|
||||
'bound_schedule_task_id': getattr(agent, 'bound_schedule_task_id', None),
|
||||
'handler_config': handler_config if isinstance(handler_config, dict) else None,
|
||||
'tool_run_progress': tool_run_progress,
|
||||
**intent_meta,
|
||||
}
|
||||
|
||||
|
||||
def serialize_agent_for_admin(agent: Agent) -> dict[str, Any]:
|
||||
"""Agent ORM → 管理端列表/详情 JSON(API Key 仅返回脱敏值,明文不可读)。"""
|
||||
registry = serialize_registry_fields(agent)
|
||||
return {
|
||||
'id': agent.id,
|
||||
'name': agent.name,
|
||||
'description': agent.description,
|
||||
'icon_url': agent.icon_url,
|
||||
'is_active': agent.is_active,
|
||||
'type': agent.type,
|
||||
'dify_app_id': agent.dify_app_id,
|
||||
'dify_api_key_masked': mask_dify_api_key(agent.dify_api_key),
|
||||
'api_base_url': agent.api_base_url,
|
||||
'extra_config': agent.extra_config if isinstance(agent.extra_config, dict) else {},
|
||||
**registry,
|
||||
}
|
||||
|
||||
|
||||
def serialize_agent_for_user_list(agent: Agent | Any) -> dict[str, Any]:
|
||||
"""用户可见 Agent 列表项(聊天侧栏、工作台)。"""
|
||||
name = agent.name if hasattr(agent, 'name') else agent.get('name')
|
||||
description = agent.description if hasattr(agent, 'description') else agent.get('description')
|
||||
icon_url = agent.icon_url if hasattr(agent, 'icon_url') else agent.get('icon_url')
|
||||
agent_id = int(agent.id if hasattr(agent, 'id') else agent.get('id') or 0)
|
||||
registry = serialize_registry_fields(agent)
|
||||
return {
|
||||
'id': agent_id,
|
||||
'name': name,
|
||||
'description': description,
|
||||
'icon_url': icon_url,
|
||||
**registry,
|
||||
}
|
||||
|
||||
|
||||
def serialize_agent_for_user_detail(
|
||||
agent: Agent,
|
||||
*,
|
||||
dify_parameters: dict[str, Any] | None = None,
|
||||
extra_config: dict[str, Any] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""用户 Agent 详情(不含 dify_api_key)。"""
|
||||
registry = serialize_registry_fields(agent)
|
||||
extra = extra_config if isinstance(extra_config, dict) else (
|
||||
agent.extra_config if isinstance(agent.extra_config, dict) else {}
|
||||
)
|
||||
return {
|
||||
'id': agent.id,
|
||||
'name': agent.name,
|
||||
'description': agent.description,
|
||||
'type': agent.type,
|
||||
'icon_url': agent.icon_url,
|
||||
'extra_config': extra,
|
||||
'dify_parameters': dify_parameters,
|
||||
**registry,
|
||||
}
|
||||
|
||||
|
||||
def build_tool_run_progress_view(agent: Agent) -> dict[str, Any] | None:
|
||||
"""
|
||||
从 Registry 列构建前端 tool_run_progress 视图(迁移期兼容 extra 读者)。
|
||||
|
||||
核心逻辑:capabilities.task_progress 为 true 且 bound_schedule_task_id 已注册时返回配置 dict。
|
||||
"""
|
||||
caps = resolve_capabilities(agent)
|
||||
if not caps.get('task_progress'):
|
||||
return None
|
||||
task_id = getattr(agent, 'bound_schedule_task_id', None)
|
||||
try:
|
||||
schedule_task_id = int(task_id)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
if schedule_task_id <= 0:
|
||||
return None
|
||||
|
||||
handler_config = getattr(agent, 'handler_config', None)
|
||||
from app.services.agent_registry.catalog_loader import get_tool_run_progress_defaults
|
||||
from app.services.agent_registry.resolver import resolve_agent_code
|
||||
|
||||
defaults = get_tool_run_progress_defaults()
|
||||
cfg: dict[str, Any] = {
|
||||
'type': str(defaults.get('progress_type') or resolve_agent_code(agent) or 'file_check'),
|
||||
'schedule_task_id': schedule_task_id,
|
||||
}
|
||||
cfg['poll_interval_seconds'] = defaults.get('poll_interval_seconds', 2)
|
||||
cfg['lock_input_when_running'] = defaults.get('lock_input_when_running', True)
|
||||
if isinstance(handler_config, dict):
|
||||
if handler_config.get('poll_interval_seconds') is not None:
|
||||
cfg['poll_interval_seconds'] = handler_config['poll_interval_seconds']
|
||||
if handler_config.get('lock_input_when_running') is not None:
|
||||
cfg['lock_input_when_running'] = handler_config['lock_input_when_running']
|
||||
return cfg
|
||||
Reference in new issue
Block a user