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