feat(core): Wire tool toggles to AI provider tool declaration payload

This commit is contained in:
2026-02-23 11:30:36 -05:00
parent 1677d25298
commit 92aa33c6d3
3 changed files with 89 additions and 46 deletions

View File

@@ -286,37 +286,53 @@ def _list_anthropic_models() -> list[str]:
TOOL_NAME = "run_powershell" TOOL_NAME = "run_powershell"
_agent_tools: dict = {}
def set_agent_tools(tools: dict):
global _agent_tools, _CACHED_ANTHROPIC_TOOLS
_agent_tools = tools
_CACHED_ANTHROPIC_TOOLS = None
def _build_anthropic_tools() -> list[dict]: def _build_anthropic_tools() -> list[dict]:
"""Build the full Anthropic tools list: run_powershell + MCP file tools.""" """Build the full Anthropic tools list: run_powershell + MCP file tools."""
mcp_tools = [] mcp_tools = []
for spec in mcp_client.MCP_TOOL_SPECS: for spec in mcp_client.MCP_TOOL_SPECS:
mcp_tools.append({ if _agent_tools.get(spec["name"], True):
"name": spec["name"], mcp_tools.append({
"description": spec["description"], "name": spec["name"],
"input_schema": spec["parameters"], "description": spec["description"],
}) "input_schema": spec["parameters"],
powershell_tool = { })
"name": TOOL_NAME,
"description": ( tools_list = mcp_tools
"Run a PowerShell script within the project base_dir. " if _agent_tools.get(TOOL_NAME, True):
"Use this to create, edit, rename, or delete files and directories. " powershell_tool = {
"The working directory is set to base_dir automatically. " "name": TOOL_NAME,
"Always prefer targeted edits over full rewrites where possible. " "description": (
"stdout and stderr are returned to you as the result." "Run a PowerShell script within the project base_dir. "
), "Use this to create, edit, rename, or delete files and directories. "
"input_schema": { "The working directory is set to base_dir automatically. "
"type": "object", "Always prefer targeted edits over full rewrites where possible. "
"properties": { "stdout and stderr are returned to you as the result."
"script": { ),
"type": "string", "input_schema": {
"description": "The PowerShell script to execute." "type": "object",
} "properties": {
"script": {
"type": "string",
"description": "The PowerShell script to execute."
}
},
"required": ["script"]
}, },
"required": ["script"] "cache_control": {"type": "ephemeral"},
}, }
"cache_control": {"type": "ephemeral"}, tools_list.append(powershell_tool)
} elif tools_list:
return mcp_tools + [powershell_tool] # Anthropic requires the LAST tool to have cache_control for the prefix caching to work optimally on tools
tools_list[-1]["cache_control"] = {"type": "ephemeral"}
return tools_list
_ANTHROPIC_TOOLS = _build_anthropic_tools() _ANTHROPIC_TOOLS = _build_anthropic_tools()
@@ -338,6 +354,8 @@ def _gemini_tool_declaration():
# MCP file tools # MCP file tools
for spec in mcp_client.MCP_TOOL_SPECS: for spec in mcp_client.MCP_TOOL_SPECS:
if not _agent_tools.get(spec["name"], True):
continue
props = {} props = {}
for pname, pdef in spec["parameters"].get("properties", {}).items(): for pname, pdef in spec["parameters"].get("properties", {}).items():
props[pname] = types.Schema( props[pname] = types.Schema(
@@ -355,27 +373,28 @@ def _gemini_tool_declaration():
)) ))
# PowerShell tool # PowerShell tool
declarations.append(types.FunctionDeclaration( if _agent_tools.get(TOOL_NAME, True):
name=TOOL_NAME, declarations.append(types.FunctionDeclaration(
description=( name=TOOL_NAME,
"Run a PowerShell script within the project base_dir. " description=(
"Use this to create, edit, rename, or delete files and directories. " "Run a PowerShell script within the project base_dir. "
"The working directory is set to base_dir automatically. " "Use this to create, edit, rename, or delete files and directories. "
"stdout and stderr are returned to you as the result." "The working directory is set to base_dir automatically. "
), "stdout and stderr are returned to you as the result."
parameters=types.Schema( ),
type=types.Type.OBJECT, parameters=types.Schema(
properties={ type=types.Type.OBJECT,
"script": types.Schema( properties={
type=types.Type.STRING, "script": types.Schema(
description="The PowerShell script to execute." type=types.Type.STRING,
) description="The PowerShell script to execute."
}, )
required=["script"] },
), required=["script"]
)) ),
))
return types.Tool(function_declarations=declarations) return types.Tool(function_declarations=declarations) if declarations else None
def _run_script(script: str, base_dir: str) -> str: def _run_script(script: str, base_dir: str) -> str:

1
gui.py
View File

@@ -1203,6 +1203,7 @@ class App:
if global_sp: combined_sp.append(global_sp.strip()) if global_sp: combined_sp.append(global_sp.strip())
if project_sp: combined_sp.append(project_sp.strip()) if project_sp: combined_sp.append(project_sp.strip())
ai_client.set_custom_system_prompt("\n\n".join(combined_sp)) ai_client.set_custom_system_prompt("\n\n".join(combined_sp))
ai_client.set_agent_tools(self.project.get("agent", {}).get("tools", {}))
temp = dpg.get_value("ai_temperature") if dpg.does_item_exist("ai_temperature") else 0.0 temp = dpg.get_value("ai_temperature") if dpg.does_item_exist("ai_temperature") else 0.0
max_tok = dpg.get_value("ai_max_tokens") if dpg.does_item_exist("ai_max_tokens") else 8192 max_tok = dpg.get_value("ai_max_tokens") if dpg.does_item_exist("ai_max_tokens") else 8192
trunc = dpg.get_value("ai_history_trunc") if dpg.does_item_exist("ai_history_trunc") else 8000 trunc = dpg.get_value("ai_history_trunc") if dpg.does_item_exist("ai_history_trunc") else 8000

View File

@@ -0,0 +1,23 @@
import pytest
from ai_client import set_agent_tools, _build_anthropic_tools
def test_agent_tools_wiring():
# Only enable read_file and run_powershell
agent_tools = {
"run_powershell": True,
"read_file": True,
"list_directory": False,
"search_files": False,
"get_file_summary": False,
"web_search": False,
"fetch_url": False
}
set_agent_tools(agent_tools)
anth_tools = _build_anthropic_tools()
tool_names = [t["name"] for t in anth_tools]
assert "read_file" in tool_names
assert "run_powershell" in tool_names
assert "list_directory" not in tool_names
assert "web_search" not in tool_names