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 top-level directory in a project.
16 ///
17 /// <example>
18 /// If the project has the following top-level 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 top-level 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(worktree_root_name) = input.path.components().next() else {
66 return Task::ready(Err(anyhow!("Invalid path")));
67 };
68 let Some(worktree) = project
69 .read(cx)
70 .worktree_for_root_name(&worktree_root_name.as_os_str().to_string_lossy(), cx)
71 else {
72 return Task::ready(Err(anyhow!("Directory not found in the project")));
73 };
74 let path = input.path.strip_prefix(worktree_root_name).unwrap();
75 let mut output = String::new();
76 for entry in worktree.read(cx).child_entries(path) {
77 writeln!(
78 output,
79 "{}",
80 Path::new(worktree_root_name.as_os_str())
81 .join(&entry.path)
82 .display(),
83 )
84 .unwrap();
85 }
86 Task::ready(Ok(output))
87 }
88}