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

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
        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)
83
            except Exception:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
84
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
85

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

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

Michael Poluektov's avatar
Michael Poluektov committed
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
        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]
101
            except Exception:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
102
                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
        with get_db() as db:

            try:
                memory = db.get(Memory, id)
                return MemoryModel.model_validate(memory)
110
            except Exception:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
111
                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
        with get_db() as db:

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

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
120
                return True
Timothy J. Baek's avatar
Timothy J. Baek committed
121

122
            except Exception:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
123
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
124

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

            try:
                db.query(Memory).filter_by(user_id=user_id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
130
131
                db.commit()

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
132
                return True
133
            except Exception:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
134
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
135

136
    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
137
138
139
140
        with get_db() as db:

            try:
                db.query(Memory).filter_by(id=id, user_id=user_id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
141
142
                db.commit()

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
143
                return True
144
            except Exception:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
145
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
146
147


148
Memories = MemoriesTable()