list_directory_tool.rs

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