Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,8 @@ describe('AssistantChat', function () {
userEvent.type(inputField, 'What is aggregation?');
userEvent.click(sendButton);

expect(ensureOptInAndSendStub.called).to.be.true;

await waitFor(() => {
expect(ensureOptInAndSendStub.called).to.be.true;
expect(track).to.have.been.calledWith('Assistant Prompt Submitted', {
user_input_length: 'What is aggregation?'.length,
});
Expand Down Expand Up @@ -281,9 +280,8 @@ describe('AssistantChat', function () {
userEvent.type(inputField, ' What is sharding? ');
userEvent.click(screen.getByLabelText('Send message'));

expect(ensureOptInAndSendStub.called).to.be.true;

await waitFor(() => {
expect(ensureOptInAndSendStub.called).to.be.true;
expect(track).to.have.been.calledWith('Assistant Prompt Submitted', {
user_input_length: 'What is sharding?'.length,
});
Expand Down
36 changes: 32 additions & 4 deletions packages/compass-assistant/src/components/assistant-chat.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useEffect, useContext } from 'react';
import React, { useCallback, useEffect, useContext, useRef } from 'react';
import type { AssistantMessage } from '../compass-assistant-provider';
import { AssistantActionsContext } from '../compass-assistant-provider';
import type { Chat } from '../@ai-sdk/react/chat-react';
Expand Down Expand Up @@ -203,6 +203,10 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
}) => {
const track = useTelemetry();
const darkMode = useDarkMode();
const messagesContainerRef = useRef<HTMLDivElement>(null);
const previousLastMessageId = useRef<string | undefined>(undefined);
const { id: lastMessageId, role: lastMessageRole } =
chat.messages[chat.messages.length - 1] ?? {};

const { ensureOptInAndSend } = useContext(AssistantActionsContext);
const { messages, status, error, clearError, setMessages } = useChat({
Expand All @@ -214,6 +218,26 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
},
});

const scrollToBottom = useCallback(() => {
if (messagesContainerRef.current) {
// Since the container uses flexDirection: 'column-reverse',
// scrolling to the bottom means setting scrollTop to 0
messagesContainerRef.current.scrollTop = 0;
}
}, []);

useEffect(() => {
if (
lastMessageId &&
previousLastMessageId.current !== undefined &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check if there was a message and one before that? If not, what's the harm in scrolling to the bottom if the list is and was empty? If there's a reason we should probably spell it all out in a comment.

Copy link
Contributor Author

@gagik gagik Sep 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to avoid unnecessary behavior of scroll to bottom, either is fine.

lastMessageId !== previousLastMessageId.current &&
lastMessageRole === 'user'
) {
scrollToBottom();
}
previousLastMessageId.current = lastMessageId;
}, [lastMessageId, lastMessageRole, scrollToBottom]);

useEffect(() => {
const hasExistingNonGenuineWarning = chat.messages.some(
(message) => message.id === 'non-genuine-warning'
Expand All @@ -232,17 +256,18 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
}, [hasNonGenuineConnections, chat, setMessages]);

const handleMessageSend = useCallback(
(messageBody: string) => {
async (messageBody: string) => {
const trimmedMessageBody = messageBody.trim();
if (trimmedMessageBody) {
await chat.stop();
void ensureOptInAndSend?.({ text: trimmedMessageBody }, {}, () => {
track('Assistant Prompt Submitted', {
user_input_length: trimmedMessageBody.length,
});
});
}
},
[track, ensureOptInAndSend]
[track, ensureOptInAndSend, chat]
);

const handleFeedback = useCallback(
Expand Down Expand Up @@ -343,6 +368,7 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
<div
data-testid="assistant-chat-messages"
className={messageFeedFixesStyles}
ref={messagesContainerRef}
>
<div className={messagesWrapStyles}>
{messages.map((message, index) => {
Expand Down Expand Up @@ -436,7 +462,9 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
<div className={inputBarStyleFixes}>
<InputBar
data-testid="assistant-chat-input"
onMessageSend={handleMessageSend}
onMessageSend={(messageBody) =>
void handleMessageSend(messageBody)
}
state={status === 'submitted' ? 'loading' : undefined}
textareaProps={{
placeholder: 'Ask a question',
Expand Down
Loading