From 422159477809730c85fadd06ef9dd3cefb3deb32 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 26 Dec 2023 22:10:22 -0800 Subject: [PATCH] feat: chat export --- backend/apps/web/main.py | 7 +++-- backend/apps/web/models/chats.py | 8 +++++ backend/apps/web/routers/chats.py | 22 ++++++++++++++ src/lib/apis/chats/index.ts | 31 ++++++++++++++++++++ src/lib/components/chat/SettingsModal.svelte | 24 +++++++++------ 5 files changed, 81 insertions(+), 11 deletions(-) diff --git a/backend/apps/web/main.py b/backend/apps/web/main.py index 52238aaed..03273f8bc 100644 --- a/backend/apps/web/main.py +++ b/backend/apps/web/main.py @@ -1,7 +1,7 @@ from fastapi import FastAPI, Request, Depends, HTTPException from fastapi.middleware.cors import CORSMiddleware -from apps.web.routers import auths, users, chats, utils +from apps.web.routers import auths, users, chats, modelfiles, utils from config import WEBUI_VERSION, WEBUI_AUTH app = FastAPI() @@ -19,8 +19,11 @@ app.add_middleware( app.include_router(auths.router, prefix="/auths", tags=["auths"]) app.include_router(users.router, prefix="/users", tags=["users"]) -app.include_router(utils.router, prefix="/utils", tags=["utils"]) app.include_router(chats.router, prefix="/chats", tags=["chats"]) +app.include_router(modelfiles.router, prefix="/modelfiles", tags=["modelfiles"]) + + +app.include_router(utils.router, prefix="/utils", tags=["utils"]) @app.get("/") diff --git a/backend/apps/web/models/chats.py b/backend/apps/web/models/chats.py index 7406539fd..8f075e408 100644 --- a/backend/apps/web/models/chats.py +++ b/backend/apps/web/models/chats.py @@ -123,6 +123,14 @@ class ChatTable: # .offset(skip) ] + def get_all_chats_by_user_id(self, user_id: str) -> List[ChatModel]: + return [ + ChatModel(**model_to_dict(chat)) + for chat in Chat.select() + .where(Chat.user_id == user_id) + .order_by(Chat.timestamp.desc()) + ] + def get_chat_by_id_and_user_id(self, id: str, user_id: str) -> Optional[ChatModel]: try: chat = Chat.get(Chat.id == id, Chat.user_id == user_id) diff --git a/backend/apps/web/routers/chats.py b/backend/apps/web/routers/chats.py index 421251020..798972e7c 100644 --- a/backend/apps/web/routers/chats.py +++ b/backend/apps/web/routers/chats.py @@ -43,6 +43,28 @@ async def get_user_chats(skip: int = 0, limit: int = 50, cred=Depends(bearer_sch ) +############################ +# GetAllChats +############################ + + +@router.get("/all", response_model=List[ChatResponse]) +async def get_all_user_chats(cred=Depends(bearer_scheme)): + token = cred.credentials + user = Users.get_user_by_token(token) + + if user: + return [ + ChatResponse(**{**chat.model_dump(), "chat": json.loads(chat.chat)}) + for chat in Chats.get_all_chats_by_user_id(user.id) + ] + else: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.INVALID_TOKEN, + ) + + ############################ # CreateNewChat ############################ diff --git a/src/lib/apis/chats/index.ts b/src/lib/apis/chats/index.ts index 0abac06a0..354e4f74d 100644 --- a/src/lib/apis/chats/index.ts +++ b/src/lib/apis/chats/index.ts @@ -62,6 +62,37 @@ export const getChatList = async (token: string = '') => { return res; }; +export const getAllChats = async (token: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/chats/all`, { + 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 getChatById = async (token: string, id: string) => { let error = null; diff --git a/src/lib/components/chat/SettingsModal.svelte b/src/lib/components/chat/SettingsModal.svelte index 4f7b9d0f0..7a49143c7 100644 --- a/src/lib/components/chat/SettingsModal.svelte +++ b/src/lib/components/chat/SettingsModal.svelte @@ -1,20 +1,23 @@