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

Fix: fire change event when chat content resolves #188553

Merged
merged 1 commit into from
Jul 22, 2023
Merged
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
27 changes: 15 additions & 12 deletions src/vs/workbench/contrib/chat/common/chatModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ export class ChatRequestModel implements IChatRequestModel {

interface ResponsePart { string: IMarkdownString; resolving?: boolean }
class Response {
private _onDidChangeValue = new Emitter<void>();
public get onDidChangeValue() {
return this._onDidChangeValue.event;
}

private _responseParts: ResponsePart[];
private _responseRepr: IMarkdownString;

Expand All @@ -96,7 +101,7 @@ class Response {
this._responseParts = [{ string: value }];
}

updateContent(responsePart: string | { placeholder: string; resolvedContent?: Promise<string> }): void {
updateContent(responsePart: string | { placeholder: string; resolvedContent?: Promise<string> }, quiet?: boolean): void {
if (typeof responsePart === 'string') {
const responsePartLength = this._responseParts.length - 1;
const lastResponsePart = this._responseParts[responsePartLength];
Expand All @@ -109,22 +114,25 @@ class Response {
this._responseParts[responsePartLength] = { string: new MarkdownString(lastResponsePart.string.value + responsePart) };
}

this._updateRepr();
this._updateRepr(quiet);
} else {
// Add a new resolving part
const responsePosition = this._responseParts.push({ string: new MarkdownString(responsePart.placeholder), resolving: true });
this._updateRepr();
this._updateRepr(quiet);

responsePart.resolvedContent?.then((content) => {
// Replace the resolving part's content with the resolved response
this._responseParts[responsePosition] = { string: new MarkdownString(content) };
this._updateRepr();
this._updateRepr(quiet);
});
}
}

private _updateRepr() {
private _updateRepr(quiet?: boolean) {
this._responseRepr = new MarkdownString(this._responseParts.map(r => r.string.value).join('\n'));
if (!quiet) {
this._onDidChangeValue.fire();
}
}
}

Expand Down Expand Up @@ -192,17 +200,12 @@ export class ChatResponseModel extends Disposable implements IChatResponseModel
) {
super();
this._response = new Response(_response);
this._register(this._response.onDidChangeValue(() => this._onDidChange.fire()));
this._id = 'response_' + ChatResponseModel.nextId++;
}

updateContent(responsePart: string | { placeholder: string; resolvedContent?: Promise<string> }, quiet?: boolean) {
try {
this._response.updateContent(responsePart);
} finally {
if (!quiet) {
this._onDidChange.fire();
}
}
this._response.updateContent(responsePart, quiet);
}

setProviderResponseId(providerResponseId: string) {
Expand Down