cli: move helpers to cli modules, main.rs under 1100 lines
Move CLI-specific helpers to their cli/ modules: - journal_tail_entries, journal_tail_digests, extract_title, find_current_transcript → cli/journal.rs - get_group_content → cli/misc.rs - cmd_journal_write, cmd_journal_tail, cmd_load_context follow These are presentation/session helpers, not library code — they belong in the CLI layer per Kent's guidance. main.rs: 3130 → 1054 lines (66% reduction). Co-Authored-By: Kent Overstreet <kent.overstreet@linux.dev>
This commit is contained in:
parent
8640d50990
commit
99db511403
3 changed files with 282 additions and 281 deletions
|
|
@ -208,3 +208,103 @@ pub fn cmd_query(expr: &[String]) -> Result<(), String> {
|
|||
crate::query_parser::run_query(&store, &graph, &query_str)
|
||||
}
|
||||
|
||||
fn get_group_content(group: &crate::config::ContextGroup, store: &crate::store::Store, cfg: &crate::config::Config) -> Vec<(String, String)> {
|
||||
match group.source {
|
||||
crate::config::ContextSource::Journal => {
|
||||
let mut entries = Vec::new();
|
||||
let now = crate::store::now_epoch();
|
||||
let window: i64 = cfg.journal_days as i64 * 24 * 3600;
|
||||
let cutoff = now - window;
|
||||
let key_date_re = regex::Regex::new(r"j-(\d{4}-\d{2}-\d{2})").unwrap();
|
||||
|
||||
let journal_ts = |n: &crate::store::Node| -> i64 {
|
||||
if n.created_at > 0 { return n.created_at; }
|
||||
if let Some(caps) = key_date_re.captures(&n.key) {
|
||||
use chrono::{NaiveDate, TimeZone, Local};
|
||||
if let Ok(d) = NaiveDate::parse_from_str(&caps[1], "%Y-%m-%d") {
|
||||
if let Some(dt) = Local.from_local_datetime(&d.and_hms_opt(0, 0, 0).unwrap()).earliest() {
|
||||
return dt.timestamp();
|
||||
}
|
||||
}
|
||||
}
|
||||
n.timestamp
|
||||
};
|
||||
|
||||
let mut journal_nodes: Vec<_> = store.nodes.values()
|
||||
.filter(|n| n.node_type == crate::store::NodeType::EpisodicSession && journal_ts(n) >= cutoff)
|
||||
.collect();
|
||||
journal_nodes.sort_by_key(|n| journal_ts(n));
|
||||
|
||||
let max = cfg.journal_max;
|
||||
let skip = journal_nodes.len().saturating_sub(max);
|
||||
for node in journal_nodes.iter().skip(skip) {
|
||||
entries.push((node.key.clone(), node.content.clone()));
|
||||
}
|
||||
entries
|
||||
}
|
||||
crate::config::ContextSource::File => {
|
||||
group.keys.iter().filter_map(|key| {
|
||||
let content = std::fs::read_to_string(cfg.data_dir.join(key)).ok()?;
|
||||
if content.trim().is_empty() { return None; }
|
||||
Some((key.clone(), content.trim().to_string()))
|
||||
}).collect()
|
||||
}
|
||||
crate::config::ContextSource::Store => {
|
||||
group.keys.iter().filter_map(|key| {
|
||||
let content = store.render_file(key)?;
|
||||
if content.trim().is_empty() { return None; }
|
||||
Some((key.clone(), content.trim().to_string()))
|
||||
}).collect()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn cmd_load_context(stats: bool) -> Result<(), String> {
|
||||
let cfg = crate::config::get();
|
||||
let store = crate::store::Store::load()?;
|
||||
|
||||
if stats {
|
||||
let mut total_words = 0;
|
||||
let mut total_entries = 0;
|
||||
println!("{:<25} {:>6} {:>8}", "GROUP", "ITEMS", "WORDS");
|
||||
println!("{}", "-".repeat(42));
|
||||
|
||||
for group in &cfg.context_groups {
|
||||
let entries = get_group_content(group, &store, cfg);
|
||||
let words: usize = entries.iter()
|
||||
.map(|(_, c)| c.split_whitespace().count())
|
||||
.sum();
|
||||
let count = entries.len();
|
||||
println!("{:<25} {:>6} {:>8}", group.label, count, words);
|
||||
total_words += words;
|
||||
total_entries += count;
|
||||
}
|
||||
|
||||
println!("{}", "-".repeat(42));
|
||||
println!("{:<25} {:>6} {:>8}", "TOTAL", total_entries, total_words);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
println!("=== MEMORY SYSTEM ({}) ===", cfg.assistant_name);
|
||||
println!();
|
||||
|
||||
for group in &cfg.context_groups {
|
||||
let entries = get_group_content(group, &store, cfg);
|
||||
if !entries.is_empty() && group.source == crate::config::ContextSource::Journal {
|
||||
println!("--- recent journal entries ({}/{}) ---",
|
||||
entries.len(), cfg.journal_max);
|
||||
}
|
||||
for (key, content) in entries {
|
||||
if group.source == crate::config::ContextSource::Journal {
|
||||
println!("## {}", key);
|
||||
} else {
|
||||
println!("--- {} ({}) ---", key, group.label);
|
||||
}
|
||||
println!("{}\n", content);
|
||||
}
|
||||
}
|
||||
|
||||
println!("=== END MEMORY LOAD ===");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue