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_agent;
 11mod edit_file_tool;
 12mod fetch_tool;
 13mod find_path_tool;
 14mod grep_tool;
 15mod list_directory_tool;
 16mod move_path_tool;
 17mod now_tool;
 18mod open_tool;
 19mod read_file_tool;
 20mod rename_tool;
 21mod replace;
 22mod schema;
 23mod streaming_edit_file_tool;
 24mod symbol_info_tool;
 25mod templates;
 26mod terminal_tool;
 27mod thinking_tool;
 28mod ui;
 29mod web_search_tool;
 30
 31use std::sync::Arc;
 32
 33use assistant_settings::AssistantSettings;
 34use assistant_tool::ToolRegistry;
 35use copy_path_tool::CopyPathTool;
 36use feature_flags::{AgentStreamEditsFeatureFlag, FeatureFlagAppExt};
 37use gpui::App;
 38use http_client::HttpClientWithUrl;
 39use language_model::LanguageModelRegistry;
 40use move_path_tool::MovePathTool;
 41use settings::{Settings, SettingsStore};
 42use web_search_tool::WebSearchTool;
 43
 44pub(crate) use templates::*;
 45
 46use crate::batch_tool::BatchTool;
 47use crate::code_action_tool::CodeActionTool;
 48use crate::code_symbols_tool::CodeSymbolsTool;
 49use crate::contents_tool::ContentsTool;
 50use crate::create_directory_tool::CreateDirectoryTool;
 51use crate::create_file_tool::CreateFileTool;
 52use crate::delete_path_tool::DeletePathTool;
 53use crate::diagnostics_tool::DiagnosticsTool;
 54use crate::edit_file_tool::EditFileTool;
 55use crate::fetch_tool::FetchTool;
 56use crate::find_path_tool::FindPathTool;
 57use crate::grep_tool::GrepTool;
 58use crate::list_directory_tool::ListDirectoryTool;
 59use crate::now_tool::NowTool;
 60use crate::open_tool::OpenTool;
 61use crate::read_file_tool::ReadFileTool;
 62use crate::rename_tool::RenameTool;
 63use crate::streaming_edit_file_tool::StreamingEditFileTool;
 64use crate::symbol_info_tool::SymbolInfoTool;
 65use crate::terminal_tool::TerminalTool;
 66use crate::thinking_tool::ThinkingTool;
 67
 68pub use create_file_tool::CreateFileToolInput;
 69pub use edit_file_tool::EditFileToolInput;
 70pub use find_path_tool::FindPathToolInput;
 71pub use read_file_tool::ReadFileToolInput;
 72
 73pub fn init(http_client: Arc<HttpClientWithUrl>, cx: &mut App) {
 74    assistant_tool::init(cx);
 75
 76    let registry = ToolRegistry::global(cx);
 77    registry.register_tool(TerminalTool);
 78    registry.register_tool(BatchTool);
 79    registry.register_tool(CreateDirectoryTool);
 80    registry.register_tool(CopyPathTool);
 81    registry.register_tool(DeletePathTool);
 82    registry.register_tool(SymbolInfoTool);
 83    registry.register_tool(CodeActionTool);
 84    registry.register_tool(MovePathTool);
 85    registry.register_tool(DiagnosticsTool);
 86    registry.register_tool(ListDirectoryTool);
 87    registry.register_tool(NowTool);
 88    registry.register_tool(OpenTool);
 89    registry.register_tool(CodeSymbolsTool);
 90    registry.register_tool(ContentsTool);
 91    registry.register_tool(FindPathTool);
 92    registry.register_tool(ReadFileTool);
 93    registry.register_tool(GrepTool);
 94    registry.register_tool(RenameTool);
 95    registry.register_tool(ThinkingTool);
 96    registry.register_tool(FetchTool::new(http_client));
 97
 98    register_edit_file_tool(cx);
 99    cx.observe_flag::<AgentStreamEditsFeatureFlag, _>(|_, cx| register_edit_file_tool(cx))
100        .detach();
101    cx.observe_global::<SettingsStore>(register_edit_file_tool)
102        .detach();
103
104    cx.subscribe(
105        &LanguageModelRegistry::global(cx),
106        move |registry, event, cx| match event {
107            language_model::Event::DefaultModelChanged => {
108                let using_zed_provider = registry
109                    .read(cx)
110                    .default_model()
111                    .map_or(false, |default| default.is_provided_by_zed());
112                if using_zed_provider {
113                    ToolRegistry::global(cx).register_tool(WebSearchTool);
114                } else {
115                    ToolRegistry::global(cx).unregister_tool(WebSearchTool);
116                }
117            }
118            _ => {}
119        },
120    )
121    .detach();
122}
123
124fn register_edit_file_tool(cx: &mut App) {
125    let registry = ToolRegistry::global(cx);
126
127    registry.unregister_tool(CreateFileTool);
128    registry.unregister_tool(EditFileTool);
129    registry.unregister_tool(StreamingEditFileTool);
130
131    if AssistantSettings::get_global(cx).stream_edits(cx) {
132        registry.register_tool(StreamingEditFileTool);
133    } else {
134        registry.register_tool(CreateFileTool);
135        registry.register_tool(EditFileTool);
136    }
137}
138
139#[cfg(test)]
140mod tests {
141    use super::*;
142    use client::Client;
143    use clock::FakeSystemClock;
144    use http_client::FakeHttpClient;
145    use schemars::JsonSchema;
146    use serde::Serialize;
147
148    #[test]
149    fn test_json_schema() {
150        #[derive(Serialize, JsonSchema)]
151        struct GetWeatherTool {
152            location: String,
153        }
154
155        let schema = schema::json_schema_for::<GetWeatherTool>(
156            language_model::LanguageModelToolSchemaFormat::JsonSchema,
157        )
158        .unwrap();
159
160        assert_eq!(
161            schema,
162            serde_json::json!({
163                "type": "object",
164                "properties": {
165                    "location": {
166                        "type": "string"
167                    }
168                },
169                "required": ["location"],
170            })
171        );
172    }
173
174    #[gpui::test]
175    fn test_builtin_tool_schema_compatibility(cx: &mut App) {
176        settings::init(cx);
177        AssistantSettings::register(cx);
178
179        let client = Client::new(
180            Arc::new(FakeSystemClock::new()),
181            FakeHttpClient::with_200_response(),
182            cx,
183        );
184        language_model::init(client.clone(), cx);
185        crate::init(client.http_client(), cx);
186
187        for tool in ToolRegistry::global(cx).tools() {
188            let actual_schema = tool
189                .input_schema(language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset)
190                .unwrap();
191            let mut expected_schema = actual_schema.clone();
192            assistant_tool::adapt_schema_to_format(
193                &mut expected_schema,
194                language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset,
195            )
196            .unwrap();
197
198            let error_message = format!(
199                "Tool schema for `{}` is not compatible with `language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset` (Gemini Models).\n\
200                Are you using `schema::json_schema_for<T>(format)` to generate the schema?",
201                tool.name(),
202            );
203
204            assert_eq!(actual_schema, expected_schema, "{}", error_message)
205        }
206    }
207}