166 lines
5.9 KiB
Python
166 lines
5.9 KiB
Python
"""
|
||
backend/app/api/admin/agents.py
|
||
|
||
用途:管理后台 Agent CRUD、连通性测试、权限绑定 API(薄路由层)。
|
||
依赖:admin.blueprint、services/admin/agent_service、core.response.ok。
|
||
逻辑:/agents* 路由;统一 success/data/request_id envelope。
|
||
"""
|
||
from flask import g, request
|
||
|
||
from app.api.admin.blueprint import admin_bp
|
||
from app.core.decorators import require_admin
|
||
from app.core.errors import ValidationError
|
||
from app.core.response import ok
|
||
from app.services.admin import agent_service
|
||
|
||
|
||
def _request_id() -> str | None:
|
||
return getattr(g, 'request_id', None)
|
||
|
||
|
||
@admin_bp.route('/agents/schema', methods=['GET'])
|
||
@require_admin
|
||
def get_agent_registry_schema():
|
||
"""返回 Admin Agent Registry 权威 schema(平台枚举、Catalog、handler_config schema)。"""
|
||
from app.services.agent_registry.admin_schema import build_agent_registry_schema
|
||
|
||
return ok(build_agent_registry_schema(), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/infer-content-profile', methods=['POST'])
|
||
@require_admin
|
||
def infer_agent_content_profile():
|
||
"""根据 intent_routes 推断 content_profile(Admin 表单建议,不参与运行时)。"""
|
||
from app.services.agent_registry.content_profiles import infer_content_profile_from_intent_routes
|
||
|
||
data = request.get_json(silent=True) or {}
|
||
routes = data.get('intent_routes') if isinstance(data.get('intent_routes'), list) else []
|
||
profile = infer_content_profile_from_intent_routes(routes)
|
||
return ok({'content_profile': profile}, request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/type-catalog', methods=['GET'])
|
||
@require_admin
|
||
def list_agent_type_catalog():
|
||
"""返回 Admin 应用类型 Catalog 下拉列表。"""
|
||
from app.services.agent_registry.catalog import list_type_catalog
|
||
|
||
return ok(list_type_catalog(), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/content-profiles', methods=['GET'])
|
||
@require_admin
|
||
def list_agent_content_profiles():
|
||
"""返回 Admin 可选 content_profile 及默认文案模板。"""
|
||
from app.services.agent_registry.content_profiles import list_content_profiles
|
||
|
||
return ok(list_content_profiles(), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/intent-routes', methods=['GET'])
|
||
@require_admin
|
||
def list_agent_intent_routes():
|
||
"""返回 Admin 可选 Intent route 注册表(前端表单动态选项)。"""
|
||
from app.services.chat.intent_route_registry import list_chat_intent_route_definitions
|
||
|
||
return ok(list_chat_intent_route_definitions(), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents', methods=['GET'])
|
||
@require_admin
|
||
def list_agents():
|
||
"""返回全部 Agent 列表。"""
|
||
return ok(agent_service.list_agents(), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents', methods=['POST'])
|
||
@require_admin
|
||
def create_agent():
|
||
"""创建 Agent。"""
|
||
data = request.get_json() or {}
|
||
payload = agent_service.create_agent(data)
|
||
return ok(payload, request_id=_request_id(), status=201)
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>', methods=['PUT'])
|
||
@require_admin
|
||
def update_agent(id: int):
|
||
"""更新 Agent。"""
|
||
data = request.get_json() or {}
|
||
agent_service.update_agent(id, data)
|
||
return ok({'status': 'ok'}, request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>', methods=['DELETE'])
|
||
@require_admin
|
||
def delete_agent(id: int):
|
||
"""删除 Agent。"""
|
||
agent_service.delete_agent(id)
|
||
return ok({'status': 'ok'}, request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>/test', methods=['POST'])
|
||
@require_admin
|
||
def test_agent(id: int):
|
||
"""测试 Agent 连通性。"""
|
||
payload = agent_service.test_agent_connectivity(id)
|
||
return ok(payload, request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>/runtime-bindings', methods=['GET'])
|
||
@require_admin
|
||
def list_agent_runtime_bindings(id: int):
|
||
"""列出入口 Agent 的显式运行能力绑定。"""
|
||
from app.services.agent_runtime_binding_service import list_runtime_bindings
|
||
|
||
return ok(list_runtime_bindings(id), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>/runtime-bindings/<path:capability_code>', methods=['PUT'])
|
||
@require_admin
|
||
def save_agent_runtime_binding(id: int, capability_code: str):
|
||
"""保存单个能力 Provider;API Key 仍由 Provider Agent 自身配置管理。"""
|
||
body = request.get_json(silent=True)
|
||
if not isinstance(body, dict):
|
||
raise ValidationError('JSON body must be an object')
|
||
try:
|
||
provider_agent_id = int(body.get('provider_agent_id'))
|
||
except (TypeError, ValueError) as exc:
|
||
raise ValidationError('provider_agent_id is required') from exc
|
||
from app.services.agent_runtime_binding_service import save_runtime_binding, serialize_runtime_binding
|
||
|
||
binding = save_runtime_binding(
|
||
owner_agent_id=id,
|
||
capability_code=capability_code,
|
||
provider_agent_id=provider_agent_id,
|
||
enabled=body.get('enabled') is not False,
|
||
config=body.get('config'),
|
||
)
|
||
return ok(serialize_runtime_binding(binding), request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>/permissions', methods=['POST'])
|
||
@require_admin
|
||
def update_agent_permissions(id: int):
|
||
"""设置 Agent 授权用户列表。"""
|
||
data = request.get_json() or {}
|
||
user_ids = data.get('user_ids', [])
|
||
agent_service.update_agent_permissions(id, user_ids)
|
||
return ok({'status': 'ok'}, request_id=_request_id())
|
||
|
||
|
||
@admin_bp.route('/agents/<int:id>/training-question-bank/import-pdf', methods=['POST'])
|
||
@require_admin
|
||
def import_training_question_bank_from_pdf(id: int):
|
||
"""
|
||
从已上传的培训试题 PDF 解析题库。
|
||
|
||
用途:给 Agent 管理表单提供“从试题 PDF 导入题库”入口,返回结构化题目供前端回填并继续人工微调。
|
||
"""
|
||
data = request.get_json(silent=True) or {}
|
||
payload = agent_service.import_training_question_bank_from_pdf(
|
||
agent_id=id,
|
||
guide_id=data.get('guide_id'),
|
||
)
|
||
return ok(payload, request_id=_request_id())
|