Skip to main content

second_brain_api/
lib.rs

1pub mod associate;
2pub mod eval;
3pub mod heap;
4pub mod ingest;
5pub mod maintenance;
6pub mod mentions;
7pub mod repairs;
8pub mod routes;
9pub mod watcher;
10
11use std::sync::{Arc, RwLock};
12
13use second_brain_core::embedding::Embedder;
14use second_brain_core::index::Indexes;
15use second_brain_core::machine::MachineIdentity;
16use second_brain_core::maintenance::MaintenanceSummary;
17use second_brain_core::retrieval::{Reranker, RetrievalTunables};
18use second_brain_core::sqlite_store::SqliteStore;
19use second_brain_core::store::StoreAdmin;
20
21pub struct AppState {
22    pub store: Arc<dyn StoreAdmin>,
23    /// Same store as `store`: Retriever::candidates and the outbox drain need
24    /// the concrete SqliteStore, which the trait object cannot provide.
25    pub sqlite: Arc<SqliteStore>,
26    /// A read-only `Indexes` handle shared by every recall call; searches take
27    /// the `RwLock` read side, which does not block other readers. The drain
28    /// loop owns a separate read-write `Indexes` and only reaches into this
29    /// lock's write side briefly, to call `reload_view` after persisting.
30    /// None when index open/rebuild failed; recall then uses the legacy engine.
31    pub indexes: Option<Arc<RwLock<Indexes>>>,
32    pub reranker: Option<Arc<Reranker>>,
33    pub tunables: RetrievalTunables,
34    pub embedder: Option<Arc<Embedder>>,
35    pub machine: MachineIdentity,
36    /// The most recent repair, scheduled or requested; `/v1/status` reports
37    /// it without recomputing anything.
38    pub maintenance: Arc<RwLock<Option<MaintenanceSummary>>>,
39    /// Hands a repair request to the drain thread, the one owner of the
40    /// writable indexes, so an on-demand repair never races the scheduled
41    /// one. None when the indexes failed to open and no drain thread exists;
42    /// the repair route then runs against the store alone.
43    pub repair_tx: Option<std::sync::mpsc::Sender<maintenance::RepairRequest>>,
44}
45
46/// Extract the entity names a query refers to, using the same gazetteer
47/// extraction the ingest path applies to transcripts. Matching extractors on
48/// both sides is what lets query entities resolve to stored entity ids, so the
49/// graph term in recall is conditioned on the query rather than raw
50/// connectedness. Shared by the live recall route and the eval harness so both
51/// measure the same ranking.
52pub fn link_query_entities(text: &str) -> Vec<String> {
53    second_brain_adapters::claude_code::extract_entities(text)
54        .into_iter()
55        .map(|e| e.name)
56        .collect()
57}