-
Notifications
You must be signed in to change notification settings - Fork 39.3k
Expand file tree
/
Copy pathcopilotcliSession.ts
More file actions
1336 lines (1247 loc) · 59 KB
/
copilotcliSession.ts
File metadata and controls
1336 lines (1247 loc) · 59 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, SendOptions, Session, SessionOptions } from '@github/copilot/sdk';
import * as l10n from '@vscode/l10n';
import type * as vscode from 'vscode';
import type { ChatParticipantToolToken } from 'vscode';
import { ConfigKey, IConfigurationService } from '../../../../platform/configuration/common/configurationService';
import { ILogService } from '../../../../platform/log/common/logService';
import { GenAiMetrics } from '../../../../platform/otel/common/genAiMetrics';
import { CopilotChatAttr, GenAiAttr, GenAiOperationName, IOTelService, ISpanHandle, SpanKind, SpanStatusCode, truncateForOTel } from '../../../../platform/otel/common/index';
import { CapturingToken } from '../../../../platform/requestLogger/common/capturingToken';
import { IRequestLogger, LoggedRequestKind } from '../../../../platform/requestLogger/common/requestLogger';
import { PromptTokenCategory, PromptTokenLabel } from '../../../../platform/tokenizer/node/promptTokenDetails';
import { IWorkspaceService } from '../../../../platform/workspace/common/workspaceService';
import { raceCancellation } from '../../../../util/vs/base/common/async';
import { CancellationToken } from '../../../../util/vs/base/common/cancellation';
import { Codicon } from '../../../../util/vs/base/common/codicons';
import { Emitter } from '../../../../util/vs/base/common/event';
import { DisposableStore, IDisposable, toDisposable } from '../../../../util/vs/base/common/lifecycle';
import { extUriBiasedIgnorePathCase, isEqual } from '../../../../util/vs/base/common/resources';
import { truncate } from '../../../../util/vs/base/common/strings';
import { ThemeIcon } from '../../../../util/vs/base/common/themables';
import { IInstantiationService } from '../../../../util/vs/platform/instantiation/common/instantiation';
import { ChatResponseMarkdownPart, ChatResponseThinkingProgressPart, ChatSessionStatus, ChatToolInvocationPart, EventEmitter, Uri } from '../../../../vscodeTypes';
import { IToolsService } from '../../../tools/common/toolsService';
import { IChatSessionMetadataStore } from '../../common/chatSessionMetadataStore';
import { ExternalEditTracker } from '../../common/externalEditTracker';
import { getWorkingDirectory, isIsolationEnabled, IWorkspaceInfo } from '../../common/workspaceInfo';
import { enrichToolInvocationWithSubagentMetadata, getAffectedUrisForEditTool, isCopilotCliEditToolCall, isCopilotCLIToolThatCouldRequirePermissions, processToolExecutionComplete, processToolExecutionStart, ToolCall, updateTodoList } from '../common/copilotCLITools';
import { getCopilotCLISessionStateDir } from './cliHelpers';
import type { CopilotCliBridgeSpanProcessor } from './copilotCliBridgeSpanProcessor';
import { ICopilotCLIImageSupport } from './copilotCLIImageSupport';
import { PermissionRequest, requestPermission, requiresFileEditconfirmation } from './permissionHelpers';
import { IQuestion, IUserQuestionHandler } from './userInputHelpers';
/**
* Known commands that can be sent to a CopilotCLI session instead of a free-form prompt.
*/
export type CopilotCLICommand = 'compact' | 'plan' | 'fleet';
/**
* The set of all known CopilotCLI commands. Used by callers that need to
* distinguish a slash-command from a regular prompt at runtime.
*/
export const copilotCLICommands: readonly CopilotCLICommand[] = ['compact', 'plan', 'fleet'] as const;
export const builtinSlashSCommands = {
commit: '/commit',
sync: '/sync',
merge: '/merge',
createPr: '/create-pr',
createDraftPr: '/create-draft-pr',
updatePr: '/update-pr',
};
/**
* Either a free-form prompt **or** a known command.
*/
export type CopilotCLISessionInput =
| { readonly prompt: string }
| { readonly prompt?: string; readonly command: CopilotCLICommand };
function getPromptLabel(input: CopilotCLISessionInput): string {
if ('command' in input) {
const prompt = input.prompt ?? '';
return prompt ? `/${input.command} ${prompt}` : `/${input.command}`;
}
return input.prompt;
}
export interface ICopilotCLISession extends IDisposable {
readonly sessionId: string;
readonly title?: string;
readonly createdPullRequestUrl: string | undefined;
readonly onDidChangeTitle: vscode.Event<string>;
readonly status: vscode.ChatSessionStatus | undefined;
readonly onDidChangeStatus: vscode.Event<vscode.ChatSessionStatus | undefined>;
readonly workspace: IWorkspaceInfo;
readonly additionalWorkspaces: IWorkspaceInfo[];
readonly pendingPrompt: string | undefined;
attachStream(stream: vscode.ChatResponseStream): IDisposable;
setPermissionLevel(level: string | undefined): void;
handleRequest(
request: { id: string; toolInvocationToken: ChatParticipantToolToken; sessionResource?: vscode.Uri },
input: CopilotCLISessionInput,
attachments: Attachment[],
model: { model: string; reasoningEffort?: string } | undefined,
authInfo: NonNullable<SessionOptions['authInfo']>,
token: vscode.CancellationToken
): Promise<void>;
addUserMessage(content: string): void;
addUserAssistantMessage(content: string): void;
getSelectedModelId(): Promise<string | undefined>;
}
export class CopilotCLISession extends DisposableStore implements ICopilotCLISession {
public readonly sessionId: string;
private _createdPullRequestUrl: string | undefined;
public get createdPullRequestUrl(): string | undefined {
return this._createdPullRequestUrl;
}
private _status?: vscode.ChatSessionStatus;
public get status(): vscode.ChatSessionStatus | undefined {
return this._status;
}
private readonly _statusChange = this.add(new EventEmitter<vscode.ChatSessionStatus | undefined>());
public readonly onDidChangeStatus = this._statusChange.event;
private _permissionRequested?: PermissionRequest;
public get permissionRequested(): PermissionRequest | undefined {
return this._permissionRequested;
}
private _title?: string;
public get title(): string | undefined {
return this._title;
}
private _onDidChangeTitle = this.add(new Emitter<string>());
public onDidChangeTitle = this._onDidChangeTitle.event;
private _stream?: vscode.ChatResponseStream;
private _toolInvocationToken?: ChatParticipantToolToken;
public get sdkSession() {
return this._sdkSession;
}
public get workspace() {
return this._workspaceInfo;
}
public get additionalWorkspaces() {
return this._additionalWorkspaces;
}
private _lastUsedModel: string | undefined;
private _permissionLevel: string | undefined;
private _pendingPrompt: string | undefined;
private _bridgeProcessor: CopilotCliBridgeSpanProcessor | undefined;
/** Callback to propagate trace context to the SDK's OtelLifecycle. */
private _updateSdkTraceContext: ((traceparent?: string, tracestate?: string) => void) | undefined;
public get pendingPrompt(): string | undefined {
return this._pendingPrompt;
}
/** Set the bridge processor for forwarding SDK spans to the debug panel. */
setBridgeProcessor(bridge: CopilotCliBridgeSpanProcessor | undefined): void {
this._bridgeProcessor = bridge;
}
/** Set the SDK OTel trace context updater (pre-bound with sessionId). */
setSdkTraceContextUpdater(updater: ((traceparent?: string, tracestate?: string) => void) | undefined): void {
this._updateSdkTraceContext = updater;
}
constructor(
private readonly _workspaceInfo: IWorkspaceInfo,
private readonly _agentName: string | undefined,
private readonly _sdkSession: Session,
private readonly _additionalWorkspaces: IWorkspaceInfo[],
@ILogService private readonly logService: ILogService,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@IChatSessionMetadataStore private readonly _chatSessionMetadataStore: IChatSessionMetadataStore,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IRequestLogger private readonly _requestLogger: IRequestLogger,
@ICopilotCLIImageSupport private readonly _imageSupport: ICopilotCLIImageSupport,
@IToolsService private readonly _toolsService: IToolsService,
@IUserQuestionHandler private readonly _userQuestionHandler: IUserQuestionHandler,
@IConfigurationService private readonly configurationService: IConfigurationService,
@IOTelService private readonly _otelService: IOTelService,
) {
super();
this.sessionId = _sdkSession.sessionId;
}
attachStream(stream: vscode.ChatResponseStream): IDisposable {
this._stream = stream;
return toDisposable(() => {
if (this._stream === stream) {
this._stream = undefined;
}
});
}
public setPermissionLevel(level: string | undefined): void {
this._permissionLevel = level;
}
// TODO: This should be pre-populated when we restore a session based on its original context.
// E.g. if we're resuming a session, and it tries to read a file, we shouldn't prompt for permissions again.
/**
* Accumulated attachments across all requests in this session.
* Used for permission auto-approval: if a file was attached by the user in any
* request, read access is auto-approved for that file in subsequent turns.
*/
private readonly attachments: Attachment[] = [];
/**
* Promise chain that serialises request completion tracking.
* When a steering request arrives while a previous request is still running,
* the steering handler awaits both `previousRequest` and its own SDK send so
* that the steering message does not resolve until the original request finishes.
*/
private previousRequest: Promise<unknown> = Promise.resolve();
/**
* Entry point for every chat request against this session.
*
* **Steering behaviour**: if the session is already busy (`InProgress` or
* `NeedsInput`), the incoming message is treated as a *steering* request.
* Steering sends the new prompt to the SDK with `mode: 'immediate'` so it is
* injected into the running conversation as additional context. The steering
* request only resolves once *both* the steering send and the original
* in-flight request have completed, keeping the session's promise chain
* consistent.
*
* When the session is idle, a normal full request is started instead.
*/
public async handleRequest(
request: { id: string; toolInvocationToken: ChatParticipantToolToken; sessionResource?: vscode.Uri },
input: CopilotCLISessionInput,
attachments: Attachment[],
model: { model: string; reasoningEffort?: string } | undefined,
authInfo: NonNullable<SessionOptions['authInfo']>,
token: vscode.CancellationToken
): Promise<void> {
if (this.isDisposed) {
throw new Error('Session disposed');
}
const label = getPromptLabel(input);
const promptLabel = truncate(label, 50);
const capturingToken = new CapturingToken(`Copilot CLI | ${promptLabel}`, 'worktree', undefined, undefined, this.sessionId);
const isAlreadyBusyWithAnotherRequest = !!this._status && (this._status === ChatSessionStatus.InProgress || this._status === ChatSessionStatus.NeedsInput);
this._toolInvocationToken = request.toolInvocationToken;
const previousRequestSnapshot = this.previousRequest;
const handled = this._requestLogger.captureInvocation(capturingToken, async () => {
await this.updateModel(model?.model, model?.reasoningEffort, authInfo, token);
if (isAlreadyBusyWithAnotherRequest) {
return this._handleRequestSteering(input, attachments, model, previousRequestSnapshot, token);
} else {
return this._handleRequestImpl(request, input, attachments, model, token);
}
});
this.previousRequest = this.previousRequest.then(() => handled);
return handled;
}
/**
* Handles a steering request – a message sent while the session is already
* busy with a previous request.
*
* The steering prompt is sent to the SDK with `mode: 'immediate'` (via
* {@link sendRequestInternal}) so the SDK injects it into the running
* conversation as additional user context. The SDK send itself typically
* completes quickly (it only enqueues the message), but we also await
* `previousRequestPromise` so that this method does not resolve until the
* original in-flight request is fully done. This ensures callers see the
* correct session state when the returned promise settles.
*
* @param previousRequestPromise A snapshot of `this.previousRequest` captured
* *before* the promise chain was extended with the current call. Using the
* snapshot avoids a circular await that would deadlock.
*/
private async _handleRequestSteering(
input: CopilotCLISessionInput,
attachments: Attachment[],
model: { model: string; reasoningEffort?: string } | undefined,
previousRequestPromise: Promise<unknown>,
token: vscode.CancellationToken,
): Promise<void> {
this.attachments.push(...attachments);
const prompt = getPromptLabel(input);
this._pendingPrompt = prompt;
this.logService.info(`[CopilotCLISession] Steering session ${this.sessionId}`);
const disposables = new DisposableStore();
const logStartTime = Date.now();
disposables.add(token.onCancellationRequested(() => {
this._sdkSession.abort();
}));
disposables.add(toDisposable(() => this._sdkSession.abort()));
try {
// Send the steering prompt (completes quickly) and also wait for the
// previous request to finish, so this promise settles only once all
// in-flight work is done.
await Promise.all([previousRequestPromise, this.sendRequestInternal(input, attachments, true, logStartTime)]);
this._logConversation(prompt, '', model?.model || '', attachments, logStartTime, 'Completed');
} catch (error) {
this._logConversation(prompt, '', model?.model || '', attachments, logStartTime, 'Failed', error instanceof Error ? error.message : String(error));
throw error;
} finally {
disposables.dispose();
}
}
private async _handleRequestImpl(
request: { id: string; toolInvocationToken: ChatParticipantToolToken },
input: CopilotCLISessionInput,
attachments: Attachment[],
model: { model: string; reasoningEffort?: string } | undefined,
token: vscode.CancellationToken
): Promise<void> {
const modelId = model?.model;
return this._otelService.startActiveSpan(
'invoke_agent copilotcli',
{
kind: SpanKind.INTERNAL,
attributes: {
[GenAiAttr.OPERATION_NAME]: GenAiOperationName.INVOKE_AGENT,
[GenAiAttr.AGENT_NAME]: 'copilotcli',
[GenAiAttr.PROVIDER_NAME]: 'github',
[GenAiAttr.CONVERSATION_ID]: this.sessionId,
[CopilotChatAttr.SESSION_ID]: this.sessionId,
[CopilotChatAttr.CHAT_SESSION_ID]: this.sessionId,
...(modelId ? { [GenAiAttr.REQUEST_MODEL]: modelId } : {}),
},
},
async span => {
// Register the trace context so the bridge processor can inject CHAT_SESSION_ID
const traceCtx = span.getSpanContext();
if (traceCtx && this._bridgeProcessor) {
this._bridgeProcessor.registerTrace(traceCtx.traceId, this.sessionId);
}
// Propagate trace context to SDK so its spans are children of this span
if (traceCtx && this._updateSdkTraceContext) {
const traceparent = `00-${traceCtx.traceId}-${traceCtx.spanId}-01`;
this._updateSdkTraceContext(traceparent);
}
try {
return await this._handleRequestImplInner(span, request, input, attachments, modelId, token);
} finally {
if (traceCtx && this._bridgeProcessor) {
this._bridgeProcessor.unregisterTrace(traceCtx.traceId);
}
// Clear SDK trace context so it doesn't leak to next request
if (this._updateSdkTraceContext) {
this._updateSdkTraceContext(undefined);
}
}
},
);
}
private async _handleRequestImplInner(
invokeAgentSpan: ISpanHandle,
request: { id: string; toolInvocationToken: ChatParticipantToolToken },
input: CopilotCLISessionInput,
attachments: Attachment[],
modelId: string | undefined,
token: vscode.CancellationToken
): Promise<void> {
this.attachments.push(...attachments);
const prompt = getPromptLabel(input);
this._pendingPrompt = prompt;
this.logService.info(`[CopilotCLISession] Invoking session ${this.sessionId}`);
const disposables = new DisposableStore();
const logStartTime = Date.now();
disposables.add(token.onCancellationRequested(() => {
this._sdkSession.abort();
}));
disposables.add(toDisposable(() => this._sdkSession.abort()));
this._status = ChatSessionStatus.InProgress;
this._statusChange.fire(this._status);
const pendingToolInvocations = new Map<string, [ChatToolInvocationPart | ChatResponseMarkdownPart | ChatResponseThinkingProgressPart, toolData: ToolCall, parentToolCallId: string | undefined]>();
const editToolIds = new Set<string>();
const toolCalls = new Map<string, ToolCall>();
const editTracker = new ExternalEditTracker();
let sdkRequestId: string | undefined;
const toolIdEditMap = new Map<string, Promise<string | undefined>>();
/**
* The sequence of events from the SDK is as follows:
* tool.start -> About to run a terminal command
* permission request -> Asks user for permission to run the command
* tool.complete -> Command has completed running, contains the output or error
*
* There's a problem with this flow, we end up displaying the UI about execution in progress, even before we asked for permissions.
* This looks weird because we display two UI elements in sequence, one for "Running command..." and then immediately after "Permission requested: Allow running this command?".
* To fix this, we delay showing the "Running command..." UI until after the permission request is resolved. If the permission request is approved, we then show the "Running command..." UI. If the permission request is denied, we show a message indicating that the command was not run due to lack of permissions.
* & if we don't get a permission request, but get some other event, then we show the "Running command..." UI immediately as before.
*/
const toolCallWaitingForPermissions: [ChatToolInvocationPart, ToolCall][] = [];
const flushPendingInvocationMessages = () => {
for (const [invocationMessage,] of toolCallWaitingForPermissions) {
this._stream?.push(invocationMessage);
}
toolCallWaitingForPermissions.length = 0;
};
// Flush only the tool invocation matching the given toolCallId, leaving other
// pending tools in the array. This prevents parallel tool calls from being
// prematurely pushed to the stream when only one of them has been approved.
const flushPendingInvocationMessageForToolCallId = (toolCallId: string | undefined) => {
if (!toolCallId) {
flushPendingInvocationMessages();
return;
}
const index = toolCallWaitingForPermissions.findIndex(([, tc]) => tc.toolCallId === toolCallId);
if (index !== -1) {
const [[invocationMessage]] = toolCallWaitingForPermissions.splice(index, 1);
this._stream?.push(invocationMessage);
}
};
const chunkMessageIds = new Set<string>();
const assistantMessageChunks: string[] = [];
let lastUsageInfo: UsageInfoData | undefined;
const reportUsage = (promptTokens: number, completionTokens: number) => {
if (token.isCancellationRequested || !this._stream) {
return;
}
this._stream.usage({
promptTokens,
completionTokens,
promptTokenDetails: buildPromptTokenDetails(lastUsageInfo),
});
};
const updateUsageInfo = (async () => {
const metrics = await this._sdkSession.usage.getMetrics();
const promptTokens = lastUsageInfo?.currentTokens || metrics.lastCallInputTokens;
reportUsage(promptTokens, metrics.lastCallOutputTokens);
})();
try {
const shouldHandleExitPlanModeRequests = this.configurationService.getConfig(ConfigKey.Advanced.CLIPlanExitModeEnabled);
disposables.add(toDisposable(this._sdkSession.on('*', (event) => {
this.logService.trace(`[CopilotCLISession] CopilotCLI Event: ${JSON.stringify(event, null, 2)}`);
})));
disposables.add(toDisposable(this._sdkSession.on('permission.requested', async (event) => {
const permissionRequest = event.data.permissionRequest;
const requestId = event.data.requestId;
const response = await this.requestPermission(permissionRequest, editTracker,
(toolCallId: string) => {
const toolData = toolCalls.get(toolCallId);
if (!toolData) {
return undefined;
}
const data = pendingToolInvocations.get(toolCallId);
if (data) {
return [toolData, data[2]] as const;
}
return [toolData, undefined] as const;
},
token
);
flushPendingInvocationMessageForToolCallId(permissionRequest.toolCallId);
this._requestLogger.addEntry({
type: LoggedRequestKind.MarkdownContentRequest,
debugName: `Permission Request`,
startTimeMs: Date.now(),
icon: Codicon.question,
markdownContent: this._renderPermissionToMarkdown(permissionRequest, response.kind),
isConversationRequest: true
});
this._sdkSession.respondToPermission(requestId, response);
})));
if (shouldHandleExitPlanModeRequests) {
disposables.add(toDisposable(this._sdkSession.on('exit_plan_mode.requested', async (event) => {
this.updateArtifacts();
type ActionType = Parameters<NonNullable<SessionOptions['onExitPlanMode']>>[0]['actions'][number];
if (this._permissionLevel === 'autopilot') {
this.logService.trace('[CopilotCLISession] Auto-approving exit plan mode in autopilot');
const choices: ActionType[] = (event.data.actions as ActionType[]) ?? [];
if (event.data.recommendedAction && choices.includes(event.data.recommendedAction as ActionType)) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, selectedAction: event.data.recommendedAction as ActionType, autoApproveEdits: true });
return;
}
if (choices.includes('autopilot')) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, selectedAction: 'autopilot', autoApproveEdits: true });
return;
}
if (choices.includes('autopilot_fleet')) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, selectedAction: 'autopilot_fleet', autoApproveEdits: true });
return;
}
if (choices.includes('interactive')) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, selectedAction: 'interactive' });
return;
}
if (choices.includes('exit_only')) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, selectedAction: 'exit_only' });
return;
}
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, autoApproveEdits: true });
return;
}
if (!(this._toolInvocationToken as unknown)) {
this.logService.warn('[ConfirmationTool] No toolInvocationToken available, cannot request exit plan mode approval');
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: false });
return;
}
const actionDescriptions: Record<string, { label: string; description: string }> = {
'autopilot': { label: 'Autopilot', description: l10n.t('Auto-approve all tool calls and continue until the task is done') },
'interactive': { label: 'Interactive', description: l10n.t('Let the agent continue in interactive mode, asking for input and approval for each action.') },
'exit_only': { label: 'Approve and exit', description: l10n.t('Exit planning, but do not execute the plan. I will execute the plan myself.') },
'autopilot_fleet': { label: 'Autopilot Fleet', description: l10n.t('Auto-approve all tool calls, including fleet management actions, and continue until the task is done.') },
} satisfies Record<ActionType, { label: string; description: string }>;
const approved = true;
try {
const planPath = this._sdkSession.getPlanPath();
const userInputRequest: IQuestion = {
question: planPath ? l10n.t('Approve this plan {0}?', `[Plan.md](${Uri.file(planPath).toString()})`) : l10n.t('Approve this plan?'),
header: l10n.t('Approve this plan?'),
options: event.data.actions.map(a => ({
label: actionDescriptions[a]?.label ?? a,
recommended: a === event.data.recommendedAction,
description: actionDescriptions[a]?.description ?? '',
})),
allowFreeformInput: true,
};
const answer = await this._userQuestionHandler.askUserQuestion(userInputRequest, this._toolInvocationToken as unknown as never, token);
flushPendingInvocationMessages();
if (!answer) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: false });
return;
}
if (answer.freeText) {
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: false, feedback: answer.freeText });
} else {
let selectedAction: ActionType = answer.selected[0] as ActionType;
Object.entries(actionDescriptions).forEach(([action, item]) => {
if (item.label === selectedAction) {
selectedAction = action as ActionType;
}
});
const autoApproveEdits = approved && this._permissionLevel === 'autoApprove' ? true : undefined;
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: true, selectedAction, autoApproveEdits });
}
} catch (error) {
this.logService.error(error, '[ConfirmationTool] Error showing confirmation tool for exit plan mode');
}
this._sdkSession.respondToExitPlanMode(event.data.requestId, { approved: false });
})));
}
disposables.add(toDisposable(this._sdkSession.on('user_input.requested', async (event) => {
// auto approve user input
if (this._permissionLevel === 'autopilot') {
this.logService.trace('[CopilotCLISession] Auto-responding to user input in autopilot');
this._sdkSession.respondToUserInput(event.data.requestId, { answer: 'The user is not available to respond and will review your work later. Work autonomously and make good decisions.', wasFreeform: true });
return;
}
if (!(this._toolInvocationToken as unknown)) {
this.logService.warn('[AskQuestionsTool] No stream available, cannot show question carousel');
this._sdkSession.respondToUserInput(event.data.requestId, { answer: '', wasFreeform: false });
return;
}
const userInputRequest: IQuestion = {
question: event.data.question,
options: (event.data.choices ?? []).map(c => ({ label: c })),
allowFreeformInput: event.data.allowFreeform,
header: event.data.question,
};
const answer = await this._userQuestionHandler.askUserQuestion(userInputRequest, this._toolInvocationToken as unknown as never, token);
flushPendingInvocationMessages();
if (!answer) {
this._sdkSession.respondToUserInput(event.data.requestId, { answer: '', wasFreeform: false });
return;
}
if (answer.freeText) {
this._sdkSession.respondToUserInput(event.data.requestId, { answer: answer.freeText, wasFreeform: true });
} else {
this._sdkSession.respondToUserInput(event.data.requestId, { answer: answer.selected.join(', '), wasFreeform: false });
}
})));
disposables.add(toDisposable(this._sdkSession.on('session.title_changed', (event) => {
this._title = event.data.title;
this._onDidChangeTitle.fire(event.data.title);
})));
disposables.add(toDisposable(this._sdkSession.on('user.message', (event) => {
sdkRequestId = event.id;
})));
disposables.add(toDisposable(this._sdkSession.on('assistant.usage', (event) => {
if (this._stream && typeof event.data.outputTokens === 'number' && typeof event.data.inputTokens === 'number') {
reportUsage(event.data.inputTokens, event.data.outputTokens);
}
})));
disposables.add(toDisposable(this._sdkSession.on('session.usage_info', (event) => {
lastUsageInfo = {
currentTokens: event.data.currentTokens,
systemTokens: event.data.systemTokens,
conversationTokens: event.data.conversationTokens,
toolDefinitionsTokens: event.data.toolDefinitionsTokens,
tokenLimit: event.data.tokenLimit,
};
reportUsage(lastUsageInfo.currentTokens, 0);
})));
disposables.add(toDisposable(this._sdkSession.on('assistant.message_delta', (event) => {
// Support for streaming delta messages.
if (typeof event.data.deltaContent === 'string' && event.data.deltaContent.length) {
// Ensure pending invocation messages are flushed even if we skip sub-agent markdown
flushPendingInvocationMessages();
// Skip sub-agent markdown — it will be captured in the subagent tool's result
if (event.data.parentToolCallId) {
return;
}
chunkMessageIds.add(event.data.messageId);
assistantMessageChunks.push(event.data.deltaContent);
this._stream?.markdown(event.data.deltaContent);
}
})));
disposables.add(toDisposable(this._sdkSession.on('assistant.message', (event) => {
if (typeof event.data.content === 'string' && event.data.content.length && !chunkMessageIds.has(event.data.messageId)) {
// Skip sub-agent markdown — it will be captured in the subagent tool's result
if (event.data.parentToolCallId) {
return;
}
assistantMessageChunks.push(event.data.content);
flushPendingInvocationMessages();
this._stream?.markdown(event.data.content);
}
})));
disposables.add(toDisposable(this._sdkSession.on('tool.execution_start', (event) => {
toolCalls.set(event.data.toolCallId, event.data as unknown as ToolCall);
if (isCopilotCliEditToolCall(event.data)) {
flushPendingInvocationMessages();
editToolIds.add(event.data.toolCallId);
} else {
const responsePart = processToolExecutionStart(event, pendingToolInvocations, getWorkingDirectory(this.workspace));
if (responsePart instanceof ChatResponseThinkingProgressPart) {
flushPendingInvocationMessages();
this._stream?.push(responsePart);
this._stream?.push(new ChatResponseThinkingProgressPart('', '', { vscodeReasoningDone: true }));
} else if (responsePart instanceof ChatResponseMarkdownPart) {
// Wait for completion to push into stream.
} else if (responsePart instanceof ChatToolInvocationPart) {
responsePart.enablePartialUpdate = true;
if (isCopilotCLIToolThatCouldRequirePermissions(event)) {
toolCallWaitingForPermissions.push([responsePart, event.data as ToolCall]);
} else {
flushPendingInvocationMessages();
this._stream?.push(responsePart);
}
if ((event.data as ToolCall).toolName === 'update_todo') {
updateTodoList(event, this._toolsService, request.toolInvocationToken, token).catch(error => {
this.logService.error(`[CopilotCLISession] Failed to invoke todo tool for toolCallId ${event.data.toolCallId}`, error);
});
}
}
}
this.logService.trace(`[CopilotCLISession] Start Tool ${event.data.toolName || '<unknown>'}`);
})));
disposables.add(toDisposable(this._sdkSession.on('tool.execution_complete', (event) => {
const toolName = toolCalls.get(event.data.toolCallId)?.toolName || '<unknown>';
if (toolName.endsWith('create_pull_request') && event.data.success) {
const pullRequestUrl = extractPullRequestUrlFromToolResult(event.data.result);
if (pullRequestUrl) {
this._createdPullRequestUrl = pullRequestUrl;
this.logService.trace(`[CopilotCLISession] Captured pull request URL: ${pullRequestUrl}`);
GenAiMetrics.incrementPullRequestCount(this._otelService);
}
}
// Log tool call to request logger
const eventError = event.data.error ? { ...event.data.error, code: event.data.error.code || '' } : undefined;
const eventData = { ...event.data, error: eventError };
this._logToolCall(event.data.toolCallId, toolName, toolCalls.get(event.data.toolCallId)?.arguments, eventData);
// Mark the end of the edit if this was an edit tool.
toolIdEditMap.set(event.data.toolCallId, editTracker.completeEdit(event.data.toolCallId));
if (editToolIds.has(event.data.toolCallId)) {
this.logService.trace(`[CopilotCLISession] Completed edit tracking for toolCallId ${event.data.toolCallId}`);
return;
}
// Just complete the tool invocation - the part was already pushed with partial updates enabled
const [responsePart,] = processToolExecutionComplete(event, pendingToolInvocations, this.logService, getWorkingDirectory(this.workspace)) ?? [];
if (responsePart) {
flushPendingInvocationMessageForToolCallId(event.data.toolCallId);
if (responsePart instanceof ChatToolInvocationPart) {
responsePart.enablePartialUpdate = true;
}
this._stream?.push(responsePart);
}
const success = `success: ${event.data.success}`;
const error = event.data.error ? `error: ${event.data.error.code},${event.data.error.message}` : '';
const result = event.data.result ? `result: ${event.data.result?.content}` : '';
const parts = [success, error, result].filter(part => part.length > 0).join(', ');
this.logService.trace(`[CopilotCLISession]Complete Tool ${toolName}, ${parts}`);
})));
disposables.add(toDisposable(this._sdkSession.on('session.error', (event) => {
flushPendingInvocationMessages();
this.logService.error(`[CopilotCLISession]CopilotCLI error: (${event.data.errorType}), ${event.data.message}`);
this._stream?.markdown(`\n\n❌ Error: (${event.data.errorType}) ${event.data.message}`);
const errorMarkdown = [`# Error Details`, `Type: ${event.data.errorType}`, `Message: ${event.data.message}`, `## Stack`, event.data.stack || ''].join('\n');
this._requestLogger.addEntry({
type: LoggedRequestKind.MarkdownContentRequest,
debugName: `Session Error`,
startTimeMs: Date.now(),
icon: Codicon.error,
markdownContent: errorMarkdown,
isConversationRequest: true
});
})));
disposables.add(toDisposable(this._sdkSession.on('subagent.started', (event) => {
this.logService.trace(`[CopilotCLISession] Subagent started: ${event.data.agentDisplayName} (toolCallId: ${event.data.toolCallId})`);
enrichToolInvocationWithSubagentMetadata(
event.data.toolCallId,
event.data.agentDisplayName,
event.data.agentDescription,
pendingToolInvocations
);
})));
disposables.add(toDisposable(this._sdkSession.on('subagent.completed', (event) => {
this.logService.trace(`[CopilotCLISession] Subagent completed: ${event.data.agentDisplayName} (toolCallId: ${event.data.toolCallId})`);
})));
disposables.add(toDisposable(this._sdkSession.on('subagent.failed', (event) => {
this.logService.trace(`[CopilotCLISession] Subagent failed: ${event.data.agentDisplayName} (toolCallId: ${event.data.toolCallId})`);
})));
// Stash hook event data on the bridge processor so SDK hook spans
// are enriched with input/output details for the debug panel.
disposables.add(toDisposable(this._sdkSession.on('hook.start', (event) => {
this.logService.trace(`[CopilotCLISession] Hook ${event.data.hookType} started (${event.data.hookInvocationId})`);
let input: string | undefined;
try {
input = truncateForOTel(JSON.stringify(event.data.input));
} catch { /* swallow serialization errors */ }
this._bridgeProcessor?.stashHookInput(event.data.hookInvocationId, event.data.hookType, input);
})));
disposables.add(toDisposable(this._sdkSession.on('hook.end', (event) => {
this.logService.trace(`[CopilotCLISession] Hook ${event.data.hookType} ended (${event.data.hookInvocationId}), success=${event.data.success}`);
const resultKind = event.data.success ? 'success' as const : 'error' as const;
let output: string | undefined;
if (event.data.success) {
try {
output = truncateForOTel(JSON.stringify(event.data.output));
} catch { /* swallow serialization errors */ }
}
this._bridgeProcessor?.stashHookEnd(
event.data.hookInvocationId,
event.data.hookType,
output,
resultKind,
event.data.error?.message,
);
})));
if (!token.isCancellationRequested) {
await this.sendRequestInternal(input, attachments, false, logStartTime);
}
this.logService.trace(`[CopilotCLISession] Invoking session (completed) ${this.sessionId}`);
const resolvedToolIdEditMap: Record<string, string> = {};
await Promise.all(Array.from(toolIdEditMap.entries()).map(async ([toolId, editFilePromise]) => {
const editId = await editFilePromise.catch(() => undefined);
if (editId) {
resolvedToolIdEditMap[toolId] = editId;
}
}));
if (sdkRequestId) {
await this._chatSessionMetadataStore.updateRequestDetails(this.sessionId, [{
vscodeRequestId: request.id,
copilotRequestId: sdkRequestId,
toolIdEditMap: resolvedToolIdEditMap,
agentId: this._agentName,
}]).catch(error => {
this.logService.error(`[CopilotCLISession] Failed to update chat session metadata store for request ${request.id}`, error);
});
}
await updateUsageInfo.catch(error => {
this.logService.error(`[CopilotCLISession] Failed to update usage info after request ${request.id}`, error);
});
this._status = ChatSessionStatus.Completed;
this._statusChange.fire(this._status);
// Log the completed conversation
this._logConversation(prompt, assistantMessageChunks.join(''), modelId || '', attachments, logStartTime, 'Completed');
} catch (error) {
this._status = ChatSessionStatus.Failed;
this._statusChange.fire(this._status);
this.logService.error(`[CopilotCLISession] Invoking session (error) ${this.sessionId}`, error);
this._stream?.markdown(`\n\n❌ Error: ${error instanceof Error ? error.message : String(error)}`);
invokeAgentSpan.setStatus(SpanStatusCode.ERROR, error instanceof Error ? error.message : String(error));
if (error instanceof Error) {
invokeAgentSpan.recordException(error);
}
// Log the failed conversation
this._logConversation(prompt, assistantMessageChunks.join(''), modelId || '', attachments, logStartTime, 'Failed', error instanceof Error ? error.message : String(error));
} finally {
// End the invoke_agent wrapper span
const durationSec = (Date.now() - logStartTime) / 1000;
invokeAgentSpan.setAttribute('copilot_chat.duration_sec', durationSec);
invokeAgentSpan.end();
this._pendingPrompt = undefined;
disposables.dispose();
this.updateArtifacts();
}
}
private async updateModel(modelId: string | undefined, reasoningEffort: string | undefined, authInfo: NonNullable<SessionOptions['authInfo']>, token: CancellationToken): Promise<void> {
// Where possible try to avoid an extra call to getSelectedModel by using cached value.
let currentModel: string | undefined = undefined;
if (modelId) {
if (this._lastUsedModel) {
currentModel = this._lastUsedModel;
} else {
currentModel = await raceCancellation(this._sdkSession.getSelectedModel(), token);
}
}
if (token.isCancellationRequested) {
return;
}
if (authInfo) {
this._sdkSession.setAuthInfo(authInfo);
}
if (modelId) {
if (modelId !== currentModel) {
this._lastUsedModel = modelId;
if (this.configurationService.getConfig(ConfigKey.Advanced.CLIThinkingEffortEnabled)) {
await raceCancellation(this._sdkSession.setSelectedModel(modelId, reasoningEffort), token);
} else {
await raceCancellation(this._sdkSession.setSelectedModel(modelId), token);
}
} else if (reasoningEffort && this._sdkSession.getReasoningEffort() !== reasoningEffort && this.configurationService.getConfig(ConfigKey.Advanced.CLIThinkingEffortEnabled)) {
await raceCancellation(this._sdkSession.setSelectedModel(modelId, reasoningEffort), token);
}
}
}
private updateArtifacts() {
const shouldHandleExitPlanModeRequests = this.configurationService.getConfig(ConfigKey.Advanced.CLIPlanExitModeEnabled);
if (!shouldHandleExitPlanModeRequests || !this._toolsService.getTool('setArtifacts') || !this._toolInvocationToken) {
return;
}
const artifacts: { label: string; uri: string; type: 'devServer' | 'screenshot' | 'plan' }[] = [];
const planPath = this._sdkSession.getPlanPath();
if (planPath) {
artifacts.push({ label: l10n.t('Plan'), uri: Uri.file(planPath).toString(), type: 'plan' });
}
Promise.resolve(this._toolsService
.invokeTool('setArtifacts', { input: { artifacts }, toolInvocationToken: this._toolInvocationToken }, CancellationToken.None))
.catch(error => {
this.logService.error(error, '[CopilotCLISession] Failed to update artifacts');
});
}
/**
* Sends a request to the underlying SDK session.
*
* @param steering When `true`, the SDK send uses `mode: 'immediate'` so the
* prompt is injected into the already-running conversation rather than
* starting a new turn. This is the mechanism behind session steering.
*/
private async sendRequestInternal(input: CopilotCLISessionInput, attachments: Attachment[], steering = false, logStartTime: number): Promise<void> {
const prompt = getPromptLabel(input);
this._logRequest(prompt, this._lastUsedModel || '', attachments, logStartTime);
if ('command' in input && input.command !== 'plan') {
switch (input.command) {
case 'compact': {
this._stream?.progress(l10n.t('Compacting conversation...'));
await this._sdkSession.initializeAndValidateTools();
this._sdkSession.currentMode = 'interactive';
const result = await this._sdkSession.compactHistory();
if (result.success) {
this._stream?.markdown(l10n.t('Compacted conversation.'));
} else {
this._stream?.markdown(l10n.t('Unable to compact conversation.'));
}
break;
}
case 'fleet': {
await this._startFleetAndWaitForIdle(input);
break;
}
}
} else {
if ('command' in input && input.command === 'plan') {
this._sdkSession.currentMode = 'plan';
} else if (this._permissionLevel === 'autopilot') {
this._sdkSession.currentMode = 'autopilot';
} else {
this._sdkSession.currentMode = 'interactive';
}
const sendOptions: SendOptions = { prompt: input.prompt ?? '', attachments, agentMode: this._sdkSession.currentMode };
if (steering) {
sendOptions.mode = 'immediate';
}
await this._sdkSession.send(sendOptions);
}
}
private async _startFleetAndWaitForIdle(input: CopilotCLISessionInput): Promise<void> {
const prompt = 'prompt' in input ? input.prompt : undefined;
try {
const promise = new Promise<void>((resolve) => {
const off = this._sdkSession.on('session.idle', () => {
resolve();
off();
});
});
if (this._permissionLevel === 'autopilot') {
this._sdkSession.currentMode = 'autopilot';
} else {
this._sdkSession.currentMode = 'interactive';
}
const result = await this._sdkSession.fleet.start({ prompt });
if (!result.started) {
this.logService.info('[CopilotCLISession] Fleet mode not started');
return;
}
await promise;
} catch (error) {
this.logService.error(`[CopilotCLISession] Fleet error: ${error}`);
}
}
addUserMessage(content: string) {
this._sdkSession.emit('user.message', { content });
}
addUserAssistantMessage(content: string) {
this._sdkSession.emit('assistant.message', {
messageId: `msg_${Date.now()}`,
content
});
}
public getSelectedModelId() {
return this._sdkSession.getSelectedModel();
}
private isFileFromSessionWorkspace(file: Uri): boolean {
const workingDirectory = getWorkingDirectory(this.workspace);
if (workingDirectory && extUriBiasedIgnorePathCase.isEqualOrParent(file, workingDirectory)) {
return true;
}
if (this.workspace.folder && extUriBiasedIgnorePathCase.isEqualOrParent(file, this.workspace.folder)) {
return true;
}
// Only if we have a worktree should we check the repository.
// As this means the user created a worktree and we have a repository.
// & if the worktree is automatically trusted, then so is the repository as we created the worktree from that.
if (this.workspace.worktree && this.workspace.repository && extUriBiasedIgnorePathCase.isEqualOrParent(file, this.workspace.repository)) {
return true;
}
return false;
}
private async requestPermission(
permissionRequest: PermissionRequest,
editTracker: ExternalEditTracker,
getToolCall: (toolCallId: string) => undefined | [ToolCall, parentToolCallId: string | undefined],
token: vscode.CancellationToken
): Promise<{ kind: 'approved' } | { kind: 'denied-interactively-by-user' }> {
if (this._permissionLevel === 'autoApprove' || this._permissionLevel === 'autopilot') {
this.logService.trace(`[CopilotCLISession] Auto Approving ${permissionRequest.kind} request (permission level: ${this._permissionLevel})`);
return { kind: 'approved' };
}
const workingDirectory = getWorkingDirectory(this.workspace);
if (permissionRequest.kind === 'read') {
// If user is reading a file in the working directory or workspace, auto-approve
// read requests. Outside workspace reads (e.g., /etc/passwd) will still require
// approval.
const data = Uri.file(permissionRequest.path);
if (this._imageSupport.isTrustedImage(data)) {
return { kind: 'approved' };
}
if (this.isFileFromSessionWorkspace(data)) {
this.logService.trace(`[CopilotCLISession] Auto Approving request to read file in session workspace ${permissionRequest.path}`);
return { kind: 'approved' };
}
if (this.workspaceService.getWorkspaceFolder(data)) {
this.logService.trace(`[CopilotCLISession] Auto Approving request to read workspace file ${permissionRequest.path}`);
return { kind: 'approved' };
}
// If reading a file from session directory, e.g. plan.md, then auto approve it, this is internal file to Cli.
const sessionDir = Uri.joinPath(Uri.file(getCopilotCLISessionStateDir()), this.sessionId);
if (extUriBiasedIgnorePathCase.isEqualOrParent(data, sessionDir)) {
this.logService.trace(`[CopilotCLISession] Auto Approving request to read Copilot CLI session resource ${permissionRequest.path}`);
return { kind: 'approved' };
}
// If model is trying to read the contents of a file thats attached, then auto-approve it, as this is an explicit action by the user to share the file with the model.
if (this.attachments.some(attachment => attachment.type === 'file' && isEqual(Uri.file(attachment.path), data))) {
this.logService.trace(`[CopilotCLISession] Auto Approving request to read attached file ${permissionRequest.path}`);
return { kind: 'approved' };
}
}
// Get hold of file thats being edited if this is a edit tool call (requiring write permissions).
const toolData = permissionRequest.toolCallId ? getToolCall(permissionRequest.toolCallId) : undefined;