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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue