create_directory_tool.rs

 1use crate::schema::json_schema_for;
 2use anyhow::{Context as _, Result, anyhow};
 3use assistant_tool::{ActionLog, Tool, ToolResult};
 4use gpui::AnyWindowHandle;
 5use gpui::{App, Entity, Task};
 6use language_model::{LanguageModel, LanguageModelRequest, LanguageModelToolSchemaFormat};
 7use project::Project;
 8use schemars::JsonSchema;
 9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11use ui::IconName;
12use util::markdown::MarkdownInlineCode;
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 description(&self) -> String {
37        include_str!("./create_directory_tool/description.md").into()
38    }
39
40    fn needs_confirmation(&self, _: &serde_json::Value, _: &Entity<Project>, _: &App) -> bool {
41        false
42    }
43
44    fn may_perform_edits(&self) -> bool {
45        false
46    }
47
48    fn icon(&self) -> IconName {
49        IconName::ToolFolder
50    }
51
52    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
53        json_schema_for::<CreateDirectoryToolInput>(format)
54    }
55
56    fn ui_text(&self, input: &serde_json::Value) -> String {
57        match serde_json::from_value::<CreateDirectoryToolInput>(input.clone()) {
58            Ok(input) => {
59                format!("Create directory {}", MarkdownInlineCode(&input.path))
60            }
61            Err(_) => "Create directory".to_string(),
62        }
63    }
64
65    fn run(
66        self: Arc<Self>,
67        input: serde_json::Value,
68        _request: Arc<LanguageModelRequest>,
69        project: Entity<Project>,
70        _action_log: Entity<ActionLog>,
71        _model: Arc<dyn LanguageModel>,
72        _window: Option<AnyWindowHandle>,
73        cx: &mut App,
74    ) -> ToolResult {
75        let input = match serde_json::from_value::<CreateDirectoryToolInput>(input) {
76            Ok(input) => input,
77            Err(err) => return Task::ready(Err(anyhow!(err))).into(),
78        };
79        let project_path = match project.read(cx).find_project_path(&input.path, cx) {
80            Some(project_path) => project_path,
81            None => {
82                return Task::ready(Err(anyhow!("Path to create was outside the project"))).into();
83            }
84        };
85        let destination_path: Arc<str> = input.path.as_str().into();
86
87        cx.spawn(async move |cx| {
88            project
89                .update(cx, |project, cx| {
90                    project.create_entry(project_path.clone(), true, cx)
91                })?
92                .await
93                .with_context(|| format!("Creating directory {destination_path}"))?;
94
95            Ok(format!("Created directory {destination_path}").into())
96        })
97        .into()
98    }
99}