1use anyhow::{anyhow, Result};
2use assistant_tool::Tool;
3use gpui::{App, Entity, Task};
4use language_model::LanguageModelRequestMessage;
5use project::Project;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::{fmt::Write, path::Path, sync::Arc};
9
10#[derive(Debug, Serialize, Deserialize, JsonSchema)]
11pub struct ListDirectoryToolInput {
12 /// The relative path of the directory to list.
13 ///
14 /// This path should never be absolute, and the first component
15 /// of the path should always be a root directory in a project.
16 ///
17 /// <example>
18 /// If the project has the following root directories:
19 ///
20 /// - directory1
21 /// - directory2
22 ///
23 /// You can list the contents of `directory1` by using the path `directory1`.
24 /// </example>
25 ///
26 /// <example>
27 /// If the project has the following root directories:
28 ///
29 /// - foo
30 /// - bar
31 ///
32 /// If you wanna list contents in the directory `foo/baz`, you should use the path `foo/baz`.
33 /// </example>
34 pub path: Arc<Path>,
35}
36
37pub struct ListDirectoryTool;
38
39impl Tool for ListDirectoryTool {
40 fn name(&self) -> String {
41 "list-directory".into()
42 }
43
44 fn description(&self) -> String {
45 include_str!("./list_directory_tool/description.md").into()
46 }
47
48 fn input_schema(&self) -> serde_json::Value {
49 let schema = schemars::schema_for!(ListDirectoryToolInput);
50 serde_json::to_value(&schema).unwrap()
51 }
52
53 fn run(
54 self: Arc<Self>,
55 input: serde_json::Value,
56 _messages: &[LanguageModelRequestMessage],
57 project: Entity<Project>,
58 cx: &mut App,
59 ) -> Task<Result<String>> {
60 let input = match serde_json::from_value::<ListDirectoryToolInput>(input) {
61 Ok(input) => input,
62 Err(err) => return Task::ready(Err(anyhow!(err))),
63 };
64
65 let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
66 return Task::ready(Err(anyhow!("Path not found in project")));
67 };
68 let Some(worktree) = project
69 .read(cx)
70 .worktree_for_id(project_path.worktree_id, cx)
71 else {
72 return Task::ready(Err(anyhow!("Worktree not found")));
73 };
74 let worktree = worktree.read(cx);
75
76 let Some(entry) = worktree.entry_for_path(&project_path.path) else {
77 return Task::ready(Err(anyhow!("Path not found: {}", input.path.display())));
78 };
79
80 if !entry.is_dir() {
81 return Task::ready(Err(anyhow!("{} is a file.", input.path.display())));
82 }
83
84 let mut output = String::new();
85 for entry in worktree.child_entries(&project_path.path) {
86 writeln!(
87 output,
88 "{}",
89 Path::new(worktree.root_name()).join(&entry.path).display(),
90 )
91 .unwrap();
92 }
93 if output.is_empty() {
94 return Task::ready(Ok(format!("{} is empty.", input.path.display())));
95 }
96 Task::ready(Ok(output))
97 }
98}