- hx-tui: ratatui app with streaming transcript, markdown-ish rendering, tool call status, colored edit diffs, collapsible bash/read output (ctrl-o), scrollback, spinner + footer (provider, model, ctx, tokens) - editor: multi-line, history, word/line kill, bracketed paste, @file fuzzy tab completion; mentioned files attached - message queue while agent runs; esc/ctrl-c abort; /model /provider /compact /clear /help /quit; resumed history shown - hx-core: compaction (summarize older prompts, keep recent verbatim, session rebase keeps full file history), auto-compact at threshold; abort only ends current run - config: ~/.hx/config.toml + project .hx/config.toml (provider, per-provider models, compact_at, max_tokens, system_prompt) - API-equivalent cost estimate in exit summary - tests/tui_smoke.py drives the real TUI in a pty against the fake API Closes #15, closes #16, closes #17, closes #18, closes #19
This commit is contained in:
1 parent
21d4d2bb3d
commit
89c9833e43
13 files changed
+2875
-48
No files matched your search
@@ -15,7 +15,10 @@ clap = { version = "4.6.7", features = ["derive", "env"] }
|
||||
hx-ai = { version = "0.1.0", path = "../hx-ai" }
|
||||
hx-core = { version = "0.1.0", path = "../hx-core" }
|
||||
hx-tools = { version = "0.1.0", path = "../hx-tools" }
|
||||
hx-tui = { version = "0.1.0", path = "../hx-tui" }
|
||||
serde = { version = "1.0.229", features = ["derive"] }
|
||||
tokio = { version = "1.53.1", features = ["rt-multi-thread", "macros", "signal"] }
|
||||
toml = "1.1.6"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
+112
-13
@@ -18,9 +18,9 @@ struct Cli {
|
||||
/// Print mode: send one prompt, stream the reply to stdout, exit.
|
||||
#[arg(short, long)]
|
||||
print: Option<String>,
|
||||
/// claude | codex
|
||||
#[arg(long, env = "HX_PROVIDER", default_value = "claude")]
|
||||
provider: Kind,
|
||||
/// claude | codex (default from ~/.hx/config.toml, else claude)
|
||||
#[arg(long, env = "HX_PROVIDER")]
|
||||
provider: Option<Kind>,
|
||||
#[arg(short, long, env = "HX_MODEL")]
|
||||
model: Option<String>,
|
||||
/// Session file to resume (JSONL). Default: new file under ~/.hx/sessions.
|
||||
@@ -39,11 +39,70 @@ enum Cmd {
|
||||
Logout { provider: Kind },
|
||||
}
|
||||
|
||||
fn default_model(k: Kind) -> &'static str {
|
||||
/// `~/.hx/config.toml`, overridden by `.hx/config.toml` in the project.
|
||||
#[derive(Default, serde::Deserialize)]
|
||||
#[serde(default, deny_unknown_fields)]
|
||||
struct Config {
|
||||
provider: Option<Kind>,
|
||||
model: Option<String>,
|
||||
claude_model: Option<String>,
|
||||
codex_model: Option<String>,
|
||||
/// Auto-compact threshold in estimated tokens.
|
||||
compact_at: Option<usize>,
|
||||
max_tokens: Option<u32>,
|
||||
/// Replaces the built-in system prompt (AGENTS.md etc. are still appended).
|
||||
system_prompt: Option<String>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
fn load(cwd: &std::path::Path) -> Result<Self> {
|
||||
let mut c = Config::default();
|
||||
for p in [
|
||||
context::hx_home().join("config.toml"),
|
||||
cwd.join(".hx/config.toml"),
|
||||
] {
|
||||
let Ok(s) = std::fs::read_to_string(&p) else {
|
||||
continue;
|
||||
};
|
||||
let o: Config =
|
||||
toml::from_str(&s).map_err(|e| anyhow::anyhow!("{}: {e}", p.display()))?;
|
||||
c = Config {
|
||||
provider: o.provider.or(c.provider),
|
||||
model: o.model.or(c.model),
|
||||
claude_model: o.claude_model.or(c.claude_model),
|
||||
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),
|
||||
system_prompt: o.system_prompt.or(c.system_prompt),
|
||||
};
|
||||
}
|
||||
Ok(c)
|
||||
}
|
||||
}
|
||||
|
||||
fn default_model(k: Kind) -> String {
|
||||
match k {
|
||||
Kind::Claude => "claude-sonnet-4-5",
|
||||
Kind::Codex => "gpt-5-codex",
|
||||
}
|
||||
.into()
|
||||
}
|
||||
|
||||
/// USD per million tokens. ponytail: subscription usage isn't billed per token; this is an API-equivalent estimate.
|
||||
fn pricing(model: &str) -> Option<hx_ai::Pricing> {
|
||||
let p = |input, output, cache_read, cache_write| hx_ai::Pricing {
|
||||
input,
|
||||
output,
|
||||
cache_read,
|
||||
cache_write,
|
||||
};
|
||||
Some(match model {
|
||||
m if m.contains("opus") => p(15.0, 75.0, 1.5, 18.75),
|
||||
m if m.contains("sonnet") => p(3.0, 15.0, 0.3, 3.75),
|
||||
m if m.contains("haiku") => p(1.0, 5.0, 0.1, 1.25),
|
||||
m if m.starts_with("gpt-5") => p(1.25, 10.0, 0.125, 0.0),
|
||||
_ => return None,
|
||||
})
|
||||
}
|
||||
|
||||
fn provider(k: Kind) -> hx_ai::Result<Arc<dyn Provider>> {
|
||||
@@ -117,10 +176,16 @@ async fn main() -> Result<()> {
|
||||
Some(Cmd::Logout { provider }) => return Ok(auth::logout(provider)?),
|
||||
None => {}
|
||||
}
|
||||
let Some(prompt) = cli.print else {
|
||||
anyhow::bail!("interactive mode not implemented yet; use -p \"prompt\"");
|
||||
};
|
||||
let cwd = std::env::current_dir()?;
|
||||
let cfg = Config::load(&cwd)?;
|
||||
let kind = cli.provider.or(cfg.provider).unwrap_or(Kind::Claude);
|
||||
let model = cli.model.or(cfg.model).unwrap_or_else(|| {
|
||||
match kind {
|
||||
Kind::Claude => cfg.claude_model.clone(),
|
||||
Kind::Codex => cfg.codex_model.clone(),
|
||||
}
|
||||
.unwrap_or_else(|| default_model(kind))
|
||||
});
|
||||
let dir = session_dir(&cwd);
|
||||
let path = match (cli.session, cli.r#continue) {
|
||||
(Some(p), _) => p,
|
||||
@@ -140,15 +205,30 @@ async fn main() -> Result<()> {
|
||||
state: Default::default(),
|
||||
};
|
||||
let (mut agent, abort) = Agent::new(
|
||||
provider(cli.provider)?,
|
||||
provider(kind)?,
|
||||
hx_tools::registry(),
|
||||
Session::open(&path)?,
|
||||
cx,
|
||||
);
|
||||
agent.model = cli
|
||||
.model
|
||||
.unwrap_or_else(|| default_model(cli.provider).into());
|
||||
agent.system = context::system_prompt(None, &context::hx_home(), &cwd);
|
||||
agent.model = model;
|
||||
agent.system = context::system_prompt(cfg.system_prompt.as_deref(), &context::hx_home(), &cwd);
|
||||
if let Some(n) = cfg.compact_at {
|
||||
agent.compact_at = n;
|
||||
}
|
||||
if let Some(n) = cfg.max_tokens {
|
||||
agent.max_tokens = n;
|
||||
}
|
||||
let Some(prompt) = cli.print else {
|
||||
hx_tui::run(hx_tui::Setup {
|
||||
agent,
|
||||
kind,
|
||||
make_provider: Box::new(provider),
|
||||
default_model,
|
||||
pricing,
|
||||
})
|
||||
.await?;
|
||||
return Ok(());
|
||||
};
|
||||
agent.bus.add(Printer);
|
||||
tokio::spawn(async move {
|
||||
if tokio::signal::ctrl_c().await.is_ok() {
|
||||
@@ -157,8 +237,11 @@ async fn main() -> Result<()> {
|
||||
});
|
||||
let r = agent.run(prompt).await;
|
||||
let u = &agent.usage;
|
||||
let cost = pricing(&agent.model)
|
||||
.map(|p| format!(" ~${:.4}", u.cost(&p)))
|
||||
.unwrap_or_default();
|
||||
eprintln!(
|
||||
"[{} in / {} out tokens; session {}]",
|
||||
"[{} in / {} out tokens{cost}; session {}]",
|
||||
u.input_tokens + u.cache_read_tokens,
|
||||
u.output_tokens,
|
||||
path.display()
|
||||
@@ -166,3 +249,19 @@ async fn main() -> Result<()> {
|
||||
r?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn config_parse() {
|
||||
let c: Config =
|
||||
toml::from_str("provider = \"codex\"\ncodex_model = \"gpt-5\"\ncompact_at = 1000")
|
||||
.unwrap();
|
||||
assert_eq!(c.provider, Some(Kind::Codex));
|
||||
assert_eq!(c.codex_model.as_deref(), Some("gpt-5"));
|
||||
assert!(toml::from_str::<Config>("typo = 1").is_err());
|
||||
assert!(pricing("claude-sonnet-4-5").is_some() && pricing("mystery").is_none());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user