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

chore(participant): add progress indicators for asynchronous requests in chat VSCODE-613 #827

Merged
Merged
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
173 changes: 96 additions & 77 deletions src/participant/participant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,24 +449,30 @@ export default class ParticipantController {
) as Promise<boolean>;
}

async getDatabasesTree({
async renderDatabasesTree({
command,
context,
stream,
}: {
command: ParticipantCommand;
context: vscode.ChatContext;
}): Promise<vscode.MarkdownString[]> {
stream: vscode.ChatResponseStream;
}): Promise<void> {
const dataService = this._connectionController.getActiveDataService();
if (!dataService) {
return [];
return;
}

stream.push(
new vscode.ChatResponseProgressPart('Fetching database names...')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So does the progress bar disappear as soon as we call stream.markdown or a similar output method?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

);

try {
const databases = await dataService.listDatabases({
nameOnly: true,
});
return [
...databases.slice(0, MAX_MARKDOWN_LIST_LENGTH).map((db) =>
databases.slice(0, MAX_MARKDOWN_LIST_LENGTH).forEach((db) =>
stream.markdown(
createMarkdownLink({
commandId: EXTENSION_COMMANDS.SELECT_DATABASE_WITH_PARTICIPANT,
data: {
Expand All @@ -478,46 +484,54 @@ export default class ParticipantController {
},
name: db.name,
})
),
...(databases.length > MAX_MARKDOWN_LIST_LENGTH
? [
createMarkdownLink({
data: {
command,
chatId: ChatMetadataStore.getChatIdFromHistoryOrNewChatId(
context.history
),
},
commandId: EXTENSION_COMMANDS.SELECT_DATABASE_WITH_PARTICIPANT,
name: 'Show more',
}),
]
: []),
];
)
);
if (databases.length > MAX_MARKDOWN_LIST_LENGTH) {
stream.markdown(
createMarkdownLink({
data: {
command,
chatId: ChatMetadataStore.getChatIdFromHistoryOrNewChatId(
context.history
),
},
commandId: EXTENSION_COMMANDS.SELECT_DATABASE_WITH_PARTICIPANT,
name: 'Show more',
})
);
}
} catch (error) {
log.error('Unable to fetch databases:', error);

// Users can always do this manually when asked to provide a database name.
return [];
return;
}
}

async getCollectionTree({
async renderCollectionsTree({
command,
context,
databaseName,
stream,
}: {
command: ParticipantCommand;
databaseName: string;
context: vscode.ChatContext;
}): Promise<vscode.MarkdownString[]> {
stream: vscode.ChatResponseStream;
}): Promise<void> {
const dataService = this._connectionController.getActiveDataService();
if (!dataService) {
return [];
return;
}

stream.push(
new vscode.ChatResponseProgressPart('Fetching collection names...')
);

try {
const collections = await dataService.listCollections(databaseName);
return [
...collections.slice(0, MAX_MARKDOWN_LIST_LENGTH).map((coll) =>
collections.slice(0, MAX_MARKDOWN_LIST_LENGTH).forEach((coll) =>
stream.markdown(
createMarkdownLink({
commandId: EXTENSION_COMMANDS.SELECT_COLLECTION_WITH_PARTICIPANT,
data: {
Expand All @@ -530,27 +544,28 @@ export default class ParticipantController {
},
name: coll.name,
})
),
...(collections.length > MAX_MARKDOWN_LIST_LENGTH
? [
createMarkdownLink({
commandId:
EXTENSION_COMMANDS.SELECT_COLLECTION_WITH_PARTICIPANT,
data: {
command,
chatId: ChatMetadataStore.getChatIdFromHistoryOrNewChatId(
context.history
),
databaseName,
},
name: 'Show more',
}),
]
: []),
];
)
);
if (collections.length > MAX_MARKDOWN_LIST_LENGTH) {
stream.markdown(
createMarkdownLink({
commandId: EXTENSION_COMMANDS.SELECT_COLLECTION_WITH_PARTICIPANT,
data: {
command,
chatId: ChatMetadataStore.getChatIdFromHistoryOrNewChatId(
context.history
),
databaseName,
},
name: 'Show more',
})
);
}
} catch (error) {
log.error('Unable to fetch collections:', error);

// Users can always do this manually when asked to provide a collection name.
return [];
return;
}
}

Expand Down Expand Up @@ -614,30 +629,26 @@ export default class ParticipantController {
// we retrieve the available namespaces from the current connection.
// Users can then select a value by clicking on an item in the list.
if (!databaseName) {
const tree = await this.getDatabasesTree({
command,
context,
});
stream.markdown(
`What is the name of the database you would like${
command === '/query' ? ' this query' : ''
} to run against?\n\n`
);
for (const item of tree) {
stream.markdown(item);
}
} else if (!collectionName) {
const tree = await this.getCollectionTree({
await this.renderDatabasesTree({
command,
databaseName,
context,
stream,
});
} else if (!collectionName) {
stream.markdown(
`Which collection would you like to use within ${databaseName}?\n\n`
);
for (const item of tree) {
stream.markdown(item);
}
await this.renderCollectionsTree({
command,
databaseName,
context,
stream,
});
}

return namespaceRequestChatResult({
Expand Down Expand Up @@ -675,12 +686,14 @@ export default class ParticipantController {
collectionName,
amountOfDocumentsToSample = NUM_DOCUMENTS_TO_SAMPLE,
schemaFormat = 'simplified',
stream,
}: {
abortSignal;
databaseName: string;
collectionName: string;
amountOfDocumentsToSample?: number;
schemaFormat?: 'simplified' | 'full';
stream: vscode.ChatResponseStream;
}): Promise<{
schema?: string;
sampleDocuments?: Document[];
Expand All @@ -693,6 +706,12 @@ export default class ParticipantController {
};
}

stream.push(
new vscode.ChatResponseProgressPart(
'Fetching documents and analyzing schema...'
)
);

try {
const sampleDocuments = await dataService.sample(
`${databaseName}.${collectionName}`,
Expand Down Expand Up @@ -735,7 +754,7 @@ export default class ParticipantController {
amountOfDocumentsSampled: sampleDocuments.length,
};
} catch (err: any) {
log.error('Unable to fetch schema and sample documents', err);
log.error('Unable to fetch schema and sample documents:', err);
throw err;
}
}
Expand All @@ -759,35 +778,32 @@ export default class ParticipantController {

// When the last message was asking for a database or collection name,
// we re-ask the question.
let tree: vscode.MarkdownString[];
const databaseName = lastMessage.metadata.databaseName;
if (databaseName) {
stream.markdown(
vscode.l10n.t(
'Please select a collection by either clicking on an item in the list or typing the name manually in the chat.'
)
);
tree = await this.getCollectionTree({
await this.renderCollectionsTree({
command,
databaseName,
context,
stream,
});
} else {
stream.markdown(
vscode.l10n.t(
'Please select a database by either clicking on an item in the list or typing the name manually in the chat.'
)
);
tree = await this.getDatabasesTree({
await this.renderDatabasesTree({
command,
context,
stream,
});
}

for (const item of tree) {
stream.markdown(item);
}

return namespaceRequestChatResult({
databaseName,
collectionName: undefined,
Expand Down Expand Up @@ -841,12 +857,6 @@ export default class ParticipantController {
abortController.abort();
});

stream.push(
new vscode.ChatResponseProgressPart(
'Fetching documents and analyzing schema...'
)
);

let sampleDocuments: Document[] | undefined;
let amountOfDocumentsSampled: number;
let schema: string | undefined;
Expand All @@ -861,6 +871,7 @@ export default class ParticipantController {
schemaFormat: 'full',
collectionName,
amountOfDocumentsToSample: DOCUMENTS_TO_SAMPLE_FOR_SCHEMA_PROMPT,
stream,
}));

if (!schema || amountOfDocumentsSampled === 0) {
Expand Down Expand Up @@ -971,14 +982,15 @@ export default class ParticipantController {
abortSignal: abortController.signal,
databaseName,
collectionName,
stream,
}));
} catch (e) {
// When an error fetching the collection schema or sample docs occurs,
// we still want to continue as it isn't critical, however,
// we do want to notify the user.
stream.markdown(
vscode.l10n.t(
'An error occurred while fetching the collection schema and sample documents.\nThe generated query will not be able to reference your data.'
'An error occurred while fetching the collection schema and sample documents.\nThe generated query will not be able to reference the shape of your data.'
)
);
}
Expand Down Expand Up @@ -1012,14 +1024,20 @@ export default class ParticipantController {
async _handleDocsRequestWithChatbot({
prompt,
chatId,
stream,
}: {
prompt: string;
chatId: string;
stream: vscode.ChatResponseStream;
}): Promise<{
responseContent: string;
responseReferences?: Reference[];
docsChatbotMessageId: string;
}> {
stream.push(
new vscode.ChatResponseProgressPart('Consulting MongoDB documentation...')
);

let { docsChatbotConversationId } =
this._chatMetadataStore.getChatMetadata(chatId) ?? {};
if (!docsChatbotConversationId) {
Expand Down Expand Up @@ -1115,6 +1133,7 @@ export default class ParticipantController {
docsResult = await this._handleDocsRequestWithChatbot({
prompt: request.prompt,
chatId,
stream,
});
} catch (error) {
// If the docs chatbot API is not available, fall back to Copilot’s LLM and include
Expand Down Expand Up @@ -1163,9 +1182,9 @@ export default class ParticipantController {
if (!hasBeenShownWelcomeMessageAlready) {
stream.markdown(
vscode.l10n.t(`
Welcome to MongoDB Participant!\n\n
Interact with your MongoDB clusters and generate MongoDB-related code more efficiently with intelligent AI-powered feature, available today in the MongoDB extension.\n\n
Please see our [FAQ](https://www.mongodb.com/docs/generative-ai-faq/) for more information.\n\n`)
Welcome to MongoDB Participant!\n\n
Interact with your MongoDB clusters and generate MongoDB-related code more efficiently with intelligent AI-powered feature, available today in the MongoDB extension.\n\n
Please see our [FAQ](https://www.mongodb.com/docs/generative-ai-faq/) for more information.\n\n`)
);

this._telemetryService.track(
Expand Down
Loading