agentmux_srv/server/
quit_question.rs1use axum::extract::State;
13use axum::response::Json;
14use serde_json::{json, Value};
15
16use super::AppState;
17
18const CONFIRM_SETTING: &str = "window:confirmcloseworking";
20
21pub(crate) fn question(agents: &[(String, bool)], confirm: bool) -> Value {
24 let mut working: Vec<&str> = agents.iter().filter(|(_, working)| *working).map(|(name, _)| name.as_str()).collect();
25 working.sort_unstable();
26 json!({ "ask": confirm && !working.is_empty(), "working": working, "agents": agents.len() })
27}
28
29pub(super) async fn handle_quit_question(State(state): State<AppState>) -> Json<Value> {
30 let agents: Vec<(String, bool)> = crate::backend::blockcontroller::get_all_controllers()
31 .keys()
32 .filter_map(|block_id| super::service::session_closed::agent_of(&state.mstore, block_id))
33 .collect();
34 let confirm = state.config_watcher.get_full_config().settings.extra.get(CONFIRM_SETTING) != Some(&Value::Bool(false));
35 Json(question(&agents, confirm))
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 fn agents(list: &[(&str, bool)]) -> Vec<(String, bool)> {
43 list.iter().map(|(name, working)| (name.to_string(), *working)).collect()
44 }
45
46 #[test]
47 fn it_asks_only_when_an_agent_is_mid_turn_and_names_those() {
48 let q = question(&agents(&[("Loap", true), ("Korp", true), ("Camper", false)]), true);
49 assert_eq!(q, json!({ "ask": true, "working": ["Korp", "Loap"], "agents": 3 }));
50 let idle = question(&agents(&[("Camper", false)]), true);
51 assert_eq!(idle, json!({ "ask": false, "working": [], "agents": 1 }));
52 assert_eq!(question(&[], true), json!({ "ask": false, "working": [], "agents": 0 }));
53 }
54
55 #[test]
56 fn dont_ask_again_turns_the_question_off_and_still_says_who_is_working() {
57 let q = question(&agents(&[("Korp", true)]), false);
58 assert_eq!(q, json!({ "ask": false, "working": ["Korp"], "agents": 1 }));
59 }
60}