@@ -2,12 +2,10 @@ import { aiCommand } from './decorators';
22import { AiProvider , EmptyAiProvider } from './providers/ai-provider' ;
33import { getAtlasAiProvider } from './providers/atlas/atlas-ai-provider' ;
44import { getDocsAiProvider } from './providers/docs/docs-ai-provider' ;
5- import {
6- getAiSdkProvider ,
7- models ,
8- } from './providers/generic/ai-sdk-provider' ;
5+ import { getAiSdkProvider , models } from './providers/generic/ai-sdk-provider' ;
96import { Config , ConfigSchema } from './config' ;
107import { CliContext , wrapAllFunctions , formatHelpCommands } from './helpers' ;
8+ import chalk from 'chalk' ;
119
1210class AI {
1311 private readonly replConfig : {
@@ -37,12 +35,13 @@ class AI {
3735 this . ai = this . getProvider ( event . value as ConfigSchema [ 'provider' ] ) ;
3836 break ;
3937 case 'model' :
40- if ( Object . keys ( models ) . includes ( event . value ) ) {
38+ if ( Object . keys ( models ) . includes ( event . value as string ) ) {
4139 this . ai = getAiSdkProvider (
4240 models [ this . config . get ( 'provider' ) as keyof typeof models ] (
43- event . value ,
41+ event . value as string ,
4442 ) ,
4543 this . cliContext ,
44+ this . config ,
4645 ) ;
4746 } else {
4847 throw new Error ( `Invalid model: ${ event . value } ` ) ;
@@ -53,7 +52,9 @@ class AI {
5352 }
5453 } ) ;
5554
56- this . ai = this . getProvider ( process . env . MONGOSH_AI_PROVIDER as ConfigSchema [ 'provider' ] | undefined ) ;
55+ this . ai = this . getProvider (
56+ process . env . MONGOSH_AI_PROVIDER as ConfigSchema [ 'provider' ] | undefined ,
57+ ) ;
5758 wrapAllFunctions ( this . cliContext , this ) ;
5859
5960 this . setupConfig ( ) ;
@@ -65,66 +66,85 @@ class AI {
6566 this . ai = this . getProvider ( this . config . get ( 'provider' ) ) ;
6667 }
6768
68- private getProvider ( provider : ConfigSchema [ 'provider' ] | undefined ) : AiProvider {
69+ private getProvider (
70+ provider : ConfigSchema [ 'provider' ] | undefined ,
71+ ) : AiProvider {
6972 switch ( provider ) {
7073 case 'docs' :
71- return getDocsAiProvider ( this . cliContext ) ;
74+ return getDocsAiProvider ( this . cliContext , this . config ) ;
7275 case 'atlas' :
73- return getAtlasAiProvider ( this . cliContext ) ;
76+ return getAtlasAiProvider ( this . cliContext , this . config ) ;
7477 case 'openai' :
7578 case 'mistral' :
7679 case 'ollama' :
7780 const model = this . config . get ( 'model' ) ;
7881 return getAiSdkProvider (
7982 models [ provider ] ( model === 'default' ? undefined : model ) ,
8083 this . cliContext ,
84+ this . config ,
8185 ) ;
8286 default :
83- return new EmptyAiProvider ( this . cliContext ) ;
87+ return new EmptyAiProvider ( this . cliContext , this . config ) ;
8488 }
8589 }
8690
87- @aiCommand
88- async command ( prompt : string ) {
89- await this . ai . command ( prompt ) ;
91+ @aiCommand ( )
92+ async shell ( prompt : string ) {
93+ await this . ai . shell ( prompt ) ;
94+ }
95+
96+ @aiCommand ( )
97+ async data ( prompt : string ) {
98+ await this . ai . data ( prompt ) ;
9099 }
91100
92- @aiCommand
101+ @aiCommand ( )
93102 async query ( prompt : string ) {
94103 await this . ai . query ( prompt ) ;
95104 }
96105
97- @aiCommand
106+ @aiCommand ( )
98107 async ask ( prompt : string ) {
99108 await this . ai . ask ( prompt ) ;
100109 }
101110
102- @aiCommand
111+ @aiCommand ( )
103112 async aggregate ( prompt : string ) {
104113 await this . ai . aggregate ( prompt ) ;
105114 }
106115
107- @aiCommand
108- async help ( ...args : string [ ] ) {
109- const commands = [
110- { cmd : 'ai.ask' , desc : 'ask questions' , example : 'ai.ask how do I run queries in mongosh?' } ,
111- { cmd : 'ai.command' , desc : 'generate any mongosh command' , example : 'ai.command create a new database' } ,
112- { cmd : 'ai.query' , desc : 'generate a MongoDB query' , example : 'ai.query find documents where name = "Ada"' } ,
113- { cmd : 'ai.aggregate' , desc : 'generate a MongoDB aggregation' , example : 'ai.aggregate find documents where name = "Ada"' } ,
114- { cmd : 'ai.config' , desc : 'configure the AI commands' , example : 'ai.config.set("provider", "ollama")' }
115- ] ;
116-
117- this . ai . respond (
118- formatHelpCommands (
119- commands ,
120- this . config . get ( 'provider' ) ,
121- this . config . get ( 'model' )
122- )
123- ) ;
116+ @aiCommand ( { requiresPrompt : false } )
117+ async help ( ) {
118+ this . ai . help ( {
119+ provider : this . config . get ( 'provider' ) ,
120+ model : this . config . get ( 'model' ) ,
121+ } ) ;
122+ }
123+
124+ @aiCommand ( )
125+ async clear ( ) {
126+ this . ai . clear ( ) ;
127+ }
128+
129+ @aiCommand ( )
130+ async collection ( name : string ) {
131+ await this . ai . collection ( name ) ;
132+ }
133+
134+ @aiCommand ( )
135+ async provider ( provider : string ) {
136+ this . config . set ( 'provider' , provider ) ;
137+ this . ai . respond ( `Switched to ${ chalk . blue ( provider ) } provider` ) ;
138+ }
139+
140+ @aiCommand ( )
141+ async model ( model : string ) {
142+ this . config . set ( 'model' , model ) ;
143+ this . ai . respond ( `Switched to ${ chalk . blue ( model ) } model` ) ;
124144 }
125145
126146 [ Symbol . for ( 'nodejs.util.inspect.custom' ) ] ( ) {
127- this . ai . help ( ) ;
147+ this . help ( ) ;
128148 return '' ;
129149 }
130150}
0 commit comments