1
0

chats.py 33 KB

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