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
273
274
275
276
277
278
279
//! Agent routing engine.
//!
//! Routes inbound messages to the appropriate agent based on channel
//! and user match criteria. Supports first-match-wins semantics,
//! catch-all fallback, and automatic workspace creation.
//!
//! # Routing algorithm
//!
//! 1. Walk routes in order; first match wins.
//! 2. If no route matches and a catch-all is configured, use it.
//! 3. If no route matches and no catch-all: reject with error.
//! 4. Anonymous messages (empty `sender_id`) route to catch-all or
//! a dedicated "anonymous" agent with reduced permissions.
use tracing::warn;
use clawft_types::agent_routing::{AgentRoute, AgentRoutingConfig};
use clawft_types::event::InboundMessage;
/// Result of routing an inbound message to an agent.
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RoutingResult {
/// Message routed to a specific agent by rule match.
Agent(String),
/// Message routed to the catch-all agent.
CatchAll(String),
/// No matching route and no catch-all configured.
NoMatch,
}
/// Engine that routes inbound messages to agent instances.
///
/// Created from an [`AgentRoutingConfig`] loaded from `clawft.toml`.
/// Routes are evaluated in order; first match wins.
pub struct AgentRouter {
routes: Vec<AgentRoute>,
catch_all: Option<String>,
}
impl AgentRouter {
/// Create a new router from configuration.
pub fn new(config: AgentRoutingConfig) -> Self {
Self {
routes: config.routes,
catch_all: config.catch_all,
}
}
/// Create an empty router with no rules.
pub fn empty() -> Self {
Self {
routes: Vec::new(),
catch_all: None,
}
}
/// Create a router with only a catch-all agent (no rules).
pub fn with_catch_all(agent: impl Into<String>) -> Self {
Self {
routes: Vec::new(),
catch_all: Some(agent.into()),
}
}
/// Route a message to the appropriate agent ID.
///
/// # Routing semantics
///
/// - First matching rule wins.
/// - Anonymous messages (empty `sender_id`) are routed to the
/// catch-all agent (if configured).
/// - No match + no catch-all: returns [`RoutingResult::NoMatch`]
/// and logs a warning.
pub fn route(&self, msg: &InboundMessage) -> RoutingResult {
// Anonymous messages go straight to catch-all.
if msg.sender_id.is_empty() {
return self.route_anonymous(msg);
}
// Walk rules in order; first match wins.
for rule in &self.routes {
if rule.channel == msg.channel
&& rule.match_criteria.matches(&msg.sender_id, &msg.chat_id)
{
return RoutingResult::Agent(rule.agent.clone());
}
}
// No rule matched.
if let Some(ref catch_all) = self.catch_all {
RoutingResult::CatchAll(catch_all.clone())
} else {
warn!(
channel = %msg.channel,
sender_id = %msg.sender_id,
chat_id = %msg.chat_id,
"no agent configured for this channel/user"
);
RoutingResult::NoMatch
}
}
/// Route an anonymous message (empty sender_id).
fn route_anonymous(&self, msg: &InboundMessage) -> RoutingResult {
if let Some(ref catch_all) = self.catch_all {
RoutingResult::CatchAll(catch_all.clone())
} else {
warn!(
channel = %msg.channel,
chat_id = %msg.chat_id,
"anonymous message with no catch-all configured"
);
RoutingResult::NoMatch
}
}
/// Number of configured routes.
pub fn route_count(&self) -> usize {
self.routes.len()
}
/// Whether a catch-all agent is configured.
pub fn has_catch_all(&self) -> bool {
self.catch_all.is_some()
}
}
#[cfg(test)]
mod tests {
use super::*;
use chrono::Utc;
use clawft_types::agent_routing::{AgentRoute, AgentRoutingConfig, MatchCriteria};
use std::collections::HashMap;
fn make_msg(channel: &str, sender_id: &str, chat_id: &str) -> InboundMessage {
InboundMessage {
channel: channel.into(),
sender_id: sender_id.into(),
chat_id: chat_id.into(),
content: "test".into(),
timestamp: Utc::now(),
media: vec![],
metadata: HashMap::new(),
}
}
fn make_router() -> AgentRouter {
AgentRouter::new(AgentRoutingConfig {
routes: vec![
AgentRoute {
channel: "telegram".into(),
match_criteria: MatchCriteria {
user_id: Some("user123".into()),
..Default::default()
},
agent: "work-agent".into(),
},
AgentRoute {
channel: "whatsapp".into(),
match_criteria: MatchCriteria {
phone: Some("+15551234".into()),
..Default::default()
},
agent: "personal-agent".into(),
},
AgentRoute {
channel: "slack".into(),
match_criteria: MatchCriteria::default(),
agent: "slack-agent".into(),
},
],
catch_all: Some("default-agent".into()),
})
}
#[test]
fn route_first_match_wins() {
let router = make_router();
let msg = make_msg("telegram", "user123", "chat1");
assert_eq!(
router.route(&msg),
RoutingResult::Agent("work-agent".into())
);
}
#[test]
fn route_whatsapp_by_phone() {
let router = make_router();
let msg = make_msg("whatsapp", "+15551234", "chat2");
assert_eq!(
router.route(&msg),
RoutingResult::Agent("personal-agent".into())
);
}
#[test]
fn route_wildcard_match() {
let router = make_router();
// Slack rule has empty match criteria (wildcard).
let msg = make_msg("slack", "any_user", "any_chat");
assert_eq!(
router.route(&msg),
RoutingResult::Agent("slack-agent".into())
);
}
#[test]
fn route_no_match_falls_to_catch_all() {
let router = make_router();
let msg = make_msg("discord", "user456", "chat3");
assert_eq!(
router.route(&msg),
RoutingResult::CatchAll("default-agent".into())
);
}
#[test]
fn route_no_match_no_catch_all() {
let router = AgentRouter::new(AgentRoutingConfig {
routes: vec![AgentRoute {
channel: "telegram".into(),
match_criteria: MatchCriteria {
user_id: Some("user123".into()),
..Default::default()
},
agent: "work-agent".into(),
}],
catch_all: None,
});
let msg = make_msg("discord", "user456", "chat3");
assert_eq!(router.route(&msg), RoutingResult::NoMatch);
}
#[test]
fn route_anonymous_to_catch_all() {
let router = make_router();
let msg = make_msg("telegram", "", "chat1");
assert_eq!(
router.route(&msg),
RoutingResult::CatchAll("default-agent".into())
);
}
#[test]
fn route_anonymous_no_catch_all() {
let router = AgentRouter::empty();
let msg = make_msg("telegram", "", "chat1");
assert_eq!(router.route(&msg), RoutingResult::NoMatch);
}
#[test]
fn route_wrong_channel_no_match() {
let router = make_router();
// user123 exists but on telegram, not discord.
let msg = make_msg("discord", "user123", "chat1");
assert_eq!(
router.route(&msg),
RoutingResult::CatchAll("default-agent".into())
);
}
#[test]
fn empty_router() {
let router = AgentRouter::empty();
assert_eq!(router.route_count(), 0);
assert!(!router.has_catch_all());
}
#[test]
fn with_catch_all_only() {
let router = AgentRouter::with_catch_all("fallback");
let msg = make_msg("any", "user", "chat");
assert_eq!(
router.route(&msg),
RoutingResult::CatchAll("fallback".into())
);
}
}