From 0caa021ef18485e35344d97ff5cf3d9706873e29 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Fri, 17 Jan 2025 05:25:35 +1100 Subject: [PATCH] [8.x] [Obs AI Assistant] Fix editing prompt from contextual insights (#206673) (#206982) # Backport This will backport the following commits from `main` to `8.x`: - [[Obs AI Assistant] Fix editing prompt from contextual insights (#206673)](https://github.com/elastic/kibana/pull/206673) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Viduni Wickramarachchi --- .../public/components/insight/insight.tsx | 60 ++++++++++++++----- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/public/components/insight/insight.tsx b/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/public/components/insight/insight.tsx index 680a92f559f0a..dc09d750b54ad 100644 --- a/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/public/components/insight/insight.tsx +++ b/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/public/components/insight/insight.tsx @@ -14,6 +14,7 @@ import { EuiText, EuiTextArea, EuiCallOut, + EuiLoadingSpinner, } from '@elastic/eui'; import { i18n } from '@kbn/i18n'; import { cloneDeep, isArray, isEmpty, last, once } from 'lodash'; @@ -288,16 +289,47 @@ export function Insight({ [service] ); + const getPromptToEdit = () => { + const clonedMessages = cloneDeep(messages.messages); + const lastUserPrompt = getLastMessageOfType(clonedMessages, MessageRole.User)?.message.content; + + if (!lastUserPrompt) { + return ''; + } + + try { + const { instructions = '' } = JSON.parse(lastUserPrompt); + return instructions.trim(); + } catch (e) { + return ''; + } + }; + const onEditPrompt = (newPrompt: string) => { const clonedMessages = cloneDeep(messages.messages); const userMessage = getLastMessageOfType(clonedMessages, MessageRole.User); if (!userMessage) return false; - userMessage.message.content = newPrompt; - setIsPromptUpdated(true); - setMessages({ messages: clonedMessages, status: FETCH_STATUS.SUCCESS }); - setEditingPrompt(false); - return true; + try { + const parsedContent = JSON.parse(userMessage.message.content || ''); + + if (!parsedContent.instructions) { + return false; + } + + // Assign the updated instructions + parsedContent.instructions = newPrompt; + userMessage.message.content = JSON.stringify(parsedContent); + + setIsPromptUpdated(true); + setMessages({ messages: clonedMessages, status: FETCH_STATUS.SUCCESS }); + setEditingPrompt(false); + return true; + } catch (e) { + // eslint-disable-next-line no-console + console.error('Failed to edit prompt:', e); + return false; + } }; const handleCancel = () => { @@ -372,15 +404,15 @@ export function Insight({ ); } else if (isEditingPrompt) { - children = ( - - ); + const promptToEdit = getPromptToEdit(); + + if (messages.status === FETCH_STATUS.SUCCESS && promptToEdit) { + children = ( + + ); + } else { + children = ; + } } else if (!connectors.loading && !connectors.connectors?.length) { children = (