assistant_tools.rs

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