kiromi-ai-cli 0.2.0

Operator and developer CLI for the kiromi-ai-memory store: append, search, snapshot, regenerate, migrate-scheme, gc, audit-tail.
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! `kiromi-ai-memory related <id>`.

use std::str::FromStr;

use kiromi_ai_memory::{MemoryId, MemoryRef};

use crate::cli::{GlobalArgs, RelatedArgs};
use crate::cmd::get::probe_partition;
use crate::error::{CliError, ExitCode};
use crate::output;
use crate::runtime::Runtime;

pub(crate) async fn run(args: RelatedArgs, 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 memory id: {e}"),
    })?;
    // First fetch to learn the partition.
    let probe = MemoryRef {
        id,
        partition: probe_partition()?,
    };
    let rec = rt.mem.get(&probe).await?;
    let r = rec.r#ref;
    let hits = rt.mem.related(&r, args.top).await?;

    if globals.json {
        let wire: Vec<_> = hits
            .iter()
            .map(|h| {
                serde_json::json!({
                    "score": h.score,
                    "id": h.r#ref.id.to_string(),
                    "partition": h.r#ref.partition.as_str(),
                })
            })
            .collect();
        println!("{}", output::to_json(&wire));
    } else {
        println!("{}", output::hits_table(&hits));
    }
    rt.mem.close().await?;
    Ok(())
}