1use crate::{AgentTool, ToolCallEventStream};
2use agent_client_protocol::ToolKind;
3use anyhow::{Context as _, Result, anyhow};
4use gpui::{App, AppContext, Entity, SharedString, Task};
5use project::Project;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::{path::Path, sync::Arc};
9use util::markdown::MarkdownInlineCode;
10
11/// Moves or rename a file or directory in the project, and returns confirmation that the move succeeded.
12///
13/// If the source and destination directories are the same, but the filename is different, this performs a rename. Otherwise, it performs a move.
14///
15/// This tool should be used when it's desirable to move or rename a file or directory without changing its contents at all.
16#[derive(Debug, Serialize, Deserialize, JsonSchema)]
17pub struct MovePathToolInput {
18 /// The source path of the file or directory to move/rename.
19 ///
20 /// <example>
21 /// If the project has the following files:
22 ///
23 /// - directory1/a/something.txt
24 /// - directory2/a/things.txt
25 /// - directory3/a/other.txt
26 ///
27 /// You can move the first file by providing a source_path of "directory1/a/something.txt"
28 /// </example>
29 pub source_path: String,
30
31 /// The destination path where the file or directory should be moved/renamed to.
32 /// If the paths are the same except for the filename, then this will be a rename.
33 ///
34 /// <example>
35 /// To move "directory1/a/something.txt" to "directory2/b/renamed.txt",
36 /// provide a destination_path of "directory2/b/renamed.txt"
37 /// </example>
38 pub destination_path: String,
39}
40
41pub struct MovePathTool {
42 project: Entity<Project>,
43}
44
45impl MovePathTool {
46 pub fn new(project: Entity<Project>) -> Self {
47 Self { project }
48 }
49}
50
51impl AgentTool for MovePathTool {
52 type Input = MovePathToolInput;
53 type Output = String;
54
55 fn name() -> &'static str {
56 "move_path"
57 }
58
59 fn kind() -> ToolKind {
60 ToolKind::Move
61 }
62
63 fn initial_title(
64 &self,
65 input: Result<Self::Input, serde_json::Value>,
66 _cx: &mut App,
67 ) -> SharedString {
68 if let Ok(input) = input {
69 let src = MarkdownInlineCode(&input.source_path);
70 let dest = MarkdownInlineCode(&input.destination_path);
71 let src_path = Path::new(&input.source_path);
72 let dest_path = Path::new(&input.destination_path);
73
74 match dest_path
75 .file_name()
76 .and_then(|os_str| os_str.to_os_string().into_string().ok())
77 {
78 Some(filename) if src_path.parent() == dest_path.parent() => {
79 let filename = MarkdownInlineCode(&filename);
80 format!("Rename {src} to {filename}").into()
81 }
82 _ => format!("Move {src} to {dest}").into(),
83 }
84 } else {
85 "Move path".into()
86 }
87 }
88
89 fn run(
90 self: Arc<Self>,
91 input: Self::Input,
92 _event_stream: ToolCallEventStream,
93 cx: &mut App,
94 ) -> Task<Result<Self::Output>> {
95 let rename_task = self.project.update(cx, |project, cx| {
96 match project
97 .find_project_path(&input.source_path, cx)
98 .and_then(|project_path| project.entry_for_path(&project_path, cx))
99 {
100 Some(entity) => match project.find_project_path(&input.destination_path, cx) {
101 Some(project_path) => project.rename_entry(entity.id, project_path, cx),
102 None => Task::ready(Err(anyhow!(
103 "Destination path {} was outside the project.",
104 input.destination_path
105 ))),
106 },
107 None => Task::ready(Err(anyhow!(
108 "Source path {} was not found in the project.",
109 input.source_path
110 ))),
111 }
112 });
113
114 cx.background_spawn(async move {
115 let _ = rename_task.await.with_context(|| {
116 format!("Moving {} to {}", input.source_path, input.destination_path)
117 })?;
118 Ok(format!(
119 "Moved {} to {}",
120 input.source_path, input.destination_path
121 ))
122 })
123 }
124}