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};
9use ui::IconName;
10
11#[derive(Debug, Serialize, Deserialize, JsonSchema)]
12pub struct ListDirectoryToolInput {
13 /// The relative path of the directory to list.
14 ///
15 /// This path should never be absolute, and the first component
16 /// of the path should always be a root directory in a project.
17 ///
18 /// <example>
19 /// If the project has the following root directories:
20 ///
21 /// - directory1
22 /// - directory2
23 ///
24 /// You can list the contents of `directory1` by using the path `directory1`.
25 /// </example>
26 ///
27 /// <example>
28 /// If the project has the following root directories:
29 ///
30 /// - foo
31 /// - bar
32 ///
33 /// If you wanna list contents in the directory `foo/baz`, you should use the path `foo/baz`.
34 /// </example>
35 pub path: String,
36}
37
38pub struct ListDirectoryTool;
39
40impl Tool for ListDirectoryTool {
41 fn name(&self) -> String {
42 "list-directory".into()
43 }
44
45 fn needs_confirmation(&self) -> bool {
46 false
47 }
48
49 fn description(&self) -> String {
50 include_str!("./list_directory_tool/description.md").into()
51 }
52
53 fn icon(&self) -> IconName {
54 IconName::Folder
55 }
56
57 fn input_schema(&self) -> serde_json::Value {
58 let schema = schemars::schema_for!(ListDirectoryToolInput);
59 serde_json::to_value(&schema).unwrap()
60 }
61
62 fn ui_text(&self, input: &serde_json::Value) -> String {
63 match serde_json::from_value::<ListDirectoryToolInput>(input.clone()) {
64 Ok(input) => format!("List the `{}` directory's contents", input.path),
65 Err(_) => "List directory".to_string(),
66 }
67 }
68
69 fn run(
70 self: Arc<Self>,
71 input: serde_json::Value,
72 _messages: &[LanguageModelRequestMessage],
73 project: Entity<Project>,
74 _action_log: Entity<ActionLog>,
75 cx: &mut App,
76 ) -> Task<Result<String>> {
77 let input = match serde_json::from_value::<ListDirectoryToolInput>(input) {
78 Ok(input) => input,
79 Err(err) => return Task::ready(Err(anyhow!(err))),
80 };
81
82 // Sometimes models will return these even though we tell it to give a path and not a glob.
83 // When this happens, just list the root worktree directories.
84 if matches!(input.path.as_str(), "." | "" | "./" | "*") {
85 let output = project
86 .read(cx)
87 .worktrees(cx)
88 .filter_map(|worktree| {
89 worktree.read(cx).root_entry().and_then(|entry| {
90 if entry.is_dir() {
91 entry.path.to_str()
92 } else {
93 None
94 }
95 })
96 })
97 .collect::<Vec<_>>()
98 .join("\n");
99
100 return Task::ready(Ok(output));
101 }
102
103 let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
104 return Task::ready(Err(anyhow!("Path {} not found in project", input.path)));
105 };
106 let Some(worktree) = project
107 .read(cx)
108 .worktree_for_id(project_path.worktree_id, cx)
109 else {
110 return Task::ready(Err(anyhow!("Worktree not found")));
111 };
112 let worktree = worktree.read(cx);
113
114 let Some(entry) = worktree.entry_for_path(&project_path.path) else {
115 return Task::ready(Err(anyhow!("Path not found: {}", input.path)));
116 };
117
118 if !entry.is_dir() {
119 return Task::ready(Err(anyhow!("{} is not a directory.", input.path)));
120 }
121
122 let mut output = String::new();
123 for entry in worktree.child_entries(&project_path.path) {
124 writeln!(
125 output,
126 "{}",
127 Path::new(worktree.root_name()).join(&entry.path).display(),
128 )
129 .unwrap();
130 }
131 if output.is_empty() {
132 return Task::ready(Ok(format!("{} is empty.", input.path)));
133 }
134 Task::ready(Ok(output))
135 }
136}