super-voice-app-core/src/state/assistants.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The Assistants page: what the daemon says about each AI assistant, and the |
3 | | //! two dialogs the page opens. |
4 | | |
5 | | use std::collections::HashSet; |
6 | | |
7 | | use super_voice_shared::models::assistants::{AssistantId, AssistantList, NewKey}; |
8 | | |
9 | | /// What the Assistants page shows. |
10 | | #[derive(Debug, Clone, Default)] |
11 | | pub struct AssistantsState { |
12 | | /// The daemon's last answer. `None` until the page is first shown. |
13 | | pub list: Option<AssistantList>, |
14 | | /// The Connect dialog, while it is open. |
15 | | pub connect: Option<ConnectDialog>, |
16 | | /// The dialog that makes a key for another assistant, while it is open. |
17 | | pub key: Option<KeyDialog>, |
18 | | /// The assistants with a Disconnect running, whose button waits for it. |
19 | | pub disconnecting: HashSet<AssistantId>, |
20 | | /// The assistants connected since the app started. An assistant reads its |
21 | | /// config when it starts, so each row says to restart it. |
22 | | pub connected_here: HashSet<AssistantId>, |
23 | | /// The connected assistant whose own page is open, in the list's place. |
24 | | pub open: Option<AssistantId>, |
25 | | } |
26 | | |
27 | | impl AssistantsState { |
28 | | /// Whether a dialog is over the page, which then shows the page's error. |
29 | | #[must_use] |
30 | 0 | pub fn dialog_open(&self) -> bool { |
31 | 0 | self.connect.is_some() || self.key.is_some() |
32 | 0 | } |
33 | | } |
34 | | |
35 | | /// The Connect dialog: the assistant it asks about. |
36 | | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
37 | | pub struct ConnectDialog { |
38 | | pub id: AssistantId, |
39 | | /// Whether the user said Connect and the daemon has not answered yet. |
40 | | pub connecting: bool, |
41 | | } |
42 | | |
43 | | /// The dialog that makes a key for another assistant. |
44 | | #[derive(Debug, Clone, PartialEq, Eq)] |
45 | | pub enum KeyDialog { |
46 | | /// Asking for the key's name. |
47 | | Naming { |
48 | | name: String, |
49 | | /// Whether the user said Create and the daemon has not answered yet. |
50 | | creating: bool, |
51 | | }, |
52 | | /// The key the daemon made. It is shown only now. |
53 | | Made(NewKey), |
54 | | } |
55 | | |
56 | | impl KeyDialog { |
57 | | /// The dialog as it opens: an empty name. |
58 | | #[must_use] |
59 | 3 | pub fn new() -> Self { |
60 | 3 | Self::Naming { |
61 | 3 | name: String::new(), |
62 | 3 | creating: false, |
63 | 3 | } |
64 | 3 | } |
65 | | |
66 | | /// The name to make the key under, once there is one to send: not blank, |
67 | | /// and not already sent. |
68 | | #[must_use] |
69 | 6 | pub fn name_to_create(&self) -> Option<&str> { |
70 | 6 | match self { |
71 | | Self::Naming { |
72 | | name, |
73 | | creating: false, |
74 | 5 | } if !name.trim().is_empty() => Some(name.trim())2 , |
75 | 4 | _ => None, |
76 | | } |
77 | 6 | } |
78 | | } |
79 | | |
80 | | impl Default for KeyDialog { |
81 | 0 | fn default() -> Self { |
82 | 0 | Self::new() |
83 | 0 | } |
84 | | } |
85 | | |
86 | | #[cfg(test)] |
87 | | mod tests { |
88 | | use super::KeyDialog; |
89 | | |
90 | | /// Create waits for a name, and is sent once. |
91 | | #[test] |
92 | 1 | fn a_key_is_made_once_and_only_with_a_name() { |
93 | 1 | assert_eq!(KeyDialog::new().name_to_create(), None); |
94 | 1 | let blank = KeyDialog::Naming { |
95 | 1 | name: " ".to_string(), |
96 | 1 | creating: false, |
97 | 1 | }; |
98 | 1 | assert_eq!(blank.name_to_create(), None); |
99 | 1 | let named = KeyDialog::Naming { |
100 | 1 | name: " Zed ".to_string(), |
101 | 1 | creating: false, |
102 | 1 | }; |
103 | 1 | assert_eq!(named.name_to_create(), Some("Zed")); |
104 | 1 | let sent = KeyDialog::Naming { |
105 | 1 | name: "Zed".to_string(), |
106 | 1 | creating: true, |
107 | 1 | }; |
108 | 1 | assert_eq!(sent.name_to_create(), None); |
109 | 1 | } |
110 | | } |