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
46 changes: 40 additions & 6 deletions packages/compass-assistant/src/assistant-chat.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
screen,
userEvent,
waitFor,
within,
} from '@mongodb-js/testing-library-compass';
import { AssistantChat } from './assistant-chat';
import { expect } from 'chai';
Expand All @@ -30,6 +31,12 @@ describe('AssistantChat', function () {
type: 'text',
text: 'Hello! How can I help you with MongoDB today?',
},
{
type: 'source-url',
title: 'MongoDB',
url: 'https://en.wikipedia.org/wiki/MongoDB',
sourceId: '1',
},
],
},
];
Expand Down Expand Up @@ -471,16 +478,14 @@ describe('AssistantChat', function () {
);

// First click thumbs down to potentially open feedback form
const thumbsDownButton = assistantMessage.querySelector(
'[aria-label="Thumbs Down Icon"]'
) as HTMLElement;
const thumbsDownButton = within(assistantMessage).getByLabelText(
'Dislike this message'
);

userEvent.click(thumbsDownButton);

// Look for feedback text area (the exact implementation depends on LeafyGreen)
const feedbackTextArea = screen.getByTestId(
'lg-chat-message_actions-feedback_textarea'
);
const feedbackTextArea = within(assistantMessage).getByRole('textbox');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I spent half an hour trying to figure out why it suddenly cannot find the textarea by textid even though it is there and the testid matches 🤷


userEvent.type(feedbackTextArea, 'This response was not helpful');

Expand Down Expand Up @@ -527,4 +532,33 @@ describe('AssistantChat', function () {
expect(screen.queryByLabelText('Thumbs Down Icon')).to.not.exist;
});
});

describe('related sources', function () {
it('displays related resources links for assistant messages that include them', async function () {
renderWithChat(mockMessages);
userEvent.click(screen.getByLabelText('Expand Related Resources'));

// TODO(COMPASS-9860) can't find the links in test-electron on RHEL and Ubuntu.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤷

if ((process as any).type === 'renderer') {
return this.skip();
}

await waitFor(() => {
expect(screen.getByRole('link', { name: 'MongoDB' })).to.have.attribute(
'href',
'https://en.wikipedia.org/wiki/MongoDB'
);
});
});

it('does not display related resources section when there are no source-url parts', function () {
const messages = mockMessages.map((message) => ({
...message,
parts: message.parts.filter((part) => part.type !== 'source-url'),
}));
renderWithChat(messages);

expect(screen.queryByLabelText('Expand Related Resources')).to.not.exist;
});
});
});
22 changes: 18 additions & 4 deletions packages/compass-assistant/src/assistant-chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
LgChatChatWindow,
LgChatLeafygreenChatProvider,
LgChatMessage,
LgChatMessageActions,
LgChatInputBar,
spacing,
css,
Expand All @@ -26,7 +25,6 @@ const { DisclaimerText } = LgChatChatDisclaimer;
const { ChatWindow } = LgChatChatWindow;
const { LeafyGreenChatProvider, Variant } = LgChatLeafygreenChatProvider;
const { Message } = LgChatMessage;
const { MessageActions } = LgChatMessageActions;
const { InputBar } = LgChatInputBar;

const GEN_AI_FAQ_LINK = 'https://www.mongodb.com/docs/generative-ai-faq/';
Expand Down Expand Up @@ -141,6 +139,12 @@ const errorBannerWrapperStyles = css({
margin: spacing[400],
});

const messagesWrapStyles = css({
display: 'flex',
flexDirection: 'column',
gap: spacing[400],
});

export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
chat,
hasNonGenuineConnections,
Expand Down Expand Up @@ -185,6 +189,13 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
.map((part) => part.text)
.join(''),
isSender: message.role === 'user',
sources: message.parts
.filter((part) => part.type === 'source-url')
.map((part) => ({
children: part.title || 'Documentation Link',
href: part.url,
variant: 'Docs',
})),
}));

const handleMessageSend = useCallback(
Expand Down Expand Up @@ -247,7 +258,7 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
data-testid="assistant-chat-messages"
className={messageFeedFixesStyles}
>
<div>
<div className={messagesWrapStyles}>
Copy link
Contributor Author

Choose a reason for hiding this comment

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

drive-by: I broke the spacing between messages in the recent PR where I fixed the scrolling in a way where it didn't in turn break text selection across messages.

{lgMessages.map((messageFields) => (
<Message
key={messageFields.id}
Expand All @@ -256,11 +267,14 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
data-testid={`assistant-message-${messageFields.id}`}
>
{messageFields.isSender === false && (
<MessageActions
<Message.Actions
Copy link
Contributor Author

Choose a reason for hiding this comment

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

drive-by: MessageActions is deprecated. Just like MessageLinks. These are now on Message like Message.Actions and Message.Links.

onRatingChange={handleFeedback}
onSubmitFeedback={handleFeedback}
/>
)}
{messageFields.sources.length > 0 && (
<Message.Links links={messageFields.sources} />
)}
</Message>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class DocsProviderTransport implements ChatTransport<UIMessage> {
},
});

return Promise.resolve(result.toUIMessageStream());
return Promise.resolve(result.toUIMessageStream({ sendSources: true }));
}

reconnectToStream(): Promise<ReadableStream<UIMessageChunk> | null> {
Expand Down
2 changes: 2 additions & 0 deletions packages/compass-assistant/src/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ You CANNOT:
2. Query MongoDB directly or execute code.
3. Access the current state of the UI
</inabilities>
Always call the 'search_content' tool when asked a technical question that would benefit from getting relevant info from the documentation.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Jedi mind trick of getting this thing to use the sources and therefore reliably return some. https://mongodb.github.io/chatbot/server/context-engineering#elicit-internal-retrieval-augmented-generation-tool-calls

`;
};

Expand Down
1 change: 0 additions & 1 deletion packages/compass-assistant/test/assistant.eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ async function makeAssistantCall(

const sources = resolvedSources
.map((source) => {
console.log(source);
return source.url;
})
.filter((url) => !!url);
Expand Down
Loading