From 01110945a1cb7a6f0d94007b56e69c8d459bd091 Mon Sep 17 00:00:00 2001 From: Illuminated2020 <2357303264@qq.com> Date: Sun, 10 May 2026 15:56:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(dbx-cli):=20=E8=A1=A5=E9=BD=90=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=20runtime=20client=20=E6=A8=A1=E5=9D=97=E4=BB=A5?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 commands 和 runtime_client 最小实现,补足 main.rs 所需模块 - 允许 context 读取 GUI runtime,GUI 专属命令在 headless 下返回结构化错误 - 更新 Cargo.lock 并确认 cargo check -p dbx-cli --locked 通过 --- Cargo.lock | 13 ++++++ crates/dbx-cli/src/commands.rs | 61 ++++++++++++++++++++++++++++ crates/dbx-cli/src/runtime_client.rs | 42 +++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 crates/dbx-cli/src/commands.rs create mode 100644 crates/dbx-cli/src/runtime_client.rs 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()) +}