Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/node/summarizer.ts
13399 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { Raw } from '@vscode/prompt-tsx';
7
import type * as vscode from 'vscode';
8
import { ChatFetchResponseType, ChatLocation } from '../../../platform/chat/common/commonTypes';
9
import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';
10
import { ILogService } from '../../../platform/log/common/logService';
11
import { CapturingToken } from '../../../platform/requestLogger/common/capturingToken';
12
import { IRequestLogger } from '../../../platform/requestLogger/common/requestLogger';
13
import { URI } from '../../../util/vs/base/common/uri';
14
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
15
import { ConversationHistorySummarizationPrompt } from '../../prompts/node/agent/summarizedConversationHistory';
16
import { renderPromptElement } from '../../prompts/node/base/promptRenderer';
17
import { ChatVariablesCollection } from '../common/chatVariablesCollection';
18
import { TurnStatus } from '../common/conversation';
19
import { IBuildPromptContext } from '../common/intents';
20
import { addHistoryToConversation } from './chatParticipantRequestHandler';
21
22
import { sessionResourceToId } from '../../../platform/chat/common/chatDebugFileLoggerService';
23
24
export class ChatSummarizerProvider implements vscode.ChatSummarizer {
25
26
constructor(
27
@ILogService private readonly logService: ILogService,
28
@IEndpointProvider private endpointProvider: IEndpointProvider,
29
@IInstantiationService private readonly instantiationService: IInstantiationService,
30
@IRequestLogger private readonly requestLogger: IRequestLogger,
31
) { }
32
33
async provideChatSummary(
34
context: vscode.ChatContext,
35
token: vscode.CancellationToken,
36
): Promise<string> {
37
38
const { turns } = this.instantiationService.invokeFunction(accessor => addHistoryToConversation(accessor, context.history));
39
if (turns.filter(t => t.responseStatus === TurnStatus.Success).length === 0) {
40
return '';
41
}
42
43
const endpoint = await this.endpointProvider.getChatEndpoint('copilot-fast');
44
const promptContext: IBuildPromptContext = {
45
requestId: 'chat-summary',
46
query: '',
47
history: turns,
48
chatVariables: new ChatVariablesCollection(),
49
isContinuation: false,
50
toolCallRounds: undefined,
51
toolCallResults: undefined,
52
};
53
54
let allMessages: Raw.ChatMessage[];
55
try {
56
const rendered = await renderPromptElement(
57
this.instantiationService,
58
endpoint,
59
ConversationHistorySummarizationPrompt,
60
{
61
priority: 0,
62
endpoint,
63
location: ChatLocation.Panel,
64
promptContext,
65
maxToolResultLength: 2000,
66
triggerSummarize: false,
67
simpleMode: false,
68
maxSummaryTokens: 7_000,
69
},
70
undefined,
71
token
72
);
73
allMessages = rendered.messages;
74
} catch (err) {
75
this.logService.error(`Failed to render conversation summarization prompt: ${err instanceof Error ? err.message : String(err)}`);
76
return '';
77
}
78
79
// Extract the parent session ID from the context's sessionResource (provided by VS Code)
80
const sessionResource = context.sessionResource;
81
const parentChatSessionId = sessionResource ? sessionResourceToId(URI.from(sessionResource)) : undefined;
82
83
const capturingToken = new CapturingToken(
84
'summarize',
85
undefined,
86
undefined,
87
undefined,
88
undefined,
89
parentChatSessionId,
90
'summarize',
91
);
92
93
const response = await this.requestLogger.captureInvocation(capturingToken, () => endpoint.makeChatRequest(
94
'summarize',
95
allMessages,
96
undefined,
97
token,
98
ChatLocation.Panel,
99
undefined,
100
undefined,
101
false
102
));
103
104
if (token.isCancellationRequested) {
105
return '';
106
}
107
108
if (response.type === ChatFetchResponseType.Success) {
109
let summary = response.value.trim();
110
if (summary.match(/^".*"$/)) {
111
summary = summary.slice(1, -1);
112
}
113
return summary;
114
} else {
115
this.logService.error(`Failed to fetch conversation summary because of response type (${response.type}) and reason (${response.reason})`);
116
return '';
117
}
118
}
119
}
120
121