Skip to content

Commit 6fa086d

Browse files
authored
chore(compass-assistant): keep track of confirmation and entry point sources (#7344)
1 parent a90ac01 commit 6fa086d

File tree

4 files changed

+138
-11
lines changed

4 files changed

+138
-11
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export type AssistantMessage = UIMessage & {
4141
* Used for warning messages in cases like using non-genuine MongoDB.
4242
*/
4343
isPermanent?: boolean;
44+
/** The source of the message (i.e. the entry point used) */
45+
source?: 'explain plan' | 'performance insights' | 'connection error';
4446
/** Information for confirmation messages. */
4547
confirmation?: {
4648
description: string;
@@ -184,7 +186,10 @@ export const AssistantProvider: React.FunctionComponent<
184186
void assistantActionsContext.current.ensureOptInAndSend(
185187
{
186188
text: prompt,
187-
metadata,
189+
metadata: {
190+
...metadata,
191+
source: entryPointName,
192+
},
188193
},
189194
{},
190195
() => {

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ describe('AssistantChat', function () {
3939
sourceId: '1',
4040
},
4141
],
42+
metadata: {
43+
source: 'performance insights',
44+
},
4245
},
4346
];
4447

@@ -427,6 +430,7 @@ describe('AssistantChat', function () {
427430
feedback: 'positive',
428431
text: undefined,
429432
request_id: null,
433+
source: 'performance insights',
430434
});
431435
});
432436
});
@@ -453,6 +457,7 @@ describe('AssistantChat', function () {
453457
feedback: 'negative',
454458
text: undefined,
455459
request_id: null,
460+
source: 'performance insights',
456461
});
457462
});
458463
});
@@ -489,12 +494,42 @@ describe('AssistantChat', function () {
489494
feedback: 'negative',
490495
text: undefined,
491496
request_id: null,
497+
source: 'performance insights',
492498
});
493499

494500
expect(track).to.have.been.calledWith('Assistant Feedback Submitted', {
495501
feedback: 'negative',
496502
text: 'This response was not helpful',
497503
request_id: null,
504+
source: 'performance insights',
505+
});
506+
});
507+
});
508+
509+
it('tracks it as "chat response" when source is not present', async function () {
510+
const { result } = renderWithChat([
511+
{
512+
...mockMessages[1],
513+
metadata: {
514+
...mockMessages[1].metadata,
515+
source: undefined,
516+
},
517+
},
518+
]);
519+
const { track } = result;
520+
521+
const thumbsDownButton = within(
522+
screen.getByTestId('assistant-message-assistant')
523+
).getByLabelText('Dislike this message');
524+
525+
userEvent.click(thumbsDownButton);
526+
527+
await waitFor(() => {
528+
expect(track).to.have.been.calledWith('Assistant Feedback Submitted', {
529+
feedback: 'negative',
530+
text: undefined,
531+
request_id: null,
532+
source: 'chat response',
498533
});
499534
});
500535
});
@@ -534,6 +569,7 @@ describe('AssistantChat', function () {
534569
state: 'pending',
535570
description: 'Are you sure you want to proceed with this action?',
536571
},
572+
source: 'performance insights',
537573
},
538574
};
539575
});
@@ -750,6 +786,65 @@ describe('AssistantChat', function () {
750786
expect(screen.queryByText('Cancel')).to.not.exist;
751787
expect(screen.getByText('This is a regular message')).to.exist;
752788
});
789+
790+
it('tracks confirmation submitted when confirm button is clicked', async function () {
791+
const { result } = renderWithChat([mockConfirmationMessage]);
792+
const { track } = result;
793+
794+
const confirmButton = screen.getByText('Confirm');
795+
userEvent.click(confirmButton);
796+
797+
await waitFor(() => {
798+
expect(track).to.have.been.calledWith(
799+
'Assistant Confirmation Submitted',
800+
{
801+
status: 'confirmed',
802+
source: 'performance insights',
803+
}
804+
);
805+
});
806+
});
807+
808+
it('tracks confirmation submitted when cancel button is clicked', async function () {
809+
const { result } = renderWithChat([mockConfirmationMessage]);
810+
const { track } = result;
811+
812+
const cancelButton = screen.getByText('Cancel');
813+
userEvent.click(cancelButton);
814+
815+
await waitFor(() => {
816+
expect(track).to.have.been.calledWith(
817+
'Assistant Confirmation Submitted',
818+
{
819+
status: 'rejected',
820+
source: 'performance insights',
821+
}
822+
);
823+
});
824+
});
825+
826+
it('tracks it as "chat response" when source is not present', async function () {
827+
const { result } = renderWithChat([
828+
{
829+
...mockConfirmationMessage,
830+
metadata: {
831+
...mockConfirmationMessage.metadata,
832+
source: undefined,
833+
},
834+
},
835+
]);
836+
const { track } = result;
837+
838+
const confirmButton = screen.getByText('Confirm');
839+
userEvent.click(confirmButton);
840+
841+
await waitFor(() => {
842+
expect(track).to.have.been.calledWith(
843+
'Assistant Confirmation Submitted',
844+
{ status: 'confirmed', source: 'chat response' }
845+
);
846+
});
847+
});
753848
});
754849

755850
describe('related sources', function () {

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,11 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
203203
);
204204

205205
const handleFeedback = useCallback(
206-
(
207-
event,
206+
({
207+
state,
208+
message,
209+
}: {
210+
message: AssistantMessage;
208211
state:
209212
| {
210213
feedback: string;
@@ -213,8 +216,8 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
213216
| {
214217
rating: string;
215218
}
216-
| undefined
217-
) => {
219+
| undefined;
220+
}) => {
218221
if (!state) {
219222
return;
220223
}
@@ -227,6 +230,7 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
227230
feedback,
228231
text: textFeedback,
229232
request_id: null,
233+
source: message.metadata?.source ?? 'chat response',
230234
});
231235
},
232236
[track]
@@ -270,6 +274,10 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
270274
}
271275
return newMessages;
272276
});
277+
track('Assistant Confirmation Submitted', {
278+
status: newState,
279+
source: confirmedMessage.metadata?.source ?? 'chat response',
280+
});
273281
if (newState === 'confirmed') {
274282
// Force the new message request to be sent
275283
void ensureOptInAndSend?.(undefined, {}, () => {});
@@ -344,8 +352,12 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
344352
>
345353
{isSender === false && (
346354
<Message.Actions
347-
onRatingChange={handleFeedback}
348-
onSubmitFeedback={handleFeedback}
355+
onRatingChange={(event, state) =>
356+
handleFeedback({ message, state })
357+
}
358+
onSubmitFeedback={(event, state) =>
359+
handleFeedback({ message, state })
360+
}
349361
/>
350362
)}
351363
{sources.length > 0 && <Message.Links links={sources} />}

packages/compass-telemetry/src/telemetry-events.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,18 @@ type AssistantPromptSubmittedEvent = CommonEvent<{
14711471
};
14721472
}>;
14731473

1474+
/**
1475+
* This event is fired when a user uses an assistant entry point.
1476+
*
1477+
* @category Gen AI
1478+
*/
1479+
type AssistantEntryPointUsedEvent = CommonEvent<{
1480+
name: 'Assistant Entry Point Used';
1481+
payload: {
1482+
source: 'explain plan' | 'performance insights' | 'connection error';
1483+
};
1484+
}>;
1485+
14741486
/**
14751487
* This event is fired when a user submits feedback for the assistant.
14761488
*
@@ -1482,18 +1494,20 @@ type AssistantFeedbackSubmittedEvent = CommonEvent<{
14821494
feedback: 'positive' | 'negative';
14831495
text: string | undefined;
14841496
request_id: string | null;
1497+
source: AssistantEntryPointUsedEvent['payload']['source'] | 'chat response';
14851498
};
14861499
}>;
14871500

14881501
/**
1489-
* This event is fired when a user uses an assistant entry point.
1502+
* This event is fired when a user confirms a confirmation message in the assistant chat.
14901503
*
14911504
* @category Gen AI
14921505
*/
1493-
type AssistantEntryPointUsedEvent = CommonEvent<{
1494-
name: 'Assistant Entry Point Used';
1506+
type AssistantConfirmationSubmittedEvent = CommonEvent<{
1507+
name: 'Assistant Confirmation Submitted';
14951508
payload: {
1496-
source: 'explain plan' | 'performance insights' | 'connection error';
1509+
status: 'confirmed' | 'rejected';
1510+
source: AssistantEntryPointUsedEvent['payload']['source'] | 'chat response';
14971511
};
14981512
}>;
14991513

@@ -3032,6 +3046,7 @@ export type TelemetryEvent =
30323046
| AssistantResponseFailedEvent
30333047
| AssistantFeedbackSubmittedEvent
30343048
| AssistantEntryPointUsedEvent
3049+
| AssistantConfirmationSubmittedEvent
30353050
| AiOptInModalShownEvent
30363051
| AiOptInModalDismissedEvent
30373052
| AiGenerateQueryClickedEvent

0 commit comments

Comments
 (0)