tools.rs

  1mod context_server_registry;
  2mod copy_path_tool;
  3mod create_directory_tool;
  4mod delete_path_tool;
  5mod diagnostics_tool;
  6mod edit_file_tool;
  7#[cfg(all(test, feature = "unit-eval"))]
  8mod evals;
  9mod fetch_tool;
 10mod find_path_tool;
 11mod grep_tool;
 12mod list_directory_tool;
 13mod move_path_tool;
 14mod now_tool;
 15mod open_tool;
 16mod read_file_tool;
 17mod restore_file_from_disk_tool;
 18mod save_file_tool;
 19mod skill_tool;
 20mod spawn_agent_tool;
 21mod streaming_edit_file_tool;
 22mod terminal_tool;
 23mod tool_edit_parser;
 24mod tool_permissions;
 25mod update_plan_tool;
 26mod web_search_tool;
 27
 28use crate::AgentTool;
 29use language_model::{LanguageModelRequestTool, LanguageModelToolSchemaFormat};
 30
 31pub use context_server_registry::*;
 32pub use copy_path_tool::*;
 33pub use create_directory_tool::*;
 34pub use delete_path_tool::*;
 35pub use diagnostics_tool::*;
 36pub use edit_file_tool::*;
 37pub use fetch_tool::*;
 38pub use find_path_tool::*;
 39pub use grep_tool::*;
 40pub use list_directory_tool::*;
 41pub use move_path_tool::*;
 42pub use now_tool::*;
 43pub use open_tool::*;
 44pub use read_file_tool::*;
 45pub use restore_file_from_disk_tool::*;
 46pub use save_file_tool::*;
 47pub use skill_tool::*;
 48pub use spawn_agent_tool::*;
 49pub use streaming_edit_file_tool::*;
 50pub use terminal_tool::*;
 51pub use tool_permissions::*;
 52pub use update_plan_tool::*;
 53pub use web_search_tool::*;
 54
 55macro_rules! tools {
 56    ($($tool:ty),* $(,)?) => {
 57        /// Every built-in tool name, determined at compile time.
 58        pub const ALL_TOOL_NAMES: &[&str] = &[
 59            $(<$tool>::NAME,)*
 60        ];
 61
 62        const _: () = {
 63            const fn str_eq(a: &str, b: &str) -> bool {
 64                let a = a.as_bytes();
 65                let b = b.as_bytes();
 66                if a.len() != b.len() {
 67                    return false;
 68                }
 69                let mut i = 0;
 70                while i < a.len() {
 71                    if a[i] != b[i] {
 72                        return false;
 73                    }
 74                    i += 1;
 75                }
 76                true
 77            }
 78
 79            const NAMES: &[&str] = ALL_TOOL_NAMES;
 80            let mut i = 0;
 81            while i < NAMES.len() {
 82                let mut j = i + 1;
 83                while j < NAMES.len() {
 84                    if str_eq(NAMES[i], NAMES[j]) {
 85                        panic!("Duplicate tool name in tools! macro");
 86                    }
 87                    j += 1;
 88                }
 89                i += 1;
 90            }
 91        };
 92
 93        /// Returns whether the tool with the given name supports the given provider.
 94        pub fn tool_supports_provider(name: &str, provider: &language_model::LanguageModelProviderId) -> bool {
 95            $(
 96                if name == <$tool>::NAME {
 97                    return <$tool>::supports_provider(provider);
 98                }
 99            )*
100            false
101        }
102
103        /// A list of all built-in tools
104        pub fn built_in_tools() -> impl Iterator<Item = LanguageModelRequestTool> {
105            fn language_model_tool<T: AgentTool>() -> LanguageModelRequestTool {
106                LanguageModelRequestTool {
107                    name: T::NAME.to_string(),
108                    description: T::description().to_string(),
109                    input_schema: T::input_schema(LanguageModelToolSchemaFormat::JsonSchema).to_value(),
110                    use_input_streaming: T::supports_input_streaming(),
111                }
112            }
113            [
114                $(
115                    language_model_tool::<$tool>(),
116                )*
117            ]
118            .into_iter()
119        }
120    };
121}
122
123tools! {
124    CopyPathTool,
125    CreateDirectoryTool,
126    DeletePathTool,
127    DiagnosticsTool,
128    EditFileTool,
129    FetchTool,
130    FindPathTool,
131    GrepTool,
132    ListDirectoryTool,
133    MovePathTool,
134    NowTool,
135    OpenTool,
136    ReadFileTool,
137    RestoreFileFromDiskTool,
138    SaveFileTool,
139    SkillTool,
140    SpawnAgentTool,
141    TerminalTool,
142    UpdatePlanTool,
143    WebSearchTool,
144}