1use anyhow::{anyhow, Result};
2use assistant_tool::{ActionLog, 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: String,
35}
36
37pub struct ListDirectoryTool;
38
39impl Tool for ListDirectoryTool {
40 fn name(&self) -> String {
41 "list-directory".into()
42 }
43
44 fn needs_confirmation(&self) -> bool {
45 false
46 }
47
48 fn description(&self) -> String {
49 include_str!("./list_directory_tool/description.md").into()
50 }
51
52 fn input_schema(&self) -> serde_json::Value {
53 let schema = schemars::schema_for!(ListDirectoryToolInput);
54 serde_json::to_value(&schema).unwrap()
55 }
56
57 fn ui_text(&self, input: &serde_json::Value) -> String {
58 match serde_json::from_value::<ListDirectoryToolInput>(input.clone()) {
59 Ok(input) => format!("List the `{}` directory's contents", input.path),
60 Err(_) => "List directory".to_string(),
61 }
62 }
63
64 fn run(
65 self: Arc<Self>,
66 input: serde_json::Value,
67 _messages: &[LanguageModelRequestMessage],
68 project: Entity<Project>,
69 _action_log: Entity<ActionLog>,
70 cx: &mut App,
71 ) -> Task<Result<String>> {
72 let input = match serde_json::from_value::<ListDirectoryToolInput>(input) {
73 Ok(input) => input,
74 Err(err) => return Task::ready(Err(anyhow!(err))),
75 };
76
77 // Sometimes models will return these even though we tell it to give a path and not a glob.
78 // When this happens, just list the root worktree directories.
79 if matches!(input.path.as_str(), "." | "" | "./" | "*") {
80 let output = project
81 .read(cx)
82 .worktrees(cx)
83 .filter_map(|worktree| {
84 worktree.read(cx).root_entry().and_then(|entry| {
85 if entry.is_dir() {
86 entry.path.to_str()
87 } else {
88 None
89 }
90 })
91 })
92 .collect::<Vec<_>>()
93 .join("\n");
94
95 return Task::ready(Ok(output));
96 }
97
98 let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
99 return Task::ready(Err(anyhow!("Path {} not found in project", input.path)));
100 };
101 let Some(worktree) = project
102 .read(cx)
103 .worktree_for_id(project_path.worktree_id, cx)
104 else {
105 return Task::ready(Err(anyhow!("Worktree not found")));
106 };
107 let worktree = worktree.read(cx);
108
109 let Some(entry) = worktree.entry_for_path(&project_path.path) else {
110 return Task::ready(Err(anyhow!("Path not found: {}", input.path)));
111 };
112
113 if !entry.is_dir() {
114 return Task::ready(Err(anyhow!("{} is not a directory.", input.path)));
115 }
116
117 let mut output = String::new();
118 for entry in worktree.child_entries(&project_path.path) {
119 writeln!(
120 output,
121 "{}",
122 Path::new(worktree.root_name()).join(&entry.path).display(),
123 )
124 .unwrap();
125 }
126 if output.is_empty() {
127 return Task::ready(Ok(format!("{} is empty.", input.path)));
128 }
129 Task::ready(Ok(output))
130 }
131}