1use crate::{AgentTool, ToolCallEventStream};
2use agent_client_protocol::ToolKind;
3use anyhow::{Context as _, Result, anyhow};
4use gpui::{App, AppContext, Entity, Task};
5use project::Project;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9use util::markdown::MarkdownInlineCode;
10
11/// Copies a file or directory in the project, and returns confirmation that the copy succeeded.
12/// Directory contents will be copied recursively.
13///
14/// This tool should be used when it's desirable to create a copy of a file or directory without modifying the original.
15/// It's much more efficient than doing this by separately reading and then writing the file or directory's contents, so this tool should be preferred over that approach whenever copying is the goal.
16#[derive(Debug, Serialize, Deserialize, JsonSchema)]
17pub struct CopyPathToolInput {
18 /// The source path of the file or directory to copy.
19 /// If a directory is specified, its contents will be copied recursively.
20 ///
21 /// <example>
22 /// If the project has the following files:
23 ///
24 /// - directory1/a/something.txt
25 /// - directory2/a/things.txt
26 /// - directory3/a/other.txt
27 ///
28 /// You can copy the first file by providing a source_path of "directory1/a/something.txt"
29 /// </example>
30 pub source_path: String,
31 /// The destination path where the file or directory should be copied to.
32 ///
33 /// <example>
34 /// To copy "directory1/a/something.txt" to "directory2/b/copy.txt", provide a destination_path of "directory2/b/copy.txt"
35 /// </example>
36 pub destination_path: String,
37}
38
39pub struct CopyPathTool {
40 project: Entity<Project>,
41}
42
43impl CopyPathTool {
44 pub fn new(project: Entity<Project>) -> Self {
45 Self { project }
46 }
47}
48
49impl AgentTool for CopyPathTool {
50 type Input = CopyPathToolInput;
51 type Output = String;
52
53 fn name() -> &'static str {
54 "copy_path"
55 }
56
57 fn kind() -> ToolKind {
58 ToolKind::Move
59 }
60
61 fn initial_title(
62 &self,
63 input: Result<Self::Input, serde_json::Value>,
64 _cx: &mut App,
65 ) -> ui::SharedString {
66 if let Ok(input) = input {
67 let src = MarkdownInlineCode(&input.source_path);
68 let dest = MarkdownInlineCode(&input.destination_path);
69 format!("Copy {src} to {dest}").into()
70 } else {
71 "Copy path".into()
72 }
73 }
74
75 fn run(
76 self: Arc<Self>,
77 input: Self::Input,
78 _event_stream: ToolCallEventStream,
79 cx: &mut App,
80 ) -> Task<Result<Self::Output>> {
81 let copy_task = self.project.update(cx, |project, cx| {
82 match project
83 .find_project_path(&input.source_path, cx)
84 .and_then(|project_path| project.entry_for_path(&project_path, cx))
85 {
86 Some(entity) => match project.find_project_path(&input.destination_path, cx) {
87 Some(project_path) => {
88 project.copy_entry(entity.id, None, project_path.path, cx)
89 }
90 None => Task::ready(Err(anyhow!(
91 "Destination path {} was outside the project.",
92 input.destination_path
93 ))),
94 },
95 None => Task::ready(Err(anyhow!(
96 "Source path {} was not found in the project.",
97 input.source_path
98 ))),
99 }
100 });
101
102 cx.background_spawn(async move {
103 let _ = copy_task.await.with_context(|| {
104 format!(
105 "Copying {} to {}",
106 input.source_path, input.destination_path
107 )
108 })?;
109 Ok(format!(
110 "Copied {} to {}",
111 input.source_path, input.destination_path
112 ))
113 })
114 }
115}