tinyharness_lib/tools/
mod.rs1pub mod auto_compact;
2pub mod edit;
3pub mod glob;
4pub mod grep;
5pub mod invoke_skill;
6pub mod ls;
7pub mod question;
8pub mod read;
9pub mod run;
10pub mod screenshot;
11pub mod switch_mode;
12pub mod tool;
13pub mod web_search;
14pub mod write;
15
16use crate::mode::AgentMode;
17use crate::provider::ToolDefinition;
18use crate::tools::tool::{Tool, ToolCategory};
19
20#[derive(Debug, Clone)]
25pub enum SignalEvent {
26 SwitchMode { mode: AgentMode },
28 Question {
30 question: String,
31 answers: Vec<String>,
32 },
33 AutoCompact { focus: String },
35 InvokeSkill { skill_name: String },
37}
38
39#[derive(Default)]
40pub struct ToolManager {
41 tools: Vec<Tool>,
42}
43
44impl ToolManager {
45 pub fn new() -> Self {
46 Self::default()
47 }
48
49 pub fn register_defaults(&mut self) {
51 self.register_tool(crate::tools::auto_compact::auto_compact_tool_entry());
52 self.register_tool(crate::tools::ls::ls_tool_entry());
53 self.register_tool(crate::tools::read::read_tool_entry());
54 self.register_tool(crate::tools::write::write_tool_entry());
55 self.register_tool(crate::tools::edit::edit_tool_entry());
56 self.register_tool(crate::tools::grep::grep_tool_entry());
57 self.register_tool(crate::tools::run::run_tool_entry());
58 self.register_tool(crate::tools::glob::glob_tool_entry());
59 self.register_tool(crate::tools::web_search::web_search_tool_entry());
60 self.register_tool(crate::tools::web_search::web_fetch_tool_entry());
61 self.register_tool(crate::tools::switch_mode::switch_mode_tool_entry());
62 self.register_tool(crate::tools::question::question_tool_entry());
63 self.register_tool(crate::tools::invoke_skill::invoke_skill_tool_entry());
64 self.register_tool(crate::tools::screenshot::screenshot_tool_entry());
65 }
66
67 pub fn register_tool(&mut self, tool: Tool) {
68 self.tools.push(tool);
69 }
70
71 pub fn register_custom_tools(&mut self, tools: &[crate::custom_tools::CustomToolDefinition]) {
74 let existing: std::collections::HashSet<String> =
75 self.tools.iter().map(|t| t.name.clone()).collect();
76 for def in tools {
77 if existing.contains(&def.name) {
78 tracing::warn!(
79 "Custom tool '{}' collides with an existing tool — skipping",
80 def.name
81 );
82 continue;
83 }
84 match def.build_tool() {
85 Ok(tool) => self.register_tool(tool),
86 Err(e) => tracing::warn!("Failed to register custom tool '{}': {}", def.name, e),
87 }
88 }
89 }
90
91 pub fn get_all_tool_definitions(&self) -> Vec<ToolDefinition> {
93 self.tools.iter().map(|t| t.to_definition()).collect()
94 }
95
96 pub fn tools_for_mode(
99 &self,
100 mode: AgentMode,
101 auto_compact_enabled: bool,
102 ) -> Vec<ToolDefinition> {
103 let filter_compact = |t: &&Tool| {
104 if t.name == "auto_compact" {
105 auto_compact_enabled
106 } else {
107 true
108 }
109 };
110 match mode {
111 AgentMode::Agent => self
112 .tools
113 .iter()
114 .filter(filter_compact)
115 .map(|t| t.to_definition())
116 .collect(),
117 AgentMode::Casual => self
118 .tools
119 .iter()
120 .filter(|t| filter_compact(t) && (t.name == "web_search" || t.name == "web_fetch"))
121 .map(|t| t.to_definition())
122 .collect(),
123 AgentMode::Planning => self
124 .tools
125 .iter()
126 .filter(|t| {
127 filter_compact(t)
128 && (t.category == ToolCategory::ReadOnly
129 || t.category == ToolCategory::Signal)
130 })
131 .map(|t| t.to_definition())
132 .collect(),
133 AgentMode::Research => self
134 .tools
135 .iter()
136 .filter(|t| {
137 filter_compact(t)
138 && (t.category == ToolCategory::ReadOnly
139 || t.category == ToolCategory::Signal)
140 })
141 .map(|t| t.to_definition())
142 .collect(),
143 }
144 }
145
146 pub fn category_of(&self, tool_name: &str) -> Option<ToolCategory> {
148 self.tools
149 .iter()
150 .find(|t| t.name == tool_name)
151 .map(|t| t.category)
152 }
153
154 pub fn needs_approval(&self, tool_name: &str) -> bool {
158 self.category_of(tool_name)
159 .map(|c| c == ToolCategory::Destructive || c == ToolCategory::Signal)
160 .unwrap_or(false)
161 }
162
163 pub fn is_signal_tool(&self, tool_name: &str) -> bool {
166 self.category_of(tool_name) == Some(ToolCategory::Signal)
167 }
168
169 pub fn parse_signal_event(
177 &self,
178 tool_name: &str,
179 arguments: &serde_json::Value,
180 ) -> Option<SignalEvent> {
181 match tool_name {
182 "switch_mode" => {
183 let mode_str = arguments.get("mode").and_then(|v| v.as_str()).unwrap_or("");
184 mode_str
185 .parse::<AgentMode>()
186 .ok()
187 .map(|mode| SignalEvent::SwitchMode { mode })
188 }
189 "question" => {
190 let question = arguments
191 .get("question")
192 .and_then(|v| v.as_str())
193 .unwrap_or("")
194 .to_string();
195 let answers: Vec<String> = arguments
196 .get("answers")
197 .and_then(|v| v.as_array())
198 .map(|arr| {
199 arr.iter()
200 .filter_map(|item| item.as_str().map(|s| s.to_string()))
201 .collect()
202 })
203 .unwrap_or_default();
204 Some(SignalEvent::Question { question, answers })
205 }
206 "auto_compact" => {
207 let focus = arguments
208 .get("focus")
209 .and_then(|v| v.as_str())
210 .unwrap_or("")
211 .to_string();
212 Some(SignalEvent::AutoCompact { focus })
213 }
214 "invoke_skill" => {
215 let skill_name = arguments
216 .get("skill_name")
217 .and_then(|v| v.as_str())
218 .unwrap_or("")
219 .to_string();
220 if skill_name.is_empty() {
221 None
222 } else {
223 Some(SignalEvent::InvokeSkill { skill_name })
224 }
225 }
226 _ => None,
227 }
228 }
229
230 pub async fn execute_tool_call(
231 &self,
232 tool_name: &str,
233 arguments: &serde_json::Value,
234 ) -> String {
235 if let Some(tool) = self.tools.iter().find(|t| t.name == tool_name) {
236 tool::execute_tool_call(tool, arguments).await
237 } else {
238 format!("Error: Tool '{}' not found", tool_name)
239 }
240 }
241}