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 run(
43 self: Arc<Self>,
44 input: serde_json::Value,
45 _messages: &[LanguageModelRequestMessage],
46 project: Entity<Project>,
47 _action_log: Entity<ActionLog>,
48 cx: &mut App,
49 ) -> Task<Result<String>> {
50 let path_str = match serde_json::from_value::<DeletePathToolInput>(input) {
51 Ok(input) => input.path,
52 Err(err) => return Task::ready(Err(anyhow!(err))),
53 };
54
55 match project
56 .read(cx)
57 .find_project_path(&path_str, cx)
58 .and_then(|path| project.update(cx, |project, cx| project.delete_file(path, false, cx)))
59 {
60 Some(deletion_task) => cx.background_spawn(async move {
61 match deletion_task.await {
62 Ok(()) => Ok(format!("Deleted {}", &path_str)),
63 Err(err) => Err(anyhow!("Failed to delete {}: {}", &path_str, err)),
64 }
65 }),
66 None => Task::ready(Err(anyhow!(
67 "Couldn't delete {} because that path isn't in this project.",
68 path_str
69 ))),
70 }
71 }
72}