1use agent_client_protocol::ToolKind;
2use anyhow::{Context as _, Result, anyhow};
3use gpui::{App, Entity, SharedString, Task};
4use project::Project;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use std::sync::Arc;
8use util::markdown::MarkdownInlineCode;
9
10use crate::{AgentTool, ToolCallEventStream};
11
12/// Creates a new directory at the specified path within the project. Returns
13/// confirmation that the directory was created.
14///
15/// This tool creates a directory and all necessary parent directories (similar
16/// to `mkdir -p`). It should be used whenever you need to create new
17/// directories within the project.
18#[derive(Debug, Serialize, Deserialize, JsonSchema)]
19pub struct CreateDirectoryToolInput {
20 /// The path of the new directory.
21 ///
22 /// <example>
23 /// If the project has the following structure:
24 ///
25 /// - directory1/
26 /// - directory2/
27 ///
28 /// You can create a new directory by providing a path of "directory1/new_directory"
29 /// </example>
30 pub path: String,
31}
32
33pub struct CreateDirectoryTool {
34 project: Entity<Project>,
35}
36
37impl CreateDirectoryTool {
38 pub fn new(project: Entity<Project>) -> Self {
39 Self { project }
40 }
41}
42
43impl AgentTool for CreateDirectoryTool {
44 type Input = CreateDirectoryToolInput;
45 type Output = String;
46
47 fn name(&self) -> SharedString {
48 "create_directory".into()
49 }
50
51 fn kind(&self) -> ToolKind {
52 ToolKind::Read
53 }
54
55 fn initial_title(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString {
56 if let Ok(input) = input {
57 format!("Create directory {}", MarkdownInlineCode(&input.path)).into()
58 } else {
59 "Create directory".into()
60 }
61 }
62
63 fn run(
64 self: Arc<Self>,
65 input: Self::Input,
66 _event_stream: ToolCallEventStream,
67 cx: &mut App,
68 ) -> Task<Result<Self::Output>> {
69 let project_path = match self.project.read(cx).find_project_path(&input.path, cx) {
70 Some(project_path) => project_path,
71 None => {
72 return Task::ready(Err(anyhow!("Path to create was outside the project")));
73 }
74 };
75 let destination_path: Arc<str> = input.path.as_str().into();
76
77 let create_entry = self.project.update(cx, |project, cx| {
78 project.create_entry(project_path.clone(), true, cx)
79 });
80
81 cx.spawn(async move |_cx| {
82 create_entry
83 .await
84 .with_context(|| format!("Creating directory {destination_path}"))?;
85
86 Ok(format!("Created directory {destination_path}"))
87 })
88 }
89}