super-voice-daemon/src/assistants/sessions.rs
Line | Count | Source |
1 | | // SPDX-License-Identifier: GPL-3.0-only |
2 | | //! The sessions of the AI assistants talking to Super Voice. A session is one |
3 | | //! conversation of one assistant: one Claude Code session, one Codex thread. |
4 | | //! |
5 | | //! A request names its session by what its client says of itself (the MCP |
6 | | //! server's `identity`). A session is kept from its first request until its |
7 | | //! plugin says it ended, or it goes quiet for too long. The speech and the |
8 | | //! recording a session starts are its own, so stopping acts on them alone. |
9 | | |
10 | | use super::calls::Caller; |
11 | | use chrono::{DateTime, Utc}; |
12 | | use parking_lot::Mutex; |
13 | | use std::collections::{BTreeSet, VecDeque}; |
14 | | use std::sync::Arc; |
15 | | use std::time::{Duration, Instant}; |
16 | | use tokio::task::AbortHandle; |
17 | | |
18 | | /// How long a session its plugin opened may go without a request. The plugin |
19 | | /// checks in at least once a minute while the session is open. |
20 | | pub const OPENED_IDLE: Duration = Duration::from_mins(3); |
21 | | |
22 | | /// How long a session nothing opened may go without a request: nothing says |
23 | | /// when it ends. |
24 | | pub const SEEN_IDLE: Duration = Duration::from_mins(30); |
25 | | |
26 | | /// How long a session is remembered once it has left the list: its turn, |
27 | | /// spoken or typed, and its replies. A turn may run long with nothing said |
28 | | /// to Super Voice, as a Codex task working on its own does, and is still the |
29 | | /// turn it was when it ends. |
30 | | pub const REMEMBERED: Duration = Duration::from_hours(24); |
31 | | |
32 | | /// What a request says of the session it comes from. |
33 | | #[derive(Clone, Debug, Default, PartialEq, Eq)] |
34 | | pub struct Named { |
35 | | /// The session's id, as its client names it. `None` when it names none: |
36 | | /// such requests are one unnamed session of their caller. |
37 | | pub id: Option<String>, |
38 | | /// The agent within the session that made the request, when it is not |
39 | | /// the session's own, such as a subagent. |
40 | | pub agent: Option<String>, |
41 | | /// The folder the session works in. |
42 | | pub folder: Option<String>, |
43 | | /// The session's name, as its assistant shows it, like the title of a |
44 | | /// Claude Code session. Super Voice's plugins say it; a Codex thread's |
45 | | /// is read from Codex's own files ([`super::thread_names`]). |
46 | | pub name: Option<String>, |
47 | | } |
48 | | |
49 | | /// The session a request comes from: its caller, what it says of itself, |
50 | | /// and the connection it came over, if the client keeps one. |
51 | | #[derive(Clone, Debug, PartialEq, Eq)] |
52 | | pub struct Who { |
53 | | pub caller: Caller, |
54 | | pub named: Named, |
55 | | pub connection: Option<String>, |
56 | | } |
57 | | |
58 | | /// A session as the assistants list shows it. |
59 | | #[derive(Clone, Debug, PartialEq, Eq)] |
60 | | pub struct SessionView { |
61 | | pub caller: Caller, |
62 | | pub id: Option<String>, |
63 | | pub folder: Option<String>, |
64 | | /// Its name, as its assistant shows it, when its plugin says. |
65 | | pub name: Option<String>, |
66 | | /// How many agents besides its own have made requests. |
67 | | pub agents: usize, |
68 | | /// The utterance it started last, whether or not it still plays. |
69 | | pub utterance: Option<String>, |
70 | | /// Whether one of its `listen` calls is recording. |
71 | | pub listening: bool, |
72 | | pub started_at: DateTime<Utc>, |
73 | | /// When it last spoke or listened. A request that only checks in, like |
74 | | /// its plugin's heartbeat, is not a use. |
75 | | pub last_used_at: Option<DateTime<Utc>>, |
76 | | } |
77 | | |
78 | | struct Entry { |
79 | | caller: Caller, |
80 | | id: Option<String>, |
81 | | /// The connection the session's calls last came over, which names it for |
82 | | /// a call that names nothing else. |
83 | | connection: Option<String>, |
84 | | folder: Option<String>, |
85 | | /// Its name, as its plugin last said it. |
86 | | name: Option<String>, |
87 | | agents: BTreeSet<String>, |
88 | | started_at: DateTime<Utc>, |
89 | | /// When it last made any request, which keeps it listed. |
90 | | last_active: Instant, |
91 | | /// When it last spoke or listened. |
92 | | used_at: Option<DateTime<Utc>>, |
93 | | /// Its plugin said it started, so it checks in while it is open. |
94 | | opened: bool, |
95 | | utterance: Option<String>, |
96 | | /// The task making its last speech, from the moment it was asked for: |
97 | | /// its speech while it waits for the model and is made, before any |
98 | | /// utterance plays. |
99 | | speaking: Option<AbortHandle>, |
100 | | /// How many times its speech was stopped, so a speech of its own waiting |
101 | | /// its turn knows the user asked for quiet. |
102 | | stops: u64, |
103 | | recordings: usize, |
104 | | /// Whether the turn under way was spoken, once its plugin has said. |
105 | | spoken: Option<bool>, |
106 | | /// The prompt that started it, as its plugin last said. |
107 | | prompt: Option<String>, |
108 | | /// Its last [`READ_BACK`] replies, as its plugin said them, the last one |
109 | | /// first. |
110 | | replies: VecDeque<String>, |
111 | | } |
112 | | |
113 | | impl Entry { |
114 | 256 | fn is(&self, who: &Who) -> bool { |
115 | 256 | self.caller == who.caller && self.id == who.named.id251 |
116 | 256 | } |
117 | | |
118 | | /// Whether it is listed at `now`: it records, or has not been quiet for |
119 | | /// longer than a session like it may be. |
120 | 32 | fn is_listed(&self, now: Instant) -> bool { |
121 | 32 | let idle = if self.opened { OPENED_IDLE12 } else { SEEN_IDLE20 }; |
122 | 32 | self.recordings > 0 || now.saturating_duration_since(self.last_active) < idle29 |
123 | 32 | } |
124 | | |
125 | | /// What names it to people: the name its plugin gave it, else the last |
126 | | /// part of the folder it works in. |
127 | 11 | fn label(&self) -> Option<String> { |
128 | 11 | self.name |
129 | 11 | .clone() |
130 | 7 | .or_else(|| self.folder.as_deref().map(folder_name)) |
131 | 11 | } |
132 | | |
133 | 29 | fn view(&self) -> SessionView { |
134 | 29 | SessionView { |
135 | 29 | caller: self.caller.clone(), |
136 | 29 | id: self.id.clone(), |
137 | 29 | folder: self.folder.clone(), |
138 | 29 | name: self.name.clone(), |
139 | 29 | agents: self.agents.len(), |
140 | 29 | utterance: self.utterance.clone(), |
141 | 29 | listening: self.recordings > 0, |
142 | 29 | started_at: self.started_at, |
143 | 29 | last_used_at: self.used_at, |
144 | 29 | } |
145 | 29 | } |
146 | | } |
147 | | |
148 | | /// The last part of `folder`, or all of it when it has none, as `/` does. |
149 | 4 | fn folder_name(folder: &str) -> String { |
150 | 4 | std::path::Path::new(folder).file_name().map_or_else( |
151 | 0 | || folder.to_string(), |
152 | 4 | |name| name.to_string_lossy().into_owned(), |
153 | | ) |
154 | 4 | } |
155 | | |
156 | | /// How many of a session's replies are kept to read aloud again. |
157 | | pub const READ_BACK: usize = 10; |
158 | | |
159 | | /// Every session the daemon knows of. |
160 | | #[derive(Default)] |
161 | | pub struct Sessions { |
162 | | entries: Mutex<Vec<Entry>>, |
163 | | } |
164 | | |
165 | | /// A recording one session's `listen` holds. Dropped when the take ends. |
166 | | #[must_use = "dropping it ends the session's recording at once"] |
167 | | pub struct Recording { |
168 | | sessions: Arc<Sessions>, |
169 | | who: Who, |
170 | | } |
171 | | |
172 | | impl Drop for Recording { |
173 | 6 | fn drop(&mut self) { |
174 | 6 | let mut entries = self.sessions.entries.lock(); |
175 | 10 | if let Some(entry) = entries.iter_mut()6 .find(|entry| entry.is(&self.who)) { |
176 | 6 | entry.recordings = entry.recordings.saturating_sub(1); |
177 | 0 | } |
178 | 6 | } |
179 | | } |
180 | | |
181 | | impl Sessions { |
182 | | /// Name the session of a call that came over `connection`, the |
183 | | /// `Mcp-Session-Id` the client sent, and said `named` of itself. |
184 | | /// |
185 | | /// A call that names its session ties the connection to it, so a later |
186 | | /// call over the same connection that names nothing, such as a model's |
187 | | /// whose client dropped what its plugin added, is the same session's. A |
188 | | /// call over a connection nothing named is that connection's own session. |
189 | | #[must_use] |
190 | 17 | pub fn resolve(&self, caller: &Caller, mut named: Named, connection: Option<&str>) -> Named { |
191 | 17 | let Some(connection) = connection else { |
192 | 10 | return named; |
193 | | }; |
194 | 7 | let mut entries = self.entries.lock(); |
195 | 7 | if named.id.is_some() { |
196 | | // A newer session on the connection, as after Claude Code's |
197 | | // `/clear`, takes it over: `touch` ties it to this one. |
198 | 3 | for entry in entries.iter_mut().filter(|entry| { |
199 | 3 | entry.caller == *caller |
200 | 3 | && entry.id != named.id |
201 | 3 | && entry.connection.as_deref() == Some(connection) |
202 | 3 | }) { |
203 | 1 | entry.connection = None; |
204 | | } |
205 | 3 | return named; |
206 | 4 | } |
207 | 4 | named.id = entries |
208 | 4 | .iter() |
209 | 8 | .find(|entry| { |
210 | 8 | entry.caller == *caller && entry.connection.as_deref() == Some(connection) |
211 | 8 | }) |
212 | 3 | .and_then(|entry| entry.id.clone()) |
213 | 1 | .or_else(|| Some(connection.to_string())); |
214 | 4 | named |
215 | 17 | } |
216 | | |
217 | | /// A request from `who`: it is active now. A session is kept from its |
218 | | /// first request, and learns its folder, name and agents as requests name |
219 | | /// them. |
220 | 28 | pub fn touch(&self, who: &Who) { |
221 | 28 | self.with(who, |_| {}); |
222 | 28 | } |
223 | | |
224 | | /// `who`'s plugin says the session started. It is kept until it says the |
225 | | /// session ended, or the session goes quiet for [`OPENED_IDLE`]. |
226 | 6 | pub fn open(&self, who: &Who) { |
227 | 6 | self.with(who, |entry| entry.opened = true); |
228 | 6 | } |
229 | | |
230 | | /// `who`'s plugin says the session ended. |
231 | 3 | pub fn close(&self, who: &Who) { |
232 | 6 | self.entries.lock()3 .retain(|entry| !entry.is(who)); |
233 | 3 | } |
234 | | |
235 | | /// `who` started `utterance`. |
236 | 3 | pub fn spoke(&self, who: &Who, utterance: String) { |
237 | 3 | self.with(who, |entry| { |
238 | 3 | entry.utterance = Some(utterance); |
239 | 3 | entry.used_at = Some(Utc::now()); |
240 | 3 | }); |
241 | 3 | } |
242 | | |
243 | | /// What names `who`'s session to people, as the app's Assistants page |
244 | | /// names it. `None` when nothing does. |
245 | | #[must_use] |
246 | 12 | pub fn label(&self, who: &Who) -> Option<String> { |
247 | 12 | self.read(who, Entry::label).flatten() |
248 | 12 | } |
249 | | |
250 | | /// `who` asked to speak, on `task`. |
251 | 16 | pub fn asked_to_speak(&self, who: &Who, task: AbortHandle) { |
252 | 16 | self.with(who, |entry| entry.speaking = Some(task)); |
253 | 16 | } |
254 | | |
255 | | /// `who`'s speech was stopped: a speech of its own waiting its turn is |
256 | | /// not said. |
257 | 3 | pub fn speech_stopped(&self, who: &Who) { |
258 | 3 | self.with(who, |entry| entry.stops += 1); |
259 | 3 | } |
260 | | |
261 | | /// How many times `who`'s speech was stopped. |
262 | | #[must_use] |
263 | 12 | pub fn stops(&self, who: &Who) -> u64 { |
264 | 12 | self.read(who, |entry| entry10 .stops).unwrap_or(0) |
265 | 12 | } |
266 | | |
267 | | /// The task making `who`'s last speech, while it is still at it. |
268 | | #[must_use] |
269 | 43 | pub fn speaking(&self, who: &Who) -> Option<AbortHandle> { |
270 | 43 | self.read(who, |entry| entry.speaking38 .clone()) |
271 | | .flatten() |
272 | 30 | .filter(|task| !task.is_finished()) |
273 | 43 | } |
274 | | |
275 | | /// The utterance `who` started last, whether or not it still plays. |
276 | | #[must_use] |
277 | 26 | pub fn utterance(&self, who: &Who) -> Option<String> { |
278 | 26 | self.read(who, |entry| entry.utterance20 .clone()).flatten() |
279 | 26 | } |
280 | | |
281 | | /// `who`'s `listen` is recording until the returned guard is dropped. |
282 | 6 | pub fn recording(self: &Arc<Self>, who: &Who) -> Recording { |
283 | 6 | self.with(who, |entry| { |
284 | 6 | entry.recordings += 1; |
285 | 6 | entry.used_at = Some(Utc::now()); |
286 | 6 | }); |
287 | 6 | Recording { |
288 | 6 | sessions: Arc::clone(self), |
289 | 6 | who: who.clone(), |
290 | 6 | } |
291 | 6 | } |
292 | | |
293 | | /// Whether one of `who`'s `listen` calls is recording. |
294 | | #[must_use] |
295 | 14 | pub fn is_recording(&self, who: &Who) -> bool { |
296 | 14 | self.read(who, |entry| entry.recordings13 > 013 ) == Some(true) |
297 | 14 | } |
298 | | |
299 | | /// `who`'s turn under way was `spoken`, or typed. |
300 | 18 | pub fn set_spoken(&self, who: &Who, spoken: bool) { |
301 | 18 | self.with(who, |entry| entry.spoken = Some(spoken)); |
302 | 18 | } |
303 | | |
304 | | /// `who`'s plugin says the user sent `prompt`. Said `again`, as a hook |
305 | | /// that runs before each model call of a turn says it, and the prompt the |
306 | | /// turn under way started with: what that turn was. Otherwise `None`: a |
307 | | /// new turn, whose prompt it keeps. The same text sent in a new turn is a |
308 | | /// new turn. |
309 | 9 | pub fn prompted(&self, who: &Who, prompt: &str, again: bool) -> Option<bool> { |
310 | 9 | let mut said = None; |
311 | 9 | self.with(who, |entry| { |
312 | 9 | if again && entry.prompt.as_deref() == Some(prompt)1 { |
313 | 1 | said = entry.spoken; |
314 | | } else { |
315 | 8 | entry.prompt = Some(prompt.to_string()); |
316 | | } |
317 | 9 | }); |
318 | 9 | said |
319 | 9 | } |
320 | | |
321 | | /// `who`'s plugin says the assistant answered its turn with `reply`. The |
322 | | /// same reply said again, as a turn that ended before the model ran |
323 | | /// says, is kept once. |
324 | 21 | pub fn replied(&self, who: &Who, reply: &str) { |
325 | 21 | let reply = reply.trim(); |
326 | 21 | if reply.is_empty() { |
327 | 1 | return; |
328 | 20 | } |
329 | 20 | self.with(who, |entry| { |
330 | 20 | if entry.replies.front().map(String::as_str) != Some(reply) { |
331 | 18 | entry.replies.push_front(reply.to_string()); |
332 | 18 | entry.replies.truncate(READ_BACK); |
333 | 2 | } |
334 | 20 | }); |
335 | 21 | } |
336 | | |
337 | | /// `who`'s reply `back` replies before its last one. |
338 | | /// |
339 | | /// # Errors |
340 | | /// |
341 | | /// How many replies it has kept, when there is none that far back. |
342 | 12 | pub fn reply(&self, who: &Who, back: usize) -> Result<String, usize> { |
343 | 12 | self.read(who, |entry| {10 |
344 | 10 | entry.replies.get(back).cloned().ok_or(entry.replies.len()) |
345 | 10 | }) |
346 | 12 | .unwrap_or(Err(0)) |
347 | 12 | } |
348 | | |
349 | | /// Whether `who`'s turn under way was spoken, once its plugin has said. |
350 | | #[must_use] |
351 | 24 | pub fn spoken(&self, who: &Who) -> Option<bool> { |
352 | 24 | self.read(who, |entry| entry17 .spoken).flatten() |
353 | 24 | } |
354 | | |
355 | | /// Forget every session of `caller`, whose key was revoked. |
356 | 8 | pub fn forget(&self, caller: &Caller) { |
357 | 8 | self.entries.lock().retain(|entry| entry.caller5 != *caller5 ); |
358 | 8 | } |
359 | | |
360 | | /// Forget the sessions gone quiet by `now`. A session still recording is |
361 | | /// kept: its `listen` may wait minutes for the user. How many went. |
362 | 202 | pub fn sweep(&self, now: Instant) -> usize { |
363 | 202 | let mut entries = self.entries.lock(); |
364 | 202 | let before = entries.len(); |
365 | 202 | entries.retain(|entry| {6 |
366 | 6 | entry.recordings > 0 || now.saturating_duration_since(entry.last_active) < REMEMBERED5 |
367 | 6 | }); |
368 | 202 | before - entries.len() |
369 | 202 | } |
370 | | |
371 | | /// Every session listed, oldest first. |
372 | | #[must_use] |
373 | 41 | pub fn list(&self) -> Vec<SessionView> { |
374 | 41 | self.list_at(Instant::now()) |
375 | 41 | } |
376 | | |
377 | | /// Every session listed at `now`, oldest first. |
378 | 44 | fn list_at(&self, now: Instant) -> Vec<SessionView> { |
379 | 44 | self.entries |
380 | 44 | .lock() |
381 | 44 | .iter() |
382 | 32 | .filter(|entry| entry.is_listed(now)) |
383 | 44 | .map(Entry::view) |
384 | | .collect() |
385 | 44 | } |
386 | | |
387 | | /// What `look` finds in `who`'s session, when there is one. |
388 | 143 | fn read<T>(&self, who: &Who, look: impl FnOnce(&Entry) -> T) -> Option<T> { |
389 | 143 | self.entries |
390 | 143 | .lock() |
391 | 143 | .iter() |
392 | 148 | .find(|entry| entry.is(who)) |
393 | 143 | .map(look) |
394 | 143 | } |
395 | | |
396 | | /// Run `change` on `who`'s session, made now if it is new, and mark it |
397 | | /// active. |
398 | 109 | fn with(&self, who: &Who, change: impl FnOnce(&mut Entry)) { |
399 | 109 | let mut entries = self.entries.lock(); |
400 | 109 | let index = if let Some(index) = entries.iter().position(|entry| entry92 .is(who92 )) { |
401 | 70 | index |
402 | | } else { |
403 | 39 | entries.push(Entry { |
404 | 39 | caller: who.caller.clone(), |
405 | 39 | id: who.named.id.clone(), |
406 | 39 | connection: None, |
407 | 39 | folder: None, |
408 | 39 | name: None, |
409 | 39 | agents: BTreeSet::new(), |
410 | 39 | started_at: Utc::now(), |
411 | 39 | last_active: Instant::now(), |
412 | 39 | used_at: None, |
413 | 39 | opened: false, |
414 | 39 | utterance: None, |
415 | 39 | speaking: None, |
416 | 39 | stops: 0, |
417 | 39 | recordings: 0, |
418 | 39 | spoken: None, |
419 | 39 | prompt: None, |
420 | 39 | replies: VecDeque::new(), |
421 | 39 | }); |
422 | 39 | entries.len() - 1 |
423 | | }; |
424 | 109 | let entry = &mut entries[index]; |
425 | 109 | entry.last_active = Instant::now(); |
426 | 109 | if let Some(connection) = &who.connection { |
427 | 4 | entry.connection = Some(connection.clone()); |
428 | 105 | } |
429 | 109 | if let Some(folder) = &who.named.folder { |
430 | 8 | entry.folder = Some(folder.clone()); |
431 | 101 | } |
432 | | // A call that says no name, like the model's own, keeps the one its |
433 | | // plugin said. |
434 | 109 | if let Some(name) = &who.named.name { |
435 | 4 | entry.name = Some(name.clone()); |
436 | 105 | } |
437 | 109 | if let Some(agent) = &who.named.agent { |
438 | 2 | entry.agents.insert(agent.clone()); |
439 | 107 | } |
440 | 109 | change(entry); |
441 | 109 | } |
442 | | } |
443 | | |
444 | | #[cfg(test)] |
445 | | mod tests { |
446 | | use super::*; |
447 | | |
448 | 22 | fn who(key: &str, id: Option<&str>) -> Who { |
449 | 22 | Who { |
450 | 22 | caller: Caller::Key(key.to_string()), |
451 | 22 | named: Named { |
452 | 22 | id: id.map(str::to_string), |
453 | 22 | ..Named::default() |
454 | 22 | }, |
455 | 22 | connection: None, |
456 | 22 | } |
457 | 22 | } |
458 | | |
459 | | #[test] |
460 | 1 | fn a_session_is_kept_from_its_first_request_and_learns_its_folder() { |
461 | 1 | let sessions = Sessions::default(); |
462 | 1 | let mut one = who("a", Some("s1")); |
463 | 1 | sessions.touch(&one); |
464 | 1 | one.named.folder = Some("/home/me/project".to_string()); |
465 | 1 | one.named.agent = Some("sub".to_string()); |
466 | 1 | sessions.touch(&one); |
467 | 1 | sessions.touch(&who("a", Some("s2"))); |
468 | 1 | sessions.touch(&who("b", Some("s1"))); |
469 | 1 | let list = sessions.list(); |
470 | 1 | assert_eq!(list.len(), 3, "{list:?}"); |
471 | 1 | assert_eq!(list[0].folder.as_deref(), Some("/home/me/project")); |
472 | 1 | assert_eq!(list[0].agents, 1); |
473 | 1 | } |
474 | | |
475 | | /// A session is named by the name its plugin says, else by its folder's |
476 | | /// last part. A call that says no name keeps the last one said. |
477 | | #[test] |
478 | 1 | fn a_session_is_named_by_its_name_else_its_folder() { |
479 | 1 | let sessions = Sessions::default(); |
480 | 1 | let mut one = who("a", Some("s1")); |
481 | 1 | sessions.touch(&one); |
482 | 1 | assert_eq!(sessions.label(&one), None); |
483 | 1 | one.named.folder = Some("/home/me/super-voice".to_string()); |
484 | 1 | sessions.touch(&one); |
485 | 1 | assert_eq!(sessions.label(&one).as_deref(), Some("super-voice")); |
486 | 1 | assert_eq!(sessions.list()[0].name, None); |
487 | | |
488 | 1 | one.named.name = Some("Super Voice macOS".to_string()); |
489 | 1 | sessions.touch(&one); |
490 | 1 | assert_eq!(sessions.label(&one).as_deref(), Some("Super Voice macOS")); |
491 | | // The model's own call names only the session. |
492 | 1 | sessions.touch(&who("a", Some("s1"))); |
493 | 1 | assert_eq!(sessions.label(&one).as_deref(), Some("Super Voice macOS")); |
494 | 1 | assert_eq!( |
495 | 1 | sessions.list()[0].name.as_deref(), |
496 | 1 | Some("Super Voice macOS") |
497 | | ); |
498 | 1 | assert_eq!(sessions.label(&who("a", Some("s2"))), None); |
499 | 1 | } |
500 | | |
501 | | /// A connection a session named itself on names it for a call that names |
502 | | /// nothing; a newer session on it takes it over; an unnamed connection is |
503 | | /// its own session. |
504 | | #[test] |
505 | 1 | fn a_connection_names_the_session_that_last_named_itself_on_it() { |
506 | 1 | let sessions = Sessions::default(); |
507 | 1 | let caller = Caller::Key("a".to_string()); |
508 | 1 | let bare = Named::default(); |
509 | 2 | let tie = |id: &str| { |
510 | 2 | let named = sessions.resolve(&caller, who("a", Some(id)).named, Some("conn")); |
511 | 2 | sessions.touch(&Who { |
512 | 2 | caller: caller.clone(), |
513 | 2 | named, |
514 | 2 | connection: Some("conn".to_string()), |
515 | 2 | }); |
516 | 2 | }; |
517 | 1 | tie("s1"); |
518 | 1 | assert_eq!( |
519 | 1 | sessions |
520 | 1 | .resolve(&caller, bare.clone(), Some("conn")) |
521 | 1 | .id |
522 | 1 | .as_deref(), |
523 | 1 | Some("s1") |
524 | | ); |
525 | 1 | tie("s2"); |
526 | 1 | assert_eq!( |
527 | 1 | sessions |
528 | 1 | .resolve(&caller, bare.clone(), Some("conn")) |
529 | 1 | .id |
530 | 1 | .as_deref(), |
531 | 1 | Some("s2") |
532 | | ); |
533 | 1 | assert_eq!( |
534 | 1 | sessions |
535 | 1 | .resolve(&caller, bare.clone(), Some("other")) |
536 | 1 | .id |
537 | 1 | .as_deref(), |
538 | 1 | Some("other") |
539 | | ); |
540 | 1 | assert_eq!(sessions.resolve(&caller, bare, None).id, None); |
541 | 1 | } |
542 | | |
543 | | /// Each session's speech and recording are its own. |
544 | | #[test] |
545 | 1 | fn speech_and_recording_belong_to_their_session() { |
546 | 1 | let sessions = Arc::new(Sessions::default()); |
547 | 1 | let one = who("a", Some("s1")); |
548 | 1 | let two = who("a", Some("s2")); |
549 | 1 | sessions.spoke(&one, "utt_1".to_string()); |
550 | 1 | assert_eq!(sessions.utterance(&one).as_deref(), Some("utt_1")); |
551 | 1 | assert_eq!(sessions.utterance(&two), None); |
552 | | |
553 | 1 | let take = sessions.recording(&two); |
554 | 1 | assert!(sessions.is_recording(&two)); |
555 | 1 | assert!(!sessions.is_recording(&one)); |
556 | 1 | drop(take); |
557 | 1 | assert!(!sessions.is_recording(&two)); |
558 | 1 | } |
559 | | |
560 | | /// A session is used when it speaks or listens, not when it only checks |
561 | | /// in, as a plugin's heartbeat does. |
562 | | #[test] |
563 | 1 | fn a_session_is_used_when_it_speaks_or_listens() { |
564 | 1 | let sessions = Arc::new(Sessions::default()); |
565 | 1 | let speaks = who("a", Some("s1")); |
566 | 1 | let listens = who("a", Some("s2")); |
567 | 1 | sessions.open(&speaks); |
568 | 1 | sessions.touch(&speaks); |
569 | 1 | sessions.touch(&listens); |
570 | 2 | assert!1 (sessions.list().iter()1 .all(|s| s.last_used_at.is_none())); |
571 | | |
572 | 1 | sessions.spoke(&speaks, "utt_1".to_string()); |
573 | 1 | drop(sessions.recording(&listens)); |
574 | 2 | assert!1 (sessions.list().iter()1 .all(|s| s.last_used_at.is_some())); |
575 | 1 | } |
576 | | |
577 | | #[test] |
578 | 1 | fn a_session_ends_when_its_plugin_says_so_or_its_key_is_revoked() { |
579 | 1 | let sessions = Sessions::default(); |
580 | 1 | let one = who("a", Some("s1")); |
581 | 1 | sessions.open(&one); |
582 | 1 | sessions.touch(&who("a", Some("s2"))); |
583 | 1 | sessions.touch(&who("b", None)); |
584 | 1 | sessions.close(&one); |
585 | 1 | assert_eq!(sessions.list().len(), 2); |
586 | 1 | sessions.forget(&Caller::Key("a".to_string())); |
587 | 1 | let list = sessions.list(); |
588 | 1 | assert_eq!(list.len(), 1); |
589 | 1 | assert_eq!(list[0].id, None); |
590 | 1 | } |
591 | | |
592 | | /// One its plugin opened leaves the list after a few quiet minutes, one |
593 | | /// nothing opened after half an hour, and one recording stays. Off the |
594 | | /// list, a session still holds its turn, until a day has gone by. |
595 | | #[test] |
596 | 1 | fn quiet_sessions_leave_the_list_and_are_forgotten_later() { |
597 | 1 | let sessions = Arc::new(Sessions::default()); |
598 | 1 | let opened = who("a", Some("opened")); |
599 | 1 | let seen = who("a", Some("seen")); |
600 | 1 | let listening = who("a", Some("listening")); |
601 | 1 | sessions.open(&opened); |
602 | 1 | sessions.touch(&seen); |
603 | 1 | sessions.set_spoken(&seen, false); |
604 | 1 | let take = sessions.recording(&listening); |
605 | 3 | let listed = |at| { |
606 | 3 | sessions |
607 | 3 | .list_at(at) |
608 | 3 | .into_iter() |
609 | 6 | .filter_map(|session| session.id) |
610 | | .collect::<Vec<_>>() |
611 | 3 | }; |
612 | | |
613 | 1 | let now = Instant::now(); |
614 | 1 | assert_eq!(listed(now + OPENED_IDLE / 2).len(), 3); |
615 | 1 | assert_eq!( |
616 | 1 | listed(now + OPENED_IDLE + Duration::from_secs(1)), |
617 | 1 | ["seen", "listening"] |
618 | | ); |
619 | 1 | let after_half_an_hour = now + SEEN_IDLE + Duration::from_secs(1); |
620 | 1 | assert_eq!(listed(after_half_an_hour), ["listening"]); |
621 | | // A typed turn stays typed off the list: a long task's speech at |
622 | | // its end is still refused. |
623 | 1 | assert_eq!(sessions.sweep(after_half_an_hour), 0); |
624 | 1 | assert_eq!(sessions.spoken(&seen), Some(false)); |
625 | | |
626 | 1 | drop(take); |
627 | 1 | let after_a_day = now + REMEMBERED + Duration::from_secs(1); |
628 | 1 | assert_eq!(sessions.sweep(after_a_day), 3); |
629 | 1 | assert_eq!(sessions.spoken(&seen), None); |
630 | 1 | } |
631 | | |
632 | | /// A session keeps its last replies, the last one first, each once, and |
633 | | /// says how many it has when asked for one further back. |
634 | | #[test] |
635 | 1 | fn a_session_keeps_its_last_replies() { |
636 | 1 | let sessions = Sessions::default(); |
637 | 1 | let one = who("a", Some("s1")); |
638 | 1 | assert_eq!(sessions.reply(&one, 0), Err(0)); |
639 | 1 | sessions.replied(&one, " First. "); |
640 | 1 | sessions.replied(&one, "Second."); |
641 | | // A turn that ended before the model ran says its last reply again. |
642 | 1 | sessions.replied(&one, "Second."); |
643 | 1 | sessions.replied(&one, " "); |
644 | 1 | assert_eq!(sessions.reply(&one, 0).as_deref(), Ok("Second.")); |
645 | 1 | assert_eq!(sessions.reply(&one, 1).as_deref(), Ok("First.")); |
646 | 1 | assert_eq!(sessions.reply(&one, 2), Err(2)); |
647 | 1 | assert_eq!(sessions.reply(&who("a", Some("s2")), 0), Err(0)); |
648 | | |
649 | 1 | for reply in 0..READ_BACK + 2 { |
650 | 12 | sessions.replied(&one, &format!("Reply {reply}.")); |
651 | | } |
652 | 1 | assert_eq!(sessions.reply(&one, 0).as_deref(), Ok("Reply 11.")); |
653 | 1 | assert_eq!( |
654 | 1 | sessions.reply(&one, READ_BACK - 1).as_deref(), |
655 | 1 | Ok("Reply 2.") |
656 | | ); |
657 | 1 | assert_eq!(sessions.reply(&one, READ_BACK), Err(READ_BACK)); |
658 | 1 | } |
659 | | |
660 | | /// A session's turn is spoken or typed once its plugin says, and the next |
661 | | /// prompt says again. |
662 | | #[test] |
663 | 1 | fn a_turn_is_spoken_or_typed() { |
664 | 1 | let sessions = Sessions::default(); |
665 | 1 | let one = who("a", Some("s1")); |
666 | 1 | assert_eq!(sessions.spoken(&one), None); |
667 | 1 | sessions.set_spoken(&one, true); |
668 | 1 | assert_eq!(sessions.spoken(&one), Some(true)); |
669 | 1 | sessions.set_spoken(&one, false); |
670 | 1 | assert_eq!(sessions.spoken(&one), Some(false)); |
671 | 1 | assert_eq!(sessions.spoken(&who("a", Some("s2"))), None); |
672 | 1 | } |
673 | | } |