fix(dbx-cli): 补齐最小 runtime client 模块以恢复编译

- 新增 commands 和 runtime_client 最小实现,补足 main.rs 所需模块

- 允许 context 读取 GUI runtime,GUI 专属命令在 headless 下返回结构化错误

- 更新 Cargo.lock 并确认 cargo check -p dbx-cli --locked 通过
This commit is contained in:
Illuminated2020
2026-05-12 16:43:46 +08:00
parent 5720f05dd0
commit 01110945a1
3 changed files with 116 additions and 0 deletions
Generated
+13
View File
@@ -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"
+61
View File
@@ -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<T> {
Success { ok: bool, source: CliSource, data: T },
Failure { ok: bool, source: CliSource, error: CliError },
}
pub(crate) async fn run(args: Vec<String>) -> 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<serde_json::Value> {
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<T>(source: CliSource, data: T) -> CliEnvelope<T> {
CliEnvelope::Success { ok: true, source, data }
}
fn runtime_required(message: &str) -> CliEnvelope<serde_json::Value> {
CliEnvelope::Failure {
ok: false,
source: CliSource::Headless,
error: CliError { code: CliErrorCode::GuiRuntimeRequired, message: message.to_string(), recoverable: true },
}
}
+42
View File
@@ -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<RuntimeDiscovery> {
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<serde_json::Value, String> {
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())
}