hx serve --acp: headless Agent Client Protocol mode
JSON-RPC on stdio with permission requests, hx/step|message|interrupt|compact in and hx/step_result|intent|edit|usage|decision|plan out. Sessions persist by id. Limits are soft: a limit stop is blocked with reason "limit" and no catch; catch is only a validated name from the step's catches list. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5408db83a9
commit
aec92ff5a5
7 files changed
+952
-1
No files matched your search
@@ -11,6 +11,7 @@ use hx_core::switch::Switcher;
|
||||
use hx_core::tool::ToolCtx;
|
||||
use std::io::Write;
|
||||
|
||||
mod acp;
|
||||
mod rpc;
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -97,6 +98,18 @@ enum Cmd {
|
||||
#[command(subcommand)]
|
||||
action: ConfigAction,
|
||||
},
|
||||
/// Run headless for an orchestrator: Agent Client Protocol (JSON-RPC) on stdio, no TUI.
|
||||
Serve {
|
||||
/// Speak ACP on stdio (the only protocol so far).
|
||||
#[arg(long)]
|
||||
acp: bool,
|
||||
/// Working directory (the worktree to edit).
|
||||
#[arg(long)]
|
||||
cwd: std::path::PathBuf,
|
||||
/// Session id; a later `hx serve` with the same id continues that conversation.
|
||||
#[arg(long)]
|
||||
session: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -580,7 +593,29 @@ fn session_dir(cwd: &std::path::Path) -> std::path::PathBuf {
|
||||
async fn main() -> Result<()> {
|
||||
trace("main");
|
||||
let cli = Cli::parse();
|
||||
let mut acp_id = None;
|
||||
match cli.cmd {
|
||||
Some(Cmd::Serve { acp, cwd, session }) => {
|
||||
anyhow::ensure!(acp, "hx serve: --acp is required");
|
||||
// file-name safe, so the id maps to one session file
|
||||
let id: String = session
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || "-_.".contains(c) {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
anyhow::ensure!(
|
||||
!id.trim_matches('.').is_empty(),
|
||||
"hx serve: --session is empty"
|
||||
);
|
||||
std::env::set_current_dir(&cwd)
|
||||
.map_err(|e| anyhow::anyhow!("--cwd {}: {e}", cwd.display()))?;
|
||||
acp_id = Some(id);
|
||||
}
|
||||
Some(Cmd::Login { provider }) => {
|
||||
auth::login(provider).await?;
|
||||
eprintln!("logged in; credentials in {}", auth::store_path().display());
|
||||
@@ -872,6 +907,10 @@ async fn main() -> Result<()> {
|
||||
let dir = session_dir(&cwd);
|
||||
let query = cli.resume.as_deref().filter(|q| !q.is_empty());
|
||||
let path = match (cli.session, cli.r#continue, query) {
|
||||
_ if acp_id.is_some() => dir.join(format!(
|
||||
"acp-{}.jsonl",
|
||||
acp_id.as_deref().unwrap_or_default()
|
||||
)),
|
||||
(Some(p), _, _) => p,
|
||||
(None, _, Some(q)) => {
|
||||
let all = hx_core::session::list(&dir);
|
||||
@@ -1104,6 +1143,18 @@ async fn main() -> Result<()> {
|
||||
let _ = hx_core::approve::serve(&p, cx, bus, mode).await;
|
||||
});
|
||||
}
|
||||
if let Some(id) = acp_id {
|
||||
// no TUI, no prompts: every permission question goes to the orchestrator
|
||||
let opts = acp::Opts {
|
||||
session: id,
|
||||
cwd: cwd.clone(),
|
||||
pricing: pricing(agent.model()),
|
||||
};
|
||||
let r = acp::serve(agent, abort, opts).await;
|
||||
hx_core::herdr::release().await;
|
||||
mgr.shutdown().await;
|
||||
return r;
|
||||
}
|
||||
let Some(prompt) = cli.print else {
|
||||
hx_tui::run(hx_tui::Setup {
|
||||
agent,
|
||||
|
||||
Reference in new issue
Block a user