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 needs_confirmation(&self) -> bool {
34 true
35 }
36
37 fn description(&self) -> String {
38 include_str!("./delete_path_tool/description.md").into()
39 }
40
41 fn input_schema(&self) -> serde_json::Value {
42 let schema = schemars::schema_for!(DeletePathToolInput);
43 serde_json::to_value(&schema).unwrap()
44 }
45
46 fn ui_text(&self, input: &serde_json::Value) -> String {
47 match serde_json::from_value::<DeletePathToolInput>(input.clone()) {
48 Ok(input) => format!("Delete “`{}`”", input.path),
49 Err(_) => "Delete path".to_string(),
50 }
51 }
52
53 fn run(
54 self: Arc<Self>,
55 input: serde_json::Value,
56 _messages: &[LanguageModelRequestMessage],
57 project: Entity<Project>,
58 _action_log: Entity<ActionLog>,
59 cx: &mut App,
60 ) -> Task<Result<String>> {
61 let path_str = match serde_json::from_value::<DeletePathToolInput>(input) {
62 Ok(input) => input.path,
63 Err(err) => return Task::ready(Err(anyhow!(err))),
64 };
65
66 match project
67 .read(cx)
68 .find_project_path(&path_str, cx)
69 .and_then(|path| project.update(cx, |project, cx| project.delete_file(path, false, cx)))
70 {
71 Some(deletion_task) => cx.background_spawn(async move {
72 match deletion_task.await {
73 Ok(()) => Ok(format!("Deleted {path_str}")),
74 Err(err) => Err(anyhow!("Failed to delete {path_str}: {err}")),
75 }
76 }),
77 None => Task::ready(Err(anyhow!(
78 "Couldn't delete {path_str} because that path isn't in this project."
79 ))),
80 }
81 }
82}