Files
syops/backend/app/api/agents.py
T
hugo 05913f742f 1.排程管理新增企业微信通知功能
2.可用应用以及智能体显示只显示Chatflow类型应用
2026-05-21 14:01:12 +08:00

77 lines
2.4 KiB
Python

"""
backend/app/api/agents.py
文件用途:普通用户可见的 Agent 列表接口
引用文件:flask, models, decorators
逻辑关系:根据当前用户的权限返回其可访问的 Agent 集合
"""
from flask import Blueprint, jsonify
from app import db
from app.decorators import _get_current_user # 内部工具函数
from app.models import Agent, User
# 定义 Blueprint,将在应用工厂中注册为 /api/agents
agents_bp = Blueprint('agents', __name__)
def _serialize_agent_row(row) -> dict:
return {
'id': row.id,
'name': row.name,
'description': row.description,
'icon_url': row.icon_url,
'dify_app_type': (row.extra_config or {}).get('dify_app_type') if hasattr(row, 'extra_config') else None,
}
@agents_bp.route('', methods=['GET'])
def list_user_agents():
"""
获取当前用户可见的 Agent 列表
用途:Workspace 页面展示应用网格
"""
# 获取当前用户(目前固定为 admin 用户,用于演示)
user: User = _get_current_user()
# 管理员:返回所有激活的 Agent
if user.role == 'admin':
agent_rows = (
db.session.query(Agent.id, Agent.name, Agent.description, Agent.icon_url, Agent.extra_config)
.filter(Agent.is_active.is_(True))
.order_by(Agent.id.asc())
.all()
)
else:
# 普通用户:仅返回多对多关系中已授权的 Agent
agent_rows = [
type('AgentRow', (), {
'id': a.id,
'name': a.name,
'description': a.description,
'icon_url': a.icon_url,
'extra_config': a.extra_config,
})()
for a in user.agents
if a.is_active
]
# 序列化为前端所需字段
return jsonify([_serialize_agent_row(row) for row in agent_rows])
@agents_bp.route('/public', methods=['GET'])
def list_public_agents():
"""
获取公开的 Agent 列表 (无需登录)
用途:登录页面展示
"""
# 返回所有激活的 Agent,包含基本信息
agent_rows = (
db.session.query(Agent.id, Agent.name, Agent.description, Agent.icon_url, Agent.extra_config)
.filter(Agent.is_active.is_(True))
.order_by(Agent.id.asc())
.all()
)
return jsonify([_serialize_agent_row(row) for row in agent_rows])