-
Notifications
You must be signed in to change notification settings - Fork 40.4k
Expand file tree
/
Copy pathcopilotCLIChatSessions.ts
More file actions
1686 lines (1491 loc) · 73.5 KB
/
copilotCLIChatSessions.ts
File metadata and controls
1686 lines (1491 loc) · 73.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import type { Attachment, SessionOptions, SweCustomAgent } from '@github/copilot/sdk';
import * as l10n from '@vscode/l10n';
import * as vscode from 'vscode';
import { ChatExtendedRequestHandler, ChatRequestTurn2, Uri } from 'vscode';
import { IRunCommandExecutionService } from '../../../platform/commands/common/runCommandExecutionService';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { INativeEnvService } from '../../../platform/env/common/envService';
import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
import { IGitService } from '../../../platform/git/common/gitService';
import { toGitUri } from '../../../platform/git/common/utils';
import { ILogService } from '../../../platform/log/common/logService';
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { isUri } from '../../../util/common/types';
import { DeferredPromise, IntervalTimer } from '../../../util/vs/base/common/async';
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
import { isCancellationError } from '../../../util/vs/base/common/errors';
import { Emitter, Event } from '../../../util/vs/base/common/event';
import { Disposable, DisposableStore, IDisposable, IReference } from '../../../util/vs/base/common/lifecycle';
import { ResourceMap } from '../../../util/vs/base/common/map';
import { relative } from '../../../util/vs/base/common/path';
import { basename, dirname, extUri } from '../../../util/vs/base/common/resources';
import { StopWatch } from '../../../util/vs/base/common/stopwatch';
import { hasKey } from '../../../util/vs/base/common/types';
import { EXTENSION_ID } from '../../common/constants';
import { GitBranchNameGenerator } from '../../prompt/node/gitBranch';
import { IChatSessionMetadataStore, RepositoryProperties } from '../common/chatSessionMetadataStore';
import { IChatSessionWorkspaceFolderService } from '../common/chatSessionWorkspaceFolderService';
import { IChatSessionWorktreeService } from '../common/chatSessionWorktreeService';
import { IChatFolderMruService, IFolderRepositoryManager, IsolationMode } from '../common/folderRepositoryManager';
import { getWorkingDirectory, IWorkspaceInfo } from '../common/workspaceInfo';
import { ICustomSessionTitleService } from '../copilotcli/common/customSessionTitleService';
import { IChatDelegationSummaryService } from '../copilotcli/common/delegationSummaryService';
import { SessionIdForCLI } from '../copilotcli/common/utils';
import { getCopilotCLISessionDir } from '../copilotcli/node/cliHelpers';
import { ICopilotCLISDK } from '../copilotcli/node/copilotCli';
import { CopilotCLIPromptResolver } from '../copilotcli/node/copilotcliPromptResolver';
import { builtinSlashSCommands, CopilotCLICommand, copilotCLICommands, ICopilotCLISession } from '../copilotcli/node/copilotcliSession';
import { ICopilotCLISessionItem, ICopilotCLISessionService } from '../copilotcli/node/copilotcliSessionService';
import { buildMcpServerMappings } from '../copilotcli/node/mcpHandler';
import { ICopilotCLISessionTracker } from '../copilotcli/vscode-node/copilotCLISessionTracker';
import { ICopilotCLIChatSessionInitializer, SessionInitOptions } from './copilotCLIChatSessionInitializer';
import { convertReferenceToVariable } from './copilotCLIPromptReferences';
import { ICopilotCLITerminalIntegration, TerminalOpenLocation } from './copilotCLITerminalIntegration';
import { CopilotCloudSessionsProvider } from './copilotCloudSessionsProvider';
import { UNTRUSTED_FOLDER_MESSAGE } from './folderRepositoryManagerImpl';
import { IPullRequestDetectionService } from './pullRequestDetectionService';
import { getSelectedSessionOptions, ISessionOptionGroupBuilder, OPEN_REPOSITORY_COMMAND_ID, toRepositoryOptionItem, toWorkspaceFolderOptionItem } from './sessionOptionGroupBuilder';
import { ISessionRequestLifecycle } from './sessionRequestLifecycle';
/**
* ODO:
* 3. Verify all command handlers do the exact same thing
* 6. Is chatSessionContext?.initialSessionOptions still valid with new API
* 7. Validated selected MRU item
* 8. We shouldn't have to pass model information into CLISession class, and then update sdk with the model info. Instead when we call get/create session, we should be able to pass the model info there and update the SDK session accordingly.
* This makes it unnecessary to pass model information.
* 2. Behavioral Change: trusted flag no longer unlocks dropdowns on trust failure
In the old code, when sessionResult.trusted === false, there was a call to this.unlockRepoOptionForSession(context, token) to reset dropdown selections. The new code at copilotCLIChatSessions.ts:634 simply returns {} without any dropdown reset. However, lockRepoOptionForSession and unlockRepoOptionForSession were already dead code (commented out), so this is actually correct — removing a no-op.
*
* Cases to cover:
* 1. Hook up the dropdowns for empty workspace folders as well
* 2. In mult-root workspace we need to display workspace/worktree dropdown along with the repo dropdown
* 3. Temporarily lock/unlock dropdowns while creating session
* 4. Lock dropdowns when opening an existing session
* 5. Browse folders command in empty workspaces
* 6. Branch dropdown should only be displayed when we select a folder/repo thats a git repo.
*
* Test:
* 1. All of the above
* 2. Forking sessions
* 3. Steering messages
* 4. Queued messages
* 5. Selecting a new folder in browse folders command should end up with that folder in the dropdown.
* 6. Delegate from CLI to Cloud
* 7. Delegate from Local to CLI
*/
export interface ICopilotCLIChatSessionItemProvider extends IDisposable {
refreshSession(refreshOptions: { reason: 'update'; sessionId: string } | { reason: 'update'; sessionIds: string[] } | { reason: 'delete'; sessionId: string }): Promise<void>;
}
const OPEN_IN_COPILOT_CLI_COMMAND_ID = 'github.copilot.cli.openInCopilotCLI';
const CHECK_FOR_STEERING_DELAY = 100; // ms
const _invalidCopilotCLISessionIdsWithErrorMessage = new Map<string, string>();
// Re-export for backward compatibility
export { resolveBranchLockState, resolveBranchSelection, resolveIsolationSelection } from './sessionOptionGroupBuilder';
/**
* Escape XML special characters
*/
function escapeXml(text: string): string {
return text
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
function getIssueRuntimeInfo(): { readonly platform: string; readonly vscodeInfo: string; readonly extensionVersion: string } {
const extensionVersion = vscode.extensions.getExtension(EXTENSION_ID)?.packageJSON?.version;
return {
platform: `${process.platform}-${process.arch}`,
vscodeInfo: `${vscode.env.appName} ${vscode.version}`,
extensionVersion: extensionVersion ?? 'unknown'
};
}
function getSessionLoadFailureIssueInfo(invalidSessionMessage: string): { readonly issueBody: string; readonly issueUrl: string } {
const runtimeInfo = getIssueRuntimeInfo();
const issueTitle = '[Copilot CLI] Failed to load chat session';
const issueBody = `## Description\n\nFailed to load a Copilot CLI chat session.\n\n## Environment\n\n- Platform: ${runtimeInfo.platform}\n- VS Code: ${runtimeInfo.vscodeInfo}\n- Chat Extension Version: ${runtimeInfo.extensionVersion}\n\n## Error\n\n\`\`\`\n${invalidSessionMessage}\n\`\`\``;
const issueUrl = `https://github.com/microsoft/vscode/issues/new?title=${encodeURIComponent(issueTitle)}&body=${encodeURIComponent(issueBody)}`;
return { issueBody, issueUrl };
}
/**
* Resolves candidate session directories for a CLI terminal, ordered by
* terminal affinity.
*
* Sessions whose owning terminal matches `terminal` are returned first so the
* link provider's file-existence probing hits the correct session-state dir
* before unrelated ones. Unrelated sessions are still included at the tail
* because a new session may not have registered its terminal yet (session IDs
* arrive later via MCP?).
*/
export async function resolveSessionDirsForTerminal(
sessionTracker: ICopilotCLISessionTracker,
terminal: vscode.Terminal,
): Promise<Uri[]> {
const activeIds = sessionTracker.getSessionIds();
const matching: Uri[] = [];
const rest: Uri[] = [];
for (const id of activeIds) {
const sessionTerminal = await sessionTracker.getTerminal(id);
const dir = Uri.file(getCopilotCLISessionDir(id));
if (sessionTerminal === terminal) {
matching.push(dir);
} else {
rest.push(dir);
}
}
return [...matching, ...rest];
}
export class CopilotCLIChatSessionContentProvider extends Disposable implements vscode.ChatSessionContentProvider, ICopilotCLIChatSessionItemProvider {
private readonly _onDidCommitChatSessionItem = this._register(new Emitter<{ original: vscode.ChatSessionItem; modified: vscode.ChatSessionItem }>());
public readonly onDidCommitChatSessionItem: Event<{ original: vscode.ChatSessionItem; modified: vscode.ChatSessionItem }> = this._onDidCommitChatSessionItem.event;
private readonly controller: vscode.ChatSessionItemController;
private readonly newSessions = new ResourceMap<vscode.ChatSessionItem>();
constructor(
@ICopilotCLISessionService private readonly sessionService: ICopilotCLISessionService,
@IChatSessionWorktreeService private readonly copilotCLIWorktreeManagerService: IChatSessionWorktreeService,
@IFolderRepositoryManager private readonly folderRepositoryManager: IFolderRepositoryManager,
@IConfigurationService private readonly configurationService: IConfigurationService,
@ICustomSessionTitleService private readonly customSessionTitleService: ICustomSessionTitleService,
@IRunCommandExecutionService private readonly commandExecutionService: IRunCommandExecutionService,
@ILogService private readonly logService: ILogService,
@IPullRequestDetectionService private readonly _prDetectionService: IPullRequestDetectionService,
@ISessionOptionGroupBuilder private readonly _optionGroupBuilder: ISessionOptionGroupBuilder,
@IGitService private readonly _gitService: IGitService,
@IChatSessionWorkspaceFolderService private readonly _workspaceFolderService: IChatSessionWorkspaceFolderService,
@IChatSessionMetadataStore private readonly _metadataStore: IChatSessionMetadataStore,
@IWorkspaceService private readonly _workspaceService: IWorkspaceService,
) {
super();
let isRefreshing = false;
const controller = this.controller = this._register(vscode.chat.createChatSessionItemController(
'copilotcli',
async () => {
if (isRefreshing) {
return;
}
isRefreshing = true;
try {
const sessions = await this.sessionService.getAllSessions(CancellationToken.None);
const items = await Promise.all(sessions.map(async session => this.toChatSessionItem(session)));
const count = items.length;
void this.commandExecutionService.executeCommand('setContext', 'github.copilot.chat.cliSessionsEmpty', count === 0);
controller.items.replace(items);
} finally {
isRefreshing = false;
}
}
));
controller.newChatSessionItemHandler = async (context) => {
const sessionId = this.sessionService.createNewSessionId();
const resource = SessionIdForCLI.getResource(sessionId);
const session = controller.createChatSessionItem(resource, context.request.prompt ?? context.request.command ?? '');
this.customSessionTitleService.generateSessionTitle(sessionId, context.request, CancellationToken.None)
.then(() => {
// Given we're done generating a title, refresh the contents of this session so that the new title is picked up.
if (this.controller.items.get(resource)) {
this.refreshSession({ reason: 'update', sessionId }).catch(() => { /* expected if session was deleted */ });
}
})
.catch(ex => this.logService.error(ex, 'Failed to generate custom session title'));
controller.items.add(session);
this.newSessions.set(resource, session);
return session;
};
if (this.configurationService.getConfig(ConfigKey.Advanced.CLIForkSessionsEnabled)) {
controller.forkHandler = async (sessionResource: Uri, request: ChatRequestTurn2 | undefined, token: vscode.CancellationToken) => {
const sessionId = SessionIdForCLI.parse(sessionResource);
const folderInfo = await this.folderRepositoryManager.getFolderRepository(sessionId, undefined, token);
const forkedSessionId = await this.sessionService.forkSession({ sessionId, requestId: request?.id, workspace: folderInfo }, token);
const item = await this.sessionService.getSessionItem(forkedSessionId, token);
if (!item) {
throw new Error(`Failed to get session item for forked session ${forkedSessionId}`);
}
return this.toChatSessionItem(item);
};
}
this._register(this.sessionService.onDidDeleteSession(async (e) => {
controller.items.delete(SessionIdForCLI.getResource(e));
}));
this._register(this.sessionService.onDidChangeSession(async (e) => {
const item = await this.toChatSessionItem(e);
controller.items.add(item);
}));
this._register(this.sessionService.onDidCreateSession(async (e) => {
const resource = SessionIdForCLI.getResource(e.id);
if (controller.items.get(resource)) {
return;
}
const item = await this.toChatSessionItem(e);
controller.items.add(item);
}));
// Handle worktree cleanup/recreation when archive state changes
if (controller.onDidChangeChatSessionItemState) {
this._register(controller.onDidChangeChatSessionItemState(async (item) => {
const sessionId = SessionIdForCLI.parse(item.resource);
if (item.archived) {
try {
const result = await this.copilotCLIWorktreeManagerService.cleanupWorktreeOnArchive(sessionId);
this.logService.trace(`[CopilotCLI] Worktree cleanup for session ${sessionId}: ${result.cleaned ? 'cleaned' : result.reason}`);
} catch (error) {
this.logService.error(`[CopilotCLI] Failed to cleanup worktree for archived session ${sessionId}:`, error);
}
} else {
try {
const result = await this.copilotCLIWorktreeManagerService.recreateWorktreeOnUnarchive(sessionId);
this.logService.trace(`[CopilotCLI] Worktree recreation for session ${sessionId}: ${result.recreated ? 'recreated' : result.reason}`);
if (result.recreated) {
await this.refreshSession({ reason: 'update', sessionId });
}
} catch (error) {
this.logService.error(`[CopilotCLI] Failed to recreate worktree for unarchived session ${sessionId}:`, error);
}
}
}));
}
const newInputStates: WeakRef<vscode.ChatSessionInputState>[] = [];
controller.getChatSessionInputState = async (sessionResource, context, token) => {
const isExistingSession = sessionResource && !this.sessionService.isNewSessionId(SessionIdForCLI.parse(sessionResource));
if (isExistingSession) {
const groups = await this._optionGroupBuilder.buildExistingSessionInputStateGroups(sessionResource, token);
return controller.createChatSessionInputState(groups);
} else {
const groups = await this._optionGroupBuilder.provideChatSessionProviderOptionGroups(context.previousInputState);
const state = controller.createChatSessionInputState(groups);
// Only wire dynamic updates for new sessions (existing sessions are fully locked).
// Note: don't use the getChatSessionInputState token here — it's a one-shot token
// that may be disposed by the time the user interacts with the dropdowns.
newInputStates.push(new WeakRef(state));
state.onDidChange(() => {
void this._optionGroupBuilder.handleInputStateChange(state);
});
return state;
}
};
// Refresh new-session dropdown groups when git or workspace state changes
// (e.g. after git init, opening a repo, or adding/removing workspace folders).
const refreshActiveInputState = () => {
// Sweep stale WeakRefs before iterating
for (let i = newInputStates.length - 1; i >= 0; i--) {
if (!newInputStates[i].deref()) {
newInputStates.splice(i, 1);
}
}
for (const weakRef of newInputStates) {
const state = weakRef.deref();
if (state) {
void this._optionGroupBuilder.rebuildInputState(state);
}
}
};
this._register(this._gitService.onDidFinishInitialization(refreshActiveInputState));
this._register(this._gitService.onDidOpenRepository(refreshActiveInputState));
this._register(this._gitService.onDidCloseRepository(refreshActiveInputState));
this._register(this._workspaceService.onDidChangeWorkspaceFolders(refreshActiveInputState));
}
public async updateInputStateAfterFolderSelection(inputState: vscode.ChatSessionInputState, folderUri: vscode.Uri): Promise<void> {
this._optionGroupBuilder.setNewFolderForInputState(inputState, folderUri);
await this._optionGroupBuilder.rebuildInputState(inputState, folderUri);
}
public async refreshSession(refreshOptions: { reason: 'update'; sessionId: string } | { reason: 'update'; sessionIds: string[] } | { reason: 'delete'; sessionId: string }): Promise<void> {
if (refreshOptions.reason === 'delete') {
const uri = SessionIdForCLI.getResource(refreshOptions.sessionId);
this.controller.items.delete(uri);
} else if (refreshOptions.reason === 'update' && hasKey(refreshOptions, { 'sessionIds': true })) {
await Promise.allSettled(refreshOptions.sessionIds.map(async sessionId => {
const item = await this.sessionService.getSessionItem(sessionId, CancellationToken.None);
if (item) {
const chatSessionItem = await this.toChatSessionItem(item);
this.controller.items.add(chatSessionItem);
}
}));
} else {
const item = await this.sessionService.getSessionItem(refreshOptions.sessionId, CancellationToken.None);
if (item) {
const chatSessionItem = await this.toChatSessionItem(item);
this.controller.items.add(chatSessionItem);
}
}
}
public async toChatSessionItem(session: ICopilotCLISessionItem): Promise<vscode.ChatSessionItem> {
const resource = SessionIdForCLI.getResource(session.id);
const item = this.controller.createChatSessionItem(resource, session.label);
const worktreeProperties = await this.copilotCLIWorktreeManagerService.getWorktreeProperties(session.id);
const workingDirectory = worktreeProperties?.worktreePath ? vscode.Uri.file(worktreeProperties.worktreePath)
: session.workingDirectory;
item.timing = session.timing;
item.status = session.status ?? vscode.ChatSessionStatus.Completed;
const [badge, changes, metadata] = await Promise.all([
this.buildBadge(worktreeProperties, workingDirectory),
this.buildChanges(session.id, worktreeProperties, workingDirectory),
this.buildMetadata(session.id, worktreeProperties, workingDirectory),
]);
item.badge = badge;
item.changes = changes;
item.metadata = metadata;
return item;
}
private async buildBadge(
worktreeProperties: Awaited<ReturnType<IChatSessionWorktreeService['getWorktreeProperties']>>,
workingDirectory: vscode.Uri | undefined,
): Promise<vscode.MarkdownString | undefined> {
const repositories = this._gitService.repositories.filter(r => r.kind !== 'worktree');
const shouldShow = vscode.workspace.workspaceFolders === undefined ||
vscode.workspace.isAgentSessionsWorkspace ||
repositories.length > 1;
if (!shouldShow) {
return undefined;
}
const badgeUri = worktreeProperties?.repositoryPath
? vscode.Uri.file(worktreeProperties.repositoryPath)
: workingDirectory;
if (!badgeUri) {
return undefined;
}
const isTrusted = await vscode.workspace.isResourceTrusted(badgeUri);
const isRepo = !!worktreeProperties?.repositoryPath;
const icon = isTrusted ? (isRepo ? '$(repo)' : '$(folder)') : '$(workspace-untrusted)';
const badge = new vscode.MarkdownString(`${icon} ${basename(badgeUri)}`);
badge.supportThemeIcons = true;
return badge;
}
private async buildChanges(
sessionId: string,
worktreeProperties: Awaited<ReturnType<IChatSessionWorktreeService['getWorktreeProperties']>>,
workingDirectory: vscode.Uri | undefined,
): Promise<vscode.ChatSessionChangedFile2[]> {
const changes: vscode.ChatSessionChangedFile2[] = [];
if (worktreeProperties?.repositoryPath && await vscode.workspace.isResourceTrusted(vscode.Uri.file(worktreeProperties.repositoryPath))) {
changes.push(...(await this.copilotCLIWorktreeManagerService.getWorktreeChanges(sessionId) ?? []));
} else if (workingDirectory && await vscode.workspace.isResourceTrusted(workingDirectory)) {
const workspaceChanges = await this._workspaceFolderService.getWorkspaceChanges(sessionId) ?? [];
changes.push(...workspaceChanges.map(change => new vscode.ChatSessionChangedFile2(
vscode.Uri.file(change.filePath),
change.originalFilePath
? toGitUri(vscode.Uri.file(change.originalFilePath), 'HEAD')
: undefined,
change.modifiedFilePath
? toGitUri(vscode.Uri.file(change.modifiedFilePath), '')
: undefined,
change.statistics.additions,
change.statistics.deletions)));
}
return changes;
}
private async buildMetadata(
sessionId: string,
worktreeProperties: Awaited<ReturnType<IChatSessionWorktreeService['getWorktreeProperties']>>,
workingDirectory: vscode.Uri | undefined,
): Promise<{ readonly [key: string]: unknown }> {
if (worktreeProperties) {
return {
autoCommit: worktreeProperties.autoCommit !== false,
baseCommit: worktreeProperties?.baseCommit,
baseBranchName: worktreeProperties.version === 2
? worktreeProperties.baseBranchName
: undefined,
baseBranchProtected: worktreeProperties.version === 2
? worktreeProperties.baseBranchProtected === true
: undefined,
branchName: worktreeProperties?.branchName,
isolationMode: IsolationMode.Worktree,
repositoryPath: worktreeProperties?.repositoryPath,
worktreePath: worktreeProperties?.worktreePath,
pullRequestUrl: worktreeProperties.version === 2
? worktreeProperties.pullRequestUrl
: undefined,
pullRequestState: worktreeProperties.version === 2
? worktreeProperties.pullRequestState
: undefined,
firstCheckpointRef: worktreeProperties.version === 2
? worktreeProperties.firstCheckpointRef
: undefined,
baseCheckpointRef: worktreeProperties.version === 2
? worktreeProperties.baseCheckpointRef
: undefined,
lastCheckpointRef: worktreeProperties.version === 2
? worktreeProperties.lastCheckpointRef
: undefined
} satisfies { readonly [key: string]: unknown };
}
const [sessionRequestDetails, repositoryProperties] = await Promise.all([
this._metadataStore.getRequestDetails(sessionId),
this._metadataStore.getRepositoryProperties(sessionId)
]);
let lastCheckpointRef: string | undefined;
for (let i = sessionRequestDetails.length - 1; i >= 0; i--) {
const checkpointRef = sessionRequestDetails[i]?.checkpointRef;
if (checkpointRef !== undefined) {
lastCheckpointRef = checkpointRef;
break;
}
}
const firstCheckpointRef = lastCheckpointRef
? `${lastCheckpointRef.slice(0, lastCheckpointRef.lastIndexOf('/'))}/0`
: undefined;
return {
isolationMode: IsolationMode.Workspace,
repositoryPath: repositoryProperties?.repositoryPath,
branchName: repositoryProperties?.branchName,
baseBranchName: repositoryProperties?.baseBranchName,
workingDirectoryPath: workingDirectory?.fsPath,
firstCheckpointRef,
lastCheckpointRef
} satisfies { readonly [key: string]: unknown };
}
async provideChatSessionContent(resource: Uri, token: vscode.CancellationToken, _context?: { readonly inputState: vscode.ChatSessionInputState; readonly sessionOptions: ReadonlyArray<{ optionId: string; value: string | vscode.ChatSessionProviderOptionItem }> }): Promise<vscode.ChatSession> {
const stopwatch = new StopWatch();
try {
const copilotcliSessionId = SessionIdForCLI.parse(resource);
if (copilotcliSessionId.startsWith('untitled:') || copilotcliSessionId.startsWith('untitled-')) {
return {
history: [],
requestHandler: undefined,
};
}
if (this.sessionService.isNewSessionId(copilotcliSessionId)) {
const session = this.newSessions.get(resource);
if (!session) {
throw new Error('Session not found');
}
return {
history: [],
requestHandler: undefined,
title: session.label,
activeResponseCallback: undefined,
};
} else {
this.newSessions.delete(resource);
return await this.provideChatSessionContentForExistingSession(resource, token);
}
} finally {
this.logService.info(`[CopilotCLIChatSessionContentProvider] provideChatSessionContent for ${resource.toString()} took ${stopwatch.elapsed()}ms`);
}
}
private async provideChatSessionContentForExistingSession(resource: Uri, token: vscode.CancellationToken): Promise<vscode.ChatSession> {
const copilotcliSessionId = SessionIdForCLI.parse(resource);
// Fire-and-forget: detect PR when the user opens a session.
this._prDetectionService.detectPullRequest(copilotcliSessionId);
const folderRepo = await this.folderRepositoryManager.getFolderRepository(copilotcliSessionId, undefined, token);
const [history, title, optionGroups] = await Promise.all([
this.getSessionHistory(copilotcliSessionId, folderRepo, token),
this.customSessionTitleService.getCustomSessionTitle(copilotcliSessionId),
this._optionGroupBuilder.buildExistingSessionInputStateGroups(resource, token),
]);
const options: Record<string, string | vscode.ChatSessionProviderOptionItem> = {};
for (const group of optionGroups) {
if (group.selected) {
options[group.id] = { ...group.selected, locked: true };
}
}
return {
title,
history,
options,
requestHandler: undefined,
};
}
private async getSessionHistory(sessionId: string, workspaceInfo: IWorkspaceInfo, token: vscode.CancellationToken) {
try {
_invalidCopilotCLISessionIdsWithErrorMessage.delete(sessionId);
const history = await this.sessionService.getChatHistory({ sessionId, workspace: workspaceInfo }, token);
return history;
} catch (error) {
if (!isUnknownEventTypeError(error)) {
throw error;
}
const partialHistory = await this.sessionService.tryGetPartialSesionHistory(sessionId);
if (partialHistory) {
_invalidCopilotCLISessionIdsWithErrorMessage.set(sessionId, error.message || String(error));
return partialHistory;
}
throw error;
}
}
}
export class CopilotCLIChatSessionParticipant extends Disposable {
constructor(
private readonly sessionItemProvider: ICopilotCLIChatSessionItemProvider,
private readonly promptResolver: CopilotCLIPromptResolver,
private readonly cloudSessionProvider: CopilotCloudSessionsProvider | undefined,
private readonly branchNameGenerator: GitBranchNameGenerator | undefined,
@IGitService private readonly gitService: IGitService,
@ICopilotCLISessionService private readonly sessionService: ICopilotCLISessionService,
@IChatSessionWorktreeService private readonly copilotCLIWorktreeManagerService: IChatSessionWorktreeService,
@ITelemetryService private readonly telemetryService: ITelemetryService,
@ILogService private readonly logService: ILogService,
@IChatDelegationSummaryService private readonly chatDelegationSummaryService: IChatDelegationSummaryService,
@IConfigurationService private readonly configurationService: IConfigurationService,
@ICopilotCLISDK private readonly copilotCLISDK: ICopilotCLISDK,
@ICopilotCLIChatSessionInitializer private readonly sessionInitializer: ICopilotCLIChatSessionInitializer,
@ISessionRequestLifecycle private readonly sessionRequestLifecycle: ISessionRequestLifecycle,
@IPullRequestDetectionService private readonly prDetectionService: IPullRequestDetectionService,
) {
super();
this._register(this.prDetectionService.onDidDetectPullRequest(sessionId => {
this.sessionItemProvider.refreshSession({ reason: 'update', sessionId }).catch(error => this.logService.error(error, 'Failed to refresh session after PR detection'));
}));
}
createHandler(): ChatExtendedRequestHandler {
return this.handleRequest.bind(this);
}
private readonly contextForRequest = new Map<string, { prompt: string; attachments: Attachment[]; agent?: string }>();
/**
* Outer request handler that supports *yielding* for session steering.
*
* ## How steering works end-to-end
*
* 1. The user sends a message while the session is already processing a
* previous request (status is `InProgress` or `NeedsInput`).
* 2. VS Code signals this by setting `context.yieldRequested = true` on the
* *previous* request's context object.
* 3. This handler polls `context.yieldRequested` every 100 ms. Once detected
* the outer `Promise.race` resolves, returning control to VS Code so it
* can dispatch the new (steering) request.
* 4. Crucially, the inner `handleRequestImpl` promise is **not** cancelled
* or disposed – the original SDK session continues running in the
* background.
* 5. When the new request arrives, `handleRequest` on the underlying
* {@link CopilotCLISession} detects the session is still busy and routes
* through `_handleRequestSteering`, which sends the new prompt with
* `mode: 'immediate'` and waits for both the steering send and the
* original request to complete.
*/
private async handleRequest(request: vscode.ChatRequest, context: vscode.ChatContext, stream: vscode.ChatResponseStream, token: vscode.CancellationToken): Promise<vscode.ChatResult | void> {
const disposables = new DisposableStore();
try {
const handled = this.handleRequestImpl(request, context, stream, token);
const interval = disposables.add(new IntervalTimer());
const yielded = new DeferredPromise<void>();
interval.cancelAndSet(() => {
if (context.yieldRequested) {
yielded.complete();
}
}, CHECK_FOR_STEERING_DELAY);
return await Promise.race([yielded.p, handled]);
} finally {
disposables.dispose();
}
}
private sendTelemetryForHandleRequest(request: vscode.ChatRequest, context: vscode.ChatContext): void {
const { chatSessionContext } = context;
const hasChatSessionItem = String(!!chatSessionContext?.chatSessionItem);
const sessionId = chatSessionContext ? SessionIdForCLI.parse(chatSessionContext.chatSessionItem.resource) : undefined;
const isUntitled = sessionId ? String(this.sessionService.isNewSessionId(sessionId)) : 'false';
const hasDelegatePrompt = String(request.command === 'delegate');
/* __GDPR__
"copilotcli.chat.invoke" : {
"owner": "joshspicer",
"comment": "Event sent when a CopilotCLI chat request is made.",
"chatRequestId": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "The unique chat request ID." },
"hasChatSessionItem": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Invoked with a chat session item." },
"isUntitled": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Indicates if the chat session is untitled." },
"hasDelegatePrompt": { "classification": "SystemMetaData", "purpose": "FeatureInsight", "comment": "Indicates if the prompt is a /delegate command." }
}
*/
this.telemetryService.sendMSFTTelemetryEvent('copilotcli.chat.invoke', {
chatRequestId: request.id,
hasChatSessionItem,
isUntitled,
hasDelegatePrompt
});
}
private async authenticate(): Promise<NonNullable<SessionOptions['authInfo']>> {
const authInfo = await this.copilotCLISDK.getAuthInfo().catch((ex) => this.logService.error(ex, 'Authorization failed'));
if (!authInfo) {
this.logService.error(`Authorization failed`);
throw new Error(vscode.l10n.t('Authorization failed. Please sign into GitHub and try again.'));
}
if ((authInfo.type === 'token' && !authInfo.token) && !this.configurationService.getConfig(ConfigKey.Shared.DebugOverrideProxyUrl)) {
this.logService.error(`Authorization failed`);
throw new Error(vscode.l10n.t('Authorization failed. Please sign into GitHub and try again.'));
}
return authInfo;
}
/**
* Resolve the input and attachments for the SDK session based on request type.
*
* The VS Code chat API creates the session before firing the request handler,
* so delegated requests pre-resolve and cache prompt/attachments in `contextForRequest`.
*/
private async resolveInput(
request: vscode.ChatRequest,
session: ICopilotCLISession,
isNewSession: boolean,
token: vscode.CancellationToken,
): Promise<{ input: { prompt: string; command?: CopilotCLICommand }; attachments: Attachment[] }> {
const contextForRequest = this.contextForRequest.get(session.sessionId);
this.contextForRequest.delete(session.sessionId);
if (contextForRequest) {
return { input: { prompt: contextForRequest.prompt }, attachments: contextForRequest.attachments };
}
if (request.command && !request.prompt && !isNewSession) {
const input = (copilotCLICommands as readonly string[]).includes(request.command)
? { command: request.command as CopilotCLICommand, prompt: '' }
: { prompt: `/${request.command}` };
return { input, attachments: [] };
}
const { prompt, attachments } = await this.promptResolver.resolvePrompt(request, undefined, [], session.workspace, [], token);
const input = (request.command && (copilotCLICommands as readonly string[]).includes(request.command))
? { command: request.command as CopilotCLICommand, prompt }
: { prompt };
return { input, attachments };
}
private async handleRequestImpl(request: vscode.ChatRequest, context: vscode.ChatContext, stream: vscode.ChatResponseStream, token: vscode.CancellationToken): Promise<vscode.ChatResult | void> {
const { chatSessionContext } = context;
const disposables = new DisposableStore();
let sdkSessionId: string | undefined = undefined;
let session: IReference<ICopilotCLISession> | undefined = undefined;
try {
this.sendTelemetryForHandleRequest(request, context);
const authInfo = await this.authenticate();
if (!chatSessionContext) {
return await this.handleDelegationFromAnotherChat(request, undefined, request.references, context, stream, authInfo, token);
}
const { resource } = chatSessionContext.chatSessionItem;
const sessionId = SessionIdForCLI.parse(resource);
const isNewSession = this.sessionService.isNewSessionId(sessionId);
const invalidSessionMessage = _invalidCopilotCLISessionIdsWithErrorMessage.get(sessionId);
if (invalidSessionMessage) {
const { issueUrl } = getSessionLoadFailureIssueInfo(invalidSessionMessage);
const warningMessage = new vscode.MarkdownString();
warningMessage.appendMarkdown(l10n.t({
message: "Failed loading this session. If this issue persists, please [report an issue]({issueUrl}). \nError: ",
args: { issueUrl },
comment: [`{Locked=']({'}`]
}));
warningMessage.appendText(invalidSessionMessage);
stream.warning(warningMessage);
return {};
}
const requestTurn = new ChatRequestTurn2(request.prompt ?? '', request.command, [], '', [], [], undefined, undefined, undefined);
const fakeContext: vscode.ChatContext = {
history: [requestTurn],
yieldRequested: false,
};
const branchNamePromise = (isNewSession && request.prompt && this.branchNameGenerator) ? this.branchNameGenerator.generateBranchName(fakeContext, token) : Promise.resolve(undefined);
const selectedOptions = getSelectedSessionOptions(chatSessionContext.inputState);
const sessionResult = await this.getOrCreateSession(request, chatSessionContext.chatSessionItem.resource, { ...selectedOptions, newBranch: branchNamePromise, stream }, disposables, token);
({ session } = sessionResult);
const { model, agent } = sessionResult;
if (!session || token.isCancellationRequested) {
return {};
}
sdkSessionId = session.object.sessionId;
await this.sessionRequestLifecycle.startRequest(sdkSessionId, request, context.history.length === 0, session.object.workspace, agent?.name ?? this.contextForRequest.get(session.object.sessionId)?.agent);
if (request.command === 'delegate') {
await this.handleDelegationToCloud(session.object, request, context, stream, token);
} else {
const { input, attachments } = await this.resolveInput(request, session.object, isNewSession, token);
await session.object.handleRequest(request, input, attachments, model, authInfo, token);
}
return {};
} catch (ex) {
if (isCancellationError(ex)) {
return {};
}
throw ex;
} finally {
if (sdkSessionId && session) {
await this.sessionRequestLifecycle.endRequest(
sdkSessionId, request,
{ status: session.object.status, workspace: session.object.workspace, createdPullRequestUrl: session.object.createdPullRequestUrl },
token,
);
this.sessionItemProvider.refreshSession({ reason: 'update', sessionId: sdkSessionId })
.catch(error => this.logService.error(error, 'Failed to refresh session item after handling request'));
}
disposables.dispose();
}
}
private async getOrCreateSession(request: vscode.ChatRequest, chatResource: vscode.Uri, options: SessionInitOptions, disposables: DisposableStore, token: vscode.CancellationToken): Promise<{ session: IReference<ICopilotCLISession> | undefined; isNewSession: boolean; model: { model: string; reasoningEffort?: string } | undefined; agent: SweCustomAgent | undefined; trusted: boolean }> {
const result = await this.sessionInitializer.getOrCreateSession(request, chatResource, options, disposables, token);
const { session, isNewSession, model, agent, trusted } = result;
if (!session || token.isCancellationRequested) {
return { session: undefined, isNewSession, model, agent, trusted };
}
if (isNewSession) {
this.sessionItemProvider.refreshSession({ reason: 'update', sessionId: session.object.sessionId });
}
return { session, isNewSession, model, agent, trusted };
}
private async handleDelegationToCloud(session: ICopilotCLISession, request: vscode.ChatRequest, context: vscode.ChatContext, stream: vscode.ChatResponseStream, token: vscode.CancellationToken) {
if (!this.cloudSessionProvider) {
stream.warning(l10n.t('No cloud agent available'));
return;
}
// Check for uncommitted changes
const worktreeProperties = await this.copilotCLIWorktreeManagerService.getWorktreeProperties(session.sessionId);
const repositoryPath = worktreeProperties?.repositoryPath ? Uri.file(worktreeProperties.repositoryPath) : getWorkingDirectory(session.workspace);
const repository = repositoryPath ? await this.gitService.getRepository(repositoryPath) : undefined;
const hasChanges = (repository?.changes?.indexChanges && repository.changes.indexChanges.length > 0);
if (hasChanges) {
stream.warning(l10n.t('You have uncommitted changes in your workspace. The cloud agent will start from the last committed state. Consider committing your changes first if you want to include them.'));
}
const prInfo = await this.cloudSessionProvider.delegate(request, stream, context, token, { prompt: request.prompt, chatContext: context });
await this.recordPushToSession(session, `/delegate ${request.prompt}`, prInfo);
}
private async handleDelegationFromAnotherChat(
request: vscode.ChatRequest,
userPrompt: string | undefined,
otherReferences: readonly vscode.ChatPromptReference[] | undefined,
context: vscode.ChatContext,
stream: vscode.ChatResponseStream,
authInfo: NonNullable<SessionOptions['authInfo']>,
token: vscode.CancellationToken
): Promise<vscode.ChatResult> {
let summary: string | undefined;
const requestPromptPromise = (async () => {
if (this.hasHistoryToSummarize(context.history)) {
stream.progress(l10n.t('Analyzing chat history'));
summary = await this.chatDelegationSummaryService.summarize(context, token);
summary = summary ? `**Summary**\n${summary}` : undefined;
}
// Give priority to userPrompt if provided (e.g., from confirmation metadata)
userPrompt = userPrompt || request.prompt;
return summary ? `${userPrompt}\n${summary}` : userPrompt;
})();
const { workspaceInfo, cancelled } = await this.sessionInitializer.initializeWorkingDirectory(undefined, { stream }, request.toolInvocationToken, token);
if (cancelled || token.isCancellationRequested) {
stream.markdown(l10n.t('Copilot CLI delegation cancelled.'));
return {};
}
const { prompt, attachments, references } = await this.promptResolver.resolvePrompt(request, await requestPromptPromise, (otherReferences || []).concat([]), workspaceInfo, [], token);
const mcpServerMappings = buildMcpServerMappings(request.tools);
const { session, model, agent } = await this.sessionInitializer.createDelegatedSession(request, workspaceInfo, { mcpServerMappings }, token);
if (summary) {
const summaryRef = await this.chatDelegationSummaryService.trackSummaryUsage(session.object.sessionId, summary);
if (summaryRef) {
references.push(summaryRef);
}
}
try {
this.contextForRequest.set(session.object.sessionId, { prompt, attachments, agent: agent?.name });
// this.sessionItemProvider.notifySessionsChange();
// TODO @DonJayamanne I don't think we need to refresh the list of session here just yet, or perhaps we do,
// Same as getOrCreate session, we need a dummy title or the initial prompt to show in the sessions list.
await vscode.commands.executeCommand('workbench.action.chat.openSessionWithPrompt.copilotcli', {
resource: SessionIdForCLI.getResource(session.object.sessionId),
prompt: userPrompt || request.prompt,
attachedContext: references.map(ref => convertReferenceToVariable(ref, attachments))
});
} catch {
this.contextForRequest.delete(session.object.sessionId);
session.object.handleRequest(request, { prompt }, attachments, model, authInfo, token)
.then(() => this.sessionRequestLifecycle.endRequest(session.object.sessionId, request, { status: session.object.status, workspace: session.object.workspace, createdPullRequestUrl: session.object.createdPullRequestUrl }, token))
.catch(error => {
this.logService.error(`Failed to handle CLI session request: ${error}`);
})
.finally(() => {
session.dispose();
});
}
stream.markdown(l10n.t('A Copilot CLI session has begun working on your request. Follow its progress in the sessions list.'));
return {};
}
private hasHistoryToSummarize(history: readonly (vscode.ChatRequestTurn | vscode.ChatResponseTurn)[]): boolean {
if (!history || history.length === 0) {
return false;
}
const allResponsesEmpty = history.every(turn => {
if (turn instanceof vscode.ChatResponseTurn) {
return turn.response.length === 0;
}
return true;
});
return !allResponsesEmpty;
}
private async recordPushToSession(
session: ICopilotCLISession,
userPrompt: string,
prInfo: vscode.ChatResponsePullRequestPart
): Promise<void> {
// Add user message event
session.addUserMessage(userPrompt);
// Add assistant message event with embedded PR metadata
const assistantMessage = `A cloud agent has begun working on your request. Follow its progress in the associated chat and pull request.\n<pr_metadata uri="${prInfo.uri?.toString()}" title="${escapeXml(prInfo.title)}" description="${escapeXml(prInfo.description)}" author="${escapeXml(prInfo.author)}" linkTag="${escapeXml(prInfo.linkTag)}"/>`;
session.addUserAssistantMessage(assistantMessage);
}
}
export function registerCLIChatCommands(
copilotCLISessionService: ICopilotCLISessionService,
copilotCLIWorktreeManagerService: IChatSessionWorktreeService,
gitService: IGitService,
copilotCliWorkspaceSession: IChatSessionWorkspaceFolderService,
contentProvider: CopilotCLIChatSessionContentProvider,
folderRepositoryManager: IFolderRepositoryManager,
copilotCLIFolderMruService: IChatFolderMruService,
envService: INativeEnvService,
fileSystemService: IFileSystemService,
sessionTracker: ICopilotCLISessionTracker,
terminalIntegration: ICopilotCLITerminalIntegration,
logService: ILogService
): IDisposable {
const disposableStore = new DisposableStore();
// Terminal integration setup: resolve session dirs for terminal links.
disposableStore.add(terminalIntegration);
terminalIntegration.setSessionDirResolver(terminal =>
resolveSessionDirsForTerminal(sessionTracker, terminal)
);
disposableStore.add(vscode.commands.registerCommand('github.copilot.cli.sessions.delete', async (sessionItem?: vscode.ChatSessionItem) => {
if (sessionItem?.resource) {
const id = SessionIdForCLI.parse(sessionItem.resource);
const worktree = await copilotCLIWorktreeManagerService.getWorktreeProperties(id);
const worktreePath = await copilotCLIWorktreeManagerService.getWorktreePath(id);
const confirmMessage = worktreePath
? l10n.t('Are you sure you want to delete the session and its associated worktree?')
: l10n.t('Are you sure you want to delete the session?');
const deleteLabel = l10n.t('Delete');
const result = await vscode.window.showWarningMessage(
confirmMessage,
{ modal: true },
deleteLabel
);
if (result === deleteLabel) {
await copilotCLISessionService.deleteSession(id);
await copilotCliWorkspaceSession.deleteTrackedWorkspaceFolder(id);
if (worktreePath) {
try {
const repository = worktree ? await gitService.getRepository(vscode.Uri.file(worktree.repositoryPath), true) : undefined;
if (!repository) {
throw new Error(l10n.t('No active repository found to delete worktree.'));
}
await gitService.deleteWorktree(repository.rootUri, worktreePath.fsPath);
} catch (error) {
vscode.window.showErrorMessage(l10n.t('Failed to delete worktree: {0}', error instanceof Error ? error.message : String(error)));
}
}
await contentProvider.refreshSession({ reason: 'delete', sessionId: id });
}
}
}));
disposableStore.add(vscode.commands.registerCommand('github.copilot.cli.sessions.resumeInTerminal', async (sessionItem?: vscode.ChatSessionItem) => {
if (sessionItem?.resource) {
const id = SessionIdForCLI.parse(sessionItem.resource);
const existingTerminal = await sessionTracker.getTerminal(id);
if (existingTerminal) {
existingTerminal.show();
return;
}
const terminalName = sessionItem.label || id;
const cliArgs = ['--resume', id];
const token = new vscode.CancellationTokenSource();
try {
const folderInfo = await folderRepositoryManager.getFolderRepository(id, undefined, token.token);
const cwd = folderInfo.worktree ?? folderInfo.repository ?? folderInfo.folder;
const terminal = await terminalIntegration.openTerminal(terminalName, cliArgs, cwd?.fsPath);
if (terminal) {
sessionTracker.setSessionTerminal(id, terminal);
terminalIntegration.setTerminalSessionDir(terminal, Uri.file(getCopilotCLISessionDir(id)));
}
} finally {
token.dispose();
}
}
}));
disposableStore.add(vscode.commands.registerCommand('github.copilot.cli.sessions.rename', async (sessionItem?: vscode.ChatSessionItem) => {
if (!sessionItem?.resource) {
return;
}
const id = SessionIdForCLI.parse(sessionItem.resource);
const newTitle = await vscode.window.showInputBox({
prompt: l10n.t('New agent session title'),
value: sessionItem.label,
validateInput: value => {
if (!value.trim()) {
return l10n.t('Title cannot be empty');
}
return undefined;
}
});
if (newTitle) {
const trimmedTitle = newTitle.trim();