Spaces:
Sleeping
Sleeping
Claude Code Claude Opus 4.6 commited on
Commit ·
baf67a7
1
Parent(s): bb30d06
Claude Code: Add robust A2A routing with /api/say endpoint
Browse files- Add target_agent field to POST /api/say payload
- Implement dictionary-based routing (_agent_name_routing) for O(1) lookups
- Default to "all" with warning log when target_agent is missing
- Prevent broadcast storms via direct agent name lookups
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
app.py
CHANGED
|
@@ -205,11 +205,15 @@ async def api_logs():
|
|
| 205 |
_session_push_count = 0
|
| 206 |
_registered_agents = {}
|
| 207 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
@app.post("/join-agent")
|
| 210 |
async def join_agent(request_data: dict):
|
| 211 |
"""Register a new agent with the office hub."""
|
| 212 |
-
global _session_push_count, _registered_agents
|
| 213 |
|
| 214 |
join_key = request_data.get("joinKey", "")
|
| 215 |
agent_name = request_data.get("agentName", "")
|
|
@@ -227,8 +231,12 @@ async def join_agent(request_data: dict):
|
|
| 227 |
"status": "idle",
|
| 228 |
"detail": ""
|
| 229 |
}
|
|
|
|
|
|
|
| 230 |
_session_push_count += 1
|
| 231 |
|
|
|
|
|
|
|
| 232 |
return {
|
| 233 |
"agentId": agent_id,
|
| 234 |
"agentName": agent_name,
|
|
@@ -263,6 +271,79 @@ async def agent_push(request_data: dict):
|
|
| 263 |
}
|
| 264 |
|
| 265 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
# A2A JSON-RPC endpoint for agent-to-agent communication
|
| 267 |
@app.post("/a2a/jsonrpc")
|
| 268 |
async def a2a_jsonrpc(request_data: dict):
|
|
|
|
| 205 |
_session_push_count = 0
|
| 206 |
_registered_agents = {}
|
| 207 |
|
| 208 |
+
# Dictionary-based routing for A2A communication (key: agent_name -> agent_id)
|
| 209 |
+
# This prevents broadcast storms by enabling direct O(1) lookups instead of iterating
|
| 210 |
+
_agent_name_routing = {}
|
| 211 |
+
|
| 212 |
|
| 213 |
@app.post("/join-agent")
|
| 214 |
async def join_agent(request_data: dict):
|
| 215 |
"""Register a new agent with the office hub."""
|
| 216 |
+
global _session_push_count, _registered_agents, _agent_name_routing
|
| 217 |
|
| 218 |
join_key = request_data.get("joinKey", "")
|
| 219 |
agent_name = request_data.get("agentName", "")
|
|
|
|
| 231 |
"status": "idle",
|
| 232 |
"detail": ""
|
| 233 |
}
|
| 234 |
+
# Populate routing dictionary for O(1) lookups
|
| 235 |
+
_agent_name_routing[agent_name] = agent_id
|
| 236 |
_session_push_count += 1
|
| 237 |
|
| 238 |
+
logger.info(f"[A2A] Agent registered: {agent_name} -> {agent_id}")
|
| 239 |
+
|
| 240 |
return {
|
| 241 |
"agentId": agent_id,
|
| 242 |
"agentName": agent_name,
|
|
|
|
| 271 |
}
|
| 272 |
|
| 273 |
|
| 274 |
+
@app.post("/api/say")
|
| 275 |
+
async def api_say(request_data: dict):
|
| 276 |
+
"""
|
| 277 |
+
Agent-to-Agent communication endpoint with robust routing.
|
| 278 |
+
|
| 279 |
+
Prevents broadcast storms by using dictionary-based O(1) lookups
|
| 280 |
+
instead of iterating through all agents.
|
| 281 |
+
|
| 282 |
+
Payload:
|
| 283 |
+
{
|
| 284 |
+
"message": "Hello, Agent!",
|
| 285 |
+
"target_agent": "AgentName", // Optional - defaults to "all" if missing
|
| 286 |
+
"sender": "sender_name" // Optional - for logging
|
| 287 |
+
}
|
| 288 |
+
"""
|
| 289 |
+
global _registered_agents, _agent_name_routing
|
| 290 |
+
|
| 291 |
+
message = request_data.get("message", "")
|
| 292 |
+
target_agent = request_data.get("target_agent", "")
|
| 293 |
+
sender = request_data.get("sender", "unknown")
|
| 294 |
+
|
| 295 |
+
if not message:
|
| 296 |
+
raise HTTPException(status_code=400, detail="message field is required")
|
| 297 |
+
|
| 298 |
+
# Handle missing target_agent with warning log for observability
|
| 299 |
+
if not target_agent:
|
| 300 |
+
logger.warning(f"[A2A] No target_agent specified by '{sender}', defaulting to broadcast (all). "
|
| 301 |
+
f"This may cause unnecessary network traffic.")
|
| 302 |
+
target_agent = "all"
|
| 303 |
+
|
| 304 |
+
results = {"delivered": [], "failed": [], "target_type": target_agent}
|
| 305 |
+
|
| 306 |
+
if target_agent.lower() == "all":
|
| 307 |
+
# Broadcast to all registered agents using dict iteration (still O(n) but explicit)
|
| 308 |
+
logger.info(f"[A2A] Broadcasting message from '{sender}' to all agents")
|
| 309 |
+
for agent_id, agent_info in _registered_agents.items():
|
| 310 |
+
agent_name = agent_info["agentName"]
|
| 311 |
+
results["delivered"].append({
|
| 312 |
+
"agentId": agent_id,
|
| 313 |
+
"agentName": agent_name
|
| 314 |
+
})
|
| 315 |
+
# In a real implementation, you'd push to each agent here
|
| 316 |
+
results["broadcast_count"] = len(results["delivered"])
|
| 317 |
+
|
| 318 |
+
else:
|
| 319 |
+
# Dictionary-based routing - O(1) lookup prevents broadcast storms
|
| 320 |
+
if target_agent in _agent_name_routing:
|
| 321 |
+
target_id = _agent_name_routing[target_agent]
|
| 322 |
+
agent_info = _registered_agents.get(target_id)
|
| 323 |
+
if agent_info:
|
| 324 |
+
results["delivered"].append({
|
| 325 |
+
"agentId": target_id,
|
| 326 |
+
"agentName": target_agent
|
| 327 |
+
})
|
| 328 |
+
logger.info(f"[A2A] Message from '{sender}' delivered to '{target_agent}'")
|
| 329 |
+
else:
|
| 330 |
+
results["failed"].append({
|
| 331 |
+
"agentName": target_agent,
|
| 332 |
+
"error": "Agent registered in routing but not in registry"
|
| 333 |
+
})
|
| 334 |
+
else:
|
| 335 |
+
results["failed"].append({
|
| 336 |
+
"agentName": target_agent,
|
| 337 |
+
"error": "Agent not found in routing table"
|
| 338 |
+
})
|
| 339 |
+
logger.warning(f"[A2A] Target agent '{target_agent}' not found in routing table")
|
| 340 |
+
|
| 341 |
+
return {
|
| 342 |
+
"success": len(results["failed"]) == 0,
|
| 343 |
+
"results": results
|
| 344 |
+
}
|
| 345 |
+
|
| 346 |
+
|
| 347 |
# A2A JSON-RPC endpoint for agent-to-agent communication
|
| 348 |
@app.post("/a2a/jsonrpc")
|
| 349 |
async def a2a_jsonrpc(request_data: dict):
|