chats.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. import json
  2. import time
  3. import uuid
  4. from typing import Optional
  5. from open_webui.apps.webui.internal.db import Base, get_db
  6. from pydantic import BaseModel, ConfigDict
  7. from sqlalchemy import BigInteger, Boolean, Column, String, Text
  8. ####################
  9. # Chat DB Schema
  10. ####################
  11. class Chat(Base):
  12. __tablename__ = "chat"
  13. id = Column(String, primary_key=True)
  14. user_id = Column(String)
  15. title = Column(Text)
  16. chat = Column(Text) # Save Chat JSON as Text
  17. created_at = Column(BigInteger)
  18. updated_at = Column(BigInteger)
  19. share_id = Column(Text, unique=True, nullable=True)
  20. archived = Column(Boolean, default=False)
  21. class ChatModel(BaseModel):
  22. model_config = ConfigDict(from_attributes=True)
  23. id: str
  24. user_id: str
  25. title: str
  26. chat: str
  27. created_at: int # timestamp in epoch
  28. updated_at: int # timestamp in epoch
  29. share_id: Optional[str] = None
  30. archived: bool = False
  31. ####################
  32. # Forms
  33. ####################
  34. class ChatForm(BaseModel):
  35. chat: dict
  36. class ChatTitleForm(BaseModel):
  37. title: str
  38. class ChatResponse(BaseModel):
  39. id: str
  40. user_id: str
  41. title: str
  42. chat: dict
  43. updated_at: int # timestamp in epoch
  44. created_at: int # timestamp in epoch
  45. share_id: Optional[str] = None # id of the chat to be shared
  46. archived: bool
  47. class ChatTitleIdResponse(BaseModel):
  48. id: str
  49. title: str
  50. updated_at: int
  51. created_at: int
  52. class ChatTable:
  53. def insert_new_chat(self, user_id: str, form_data: ChatForm) -> Optional[ChatModel]:
  54. with get_db() as db:
  55. id = str(uuid.uuid4())
  56. chat = ChatModel(
  57. **{
  58. "id": id,
  59. "user_id": user_id,
  60. "title": (
  61. form_data.chat["title"]
  62. if "title" in form_data.chat
  63. else "New Chat"
  64. ),
  65. "chat": json.dumps(form_data.chat),
  66. "created_at": int(time.time()),
  67. "updated_at": int(time.time()),
  68. }
  69. )
  70. result = Chat(**chat.model_dump())
  71. db.add(result)
  72. db.commit()
  73. db.refresh(result)
  74. return ChatModel.model_validate(result) if result else None
  75. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  76. try:
  77. with get_db() as db:
  78. chat_obj = db.get(Chat, id)
  79. chat_obj.chat = json.dumps(chat)
  80. chat_obj.title = chat["title"] if "title" in chat else "New Chat"
  81. chat_obj.updated_at = int(time.time())
  82. db.commit()
  83. db.refresh(chat_obj)
  84. return ChatModel.model_validate(chat_obj)
  85. except Exception:
  86. return None
  87. def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
  88. with get_db() as db:
  89. # Get the existing chat to share
  90. chat = db.get(Chat, chat_id)
  91. # Check if the chat is already shared
  92. if chat.share_id:
  93. return self.get_chat_by_id_and_user_id(chat.share_id, "shared")
  94. # Create a new chat with the same data, but with a new ID
  95. shared_chat = ChatModel(
  96. **{
  97. "id": str(uuid.uuid4()),
  98. "user_id": f"shared-{chat_id}",
  99. "title": chat.title,
  100. "chat": chat.chat,
  101. "created_at": chat.created_at,
  102. "updated_at": int(time.time()),
  103. }
  104. )
  105. shared_result = Chat(**shared_chat.model_dump())
  106. db.add(shared_result)
  107. db.commit()
  108. db.refresh(shared_result)
  109. # Update the original chat with the share_id
  110. result = (
  111. db.query(Chat)
  112. .filter_by(id=chat_id)
  113. .update({"share_id": shared_chat.id})
  114. )
  115. db.commit()
  116. return shared_chat if (shared_result and result) else None
  117. def update_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
  118. try:
  119. with get_db() as db:
  120. print("update_shared_chat_by_id")
  121. chat = db.get(Chat, chat_id)
  122. print(chat)
  123. chat.title = chat.title
  124. chat.chat = chat.chat
  125. db.commit()
  126. db.refresh(chat)
  127. return self.get_chat_by_id(chat.share_id)
  128. except Exception:
  129. return None
  130. def delete_shared_chat_by_chat_id(self, chat_id: str) -> bool:
  131. try:
  132. with get_db() as db:
  133. db.query(Chat).filter_by(user_id=f"shared-{chat_id}").delete()
  134. db.commit()
  135. return True
  136. except Exception:
  137. return False
  138. def update_chat_share_id_by_id(
  139. self, id: str, share_id: Optional[str]
  140. ) -> Optional[ChatModel]:
  141. try:
  142. with get_db() as db:
  143. chat = db.get(Chat, id)
  144. chat.share_id = share_id
  145. db.commit()
  146. db.refresh(chat)
  147. return ChatModel.model_validate(chat)
  148. except Exception:
  149. return None
  150. def toggle_chat_archive_by_id(self, id: str) -> Optional[ChatModel]:
  151. try:
  152. with get_db() as db:
  153. chat = db.get(Chat, id)
  154. chat.archived = not chat.archived
  155. db.commit()
  156. db.refresh(chat)
  157. return ChatModel.model_validate(chat)
  158. except Exception:
  159. return None
  160. def archive_all_chats_by_user_id(self, user_id: str) -> bool:
  161. try:
  162. with get_db() as db:
  163. db.query(Chat).filter_by(user_id=user_id).update({"archived": True})
  164. db.commit()
  165. return True
  166. except Exception:
  167. return False
  168. def get_archived_chat_list_by_user_id(
  169. self, user_id: str, skip: int = 0, limit: int = 50
  170. ) -> list[ChatModel]:
  171. with get_db() as db:
  172. all_chats = (
  173. db.query(Chat)
  174. .filter_by(user_id=user_id, archived=True)
  175. .order_by(Chat.updated_at.desc())
  176. # .limit(limit).offset(skip)
  177. .all()
  178. )
  179. return [ChatModel.model_validate(chat) for chat in all_chats]
  180. def get_chat_list_by_user_id(
  181. self,
  182. user_id: str,
  183. include_archived: bool = False,
  184. skip: int = 0,
  185. limit: int = 50,
  186. ) -> list[ChatModel]:
  187. with get_db() as db:
  188. query = db.query(Chat).filter_by(user_id=user_id)
  189. if not include_archived:
  190. query = query.filter_by(archived=False)
  191. all_chats = (
  192. query.order_by(Chat.updated_at.desc())
  193. # .limit(limit).offset(skip)
  194. .all()
  195. )
  196. return [ChatModel.model_validate(chat) for chat in all_chats]
  197. def get_chat_title_id_list_by_user_id(
  198. self,
  199. user_id: str,
  200. include_archived: bool = False,
  201. skip: Optional[int] = None,
  202. limit: Optional[int] = None,
  203. ) -> list[ChatTitleIdResponse]:
  204. with get_db() as db:
  205. query = db.query(Chat).filter_by(user_id=user_id)
  206. if not include_archived:
  207. query = query.filter_by(archived=False)
  208. query = query.order_by(Chat.updated_at.desc()).with_entities(
  209. Chat.id, Chat.title, Chat.updated_at, Chat.created_at
  210. )
  211. if limit:
  212. query = query.limit(limit)
  213. if skip:
  214. query = query.offset(skip)
  215. all_chats = query.all()
  216. # result has to be destrctured from sqlalchemy `row` and mapped to a dict since the `ChatModel`is not the returned dataclass.
  217. return [
  218. ChatTitleIdResponse.model_validate(
  219. {
  220. "id": chat[0],
  221. "title": chat[1],
  222. "updated_at": chat[2],
  223. "created_at": chat[3],
  224. }
  225. )
  226. for chat in all_chats
  227. ]
  228. def get_chat_list_by_chat_ids(
  229. self, chat_ids: list[str], skip: int = 0, limit: int = 50
  230. ) -> list[ChatModel]:
  231. with get_db() as db:
  232. all_chats = (
  233. db.query(Chat)
  234. .filter(Chat.id.in_(chat_ids))
  235. .filter_by(archived=False)
  236. .order_by(Chat.updated_at.desc())
  237. .all()
  238. )
  239. return [ChatModel.model_validate(chat) for chat in all_chats]
  240. def get_chat_by_id(self, id: str) -> Optional[ChatModel]:
  241. try:
  242. with get_db() as db:
  243. chat = db.get(Chat, id)
  244. return ChatModel.model_validate(chat)
  245. except Exception:
  246. return None
  247. def get_chat_by_share_id(self, id: str) -> Optional[ChatModel]:
  248. try:
  249. with get_db() as db:
  250. chat = db.query(Chat).filter_by(share_id=id).first()
  251. if chat:
  252. return self.get_chat_by_id(id)
  253. else:
  254. return None
  255. except Exception:
  256. return None
  257. def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]:
  258. try:
  259. with get_db() as db:
  260. chat = db.query(Chat).filter_by(id=id, user_id=user_id).first()
  261. return ChatModel.model_validate(chat)
  262. except Exception:
  263. return None
  264. def get_chats(self, skip: int = 0, limit: int = 50) -> list[ChatModel]:
  265. with get_db() as db:
  266. all_chats = (
  267. db.query(Chat)
  268. # .limit(limit).offset(skip)
  269. .order_by(Chat.updated_at.desc())
  270. )
  271. return [ChatModel.model_validate(chat) for chat in all_chats]
  272. def get_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  273. with get_db() as db:
  274. all_chats = (
  275. db.query(Chat)
  276. .filter_by(user_id=user_id)
  277. .order_by(Chat.updated_at.desc())
  278. )
  279. return [ChatModel.model_validate(chat) for chat in all_chats]
  280. def get_archived_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  281. with get_db() as db:
  282. all_chats = (
  283. db.query(Chat)
  284. .filter_by(user_id=user_id, archived=True)
  285. .order_by(Chat.updated_at.desc())
  286. )
  287. return [ChatModel.model_validate(chat) for chat in all_chats]
  288. def delete_chat_by_id(self, id: str) -> bool:
  289. try:
  290. with get_db() as db:
  291. db.query(Chat).filter_by(id=id).delete()
  292. db.commit()
  293. return True and self.delete_shared_chat_by_chat_id(id)
  294. except Exception:
  295. return False
  296. def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  297. try:
  298. with get_db() as db:
  299. db.query(Chat).filter_by(id=id, user_id=user_id).delete()
  300. db.commit()
  301. return True and self.delete_shared_chat_by_chat_id(id)
  302. except Exception:
  303. return False
  304. def delete_chats_by_user_id(self, user_id: str) -> bool:
  305. try:
  306. with get_db() as db:
  307. self.delete_shared_chats_by_user_id(user_id)
  308. db.query(Chat).filter_by(user_id=user_id).delete()
  309. db.commit()
  310. return True
  311. except Exception:
  312. return False
  313. def delete_shared_chats_by_user_id(self, user_id: str) -> bool:
  314. try:
  315. with get_db() as db:
  316. chats_by_user = db.query(Chat).filter_by(user_id=user_id).all()
  317. shared_chat_ids = [f"shared-{chat.id}" for chat in chats_by_user]
  318. db.query(Chat).filter(Chat.user_id.in_(shared_chat_ids)).delete()
  319. db.commit()
  320. return True
  321. except Exception:
  322. return False
  323. Chats = ChatTable()