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(&self, input: Result<Self::Input, serde_json::Value>) -> SharedString {
64 if let Ok(input) = input {
65 let src = MarkdownInlineCode(&input.source_path);
66 let dest = MarkdownInlineCode(&input.destination_path);
67 let src_path = Path::new(&input.source_path);
68 let dest_path = Path::new(&input.destination_path);
69
70 match dest_path
71 .file_name()
72 .and_then(|os_str| os_str.to_os_string().into_string().ok())
73 {
74 Some(filename) if src_path.parent() == dest_path.parent() => {
75 let filename = MarkdownInlineCode(&filename);
76 format!("Rename {src} to {filename}").into()
77 }
78 _ => format!("Move {src} to {dest}").into(),
79 }
80 } else {
81 "Move path".into()
82 }
83 }
84
85 fn run(
86 self: Arc<Self>,
87 input: Self::Input,
88 _event_stream: ToolCallEventStream,
89 cx: &mut App,
90 ) -> Task<Result<Self::Output>> {
91 let rename_task = self.project.update(cx, |project, cx| {
92 match project
93 .find_project_path(&input.source_path, cx)
94 .and_then(|project_path| project.entry_for_path(&project_path, cx))
95 {
96 Some(entity) => match project.find_project_path(&input.destination_path, cx) {
97 Some(project_path) => project.rename_entry(entity.id, project_path.path, cx),
98 None => Task::ready(Err(anyhow!(
99 "Destination path {} was outside the project.",
100 input.destination_path
101 ))),
102 },
103 None => Task::ready(Err(anyhow!(
104 "Source path {} was not found in the project.",
105 input.source_path
106 ))),
107 }
108 });
109
110 cx.background_spawn(async move {
111 let _ = rename_task.await.with_context(|| {
112 format!("Moving {} to {}", input.source_path, input.destination_path)
113 })?;
114 Ok(format!(
115 "Moved {} to {}",
116 input.source_path, input.destination_path
117 ))
118 })
119 }
120}