Skip to content

Commit 6018ef5

Browse files
committed
fix: working atlas provider
1 parent 2a5f7cd commit 6018ef5

17 files changed

+800
-317
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { log } from '../logger';
1+
import { log } from '../../logger';
22
import { AuthService } from './auth-service';
33
import type { AtlasServiceConfig } from './util';
44
import { throwIfAborted } from './util';
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EventEmitter } from 'events';
66
import { createMongoDBOIDCPlugin } from '@mongodb-js/oidc-plugin';
77
import { oidcServerRequestHandler } from '@mongodb-js/devtools-connect';
88
import { Server } from 'http';
9-
import { log } from '../logger';
9+
import { log } from '../../logger';
1010

1111
const redirectRequestHandler = oidcServerRequestHandler.bind(null, {
1212
productName: 'mongosh AI Suite',

snippets/ai/auth/atlas-auth-cli.ts

Lines changed: 0 additions & 94 deletions
This file was deleted.

snippets/ai/decorators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { output } from "./helpers";
22
import { createLoadingAnimation } from "./helpers";
33

4+
45
export function aiCommand<T extends Function>(
56
value: T,
67
// eslint-disable-next-line @typescript-eslint/no-unused-vars

snippets/ai/helpers.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
import { AIQuery } from "./auth/atlas-ai-service";
1+
import process from "process";
22

33
export function output(text: string) {
44
process.stdout.write(`${text}`);
55
}
66

7-
export function setInput(text: string) {
8-
process.stdin.unshift(text);
9-
}
107
export function createLoadingAnimation({signal, message = 'Loading'}: {signal: AbortSignal, message?: string}): {
118
start: (message?: string) => void;
129
stop: () => void;
@@ -38,13 +35,3 @@ export function createLoadingAnimation({signal, message = 'Loading'}: {signal: A
3835
};
3936
}
4037

41-
export class MongoshCommandBuilder {
42-
createMongoShellQuery(params: AIQuery['content']): string {
43-
const {filter, project, collation, sort, skip, limit} = params.query;
44-
45-
return `db.collection.find(
46-
${filter},
47-
${project ? `{ projection: ${project} }` : '{}'}
48-
)${collation ? `.collation(${collation})` : ''}${sort ? `.sort(${sort})` : ''}${skip ? `.skip(${skip})` : ''}${limit ? `.limit(${limit})` : ''}`
49-
};
50-
}

snippets/ai/index.ts

Lines changed: 20 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,29 @@
1-
import { AuthService } from './auth/auth-service';
2-
import { AtlasService } from './auth/atlas-service';
3-
import { AtlasAiService } from './auth/atlas-ai-service';
4-
import { config } from './auth/util';
5-
import { createLoadingAnimation, MongoshCommandBuilder, output, setInput } from './helpers';
6-
import open from 'open';
71
import { aiCommand, withLoadingAnimation } from './decorators';
8-
9-
const authService = new AuthService({
10-
...config['atlas'],
11-
openBrowser: async (url: string) => {
12-
output('Opening authentication page in your default browser...');
13-
await open(url);
14-
},
15-
});
16-
17-
const atlasService = new AtlasService(authService, {
18-
...config['atlas'],
19-
});
20-
21-
const aiService = new AtlasAiService({
22-
atlasService,
23-
apiURLPreset: 'admin-api',
24-
});
25-
26-
const mongoshCommandBuilder = new MongoshCommandBuilder();
2+
import { AiProvider } from './providers/ai-provider';
3+
import { getAtlasAiProvider } from './providers/atlas/atlas-ai-provider';
274

285
class AI {
29-
constructor(private readonly context: any, private readonly aiService: AtlasAiService) {
6+
constructor(private readonly cliContext: any, private readonly ai: AiProvider) {
307
const methods = Object.getOwnPropertyNames(Object.getPrototypeOf(this))
318
.filter(name => {
329
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(this), name);
3310
return descriptor && typeof descriptor.value === 'function' && name !== 'constructor';
3411
});
35-
console.log('Class methods:', methods);
3612

3713
// for all methods, wrap them with the wrapFunction method
3814
for (const methodName of methods) {
3915
const method = (this as any)[methodName];
4016
if (typeof method === 'function' && method.isDirectShellCommand) {
41-
this.wrapFunction(methodName, method);
17+
this.wrapFunction(methodName, method.bind(this));
4218
}
4319
}
20+
const instanceState = this.cliContext.db._mongo._instanceState;
21+
instanceState.registerPlugin(this);
22+
23+
this.wrapFunction(undefined, this.help.bind(this));
4424
}
4525

46-
wrapFunction(name: string, fn: Function) {
26+
private wrapFunction(name: string | undefined, fn: Function) {
4727
const wrapperFn = (...args: string[]) => {
4828
return Object.assign(fn(...args), {
4929
[Symbol.for('@@mongosh.syntheticPromise')]: true,
@@ -52,49 +32,31 @@ class AI {
5232
wrapperFn.isDirectShellCommand = true;
5333
wrapperFn.returnsPromise = true;
5434

55-
const instanceState = this.context.db._mongo._instanceState;
35+
const instanceState = this.cliContext.db._mongo._instanceState;
5636

57-
(instanceState as any).shellApi[`ai.${name}`] = (instanceState as any).context[`ai.${name}`] = wrapperFn;
58-
instanceState.registerPlugin(this);
37+
instanceState.shellApi[name ? `ai.${name}` : 'ai'] = instanceState.context[name ? `ai.${name}` : 'ai'] = wrapperFn;
5938
}
6039

6140
@aiCommand
41+
@withLoadingAnimation('Generating query...')
6242
async query(code: string) {
63-
const signal = AbortSignal.timeout(10000);
64-
const loadingAnimation = createLoadingAnimation({signal, message: 'Generating query...'});
65-
loadingAnimation.start();
66-
67-
const result = await aiService.getQueryFromUserInput(
68-
{
69-
userInput: code,
70-
databaseName: 'test',
71-
collectionName: 'test',
72-
signal,
73-
requestId: 'test',
74-
},
75-
{
76-
connectionOptions: {
77-
connectionString: 'mongodb://localhost:27017',
78-
},
79-
id: '1234',
80-
},
81-
);
82-
loadingAnimation.stop();
43+
return await this.ai.query(code);
44+
}
8345

84-
const query = mongoshCommandBuilder.createMongoShellQuery(result.content);
85-
setInput(query);
46+
@aiCommand
47+
@withLoadingAnimation('Thinking...')
48+
async ask(code: string) {
49+
return await this.ai.ask(code);
8650
}
8751

8852
@aiCommand
89-
@withLoadingAnimation('Generating help...')
9053
async help(...args: string[]) {
91-
await new Promise(resolve => setTimeout(resolve, 1000));
54+
this.ai.help();
9255
}
9356
}
9457

95-
9658
module.exports = (globalThis: any) => {
97-
globalThis.ai = new AI(globalThis, aiService);
59+
globalThis.ai = new AI(globalThis, getAtlasAiProvider());
9860
};
9961

10062

0 commit comments

Comments
 (0)