Skip to content

Commit 9d30a63

Browse files
committed
chore: cleanup commands
1 parent ef317d6 commit 9d30a63

File tree

5 files changed

+36
-56
lines changed

5 files changed

+36
-56
lines changed

snippets/ai/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# AI Command Snippets
1+
# AI Snippets for mongosh
22

33
> [!CAUTION]
44
> This is an experimental, early-stage snippet that is not meant for production use.
@@ -9,11 +9,9 @@ This snippet adds a suite of commands accessible with the `ai` command. This inc
99
|---------|-------------|---------|
1010
| `ai.ask` | Ask questions about MongoDB | `ai.ask how do I run queries in mongosh?` |
1111
| `ai.data` | Generate data-related mongosh commands | `ai.data insert some sample user info` |
12-
| `ai.query` | Generate a MongoDB query | `ai.query find documents where name = "Ada"` |
13-
| `ai.aggregate` | Generate a MongoDB aggregation | `ai.aggregate find documents where name = "Ada"` |
14-
| `ai.collection` | Set the active collection | `ai.collection("users")` |
15-
| `ai.shell` | Generate general mongosh commands | `ai.shell get sharding info` |
16-
| `ai.general` | Ask general questions to your model | `ai.general what is the meaning of life?`
12+
| `ai.query` | Generate a MongoDB query or aggregation | `ai.query find documents where name = "Ada"` |
13+
| `ai.collection` | Set the active collection | `ai.collection users` |
14+
| `ai.command` | Generate general mongosh commands _alias:_ `ai.cmd` | `ai.command get sharding info` |
1715
| `ai.config` | Configure the AI commands | `ai.config.set("provider", "ollama")` |
1816

1917
This currently supports 5 different AI providers: `docs, openai | mistral | atlas | ollama` and any model they support. For cloud providers, you can specify the API key with `MONGOSH_AI_API_KEY`.

snippets/ai/src/ai.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ module.exports = async (globalThis: CliContext) => {
3737
const provider = new AiProvider(
3838
cliContext,
3939
config,
40-
models[config.get('provider') as keyof typeof models](
40+
models[config.get('provider')](
4141
config.get('model') === 'default' ? undefined : config.get('model'),
4242
),
4343
);
@@ -52,18 +52,13 @@ module.exports = async (globalThis: CliContext) => {
5252
}
5353

5454
@aiCommand()
55-
async shell(prompt: string) {
55+
async command(prompt: string) {
5656
await this.ai.shell(prompt);
5757
}
5858

5959
@aiCommand()
60-
async query(prompt: string) {
61-
await this.ai.query(prompt);
62-
}
63-
64-
@aiCommand()
65-
async aggregate(prompt: string) {
66-
await this.ai.aggregate(prompt);
60+
async cmd(prompt: string) {
61+
await this.command(prompt);
6762
}
6863

6964
@aiCommand()
@@ -84,24 +79,19 @@ module.exports = async (globalThis: CliContext) => {
8479
example: 'ai.ask how do I run queries in mongosh?',
8580
},
8681
{
87-
cmd: 'ai.query',
88-
desc: 'generate a MongoDB query',
89-
example: 'ai.query find documents where name = "Ada"',
90-
},
91-
{
92-
cmd: 'ai.aggregate',
93-
desc: 'generate a MongoDB aggregation',
94-
example: 'ai.aggregate find documents where name = "Ada"',
82+
cmd: 'ai.find',
83+
desc: 'generate a MongoDB query or aggregation',
84+
example: 'ai.find documents where name = "Ada"',
9585
},
9686
{
9787
cmd: 'ai.collection',
9888
desc: 'set the active collection',
99-
example: 'ai.collection("users")',
89+
example: 'ai.collection users',
10090
},
10191
{
102-
cmd: 'ai.shell',
103-
desc: 'generate administrative mongosh commands',
104-
example: 'ai.shell insert a new sample document',
92+
cmd: 'ai.command',
93+
desc: `Generate mongosh commands`,
94+
example: 'ai.command insert a new sample document | alias: ai.cmd',
10595
},
10696
{
10797
cmd: 'ai.config',

snippets/ai/src/helpers.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import chalk from "chalk";
1+
import chalk from 'chalk';
22

33
export function output(text: string) {
44
process.stdout.write(`${text}`);
@@ -92,12 +92,17 @@ export function wrapFunction(
9292
] = wrapperFn;
9393
}
9494

95-
export function wrapAllFunctions(cliContext: CliContext, currentInstance: unknown) {
95+
export function wrapAllFunctions(
96+
cliContext: CliContext,
97+
currentInstance: unknown,
98+
) {
9699
const instanceState = cliContext.db._mongo._instanceState;
97100
const instance = currentInstance as {
98-
[key: string]: (...args: unknown[]) => Record<string, unknown> | {
99-
isDirectShellCommand: boolean;
100-
}
101+
[key: string]: (...args: unknown[]) =>
102+
| Record<string, unknown>
103+
| {
104+
isDirectShellCommand: boolean;
105+
};
101106
};
102107

103108
const methods = Object.getOwnPropertyNames(
@@ -114,12 +119,14 @@ export function wrapAllFunctions(cliContext: CliContext, currentInstance: unknow
114119
);
115120
});
116121

117-
118-
119122
// for all methods, wrap them with the wrapFunction method
120123
for (const methodName of methods) {
121124
const method = instance[methodName];
122-
if (typeof method === 'function' && (method as unknown as { isDirectShellCommand: boolean }).isDirectShellCommand) {
125+
if (
126+
typeof method === 'function' &&
127+
(method as unknown as { isDirectShellCommand: boolean })
128+
.isDirectShellCommand
129+
) {
123130
wrapFunction(cliContext, instance, methodName, method.bind(instance));
124131
}
125132
}
@@ -146,13 +153,14 @@ export function formatHelpCommands(
146153
const formattedCommands = commands
147154
.map((c) => {
148155
const padding = ' '.repeat(maxCmdLength - c.cmd.length);
149-
const base = ` ${chalk.yellow(c.cmd)}${padding} ${chalk.white(c.desc)}`;
156+
const base = ` ${chalk.cyan(c.cmd)}${padding} ${chalk.white(c.desc)}`;
150157
return c.example ? `${base} ${chalk.gray(`| ${c.example}`)}` : base;
151158
})
152159
.join('\n');
153160

154-
return `${chalk.blue.bold('AI command suite for mongosh')}
155-
${chalk.gray(`Collection: ${chalk.white.bold(collection ?? 'not set')}. Set it with ai.collection("collection_name")`)}\n
161+
return `${chalk.cyan.bold('mongosh AI snippet')}
162+
${chalk.yellow.bold('Note: This snippet is experimental and not meant for production use.')}
163+
${chalk.gray(`Collection: ${chalk.white.bold(collection ?? 'not set')}. Set it with ${chalk.white.bold('ai.collection("collection_name")')}`)}\n
156164
${formattedCommands}\n
157165
${chalk.gray(`Using ${chalk.white.bold(provider)} as provider and its ${chalk.white.bold(model)} model`)}
158166
`.trim();

snippets/ai/src/providers/ai-provider.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,6 @@ export class AiProvider {
229229
});
230230
}
231231

232-
async query(prompt: string): Promise<void> {
233-
const signal = AbortSignal.timeout(30_000);
234-
await this.ensureCollectionName(prompt);
235-
236-
await this.processResponse(prompt, {
237-
systemPrompt: await this.getSystemPrompt(
238-
"You generate the exact valid mongosh command that matches the user's request.",
239-
{ includeSampleDocs: true },
240-
),
241-
signal,
242-
expectedOutput: 'command',
243-
});
244-
}
245-
246232
private formatResponse({
247233
response,
248234
expectedOutput,

snippets/ai/src/providers/models.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ export const models = {
88
baseURL: 'https://knowledge.mongodb.com/api/v1',
99
apiKey: '',
1010
headers: {
11-
// TODO: Change to actual origin
1211
'X-Request-Origin': 'mongodb-compass',
13-
origin: 'mongodb-compass',
1412
'user-agent':
1513
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) MongoDBCompass/1.47.0 Chrome/138.0.7204.251 Electron/37.6.0 Safari/537.36',
1614
},
@@ -31,12 +29,12 @@ export const models = {
3129
},
3230
mistral(model = 'mistral-small-latest') {
3331
return createMistral({
34-
apiKey: process.env.MONGOSH_AI_API_KEY,
32+
apiKey: process.env.MONGOSH_AI_MISTRAL_API_KEY,
3533
}).languageModel(model);
3634
},
3735
openai(model = 'gpt-4.1-mini') {
3836
return createOpenAI({
39-
apiKey: process.env.MONGOSH_AI_API_KEY,
37+
apiKey: process.env.MONGOSH_AI_OPENAI_API_KEY,
4038
}).languageModel(model);
4139
},
4240
};

0 commit comments

Comments
 (0)