43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
"""
|
|
backend/app/api/agents.py
|
|
文件用途:普通用户可见的 Agent 列表接口
|
|
引用文件:flask, models, decorators
|
|
逻辑关系:根据当前用户的权限返回其可访问的 Agent 集合
|
|
"""
|
|
from flask import Blueprint, jsonify, g
|
|
from app.models import Agent, User
|
|
from app.decorators import _get_current_user # 内部工具函数
|
|
|
|
# 定义 Blueprint,将在应用工厂中注册为 /api/agents
|
|
agents_bp = Blueprint('agents', __name__)
|
|
|
|
|
|
@agents_bp.route('', methods=['GET'])
|
|
def list_user_agents():
|
|
"""
|
|
获取当前用户可见的 Agent 列表
|
|
用途:Workspace 页面展示应用网格
|
|
"""
|
|
# 获取当前用户(目前固定为 admin 用户,用于演示)
|
|
user: User = _get_current_user()
|
|
|
|
# 管理员:返回所有激活的 Agent
|
|
if user.role == 'admin':
|
|
agents = Agent.query.filter_by(is_active=True).order_by(Agent.id.asc()).all()
|
|
else:
|
|
# 普通用户:仅返回多对多关系中已授权的 Agent
|
|
agents = [a for a in user.agents if a.is_active]
|
|
|
|
# 序列化为前端所需字段
|
|
data = [
|
|
{
|
|
'id': a.id,
|
|
'name': a.name,
|
|
'description': a.description,
|
|
'icon_url': a.icon_url,
|
|
}
|
|
for a in agents
|
|
]
|
|
return jsonify(data)
|
|
|