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