1use anyhow::{anyhow, Context as _, Result};
2use assistant_tool::{ActionLog, Tool};
3use gpui::{App, AppContext, Entity, Task};
4use language_model::LanguageModelRequestMessage;
5use project::Project;
6use schemars::JsonSchema;
7use serde::{Deserialize, Serialize};
8use std::sync::Arc;
9use ui::IconName;
10use util::markdown::MarkdownString;
11
12#[derive(Debug, Serialize, Deserialize, JsonSchema)]
13pub struct OpenToolInput {
14 /// The path or URL to open with the default application.
15 path_or_url: String,
16}
17
18pub struct OpenTool;
19
20impl Tool for OpenTool {
21 fn name(&self) -> String {
22 "open".to_string()
23 }
24
25 fn needs_confirmation(&self) -> bool {
26 true
27 }
28
29 fn description(&self) -> String {
30 include_str!("./open_tool/description.md").to_string()
31 }
32
33 fn icon(&self) -> IconName {
34 IconName::ExternalLink
35 }
36
37 fn input_schema(&self) -> serde_json::Value {
38 let schema = schemars::schema_for!(OpenToolInput);
39 serde_json::to_value(&schema).unwrap()
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 `{}`", MarkdownString::escape(&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 cx: &mut App,
56 ) -> Task<Result<String>> {
57 let input: OpenToolInput = match serde_json::from_value(input) {
58 Ok(input) => input,
59 Err(err) => return Task::ready(Err(anyhow!(err))),
60 };
61
62 cx.background_spawn(async move {
63 open::that(&input.path_or_url).context("Failed to open URL or file path")?;
64
65 Ok(format!("Successfully opened {}", input.path_or_url))
66 })
67 }
68}