use std::str::FromStr;
use kiromi_ai_memory::{DeleteOpts, MemoryId, MemoryRef, PartitionPath, Partitions};
use crate::cli::{DeleteArgs, DeletePartitionArgs, GlobalArgs};
use crate::cmd::get::probe_partition;
use crate::error::{CliError, ExitCode};
use crate::output;
use crate::runtime::Runtime;
pub(crate) async fn run_delete(args: DeleteArgs, globals: &GlobalArgs) -> Result<(), CliError> {
let rt = Runtime::open(globals).await?;
let id = MemoryId::from_str(&args.id).map_err(|e| CliError {
kind: ExitCode::Config,
source: anyhow::anyhow!("bad id: {e}"),
})?;
let probe = MemoryRef {
id,
partition: probe_partition()?,
};
let rec = rt.mem.get(&probe).await?;
rt.mem.delete(&rec.r#ref).await?;
if globals.json {
println!(
"{}",
output::to_json(&serde_json::json!({"deleted": id.to_string()}))
);
} else {
println!("deleted {id}");
}
rt.mem.close().await?;
Ok(())
}
pub(crate) async fn run_delete_partition(
args: DeletePartitionArgs,
globals: &GlobalArgs,
) -> Result<(), CliError> {
let rt = Runtime::open(globals).await?;
let path = PartitionPath::from_str(&args.path).map_err(|e| CliError {
kind: ExitCode::Config,
source: anyhow::anyhow!("bad partition path {:?}: {e}", args.path),
})?;
let partitions = Partitions::from_path(&path);
let n = rt
.mem
.delete_partition(partitions, DeleteOpts::soft())
.await?;
if globals.json {
println!("{}", output::to_json(&serde_json::json!({"tombstoned": n})));
} else {
println!("tombstoned {n} memories under {}", path.as_str());
}
rt.mem.close().await?;
Ok(())
}