feat: add --schema, --max-turns and rpc --ask
- hx -p --schema <file> validates the final answer against a JSON schema, with one retry that includes the validation errors - max_turns config key and --max-turns cap the main agent's model calls - hx --mode rpc --ask sends permission requests to the client instead of auto-allowing bash/write/edit calls Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
13d914c8c0
commit
dfec176f43
11 files changed
+599
-14
No files matched your search
@@ -13,6 +13,7 @@ use std::io::Write;
|
||||
|
||||
mod acp;
|
||||
mod rpc;
|
||||
mod schema;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Parser)]
|
||||
@@ -63,6 +64,22 @@ struct Cli {
|
||||
/// 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,
|
||||
#[arg(
|
||||
long,
|
||||
help = "rpc mode: send a `permission` request to the client for bash/write/edit calls"
|
||||
)]
|
||||
ask: bool,
|
||||
#[arg(
|
||||
long,
|
||||
help = "Model calls per prompt before the run stops (default 200)"
|
||||
)]
|
||||
max_turns: Option<u32>,
|
||||
#[arg(
|
||||
long,
|
||||
requires = "print",
|
||||
help = "JSON schema file: the final answer must match it (one retry); prints only the JSON"
|
||||
)]
|
||||
schema: Option<std::path::PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, PartialEq, clap::ValueEnum)]
|
||||
@@ -154,6 +171,7 @@ struct Config {
|
||||
codex_model: Option<String>,
|
||||
compact_at: Option<usize>,
|
||||
max_tokens: Option<u32>,
|
||||
max_turns: Option<u32>,
|
||||
explore_model: Option<String>,
|
||||
subagents: Option<bool>,
|
||||
subagent_parallel: Option<usize>,
|
||||
@@ -222,6 +240,7 @@ impl Config {
|
||||
codex_model: o.codex_model.or(c.codex_model),
|
||||
compact_at: o.compact_at.or(c.compact_at),
|
||||
max_tokens: o.max_tokens.or(c.max_tokens),
|
||||
max_turns: o.max_turns.or(c.max_turns),
|
||||
explore_model: o.explore_model.or(c.explore_model),
|
||||
subagents: o.subagents.or(c.subagents),
|
||||
subagent_parallel: o.subagent_parallel.or(c.subagent_parallel),
|
||||
@@ -1137,6 +1156,9 @@ async fn main() -> Result<()> {
|
||||
.judge
|
||||
.set(hx_core::danger::Judge { provider: p, model });
|
||||
}
|
||||
if let Some(n) = cli.max_turns.or(cfg.max_turns) {
|
||||
agent.set_max_turns(n);
|
||||
}
|
||||
if cfg.codemode.unwrap_or(false) {
|
||||
let cm = hx_tools::codemode::Codemode::new(&agent.tools);
|
||||
agent.tools.add(cm);
|
||||
@@ -1161,6 +1183,7 @@ async fn main() -> Result<()> {
|
||||
.auto_cfg(auto_cfg.clone())
|
||||
.remember_in(last_path.clone(), start.clone())
|
||||
.auto_on(want_auto),
|
||||
ask: cli.ask,
|
||||
},
|
||||
)
|
||||
.await;
|
||||
@@ -1321,9 +1344,17 @@ async fn main() -> Result<()> {
|
||||
mgr.shutdown().await;
|
||||
return Ok(());
|
||||
};
|
||||
let schema = cli
|
||||
.schema
|
||||
.as_deref()
|
||||
.map(schema::Schema::load)
|
||||
.transpose()?;
|
||||
if let Some(s) = &schema {
|
||||
agent.append_system(&format!("{}{}", schema::INSTRUCTION, s.text));
|
||||
}
|
||||
if cli.mode == Mode::Json {
|
||||
agent.bus.add(rpc::Ndjson);
|
||||
} else {
|
||||
} else if schema.is_none() {
|
||||
agent.bus.add(Printer);
|
||||
}
|
||||
tokio::spawn(async move {
|
||||
@@ -1331,10 +1362,40 @@ async fn main() -> Result<()> {
|
||||
abort.abort();
|
||||
}
|
||||
});
|
||||
let r = agent.run(prompt).await;
|
||||
let mut r = agent.run(prompt).await;
|
||||
let mut output = None;
|
||||
let mut schema_err = None;
|
||||
if let Some(s) = &schema {
|
||||
for attempt in 0..2 {
|
||||
let Ok(m) = &r else { break };
|
||||
match s.check(&m.text()) {
|
||||
Ok(v) => {
|
||||
schema_err = None;
|
||||
output = Some(v);
|
||||
break;
|
||||
}
|
||||
Err(p) => {
|
||||
schema_err = Some(p.clone());
|
||||
if attempt == 0 {
|
||||
r = agent.run(schema::retry_prompt(&p)).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(v) = &output
|
||||
&& cli.mode != Mode::Json
|
||||
{
|
||||
println!("{v}");
|
||||
}
|
||||
}
|
||||
let error = r
|
||||
.as_ref()
|
||||
.err()
|
||||
.map(|e| e.to_string())
|
||||
.or(schema_err.map(|e| format!("answer does not match the schema: {e}")));
|
||||
if cli.mode == Mode::Json {
|
||||
rpc::emit(
|
||||
&serde_json::json!({"type": "done", "error": r.as_ref().err().map(|e| e.to_string()),
|
||||
&serde_json::json!({"type": "done", "error": error, "output": output,
|
||||
"usage": agent.usage(), "session": path}),
|
||||
);
|
||||
}
|
||||
@@ -1359,6 +1420,9 @@ async fn main() -> Result<()> {
|
||||
hx_core::herdr::release().await;
|
||||
mgr.shutdown().await;
|
||||
r?;
|
||||
if let Some(e) = error {
|
||||
anyhow::bail!(e);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user