1use super::edit_file_tool::{
2 SensitiveSettingsKind, is_sensitive_settings_path, sensitive_settings_kind,
3};
4use crate::{AgentTool, ToolCallEventStream, ToolPermissionDecision, decide_permission_for_paths};
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 paths = vec![input.source_path.clone(), input.destination_path.clone()];
102 let decision = decide_permission_for_paths(Self::NAME, &paths, settings);
103 if let ToolPermissionDecision::Deny(reason) = decision {
104 return Task::ready(Err(anyhow!("{}", reason)));
105 }
106
107 let needs_confirmation = matches!(decision, ToolPermissionDecision::Confirm)
108 || (matches!(decision, ToolPermissionDecision::Allow)
109 && (is_sensitive_settings_path(Path::new(&input.source_path))
110 || is_sensitive_settings_path(Path::new(&input.destination_path))));
111
112 let authorize = if needs_confirmation {
113 let src = MarkdownInlineCode(&input.source_path);
114 let dest = MarkdownInlineCode(&input.destination_path);
115 let context = crate::ToolPermissionContext {
116 tool_name: Self::NAME.to_string(),
117 input_values: vec![input.source_path.clone(), input.destination_path.clone()],
118 };
119 let title = format!("Move {src} to {dest}");
120 let settings_kind = sensitive_settings_kind(Path::new(&input.source_path))
121 .or_else(|| sensitive_settings_kind(Path::new(&input.destination_path)));
122 let title = match settings_kind {
123 Some(SensitiveSettingsKind::Local) => format!("{title} (local settings)"),
124 Some(SensitiveSettingsKind::Global) => format!("{title} (settings)"),
125 None => title,
126 };
127 Some(event_stream.authorize(title, context, cx))
128 } else {
129 None
130 };
131
132 let project = self.project.clone();
133 cx.spawn(async move |cx| {
134 if let Some(authorize) = authorize {
135 authorize.await?;
136 }
137
138 let rename_task = project.update(cx, |project, cx| {
139 match project
140 .find_project_path(&input.source_path, cx)
141 .and_then(|project_path| project.entry_for_path(&project_path, cx))
142 {
143 Some(entity) => match project.find_project_path(&input.destination_path, cx) {
144 Some(project_path) => Ok(project.rename_entry(entity.id, project_path, cx)),
145 None => Err(anyhow!(
146 "Destination path {} was outside the project.",
147 input.destination_path
148 )),
149 },
150 None => Err(anyhow!(
151 "Source path {} was not found in the project.",
152 input.source_path
153 )),
154 }
155 })?;
156
157 let result = futures::select! {
158 result = rename_task.fuse() => result,
159 _ = event_stream.cancelled_by_user().fuse() => {
160 anyhow::bail!("Move cancelled by user");
161 }
162 };
163 result.with_context(|| {
164 format!("Moving {} to {}", input.source_path, input.destination_path)
165 })?;
166 Ok(format!(
167 "Moved {} to {}",
168 input.source_path, input.destination_path
169 ))
170 })
171 }
172}