use anyhow::Result; use clap::Subcommand; use kigi_shell::session::merge::MergedSession; use kigi_shell::util::kigi_home::kigi_home; #[derive(Debug, clap::Args, Clone)] pub struct SessionsArgs { #[command(subcommand)] command: SessionsCommand, } #[derive(Debug, Subcommand, Clone)] enum SessionsCommand { /// List recent sessions (same as search with no query) List { /// Maximum number of sessions to show #[arg(short = 'n', long, default_value = "20")] limit: usize, }, /// Search sessions by keyword Search { /// Search query (searches summaries and first prompts). query: String, /// Maximum number of sessions to show #[arg(short = 'n', long, default_value = "20")] limit: usize, }, /// Permanently delete a session from history Delete { /// Session id to delete. id: String, }, } pub async fn run(args: SessionsArgs) -> Result<()> { let cwd = std::env::current_dir().unwrap_or_else(|_| ".".into()); match args.command { SessionsCommand::List { limit } => { let sessions = kigi_shell::session::merge::fetch_merged(None, cwd.to_str(), None, limit).await; print_sessions_grouped(&sessions); } SessionsCommand::Search { query, limit } => { use kigi_shell::session::storage::search::{SessionSearchRequest, execute_search}; let req = SessionSearchRequest { query, cwd: Some(cwd.to_string_lossy().to_string()), limit, offset: 0, include_content: true, }; let root = kigi_home(); let resp = execute_search(&root, &req).await?; for hit in &resp.results { let title = if hit.title.is_empty() { "(untitled)" } else { &hit.title }; let time = chrono::DateTime::from_timestamp(hit.updated_at_unix, 0) .map(|dt| { dt.with_timezone(&chrono::Local) .format("%b %d, %l:%M%P") .to_string() }) .unwrap_or_default(); println!( "{} (score: {:.2}) {}\n {}\n {}", hit.session_id, hit.score, time, title, hit.snippet.as_deref().unwrap_or("") ); } println!("\nTotal: {}", resp.results.len()); } SessionsCommand::Delete { id } => { // Pass `cwd = None` so the session is found by id regardless of // which workspace it was created in; the local delete still uses // the resolved per-session cwd. let deletion = kigi_shell::session::persistence::delete_session_history(&id, None).await?; if deletion.any_removed() { println!("Deleted session {id}"); } else { println!("No session found with id {id}."); } } } Ok(()) } /// Print sessions grouped by worktree label, preserving the original table /// format with a `Label: