2024-05-19 15:00:07 +00:00
|
|
|
import time
|
|
|
|
import uuid
|
2024-08-27 22:10:27 +00:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from apps.webui.internal.db import Base, get_db
|
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from sqlalchemy import BigInteger, Column, String, Text
|
2024-05-19 15:00:07 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Memory DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class Memory(Base):
|
|
|
|
__tablename__ = "memory"
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
id = Column(String, primary_key=True)
|
|
|
|
user_id = Column(String)
|
2024-06-24 11:21:51 +00:00
|
|
|
content = Column(Text)
|
2024-06-18 13:03:31 +00:00
|
|
|
updated_at = Column(BigInteger)
|
|
|
|
created_at = Column(BigInteger)
|
2024-05-19 15:00:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MemoryModel(BaseModel):
|
|
|
|
id: str
|
|
|
|
user_id: str
|
|
|
|
content: str
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
2024-05-19 15:00:07 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class MemoriesTable:
|
|
|
|
def insert_new_memory(
|
|
|
|
self,
|
|
|
|
user_id: str,
|
|
|
|
content: str,
|
|
|
|
) -> Optional[MemoryModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
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
|
2024-06-15 09:36:17 +00:00
|
|
|
|
2024-06-14 20:23:34 +00:00
|
|
|
def update_memory_by_id(
|
2024-06-13 01:01:50 +00:00
|
|
|
self,
|
|
|
|
id: str,
|
|
|
|
content: str,
|
|
|
|
) -> Optional[MemoryModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
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)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return None
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-08-14 12:46:31 +00:00
|
|
|
def get_memories(self) -> list[MemoryModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
try:
|
|
|
|
memories = db.query(Memory).all()
|
|
|
|
return [MemoryModel.model_validate(memory) for memory in memories]
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return None
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-08-14 12:46:31 +00:00
|
|
|
def get_memories_by_user_id(self, user_id: str) -> list[MemoryModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
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]
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return None
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_memory_by_id(self, id: str) -> Optional[MemoryModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
try:
|
|
|
|
memory = db.get(Memory, id)
|
|
|
|
return MemoryModel.model_validate(memory)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return None
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def delete_memory_by_id(self, id: str) -> bool:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
try:
|
|
|
|
db.query(Memory).filter_by(id=id).delete()
|
2024-07-06 15:10:58 +00:00
|
|
|
db.commit()
|
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
return True
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return False
|
2024-05-19 15:00:07 +00:00
|
|
|
|
2024-06-24 11:06:15 +00:00
|
|
|
def delete_memories_by_user_id(self, user_id: str) -> bool:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
try:
|
|
|
|
db.query(Memory).filter_by(user_id=user_id).delete()
|
2024-07-06 15:10:58 +00:00
|
|
|
db.commit()
|
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
return True
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return False
|
2024-05-19 16:26:24 +00:00
|
|
|
|
2024-06-24 11:55:18 +00:00
|
|
|
def delete_memory_by_id_and_user_id(self, id: str, user_id: str) -> bool:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
try:
|
|
|
|
db.query(Memory).filter_by(id=id, user_id=user_id).delete()
|
2024-07-06 15:10:58 +00:00
|
|
|
db.commit()
|
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
return True
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-07-04 06:32:39 +00:00
|
|
|
return False
|
2024-05-19 15:00:07 +00:00
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Memories = MemoriesTable()
|