open_tool.rs

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