| 1 | import os |
| 2 | from typing import Any |
| 3 | |
| 4 | from helpers.extension import Extension, extensible |
| 5 | from helpers import files, subagents, tool_policy |
| 6 | from helpers.print_style import PrintStyle |
| 7 | from agent import Agent, LoopData |
| 8 | |
| 9 | |
| 10 | TOOL_KWARGS_KEY = "_tool_prompt_kwargs" |
| 11 | |
| 12 | |
| 13 | class ToolsPrompt(Extension): |
| 14 | |
| 15 | async def execute( |
| 16 | self, |
| 17 | system_prompt: list[str] = [], |
| 18 | loop_data: LoopData = LoopData(), |
| 19 | **kwargs: Any, |
| 20 | ): |
| 21 | if not self.agent: |
| 22 | return |
| 23 | prompt = await build_prompt(self.agent) |
| 24 | system_prompt.append(prompt) |
| 25 | |
| 26 | |
| 27 | @extensible |
| 28 | async def build_prompt(agent: Agent) -> str: |
| 29 | # collect tool files from all prompt directories |
| 30 | prompt_dirs = subagents.get_paths(agent, "prompts") |
| 31 | tool_files = files.get_unique_filenames_in_dirs( |
| 32 | prompt_dirs, "agent.system.tool.*.md" |
| 33 | ) |
| 34 | |
| 35 | # per-file kwargs registered by plugin config extensions (e.g. _09_text_editor_config) |
| 36 | all_tool_kwargs: dict[str, dict[str, Any]] = agent.get_data(TOOL_KWARGS_KEY) or {} |
| 37 | |
| 38 | tools: list[str] = [] |
| 39 | for tool_file in tool_files: |
| 40 | try: |
| 41 | basename = os.path.basename(tool_file) |
| 42 | extra = all_tool_kwargs.get(basename, {}) |
| 43 | tool = agent.read_prompt(basename, **extra) |
| 44 | tool = tool_policy.filter_tool_prompt(agent, basename, tool) |
| 45 | if tool: |
| 46 | tools.append(tool) |
| 47 | except Exception as e: |
| 48 | PrintStyle().error(f"Error loading tool '{tool_file}': {e}") |
| 49 | |
| 50 | tools_str = "\n\n".join(tools) |
| 51 | prompt = agent.read_prompt("agent.system.tools.md", tools=tools_str) |
| 52 | |
| 53 | # vision support |
| 54 | from plugins._model_config.helpers.model_config import ( |
| 55 | get_chat_model_config, |
| 56 | get_vision_model_config, |
| 57 | ) |
| 58 | |
| 59 | chat_cfg = get_chat_model_config(agent) |
| 60 | if get_vision_model_config(agent) or chat_cfg.get("vision", False): |
| 61 | prompt += "\n\n" + agent.read_prompt("agent.system.tools_vision.md") |
| 62 | |
| 63 | return prompt |