list_directory_tool.rs

  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 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        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 worktree = worktree.read(cx);
 76
 77        let Some(entry) = worktree.entry_for_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(path) {
 87            writeln!(
 88                output,
 89                "{}",
 90                Path::new(worktree_root_name.as_os_str())
 91                    .join(&entry.path)
 92                    .display(),
 93            )
 94            .unwrap();
 95        }
 96        if output.is_empty() {
 97            return Task::ready(Ok(format!("{} is empty.", input.path.display())));
 98        }
 99        Task::ready(Ok(output))
100    }
101}