memories.py 3.56 KB
Newer Older
1
from pydantic import BaseModel, ConfigDict
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
from typing import List, Union, Optional

4
from sqlalchemy import Column, String, BigInteger, Text
5

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
6
from apps.webui.internal.db import Base, get_db
Timothy J. Baek's avatar
Timothy J. Baek committed
7
8
9
10
11
12
13
14
15

import time
import uuid

####################
# Memory DB Schema
####################


16
17
class Memory(Base):
    __tablename__ = "memory"
Timothy J. Baek's avatar
Timothy J. Baek committed
18

19
20
    id = Column(String, primary_key=True)
    user_id = Column(String)
21
    content = Column(Text)
22
23
    updated_at = Column(BigInteger)
    created_at = Column(BigInteger)
Timothy J. Baek's avatar
Timothy J. Baek committed
24
25
26
27
28
29
30
31
32


class MemoryModel(BaseModel):
    id: str
    user_id: str
    content: str
    updated_at: int  # timestamp in epoch
    created_at: int  # timestamp in epoch

33
34
    model_config = ConfigDict(from_attributes=True)

Timothy J. Baek's avatar
Timothy J. Baek committed
35
36
37
38
39
40
41
42
43
44
45
46
47

####################
# Forms
####################


class MemoriesTable:

    def insert_new_memory(
        self,
        user_id: str,
        content: str,
    ) -> Optional[MemoryModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

        with get_db() as db:
            id = str(uuid.uuid4())

            memory = MemoryModel(
                **{
                    "id": id,
                    "user_id": user_id,
                    "content": content,
                    "created_at": int(time.time()),
                    "updated_at": int(time.time()),
                }
            )
            result = Memory(**memory.model_dump())
            db.add(result)
            db.commit()
            db.refresh(result)
            if result:
                return MemoryModel.model_validate(result)
            else:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
69

70
    def update_memory_by_id(
Peter De-Ath's avatar
Peter De-Ath committed
71
72
73
74
        self,
        id: str,
        content: str,
    ) -> Optional[MemoryModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
75
76
77
78
79
80
81
82
83
84
        with get_db() as db:

            try:
                db.query(Memory).filter_by(id=id).update(
                    {"content": content, "updated_at": int(time.time())}
                )
                db.commit()
                return self.get_memory_by_id(id)
            except:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
85

86
    def get_memories(self) -> List[MemoryModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
87
88
89
90
91
92
93
        with get_db() as db:

            try:
                memories = db.query(Memory).all()
                return [MemoryModel.model_validate(memory) for memory in memories]
            except:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
94

95
    def get_memories_by_user_id(self, user_id: str) -> List[MemoryModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
96
97
98
99
100
101
102
        with get_db() as db:

            try:
                memories = db.query(Memory).filter_by(user_id=user_id).all()
                return [MemoryModel.model_validate(memory) for memory in memories]
            except:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
103

104
    def get_memory_by_id(self, id: str) -> Optional[MemoryModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
105
106
107
108
109
110
111
        with get_db() as db:

            try:
                memory = db.get(Memory, id)
                return MemoryModel.model_validate(memory)
            except:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
112

113
    def delete_memory_by_id(self, id: str) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
114
115
116
117
118
        with get_db() as db:

            try:
                db.query(Memory).filter_by(id=id).delete()
                return True
Timothy J. Baek's avatar
Timothy J. Baek committed
119

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
120
121
            except:
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
122

123
    def delete_memories_by_user_id(self, user_id: str) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
124
125
126
127
128
129
130
        with get_db() as db:

            try:
                db.query(Memory).filter_by(user_id=user_id).delete()
                return True
            except:
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
131

132
    def delete_memory_by_id_and_user_id(self, id: str, user_id: str) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
133
134
135
136
137
138
139
        with get_db() as db:

            try:
                db.query(Memory).filter_by(id=id, user_id=user_id).delete()
                return True
            except:
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
140
141


142
Memories = MemoriesTable()