implement broadcast messaging: send to '*' reaches all agents with hint
This commit is contained in:
parent
a57e500f48
commit
abcf7a0c41
4 changed files with 41 additions and 11 deletions
|
|
@ -102,15 +102,42 @@ async fn dispatch(req: &AgentRequest, agent: &str, coord: &Arc<Coordinator>) ->
|
|||
let broker = &coord.broker;
|
||||
match req {
|
||||
AgentRequest::Send { to, body } => {
|
||||
match broker.send(&Message {
|
||||
from: agent.to_owned(),
|
||||
to: to.clone(),
|
||||
body: body.clone(),
|
||||
}) {
|
||||
Ok(()) => AgentResponse::Ok,
|
||||
Err(e) => AgentResponse::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
// Handle broadcast sends (recipient = "*")
|
||||
if to == "*" {
|
||||
let agents = coord.list_agents();
|
||||
let broadcast_hint = "\n\n⚠️ _hint: this was a broadcast and may not need any action from you_";
|
||||
let broadcast_body = format!("{}{}", body, broadcast_hint);
|
||||
let mut errors = Vec::new();
|
||||
|
||||
for agent_name in agents {
|
||||
if let Err(e) = broker.send(&Message {
|
||||
from: agent.to_owned(),
|
||||
to: agent_name.clone(),
|
||||
body: broadcast_body.clone(),
|
||||
}) {
|
||||
errors.push(format!("{}: {e}", agent_name));
|
||||
}
|
||||
}
|
||||
|
||||
if errors.is_empty() {
|
||||
AgentResponse::Ok
|
||||
} else {
|
||||
AgentResponse::Err {
|
||||
message: format!("broadcast failed for agents: {}", errors.join(", ")),
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Normal unicast send
|
||||
match broker.send(&Message {
|
||||
from: agent.to_owned(),
|
||||
to: to.clone(),
|
||||
body: body.clone(),
|
||||
}) {
|
||||
Ok(()) => AgentResponse::Ok,
|
||||
Err(e) => AgentResponse::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
AgentRequest::Recv { wait_seconds } => match broker
|
||||
|
|
|
|||
|
|
@ -117,6 +117,9 @@ impl Coordinator {
|
|||
let _ = std::fs::remove_file(&socket.path);
|
||||
}
|
||||
}
|
||||
pub fn list_agents(&self) -> Vec<String> {
|
||||
self.agents.lock().unwrap().keys().cloned().collect()
|
||||
}
|
||||
|
||||
/// Mark an agent as in-progress (only one state per agent for now).
|
||||
pub fn set_transient(&self, name: &str, kind: TransientKind) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue