1use anyhow::{anyhow, Result};
2use assistant_tool::{ActionLog, Tool};
3use gpui::{App, AppContext, Entity, Task};
4use language_model::LanguageModelRequestMessage;
5use project::Project;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9
10#[derive(Debug, Serialize, Deserialize, JsonSchema)]
11pub struct DeletePathToolInput {
12 /// The path of the file or directory to delete.
13 ///
14 /// <example>
15 /// If the project has the following files:
16 ///
17 /// - directory1/a/something.txt
18 /// - directory2/a/things.txt
19 /// - directory3/a/other.txt
20 ///
21 /// You can delete the first file by providing a path of "directory1/a/something.txt"
22 /// </example>
23 pub path: String,
24}
25
26pub struct DeletePathTool;
27
28impl Tool for DeletePathTool {
29 fn name(&self) -> String {
30 "delete-path".into()
31 }
32
33 fn description(&self) -> String {
34 include_str!("./delete_path_tool/description.md").into()
35 }
36
37 fn input_schema(&self) -> serde_json::Value {
38 let schema = schemars::schema_for!(DeletePathToolInput);
39 serde_json::to_value(&schema).unwrap()
40 }
41
42 fn ui_text(&self, input: &serde_json::Value) -> String {
43 match serde_json::from_value::<DeletePathToolInput>(input.clone()) {
44 Ok(input) => format!("Delete “`{}`”", input.path),
45 Err(_) => "Delete path".to_string(),
46 }
47 }
48
49 fn run(
50 self: Arc<Self>,
51 input: serde_json::Value,
52 _messages: &[LanguageModelRequestMessage],
53 project: Entity<Project>,
54 _action_log: Entity<ActionLog>,
55 cx: &mut App,
56 ) -> Task<Result<String>> {
57 let path_str = match serde_json::from_value::<DeletePathToolInput>(input) {
58 Ok(input) => input.path,
59 Err(err) => return Task::ready(Err(anyhow!(err))),
60 };
61
62 match project
63 .read(cx)
64 .find_project_path(&path_str, cx)
65 .and_then(|path| project.update(cx, |project, cx| project.delete_file(path, false, cx)))
66 {
67 Some(deletion_task) => cx.background_spawn(async move {
68 match deletion_task.await {
69 Ok(()) => Ok(format!("Deleted {path_str}")),
70 Err(err) => Err(anyhow!("Failed to delete {path_str}: {err}")),
71 }
72 }),
73 None => Task::ready(Err(anyhow!(
74 "Couldn't delete {path_str} because that path isn't in this project."
75 ))),
76 }
77 }
78}