second-brain-api 0.9.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::{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>) {
    let machine = MachineIdentity {
        id: "smoke-machine".to_string(),
        name: "smoke".to_string(),
    };
    let store = SqliteStore::in_memory(machine.id.clone()).unwrap();
    store.register_machine(&machine).unwrap();
    let sqlite = Arc::new(store);
    let state = Arc::new(AppState {
        store: sqlite.clone(),
        sqlite,
        indexes: None,
        reranker: None,
        tunables: RetrievalTunables::default(),
        embedder: None,
        machine,
        maintenance: Arc::new(RwLock::new(None)),
        repair_tx: None,
    });
    let store = state.sqlite.clone();
    (routes::router().with_state(state), store)
}

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()
}