diff --git a/src/vs/workbench/contrib/chat/browser/chatInputPart.ts b/src/vs/workbench/contrib/chat/browser/chatInputPart.ts index 7b7b5abb9fd..a2bd5d7f328 100644 --- a/src/vs/workbench/contrib/chat/browser/chatInputPart.ts +++ b/src/vs/workbench/contrib/chat/browser/chatInputPart.ts @@ -200,7 +200,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge private inputEditorHasFocus: IContextKey; private readonly _waitForPersistedLanguageModel = this._register(new MutableDisposable()); - private _onDidChangeCurrentLanguageModel = new Emitter(); + private _onDidChangeCurrentLanguageModel = this._register(new Emitter()); private _currentLanguageModel: string | undefined; get currentLanguageModel() { return this._currentLanguageModel; diff --git a/src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts b/src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts index 679cc961fe5..17d1b391b3b 100644 --- a/src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts +++ b/src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts @@ -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(INLINE_CHAT_ID); } + private static readonly _storageKey = 'inlineChatController.state'; + private _isDisposed: boolean = false; private readonly _store = new DisposableStore(); private readonly _ui: Lazy; - private _uiInitViewState: IChatViewState | undefined; private readonly _ctxVisible: IContextKey; private readonly _ctxEditing: IContextKey; @@ -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(); }