Skip to main content

kasl/libs/config/
mod.rs

1//! Application configuration: the data model, disk IO, and PATH setup.
2//! The interactive setup wizard lives in [`wizard`] (reached via
3//! [`Config::init`]).
4//!
5//! ```rust,no_run
6//! # fn main() -> anyhow::Result<()> {
7//! use kasl::libs::config::Config;
8//!
9//! let config = Config::read()?;
10//! let updated_config = Config::init()?;
11//! updated_config.save()?;
12//! # Ok(())
13//! # }
14//! ```
15
16mod wizard;
17
18use super::data_storage::DataStorage;
19use crate::api::gitlab::GitLabConfig;
20use crate::api::jira::JiraConfig;
21use crate::api::si::SiConfig;
22use crate::libs::messages::Message;
23use crate::libs::task::normalize_task_name;
24use crate::msg_error;
25use anyhow::Result;
26use serde::{Deserialize, Serialize};
27use std::env;
28use std::fs;
29use std::path::PathBuf;
30use std::process::Command;
31use std::str;
32
33/// Configuration filename inside the app data directory.
34pub const CONFIG_FILE_NAME: &str = "config.json";
35
36/// A configurable module as listed by the setup wizard.
37#[derive(Debug, Clone)]
38pub struct ConfigModule {
39    /// Internal key used for routing.
40    pub key: String,
41    /// Name shown in the wizard.
42    pub name: String,
43}
44
45/// Activity monitor thresholds and intervals.
46#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
47pub struct MonitorConfig {
48    /// Minimum pause length in minutes to keep as a pause record; shorter
49    /// interruptions are noise, not breaks.
50    pub min_pause_duration: u64,
51
52    /// Seconds without input before a pause is detected.
53    pub pause_threshold: u64,
54
55    /// Milliseconds between activity checks.
56    pub poll_interval: u64,
57
58    /// Seconds of sustained activity required to start a workday - keeps
59    /// a stray mouse nudge from opening the day.
60    pub activity_threshold: u64,
61
62    /// Minimum work interval in minutes; shorter ones are filtered from
63    /// report display.
64    pub min_work_interval: u64,
65
66    /// Maximum gap in seconds between two consecutive pauses to merge them.
67    ///
68    /// When the activity monitor briefly registers a stray input between two
69    /// otherwise continuous inactivity periods, it splits a single break into
70    /// several adjacent pause records. Pauses separated by a gap no longer than
71    /// this value are treated as one continuous pause so that sub-threshold
72    /// segments are not dropped from calculations. The value should stay small
73    /// (a few tens of seconds) so that genuine short work periods between pauses
74    /// are preserved rather than swallowed into the break.
75    #[serde(default = "default_pause_merge_gap")]
76    pub pause_merge_gap: u64,
77}
78
79/// Default gap (in seconds) below which consecutive pauses are merged.
80///
81/// Used both by [`MonitorConfig::default`] and by serde when an existing
82/// configuration file predates the `pause_merge_gap` field.
83fn default_pause_merge_gap() -> u64 {
84    30
85}
86
87/// Productivity thresholds for warnings and report validation.
88#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
89pub struct ProductivityConfig {
90    /// Productivity percentage below which reports warn.
91    pub min_productivity_threshold: f64,
92
93    /// Expected workday length in hours.
94    pub workday_hours: f64,
95
96    /// Fraction of the workday that must pass before warnings appear -
97    /// early-day ratios swing too wildly to act on.
98    pub min_workday_fraction_before_suggest: f64,
99}
100
101/// Report export defaults: output directory and file naming.
102#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
103pub struct ReportConfig {
104    /// Default directory for exports without an explicit `--output`
105    /// (created if missing); unset = timestamped file in the current dir.
106    #[serde(skip_serializing_if = "Option::is_none")]
107    pub output_dir: Option<String>,
108
109    /// File name template (without extension) for generated reports.
110    ///
111    /// Supported placeholders:
112    /// - `{date}` — the report date in `YYYY-MM-DD` format
113    /// - `{seq}`  — a per-day sequence suffix: empty for the first report of the
114    ///   day, then `_2`, `_3`, … for subsequent reports on the same date
115    ///
116    /// The file extension is appended automatically based on the export format.
117    /// Defaults to `daily_report_{date}{seq}` when unset.
118    #[serde(skip_serializing_if = "Option::is_none")]
119    pub filename_template: Option<String>,
120
121    /// Report label language: `en` (default) or `ru`; unknown values fall
122    /// back to `en`.
123    ///
124    /// Russian was the default before 1.0, when the product spoke Russian; the
125    /// shipped default is English and this opts back in.
126    #[serde(skip_serializing_if = "Option::is_none")]
127    pub language: Option<String>,
128
129    /// Design template name (`<data>/report_templates/<name>.json`);
130    /// unset or missing falls back to the built-in `siserver` look.
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub template: Option<String>,
133}
134
135/// SiServer connection used by `report --send`.
136///
137/// Named `server` for historical reasons - this is the third-party corporate
138/// reporting API, not kasl-server. The team server lives in
139/// [`KaslServerConfig`] under its own key so the two channels can be
140/// configured independently, and eventually used side by side.
141#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
142pub struct ServerConfig {
143    /// Base URL of the reporting API.
144    pub api_url: String,
145
146    /// Token sent with report submissions.
147    pub auth_token: String,
148}
149
150/// Connection to a kasl-server instance: where to reach it, and how to trust
151/// it.
152///
153/// The agent token is deliberately absent - it lives in the OS keyring, like
154/// every other kasl credential, so a readable config file never carries one.
155#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
156pub struct KaslServerConfig {
157    /// Base URL of the server, e.g. `https://kasl.example.com`. Stored
158    /// without a trailing slash so endpoint paths can be appended as-is.
159    pub url: String,
160
161    /// PEM file holding the certificate authority that signed the server's
162    /// certificate.
163    ///
164    /// Self-hosted deployments are routinely behind a company CA or a
165    /// self-signed certificate, which the system trust store does not know.
166    /// Naming the certificate here keeps verification on: the alternative -
167    /// switching it off - would also accept anyone else's certificate.
168    #[serde(default, skip_serializing_if = "Option::is_none")]
169    pub ca_certificate: Option<String>,
170
171    /// Whether the watcher tells this server, once a minute, if you are
172    /// working, on a break, or not in a day.
173    ///
174    /// Off until the employee turns it on with `kasl server pulse enable`:
175    /// connecting agrees to send finished days, and a live signal of whether
176    /// someone is at their keyboard is a different thing to agree to. It
177    /// lives here, beside the address, so the consent belongs to this
178    /// connection - disconnecting forgets it, and connecting to another
179    /// server does not inherit it.
180    #[serde(default)]
181    pub pulse: bool,
182}
183
184/// The root configuration. Every module is optional, and unset modules
185/// are omitted from the JSON, so the file only names what is configured.
186#[derive(Serialize, Deserialize, Clone, Debug, Default)]
187pub struct Config {
188    /// SiServer integration.
189    #[serde(skip_serializing_if = "Option::is_none")]
190    pub si: Option<SiConfig>,
191
192    /// GitLab integration (commit discovery).
193    #[serde(skip_serializing_if = "Option::is_none")]
194    pub gitlab: Option<GitLabConfig>,
195
196    /// Jira integration (issue discovery, inbox).
197    #[serde(skip_serializing_if = "Option::is_none")]
198    pub jira: Option<JiraConfig>,
199
200    /// Activity monitor settings.
201    #[serde(skip_serializing_if = "Option::is_none")]
202    pub monitor: Option<MonitorConfig>,
203
204    /// SiServer, the third-party corporate reporting API.
205    #[serde(skip_serializing_if = "Option::is_none")]
206    pub server: Option<ServerConfig>,
207
208    /// kasl-server: the team server this agent reports to.
209    #[serde(default, skip_serializing_if = "Option::is_none")]
210    pub kasl_server: Option<KaslServerConfig>,
211
212    /// Productivity thresholds.
213    #[serde(skip_serializing_if = "Option::is_none")]
214    pub productivity: Option<ProductivityConfig>,
215
216    /// Report export defaults.
217    #[serde(skip_serializing_if = "Option::is_none")]
218    pub report: Option<ReportConfig>,
219
220    /// Task discovery ignore list; built-in defaults apply when absent.
221    #[serde(default, skip_serializing_if = "Option::is_none")]
222    pub task_discovery: Option<TaskDiscoveryConfig>,
223
224    /// Jira inbox polling; requires `jira`, disabled when absent.
225    #[serde(default, skip_serializing_if = "Option::is_none")]
226    pub jira_inbox: Option<JiraInboxConfig>,
227}
228
229/// Settings for polling assigned open Jira issues into the local inbox.
230#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
231pub struct JiraInboxConfig {
232    /// Whether the watcher should poll Jira for open assigned issues.
233    #[serde(default = "default_true")]
234    pub enabled: bool,
235
236    /// Seconds between Jira inbox polls (default 300 = 5 minutes).
237    #[serde(default = "default_jira_inbox_poll_interval")]
238    pub poll_interval_secs: u64,
239
240    /// Whether to show a desktop toast when a new issue appears.
241    #[serde(default = "default_true")]
242    pub notify: bool,
243
244    /// Whether to show a toast when an existing issue changes status or
245    /// priority. Score changes and returns only set the badge.
246    #[serde(default = "default_true")]
247    pub notify_changes: bool,
248
249    /// Whether to show a toast when an issue leaves the inbox
250    /// (closed or reassigned). Off by default.
251    #[serde(default)]
252    pub notify_gone: bool,
253
254    /// Extra Jira fields to fetch (custom fields such as Scoring).
255    #[serde(default)]
256    pub custom_fields: Vec<JiraCustomField>,
257
258    /// Field id used for ranking (DESC), typically Scoring (`customfield_…`).
259    #[serde(default, skip_serializing_if = "Option::is_none")]
260    pub sort_by_field: Option<String>,
261
262    /// How long a toast's Snooze button sleeps: 3d, 12h, 2w.
263    ///
264    /// Shares its spelling with `inbox triage --snooze-for`, because a button
265    /// and the picker putting an issue to sleep for different lengths would be
266    /// two behaviours wearing one word.
267    #[serde(default = "default_toast_snooze_for")]
268    pub toast_snooze_for: String,
269
270    /// Whether taking an issue also starts it in Jira.
271    ///
272    /// Moves the issue into the in-progress category of its workflow on every
273    /// take - from the command line, from triage and from a toast button. Off
274    /// by default: it writes to a tracker the whole team reads, and in some
275    /// teams "In Progress" means more than "on my list".
276    #[serde(default)]
277    pub start_on_take: bool,
278}
279
280/// A user-configured Jira custom field for inbox sync / display.
281#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
282pub struct JiraCustomField {
283    /// Jira field id, e.g. `customfield_12345`.
284    pub id: String,
285    /// Human-readable label, e.g. `Scoring`.
286    pub label: String,
287}
288
289fn default_true() -> bool {
290    true
291}
292
293fn default_jira_inbox_poll_interval() -> u64 {
294    300
295}
296
297fn default_toast_snooze_for() -> String {
298    "1d".to_string()
299}
300
301impl Default for JiraInboxConfig {
302    fn default() -> Self {
303        Self {
304            enabled: true,
305            poll_interval_secs: default_jira_inbox_poll_interval(),
306            notify: true,
307            notify_changes: true,
308            notify_gone: false,
309            custom_fields: Vec::new(),
310            sort_by_field: None,
311            toast_snooze_for: default_toast_snooze_for(),
312            start_on_take: false,
313        }
314    }
315}
316
317impl JiraInboxConfig {
318    /// Field ids to request from Jira search (custom fields only).
319    pub fn extra_field_ids(&self) -> Vec<String> {
320        let mut ids: Vec<String> = self.custom_fields.iter().map(|f| f.id.trim().to_string()).filter(|id| !id.is_empty()).collect();
321        if let Some(sort_id) = &self.sort_by_field {
322            let trimmed = sort_id.trim();
323            if !trimmed.is_empty() && !ids.iter().any(|id| id == trimmed) {
324                ids.push(trimmed.to_string());
325            }
326        }
327        ids
328    }
329}
330
331/// Settings for intelligent task discovery (`kasl task find`).
332#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
333pub struct TaskDiscoveryConfig {
334    /// Names/prefixes of tasks and commits to exclude from discovery.
335    ///
336    /// Matching is case-insensitive after normalization; a pattern matches
337    /// when the candidate name equals it or starts with it.
338    #[serde(default = "default_ignore_names")]
339    pub ignore_names: Vec<String>,
340}
341
342/// Built-in ignore patterns moved from the former hardcoded noise filter.
343pub fn default_ignore_names() -> Vec<String> {
344    vec![
345        "Merge remote-tracking branch".to_string(),
346        "Merge branch ".to_string(),
347        "update webui".to_string(),
348    ]
349}
350
351impl Default for TaskDiscoveryConfig {
352    fn default() -> Self {
353        Self {
354            ignore_names: default_ignore_names(),
355        }
356    }
357}
358
359impl Default for MonitorConfig {
360    fn default() -> Self {
361        MonitorConfig {
362            min_pause_duration: 20,
363            pause_threshold: 60,
364            poll_interval: 500,
365            activity_threshold: 30,
366            min_work_interval: 10,
367            pause_merge_gap: default_pause_merge_gap(),
368        }
369    }
370}
371
372impl Default for ProductivityConfig {
373    fn default() -> Self {
374        ProductivityConfig {
375            min_productivity_threshold: 75.0,
376            workday_hours: 8.0,
377            min_workday_fraction_before_suggest: 0.5,
378        }
379    }
380}
381
382impl Config {
383    /// Loads the config file, or defaults when none exists yet.
384    ///
385    /// ```rust,no_run
386    /// # fn main() -> anyhow::Result<()> {
387    /// use kasl::libs::config::Config;
388    ///
389    /// let config = Config::read()?;
390    ///
391    /// if config.jira.is_some() {
392    ///     println!("Jira integration is configured");
393    /// }
394    /// # Ok(())
395    /// # }
396    /// ```
397    pub fn read() -> Result<Config> {
398        let config_file_path = DataStorage::new().get_path(CONFIG_FILE_NAME)?;
399
400        if !config_file_path.exists() {
401            return Ok(Config::default());
402        }
403
404        let config_str = fs::read_to_string(config_file_path)?;
405        let config: Config = serde_json::from_str(&config_str)?;
406        Ok(config)
407    }
408
409    /// Writes the config as pretty-printed JSON, overwriting the file.
410    ///
411    /// ```rust,no_run
412    /// # fn main() -> anyhow::Result<()> {
413    /// use kasl::libs::config::{Config, MonitorConfig};
414    ///
415    /// let mut config = Config::read()?;
416    /// config.monitor = Some(MonitorConfig::default());
417    /// config.save()?;
418    /// # Ok(())
419    /// # }
420    /// ```
421    pub fn save(&self) -> Result<()> {
422        let config_file_path = DataStorage::new().get_path(CONFIG_FILE_NAME)?;
423        let json_content = serde_json::to_string_pretty(&self)?;
424        fs::write(config_file_path, json_content)?;
425        Ok(())
426    }
427
428    /// Returns the ignore list for task discovery.
429    ///
430    /// When `task_discovery` is not configured, returns the built-in defaults.
431    pub fn effective_ignore_names(&self) -> Vec<String> {
432        self.task_discovery
433            .as_ref()
434            .map(|c| c.ignore_names.clone())
435            .unwrap_or_else(default_ignore_names)
436    }
437
438    /// Appends unique names to the discovery ignore list and saves the config.
439    ///
440    /// Uniqueness is determined by [`normalize_task_name`]. Returns how many
441    /// new entries were added.
442    pub fn add_ignore_names(&mut self, names: &[String]) -> Result<usize> {
443        let mut discovery = self.task_discovery.clone().unwrap_or_default();
444        let mut added = 0;
445
446        for name in names {
447            let trimmed = name.trim();
448            if trimmed.is_empty() {
449                continue;
450            }
451            let key = normalize_task_name(trimmed);
452            let exists = discovery.ignore_names.iter().any(|existing| normalize_task_name(existing) == key);
453            if !exists {
454                discovery.ignore_names.push(trimmed.to_string());
455                added += 1;
456            }
457        }
458
459        self.task_discovery = Some(discovery);
460        self.save()?;
461        Ok(added)
462    }
463
464    /// Adds the executable's directory to the global PATH.
465    ///
466    /// Windows-shaped: checks the process PATH first, then edits the
467    /// machine-level registry value via `reg` (which needs admin rights;
468    /// the setup command downgrades a failure here to a warning).
469    ///
470    /// ```rust,no_run
471    /// # fn main() -> anyhow::Result<()> {
472    /// use kasl::libs::config::Config;
473    ///
474    /// Config::set_app_global()?;
475    /// # Ok(())
476    /// # }
477    /// ```
478    pub fn set_app_global() -> Result<()> {
479        let current_exe_path = env::current_exe()?;
480        let exe_dir = current_exe_path.parent().unwrap();
481
482        let mut paths: Vec<PathBuf> = env::split_paths(&env::var_os("PATH").unwrap()).collect();
483        let str_paths: Vec<&str> = paths.iter().filter_map(|p| p.to_str()).collect();
484
485        if str_paths.contains(&exe_dir.to_str().unwrap()) {
486            return Ok(());
487        }
488
489        if paths.iter().any(|p| p.to_str() == Some(exe_dir.to_str().unwrap())) {
490            return Ok(());
491        }
492
493        paths.push(exe_dir.to_path_buf());
494
495        let new_path = env::join_paths(paths).unwrap_or_else(|_| panic!("{}", Message::FailedToJoinPaths.to_string()));
496
497        let path_key = r"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment";
498
499        let reg_query_output = Command::new("reg")
500            .arg("query")
501            .arg(path_key)
502            .arg("/v")
503            .arg("Path")
504            .output()
505            .unwrap_or_else(|_| panic!("{}", Message::FailedToExecuteRegQuery.to_string()));
506
507        if !reg_query_output.status.success() {
508            let status = reg_query_output.status.to_string();
509            msg_error!(Message::PathRegistryQueryError { status: status.clone() });
510            return Err(anyhow::anyhow!("{}", Message::PathRegistryQueryError { status }));
511        }
512
513        let current_path = str::from_utf8(&reg_query_output.stdout)
514            .unwrap_or_else(|_| panic!("{}", Message::FailedToParseRegOutput.to_string()))
515            .split_whitespace()
516            .last()
517            .unwrap_or_else(|| panic!("{}", Message::FailedToGetPathFromReg.to_string()));
518
519        let reg_set_output = Command::new("reg")
520            .arg("add")
521            .arg(path_key)
522            .arg("/v")
523            .arg("Path")
524            .arg("/t")
525            .arg("REG_EXPAND_SZ") // Expandable string type for environment variables
526            .arg("/d")
527            .arg(format!("{};{}", current_path, new_path.to_string_lossy()))
528            .arg("/f") // Force overwrite without confirmation
529            .output()
530            .unwrap_or_else(|_| panic!("{}", Message::FailedToExecuteRegSet.to_string()));
531
532        if !reg_set_output.status.success() {
533            let status = reg_set_output.status.to_string();
534            let stderr = String::from_utf8_lossy(&reg_set_output.stderr).to_string();
535            msg_error!(Message::PathRegistryUpdateError {
536                status: status.clone(),
537                stderr: stderr.clone()
538            });
539            return Err(anyhow::anyhow!("{}", Message::PathRegistryUpdateError { status, stderr }));
540        }
541
542        Ok(())
543    }
544}