list_directory_tool.rs

 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: 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        _action_log: Entity<ActionLog>,
59        cx: &mut App,
60    ) -> Task<Result<String>> {
61        let input = match serde_json::from_value::<ListDirectoryToolInput>(input) {
62            Ok(input) => input,
63            Err(err) => return Task::ready(Err(anyhow!(err))),
64        };
65
66        let Some(project_path) = project.read(cx).find_project_path(&input.path, cx) else {
67            return Task::ready(Err(anyhow!("Path not found in project")));
68        };
69        let Some(worktree) = project
70            .read(cx)
71            .worktree_for_id(project_path.worktree_id, cx)
72        else {
73            return Task::ready(Err(anyhow!("Worktree not found")));
74        };
75        let worktree = worktree.read(cx);
76
77        let Some(entry) = worktree.entry_for_path(&project_path.path) else {
78            return Task::ready(Err(anyhow!("Path not found: {}", input.path.display())));
79        };
80
81        if !entry.is_dir() {
82            return Task::ready(Err(anyhow!("{} is a file.", input.path.display())));
83        }
84
85        let mut output = String::new();
86        for entry in worktree.child_entries(&project_path.path) {
87            writeln!(
88                output,
89                "{}",
90                Path::new(worktree.root_name()).join(&entry.path).display(),
91            )
92            .unwrap();
93        }
94        if output.is_empty() {
95            return Task::ready(Ok(format!("{} is empty.", input.path.display())));
96        }
97        Task::ready(Ok(output))
98    }
99}