chats.py 14 KB

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