second-brain-api 0.13.5

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
use std::sync::Arc;

use axum::Json;
use axum::extract::{Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use second_brain_core::redact::redact_for_store;
use second_brain_core::schema::{Memory, MemoryType, Relation, RelationType};
use second_brain_core::session_dates::latest_session;
use serde::Deserialize;
use uuid::Uuid;

use crate::AppState;

#[derive(Deserialize)]
pub(super) struct ConsolidateBatchParams {
    limit: Option<usize>,
}

pub(super) async fn consolidate_batch(
    State(state): State<Arc<AppState>>,
    Query(params): Query<ConsolidateBatchParams>,
) -> impl IntoResponse {
    let store = state.store.clone();
    let limit = params.limit.unwrap_or(50);

    let result = tokio::task::spawn_blocking(move || -> anyhow::Result<serde_json::Value> {
        let total = store.memory_count()?;
        let consolidation = store.consolidation_stats()?;
        let memories = store.unconsolidated_memories(limit)?;
        let mem_json: Vec<serde_json::Value> = memories
            .iter()
            .map(|m| {
                serde_json::json!({
                    "id": m.id.to_string(),
                    "content": m.content,
                    "memory_type": format!("{:?}", m.memory_type).to_lowercase(),
                    "source": m.source,
                    "source_id": m.source_id,
                    "created_at": m.created_at.to_rfc3339(),
                    "project_path": m.project_path,
                })
            })
            .collect();
        Ok(serde_json::json!({
            "total": total,
            "consolidation": consolidation,
            "memories": mem_json,
        }))
    })
    .await;

    match result {
        Ok(Ok(json)) => Json(json).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
    }
}

#[derive(Deserialize)]
pub(super) struct ConsolidateStoreRequest {
    raw_ids: Vec<String>,
    /// The group's shared source_id (they were grouped by it), reused as the
    /// distilled memory's own source_id so a future project_path backfill
    /// can still resolve it once its raws are gone.
    source_id: String,
    #[serde(default)]
    project_path: Option<String>,
    distilled_content: String,
    model: String,
    #[serde(default)]
    skip: bool,
}

pub(super) async fn consolidate_store(
    State(state): State<Arc<AppState>>,
    Json(req): Json<ConsolidateStoreRequest>,
) -> impl IntoResponse {
    let store = state.store.clone();
    let embedder = state.embedder.clone();

    let result = tokio::task::spawn_blocking(move || -> anyhow::Result<serde_json::Value> {
        let raw_ids: Vec<Uuid> = req
            .raw_ids
            .iter()
            .map(|s| Uuid::parse_str(s))
            .collect::<Result<_, _>>()?;

        if req.skip {
            for raw_id in &raw_ids {
                store.mark_consolidated(*raw_id, *raw_id, &req.model)?;
            }
            return Ok(serde_json::json!({"status": "skipped", "count": raw_ids.len()}));
        }

        let distilled_content = redact_for_store(&req.distilled_content);
        let mut distilled = Memory::new(
            distilled_content.clone(),
            MemoryType::Semantic,
            "consolidation".to_string(),
            req.source_id.clone(),
        );
        distilled.project_path = req.project_path.clone();
        let raws = raw_ids
            .iter()
            .filter_map(|id| store.get_memory(*id).transpose())
            .collect::<anyhow::Result<Vec<Memory>>>()?;
        distilled.session_at = latest_session(&raws);

        if let Some(ref emb) = embedder
            && let Ok(embedding) = emb.embed(&distilled_content)
        {
            distilled.embedding = embedding;
        }

        store.store_memory(&distilled)?;

        for raw_id in &raw_ids {
            let link = Relation {
                from_id: distilled.id,
                to_id: *raw_id,
                relation_type: RelationType::DistilledFrom,
                strength: 1.0,
                context: Some(req.model.clone()),
            };
            store.store_relation(&link).ok();
            store.mark_consolidated(*raw_id, distilled.id, &req.model)?;
        }

        Ok(serde_json::json!({
            "status": "stored",
            "distilled_id": distilled.id.to_string(),
            "raw_count": raw_ids.len(),
        }))
    })
    .await;

    match result {
        Ok(Ok(json)) => Json(json).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
    }
}

pub(super) async fn consolidate_cleanup(State(state): State<Arc<AppState>>) -> impl IntoResponse {
    let store = state.store.clone();
    let result = tokio::task::spawn_blocking(move || store.retire_consolidated_raws()).await;
    match result {
        // `deleted` stays, always zero, because hearth's consolidate job on the
        // Pi reads that key and must not fail on a daemon that no longer deletes.
        Ok(Ok(count)) => Json(serde_json::json!({"archived": count, "deleted": 0})).into_response(),
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
    }
}

#[derive(Deserialize)]
pub(super) struct DailyMemoriesRequest {
    date: String,
}

pub(super) async fn daily_memories(
    State(state): State<Arc<AppState>>,
    Json(req): Json<DailyMemoriesRequest>,
) -> impl IntoResponse {
    let date = match chrono::NaiveDate::parse_from_str(&req.date, "%Y-%m-%d") {
        Ok(d) => d,
        Err(_) => {
            return (
                StatusCode::BAD_REQUEST,
                Json(serde_json::json!({"error": "invalid date, expected YYYY-MM-DD"})),
            )
                .into_response();
        }
    };

    let day_start = date.and_hms_opt(0, 0, 0).unwrap().and_utc();
    let day_end = (date + chrono::Duration::days(1))
        .and_hms_opt(0, 0, 0)
        .unwrap()
        .and_utc();

    let store = state.store.clone();
    let result =
        tokio::task::spawn_blocking(move || store.memories_created_between(&day_start, &day_end))
            .await;

    match result {
        Ok(Ok(memories)) => {
            let count = memories.len();
            let items: Vec<serde_json::Value> = memories
                .into_iter()
                .map(|m| {
                    serde_json::json!({
                        "id": m.id.to_string(),
                        "content": m.content,
                        "memory_type": format!("{:?}", m.memory_type).to_lowercase(),
                        "confidence": m.confidence,
                        "created_at": m.created_at.to_rfc3339(),
                        "source": m.source,
                    })
                })
                .collect();
            Json(serde_json::json!({"memories": items, "count": count})).into_response()
        }
        Ok(Err(e)) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
        Err(e) => (
            StatusCode::INTERNAL_SERVER_ERROR,
            Json(serde_json::json!({"error": e.to_string()})),
        )
            .into_response(),
    }
}