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 needs_confirmation(&self, _: &serde_json::Value, _: &App) -> bool {
37 false
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!("Create directory {}", MarkdownInlineCode(&input.path))
56 }
57 Err(_) => "Create directory".to_string(),
58 }
59 }
60
61 fn run(
62 self: Arc<Self>,
63 input: serde_json::Value,
64 _request: Arc<LanguageModelRequest>,
65 project: Entity<Project>,
66 _action_log: Entity<ActionLog>,
67 _model: Arc<dyn LanguageModel>,
68 _window: Option<AnyWindowHandle>,
69 cx: &mut App,
70 ) -> ToolResult {
71 let input = match serde_json::from_value::<CreateDirectoryToolInput>(input) {
72 Ok(input) => input,
73 Err(err) => return Task::ready(Err(anyhow!(err))).into(),
74 };
75 let project_path = match project.read(cx).find_project_path(&input.path, cx) {
76 Some(project_path) => project_path,
77 None => {
78 return Task::ready(Err(anyhow!("Path to create was outside the project"))).into();
79 }
80 };
81 let destination_path: Arc<str> = input.path.as_str().into();
82
83 cx.spawn(async move |cx| {
84 project
85 .update(cx, |project, cx| {
86 project.create_entry(project_path.clone(), true, cx)
87 })?
88 .await
89 .with_context(|| format!("Creating directory {destination_path}"))?;
90
91 Ok(format!("Created directory {destination_path}").into())
92 })
93 .into()
94 }
95}