diff --git a/Cargo.lock b/Cargo.lock index c8f06c8e7..27dfd5fef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1673,6 +1673,19 @@ dependencies = [ "zip 4.6.1", ] +[[package]] +name = "dbx-cli" +version = "0.5.2" +dependencies = [ + "chrono", + "dbx-core", + "reqwest 0.12.28", + "serde", + "serde_json", + "tokio", + "uuid", +] + [[package]] name = "dbx-core" version = "0.1.0" diff --git a/crates/dbx-cli/src/commands.rs b/crates/dbx-cli/src/commands.rs new file mode 100644 index 000000000..60d828322 --- /dev/null +++ b/crates/dbx-cli/src/commands.rs @@ -0,0 +1,61 @@ +use serde::Serialize; + +#[derive(Debug, Serialize)] +#[serde(rename_all = "SCREAMING_SNAKE_CASE")] +pub(crate) enum CliErrorCode { + GuiRuntimeRequired, +} + +#[derive(Debug, Serialize)] +#[serde(rename_all = "kebab-case")] +pub(crate) enum CliSource { + GuiRuntime, + Headless, +} + +#[derive(Debug, Serialize)] +pub(crate) struct CliError { + code: CliErrorCode, + message: String, + recoverable: bool, +} + +#[derive(Debug, Serialize)] +#[serde(untagged)] +pub(crate) enum CliEnvelope { + Success { ok: bool, source: CliSource, data: T }, + Failure { ok: bool, source: CliSource, error: CliError }, +} + +pub(crate) async fn run(args: Vec) -> Result<(), CliEnvelope<()>> { + let output = match args.as_slice() { + [cmd, ..] if cmd == "context" => context().await, + [cmd, ..] if cmd == "selection" => runtime_required("dbx selection requires DBX GUI runtime."), + [cmd, sub, ..] if cmd == "result" && sub == "current" => { + runtime_required("dbx result current requires DBX GUI runtime.") + } + _ => ok(CliSource::Headless, serde_json::json!({ "runtime": "headless" })), + }; + + println!("{}", serde_json::to_string_pretty(&output).unwrap()); + Ok(()) +} + +async fn context() -> CliEnvelope { + match crate::runtime_client::get_json("/context").await { + Ok(data) => ok(CliSource::GuiRuntime, data), + Err(_) => ok(CliSource::Headless, serde_json::json!({ "runtime": "headless" })), + } +} + +fn ok(source: CliSource, data: T) -> CliEnvelope { + CliEnvelope::Success { ok: true, source, data } +} + +fn runtime_required(message: &str) -> CliEnvelope { + CliEnvelope::Failure { + ok: false, + source: CliSource::Headless, + error: CliError { code: CliErrorCode::GuiRuntimeRequired, message: message.to_string(), recoverable: true }, + } +} diff --git a/crates/dbx-cli/src/runtime_client.rs b/crates/dbx-cli/src/runtime_client.rs new file mode 100644 index 000000000..a3db99811 --- /dev/null +++ b/crates/dbx-cli/src/runtime_client.rs @@ -0,0 +1,42 @@ +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct RuntimeDiscovery { + pub port: u16, + pub token: String, +} + +pub fn app_data_dir() -> PathBuf { + let home = std::env::var(if cfg!(windows) { "APPDATA" } else { "HOME" }).unwrap_or_else(|_| ".".to_string()); + + if cfg!(target_os = "macos") { + PathBuf::from(home).join("Library/Application Support/com.dbx.app") + } else if cfg!(windows) { + PathBuf::from(home).join("com.dbx.app") + } else { + PathBuf::from(home).join(".config/com.dbx.app") + } +} + +pub fn load_runtime() -> Option { + let path = app_data_dir().join("agent-runtime.json"); + let json = std::fs::read_to_string(path).ok()?; + serde_json::from_str(&json).ok() +} + +pub async fn get_json(path: &str) -> Result { + let runtime = load_runtime().ok_or_else(|| "runtime unavailable".to_string())?; + let url = format!("http://127.0.0.1:{}{}", runtime.port, path); + + reqwest::Client::new() + .get(url) + .bearer_auth(runtime.token) + .send() + .await + .map_err(|err| err.to_string())? + .json() + .await + .map_err(|err| err.to_string()) +}