1use super::edit_file_tool::{
2 SensitiveSettingsKind, is_sensitive_settings_path, sensitive_settings_kind,
3};
4use crate::{AgentTool, ToolCallEventStream, ToolPermissionDecision, decide_permission_for_path};
5use agent_client_protocol::ToolKind;
6use agent_settings::AgentSettings;
7use anyhow::{Context as _, Result, anyhow};
8use futures::FutureExt as _;
9use gpui::{App, Entity, SharedString, Task};
10use project::Project;
11use schemars::JsonSchema;
12use serde::{Deserialize, Serialize};
13use settings::Settings;
14use std::{path::Path, sync::Arc};
15use util::markdown::MarkdownInlineCode;
16
17/// Moves or rename a file or directory in the project, and returns confirmation that the move succeeded.
18///
19/// If the source and destination directories are the same, but the filename is different, this performs a rename. Otherwise, it performs a move.
20///
21/// This tool should be used when it's desirable to move or rename a file or directory without changing its contents at all.
22#[derive(Debug, Serialize, Deserialize, JsonSchema)]
23pub struct MovePathToolInput {
24 /// The source path of the file or directory to move/rename.
25 ///
26 /// <example>
27 /// If the project has the following files:
28 ///
29 /// - directory1/a/something.txt
30 /// - directory2/a/things.txt
31 /// - directory3/a/other.txt
32 ///
33 /// You can move the first file by providing a source_path of "directory1/a/something.txt"
34 /// </example>
35 pub source_path: String,
36
37 /// The destination path where the file or directory should be moved/renamed to.
38 /// If the paths are the same except for the filename, then this will be a rename.
39 ///
40 /// <example>
41 /// To move "directory1/a/something.txt" to "directory2/b/renamed.txt",
42 /// provide a destination_path of "directory2/b/renamed.txt"
43 /// </example>
44 pub destination_path: String,
45}
46
47pub struct MovePathTool {
48 project: Entity<Project>,
49}
50
51impl MovePathTool {
52 pub fn new(project: Entity<Project>) -> Self {
53 Self { project }
54 }
55}
56
57impl AgentTool for MovePathTool {
58 type Input = MovePathToolInput;
59 type Output = String;
60
61 const NAME: &'static str = "move_path";
62
63 fn kind() -> ToolKind {
64 ToolKind::Move
65 }
66
67 fn initial_title(
68 &self,
69 input: Result<Self::Input, serde_json::Value>,
70 _cx: &mut App,
71 ) -> SharedString {
72 if let Ok(input) = input {
73 let src = MarkdownInlineCode(&input.source_path);
74 let dest = MarkdownInlineCode(&input.destination_path);
75 let src_path = Path::new(&input.source_path);
76 let dest_path = Path::new(&input.destination_path);
77
78 match dest_path
79 .file_name()
80 .and_then(|os_str| os_str.to_os_string().into_string().ok())
81 {
82 Some(filename) if src_path.parent() == dest_path.parent() => {
83 let filename = MarkdownInlineCode(&filename);
84 format!("Rename {src} to {filename}").into()
85 }
86 _ => format!("Move {src} to {dest}").into(),
87 }
88 } else {
89 "Move path".into()
90 }
91 }
92
93 fn run(
94 self: Arc<Self>,
95 input: Self::Input,
96 event_stream: ToolCallEventStream,
97 cx: &mut App,
98 ) -> Task<Result<Self::Output>> {
99 let settings = AgentSettings::get_global(cx);
100
101 let source_decision = decide_permission_for_path(Self::NAME, &input.source_path, settings);
102 if let ToolPermissionDecision::Deny(reason) = source_decision {
103 return Task::ready(Err(anyhow!("{}", reason)));
104 }
105
106 let dest_decision =
107 decide_permission_for_path(Self::NAME, &input.destination_path, settings);
108 if let ToolPermissionDecision::Deny(reason) = dest_decision {
109 return Task::ready(Err(anyhow!("{}", reason)));
110 }
111
112 let needs_confirmation = matches!(source_decision, ToolPermissionDecision::Confirm)
113 || matches!(dest_decision, ToolPermissionDecision::Confirm)
114 || (!settings.always_allow_tool_actions
115 && matches!(source_decision, ToolPermissionDecision::Allow)
116 && is_sensitive_settings_path(Path::new(&input.source_path)))
117 || (!settings.always_allow_tool_actions
118 && matches!(dest_decision, ToolPermissionDecision::Allow)
119 && is_sensitive_settings_path(Path::new(&input.destination_path)));
120
121 let authorize = if needs_confirmation {
122 let src = MarkdownInlineCode(&input.source_path);
123 let dest = MarkdownInlineCode(&input.destination_path);
124 let context = crate::ToolPermissionContext {
125 tool_name: Self::NAME.to_string(),
126 input_value: format!("{}\n{}", input.source_path, input.destination_path),
127 };
128 let title = format!("Move {src} to {dest}");
129 let settings_kind = sensitive_settings_kind(Path::new(&input.source_path))
130 .or_else(|| sensitive_settings_kind(Path::new(&input.destination_path)));
131 let title = match settings_kind {
132 Some(SensitiveSettingsKind::Local) => format!("{title} (local settings)"),
133 Some(SensitiveSettingsKind::Global) => format!("{title} (settings)"),
134 None => title,
135 };
136 Some(event_stream.authorize(title, context, cx))
137 } else {
138 None
139 };
140
141 let project = self.project.clone();
142 cx.spawn(async move |cx| {
143 if let Some(authorize) = authorize {
144 authorize.await?;
145 }
146
147 let rename_task = project.update(cx, |project, cx| {
148 match project
149 .find_project_path(&input.source_path, cx)
150 .and_then(|project_path| project.entry_for_path(&project_path, cx))
151 {
152 Some(entity) => match project.find_project_path(&input.destination_path, cx) {
153 Some(project_path) => Ok(project.rename_entry(entity.id, project_path, cx)),
154 None => Err(anyhow!(
155 "Destination path {} was outside the project.",
156 input.destination_path
157 )),
158 },
159 None => Err(anyhow!(
160 "Source path {} was not found in the project.",
161 input.source_path
162 )),
163 }
164 })?;
165
166 let result = futures::select! {
167 result = rename_task.fuse() => result,
168 _ = event_stream.cancelled_by_user().fuse() => {
169 anyhow::bail!("Move cancelled by user");
170 }
171 };
172 let _ = result.with_context(|| {
173 format!("Moving {} to {}", input.source_path, input.destination_path)
174 })?;
175 Ok(format!(
176 "Moved {} to {}",
177 input.source_path, input.destination_path
178 ))
179 })
180 }
181}