From ccae1e9f60916cfb1ed942b4c3adc84c2ea650d6 Mon Sep 17 00:00:00 2001 From: pablodanswer Date: Sat, 23 Nov 2024 17:33:20 -0800 Subject: [PATCH] update update flow + assistant creation --- backend/danswer/db/persona.py | 3 ++ .../app/admin/assistants/AssistantEditor.tsx | 1 + web/src/app/admin/assistants/lib.ts | 47 +++++++++---------- .../assistants/fetchPersonaEditorInfoSS.ts | 36 +++++++------- 4 files changed, 44 insertions(+), 43 deletions(-) diff --git a/backend/danswer/db/persona.py b/backend/danswer/db/persona.py index f2f40d78e37..98a50d50e9d 100644 --- a/backend/danswer/db/persona.py +++ b/backend/danswer/db/persona.py @@ -461,6 +461,9 @@ def upsert_persona( validate_persona_tools(tools) if persona: + if persona.builtin_persona and not builtin_persona: + raise ValueError("Cannot update builtin persona with non-builtin.") + # this checks if the user has permission to edit the persona persona = fetch_persona_by_id( db_session=db_session, persona_id=persona.id, user=user, get_editable=True diff --git a/web/src/app/admin/assistants/AssistantEditor.tsx b/web/src/app/admin/assistants/AssistantEditor.tsx index 45f8a6439ec..650c4d199a8 100644 --- a/web/src/app/admin/assistants/AssistantEditor.tsx +++ b/web/src/app/admin/assistants/AssistantEditor.tsx @@ -379,6 +379,7 @@ export function AssistantEditor({ if (!promptResponse.ok) { error = await promptResponse.text(); } + if (!personaResponse) { error = "Failed to create Assistant - no response received"; } else if (!personaResponse.ok) { diff --git a/web/src/app/admin/assistants/lib.ts b/web/src/app/admin/assistants/lib.ts index 760fdcc42bf..2ba5057b298 100644 --- a/web/src/app/admin/assistants/lib.ts +++ b/web/src/app/admin/assistants/lib.ts @@ -259,9 +259,29 @@ export async function updatePersona( ): Promise<[Response, Response | null]> { const { id, existingPromptId } = personaUpdateRequest; - // first update prompt + let fileId = null; + if (personaUpdateRequest.uploaded_image) { + fileId = await uploadFile(personaUpdateRequest.uploaded_image); + if (!fileId) { + return [new Response(null, { status: 400 }), null]; + } + } + + const updatePersonaResponse = await fetch(`/api/persona/${id}`, { + method: "PATCH", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify( + buildPersonaAPIBody(personaUpdateRequest, existingPromptId ?? 0, fileId) + ), + }); + + if (!updatePersonaResponse.ok) { + return [updatePersonaResponse, null]; + } + let promptResponse; - let promptId; if (existingPromptId !== undefined) { promptResponse = await updatePrompt({ promptId: existingPromptId, @@ -270,7 +290,6 @@ export async function updatePersona( taskPrompt: personaUpdateRequest.task_prompt, includeCitations: personaUpdateRequest.include_citations, }); - promptId = existingPromptId; } else { promptResponse = await createPrompt({ personaName: personaUpdateRequest.name, @@ -278,30 +297,8 @@ export async function updatePersona( taskPrompt: personaUpdateRequest.task_prompt, includeCitations: personaUpdateRequest.include_citations, }); - promptId = promptResponse.ok ? (await promptResponse.json()).id : null; } - let fileId = null; - if (personaUpdateRequest.uploaded_image) { - fileId = await uploadFile(personaUpdateRequest.uploaded_image); - if (!fileId) { - return [promptResponse, null]; - } - } - - const updatePersonaResponse = - promptResponse.ok && promptId - ? await fetch(`/api/persona/${id}`, { - method: "PATCH", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify( - buildPersonaAPIBody(personaUpdateRequest, promptId, fileId) - ), - }) - : null; - return [promptResponse, updatePersonaResponse]; } diff --git a/web/src/lib/assistants/fetchPersonaEditorInfoSS.ts b/web/src/lib/assistants/fetchPersonaEditorInfoSS.ts index 9873e0a256b..1939ad038ac 100644 --- a/web/src/lib/assistants/fetchPersonaEditorInfoSS.ts +++ b/web/src/lib/assistants/fetchPersonaEditorInfoSS.ts @@ -8,13 +8,6 @@ import { } from "@/app/admin/configuration/llm/interfaces"; import { ToolSnapshot } from "../tools/interfaces"; import { fetchToolsSS } from "../tools/fetchTools"; -import { - OpenAIIcon, - AnthropicIcon, - AWSIcon, - AzureIcon, - OpenSourceIcon, -} from "@/components/icons/icons"; export async function fetchAssistantEditorInfoSS( personaId?: number | string @@ -104,15 +97,22 @@ export async function fetchAssistantEditorInfoSS( ? ((await personaResponse.json()) as Persona) : null; - return [ - { - ccPairs, - documentSets, - llmProviders, - user, - existingPersona, - tools: toolsResponse, - }, - null, - ]; + let error: string | null = null; + if (existingPersona?.builtin_persona) { + return [null, "cannot update builtin persona"]; + } + + return ( + error || [ + { + ccPairs, + documentSets, + llmProviders, + user, + existingPersona, + tools: toolsResponse, + }, + null, + ] + ); }