codetether-agent 4.0.0

A2A-native AI coding agent for the CodeTether ecosystem
Documentation
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//! Relay AutoChat Tool - Autonomous relay communication between agents
//!
//! Enables task delegation and result aggregation between agents using
//! the protocol-first relay runtime. This tool allows LLMs to trigger
//! agent handoffs and coordinate multi-agent workflows.

use super::{Tool, ToolResult};
use crate::bus::AgentBus;
use crate::bus::relay::ProtocolRelayRuntime;
use anyhow::Result;
use async_trait::async_trait;
use parking_lot::RwLock;
use serde_json::{Value, json};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::OnceCell;
use uuid::Uuid;

lazy_static::lazy_static! {
    static ref RELAY_STORE: RwLock<HashMap<String, Arc<ProtocolRelayRuntime>>> = RwLock::new(HashMap::new());
    static ref AGENT_BUS: OnceCell<Arc<AgentBus>> = OnceCell::const_new();
}

async fn get_agent_bus() -> Result<Arc<AgentBus>> {
    let bus = AGENT_BUS
        .get_or_try_init(|| async {
            let bus = AgentBus::new().into_arc();
            Ok::<_, anyhow::Error>(bus)
        })
        .await?;
    Ok(bus.clone())
}

pub struct RelayAutoChatTool;

impl RelayAutoChatTool {
    pub fn new() -> Self {
        Self
    }
}

#[async_trait]
impl Tool for RelayAutoChatTool {
    fn id(&self) -> &str {
        "relay_autochat"
    }

    fn name(&self) -> &str {
        "Relay AutoChat"
    }

    fn description(&self) -> &str {
        "Autonomous relay communication between agents for task delegation and result aggregation. \
         Actions: delegate (send task to target agent), handoff (pass context between agents), \
         status (check relay status), list_agents (show available agents in relay), \
         init (initialize a new relay with task), complete (finish relay and aggregate results)."
    }

    fn parameters(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["delegate", "handoff", "status", "list_agents", "init", "complete"],
                    "description": "Action to perform"
                },
                "target_agent": {
                    "type": "string",
                    "description": "Target agent name for delegation/handoff"
                },
                "message": {
                    "type": "string",
                    "description": "Message to send to the target agent"
                },
                "context": {
                    "type": "object",
                    "description": "Additional context to pass along (JSON object)"
                },
                "relay_id": {
                    "type": "string",
                    "description": "Relay ID to use (auto-generated if not provided)"
                },
                "okr_id": {
                    "type": "string",
                    "description": "Optional OKR ID to associate with this relay"
                },
                "task": {
                    "type": "string",
                    "description": "Task description for initializing a new relay"
                }
            },
            "required": ["action"]
        })
    }

    async fn execute(&self, params: Value) -> Result<ToolResult> {
        let action = match params.get("action").and_then(|v| v.as_str()) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "action is required. Valid actions: init, delegate, handoff, status, list_agents, complete",
                    Some(vec!["action"]),
                    Some(json!({
                        "action": "init",
                        "task": "description of the relay task"
                    })),
                ));
            }
        };

        let relay_id = params
            .get("relay_id")
            .and_then(|v| v.as_str())
            .map(String::from);
        let target_agent = params
            .get("target_agent")
            .and_then(|v| v.as_str())
            .map(String::from);
        let message = params
            .get("message")
            .and_then(|v| v.as_str())
            .map(String::from);
        let context = params.get("context").cloned();
        let okr_id = params
            .get("okr_id")
            .and_then(|v| v.as_str())
            .map(String::from);
        let task = params
            .get("task")
            .and_then(|v| v.as_str())
            .map(String::from);

        match action.as_str() {
            "init" => self.init_relay(relay_id, task, context, okr_id).await,
            "delegate" => {
                self.delegate_task(relay_id, target_agent, message, context, okr_id)
                    .await
            }
            "handoff" => {
                self.handoff_context(relay_id, target_agent, message, context)
                    .await
            }
            "status" => self.get_status(relay_id).await,
            "list_agents" => self.list_agents(relay_id).await,
            "complete" => self.complete_relay(relay_id).await,
            _ => Ok(ToolResult::structured_error(
                "INVALID_ACTION",
                "relay_autochat",
                &format!(
                    "Unknown action: '{action}'. Valid actions: init, delegate, handoff, status, list_agents, complete"
                ),
                None,
                Some(json!({
                    "action": "init",
                    "task": "description of the relay task"
                })),
            )),
        }
    }
}

impl RelayAutoChatTool {
    /// Initialize a new relay with a task
    async fn init_relay(
        &self,
        relay_id: Option<String>,
        task: Option<String>,
        _context: Option<Value>,
        okr_id: Option<String>,
    ) -> Result<ToolResult> {
        let task = task.unwrap_or_else(|| "Unspecified task".to_string());
        let relay_id =
            relay_id.unwrap_or_else(|| format!("relay-{}", &Uuid::new_v4().to_string()[..8]));

        let bus = get_agent_bus().await?;
        let runtime = ProtocolRelayRuntime::with_relay_id(bus, relay_id.clone());

        // Store the runtime
        {
            let mut store = RELAY_STORE.write();
            store.insert(relay_id.clone(), Arc::new(runtime.clone()));
        }

        let response = json!({
            "status": "initialized",
            "relay_id": relay_id,
            "task": task,
            "okr_id": okr_id,
            "message": "Relay initialized. Use 'delegate' to assign tasks to agents, or 'list_agents' to see available agents."
        });

        let mut result = ToolResult::success(
            serde_json::to_string_pretty(&response).unwrap_or_else(|_| format!("{:?}", response)),
        )
        .with_metadata("relay_id", json!(relay_id));
        if let Some(okr_id) = response.get("okr_id").and_then(|v| v.as_str()) {
            result = result.with_metadata("okr_id", json!(okr_id));
        }

        Ok(result)
    }

    /// Delegate a task to a target agent
    async fn delegate_task(
        &self,
        relay_id: Option<String>,
        target_agent: Option<String>,
        message: Option<String>,
        context: Option<Value>,
        okr_id: Option<String>,
    ) -> Result<ToolResult> {
        let relay_id = match relay_id {
            Some(id) => id,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "relay_id is required for delegate action",
                    Some(vec!["relay_id"]),
                    Some(
                        json!({"action": "delegate", "relay_id": "relay-xxx", "target_agent": "agent-name", "message": "task description"}),
                    ),
                ));
            }
        };
        let target_agent = match target_agent {
            Some(a) => a,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "target_agent is required for delegate action",
                    Some(vec!["target_agent"]),
                    Some(
                        json!({"action": "delegate", "relay_id": relay_id, "target_agent": "agent-name", "message": "task description"}),
                    ),
                ));
            }
        };
        let message = message.unwrap_or_else(|| "New task assigned".to_string());

        // Get or create the runtime
        let runtime = {
            let store = RELAY_STORE.read();
            store.get(&relay_id).cloned()
        };

        let runtime = match runtime {
            Some(r) => r,
            None => {
                // Create a new runtime if it doesn't exist
                let bus = get_agent_bus().await?;
                let new_runtime = ProtocolRelayRuntime::with_relay_id(bus, relay_id.clone());
                let arc_runtime = Arc::new(new_runtime);
                {
                    let mut store = RELAY_STORE.write();
                    store.insert(relay_id.clone(), arc_runtime.clone());
                }
                arc_runtime
            }
        };

        // Build context payload if provided
        let context_msg = if let Some(ref ctx) = context {
            format!(
                "{}\n\nContext: {}",
                message,
                serde_json::to_string_pretty(ctx).unwrap_or_default()
            )
        } else {
            message.clone()
        };

        // Send the delegation message
        runtime.send_handoff("system", &target_agent, &context_msg);

        let response = json!({
            "status": "delegated",
            "relay_id": relay_id,
            "target_agent": target_agent,
            "okr_id": okr_id,
            "message": message,
            "initial_results": {
                "task_assigned": true,
                "agent_notified": true
            }
        });

        let mut result = ToolResult::success(
            serde_json::to_string_pretty(&response).unwrap_or_else(|_| format!("{:?}", response)),
        )
        .with_metadata("relay_id", json!(relay_id))
        .with_metadata("target_agent", json!(target_agent));
        if let Some(okr_id) = response.get("okr_id").and_then(|v| v.as_str()) {
            result = result.with_metadata("okr_id", json!(okr_id));
        }

        Ok(result)
    }

    /// Hand off context between agents
    async fn handoff_context(
        &self,
        relay_id: Option<String>,
        target_agent: Option<String>,
        message: Option<String>,
        context: Option<Value>,
    ) -> Result<ToolResult> {
        let relay_id = match relay_id {
            Some(id) => id,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "relay_id is required for handoff action",
                    Some(vec!["relay_id"]),
                    Some(
                        json!({"action": "handoff", "relay_id": "relay-xxx", "target_agent": "agent-name"}),
                    ),
                ));
            }
        };
        let target_agent = match target_agent {
            Some(a) => a,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "target_agent is required for handoff action",
                    Some(vec!["target_agent"]),
                    Some(
                        json!({"action": "handoff", "relay_id": relay_id, "target_agent": "agent-name"}),
                    ),
                ));
            }
        };
        let message = message.unwrap_or_else(|| "Context handoff".to_string());

        let store = RELAY_STORE.read();
        let runtime = match store.get(&relay_id) {
            Some(r) => r.clone(),
            None => {
                return Ok(ToolResult::structured_error(
                    "NOT_FOUND",
                    "relay_autochat",
                    &format!(
                        "Relay not found: {relay_id}. Use 'init' action to create a relay first."
                    ),
                    None,
                    Some(json!({"action": "init", "task": "description of the relay task"})),
                ));
            }
        };
        // need to drop the lock before await
        drop(store);

        // Build context payload
        let context_msg = if let Some(ref ctx) = context {
            format!(
                "{}\n\nContext: {}",
                message,
                serde_json::to_string_pretty(ctx).unwrap_or_default()
            )
        } else {
            message
        };

        // Send handoff
        runtime.send_handoff("previous_agent", &target_agent, &context_msg);

        let response = json!({
            "status": "handoff_complete",
            "relay_id": relay_id,
            "target_agent": target_agent,
            "message": "Context successfully handed off to target agent"
        });

        Ok(ToolResult::success(
            serde_json::to_string_pretty(&response).unwrap_or_else(|_| format!("{:?}", response)),
        ))
    }

    /// Get status of a relay
    async fn get_status(&self, relay_id: Option<String>) -> Result<ToolResult> {
        let relay_id = match relay_id {
            Some(id) => id,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "relay_id is required for status action",
                    Some(vec!["relay_id"]),
                    Some(json!({"action": "status", "relay_id": "relay-xxx"})),
                ));
            }
        };

        let store = RELAY_STORE.read();

        if store.contains_key(&relay_id) {
            let response = json!({
                "status": "active",
                "relay_id": relay_id,
                "message": "Relay is active"
            });

            Ok(ToolResult::success(
                serde_json::to_string_pretty(&response)
                    .unwrap_or_else(|_| format!("{:?}", response)),
            ))
        } else {
            Ok(ToolResult::error(format!("Relay not found: {}", relay_id)))
        }
    }

    /// List agents in a relay
    async fn list_agents(&self, relay_id: Option<String>) -> Result<ToolResult> {
        let relay_id = match relay_id {
            Some(id) => id,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "relay_id is required for list_agents action",
                    Some(vec!["relay_id"]),
                    Some(json!({"action": "list_agents", "relay_id": "relay-xxx"})),
                ));
            }
        };

        let relay_exists = {
            let store = RELAY_STORE.read();
            store.contains_key(&relay_id)
        };

        if relay_exists {
            let bus = get_agent_bus().await?;
            let agents: Vec<Value> = bus
                .registry
                .agent_ids()
                .iter()
                .map(|name| json!({ "name": name }))
                .collect();

            let response = json!({
                "relay_id": relay_id,
                "agents": agents,
                "count": agents.len()
            });

            Ok(ToolResult::success(
                serde_json::to_string_pretty(&response)
                    .unwrap_or_else(|_| format!("{:?}", response)),
            ))
        } else {
            Ok(ToolResult::error(format!("Relay not found: {}", relay_id)))
        }
    }

    /// Complete a relay and aggregate results
    async fn complete_relay(&self, relay_id: Option<String>) -> Result<ToolResult> {
        let relay_id = match relay_id {
            Some(id) => id,
            None => {
                return Ok(ToolResult::structured_error(
                    "MISSING_FIELD",
                    "relay_autochat",
                    "relay_id is required for complete action",
                    Some(vec!["relay_id"]),
                    Some(json!({"action": "complete", "relay_id": "relay-xxx"})),
                ));
            }
        };

        // Get the runtime and shutdown agents
        let runtime = {
            let mut store = RELAY_STORE.write();
            store.remove(&relay_id)
        };

        if let Some(runtime) = runtime {
            runtime.shutdown_agents(&[]); // Shutdown all registered agents
        }

        let response = json!({
            "status": "completed",
            "relay_id": relay_id,
            "message": "Relay completed successfully. Results aggregated.",
            "aggregated_results": {
                "completed": true,
                "total_agents": 0
            }
        });

        Ok(ToolResult::success(
            serde_json::to_string_pretty(&response).unwrap_or_else(|_| format!("{:?}", response)),
        ))
    }
}