Skip to content
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
3 changes: 2 additions & 1 deletion package-lock.json

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

21 changes: 18 additions & 3 deletions packages/compass-assistant/test/tool-calls.eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import { strict as assert } from 'node:assert';
import { MessageChannel } from 'node:worker_threads';
import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';
import type { ToolSet, ToolCallPart } from 'ai';
Expand All @@ -17,6 +18,7 @@ import {
ToolsController,
type ToolGroup,
} from '@mongodb-js/compass-generative-ai/provider';
import { ToolsHost } from '@mongodb-js/compass-generative-ai/utility';
import { createNoopLogger } from '@mongodb-js/compass-logging/provider';
import { createNoopTrack } from '@mongodb-js/compass-telemetry/provider';
import { defaultPreferencesInstance } from '@mongodb-js/compass-preferences-model';
Expand Down Expand Up @@ -65,13 +67,26 @@ const DEFAULT_EVAL_TASK_CONFIG: EvalTaskConfig = {

const logger = createNoopLogger('EVAL-TOOLS-CONTROLLER');

// There is no utility process in Node, so the host runs in this one, on the
// other end of a plain MessageChannel. Held in a variable so the run can
// release its server and driver connection when it finishes.
const evalChannel = new MessageChannel();
Comment thread
nbbeeken marked this conversation as resolved.
Resolved

// worker_threads ports are structurally identical to DOM MessagePorts but not
// nominally, so tests bridge them with this cast.
const asRendererPort = (port: MessageChannel['port1']): MessagePort =>
port as unknown as MessagePort;

const evalHost = new ToolsHost(asRendererPort(evalChannel.port2), { logger });

const toolsController = new ToolsController({
logger,
getTelemetryAnonymousId: () => 'eval-anonymous-id',
track: createNoopTrack(),
enableMCPTelemetry: false,
preferences: defaultPreferencesInstance,
atlasAdminApi: {} as AtlasAdminApiService,
openPort: () => asRendererPort(evalChannel.port1),
});

let toolsInitialized = false;
Expand All @@ -83,9 +98,6 @@ let toolsInitialized = false;
async function ensureToolsInitialized(): Promise<void> {
if (toolsInitialized) return;
await toolsController.startServer();
if (!toolsController.server) {
throw new Error('Failed to start ToolsController server for eval tools');
}
toolsInitialized = true;
}

Expand Down Expand Up @@ -218,6 +230,8 @@ function createToolCallAssistantTask(config: EvalTaskConfig) {
connectionConfig,
});

assert(tools['find'], 'ToolsController did not provide database tools');

const contextPrompt = buildContextPromptText({
custom,
connectionString: connectionConfig.connectionString,
Expand Down Expand Up @@ -277,6 +291,7 @@ async function main() {
});
} finally {
await toolsController.stopServer();
await evalHost.stop();
await client.close();
await cluster.close();
// FIXME(COMPASS-10486): We need to have this otherwise the process hangs.
Expand Down
30 changes: 30 additions & 0 deletions packages/compass-generative-ai/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,34 @@ module.exports = {
tsconfigRootDir: __dirname,
project: ['./tsconfig.json'],
},
overrides: [
{
// Files in this directory run outside the renderer: in the AI assistant
// utility process on desktop, and in the page for compass-web. Renderer
// entries (the provider subpaths, the design system) would drag the UI
// stack into the utility bundle, so utility-side code takes its
// cross-world dependencies via config instead.
files: ['src/tools/infrastructure/**'],
excludedFiles: ['**/*.spec.ts'],
rules: {
'@typescript-eslint/no-restricted-imports': [
'error',
{
patterns: [
{
group: [
'@mongodb-js/compass-logging/provider',
'@mongodb-js/compass-telemetry/provider',
'@mongodb-js/compass-components',
'@mongodb-js/compass-app-registry',
],
message:
'Utility-side code must not import renderer entries; take what it needs via config.',
},
],
},
],
},
},
],
};
8 changes: 5 additions & 3 deletions packages/compass-generative-ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
"compass:main": "src/index.ts",
"exports": {
".": "./dist/index.js",
"./provider": "./dist/provider.js"
"./provider": "./dist/provider.js",
"./utility": "./dist/tools/infrastructure/tools-host.js"
},
"compass:exports": {
".": "./src/index.ts",
"./provider": "./src/provider.tsx"
"./provider": "./src/provider.tsx",
"./utility": "./src/tools/infrastructure/tools-host.ts"
},
"types": "./dist/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -71,6 +73,7 @@
"@mongosh/service-provider-core": "^5.2.0",
"@mongosh/service-provider-node-driver": "^5.2.0",
"ai": "^7.0.0",
"bson": "^7.3.2",
"ipaddr.js": "^2.5.0",
"mongodb": "^7.5.0",
"mongodb-connection-string-url": "^7.0.2",
Expand All @@ -96,7 +99,6 @@
"@types/sinon-chai": "^3.2.5",
"autoevals": "^0.4.0",
"braintrust": "^3.35.0",
"bson": "^7.3.2",
"chai": "^4.3.6",
"depcheck": "^1.4.1",
"electron-mocha": "^12.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('ToolsConnectionManager', function () {
connectionManager = new ToolsConnectionManager({
logger,
getTelemetryAnonymousId,
mongoLogId: (id) => ({ __value: id }),
});
});

Expand Down
12 changes: 8 additions & 4 deletions packages/compass-generative-ai/src/tools-connection-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from '@mongosh/service-provider-node-driver';
import type { ServiceProvider } from '@mongosh/service-provider-core';
import { isAtlas, isAtlasStream } from '@mongodb-js/mongodb-build-info';
import type { Logger } from '@mongodb-js/compass-logging';
import ConnectionString from 'mongodb-connection-string-url';
import { mongoLogId } from '@mongodb-js/compass-logging/provider';

export interface ToolsConnectParams {
connectionId: string;
Expand All @@ -25,11 +25,13 @@ const DEFAULT_TELEMETRY_APP_NAME = 'MongoDB Compass';
type ToolsConnectionManagerConfig = {
logger: LoggerBase;
getTelemetryAnonymousId: () => string;
mongoLogId: Logger['mongoLogId'];
};

export class ToolsConnectionManager extends ConnectionManager {
private logger: LoggerBase;
private getTelemetryAnonymousId: () => string;
private mongoLogId: ToolsConnectionManagerConfig['mongoLogId'];
private activeConnection: {
id: string;
provider: ServiceProvider;
Expand All @@ -38,10 +40,12 @@ export class ToolsConnectionManager extends ConnectionManager {
constructor({
logger,
getTelemetryAnonymousId,
mongoLogId,
}: ToolsConnectionManagerConfig) {
super();
this.logger = logger;
this.getTelemetryAnonymousId = getTelemetryAnonymousId;
this.mongoLogId = mongoLogId;
}

override connect(): Promise<never> {
Expand All @@ -62,7 +66,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www.
await this.activeConnection?.provider?.close();
} catch (error) {
this.logger.error({
id: mongoLogId(1_001_000_411),
id: this.mongoLogId(1_001_000_411),
context: 'compass-tools-connection-manager',
message: `Error disconnecting from Compass connection - ${
error instanceof Error ? error.message : String(error)
Expand Down Expand Up @@ -93,7 +97,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www.

if (isAtlasStream(connectParams.connectionString)) {
this.logger.warning({
id: mongoLogId(1_001_000_413),
id: this.mongoLogId(1_001_000_413),
context: 'compass-tools-connection-manager',
message: 'Attempting a connection to an Atlas Stream.',
});
Expand Down Expand Up @@ -129,7 +133,7 @@ To connect, choose a connection from Compass's connection sidebar - https://www.
);
} catch (error) {
this.logger.error({
id: mongoLogId(1_001_000_412),
id: this.mongoLogId(1_001_000_412),
context: 'compass-tools-connection-manager',
message: `Error connecting to Compass connection - ${
error instanceof Error ? error.message : String(error)
Expand Down
Loading
Loading