from fastapi import APIRouter, Depends, HTTPException, status, Path import uuid from api.dependencies.auth import get_current_user from api.dependencies.memory import get_mem0_client from db.schemas import memory as memory_schemas from workflow.memory_client import memory_client router = APIRouter() @router.get( "/", response_model=memory_schemas.GetAllMemoriesResponse, summary="Get all memories for a user" ) async def get_all_user_memories( user_id: uuid.UUID = Depends(get_current_user), client = Depends(get_mem0_client) ): try: memories_data = client.get_all(version="v2", filters={"user_id": str(user_id)}) formatted_memories = [ memory_schemas.MemoryItemResponse(**mem_item) for mem_item in memories_data ] return memory_schemas.GetAllMemoriesResponse(memories=formatted_memories) except Exception as e: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to retrieve memories: {e}") @router.delete( "/{memory_id}", response_model=memory_schemas.DeleteMemoryResponse, status_code=status.HTTP_200_OK, summary="Delete a single memory by ID", description="Deletes a specific long-term memory item by its unique ID." ) async def delete_single_memory( memory_id: str = Path(..., description="The ID of the memory to delete."), user_id: uuid.UUID = Depends(get_current_user), client = Depends(get_mem0_client) ): try: response = client.delete(memory_id=memory_id) if response.get("message") == "Memory deleted successfully!": return memory_schemas.DeleteMemoryResponse(message=response["message"]) else: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Mem0 reported an issue: {response.get('message', 'Unknown error')}") except Exception as e: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to delete memory: {e}") @router.delete( "/", response_model=memory_schemas.DeleteAllUserMemoriesResponse, status_code=status.HTTP_200_OK, summary="Delete all memories for a user" ) async def delete_all_user_memories( user_id: uuid.UUID = Depends(get_current_user), client = Depends(get_mem0_client) ): try: response = client.delete_all(user_id=str(user_id)) if response.get("message") == "Memories deleted successfully!": return memory_schemas.DeleteAllUserMemoriesResponse(message=response["message"]) else: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Mem0 reported an issue: {response.get('message', 'Unknown error')}") except Exception as e: raise HTTPException(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Failed to delete all memories: {e}")