feat(plugin): add plugin framework workflow
This commit is contained in:
247 files changed
+33841
-2256
No files matched your search
+115
@@ -0,0 +1,115 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "dbx-example-hello"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"dbx-plugin-sdk",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "dbx-plugin-sdk"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.47"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba"
|
||||
dependencies = [
|
||||
"serde_core",
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_core"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.229"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.151"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
"serde_core",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "3.0.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53e9bae58849f64dfa4f5d5ae372c8341f7305f82a3868709269343628b659a3"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"
|
||||
|
||||
[[package]]
|
||||
name = "zmij"
|
||||
version = "1.0.23"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b"
|
||||
@@ -0,0 +1,16 @@
|
||||
[package]
|
||||
name = "dbx-example-hello"
|
||||
version = "1.0.0"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
|
||||
[workspace]
|
||||
|
||||
[dependencies]
|
||||
dbx-plugin-sdk = { path = "../../../sdk/rust/dbx-plugin-sdk" }
|
||||
serde_json = "1"
|
||||
|
||||
[profile.release]
|
||||
strip = true
|
||||
lto = true
|
||||
codegen-units = 1
|
||||
@@ -0,0 +1,195 @@
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Mutex;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
use dbx_plugin_sdk::{PluginEmitter, PluginError, PluginHandler, PluginMetadata, PluginServer, RequestContext};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
#[derive(Default)]
|
||||
struct HelloPlugin {
|
||||
connections: Mutex<HashMap<String, String>>,
|
||||
}
|
||||
|
||||
impl PluginHandler for HelloPlugin {
|
||||
fn handle(
|
||||
&self,
|
||||
_context: RequestContext,
|
||||
method: &str,
|
||||
params: Value,
|
||||
emitter: &PluginEmitter,
|
||||
) -> Result<Value, PluginError> {
|
||||
match method {
|
||||
"connection/test" => {
|
||||
let connection = params.get("connection").cloned().unwrap_or_default();
|
||||
Ok(json!({
|
||||
"success": true,
|
||||
"message": format!(
|
||||
"Provider is ready for {}:{}.",
|
||||
connection.get("host").and_then(Value::as_str).unwrap_or("localhost"),
|
||||
connection.get("port").and_then(Value::as_u64).unwrap_or(0)
|
||||
)
|
||||
}))
|
||||
}
|
||||
"connection/connect" => {
|
||||
let connection =
|
||||
params.get("connection").ok_or_else(|| PluginError::new(-32602, "Missing connection"))?;
|
||||
let connection_id = connection
|
||||
.get("id")
|
||||
.and_then(Value::as_str)
|
||||
.ok_or_else(|| PluginError::new(-32602, "Missing connection id"))?;
|
||||
let connection_name = connection.get("name").and_then(Value::as_str).unwrap_or("Hello connection");
|
||||
self.connections
|
||||
.lock()
|
||||
.map_err(|_| PluginError::new(-32000, "Connection registry is poisoned"))?
|
||||
.insert(connection_id.to_string(), connection_name.to_string());
|
||||
emitter
|
||||
.event("hello/connectionChanged", json!({ "connectionId": connection_id, "state": "connected" }))?;
|
||||
Ok(json!({ "success": true }))
|
||||
}
|
||||
"connection/disconnect" => {
|
||||
let connection_id = params
|
||||
.get("connection")
|
||||
.and_then(|connection| connection.get("id"))
|
||||
.and_then(Value::as_str)
|
||||
.ok_or_else(|| PluginError::new(-32602, "Missing connection id"))?;
|
||||
self.connections
|
||||
.lock()
|
||||
.map_err(|_| PluginError::new(-32000, "Connection registry is poisoned"))?
|
||||
.remove(connection_id);
|
||||
emitter.event(
|
||||
"hello/connectionChanged",
|
||||
json!({ "connectionId": connection_id, "state": "disconnected" }),
|
||||
)?;
|
||||
Ok(json!({ "success": true }))
|
||||
}
|
||||
"connection/action" => {
|
||||
match params.get("action").and_then(|action| action.get("id")).and_then(Value::as_str) {
|
||||
Some("suggest-greeting") => Ok(json!({
|
||||
"success": true,
|
||||
"message": "Greeting updated by the plugin action.",
|
||||
"fieldValues": {
|
||||
"greeting": "Hello from plugin action"
|
||||
}
|
||||
})),
|
||||
Some(action_id) => Err(PluginError::new(-32602, format!("Unknown connection action: {action_id}"))),
|
||||
None => Err(PluginError::new(-32602, "Missing connection action id")),
|
||||
}
|
||||
}
|
||||
"hello/greet" => {
|
||||
let connection_id = params.get("connectionId").and_then(Value::as_str).unwrap_or_default();
|
||||
let connection_name = self
|
||||
.connections
|
||||
.lock()
|
||||
.map_err(|_| PluginError::new(-32000, "Connection registry is poisoned"))?
|
||||
.get(connection_id)
|
||||
.cloned()
|
||||
.ok_or_else(|| PluginError::new(-32010, "Open a saved Hello connection first"))?;
|
||||
let name = params.get("name").and_then(Value::as_str).unwrap_or("DBX");
|
||||
emitter.event(
|
||||
"hello/progress",
|
||||
json!({ "stage": "started", "name": name, "connectionId": connection_id }),
|
||||
)?;
|
||||
thread::sleep(Duration::from_millis(120));
|
||||
emitter.event(
|
||||
"hello/progress",
|
||||
json!({ "stage": "finished", "name": name, "connectionId": connection_id }),
|
||||
)?;
|
||||
Ok(json!({
|
||||
"message": format!("Hello, {name}, from {connection_name}!"),
|
||||
"connectionId": connection_id,
|
||||
"pluginId": std::env::var("DBX_PLUGIN_ID").unwrap_or_default(),
|
||||
"dbxVersion": std::env::var("DBX_APP_VERSION").unwrap_or_default()
|
||||
}))
|
||||
}
|
||||
"filesystem/list" => {
|
||||
require_filesystem_provider(¶ms)?;
|
||||
let uri = params.get("uri").and_then(Value::as_str).unwrap_or("hello:/");
|
||||
let entries = match uri {
|
||||
"hello:/" => json!([
|
||||
{
|
||||
"name": "examples",
|
||||
"uri": "hello:/examples/",
|
||||
"kind": "directory",
|
||||
"modifiedAt": "2026-07-28T00:00:00Z"
|
||||
},
|
||||
{
|
||||
"name": "README.txt",
|
||||
"uri": "hello:/README.txt",
|
||||
"kind": "file",
|
||||
"size": 78,
|
||||
"modifiedAt": "2026-07-28T00:00:00Z",
|
||||
"contentType": "text/plain"
|
||||
}
|
||||
]),
|
||||
"hello:/examples/" => json!([
|
||||
{
|
||||
"name": "hello.json",
|
||||
"uri": "hello:/examples/hello.json",
|
||||
"kind": "file",
|
||||
"size": 55,
|
||||
"modifiedAt": "2026-07-28T00:00:00Z",
|
||||
"contentType": "application/json"
|
||||
}
|
||||
]),
|
||||
_ => return Err(PluginError::new(-32044, format!("Directory not found: {uri}"))),
|
||||
};
|
||||
Ok(json!({ "entries": entries }))
|
||||
}
|
||||
"filesystem/read" => {
|
||||
require_filesystem_provider(¶ms)?;
|
||||
let uri = params.get("uri").and_then(Value::as_str).unwrap_or_default();
|
||||
let content = match uri {
|
||||
"hello:/README.txt" => "This virtual file is rendered by DBX's host-owned plugin file manager.\n",
|
||||
"hello:/examples/hello.json" => "{\n \"hello\": \"DBX\",\n \"provider\": \"filesystem\"\n}\n",
|
||||
_ => return Err(PluginError::new(-32044, format!("File not found: {uri}"))),
|
||||
};
|
||||
let max_bytes = params.get("maxBytes").and_then(Value::as_u64).unwrap_or(content.len() as u64);
|
||||
let preview_length = usize::try_from(max_bytes).unwrap_or(usize::MAX).min(content.len());
|
||||
let preview = &content.as_bytes()[..preview_length];
|
||||
Ok(json!({
|
||||
"dataBase64": encode_base64(preview),
|
||||
"contentType": if uri.ends_with(".json") { "application/json" } else { "text/plain" },
|
||||
"truncated": preview_length < content.len(),
|
||||
"etag": format!("hello-{preview_length}")
|
||||
}))
|
||||
}
|
||||
_ => Err(PluginError::method_not_found(method)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn require_filesystem_provider(params: &Value) -> Result<(), PluginError> {
|
||||
if params.get("providerId").and_then(Value::as_str) == Some("dbx.example.hello.files") {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(PluginError::new(-32602, "Unknown filesystem provider"))
|
||||
}
|
||||
}
|
||||
|
||||
fn encode_base64(bytes: &[u8]) -> String {
|
||||
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
let mut encoded = String::with_capacity(bytes.len().div_ceil(3) * 4);
|
||||
for chunk in bytes.chunks(3) {
|
||||
let first = chunk[0];
|
||||
let second = chunk.get(1).copied().unwrap_or(0);
|
||||
let third = chunk.get(2).copied().unwrap_or(0);
|
||||
encoded.push(ALPHABET[(first >> 2) as usize] as char);
|
||||
encoded.push(ALPHABET[(((first & 0x03) << 4) | (second >> 4)) as usize] as char);
|
||||
encoded.push(if chunk.len() > 1 {
|
||||
ALPHABET[(((second & 0x0f) << 2) | (third >> 6)) as usize] as char
|
||||
} else {
|
||||
'='
|
||||
});
|
||||
encoded.push(if chunk.len() > 2 { ALPHABET[(third & 0x3f) as usize] as char } else { '=' });
|
||||
}
|
||||
encoded
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
let metadata = PluginMetadata::new("dbx.example.hello", env!("CARGO_PKG_VERSION"))
|
||||
.with_capability("connections")
|
||||
.with_capability("events")
|
||||
.with_capability("filesystem");
|
||||
PluginServer::new(metadata, HelloPlugin::default()).serve()
|
||||
}
|
||||
Reference in new issue
Block a user