assistant_tools.rs

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