1use crate::schema::json_schema_for;
2use anyhow::{Context as _, Result, anyhow};
3use assistant_tool::{ActionLog, Tool, ToolResult};
4use gpui::{AnyWindowHandle, App, AppContext, Entity, Task};
5use language_model::{LanguageModel, LanguageModelRequest, LanguageModelToolSchemaFormat};
6use project::Project;
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9use std::{path::PathBuf, sync::Arc};
10use ui::IconName;
11use util::markdown::MarkdownEscaped;
12
13#[derive(Debug, Serialize, Deserialize, JsonSchema)]
14pub struct OpenToolInput {
15 /// The path or URL to open with the default application.
16 path_or_url: String,
17}
18
19pub struct OpenTool;
20
21impl Tool for OpenTool {
22 fn name(&self) -> String {
23 "open".to_string()
24 }
25
26 fn needs_confirmation(&self, _: &serde_json::Value, _: &Entity<Project>, _: &App) -> bool {
27 true
28 }
29 fn may_perform_edits(&self) -> bool {
30 false
31 }
32 fn description(&self) -> String {
33 include_str!("./open_tool/description.md").to_string()
34 }
35
36 fn icon(&self) -> IconName {
37 IconName::ArrowUpRight
38 }
39
40 fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
41 json_schema_for::<OpenToolInput>(format)
42 }
43
44 fn ui_text(&self, input: &serde_json::Value) -> String {
45 match serde_json::from_value::<OpenToolInput>(input.clone()) {
46 Ok(input) => format!("Open `{}`", MarkdownEscaped(&input.path_or_url)),
47 Err(_) => "Open file or URL".to_string(),
48 }
49 }
50
51 fn run(
52 self: Arc<Self>,
53 input: serde_json::Value,
54 _request: Arc<LanguageModelRequest>,
55 project: Entity<Project>,
56 _action_log: Entity<ActionLog>,
57 _model: Arc<dyn LanguageModel>,
58 _window: Option<AnyWindowHandle>,
59 cx: &mut App,
60 ) -> ToolResult {
61 let input: OpenToolInput = match serde_json::from_value(input) {
62 Ok(input) => input,
63 Err(err) => return Task::ready(Err(anyhow!(err))).into(),
64 };
65
66 // If path_or_url turns out to be a path in the project, make it absolute.
67 let abs_path = to_absolute_path(&input.path_or_url, project, cx);
68
69 cx.background_spawn(async move {
70 match abs_path {
71 Some(path) => open::that(path),
72 None => open::that(&input.path_or_url),
73 }
74 .context("Failed to open URL or file path")?;
75
76 Ok(format!("Successfully opened {}", input.path_or_url).into())
77 })
78 .into()
79 }
80}
81
82fn to_absolute_path(
83 potential_path: &str,
84 project: Entity<Project>,
85 cx: &mut App,
86) -> Option<PathBuf> {
87 let project = project.read(cx);
88 project
89 .find_project_path(PathBuf::from(potential_path), cx)
90 .and_then(|project_path| project.absolute_path(&project_path, cx))
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96 use gpui::TestAppContext;
97 use project::{FakeFs, Project};
98 use settings::SettingsStore;
99 use std::path::Path;
100 use tempfile::TempDir;
101
102 #[gpui::test]
103 async fn test_to_absolute_path(cx: &mut TestAppContext) {
104 init_test(cx);
105 let temp_dir = TempDir::new().expect("Failed to create temp directory");
106 let temp_path = temp_dir.path().to_string_lossy().to_string();
107
108 let fs = FakeFs::new(cx.executor());
109 fs.insert_tree(
110 &temp_path,
111 serde_json::json!({
112 "src": {
113 "main.rs": "fn main() {}",
114 "lib.rs": "pub fn lib_fn() {}"
115 },
116 "docs": {
117 "readme.md": "# Project Documentation"
118 }
119 }),
120 )
121 .await;
122
123 // Use the temp_path as the root directory, not just its filename
124 let project = Project::test(fs.clone(), [temp_dir.path()], cx).await;
125
126 // Test cases where the function should return Some
127 cx.update(|cx| {
128 // Project-relative paths should return Some
129 // Create paths using the last segment of the temp path to simulate a project-relative path
130 let root_dir_name = Path::new(&temp_path)
131 .file_name()
132 .unwrap_or_else(|| std::ffi::OsStr::new("temp"))
133 .to_string_lossy();
134
135 assert!(
136 to_absolute_path(&format!("{root_dir_name}/src/main.rs"), project.clone(), cx)
137 .is_some(),
138 "Failed to resolve main.rs path"
139 );
140
141 assert!(
142 to_absolute_path(
143 &format!("{root_dir_name}/docs/readme.md",),
144 project.clone(),
145 cx,
146 )
147 .is_some(),
148 "Failed to resolve readme.md path"
149 );
150
151 // External URL should return None
152 let result = to_absolute_path("https://example.com", project.clone(), cx);
153 assert_eq!(result, None, "External URLs should return None");
154
155 // Path outside project
156 let result = to_absolute_path("../invalid/path", project.clone(), cx);
157 assert_eq!(result, None, "Paths outside the project should return None");
158 });
159 }
160
161 fn init_test(cx: &mut TestAppContext) {
162 cx.update(|cx| {
163 let settings_store = SettingsStore::test(cx);
164 cx.set_global(settings_store);
165 language::init(cx);
166 Project::init_settings(cx);
167 });
168 }
169}