forked from kent/consciousness
cli: convert simple commands to use memory_rpc
Commands now forward to daemon (or fallback to local store): - query → memory_query - journal tail → journal_tail - graph link-set → memory_link_set - graph link-add → memory_link_add - weight-set → memory_weight_set - node rename → memory_rename Removes ~50 lines of duplicated store access code. Co-Authored-By: Proof of Concept <poc@bcachefs.org>
This commit is contained in:
parent
d2a82d4327
commit
11b58e6b0b
4 changed files with 34 additions and 87 deletions
|
|
@ -90,39 +90,14 @@ pub fn find_current_transcript() -> Option<String> {
|
|||
newest.map(|(_, p)| p.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
fn journal_tail_query(store: &crate::store::Store, query: &str, n: usize, full: bool) -> Result<(), String> {
|
||||
let graph = store.build_graph();
|
||||
let stages = crate::query_parser::parse_stages(query)?;
|
||||
let results = crate::search::run_query(&stages, vec![], &graph, store, false, n);
|
||||
|
||||
// Query sorts desc and limits, so reverse to show oldest-to-newest
|
||||
for (key, _score) in results.into_iter().rev() {
|
||||
let Some(node) = store.nodes.get(&key) else { continue };
|
||||
let ts = if node.created_at > 0 {
|
||||
crate::store::format_datetime(node.created_at)
|
||||
} else if node.timestamp > 0 {
|
||||
crate::store::format_datetime(node.timestamp)
|
||||
} else {
|
||||
node.key.clone()
|
||||
};
|
||||
let title = extract_title(&node.content);
|
||||
if full {
|
||||
println!("--- [{}] {} ---\n{}\n", ts, title, node.content);
|
||||
} else {
|
||||
println!("[{}] {}", ts, title);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_journal_tail(n: usize, full: bool, level: u8) -> Result<(), String> {
|
||||
let store = crate::store::Store::load()?;
|
||||
|
||||
let query = format!("all | type:{} | sort:timestamp | limit:{}",
|
||||
match level { 0 => "episodic", 1 => "daily", 2 => "weekly", _ => "monthly" },
|
||||
n
|
||||
);
|
||||
journal_tail_query(&store, &query, n, full)
|
||||
let format = if full { "full" } else { "compact" };
|
||||
let result = crate::mcp_server::memory_rpc(
|
||||
"journal_tail",
|
||||
serde_json::json!({"count": n, "level": level, "format": format}),
|
||||
).map_err(|e| e.to_string())?;
|
||||
print!("{}", result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn cmd_journal_write(name: &str, text: &[String]) -> Result<(), String> {
|
||||
|
|
@ -163,21 +138,3 @@ pub fn cmd_journal_write(name: &str, text: &[String]) -> Result<(), String> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
fn extract_title(content: &str) -> String {
|
||||
let date_re = regex::Regex::new(r"(\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2})").unwrap();
|
||||
for line in content.lines() {
|
||||
let stripped = line.trim();
|
||||
if stripped.is_empty() { continue; }
|
||||
if date_re.is_match(stripped) && stripped.len() < 25 { continue; }
|
||||
if let Some(h) = stripped.strip_prefix("## ") {
|
||||
return h.to_string();
|
||||
} else if let Some(h) = stripped.strip_prefix("# ") {
|
||||
return h.to_string();
|
||||
} else {
|
||||
return crate::util::truncate(stripped, 67, "...");
|
||||
}
|
||||
}
|
||||
String::from("(untitled)")
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue