1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
use std::collections::{HashSet, VecDeque};
use serde::{Deserialize, Serialize};
use crate::actions::AgiAction;
use crate::events::EventType;
use crate::state::DispatchState;
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", content = "events", rename_all = "snake_case")]
pub enum EventSpec {
One(EventType),
Any(Vec<EventType>),
All(Vec<EventType>),
}
impl EventSpec {
pub fn is_satisfied(&self, seen: &HashSet<EventType>) -> bool {
match self {
Self::One(event) => seen.contains(event),
Self::Any(events) => events.iter().any(|event| seen.contains(event)),
Self::All(events) => events.iter().all(|event| seen.contains(event)),
}
}
}
pub type ActionFactory = Box<dyn FnOnce(&DispatchState) -> Option<AgiAction> + Send + 'static>;
pub struct WaitingRule {
pub spec: EventSpec,
make_action: ActionFactory,
}
impl WaitingRule {
pub fn new<F>(spec: EventSpec, make_action: F) -> Self
where
F: FnOnce(&DispatchState) -> Option<AgiAction> + Send + 'static,
{
Self {
spec,
make_action: Box::new(make_action),
}
}
pub fn one<F>(event: EventType, make_action: F) -> Self
where
F: FnOnce(&DispatchState) -> Option<AgiAction> + Send + 'static,
{
Self::new(EventSpec::One(event), make_action)
}
pub fn any<F>(events: Vec<EventType>, make_action: F) -> Self
where
F: FnOnce(&DispatchState) -> Option<AgiAction> + Send + 'static,
{
Self::new(EventSpec::Any(events), make_action)
}
pub fn all<F>(events: Vec<EventType>, make_action: F) -> Self
where
F: FnOnce(&DispatchState) -> Option<AgiAction> + Send + 'static,
{
Self::new(EventSpec::All(events), make_action)
}
}
#[derive(Default)]
pub struct WaitingEngine {
rules: VecDeque<WaitingRule>,
seen: HashSet<EventType>,
}
impl WaitingEngine {
pub fn new() -> Self {
Self::default()
}
pub fn record(&mut self, event: EventType) {
self.seen.insert(event);
}
pub fn forget(&mut self, event: EventType) {
self.seen.remove(&event);
}
pub fn push(&mut self, rule: WaitingRule) {
self.rules.push_back(rule);
}
pub fn len(&self) -> usize {
self.rules.len()
}
pub fn is_empty(&self) -> bool {
self.rules.is_empty()
}
/// Fire every rule whose condition currently holds. Fired rules are
/// removed; the rest stay queued in their original order.
pub fn drain_ready(&mut self, state: &DispatchState) -> Vec<AgiAction> {
let mut actions = Vec::new();
let mut pending = VecDeque::new();
while let Some(rule) = self.rules.pop_front() {
if rule.spec.is_satisfied(&self.seen) {
if let Some(action) = (rule.make_action)(state) {
actions.push(action);
}
} else {
pending.push_back(rule);
}
}
self.rules = pending;
actions
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::actions::{NewThreadAction, RunThreadAction};
fn new_thread_action(state: &DispatchState) -> Option<AgiAction> {
state
.active_agent
.clone()
.map(|agent_id| AgiAction::NewThread(NewThreadAction { agent_id }))
}
#[test]
fn one_event_matches_after_it_is_seen() {
let mut engine = WaitingEngine::new();
engine.push(WaitingRule::one(
EventType::NewAgentCreated,
new_thread_action,
));
engine.record(EventType::NewThreadCreated);
assert!(engine
.drain_ready(&DispatchState {
active_agent: Some("a1".to_string()),
active_thread: None,
})
.is_empty());
engine.record(EventType::NewAgentCreated);
let actions = engine.drain_ready(&DispatchState {
active_agent: Some("a1".to_string()),
active_thread: None,
});
assert_eq!(
actions,
vec![AgiAction::NewThread(NewThreadAction {
agent_id: "a1".to_string(),
})]
);
assert!(engine.is_empty());
}
#[test]
fn any_and_all_specs_work() {
let mut any_engine = WaitingEngine::new();
any_engine.push(WaitingRule::any(
vec![
EventType::NewAgentCreated,
EventType::ExistingAgentRetrieved,
],
new_thread_action,
));
any_engine.record(EventType::ExistingAgentRetrieved);
assert_eq!(
any_engine
.drain_ready(&DispatchState {
active_agent: Some("a1".to_string()),
active_thread: None,
})
.len(),
1
);
let mut all_engine = WaitingEngine::new();
all_engine.push(WaitingRule::all(
vec![EventType::NewThreadCreated, EventType::ThreadMessageAdded],
|state| {
Some(AgiAction::RunThread(RunThreadAction {
agent_id: state.active_agent.clone()?,
thread_id: state.active_thread.clone()?,
}))
},
));
all_engine.record(EventType::NewThreadCreated);
assert!(all_engine
.drain_ready(&DispatchState {
active_agent: Some("a1".to_string()),
active_thread: Some("t1".to_string()),
})
.is_empty());
all_engine.record(EventType::ThreadMessageAdded);
assert_eq!(
all_engine
.drain_ready(&DispatchState {
active_agent: Some("a1".to_string()),
active_thread: Some("t1".to_string()),
})
.len(),
1
);
}
#[test]
fn forget_blocks_stale_matches() {
let mut engine = WaitingEngine::new();
engine.record(EventType::ThreadMessageAdded);
engine.push(WaitingRule::one(
EventType::ThreadMessageAdded,
new_thread_action,
));
engine.forget(EventType::ThreadMessageAdded);
assert!(engine
.drain_ready(&DispatchState {
active_agent: Some("a1".to_string()),
active_thread: None,
})
.is_empty());
assert_eq!(engine.len(), 1);
}
#[test]
fn rules_fire_in_insertion_order() {
let mut engine = WaitingEngine::new();
engine.record(EventType::NewAgentCreated);
let state = DispatchState {
active_agent: Some("a1".to_string()),
active_thread: None,
};
engine.push(WaitingRule::one(EventType::NewAgentCreated, |_| {
Some(AgiAction::NewThread(NewThreadAction {
agent_id: "first".to_string(),
}))
}));
engine.push(WaitingRule::one(EventType::NewAgentCreated, |_| {
Some(AgiAction::NewThread(NewThreadAction {
agent_id: "second".to_string(),
}))
}));
let actions = engine.drain_ready(&state);
assert_eq!(
actions,
vec![
AgiAction::NewThread(NewThreadAction {
agent_id: "first".to_string(),
}),
AgiAction::NewThread(NewThreadAction {
agent_id: "second".to_string(),
}),
]
);
}
#[test]
fn factory_returning_none_consumes_the_rule() {
let mut engine = WaitingEngine::new();
engine.record(EventType::NewAgentCreated);
engine.push(WaitingRule::one(EventType::NewAgentCreated, |_| None));
assert!(engine.drain_ready(&DispatchState::default()).is_empty());
assert!(engine.is_empty());
}
}