list_directory_tool.rs

  1use crate::schema::json_schema_for;
  2use anyhow::{Result, anyhow};
  3use assistant_tool::{ActionLog, Tool, ToolResult};
  4use gpui::{AnyWindowHandle, App, Entity, Task};
  5use language_model::{LanguageModel, LanguageModelRequest, LanguageModelToolSchemaFormat};
  6use project::Project;
  7use schemars::JsonSchema;
  8use serde::{Deserialize, Serialize};
  9use std::{fmt::Write, path::Path, sync::Arc};
 10use ui::IconName;
 11use util::markdown::MarkdownInlineCode;
 12
 13#[derive(Debug, Serialize, Deserialize, JsonSchema)]
 14pub struct ListDirectoryToolInput {
 15    /// The fully-qualified path of the directory to list in the project.
 16    ///
 17    /// This path should never be absolute, and the first component
 18    /// of the path should always be a root directory in a project.
 19    ///
 20    /// <example>
 21    /// If the project has the following root directories:
 22    ///
 23    /// - directory1
 24    /// - directory2
 25    ///
 26    /// You can list the contents of `directory1` by using the path `directory1`.
 27    /// </example>
 28    ///
 29    /// <example>
 30    /// If the project has the following root directories:
 31    ///
 32    /// - foo
 33    /// - bar
 34    ///
 35    /// If you wanna list contents in the directory `foo/baz`, you should use the path `foo/baz`.
 36    /// </example>
 37    pub path: String,
 38}
 39
 40pub struct ListDirectoryTool;
 41
 42impl Tool for ListDirectoryTool {
 43    fn name(&self) -> String {
 44        "list_directory".into()
 45    }
 46
 47    fn needs_confirmation(&self, _: &serde_json::Value, _: &App) -> bool {
 48        false
 49    }
 50
 51    fn description(&self) -> String {
 52        include_str!("./list_directory_tool/description.md").into()
 53    }
 54
 55    fn icon(&self) -> IconName {
 56        IconName::Folder
 57    }
 58
 59    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
 60        json_schema_for::<ListDirectoryToolInput>(format)
 61    }
 62
 63    fn ui_text(&self, input: &serde_json::Value) -> String {
 64        match serde_json::from_value::<ListDirectoryToolInput>(input.clone()) {
 65            Ok(input) => {
 66                let path = MarkdownInlineCode(&input.path);
 67                format!("List the {path} directory's contents")
 68            }
 69            Err(_) => "List directory".to_string(),
 70        }
 71    }
 72
 73    fn run(
 74        self: Arc<Self>,
 75        input: serde_json::Value,
 76        _request: Arc<LanguageModelRequest>,
 77        project: Entity<Project>,
 78        _action_log: Entity<ActionLog>,
 79        _model: Arc<dyn LanguageModel>,
 80        _window: Option<AnyWindowHandle>,
 81        cx: &mut App,
 82    ) -> ToolResult {
 83        let input = match serde_json::from_value::<ListDirectoryToolInput>(input) {
 84            Ok(input) => input,
 85            Err(err) => return Task::ready(Err(anyhow!(err))).into(),
 86        };
 87
 88        // Sometimes models will return these even though we tell it to give a path and not a glob.
 89        // When this happens, just list the root worktree directories.
 90        if matches!(input.path.as_str(), "." | "" | "./" | "*") {
 91            let output = project
 92                .read(cx)
 93                .worktrees(cx)
 94                .filter_map(|worktree| {
 95                    worktree.read(cx).root_entry().and_then(|entry| {
 96                        if entry.is_dir() {
 97                            entry.path.to_str()
 98                        } else {
 99                            None
100                        }
101                    })
102                })
103                .collect::<Vec<_>>()
104                .join("\n");
105
106            return Task::ready(Ok(output.into())).into();
107        }
108
109        let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
110            return Task::ready(Err(anyhow!("Path {} not found in project", input.path))).into();
111        };
112        let Some(worktree) = project
113            .read(cx)
114            .worktree_for_id(project_path.worktree_id, cx)
115        else {
116            return Task::ready(Err(anyhow!("Worktree not found"))).into();
117        };
118        let worktree = worktree.read(cx);
119
120        let Some(entry) = worktree.entry_for_path(&project_path.path) else {
121            return Task::ready(Err(anyhow!("Path not found: {}", input.path))).into();
122        };
123
124        if !entry.is_dir() {
125            return Task::ready(Err(anyhow!("{} is not a directory.", input.path))).into();
126        }
127
128        let mut folders = Vec::new();
129        let mut files = Vec::new();
130
131        for entry in worktree.child_entries(&project_path.path) {
132            let full_path = Path::new(worktree.root_name())
133                .join(&entry.path)
134                .display()
135                .to_string();
136            if entry.is_dir() {
137                folders.push(full_path);
138            } else {
139                files.push(full_path);
140            }
141        }
142
143        let mut output = String::new();
144
145        if !folders.is_empty() {
146            writeln!(output, "# Folders:\n{}", folders.join("\n")).unwrap();
147        }
148
149        if !files.is_empty() {
150            writeln!(output, "\n# Files:\n{}", files.join("\n")).unwrap();
151        }
152
153        if output.is_empty() {
154            writeln!(output, "{} is empty.", input.path).unwrap();
155        }
156
157        Task::ready(Ok(output.into())).into()
158    }
159}
160
161#[cfg(test)]
162mod tests {
163    use super::*;
164    use assistant_tool::Tool;
165    use gpui::{AppContext, TestAppContext};
166    use indoc::indoc;
167    use language_model::fake_provider::FakeLanguageModel;
168    use project::{FakeFs, Project};
169    use serde_json::json;
170    use settings::SettingsStore;
171    use util::path;
172
173    fn platform_paths(path_str: &str) -> String {
174        if cfg!(target_os = "windows") {
175            path_str.replace("/", "\\")
176        } else {
177            path_str.to_string()
178        }
179    }
180
181    fn init_test(cx: &mut TestAppContext) {
182        cx.update(|cx| {
183            let settings_store = SettingsStore::test(cx);
184            cx.set_global(settings_store);
185            language::init(cx);
186            Project::init_settings(cx);
187        });
188    }
189
190    #[gpui::test]
191    async fn test_list_directory_separates_files_and_dirs(cx: &mut TestAppContext) {
192        init_test(cx);
193
194        let fs = FakeFs::new(cx.executor());
195        fs.insert_tree(
196            "/project",
197            json!({
198                "src": {
199                    "main.rs": "fn main() {}",
200                    "lib.rs": "pub fn hello() {}",
201                    "models": {
202                        "user.rs": "struct User {}",
203                        "post.rs": "struct Post {}"
204                    },
205                    "utils": {
206                        "helper.rs": "pub fn help() {}"
207                    }
208                },
209                "tests": {
210                    "integration_test.rs": "#[test] fn test() {}"
211                },
212                "README.md": "# Project",
213                "Cargo.toml": "[package]"
214            }),
215        )
216        .await;
217
218        let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
219        let action_log = cx.new(|_| ActionLog::new(project.clone()));
220        let model = Arc::new(FakeLanguageModel::default());
221        let tool = Arc::new(ListDirectoryTool);
222
223        // Test listing root directory
224        let input = json!({
225            "path": "project"
226        });
227
228        let result = cx
229            .update(|cx| {
230                tool.clone().run(
231                    input,
232                    Arc::default(),
233                    project.clone(),
234                    action_log.clone(),
235                    model.clone(),
236                    None,
237                    cx,
238                )
239            })
240            .output
241            .await
242            .unwrap();
243
244        let content = result.content.as_str().unwrap();
245        assert_eq!(
246            content,
247            platform_paths(indoc! {"
248                # Folders:
249                project/src
250                project/tests
251
252                # Files:
253                project/Cargo.toml
254                project/README.md
255            "})
256        );
257
258        // Test listing src directory
259        let input = json!({
260            "path": "project/src"
261        });
262
263        let result = cx
264            .update(|cx| {
265                tool.clone().run(
266                    input,
267                    Arc::default(),
268                    project.clone(),
269                    action_log.clone(),
270                    model.clone(),
271                    None,
272                    cx,
273                )
274            })
275            .output
276            .await
277            .unwrap();
278
279        let content = result.content.as_str().unwrap();
280        assert_eq!(
281            content,
282            platform_paths(indoc! {"
283                # Folders:
284                project/src/models
285                project/src/utils
286
287                # Files:
288                project/src/lib.rs
289                project/src/main.rs
290            "})
291        );
292
293        // Test listing directory with only files
294        let input = json!({
295            "path": "project/tests"
296        });
297
298        let result = cx
299            .update(|cx| {
300                tool.clone().run(
301                    input,
302                    Arc::default(),
303                    project.clone(),
304                    action_log.clone(),
305                    model.clone(),
306                    None,
307                    cx,
308                )
309            })
310            .output
311            .await
312            .unwrap();
313
314        let content = result.content.as_str().unwrap();
315        assert!(!content.contains("# Folders:"));
316        assert!(content.contains("# Files:"));
317        assert!(content.contains(&platform_paths("project/tests/integration_test.rs")));
318    }
319
320    #[gpui::test]
321    async fn test_list_directory_empty_directory(cx: &mut TestAppContext) {
322        init_test(cx);
323
324        let fs = FakeFs::new(cx.executor());
325        fs.insert_tree(
326            "/project",
327            json!({
328                "empty_dir": {}
329            }),
330        )
331        .await;
332
333        let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
334        let action_log = cx.new(|_| ActionLog::new(project.clone()));
335        let model = Arc::new(FakeLanguageModel::default());
336        let tool = Arc::new(ListDirectoryTool);
337
338        let input = json!({
339            "path": "project/empty_dir"
340        });
341
342        let result = cx
343            .update(|cx| tool.run(input, Arc::default(), project, action_log, model, None, cx))
344            .output
345            .await
346            .unwrap();
347
348        let content = result.content.as_str().unwrap();
349        assert_eq!(content, "project/empty_dir is empty.\n");
350    }
351
352    #[gpui::test]
353    async fn test_list_directory_error_cases(cx: &mut TestAppContext) {
354        init_test(cx);
355
356        let fs = FakeFs::new(cx.executor());
357        fs.insert_tree(
358            "/project",
359            json!({
360                "file.txt": "content"
361            }),
362        )
363        .await;
364
365        let project = Project::test(fs.clone(), [path!("/project").as_ref()], cx).await;
366        let action_log = cx.new(|_| ActionLog::new(project.clone()));
367        let model = Arc::new(FakeLanguageModel::default());
368        let tool = Arc::new(ListDirectoryTool);
369
370        // Test non-existent path
371        let input = json!({
372            "path": "project/nonexistent"
373        });
374
375        let result = cx
376            .update(|cx| {
377                tool.clone().run(
378                    input,
379                    Arc::default(),
380                    project.clone(),
381                    action_log.clone(),
382                    model.clone(),
383                    None,
384                    cx,
385                )
386            })
387            .output
388            .await;
389
390        assert!(result.is_err());
391        assert!(result.unwrap_err().to_string().contains("Path not found"));
392
393        // Test trying to list a file instead of directory
394        let input = json!({
395            "path": "project/file.txt"
396        });
397
398        let result = cx
399            .update(|cx| tool.run(input, Arc::default(), project, action_log, model, None, cx))
400            .output
401            .await;
402
403        assert!(result.is_err());
404        assert!(
405            result
406                .unwrap_err()
407                .to_string()
408                .contains("is not a directory")
409        );
410    }
411}