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 switch_mode;
11pub mod tool;
12pub mod web_search;
13pub mod write;
14
15use crate::mode::AgentMode;
16use crate::provider::ToolDefinition;
17use crate::tools::tool::{Tool, ToolCategory};
18
19#[derive(Debug, Clone)]
24pub enum SignalEvent {
25 SwitchMode { mode: AgentMode },
27 Question {
29 question: String,
30 answers: Vec<String>,
31 },
32 AutoCompact { focus: String },
34 InvokeSkill { skill_name: String },
36}
37
38#[derive(Default)]
39pub struct ToolManager {
40 tools: Vec<Tool>,
41}
42
43impl ToolManager {
44 pub fn new() -> Self {
45 Self::default()
46 }
47
48 pub fn register_defaults(&mut self) {
50 self.register_tool(crate::tools::auto_compact::auto_compact_tool_entry());
51 self.register_tool(crate::tools::ls::ls_tool_entry());
52 self.register_tool(crate::tools::read::read_tool_entry());
53 self.register_tool(crate::tools::write::write_tool_entry());
54 self.register_tool(crate::tools::edit::edit_tool_entry());
55 self.register_tool(crate::tools::grep::grep_tool_entry());
56 self.register_tool(crate::tools::run::run_tool_entry());
57 self.register_tool(crate::tools::glob::glob_tool_entry());
58 self.register_tool(crate::tools::web_search::web_search_tool_entry());
59 self.register_tool(crate::tools::web_search::web_fetch_tool_entry());
60 self.register_tool(crate::tools::switch_mode::switch_mode_tool_entry());
61 self.register_tool(crate::tools::question::question_tool_entry());
62 self.register_tool(crate::tools::invoke_skill::invoke_skill_tool_entry());
63 }
64
65 pub fn register_tool(&mut self, tool: Tool) {
66 self.tools.push(tool);
67 }
68
69 pub fn get_all_tool_definitions(&self) -> Vec<ToolDefinition> {
71 self.tools.iter().map(|t| t.to_definition()).collect()
72 }
73
74 pub fn tools_for_mode(&self, mode: AgentMode) -> Vec<ToolDefinition> {
76 match mode {
77 AgentMode::Agent => self.get_all_tool_definitions(),
78 AgentMode::Casual => Vec::new(),
79 AgentMode::Planning => self
80 .tools
81 .iter()
82 .filter(|t| {
83 t.category == ToolCategory::ReadOnly || t.category == ToolCategory::Signal
84 })
85 .map(|t| t.to_definition())
86 .collect(),
87 AgentMode::Research => self
88 .tools
89 .iter()
90 .filter(|t| {
91 t.category == ToolCategory::ReadOnly || t.category == ToolCategory::Signal
92 })
93 .map(|t| t.to_definition())
94 .collect(),
95 }
96 }
97
98 pub fn category_of(&self, tool_name: &str) -> Option<ToolCategory> {
100 self.tools
101 .iter()
102 .find(|t| t.name == tool_name)
103 .map(|t| t.category)
104 }
105
106 pub fn needs_approval(&self, tool_name: &str) -> bool {
110 self.category_of(tool_name)
111 .map(|c| c == ToolCategory::Destructive || c == ToolCategory::Signal)
112 .unwrap_or(false)
113 }
114
115 pub fn is_signal_tool(&self, tool_name: &str) -> bool {
118 self.category_of(tool_name) == Some(ToolCategory::Signal)
119 }
120
121 pub fn parse_signal_event(
129 &self,
130 tool_name: &str,
131 arguments: &serde_json::Value,
132 ) -> Option<SignalEvent> {
133 match tool_name {
134 "switch_mode" => {
135 let mode_str = arguments.get("mode").and_then(|v| v.as_str()).unwrap_or("");
136 mode_str
137 .parse::<AgentMode>()
138 .ok()
139 .map(|mode| SignalEvent::SwitchMode { mode })
140 }
141 "question" => {
142 let question = arguments
143 .get("question")
144 .and_then(|v| v.as_str())
145 .unwrap_or("")
146 .to_string();
147 let answers: Vec<String> = arguments
148 .get("answers")
149 .and_then(|v| v.as_array())
150 .map(|arr| {
151 arr.iter()
152 .filter_map(|item| item.as_str().map(|s| s.to_string()))
153 .collect()
154 })
155 .unwrap_or_default();
156 Some(SignalEvent::Question { question, answers })
157 }
158 "auto_compact" => {
159 let focus = arguments
160 .get("focus")
161 .and_then(|v| v.as_str())
162 .unwrap_or("")
163 .to_string();
164 Some(SignalEvent::AutoCompact { focus })
165 }
166 "invoke_skill" => {
167 let skill_name = arguments
168 .get("skill_name")
169 .and_then(|v| v.as_str())
170 .unwrap_or("")
171 .to_string();
172 if skill_name.is_empty() {
173 None
174 } else {
175 Some(SignalEvent::InvokeSkill { skill_name })
176 }
177 }
178 _ => None,
179 }
180 }
181
182 pub async fn execute_tool_call(
183 &self,
184 tool_name: &str,
185 arguments: &serde_json::Value,
186 ) -> String {
187 if let Some(tool) = self.tools.iter().find(|t| t.name == tool_name) {
188 tool::execute_tool_call(tool, arguments).await
189 } else {
190 format!("Error: Tool '{}' not found", tool_name)
191 }
192 }
193}