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