File size: 2,904 Bytes
03a551b
 
 
 
 
 
 
 
 
 
 
 
38ccc4e
03a551b
 
 
 
 
 
 
 
65127e8
03a551b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38ccc4e
03a551b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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}")