fix: conditionally include codebase_search in native tools based on feature status

This commit is contained in:
daniel-lxs 2025-11-25 14:28:12 -05:00
parent 311940b085
commit 1236c3b571
No known key found for this signature in database
GPG key ID: 21C74479048B3AA6
3 changed files with 82 additions and 5 deletions

View file

@ -0,0 +1,61 @@
import { describe, it, expect } from "vitest"
import { getNativeTools } from "../index"
// Helper to extract tool names
function getToolNames(tools: ReturnType<typeof getNativeTools>): string[] {
return tools.map((t) => ("function" in t ? t.function.name : ""))
}
// Helper to find a tool by name
function findTool(tools: ReturnType<typeof getNativeTools>, name: string) {
return tools.find((t) => "function" in t && t.function.name === name)
}
describe("getNativeTools", () => {
it("should include codebase_search when codebaseSearchEnabled is true", () => {
const tools = getNativeTools(true, true)
const toolNames = getToolNames(tools)
expect(toolNames).toContain("codebase_search")
})
it("should exclude codebase_search when codebaseSearchEnabled is false", () => {
const tools = getNativeTools(true, false)
const toolNames = getToolNames(tools)
expect(toolNames).not.toContain("codebase_search")
})
it("should include codebase_search by default (codebaseSearchEnabled defaults to true)", () => {
const tools = getNativeTools(true)
const toolNames = getToolNames(tools)
expect(toolNames).toContain("codebase_search")
})
it("should include read_file with line_ranges when partialReadsEnabled is true", () => {
const tools = getNativeTools(true, true)
const readFileTool = findTool(tools, "read_file")
expect(readFileTool).toBeDefined()
if (readFileTool && "function" in readFileTool) {
expect(readFileTool.function.description).toContain("line_ranges")
}
})
it("should include read_file without line_ranges when partialReadsEnabled is false", () => {
const tools = getNativeTools(false, true)
const readFileTool = findTool(tools, "read_file")
expect(readFileTool).toBeDefined()
if (readFileTool && "function" in readFileTool) {
expect(readFileTool.function.description).not.toContain("line_ranges")
}
})
it("should always include core tools regardless of settings", () => {
const tools = getNativeTools(true, true)
const toolNames = getToolNames(tools)
expect(toolNames).toContain("read_file")
expect(toolNames).toContain("write_to_file")
expect(toolNames).toContain("execute_command")
expect(toolNames).toContain("apply_diff")
expect(toolNames).toContain("ask_followup_question")
expect(toolNames).toContain("attempt_completion")
})
})

View file

@ -26,16 +26,19 @@ export { convertOpenAIToolToAnthropic, convertOpenAIToolsToAnthropic } from "./c
* Get native tools array, optionally customizing based on settings.
*
* @param partialReadsEnabled - Whether to include line_ranges support in read_file tool (default: true)
* @param codebaseSearchEnabled - Whether to include codebase_search tool (default: true)
* @returns Array of native tool definitions
*/
export function getNativeTools(partialReadsEnabled: boolean = true): OpenAI.Chat.ChatCompletionTool[] {
return [
export function getNativeTools(
partialReadsEnabled: boolean = true,
codebaseSearchEnabled: boolean = true,
): OpenAI.Chat.ChatCompletionTool[] {
const tools: OpenAI.Chat.ChatCompletionTool[] = [
accessMcpResource,
apply_diff_single_file,
askFollowupQuestion,
attemptCompletion,
browserAction,
codebaseSearch,
executeCommand,
fetchInstructions,
generateImage,
@ -49,7 +52,13 @@ export function getNativeTools(partialReadsEnabled: boolean = true): OpenAI.Chat
switchMode,
updateTodoList,
writeToFile,
] satisfies OpenAI.Chat.ChatCompletionTool[]
]
if (codebaseSearchEnabled) {
tools.push(codebaseSearch)
}
return tools
}
// Backward compatibility: export default tools with line ranges enabled

View file

@ -41,8 +41,15 @@ export async function buildNativeToolsArray(options: BuildToolsOptions): Promise
// Determine if partial reads are enabled based on maxReadFileLine setting
const partialReadsEnabled = maxReadFileLine !== -1
// Determine if codebase_search should be included
const codebaseSearchEnabled = !!(
codeIndexManager?.isFeatureEnabled &&
codeIndexManager?.isFeatureConfigured &&
codeIndexManager?.isInitialized
)
// Build native tools with dynamic read_file tool based on partialReadsEnabled
const nativeTools = getNativeTools(partialReadsEnabled)
const nativeTools = getNativeTools(partialReadsEnabled, codebaseSearchEnabled)
// Filter native tools based on mode restrictions
const filteredNativeTools = filterNativeToolsForMode(