You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

agents.py 1.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from typing import Dict
  2. from fastapi import APIRouter, Depends, HTTPException
  3. from ...database import DatabaseManager # Add this import
  4. from ...datamodel import Agent, Model, Tool
  5. from ..deps import get_db
  6. router = APIRouter()
  7. @router.get("/")
  8. async def list_agents(user_id: str, db: DatabaseManager = Depends(get_db)) -> Dict:
  9. """List all agents for a user"""
  10. response = db.get(Agent, filters={"user_id": user_id})
  11. return {"status": True, "data": response.data}
  12. @router.get("/{agent_id}")
  13. async def get_agent(agent_id: int, user_id: str, db: DatabaseManager = Depends(get_db)) -> Dict:
  14. """Get a specific agent"""
  15. response = db.get(Agent, filters={"id": agent_id, "user_id": user_id})
  16. if not response.status or not response.data:
  17. raise HTTPException(status_code=404, detail="Agent not found")
  18. return {"status": True, "data": response.data[0]}
  19. @router.post("/")
  20. async def create_agent(agent: Agent, db: DatabaseManager = Depends(get_db)) -> Dict:
  21. """Create a new agent"""
  22. response = db.upsert(agent)
  23. if not response.status:
  24. raise HTTPException(status_code=400, detail=response.message)
  25. return {"status": True, "data": response.data}
  26. @router.delete("/{agent_id}")
  27. async def delete_agent(agent_id: int, user_id: str, db: DatabaseManager = Depends(get_db)) -> Dict:
  28. """Delete an agent"""
  29. db.delete(filters={"id": agent_id, "user_id": user_id}, model_class=Agent)
  30. return {"status": True, "message": "Agent deleted successfully"}
  31. # Agent-Model link endpoints
  32. @router.post("/{agent_id}/models/{model_id}")
  33. async def link_agent_model(agent_id: int, model_id: int, db: DatabaseManager = Depends(get_db)) -> Dict:
  34. """Link a model to an agent"""
  35. db.link(link_type="agent_model", primary_id=agent_id, secondary_id=model_id)
  36. return {"status": True, "message": "Model linked to agent successfully"}