Skip to main content

kaizen/shell/
extensions.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2//! Commands for interchange and audit extensions.
3
4use crate::extensions::{aggregates, atif, hash_chain, jsonl};
5use crate::shell::cli::workspace_path;
6use crate::store::Store;
7use anyhow::Result;
8use std::path::Path;
9
10pub fn cmd_aggregates_rebuild(workspace: Option<&Path>) -> Result<()> {
11    let ws = workspace_path(workspace)?;
12    let store = open_store(&ws)?;
13    let count = aggregates::rebuild_workspace(&store, &ws.to_string_lossy())?;
14    println!("rebuilt session_aggregates: {count}");
15    Ok(())
16}
17
18pub fn cmd_export_atif(workspace: Option<&Path>, session: &str) -> Result<()> {
19    let ws = workspace_path(workspace)?;
20    let store = open_store(&ws)?;
21    let doc = atif::export_session(&store, session)?;
22    println!("{}", serde_json::to_string_pretty(&doc)?);
23    Ok(())
24}
25
26pub fn cmd_import_atif(workspace: Option<&Path>, file: &Path) -> Result<()> {
27    let ws = workspace_path(workspace)?;
28    let store = open_store(&ws)?;
29    let doc = atif::import_file(&store, file, &ws.to_string_lossy())?;
30    println!(
31        "imported atif: {} steps={}",
32        doc.trajectory_id,
33        doc.steps.len()
34    );
35    Ok(())
36}
37
38pub fn cmd_import_jsonl(workspace: Option<&Path>, file: &Path) -> Result<()> {
39    let ws = workspace_path(workspace)?;
40    let store = open_store(&ws)?;
41    let report = jsonl::import_file(&store, file, &ws.to_string_lossy())?;
42    println!(
43        "imported jsonl: events={} sessions_created={}",
44        report.imported_events, report.sessions_created
45    );
46    Ok(())
47}
48
49pub fn cmd_verify_hash_chain(workspace: Option<&Path>, session: Option<&str>) -> Result<()> {
50    let ws = workspace_path(workspace)?;
51    let store = open_store(&ws)?;
52    let report = hash_chain::verify(&store, &ws.to_string_lossy(), session)?;
53    println!("{}", serde_json::to_string_pretty(&report)?);
54    Ok(())
55}
56
57fn open_store(workspace: &Path) -> Result<Store> {
58    Store::open(&crate::core::workspace::db_path(workspace)?)
59}