-
Notifications
You must be signed in to change notification settings - Fork 90
feat(participant): export to a playground VSCODE-574 #832
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
7a456b4
ebe0b8d
f23eb4a
da0cd94
e2692de
c897181
ba5fdb6
94d88c0
55d634e
49f330a
ea05667
5d873df
b6c30eb
1eb6bf5
52d66b5
05ea3c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ import EXTENSION_COMMANDS from '../commands'; | |
| import type { StorageController } from '../storage'; | ||
| import { StorageVariables } from '../storage'; | ||
| import { GenericPrompt, isPromptEmpty } from './prompts/generic'; | ||
| import { ExportToPlaygroundPrompt } from './prompts/exportToPlayground'; | ||
| import type { ChatResult } from './constants'; | ||
| import { | ||
| askToConnectChatResult, | ||
|
|
@@ -42,6 +43,7 @@ import { | |
| } from '../telemetry/telemetryService'; | ||
| import { DocsChatbotAIService } from './docsChatbotAIService'; | ||
| import type TelemetryService from '../telemetry/telemetryService'; | ||
| import formatError from '../utils/formatError'; | ||
|
|
||
| const log = createLogger('participant'); | ||
|
|
||
|
|
@@ -132,7 +134,7 @@ export default class ParticipantController { | |
| return this._participant; | ||
| } | ||
|
|
||
| handleError(err: any, command: string): never { | ||
| handleError(err: any, command: string): void { | ||
alenakhineika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let errorCode: string | undefined; | ||
| let errorName: ParticipantErrorTypes; | ||
| // Making the chat request might fail because | ||
|
|
@@ -168,9 +170,6 @@ export default class ParticipantController { | |
| error_name: errorName, | ||
| } | ||
| ); | ||
|
|
||
| // Re-throw other errors so they show up in the UI. | ||
| throw err; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -1201,6 +1200,79 @@ export default class ParticipantController { | |
| }); | ||
| } | ||
|
|
||
| async exportCodeToPlayground(): Promise<boolean> { | ||
| const activeTextEditor = vscode.window.activeTextEditor; | ||
| if (!activeTextEditor) { | ||
| await vscode.window.showErrorMessage('Active editor not found.'); | ||
| return false; | ||
| } | ||
|
|
||
| const sortedSelections = Array.from(activeTextEditor.selections).sort( | ||
| (a, b) => a.start.compareTo(b.start) | ||
| ); | ||
| const selectedText = sortedSelections | ||
| .map((selection) => activeTextEditor.document.getText(selection)) | ||
| .join('\n'); | ||
| const code = | ||
| selectedText || activeTextEditor.document.getText().trim() || ''; | ||
| try { | ||
| const progressResult = await vscode.window.withProgress( | ||
| { | ||
| location: vscode.ProgressLocation.Notification, | ||
| title: 'Exporting code to a playground...', | ||
| cancellable: true, | ||
| }, | ||
| async (progress, token): Promise<string | undefined> => { | ||
| token.onCancellationRequested(async () => { | ||
| await vscode.window.showInformationMessage( | ||
| 'The running export to a playground operation was canceled.' | ||
| ); | ||
| }); | ||
|
|
||
| const messages = ExportToPlaygroundPrompt.buildMessages(code); | ||
| return Promise.race([ | ||
| this.getChatResponseContent({ | ||
| messages, | ||
| token, | ||
| }), | ||
| new Promise<undefined>((resolve) => | ||
| token.onCancellationRequested(() => { | ||
| resolve(undefined); | ||
| }) | ||
|
Comment on lines
+1492
to
+1497
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We're already passing the cancellation token to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't seem that the token passed to the |
||
| ), | ||
| ]); | ||
| } | ||
| ); | ||
|
|
||
| if (progressResult?.includes("Sorry, I can't assist with that.")) { | ||
| void vscode.window.showErrorMessage("Sorry, I can't assist with that."); | ||
| return Promise.resolve(false); | ||
Anemy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if (progressResult) { | ||
| const runnableContent = getRunnableContentFromString(progressResult); | ||
| if (runnableContent) { | ||
| await vscode.commands.executeCommand( | ||
| EXTENSION_COMMANDS.OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND, | ||
| { | ||
| runnableContent, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| return Promise.resolve(true); | ||
alenakhineika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } catch (error) { | ||
| this.handleError(error, 'exportToPlayground'); | ||
| await vscode.window.showErrorMessage( | ||
| `An error occurred exporting to a playground: ${ | ||
| formatError(error).message | ||
| }` | ||
| ); | ||
| return Promise.resolve(false); | ||
alenakhineika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
|
|
||
| async chatHandler( | ||
| ...args: [ | ||
| vscode.ChatRequest, | ||
|
|
@@ -1249,6 +1321,8 @@ Please see our [FAQ](https://www.mongodb.com/docs/generative-ai-faq/) for more i | |
| } | ||
| } catch (e) { | ||
| this.handleError(e, request.command || 'generic'); | ||
| // Re-throw other errors so they show up in the UI. | ||
| throw e; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import * as vscode from 'vscode'; | ||
|
|
||
| export class ExportToPlaygroundPrompt { | ||
| static getAssistantPrompt(): vscode.LanguageModelChatMessage { | ||
| const prompt = `You are a MongoDB expert. | ||
| Your task is to help the user build MongoDB queries and aggregation pipelines that perform their task. | ||
| You convert user's code written in any programming language to the MongoDB Shell syntax. | ||
| Take a user prompt as an input string and translate it to the MongoDB Shell language. | ||
| Keep your response concise. | ||
| You should suggest queries that are performant and correct. | ||
alenakhineika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Respond with markdown, suggest code in a Markdown code block that begins with \`\`\`javascript and ends with \`\`\`. | ||
Anemy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Respond in MongoDB shell syntax using the \`\`\`javascript code block syntax.`; | ||
|
|
||
| // eslint-disable-next-line new-cap | ||
| return vscode.LanguageModelChatMessage.Assistant(prompt); | ||
| } | ||
|
|
||
| static getUserPrompt(prompt: string): vscode.LanguageModelChatMessage { | ||
| // eslint-disable-next-line new-cap | ||
| return vscode.LanguageModelChatMessage.User(prompt); | ||
Anemy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| static buildMessages(prompt: string): vscode.LanguageModelChatMessage[] { | ||
| const messages = [ | ||
| ExportToPlaygroundPrompt.getAssistantPrompt(), | ||
| ExportToPlaygroundPrompt.getUserPrompt(prompt), | ||
| ]; | ||
|
|
||
| return messages; | ||
alenakhineika marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.