diff --git a/crates/amalthea/src/comm/connections_comm.rs b/crates/amalthea/src/comm/connections_comm.rs index cd5d29828..9864c4861 100644 --- a/crates/amalthea/src/comm/connections_comm.rs +++ b/crates/amalthea/src/comm/connections_comm.rs @@ -31,6 +31,26 @@ pub struct FieldSchema { pub dtype: String } +/// MetadataSchema in Schemas +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct MetadataSchema { + /// Connection name + pub name: String, + + /// Language ID for the connections. Essentially just R or python + pub language_id: String, + + /// Connection host + pub host: Option, + + /// Connection type + #[serde(rename = "type")] + pub metadata_schema_type: Option, + + /// Code used to re-create the connection + pub code: Option +} + /// Parameters for the ListObjects method. #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct ListObjectsParams { @@ -66,6 +86,13 @@ pub struct PreviewObjectParams { pub path: Vec, } +/// Parameters for the GetMetadata method. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct GetMetadataParams { + /// The comm_id of the client we want to retrieve metdata for. + pub comm_id: String, +} + /** * Backend RPC request types for the connections comm */ @@ -103,6 +130,12 @@ pub enum ConnectionsBackendRequest { #[serde(rename = "preview_object")] PreviewObject(PreviewObjectParams), + /// Gets metadata from the connections + /// + /// A connection has tied metadata such as an icon, the host, etc. + #[serde(rename = "get_metadata")] + GetMetadata(GetMetadataParams), + } /** @@ -125,6 +158,8 @@ pub enum ConnectionsBackendReply { PreviewObjectReply(), + GetMetadataReply(MetadataSchema), + } /** diff --git a/crates/ark/src/connections/r_connection.rs b/crates/ark/src/connections/r_connection.rs index b859811dc..d8a2096be 100644 --- a/crates/ark/src/connections/r_connection.rs +++ b/crates/ark/src/connections/r_connection.rs @@ -4,6 +4,8 @@ // Copyright (C) 2023 by Posit Software, PBC // +use std::collections::HashMap; + use amalthea::comm::comm_channel::CommMsg; use amalthea::comm::connections_comm::ConnectionsBackendReply; use amalthea::comm::connections_comm::ConnectionsBackendRequest; @@ -11,13 +13,16 @@ use amalthea::comm::connections_comm::ConnectionsFrontendEvent; use amalthea::comm::connections_comm::ContainsDataParams; use amalthea::comm::connections_comm::FieldSchema; use amalthea::comm::connections_comm::GetIconParams; +use amalthea::comm::connections_comm::GetMetadataParams; use amalthea::comm::connections_comm::ListFieldsParams; use amalthea::comm::connections_comm::ListObjectsParams; +use amalthea::comm::connections_comm::MetadataSchema; use amalthea::comm::connections_comm::ObjectSchema; use amalthea::comm::connections_comm::PreviewObjectParams; use amalthea::comm::event::CommManagerEvent; use amalthea::socket::comm::CommInitiator; use amalthea::socket::comm::CommSocket; +use anyhow::anyhow; use crossbeam::channel::Sender; use harp::exec::RFunction; use harp::exec::RFunctionExt; @@ -32,6 +37,7 @@ use stdext::unwrap; use uuid::Uuid; use crate::interface::RMain; +use crate::modules::ARK_ENVS; use crate::r_task; #[derive(Deserialize, Serialize, Clone)] @@ -216,6 +222,29 @@ impl RConnection { })?; Ok(ConnectionsBackendReply::ContainsDataReply(contains_data)) }, + ConnectionsBackendRequest::GetMetadata(GetMetadataParams { comm_id }) => { + let metadata = r_task(|| -> Result<_, anyhow::Error> { + let r_metadata: HashMap = + RFunction::new("", ".ps.connection_metadata") + .add(comm_id) + .call_in(ARK_ENVS.positron_ns)? + .try_into()?; + + let schema = MetadataSchema { + language_id: String::from("r"), + name: r_metadata + .get("displayName") + .ok_or(anyhow!("Need a display name"))? + .clone(), + host: r_metadata.get("host").cloned(), + metadata_schema_type: r_metadata.get("type").cloned(), + code: r_metadata.get("connectionCode").cloned(), + }; + + Ok(schema) + })?; + Ok(ConnectionsBackendReply::GetMetadataReply(metadata)) + }, } } diff --git a/crates/ark/src/modules/positron/connection.R b/crates/ark/src/modules/positron/connection.R index 4343544f4..9e95cef4f 100644 --- a/crates/ark/src/modules/positron/connection.R +++ b/crates/ark/src/modules/positron/connection.R @@ -188,6 +188,21 @@ connection_flatten_object_types <- function(object_tree) { identical(object_types$contains, "data") } +.ps.connection_metadata <- function(id) { + con <- get(id, getOption("connectionObserver")$.connections) + + if (is.null(con)) { + stop("Expected a valid comm_id") + } + + list( + type = con$type, + host = con$host, + displayName = con$displayName, + connectCode = con$connectCode + ) +} + .ps.register_dummy_connection <- function() { # This is used for testing the connections service observer <- getOption("connectionObserver")