first commit
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
from flask import jsonify, request
|
||||
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||
from . import api_bp
|
||||
from ..models import Agent, Workspace
|
||||
from ..schemas import AgentSchema, AgentCreateSchema, AgentUpdateSchema
|
||||
from ..services import AgentService
|
||||
|
||||
|
||||
agent_schema = AgentSchema()
|
||||
agent_create_schema = AgentCreateSchema()
|
||||
agent_update_schema = AgentUpdateSchema()
|
||||
|
||||
|
||||
@api_bp.route("/workspaces/<int:workspace_id>/agents", methods=["GET"])
|
||||
@jwt_required()
|
||||
def get_agents(workspace_id):
|
||||
user_id = get_jwt_identity()
|
||||
workspace = Workspace.query.filter_by(id=workspace_id, user_id=user_id).first()
|
||||
if not workspace:
|
||||
return jsonify({"error": "工作空间不存在"}), 404
|
||||
|
||||
agents = Agent.query.filter_by(workspace_id=workspace_id).all()
|
||||
return jsonify(agent_schema.dump(agents, many=True))
|
||||
|
||||
|
||||
@api_bp.route("/workspaces/<int:workspace_id>/agents", methods=["POST"])
|
||||
@jwt_required()
|
||||
def create_agent(workspace_id):
|
||||
user_id = get_jwt_identity()
|
||||
workspace = Workspace.query.filter_by(id=workspace_id, user_id=user_id).first()
|
||||
if not workspace:
|
||||
return jsonify({"error": "工作空间不存在"}), 404
|
||||
|
||||
data = request.get_json()
|
||||
errors = agent_create_schema.validate(data)
|
||||
if errors:
|
||||
return jsonify({"error": errors}), 400
|
||||
|
||||
try:
|
||||
agent = AgentService.create_agent(workspace_id, data)
|
||||
return jsonify(agent_schema.dump(agent)), 201
|
||||
except ValueError as e:
|
||||
return jsonify({"error": str(e)}), 400
|
||||
|
||||
|
||||
@api_bp.route("/agents/<int:agent_id>", methods=["GET"])
|
||||
@jwt_required()
|
||||
def get_agent(agent_id):
|
||||
user_id = get_jwt_identity()
|
||||
agent = Agent.query.filter_by(id=agent_id).first()
|
||||
if not agent:
|
||||
return jsonify({"error": "Agent 不存在"}), 404
|
||||
|
||||
workspace = Workspace.query.filter_by(
|
||||
id=agent.workspace_id, user_id=user_id
|
||||
).first()
|
||||
if not workspace:
|
||||
return jsonify({"error": "无权访问此 Agent"}), 403
|
||||
|
||||
return jsonify(agent_schema.dump(agent))
|
||||
|
||||
|
||||
@api_bp.route("/agents/<int:agent_id>", methods=["PUT"])
|
||||
@jwt_required()
|
||||
def update_agent(agent_id):
|
||||
user_id = get_jwt_identity()
|
||||
agent = Agent.query.filter_by(id=agent_id).first()
|
||||
if not agent:
|
||||
return jsonify({"error": "Agent 不存在"}), 404
|
||||
|
||||
workspace = Workspace.query.filter_by(
|
||||
id=agent.workspace_id, user_id=user_id
|
||||
).first()
|
||||
if not workspace:
|
||||
return jsonify({"error": "无权访问此 Agent"}), 403
|
||||
|
||||
data = request.get_json()
|
||||
errors = agent_update_schema.validate(data)
|
||||
if errors:
|
||||
return jsonify({"error": errors}), 400
|
||||
|
||||
try:
|
||||
updated_agent = AgentService.update_agent(agent, data)
|
||||
return jsonify(agent_schema.dump(updated_agent))
|
||||
except ValueError as e:
|
||||
return jsonify({"error": str(e)}), 400
|
||||
|
||||
|
||||
@api_bp.route("/agents/<int:agent_id>", methods=["DELETE"])
|
||||
@jwt_required()
|
||||
def delete_agent(agent_id):
|
||||
user_id = get_jwt_identity()
|
||||
agent = Agent.query.filter_by(id=agent_id).first()
|
||||
if not agent:
|
||||
return jsonify({"error": "Agent 不存在"}), 404
|
||||
|
||||
workspace = Workspace.query.filter_by(
|
||||
id=agent.workspace_id, user_id=user_id
|
||||
).first()
|
||||
if not workspace:
|
||||
return jsonify({"error": "无权访问此 Agent"}), 403
|
||||
|
||||
try:
|
||||
AgentService.delete_agent(agent)
|
||||
return jsonify({"message": "Agent 已删除"})
|
||||
except ValueError as e:
|
||||
return jsonify({"error": str(e)}), 400
|
||||
Reference in New Issue
Block a user