M10: /new, session listing, /sessions picker, hx -r, session titles, rpc session ops
Closes #86, closes #87, closes #88, closes #89, closes #90, closes #91
This commit is contained in:
1 parent
1e87857a34
commit
23eb00d920
12 files changed
+957
-124
No files matched your search
@@ -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() {
|
||||
|
||||
Reference in new issue
Block a user