diff --git a/backend/apps/web/routers/prompts.py b/backend/apps/web/routers/prompts.py index e62ddda2..5a002c94 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 ec3ce337..0817445b 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 deee6de8..7ed303b3 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 865dc36a..203bf9cd 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}