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 output = String::new();
129 for entry in worktree.child_entries(&project_path.path) {
130 writeln!(
131 output,
132 "{}",
133 Path::new(worktree.root_name()).join(&entry.path).display(),
134 )
135 .unwrap();
136 }
137 if output.is_empty() {
138 return Task::ready(Ok(format!("{} is empty.", input.path).into())).into();
139 }
140 Task::ready(Ok(output.into())).into()
141 }
142}