Skip to main content

algocline_app/service/
status.rs

1use super::AppService;
2
3impl AppService {
4    /// Snapshot of all active sessions for external observation.
5    ///
6    /// Returns JSON with session status, metrics, progress, and strategy name.
7    /// Only includes sessions currently held in the registry (paused, awaiting
8    /// host LLM responses). Completed sessions are not listed here — use
9    /// `alc_log_view` for historical data.
10    pub async fn status(&self, session_id: Option<&str>) -> Result<String, String> {
11        let snapshots = self.registry.list_snapshots().await;
12
13        // If a specific session requested, return just that one
14        if let Some(sid) = session_id {
15            return match snapshots.get(sid) {
16                Some(snapshot) => {
17                    let mut result = snapshot.clone();
18                    // Enrich with strategy name
19                    if let Ok(strategies) = self.session_strategies.lock() {
20                        if let Some(name) = strategies.get(sid) {
21                            result["strategy"] = serde_json::json!(name);
22                        }
23                    }
24                    result["session_id"] = serde_json::json!(sid);
25                    serde_json::to_string_pretty(&result).map_err(|e| e.to_string())
26                }
27                None => Err(format!("session '{sid}' not found (may have completed)")),
28            };
29        }
30
31        // List all active sessions
32        if snapshots.is_empty() {
33            return Ok(serde_json::json!({
34                "active_sessions": 0,
35                "sessions": [],
36            })
37            .to_string());
38        }
39
40        let strategies = self.session_strategies.lock().ok();
41        let sessions: Vec<serde_json::Value> = snapshots
42            .into_iter()
43            .map(|(id, mut snapshot)| {
44                if let Some(ref strats) = strategies {
45                    if let Some(name) = strats.get(&id) {
46                        snapshot["strategy"] = serde_json::json!(name);
47                    }
48                }
49                snapshot["session_id"] = serde_json::json!(id);
50                snapshot
51            })
52            .collect();
53
54        let result = serde_json::json!({
55            "active_sessions": sessions.len(),
56            "sessions": sessions,
57        });
58
59        serde_json::to_string_pretty(&result).map_err(|e| e.to_string())
60    }
61}