Skip to main content

wisp/session/
session_config_view.rs

1use acp_utils::config_meta::{ConfigOptionMeta, SelectOptionMeta};
2use acp_utils::config_option_id::ConfigOptionId;
3use agent_client_protocol::schema::v2::{self as acp, SessionConfigOptionCategory};
4use utils::ReasoningEffort;
5
6/// Client-owned configuration state projected from an ACP session schema.
7///
8/// ACP values are copied at the protocol boundary so optimistic edits and
9/// reconciliation never mutate a protocol object borrowed by the UI.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct LocalConfigOption {
12    pub id: String,
13    pub name: String,
14    pub(crate) category: Option<SessionConfigOptionCategory>,
15    pub(crate) meta: Option<acp::Meta>,
16    pub(crate) kind: LocalConfigKind,
17}
18
19#[derive(Debug, Clone, PartialEq, Eq)]
20pub enum LocalConfigKind {
21    Select { current_value: String, values: Vec<LocalConfigValue>, multi_select: bool },
22    Boolean { current_value: bool },
23}
24
25#[derive(Debug, Clone, Default, PartialEq, Eq)]
26pub struct LocalConfigValue {
27    pub value: String,
28    pub name: String,
29    pub group: Option<String>,
30    pub description: Option<String>,
31    pub is_disabled: bool,
32    pub meta: SelectOptionMeta,
33}
34
35impl LocalConfigOption {
36    pub fn from_acp(option: acp::SessionConfigOption) -> Self {
37        let kind = match option.kind {
38            acp::SessionConfigKind::Select(select) => LocalConfigKind::Select {
39                current_value: select.current_value.0.to_string(),
40                values: match select.options {
41                    acp::SessionConfigSelectOptions::Ungrouped(options) => {
42                        options.into_iter().map(|option| (None, option)).collect()
43                    }
44                    acp::SessionConfigSelectOptions::Grouped(groups) => groups
45                        .into_iter()
46                        .flat_map(|group| {
47                            let name = group.name;
48                            group.options.into_iter().map(move |option| (Some(name.clone()), option))
49                        })
50                        .collect(),
51                    _ => Vec::new(),
52                }
53                .into_iter()
54                .map(|(group, value)| LocalConfigValue {
55                    value: value.value.0.to_string(),
56                    name: value.name,
57                    group,
58                    is_disabled: value.description.as_deref().is_some_and(|text| text.starts_with("Unavailable:")),
59                    description: value.description,
60                    meta: SelectOptionMeta::from_meta(value.meta.as_ref()),
61                })
62                .collect(),
63                multi_select: ConfigOptionMeta::from_meta(option.meta.as_ref()).multi_select,
64            },
65            acp::SessionConfigKind::Boolean(boolean) => {
66                LocalConfigKind::Boolean { current_value: boolean.current_value }
67            }
68            _ => LocalConfigKind::Boolean { current_value: false },
69        };
70        Self {
71            id: option.config_id.0.to_string(),
72            name: option.name,
73            category: option.category,
74            meta: option.meta,
75            kind,
76        }
77    }
78
79    pub(crate) fn select(&self) -> Option<LocalConfigSelect<'_>> {
80        match &self.kind {
81            LocalConfigKind::Select { current_value, values, multi_select: _ } => {
82                Some(LocalConfigSelect { current_value, values })
83            }
84            LocalConfigKind::Boolean { .. } => None,
85        }
86    }
87
88    /// The selected value of a select option, `None` for other kinds.
89    pub fn current_value(&self) -> Option<&str> {
90        match &self.kind {
91            LocalConfigKind::Select { current_value, .. } => Some(current_value),
92            LocalConfigKind::Boolean { .. } => None,
93        }
94    }
95}
96
97pub struct LocalConfigSelect<'a> {
98    pub current_value: &'a str,
99    pub values: &'a [LocalConfigValue],
100}
101
102/// A pure view over client-owned configuration state.
103pub struct LocalConfigView<'a> {
104    options: &'a [LocalConfigOption],
105}
106
107impl<'a> LocalConfigView<'a> {
108    pub fn new(options: &'a [LocalConfigOption]) -> Self {
109        Self { options }
110    }
111
112    pub fn select(&self, id: ConfigOptionId) -> Option<LocalConfigSelect<'a>> {
113        self.options.iter().find(|option| option.id == id.as_str()).and_then(LocalConfigOption::select)
114    }
115
116    pub fn flattened_options(&self, id: ConfigOptionId) -> Vec<&'a LocalConfigValue> {
117        self.select(id).map_or_else(Vec::new, |select| select.values.iter().collect())
118    }
119
120    pub fn current_values(&self, id: ConfigOptionId) -> Vec<&'a str> {
121        self.select(id)
122            .map(|select| select.current_value.split(',').map(str::trim).filter(|value| !value.is_empty()).collect())
123            .unwrap_or_default()
124    }
125
126    pub fn current_display_name(&self, id: ConfigOptionId) -> Option<String> {
127        let values = self.current_values(id);
128        let options = self.flattened_options(id);
129        let names: Vec<_> = values
130            .iter()
131            .filter_map(|value| options.iter().find(|option| option.value == *value).map(|option| option.name.as_str()))
132            .collect();
133        (!names.is_empty()).then(|| names.join(" + "))
134    }
135
136    pub fn next_mode(&self) -> Option<(&'a str, &'a str)> {
137        let option = self
138            .options
139            .iter()
140            .find(|option| option.category == Some(SessionConfigOptionCategory::Mode) && option.select().is_some())?;
141        let select = option.select()?;
142        let current = select.values.iter().position(|value| value.value == select.current_value).unwrap_or(0);
143        let next = select.values.get((current + 1) % select.values.len().max(1))?;
144        Some((option.id.as_str(), next.value.as_str()))
145    }
146
147    pub fn reasoning_levels(&self) -> Vec<ReasoningEffort> {
148        self.flattened_options(ConfigOptionId::ReasoningEffort)
149            .into_iter()
150            .filter_map(|option| option.value.parse().ok())
151            .filter(|effort| *effort != ReasoningEffort::Default)
152            .collect()
153    }
154
155    pub fn reasoning_effort(&self) -> Option<ReasoningEffort> {
156        ReasoningEffort::parse(self.select(ConfigOptionId::ReasoningEffort)?.current_value).unwrap_or(None)
157    }
158
159    pub fn selected_model_metadata(&self) -> Vec<SelectOptionMeta> {
160        let values = self.current_values(ConfigOptionId::Model);
161        let options = self.flattened_options(ConfigOptionId::Model);
162        values
163            .iter()
164            .filter_map(|value| options.iter().find(|option| option.value == *value).map(|option| option.meta.clone()))
165            .collect()
166    }
167}