Refactor model selection logic into separate file. Add tons of tests#299210
Merged
Refactor model selection logic into separate file. Add tons of tests#299210
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors chat model selection/reset logic out of ChatInputPart into a dedicated chatModelSelectionLogic.ts module and adds extensive unit test coverage to reduce “model picker switches out from under me” issues reported across multi-window/session scenarios.
Changes:
- Added
chatModelSelectionLogic.tswith reusable helpers for model filtering, default selection, persistence restore, sync-state reconciliation, and cache merging. - Updated
ChatInputPartto use the extracted helpers (model list changes, persisted restore, sync-from-state, validation/reset, cache merge, default selection). - Added a large new test suite covering mode/session/inline constraints, persistence semantics, cache merge behavior, and race-like sequences.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/vs/workbench/contrib/chat/browser/widget/input/chatModelSelectionLogic.ts |
New module encapsulating model selection, filtering, reset, and cache-merge decision logic. |
src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts |
Switched in-widget logic to the new helpers; refactored persistence restore and sync/reset paths. |
src/vs/workbench/contrib/chat/test/browser/widget/input/chatModelSelectionLogic.test.ts |
New, comprehensive unit test suite for the extracted logic and key scenarios/races. |
Comments suppressed due to low confidence (2)
src/vs/workbench/contrib/chat/browser/widget/input/chatModelSelectionLogic.ts:32
- The session-type check hardcodes the string
'local'. Since the codebase already defineslocalChatSessionType(inchatSessionsService.ts), it would be more robust/maintainable to depend on that constant here to avoid future drift.
if (sessionType && sessionType !== 'local') {
return models.filter(entry =>
src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts:953
- There are now several repeated blocks that rebuild
allModelsviagetLanguageModelIds().map(lookupLanguageModel(...))(e.g._syncFromModel,checkModelSupported,hasModelsTargetingSessionType,isModelValidForCurrentSession). Besides allocations, this duplication makes it easy for these call sites to diverge (e.g. around cache inclusion). Consider extracting a single helper (and reusing the same merged live+cache list where needed).
const allModels = this.languageModelsService.getLanguageModelIds()
.map(modelId => ({ identifier: modelId, metadata: this.languageModelsService.lookupLanguageModel(modelId)! }));
const sessionType = this.getCurrentSessionType();
const syncResult = resolveModelFromSyncState(state.selectedModel, this._currentLanguageModel.get(), allModels, sessionType, {
location: this.location,
currentModeKind: this.currentModeKind,
isInlineChatV2Enabled: !!this.configurationService.getValue(InlineChatConfigKeys.EnableV2),
sessionType,
});
if (syncResult.action === 'apply') {
src/vs/workbench/contrib/chat/browser/widget/input/chatModelSelectionLogic.ts
Outdated
Show resolved
Hide resolved
src/vs/workbench/contrib/chat/browser/widget/input/chatInputPart.ts
Outdated
Show resolved
Hide resolved
aeschli
approved these changes
Mar 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The number of user reports about the model picker incorrectly switching from under them is quite high.
This extracts the logic into its own file since it's a bit complex and also adds a ton of tests
Some bugs include