chats.py 36 KB

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