Skip to content

Commit eeec4c3

Browse files
authored
Persist selected chat model globally, not per-workspace (microsoft#234335)
* Persist selected chat model globally, not per-workspace * Cleanup
1 parent b69589c commit eeec4c3

File tree

4 files changed

+43
-51
lines changed

4 files changed

+43
-51
lines changed

src/vs/workbench/contrib/chat/browser/chatEditor.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ export class ChatEditor extends EditorPane {
128128

129129
// Need to set props individually on the memento
130130
this._viewState.inputValue = widgetViewState.inputValue;
131-
this._viewState.selectedLanguageModelId = widgetViewState.selectedLanguageModelId;
132131
this._memento.saveMemento();
133132
}
134133
}

src/vs/workbench/contrib/chat/browser/chatInputPart.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import { WorkbenchList } from '../../../../platform/list/browser/listService.js'
6565
import { ILogService } from '../../../../platform/log/common/log.js';
6666
import { INotificationService } from '../../../../platform/notification/common/notification.js';
6767
import { IOpenerService, type OpenInternalOptions } from '../../../../platform/opener/common/opener.js';
68+
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
6869
import { FolderThemeIcon, IThemeService } from '../../../../platform/theme/common/themeService.js';
6970
import { fillEditorsDragData } from '../../../browser/dnd.js';
7071
import { IFileLabelOptions, ResourceLabels } from '../../../browser/labels.js';
@@ -281,6 +282,7 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
281282
@IMenuService private readonly menuService: IMenuService,
282283
@ILanguageService private readonly languageService: ILanguageService,
283284
@IThemeService private readonly themeService: IThemeService,
285+
@IStorageService private readonly storageService: IStorageService,
284286
) {
285287
super();
286288

@@ -316,6 +318,35 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
316318
this._chatEditsListPool = this._register(this.instantiationService.createInstance(CollapsibleListPool, this._onDidChangeVisibility.event, MenuId.ChatEditingWidgetModifiedFilesToolbar));
317319

318320
this._hasFileAttachmentContextKey = ChatContextKeys.hasFileAttachments.bindTo(contextKeyService);
321+
322+
this.initSelectedModel();
323+
}
324+
325+
private getSelectedModelStorageKey(): string {
326+
return `chat.currentLanguageModel.${this.location}`;
327+
}
328+
329+
private initSelectedModel() {
330+
const persistedSelection = this.storageService.get(this.getSelectedModelStorageKey(), StorageScope.APPLICATION);
331+
if (persistedSelection) {
332+
const model = this.languageModelsService.lookupLanguageModel(persistedSelection);
333+
if (model) {
334+
this._currentLanguageModel = persistedSelection;
335+
this._onDidChangeCurrentLanguageModel.fire(this._currentLanguageModel);
336+
} else {
337+
this._waitForPersistedLanguageModel.value = this.languageModelsService.onDidChangeLanguageModels(e => {
338+
const persistedModel = e.added?.find(m => m.identifier === persistedSelection);
339+
if (persistedModel) {
340+
this._waitForPersistedLanguageModel.clear();
341+
342+
if (persistedModel.metadata.isUserSelectable) {
343+
this._currentLanguageModel = persistedSelection;
344+
this._onDidChangeCurrentLanguageModel.fire(this._currentLanguageModel!);
345+
}
346+
}
347+
});
348+
}
349+
}
319350
}
320351

321352
private setCurrentLanguageModelToDefault() {
@@ -335,6 +366,8 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
335366
if (this.cachedDimensions) {
336367
this.layout(this.cachedDimensions.height, this.cachedDimensions.width);
337368
}
369+
370+
this.storageService.store(this.getSelectedModelStorageKey(), modelId, StorageScope.APPLICATION, StorageTarget.USER);
338371
}
339372

340373
private loadHistory(): HistoryNavigator2<IChatHistoryEntry> {
@@ -367,26 +400,6 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
367400
if (state.inputValue) {
368401
this.setValue(state.inputValue, false);
369402
}
370-
371-
if (state.selectedLanguageModelId) {
372-
const model = this.languageModelsService.lookupLanguageModel(state.selectedLanguageModelId);
373-
if (model) {
374-
this._currentLanguageModel = state.selectedLanguageModelId;
375-
this._onDidChangeCurrentLanguageModel.fire(this._currentLanguageModel);
376-
} else {
377-
this._waitForPersistedLanguageModel.value = this.languageModelsService.onDidChangeLanguageModels(e => {
378-
const persistedModel = e.added?.find(m => m.identifier === state.selectedLanguageModelId);
379-
if (persistedModel) {
380-
this._waitForPersistedLanguageModel.clear();
381-
382-
if (persistedModel.metadata.isUserSelectable) {
383-
this._currentLanguageModel = state.selectedLanguageModelId;
384-
this._onDidChangeCurrentLanguageModel.fire(this._currentLanguageModel!);
385-
}
386-
}
387-
});
388-
}
389-
}
390403
}
391404

392405
logInputHistory(): void {

src/vs/workbench/contrib/chat/browser/chatWidget.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ const $ = dom.$;
6060
export interface IChatViewState {
6161
inputValue?: string;
6262
inputState?: IChatInputState;
63-
selectedLanguageModelId?: string;
6463
}
6564

6665
export interface IChatWidgetStyles extends IChatInputStyles {
@@ -1261,8 +1260,7 @@ export class ChatWidget extends Disposable implements IChatWidget {
12611260
getViewState(): IChatViewState {
12621261
return {
12631262
inputValue: this.getInput(),
1264-
inputState: this.inputPart.getViewState(),
1265-
selectedLanguageModelId: this.inputPart.currentLanguageModel,
1263+
inputState: this.inputPart.getViewState()
12661264
};
12671265
}
12681266

src/vs/workbench/contrib/inlineChat/browser/inlineChatController.ts

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,22 @@ import { IContextKey, IContextKeyService } from '../../../../platform/contextkey
3333
import { IDialogService } from '../../../../platform/dialogs/common/dialogs.js';
3434
import { IInstantiationService, ServicesAccessor } from '../../../../platform/instantiation/common/instantiation.js';
3535
import { ILogService } from '../../../../platform/log/common/log.js';
36+
import { IEditorService, SIDE_GROUP } from '../../../services/editor/common/editorService.js';
37+
import { IViewsService } from '../../../services/views/common/viewsService.js';
3638
import { showChatView } from '../../chat/browser/chat.js';
37-
import { IChatViewState, IChatWidgetLocationOptions } from '../../chat/browser/chatWidget.js';
39+
import { IChatWidgetLocationOptions } from '../../chat/browser/chatWidget.js';
3840
import { ChatAgentLocation } from '../../chat/common/chatAgents.js';
41+
import { ChatContextKeys } from '../../chat/common/chatContextKeys.js';
3942
import { ChatModel, ChatRequestRemovalReason, IChatRequestModel, IChatTextEditGroup, IChatTextEditGroupState, IResponse } from '../../chat/common/chatModel.js';
4043
import { IChatService } from '../../chat/common/chatService.js';
41-
import { HunkInformation, Session, StashedSession } from './inlineChatSession.js';
42-
import { InlineChatError } from './inlineChatSessionServiceImpl.js';
43-
import { EditModeStrategy, HunkAction, IEditObserver, LiveStrategy, PreviewStrategy, ProgressingEditsOptions } from './inlineChatStrategies.js';
44-
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';
4544
import { INotebookEditorService } from '../../notebook/browser/services/notebookEditorService.js';
46-
import { IEditorService, SIDE_GROUP } from '../../../services/editor/common/editorService.js';
47-
import { IViewsService } from '../../../services/views/common/viewsService.js';
45+
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';
4846
import { IInlineChatSavingService } from './inlineChatSavingService.js';
47+
import { HunkInformation, Session, StashedSession } from './inlineChatSession.js';
4948
import { IInlineChatSessionService } from './inlineChatSessionService.js';
49+
import { InlineChatError } from './inlineChatSessionServiceImpl.js';
50+
import { EditModeStrategy, HunkAction, IEditObserver, LiveStrategy, PreviewStrategy, ProgressingEditsOptions } from './inlineChatStrategies.js';
5051
import { InlineChatZoneWidget } from './inlineChatZoneWidget.js';
51-
import { ChatContextKeys } from '../../chat/common/chatContextKeys.js';
52-
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
5352

5453
export const enum State {
5554
CREATE_SESSION = 'CREATE_SESSION',
@@ -104,8 +103,6 @@ export class InlineChatController implements IEditorContribution {
104103
return editor.getContribution<InlineChatController>(INLINE_CHAT_ID);
105104
}
106105

107-
private static readonly _storageKey = 'inlineChatController.state';
108-
109106
private _isDisposed: boolean = false;
110107
private readonly _store = new DisposableStore();
111108

@@ -147,7 +144,6 @@ export class InlineChatController implements IEditorContribution {
147144
@IContextKeyService contextKeyService: IContextKeyService,
148145
@IChatService private readonly _chatService: IChatService,
149146
@IEditorService private readonly _editorService: IEditorService,
150-
@IStorageService private readonly _storageService: IStorageService,
151147
@INotebookEditorService notebookEditorService: INotebookEditorService,
152148
) {
153149
this._ctxVisible = CTX_INLINE_CHAT_VISIBLE.bindTo(contextKeyService);
@@ -411,7 +407,7 @@ export class InlineChatController implements IEditorContribution {
411407
this._sessionStore.add(this._session.wholeRange.onDidChange(handleWholeRangeChange));
412408
handleWholeRangeChange();
413409

414-
this._ui.value.widget.setChatModel(this._session.chatModel, this._retrieveWidgetState());
410+
this._ui.value.widget.setChatModel(this._session.chatModel);
415411
this._updatePlaceholder();
416412

417413
const isModelEmpty = !this._session.chatModel.hasRequests;
@@ -893,11 +889,6 @@ export class InlineChatController implements IEditorContribution {
893889
this._ctxUserDidEdit.reset();
894890

895891
if (this._ui.rawValue) {
896-
// persist selected LM in memento
897-
const { selectedLanguageModelId } = this._ui.rawValue.widget.chatWidget.getViewState();
898-
const state = { selectedLanguageModelId };
899-
this._storageService.store(InlineChatController._storageKey, state, StorageScope.PROFILE, StorageTarget.USER);
900-
901892
this._ui.rawValue.hide();
902893
}
903894

@@ -907,15 +898,6 @@ export class InlineChatController implements IEditorContribution {
907898
}
908899
}
909900

910-
private _retrieveWidgetState(): IChatViewState | undefined {
911-
try {
912-
const state = JSON.parse(this._storageService.get(InlineChatController._storageKey, StorageScope.PROFILE) ?? '{}');
913-
return state;
914-
} catch {
915-
return undefined;
916-
}
917-
}
918-
919901
private _updateCtxResponseType(): void {
920902

921903
if (!this._session) {

0 commit comments

Comments
 (0)