1use crate::{AgentTool, ToolCallEventStream};
2use action_log::ActionLog;
3use agent_client_protocol::ToolKind;
4use anyhow::{Context as _, Result, anyhow};
5use futures::{SinkExt, StreamExt, channel::mpsc};
6use gpui::{App, AppContext, Entity, SharedString, Task};
7use project::{Project, ProjectPath};
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11
12/// Deletes the file or directory (and the directory's contents, recursively) at the specified path in the project, and returns confirmation of the deletion.
13#[derive(Debug, Serialize, Deserialize, JsonSchema)]
14pub struct DeletePathToolInput {
15 /// The path of the file or directory to delete.
16 ///
17 /// <example>
18 /// If the project has the following files:
19 ///
20 /// - directory1/a/something.txt
21 /// - directory2/a/things.txt
22 /// - directory3/a/other.txt
23 ///
24 /// You can delete the first file by providing a path of "directory1/a/something.txt"
25 /// </example>
26 pub path: String,
27}
28
29pub struct DeletePathTool {
30 project: Entity<Project>,
31 action_log: Entity<ActionLog>,
32}
33
34impl DeletePathTool {
35 pub fn new(project: Entity<Project>, action_log: Entity<ActionLog>) -> Self {
36 Self {
37 project,
38 action_log,
39 }
40 }
41}
42
43impl AgentTool for DeletePathTool {
44 type Input = DeletePathToolInput;
45 type Output = String;
46
47 fn name() -> &'static str {
48 "delete_path"
49 }
50
51 fn kind() -> ToolKind {
52 ToolKind::Delete
53 }
54
55 fn initial_title(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString {
56 if let Ok(input) = input {
57 format!("Delete “`{}`”", input.path).into()
58 } else {
59 "Delete path".into()
60 }
61 }
62
63 fn run(
64 self: Arc<Self>,
65 input: Self::Input,
66 _event_stream: ToolCallEventStream,
67 cx: &mut App,
68 ) -> Task<Result<Self::Output>> {
69 let path = input.path;
70 let Some(project_path) = self.project.read(cx).find_project_path(&path, cx) else {
71 return Task::ready(Err(anyhow!(
72 "Couldn't delete {path} because that path isn't in this project."
73 )));
74 };
75
76 let Some(worktree) = self
77 .project
78 .read(cx)
79 .worktree_for_id(project_path.worktree_id, cx)
80 else {
81 return Task::ready(Err(anyhow!(
82 "Couldn't delete {path} because that path isn't in this project."
83 )));
84 };
85
86 let worktree_snapshot = worktree.read(cx).snapshot();
87 let (mut paths_tx, mut paths_rx) = mpsc::channel(256);
88 cx.background_spawn({
89 let project_path = project_path.clone();
90 async move {
91 for entry in
92 worktree_snapshot.traverse_from_path(true, false, false, &project_path.path)
93 {
94 if !entry.path.starts_with(&project_path.path) {
95 break;
96 }
97 paths_tx
98 .send(ProjectPath {
99 worktree_id: project_path.worktree_id,
100 path: entry.path.clone(),
101 })
102 .await?;
103 }
104 anyhow::Ok(())
105 }
106 })
107 .detach();
108
109 let project = self.project.clone();
110 let action_log = self.action_log.clone();
111 cx.spawn(async move |cx| {
112 while let Some(path) = paths_rx.next().await {
113 if let Ok(buffer) = project
114 .update(cx, |project, cx| project.open_buffer(path, cx))?
115 .await
116 {
117 action_log.update(cx, |action_log, cx| {
118 action_log.will_delete_buffer(buffer.clone(), cx)
119 })?;
120 }
121 }
122
123 let deletion_task = project
124 .update(cx, |project, cx| {
125 project.delete_file(project_path, false, cx)
126 })?
127 .with_context(|| {
128 format!("Couldn't delete {path} because that path isn't in this project.")
129 })?;
130 deletion_task
131 .await
132 .with_context(|| format!("Deleting {path}"))?;
133 Ok(format!("Deleted {path}"))
134 })
135 }
136}