1use anyhow::{anyhow, Result};
2use assistant_tool::{ActionLog, Tool};
3use gpui::{App, AppContext, Entity, Task};
4use language_model::LanguageModelRequestMessage;
5use project::Project;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9use ui::IconName;
10
11#[derive(Debug, Serialize, Deserialize, JsonSchema)]
12pub struct CopyPathToolInput {
13 /// The source path of the file or directory to copy.
14 /// If a directory is specified, its contents will be copied recursively (like `cp -r`).
15 ///
16 /// <example>
17 /// If the project has the following files:
18 ///
19 /// - directory1/a/something.txt
20 /// - directory2/a/things.txt
21 /// - directory3/a/other.txt
22 ///
23 /// You can copy the first file by providing a source_path of "directory1/a/something.txt"
24 /// </example>
25 pub source_path: String,
26
27 /// The destination path where the file or directory should be copied to.
28 ///
29 /// <example>
30 /// To copy "directory1/a/something.txt" to "directory2/b/copy.txt",
31 /// provide a destination_path of "directory2/b/copy.txt"
32 /// </example>
33 pub destination_path: String,
34}
35
36pub struct CopyPathTool;
37
38impl Tool for CopyPathTool {
39 fn name(&self) -> String {
40 "copy-path".into()
41 }
42
43 fn needs_confirmation(&self) -> bool {
44 true
45 }
46
47 fn description(&self) -> String {
48 include_str!("./copy_path_tool/description.md").into()
49 }
50
51 fn icon(&self) -> IconName {
52 IconName::Clipboard
53 }
54
55 fn input_schema(&self) -> serde_json::Value {
56 let schema = schemars::schema_for!(CopyPathToolInput);
57 serde_json::to_value(&schema).unwrap()
58 }
59
60 fn ui_text(&self, input: &serde_json::Value) -> String {
61 match serde_json::from_value::<CopyPathToolInput>(input.clone()) {
62 Ok(input) => {
63 let src = input.source_path.as_str();
64 let dest = input.destination_path.as_str();
65 format!("Copy `{src}` to `{dest}`")
66 }
67 Err(_) => "Copy path".to_string(),
68 }
69 }
70
71 fn run(
72 self: Arc<Self>,
73 input: serde_json::Value,
74 _messages: &[LanguageModelRequestMessage],
75 project: Entity<Project>,
76 _action_log: Entity<ActionLog>,
77 cx: &mut App,
78 ) -> Task<Result<String>> {
79 let input = match serde_json::from_value::<CopyPathToolInput>(input) {
80 Ok(input) => input,
81 Err(err) => return Task::ready(Err(anyhow!(err))),
82 };
83 let copy_task = project.update(cx, |project, cx| {
84 match project
85 .find_project_path(&input.source_path, cx)
86 .and_then(|project_path| project.entry_for_path(&project_path, cx))
87 {
88 Some(entity) => match project.find_project_path(&input.destination_path, cx) {
89 Some(project_path) => {
90 project.copy_entry(entity.id, None, project_path.path, cx)
91 }
92 None => Task::ready(Err(anyhow!(
93 "Destination path {} was outside the project.",
94 input.destination_path
95 ))),
96 },
97 None => Task::ready(Err(anyhow!(
98 "Source path {} was not found in the project.",
99 input.source_path
100 ))),
101 }
102 });
103
104 cx.background_spawn(async move {
105 match copy_task.await {
106 Ok(_) => Ok(format!(
107 "Copied {} to {}",
108 input.source_path, input.destination_path
109 )),
110 Err(err) => Err(anyhow!(
111 "Failed to copy {} to {}: {}",
112 input.source_path,
113 input.destination_path,
114 err
115 )),
116 }
117 })
118 }
119}