11import { aiCommand } from '../../decorators' ;
22import { output } from '../../helpers' ;
3- import { AiProvider } from '../ai-provider' ;
3+ import { AiProvider , CliContext } from '../ai-provider' ;
44import { AtlasAiService , AIAggregation , AIQuery } from './atlas-ai-service' ;
55import { AtlasService } from './atlas-service' ;
66import { AuthService } from './auth-service' ;
77import { config } from './util' ;
88import open from 'open' ;
99
1010export class AtlasAiProvider extends AiProvider {
11- constructor ( private readonly aiService : AtlasAiService ) {
12- super ( ) ;
11+ constructor (
12+ private readonly aiService : AtlasAiService ,
13+ cliContext : CliContext ,
14+ ) {
15+ super ( cliContext ) ;
1316 }
14-
17+
1518 async query ( prompt : string ) : Promise < void > {
16- const query = await this . aiService . getQueryFromUserInput ( {
17- userInput : prompt ,
18- signal : AbortSignal . timeout ( 10000 ) ,
19- requestId : crypto . randomUUID ( ) ,
20- ...this . getDatabaseContext ( ) ,
21- } , this . getConnectionInfo ( ) ) ;
19+ const query = await this . aiService . getQueryFromUserInput (
20+ {
21+ userInput : prompt ,
22+ signal : AbortSignal . timeout ( 10000 ) ,
23+ requestId : crypto . randomUUID ( ) ,
24+ ...this . getDatabaseContext ( ) ,
25+ } ,
26+ this . getConnectionInfo ( ) ,
27+ ) ;
2228
2329 this . setInput ( this . createMongoShellQuery ( query . content ) ) ;
2430 }
@@ -28,48 +34,53 @@ export class AtlasAiProvider extends AiProvider {
2834 }
2935
3036 async aggregate ( prompt : string ) : Promise < void > {
31- const aggregation = await this . aiService . getAggregationFromUserInput ( {
32- userInput : prompt ,
33- signal : AbortSignal . timeout ( 10000 ) ,
34- requestId : crypto . randomUUID ( ) ,
35- ...this . getDatabaseContext ( ) ,
36- } , this . getConnectionInfo ( ) ) ;
37+ const aggregation = await this . aiService . getAggregationFromUserInput (
38+ {
39+ userInput : prompt ,
40+ signal : AbortSignal . timeout ( 10000 ) ,
41+ requestId : crypto . randomUUID ( ) ,
42+ ...this . getDatabaseContext ( ) ,
43+ } ,
44+ this . getConnectionInfo ( ) ,
45+ ) ;
3746
3847 this . respond ( this . createMongoShellAggregation ( aggregation . content ) ) ;
3948 }
4049
4150 private createMongoShellQuery ( params : AIQuery [ 'content' ] ) : string {
42- const { filter, project, collation, sort, skip, limit} = params . query ;
43-
44- return `db.collection.find(
51+ const { filter, project, collation, sort, skip, limit } = params . query ;
52+
53+ return `db.collection.find(
4554 ${ filter } ,
4655 ${ project ? `{ projection: ${ project } }` : '{}' }
47- )${ collation ? `.collation(${ collation } )` : '' } ${ sort ? `.sort(${ sort } )` : '' } ${ skip ? `.skip(${ skip } )` : '' } ${ limit ? `.limit(${ limit } )` : '' } `
48- } ;
56+ )${ collation ? `.collation(${ collation } )` : '' } ${ sort ? `.sort(${ sort } )` : '' } ${ skip ? `.skip(${ skip } )` : '' } ${ limit ? `.limit(${ limit } )` : '' } ` ;
57+ }
4958
50- private createMongoShellAggregation ( params : AIAggregation [ 'content' ] ) : string {
51- const { aggregation} = params ;
59+ private createMongoShellAggregation (
60+ params : AIAggregation [ 'content' ] ,
61+ ) : string {
62+ const { aggregation } = params ;
5263 return `db.collection.aggregate(${ aggregation ?. pipeline } )` ;
5364 }
5465}
5566
56- export function getAtlasAiProvider ( ) : AtlasAiProvider {
67+ export function getAtlasAiProvider ( cliContext : CliContext ) : AtlasAiProvider {
5768 const authService = new AuthService ( {
5869 ...config [ 'atlas' ] ,
5970 openBrowser : async ( url : string ) => {
6071 output ( 'Opening authentication page in your default browser...' ) ;
6172 await open ( url ) ;
6273 } ,
6374 } ) ;
64-
75+
6576 const atlasService = new AtlasService ( authService , {
6677 ...config [ 'atlas' ] ,
6778 } ) ;
68-
79+
6980 const aiService = new AtlasAiService ( {
7081 atlasService,
7182 apiURLPreset : 'admin-api' ,
7283 } ) ;
7384
74- return new AtlasAiProvider ( aiService ) ;
75- }
85+ return new AtlasAiProvider ( aiService , cliContext ) ;
86+ }
0 commit comments