Skip to content

Commit 6b59062

Browse files
authored
feat(assistant): add suggested prompts COMPASS-10141 (#7667)
1 parent 7989492 commit 6b59062

File tree

7 files changed

+513
-11
lines changed

7 files changed

+513
-11
lines changed

package-lock.json

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/compass-assistant/src/compass-assistant-drawer.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export const CompassAssistantDrawer: React.FunctionComponent<{
106106
export const ClearChatButton: React.FunctionComponent<{
107107
chat: Chat<AssistantMessage>;
108108
}> = ({ chat }) => {
109-
const { clearError, stop } = useChat({ chat });
109+
const { clearError, stop, setMessages } = useChat({ chat });
110110

111111
const handleClearChat = useCallback(async () => {
112112
const confirmed = await showConfirmation({
@@ -120,15 +120,19 @@ export const ClearChatButton: React.FunctionComponent<{
120120
if (confirmed) {
121121
await stop();
122122
clearError();
123-
// Instead of breaking React rules, we should probably expose the "clear"
124-
// as an interface on the chat class. Otherwise it's kinda expected taht
125-
// we "mutate" messages directly to update the state
123+
124+
// TODO: We use one chat instance for the entire Assistant service but when a conversation is cleared,
125+
// we need to treat it as a new chat. So, we override the ID to a newly generated one.
126+
// This is used by e.g. SuggestedPrompts to reset the state of the selected index.
127+
// @ts-expect-error This is a readonly property but we need to mutate it to generate a new ID
126128
// eslint-disable-next-line react-hooks/immutability
127-
chat.messages = chat.messages.filter(
128-
(message) => message.metadata?.isPermanent
129+
chat.id = chat.generateId();
130+
131+
setMessages(
132+
chat.messages.filter((message) => message.metadata?.isPermanent)
129133
);
130134
}
131-
}, [stop, clearError, chat]);
135+
}, [stop, clearError, chat, setMessages]);
132136

133137
const isChatEmpty =
134138
chat.messages.filter((message) => !message.metadata?.isPermanent).length ===

packages/compass-assistant/src/components/assistant-chat.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { ConfirmationMessage } from './confirmation-message';
2121
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
2222
import { NON_GENUINE_WARNING_MESSAGE } from '../preset-messages';
23+
import { SuggestedPrompts } from './suggested-prompts';
2324

2425
const { ChatWindow } = LgChatChatWindow;
2526
const { LeafyGreenChatProvider } = LgChatLeafygreenChatProvider;
@@ -31,6 +32,11 @@ interface AssistantChatProps {
3132
hasNonGenuineConnections: boolean;
3233
}
3334

35+
export type SendMessageOptions = {
36+
text: string;
37+
metadata?: AssistantMessage['metadata'];
38+
};
39+
3440
// TODO(COMPASS-9751): These are temporary patches to make the Assistant chat take the entire
3541
// width and height of the drawer since Leafygreen doesn't support this yet.
3642
const assistantChatFixesStyles = css({
@@ -239,12 +245,15 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
239245
}, [hasNonGenuineConnections, chat, setMessages]);
240246

241247
const handleMessageSend = useCallback(
242-
async (messageBody: string) => {
243-
const trimmedMessageBody = messageBody.trim();
248+
async ({ text, metadata }: SendMessageOptions) => {
249+
const trimmedMessageBody = text.trim();
244250
if (trimmedMessageBody) {
245251
await chat.stop();
246252
void ensureOptInAndSend?.(
247-
{ text: trimmedMessageBody, metadata: { sendContext: true } },
253+
{
254+
text: trimmedMessageBody,
255+
metadata: { sendContext: true, ...metadata },
256+
},
248257
{},
249258
() => {
250259
track('Assistant Prompt Submitted', {
@@ -466,9 +475,10 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
466475
</p>
467476
</div>
468477
)}
478+
<SuggestedPrompts chat={chat} onMessageSend={handleMessageSend} />
469479
<InputBar
470480
data-testid="assistant-chat-input"
471-
onMessageSend={(messageBody) => void handleMessageSend(messageBody)}
481+
onMessageSend={(text) => void handleMessageSend({ text })}
472482
state={status === 'submitted' ? 'loading' : undefined}
473483
textareaProps={inputBarTextareaProps}
474484
/>

0 commit comments

Comments
 (0)