diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index 4895740d..ca55c71d 100644 --- a/backend/apps/web/models/chats.py +++ b/backend/apps/web/models/chats.py @@ -70,9 +70,9 @@ class ChatTable: **{ "id": id, "user_id": user_id, - "title": form_data.chat["title"] - if "title" in form_data.chat - else "New Chat", + "title": ( + form_data.chat["title"] if "title" in form_data.chat else "New Chat" + ), "chat": json.dumps(form_data.chat), "timestamp": int(time.time()), } @@ -131,6 +131,12 @@ class ChatTable: .order_by(Chat.timestamp.desc()) ] + def get_all_chats(self) -> List[ChatModel]: + return [ + ChatModel(**model_to_dict(chat)) + for chat in Chat.select().order_by(Chat.timestamp.desc()) + ] + def get_all_chats_by_user_id(self, user_id: str) -> List[ChatModel]: return [ ChatModel(**model_to_dict(chat)) diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index d2830095..aa725409 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -54,6 +54,25 @@ async def get_all_user_chats(user=Depends(get_current_user)): ] +############################ +# GetAllChatsInDB +############################ + + +@router.get("/all/db", response_model=List[ChatResponse]) +async def get_all_user_chats_in_db(user=Depends(get_current_user)): + if user.role == "admin": + return [ + ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) + for chat in Chats.get_all_chats() + ] + else: + raise HTTPException( + status_code=status.HTTP_403_FORBIDDEN, + detail=ERROR_MESSAGES.ACCESS_PROHIBITED, + ) + + ############################ # CreateNewChat ############################ diff --git a/src/lib/apis/chats/index.ts b/src/lib/apis/chats/index.ts index 7c515f11..58c14bb9 100644 --- a/src/lib/apis/chats/index.ts +++ b/src/lib/apis/chats/index.ts @@ -93,6 +93,37 @@ export const getAllChats = async (token: string) => { return res; }; +export const getAllUserChats = async (token: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all/db`, { + method: 'GET', + headers: { + Accept: 'application/json', + 'Content-Type': 'application/json', + ...(token && { authorization: `Bearer ${token}` }) + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .then((json) => { + return json; + }) + .catch((err) => { + error = err; + console.log(err); + return null; + }); + + if (error) { + throw error; + } + + return res; +}; + export const getAllChatTags = async (token: string) => { let error = null; diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index e348c807..82973864 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -17,7 +17,13 @@ deleteModel } from '$lib/apis/ollama'; import { updateUserPassword } from '$lib/apis/auths'; - import { createNewChat, deleteAllChats, getAllChats, getChatList } from '$lib/apis/chats'; + import { + createNewChat, + deleteAllChats, + getAllChats, + getAllUserChats, + getChatList + } from '$lib/apis/chats'; import { WEB_UI_VERSION, WEBUI_API_BASE_URL } from '$lib/constants'; import { config, models, voices, settings, user, chats } from '$lib/stores'; @@ -179,6 +185,13 @@ saveAs(blob, `chat-export-${Date.now()}.json`); }; + const exportAllUserChats = async () => { + let blob = new Blob([JSON.stringify(await getAllUserChats(localStorage.token))], { + type: 'application/json' + }); + saveAs(blob, `all-chats-export-${Date.now()}.json`); + }; + const deleteChats = async () => { await goto('/'); await deleteAllChats(localStorage.token); @@ -1962,23 +1975,48 @@ fill="currentColor" class="w-4 h-4" > - -
Delete All Chats
+
Delete Chats
{/if} {#if $user?.role === 'admin'}
+ + +
+