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(CreateFileTool);
81 registry.register_tool(CopyPathTool);
82 registry.register_tool(DeletePathTool);
83 registry.register_tool(SymbolInfoTool);
84 registry.register_tool(CodeActionTool);
85 registry.register_tool(MovePathTool);
86 registry.register_tool(DiagnosticsTool);
87 registry.register_tool(ListDirectoryTool);
88 registry.register_tool(NowTool);
89 registry.register_tool(OpenTool);
90 registry.register_tool(CodeSymbolsTool);
91 registry.register_tool(ContentsTool);
92 registry.register_tool(FindPathTool);
93 registry.register_tool(ReadFileTool);
94 registry.register_tool(GrepTool);
95 registry.register_tool(RenameTool);
96 registry.register_tool(ThinkingTool);
97 registry.register_tool(FetchTool::new(http_client));
98
99 register_edit_file_tool(cx);
100 cx.observe_flag::<AgentStreamEditsFeatureFlag, _>(|_, cx| register_edit_file_tool(cx))
101 .detach();
102 cx.observe_global::<SettingsStore>(register_edit_file_tool)
103 .detach();
104
105 cx.subscribe(
106 &LanguageModelRegistry::global(cx),
107 move |registry, event, cx| match event {
108 language_model::Event::DefaultModelChanged => {
109 let using_zed_provider = registry
110 .read(cx)
111 .default_model()
112 .map_or(false, |default| default.is_provided_by_zed());
113 if using_zed_provider {
114 ToolRegistry::global(cx).register_tool(WebSearchTool);
115 } else {
116 ToolRegistry::global(cx).unregister_tool(WebSearchTool);
117 }
118 }
119 _ => {}
120 },
121 )
122 .detach();
123}
124
125fn register_edit_file_tool(cx: &mut App) {
126 let registry = ToolRegistry::global(cx);
127
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(EditFileTool);
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141 use client::Client;
142 use clock::FakeSystemClock;
143 use http_client::FakeHttpClient;
144 use schemars::JsonSchema;
145 use serde::Serialize;
146
147 #[test]
148 fn test_json_schema() {
149 #[derive(Serialize, JsonSchema)]
150 struct GetWeatherTool {
151 location: String,
152 }
153
154 let schema = schema::json_schema_for::<GetWeatherTool>(
155 language_model::LanguageModelToolSchemaFormat::JsonSchema,
156 )
157 .unwrap();
158
159 assert_eq!(
160 schema,
161 serde_json::json!({
162 "type": "object",
163 "properties": {
164 "location": {
165 "type": "string"
166 }
167 },
168 "required": ["location"],
169 })
170 );
171 }
172
173 #[gpui::test]
174 fn test_builtin_tool_schema_compatibility(cx: &mut App) {
175 settings::init(cx);
176 AssistantSettings::register(cx);
177
178 let client = Client::new(
179 Arc::new(FakeSystemClock::new()),
180 FakeHttpClient::with_200_response(),
181 cx,
182 );
183 language_model::init(client.clone(), cx);
184 crate::init(client.http_client(), cx);
185
186 for tool in ToolRegistry::global(cx).tools() {
187 let actual_schema = tool
188 .input_schema(language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset)
189 .unwrap();
190 let mut expected_schema = actual_schema.clone();
191 assistant_tool::adapt_schema_to_format(
192 &mut expected_schema,
193 language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset,
194 )
195 .unwrap();
196
197 let error_message = format!(
198 "Tool schema for `{}` is not compatible with `language_model::LanguageModelToolSchemaFormat::JsonSchemaSubset` (Gemini Models).\n\
199 Are you using `schema::json_schema_for<T>(format)` to generate the schema?",
200 tool.name(),
201 );
202
203 assert_eq!(actual_schema, expected_schema, "{}", error_message)
204 }
205 }
206}