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