Skip to main content

vtcode_ui/tui/session_options/
host_selection.rs

1use super::*;
2
3/// Validate grouped choice identities against their owning wizard step.
4pub fn host_interaction_response_grouped(
5    interaction: &HostInteraction,
6    groups: &[Vec<String>],
7    answer_texts: &[Option<String>],
8    cancelled: bool,
9) -> Option<InlineEvent> {
10    if cancelled {
11        return host_interaction_response(interaction, &[], &[], true);
12    }
13    if !interaction.wizard || interaction.steps.is_empty() || groups.len() != interaction.steps.len() {
14        return None;
15    }
16    let mut selections = Vec::new();
17    for (index, step) in interaction.steps.iter().enumerate() {
18        let ids = groups.get(index)?;
19        if !step.multi_select && ids.len() > 1 {
20            return None;
21        }
22        if ids.iter().any(|id| !step.choices.iter().any(|choice| &choice.id == id)) {
23            return None;
24        }
25        let text = answer_texts
26            .get(index)
27            .and_then(Option::as_deref)
28            .map(str::trim)
29            .filter(|text| !text.is_empty());
30        if text.is_some() && !step.allow_freeform {
31            return None;
32        }
33        if !step.multi_select {
34            let selected = ids.first().and_then(|id| step.choices.iter().find(|choice| &choice.id == id));
35            let mut selection = if let Some(choice) = selected {
36                choice.selection.clone()
37            } else {
38                let question_id = step.choices.iter().find_map(|choice| match &choice.selection {
39                    InlineListSelection::RequestUserInputAnswer { question_id, .. } => Some(question_id.clone()),
40                    _ => None,
41                })?;
42                text?;
43                InlineListSelection::RequestUserInputAnswer { question_id, selected: Vec::new(), other: None }
44            };
45            if let Some(text) = text {
46                apply_freeform_answer(&mut selection, text);
47            }
48            selections.push(selection);
49            continue;
50        }
51        let question_id = step.choices.iter().find_map(|choice| match &choice.selection {
52            InlineListSelection::RequestUserInputAnswer { question_id, .. } => Some(question_id.clone()),
53            _ => None,
54        })?;
55        let mut selected = Vec::new();
56        for choice in &step.choices {
57            if ids.contains(&choice.id) {
58                let InlineListSelection::RequestUserInputAnswer { selected: values, .. } = &choice.selection else {
59                    return None;
60                };
61                for value in values {
62                    if !selected.contains(value) {
63                        selected.push(value.clone());
64                    }
65                }
66            }
67        }
68        if selected.is_empty() && text.is_none() {
69            return None;
70        }
71        selections.push(InlineListSelection::RequestUserInputAnswer {
72            question_id,
73            selected,
74            other: text.map(str::to_string),
75        });
76    }
77    Some(InlineEvent::Transient(TransientEvent::Submitted(TransientSubmission::Wizard(selections))))
78}