Skip to main content

algocline_app/service/
mod.rs

1mod config;
2mod eval;
3mod eval_store;
4mod logging;
5pub(crate) mod path;
6mod pkg;
7pub(crate) mod resolve;
8mod run;
9mod scenario;
10mod transcript;
11
12#[cfg(test)]
13mod tests;
14
15use std::path::Path;
16use std::sync::Arc;
17
18use algocline_engine::{Executor, SessionRegistry};
19
20pub use config::{AppConfig, LogDirSource};
21pub use resolve::QueryResponse;
22
23// ─── Application Service ────────────────────────────────────────
24
25/// Tracks which sessions are eval sessions and their strategy name.
26type EvalSessions = std::sync::Mutex<std::collections::HashMap<String, String>>;
27
28/// Tracks session_id → strategy name for all strategy-based sessions (advice, eval).
29type SessionStrategies = std::sync::Mutex<std::collections::HashMap<String, String>>;
30
31#[derive(Clone)]
32pub struct AppService {
33    executor: Arc<Executor>,
34    registry: Arc<SessionRegistry>,
35    log_config: AppConfig,
36    /// session_id → strategy name for eval sessions (cleared on completion).
37    eval_sessions: Arc<EvalSessions>,
38    /// session_id → strategy name for log/stats tracking (cleared on session completion).
39    session_strategies: Arc<SessionStrategies>,
40}
41
42impl AppService {
43    pub fn new(executor: Arc<Executor>, log_config: AppConfig) -> Self {
44        Self {
45            executor,
46            registry: Arc::new(SessionRegistry::new()),
47            log_config,
48            eval_sessions: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
49            session_strategies: Arc::new(std::sync::Mutex::new(std::collections::HashMap::new())),
50        }
51    }
52
53    /// Returns the log directory, or an error if file logging is unavailable.
54    fn require_log_dir(&self) -> Result<&Path, String> {
55        self.log_config
56            .log_dir
57            .as_deref()
58            .ok_or_else(|| "File logging is not available (no writable log directory)".to_string())
59    }
60}