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