second-brain-api 0.11.0

HTTP API server for second-brain: REST endpoints for recall, remember, and ingest
Documentation
#![allow(dead_code)]

use std::sync::{Arc, RwLock};

use axum::body::Body;
use axum::http::{Request, header};
use http_body_util::BodyExt;

use second_brain_api::health::Health;
use second_brain_api::write_gate::WriteGate;
use second_brain_api::{AppState, routes};
use second_brain_core::machine::MachineIdentity;
use second_brain_core::retrieval::RetrievalTunables;
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_core::store::StoreAdmin;

pub fn app() -> axum::Router {
    app_with_store().0
}

pub fn app_with_store() -> (axum::Router, Arc<SqliteStore>) {
    app_with_parts(smoke_store(), fresh_health())
}

pub fn app_with_sqlite(store: SqliteStore) -> (axum::Router, Arc<SqliteStore>) {
    app_with_parts(store, fresh_health())
}

pub fn app_with_health(health: Arc<Health>) -> (axum::Router, Arc<SqliteStore>) {
    app_with_parts(smoke_store(), health)
}

pub fn state() -> Arc<AppState> {
    state_with(smoke_store(), fresh_health())
}

/// A state whose write gate queues into `queue_dir` during maintenance.
pub fn state_with_queue(queue_dir: std::path::PathBuf) -> Arc<AppState> {
    state_with_gate(smoke_store(), fresh_health(), WriteGate::new(queue_dir))
}

fn smoke_store() -> SqliteStore {
    SqliteStore::in_memory("smoke-machine".to_string()).unwrap()
}

fn fresh_health() -> Arc<Health> {
    Arc::new(Health::new(chrono::Utc::now(), None))
}

fn app_with_parts(store: SqliteStore, health: Arc<Health>) -> (axum::Router, Arc<SqliteStore>) {
    let state = state_with(store, health);
    let store = state.sqlite.clone();
    (routes::router().with_state(state), store)
}

fn state_with(store: SqliteStore, health: Arc<Health>) -> Arc<AppState> {
    state_with_gate(store, health, WriteGate::default())
}

fn state_with_gate(store: SqliteStore, health: Arc<Health>, gate: WriteGate) -> Arc<AppState> {
    let machine = MachineIdentity {
        id: store.machine_id().to_string(),
        name: "smoke".to_string(),
    };
    store.register_machine(&machine).unwrap();
    let sqlite = Arc::new(store);
    Arc::new(AppState {
        gate: Arc::new(gate),
        store: sqlite.clone(),
        sqlite,
        indexes: None,
        reranker: None,
        tunables: RetrievalTunables::default(),
        embedder: None,
        machine,
        maintenance: Arc::new(RwLock::new(None)),
        repair_tx: None,
        near_duplicate_threshold: 0.97,
        near_duplicates: std::sync::Arc::new(std::sync::RwLock::new(None)),
        health,
    })
}

pub async fn json_body(response: axum::response::Response) -> serde_json::Value {
    let bytes = response.into_body().collect().await.unwrap().to_bytes();
    serde_json::from_slice(&bytes).unwrap()
}

pub fn post_json(uri: &str, body: serde_json::Value) -> Request<Body> {
    Request::builder()
        .method("POST")
        .uri(uri)
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(body.to_string()))
        .unwrap()
}

pub fn get(uri: &str) -> Request<Body> {
    Request::builder().uri(uri).body(Body::empty()).unwrap()
}

pub fn delete(uri: &str) -> Request<Body> {
    Request::builder()
        .method("DELETE")
        .uri(uri)
        .body(Body::empty())
        .unwrap()
}