don't use controller's viewstate management but use storage service directly, (#233589)

fixes https://github.com/microsoft/vscode-copilot/issues/10315 and other bugs
This commit is contained in:
Johannes Rieken
2024-11-11 16:29:39 +01:00
committed by GitHub
parent afc3a6a861
commit 5261d038a7
2 changed files with 25 additions and 38 deletions

View File

@@ -38,7 +38,7 @@ import { IChatViewState, IChatWidgetLocationOptions } from '../../chat/browser/c
import { ChatAgentLocation } from '../../chat/common/chatAgents.js';
import { ChatModel, ChatRequestRemovalReason, IChatRequestModel, IChatTextEditGroup, IChatTextEditGroupState, IResponse } from '../../chat/common/chatModel.js';
import { IChatService } from '../../chat/common/chatService.js';
import { HunkInformation, HunkState, Session, StashedSession } from './inlineChatSession.js';
import { HunkInformation, Session, StashedSession } from './inlineChatSession.js';
import { InlineChatError } from './inlineChatSessionServiceImpl.js';
import { EditModeStrategy, HunkAction, IEditObserver, LiveStrategy, PreviewStrategy, ProgressingEditsOptions } from './inlineChatStrategies.js';
import { CTX_INLINE_CHAT_EDITING, CTX_INLINE_CHAT_REQUEST_IN_PROGRESS, CTX_INLINE_CHAT_RESPONSE_TYPE, CTX_INLINE_CHAT_USER_DID_EDIT, CTX_INLINE_CHAT_VISIBLE, EditMode, INLINE_CHAT_ID, InlineChatConfigKeys, InlineChatResponseType } from '../common/inlineChat.js';
@@ -49,6 +49,7 @@ import { IInlineChatSavingService } from './inlineChatSavingService.js';
import { IInlineChatSessionService } from './inlineChatSessionService.js';
import { InlineChatZoneWidget } from './inlineChatZoneWidget.js';
import { ChatContextKeys } from '../../chat/common/chatContextKeys.js';
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
export const enum State {
CREATE_SESSION = 'CREATE_SESSION',
@@ -103,11 +104,12 @@ export class InlineChatController implements IEditorContribution {
return editor.getContribution<InlineChatController>(INLINE_CHAT_ID);
}
private static readonly _storageKey = 'inlineChatController.state';
private _isDisposed: boolean = false;
private readonly _store = new DisposableStore();
private readonly _ui: Lazy<InlineChatZoneWidget>;
private _uiInitViewState: IChatViewState | undefined;
private readonly _ctxVisible: IContextKey<boolean>;
private readonly _ctxEditing: IContextKey<boolean>;
@@ -145,6 +147,7 @@ export class InlineChatController implements IEditorContribution {
@IContextKeyService contextKeyService: IContextKeyService,
@IChatService private readonly _chatService: IChatService,
@IEditorService private readonly _editorService: IEditorService,
@IStorageService private readonly _storageService: IStorageService,
@INotebookEditorService notebookEditorService: INotebookEditorService,
) {
this._ctxVisible = CTX_INLINE_CHAT_VISIBLE.bindTo(contextKeyService);
@@ -239,19 +242,6 @@ export class InlineChatController implements IEditorContribution {
this._log('DISPOSED controller');
}
saveViewState(): any {
if (!this._ui.rawValue) {
return undefined;
}
// only take select lm
const { selectedLanguageModelId } = this._ui.rawValue.widget.chatWidget.getViewState();
return { selectedLanguageModelId };
}
restoreViewState(state: any): void {
this._uiInitViewState = state;
}
private _log(message: string | Error, ...more: any[]): void {
if (message instanceof Error) {
this._logService.error(message, ...more);
@@ -421,11 +411,9 @@ export class InlineChatController implements IEditorContribution {
this._sessionStore.add(this._session.wholeRange.onDidChange(handleWholeRangeChange));
handleWholeRangeChange();
this._ui.value.widget.setChatModel(this._session.chatModel, this._uiInitViewState);
this._uiInitViewState = undefined;
this._ui.value.widget.setChatModel(this._session.chatModel, this._retrieveWidgetState());
this._updatePlaceholder();
const isModelEmpty = !this._session.chatModel.hasRequests;
this._ui.value.widget.updateToolbar(true);
this._ui.value.widget.toggleStatus(!isModelEmpty);
@@ -899,11 +887,19 @@ export class InlineChatController implements IEditorContribution {
}
private _resetWidget() {
this._sessionStore.clear();
this._ctxVisible.reset();
this._ctxUserDidEdit.reset();
this._ui.rawValue?.hide();
if (this._ui.rawValue) {
// persist selected LM in memento
const { selectedLanguageModelId } = this._ui.rawValue.widget.chatWidget.getViewState();
const state = { selectedLanguageModelId };
this._storageService.store(InlineChatController._storageKey, state, StorageScope.PROFILE, StorageTarget.USER);
this._ui.rawValue.hide();
}
// Return focus to the editor only if the current focus is within the editor widget
if (this._editor.hasWidgetFocus()) {
@@ -911,6 +907,15 @@ export class InlineChatController implements IEditorContribution {
}
}
private _retrieveWidgetState(): IChatViewState | undefined {
try {
const state = JSON.parse(this._storageService.get(InlineChatController._storageKey, StorageScope.PROFILE) ?? '{}');
return state;
} catch {
return undefined;
}
}
private _updateCtxResponseType(): void {
if (!this._session) {
@@ -985,24 +990,6 @@ export class InlineChatController implements IEditorContribution {
// ---- controller API
showSaveHint(): void {
if (!this._session) {
return;
}
const status = localize('savehint', "Accept or discard changes to continue saving.");
this._ui.value.widget.updateStatus(status, { classes: ['warn'] });
if (this._ui.value.position) {
this._editor.revealLineInCenterIfOutsideViewport(this._ui.value.position.lineNumber);
} else {
const hunk = this._session.hunkData.getInfo().find(info => info.getState() === HunkState.Pending);
if (hunk) {
this._editor.revealLineInCenterIfOutsideViewport(hunk.getRangesN()[0].startLineNumber);
}
}
}
acceptInput() {
return this.chatWidget.acceptInput();
}