M7: json + rpc modes, subprocess extensions (closes #52 #53 #54)
ci / check (pull_request) Canceled after 0s
ci / check (push) Canceled after 0s

This commit is contained in:
hermes committed 2026-09-30 21:40:20 +00:00
1 parent cf8268d17b
commit a01d0cbb5b
15 files changed
+723 -3

No files matched your search

+43 -1
View File
@@ -8,6 +8,8 @@ use hx_core::hook::{Event, Hook, HookAction};
use hx_core::session::Session;
use hx_core::tool::ToolCtx;
use std::io::Write;
mod rpc;
use std::sync::Arc;
#[derive(Parser)]
@@ -29,6 +31,16 @@ struct Cli {
/// Continue the most recent session in this directory.
#[arg(short, long)]
r#continue: bool,
/// Output mode: text (default for -p), json (NDJSON events on stdout; needs -p), rpc (JSON-RPC on stdio).
#[arg(long, value_enum, default_value_t = Mode::Text)]
mode: Mode,
}
#[derive(Clone, Copy, PartialEq, clap::ValueEnum)]
enum Mode {
Text,
Json,
Rpc,
}
#[derive(Subcommand)]
@@ -57,6 +69,8 @@ struct Config {
/// Replaces the built-in system prompt (AGENTS.md etc. are still appended).
system_prompt: Option<String>,
lsp: Option<LspCfg>,
/// subprocess extensions (JSON-RPC over stdio); global and project lists are concatenated
extensions: Vec<hx_core::ext::ExtCfg>,
}
#[derive(Default, Clone, serde::Deserialize)]
@@ -97,6 +111,7 @@ impl Config {
max_tokens: o.max_tokens.or(c.max_tokens),
system_prompt: o.system_prompt.or(c.system_prompt),
lsp: o.lsp.or(c.lsp),
extensions: [c.extensions, o.extensions].concat(),
};
}
Ok(c)
@@ -301,6 +316,15 @@ async fn main() -> Result<()> {
if let Some(n) = cfg.max_tokens {
agent.max_tokens = n;
}
for e in &cfg.extensions {
let x = hx_core::ext::Extension::start(e, &cwd)
.await
.map_err(|err| anyhow::anyhow!("extension {:?}: {err}", e.command))?;
for t in x.tools() {
agent.tools.add(t);
}
agent.bus.add_arc(x);
}
let lcfg = cfg.lsp.clone().unwrap_or_default();
let mut servers = hx_lens::manager::default_servers();
servers.retain(|s| !lcfg.disable.contains(&s.id) && !lcfg.servers.iter().any(|o| o.id == s.id));
@@ -353,6 +377,14 @@ async fn main() -> Result<()> {
.collect();
mgr.warm(probe).await;
}
if cli.mode == Mode::Rpc {
let r = rpc::serve(agent, abort).await;
mgr.shutdown().await;
return r;
}
if cli.mode == Mode::Json && cli.print.is_none() {
anyhow::bail!("--mode json needs -p <prompt>");
}
let Some(prompt) = cli.print else {
hx_tui::run(hx_tui::Setup {
agent,
@@ -423,13 +455,23 @@ async fn main() -> Result<()> {
mgr.shutdown().await;
return Ok(());
};
agent.bus.add(Printer);
if cli.mode == Mode::Json {
agent.bus.add(rpc::Ndjson);
} else {
agent.bus.add(Printer);
}
tokio::spawn(async move {
if tokio::signal::ctrl_c().await.is_ok() {
abort.abort();
}
});
let r = agent.run(prompt).await;
if cli.mode == Mode::Json {
rpc::emit(
&serde_json::json!({"type": "done", "error": r.as_ref().err().map(|e| e.to_string()),
"usage": agent.usage, "session": path}),
);
}
let u = &agent.usage;
let cost = pricing(&agent.model)
.map(|p| format!(" ~${:.4}", u.cost(&p)))