second-brain-api 0.13.1

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
//! One-off data repairs, each idempotent: a second run finds nothing left
//! to change, so running one again after a partial failure is always safe.

use std::sync::Arc;

use axum::Json;
use axum::extract::State;
use axum::response::{IntoResponse, Response};
use second_brain_adapters::session_evidence::TranscriptEvidence;
use serde::{Deserialize, Serialize};

use crate::AppState;
use crate::routes::internal_error;

async fn run<T, F>(state: Arc<AppState>, repair: F) -> Response
where
    T: Serialize + Send + 'static,
    F: FnOnce(&second_brain_core::sqlite_store::SqliteStore) -> anyhow::Result<T> + Send + 'static,
{
    let result = tokio::task::spawn_blocking(move || repair(&state.sqlite)).await;
    match result {
        Ok(Ok(outcome)) => Json(outcome).into_response(),
        Ok(Err(e)) => internal_error(e),
        Err(e) => internal_error(e),
    }
}

pub async fn refile_nested_projects(State(state): State<Arc<AppState>>) -> Response {
    run(state, |store| {
        Ok(serde_json::json!({
            "memories_refiled": store.refile_nested_transcript_projects()?
        }))
    })
    .await
}

pub async fn restore_explicit(State(state): State<Arc<AppState>>) -> Response {
    run(state, |store| store.restore_explicit_memories()).await
}

pub async fn refile_stray_projects(State(state): State<Arc<AppState>>) -> Response {
    run(state, |store| store.refile_stray_project_slugs()).await
}

#[derive(Deserialize)]
pub struct SessionBackfillRequest {
    #[serde(default)]
    dry_run: bool,
    #[serde(default)]
    reemit: bool,
}

/// Reads this machine's transcripts, so it only finds evidence on the
/// machine that captured the sessions; peers receive the dates by sync.
pub async fn backfill_session_at(
    State(state): State<Arc<AppState>>,
    Json(req): Json<SessionBackfillRequest>,
) -> Response {
    run(state, move |store| {
        store.backfill_session_at(&TranscriptEvidence::default(), req.dry_run, req.reemit)
    })
    .await
}