forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaultIntentRequestHandler.ts
More file actions
858 lines (773 loc) · 40.8 KB
/
Copy pathdefaultIntentRequestHandler.ts
File metadata and controls
858 lines (773 loc) · 40.8 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import { Raw } from '@vscode/prompt-tsx';
import type { ChatRequest, ChatResponseReferencePart, ChatResponseStream, ChatResult, LanguageModelToolInformation, Progress } from 'vscode';
import { IAuthenticationService } from '../../../platform/authentication/common/authentication';
import { IAuthenticationChatUpgradeService } from '../../../platform/authentication/common/authenticationUpgrade';
import { IChatHookService, UserPromptSubmitHookInput, UserPromptSubmitHookOutput } from '../../../platform/chat/common/chatHookService';
import { CanceledResult, ChatFetchError, ChatFetchResponseType, ChatLocation, ChatResponse, getErrorDetailsFromChatFetchError } from '../../../platform/chat/common/commonTypes';
import { IConversationOptions } from '../../../platform/chat/common/conversationOptions';
import { ISessionTranscriptService } from '../../../platform/chat/common/sessionTranscriptService';
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
import { IEditSurvivalTrackerService, IEditSurvivalTrackingSession, NullEditSurvivalTrackingSession } from '../../../platform/editSurvivalTracking/common/editSurvivalTrackerService';
import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';
import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
import { IGitService } from '../../../platform/git/common/gitService';
import { IOctoKitService } from '../../../platform/github/common/githubService';
import { HAS_IGNORED_FILES_MESSAGE } from '../../../platform/ignore/common/ignoreService';
import { ILogService } from '../../../platform/log/common/logService';
import { isAnthropicContextEditingEnabled } from '../../../platform/networking/common/anthropic';
import { FilterReason } from '../../../platform/networking/common/openai';
import { IOTelService } from '../../../platform/otel/common/otelService';
import { CapturingToken } from '../../../platform/requestLogger/common/capturingToken';
import { IRequestLogger } from '../../../platform/requestLogger/common/requestLogger';
import { ISurveyService } from '../../../platform/survey/common/surveyService';
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
import { ChatResponseStreamImpl } from '../../../util/common/chatResponseStreamImpl';
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
import { isCancellationError } from '../../../util/vs/base/common/errors';
import { Event } from '../../../util/vs/base/common/event';
import { Iterable } from '../../../util/vs/base/common/iterator';
import { DisposableStore } from '../../../util/vs/base/common/lifecycle';
import { mixin } from '../../../util/vs/base/common/objects';
import { assertType, Mutable } from '../../../util/vs/base/common/types';
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
import { ChatResponseMarkdownPart, ChatResponseProgressPart, ChatResponseTextEditPart, LanguageModelToolResult2 } from '../../../vscodeTypes';
import { CodeBlocksMetadata, CodeBlockTrackingChatResponseStream } from '../../codeBlocks/node/codeBlockProcessor';
import { CopilotInteractiveEditorResponse, InteractionOutcomeComputer } from '../../inlineChat/node/promptCraftingTypes';
import { formatHookErrorMessage, HookAbortError, isHookAbortError, processHookResults } from '../../intents/node/hookResultProcessor';
import { EmptyPromptError, IToolCallingBuiltPromptEvent, IToolCallingLoopOptions, IToolCallingResponseEvent, IToolCallLoopResult, ToolCallingLoop, ToolCallingLoopFetchOptions, ToolCallLimitBehavior } from '../../intents/node/toolCallingLoop';
import { UnknownIntent } from '../../intents/node/unknownIntent';
import { ResponseStreamWithLinkification } from '../../linkify/common/responseStreamWithLinkification';
import { SummarizedConversationHistoryMetadata } from '../../prompts/node/agent/summarizedConversationHistory';
import { normalizeToolSchema } from '../../tools/common/toolSchemaNormalizer';
import { ToolCallCancelledError } from '../../tools/common/toolsService';
import { IToolGrouping, IToolGroupingService } from '../../tools/common/virtualTools/virtualToolTypes';
import { ChatVariablesCollection } from '../common/chatVariablesCollection';
import { Conversation, getUniqueReferences, GlobalContextMessageMetadata, IResultMetadata, RenderedUserMessageMetadata, RequestDebugInformation, ResponseStreamParticipant, Turn, TurnStatus, TurnTokenUsageMetadata } from '../common/conversation';
import { IBuildPromptContext, IToolCallRound } from '../common/intents';
import { isToolCallLimitCancellation, ISwitchToAutoOnRateLimitConfirmation } from '../common/specialRequestTypes';
import { ChatTelemetry, ChatTelemetryBuilder } from './chatParticipantTelemetry';
import { IntentInvocationMetadata } from './conversation';
import { IDocumentContext } from './documentContext';
import { IBuildPromptResult, IIntent, IIntentInvocation, IResponseProcessor, TelemetryData } from './intents';
import { ConversationalBaseTelemetryData, createTelemetryWithId, sendModelMessageTelemetry } from './telemetry';
export interface IDefaultIntentRequestHandlerOptions {
maxToolCallIterations: number;
/**
* Whether to ask the user if they want to continue when the tool call limit
* is exceeded. Defaults to true.
*/
confirmOnMaxToolIterations?: boolean;
temperature?: number;
overrideRequestLocation?: ChatLocation;
}
/*
* Handles a single chat-request via an intent-invocation.
*/
export class DefaultIntentRequestHandler {
private readonly turn: Turn;
private _editSurvivalTracker: IEditSurvivalTrackingSession = new NullEditSurvivalTrackingSession();
private _loop!: DefaultToolCallingLoop;
constructor(
private readonly intent: IIntent,
private readonly conversation: Conversation,
protected readonly request: ChatRequest,
protected readonly stream: ChatResponseStream,
private readonly token: CancellationToken,
protected readonly documentContext: IDocumentContext | undefined,
private readonly location: ChatLocation,
private readonly chatTelemetryBuilder: ChatTelemetryBuilder,
private readonly handlerOptions: IDefaultIntentRequestHandlerOptions = { maxToolCallIterations: 15 },
private readonly yieldRequested: (() => boolean) | undefined,
@IInstantiationService private readonly _instantiationService: IInstantiationService,
@IConversationOptions private readonly options: IConversationOptions,
@ITelemetryService private readonly _telemetryService: ITelemetryService,
@ILogService private readonly _logService: ILogService,
@ISurveyService private readonly _surveyService: ISurveyService,
@IRequestLogger private readonly _requestLogger: IRequestLogger,
@IEditSurvivalTrackerService private readonly _editSurvivalTrackerService: IEditSurvivalTrackerService,
@IAuthenticationService private readonly _authenticationService: IAuthenticationService,
@IChatHookService private readonly _chatHookService: IChatHookService,
@IOctoKitService private readonly _octoKitService: IOctoKitService,
@IConfigurationService private readonly _configurationService: IConfigurationService,
) {
// Initialize properties
this.turn = conversation.getLatestTurn();
}
async getResult(): Promise<ChatResult> {
if (isToolCallLimitCancellation(this.request)) {
// Just some friendly text instead of an empty message on cancellation:
this.stream.markdown(l10n.t("Let me know if there's anything else I can help with!"));
return {};
}
try {
if (this.token.isCancellationRequested) {
return CanceledResult;
}
this._logService.trace('Processing intent');
const intentInvocation = await this.intent.invoke({ location: this.location, documentContext: this.documentContext, request: this.request });
if (this.token.isCancellationRequested) {
return CanceledResult;
}
this._logService.trace('Processed intent');
this.turn.setMetadata(new IntentInvocationMetadata(intentInvocation));
const confirmationResult = await this.handleConfirmationsIfNeeded();
if (confirmationResult) {
return confirmationResult;
}
// For subagent requests, use the subAgentInvocationId as the session ID.
// This enables explicit linking between the parent's runSubagent tool call and the subagent trajectory.
// For main requests, use the VS Code chat sessionId directly as the trajectory session ID.
const isSubagent = !!this.request.subAgentInvocationId;
const capturingToken = new CapturingToken(
this.request.prompt,
'comment',
this.request.subAgentInvocationId,
this.request.subAgentName,
// For subagents, use invocation ID as chatSessionId so spans get their own log file
isSubagent ? this.request.subAgentInvocationId : this.request.sessionId,
// For subagents, link back to the parent session
isSubagent ? this.request.sessionId : undefined,
isSubagent ? `runSubagent-${this.request.subAgentName ?? 'default'}` : undefined,
);
const resultDetails = await this._requestLogger.captureInvocation(capturingToken, () => this.runWithToolCalling(intentInvocation));
let chatResult = resultDetails.chatResult || {};
this._surveyService.signalUsage(`${this.location === ChatLocation.Editor ? 'inline' : 'panel'}.${this.intent.id}`, this.documentContext?.document.languageId);
const responseMessage = resultDetails.toolCallRounds.at(-1)?.response ?? '';
const metadataFragment: Partial<IResultMetadata> = {
toolCallRounds: resultDetails.toolCallRounds,
toolCallResults: this._collectRelevantToolCallResults(resultDetails.toolCallRounds, resultDetails.toolCallResults),
resolvedModel: resultDetails.response.type === ChatFetchResponseType.Success ? resultDetails.response.resolvedModel : undefined,
};
mixin(chatResult, { metadata: metadataFragment }, true);
const baseModelTelemetry = createTelemetryWithId();
chatResult = await this.processResult(resultDetails.response, responseMessage, chatResult, metadataFragment, baseModelTelemetry, resultDetails.toolCallRounds);
if (chatResult.errorDetails && intentInvocation.modifyErrorDetails) {
chatResult.errorDetails = intentInvocation.modifyErrorDetails(chatResult.errorDetails, resultDetails.response);
}
if (resultDetails.hadIgnoredFiles) {
this.stream.markdown(HAS_IGNORED_FILES_MESSAGE);
}
return chatResult;
} catch (err) {
if (err instanceof ToolCallCancelledError) {
this.turn.setResponse(TurnStatus.Cancelled, { message: err.message, type: 'meta' }, undefined, {});
return {};
} else if (isCancellationError(err)) {
return CanceledResult;
} else if (err instanceof EmptyPromptError) {
return {};
} else if (isHookAbortError(err)) {
this._logService.info(`[DefaultIntentRequestHandler] Hook ${err.hookType} aborted: ${err.stopReason}`);
return {};
}
this._logService.error(err);
this._telemetryService.sendGHTelemetryException(err, 'Error');
const errorMessage = (<Error>err).message;
const chatResult = { errorDetails: { message: errorMessage } };
this.turn.setResponse(TurnStatus.Error, { message: errorMessage, type: 'meta' }, undefined, chatResult);
return chatResult;
}
}
private _collectRelevantToolCallResults(toolCallRounds: IToolCallRound[], toolCallResults: Record<string, LanguageModelToolResult2>): Record<string, LanguageModelToolResult2> | undefined {
const resultsUsedInThisTurn: Record<string, LanguageModelToolResult2> = {};
for (const round of toolCallRounds) {
for (const toolCall of round.toolCalls) {
resultsUsedInThisTurn[toolCall.id] = toolCallResults[toolCall.id];
}
}
return Object.keys(resultsUsedInThisTurn).length ? resultsUsedInThisTurn : undefined;
}
private _sendInitialChatReferences({ result: buildPromptResult }: IToolCallingBuiltPromptEvent) {
const [includedVariableReferences, ignoredVariableReferences] = [getUniqueReferences(buildPromptResult.references), getUniqueReferences(buildPromptResult.omittedReferences)].map((refs) => refs.reduce((acc, ref) => {
if ('variableName' in ref.anchor) {
acc.add(ref.anchor.variableName);
}
return acc;
}, new Set<string>()));
for (const reference of buildPromptResult.references) {
// Report variables which were partially sent to the model
const options = reference.options ?? ('variableName' in reference.anchor && ignoredVariableReferences.has(reference.anchor.variableName)
? { status: { kind: 2, description: l10n.t('Part of this attachment was not sent to the model due to context window limitations.') } }
: undefined);
if (!reference.options?.isFromTool) {
// References reported by a tool result will be shown in a separate list, don't need to be reported as references
this.stream.reference2(reference.anchor, undefined, options);
}
}
for (const omittedReference of buildPromptResult.omittedReferences) {
if ('variableName' in omittedReference.anchor && !includedVariableReferences.has(omittedReference.anchor.variableName)) {
this.stream.reference2(omittedReference.anchor, undefined, { status: { kind: 3, description: l10n.t('This attachment was not sent to the model due to context window limitations.') } });
}
}
}
private makeResponseStreamParticipants(intentInvocation: IIntentInvocation): ResponseStreamParticipant[] {
const participants: ResponseStreamParticipant[] = [];
// 1. Tracking of code blocks. Currently used in stests. todo@connor4312:
// can we simplify this so it's not used otherwise?
participants.push(stream => {
const codeBlockTrackingResponseStream = this._instantiationService.createInstance(CodeBlockTrackingChatResponseStream, stream, intentInvocation.codeblocksRepresentEdits);
return ChatResponseStreamImpl.spy(
codeBlockTrackingResponseStream,
v => v,
() => {
const codeBlocksMetaData = codeBlockTrackingResponseStream.finish();
this.turn.setMetadata(codeBlocksMetaData);
}
);
});
// 2. Track the survival of edits made in the editor
if (this.documentContext && this.location === ChatLocation.Editor) {
participants.push(stream => {
const firstTurnWithAIEditCollector = this.conversation.turns.find(turn => turn.getMetadata(CopilotInteractiveEditorResponse)?.editSurvivalTracker);
this._editSurvivalTracker = firstTurnWithAIEditCollector?.getMetadata(CopilotInteractiveEditorResponse)?.editSurvivalTracker ?? this._editSurvivalTrackerService.initialize(this.documentContext!.document.document);
return ChatResponseStreamImpl.spy(stream, value => {
if (value instanceof ChatResponseTextEditPart) {
this._editSurvivalTracker.collectAIEdits(value.edits);
}
});
});
}
// 3. Track the survival of other(?) interactions
// todo@connor4312: can these two streams be combined?
const interactionOutcomeComputer = new InteractionOutcomeComputer(this.documentContext?.document.uri);
participants.push(stream => interactionOutcomeComputer.spyOnStream(stream));
// 4. Linkify the stream unless told otherwise, or if this is a subagent request
if (!intentInvocation.linkification?.disable && !this.request.subAgentInvocationId) {
participants.push(stream => {
const linkStream = this._instantiationService.createInstance(ResponseStreamWithLinkification, { requestId: this.turn.id, references: this.turn.references }, stream, intentInvocation.linkification?.additionaLinkifiers ?? [], this.token);
return ChatResponseStreamImpl.spy(linkStream, p => p, () => {
this._loop.telemetry.markAddedLinks(linkStream.totalAddedLinkCount);
});
});
}
// 5. General telemetry on emitted components
participants.push(stream => ChatResponseStreamImpl.spy(stream, (part) => {
if (part instanceof ChatResponseMarkdownPart) {
this._loop.telemetry.markEmittedMarkdown(part.value);
}
if (part instanceof ChatResponseTextEditPart) {
this._loop.telemetry.markEmittedEdits(part.uri, part.edits);
}
}));
return participants;
}
private async _onDidReceiveResponse({ response, toolCalls, interactionOutcome }: IToolCallingResponseEvent) {
const responseMessage = (response.type === ChatFetchResponseType.Success ? response.value : '');
await this._loop.telemetry.sendTelemetry(response.requestId, response.type, responseMessage, interactionOutcome.interactionOutcome, toolCalls);
if (this.documentContext) {
this.turn.setMetadata(new CopilotInteractiveEditorResponse(
interactionOutcome.store,
{ ...this.documentContext, intent: this.intent, query: this.request.prompt },
this.chatTelemetryBuilder.telemetryMessageId,
this._loop.telemetry,
this._editSurvivalTracker,
));
const documentText = this.documentContext?.document.getText();
this.turn.setMetadata(new RequestDebugInformation(
this.documentContext.document.uri,
this.intent.id,
this.documentContext.document.languageId,
documentText!,
this.request.prompt,
this.documentContext.selection
));
}
}
private async runWithToolCalling(intentInvocation: IIntentInvocation): Promise<IInternalRequestResult> {
const store = new DisposableStore();
const loop = this._loop = store.add(this._instantiationService.createInstance(
DefaultToolCallingLoop,
{
conversation: this.conversation,
intent: this.intent,
invocation: intentInvocation,
toolCallLimit: this.handlerOptions.maxToolCallIterations,
onHitToolCallLimit: this.handlerOptions.confirmOnMaxToolIterations !== false
? ToolCallLimitBehavior.Confirm : ToolCallLimitBehavior.Stop,
request: this.request,
documentContext: this.documentContext,
streamParticipants: this.makeResponseStreamParticipants(intentInvocation),
temperature: this.handlerOptions.temperature ?? this.options.temperature,
location: this.location,
overrideRequestLocation: this.handlerOptions.overrideRequestLocation,
interactionContext: this.documentContext?.document.uri,
responseProcessor: typeof intentInvocation.processResponse === 'function' ? intentInvocation as IResponseProcessor : undefined,
yieldRequested: this.yieldRequested,
},
this.chatTelemetryBuilder,
));
store.add(Event.once(loop.onDidBuildPrompt)(this._sendInitialChatReferences, this));
// We need to wait for all response handlers to finish before
// we can dispose the store. This is because the telemetry machine
// still needs the tokenizers to count tokens. There was a case in vitests
// in which the store, and the tokenizers, were disposed before the telemetry
// machine could count the tokens, which resulted in an error.
// src/extension/prompt/node/chatParticipantTelemetry.ts#L521-L522
//
// cc @lramos15
const responseHandlers: Promise<unknown>[] = [];
store.add(loop.onDidReceiveResponse(res => {
const promise = this._onDidReceiveResponse(res);
responseHandlers.push(promise);
return promise;
}, this));
try {
// Execute start hooks first (SessionStart/SubagentStart), then UserPromptSubmit
await loop.runStartHooks(this.stream, this.token);
const userPromptSubmitResults = await this._chatHookService.executeHook('UserPromptSubmit', this.request.hooks, { prompt: this.request.prompt } satisfies UserPromptSubmitHookInput, this.conversation.sessionId, this.token);
const additionalContexts: string[] = [];
processHookResults({
hookType: 'UserPromptSubmit',
results: userPromptSubmitResults,
outputStream: this.stream,
logService: this._logService,
onSuccess: (output) => {
if (typeof output === 'object' && output !== null) {
const typedOutput = output as UserPromptSubmitHookOutput & { additionalContext?: string };
const additionalContext = typedOutput.hookSpecificOutput?.additionalContext ?? typedOutput.additionalContext;
if (additionalContext) {
additionalContexts.push(additionalContext);
}
// Check for block decision output
if (typedOutput.decision === 'block') {
const blockReason = typedOutput.reason || l10n.t('No reason provided');
this._logService.info(`[DefaultIntentRequestHandler] UserPromptSubmit hook block decision: ${blockReason}`);
this.stream.hookProgress('UserPromptSubmit', formatHookErrorMessage(blockReason));
throw new HookAbortError('UserPromptSubmit', blockReason);
}
}
},
});
if (additionalContexts.length > 0) {
loop.appendAdditionalHookContext(additionalContexts.join('\n'));
}
const result = await loop.run(this.stream, this.token);
if (!result.round.toolCalls.length || result.response.type !== ChatFetchResponseType.Success) {
loop.telemetry.sendToolCallingTelemetry(result.toolCallRounds, result.availableTools, this.token.isCancellationRequested ? 'cancelled' : result.response.type);
}
result.chatResult ??= {};
if ((result.chatResult.metadata as IResultMetadata)?.maxToolCallsExceeded) {
loop.telemetry.sendToolCallingTelemetry(result.toolCallRounds, result.availableTools, 'maxToolCalls');
}
// TODO need proper typing for all chat metadata and a better pattern to build it up from random places
result.chatResult = this.resultWithMetadatas(result.chatResult);
return { ...result, lastRequestTelemetry: loop.telemetry };
} finally {
await Promise.allSettled(responseHandlers);
store.dispose();
}
}
private resultWithMetadatas(chatResult: ChatResult | undefined): ChatResult | undefined {
const codeBlocks = this.turn.getMetadata(CodeBlocksMetadata);
const allSummarizedConversationHistory = this.turn.getAllMetadata(SummarizedConversationHistoryMetadata);
const renderedUserMessageMetadata = this.turn.getMetadata(RenderedUserMessageMetadata);
const globalContextMetadata = this.turn.getMetadata(GlobalContextMessageMetadata);
const turnTokenUsageMetadata = this.turn.getMetadata(TurnTokenUsageMetadata);
return codeBlocks || allSummarizedConversationHistory?.length || renderedUserMessageMetadata || globalContextMetadata || turnTokenUsageMetadata ?
{
...chatResult,
metadata: {
...chatResult?.metadata,
...codeBlocks,
...allSummarizedConversationHistory && allSummarizedConversationHistory.length > 0 && { summaries: allSummarizedConversationHistory },
...renderedUserMessageMetadata,
...globalContextMetadata,
...turnTokenUsageMetadata,
} satisfies Partial<IResultMetadata>,
} : chatResult;
}
private async handleConfirmationsIfNeeded(): Promise<ChatResult | undefined> {
const intentInvocation = this.turn.getMetadata(IntentInvocationMetadata)?.value;
assertType(intentInvocation);
if ((this.request.acceptedConfirmationData?.length || this.request.rejectedConfirmationData?.length) && intentInvocation.confirmationHandler) {
await intentInvocation.confirmationHandler(this.request.acceptedConfirmationData, this.request.rejectedConfirmationData, this.stream);
return {};
}
}
private async processSuccessfulFetchResult(appliedText: string, requestId: string, chatResult: ChatResult, baseModelTelemetry: ConversationalBaseTelemetryData, rounds: IToolCallRound[]): Promise<ChatResult> {
if (appliedText.length === 0 && !rounds.some(r => r.toolCalls.length)) {
const message = l10n.t('The model unexpectedly did not return a response. Request ID: {0}', requestId);
this.turn.setResponse(TurnStatus.Error, { type: 'meta', message }, baseModelTelemetry.properties.messageId, chatResult);
return {
errorDetails: {
message
},
};
}
this.turn.setResponse(TurnStatus.Success, { type: 'model', message: appliedText }, baseModelTelemetry.properties.messageId, chatResult);
baseModelTelemetry.markAsDisplayed();
sendModelMessageTelemetry(
this._telemetryService,
this.conversation,
this.location,
appliedText,
requestId,
this.documentContext?.document,
baseModelTelemetry,
this.getModeNameForTelemetry()
);
return chatResult;
}
private getModeNameForTelemetry(): string {
const modeInstructionsName = this.request.modeInstructions2?.name?.toLowerCase();
if (modeInstructionsName) {
return this.request.modeInstructions2?.isBuiltin ? this.request.modeInstructions2.name.toLowerCase() : 'custom';
}
if (this.intent.id === 'editAgent') {
return 'agent';
}
if (this.intent.id === 'edit') {
return 'edit';
}
return 'ask';
}
private processOffTopicFetchResult(baseModelTelemetry: ConversationalBaseTelemetryData): ChatResult {
// Create starting off topic telemetry and mark event as issued and displayed
this.stream.markdown(this.options.rejectionMessage);
this.turn.setResponse(TurnStatus.OffTopic, { message: this.options.rejectionMessage, type: 'offtopic-detection' }, baseModelTelemetry.properties.messageId, {});
return {};
}
private async getErrorDetails(error: ChatFetchError) {
const status = await this._octoKitService.getGitHubOutageStatus();
return getErrorDetailsFromChatFetchError(error, this._authenticationService.copilotToken?.copilotPlan, status, this._authenticationService.copilotToken?.tokenBasedBilling, this._authenticationService.copilotToken?.quotaInfo.quota_reset_date);
}
private async processResult(fetchResult: ChatResponse, responseMessage: string, chatResult: ChatResult | void, metadataFragment: Partial<IResultMetadata>, baseModelTelemetry: ConversationalBaseTelemetryData, rounds: IToolCallRound[]): Promise<ChatResult> {
switch (fetchResult.type) {
case ChatFetchResponseType.Success:
return await this.processSuccessfulFetchResult(responseMessage, fetchResult.requestId, chatResult ?? {}, baseModelTelemetry, rounds);
case ChatFetchResponseType.OffTopic:
return this.processOffTopicFetchResult(baseModelTelemetry);
case ChatFetchResponseType.Canceled: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: metadataFragment };
this.turn.setResponse(TurnStatus.Cancelled, { message: errorDetails.message, type: 'user' }, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.QuotaExceeded:
case ChatFetchResponseType.RateLimited: {
const errorDetails = await this.getErrorDetails(fetchResult);
if (fetchResult.type === ChatFetchResponseType.RateLimited
&& fetchResult.capiError?.code?.startsWith('user_model_rate_limited')
&& !fetchResult.isAuto) {
if (this._configurationService.getConfig(ConfigKey.RateLimitAutoSwitchToAuto)) {
metadataFragment.shouldAutoSwitchToAuto = true;
} else {
errorDetails.confirmationButtons = [
{ data: { copilotSwitchToAutoOnRateLimit: true, alwaysSwitchToAuto: true } satisfies ISwitchToAutoOnRateLimitConfirmation, label: l10n.t('Switch to Auto (always)') },
{ data: { copilotSwitchToAutoOnRateLimit: true, alwaysSwitchToAuto: false } satisfies ISwitchToAutoOnRateLimitConfirmation, label: l10n.t('Switch to Auto') },
];
}
}
const chatResult = { errorDetails, metadata: metadataFragment };
this.turn.setResponse(TurnStatus.Error, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.BadRequest:
case ChatFetchResponseType.NetworkError:
case ChatFetchResponseType.Failed: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: metadataFragment };
this.turn.setResponse(TurnStatus.Error, { message: errorDetails.message, type: 'server' }, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.Filtered: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: { ...metadataFragment, filterReason: fetchResult.category } };
this.turn.setResponse(TurnStatus.Filtered, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.PromptFiltered: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: { ...metadataFragment, filterReason: FilterReason.Prompt } };
this.turn.setResponse(TurnStatus.PromptFiltered, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.AgentUnauthorized: {
const chatResult = {};
this.turn.setResponse(TurnStatus.Error, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.AgentFailedDependency: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: metadataFragment };
this.turn.setResponse(TurnStatus.Error, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.Length: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: metadataFragment };
this.turn.setResponse(TurnStatus.Error, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.NotFound: // before we had `NotFound`, it would fall into Unknown, so behavior should be consistent
case ChatFetchResponseType.Unknown: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: metadataFragment };
this.turn.setResponse(TurnStatus.Error, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.ExtensionBlocked: {
const errorDetails = await this.getErrorDetails(fetchResult);
const chatResult = { errorDetails, metadata: metadataFragment };
// This shouldn't happen, only 3rd party extensions should be blocked
this.turn.setResponse(TurnStatus.Error, undefined, baseModelTelemetry.properties.messageId, chatResult);
return chatResult;
}
case ChatFetchResponseType.InvalidStatefulMarker:
throw new Error('unreachable'); // retried within the endpoint
}
}
}
interface IInternalRequestResult {
response: ChatResponse;
round: IToolCallRound;
chatResult?: ChatResult; // TODO should just be metadata
hadIgnoredFiles: boolean;
lastRequestMessages: Raw.ChatMessage[];
lastRequestTelemetry: ChatTelemetry;
}
interface IDefaultToolLoopOptions extends IToolCallingLoopOptions {
invocation: IIntentInvocation;
intent: IIntent;
documentContext: IDocumentContext | undefined;
location: ChatLocation;
temperature: number;
overrideRequestLocation?: ChatLocation;
}
class DefaultToolCallingLoop extends ToolCallingLoop<IDefaultToolLoopOptions> {
public telemetry!: ChatTelemetry;
private toolGrouping?: IToolGrouping;
constructor(
options: IDefaultToolLoopOptions,
telemetryBuilder: ChatTelemetryBuilder,
@IInstantiationService instantiationService: IInstantiationService,
@ILogService logService: ILogService,
@IRequestLogger requestLogger: IRequestLogger,
@IEndpointProvider endpointProvider: IEndpointProvider,
@IAuthenticationChatUpgradeService authenticationChatUpgradeService: IAuthenticationChatUpgradeService,
@ITelemetryService telemetryService: ITelemetryService,
@IExperimentationService experimentationService: IExperimentationService,
@IConfigurationService configurationService: IConfigurationService,
@IToolGroupingService private readonly toolGroupingService: IToolGroupingService,
@IChatHookService chatHookService: IChatHookService,
@ISessionTranscriptService sessionTranscriptService: ISessionTranscriptService,
@IFileSystemService fileSystemService: IFileSystemService,
@IOTelService otelService: IOTelService,
@IGitService gitService: IGitService,
) {
super(options, instantiationService, endpointProvider, logService, requestLogger, authenticationChatUpgradeService, telemetryService, configurationService, experimentationService, chatHookService, sessionTranscriptService, fileSystemService, otelService, gitService);
this._register(this.onDidBuildPrompt(({ result, tools, promptTokenLength, toolTokenCount }) => {
if (result.metadata.get(SummarizedConversationHistoryMetadata)) {
this.toolGrouping?.didInvalidateCache();
}
this.telemetry = telemetryBuilder.makeRequest(
options.intent!,
options.location,
options.conversation,
result.messages,
promptTokenLength,
result.references,
options.invocation.endpoint,
result.metadata.getAll(TelemetryData) ?? [],
tools.length,
toolTokenCount
);
}));
this._register(this.onDidReceiveResponse(() => {
this.toolGrouping?.didTakeTurn();
}));
}
protected override createPromptContext(availableTools: LanguageModelToolInformation[], outputStream: ChatResponseStream | undefined): Mutable<IBuildPromptContext> {
const context = super.createPromptContext(availableTools, outputStream);
this._handleVirtualCalls(context);
const extraVars = this.options.invocation.getAdditionalVariables?.(context);
if (extraVars?.hasVariables()) {
return {
...context,
chatVariables: ChatVariablesCollection.merge(context.chatVariables, extraVars),
};
}
return context;
}
private _handleVirtualCalls(context: Mutable<IBuildPromptContext>) {
if (!this.toolGrouping) {
return;
}
for (const call of context.toolCallRounds?.at(-1)?.toolCalls || Iterable.empty()) {
if (context.toolCallResults?.[call.id]) {
continue;
}
const expanded = this.toolGrouping.didCall(context.toolCallRounds!.length, call.name);
if (expanded) {
context.toolCallResults ??= {};
context.toolCallResults[call.id] = expanded;
}
}
}
protected override async buildPrompt(buildPromptContext: IBuildPromptContext, progress: Progress<ChatResponseReferencePart | ChatResponseProgressPart>, token: CancellationToken): Promise<IBuildPromptResult> {
const buildPromptResult = await this.options.invocation.buildPrompt(buildPromptContext, progress, token);
this.fixMessageNames(buildPromptResult.messages);
return buildPromptResult;
}
protected override async fetch(opts: ToolCallingLoopFetchOptions, token: CancellationToken): Promise<ChatResponse> {
const messageSourcePrefix = this.options.location === ChatLocation.Editor ? 'inline' : 'chat';
const debugName = this.options.request.subAgentInvocationId ?
`tool/runSubagent${this.options.request.subAgentName ? `-${this.options.request.subAgentName}` : ''}` :
`${ChatLocation.toStringShorter(this.options.location)}/${this.options.intent?.id}`;
const location = this.options.overrideRequestLocation ?? this.options.location;
const isThinkingLocation = location === ChatLocation.Agent || location === ChatLocation.MessagesProxy;
const rawEffort = this.options.request.modelConfiguration?.reasoningEffort;
const reasoningEffort = typeof rawEffort === 'string' ? rawEffort : undefined;
const isSubagent = !!this.options.request.subAgentInvocationId;
const modeChanged = this.didModeChangeSincePreviousRequest();
return this.options.invocation.endpoint.makeChatRequest2({
...opts,
modeChanged,
modelCapabilities: {
...opts.modelCapabilities,
enableThinking: isThinkingLocation && opts.modelCapabilities?.enableThinking,
reasoningEffort,
enableToolSearch: !isSubagent && !!this.options.invocation.endpoint.supportsToolSearch,
enableContextEditing: !isSubagent && isAnthropicContextEditingEnabled(this.options.invocation.endpoint, this._configurationService, this._experimentationService),
},
debugName,
conversationId: this.options.conversation.sessionId,
turnId: opts.turnId,
finishedCb: (text, index, delta) => {
this.telemetry.markReceivedToken();
return opts.finishedCb!(text, index, delta);
},
location,
requestOptions: {
...opts.requestOptions,
tools: normalizeToolSchema(
this.options.invocation.endpoint.family,
opts.requestOptions.tools,
(tool, rule) => {
this._logService.warn(`Tool ${tool} failed validation: ${rule}`);
},
),
temperature: this.calculateTemperature(),
},
telemetryProperties: {
messageId: this.telemetry.telemetryMessageId,
conversationId: this.options.conversation.sessionId,
messageSource: this.options.intent?.id && this.options.intent.id !== UnknownIntent.ID ? `${messageSourcePrefix}.${this.options.intent.id}` : `${messageSourcePrefix}.user`,
subType: this.options.request.subAgentInvocationId ? `subagent` : this.options.request.isSystemInitiated ? 'system-initiated' : undefined,
parentRequestId: this.options.request.parentRequestId,
iterationNumber: opts.iterationNumber.toString(),
},
interactionTypeOverride: this.options.request.subAgentInvocationId ? 'conversation-subagent' : undefined,
enableRetryOnFilter: true
}, token);
}
private didModeChangeSincePreviousRequest(): boolean {
if (this.options.invocation.endpoint.apiType !== 'responses') {
return false;
}
const previousTurn = this.options.conversation.turns.at(-2);
if (!previousTurn) {
return false;
}
// Once a mode-switched turn has successfully produced a fresh responses-api
// stateful marker, later requests in the same turn should resume from that
// new chain instead of continuing to invalidate previous_response_id.
// This is especially important for websocket follow-up requests after tool
// calls: keeping modeChanged=true for the entire turn would force the full
// pre-switch history back into every follow-up request, which can pull the
// model back toward the prior mode (for example implementation after
// switching into Plan mode).
if (this.currentToolCallRounds.some(round => !!round.statefulMarker)) {
return false;
}
const previousModeInstructions = previousTurn.modeInstructions;
if (!previousModeInstructions && !this.options.request.modeInstructions2) {
return false;
}
const modeChanged = !areModeInstructionsEqual(previousModeInstructions, this.options.request.modeInstructions2);
if (modeChanged) {
this._logService.trace('[DefaultIntentRequestHandler] Detected mode instructions changed between requests');
}
return modeChanged;
}
protected override async getAvailableTools(outputStream: ChatResponseStream | undefined, token: CancellationToken): Promise<LanguageModelToolInformation[]> {
const tools = await this.options.invocation.getAvailableTools?.() ?? [];
if (this.toolGrouping) {
this.toolGrouping.tools = tools;
} else {
this.toolGrouping = this.toolGroupingService.create(this.options.conversation.sessionId, tools);
for (const ref of this.options.request.toolReferences) {
this.toolGrouping.ensureExpanded(ref.name);
}
}
const computePromise = this.toolGrouping.compute(this.options.request.prompt, token); // Show progress if this takes a moment...
const timeout = setTimeout(() => {
outputStream?.progress(l10n.t('Optimizing tool selection'), async () => {
await computePromise;
return l10n.t('Optimized tool selection');
});
}, 1000);
try {
return await computePromise;
} finally {
clearTimeout(timeout);
}
}
private fixMessageNames(messages: Raw.ChatMessage[]): void {
messages.forEach(m => {
if (m.role !== Raw.ChatRole.System && 'name' in m && m.name === this.options.intent?.id) {
// Assistant messages from the current intent should not have 'name' set.
// It's not well-documented how this works in OpenAI models but this seems to be the expectation
m.name = undefined;
}
});
}
private calculateTemperature(): number {
if (this.options.request.attempt > 0) {
return Math.min(
this.options.temperature * (this.options.request.attempt + 1),
2 /* MAX temperature - https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature */
);
} else {
return this.options.temperature;
}
}
}
interface IInternalRequestResult extends IToolCallLoopResult {
lastRequestTelemetry: ChatTelemetry;
}
type ModeInstructions = NonNullable<ChatRequest['modeInstructions2']>;
type ModeInstructionMetadata = ModeInstructions['metadata'];
function areModeInstructionsEqual(a: ChatRequest['modeInstructions2'], b: ChatRequest['modeInstructions2']): boolean {
if (!a || !b) {
return a === b;
}
return a.uri?.toString() === b.uri?.toString()
&& a.name === b.name
&& a.content === b.content
&& a.isBuiltin === b.isBuiltin
&& serializeModeInstructionMetadata(a.metadata) === serializeModeInstructionMetadata(b.metadata);
}
function normalizeModeInstructionMetadata(metadata: ModeInstructionMetadata): Record<string, boolean | string | number> | undefined {
if (!metadata) {
return undefined;
}
const entries = Object.entries(metadata).sort(([left], [right]) => left.localeCompare(right));
if (entries.length === 0) {
return undefined;
}
return entries.reduce<Record<string, boolean | string | number>>((result, [key, value]) => {
result[key] = value;
return result;
}, {});
}
function serializeModeInstructionMetadata(metadata: ModeInstructionMetadata): string | undefined {
const normalizedMetadata = normalizeModeInstructionMetadata(metadata);
return normalizedMetadata ? JSON.stringify(normalizedMetadata) : undefined;
}