Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/mcp/vscode-node/mcpToolCallingLoop.tsx
13400 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 { CancellationToken, LanguageModelToolInformation, Progress } from 'vscode';
7
import { IAuthenticationChatUpgradeService } from '../../../platform/authentication/common/authenticationUpgrade';
8
import { IChatHookService } from '../../../platform/chat/common/chatHookService';
9
import { ChatLocation, ChatResponse } from '../../../platform/chat/common/commonTypes';
10
import { ISessionTranscriptService } from '../../../platform/chat/common/sessionTranscriptService';
11
import { IConfigurationService } from '../../../platform/configuration/common/configurationService';
12
import { IEndpointProvider } from '../../../platform/endpoint/common/endpointProvider';
13
import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
14
import { IGitService } from '../../../platform/git/common/gitService';
15
import { ILogService } from '../../../platform/log/common/logService';
16
import { IOTelService } from '../../../platform/otel/common/otelService';
17
import { IRequestLogger } from '../../../platform/requestLogger/common/requestLogger';
18
import { IExperimentationService } from '../../../platform/telemetry/common/nullExperimentationService';
19
import { ITelemetryService } from '../../../platform/telemetry/common/telemetry';
20
import { IInstantiationService } from '../../../util/vs/platform/instantiation/common/instantiation';
21
import { ChatResponseProgressPart, ChatResponseReferencePart } from '../../../vscodeTypes';
22
import { IToolCallingLoopOptions, ToolCallingLoop, ToolCallingLoopFetchOptions } from '../../intents/node/toolCallingLoop';
23
import { IBuildPromptContext } from '../../prompt/common/intents';
24
import { IBuildPromptResult } from '../../prompt/node/intents';
25
import { PromptRenderer } from '../../prompts/node/base/promptRenderer';
26
import { IMcpToolCallingLoopPromptContext, McpToolCallingLoopPrompt } from './mcpToolCallingLoopPrompt';
27
import { QuickInputTool, QuickPickTool } from './mcpToolCallingTools';
28
29
export interface IMcpToolCallingLoopOptions extends IToolCallingLoopOptions {
30
props: IMcpToolCallingLoopPromptContext;
31
}
32
33
export class McpToolCallingLoop extends ToolCallingLoop<IMcpToolCallingLoopOptions> {
34
public static readonly ID = 'mcpToolSetupLoop';
35
36
constructor(
37
options: IMcpToolCallingLoopOptions,
38
@IInstantiationService private readonly instantiationService: IInstantiationService,
39
@ILogService logService: ILogService,
40
@IRequestLogger requestLogger: IRequestLogger,
41
@IEndpointProvider private readonly endpointProvider: IEndpointProvider,
42
@IAuthenticationChatUpgradeService authenticationChatUpgradeService: IAuthenticationChatUpgradeService,
43
@ITelemetryService telemetryService: ITelemetryService,
44
@IConfigurationService configurationService: IConfigurationService,
45
@IExperimentationService experimentationService: IExperimentationService,
46
@IChatHookService chatHookService: IChatHookService,
47
@ISessionTranscriptService sessionTranscriptService: ISessionTranscriptService,
48
@IFileSystemService fileSystemService: IFileSystemService,
49
@IOTelService otelService: IOTelService,
50
@IGitService gitService: IGitService,
51
) {
52
super(options, instantiationService, endpointProvider, logService, requestLogger, authenticationChatUpgradeService, telemetryService, configurationService, experimentationService, chatHookService, sessionTranscriptService, fileSystemService, otelService, gitService);
53
}
54
55
private async getEndpoint() {
56
return await this.endpointProvider.getChatEndpoint('copilot-fast');
57
}
58
59
protected async buildPrompt(buildPromptContext: IBuildPromptContext, progress: Progress<ChatResponseReferencePart | ChatResponseProgressPart>, token: CancellationToken): Promise<IBuildPromptResult> {
60
const endpoint = await this.getEndpoint();
61
const renderer = PromptRenderer.create(
62
this.instantiationService,
63
endpoint,
64
McpToolCallingLoopPrompt,
65
{
66
promptContext: buildPromptContext,
67
...this.options.props
68
}
69
);
70
return await renderer.render(progress, token);
71
}
72
73
protected async getAvailableTools(): Promise<LanguageModelToolInformation[]> {
74
if (this.options.conversation.turns.length > 5) {
75
return []; // force a response
76
}
77
78
return [{
79
description: QuickInputTool.description,
80
name: QuickInputTool.ID,
81
inputSchema: QuickInputTool.schema,
82
source: undefined,
83
tags: [],
84
}, {
85
description: QuickPickTool.description,
86
name: QuickPickTool.ID,
87
inputSchema: QuickPickTool.schema,
88
source: undefined,
89
tags: [],
90
}];
91
}
92
93
protected async fetch(opts: ToolCallingLoopFetchOptions, token: CancellationToken): Promise<ChatResponse> {
94
const endpoint = await this.getEndpoint();
95
return endpoint.makeChatRequest2({
96
...opts,
97
debugName: McpToolCallingLoop.ID,
98
location: ChatLocation.Agent,
99
requestOptions: {
100
...opts.requestOptions,
101
temperature: 0
102
},
103
}, token);
104
}
105
}
106
107