lean_ctx/core/
embedding_access.rs1#[cfg(feature = "embeddings")]
7use crate::core::embeddings::EmbeddingEngine;
8
9#[cfg(feature = "embeddings")]
14pub fn embeddings_auto_download_allowed() -> bool {
15 if let Ok(v) = std::env::var("LEAN_CTX_EMBEDDINGS_AUTO_DOWNLOAD") {
16 return matches!(
17 v.trim().to_lowercase().as_str(),
18 "1" | "true" | "yes" | "on"
19 );
20 }
21 crate::core::config::Config::load()
22 .embedding
23 .auto_download
24 .unwrap_or(true)
25}
26
27#[cfg(feature = "embeddings")]
28pub fn embedding_engine() -> Option<&'static EmbeddingEngine> {
29 embedding_engine_impl(false)
30}
31
32#[cfg(feature = "embeddings")]
36pub fn embedding_engine_nonblocking() -> Option<&'static EmbeddingEngine> {
37 embedding_engine_impl(true)
38}
39
40#[cfg(feature = "embeddings")]
41fn embedding_engine_impl(nonblocking: bool) -> Option<&'static EmbeddingEngine> {
42 let cfg = crate::core::config::Config::load();
43 let profile = crate::core::config::MemoryProfile::effective(&cfg);
44 if !profile.embeddings_enabled() {
45 return None;
46 }
47 if !EmbeddingEngine::is_available() && !embeddings_auto_download_allowed() {
48 return None;
49 }
50 if nonblocking {
51 let engine = crate::core::embeddings::try_shared_engine();
52 if engine.is_none() {
53 ensure_engine_background();
54 }
55 engine
56 } else {
57 crate::core::embeddings::shared_engine()
58 }
59}
60
61#[cfg(feature = "embeddings")]
67fn ensure_engine_background() {
68 use std::sync::atomic::{AtomicBool, Ordering};
69 if !crate::core::embeddings::background_load_allowed() {
77 return;
78 }
79 static STARTED: AtomicBool = AtomicBool::new(false);
80 if STARTED.swap(true, Ordering::SeqCst) {
81 return;
82 }
83 std::thread::spawn(|| {
84 let loaded = crate::core::embeddings::shared_engine().is_some();
88 tracing::info!("embedding engine background activation finished (loaded={loaded})");
89 });
90}
91
92pub fn engine_status_line() -> String {
94 #[cfg(feature = "embeddings")]
95 {
96 let cfg = crate::core::config::Config::load();
97 let profile = crate::core::config::MemoryProfile::effective(&cfg);
98 if !profile.embeddings_enabled() {
99 return "off (memory profile: low)".to_string();
100 }
101 if crate::core::embeddings::try_shared_engine().is_some() {
102 return "loaded".to_string();
103 }
104 if EmbeddingEngine::is_available() {
105 return "model present, engine loads on first use".to_string();
106 }
107 if embeddings_auto_download_allowed() {
108 return "model missing — downloads in background on first semantic need".to_string();
109 }
110 "off (auto-download disabled, no model present)".to_string()
111 }
112 #[cfg(not(feature = "embeddings"))]
113 {
114 "off (binary built without embeddings feature)".to_string()
115 }
116}