Skip to content

Commit 97fa308

Browse files
committed
skunkworks - mongosh AI snippets
1 parent 7fbdd03 commit 97fa308

File tree

5 files changed

+25
-13
lines changed

5 files changed

+25
-13
lines changed

snippets/ai/decorators.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ export function aiCommand({
1414
if (requiresPrompt === false && args.length > 0) {
1515
throw new Error('This command does not accept any arguments');
1616
} else if (requiresPrompt && args.length === 0) {
17-
throw new Error('Please specify a prompt to run');
17+
throw new Error('Please specify arguments to run');
1818
}
1919
// Combine all arguments into a single string
20-
const combinedString = args.join(' ');
20+
const combinedString = args.join(' ').trim();
2121
// Call the original function with the combined string
2222
return value.call(this, combinedString);
2323
} as unknown as T; // Cast the wrapped function to match the original type

snippets/ai/index.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getAtlasAiProvider } from './providers/atlas/atlas-ai-provider';
44
import { getDocsAiProvider } from './providers/docs/docs-ai-provider';
55
import { getAiSdkProvider, models } from './providers/generic/ai-sdk-provider';
66
import { Config, ConfigSchema } from './config';
7-
import { CliContext, wrapAllFunctions, formatHelpCommands } from './helpers';
7+
import { CliContext, wrapAllFunctions } from './helpers';
88
import chalk from 'chalk';
99

1010
class AI {
@@ -29,22 +29,29 @@ class AI {
2929
this.config = new Config(this.replConfig);
3030

3131
// Set up provider change listener
32-
this.config.on('change', (event) => {
32+
this.config.on('change', async (event) => {
3333
switch (event.key) {
3434
case 'provider':
3535
this.ai = this.getProvider(event.value as ConfigSchema['provider']);
3636
break;
3737
case 'model':
38-
if (Object.keys(models).includes(event.value as string)) {
38+
if (!Object.keys(models).includes(this.config.get('provider') as keyof typeof models)) {
39+
if (event.value == 'default') {
40+
return;
41+
}
42+
await this.config.set('model', 'default');
43+
throw new Error(`${this.config.get('provider')} does not support custom models`);
44+
}
45+
try {
3946
this.ai = getAiSdkProvider(
4047
models[this.config.get('provider') as keyof typeof models](
4148
event.value as string,
4249
),
4350
this.cliContext,
4451
this.config,
4552
);
46-
} else {
47-
throw new Error(`Invalid model: ${event.value}`);
53+
} catch (error) {
54+
throw new Error(`Invalid model, please ensure your name is correct: ${error}`);
4855
}
4956
break;
5057
default:

snippets/ai/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@gagik/snippet-ai",
2+
"name": "@gagik.co/snippet-ai",
33
"snippetName": "ai",
44
"version": "0.0.1",
55
"description": "Provides a ai command suite for mongosh to ask for MongoDB query expressions in natural language.",

snippets/ai/providers/ai-provider.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,12 @@ export abstract class AiProvider {
7070

7171
/** @internal */
7272
setInput(text: string) {
73-
let actualText = text.replace('\n\n', '\n');
74-
process.stdin.unshift(actualText, 'utf-8');
73+
const trimmedText = text.trim();
74+
if (/[\n\r]/.test(trimmedText)) {
75+
// If the text includes a newline or carriage return, we should enter editor mode first
76+
process.stdin.unshift('.editor\n', 'utf-8');
77+
}
78+
process.stdin.unshift(trimmedText, 'utf-8');
7579
}
7680
/** @internal */
7781
respond(text: string) {
@@ -283,13 +287,14 @@ export abstract class AiProvider {
283287
},
284288
): string {
285289
if (expectedOutput === 'mongoshCommand') {
290+
// Often the models will return a command in a markdown code block, which we don't want
286291
return text
287292
.replace(/```\w*/g, '')
288293
.replace(/```/g, '')
289294
.trim();
290295
}
291296

292-
return text;
297+
return chalk.blue.bold('Answer: ') + text;
293298
}
294299

295300
abstract getResponse(

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class DocsAiProvider extends AiProvider {
2020
): Promise<string> {
2121
try {
2222
// Initialize conversation if not exists
23-
if (!this.conversation) {
23+
if (!this.docsConversation) {
2424
const conv = await this.aiService.createConversation({
2525
signal,
2626
});
@@ -37,7 +37,7 @@ export class DocsAiProvider extends AiProvider {
3737
let formattedResponse = response.content;
3838
if (expectedOutput === 'text') {
3939
// Format and display the response
40-
formattedResponse = chalk.blue.bold('Answer:\n') + response.content;
40+
formattedResponse = chalk.blue.bold('Answer: ') + response.content;
4141

4242
// Add references if they exist
4343
if (response.references && response.references.length > 0) {

0 commit comments

Comments
 (0)