capri-perception: add settings module
This commit is contained in:
1 file changed
+152
@@ -0,0 +1,152 @@
|
|||||||
|
//! Settings sections owned by this crate, stored under `sections[NAME]`.
|
||||||
|
use capri_core::Section;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
/// Screen snapshot / observation preferences. Never starts observation by itself.
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
#[serde(default, rename_all = "camelCase")]
|
||||||
|
pub struct ScreenSettings {
|
||||||
|
/// Minimum gap between background analyses, 15 000..=300 000 ms.
|
||||||
|
pub interval_ms: u64,
|
||||||
|
/// Observation lease, 30 000..=7 200 000 ms.
|
||||||
|
pub duration_ms: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ScreenSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { interval_ms: 30_000, duration_ms: 600_000 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Section for ScreenSettings {
|
||||||
|
const NAME: &'static str = "screenObservation";
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ScreenSettings {
|
||||||
|
pub fn clamped(&self) -> Self {
|
||||||
|
Self { interval_ms: self.interval_ms.clamp(15_000, 300_000), duration_ms: self.duration_ms.clamp(30_000, 7_200_000) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, Default)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum Level {
|
||||||
|
#[default]
|
||||||
|
Off,
|
||||||
|
Occasional,
|
||||||
|
Conversational,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initiative (exactly Capri's nine keys).
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
#[serde(default, rename_all = "camelCase")]
|
||||||
|
pub struct InitiativeSettings {
|
||||||
|
pub level: Level,
|
||||||
|
/// 1..=30 per hour; `None` = 10 (occasional) / 30 (conversational).
|
||||||
|
pub hourly_limit: Option<u8>,
|
||||||
|
pub quiet_start: u8,
|
||||||
|
pub quiet_end: u8,
|
||||||
|
pub quiet_hours_enabled: bool,
|
||||||
|
pub spontaneous: bool,
|
||||||
|
pub return_reactions: bool,
|
||||||
|
pub app_reactions: bool,
|
||||||
|
/// Foreground process names (lower-case, `^[a-z0-9_.-]{1,60}$`, <= 30) that block comments.
|
||||||
|
pub excluded_apps: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for InitiativeSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
level: Level::Off,
|
||||||
|
hourly_limit: None,
|
||||||
|
quiet_start: 23,
|
||||||
|
quiet_end: 9,
|
||||||
|
quiet_hours_enabled: true,
|
||||||
|
spontaneous: true,
|
||||||
|
return_reactions: true,
|
||||||
|
app_reactions: false,
|
||||||
|
// Capri's Windows defaults plus their Linux process names (/proc/<pid>/comm).
|
||||||
|
excluded_apps: ["discord", "ms-teams", "teams", "zoom", "powerpnt", "obs64", "teams-for-linux", "obs", "soffice.bin"].map(String::from).to_vec(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Section for InitiativeSettings {
|
||||||
|
const NAME: &'static str = "initiative";
|
||||||
|
}
|
||||||
|
|
||||||
|
impl InitiativeSettings {
|
||||||
|
/// Normalized copy: invalid values fall back to defaults field by field (Capri rejects the file).
|
||||||
|
pub fn validated(&self) -> Self {
|
||||||
|
let d = Self::default();
|
||||||
|
let mut apps: Vec<String> = Vec::new();
|
||||||
|
for a in &self.excluded_apps {
|
||||||
|
let a = a.to_lowercase();
|
||||||
|
if !a.is_empty() && a.len() <= 60 && a.chars().all(|c| c.is_ascii_alphanumeric() || "_.-".contains(c)) && !apps.contains(&a) && apps.len() < 30 {
|
||||||
|
apps.push(a);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Self {
|
||||||
|
level: self.level,
|
||||||
|
hourly_limit: self.hourly_limit.filter(|l| (1..=30).contains(l)),
|
||||||
|
quiet_start: if self.quiet_start < 24 { self.quiet_start } else { d.quiet_start },
|
||||||
|
quiet_end: if self.quiet_end < 24 { self.quiet_end } else { d.quiet_end },
|
||||||
|
excluded_apps: apps,
|
||||||
|
..self.clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn quiet_hour(&self, hour: u8) -> bool {
|
||||||
|
let (s, e) = (self.quiet_start, self.quiet_end);
|
||||||
|
self.quiet_hours_enabled && s != e && if s < e { hour >= s && hour < e } else { hour >= s || hour < e }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn limit(&self) -> u32 {
|
||||||
|
self.hourly_limit.map(u32::from).unwrap_or(if self.level == Level::Occasional { 10 } else { 30 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Resource HUD.
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
||||||
|
#[serde(default, rename_all = "camelCase")]
|
||||||
|
pub struct ResourceSettings {
|
||||||
|
pub memory_monitor: bool,
|
||||||
|
pub process_graphs: bool,
|
||||||
|
/// `rings` or `bars`.
|
||||||
|
pub memory_style: String,
|
||||||
|
/// 3, 5 or 10.
|
||||||
|
pub memory_interval_seconds: u8,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ResourceSettings {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self { memory_monitor: true, process_graphs: true, memory_style: "rings".into(), memory_interval_seconds: 3 }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Section for ResourceSettings {
|
||||||
|
const NAME: &'static str = "resources";
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn initiative_rules() {
|
||||||
|
let mut s = InitiativeSettings { quiet_start: 23, quiet_end: 9, ..Default::default() };
|
||||||
|
assert!(s.quiet_hour(23) && s.quiet_hour(3) && !s.quiet_hour(9) && !s.quiet_hour(12));
|
||||||
|
s.quiet_start = 9;
|
||||||
|
s.quiet_end = 17;
|
||||||
|
assert!(s.quiet_hour(9) && !s.quiet_hour(17));
|
||||||
|
s.quiet_end = 9;
|
||||||
|
assert!(!s.quiet_hour(9), "equal bounds: no quiet hours");
|
||||||
|
assert_eq!(InitiativeSettings { level: Level::Occasional, ..Default::default() }.limit(), 10);
|
||||||
|
assert_eq!(InitiativeSettings { level: Level::Conversational, ..Default::default() }.limit(), 30);
|
||||||
|
let v = InitiativeSettings { hourly_limit: Some(99), excluded_apps: vec!["VLC".into(), "vlc".into(), "bad name".into()], quiet_start: 30, ..Default::default() }.validated();
|
||||||
|
assert_eq!((v.hourly_limit, v.excluded_apps, v.quiet_start), (None, vec!["vlc".to_string()], 23));
|
||||||
|
let parsed: InitiativeSettings = serde_json::from_str(r#"{"level":"occasional","quietStart":1}"#).unwrap();
|
||||||
|
assert_eq!((parsed.level, parsed.quiet_start, parsed.quiet_end), (Level::Occasional, 1, 9));
|
||||||
|
assert_eq!(ScreenSettings { interval_ms: 1, duration_ms: u64::MAX }.clamped(), ScreenSettings { interval_ms: 15_000, duration_ms: 7_200_000 });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user