baodan/api/insurance/chat/agent_routes.py

49 lines
1.5 KiB
Python

"""智能体管理路由
列出 Dify 中可用的智能体(应用),供前端选择。
智能体列表为公开接口,不需要登录即可访问。
"""
from flask import Blueprint, jsonify
from insurance.db.compat import db
agent_bp = Blueprint("agent", __name__)
@agent_bp.route("/list", methods=["GET"])
def list_agents():
"""获取可用的智能体列表。"""
try:
# 直接查询 Dify 的 apps 表
result = db.session.execute(db.text("""
SELECT a.id, a.name, a.mode, a.description,
s.code as site_code,
t.token as api_token
FROM apps a
LEFT JOIN sites s ON s.app_id = a.id
LEFT JOIN api_tokens t ON t.app_id = a.id AND t.type = 'app'
WHERE a.status = 'normal' AND a.mode = 'chat'
ORDER BY a.created_at DESC
"""))
agents = []
for row in result:
agents.append({
"id": str(row.id),
"name": row.name,
"mode": row.mode, # chat / workflow / agent-chat
"description": row.description or "",
"site_code": row.site_code or "",
"api_token": row.api_token or "",
})
return jsonify({
"code": 0,
"data": agents,
})
except Exception as e:
return jsonify({
"code": 500,
"message": f"获取智能体列表失败: {str(e)}",
"data": [],
})