assistant_tools.rs

  1mod batch_tool;
  2mod code_action_tool;
  3mod code_symbols_tool;
  4mod contents_tool;
  5mod copy_path_tool;
  6mod create_directory_tool;
  7mod create_file_tool;
  8mod delete_path_tool;
  9mod diagnostics_tool;
 10mod edit_file_tool;
 11mod fetch_tool;
 12mod grep_tool;
 13mod list_directory_tool;
 14mod move_path_tool;
 15mod now_tool;
 16mod open_tool;
 17mod path_search_tool;
 18mod read_file_tool;
 19mod rename_tool;
 20mod replace;
 21mod schema;
 22mod symbol_info_tool;
 23mod terminal_tool;
 24mod thinking_tool;
 25mod ui;
 26mod web_search_tool;
 27
 28use std::sync::Arc;
 29
 30use assistant_tool::ToolRegistry;
 31use copy_path_tool::CopyPathTool;
 32use feature_flags::FeatureFlagAppExt;
 33use gpui::App;
 34use http_client::HttpClientWithUrl;
 35use move_path_tool::MovePathTool;
 36use web_search_tool::WebSearchTool;
 37
 38use crate::batch_tool::BatchTool;
 39use crate::code_action_tool::CodeActionTool;
 40use crate::code_symbols_tool::CodeSymbolsTool;
 41use crate::contents_tool::ContentsTool;
 42use crate::create_directory_tool::CreateDirectoryTool;
 43use crate::create_file_tool::CreateFileTool;
 44use crate::delete_path_tool::DeletePathTool;
 45use crate::diagnostics_tool::DiagnosticsTool;
 46use crate::edit_file_tool::EditFileTool;
 47use crate::fetch_tool::FetchTool;
 48use crate::grep_tool::GrepTool;
 49use crate::list_directory_tool::ListDirectoryTool;
 50use crate::now_tool::NowTool;
 51use crate::open_tool::OpenTool;
 52use crate::path_search_tool::PathSearchTool;
 53use crate::read_file_tool::ReadFileTool;
 54use crate::rename_tool::RenameTool;
 55use crate::symbol_info_tool::SymbolInfoTool;
 56use crate::terminal_tool::TerminalTool;
 57use crate::thinking_tool::ThinkingTool;
 58
 59pub fn init(http_client: Arc<HttpClientWithUrl>, cx: &mut App) {
 60    assistant_tool::init(cx);
 61
 62    let registry = ToolRegistry::global(cx);
 63    registry.register_tool(TerminalTool);
 64    registry.register_tool(BatchTool);
 65    registry.register_tool(CreateDirectoryTool);
 66    registry.register_tool(CreateFileTool);
 67    registry.register_tool(CopyPathTool);
 68    registry.register_tool(DeletePathTool);
 69    registry.register_tool(EditFileTool);
 70    registry.register_tool(SymbolInfoTool);
 71    registry.register_tool(CodeActionTool);
 72    registry.register_tool(MovePathTool);
 73    registry.register_tool(DiagnosticsTool);
 74    registry.register_tool(ListDirectoryTool);
 75    registry.register_tool(NowTool);
 76    registry.register_tool(OpenTool);
 77    registry.register_tool(CodeSymbolsTool);
 78    registry.register_tool(ContentsTool);
 79    registry.register_tool(PathSearchTool);
 80    registry.register_tool(ReadFileTool);
 81    registry.register_tool(GrepTool);
 82    registry.register_tool(RenameTool);
 83    registry.register_tool(ThinkingTool);
 84    registry.register_tool(FetchTool::new(http_client));
 85
 86    cx.observe_flag::<feature_flags::ZedProWebSearchTool, _>({
 87        move |is_enabled, cx| {
 88            if is_enabled {
 89                ToolRegistry::global(cx).register_tool(WebSearchTool);
 90            } else {
 91                ToolRegistry::global(cx).unregister_tool(WebSearchTool);
 92            }
 93        }
 94    })
 95    .detach();
 96}
 97
 98#[cfg(test)]
 99mod tests {
100    use http_client::FakeHttpClient;
101
102    use super::*;
103
104    #[gpui::test]
105    fn test_builtin_tool_schema_compatibility(cx: &mut App) {
106        crate::init(
107            Arc::new(http_client::HttpClientWithUrl::new(
108                FakeHttpClient::with_200_response(),
109                "https://zed.dev",
110                None,
111            )),
112            cx,
113        );
114
115        for tool in ToolRegistry::global(cx).tools() {
116            let actual_schema = tool
117                .input_schema(language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset)
118                .unwrap();
119            let mut expected_schema = actual_schema.clone();
120            assistant_tool::adapt_schema_to_format(
121                &mut expected_schema,
122                language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset,
123            )
124            .unwrap();
125
126            let error_message = format!(
127                "Tool schema for `{}` is not compatible with `language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset` (Gemini Models).\n\
128                Are you using `schema::json_schema_for<T>(format)` to generate the schema?",
129                tool.name(),
130            );
131
132            assert_eq!(actual_schema, expected_schema, "{}", error_message)
133        }
134    }
135}