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::{LanguageModelRequestMessage, 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 _messages: &[LanguageModelRequestMessage],
77 project: Entity<Project>,
78 _action_log: Entity<ActionLog>,
79 _window: Option<AnyWindowHandle>,
80 cx: &mut App,
81 ) -> ToolResult {
82 let input = match serde_json::from_value::<ListDirectoryToolInput>(input) {
83 Ok(input) => input,
84 Err(err) => return Task::ready(Err(anyhow!(err))).into(),
85 };
86
87 // Sometimes models will return these even though we tell it to give a path and not a glob.
88 // When this happens, just list the root worktree directories.
89 if matches!(input.path.as_str(), "." | "" | "./" | "*") {
90 let output = project
91 .read(cx)
92 .worktrees(cx)
93 .filter_map(|worktree| {
94 worktree.read(cx).root_entry().and_then(|entry| {
95 if entry.is_dir() {
96 entry.path.to_str()
97 } else {
98 None
99 }
100 })
101 })
102 .collect::<Vec<_>>()
103 .join("\n");
104
105 return Task::ready(Ok(output)).into();
106 }
107
108 let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
109 return Task::ready(Err(anyhow!("Path {} not found in project", input.path))).into();
110 };
111 let Some(worktree) = project
112 .read(cx)
113 .worktree_for_id(project_path.worktree_id, cx)
114 else {
115 return Task::ready(Err(anyhow!("Worktree not found"))).into();
116 };
117 let worktree = worktree.read(cx);
118
119 let Some(entry) = worktree.entry_for_path(&project_path.path) else {
120 return Task::ready(Err(anyhow!("Path not found: {}", input.path))).into();
121 };
122
123 if !entry.is_dir() {
124 return Task::ready(Err(anyhow!("{} is not a directory.", input.path))).into();
125 }
126
127 let mut output = String::new();
128 for entry in worktree.child_entries(&project_path.path) {
129 writeln!(
130 output,
131 "{}",
132 Path::new(worktree.root_name()).join(&entry.path).display(),
133 )
134 .unwrap();
135 }
136 if output.is_empty() {
137 return Task::ready(Ok(format!("{} is empty.", input.path))).into();
138 }
139 Task::ready(Ok(output)).into()
140 }
141}