diff --git a/src/commands/index.ts b/src/commands/index.ts index 9164d0a28..c41535f70 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -79,6 +79,7 @@ enum EXTENSION_COMMANDS { // Chat participant. OPEN_PARTICIPANT_CODE_IN_PLAYGROUND = 'mdb.openParticipantCodeInPlayground', + SEND_PARTICIPANT_MESSAGE = 'mdb.sendParticipantMessage', RUN_PARTICIPANT_CODE = 'mdb.runParticipantCode', CONNECT_WITH_PARTICIPANT = 'mdb.connectWithParticipant', SELECT_DATABASE_WITH_PARTICIPANT = 'mdb.selectDatabaseWithParticipant', diff --git a/src/mdbExtensionController.ts b/src/mdbExtensionController.ts index 8077e60d2..765434cce 100644 --- a/src/mdbExtensionController.ts +++ b/src/mdbExtensionController.ts @@ -46,6 +46,7 @@ import { createIdFactory, generateId } from './utils/objectIdHelper'; import { ConnectionStorage } from './storage/connectionStorage'; import type StreamProcessorTreeItem from './explorer/streamProcessorTreeItem'; import type { + SendParticipantMessageCommandArgs, ParticipantCommand, RunParticipantCodeCommandArgs, } from './participant/participant'; @@ -324,6 +325,20 @@ export default class MDBExtensionController implements vscode.Disposable { }); } ); + this.registerParticipantCommand( + EXTENSION_COMMANDS.SEND_PARTICIPANT_MESSAGE, + async ({ + message, + isPartialQuery, + isNewChat, + }: SendParticipantMessageCommandArgs) => { + await this._participantController.writeChatMessageAsUser(message, { + isPartialQuery, + isNewChat, + }); + return true; + } + ); this.registerParticipantCommand( EXTENSION_COMMANDS.RUN_PARTICIPANT_CODE, ({ runnableContent }: RunParticipantCodeCommandArgs) => { diff --git a/src/participant/participant.ts b/src/participant/participant.ts index 303b19554..d5ebdd036 100644 --- a/src/participant/participant.ts +++ b/src/participant/participant.ts @@ -64,6 +64,12 @@ interface NamespaceQuickPicks { data: string; } +export type SendParticipantMessageCommandArgs = { + message: string; + isPartialQuery: boolean; + isNewChat: boolean; +}; + export type RunParticipantCodeCommandArgs = { runnableContent: string; }; @@ -131,9 +137,25 @@ export default class ParticipantController { * in the chat. To work around this, we can write a message as the user, which will * trigger the chat handler and give us access to the model. */ - writeChatMessageAsUser(message: string): Thenable { + async writeChatMessageAsUser( + message: string, + options: { + isNewChat?: boolean; + isPartialQuery?: boolean; + } = {} + ): Promise { + const { isNewChat = false, isPartialQuery = false } = options; + + if (isNewChat) { + await vscode.commands.executeCommand('workbench.action.chat.newChat'); + await vscode.commands.executeCommand( + 'workbench.action.chat.clearHistory' + ); + } + return vscode.commands.executeCommand('workbench.action.chat.open', { query: `@MongoDB ${message}`, + isPartialQuery, }); }