chats.py 33 KB

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