Skip to content

chat: refactor input state to be central - #278683

Merged
Connor Peet (connor4312) merged 8 commits into
mainfrom
connor4312/view-state-polishing
Nov 21, 2025
Merged

Connor Peet (connor4312) merged 8 commits into
mainfrom
connor4312/view-state-polishing

Conversation

@connor4312

Copy link
Copy Markdown
Member

This is a big PR that centralizes input state into an aptly named inputState on the model. This allows state to be passed around and transferred more completely and consistently.

Model, agent, input, and selection is transferred between panel chat, editor chat, and even quick chat, and also in session history and in sessions transferred between workspaces.

More stuff can be cleaned up after #278476 is in because there is still some explicit state passing to make it work with empty sessions.

I also want to do some work on history cleanup/unification. Additionally, this code basically syncs UI state back and forth with the input model, but I want to do a little more work to actually have the model as the point of truth for some of this data. For example, instead of updating your model in the chat picker which triggers and update to save state in the model, instead have the picker take an IObservable<ChatModel> and emit an event to update the chat model and observe that change.

But that should be a less wide-reaching change.

cc roblourens

refs #277318

- Add a public created `timestamp` on the IChatModel
- Add a started `timestamp` and optional `completedAt` timestamp on
  the IChatResponseModel
- Make `isPendingConfirmation<{startedWaitingAt: number}> | undefined`
  to encode the time when the response started waiting for confirmation.
- Add a `confirmationAdjustedTimestamp` that can be used to reflect the
  duration a chat response was waiting for user input vs working.
Copilot AI review requested due to automatic review settings November 21, 2025 00:35
@vs-code-engineering

vs-code-engineering Bot commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

📬 CODENOTIFY

The following users are being notified based on files changed in this PR:

Benjamin Pasero (@bpasero)

Matched files:

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

Johannes Rieken (@jrieken)

Matched files:

  • src/vs/workbench/contrib/inlineChat/browser/inlineChatWidget.ts

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR centralizes chat input state management by moving it from the ChatWidget/ChatInputPart into the ChatModel itself. This enables automatic serialization, restoration, and transfer of input state (attachments, mode, selected model, input text, and cursor selections) when moving chats between different locations (editor, sidebar, panel, window).

Key Changes

  • New InputModel class: Manages input state as an observable that syncs bidirectionally with the UI
  • State migration: Updated history service to migrate old IChatHistoryEntry format to new IChatModelInputState format
  • Simplified transfer: Removed manual state extraction code in move actions, relying on automatic model serialization

Reviewed Changes

Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
chatModel.ts Added IInputModel interface, InputModel class, and IChatModelInputState with serialization support
chatInputPart.ts Implemented bidirectional sync between input UI and model, added debouncing for text changes
chatWidget.ts Passes input model reference to input part, simplified getViewState()
chatViewPane.ts Updated to use model's input state instead of manual state passing
chatEditor.ts Removed manual view state management, relying on model's input state
chatQuick.ts Sets empty input state when initializing model
chatWidgetHistoryService.ts Added migration logic from old IChatHistoryEntry to IChatModelInputState
chatMoveActions.ts Simplified move actions by using model's automatic state preservation
chatExecuteActions.ts Updated mode toggle to support session resource lookup
inlineChatWidget.ts Updated to use new input state interface
chatDynamicVariables.ts Updated to read/write contrib state from model's input state
mainThreadChatAgents2.ts Updated session transfer to use model's input state
modePickerActionItem.ts Added session resource to mode picker delegate
Comments suppressed due to low confidence (2)

src/vs/workbench/contrib/chat/browser/contrib/chatDynamicVariables.ts:51

  • [nitpick] Potential race condition: The onDidChangeViewModel handler at lines 46-51 attempts to read state from widget.viewModel?.model.inputModel.state.get() synchronously. However, when the view model changes, the input model reference might not be set yet in the ChatInputPart (which happens at line 2176 in chatWidget.ts). This could cause the contrib state to not be restored properly. Consider deferring this read or adding a check to ensure the input model is initialized.
		this._register(widget.inputEditor.onDidChangeModelContent(e => {

			const removed: IDynamicVariable[] = [];
			let didChange = false;

			// Don't mutate entries in _variables, since they will be returned from the getter

src/vs/workbench/contrib/chat/browser/chatQuick.ts:407

  • [nitpick] The setState call at line 406 is clearing the inputText and selections immediately before calling setModel, which will sync from the model state. This seems redundant and potentially confusing. If the intention is to ensure the input starts empty for quick chat, this should be done after setModel is called, or the comment should explain why this is necessary before model assignment.

@@ -2182,21 +2267,17 @@ export class ChatInputPart extends Disposable implements IHistoryNavigationWidge
};

Copilot AI Nov 21, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The historyKeyFn uses spread operators that don't exclude all fields that should be ignored for history comparison. Specifically, it excludes selectedModel but includes contrib, selections, and inputText which may cause duplicate history entries if these fields differ but the core input is the same. Consider whether selections and contrib should also be excluded from the history key comparison.

Copilot uses AI. Check for mistakes.

setChatModel(chatModel: IChatModel, state?: IChatViewState) {
this._chatWidget.setModel(chatModel, { ...state, inputValue: undefined });
setChatModel(chatModel: IChatModel) {

Copilot AI Nov 21, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The setChatModel method is creating a new object with spread operator and then overriding inputText and selections to empty values. This logic seems counterintuitive - if a state is provided, why would we want to clear the input text and selections? This might be intentional for inline chat context, but it should be documented or reconsidered. If the intention is to preserve other state but clear the text, this should be explained in a comment.

Suggested change
setChatModel(chatModel: IChatModel) {
setChatModel(chatModel: IChatModel) {
// Intentionally clear input text and selections when setting a new chat model.
// This ensures the input area is reset for the new session/context in the inline chat widget.

Copilot uses AI. Check for mistakes.
@@ -262,7 +270,7 @@ export class ChatViewPane extends ViewPane implements IViewWelcomeDelegate {
this.updateActions();
}

Copilot AI Nov 21, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The viewState parameter in loadSession is declared but never used (line 271). The input state should be applied to the model if provided, similar to how it's done in chatViewPane.ts lines 248-250. This parameter should either be removed if not needed, or the input state should be set on the model before calling updateModel.

Copilot uses AI. Check for mistakes.
@joaomoreno

João Moreno (joaomoreno) commented Nov 21, 2025 •

Copy link
Copy Markdown
Contributor

FYI this broke the chat input as the tools were suddenly missing:

image

Martin Aeschlimann (@aeschli) fixed it with #278761

@connor4312

Copy link
Copy Markdown
Member Author

Ah, sorry 🙈 I think the change Martin made in that PR is an overall improvement

@vs-code-engineering vs-code-engineering Bot locked and limited conversation to collaborators Jan 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants