chats.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. from fastapi import Depends, Request, HTTPException, status
  2. from datetime import datetime, timedelta
  3. from typing import List, Union, Optional
  4. from utils.utils import get_verified_user, get_admin_user
  5. from fastapi import APIRouter
  6. from pydantic import BaseModel
  7. import json
  8. import logging
  9. from apps.webui.models.users import Users
  10. from apps.webui.models.chats import (
  11. ChatModel,
  12. ChatResponse,
  13. ChatTitleForm,
  14. ChatForm,
  15. ChatTitleIdResponse,
  16. Chats,
  17. )
  18. from apps.webui.models.tags import (
  19. TagModel,
  20. ChatIdTagModel,
  21. ChatIdTagForm,
  22. ChatTagsResponse,
  23. Tags,
  24. )
  25. from constants import ERROR_MESSAGES
  26. from config import SRC_LOG_LEVELS, ENABLE_ADMIN_EXPORT
  27. log = logging.getLogger(__name__)
  28. log.setLevel(SRC_LOG_LEVELS["MODELS"])
  29. router = APIRouter()
  30. ############################
  31. # GetChatList
  32. ############################
  33. @router.get("/", response_model=List[ChatTitleIdResponse])
  34. @router.get("/list", response_model=List[ChatTitleIdResponse])
  35. async def get_session_user_chat_list(
  36. user=Depends(get_verified_user), skip: int = 0, limit: int = 50
  37. ):
  38. return Chats.get_chat_title_id_list_by_user_id(user.id, skip=skip, limit=limit)
  39. ############################
  40. # DeleteAllChats
  41. ############################
  42. @router.delete("/", response_model=bool)
  43. async def delete_all_user_chats(request: Request, user=Depends(get_verified_user)):
  44. if (
  45. user.role == "user"
  46. and not request.app.state.config.USER_PERMISSIONS["chat"]["deletion"]
  47. ):
  48. raise HTTPException(
  49. status_code=status.HTTP_401_UNAUTHORIZED,
  50. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  51. )
  52. result = Chats.delete_chats_by_user_id(user.id)
  53. return result
  54. ############################
  55. # GetUserChatList
  56. ############################
  57. @router.get("/list/user/{user_id}", response_model=List[ChatTitleIdResponse])
  58. async def get_user_chat_list_by_user_id(
  59. user_id: str,
  60. user=Depends(get_admin_user),
  61. skip: int = 0,
  62. limit: int = 50,
  63. ):
  64. return Chats.get_chat_list_by_user_id(
  65. user_id, include_archived=True, skip=skip, limit=limit
  66. )
  67. ############################
  68. # CreateNewChat
  69. ############################
  70. @router.post("/new", response_model=Optional[ChatResponse])
  71. async def create_new_chat(form_data: ChatForm, user=Depends(get_verified_user)):
  72. try:
  73. chat = Chats.insert_new_chat(user.id, form_data)
  74. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  75. except Exception as e:
  76. log.exception(e)
  77. raise HTTPException(
  78. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  79. )
  80. ############################
  81. # GetChats
  82. ############################
  83. @router.get("/all", response_model=List[ChatResponse])
  84. async def get_user_chats(user=Depends(get_verified_user)):
  85. return [
  86. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  87. for chat in Chats.get_chats_by_user_id(user.id)
  88. ]
  89. ############################
  90. # GetArchivedChats
  91. ############################
  92. @router.get("/all/archived", response_model=List[ChatResponse])
  93. async def get_user_archived_chats(user=Depends(get_verified_user)):
  94. return [
  95. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  96. for chat in Chats.get_archived_chats_by_user_id(user.id)
  97. ]
  98. ############################
  99. # GetAllChatsInDB
  100. ############################
  101. @router.get("/all/db", response_model=List[ChatResponse])
  102. async def get_all_user_chats_in_db(user=Depends(get_admin_user)):
  103. if not ENABLE_ADMIN_EXPORT:
  104. raise HTTPException(
  105. status_code=status.HTTP_401_UNAUTHORIZED,
  106. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  107. )
  108. return [
  109. ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  110. for chat in Chats.get_chats()
  111. ]
  112. ############################
  113. # GetArchivedChats
  114. ############################
  115. @router.get("/archived", response_model=List[ChatTitleIdResponse])
  116. async def get_archived_session_user_chat_list(
  117. user=Depends(get_verified_user), skip: int = 0, limit: int = 50
  118. ):
  119. return Chats.get_archived_chat_list_by_user_id(user.id, skip, limit)
  120. ############################
  121. # ArchiveAllChats
  122. ############################
  123. @router.post("/archive/all", response_model=bool)
  124. async def archive_all_chats(user=Depends(get_verified_user)):
  125. return Chats.archive_all_chats_by_user_id(user.id)
  126. ############################
  127. # GetSharedChatById
  128. ############################
  129. @router.get("/share/{share_id}", response_model=Optional[ChatResponse])
  130. async def get_shared_chat_by_id(share_id: str, user=Depends(get_verified_user)):
  131. if user.role == "pending":
  132. raise HTTPException(
  133. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  134. )
  135. if user.role == "user":
  136. chat = Chats.get_chat_by_share_id(share_id)
  137. elif user.role == "admin":
  138. chat = Chats.get_chat_by_id(share_id)
  139. if chat:
  140. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  141. else:
  142. raise HTTPException(
  143. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  144. )
  145. ############################
  146. # GetChatsByTags
  147. ############################
  148. class TagNameForm(BaseModel):
  149. name: str
  150. skip: Optional[int] = 0
  151. limit: Optional[int] = 50
  152. @router.post("/tags", response_model=List[ChatTitleIdResponse])
  153. async def get_user_chat_list_by_tag_name(
  154. form_data: TagNameForm, user=Depends(get_verified_user)
  155. ):
  156. chat_ids = [
  157. chat_id_tag.chat_id
  158. for chat_id_tag in Tags.get_chat_ids_by_tag_name_and_user_id(
  159. form_data.name, user.id
  160. )
  161. ]
  162. chats = Chats.get_chat_list_by_chat_ids(chat_ids, form_data.skip, form_data.limit)
  163. if len(chats) == 0:
  164. Tags.delete_tag_by_tag_name_and_user_id(form_data.name, user.id)
  165. return chats
  166. ############################
  167. # GetAllTags
  168. ############################
  169. @router.get("/tags/all", response_model=List[TagModel])
  170. async def get_all_tags(user=Depends(get_verified_user)):
  171. try:
  172. tags = Tags.get_tags_by_user_id(user.id)
  173. return tags
  174. except Exception as e:
  175. log.exception(e)
  176. raise HTTPException(
  177. status_code=status.HTTP_400_BAD_REQUEST, detail=ERROR_MESSAGES.DEFAULT()
  178. )
  179. ############################
  180. # GetChatById
  181. ############################
  182. @router.get("/{id}", response_model=Optional[ChatResponse])
  183. async def get_chat_by_id(id: str, user=Depends(get_verified_user)):
  184. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  185. if chat:
  186. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  187. else:
  188. raise HTTPException(
  189. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  190. )
  191. ############################
  192. # UpdateChatById
  193. ############################
  194. @router.post("/{id}", response_model=Optional[ChatResponse])
  195. async def update_chat_by_id(
  196. id: str, form_data: ChatForm, user=Depends(get_verified_user)
  197. ):
  198. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  199. if chat:
  200. updated_chat = {**json.loads(chat.chat), **form_data.chat}
  201. chat = Chats.update_chat_by_id(id, updated_chat)
  202. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  203. else:
  204. raise HTTPException(
  205. status_code=status.HTTP_401_UNAUTHORIZED,
  206. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  207. )
  208. ############################
  209. # DeleteChatById
  210. ############################
  211. @router.delete("/{id}", response_model=bool)
  212. async def delete_chat_by_id(request: Request, id: str, user=Depends(get_verified_user)):
  213. if user.role == "admin":
  214. result = Chats.delete_chat_by_id(id)
  215. return result
  216. else:
  217. if not request.app.state.config.USER_PERMISSIONS["chat"]["deletion"]:
  218. raise HTTPException(
  219. status_code=status.HTTP_401_UNAUTHORIZED,
  220. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  221. )
  222. result = Chats.delete_chat_by_id_and_user_id(id, user.id)
  223. return result
  224. ############################
  225. # CloneChat
  226. ############################
  227. @router.get("/{id}/clone", response_model=Optional[ChatResponse])
  228. async def clone_chat_by_id(id: str, user=Depends(get_verified_user)):
  229. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  230. if chat:
  231. chat_body = json.loads(chat.chat)
  232. updated_chat = {
  233. **chat_body,
  234. "originalChatId": chat.id,
  235. "branchPointMessageId": chat_body["history"]["currentId"],
  236. "title": f"Clone of {chat.title}",
  237. }
  238. chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat}))
  239. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  240. else:
  241. raise HTTPException(
  242. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  243. )
  244. ############################
  245. # ArchiveChat
  246. ############################
  247. @router.get("/{id}/archive", response_model=Optional[ChatResponse])
  248. async def archive_chat_by_id(id: str, user=Depends(get_verified_user)):
  249. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  250. if chat:
  251. chat = Chats.toggle_chat_archive_by_id(id)
  252. return ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)})
  253. else:
  254. raise HTTPException(
  255. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  256. )
  257. ############################
  258. # ShareChatById
  259. ############################
  260. @router.post("/{id}/share", response_model=Optional[ChatResponse])
  261. async def share_chat_by_id(id: str, user=Depends(get_verified_user)):
  262. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  263. if chat:
  264. if chat.share_id:
  265. shared_chat = Chats.update_shared_chat_by_chat_id(chat.id)
  266. return ChatResponse(
  267. **{**shared_chat.model_dump(), "chat": json.loads(shared_chat.chat)}
  268. )
  269. shared_chat = Chats.insert_shared_chat_by_chat_id(chat.id)
  270. if not shared_chat:
  271. raise HTTPException(
  272. status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
  273. detail=ERROR_MESSAGES.DEFAULT(),
  274. )
  275. return ChatResponse(
  276. **{**shared_chat.model_dump(), "chat": json.loads(shared_chat.chat)}
  277. )
  278. else:
  279. raise HTTPException(
  280. status_code=status.HTTP_401_UNAUTHORIZED,
  281. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  282. )
  283. ############################
  284. # DeletedSharedChatById
  285. ############################
  286. @router.delete("/{id}/share", response_model=Optional[bool])
  287. async def delete_shared_chat_by_id(id: str, user=Depends(get_verified_user)):
  288. chat = Chats.get_chat_by_id_and_user_id(id, user.id)
  289. if chat:
  290. if not chat.share_id:
  291. return False
  292. result = Chats.delete_shared_chat_by_chat_id(id)
  293. update_result = Chats.update_chat_share_id_by_id(id, None)
  294. return result and update_result != None
  295. else:
  296. raise HTTPException(
  297. status_code=status.HTTP_401_UNAUTHORIZED,
  298. detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
  299. )
  300. ############################
  301. # GetChatTagsById
  302. ############################
  303. @router.get("/{id}/tags", response_model=List[TagModel])
  304. async def get_chat_tags_by_id(id: str, user=Depends(get_verified_user)):
  305. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  306. if tags != None:
  307. return tags
  308. else:
  309. raise HTTPException(
  310. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  311. )
  312. ############################
  313. # AddChatTagById
  314. ############################
  315. @router.post("/{id}/tags", response_model=Optional[ChatIdTagModel])
  316. async def add_chat_tag_by_id(
  317. id: str, form_data: ChatIdTagForm, user=Depends(get_verified_user)
  318. ):
  319. tags = Tags.get_tags_by_chat_id_and_user_id(id, user.id)
  320. if form_data.tag_name not in tags:
  321. tag = Tags.add_tag_to_chat(user.id, form_data)
  322. if tag:
  323. return tag
  324. else:
  325. raise HTTPException(
  326. status_code=status.HTTP_401_UNAUTHORIZED,
  327. detail=ERROR_MESSAGES.NOT_FOUND,
  328. )
  329. else:
  330. raise HTTPException(
  331. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
  332. )
  333. ############################
  334. # DeleteChatTagById
  335. ############################
  336. @router.delete("/{id}/tags", response_model=Optional[bool])
  337. async def delete_chat_tag_by_id(
  338. id: str, form_data: ChatIdTagForm, user=Depends(get_verified_user)
  339. ):
  340. result = Tags.delete_tag_by_tag_name_and_chat_id_and_user_id(
  341. form_data.tag_name, id, user.id
  342. )
  343. if result:
  344. return result
  345. else:
  346. raise HTTPException(
  347. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  348. )
  349. ############################
  350. # DeleteAllChatTagsById
  351. ############################
  352. @router.delete("/{id}/tags/all", response_model=Optional[bool])
  353. async def delete_all_chat_tags_by_id(id: str, user=Depends(get_verified_user)):
  354. result = Tags.delete_tags_by_chat_id_and_user_id(id, user.id)
  355. if result:
  356. return result
  357. else:
  358. raise HTTPException(
  359. status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.NOT_FOUND
  360. )