open_tool.rs

  1use crate::AgentTool;
  2use agent_client_protocol::ToolKind;
  3use anyhow::{Context as _, Result};
  4use gpui::{App, AppContext, Entity, SharedString, Task};
  5use project::Project;
  6use schemars::JsonSchema;
  7use serde::{Deserialize, Serialize};
  8use std::{path::PathBuf, sync::Arc};
  9use util::markdown::MarkdownEscaped;
 10
 11/// This tool opens a file or URL with the default application associated with it on the user's operating system:
 12///
 13/// - On macOS, it's equivalent to the `open` command
 14/// - On Windows, it's equivalent to `start`
 15/// - On Linux, it uses something like `xdg-open`, `gio open`, `gnome-open`, `kde-open`, `wslview` as appropriate
 16///
 17/// For example, it can open a web browser with a URL, open a PDF file with the default PDF viewer, etc.
 18///
 19/// You MUST ONLY use this tool when the user has explicitly requested opening something. You MUST NEVER assume that the user would like for you to use this tool.
 20#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)]
 21pub struct OpenToolInput {
 22    /// The path or URL to open with the default application.
 23    path_or_url: String,
 24}
 25
 26pub struct OpenTool {
 27    project: Entity<Project>,
 28}
 29
 30impl OpenTool {
 31    pub fn new(project: Entity<Project>) -> Self {
 32        Self { project }
 33    }
 34}
 35
 36impl AgentTool for OpenTool {
 37    type Input = OpenToolInput;
 38    type Output = String;
 39
 40    fn name() -> &'static str {
 41        "open"
 42    }
 43
 44    fn kind() -> ToolKind {
 45        ToolKind::Execute
 46    }
 47
 48    fn initial_title(
 49        &self,
 50        input: Result<Self::Input, serde_json::Value>,
 51        _cx: &mut App,
 52    ) -> SharedString {
 53        if let Ok(input) = input {
 54            format!("Open `{}`", MarkdownEscaped(&input.path_or_url)).into()
 55        } else {
 56            "Open file or URL".into()
 57        }
 58    }
 59
 60    fn run(
 61        self: Arc<Self>,
 62        input: Self::Input,
 63        event_stream: crate::ToolCallEventStream,
 64        cx: &mut App,
 65    ) -> Task<Result<Self::Output>> {
 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, self.project.clone(), cx);
 68        let authorize = event_stream.authorize(self.initial_title(Ok(input.clone()), cx), cx);
 69        cx.background_spawn(async move {
 70            authorize.await?;
 71
 72            match abs_path {
 73                Some(path) => open::that(path),
 74                None => open::that(&input.path_or_url),
 75            }
 76            .context("Failed to open URL or file path")?;
 77
 78            Ok(format!("Successfully opened {}", input.path_or_url))
 79        })
 80    }
 81}
 82
 83fn to_absolute_path(
 84    potential_path: &str,
 85    project: Entity<Project>,
 86    cx: &mut App,
 87) -> Option<PathBuf> {
 88    let project = project.read(cx);
 89    project
 90        .find_project_path(PathBuf::from(potential_path), cx)
 91        .and_then(|project_path| project.absolute_path(&project_path, cx))
 92}
 93
 94#[cfg(test)]
 95mod tests {
 96    use super::*;
 97    use gpui::TestAppContext;
 98    use project::{FakeFs, Project};
 99    use settings::SettingsStore;
100    use std::path::Path;
101    use tempfile::TempDir;
102
103    #[gpui::test]
104    async fn test_to_absolute_path(cx: &mut TestAppContext) {
105        init_test(cx);
106        let temp_dir = TempDir::new().expect("Failed to create temp directory");
107        let temp_path = temp_dir.path().to_string_lossy().into_owned();
108
109        let fs = FakeFs::new(cx.executor());
110        fs.insert_tree(
111            &temp_path,
112            serde_json::json!({
113                "src": {
114                    "main.rs": "fn main() {}",
115                    "lib.rs": "pub fn lib_fn() {}"
116                },
117                "docs": {
118                    "readme.md": "# Project Documentation"
119                }
120            }),
121        )
122        .await;
123
124        // Use the temp_path as the root directory, not just its filename
125        let project = Project::test(fs.clone(), [temp_dir.path()], cx).await;
126
127        // Test cases where the function should return Some
128        cx.update(|cx| {
129            // Project-relative paths should return Some
130            // Create paths using the last segment of the temp path to simulate a project-relative path
131            let root_dir_name = Path::new(&temp_path)
132                .file_name()
133                .unwrap_or_else(|| std::ffi::OsStr::new("temp"))
134                .to_string_lossy();
135
136            assert!(
137                to_absolute_path(&format!("{root_dir_name}/src/main.rs"), project.clone(), cx)
138                    .is_some(),
139                "Failed to resolve main.rs path"
140            );
141
142            assert!(
143                to_absolute_path(
144                    &format!("{root_dir_name}/docs/readme.md",),
145                    project.clone(),
146                    cx,
147                )
148                .is_some(),
149                "Failed to resolve readme.md path"
150            );
151
152            // External URL should return None
153            let result = to_absolute_path("https://example.com", project.clone(), cx);
154            assert_eq!(result, None, "External URLs should return None");
155
156            // Path outside project
157            let result = to_absolute_path("../invalid/path", project.clone(), cx);
158            assert_eq!(result, None, "Paths outside the project should return None");
159        });
160    }
161
162    fn init_test(cx: &mut TestAppContext) {
163        cx.update(|cx| {
164            let settings_store = SettingsStore::test(cx);
165            cx.set_global(settings_store);
166            language::init(cx);
167            Project::init_settings(cx);
168        });
169    }
170}