From beb83e3b0686521956e3f25c1018c2715fe400fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Louv-Jansen?= Date: Mon, 2 Dec 2024 14:33:59 +0100 Subject: [PATCH] [Obs AI Assistant] It should be possible to clear the user-specific system prompt (#202279) I noticed that I was not able to clear the user-specific system prompt. I had initially entered "Please speak in Swedish" and saved. Afterwards I wanted to clear this but the save button is disabled if the text content is empty. ![image](https://github.com/user-attachments/assets/c3889831-e96a-491f-a8c1-29ae235af2ae) --- .../server/routes/knowledge_base/route.ts | 2 +- .../service/knowledge_base_service/index.ts | 2 +- ...edge_base_edit_user_instruction_flyout.tsx | 2 -- .../knowledge_base_user_instructions.spec.ts | 31 +++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/server/routes/knowledge_base/route.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/server/routes/knowledge_base/route.ts index 50ce85e3578e9..37e9248a0c624 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/server/routes/knowledge_base/route.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/server/routes/knowledge_base/route.ts @@ -133,7 +133,7 @@ const saveKnowledgeBaseUserInstruction = createObservabilityAIAssistantServerRou params: t.type({ body: t.type({ id: t.string, - text: nonEmptyStringRt, + text: t.string, public: toBooleanRt, }), }), diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts index a98cf6f810f2c..fdde5ebb49970 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/knowledge_base_service/index.ts @@ -405,7 +405,7 @@ export class KnowledgeBaseService { document: { '@timestamp': new Date().toISOString(), ...doc, - semantic_text: doc.text, + ...(doc.text ? { semantic_text: doc.text } : {}), user, namespace, }, diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant_management/public/routes/components/knowledge_base_edit_user_instruction_flyout.tsx b/x-pack/plugins/observability_solution/observability_ai_assistant_management/public/routes/components/knowledge_base_edit_user_instruction_flyout.tsx index 1c05ca6d52b3f..51801c44a6835 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant_management/public/routes/components/knowledge_base_edit_user_instruction_flyout.tsx +++ b/x-pack/plugins/observability_solution/observability_ai_assistant_management/public/routes/components/knowledge_base_edit_user_instruction_flyout.tsx @@ -31,7 +31,6 @@ export function KnowledgeBaseEditUserInstructionFlyout({ onClose }: { onClose: ( const { mutateAsync: createEntry, isLoading: isSaving } = useCreateKnowledgeBaseUserInstruction(); const [newEntryText, setNewEntryText] = useState(''); const [newEntryId, setNewEntryId] = useState(); - const isSubmitDisabled = newEntryText.trim() === ''; useEffect(() => { const userInstruction = userInstructions?.find((entry) => !entry.public); @@ -118,7 +117,6 @@ export function KnowledgeBaseEditUserInstructionFlyout({ onClose }: { onClose: ( fill isLoading={isSaving} onClick={handleSubmit} - isDisabled={isSubmitDisabled} > {i18n.translate( 'xpack.observabilityAiAssistantManagement.knowledgeBaseNewManualEntryFlyout.saveButtonLabel', diff --git a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts index 6ea2b279fd386..cde2c9e4b4a83 100644 --- a/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts +++ b/x-pack/test/observability_ai_assistant_api_integration/tests/knowledge_base/knowledge_base_user_instructions.spec.ts @@ -331,5 +331,36 @@ export default function ApiTest({ getService }: FtrProviderContext) { expect(conversation.messages.length).to.be(5); }); }); + + describe('Instructions can be saved and cleared again', () => { + async function updateInstruction(text: string) { + await observabilityAIAssistantAPIClient + .editor({ + endpoint: 'PUT /internal/observability_ai_assistant/kb/user_instructions', + params: { + body: { + id: 'my-instruction-that-will-be-cleared', + text, + public: false, + }, + }, + }) + .expect(200); + + const res = await observabilityAIAssistantAPIClient + .editor({ endpoint: 'GET /internal/observability_ai_assistant/kb/user_instructions' }) + .expect(200); + + return res.body.userInstructions[0].text; + } + + it('can clear the instruction', async () => { + const res1 = await updateInstruction('This is a user instruction that will be cleared'); + expect(res1).to.be('This is a user instruction that will be cleared'); + + const res2 = await updateInstruction(''); + expect(res2).to.be(''); + }); + }); }); }