Skip to content

Commit 92e3a57

Browse files
committed
Store last conversation in in localstorage #6993
1 parent 901b9eb commit 92e3a57

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

x-pack/packages/kbn-elastic-assistant/impl/assistant/assistant_overlay/index.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const AssistantOverlay: React.FC = React.memo(() => {
3232
WELCOME_CONVERSATION_TITLE
3333
);
3434
const [promptContextId, setPromptContextId] = useState<string | undefined>();
35-
const { setShowAssistantOverlay } = useAssistantContext();
35+
const { setShowAssistantOverlay, localStorageLastConversationId } = useAssistantContext();
3636

3737
// Bind `showAssistantOverlay` in SecurityAssistantContext to this modal instance
3838
const showOverlay = useCallback(
@@ -52,15 +52,25 @@ export const AssistantOverlay: React.FC = React.memo(() => {
5252
setShowAssistantOverlay(showOverlay);
5353
}, [setShowAssistantOverlay, showOverlay]);
5454

55+
// Called whenever platform specific shortcut for assistant is pressed
56+
const handleShortcutPress = useCallback(() => {
57+
// Try to restore the last conversation on shortcut pressed
58+
if (!isModalVisible) {
59+
setConversationId(localStorageLastConversationId || WELCOME_CONVERSATION_TITLE);
60+
}
61+
62+
setIsModalVisible(!isModalVisible);
63+
}, [isModalVisible, localStorageLastConversationId]);
64+
5565
// Register keyboard listener to show the modal when cmd + ; is pressed
5666
const onKeyDown = useCallback(
5767
(event: KeyboardEvent) => {
5868
if (event.key === ';' && (isMac ? event.metaKey : event.ctrlKey)) {
5969
event.preventDefault();
60-
setIsModalVisible(!isModalVisible);
70+
handleShortcutPress();
6171
}
6272
},
63-
[isModalVisible]
73+
[handleShortcutPress]
6474
);
6575
useEvent('keydown', onKeyDown);
6676

x-pack/packages/kbn-elastic-assistant/impl/assistant/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const AssistantComponent: React.FC<Props> = ({
7878
getComments,
7979
http,
8080
promptContexts,
81+
setLastConversationId,
8182
title,
8283
} = useAssistantContext();
8384
const [selectedPromptContexts, setSelectedPromptContexts] = useState<
@@ -98,6 +99,11 @@ const AssistantComponent: React.FC<Props> = ({
9899
[conversationId, conversations, createConversation, selectedConversationId]
99100
);
100101

102+
// Remember last selection for reuse after keyboard shortcut is pressed
103+
useEffect(() => {
104+
setLastConversationId(selectedConversationId);
105+
}, [selectedConversationId, setLastConversationId]);
106+
101107
// Welcome conversation is a special 'setup' case when no connector exists, mostly extracted to `ConnectorSetup` component,
102108
// but currently a bit of state is littered throughout the assistant component. TODO: clean up/isolate this state
103109
const welcomeConversation = useMemo(

x-pack/packages/kbn-elastic-assistant/impl/assistant_context/constants.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
export const DEFAULT_ASSISTANT_NAMESPACE = 'elasticAssistantDefault';
99
export const QUICK_PROMPT_LOCAL_STORAGE_KEY = 'quickPrompts';
1010
export const SYSTEM_PROMPT_LOCAL_STORAGE_KEY = 'systemPrompts';
11+
export const LAST_CONVERSATION_ID_LOCAL_STORAGE_KEY = 'lastConversationId';

x-pack/packages/kbn-elastic-assistant/impl/assistant_context/index.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Prompt } from '../assistant/types';
2727
import { BASE_SYSTEM_PROMPTS } from '../content/prompts/system';
2828
import {
2929
DEFAULT_ASSISTANT_NAMESPACE,
30+
LAST_CONVERSATION_ID_LOCAL_STORAGE_KEY,
3031
QUICK_PROMPT_LOCAL_STORAGE_KEY,
3132
SYSTEM_PROMPT_LOCAL_STORAGE_KEY,
3233
} from './constants';
@@ -96,6 +97,7 @@ interface UseAssistantContext {
9697
showAnonymizedValues: boolean;
9798
}) => EuiCommentProps[];
9899
http: HttpSetup;
100+
localStorageLastConversationId: string | undefined;
99101
promptContexts: Record<string, PromptContext>;
100102
nameSpace: string;
101103
registerPromptContext: RegisterPromptContext;
@@ -104,6 +106,7 @@ interface UseAssistantContext {
104106
setConversations: React.Dispatch<React.SetStateAction<Record<string, Conversation>>>;
105107
setDefaultAllow: React.Dispatch<React.SetStateAction<string[]>>;
106108
setDefaultAllowReplacement: React.Dispatch<React.SetStateAction<string[]>>;
109+
setLastConversationId: React.Dispatch<React.SetStateAction<string | undefined>>;
107110
setShowAssistantOverlay: (showAssistantOverlay: ShowAssistantOverlay) => void;
108111
showAssistantOverlay: ShowAssistantOverlay;
109112
title: string;
@@ -148,6 +151,9 @@ export const AssistantProvider: React.FC<AssistantProviderProps> = ({
148151
baseSystemPrompts
149152
);
150153

154+
const [localStorageLastConversationId, setLocalStorageLastConversationId] =
155+
useLocalStorage<string>(`${nameSpace}.${LAST_CONVERSATION_ID_LOCAL_STORAGE_KEY}`);
156+
151157
/**
152158
* Prompt contexts are used to provide components a way to register and make their data available to the assistant.
153159
*/
@@ -248,6 +254,8 @@ export const AssistantProvider: React.FC<AssistantProviderProps> = ({
248254
showAssistantOverlay,
249255
title,
250256
unRegisterPromptContext,
257+
localStorageLastConversationId,
258+
setLastConversationId: setLocalStorageLastConversationId,
251259
}),
252260
[
253261
actionTypeRegistry,
@@ -263,6 +271,7 @@ export const AssistantProvider: React.FC<AssistantProviderProps> = ({
263271
defaultAllowReplacement,
264272
getComments,
265273
http,
274+
localStorageLastConversationId,
266275
localStorageQuickPrompts,
267276
localStorageSystemPrompts,
268277
nameSpace,
@@ -271,6 +280,7 @@ export const AssistantProvider: React.FC<AssistantProviderProps> = ({
271280
registerPromptContext,
272281
setDefaultAllow,
273282
setDefaultAllowReplacement,
283+
setLocalStorageLastConversationId,
274284
setLocalStorageQuickPrompts,
275285
setLocalStorageSystemPrompts,
276286
showAssistantOverlay,

0 commit comments

Comments
 (0)