Skip to content

Commit 21f9fd9

Browse files
committed
fix: add general command
1 parent 97fa308 commit 21f9fd9

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

snippets/ai/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AI {
4545
try {
4646
this.ai = getAiSdkProvider(
4747
models[this.config.get('provider') as keyof typeof models](
48-
event.value as string,
48+
event.value == 'default' ? undefined : event.value as string,
4949
),
5050
this.cliContext,
5151
this.config,
@@ -100,6 +100,11 @@ class AI {
100100
await this.ai.shell(prompt);
101101
}
102102

103+
@aiCommand()
104+
async general(prompt: string) {
105+
await this.ai.general(prompt);
106+
}
107+
103108
@aiCommand()
104109
async data(prompt: string) {
105110
await this.ai.data(prompt);

snippets/ai/providers/ai-provider.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export abstract class AiProvider {
8686
const commands = [
8787
{
8888
cmd: 'ai.ask',
89-
desc: 'ask questions',
89+
desc: 'ask MongoDB questions',
9090
example: 'ai.ask how do I run queries in mongosh?',
9191
},
9292
{
@@ -111,9 +111,14 @@ export abstract class AiProvider {
111111
},
112112
{
113113
cmd: 'ai.shell',
114-
desc: 'generate general mongosh commands',
114+
desc: 'generate administrative mongosh commands',
115115
example: 'ai.shell get sharding info',
116116
},
117+
{
118+
cmd: 'ai.general',
119+
desc: 'use your model for general questions',
120+
example: 'ai.general what is the meaning of life?',
121+
},
117122
{
118123
cmd: 'ai.config',
119124
desc: 'configure the AI commands',
@@ -209,7 +214,7 @@ export abstract class AiProvider {
209214

210215
this.thinking.stop();
211216
this.setInput(
212-
this.cleanUpResponse(response, { expectedOutput: 'mongoshCommand' }),
217+
this.formatResponse(response, { expectedOutput: 'mongoshCommand' }),
213218
);
214219
}
215220

@@ -230,10 +235,24 @@ export abstract class AiProvider {
230235

231236
this.thinking.stop();
232237
this.setInput(
233-
this.cleanUpResponse(response, { expectedOutput: 'mongoshCommand' }),
238+
this.formatResponse(response, { expectedOutput: 'mongoshCommand' }),
234239
);
235240
}
236241

242+
async general(prompt: string): Promise<void> {
243+
const signal = AbortSignal.timeout(30_000);
244+
this.thinking.start(signal);
245+
246+
const response = await this.getResponse(prompt, {
247+
systemPrompt: 'Give brief answers without any formatting or markdown.',
248+
signal,
249+
expectedOutput: 'text',
250+
});
251+
252+
this.thinking.stop();
253+
this.respond(this.formatResponse(response, { expectedOutput: 'text' }));
254+
}
255+
237256
async data(prompt: string): Promise<void> {
238257
await this.ensureCollectionName(prompt);
239258
const signal = AbortSignal.timeout(30_000);
@@ -252,7 +271,7 @@ export abstract class AiProvider {
252271

253272
this.thinking.stop();
254273
this.setInput(
255-
this.cleanUpResponse(response, { expectedOutput: 'mongoshCommand' }),
274+
this.formatResponse(response, { expectedOutput: 'mongoshCommand' }),
256275
);
257276
}
258277

@@ -274,11 +293,11 @@ export abstract class AiProvider {
274293

275294
this.thinking.stop();
276295
this.setInput(
277-
this.cleanUpResponse(response, { expectedOutput: 'mongoshCommand' }),
296+
this.formatResponse(response, { expectedOutput: 'mongoshCommand' }),
278297
);
279298
}
280299

281-
cleanUpResponse(
300+
formatResponse(
282301
text: string,
283302
{
284303
expectedOutput,
@@ -322,7 +341,7 @@ export abstract class AiProvider {
322341
});
323342

324343
this.thinking.stop();
325-
this.respond(this.cleanUpResponse(response, { expectedOutput: 'text' }));
344+
this.respond(this.formatResponse(response, { expectedOutput: 'text' }));
326345
}
327346

328347
public clear() {

0 commit comments

Comments
 (0)