Skip to content
Draft
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
111 changes: 18 additions & 93 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/compass-assistant/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write ."
},
"dependencies": {
"@ai-sdk/openai": "^2.0.4",
"@ai-sdk/openai": "^2.0.22",
"@mongodb-js/atlas-service": "^0.56.0",
"@mongodb-js/compass-app-registry": "^9.4.20",
"@mongodb-js/compass-components": "^1.49.0",
Expand Down
11 changes: 8 additions & 3 deletions packages/compass-assistant/src/compass-assistant-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { atlasServiceLocator } from '@mongodb-js/atlas-service/provider';
import { DocsProviderTransport } from './docs-provider-transport';
import { useDrawerActions } from '@mongodb-js/compass-components';
import { buildConnectionErrorPrompt, buildExplainPlanPrompt } from './prompts';
import { buildPrompts } from './prompts';
import { usePreference } from 'compass-preferences-model/provider';
import { createLoggerLocator } from '@mongodb-js/compass-logging/provider';
import type { ConnectionInfo } from '@mongodb-js/connection-info';
Expand All @@ -21,6 +21,7 @@ export type AssistantMessage = UIMessage & {
metadata?: {
/** The text to display instead of the message text. */
displayText?: string;
type?: keyof typeof buildPrompts;
};
};

Expand Down Expand Up @@ -91,14 +92,15 @@ export const AssistantProvider: React.FunctionComponent<
const assistantActionsContext = useRef<AssistantActionsContextType>({
interpretExplainPlan: ({ explainPlan }) => {
openDrawer(ASSISTANT_DRAWER_ID);
const { prompt, displayText } = buildExplainPlanPrompt({
const { prompt, displayText } = buildPrompts['explain-plan'].user({
explainPlan,
});
void chat.sendMessage(
{
text: prompt,
metadata: {
displayText,
type: 'explain-plan',
},
},
{}
Expand All @@ -112,13 +114,16 @@ export const AssistantProvider: React.FunctionComponent<
);
const connectionError = error.toString();

const { prompt } = buildConnectionErrorPrompt({
const { prompt } = buildPrompts['connection-error'].user({
connectionString,
connectionError,
});
void chat.sendMessage(
{
text: prompt,
metadata: {
type: 'connection-error',
},
},
{}
);
Expand Down
60 changes: 53 additions & 7 deletions packages/compass-assistant/src/docs-provider-transport.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
import { createOpenAI } from '@ai-sdk/openai';
import { buildPrompts } from './prompts';
import {
type ChatTransport,
convertToModelMessages,
type UIMessage,
type UIMessageChunk,
convertToModelMessages,
type LanguageModel,
type ModelMessage,
streamText,
} from 'ai';
import { createOpenAI } from '@ai-sdk/openai';

export class DocsProviderTransport implements ChatTransport<UIMessage> {
private openai: ReturnType<typeof createOpenAI>;
private createResponse: ReturnType<typeof makeCreateResponse>;

constructor({ baseUrl }: { baseUrl: string }) {
this.openai = createOpenAI({
const openai = createOpenAI({
baseURL: baseUrl,
apiKey: '',
});
this.createResponse = makeCreateResponse({
languageModel: openai.responses('mongodb-chat-latest'),
});
}

sendMessages({
messages,
// TODO: pass the metadata correctly in a strongly typed manner.
// I'm not sure how to do this.
metadata,
Comment on lines +28 to +30
Copy link
Author

Choose a reason for hiding this comment

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

pls help with the typescripting here. i couldnt easily figure this out

abortSignal,
}: Parameters<ChatTransport<UIMessage>['sendMessages']>[0]) {
const result = streamText({
model: this.openai.responses('mongodb-chat-latest'),
const result = this.createResponse({
messages: convertToModelMessages(messages),
abortSignal: abortSignal,
abortSignal,
systemPromptType: metadata?.type as keyof typeof buildPrompts,
});

return Promise.resolve(result.toUIMessageStream());
Expand All @@ -35,3 +44,40 @@ export class DocsProviderTransport implements ChatTransport<UIMessage> {
return Promise.resolve(null);
}
}

export function makeCreateResponse({
Copy link
Author

@mongodben mongodben Aug 28, 2025

Choose a reason for hiding this comment

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

moved LLM call to a helper function which is used both here and in the evals

languageModel,
}: {
languageModel: LanguageModel;
}) {
return function createResponse({
messages,
abortSignal,
systemPromptType,
headers,
}: {
messages: ModelMessage[];
abortSignal?: AbortSignal;
systemPromptType?: keyof typeof buildPrompts;
headers?: Record<string, string>;
}) {
const instructions =
systemPromptType && buildPrompts[systemPromptType]?.system
? buildPrompts[systemPromptType].system
: undefined;

const result = streamText({
model: languageModel,
messages: messages,
abortSignal: abortSignal,
headers,
providerOptions: {
openai: {
// Have to pass like this because it only accepts JSONValue (not instructions: undefined).
...(instructions ? { instructions } : {}),
},
},
});
return result;
};
}
4 changes: 4 additions & 0 deletions packages/compass-assistant/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ export {
compassAssistantServiceLocator,
} from './compass-assistant-provider';
export type { CompassAssistantService } from './compass-assistant-provider';
export type {
DocsProviderTransport,
makeCreateResponse,
} from './docs-provider-transport';
19 changes: 17 additions & 2 deletions packages/compass-assistant/src/prompts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const buildExplainPlanPrompt = ({
const explainPlanSystemPrompt = `TODO:....add custom prompt stuff here`;
Copy link
Author

Choose a reason for hiding this comment

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

add these prompts in future PR.

julian or whoever is working on this can iterate on the prompts and PR them


const buildExplainPlanUserPrompt = ({
explainPlan,
}: {
explainPlan: string;
Expand All @@ -12,7 +14,9 @@ ${explainPlan}`,
};
};

export const buildConnectionErrorPrompt = ({
const connectionErrorSystemPrompt = `TODO:....add custom prompt stuff here`;

const buildConnectionUserErrorPrompt = ({
connectionString,
connectionError,
}: {
Expand All @@ -29,3 +33,14 @@ Error message:
${connectionError}`,
};
};

export const buildPrompts = {
Copy link
Author

Choose a reason for hiding this comment

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

more extensible by using Record data structure

'explain-plan': {
user: buildExplainPlanUserPrompt,
system: explainPlanSystemPrompt,
},
'connection-error': {
user: buildConnectionUserErrorPrompt,
system: connectionErrorSystemPrompt,
},
} as const;
Loading