Skip to content

Commit dc1b4bd

Browse files
committed
chore(compass-assistant): keep track of confirmation and entry point sources
1 parent 9c656bb commit dc1b4bd

File tree

4 files changed

+87
-11
lines changed

4 files changed

+87
-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;
@@ -182,7 +184,10 @@ export const AssistantProvider: React.FunctionComponent<
182184
void assistantActionsContext.current.ensureOptInAndSend(
183185
{
184186
text: prompt,
185-
metadata,
187+
metadata: {
188+
...metadata,
189+
source: entryPointName,
190+
},
186191
},
187192
{},
188193
() => {

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

Lines changed: 44 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

@@ -440,6 +443,7 @@ describe('AssistantChat', function () {
440443
feedback: 'positive',
441444
text: undefined,
442445
request_id: null,
446+
source: 'performance insights',
443447
});
444448
});
445449
});
@@ -466,6 +470,7 @@ describe('AssistantChat', function () {
466470
feedback: 'negative',
467471
text: undefined,
468472
request_id: null,
473+
source: 'performance insights',
469474
});
470475
});
471476
});
@@ -502,12 +507,14 @@ describe('AssistantChat', function () {
502507
feedback: 'negative',
503508
text: undefined,
504509
request_id: null,
510+
source: 'performance insights',
505511
});
506512

507513
expect(track).to.have.been.calledWith('Assistant Feedback Submitted', {
508514
feedback: 'negative',
509515
text: 'This response was not helpful',
510516
request_id: null,
517+
source: 'performance insights',
511518
});
512519
});
513520
});
@@ -547,6 +554,7 @@ describe('AssistantChat', function () {
547554
state: 'pending',
548555
description: 'Are you sure you want to proceed with this action?',
549556
},
557+
source: 'performance insights',
550558
},
551559
};
552560
});
@@ -763,6 +771,42 @@ describe('AssistantChat', function () {
763771
expect(screen.queryByText('Cancel')).to.not.exist;
764772
expect(screen.getByText('This is a regular message')).to.exist;
765773
});
774+
775+
it('tracks confirmation submitted when confirm button is clicked', async function () {
776+
const { result } = renderWithChat([mockConfirmationMessage]);
777+
const { track } = result;
778+
779+
const confirmButton = screen.getByText('Confirm');
780+
userEvent.click(confirmButton);
781+
782+
await waitFor(() => {
783+
expect(track).to.have.been.calledWith(
784+
'Assistant Confirmation Submitted',
785+
{
786+
status: 'confirmed',
787+
source: 'performance insights',
788+
}
789+
);
790+
});
791+
});
792+
793+
it('tracks confirmation submitted when cancel button is clicked', async function () {
794+
const { result } = renderWithChat([mockConfirmationMessage]);
795+
const { track } = result;
796+
797+
const cancelButton = screen.getByText('Cancel');
798+
userEvent.click(cancelButton);
799+
800+
await waitFor(() => {
801+
expect(track).to.have.been.calledWith(
802+
'Assistant Confirmation Submitted',
803+
{
804+
status: 'rejected',
805+
source: 'performance insights',
806+
}
807+
);
808+
});
809+
});
766810
});
767811

768812
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
@@ -195,8 +195,11 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
195195
);
196196

197197
const handleFeedback = useCallback(
198-
(
199-
event,
198+
({
199+
state,
200+
message,
201+
}: {
202+
message: AssistantMessage;
200203
state:
201204
| {
202205
feedback: string;
@@ -205,8 +208,8 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
205208
| {
206209
rating: string;
207210
}
208-
| undefined
209-
) => {
211+
| undefined;
212+
}) => {
210213
if (!state) {
211214
return;
212215
}
@@ -219,6 +222,7 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
219222
feedback,
220223
text: textFeedback,
221224
request_id: null,
225+
source: message.metadata?.source,
222226
});
223227
},
224228
[track]
@@ -262,6 +266,10 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
262266
}
263267
return newMessages;
264268
});
269+
track('Assistant Confirmation Submitted', {
270+
status: newState,
271+
source: confirmedMessage.metadata?.source,
272+
});
265273
if (newState === 'confirmed') {
266274
// Force the new message request to be sent
267275
void ensureOptInAndSend?.(undefined, {}, () => {});
@@ -336,8 +344,12 @@ export const AssistantChat: React.FunctionComponent<AssistantChatProps> = ({
336344
>
337345
{isSender === false && (
338346
<Message.Actions
339-
onRatingChange={handleFeedback}
340-
onSubmitFeedback={handleFeedback}
347+
onRatingChange={(event, state) =>
348+
handleFeedback({ message, state })
349+
}
350+
onSubmitFeedback={(event, state) =>
351+
handleFeedback({ message, state })
352+
}
341353
/>
342354
)}
343355
{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'] | undefined;
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'] | undefined;
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)