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