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