Skip to content

Commit bb18007

Browse files
authored
fix retaining target mode (microsoft#300075)
* fix: set isolation mode when new session is assigned * fix: update isolation mode handling in NewChatWidget and remove unused session reference * fix: remove unused session handling in BranchPicker and update NewChatWidget to set branch directly * fix: update repo handling in NewChatWidget and RepoPicker to set repository URI directly * fix: refactor repository URI handling in NewChatWidget to use a dedicated method * fix: preserve draft state in NewChatWidget to retain picker preferences during widget recreation * fix: enhance draft state preservation in NewChatWidget to conditionally store target and related properties * fix: update isolation mode handling in IsolationModePicker to use current mode as fallback
1 parent 2b1e30a commit bb18007

4 files changed

Lines changed: 45 additions & 61 deletions

File tree

‎src/vs/sessions/contrib/chat/browser/branchPicker.ts‎

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import { IActionWidgetService } from '../../../../platform/actionWidget/browser/
1212
import { ActionListItemKind, IActionListDelegate, IActionListItem } from '../../../../platform/actionWidget/browser/actionList.js';
1313
import { IGitRepository } from '../../../../workbench/contrib/git/common/gitService.js';
1414
import { renderIcon } from '../../../../base/browser/ui/iconLabel/iconLabels.js';
15-
import { INewSession } from './newSession.js';
16-
1715
const COPILOT_WORKTREE_PATTERN = 'copilot-worktree-';
1816
const FILTER_THRESHOLD = 10;
1917

@@ -32,7 +30,6 @@ export class BranchPicker extends Disposable {
3230

3331
private _selectedBranch: string | undefined;
3432
private _preferredBranch: string | undefined;
35-
private _newSession: INewSession | undefined;
3633
private _branches: string[] = [];
3734

3835
private readonly _onDidChange = this._register(new Emitter<string | undefined>());
@@ -62,13 +59,6 @@ export class BranchPicker extends Disposable {
6259
super();
6360
}
6461

65-
/**
66-
* Sets the new session that this picker writes to.
67-
*/
68-
setNewSession(session: INewSession | undefined): void {
69-
this._newSession = session;
70-
}
71-
7262
/**
7363
* Sets the git repository and loads its branches.
7464
* When undefined, the picker is shown disabled.
@@ -78,7 +68,7 @@ export class BranchPicker extends Disposable {
7868
this._selectedBranch = undefined;
7969

8070
if (!repository) {
81-
this._newSession?.setBranch(undefined);
71+
this._onDidChange.fire(undefined);
8272
this._setLoading(false);
8373
this._updateTriggerLabel();
8474
return;
@@ -196,7 +186,6 @@ export class BranchPicker extends Disposable {
196186
private _selectBranch(branch: string): void {
197187
if (this._selectedBranch !== branch) {
198188
this._selectedBranch = branch;
199-
this._newSession?.setBranch(branch);
200189
this._onDidChange.fire(branch);
201190
this._updateTriggerLabel();
202191
}

‎src/vs/sessions/contrib/chat/browser/newChatViewPane.ts‎

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class NewChatWidget extends Disposable implements IHistoryNavigationWidget {
219219
}));
220220

221221
this._register(this._branchPicker.onDidChange((branch) => {
222+
this._newSession.value?.setBranch(branch);
222223
this._syncIndicator.setBranch(branch);
223224
this._updateDraftState();
224225
this._focusEditor();
@@ -234,13 +235,17 @@ class NewChatWidget extends Disposable implements IHistoryNavigationWidget {
234235
}));
235236

236237
this._register(this._isolationModePicker.onDidChange((mode) => {
238+
this._newSession.value?.setIsolationMode(mode);
237239
this._branchPicker.setVisible(mode === 'worktree');
238240
this._syncIndicator.setVisible(mode === 'worktree');
239241
this._updateDraftState();
240242
this._focusEditor();
241243
}));
242244

243-
this._register(this._repoPicker.onDidSelectRepo(() => {
245+
this._register(this._repoPicker.onDidSelectRepo((repoId) => {
246+
if (this._targetPicker.selectedTarget !== AgentSessionProviders.Background) {
247+
this._newSession.value?.setRepoUri(this._getRepoUri(repoId));
248+
}
244249
this._updateDraftState();
245250
}));
246251

@@ -364,13 +369,15 @@ class NewChatWidget extends Disposable implements IHistoryNavigationWidget {
364369
// Wire pickers to the new session and disconnect inactive ones
365370
const target = this._targetPicker.selectedTarget;
366371
if (target === AgentSessionProviders.Background) {
367-
this._isolationModePicker.setNewSession(session);
368-
this._branchPicker.setNewSession(session);
369-
this._repoPicker.setNewSession(undefined);
372+
session.setIsolationMode(this._isolationModePicker.isolationMode);
373+
if (this._branchPicker.selectedBranch) {
374+
session.setBranch(this._branchPicker.selectedBranch);
375+
}
370376
} else {
371-
this._isolationModePicker.setNewSession(undefined);
372-
this._branchPicker.setNewSession(undefined);
373-
this._repoPicker.setNewSession(session);
377+
const selectedRepo = this._repoPicker.selectedRepo;
378+
if (selectedRepo) {
379+
session.setRepoUri(this._getRepoUri(selectedRepo));
380+
}
374381
}
375382

376383
// Set the current model on the session (for local sessions)
@@ -605,16 +612,20 @@ class NewChatWidget extends Disposable implements IHistoryNavigationWidget {
605612
// For cloud targets, use the repo picker's selection
606613
const selectedRepo = this._repoPicker.selectedRepo;
607614
if (selectedRepo && selectedRepo.includes('/')) {
608-
return URI.from({
609-
scheme: GITHUB_REMOTE_FILE_SCHEME,
610-
authority: 'github',
611-
path: `/${selectedRepo}/HEAD`,
612-
});
615+
return this._getRepoUri(selectedRepo);
613616
}
614617

615618
return undefined;
616619
}
617620

621+
private _getRepoUri(repoId: string): URI {
622+
return URI.from({
623+
scheme: GITHUB_REMOTE_FILE_SCHEME,
624+
authority: 'github',
625+
path: `/${repoId}/HEAD`,
626+
});
627+
}
628+
618629
private _createBottomToolbar(container: HTMLElement): void {
619630
const toolbar = dom.append(container, dom.$('.sessions-chat-toolbar'));
620631

@@ -1114,8 +1125,24 @@ class NewChatWidget extends Disposable implements IHistoryNavigationWidget {
11141125
}
11151126

11161127
private _clearDraftState(): void {
1117-
this._draftState = undefined;
1118-
this.storageService.remove(STORAGE_KEY_DRAFT_STATE, StorageScope.WORKSPACE);
1128+
// Preserve picker preferences so they survive widget recreation
1129+
const target = this._targetPicker.selectedTarget;
1130+
const isLocal = target === AgentSessionProviders.Background;
1131+
const preserved: IDraftState = {
1132+
inputText: '',
1133+
attachments: [],
1134+
mode: { id: ChatModeKind.Agent, kind: ChatModeKind.Agent },
1135+
selectedModel: this._draftState?.selectedModel,
1136+
selections: [],
1137+
contrib: {},
1138+
target,
1139+
isolationMode: isLocal ? this._isolationModePicker.isolationMode : undefined,
1140+
branch: isLocal ? this._branchPicker.selectedBranch : undefined,
1141+
folderUri: isLocal ? this._folderPicker.selectedFolderUri?.toString() : undefined,
1142+
repo: isLocal ? undefined : this._repoPicker.selectedRepo,
1143+
};
1144+
this._draftState = preserved;
1145+
this.storageService.store(STORAGE_KEY_DRAFT_STATE, JSON.stringify(preserved), StorageScope.WORKSPACE, StorageTarget.MACHINE);
11191146
}
11201147

11211148
saveState(): void {

‎src/vs/sessions/contrib/chat/browser/repoPicker.ts‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import { ActionListItemKind, IActionListDelegate, IActionListItem } from '../../
1313
import { IStorageService, StorageScope, StorageTarget } from '../../../../platform/storage/common/storage.js';
1414
import { renderIcon } from '../../../../base/browser/ui/iconLabel/iconLabels.js';
1515
import { ICommandService } from '../../../../platform/commands/common/commands.js';
16-
import { INewSession } from './newSession.js';
17-
import { URI } from '../../../../base/common/uri.js';
18-
import { GITHUB_REMOTE_FILE_SCHEME } from '../../fileTreeView/browser/githubFileSystemProvider.js';
1916

2017
const OPEN_REPO_COMMAND = 'github.copilot.chat.cloudSessions.openRepository';
2118
const STORAGE_KEY_LAST_REPO = 'agentSessions.lastPickedRepo';
@@ -42,9 +39,7 @@ export class RepoPicker extends Disposable {
4239

4340
private _triggerElement: HTMLElement | undefined;
4441
private readonly _renderDisposables = this._register(new DisposableStore());
45-
private _browseGeneration = 0;
4642

47-
private _newSession: INewSession | undefined;
4843
private _selectedRepo: IRepoItem | undefined;
4944
private _recentlyPickedRepos: IRepoItem[] = [];
5045

@@ -76,18 +71,6 @@ export class RepoPicker extends Disposable {
7671
} catch { /* ignore */ }
7772
}
7873

79-
/**
80-
* Sets the pending session that this picker writes to.
81-
* If a repository is already selected, notifies the session.
82-
*/
83-
setNewSession(session: INewSession | undefined): void {
84-
this._newSession = session;
85-
this._browseGeneration++;
86-
if (session && this._selectedRepo) {
87-
this._setRepo(this._selectedRepo);
88-
}
89-
}
90-
9174
/**
9275
* Renders the repo picker trigger button into the given container.
9376
* Returns the container element.
@@ -180,15 +163,13 @@ export class RepoPicker extends Disposable {
180163
this._addToRecentlyPicked(item);
181164
this.storageService.store(STORAGE_KEY_LAST_REPO, JSON.stringify(item), StorageScope.PROFILE, StorageTarget.MACHINE);
182165
this._updateTriggerLabel();
183-
this._setRepo(item);
184166
this._onDidSelectRepo.fire(item.id);
185167
}
186168

187169
private async _browseForRepo(): Promise<void> {
188-
const generation = this._browseGeneration;
189170
try {
190171
const result: string | undefined = await this.commandService.executeCommand(OPEN_REPO_COMMAND);
191-
if (result && generation === this._browseGeneration) {
172+
if (result) {
192173
this._selectRepo({ id: result, name: result });
193174
}
194175
} catch {
@@ -270,8 +251,4 @@ export class RepoPicker extends Disposable {
270251
dom.append(this._triggerElement, renderIcon(Codicon.chevronDown));
271252
}
272253

273-
private _setRepo(repo: IRepoItem): void {
274-
this._newSession?.setRepoUri(URI.parse(`${GITHUB_REMOTE_FILE_SCHEME}://github/${repo.id}/HEAD`));
275-
}
276-
277254
}

‎src/vs/sessions/contrib/chat/browser/sessionTargetPicker.ts‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import { IActionWidgetService } from '../../../../platform/actionWidget/browser/
1414
import { ActionListItemKind, IActionListDelegate, IActionListItem } from '../../../../platform/actionWidget/browser/actionList.js';
1515
import { AgentSessionProviders } from '../../../../workbench/contrib/chat/browser/agentSessions/agentSessions.js';
1616
import { IGitRepository } from '../../../../workbench/contrib/git/common/gitService.js';
17-
import { INewSession } from './newSession.js';
1817

1918
// #region --- Session Target Picker ---
2019

@@ -138,7 +137,6 @@ export class IsolationModePicker extends Disposable {
138137

139138
private _isolationMode: IsolationMode = 'worktree';
140139
private _preferredIsolationMode: IsolationMode | undefined;
141-
private _newSession: INewSession | undefined;
142140
private _repository: IGitRepository | undefined;
143141

144142
private readonly _onDidChange = this._register(new Emitter<IsolationMode>());
@@ -158,13 +156,6 @@ export class IsolationModePicker extends Disposable {
158156
super();
159157
}
160158

161-
/**
162-
* Sets the pending session that this picker writes to.
163-
*/
164-
setNewSession(session: INewSession | undefined): void {
165-
this._newSession = session;
166-
}
167-
168159
/**
169160
* Sets the git repository. When undefined, worktree option is hidden
170161
* and isolation mode falls back to 'workspace'.
@@ -174,8 +165,9 @@ export class IsolationModePicker extends Disposable {
174165
if (repository) {
175166
const preferred = this._preferredIsolationMode;
176167
this._preferredIsolationMode = undefined;
177-
this._setMode(preferred ?? 'worktree');
168+
this._setMode(preferred ?? this._isolationMode);
178169
} else if (this._isolationMode === 'worktree') {
170+
this._preferredIsolationMode ??= this._isolationMode;
179171
this._setMode('workspace');
180172
}
181173
this._updateTriggerLabel();
@@ -280,7 +272,6 @@ export class IsolationModePicker extends Disposable {
280272
private _setMode(mode: IsolationMode): void {
281273
if (this._isolationMode !== mode) {
282274
this._isolationMode = mode;
283-
this._newSession?.setIsolationMode(mode);
284275
this._onDidChange.fire(mode);
285276
this._updateTriggerLabel();
286277
}

0 commit comments

Comments
 (0)