M10: /new, session listing, /sessions picker, hx -r, session titles, rpc session ops
ci / check (push) Canceled after 0s
ci / check (pull_request) Canceled after 0s

Closes #86, closes #87, closes #88, closes #89, closes #90, closes #91
This commit is contained in:
hermes committed 2026-10-01 09:35:47 +00:00
1 parent 1e87857a34
commit 23eb00d920
12 files changed
+957 -124

No files matched your search

+26 -20
View File
@@ -31,6 +31,9 @@ struct Cli {
/// Continue the most recent session in this directory.
#[arg(short, long)]
r#continue: bool,
/// Resume: no value opens the session picker; with a query resumes the best title match.
#[arg(short, long, num_args = 0..=1, default_missing_value = "")]
resume: Option<String>,
/// 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,
@@ -235,14 +238,7 @@ impl<H: Hook> Hook for ArcHook<H> {
}
fn session_dir(cwd: &std::path::Path) -> std::path::PathBuf {
let slug: String = cwd
.to_string_lossy()
.chars()
.map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
.collect();
context::hx_home()
.join("sessions")
.join(slug.trim_matches('-'))
hx_core::session::dir(&context::hx_home(), cwd)
}
#[tokio::main]
@@ -302,19 +298,28 @@ async fn main() -> Result<()> {
.unwrap_or_else(|| default_model(kind))
});
let dir = session_dir(&cwd);
let path = match (cli.session, cli.r#continue) {
(Some(p), _) => p,
(None, true) => std::fs::read_dir(&dir)
.ok()
.and_then(|rd| rd.flatten().map(|e| e.path()).max())
.ok_or_else(|| anyhow::anyhow!("no previous session in {}", dir.display()))?,
(None, false) => {
let ts = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)?
.as_millis();
dir.join(format!("{ts}.jsonl"))
let query = cli.resume.as_deref().filter(|q| !q.is_empty());
let path = match (cli.session, cli.r#continue, query) {
(Some(p), _, _) => p,
(None, _, Some(q)) => {
let all = hx_core::session::list(&dir);
let titles: Vec<&str> = all.iter().map(|m| m.title.as_str()).collect();
let best = hx_core::command::rank(titles.iter().copied(), q, 1);
let m = best
.first()
.and_then(|t| all.iter().find(|m| m.title == *t))
.ok_or_else(|| anyhow::anyhow!("no session in {} matches {q:?}", dir.display()))?;
eprintln!("[resuming: {}]", m.title);
m.path.clone()
}
(None, true, None) => hx_core::session::list(&dir)
.into_iter()
.next()
.map(|m| m.path)
.ok_or_else(|| anyhow::anyhow!("no previous session in {}", dir.display()))?,
(None, false, None) => hx_core::session::new_path(&dir),
};
let open_sessions = cli.resume.as_deref() == Some("");
let cx = ToolCtx {
cwd: cwd.clone(),
state: Default::default(),
@@ -413,7 +418,7 @@ async fn main() -> Result<()> {
}
let specs = command_specs(&mcp, &cwd);
if cli.mode == Mode::Rpc {
let r = rpc::serve(agent, abort, specs, hx_tui::project_files(&cwd)).await;
let r = rpc::serve(agent, abort, specs, hx_tui::project_files(&cwd), dir).await;
mgr.shutdown().await;
return r;
}
@@ -428,6 +433,7 @@ async fn main() -> Result<()> {
default_model,
pricing,
specs,
open_sessions,
commands: {
let mcp = mcp.clone();
Box::new(move |name: &str, arg: &str| {
+31 -1
View File
@@ -45,12 +45,14 @@ fn reply(id: &Value, r: Result<Value, String>) {
/// Methods: `prompt {text}` (replies when the run ends; events stream meanwhile), `abort`,
/// `state`, `messages`, `fork {id}`, `clear`, `set_model {model}`, `compact`,
/// `complete {line, cursor?}` (cursor = byte offset, default end). One prompt at a time.
/// `complete {line, cursor?}` (cursor = byte offset, default end), `sessions {scope?: project|all}`,
/// `new`, `resume {path}`. One prompt at a time.
pub async fn serve(
mut agent: Agent,
abort: AbortHandle,
specs: Vec<hx_core::command::CmdSpec>,
files: Vec<String>,
session_dir: std::path::PathBuf,
) -> Result<()> {
agent.bus.add(Notify);
let agent = Arc::new(Mutex::new(agent));
@@ -119,6 +121,7 @@ pub async fn serve(
let r = match method {
"state" => Ok(json!({
"model": a.model, "session": a.session.path(), "head": a.session.head(),
"title": hx_core::session::title(&a.session.messages()),
"messages": a.session.messages().len(), "usage": a.usage,
"tools": a.tools.specs().into_iter().map(|t| t.name).collect::<Vec<_>>(),
})),
@@ -144,6 +147,33 @@ pub async fn serve(
.await
.map(|c| json!({"compacted": c}))
.map_err(|e| e.to_string()),
"sessions" => {
use hx_core::session::{list, list_all};
let v = match p["scope"].as_str() {
Some("all") => list_all(session_dir.parent().unwrap_or(&session_dir)),
_ => list(&session_dir),
};
Ok(json!(v.iter().map(|m| json!({
"path": m.path, "title": m.title, "messages": m.messages, "model": m.model,
"branches": m.branches, "project": m.project,
"modified": m.modified.duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_secs()),
})).collect::<Vec<_>>()))
}
"new" => hx_core::session::Session::open(hx_core::session::new_path(&session_dir))
.map(|s| {
a.session = s;
json!({"session": a.session.path()})
})
.map_err(|e| e.to_string()),
"resume" => match p["path"].as_str() {
Some(path) => hx_core::session::Session::open(path)
.map(|s| {
a.session = s;
json!({"session": a.session.path(), "messages": a.session.messages().len()})
})
.map_err(|e| e.to_string()),
None => Err("params.path required".into()),
},
other => Err(format!("method not found: {other}")),
};
if !id.is_null() {