Path: blob/main/extensions/copilot/src/extension/prompt/node/codebaseToolCalling.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 { randomUUID } from 'crypto';6import type { CancellationToken, ChatRequest, LanguageModelToolInformation, Progress } from 'vscode';7import { IAuthenticationChatUpgradeService } from '../../../platform/authentication/common/authenticationUpgrade';8import { IChatHookService } from '../../../platform/chat/common/chatHookService';9import { ChatLocation, ChatResponse } from '../../../platform/chat/common/commonTypes';10import { ISessionTranscriptService } from '../../../platform/chat/common/sessionTranscriptService';11import { IConfigurationService } from '../../../platform/configuration/common/configurationService';12import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';13import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';14import { IGitService } from '../../../platform/git/common/gitService';15import { ILogService } from '../../../platform/log/common/logService';16import { IOTelService } from '../../../platform/otel/common/otelService';17import { IRequestLogger } from '../../../platform/requestLogger/common/requestLogger';18import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';19import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';20import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';21import { ChatResponseProgressPart, ChatResponseReferencePart } from '../../../vscodeTypes';22import { IToolCallingLoopOptions, ToolCallingLoop, ToolCallingLoopFetchOptions } from '../../intents/node/toolCallingLoop';23import { PromptRenderer } from '../../prompts/node/base/promptRenderer';24import { CodebaseAgentPrompt } from '../../prompts/node/panel/codebaseAgentPrompt';25import { IToolsService } from '../../tools/common/toolsService';26import { IBuildPromptContext } from '../common/intents';27import { IBuildPromptResult } from './intents';2829export interface ICodebaseToolCallingLoopOptions extends IToolCallingLoopOptions {30request: ChatRequest;31location: ChatLocation;32}3334export class CodebaseToolCallingLoop extends ToolCallingLoop<ICodebaseToolCallingLoopOptions> {3536public static readonly ID = 'codebaseTool';3738constructor(39options: ICodebaseToolCallingLoopOptions,40@IInstantiationService private readonly instantiationService: IInstantiationService,41@ILogService logService: ILogService,42@IRequestLogger requestLogger: IRequestLogger,43@IEndpointProvider private readonly endpointProvider: IEndpointProvider,44@IToolsService private readonly toolsService: IToolsService,45@IAuthenticationChatUpgradeService authenticationChatUpgradeService: IAuthenticationChatUpgradeService,46@ITelemetryService telemetryService: ITelemetryService,47@IConfigurationService configurationService: IConfigurationService,48@IExperimentationService experimentationService: IExperimentationService,49@IChatHookService chatHookService: IChatHookService,50@ISessionTranscriptService sessionTranscriptService: ISessionTranscriptService,51@IFileSystemService fileSystemService: IFileSystemService,52@IOTelService otelService: IOTelService,53@IGitService gitService: IGitService,54) {55super(options, instantiationService, endpointProvider, logService, requestLogger, authenticationChatUpgradeService, telemetryService, configurationService, experimentationService, chatHookService, sessionTranscriptService, fileSystemService, otelService, gitService);56}5758private async getEndpoint(request: ChatRequest) {59let endpoint = await this.endpointProvider.getChatEndpoint(this.options.request);60if (!endpoint.supportsToolCalls) {61endpoint = await this.endpointProvider.getChatEndpoint('copilot-base');62}63return endpoint;64}6566protected async buildPrompt(buildPromptContext: IBuildPromptContext, progress: Progress<ChatResponseReferencePart | ChatResponseProgressPart>, token: CancellationToken): Promise<IBuildPromptResult> {67const endpoint = await this.getEndpoint(this.options.request);68const renderer = PromptRenderer.create(69this.instantiationService,70endpoint,71CodebaseAgentPrompt,72{73promptContext: buildPromptContext74}75);76return await renderer.render(progress, token);77}7879protected async getAvailableTools(): Promise<LanguageModelToolInformation[]> {80const endpoint = await this.getEndpoint(this.options.request);81return this.toolsService.getEnabledTools(this.options.request, endpoint, tool => tool.tags.includes('vscode_codesearch'));82}8384protected async fetch({ messages, finishedCb, requestOptions }: ToolCallingLoopFetchOptions, token: CancellationToken): Promise<ChatResponse> {85const endpoint = await this.getEndpoint(this.options.request);86return endpoint.makeChatRequest(87CodebaseToolCallingLoop.ID,88messages,89finishedCb,90token,91this.options.location,92undefined,93{94...requestOptions,95temperature: 096},97// This loop is inside a tool called from another request, so never user initiated98false,99{100messageId: randomUUID(), // @TODO@joyceerhl101messageSource: CodebaseToolCallingLoop.ID102},103);104}105}106107108