routes.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from fastapi import APIRouter, Depends, HTTPException, Query
  2. from sqlalchemy import text
  3. from sqlalchemy.orm import Session
  4. from core_domain import MemoryScopeType, MemoryStatus, ServiceHealth
  5. from app.application.services import MemoryApplicationService
  6. from app.db.session import get_db
  7. from app.domain.repositories import MemoryItemRepository
  8. from app.schemas.memory import (
  9. MemoryCreateRequest,
  10. MemoryResponse,
  11. MemorySearchRequest,
  12. MemorySearchResultResponse,
  13. MemoryStatusUpdateRequest,
  14. )
  15. router = APIRouter()
  16. def get_memory_application_service(db: Session = Depends(get_db)) -> MemoryApplicationService:
  17. return MemoryApplicationService(memory_repository=MemoryItemRepository(db))
  18. @router.get("/health", response_model=ServiceHealth)
  19. def health_check(db: Session = Depends(get_db)) -> ServiceHealth:
  20. db.execute(text("SELECT 1"))
  21. return ServiceHealth(service="memory-service", status="ok", database="ok")
  22. @router.post("", response_model=MemoryResponse)
  23. def create_memory(
  24. payload: MemoryCreateRequest,
  25. service: MemoryApplicationService = Depends(get_memory_application_service),
  26. ) -> MemoryResponse:
  27. entity = service.create_memory(payload)
  28. return MemoryResponse.from_entity(entity)
  29. @router.get("", response_model=list[MemoryResponse])
  30. def list_memories(
  31. tenant_id: str = Query(...),
  32. scope_type: MemoryScopeType | None = Query(default=None),
  33. scope_id: str | None = Query(default=None),
  34. status: MemoryStatus | None = Query(default="active"),
  35. limit: int = Query(default=100, ge=1, le=500),
  36. service: MemoryApplicationService = Depends(get_memory_application_service),
  37. ) -> list[MemoryResponse]:
  38. return [
  39. MemoryResponse.from_entity(item)
  40. for item in service.list_memories(
  41. tenant_id=tenant_id,
  42. scope_type=scope_type,
  43. scope_id=scope_id,
  44. status=status,
  45. limit=limit,
  46. )
  47. ]
  48. @router.post("/search", response_model=list[MemorySearchResultResponse])
  49. def search_memories(
  50. payload: MemorySearchRequest,
  51. service: MemoryApplicationService = Depends(get_memory_application_service),
  52. ) -> list[MemorySearchResultResponse]:
  53. return [
  54. MemorySearchResultResponse.from_entity(item, score=score)
  55. for item, score in service.search_memories(payload)
  56. ]
  57. @router.patch("/{memory_id}/status", response_model=MemoryResponse)
  58. def update_memory_status(
  59. memory_id: str,
  60. payload: MemoryStatusUpdateRequest,
  61. service: MemoryApplicationService = Depends(get_memory_application_service),
  62. ) -> MemoryResponse:
  63. entity = service.update_memory_status(memory_id=memory_id, payload=payload)
  64. if entity is None:
  65. raise HTTPException(status_code=404, detail=f"memory not found: {memory_id}")
  66. return MemoryResponse.from_entity(entity)