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

Expose promise that resolves when chat response is complete, and alert on this #182983

Merged
merged 1 commit into from May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/vs/workbench/contrib/chat/browser/chatWidget.ts
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { alert } from 'vs/base/browser/ui/aria/aria';
import * as dom from 'vs/base/browser/dom';
import { ITreeContextMenuEvent, ITreeElement } from 'vs/base/browser/ui/tree/tree';
import { CancellationToken } from 'vs/base/common/cancellation';
Expand Down Expand Up @@ -381,6 +382,13 @@ export class ChatWidget extends Disposable implements IChatWidget {
if (result) {
revealLastElement(this.tree);
this.inputPart.acceptInput(query);
result.responseCompletePromise.then(() => {
const responses = this.viewModel?.getItems().filter(isResponseVM);
const lastResponse: IInteractiveResponseViewModel | undefined = responses?.[responses.length - 1];
if (lastResponse) {
alert(lastResponse.response.value);
}
});
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vs/workbench/contrib/chat/common/chatService.ts
Expand Up @@ -184,7 +184,7 @@ export interface IChatService {
/**
* Returns whether the request was accepted.
*/
sendRequest(sessionId: string, message: string | IChatReplyFollowup): Promise<boolean>;
sendRequest(sessionId: string, message: string | IChatReplyFollowup): Promise<{ responseCompletePromise: Promise<void> } | undefined>;
cancelCurrentRequestForSession(sessionId: string): void;
getSlashCommands(sessionId: string, token: CancellationToken): Promise<ISlashCommand[] | undefined>;
clearSession(sessionId: string): void;
Expand Down
10 changes: 5 additions & 5 deletions src/vs/workbench/contrib/chat/common/chatServiceImpl.ts
Expand Up @@ -309,12 +309,12 @@ export class ChatService extends Disposable implements IChatService {
return this._startSession(data.providerId, data, CancellationToken.None);
}

async sendRequest(sessionId: string, request: string | IChatReplyFollowup): Promise<boolean> {
async sendRequest(sessionId: string, request: string | IChatReplyFollowup): Promise<{ responseCompletePromise: Promise<void> } | undefined> {
const messageText = typeof request === 'string' ? request : request.message;
this.trace('sendRequest', `sessionId: ${sessionId}, message: ${messageText.substring(0, 20)}${messageText.length > 20 ? '[...]' : ''}}`);
if (!messageText.trim()) {
this.trace('sendRequest', 'Rejected empty message');
return false;
return;
}

const model = this._sessionModels.get(sessionId);
Expand All @@ -330,12 +330,11 @@ export class ChatService extends Disposable implements IChatService {

if (this._pendingRequests.has(sessionId)) {
this.trace('sendRequest', `Session ${sessionId} already has a pending request`);
return false;
return;
}

// This method is only returning whether the request was accepted - don't block on the actual request
this._sendRequestAsync(model, provider, request);
return true;
return { responseCompletePromise: this._sendRequestAsync(model, provider, request) };
}

private async _sendRequestAsync(model: ChatModel, provider: IChatProvider, message: string | IChatReplyFollowup): Promise<void> {
Expand Down Expand Up @@ -412,6 +411,7 @@ export class ChatService extends Disposable implements IChatService {
rawResponsePromise.finally(() => {
this._pendingRequests.delete(model.sessionId);
});
return rawResponsePromise;
}

private async handleSlashCommand(sessionId: string, command: string): Promise<string> {
Expand Down