Skip to content

chore(compass-assistant): add compass AI assistant with placeholders COMPASS-9603 #7189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Aug 14, 2025
Merged
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
469 changes: 469 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/atlas-service/src/atlas-service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const ATLAS_CONFIG = {
issuer: 'http://example.com/oauth2/default',
},
authPortalUrl: 'http://example.com/account/login',
assistantApiBaseUrl: 'http://example.com/assistant',
};

function getAtlasService(
Expand Down
3 changes: 3 additions & 0 deletions packages/atlas-service/src/atlas-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ export class AtlasService {
cloudEndpoint(path?: string): string {
return `${this.config.cloudBaseUrl}${normalizePath(path)}`;
}
assistantApiEndpoint(path?: string): string {
return `${this.config.assistantApiBaseUrl}${normalizePath(path)}`;
}
regionalizedCloudEndpoint(
_atlasMetadata: Pick<AtlasClusterMetadata, 'regionalBaseUrl'>,
path?: string
Expand Down
1 change: 1 addition & 0 deletions packages/atlas-service/src/main.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('CompassAuthServiceMain', function () {
clientId: '1234abcd',
},
authPortalUrl: 'http://example.com',
assistantApiBaseUrl: 'http://example.com/assistant',
};

const fetch = CompassAuthService['fetch'];
Expand Down
13 changes: 13 additions & 0 deletions packages/atlas-service/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export type AtlasServiceConfig = {
* Atlas Account Portal UI base url
*/
authPortalUrl: string;
/**
* Assistant API base url
*/
assistantApiBaseUrl: string;
};

/**
Expand All @@ -139,6 +143,7 @@ const config = {
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
'atlas-dev': {
wsBaseUrl: '',
Expand All @@ -149,6 +154,7 @@ const config = {
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
'atlas-qa': {
wsBaseUrl: '',
Expand All @@ -159,6 +165,7 @@ const config = {
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-qa.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
atlas: {
wsBaseUrl: '',
Expand All @@ -169,6 +176,7 @@ const config = {
issuer: 'https://auth.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
'web-sandbox-atlas-local': {
wsBaseUrl: '/ccs',
Expand All @@ -179,6 +187,7 @@ const config = {
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
'web-sandbox-atlas-dev': {
wsBaseUrl: '/ccs',
Expand All @@ -189,6 +198,7 @@ const config = {
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
'web-sandbox-atlas-qa': {
wsBaseUrl: '/ccs',
Expand All @@ -199,6 +209,7 @@ const config = {
issuer: 'https://auth-qa.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account-dev.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
'web-sandbox-atlas': {
wsBaseUrl: '/ccs',
Expand All @@ -209,6 +220,7 @@ const config = {
issuer: 'https://auth.mongodb.com/oauth2/default',
},
authPortalUrl: 'https://account.mongodb.com/account/login',
assistantApiBaseUrl: 'https://knowledge.staging.corp.mongodb.com/api/v1',
},
} as const;

Expand All @@ -223,6 +235,7 @@ export function getAtlasConfig(
issuer: process.env.COMPASS_OIDC_ISSUER_OVERRIDE,
},
authPortalUrl: process.env.COMPASS_ATLAS_AUTH_PORTAL_URL_OVERRIDE,
assistantApiBaseUrl: process.env.COMPASS_ASSISTANT_BASE_URL_OVERRIDE,
};
return defaultsDeep(
envConfig,
Expand Down
11 changes: 11 additions & 0 deletions packages/compass-assistant/.depcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ignores:
- '@mongodb-js/prettier-config-compass'
- '@mongodb-js/tsconfig-compass'
- '@types/chai'
- '@types/sinon-chai'
- 'sinon'
- '@types/chai-dom'
- '@types/react'
- '@types/react-dom'
ignore-patterns:
- 'dist'
2 changes: 2 additions & 0 deletions packages/compass-assistant/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.nyc-output
dist
8 changes: 8 additions & 0 deletions packages/compass-assistant/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
root: true,
extends: ['@mongodb-js/eslint-config-compass/plugin'],
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig-lint.json'],
},
};
1 change: 1 addition & 0 deletions packages/compass-assistant/.mocharc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('@mongodb-js/mocha-config-compass/compass-plugin');
83 changes: 83 additions & 0 deletions packages/compass-assistant/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"name": "@mongodb-js/compass-assistant",
"description": "Compass plugin for the AI Assistant",
"author": {
"name": "MongoDB Inc",
"email": "[email protected]"
},
"private": true,
"bugs": {
"url": "https://jira.mongodb.org/projects/COMPASS/issues",
"email": "[email protected]"
},
"homepage": "https://github.com/mongodb-js/compass",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "https://github.com/mongodb-js/compass.git"
},
"files": [
"dist"
],
"license": "SSPL",
"main": "dist/index.js",
"compass:main": "src/index.tsx",
"exports": {
"import": "./dist/.esm-wrapper.mjs",
"require": "./dist/index.js"
},
"compass:exports": {
".": "./src/index.tsx"
},
"types": "./dist/index.d.ts",
"scripts": {
"bootstrap": "npm run compile",
"compile": "tsc -p tsconfig.json",
"typecheck": "tsc -p tsconfig-lint.json --noEmit",
"eslint": "eslint-compass",
"prettier": "prettier-compass",
"lint": "npm run eslint . && npm run prettier -- --check .",
"depcheck": "compass-scripts check-peer-deps && depcheck",
"check": "npm run typecheck && npm run lint && npm run depcheck",
"check-ci": "npm run check",
"test": "mocha",
"test-electron": "xvfb-maybe electron-mocha --no-sandbox",
"test-cov": "nyc --compact=false --produce-source-map=false -x \"**/*.spec.*\" --reporter=lcov --reporter=text --reporter=html npm run test",
"test-watch": "npm run test -- --watch",
"test-ci": "npm run test-cov",
"test-ci-electron": "npm run test-electron",
"reformat": "npm run eslint . -- --fix && npm run prettier -- --write ."
},
"dependencies": {
"@ai-sdk/openai": "^2.0.4",
"@mongodb-js/atlas-service": "^0.54.0",
"@mongodb-js/compass-app-registry": "^9.4.18",
"@mongodb-js/compass-components": "^1.30.5",
"ai": "^5.0.5",
"compass-preferences-model": "^2.49.0",
"react": "^17.0.2",
"throttleit": "^2.1.0",
"use-sync-external-store": "^1.5.0"
},
"devDependencies": {
"@mongodb-js/eslint-config-compass": "^1.4.5",
"@mongodb-js/mocha-config-compass": "^1.7.0",
"@mongodb-js/prettier-config-compass": "^1.2.8",
"@mongodb-js/testing-library-compass": "^1.3.8",
"@mongodb-js/tsconfig-compass": "^1.2.9",
"@types/chai": "^4.2.21",
"@types/chai-dom": "^0.0.10",
"@types/mocha": "^9.0.0",
"@types/react": "^17.0.5",
"@types/react-dom": "^17.0.10",
"@types/sinon-chai": "^3.2.5",
"chai": "^4.3.6",
"depcheck": "^1.4.1",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"sinon": "^17.0.1",
"typescript": "^5.8.3",
"xvfb-maybe": "^0.2.1"
},
"is_compass_plugin": true
}
149 changes: 149 additions & 0 deletions packages/compass-assistant/src/@ai-sdk/react/chat-react.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
// Copyright 2023 Vercel, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import {
AbstractChat,
type ChatInit,
type ChatState,
type ChatStatus,
type UIMessage,
} from 'ai';
import { throttle } from './throttle';

class ReactChatState<UI_MESSAGE extends UIMessage>
implements ChatState<UI_MESSAGE>
{
#messages: UI_MESSAGE[];
#status: ChatStatus = 'ready';
#error: Error | undefined = undefined;

#messagesCallbacks = new Set<() => void>();
#statusCallbacks = new Set<() => void>();
#errorCallbacks = new Set<() => void>();

constructor(initialMessages: UI_MESSAGE[] = []) {
this.#messages = initialMessages;
}

get status(): ChatStatus {
return this.#status;
}

set status(newStatus: ChatStatus) {
this.#status = newStatus;
this.#callStatusCallbacks();
}

get error(): Error | undefined {
return this.#error;
}

set error(newError: Error | undefined) {
this.#error = newError;
this.#callErrorCallbacks();
}

get messages(): UI_MESSAGE[] {
return this.#messages;
}

set messages(newMessages: UI_MESSAGE[]) {
this.#messages = [...newMessages];
this.#callMessagesCallbacks();
}

pushMessage = (message: UI_MESSAGE) => {
this.#messages = this.#messages.concat(message);
this.#callMessagesCallbacks();
};

popMessage = () => {
this.#messages = this.#messages.slice(0, -1);
this.#callMessagesCallbacks();
};

replaceMessage = (index: number, message: UI_MESSAGE) => {
this.#messages = [
...this.#messages.slice(0, index),
// We deep clone the message here to ensure the new React Compiler (currently in RC) detects deeply nested parts/metadata changes:
this.snapshot(message),
...this.#messages.slice(index + 1),
];
this.#callMessagesCallbacks();
};

snapshot = <T>(value: T): T => structuredClone(value);

'~registerMessagesCallback' = (
onChange: () => void,
throttleWaitMs?: number
): (() => void) => {
const callback = throttleWaitMs
? throttle(onChange, throttleWaitMs)
: onChange;
this.#messagesCallbacks.add(callback);
return () => {
this.#messagesCallbacks.delete(callback);
};
};

'~registerStatusCallback' = (onChange: () => void): (() => void) => {
this.#statusCallbacks.add(onChange);
return () => {
this.#statusCallbacks.delete(onChange);
};
};

'~registerErrorCallback' = (onChange: () => void): (() => void) => {
this.#errorCallbacks.add(onChange);
return () => {
this.#errorCallbacks.delete(onChange);
};
};

#callMessagesCallbacks = () => {
this.#messagesCallbacks.forEach((callback) => callback());
};

#callStatusCallbacks = () => {
this.#statusCallbacks.forEach((callback) => callback());
};

#callErrorCallbacks = () => {
this.#errorCallbacks.forEach((callback) => callback());
};
}

export class Chat<
UI_MESSAGE extends UIMessage
> extends AbstractChat<UI_MESSAGE> {
#state: ReactChatState<UI_MESSAGE>;

constructor({ messages, ...init }: ChatInit<UI_MESSAGE>) {
const state = new ReactChatState(messages);
super({ ...init, state });
this.#state = state;
}

'~registerMessagesCallback' = (
onChange: () => void,
throttleWaitMs?: number
): (() => void) =>
this.#state['~registerMessagesCallback'](onChange, throttleWaitMs);

'~registerStatusCallback' = (onChange: () => void): (() => void) =>
this.#state['~registerStatusCallback'](onChange);

'~registerErrorCallback' = (onChange: () => void): (() => void) =>
this.#state['~registerErrorCallback'](onChange);
}
22 changes: 22 additions & 0 deletions packages/compass-assistant/src/@ai-sdk/react/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2023 Vercel, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import throttleFunction from 'throttleit';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function throttle<T extends (...args: any[]) => any>(
fn: T,
waitMs: number | undefined
): T {
return waitMs !== undefined ? throttleFunction(fn, waitMs) : fn;
}
Loading
Loading