second-brain-api 0.13.5

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
use std::collections::BTreeMap;
use std::io::Write;
use std::path::PathBuf;

use anyhow::{Context, Result};
use clap::Parser;
use serde_json::json;

use second_brain_adapters::claude_code::{ScreenedChunk, screen_transcript};

/// Runs the claude-code adapter's own reading, chunking and filter over
/// transcripts without a store, so capture measurements cannot drift from
/// the code that ingests.
#[derive(Parser)]
#[command(name = "sb-capture-probe")]
struct Cli {
    /// Print per-file counts instead of one JSON line per chunk. Chunk lines
    /// carry the chunk text, so write them to a file rather than a terminal
    /// whose transcript gets ingested.
    #[arg(long)]
    summary: bool,
    transcripts: Vec<PathBuf>,
}

fn kind(chunk: &ScreenedChunk) -> &'static str {
    if chunk.turn_final { "final" } else { "mid" }
}

fn summarise(chunks: &[ScreenedChunk]) -> serde_json::Value {
    let mut counts: BTreeMap<String, usize> = BTreeMap::new();
    for chunk in chunks {
        let verdict = if chunk.kept() { "kept" } else { "rejected" };
        *counts
            .entry(format!("{}_{verdict}", kind(chunk)))
            .or_default() += 1;
        for rule in &chunk.failed {
            *counts
                .entry(format!("{}_fails_{}", kind(chunk), rule.name()))
                .or_default() += 1;
        }
    }
    json!(counts)
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    let stdout = std::io::stdout();
    let mut out = stdout.lock();
    for path in &cli.transcripts {
        let content =
            std::fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
        let chunks = screen_transcript(&content);
        let path = path.to_string_lossy();
        if cli.summary {
            writeln!(
                out,
                "{}",
                json!({"path": path, "counts": summarise(&chunks)})
            )?;
            continue;
        }
        for (index, chunk) in chunks.iter().enumerate() {
            let failed: Vec<&str> = chunk.failed.iter().map(|r| r.name()).collect();
            let line = json!({
                "path": path,
                "index": index,
                "turn_final": chunk.turn_final,
                "kept": chunk.kept(),
                "failed": failed,
                "text": chunk.text,
            });
            writeln!(out, "{line}")?;
        }
    }
    Ok(())
}