1
0

chats.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. import logging
  2. import json
  3. import time
  4. import uuid
  5. from typing import Optional
  6. from open_webui.internal.db import Base, get_db
  7. from open_webui.models.tags import TagModel, Tag, Tags
  8. from open_webui.env import SRC_LOG_LEVELS
  9. from pydantic import BaseModel, ConfigDict
  10. from sqlalchemy import BigInteger, Boolean, Column, String, Text, JSON
  11. from sqlalchemy import or_, func, select, and_, text
  12. from sqlalchemy.sql import exists
  13. from sqlalchemy.sql.expression import bindparam
  14. ####################
  15. # Chat DB Schema
  16. ####################
  17. log = logging.getLogger(__name__)
  18. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  19. class Chat(Base):
  20. __tablename__ = "chat"
  21. id = Column(String, primary_key=True)
  22. user_id = Column(String)
  23. title = Column(Text)
  24. chat = Column(JSON)
  25. created_at = Column(BigInteger)
  26. updated_at = Column(BigInteger)
  27. share_id = Column(Text, unique=True, nullable=True)
  28. archived = Column(Boolean, default=False)
  29. pinned = Column(Boolean, default=False, nullable=True)
  30. meta = Column(JSON, server_default="{}")
  31. folder_id = Column(Text, nullable=True)
  32. class ChatModel(BaseModel):
  33. model_config = ConfigDict(from_attributes=True)
  34. id: str
  35. user_id: str
  36. title: str
  37. chat: dict
  38. created_at: int # timestamp in epoch
  39. updated_at: int # timestamp in epoch
  40. share_id: Optional[str] = None
  41. archived: bool = False
  42. pinned: Optional[bool] = False
  43. meta: dict = {}
  44. folder_id: Optional[str] = None
  45. ####################
  46. # Forms
  47. ####################
  48. class ChatForm(BaseModel):
  49. chat: dict
  50. folder_id: Optional[str] = None
  51. class ChatImportForm(ChatForm):
  52. meta: Optional[dict] = {}
  53. pinned: Optional[bool] = False
  54. folder_id: Optional[str] = None
  55. created_at: Optional[int] = None
  56. updated_at: Optional[int] = None
  57. class ChatTitleMessagesForm(BaseModel):
  58. title: str
  59. messages: list[dict]
  60. class ChatTitleForm(BaseModel):
  61. title: str
  62. class ChatResponse(BaseModel):
  63. id: str
  64. user_id: str
  65. title: str
  66. chat: dict
  67. updated_at: int # timestamp in epoch
  68. created_at: int # timestamp in epoch
  69. share_id: Optional[str] = None # id of the chat to be shared
  70. archived: bool
  71. pinned: Optional[bool] = False
  72. meta: dict = {}
  73. folder_id: Optional[str] = None
  74. class ChatTitleIdResponse(BaseModel):
  75. id: str
  76. title: str
  77. updated_at: int
  78. created_at: int
  79. class ChatTable:
  80. def insert_new_chat(self, user_id: str, form_data: ChatForm) -> Optional[ChatModel]:
  81. with get_db() as db:
  82. id = str(uuid.uuid4())
  83. chat = ChatModel(
  84. **{
  85. "id": id,
  86. "user_id": user_id,
  87. "title": (
  88. form_data.chat["title"]
  89. if "title" in form_data.chat
  90. else "New Chat"
  91. ),
  92. "chat": form_data.chat,
  93. "folder_id": form_data.folder_id,
  94. "created_at": int(time.time()),
  95. "updated_at": int(time.time()),
  96. }
  97. )
  98. result = Chat(**chat.model_dump())
  99. db.add(result)
  100. db.commit()
  101. db.refresh(result)
  102. return ChatModel.model_validate(result) if result else None
  103. def import_chat(
  104. self, user_id: str, form_data: ChatImportForm
  105. ) -> Optional[ChatModel]:
  106. with get_db() as db:
  107. id = str(uuid.uuid4())
  108. chat = ChatModel(
  109. **{
  110. "id": id,
  111. "user_id": user_id,
  112. "title": (
  113. form_data.chat["title"]
  114. if "title" in form_data.chat
  115. else "New Chat"
  116. ),
  117. "chat": form_data.chat,
  118. "meta": form_data.meta,
  119. "pinned": form_data.pinned,
  120. "folder_id": form_data.folder_id,
  121. "created_at": (
  122. form_data.created_at
  123. if form_data.created_at
  124. else int(time.time())
  125. ),
  126. "updated_at": (
  127. form_data.updated_at
  128. if form_data.updated_at
  129. else int(time.time())
  130. ),
  131. }
  132. )
  133. result = Chat(**chat.model_dump())
  134. db.add(result)
  135. db.commit()
  136. db.refresh(result)
  137. return ChatModel.model_validate(result) if result else None
  138. def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
  139. try:
  140. with get_db() as db:
  141. chat_item = db.get(Chat, id)
  142. chat_item.chat = chat
  143. chat_item.title = chat["title"] if "title" in chat else "New Chat"
  144. chat_item.updated_at = int(time.time())
  145. db.commit()
  146. db.refresh(chat_item)
  147. return ChatModel.model_validate(chat_item)
  148. except Exception:
  149. return None
  150. def update_chat_title_by_id(self, id: str, title: str) -> Optional[ChatModel]:
  151. chat = self.get_chat_by_id(id)
  152. if chat is None:
  153. return None
  154. chat = chat.chat
  155. chat["title"] = title
  156. return self.update_chat_by_id(id, chat)
  157. def update_chat_tags_by_id(
  158. self, id: str, tags: list[str], user
  159. ) -> Optional[ChatModel]:
  160. chat = self.get_chat_by_id(id)
  161. if chat is None:
  162. return None
  163. self.delete_all_tags_by_id_and_user_id(id, user.id)
  164. for tag in chat.meta.get("tags", []):
  165. if self.count_chats_by_tag_name_and_user_id(tag, user.id) == 0:
  166. Tags.delete_tag_by_name_and_user_id(tag, user.id)
  167. for tag_name in tags:
  168. if tag_name.lower() == "none":
  169. continue
  170. self.add_chat_tag_by_id_and_user_id_and_tag_name(id, user.id, tag_name)
  171. return self.get_chat_by_id(id)
  172. def get_chat_title_by_id(self, id: str) -> Optional[str]:
  173. chat = self.get_chat_by_id(id)
  174. if chat is None:
  175. return None
  176. return chat.chat.get("title", "New Chat")
  177. def get_messages_by_chat_id(self, id: str) -> Optional[dict]:
  178. chat = self.get_chat_by_id(id)
  179. if chat is None:
  180. return None
  181. return chat.chat.get("history", {}).get("messages", {}) or {}
  182. def get_message_by_id_and_message_id(
  183. self, id: str, message_id: str
  184. ) -> Optional[dict]:
  185. chat = self.get_chat_by_id(id)
  186. if chat is None:
  187. return None
  188. return chat.chat.get("history", {}).get("messages", {}).get(message_id, {})
  189. def upsert_message_to_chat_by_id_and_message_id(
  190. self, id: str, message_id: str, message: dict
  191. ) -> Optional[ChatModel]:
  192. chat = self.get_chat_by_id(id)
  193. if chat is None:
  194. return None
  195. # Sanitize message content for null characters before upserting
  196. if isinstance(message.get("content"), str):
  197. message["content"] = message["content"].replace("\x00", "")
  198. chat = chat.chat
  199. history = chat.get("history", {})
  200. if message_id in history.get("messages", {}):
  201. history["messages"][message_id] = {
  202. **history["messages"][message_id],
  203. **message,
  204. }
  205. else:
  206. history["messages"][message_id] = message
  207. history["currentId"] = message_id
  208. chat["history"] = history
  209. return self.update_chat_by_id(id, chat)
  210. def add_message_status_to_chat_by_id_and_message_id(
  211. self, id: str, message_id: str, status: dict
  212. ) -> Optional[ChatModel]:
  213. chat = self.get_chat_by_id(id)
  214. if chat is None:
  215. return None
  216. chat = chat.chat
  217. history = chat.get("history", {})
  218. if message_id in history.get("messages", {}):
  219. status_history = history["messages"][message_id].get("statusHistory", [])
  220. status_history.append(status)
  221. history["messages"][message_id]["statusHistory"] = status_history
  222. chat["history"] = history
  223. return self.update_chat_by_id(id, chat)
  224. def insert_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
  225. with get_db() as db:
  226. # Get the existing chat to share
  227. chat = db.get(Chat, chat_id)
  228. # Check if the chat is already shared
  229. if chat.share_id:
  230. return self.get_chat_by_id_and_user_id(chat.share_id, "shared")
  231. # Create a new chat with the same data, but with a new ID
  232. shared_chat = ChatModel(
  233. **{
  234. "id": str(uuid.uuid4()),
  235. "user_id": f"shared-{chat_id}",
  236. "title": chat.title,
  237. "chat": chat.chat,
  238. "created_at": chat.created_at,
  239. "updated_at": int(time.time()),
  240. }
  241. )
  242. shared_result = Chat(**shared_chat.model_dump())
  243. db.add(shared_result)
  244. db.commit()
  245. db.refresh(shared_result)
  246. # Update the original chat with the share_id
  247. result = (
  248. db.query(Chat)
  249. .filter_by(id=chat_id)
  250. .update({"share_id": shared_chat.id})
  251. )
  252. db.commit()
  253. return shared_chat if (shared_result and result) else None
  254. def update_shared_chat_by_chat_id(self, chat_id: str) -> Optional[ChatModel]:
  255. try:
  256. with get_db() as db:
  257. chat = db.get(Chat, chat_id)
  258. shared_chat = (
  259. db.query(Chat).filter_by(user_id=f"shared-{chat_id}").first()
  260. )
  261. if shared_chat is None:
  262. return self.insert_shared_chat_by_chat_id(chat_id)
  263. shared_chat.title = chat.title
  264. shared_chat.chat = chat.chat
  265. shared_chat.updated_at = int(time.time())
  266. db.commit()
  267. db.refresh(shared_chat)
  268. return ChatModel.model_validate(shared_chat)
  269. except Exception:
  270. return None
  271. def delete_shared_chat_by_chat_id(self, chat_id: str) -> bool:
  272. try:
  273. with get_db() as db:
  274. db.query(Chat).filter_by(user_id=f"shared-{chat_id}").delete()
  275. db.commit()
  276. return True
  277. except Exception:
  278. return False
  279. def update_chat_share_id_by_id(
  280. self, id: str, share_id: Optional[str]
  281. ) -> Optional[ChatModel]:
  282. try:
  283. with get_db() as db:
  284. chat = db.get(Chat, id)
  285. chat.share_id = share_id
  286. db.commit()
  287. db.refresh(chat)
  288. return ChatModel.model_validate(chat)
  289. except Exception:
  290. return None
  291. def toggle_chat_pinned_by_id(self, id: str) -> Optional[ChatModel]:
  292. try:
  293. with get_db() as db:
  294. chat = db.get(Chat, id)
  295. chat.pinned = not chat.pinned
  296. chat.updated_at = int(time.time())
  297. db.commit()
  298. db.refresh(chat)
  299. return ChatModel.model_validate(chat)
  300. except Exception:
  301. return None
  302. def toggle_chat_archive_by_id(self, id: str) -> Optional[ChatModel]:
  303. try:
  304. with get_db() as db:
  305. chat = db.get(Chat, id)
  306. chat.archived = not chat.archived
  307. chat.updated_at = int(time.time())
  308. db.commit()
  309. db.refresh(chat)
  310. return ChatModel.model_validate(chat)
  311. except Exception:
  312. return None
  313. def archive_all_chats_by_user_id(self, user_id: str) -> bool:
  314. try:
  315. with get_db() as db:
  316. db.query(Chat).filter_by(user_id=user_id).update({"archived": True})
  317. db.commit()
  318. return True
  319. except Exception:
  320. return False
  321. def get_archived_chat_list_by_user_id(
  322. self,
  323. user_id: str,
  324. filter: Optional[dict] = None,
  325. skip: int = 0,
  326. limit: int = 50,
  327. ) -> list[ChatModel]:
  328. with get_db() as db:
  329. query = db.query(Chat).filter_by(user_id=user_id, archived=True)
  330. if filter:
  331. query_key = filter.get("query")
  332. if query_key:
  333. query = query.filter(Chat.title.ilike(f"%{query_key}%"))
  334. order_by = filter.get("order_by")
  335. direction = filter.get("direction")
  336. if order_by and direction and getattr(Chat, order_by):
  337. if direction.lower() == "asc":
  338. query = query.order_by(getattr(Chat, order_by).asc())
  339. elif direction.lower() == "desc":
  340. query = query.order_by(getattr(Chat, order_by).desc())
  341. else:
  342. raise ValueError("Invalid direction for ordering")
  343. else:
  344. query = query.order_by(Chat.updated_at.desc())
  345. if skip:
  346. query = query.offset(skip)
  347. if limit:
  348. query = query.limit(limit)
  349. all_chats = query.all()
  350. return [ChatModel.model_validate(chat) for chat in all_chats]
  351. def get_chat_list_by_user_id(
  352. self,
  353. user_id: str,
  354. include_archived: bool = False,
  355. filter: Optional[dict] = None,
  356. skip: int = 0,
  357. limit: int = 50,
  358. ) -> list[ChatModel]:
  359. with get_db() as db:
  360. query = db.query(Chat).filter_by(user_id=user_id)
  361. if not include_archived:
  362. query = query.filter_by(archived=False)
  363. if filter:
  364. query_key = filter.get("query")
  365. if query_key:
  366. query = query.filter(Chat.title.ilike(f"%{query_key}%"))
  367. order_by = filter.get("order_by")
  368. direction = filter.get("direction")
  369. if order_by and direction and getattr(Chat, order_by):
  370. if direction.lower() == "asc":
  371. query = query.order_by(getattr(Chat, order_by).asc())
  372. elif direction.lower() == "desc":
  373. query = query.order_by(getattr(Chat, order_by).desc())
  374. else:
  375. raise ValueError("Invalid direction for ordering")
  376. else:
  377. query = query.order_by(Chat.updated_at.desc())
  378. if skip:
  379. query = query.offset(skip)
  380. if limit:
  381. query = query.limit(limit)
  382. all_chats = query.all()
  383. return [ChatModel.model_validate(chat) for chat in all_chats]
  384. def get_chat_title_id_list_by_user_id(
  385. self,
  386. user_id: str,
  387. include_archived: bool = False,
  388. skip: Optional[int] = None,
  389. limit: Optional[int] = None,
  390. ) -> list[ChatTitleIdResponse]:
  391. with get_db() as db:
  392. query = db.query(Chat).filter_by(user_id=user_id).filter_by(folder_id=None)
  393. query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
  394. if not include_archived:
  395. query = query.filter_by(archived=False)
  396. query = query.order_by(Chat.updated_at.desc()).with_entities(
  397. Chat.id, Chat.title, Chat.updated_at, Chat.created_at
  398. )
  399. if skip:
  400. query = query.offset(skip)
  401. if limit:
  402. query = query.limit(limit)
  403. all_chats = query.all()
  404. # result has to be destructured from sqlalchemy `row` and mapped to a dict since the `ChatModel`is not the returned dataclass.
  405. return [
  406. ChatTitleIdResponse.model_validate(
  407. {
  408. "id": chat[0],
  409. "title": chat[1],
  410. "updated_at": chat[2],
  411. "created_at": chat[3],
  412. }
  413. )
  414. for chat in all_chats
  415. ]
  416. def get_chat_list_by_chat_ids(
  417. self, chat_ids: list[str], skip: int = 0, limit: int = 50
  418. ) -> list[ChatModel]:
  419. with get_db() as db:
  420. all_chats = (
  421. db.query(Chat)
  422. .filter(Chat.id.in_(chat_ids))
  423. .filter_by(archived=False)
  424. .order_by(Chat.updated_at.desc())
  425. .all()
  426. )
  427. return [ChatModel.model_validate(chat) for chat in all_chats]
  428. def get_chat_by_id(self, id: str) -> Optional[ChatModel]:
  429. try:
  430. with get_db() as db:
  431. chat = db.get(Chat, id)
  432. return ChatModel.model_validate(chat)
  433. except Exception:
  434. return None
  435. def get_chat_by_share_id(self, id: str) -> Optional[ChatModel]:
  436. try:
  437. with get_db() as db:
  438. # it is possible that the shared link was deleted. hence,
  439. # we check if the chat is still shared by checking if a chat with the share_id exists
  440. chat = db.query(Chat).filter_by(share_id=id).first()
  441. if chat:
  442. return self.get_chat_by_id(id)
  443. else:
  444. return None
  445. except Exception:
  446. return None
  447. def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]:
  448. try:
  449. with get_db() as db:
  450. chat = db.query(Chat).filter_by(id=id, user_id=user_id).first()
  451. return ChatModel.model_validate(chat)
  452. except Exception:
  453. return None
  454. def get_chats(self, skip: int = 0, limit: int = 50) -> list[ChatModel]:
  455. with get_db() as db:
  456. all_chats = (
  457. db.query(Chat)
  458. # .limit(limit).offset(skip)
  459. .order_by(Chat.updated_at.desc())
  460. )
  461. return [ChatModel.model_validate(chat) for chat in all_chats]
  462. def get_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  463. with get_db() as db:
  464. all_chats = (
  465. db.query(Chat)
  466. .filter_by(user_id=user_id)
  467. .order_by(Chat.updated_at.desc())
  468. )
  469. return [ChatModel.model_validate(chat) for chat in all_chats]
  470. def get_pinned_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  471. with get_db() as db:
  472. all_chats = (
  473. db.query(Chat)
  474. .filter_by(user_id=user_id, pinned=True, archived=False)
  475. .order_by(Chat.updated_at.desc())
  476. )
  477. return [ChatModel.model_validate(chat) for chat in all_chats]
  478. def get_archived_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  479. with get_db() as db:
  480. all_chats = (
  481. db.query(Chat)
  482. .filter_by(user_id=user_id, archived=True)
  483. .order_by(Chat.updated_at.desc())
  484. )
  485. return [ChatModel.model_validate(chat) for chat in all_chats]
  486. def get_chats_by_user_id_and_search_text(
  487. self,
  488. user_id: str,
  489. search_text: str,
  490. include_archived: bool = False,
  491. skip: int = 0,
  492. limit: int = 60,
  493. ) -> list[ChatModel]:
  494. """
  495. Filters chats based on a search query using Python, allowing pagination using skip and limit.
  496. """
  497. search_text = search_text.replace("\u0000", "").lower().strip()
  498. if not search_text:
  499. return self.get_chat_list_by_user_id(
  500. user_id, include_archived, filter={}, skip=skip, limit=limit
  501. )
  502. search_text_words = search_text.split(" ")
  503. # search_text might contain 'tag:tag_name' format so we need to extract the tag_name, split the search_text and remove the tags
  504. tag_ids = [
  505. word.replace("tag:", "").replace(" ", "_").lower()
  506. for word in search_text_words
  507. if word.startswith("tag:")
  508. ]
  509. search_text_words = [
  510. word for word in search_text_words if not word.startswith("tag:")
  511. ]
  512. search_text = " ".join(search_text_words)
  513. with get_db() as db:
  514. query = db.query(Chat).filter(Chat.user_id == user_id)
  515. if not include_archived:
  516. query = query.filter(Chat.archived == False)
  517. query = query.order_by(Chat.updated_at.desc())
  518. # Check if the database dialect is either 'sqlite' or 'postgresql'
  519. dialect_name = db.bind.dialect.name
  520. if dialect_name == "sqlite":
  521. # SQLite case: using JSON1 extension for JSON searching
  522. sqlite_content_sql = (
  523. "EXISTS ("
  524. " SELECT 1 "
  525. " FROM json_each(Chat.chat, '$.messages') AS message "
  526. " WHERE LOWER(message.value->>'content') LIKE '%' || :content_key || '%'"
  527. ")"
  528. )
  529. sqlite_content_clause = text(sqlite_content_sql)
  530. query = query.filter(
  531. or_(
  532. Chat.title.ilike(bindparam("title_key")), sqlite_content_clause
  533. ).params(title_key=f"%{search_text}%", content_key=search_text)
  534. )
  535. # Check if there are any tags to filter, it should have all the tags
  536. if "none" in tag_ids:
  537. query = query.filter(
  538. text(
  539. """
  540. NOT EXISTS (
  541. SELECT 1
  542. FROM json_each(Chat.meta, '$.tags') AS tag
  543. )
  544. """
  545. )
  546. )
  547. elif tag_ids:
  548. query = query.filter(
  549. and_(
  550. *[
  551. text(
  552. f"""
  553. EXISTS (
  554. SELECT 1
  555. FROM json_each(Chat.meta, '$.tags') AS tag
  556. WHERE tag.value = :tag_id_{tag_idx}
  557. )
  558. """
  559. ).params(**{f"tag_id_{tag_idx}": tag_id})
  560. for tag_idx, tag_id in enumerate(tag_ids)
  561. ]
  562. )
  563. )
  564. elif dialect_name == "postgresql":
  565. # PostgreSQL relies on proper JSON query for search
  566. postgres_content_sql = (
  567. "EXISTS ("
  568. " SELECT 1 "
  569. " FROM json_array_elements(Chat.chat->'messages') AS message "
  570. " WHERE LOWER(message->>'content') LIKE '%' || :content_key || '%'"
  571. ")"
  572. )
  573. postgres_content_clause = text(postgres_content_sql)
  574. query = query.filter(
  575. or_(
  576. Chat.title.ilike(bindparam("title_key")),
  577. postgres_content_clause,
  578. ).params(title_key=f"%{search_text}%", content_key=search_text)
  579. )
  580. # Check if there are any tags to filter, it should have all the tags
  581. if "none" in tag_ids:
  582. query = query.filter(
  583. text(
  584. """
  585. NOT EXISTS (
  586. SELECT 1
  587. FROM json_array_elements_text(Chat.meta->'tags') AS tag
  588. )
  589. """
  590. )
  591. )
  592. elif tag_ids:
  593. query = query.filter(
  594. and_(
  595. *[
  596. text(
  597. f"""
  598. EXISTS (
  599. SELECT 1
  600. FROM json_array_elements_text(Chat.meta->'tags') AS tag
  601. WHERE tag = :tag_id_{tag_idx}
  602. )
  603. """
  604. ).params(**{f"tag_id_{tag_idx}": tag_id})
  605. for tag_idx, tag_id in enumerate(tag_ids)
  606. ]
  607. )
  608. )
  609. else:
  610. raise NotImplementedError(
  611. f"Unsupported dialect: {db.bind.dialect.name}"
  612. )
  613. # Perform pagination at the SQL level
  614. all_chats = query.offset(skip).limit(limit).all()
  615. log.info(f"The number of chats: {len(all_chats)}")
  616. # Validate and return chats
  617. return [ChatModel.model_validate(chat) for chat in all_chats]
  618. def get_chats_by_folder_id_and_user_id(
  619. self, folder_id: str, user_id: str
  620. ) -> list[ChatModel]:
  621. with get_db() as db:
  622. query = db.query(Chat).filter_by(folder_id=folder_id, user_id=user_id)
  623. query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
  624. query = query.filter_by(archived=False)
  625. query = query.order_by(Chat.updated_at.desc())
  626. all_chats = query.all()
  627. return [ChatModel.model_validate(chat) for chat in all_chats]
  628. def get_chats_by_folder_ids_and_user_id(
  629. self, folder_ids: list[str], user_id: str
  630. ) -> list[ChatModel]:
  631. with get_db() as db:
  632. query = db.query(Chat).filter(
  633. Chat.folder_id.in_(folder_ids), Chat.user_id == user_id
  634. )
  635. query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
  636. query = query.filter_by(archived=False)
  637. query = query.order_by(Chat.updated_at.desc())
  638. all_chats = query.all()
  639. return [ChatModel.model_validate(chat) for chat in all_chats]
  640. def update_chat_folder_id_by_id_and_user_id(
  641. self, id: str, user_id: str, folder_id: str
  642. ) -> Optional[ChatModel]:
  643. try:
  644. with get_db() as db:
  645. chat = db.get(Chat, id)
  646. chat.folder_id = folder_id
  647. chat.updated_at = int(time.time())
  648. chat.pinned = False
  649. db.commit()
  650. db.refresh(chat)
  651. return ChatModel.model_validate(chat)
  652. except Exception:
  653. return None
  654. def get_chat_tags_by_id_and_user_id(self, id: str, user_id: str) -> list[TagModel]:
  655. with get_db() as db:
  656. chat = db.get(Chat, id)
  657. tags = chat.meta.get("tags", [])
  658. return [Tags.get_tag_by_name_and_user_id(tag, user_id) for tag in tags]
  659. def get_chat_list_by_user_id_and_tag_name(
  660. self, user_id: str, tag_name: str, skip: int = 0, limit: int = 50
  661. ) -> list[ChatModel]:
  662. with get_db() as db:
  663. query = db.query(Chat).filter_by(user_id=user_id)
  664. tag_id = tag_name.replace(" ", "_").lower()
  665. log.info(f"DB dialect name: {db.bind.dialect.name}")
  666. if db.bind.dialect.name == "sqlite":
  667. # SQLite JSON1 querying for tags within the meta JSON field
  668. query = query.filter(
  669. text(
  670. f"EXISTS (SELECT 1 FROM json_each(Chat.meta, '$.tags') WHERE json_each.value = :tag_id)"
  671. )
  672. ).params(tag_id=tag_id)
  673. elif db.bind.dialect.name == "postgresql":
  674. # PostgreSQL JSON query for tags within the meta JSON field (for `json` type)
  675. query = query.filter(
  676. text(
  677. "EXISTS (SELECT 1 FROM json_array_elements_text(Chat.meta->'tags') elem WHERE elem = :tag_id)"
  678. )
  679. ).params(tag_id=tag_id)
  680. else:
  681. raise NotImplementedError(
  682. f"Unsupported dialect: {db.bind.dialect.name}"
  683. )
  684. all_chats = query.all()
  685. log.debug(f"all_chats: {all_chats}")
  686. return [ChatModel.model_validate(chat) for chat in all_chats]
  687. def add_chat_tag_by_id_and_user_id_and_tag_name(
  688. self, id: str, user_id: str, tag_name: str
  689. ) -> Optional[ChatModel]:
  690. tag = Tags.get_tag_by_name_and_user_id(tag_name, user_id)
  691. if tag is None:
  692. tag = Tags.insert_new_tag(tag_name, user_id)
  693. try:
  694. with get_db() as db:
  695. chat = db.get(Chat, id)
  696. tag_id = tag.id
  697. if tag_id not in chat.meta.get("tags", []):
  698. chat.meta = {
  699. **chat.meta,
  700. "tags": list(set(chat.meta.get("tags", []) + [tag_id])),
  701. }
  702. db.commit()
  703. db.refresh(chat)
  704. return ChatModel.model_validate(chat)
  705. except Exception:
  706. return None
  707. def count_chats_by_tag_name_and_user_id(self, tag_name: str, user_id: str) -> int:
  708. with get_db() as db: # Assuming `get_db()` returns a session object
  709. query = db.query(Chat).filter_by(user_id=user_id, archived=False)
  710. # Normalize the tag_name for consistency
  711. tag_id = tag_name.replace(" ", "_").lower()
  712. if db.bind.dialect.name == "sqlite":
  713. # SQLite JSON1 support for querying the tags inside the `meta` JSON field
  714. query = query.filter(
  715. text(
  716. f"EXISTS (SELECT 1 FROM json_each(Chat.meta, '$.tags') WHERE json_each.value = :tag_id)"
  717. )
  718. ).params(tag_id=tag_id)
  719. elif db.bind.dialect.name == "postgresql":
  720. # PostgreSQL JSONB support for querying the tags inside the `meta` JSON field
  721. query = query.filter(
  722. text(
  723. "EXISTS (SELECT 1 FROM json_array_elements_text(Chat.meta->'tags') elem WHERE elem = :tag_id)"
  724. )
  725. ).params(tag_id=tag_id)
  726. else:
  727. raise NotImplementedError(
  728. f"Unsupported dialect: {db.bind.dialect.name}"
  729. )
  730. # Get the count of matching records
  731. count = query.count()
  732. # Debugging output for inspection
  733. log.info(f"Count of chats for tag '{tag_name}': {count}")
  734. return count
  735. def delete_tag_by_id_and_user_id_and_tag_name(
  736. self, id: str, user_id: str, tag_name: str
  737. ) -> bool:
  738. try:
  739. with get_db() as db:
  740. chat = db.get(Chat, id)
  741. tags = chat.meta.get("tags", [])
  742. tag_id = tag_name.replace(" ", "_").lower()
  743. tags = [tag for tag in tags if tag != tag_id]
  744. chat.meta = {
  745. **chat.meta,
  746. "tags": list(set(tags)),
  747. }
  748. db.commit()
  749. return True
  750. except Exception:
  751. return False
  752. def delete_all_tags_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  753. try:
  754. with get_db() as db:
  755. chat = db.get(Chat, id)
  756. chat.meta = {
  757. **chat.meta,
  758. "tags": [],
  759. }
  760. db.commit()
  761. return True
  762. except Exception:
  763. return False
  764. def delete_chat_by_id(self, id: str) -> bool:
  765. try:
  766. with get_db() as db:
  767. db.query(Chat).filter_by(id=id).delete()
  768. db.commit()
  769. return True and self.delete_shared_chat_by_chat_id(id)
  770. except Exception:
  771. return False
  772. def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  773. try:
  774. with get_db() as db:
  775. db.query(Chat).filter_by(id=id, user_id=user_id).delete()
  776. db.commit()
  777. return True and self.delete_shared_chat_by_chat_id(id)
  778. except Exception:
  779. return False
  780. def delete_chats_by_user_id(self, user_id: str) -> bool:
  781. try:
  782. with get_db() as db:
  783. self.delete_shared_chats_by_user_id(user_id)
  784. db.query(Chat).filter_by(user_id=user_id).delete()
  785. db.commit()
  786. return True
  787. except Exception:
  788. return False
  789. def delete_chats_by_user_id_and_folder_id(
  790. self, user_id: str, folder_id: str
  791. ) -> bool:
  792. try:
  793. with get_db() as db:
  794. db.query(Chat).filter_by(user_id=user_id, folder_id=folder_id).delete()
  795. db.commit()
  796. return True
  797. except Exception:
  798. return False
  799. def delete_shared_chats_by_user_id(self, user_id: str) -> bool:
  800. try:
  801. with get_db() as db:
  802. chats_by_user = db.query(Chat).filter_by(user_id=user_id).all()
  803. shared_chat_ids = [f"shared-{chat.id}" for chat in chats_by_user]
  804. db.query(Chat).filter(Chat.user_id.in_(shared_chat_ids)).delete()
  805. db.commit()
  806. return True
  807. except Exception:
  808. return False
  809. Chats = ChatTable()