From ea690e8a9fdbddfa787f55d40e945630c0784a93 Mon Sep 17 00:00:00 2001 From: Viduni Wickramarachchi Date: Thu, 31 Oct 2024 15:16:52 -0400 Subject: [PATCH] [Obs AI Assistant] Fix error when opening an old conversation (#197745) Closes https://github.com/elastic/kibana/issues/176299 ## Summary ### Problem When a chat is started from contextual insights, `initialMessages` are being populated from the temp conversation created from the error. Because the `initialMessages` are present, when an old conversation is clicked, the conversation ID of that conversation and the `initalMessages` from the temp chat are passed to the conversation. This throws an error, as there is a condition which restricts both of these being passed to `useConversation` which is used by the `ChatBody`. - Only one of these should be passed (not both) ### Solution With the current implementation, even though not passing `initialMessages` allows the user to open previous conversations successfully, it doesn't allow the user to start a new chat because as long as initial messages are set, they will be loaded when the New Chat button is clicked instead of showing a blank conversation. Clearing initial messages requires re-architecting how the Chat uses the conversation. Therefore, as the solution, when a chat is opened from contextual insights, the ConversationList will be hidden. The user can interact with the AI Assistant on the opened chat with initial messages from contextual insights. --- .../kbn-ai-assistant/src/chat/chat_flyout.tsx | 136 +++++++++--------- .../public/components/insight/insight.tsx | 1 + .../public/service/create_service.ts | 18 ++- .../public/types.ts | 12 +- .../public/components/nav_control/index.tsx | 6 +- 5 files changed, 101 insertions(+), 72 deletions(-) diff --git a/x-pack/packages/kbn-ai-assistant/src/chat/chat_flyout.tsx b/x-pack/packages/kbn-ai-assistant/src/chat/chat_flyout.tsx index 1343f5ed9a4bb..320beb1ca6b05 100644 --- a/x-pack/packages/kbn-ai-assistant/src/chat/chat_flyout.tsx +++ b/x-pack/packages/kbn-ai-assistant/src/chat/chat_flyout.tsx @@ -47,6 +47,7 @@ export function ChatFlyout({ isOpen, onClose, navigateToConversation, + hideConversationList, }: { initialTitle: string; initialMessages: Message[]; @@ -54,6 +55,7 @@ export function ChatFlyout({ isOpen: boolean; onClose: () => void; navigateToConversation?: (conversationId?: string) => void; + hideConversationList?: boolean; }) { const { euiTheme } = useEuiTheme(); const breakpoint = useCurrentEuiBreakpoint(); @@ -174,84 +176,86 @@ export function ChatFlyout({ }} > - - - setConversationsExpanded(!conversationsExpanded)} - /> - - } - /> - - {conversationsExpanded ? ( - { - conversationList.deleteConversation(deletedConversationId).then(() => { - if (deletedConversationId === conversationId) { - setConversationId(undefined); - } - }); - }} - onConversationSelect={(nextConversationId) => { - setConversationId(nextConversationId); - }} - /> - ) : ( + {!hideConversationList ? ( + - { - setConversationId(undefined); - }} + className={expandButtonClassName} + color="text" + data-test-subj="observabilityAiAssistantChatFlyoutButton" + iconType={conversationsExpanded ? 'transitionLeftIn' : 'transitionLeftOut'} + onClick={() => setConversationsExpanded(!conversationsExpanded)} /> } - className={newChatButtonClassName} /> - )} - + + {conversationsExpanded ? ( + { + conversationList.deleteConversation(deletedConversationId).then(() => { + if (deletedConversationId === conversationId) { + setConversationId(undefined); + } + }); + }} + onConversationSelect={(nextConversationId) => { + setConversationId(nextConversationId); + }} + /> + ) : ( + + { + setConversationId(undefined); + }} + /> + + } + className={newChatButtonClassName} + /> + )} + + ) : null} diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/public/service/create_service.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/public/service/create_service.ts index 07f967a4028d9..5b64073592430 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/public/service/create_service.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/public/service/create_service.ts @@ -36,7 +36,11 @@ export function createService({ const screenContexts$ = new BehaviorSubject([ { starterPrompts: defaultStarterPrompts }, ]); - const predefinedConversation$ = new Subject<{ messages: Message[]; title?: string }>(); + const predefinedConversation$ = new Subject<{ + messages: Message[]; + title?: string; + hideConversationList?: boolean; + }>(); const scope$ = new BehaviorSubject(scopes); @@ -104,8 +108,16 @@ export function createService({ ); }, conversations: { - openNewConversation: ({ messages, title }: { messages: Message[]; title?: string }) => { - predefinedConversation$.next({ messages, title }); + openNewConversation: ({ + messages, + title, + hideConversationList = false, + }: { + messages: Message[]; + title?: string; + hideConversationList?: boolean; + }) => { + predefinedConversation$.next({ messages, title, hideConversationList }); }, predefinedConversation$: predefinedConversation$.asObservable(), }, diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/public/types.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/public/types.ts index becc21f59c5f4..72517df5bffbc 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/public/types.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/public/types.ts @@ -91,8 +91,16 @@ export interface ObservabilityAIAssistantChatService { } export interface ObservabilityAIAssistantConversationService { - openNewConversation: ({}: { messages: Message[]; title?: string }) => void; - predefinedConversation$: Observable<{ messages: Message[]; title?: string }>; + openNewConversation: ({}: { + messages: Message[]; + title?: string; + hideConversationList?: boolean; + }) => void; + predefinedConversation$: Observable<{ + messages: Message[]; + title?: string; + hideConversationList?: boolean; + }>; } export interface ObservabilityAIAssistantService { diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant_app/public/components/nav_control/index.tsx b/x-pack/plugins/observability_solution/observability_ai_assistant_app/public/components/nav_control/index.tsx index 883317c02274f..b6095ac595cea 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant_app/public/components/nav_control/index.tsx +++ b/x-pack/plugins/observability_solution/observability_ai_assistant_app/public/components/nav_control/index.tsx @@ -103,9 +103,12 @@ export function NavControl() { }; }, [service.conversations.predefinedConversation$]); - const { messages, title } = useObservable(service.conversations.predefinedConversation$) ?? { + const { messages, title, hideConversationList } = useObservable( + service.conversations.predefinedConversation$ + ) ?? { messages: [], title: undefined, + hideConversationList: false, }; const theme = useTheme(); @@ -171,6 +174,7 @@ export function NavControl() { ) ); }} + hideConversationList={hideConversationList} /> ) : undefined}