Skip to content

Commit 4df81ea

Browse files
committed
chore: fix telemetry
1 parent 27e95a3 commit 4df81ea

File tree

5 files changed

+318
-135
lines changed

5 files changed

+318
-135
lines changed

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

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '@mongodb-js/testing-library-compass';
1111
import {
1212
CompassAssistantProvider,
13+
createDefaultChat,
1314
useAssistantActions,
1415
type AssistantMessage,
1516
} from './compass-assistant-provider';
@@ -24,8 +25,10 @@ import {
2425
import type { AtlasAuthService } from '@mongodb-js/atlas-service/provider';
2526
import type { AtlasService } from '@mongodb-js/atlas-service/provider';
2627
import { CompassAssistantDrawer } from './compass-assistant-drawer';
27-
import { createMockChat } from '../test/utils';
28+
import { createBrokenTransport, createMockChat } from '../test/utils';
2829
import type { AtlasAiService } from '@mongodb-js/compass-generative-ai/provider';
30+
import type { TrackFunction } from '@mongodb-js/compass-telemetry';
31+
import { createLogger } from '@mongodb-js/compass-logging';
2932

3033
function createMockProvider({
3134
mockAtlasService,
@@ -463,6 +466,44 @@ describe('CompassAssistantProvider', function () {
463466
expect(screen.queryByText('Hello assistant!')).to.not.exist;
464467
});
465468

469+
describe('error handling with default chat', function () {
470+
it('fires a telemetry event and displays error banner an when error occurs', async function () {
471+
const track = sinon.stub();
472+
const chat = createDefaultChat({
473+
options: {
474+
transport: createBrokenTransport(),
475+
},
476+
originForPrompt: 'mongodb-compass',
477+
appNameForPrompt: 'MongoDB Compass',
478+
atlasService: {
479+
assistantApiEndpoint: sinon
480+
.stub()
481+
.returns('https://localhost:3000'),
482+
} as unknown as AtlasService,
483+
logger: createLogger('COMPASS-ASSISTANT-TEST'),
484+
track: track as unknown as TrackFunction,
485+
});
486+
await renderOpenAssistantDrawer({
487+
chat,
488+
});
489+
490+
// Send a message
491+
userEvent.type(
492+
screen.getByPlaceholderText('Ask a question'),
493+
'Hello assistant!'
494+
);
495+
userEvent.click(screen.getByLabelText('Send message'));
496+
497+
await waitFor(() => {
498+
expect(screen.getByText(/Test connection error/)).to.exist;
499+
});
500+
501+
expect(track).to.have.been.calledWith('Assistant Response Failed', {
502+
error_name: 'ConnectionError',
503+
});
504+
});
505+
});
506+
466507
describe('clear chat button', function () {
467508
it('is hidden when the chat is empty', async function () {
468509
const mockChat = createMockChat({ messages: [] });
@@ -613,49 +654,47 @@ describe('CompassAssistantProvider', function () {
613654
});
614655
});
615656

616-
describe('CompassAssistantProvider', function () {
617-
it('uses the Atlas Service assistantApiEndpoint', async function () {
618-
const mockAtlasService = {
619-
assistantApiEndpoint: sinon
620-
.stub()
621-
.returns('https://example.com/assistant/api/v1'),
622-
};
657+
it('uses the Atlas Service assistantApiEndpoint', async function () {
658+
const mockAtlasService = {
659+
assistantApiEndpoint: sinon
660+
.stub()
661+
.returns('https://example.com/assistant/api/v1'),
662+
};
623663

624-
const mockAtlasAiService = {
625-
ensureAiFeatureAccess: sinon.stub().callsFake(() => {
626-
return Promise.resolve();
627-
}),
628-
};
664+
const mockAtlasAiService = {
665+
ensureAiFeatureAccess: sinon.stub().callsFake(() => {
666+
return Promise.resolve();
667+
}),
668+
};
629669

630-
const mockAtlasAuthService = {};
670+
const mockAtlasAuthService = {};
631671

632-
const MockedProvider = CompassAssistantProvider.withMockServices({
633-
atlasService: mockAtlasService as unknown as AtlasService,
634-
atlasAiService: mockAtlasAiService as unknown as AtlasAiService,
635-
atlasAuthService: mockAtlasAuthService as unknown as AtlasAuthService,
636-
});
672+
const MockedProvider = CompassAssistantProvider.withMockServices({
673+
atlasService: mockAtlasService as unknown as AtlasService,
674+
atlasAiService: mockAtlasAiService as unknown as AtlasAiService,
675+
atlasAuthService: mockAtlasAuthService as unknown as AtlasAuthService,
676+
});
637677

638-
render(
639-
<DrawerContentProvider>
640-
<DrawerAnchor />
641-
<MockedProvider
642-
originForPrompt="mongodb-compass"
643-
appNameForPrompt="MongoDB Compass"
644-
/>
645-
</DrawerContentProvider>,
646-
{
647-
preferences: {
648-
enableAIAssistant: true,
649-
enableGenAIFeatures: true,
650-
enableGenAIFeaturesAtlasOrg: true,
651-
cloudFeatureRolloutAccess: { GEN_AI_COMPASS: true },
652-
},
653-
}
654-
);
678+
render(
679+
<DrawerContentProvider>
680+
<DrawerAnchor />
681+
<MockedProvider
682+
originForPrompt="mongodb-compass"
683+
appNameForPrompt="MongoDB Compass"
684+
/>
685+
</DrawerContentProvider>,
686+
{
687+
preferences: {
688+
enableAIAssistant: true,
689+
enableGenAIFeatures: true,
690+
enableGenAIFeaturesAtlasOrg: true,
691+
cloudFeatureRolloutAccess: { GEN_AI_COMPASS: true },
692+
},
693+
}
694+
);
655695

656-
await waitFor(() => {
657-
expect(mockAtlasService.assistantApiEndpoint.calledOnce).to.be.true;
658-
});
696+
await waitFor(() => {
697+
expect(mockAtlasService.assistantApiEndpoint.calledOnce).to.be.true;
659698
});
660699
});
661700
});

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

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@mongodb-js/compass-app-registry';
99
import {
1010
atlasAuthServiceLocator,
11+
type AtlasService,
1112
atlasServiceLocator,
1213
} from '@mongodb-js/atlas-service/provider';
1314
import { DocsProviderTransport } from './docs-provider-transport';
@@ -23,9 +24,16 @@ import {
2324
useIsAIFeatureEnabled,
2425
usePreference,
2526
} from 'compass-preferences-model/provider';
26-
import { createLoggerLocator } from '@mongodb-js/compass-logging/provider';
27+
import {
28+
createLoggerLocator,
29+
type Logger,
30+
} from '@mongodb-js/compass-logging/provider';
2731
import type { ConnectionInfo } from '@mongodb-js/connection-info';
28-
import { useTelemetry } from '@mongodb-js/compass-telemetry/provider';
32+
import {
33+
telemetryLocator,
34+
type TrackFunction,
35+
useTelemetry,
36+
} from '@mongodb-js/compass-telemetry/provider';
2937
import type { AtlasAiService } from '@mongodb-js/compass-generative-ai/provider';
3038
import { atlasAiServiceLocator } from '@mongodb-js/compass-generative-ai/provider';
3139
import { buildConversationInstructionsPrompt } from './prompts';
@@ -280,29 +288,20 @@ export const CompassAssistantProvider = registerCompassPlugin(
280288
</AssistantProvider>
281289
);
282290
},
283-
activate: (initialProps, { atlasService, atlasAiService, logger }) => {
291+
activate: (
292+
{ chat: initialChat, originForPrompt, appNameForPrompt },
293+
{ atlasService, atlasAiService, logger, track }
294+
) => {
284295
const chat =
285-
initialProps.chat ??
286-
new Chat({
287-
transport: new DocsProviderTransport({
288-
origin: initialProps.originForPrompt,
289-
instructions: buildConversationInstructionsPrompt({
290-
target: initialProps.appNameForPrompt,
291-
}),
292-
model: createOpenAI({
293-
baseURL: atlasService.assistantApiEndpoint(),
294-
apiKey: '',
295-
}).responses('mongodb-chat-latest'),
296-
}),
297-
onError: (err: Error) => {
298-
logger.log.error(
299-
logger.mongoLogId(1_001_000_370),
300-
'Assistant',
301-
'Failed to send a message',
302-
{ err }
303-
);
304-
},
296+
initialChat ??
297+
createDefaultChat({
298+
originForPrompt,
299+
appNameForPrompt,
300+
atlasService,
301+
logger,
302+
track,
305303
});
304+
306305
return {
307306
store: { state: { chat, atlasAiService } },
308307
deactivate: () => {},
@@ -313,6 +312,51 @@ export const CompassAssistantProvider = registerCompassPlugin(
313312
atlasService: atlasServiceLocator,
314313
atlasAiService: atlasAiServiceLocator,
315314
atlasAuthService: atlasAuthServiceLocator,
315+
track: telemetryLocator,
316316
logger: createLoggerLocator('COMPASS-ASSISTANT'),
317317
}
318318
);
319+
320+
export function createDefaultChat({
321+
originForPrompt,
322+
appNameForPrompt,
323+
atlasService,
324+
logger,
325+
track,
326+
options,
327+
}: {
328+
originForPrompt: string;
329+
appNameForPrompt: string;
330+
atlasService: AtlasService;
331+
logger: Logger;
332+
track: TrackFunction;
333+
options?: {
334+
transport: Chat<AssistantMessage>['transport'];
335+
};
336+
}): Chat<AssistantMessage> {
337+
return new Chat({
338+
transport:
339+
options?.transport ??
340+
new DocsProviderTransport({
341+
origin: originForPrompt,
342+
instructions: buildConversationInstructionsPrompt({
343+
target: appNameForPrompt,
344+
}),
345+
model: createOpenAI({
346+
baseURL: atlasService.assistantApiEndpoint(),
347+
apiKey: '',
348+
}).responses('mongodb-chat-latest'),
349+
}),
350+
onError: (err: Error) => {
351+
logger.log.error(
352+
logger.mongoLogId(1_001_000_370),
353+
'Assistant',
354+
'Failed to send a message',
355+
{ err }
356+
);
357+
track('Assistant Response Failed', {
358+
error_name: err.name,
359+
});
360+
},
361+
});
362+
}

0 commit comments

Comments
 (0)