Skip to content

Commit

Permalink
feat: copilot new api VSCODE-550 (#757)
Browse files Browse the repository at this point in the history
* feat: convert copilot POC to feature utilising latest API VSCODE-550

* refactor: use prompts in code

* fix: use uri path

* refactor: pass participant to playground controller
  • Loading branch information
alenakhineika authored Jul 23, 2024
1 parent f2af626 commit 89fbc31
Show file tree
Hide file tree
Showing 9 changed files with 494 additions and 1 deletion.
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"color": "#3D4F58",
"theme": "dark"
},
"enabledApiProposals": [
"chatVariableResolver"
],
"license": "SEE LICENSE IN LICENSE.txt",
"main": "./dist/extension.js",
"scripts": {
Expand Down Expand Up @@ -71,11 +74,25 @@
},
"activationEvents": [
"onView:mongoDB",
"onStartupFinished",
"onLanguage:json",
"onLanguage:javascript",
"onLanguage:plaintext"
],
"contributes": {
"chatParticipants": [
{
"id": "mongodb.participant",
"name": "MongoDB",
"description": "Ask anything about MongoDB, from writing queries to questions about your cluster.",
"commands": [
{
"name": "query",
"description": "Ask how to write MongoDB queries or pipelines. For example, you can ask: \"Show me all the documents where the address contains the word street\"."
}
]
}
],
"viewsContainers": {
"activitybar": [
{
Expand Down Expand Up @@ -142,6 +159,14 @@
}
],
"commands": [
{
"command": "mdb.runParticipantQuery",
"title": "Run Content Generated by the Chat Participant"
},
{
"command": "mdb.openParticipantQueryInPlayground",
"title": "Open Generated by the Chat Participant Content In Playground"
},
{
"command": "mdb.connect",
"title": "MongoDB: Connect"
Expand Down Expand Up @@ -688,6 +713,14 @@
}
],
"commandPalette": [
{
"command": "mdb.openParticipantQueryInPlayground",
"when": "false"
},
{
"command": "mdb.runParticipantQuery",
"when": "false"
},
{
"command": "mdb.disconnect",
"when": "mdb.connectedToMongoDB == true"
Expand Down
4 changes: 4 additions & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ enum EXTENSION_COMMANDS {
MDB_START_STREAM_PROCESSOR = 'mdb.startStreamProcessor',
MDB_STOP_STREAM_PROCESSOR = 'mdb.stopStreamProcessor',
MDB_DROP_STREAM_PROCESSOR = 'mdb.dropStreamProcessor',

// Chat participant.
OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND = 'mdb.openParticipantQueryInPlayground',
RUN_PARTICIPANT_QUERY = 'mdb.runParticipantQuery',
}

export default EXTENSION_COMMANDS;
26 changes: 26 additions & 0 deletions src/editors/playgroundController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { DatabaseTreeItem } from '../explorer';
import type ExportToLanguageCodeLensProvider from './exportToLanguageCodeLensProvider';
import formatError from '../utils/formatError';
import type { LanguageServerController } from '../language';
import playgroundBasicTextTemplate from '../templates/playgroundBasicTextTemplate';
import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate';
import playgroundCreateCollectionTemplate from '../templates/playgroundCreateCollectionTemplate';
import playgroundCloneDocumentTemplate from '../templates/playgroundCloneDocumentTemplate';
Expand Down Expand Up @@ -44,6 +45,7 @@ import {
isPlayground,
getPlaygroundExtensionForTelemetry,
} from '../utils/playground';
import type { ParticipantController } from '../participant/participant';

const log = createLogger('playground controller');

Expand Down Expand Up @@ -132,6 +134,7 @@ export default class PlaygroundController {
private _playgroundResultTextDocument?: vscode.TextDocument;
private _statusView: StatusView;
private _playgroundResultViewProvider: PlaygroundResultProvider;
private _participantController: ParticipantController;

private _codeToEvaluate = '';

Expand All @@ -144,6 +147,7 @@ export default class PlaygroundController {
activeConnectionCodeLensProvider,
exportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider,
participantController,
}: {
connectionController: ConnectionController;
languageServerController: LanguageServerController;
Expand All @@ -153,6 +157,7 @@ export default class PlaygroundController {
activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
playgroundSelectedCodeActionProvider: PlaygroundSelectedCodeActionProvider;
participantController: ParticipantController;
}) {
this._connectionController = connectionController;
this._activeTextEditor = vscode.window.activeTextEditor;
Expand All @@ -164,6 +169,7 @@ export default class PlaygroundController {
this._exportToLanguageCodeLensProvider = exportToLanguageCodeLensProvider;
this._playgroundSelectedCodeActionProvider =
playgroundSelectedCodeActionProvider;
this._participantController = participantController;

this._connectionController.addEventListener(
DataServiceEventTypes.ACTIVE_CONNECTION_CHANGED,
Expand Down Expand Up @@ -382,6 +388,21 @@ export default class PlaygroundController {
return this._createPlaygroundFileWithContent(content);
}

createPlaygroundFromParticipantQuery({
text,
}: {
text: string;
}): Promise<boolean> {
const useDefaultTemplate = !!vscode.workspace
.getConfiguration('mdb')
.get('useDefaultTemplateForPlayground');
const content = useDefaultTemplate
? playgroundBasicTextTemplate.replace('PLAYGROUND_CONTENT', text)
: text;
this._telemetryService.trackPlaygroundCreated('agent');
return this._createPlaygroundFileWithContent(content);
}

createPlaygroundForCloneDocument(
documentContents: string,
databaseName: string,
Expand Down Expand Up @@ -802,6 +823,11 @@ export default class PlaygroundController {
return { namespace, expression };
}

async evaluateParticipantQuery({ text }: { text: string }): Promise<boolean> {
this._codeToEvaluate = text;
return this._evaluatePlayground();
}

async _transpile(): Promise<boolean> {
const { selectedText, importStatements, driverSyntax, builders, language } =
this._exportToLanguageCodeLensProvider._exportToLanguageAddons;
Expand Down
42 changes: 42 additions & 0 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import WebviewController from './views/webviewController';
import { createIdFactory, generateId } from './utils/objectIdHelper';
import { ConnectionStorage } from './storage/connectionStorage';
import type StreamProcessorTreeItem from './explorer/streamProcessorTreeItem';
import { ParticipantController } from './participant/participant';

// This class is the top-level controller for our extension.
// Commands which the extensions handles are defined in the function `activate`.
Expand All @@ -63,6 +64,7 @@ export default class MDBExtensionController implements vscode.Disposable {
_activeConnectionCodeLensProvider: ActiveConnectionCodeLensProvider;
_editDocumentCodeLensProvider: EditDocumentCodeLensProvider;
_exportToLanguageCodeLensProvider: ExportToLanguageCodeLensProvider;
_participantController: ParticipantController;

constructor(
context: vscode.ExtensionContext,
Expand Down Expand Up @@ -105,6 +107,9 @@ export default class MDBExtensionController implements vscode.Disposable {
new PlaygroundSelectedCodeActionProvider();
this._playgroundDiagnosticsCodeActionProvider =
new PlaygroundDiagnosticsCodeActionProvider();
this._participantController = new ParticipantController({
connectionController: this._connectionController,
});
this._playgroundController = new PlaygroundController({
connectionController: this._connectionController,
languageServerController: this._languageServerController,
Expand All @@ -115,6 +120,7 @@ export default class MDBExtensionController implements vscode.Disposable {
exportToLanguageCodeLensProvider: this._exportToLanguageCodeLensProvider,
playgroundSelectedCodeActionProvider:
this._playgroundSelectedCodeActionProvider,
participantController: this._participantController,
});
this._editorsController = new EditorsController({
context,
Expand Down Expand Up @@ -265,6 +271,42 @@ export default class MDBExtensionController implements vscode.Disposable {

this.registerEditorCommands();
this.registerTreeViewCommands();

// ------ CHAT PARTICIPANT ------ //
this.registerParticipantCommand(
EXTENSION_COMMANDS.OPEN_PARTICIPANT_QUERY_IN_PLAYGROUND,
() => {
return this._playgroundController.createPlaygroundFromParticipantQuery({
text:
this._participantController._chatResult.metadata.queryContent || '',
});
}
);
this.registerParticipantCommand(
EXTENSION_COMMANDS.RUN_PARTICIPANT_QUERY,
() => {
return this._playgroundController.evaluateParticipantQuery({
text:
this._participantController._chatResult.metadata.queryContent || '',
});
}
);
};

registerParticipantCommand = (
command: string,
commandHandler: (...args: any[]) => Promise<boolean>
): void => {
const commandHandlerWithTelemetry = (args: any[]): Promise<boolean> => {
this._telemetryService.trackCommandRun(command);

return commandHandler(args);
};

this._context.subscriptions.push(
this._participantController.getParticipant(this._context),
vscode.commands.registerCommand(command, commandHandlerWithTelemetry)
);
};

registerCommand = (
Expand Down
Loading

0 comments on commit 89fbc31

Please sign in to comment.