feat(core): Wire tool toggles to AI provider tool declaration payload
This commit is contained in:
111
ai_client.py
111
ai_client.py
@@ -286,37 +286,53 @@ def _list_anthropic_models() -> list[str]:
|
||||
|
||||
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]:
|
||||
"""Build the full Anthropic tools list: run_powershell + MCP file tools."""
|
||||
mcp_tools = []
|
||||
for spec in mcp_client.MCP_TOOL_SPECS:
|
||||
mcp_tools.append({
|
||||
"name": spec["name"],
|
||||
"description": spec["description"],
|
||||
"input_schema": spec["parameters"],
|
||||
})
|
||||
powershell_tool = {
|
||||
"name": TOOL_NAME,
|
||||
"description": (
|
||||
"Run a PowerShell script within the project base_dir. "
|
||||
"Use this to create, edit, rename, or delete files and directories. "
|
||||
"The working directory is set to base_dir automatically. "
|
||||
"Always prefer targeted edits over full rewrites where possible. "
|
||||
"stdout and stderr are returned to you as the result."
|
||||
),
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"script": {
|
||||
"type": "string",
|
||||
"description": "The PowerShell script to execute."
|
||||
}
|
||||
if _agent_tools.get(spec["name"], True):
|
||||
mcp_tools.append({
|
||||
"name": spec["name"],
|
||||
"description": spec["description"],
|
||||
"input_schema": spec["parameters"],
|
||||
})
|
||||
|
||||
tools_list = mcp_tools
|
||||
if _agent_tools.get(TOOL_NAME, True):
|
||||
powershell_tool = {
|
||||
"name": TOOL_NAME,
|
||||
"description": (
|
||||
"Run a PowerShell script within the project base_dir. "
|
||||
"Use this to create, edit, rename, or delete files and directories. "
|
||||
"The working directory is set to base_dir automatically. "
|
||||
"Always prefer targeted edits over full rewrites where possible. "
|
||||
"stdout and stderr are returned to you as the result."
|
||||
),
|
||||
"input_schema": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"script": {
|
||||
"type": "string",
|
||||
"description": "The PowerShell script to execute."
|
||||
}
|
||||
},
|
||||
"required": ["script"]
|
||||
},
|
||||
"required": ["script"]
|
||||
},
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
return mcp_tools + [powershell_tool]
|
||||
"cache_control": {"type": "ephemeral"},
|
||||
}
|
||||
tools_list.append(powershell_tool)
|
||||
elif tools_list:
|
||||
# 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()
|
||||
@@ -338,6 +354,8 @@ def _gemini_tool_declaration():
|
||||
|
||||
# MCP file tools
|
||||
for spec in mcp_client.MCP_TOOL_SPECS:
|
||||
if not _agent_tools.get(spec["name"], True):
|
||||
continue
|
||||
props = {}
|
||||
for pname, pdef in spec["parameters"].get("properties", {}).items():
|
||||
props[pname] = types.Schema(
|
||||
@@ -355,27 +373,28 @@ def _gemini_tool_declaration():
|
||||
))
|
||||
|
||||
# PowerShell tool
|
||||
declarations.append(types.FunctionDeclaration(
|
||||
name=TOOL_NAME,
|
||||
description=(
|
||||
"Run a PowerShell script within the project base_dir. "
|
||||
"Use this to create, edit, rename, or delete files and directories. "
|
||||
"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,
|
||||
properties={
|
||||
"script": types.Schema(
|
||||
type=types.Type.STRING,
|
||||
description="The PowerShell script to execute."
|
||||
)
|
||||
},
|
||||
required=["script"]
|
||||
),
|
||||
))
|
||||
if _agent_tools.get(TOOL_NAME, True):
|
||||
declarations.append(types.FunctionDeclaration(
|
||||
name=TOOL_NAME,
|
||||
description=(
|
||||
"Run a PowerShell script within the project base_dir. "
|
||||
"Use this to create, edit, rename, or delete files and directories. "
|
||||
"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,
|
||||
properties={
|
||||
"script": types.Schema(
|
||||
type=types.Type.STRING,
|
||||
description="The PowerShell script to execute."
|
||||
)
|
||||
},
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user