From 7fc1d7c2c7b691f9ebca859f9c12b92cbbdbadf3 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Jan 2024 21:35:47 -0800 Subject: [PATCH] feat: prompt crud --- backend/apps/web/routers/prompts.py | 23 +- backend/constants.py | 2 +- src/lib/apis/prompts/index.ts | 14 +- .../chat/MessageInput/PromptCommands.svelte | 8 +- src/routes/(app)/prompts/+page.svelte | 19 +- src/routes/(app)/prompts/create/+page.svelte | 69 ++- src/routes/(app)/prompts/edit/+page.svelte | 483 ++++-------------- 7 files changed, 167 insertions(+), 451 deletions(-) diff --git a/backend/apps/web/routers/prompts.py b/backend/apps/web/routers/prompts.py index e62ddda28..5a002c941 100644 --- a/backend/apps/web/routers/prompts.py +++ b/backend/apps/web/routers/prompts.py @@ -37,14 +37,21 @@ async def create_new_prompt(form_data: PromptForm, user=Depends(get_current_user detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) - prompt = Prompts.insert_new_prompt(user.id, form_data) + prompt = Prompts.get_prompt_by_command(form_data.command) + if prompt == None: + prompt = Prompts.insert_new_prompt(user.id, form_data) - if prompt: - return prompt + if prompt: + return prompt + else: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail=ERROR_MESSAGES.DEFAULT(), + ) else: raise HTTPException( - status_code=status.HTTP_401_UNAUTHORIZED, - detail=ERROR_MESSAGES.DEFAULT(), + status_code=status.HTTP_400_BAD_REQUEST, + detail=ERROR_MESSAGES.COMMAND_TAKEN, ) @@ -55,7 +62,7 @@ async def create_new_prompt(form_data: PromptForm, user=Depends(get_current_user @router.get("/{command}", response_model=Optional[PromptModel]) async def get_prompt_by_command(command: str, user=Depends(get_current_user)): - prompt = Prompts.get_prompt_by_command(command) + prompt = Prompts.get_prompt_by_command(f"/{command}") if prompt: return prompt @@ -81,7 +88,7 @@ async def update_prompt_by_command( detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) - prompt = Prompts.update_prompt_by_command(command, form_data) + prompt = Prompts.update_prompt_by_command(f"/{command}", form_data) if prompt: return prompt else: @@ -104,5 +111,5 @@ async def delete_prompt_by_command(command: str, user=Depends(get_current_user)) detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) - result = Prompts.delete_prompt_by_command(command) + result = Prompts.delete_prompt_by_command(f"/{command}") return result diff --git a/backend/constants.py b/backend/constants.py index ec3ce3376..0817445b5 100644 --- a/backend/constants.py +++ b/backend/constants.py @@ -17,6 +17,7 @@ class ERROR_MESSAGES(str, Enum): USERNAME_TAKEN = ( "Uh-oh! This username is already registered. Please choose another username." ) + COMMAND_TAKEN = "Uh-oh! This command is already registered. Please choose another command string." INVALID_TOKEN = ( "Your session has expired or the token is invalid. Please sign in again." ) @@ -32,5 +33,4 @@ class ERROR_MESSAGES(str, Enum): ) NOT_FOUND = "We could not find what you're looking for :/" USER_NOT_FOUND = "We could not find what you're looking for :/" - MALICIOUS = "Unusual activities detected, please try again in a few minutes." diff --git a/src/lib/apis/prompts/index.ts b/src/lib/apis/prompts/index.ts index deee6de8d..7ed303b3e 100644 --- a/src/lib/apis/prompts/index.ts +++ b/src/lib/apis/prompts/index.ts @@ -16,7 +16,7 @@ export const createNewPrompt = async ( authorization: `Bearer ${token}` }, body: JSON.stringify({ - command: command, + command: `/${command}`, title: title, content: content }) @@ -57,7 +57,7 @@ export const getPrompts = async (token: string = '') => { return json; }) .catch((err) => { - error = err; + error = err.detail; console.log(err); return null; }); @@ -88,7 +88,7 @@ export const getPromptByCommand = async (token: string, command: string) => { return json; }) .catch((err) => { - error = err; + error = err.detail; console.log(err); return null; @@ -117,7 +117,7 @@ export const updatePromptByCommand = async ( authorization: `Bearer ${token}` }, body: JSON.stringify({ - command: command, + command: `/${command}`, title: title, content: content }) @@ -130,7 +130,7 @@ export const updatePromptByCommand = async ( return json; }) .catch((err) => { - error = err; + error = err.detail; console.log(err); return null; @@ -146,6 +146,8 @@ export const updatePromptByCommand = async ( export const deletePromptByCommand = async (token: string, command: string) => { let error = null; + command = command.charAt(0) === '/' ? command.slice(1) : command; + const res = await fetch(`${WEBUI_API_BASE_URL}/prompts/${command}/delete`, { method: 'DELETE', headers: { @@ -162,7 +164,7 @@ export const deletePromptByCommand = async (token: string, command: string) => { return json; }) .catch((err) => { - error = err; + error = err.detail; console.log(err); return null; diff --git a/src/lib/components/chat/MessageInput/PromptCommands.svelte b/src/lib/components/chat/MessageInput/PromptCommands.svelte index 865dc36a2..203bf9cd2 100644 --- a/src/lib/components/chat/MessageInput/PromptCommands.svelte +++ b/src/lib/components/chat/MessageInput/PromptCommands.svelte @@ -53,8 +53,8 @@
/
-
-
+
+
{#each filteredPromptCommands as command, commandIdx}