Skip to content
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

feat(chat): add /schema command handler VSCODE-571 #820

Open
wants to merge 4 commits into
base: VSCODE-528-mongodb-copilot
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@
"name": "docs",
"isSticky": true,
"description": "Ask MongoDB-related questions and find answers in the official documentation."
},
{
"name": "schema",
"isSticky": true,
"description": "Analyze a collection's schema."
}
]
}
Expand Down Expand Up @@ -177,6 +182,10 @@
"command": "mdb.selectCollectionWithParticipant",
"title": "MongoDB: Select Collection with Participant"
},
{
"command": "mdb.participantViewRawSchemaOutput",
"title": "MongoDB: View Raw Schema JSON Output"
},
{
"command": "mdb.connectWithParticipant",
"title": "MongoDB: Change Active Connection with Participant"
Expand Down Expand Up @@ -747,6 +756,10 @@
"command": "mdb.selectCollectionWithParticipant",
"when": "false"
},
{
"command": "mdb.participantViewRawSchemaOutput",
"when": "false"
},
{
"command": "mdb.connectWithParticipant",
"when": "false"
Expand Down
1 change: 1 addition & 0 deletions src/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum EXTENSION_COMMANDS {
CONNECT_WITH_PARTICIPANT = 'mdb.connectWithParticipant',
SELECT_DATABASE_WITH_PARTICIPANT = 'mdb.selectDatabaseWithParticipant',
SELECT_COLLECTION_WITH_PARTICIPANT = 'mdb.selectCollectionWithParticipant',
PARTICIPANT_OPEN_RAW_SCHEMA_OUTPUT = 'mdb.participantViewRawSchemaOutput',
}

export default EXTENSION_COMMANDS;
37 changes: 21 additions & 16 deletions src/mdbExtensionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import { ConnectionStorage } from './storage/connectionStorage';
import type StreamProcessorTreeItem from './explorer/streamProcessorTreeItem';
import type { RunParticipantQueryCommandArgs } from './participant/participant';
import ParticipantController from './participant/participant';
import type { OpenSchemaCommandArgs } from './participant/prompts/schema';

// This class is the top-level controller for our extension.
// Commands which the extensions handles are defined in the function `activate`.
Expand Down Expand Up @@ -310,30 +311,34 @@ export default class MDBExtensionController implements vscode.Disposable {
);
this.registerCommand(
EXTENSION_COMMANDS.CONNECT_WITH_PARTICIPANT,
(id?: string) =>
this._participantController.connectWithParticipant(
id ? decodeURIComponent(id) : id
)
(data: any) => {
return this._participantController.connectWithParticipant(data);
}
);
this.registerCommand(
EXTENSION_COMMANDS.SELECT_DATABASE_WITH_PARTICIPANT,
(_data: string) => {
const data = JSON.parse(decodeURIComponent(_data));
return this._participantController.selectDatabaseWithParticipant({
chatId: data.chatId,
databaseName: data.databaseName,
});
(data: any) => {
return this._participantController.selectDatabaseWithParticipant(data);
}
);
this.registerCommand(
EXTENSION_COMMANDS.SELECT_COLLECTION_WITH_PARTICIPANT,
(_data: string) => {
const data = JSON.parse(decodeURIComponent(_data));
return this._participantController.selectCollectionWithParticipant({
chatId: data.chatId,
databaseName: data.databaseName,
collectionName: data.collectionName,
(data: any) => {
return this._participantController.selectCollectionWithParticipant(
data
);
}
);
this.registerCommand(
EXTENSION_COMMANDS.PARTICIPANT_OPEN_RAW_SCHEMA_OUTPUT,
async ({ schema }: OpenSchemaCommandArgs) => {
const document = await vscode.workspace.openTextDocument({
language: 'json',
content: schema,
});
await vscode.window.showTextDocument(document, { preview: true });

return !!document;
}
);
};
Expand Down
7 changes: 6 additions & 1 deletion src/participant/chatMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class ChatMetadataStore {
return this._chats[chatId];
}

// Exposed for stubbing in tests.
static createNewChatId(): string {
return uuidv4();
}

static getChatIdFromHistoryOrNewChatId(
history: ReadonlyArray<vscode.ChatRequestTurn | vscode.ChatResponseTurn>
): string {
Expand All @@ -32,6 +37,6 @@ export class ChatMetadataStore {
}
}

return uuidv4();
return ChatMetadataStore.createNewChatId();
}
}
6 changes: 6 additions & 0 deletions src/participant/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ export function docsRequestChatResult(chatId: string): ChatResult {
},
};
}

export function schemaRequestChatResult(
history: ReadonlyArray<vscode.ChatRequestTurn | vscode.ChatResponseTurn>
): ChatResult {
return createChatResult('schema', history);
}
20 changes: 6 additions & 14 deletions src/participant/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,14 @@ export function createMarkdownLink({
name,
}: {
commandId: string;
data?:
| {
[field: string]: any;
}
| string;
// TODO: Create types for this data so we can also then use them on the extension
// controller when we parse the result.
Anemy marked this conversation as resolved.
Show resolved Hide resolved
data: {
[field: string]: any;
};
name: string;
}): vscode.MarkdownString {
const encodedData = data
? encodeURIComponent(
`["${
typeof data === 'string'
? data
: encodeURIComponent(JSON.stringify(data))
}"]`
)
: undefined;
const encodedData = encodeURIComponent(JSON.stringify(data));
const commandQueryString = data ? `?${encodedData}` : '';
const link = new vscode.MarkdownString(
`- <a href="command:${commandId}${commandQueryString}">${name}</a>\n`
Expand Down
Loading
Loading