chats.py 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 unarchive_all_chats_by_user_id(self, user_id: str) -> bool:
  299. try:
  300. with get_db() as db:
  301. db.query(Chat).filter_by(user_id=user_id).update({"archived": False})
  302. db.commit()
  303. return True
  304. except Exception:
  305. return False
  306. def update_chat_share_id_by_id(
  307. self, id: str, share_id: Optional[str]
  308. ) -> Optional[ChatModel]:
  309. try:
  310. with get_db() as db:
  311. chat = db.get(Chat, id)
  312. chat.share_id = share_id
  313. db.commit()
  314. db.refresh(chat)
  315. return ChatModel.model_validate(chat)
  316. except Exception:
  317. return None
  318. def toggle_chat_pinned_by_id(self, id: str) -> Optional[ChatModel]:
  319. try:
  320. with get_db() as db:
  321. chat = db.get(Chat, id)
  322. chat.pinned = not chat.pinned
  323. chat.updated_at = int(time.time())
  324. db.commit()
  325. db.refresh(chat)
  326. return ChatModel.model_validate(chat)
  327. except Exception:
  328. return None
  329. def toggle_chat_archive_by_id(self, id: str) -> Optional[ChatModel]:
  330. try:
  331. with get_db() as db:
  332. chat = db.get(Chat, id)
  333. chat.archived = not chat.archived
  334. chat.updated_at = int(time.time())
  335. db.commit()
  336. db.refresh(chat)
  337. return ChatModel.model_validate(chat)
  338. except Exception:
  339. return None
  340. def archive_all_chats_by_user_id(self, user_id: str) -> bool:
  341. try:
  342. with get_db() as db:
  343. db.query(Chat).filter_by(user_id=user_id).update({"archived": True})
  344. db.commit()
  345. return True
  346. except Exception:
  347. return False
  348. def get_archived_chat_list_by_user_id(
  349. self,
  350. user_id: str,
  351. filter: Optional[dict] = None,
  352. skip: int = 0,
  353. limit: int = 50,
  354. ) -> list[ChatModel]:
  355. with get_db() as db:
  356. query = db.query(Chat).filter_by(user_id=user_id, archived=True)
  357. if filter:
  358. query_key = filter.get("query")
  359. if query_key:
  360. query = query.filter(Chat.title.ilike(f"%{query_key}%"))
  361. order_by = filter.get("order_by")
  362. direction = filter.get("direction")
  363. if order_by and direction:
  364. if not getattr(Chat, order_by, None):
  365. raise ValueError("Invalid order_by field")
  366. if direction.lower() == "asc":
  367. query = query.order_by(getattr(Chat, order_by).asc())
  368. elif direction.lower() == "desc":
  369. query = query.order_by(getattr(Chat, order_by).desc())
  370. else:
  371. raise ValueError("Invalid direction for ordering")
  372. else:
  373. query = query.order_by(Chat.updated_at.desc())
  374. if skip:
  375. query = query.offset(skip)
  376. if limit:
  377. query = query.limit(limit)
  378. all_chats = query.all()
  379. return [ChatModel.model_validate(chat) for chat in all_chats]
  380. def get_chat_list_by_user_id(
  381. self,
  382. user_id: str,
  383. include_archived: bool = False,
  384. filter: Optional[dict] = None,
  385. skip: int = 0,
  386. limit: int = 50,
  387. ) -> list[ChatModel]:
  388. with get_db() as db:
  389. query = db.query(Chat).filter_by(user_id=user_id)
  390. if not include_archived:
  391. query = query.filter_by(archived=False)
  392. if filter:
  393. query_key = filter.get("query")
  394. if query_key:
  395. query = query.filter(Chat.title.ilike(f"%{query_key}%"))
  396. order_by = filter.get("order_by")
  397. direction = filter.get("direction")
  398. if order_by and direction and getattr(Chat, order_by):
  399. if direction.lower() == "asc":
  400. query = query.order_by(getattr(Chat, order_by).asc())
  401. elif direction.lower() == "desc":
  402. query = query.order_by(getattr(Chat, order_by).desc())
  403. else:
  404. raise ValueError("Invalid direction for ordering")
  405. else:
  406. query = query.order_by(Chat.updated_at.desc())
  407. if skip:
  408. query = query.offset(skip)
  409. if limit:
  410. query = query.limit(limit)
  411. all_chats = query.all()
  412. return [ChatModel.model_validate(chat) for chat in all_chats]
  413. def get_chat_title_id_list_by_user_id(
  414. self,
  415. user_id: str,
  416. include_archived: bool = False,
  417. include_folders: bool = False,
  418. include_pinned: bool = False,
  419. skip: Optional[int] = None,
  420. limit: Optional[int] = None,
  421. ) -> list[ChatTitleIdResponse]:
  422. with get_db() as db:
  423. query = db.query(Chat).filter_by(user_id=user_id)
  424. if not include_folders:
  425. query = query.filter_by(folder_id=None)
  426. if not include_pinned:
  427. query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
  428. if not include_archived:
  429. query = query.filter_by(archived=False)
  430. query = query.order_by(Chat.updated_at.desc()).with_entities(
  431. Chat.id, Chat.title, Chat.updated_at, Chat.created_at
  432. )
  433. if skip:
  434. query = query.offset(skip)
  435. if limit:
  436. query = query.limit(limit)
  437. all_chats = query.all()
  438. # result has to be destructured from sqlalchemy `row` and mapped to a dict since the `ChatModel`is not the returned dataclass.
  439. return [
  440. ChatTitleIdResponse.model_validate(
  441. {
  442. "id": chat[0],
  443. "title": chat[1],
  444. "updated_at": chat[2],
  445. "created_at": chat[3],
  446. }
  447. )
  448. for chat in all_chats
  449. ]
  450. def get_chat_list_by_chat_ids(
  451. self, chat_ids: list[str], skip: int = 0, limit: int = 50
  452. ) -> list[ChatModel]:
  453. with get_db() as db:
  454. all_chats = (
  455. db.query(Chat)
  456. .filter(Chat.id.in_(chat_ids))
  457. .filter_by(archived=False)
  458. .order_by(Chat.updated_at.desc())
  459. .all()
  460. )
  461. return [ChatModel.model_validate(chat) for chat in all_chats]
  462. def get_chat_by_id(self, id: str) -> Optional[ChatModel]:
  463. try:
  464. with get_db() as db:
  465. chat = db.get(Chat, id)
  466. return ChatModel.model_validate(chat)
  467. except Exception:
  468. return None
  469. def get_chat_by_share_id(self, id: str) -> Optional[ChatModel]:
  470. try:
  471. with get_db() as db:
  472. # it is possible that the shared link was deleted. hence,
  473. # we check if the chat is still shared by checking if a chat with the share_id exists
  474. chat = db.query(Chat).filter_by(share_id=id).first()
  475. if chat:
  476. return self.get_chat_by_id(id)
  477. else:
  478. return None
  479. except Exception:
  480. return None
  481. def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]:
  482. try:
  483. with get_db() as db:
  484. chat = db.query(Chat).filter_by(id=id, user_id=user_id).first()
  485. return ChatModel.model_validate(chat)
  486. except Exception:
  487. return None
  488. def get_chats(self, skip: int = 0, limit: int = 50) -> list[ChatModel]:
  489. with get_db() as db:
  490. all_chats = (
  491. db.query(Chat)
  492. # .limit(limit).offset(skip)
  493. .order_by(Chat.updated_at.desc())
  494. )
  495. return [ChatModel.model_validate(chat) for chat in all_chats]
  496. def get_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  497. with get_db() as db:
  498. all_chats = (
  499. db.query(Chat)
  500. .filter_by(user_id=user_id)
  501. .order_by(Chat.updated_at.desc())
  502. )
  503. return [ChatModel.model_validate(chat) for chat in all_chats]
  504. def get_pinned_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  505. with get_db() as db:
  506. all_chats = (
  507. db.query(Chat)
  508. .filter_by(user_id=user_id, pinned=True, archived=False)
  509. .order_by(Chat.updated_at.desc())
  510. )
  511. return [ChatModel.model_validate(chat) for chat in all_chats]
  512. def get_archived_chats_by_user_id(self, user_id: str) -> list[ChatModel]:
  513. with get_db() as db:
  514. all_chats = (
  515. db.query(Chat)
  516. .filter_by(user_id=user_id, archived=True)
  517. .order_by(Chat.updated_at.desc())
  518. )
  519. return [ChatModel.model_validate(chat) for chat in all_chats]
  520. def get_chats_by_user_id_and_search_text(
  521. self,
  522. user_id: str,
  523. search_text: str,
  524. include_archived: bool = False,
  525. skip: int = 0,
  526. limit: int = 60,
  527. ) -> list[ChatModel]:
  528. """
  529. Filters chats based on a search query using Python, allowing pagination using skip and limit.
  530. """
  531. search_text = search_text.replace("\u0000", "").lower().strip()
  532. if not search_text:
  533. return self.get_chat_list_by_user_id(
  534. user_id, include_archived, filter={}, skip=skip, limit=limit
  535. )
  536. search_text_words = search_text.split(" ")
  537. # search_text might contain 'tag:tag_name' format so we need to extract the tag_name, split the search_text and remove the tags
  538. tag_ids = [
  539. word.replace("tag:", "").replace(" ", "_").lower()
  540. for word in search_text_words
  541. if word.startswith("tag:")
  542. ]
  543. # Extract folder names - handle spaces and case insensitivity
  544. folders = Folders.search_folders_by_names(
  545. user_id,
  546. [
  547. word.replace("folder:", "")
  548. for word in search_text_words
  549. if word.startswith("folder:")
  550. ],
  551. )
  552. folder_ids = [folder.id for folder in folders]
  553. is_pinned = None
  554. if "pinned:true" in search_text_words:
  555. is_pinned = True
  556. elif "pinned:false" in search_text_words:
  557. is_pinned = False
  558. is_archived = None
  559. if "archived:true" in search_text_words:
  560. is_archived = True
  561. elif "archived:false" in search_text_words:
  562. is_archived = False
  563. is_shared = None
  564. if "shared:true" in search_text_words:
  565. is_shared = True
  566. elif "shared:false" in search_text_words:
  567. is_shared = False
  568. search_text_words = [
  569. word
  570. for word in search_text_words
  571. if (
  572. not word.startswith("tag:")
  573. and not word.startswith("folder:")
  574. and not word.startswith("pinned:")
  575. and not word.startswith("archived:")
  576. and not word.startswith("shared:")
  577. )
  578. ]
  579. search_text = " ".join(search_text_words)
  580. with get_db() as db:
  581. query = db.query(Chat).filter(Chat.user_id == user_id)
  582. if is_archived is not None:
  583. query = query.filter(Chat.archived == is_archived)
  584. elif not include_archived:
  585. query = query.filter(Chat.archived == False)
  586. if is_pinned is not None:
  587. query = query.filter(Chat.pinned == is_pinned)
  588. if is_shared is not None:
  589. if is_shared:
  590. query = query.filter(Chat.share_id.isnot(None))
  591. else:
  592. query = query.filter(Chat.share_id.is_(None))
  593. if folder_ids:
  594. query = query.filter(Chat.folder_id.in_(folder_ids))
  595. query = query.order_by(Chat.updated_at.desc())
  596. # Check if the database dialect is either 'sqlite' or 'postgresql'
  597. dialect_name = db.bind.dialect.name
  598. if dialect_name == "sqlite":
  599. # SQLite case: using JSON1 extension for JSON searching
  600. sqlite_content_sql = (
  601. "EXISTS ("
  602. " SELECT 1 "
  603. " FROM json_each(Chat.chat, '$.messages') AS message "
  604. " WHERE LOWER(message.value->>'content') LIKE '%' || :content_key || '%'"
  605. ")"
  606. )
  607. sqlite_content_clause = text(sqlite_content_sql)
  608. query = query.filter(
  609. or_(
  610. Chat.title.ilike(bindparam("title_key")), sqlite_content_clause
  611. ).params(title_key=f"%{search_text}%", content_key=search_text)
  612. )
  613. # Check if there are any tags to filter, it should have all the tags
  614. if "none" in tag_ids:
  615. query = query.filter(
  616. text(
  617. """
  618. NOT EXISTS (
  619. SELECT 1
  620. FROM json_each(Chat.meta, '$.tags') AS tag
  621. )
  622. """
  623. )
  624. )
  625. elif tag_ids:
  626. query = query.filter(
  627. and_(
  628. *[
  629. text(
  630. f"""
  631. EXISTS (
  632. SELECT 1
  633. FROM json_each(Chat.meta, '$.tags') AS tag
  634. WHERE tag.value = :tag_id_{tag_idx}
  635. )
  636. """
  637. ).params(**{f"tag_id_{tag_idx}": tag_id})
  638. for tag_idx, tag_id in enumerate(tag_ids)
  639. ]
  640. )
  641. )
  642. elif dialect_name == "postgresql":
  643. # PostgreSQL doesn't allow null bytes in text. We filter those out by checking
  644. # the JSON representation for \u0000 before attempting text extraction
  645. postgres_content_sql = (
  646. "EXISTS ("
  647. " SELECT 1 "
  648. " FROM json_array_elements(Chat.chat->'messages') AS message "
  649. " WHERE message->'content' IS NOT NULL "
  650. " AND (message->'content')::text NOT LIKE '%\\u0000%' "
  651. " AND LOWER(message->>'content') LIKE '%' || :content_key || '%'"
  652. ")"
  653. )
  654. postgres_content_clause = text(postgres_content_sql)
  655. # Also filter out chats with null bytes in title
  656. query = query.filter(text("Chat.title::text NOT LIKE '%\\x00%'"))
  657. query = query.filter(
  658. or_(
  659. Chat.title.ilike(bindparam("title_key")),
  660. postgres_content_clause,
  661. ).params(title_key=f"%{search_text}%", content_key=search_text)
  662. )
  663. # Check if there are any tags to filter, it should have all the tags
  664. if "none" in tag_ids:
  665. query = query.filter(
  666. text(
  667. """
  668. NOT EXISTS (
  669. SELECT 1
  670. FROM json_array_elements_text(Chat.meta->'tags') AS tag
  671. )
  672. """
  673. )
  674. )
  675. elif tag_ids:
  676. query = query.filter(
  677. and_(
  678. *[
  679. text(
  680. f"""
  681. EXISTS (
  682. SELECT 1
  683. FROM json_array_elements_text(Chat.meta->'tags') AS tag
  684. WHERE tag = :tag_id_{tag_idx}
  685. )
  686. """
  687. ).params(**{f"tag_id_{tag_idx}": tag_id})
  688. for tag_idx, tag_id in enumerate(tag_ids)
  689. ]
  690. )
  691. )
  692. else:
  693. raise NotImplementedError(
  694. f"Unsupported dialect: {db.bind.dialect.name}"
  695. )
  696. # Perform pagination at the SQL level
  697. all_chats = query.offset(skip).limit(limit).all()
  698. log.info(f"The number of chats: {len(all_chats)}")
  699. # Validate and return chats
  700. return [ChatModel.model_validate(chat) for chat in all_chats]
  701. def get_chats_by_folder_id_and_user_id(
  702. self, folder_id: str, user_id: str, skip: int = 0, limit: int = 60
  703. ) -> list[ChatModel]:
  704. with get_db() as db:
  705. query = db.query(Chat).filter_by(folder_id=folder_id, user_id=user_id)
  706. query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
  707. query = query.filter_by(archived=False)
  708. query = query.order_by(Chat.updated_at.desc())
  709. if skip:
  710. query = query.offset(skip)
  711. if limit:
  712. query = query.limit(limit)
  713. all_chats = query.all()
  714. return [ChatModel.model_validate(chat) for chat in all_chats]
  715. def get_chats_by_folder_ids_and_user_id(
  716. self, folder_ids: list[str], user_id: str
  717. ) -> list[ChatModel]:
  718. with get_db() as db:
  719. query = db.query(Chat).filter(
  720. Chat.folder_id.in_(folder_ids), Chat.user_id == user_id
  721. )
  722. query = query.filter(or_(Chat.pinned == False, Chat.pinned == None))
  723. query = query.filter_by(archived=False)
  724. query = query.order_by(Chat.updated_at.desc())
  725. all_chats = query.all()
  726. return [ChatModel.model_validate(chat) for chat in all_chats]
  727. def update_chat_folder_id_by_id_and_user_id(
  728. self, id: str, user_id: str, folder_id: str
  729. ) -> Optional[ChatModel]:
  730. try:
  731. with get_db() as db:
  732. chat = db.get(Chat, id)
  733. chat.folder_id = folder_id
  734. chat.updated_at = int(time.time())
  735. chat.pinned = False
  736. db.commit()
  737. db.refresh(chat)
  738. return ChatModel.model_validate(chat)
  739. except Exception:
  740. return None
  741. def get_chat_tags_by_id_and_user_id(self, id: str, user_id: str) -> list[TagModel]:
  742. with get_db() as db:
  743. chat = db.get(Chat, id)
  744. tags = chat.meta.get("tags", [])
  745. return [Tags.get_tag_by_name_and_user_id(tag, user_id) for tag in tags]
  746. def get_chat_list_by_user_id_and_tag_name(
  747. self, user_id: str, tag_name: str, skip: int = 0, limit: int = 50
  748. ) -> list[ChatModel]:
  749. with get_db() as db:
  750. query = db.query(Chat).filter_by(user_id=user_id)
  751. tag_id = tag_name.replace(" ", "_").lower()
  752. log.info(f"DB dialect name: {db.bind.dialect.name}")
  753. if db.bind.dialect.name == "sqlite":
  754. # SQLite JSON1 querying for tags within the meta JSON field
  755. query = query.filter(
  756. text(
  757. f"EXISTS (SELECT 1 FROM json_each(Chat.meta, '$.tags') WHERE json_each.value = :tag_id)"
  758. )
  759. ).params(tag_id=tag_id)
  760. elif db.bind.dialect.name == "postgresql":
  761. # PostgreSQL JSON query for tags within the meta JSON field (for `json` type)
  762. query = query.filter(
  763. text(
  764. "EXISTS (SELECT 1 FROM json_array_elements_text(Chat.meta->'tags') elem WHERE elem = :tag_id)"
  765. )
  766. ).params(tag_id=tag_id)
  767. else:
  768. raise NotImplementedError(
  769. f"Unsupported dialect: {db.bind.dialect.name}"
  770. )
  771. all_chats = query.all()
  772. log.debug(f"all_chats: {all_chats}")
  773. return [ChatModel.model_validate(chat) for chat in all_chats]
  774. def add_chat_tag_by_id_and_user_id_and_tag_name(
  775. self, id: str, user_id: str, tag_name: str
  776. ) -> Optional[ChatModel]:
  777. tag = Tags.get_tag_by_name_and_user_id(tag_name, user_id)
  778. if tag is None:
  779. tag = Tags.insert_new_tag(tag_name, user_id)
  780. try:
  781. with get_db() as db:
  782. chat = db.get(Chat, id)
  783. tag_id = tag.id
  784. if tag_id not in chat.meta.get("tags", []):
  785. chat.meta = {
  786. **chat.meta,
  787. "tags": list(set(chat.meta.get("tags", []) + [tag_id])),
  788. }
  789. db.commit()
  790. db.refresh(chat)
  791. return ChatModel.model_validate(chat)
  792. except Exception:
  793. return None
  794. def count_chats_by_tag_name_and_user_id(self, tag_name: str, user_id: str) -> int:
  795. with get_db() as db: # Assuming `get_db()` returns a session object
  796. query = db.query(Chat).filter_by(user_id=user_id, archived=False)
  797. # Normalize the tag_name for consistency
  798. tag_id = tag_name.replace(" ", "_").lower()
  799. if db.bind.dialect.name == "sqlite":
  800. # SQLite JSON1 support for querying the tags inside the `meta` JSON field
  801. query = query.filter(
  802. text(
  803. f"EXISTS (SELECT 1 FROM json_each(Chat.meta, '$.tags') WHERE json_each.value = :tag_id)"
  804. )
  805. ).params(tag_id=tag_id)
  806. elif db.bind.dialect.name == "postgresql":
  807. # PostgreSQL JSONB support for querying the tags inside the `meta` JSON field
  808. query = query.filter(
  809. text(
  810. "EXISTS (SELECT 1 FROM json_array_elements_text(Chat.meta->'tags') elem WHERE elem = :tag_id)"
  811. )
  812. ).params(tag_id=tag_id)
  813. else:
  814. raise NotImplementedError(
  815. f"Unsupported dialect: {db.bind.dialect.name}"
  816. )
  817. # Get the count of matching records
  818. count = query.count()
  819. # Debugging output for inspection
  820. log.info(f"Count of chats for tag '{tag_name}': {count}")
  821. return count
  822. def count_chats_by_folder_id_and_user_id(self, folder_id: str, user_id: str) -> int:
  823. with get_db() as db:
  824. query = db.query(Chat).filter_by(user_id=user_id)
  825. query = query.filter_by(folder_id=folder_id)
  826. count = query.count()
  827. log.info(f"Count of chats for folder '{folder_id}': {count}")
  828. return count
  829. def delete_tag_by_id_and_user_id_and_tag_name(
  830. self, id: str, user_id: str, tag_name: str
  831. ) -> bool:
  832. try:
  833. with get_db() as db:
  834. chat = db.get(Chat, id)
  835. tags = chat.meta.get("tags", [])
  836. tag_id = tag_name.replace(" ", "_").lower()
  837. tags = [tag for tag in tags if tag != tag_id]
  838. chat.meta = {
  839. **chat.meta,
  840. "tags": list(set(tags)),
  841. }
  842. db.commit()
  843. return True
  844. except Exception:
  845. return False
  846. def delete_all_tags_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  847. try:
  848. with get_db() as db:
  849. chat = db.get(Chat, id)
  850. chat.meta = {
  851. **chat.meta,
  852. "tags": [],
  853. }
  854. db.commit()
  855. return True
  856. except Exception:
  857. return False
  858. def delete_chat_by_id(self, id: str) -> bool:
  859. try:
  860. with get_db() as db:
  861. db.query(Chat).filter_by(id=id).delete()
  862. db.commit()
  863. return True and self.delete_shared_chat_by_chat_id(id)
  864. except Exception:
  865. return False
  866. def delete_chat_by_id_and_user_id(self, id: str, user_id: str) -> bool:
  867. try:
  868. with get_db() as db:
  869. db.query(Chat).filter_by(id=id, user_id=user_id).delete()
  870. db.commit()
  871. return True and self.delete_shared_chat_by_chat_id(id)
  872. except Exception:
  873. return False
  874. def delete_chats_by_user_id(self, user_id: str) -> bool:
  875. try:
  876. with get_db() as db:
  877. self.delete_shared_chats_by_user_id(user_id)
  878. db.query(Chat).filter_by(user_id=user_id).delete()
  879. db.commit()
  880. return True
  881. except Exception:
  882. return False
  883. def delete_chats_by_user_id_and_folder_id(
  884. self, user_id: str, folder_id: str
  885. ) -> bool:
  886. try:
  887. with get_db() as db:
  888. db.query(Chat).filter_by(user_id=user_id, folder_id=folder_id).delete()
  889. db.commit()
  890. return True
  891. except Exception:
  892. return False
  893. def delete_shared_chats_by_user_id(self, user_id: str) -> bool:
  894. try:
  895. with get_db() as db:
  896. chats_by_user = db.query(Chat).filter_by(user_id=user_id).all()
  897. shared_chat_ids = [f"shared-{chat.id}" for chat in chats_by_user]
  898. db.query(Chat).filter(Chat.user_id.in_(shared_chat_ids)).delete()
  899. db.commit()
  900. return True
  901. except Exception:
  902. return False
  903. Chats = ChatTable()