chats.py 32 KB

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