routes.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. from fastapi import APIRouter, Depends, HTTPException, Query
  2. from sqlalchemy import text
  3. from sqlalchemy.orm import Session
  4. from core_domain import ServiceHealth
  5. from app.application.services import AgentApplicationService
  6. from app.db.session import get_db
  7. from app.domain.repositories import (
  8. AgentDefinitionRepository,
  9. AgentRunRepository,
  10. AgentVersionRepository,
  11. )
  12. from app.schemas.agent import (
  13. AgentCreateRequest,
  14. AgentResponse,
  15. AgentRunCreateRequest,
  16. AgentRunResponse,
  17. AgentRunStatusUpdateRequest,
  18. AgentStatusUpdateRequest,
  19. AgentVersionCreateRequest,
  20. AgentVersionResponse,
  21. )
  22. router = APIRouter()
  23. def get_agent_application_service(db: Session = Depends(get_db)) -> AgentApplicationService:
  24. return AgentApplicationService(
  25. agent_repository=AgentDefinitionRepository(db),
  26. agent_version_repository=AgentVersionRepository(db),
  27. agent_run_repository=AgentRunRepository(db),
  28. )
  29. @router.get("/health", response_model=ServiceHealth)
  30. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  31. db.execute(text("SELECT 1"))
  32. return ServiceHealth(service="agent-service", status="ok", database="ok")
  33. @router.post("", response_model=AgentResponse)
  34. def create_agent(
  35. payload: AgentCreateRequest,
  36. service: AgentApplicationService = Depends(get_agent_application_service),
  37. ) -> AgentResponse:
  38. entity = service.create_agent(payload)
  39. return AgentResponse.from_entity(entity)
  40. @router.get("", response_model=list[AgentResponse])
  41. def list_agents(
  42. tenant_id: str = Query(...),
  43. service: AgentApplicationService = Depends(get_agent_application_service),
  44. ) -> list[AgentResponse]:
  45. return [AgentResponse.from_entity(item) for item in service.list_agents(tenant_id=tenant_id)]
  46. @router.patch("/{agent_id}/status", response_model=AgentResponse)
  47. def update_agent_status(
  48. agent_id: str,
  49. payload: AgentStatusUpdateRequest,
  50. service: AgentApplicationService = Depends(get_agent_application_service),
  51. ) -> AgentResponse:
  52. entity = service.update_agent_status(agent_id=agent_id, payload=payload)
  53. if entity is None:
  54. raise HTTPException(status_code=404, detail=f"agent not found: {agent_id}")
  55. return AgentResponse.from_entity(entity)
  56. @router.post("/versions", response_model=AgentVersionResponse)
  57. def create_agent_version(
  58. payload: AgentVersionCreateRequest,
  59. service: AgentApplicationService = Depends(get_agent_application_service),
  60. ) -> AgentVersionResponse:
  61. try:
  62. entity = service.create_agent_version(payload)
  63. except ValueError as exc:
  64. raise HTTPException(status_code=422, detail=str(exc)) from exc
  65. return AgentVersionResponse.from_entity(entity)
  66. @router.get("/versions", response_model=list[AgentVersionResponse])
  67. def list_agent_versions(
  68. tenant_id: str = Query(...),
  69. agent_id: str = Query(...),
  70. service: AgentApplicationService = Depends(get_agent_application_service),
  71. ) -> list[AgentVersionResponse]:
  72. return [
  73. AgentVersionResponse.from_entity(item)
  74. for item in service.list_agent_versions(tenant_id=tenant_id, agent_id=agent_id)
  75. ]
  76. @router.post("/runs", response_model=AgentRunResponse)
  77. def create_agent_run(
  78. payload: AgentRunCreateRequest,
  79. service: AgentApplicationService = Depends(get_agent_application_service),
  80. ) -> AgentRunResponse:
  81. try:
  82. entity = service.create_agent_run(payload)
  83. except ValueError as exc:
  84. raise HTTPException(status_code=422, detail=str(exc)) from exc
  85. return AgentRunResponse.from_entity(entity)
  86. @router.get("/runs", response_model=list[AgentRunResponse])
  87. def list_agent_runs(
  88. tenant_id: str = Query(...),
  89. agent_id: str | None = Query(default=None),
  90. session_id: str | None = Query(default=None),
  91. service: AgentApplicationService = Depends(get_agent_application_service),
  92. ) -> list[AgentRunResponse]:
  93. return [
  94. AgentRunResponse.from_entity(item)
  95. for item in service.list_agent_runs(
  96. tenant_id=tenant_id,
  97. agent_id=agent_id,
  98. session_id=session_id,
  99. )
  100. ]
  101. @router.post("/runs/{agent_run_id}/status", response_model=AgentRunResponse)
  102. def update_agent_run_status(
  103. agent_run_id: str,
  104. payload: AgentRunStatusUpdateRequest,
  105. service: AgentApplicationService = Depends(get_agent_application_service),
  106. ) -> AgentRunResponse:
  107. entity = service.update_agent_run_status(agent_run_id=agent_run_id, payload=payload)
  108. if entity is None:
  109. raise HTTPException(status_code=404, detail=f"agent_run not found: {agent_run_id}")
  110. return AgentRunResponse.from_entity(entity)