Skip to content

Commit c01fb29

Browse files
lerouxbCopilot
andauthored
feat(compass-assistant): show related resources below messages if it exists COMPASS-9838 (#7323)
* Show related resources below messages if it exists * tests * Update packages/compass-assistant/src/assistant-chat.tsx Co-authored-by: Copilot <[email protected]> * skip in electron --------- Co-authored-by: Copilot <[email protected]>
1 parent cf65a2d commit c01fb29

File tree

5 files changed

+61
-12
lines changed

5 files changed

+61
-12
lines changed

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

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
screen,
55
userEvent,
66
waitFor,
7+
within,
78
} from '@mongodb-js/testing-library-compass';
89
import { AssistantChat } from './assistant-chat';
910
import { expect } from 'chai';
@@ -30,6 +31,12 @@ describe('AssistantChat', function () {
3031
type: 'text',
3132
text: 'Hello! How can I help you with MongoDB today?',
3233
},
34+
{
35+
type: 'source-url',
36+
title: 'MongoDB',
37+
url: 'https://en.wikipedia.org/wiki/MongoDB',
38+
sourceId: '1',
39+
},
3340
],
3441
},
3542
];
@@ -471,16 +478,14 @@ describe('AssistantChat', function () {
471478
);
472479

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

478485
userEvent.click(thumbsDownButton);
479486

480487
// Look for feedback text area (the exact implementation depends on LeafyGreen)
481-
const feedbackTextArea = screen.getByTestId(
482-
'lg-chat-message_actions-feedback_textarea'
483-
);
488+
const feedbackTextArea = within(assistantMessage).getByRole('textbox');
484489

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

@@ -527,4 +532,33 @@ describe('AssistantChat', function () {
527532
expect(screen.queryByLabelText('Thumbs Down Icon')).to.not.exist;
528533
});
529534
});
535+
536+
describe('related sources', function () {
537+
it('displays related resources links for assistant messages that include them', async function () {
538+
renderWithChat(mockMessages);
539+
userEvent.click(screen.getByLabelText('Expand Related Resources'));
540+
541+
// TODO(COMPASS-9860) can't find the links in test-electron on RHEL and Ubuntu.
542+
if ((process as any).type === 'renderer') {
543+
return this.skip();
544+
}
545+
546+
await waitFor(() => {
547+
expect(screen.getByRole('link', { name: 'MongoDB' })).to.have.attribute(
548+
'href',
549+
'https://en.wikipedia.org/wiki/MongoDB'
550+
);
551+
});
552+
});
553+
554+
it('does not display related resources section when there are no source-url parts', function () {
555+
const messages = mockMessages.map((message) => ({
556+
...message,
557+
parts: message.parts.filter((part) => part.type !== 'source-url'),
558+
}));
559+
renderWithChat(messages);
560+
561+
expect(screen.queryByLabelText('Expand Related Resources')).to.not.exist;
562+
});
563+
});
530564
});

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
LgChatChatWindow,
88
LgChatLeafygreenChatProvider,
99
LgChatMessage,
10-
LgChatMessageActions,
1110
LgChatInputBar,
1211
spacing,
1312
css,
@@ -26,7 +25,6 @@ const { DisclaimerText } = LgChatChatDisclaimer;
2625
const { ChatWindow } = LgChatChatWindow;
2726
const { LeafyGreenChatProvider, Variant } = LgChatLeafygreenChatProvider;
2827
const { Message } = LgChatMessage;
29-
const { MessageActions } = LgChatMessageActions;
3028
const { InputBar } = LgChatInputBar;
3129

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

142+
const messagesWrapStyles = css({
143+
display: 'flex',
144+
flexDirection: 'column',
145+
gap: spacing[400],
146+
});
147+
144148
export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
145149
chat,
146150
hasNonGenuineConnections,
@@ -185,6 +189,13 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
185189
.map((part) => part.text)
186190
.join(''),
187191
isSender: message.role === 'user',
192+
sources: message.parts
193+
.filter((part) => part.type === 'source-url')
194+
.map((part) => ({
195+
children: part.title || 'Documentation Link',
196+
href: part.url,
197+
variant: 'Docs',
198+
})),
188199
}));
189200

190201
const handleMessageSend = useCallback(
@@ -247,7 +258,7 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
247258
data-testid="assistant-chat-messages"
248259
className={messageFeedFixesStyles}
249260
>
250-
<div>
261+
<div className={messagesWrapStyles}>
251262
{lgMessages.map((messageFields) => (
252263
<Message
253264
key={messageFields.id}
@@ -256,11 +267,14 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
256267
data-testid={`assistant-message-${messageFields.id}`}
257268
>
258269
{messageFields.isSender === false && (
259-
<MessageActions
270+
<Message.Actions
260271
onRatingChange={handleFeedback}
261272
onSubmitFeedback={handleFeedback}
262273
/>
263274
)}
275+
{messageFields.sources.length > 0 && (
276+
<Message.Links links={messageFields.sources} />
277+
)}
264278
</Message>
265279
))}
266280
</div>

packages/compass-assistant/src/docs-provider-transport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export class DocsProviderTransport implements ChatTransport<UIMessage> {
4040
},
4141
});
4242

43-
return Promise.resolve(result.toUIMessageStream());
43+
return Promise.resolve(result.toUIMessageStream({ sendSources: true }));
4444
}
4545

4646
reconnectToStream(): Promise<ReadableStream<UIMessageChunk> | null> {

packages/compass-assistant/src/prompts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ You CANNOT:
3333
2. Query MongoDB directly or execute code.
3434
3. Access the current state of the UI
3535
</inabilities>
36+
37+
Always call the 'search_content' tool when asked a technical question that would benefit from getting relevant info from the documentation.
3638
`;
3739
};
3840

packages/compass-assistant/test/assistant.eval.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ async function makeAssistantCall(
178178

179179
const sources = resolvedSources
180180
.map((source) => {
181-
console.log(source);
182181
return source.url;
183182
})
184183
.filter((url) => !!url);

0 commit comments

Comments
 (0)