-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathbuild-tools.ts
More file actions
175 lines (150 loc) · 6.08 KB
/
build-tools.ts
File metadata and controls
175 lines (150 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import path from "path"
import type OpenAI from "openai"
import type { ProviderSettings, ModeConfig, ModelInfo } from "@roo-code/types"
import { customToolRegistry, formatNative } from "@roo-code/core"
import type { ClineProvider } from "../webview/ClineProvider"
import { getRooDirectoriesForCwd } from "../../services/roo-config/index.js"
import { getModeBySlug } from "../../shared/modes"
import { defaultModeSlug } from "../../shared/modes"
import { getNativeTools, getMcpServerTools } from "../prompts/tools/native-tools"
import {
filterNativeToolsForMode,
filterMcpToolsForMode,
resolveToolAlias,
} from "../prompts/tools/filter-tools-for-mode"
interface BuildToolsOptions {
provider: ClineProvider
cwd: string
mode: string | undefined
customModes: ModeConfig[] | undefined
experiments: Record<string, boolean> | undefined
apiConfiguration: ProviderSettings | undefined
disabledTools?: string[]
modelInfo?: ModelInfo
/**
* If true, returns all tools without mode filtering, but also includes
* the list of allowed tool names for use with allowedFunctionNames.
* This enables providers that support function call restrictions (e.g., Gemini)
* to pass all tool definitions while restricting callable tools.
*/
includeAllToolsWithRestrictions?: boolean
}
interface BuildToolsResult {
/**
* The tools to pass to the model.
* If includeAllToolsWithRestrictions is true, this includes ALL tools.
* Otherwise, it includes only mode-filtered tools.
*/
tools: OpenAI.Chat.ChatCompletionTool[]
/**
* The names of tools that are allowed to be called based on mode restrictions.
* Only populated when includeAllToolsWithRestrictions is true.
* Use this with allowedFunctionNames in providers that support it.
*/
allowedFunctionNames?: string[]
}
/**
* Extracts the function name from a tool definition.
*/
function getToolName(tool: OpenAI.Chat.ChatCompletionTool): string {
return (tool as OpenAI.Chat.ChatCompletionFunctionTool).function.name
}
/**
* Builds the complete tools array for native protocol requests.
* Combines native tools and MCP tools, filtered by mode restrictions.
*
* @param options - Configuration options for building the tools
* @returns Array of filtered native and MCP tools
*/
export async function buildNativeToolsArray(options: BuildToolsOptions): Promise<OpenAI.Chat.ChatCompletionTool[]> {
const result = await buildNativeToolsArrayWithRestrictions(options)
return result.tools
}
/**
* Builds the complete tools array for native protocol requests with optional mode restrictions.
* When includeAllToolsWithRestrictions is true, returns ALL tools but also provides
* the list of allowed tool names for use with allowedFunctionNames.
*
* This enables providers like Gemini to pass all tool definitions to the model
* (so it can reference historical tool calls) while restricting which tools
* can actually be invoked via allowedFunctionNames in toolConfig.
*
* @param options - Configuration options for building the tools
* @returns BuildToolsResult with tools array and optional allowedFunctionNames
*/
export async function buildNativeToolsArrayWithRestrictions(options: BuildToolsOptions): Promise<BuildToolsResult> {
const {
provider,
cwd,
mode,
customModes,
experiments,
apiConfiguration,
disabledTools,
modelInfo,
includeAllToolsWithRestrictions,
} = options
const mcpHub = provider.getMcpHub()
// Get CodeIndexManager for feature checking.
const { CodeIndexManager } = await import("../../services/code-index/manager")
const codeIndexManager = CodeIndexManager.getInstance(provider.context, cwd)
// Build settings object for tool filtering.
const filterSettings = {
todoListEnabled: apiConfiguration?.todoListEnabled ?? true,
disabledTools,
modelInfo,
}
// Check if the model supports images for read_file tool description.
const supportsImages = modelInfo?.supportsImages ?? false
// Build native tools with dynamic read_file tool based on settings.
const nativeTools = getNativeTools({
supportsImages,
})
// Filter native tools based on mode restrictions.
const filteredNativeTools = filterNativeToolsForMode(
nativeTools,
mode,
customModes,
experiments,
codeIndexManager,
filterSettings,
mcpHub,
)
// Resolve the current mode config to get per-mode settings like allowedMcpServers.
const modeSlug = mode ?? defaultModeSlug
const modeConfig = getModeBySlug(modeSlug, customModes)
// Filter MCP tools based on mode restrictions and per-mode allowedMcpServers.
const mcpTools = getMcpServerTools(mcpHub, modeConfig?.allowedMcpServers)
const filteredMcpTools = filterMcpToolsForMode(mcpTools, mode, customModes, experiments)
// Add custom tools if they are available and the experiment is enabled.
let nativeCustomTools: OpenAI.Chat.ChatCompletionFunctionTool[] = []
if (experiments?.customTools) {
const toolDirs = getRooDirectoriesForCwd(cwd).map((dir) => path.join(dir, "tools"))
await customToolRegistry.loadFromDirectoriesIfStale(toolDirs)
const customTools = customToolRegistry.getAllSerialized()
if (customTools.length > 0) {
nativeCustomTools = customTools.map(formatNative)
}
}
// Combine filtered tools (for backward compatibility and for allowedFunctionNames)
const filteredTools = [...filteredNativeTools, ...filteredMcpTools, ...nativeCustomTools]
// If includeAllToolsWithRestrictions is true, return ALL tools but provide
// allowed names based on mode filtering
if (includeAllToolsWithRestrictions) {
// Combine ALL tools (unfiltered native + all MCP + custom)
const allTools = [...nativeTools, ...mcpTools, ...nativeCustomTools]
// Extract names of tools that are allowed based on mode filtering.
// Resolve any alias names to canonical names to ensure consistency with allTools
// (which uses canonical names). This prevents Gemini errors when tools are renamed
// to aliases in filteredTools but allTools contains the original canonical names.
const allowedFunctionNames = filteredTools.map((tool) => resolveToolAlias(getToolName(tool)))
return {
tools: allTools,
allowedFunctionNames,
}
}
// Default behavior: return only filtered tools
return {
tools: filteredTools,
}
}