Skip to main content

magi_code/config/settings/
psm.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4/// Opt-in, passive session maintenance. It does not change conversation context.
5#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
6#[serde(default)]
7pub struct ProgressiveSessionMaintenanceSettings {
8    pub enabled: bool,
9    #[schemars(range(min = 1, max = 1000))]
10    pub event_threshold: usize,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub provider: Option<String>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub model: Option<String>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub reasoning: Option<crate::thinking::ThinkingLevel>,
17}
18
19impl Default for ProgressiveSessionMaintenanceSettings {
20    fn default() -> Self {
21        Self {
22            enabled: false,
23            event_threshold: 20,
24            provider: None,
25            model: None,
26            reasoning: None,
27        }
28    }
29}
30
31impl ProgressiveSessionMaintenanceSettings {
32    pub(crate) fn is_default(&self) -> bool {
33        self == &Self::default()
34    }
35
36    pub(crate) fn validate(&self) -> anyhow::Result<()> {
37        anyhow::ensure!(
38            (1..=1000).contains(&self.event_threshold),
39            "sessions.psm.event_threshold must be between 1 and 1000"
40        );
41        anyhow::ensure!(
42            self.provider.is_some() == self.model.is_some(),
43            "sessions.psm.provider and sessions.psm.model must both be set or both omitted"
44        );
45        for (field, value) in [("provider", &self.provider), ("model", &self.model)] {
46            if let Some(value) = value {
47                super::tools::validate_model_identifier(&format!("sessions.psm.{field}"), value)?;
48            }
49        }
50        Ok(())
51    }
52}