Skip to main content

second_brain_api/
lib.rs

1pub mod alert;
2pub mod associate;
3pub mod auth;
4pub mod eval;
5pub mod file_modes;
6pub mod health;
7pub mod heap;
8pub mod ingest;
9pub mod logging;
10pub mod maintenance;
11pub mod mentions;
12pub mod near_duplicates;
13pub mod progress;
14pub mod repairs;
15pub mod routes;
16pub mod store_compaction;
17pub mod stored_redaction;
18pub mod watcher;
19pub mod write_gate;
20pub mod write_queue;
21
22use std::sync::{Arc, RwLock};
23
24use axum::extract::DefaultBodyLimit;
25use axum::{Router, middleware};
26use tower_http::trace::TraceLayer;
27
28use second_brain_core::embedding::Embedder;
29use second_brain_core::index::Indexes;
30use second_brain_core::machine::MachineIdentity;
31use second_brain_core::maintenance::MaintenanceSummary;
32use second_brain_core::retrieval::{Reranker, RetrievalTunables};
33use second_brain_core::sqlite_store::SqliteStore;
34use second_brain_core::store::StoreAdmin;
35
36pub struct AppState {
37    pub store: Arc<dyn StoreAdmin>,
38    /// Same store as `store`: Retriever::candidates and the outbox drain need
39    /// the concrete SqliteStore, which the trait object cannot provide.
40    pub sqlite: Arc<SqliteStore>,
41    /// A read-only `Indexes` handle shared by every recall call; searches take
42    /// the `RwLock` read side, which does not block other readers. The drain
43    /// loop owns a separate read-write `Indexes` and only reaches into this
44    /// lock's write side briefly, to call `reload_view` after persisting.
45    /// None when index open/rebuild failed; recall then uses the legacy engine.
46    pub indexes: Option<Arc<RwLock<Indexes>>>,
47    pub reranker: Option<Arc<Reranker>>,
48    pub tunables: RetrievalTunables,
49    pub embedder: Option<Arc<Embedder>>,
50    pub machine: MachineIdentity,
51    /// The most recent repair, scheduled or requested; `/v1/status` reports
52    /// it without recomputing anything.
53    pub maintenance: Arc<RwLock<Option<MaintenanceSummary>>>,
54    /// Hands a repair request to the drain thread, the one owner of the
55    /// writable indexes, so an on-demand repair never races the scheduled
56    /// one. None when the indexes failed to open and no drain thread exists;
57    /// the repair route then runs against the store alone.
58    pub repair_tx: Option<std::sync::mpsc::Sender<maintenance::DrainRequest>>,
59    /// `[distill] near_duplicate_threshold`: the default for the
60    /// near-duplicates route and the threshold status counts at.
61    pub near_duplicate_threshold: f32,
62    /// The last near-duplicate count at `near_duplicate_threshold`, from a
63    /// listing or a repair. Counting is a grouping pass over every live
64    /// distillate (seconds on the Pi), so status reports this instead.
65    pub near_duplicates: Arc<RwLock<Option<near_duplicates::NearDupCount>>>,
66    pub health: Arc<health::Health>,
67    pub gate: Arc<write_gate::WriteGate>,
68}
69
70pub fn app(state: Arc<AppState>, api_token: String) -> Router {
71    Router::new()
72        .merge(routes::router())
73        .layer(middleware::from_fn_with_state(
74            state.clone(),
75            write_gate::hold_writes,
76        ))
77        .layer(DefaultBodyLimit::max(64 * 1024 * 1024))
78        .layer(middleware::from_fn_with_state(
79            Arc::<str>::from(api_token),
80            auth::require_token,
81        ))
82        .layer(TraceLayer::new_for_http())
83        .with_state(state)
84}
85
86/// Extract the entity names a query refers to, using the same gazetteer
87/// extraction the ingest path applies to transcripts. Matching extractors on
88/// both sides is what lets query entities resolve to stored entity ids, so the
89/// graph term in recall is conditioned on the query rather than raw
90/// connectedness. Shared by the live recall route and the eval harness so both
91/// measure the same ranking.
92pub fn link_query_entities(text: &str) -> Vec<String> {
93    second_brain_adapters::claude_code::extract_entities(text)
94        .into_iter()
95        .map(|e| e.name)
96        .collect()
97}