Merge pull request #2867 from MoerAI/fix/openai-tool-limit

fix(tools): add max_tools config to cap registered tools for OpenAI compatibility (fixes #2848)
This commit is contained in:
YeonGyu-Kim
2026-03-27 12:30:21 +09:00
committed by GitHub
2 changed files with 30 additions and 1 deletions

View File

@@ -21,6 +21,8 @@ export const ExperimentalConfigSchema = z.object({
hashline_edit: z.boolean().optional(),
/** Append fallback model info to session title when a runtime fallback occurs (default: false) */
model_fallback_title: z.boolean().optional(),
/** Maximum number of tools to register. When set, lower-priority tools are excluded to stay within provider limits (e.g., OpenAI's 128-tool cap). Accounts for ~20 OpenCode built-in tools. */
max_tools: z.number().int().min(1).optional(),
})
export type ExperimentalConfig = z.infer<typeof ExperimentalConfigSchema>

View File

@@ -150,7 +150,34 @@ export function createToolRegistry(args: {
normalizeToolArgSchemas(toolDefinition)
}
const filteredTools = filterDisabledTools(allTools, pluginConfig.disabled_tools)
const filteredTools: ToolsRecord = filterDisabledTools(allTools, pluginConfig.disabled_tools)
const maxTools = pluginConfig.experimental?.max_tools
if (maxTools) {
const estimatedBuiltinTools = 20
const pluginToolBudget = maxTools - estimatedBuiltinTools
const toolEntries = Object.entries(filteredTools)
if (pluginToolBudget > 0 && toolEntries.length > pluginToolBudget) {
const excess = toolEntries.length - pluginToolBudget
log(`[tool-registry] Tool count (${toolEntries.length} plugin + ~${estimatedBuiltinTools} builtin = ~${toolEntries.length + estimatedBuiltinTools}) exceeds max_tools=${maxTools}. Trimming ${excess} lower-priority tools.`)
const lowPriorityTools = [
"session_list", "session_read", "session_search", "session_info",
"call_omo_agent", "interactive_bash", "look_at",
"task_create", "task_get", "task_list", "task_update",
]
let removed = 0
for (const toolName of lowPriorityTools) {
if (removed >= excess) break
if (filteredTools[toolName]) {
delete filteredTools[toolName]
removed += 1
}
}
if (removed < excess) {
log(`[tool-registry] WARNING: Could not trim enough tools. ${toolEntries.length - removed} plugin tools remain.`)
}
}
}
return {
filteredTools,