Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompt/node/title.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 type * as vscode from 'vscode';
7
import { sessionResourceToId } from '../../../platform/chat/common/chatDebugFileLoggerService';
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 { ChatRequestTurn } from '../../../vscodeTypes';
16
import { renderPromptElement } from '../../prompts/node/base/promptRenderer';
17
import { TitlePrompt } from '../../prompts/node/panel/title';
18
19
export class ChatTitleProvider implements vscode.ChatTitleProvider {
20
21
constructor(
22
@ILogService private readonly logService: ILogService,
23
@IEndpointProvider private endpointProvider: IEndpointProvider,
24
@IInstantiationService private readonly instantiationService: IInstantiationService,
25
@IRequestLogger private readonly requestLogger: IRequestLogger,
26
) { }
27
28
async provideChatTitle(
29
context: vscode.ChatContext,
30
token: vscode.CancellationToken,
31
): Promise<string | undefined> {
32
33
// Get the first user message directly from the context
34
// Use instanceof to properly check if the first item is a ChatRequestTurn
35
const firstRequest = context.history.find(item => item instanceof ChatRequestTurn);
36
if (!firstRequest) {
37
return '';
38
}
39
40
// Extract the parent session ID from the context's sessionResource (provided by VS Code)
41
const sessionResource = context.sessionResource;
42
const parentChatSessionId = sessionResource ? sessionResourceToId(URI.from(sessionResource)) : undefined;
43
44
const endpoint = await this.endpointProvider.getChatEndpoint('copilot-fast');
45
const { messages } = await renderPromptElement(this.instantiationService, endpoint, TitlePrompt, { userRequest: firstRequest.prompt });
46
47
const capturingToken = new CapturingToken(
48
'title',
49
undefined,
50
undefined,
51
undefined,
52
undefined,
53
parentChatSessionId,
54
'title',
55
);
56
57
const doRequest = async () => {
58
const response = await endpoint.makeChatRequest2({
59
debugName: 'title',
60
messages,
61
finishedCb: undefined,
62
location: ChatLocation.Panel,
63
userInitiatedRequest: false,
64
isConversationRequest: false,
65
}, token);
66
return response;
67
};
68
69
const response = await this.requestLogger.captureInvocation(capturingToken, doRequest);
70
if (token.isCancellationRequested) {
71
return '';
72
}
73
74
if (response.type === ChatFetchResponseType.Success) {
75
let title = response.value.trim();
76
if (title.match(/^".*"$/)) {
77
title = title.slice(1, -1);
78
}
79
80
if (title.includes('can\'t assist with that')) {
81
return undefined;
82
}
83
84
return title;
85
} else {
86
this.logService.error(`Failed to fetch conversation title because of response type (${response.type}) and reason (${response.reason})`);
87
return '';
88
}
89
}
90
}
91
92