create_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;
 6use language_model::LanguageModelToolSchemaFormat;
 7use project::Project;
 8use schemars::JsonSchema;
 9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11use ui::IconName;
12use util::markdown::MarkdownString;
13
14#[derive(Debug, Serialize, Deserialize, JsonSchema)]
15pub struct CreateDirectoryToolInput {
16    /// The path of the new directory.
17    ///
18    /// <example>
19    /// If the project has the following structure:
20    ///
21    /// - directory1/
22    /// - directory2/
23    ///
24    /// You can create a new directory by providing a path of "directory1/new_directory"
25    /// </example>
26    pub path: String,
27}
28
29pub struct CreateDirectoryTool;
30
31impl Tool for CreateDirectoryTool {
32    fn name(&self) -> String {
33        "create_directory".into()
34    }
35
36    fn needs_confirmation(&self, _: &serde_json::Value, _: &App) -> bool {
37        true
38    }
39
40    fn description(&self) -> String {
41        include_str!("./create_directory_tool/description.md").into()
42    }
43
44    fn icon(&self) -> IconName {
45        IconName::Folder
46    }
47
48    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
49        json_schema_for::<CreateDirectoryToolInput>(format)
50    }
51
52    fn ui_text(&self, input: &serde_json::Value) -> String {
53        match serde_json::from_value::<CreateDirectoryToolInput>(input.clone()) {
54            Ok(input) => {
55                format!(
56                    "Create directory {}",
57                    MarkdownString::inline_code(&input.path)
58                )
59            }
60            Err(_) => "Create directory".to_string(),
61        }
62    }
63
64    fn run(
65        self: Arc<Self>,
66        input: serde_json::Value,
67        _messages: &[LanguageModelRequestMessage],
68        project: Entity<Project>,
69        _action_log: Entity<ActionLog>,
70        cx: &mut App,
71    ) -> ToolResult {
72        let input = match serde_json::from_value::<CreateDirectoryToolInput>(input) {
73            Ok(input) => input,
74            Err(err) => return Task::ready(Err(anyhow!(err))).into(),
75        };
76        let project_path = match project.read(cx).find_project_path(&input.path, cx) {
77            Some(project_path) => project_path,
78            None => {
79                return Task::ready(Err(anyhow!("Path to create was outside the project"))).into();
80            }
81        };
82        let destination_path: Arc<str> = input.path.as_str().into();
83
84        cx.spawn(async move |cx| {
85            project
86                .update(cx, |project, cx| {
87                    project.create_entry(project_path.clone(), true, cx)
88                })?
89                .await
90                .map_err(|err| anyhow!("Unable to create directory {destination_path}: {err}"))?;
91
92            Ok(format!("Created directory {destination_path}"))
93        })
94        .into()
95    }
96}