qualia_core_db/inference/inference_agent/
local_agent.rs1use crate::q_hash;
7
8use super::types::{default_local_modality, AgentBackend};
9
10pub struct LocalLlmAgent {
14 #[cfg(not(target_arch = "wasm32"))]
15 pub(super) native_request:
16 std::sync::Mutex<Option<super::native_request::NativeGovernedRequest>>,
17 pub agent_did: String,
18 pub backend: AgentBackend,
19 pub memory_used_bytes: std::sync::atomic::AtomicU64,
20 pub(super) use_sieve_output: std::sync::atomic::AtomicBool,
26 pub(super) sieve_lex_path: std::sync::Mutex<Option<String>>,
28 pub(super) sieve_spec: std::sync::Mutex<crate::neuro_symbolic_sieve::SieveLexSpec>,
30 pub(super) lora_manager: std::sync::Mutex<Option<crate::lora::LoRAAdapterManager>>,
35}
36
37impl LocalLlmAgent {
38 pub fn new(agent_did: impl Into<String>, model_path: impl Into<String>) -> Self {
40 Self::with_local_backend(
41 agent_did,
42 AgentBackend::Local {
43 model_path: model_path.into(),
44 context_window: 4096,
45 quantization: "Q4_K_M".into(),
46 vision_projector_path: None,
47 modality: default_local_modality(),
48 architecture: None,
49 },
50 )
51 }
52
53 pub fn with_local_backend(agent_did: impl Into<String>, backend: AgentBackend) -> Self {
55 Self {
56 #[cfg(not(target_arch = "wasm32"))]
57 native_request: std::sync::Mutex::new(None),
58 agent_did: agent_did.into(),
59 backend,
60 memory_used_bytes: std::sync::atomic::AtomicU64::new(0),
61 use_sieve_output: std::sync::atomic::AtomicBool::new(false),
62 sieve_lex_path: std::sync::Mutex::new(None),
63 sieve_spec: std::sync::Mutex::new(
64 crate::neuro_symbolic_sieve::SieveLexSpec::graph_mutation_default(),
65 ),
66 lora_manager: std::sync::Mutex::new(None),
67 }
68 }
69
70 #[cfg(not(target_arch = "wasm32"))]
73 pub fn bind_native_governed_request(
74 &self,
75 request: super::native_request::NativeGovernedRequest,
76 ) -> Result<(), super::governed::PolicyError> {
77 let mut slot = self
78 .native_request
79 .lock()
80 .map_err(|_| super::governed::PolicyError::Revoked)?;
81 if slot.is_some() {
82 return Err(super::governed::PolicyError::Capacity);
83 }
84 *slot = Some(request);
85 Ok(())
86 }
87
88 pub fn attach_lora_adapters(&self, adapter_dir: impl Into<std::path::PathBuf>) {
96 let mgr = crate::lora::LoRAAdapterManager::new(adapter_dir);
97 *self.lora_manager.lock().unwrap_or_else(|e| e.into_inner()) = Some(mgr);
98 }
99
100 pub fn attach_lora_adapters_with_dims(
104 &self,
105 adapter_dir: impl Into<std::path::PathBuf>,
106 n_in: usize,
107 n_out: usize,
108 ) {
109 let mut mgr = crate::lora::LoRAAdapterManager::new(adapter_dir);
110 mgr.set_expected_dims(n_in, n_out);
111 *self.lora_manager.lock().unwrap_or_else(|e| e.into_inner()) = Some(mgr);
112 }
113
114 pub fn detach_lora_adapters(&self) {
116 *self.lora_manager.lock().unwrap_or_else(|e| e.into_inner()) = None;
117 }
118
119 pub fn warm_lora_for_prompt(&self, prompt: &str) {
124 let mut guard = self.lora_manager.lock().unwrap_or_else(|e| e.into_inner());
125 if let Some(mgr) = guard.as_mut() {
126 let (ctx, conf) = mgr.detector.analyze_text(prompt);
127 if conf >= mgr.detector.confidence_threshold {
128 let _ = mgr.switch_to(ctx);
129 }
130 }
131 }
132
133 pub fn active_lora_context(&self) -> Option<crate::lora::ContextType> {
135 let guard = self.lora_manager.lock().unwrap_or_else(|e| e.into_inner());
136 guard
137 .as_ref()
138 .and_then(|m| m.active())
139 .map(|a| a.context_type)
140 }
141
142 pub fn configure_sieve_lex(&self, path: impl Into<String>) {
144 *self
145 .sieve_lex_path
146 .lock()
147 .unwrap_or_else(|e| e.into_inner()) = Some(path.into());
148 }
149
150 pub fn agent_did_hash(&self) -> u64 {
152 q_hash(&self.agent_did)
153 }
154}