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::MarkdownInlineCode;
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 false
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!("Create directory {}", MarkdownInlineCode(&input.path))
57 }
58 Err(_) => "Create directory".to_string(),
59 }
60 }
61
62 fn run(
63 self: Arc<Self>,
64 input: serde_json::Value,
65 _messages: &[LanguageModelRequestMessage],
66 project: Entity<Project>,
67 _action_log: Entity<ActionLog>,
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 .map_err(|err| anyhow!("Unable to create directory {destination_path}: {err}"))?;
90
91 Ok(format!("Created directory {destination_path}"))
92 })
93 .into()
94 }
95}