Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/agent/toolSearchInstructions.tsx
13405 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 { BasePromptElementProps, PromptElement, PromptElementProps, PromptSizing } from '@vscode/prompt-tsx';
7
import type { LanguageModelToolInformation } from 'vscode';
8
import { CUSTOM_TOOL_SEARCH_NAME } from '../../../../platform/networking/common/anthropic';
9
import { IChatEndpoint } from '../../../../platform/networking/common/networking';
10
import { IToolDeferralService } from '../../../../platform/networking/common/toolDeferralService';
11
import { Tag } from '../base/tag';
12
13
export interface ToolSearchToolPromptProps extends BasePromptElementProps {
14
readonly availableTools: readonly LanguageModelToolInformation[] | undefined;
15
}
16
17
export interface DeferredToolListReminderProps extends BasePromptElementProps {
18
readonly availableTools: readonly LanguageModelToolInformation[] | undefined;
19
}
20
21
/**
22
* True when `availableTools` contains at least one tool that the deferral
23
* service treats as deferred. Shared between the system-prompt guidance and
24
* the global-context list so they appear/disappear together.
25
*/
26
export function hasDeferredTool(
27
availableTools: readonly LanguageModelToolInformation[] | undefined,
28
toolDeferralService: IToolDeferralService,
29
): boolean {
30
return !!availableTools?.some(tool => !toolDeferralService.isNonDeferredTool(tool.name));
31
}
32
33
/**
34
* Condensed tool search instructions shared across model prompts.
35
* Renders deferred-tool search *guidance* when the endpoint supports tool
36
* search and at least one deferred tool is available. The list itself is
37
* rendered by `DeferredToolListReminder` inside the global agent context.
38
*/
39
export class ToolSearchToolPromptOptimized extends PromptElement<ToolSearchToolPromptProps> {
40
constructor(
41
props: PromptElementProps<ToolSearchToolPromptProps>,
42
@IToolDeferralService private readonly toolDeferralService: IToolDeferralService,
43
) {
44
super(props);
45
}
46
47
async render(state: void, sizing: PromptSizing) {
48
const endpoint = sizing.endpoint as IChatEndpoint | undefined;
49
if (!endpoint?.supportsToolSearch || !hasDeferredTool(this.props.availableTools, this.toolDeferralService)) {
50
return;
51
}
52
53
return <Tag name='toolSearchInstructions'>
54
You MUST use {CUSTOM_TOOL_SEARCH_NAME} to load deferred tools BEFORE calling them. Calling a deferred tool without loading it first will fail.<br />
55
<br />
56
Describe what capability you need in natural language. The search uses semantic similarity to find the most relevant tools.<br />
57
<br />
58
Do NOT call {CUSTOM_TOOL_SEARCH_NAME} again for a tool already returned by a previous search. If a search returns no matching tools, the tool is not available. Do not retry with different patterns.<br />
59
</Tag>;
60
}
61
}
62
63
/**
64
* Emits the list of deferred tools. Rendered inside `GlobalAgentContext` so it
65
* appears once at the start of the conversation and is then frozen for the
66
* remainder of the session via `GlobalContextMessageMetadata` — keeps the
67
* list out of every per-turn user message and out of the system prompt prefix.
68
*
69
* Self-gates on `endpoint.supportsToolSearch`. The surrounding `<Tag>` name
70
* matches the reference used by `tool_search`'s tool description.
71
*
72
* Note: the snapshot is taken at first render. Tools that become available
73
* later in the conversation (e.g. MCP servers connecting mid-session) won't
74
* appear in this list.
75
*/
76
export class DeferredToolListReminder extends PromptElement<DeferredToolListReminderProps> {
77
constructor(
78
props: PromptElementProps<DeferredToolListReminderProps>,
79
@IToolDeferralService private readonly toolDeferralService: IToolDeferralService,
80
) {
81
super(props);
82
}
83
84
async render(state: void, sizing: PromptSizing) {
85
const endpoint = sizing.endpoint as IChatEndpoint | undefined;
86
if (!endpoint?.supportsToolSearch || !this.props.availableTools) {
87
return;
88
}
89
90
const deferredTools = this.props.availableTools
91
.filter(tool => !this.toolDeferralService.isNonDeferredTool(tool.name))
92
.map(tool => tool.name)
93
.sort();
94
95
if (deferredTools.length === 0) {
96
return;
97
}
98
99
return <Tag name='availableDeferredTools'>
100
Available deferred tools (must be loaded with {CUSTOM_TOOL_SEARCH_NAME} before use):<br />
101
{deferredTools.join('\n')}
102
</Tag>;
103
}
104
}
105
106
export { CUSTOM_TOOL_SEARCH_NAME };
107
108