Skip to content

Commit

Permalink
Simplify IChatWidgetContrib
Browse files Browse the repository at this point in the history
Remove onDidChangeInputState
Better fix for microsoft/vscode-copilot#6957
  • Loading branch information
roblourens committed Jul 26, 2024
1 parent 699b8df commit 600a363
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 54 deletions.
32 changes: 15 additions & 17 deletions src/vs/workbench/contrib/chat/browser/chatInputPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
// private readonly editorOptions: ChatEditorOptions, // TODO this should be used
private readonly location: ChatAgentLocation,
private readonly options: IChatInputPartOptions,
private readonly getInputState: () => any,
@IChatWidgetHistoryService private readonly historyService: IChatWidgetHistoryService,
@IModelService private readonly modelService: IModelService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
Expand Down Expand Up @@ -234,11 +235,12 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
}

showPreviousValue(): void {
const inputState = this.getInputState();
if (this.history.isAtEnd()) {
this.saveCurrentValue();
this.saveCurrentValue(inputState);
} else {
if (!this.history.has({ text: this._inputEditor.getValue(), state: this.history.current().state })) {
this.saveCurrentValue();
if (!this.history.has({ text: this._inputEditor.getValue(), state: inputState })) {
this.saveCurrentValue(inputState);
this.history.resetCursor();
}
}
Expand All @@ -247,11 +249,12 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
}

showNextValue(): void {
const inputState = this.getInputState();
if (this.history.isAtEnd()) {
return;
} else {
if (!this.history.has({ text: this._inputEditor.getValue(), state: this.history.current().state })) {
this.saveCurrentValue();
if (!this.history.has({ text: this._inputEditor.getValue(), state: inputState })) {
this.saveCurrentValue(inputState);
this.history.resetCursor();
}
}
Expand Down Expand Up @@ -288,12 +291,12 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
this.inputEditor.setPosition({ lineNumber: 1, column: value.length + 1 });

if (!transient) {
this.saveCurrentValue();
this.saveCurrentValue(this.getInputState());
}
}

private saveCurrentValue(): void {
const newEntry = { text: this._inputEditor.getValue(), state: this.history.current().state };
private saveCurrentValue(inputState: any): void {
const newEntry = { text: this._inputEditor.getValue(), state: inputState };
this.history.replaceLast(newEntry);
}

Expand All @@ -312,11 +315,13 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
async acceptInput(isUserQuery?: boolean): Promise<void> {
if (isUserQuery) {
const userQuery = this._inputEditor.getValue();
const entry: IChatHistoryEntry = { text: userQuery, state: this.history.current().state };
const entry: IChatHistoryEntry = { text: userQuery, state: this.getInputState() };
this.history.replaceLast(entry);
this.history.add({ text: '' });
}

// Clear attached context, fire event to clear input state, and clear the input editor
this._attachedContext.clear();
this._onDidLoadInputState.fire({});
if (this.accessibilityService.isScreenReaderOptimized() && isMacintosh) {
this._acceptInputForVoiceover();
Expand All @@ -339,14 +344,6 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
this._inputEditor.focus();
}

clearContext(): void {
if (this._attachedContext.size > 0) {
const removed = Array.from(this._attachedContext);
this._attachedContext.clear();
this._onDidChangeContext.fire({ removed });
}
}

attachContext(overwrite: boolean, ...contentReferences: IChatRequestVariableEntry[]): void {
const removed = [];
if (overwrite) {
Expand Down Expand Up @@ -638,6 +635,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
}

saveState(): void {
this.saveCurrentValue(this.getInputState());
const inputHistory = [...this.history];
this.historyService.saveHistory(this.location, inputHistory);
}
Expand Down
19 changes: 13 additions & 6 deletions src/vs/workbench/contrib/chat/browser/chatViewPane.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export class ChatViewPane extends ViewPane {
};
}

private updateModel(model?: IChatModel | undefined, viewState?: IViewPaneState): void {
private updateModel(model?: IChatModel | undefined): void {
this.modelDisposables.clear();

model = model ?? (this.chatService.transferredSessionData?.sessionId
Expand All @@ -109,7 +109,7 @@ export class ChatViewPane extends ViewPane {
throw new Error('Could not start chat session');
}

this._widget.setModel(model, { ...(viewState ?? this.viewState) });
this._widget.setModel(model, { ...this.viewState });
this.viewState.sessionId = model.sessionId;
}

Expand Down Expand Up @@ -179,7 +179,10 @@ export class ChatViewPane extends ViewPane {
if (this.widget.viewModel) {
this.chatService.clearSession(this.widget.viewModel.sessionId);
}
this.updateModel(undefined, { ...this.viewState, inputValue: undefined });

// Grab the widget's latest view state because it will be loaded back into the widget
this.updateViewState();
this.updateModel(undefined);
}

loadSession(sessionId: string): void {
Expand Down Expand Up @@ -211,12 +214,16 @@ export class ChatViewPane extends ViewPane {
// TODO multiple chat views will overwrite each other
this._widget.saveState();

const widgetViewState = this._widget.getViewState();
this.viewState.inputValue = widgetViewState.inputValue;
this.viewState.inputState = widgetViewState.inputState;
this.updateViewState();
this.memento.saveMemento();
}

super.saveState();
}

private updateViewState(): void {
const widgetViewState = this._widget.getViewState();
this.viewState.inputValue = widgetViewState.inputValue;
this.viewState.inputState = widgetViewState.inputState;
}
}
18 changes: 2 additions & 16 deletions src/vs/workbench/contrib/chat/browser/chatWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ export interface IChatWidgetContrib extends IDisposable {
*/
getInputState?(): any;

onDidChangeInputState?: Event<void>;

/**
* Called with the result of getInputState when navigating input history.
*/
Expand Down Expand Up @@ -337,15 +335,6 @@ export class ChatWidget extends Disposable implements IChatWidget {
return undefined;
}
}).filter(isDefined);

this.contribs.forEach(c => {
if (c.onDidChangeInputState) {
this._register(c.onDidChangeInputState(() => {
const state = this.collectInputState();
this.inputPart.updateState(state);
}));
}
});
}

getContrib<T extends IChatWidgetContrib>(id: string): T | undefined {
Expand Down Expand Up @@ -586,7 +575,8 @@ export class ChatWidget extends Disposable implements IChatWidget {
renderStyle: options?.renderStyle === 'minimal' ? 'compact' : options?.renderStyle,
menus: { executeToolbar: MenuId.ChatExecute, ...this.viewOptions.menus },
editorOverflowWidgetsDomNode: this.viewOptions.editorOverflowWidgetsDomNode,
}
},
() => this.collectInputState()
));
this.inputPart.render(container, '', this);

Expand Down Expand Up @@ -788,8 +778,6 @@ export class ChatWidget extends Disposable implements IChatWidget {
if (result) {
this.inputPart.acceptInput(isUserQuery);
this._onDidSubmitAgent.fire({ agent: result.agent, slashCommand: result.slashCommand });
this.inputPart.updateState(this.collectInputState());
this.inputPart.clearContext();
result.responseCompletePromise.then(() => {
const responses = this.viewModel?.getItems().filter(isResponseVM);
const lastResponse = responses?.[responses.length - 1];
Expand All @@ -801,7 +789,6 @@ export class ChatWidget extends Disposable implements IChatWidget {
return undefined;
}


setContext(overwrite: boolean, ...contentReferences: IChatRequestVariableEntry[]) {
this.inputPart.attachContext(overwrite, ...contentReferences);
}
Expand Down Expand Up @@ -964,7 +951,6 @@ export class ChatWidget extends Disposable implements IChatWidget {
}

getViewState(): IChatViewState {
this.inputPart.saveState();
return { inputValue: this.getInput(), inputState: this.collectInputState() };
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { Emitter } from 'vs/base/common/event';
import { Disposable } from 'vs/base/common/lifecycle';
import { IChatWidget } from 'vs/workbench/contrib/chat/browser/chat';
import { ChatWidget, IChatWidgetContrib } from 'vs/workbench/contrib/chat/browser/chatWidget';
Expand All @@ -13,9 +12,6 @@ export class ChatContextAttachments extends Disposable implements IChatWidgetCon

private _attachedContext = new Set<IChatRequestVariableEntry>();

private readonly _onDidChangeInputState = this._register(new Emitter<void>());
readonly onDidChangeInputState = this._onDidChangeInputState.event;

public static readonly ID = 'chatContextAttachments';

get id() {
Expand Down Expand Up @@ -66,13 +62,11 @@ export class ChatContextAttachments extends Disposable implements IChatWidgetCon
}

this.widget.setContext(overwrite, ...attachments);
this._onDidChangeInputState.fire();
}

private _removeContext(attachments: IChatRequestVariableEntry[]) {
if (attachments.length) {
attachments.forEach(this._attachedContext.delete, this._attachedContext);
this._onDidChangeInputState.fire();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
*--------------------------------------------------------------------------------------------*/

import { coalesce } from 'vs/base/common/arrays';
import { Emitter } from 'vs/base/common/event';
import { IMarkdownString, MarkdownString } from 'vs/base/common/htmlContent';
import { Disposable } from 'vs/base/common/lifecycle';
import { basename } from 'vs/base/common/resources';
Expand Down Expand Up @@ -39,9 +38,6 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
return ChatDynamicVariableModel.ID;
}

private _onDidChangeInputState = this._register(new Emitter<void>());
readonly onDidChangeInputState = this._onDidChangeInputState.event;

constructor(
private readonly widget: IChatWidget,
@ILabelService private readonly labelService: ILabelService,
Expand Down Expand Up @@ -81,10 +77,6 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC

return ref;
}));

if (didModifyState) {
this._onDidChangeInputState.fire();
}
});

this.updateDecorations();
Expand All @@ -107,7 +99,6 @@ export class ChatDynamicVariableModel extends Disposable implements IChatWidgetC
addReference(ref: IDynamicVariable): void {
this._variables.push(ref);
this.updateDecorations();
this._onDidChangeInputState.fire();
}

private updateDecorations(): void {
Expand Down

0 comments on commit 600a363

Please sign in to comment.