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' ;
71import { 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
285class 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-
9658module . exports = ( globalThis : any ) => {
97- globalThis . ai = new AI ( globalThis , aiService ) ;
59+ globalThis . ai = new AI ( globalThis , getAtlasAiProvider ( ) ) ;
9860} ;
9961
10062
0 commit comments