open_tool.rs

 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::{LanguageModelRequestMessage, LanguageModelToolSchemaFormat};
 6use project::Project;
 7use schemars::JsonSchema;
 8use serde::{Deserialize, Serialize};
 9use std::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, _: &App) -> bool {
27        true
28    }
29
30    fn description(&self) -> String {
31        include_str!("./open_tool/description.md").to_string()
32    }
33
34    fn icon(&self) -> IconName {
35        IconName::ArrowUpRight
36    }
37
38    fn input_schema(&self, format: LanguageModelToolSchemaFormat) -> Result<serde_json::Value> {
39        json_schema_for::<OpenToolInput>(format)
40    }
41
42    fn ui_text(&self, input: &serde_json::Value) -> String {
43        match serde_json::from_value::<OpenToolInput>(input.clone()) {
44            Ok(input) => format!("Open `{}`", MarkdownEscaped(&input.path_or_url)),
45            Err(_) => "Open file or URL".to_string(),
46        }
47    }
48
49    fn run(
50        self: Arc<Self>,
51        input: serde_json::Value,
52        _messages: &[LanguageModelRequestMessage],
53        _project: Entity<Project>,
54        _action_log: Entity<ActionLog>,
55        _window: Option<AnyWindowHandle>,
56        cx: &mut App,
57    ) -> ToolResult {
58        let input: OpenToolInput = match serde_json::from_value(input) {
59            Ok(input) => input,
60            Err(err) => return Task::ready(Err(anyhow!(err))).into(),
61        };
62
63        cx.background_spawn(async move {
64            open::that(&input.path_or_url).context("Failed to open URL or file path")?;
65
66            Ok(format!("Successfully opened {}", input.path_or_url))
67        })
68        .into()
69    }
70}