1use crate::schema::json_schema_for;
2use anyhow::{Context as _, Result, anyhow};
3use assistant_tool::{ActionLog, Tool, ToolResult};
4use futures::{SinkExt, StreamExt, channel::mpsc};
5use gpui::{AnyWindowHandle, App, AppContext, Entity, Task};
6use language_model::{LanguageModel, LanguageModelRequest, LanguageModelToolSchemaFormat};
7use project::{Project, ProjectPath};
8use schemars::JsonSchema;
9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11use ui::IconName;
12
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
31impl Tool for DeletePathTool {
32 type Input = DeletePathToolInput;
33
34 fn name(&self) -> String {
35 "delete_path".into()
36 }
37
38 fn needs_confirmation(&self, _: &Self::Input, _: &App) -> bool {
39 false
40 }
41
42 fn may_perform_edits(&self) -> bool {
43 true
44 }
45
46 fn description(&self) -> String {
47 include_str!("./delete_path_tool/description.md").into()
48 }
49
50 fn icon(&self) -> IconName {
51 IconName::FileDelete
52 }
53
54 fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
55 json_schema_for::<DeletePathToolInput>(format)
56 }
57
58 fn ui_text(&self, input: &Self::Input) -> String {
59 format!("Delete “`{}`”", input.path)
60 }
61
62 fn run(
63 self: Arc<Self>,
64 input: Self::Input,
65 _request: Arc<LanguageModelRequest>,
66 project: Entity<Project>,
67 action_log: Entity<ActionLog>,
68 _model: Arc<dyn LanguageModel>,
69 _window: Option<AnyWindowHandle>,
70 cx: &mut App,
71 ) -> ToolResult {
72 let path_str = input.path;
73 let Some(project_path) = project.read(cx).find_project_path(&path_str, cx) else {
74 return Task::ready(Err(anyhow!(
75 "Couldn't delete {path_str} because that path isn't in this project."
76 )))
77 .into();
78 };
79
80 let Some(worktree) = project
81 .read(cx)
82 .worktree_for_id(project_path.worktree_id, cx)
83 else {
84 return Task::ready(Err(anyhow!(
85 "Couldn't delete {path_str} because that path isn't in this project."
86 )))
87 .into();
88 };
89
90 let worktree_snapshot = worktree.read(cx).snapshot();
91 let (mut paths_tx, mut paths_rx) = mpsc::channel(256);
92 cx.background_spawn({
93 let project_path = project_path.clone();
94 async move {
95 for entry in
96 worktree_snapshot.traverse_from_path(true, false, false, &project_path.path)
97 {
98 if !entry.path.starts_with(&project_path.path) {
99 break;
100 }
101 paths_tx
102 .send(ProjectPath {
103 worktree_id: project_path.worktree_id,
104 path: entry.path.clone(),
105 })
106 .await?;
107 }
108 anyhow::Ok(())
109 }
110 })
111 .detach();
112
113 cx.spawn(async move |cx| {
114 while let Some(path) = paths_rx.next().await {
115 if let Ok(buffer) = project
116 .update(cx, |project, cx| project.open_buffer(path, cx))?
117 .await
118 {
119 action_log.update(cx, |action_log, cx| {
120 action_log.will_delete_buffer(buffer.clone(), cx)
121 })?;
122 }
123 }
124
125 let deletion_task = project
126 .update(cx, |project, cx| {
127 project.delete_file(project_path, false, cx)
128 })?
129 .with_context(|| {
130 format!("Couldn't delete {path_str} because that path isn't in this project.")
131 })?;
132 deletion_task
133 .await
134 .with_context(|| format!("Deleting {path_str}"))?;
135 Ok(format!("Deleted {path_str}").into())
136 })
137 .into()
138 }
139}