1use crate::agent;
2use crate::agent::conversation::{ConversationManager, UserTurn};
3use crate::agent::git_monitor::GitState;
4use crate::agent::inference::{InferenceEngine, InferenceEvent};
5use crate::ui;
6use crate::ui::gpu_monitor::GpuState;
7use crate::ui::voice::VoiceManager;
8use crate::CliCockpit;
9use notify::RecommendedWatcher;
10use std::sync::Arc;
11use tokio::sync::mpsc;
12
13pub struct RuntimeServices {
14 pub engine: Arc<InferenceEngine>,
15 pub gpu_state: Arc<GpuState>,
16 pub git_state: Arc<GitState>,
17 pub voice_manager: Arc<VoiceManager>,
18 pub swarm_coordinator: Arc<agent::swarm::SwarmCoordinator>,
19 pub cancel_token: Arc<std::sync::atomic::AtomicBool>,
20}
21
22pub struct RuntimeChannels {
23 pub specular_rx: mpsc::Receiver<agent::specular::SpecularEvent>,
24 pub agent_tx: mpsc::Sender<InferenceEvent>,
25 pub agent_rx: mpsc::Receiver<InferenceEvent>,
26 pub swarm_tx: mpsc::Sender<agent::swarm::SwarmMessage>,
27 pub swarm_rx: mpsc::Receiver<agent::swarm::SwarmMessage>,
28 pub user_input_tx: mpsc::Sender<UserTurn>,
29 pub user_input_rx: mpsc::Receiver<UserTurn>,
30}
31
32pub struct RuntimeBundle {
33 pub services: RuntimeServices,
34 pub channels: RuntimeChannels,
35 pub watcher_guard: RecommendedWatcher,
36}
37
38pub struct AgentLoopRuntime {
39 pub user_input_rx: mpsc::Receiver<UserTurn>,
40 pub agent_tx: mpsc::Sender<InferenceEvent>,
41 pub services: RuntimeServices,
42}
43
44pub struct AgentLoopConfig {
45 pub yolo: bool,
46 pub professional: bool,
47 pub brief: bool,
48 pub snark: u8,
49 pub chaos: u8,
50 pub soul_personality: String,
51 pub fast_model: Option<String>,
52 pub think_model: Option<String>,
53}
54
55pub async fn build_runtime_bundle(
56 cockpit: &CliCockpit,
57 species: &str,
58 snark: u8,
59 professional: bool,
60) -> Result<RuntimeBundle, Box<dyn std::error::Error>> {
61 println!("Booting Hematite systems...");
62 let api_url = crate::agent::config::load_config()
64 .api_url
65 .unwrap_or_else(|| cockpit.url.clone());
66 let mut engine_raw = InferenceEngine::new(api_url, species.to_string(), snark)?;
67 let gpu_state = ui::gpu_monitor::spawn_gpu_monitor();
68 let git_state = agent::git_monitor::spawn_git_monitor();
69
70 if !engine_raw.health_check().await {
71 println!(
72 "ERROR: LLM Provider not detected at {}",
73 engine_raw.base_url
74 );
75 println!("Check if LM Studio (or your local server) is running and port mapped correctly.");
76 std::process::exit(1);
77 }
78
79 let model_name = engine_raw.get_loaded_model().await;
80 if let Some(name) = model_name {
81 engine_raw.set_runtime_profile(&name, engine_raw.current_context_length());
82 }
83 let detected_context = engine_raw.detect_context_length().await;
84 let detected_model = engine_raw.current_model();
85 engine_raw.set_runtime_profile(&detected_model, detected_context);
86
87 let (specular_tx, specular_rx) = mpsc::channel(32);
88 let watcher_guard = agent::specular::spawn_watcher(specular_tx)?;
89
90 let (agent_tx, agent_rx) = mpsc::channel::<InferenceEvent>(100);
91 let (swarm_tx, swarm_rx) = mpsc::channel(32);
92 let voice_manager = Arc::new(VoiceManager::new(agent_tx.clone()));
93
94 if let Some(ref worker) = cockpit.fast_model {
95 engine_raw.worker_model = Some(worker.clone());
96 }
97
98 let engine = Arc::new(engine_raw);
99 let swarm_coordinator = Arc::new(agent::swarm::SwarmCoordinator::new(
100 engine.clone(),
101 gpu_state.clone(),
102 cockpit.fast_model.clone(),
103 professional,
104 ));
105
106 let (user_input_tx, user_input_rx) = mpsc::channel::<UserTurn>(32);
107 let cancel_token = Arc::new(std::sync::atomic::AtomicBool::new(false));
108
109 Ok(RuntimeBundle {
110 services: RuntimeServices {
111 engine,
112 gpu_state,
113 git_state,
114 voice_manager,
115 swarm_coordinator,
116 cancel_token,
117 },
118 channels: RuntimeChannels {
119 specular_rx,
120 agent_tx,
121 agent_rx,
122 swarm_tx,
123 swarm_rx,
124 user_input_tx,
125 user_input_rx,
126 },
127 watcher_guard,
128 })
129}
130
131pub fn spawn_runtime_profile_sync(
132 engine: Arc<InferenceEngine>,
133 agent_tx: mpsc::Sender<InferenceEvent>,
134) -> tokio::task::JoinHandle<()> {
135 tokio::spawn(async move {
136 tokio::time::sleep(tokio::time::Duration::from_secs(4)).await;
138
139 let mut last_embed: Option<String> = None;
140
141 loop {
142 let result = engine.refresh_runtime_profile().await;
143
144 let Some((model_id, context_length, _changed)) = result else {
145 if agent_tx.is_closed() {
146 break;
147 }
148 tokio::time::sleep(tokio::time::Duration::from_secs(15)).await;
150 continue;
151 };
152
153 let poll_interval = if model_id == "no model loaded" {
155 tokio::time::Duration::from_secs(12)
156 } else {
157 tokio::time::Duration::from_secs(4)
158 };
159
160 if agent_tx
161 .send(InferenceEvent::RuntimeProfile {
162 model_id,
163 context_length,
164 })
165 .await
166 .is_err()
167 {
168 break;
169 }
170
171 let current_embed = engine.get_embedding_model().await;
173 if current_embed != last_embed {
174 if agent_tx
175 .send(InferenceEvent::EmbedProfile {
176 model_id: current_embed.clone(),
177 })
178 .await
179 .is_err()
180 {
181 break;
182 }
183 last_embed = current_embed;
184 }
185
186 tokio::time::sleep(poll_interval).await;
187 }
188 })
189}
190
191pub async fn run_agent_loop(runtime: AgentLoopRuntime, config: AgentLoopConfig) {
192 let AgentLoopRuntime {
193 mut user_input_rx,
194 agent_tx,
195 services,
196 } = runtime;
197 let RuntimeServices {
198 engine,
199 gpu_state,
200 git_state,
201 voice_manager,
202 swarm_coordinator,
203 cancel_token,
204 } = services;
205
206 let mut manager = ConversationManager::new(
207 engine,
208 config.professional,
209 config.brief,
210 config.snark,
211 config.chaos,
212 config.soul_personality,
213 config.fast_model,
214 config.think_model,
215 gpu_state.clone(),
216 git_state,
217 swarm_coordinator,
218 voice_manager,
219 );
220 manager.cancel_token = cancel_token;
221
222 let _ = agent_tx
223 .send(InferenceEvent::RuntimeProfile {
224 model_id: manager.engine.current_model(),
225 context_length: manager.engine.current_context_length(),
226 })
227 .await;
228
229 let workspace_root = crate::tools::file_ops::workspace_root();
230 let _ = crate::agent::workspace_profile::ensure_workspace_profile(&workspace_root);
231
232 let gpu_name = gpu_state.gpu_name();
235 let vram = gpu_state.label();
236 let voice_cfg = crate::agent::config::load_config();
237 let voice_status = format!(
238 "Voice: {} | Speed: {}x | Volume: {}x",
239 crate::agent::config::effective_voice(&voice_cfg),
240 crate::agent::config::effective_voice_speed(&voice_cfg),
241 crate::agent::config::effective_voice_volume(&voice_cfg),
242 );
243 let embed_status = match manager.engine.get_embedding_model().await {
244 Some(id) => format!("Embed: {} (semantic search ready)", id),
245 None => "Embed: none loaded (load nomic-embed-text-v2 for semantic search)".to_string(),
246 };
247 let workspace_root = crate::tools::file_ops::workspace_root();
248 let docs_only_mode = !crate::tools::file_ops::is_project_workspace();
249 let workspace_mode = if docs_only_mode {
250 "docs-only"
251 } else {
252 "project"
253 };
254 let launched_from_home = home::home_dir()
255 .and_then(|home| std::env::current_dir().ok().map(|cwd| cwd == home))
256 .unwrap_or(false);
257 let project_hint = if !docs_only_mode {
258 String::new()
259 } else if launched_from_home {
260 "\nTip: you launched Hematite from your home directory. That is fine for workstation questions and docs-only memory, but for project-specific build, test, script, or repo work you should relaunch in the target project directory. `.hematite/docs/`, `.hematite/imports/`, and recent local session reports remain searchable in docs-only vein mode.".to_string()
261 } else {
262 "\nTip: source indexing is disabled outside a project folder. Launch Hematite in the target project directory for project-specific build, test, script, or repo work. `.hematite/docs/`, `.hematite/imports/`, and recent local session reports remain searchable in docs-only vein mode.".to_string()
263 };
264 let display_model = {
265 let m = manager.engine.current_model();
266 if m.is_empty() {
267 "no chat model loaded".to_string()
268 } else {
269 m
270 }
271 };
272 let greeting = format!(
273 "Hematite {} Online | Model: {} | CTX: {} | GPU: {} | VRAM: {}\nEndpoint: {}\nWorkspace: {} ({})\n{}\n{}\n/ask · read-only analysis /code · implement /architect · plan-first /chat · conversation\nRecovery: /undo · /new · /forget · /clear | /version · /about{}",
274 crate::hematite_version_display(),
275 display_model,
276 manager.engine.current_context_length(),
277 gpu_name,
278 vram,
279 format!("{}/v1", manager.engine.base_url),
280 workspace_root.display(),
281 workspace_mode,
282 embed_status,
283 voice_status,
284 project_hint
285 );
286 let _ = agent_tx
287 .send(InferenceEvent::MutedToken(format!("\n{}", greeting)))
288 .await;
289
290 if let Err(e) = manager.initialize_mcp(&agent_tx).await {
291 let _ = agent_tx
292 .send(InferenceEvent::Error(format!("MCP Init Failed: {}", e)))
293 .await;
294 }
295 let indexed = manager.initialize_vein();
296 manager.initialize_repo_map();
297 let _ = agent_tx
298 .send(InferenceEvent::VeinStatus {
299 file_count: manager.vein.file_count(),
300 embedded_count: manager.vein.embedded_chunk_count(),
301 docs_only: docs_only_mode,
302 })
303 .await;
304 let _ = agent_tx
305 .send(InferenceEvent::Thought(format!(
306 "The Vein: indexed {} files",
307 indexed
308 )))
309 .await;
310
311 if let Some(cp) = crate::agent::conversation::load_checkpoint() {
313 let verify_tag = match cp.last_verify_ok {
314 Some(true) => " | last verify: PASS",
315 Some(false) => " | last verify: FAIL",
316 None => "",
317 };
318 let files_tag = if cp.working_files.is_empty() {
319 String::new()
320 } else {
321 format!(" | files: {}", cp.working_files.join(", "))
322 };
323 let goal_preview: String = cp.last_goal.chars().take(120).collect();
324 let trail = if cp.last_goal.len() > 120 { "…" } else { "" };
325 let resume_msg = format!(
326 "Resumed: {} turn{}{}{} — last goal: \"{}{}\"",
327 cp.turn_count,
328 if cp.turn_count == 1 { "" } else { "s" },
329 verify_tag,
330 files_tag,
331 goal_preview,
332 trail,
333 );
334 let _ = agent_tx.send(InferenceEvent::Thought(resume_msg)).await;
335 } else {
336 let session_path = crate::tools::file_ops::workspace_root()
337 .join(".hematite")
338 .join("session.json");
339 if !session_path.exists() {
340 let first_run_msg = "\nWelcome to Hematite! I'm your local AI workstation assistant.\n\n\
341 Since this is your first time here, what would you like to do?\n\
342 - System Check: Wondering if your tools are working? Run `/health`\n\
343 - Code: Ready to build something? Run `/architect Let's build a new feature`\n\
344 - Setup: Need help configuring Git or the workspace? Run `/ask What should I set up first?`\n\
345 - Help: Have a weird error? Type `/explain ` and paste it.\n\n\
346 Just type \"hello\" to start a normal conversation!".to_string();
347 let _ = agent_tx.send(InferenceEvent::Thought(first_run_msg)).await;
348
349 let _ = std::fs::write(&session_path, "{\"turn_count\": 0}");
351 }
352 }
353
354 let _ = agent_tx.send(InferenceEvent::Done).await;
355 let startup_config = crate::agent::config::load_config();
356 manager.engine.set_gemma_native_formatting(
357 crate::agent::config::effective_gemma_native_formatting(
358 &startup_config,
359 &manager.engine.current_model(),
360 ),
361 );
362 let startup_model = manager.engine.current_model();
363 if crate::agent::inference::is_gemma4_model_name(&startup_model) {
364 let mode = crate::agent::config::gemma_native_mode_label(&startup_config, &startup_model);
365 let status = match mode {
366 "on" => "Gemma 4 detected | Gemma Native Formatting: ON (forced)",
367 "auto" => "Gemma 4 detected | Gemma Native Formatting: ON (auto)",
368 _ => "Gemma 4 detected | Gemma Native Formatting: OFF (use /gemma-native auto|on)",
369 };
370 let _ = agent_tx
371 .send(InferenceEvent::MutedToken(status.to_string()))
372 .await;
373 }
374
375 while let Some(input) = user_input_rx.recv().await {
376 if let Err(e) = manager
377 .run_turn(&input, agent_tx.clone(), config.yolo)
378 .await
379 {
380 let _ = agent_tx.send(InferenceEvent::Error(e.to_string())).await;
381 let _ = agent_tx.send(InferenceEvent::Done).await;
382 }
383 }
384}