Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/intents/node/askAgentIntent.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 { ChatLocation } from '../../../platform/chat/common/commonTypes';
8
import { ConfigKey, IConfigurationService } from '../../../platform/configuration/common/configurationService';
9
import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';
10
import { IAutomodeService } from '../../../platform/endpoint/node/automodeService';
11
import { IEnvService } from '../../../platform/env/common/envService';
12
import { ILogService } from '../../../platform/log/common/logService';
13
import { IEditLogService } from '../../../platform/multiFileEdit/common/editLogService';
14
import { IChatEndpoint } from '../../../platform/networking/common/networking';
15
import { INotebookService } from '../../../platform/notebook/common/notebookService';
16
import { IOTelService } from '../../../platform/otel/common/otelService';
17
import { ISessionTranscriptService } from '../../../platform/chat/common/sessionTranscriptService';
18
import { IPromptPathRepresentationService } from '../../../platform/prompts/common/promptPathRepresentationService';
19
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
20
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
21
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
22
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
23
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
24
import { ICommandService } from '../../commands/node/commandService';
25
import { Intent } from '../../common/constants';
26
import { Conversation } from '../../prompt/common/conversation';
27
import { getRequestedToolCallIterationLimit } from '../../prompt/common/specialRequestTypes';
28
import { ChatTelemetryBuilder } from '../../prompt/node/chatParticipantTelemetry';
29
import { DefaultIntentRequestHandler, IDefaultIntentRequestHandlerOptions } from '../../prompt/node/defaultIntentRequestHandler';
30
import { IDocumentContext } from '../../prompt/node/documentContext';
31
import { IIntent, IIntentInvocationContext, IntentLinkificationOptions } from '../../prompt/node/intents';
32
import { AgentPrompt } from '../../prompts/node/agent/agentPrompt';
33
import { ICodeMapperService } from '../../prompts/node/codeMapper/codeMapperService';
34
import { IToolsService } from '../../tools/common/toolsService';
35
import { getAgentMaxRequests } from '../common/agentConfig';
36
import { AgentIntentInvocation } from './agentIntent';
37
38
39
const getTools = (instaService: IInstantiationService, request: vscode.ChatRequest): Promise<vscode.LanguageModelToolInformation[]> =>
40
instaService.invokeFunction(async accessor => {
41
const toolsService = accessor.get<IToolsService>(IToolsService);
42
const lookForTags = new Set<string>(['vscode_codesearch']);
43
const endpointProvider = accessor.get<IEndpointProvider>(IEndpointProvider);
44
const model = await endpointProvider.getChatEndpoint(request);
45
46
// Special case...
47
// Since AskAgent currently has no tool picker, have to duplicate the toolReference logic here.
48
// When it's no longer experimental, it should be a custom mode, have a tool picker, etc.
49
// And must return boolean to avoid falling back on other logic that we don't want, like the `extension_installed_by_tool` check.
50
return toolsService.getEnabledTools(request, model, tool => tool.tags.some(tag => lookForTags.has(tag)) || request.toolReferences.some(ref => ref.name === tool.name));
51
});
52
53
export class AskAgentIntent implements IIntent {
54
55
static readonly ID = Intent.AskAgent;
56
57
readonly id = AskAgentIntent.ID;
58
59
readonly description = 'unused';
60
readonly locations = [ChatLocation.Panel];
61
62
constructor(
63
@IInstantiationService private readonly instantiationService: IInstantiationService,
64
@IEndpointProvider private readonly endpointProvider: IEndpointProvider,
65
@IConfigurationService private readonly configurationService: IConfigurationService,
66
) { }
67
68
private getIntentHandlerOptions(request: vscode.ChatRequest): IDefaultIntentRequestHandlerOptions | undefined {
69
return {
70
maxToolCallIterations: getRequestedToolCallIterationLimit(request) ?? this.instantiationService.invokeFunction(getAgentMaxRequests),
71
temperature: this.configurationService.getConfig(ConfigKey.Advanced.AgentTemperature) ?? 0,
72
overrideRequestLocation: ChatLocation.EditingSession,
73
};
74
}
75
76
async handleRequest(conversation: Conversation, request: vscode.ChatRequest, stream: vscode.ChatResponseStream, token: CancellationToken, documentContext: IDocumentContext | undefined, agentName: string, location: ChatLocation, chatTelemetry: ChatTelemetryBuilder): Promise<vscode.ChatResult> {
77
const actual = this.instantiationService.createInstance(
78
DefaultIntentRequestHandler,
79
this,
80
conversation,
81
request,
82
stream,
83
token,
84
documentContext,
85
location,
86
chatTelemetry,
87
this.getIntentHandlerOptions(request),
88
undefined,
89
);
90
return await actual.getResult();
91
}
92
93
async invoke(invocationContext: IIntentInvocationContext) {
94
const { location, request } = invocationContext;
95
const endpoint = await this.endpointProvider.getChatEndpoint(request);
96
97
return this.instantiationService.createInstance(AskAgentIntentInvocation, this, location, endpoint, request);
98
}
99
}
100
101
export class AskAgentIntentInvocation extends AgentIntentInvocation {
102
103
public override get linkification(): IntentLinkificationOptions {
104
return { disable: false };
105
}
106
107
protected override prompt = AgentPrompt;
108
109
protected override extraPromptProps = { codesearchMode: true };
110
111
constructor(
112
intent: IIntent,
113
location: ChatLocation,
114
endpoint: IChatEndpoint,
115
request: vscode.ChatRequest,
116
@IInstantiationService instantiationService: IInstantiationService,
117
@ICodeMapperService codeMapperService: ICodeMapperService,
118
@IEnvService envService: IEnvService,
119
@IPromptPathRepresentationService promptPathRepresentationService: IPromptPathRepresentationService,
120
@IEndpointProvider endpointProvider: IEndpointProvider,
121
@IWorkspaceService workspaceService: IWorkspaceService,
122
@IToolsService toolsService: IToolsService,
123
@IConfigurationService configurationService: IConfigurationService,
124
@IEditLogService editLogService: IEditLogService,
125
@ICommandService commandService: ICommandService,
126
@ITelemetryService telemetryService: ITelemetryService,
127
@INotebookService notebookService: INotebookService,
128
@ILogService logService: ILogService,
129
@IExperimentationService expService: IExperimentationService,
130
@IAutomodeService automodeService: IAutomodeService,
131
@IOTelService otelService: IOTelService,
132
@ISessionTranscriptService sessionTranscriptService: ISessionTranscriptService,
133
) {
134
super(intent, location, endpoint, request, { processCodeblocks: true }, instantiationService, codeMapperService, envService, promptPathRepresentationService, endpointProvider, workspaceService, toolsService, configurationService, editLogService, commandService, telemetryService, notebookService, logService, expService, automodeService, otelService, sessionTranscriptService);
135
}
136
137
public override async getAvailableTools(): Promise<vscode.LanguageModelToolInformation[]> {
138
return getTools(this.instantiationService, this.request);
139
}
140
}
141
142