agentmux_srv_core/backend/
note_outbox.rs1use std::collections::HashMap;
18use std::sync::LazyLock;
19
20use crate::backend::storage::store::Store;
21use crate::backend::wconfig::ConfigState;
22
23pub(crate) const WINDOW_MS: u64 = 20_000;
25
26#[derive(Debug, Clone, PartialEq, Eq)]
28pub(crate) struct Line {
29 pub agent: String,
31 pub text: String,
33 pub held_key: Option<String>,
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
40pub(crate) struct Out {
41 pub leader: String,
42 pub text: String,
43 pub wait_key: Option<String>,
44}
45
46#[derive(Debug)]
47struct Window {
48 opened_ms: u64,
49 lines: Vec<Line>,
50}
51
52#[derive(Debug, Default)]
54pub(crate) struct Outbox {
55 windows: HashMap<String, Window>,
56}
57
58impl Outbox {
59 pub fn push(&mut self, leader: &str, line: Line, now_ms: u64) -> Option<Out> {
62 match self.windows.get_mut(leader) {
63 Some(w) => {
64 match w.lines.iter_mut().find(|l| l.agent == line.agent) {
65 Some(have) => *have = line,
66 None => w.lines.push(line),
67 }
68 None
69 }
70 None if line.held_key.is_none() => Some(compose(leader, now_ms, &[line])),
71 None => {
72 self.windows.insert(leader.to_string(), Window { opened_ms: now_ms, lines: vec![line] });
73 None
74 }
75 }
76 }
77
78 pub fn take_due(&mut self, now_ms: u64) -> Vec<Out> {
80 let due: Vec<String> = self.windows.iter().filter(|(_, w)| now_ms.saturating_sub(w.opened_ms) >= WINDOW_MS).map(|(k, _)| k.clone()).collect();
81 let mut out: Vec<Out> = due.into_iter().filter_map(|leader| self.windows.remove(&leader).map(|w| compose(&leader, w.opened_ms, &w.lines))).collect();
82 out.sort_by(|a, b| a.leader.cmp(&b.leader));
83 out
84 }
85
86 pub fn is_empty(&self) -> bool {
88 self.windows.is_empty()
89 }
90}
91
92pub(crate) fn compose(leader: &str, opened_ms: u64, lines: &[Line]) -> Out {
96 if let [only] = lines {
97 return Out { leader: leader.to_string(), text: format!("[AgentMux] {}", only.text), wait_key: only.held_key.clone() };
98 }
99 let mut text = format!("[AgentMux] {} of your agents need a look:", lines.len());
100 for l in lines {
101 text.push_str("\n- ");
102 text.push_str(&l.text);
103 }
104 let wait_key = lines.iter().all(|l| l.held_key.is_some()).then(|| format!("group:{leader}:{opened_ms}"));
105 Out { leader: leader.to_string(), text, wait_key }
106}
107
108static OUTBOX: LazyLock<parking_lot::Mutex<Outbox>> = LazyLock::new(Default::default);
109
110pub(crate) fn queue(mstore: &Store, config: &ConfigState, leaders: &[String], line: &Line, now_ms: u64) {
112 let now_out: Vec<Out> = {
113 let mut outbox = OUTBOX.lock();
114 leaders.iter().filter_map(|leader| outbox.push(leader, line.clone(), now_ms)).collect()
115 };
116 send(mstore, config, now_out);
117}
118
119pub(crate) fn flush_due(mstore: &Store, config: &ConfigState, now_ms: u64) {
121 let due = {
122 let mut outbox = OUTBOX.lock();
123 if outbox.is_empty() {
124 return;
125 }
126 outbox.take_due(now_ms)
127 };
128 send(mstore, config, due);
129}
130
131fn send(mstore: &Store, config: &ConfigState, notes: Vec<Out>) {
132 for n in notes {
133 super::worker_notes::deliver(mstore, config, &n.leader, &n.text, n.wait_key.as_deref());
134 }
135}
136
137#[cfg(test)]
138#[path = "note_outbox_tests.rs"]
139mod tests;