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