oxios-kernel 0.3.0

Oxios kernel: supervisor, event bus, state store
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! A2A tools — let agents communicate with other agents at runtime.
//!
//! Provides three tools:
//! - `a2a_delegate` — delegate a task to another agent by capability
//! - `a2a_send` — send a message to a specific agent
//! - `a2a_query` — discover agents by capability or skill

use std::sync::Arc;

use async_trait::async_trait;
use oxi_sdk::{AgentTool, AgentToolResult, ToolContext, ToolError};
use serde_json::{json, Value};
use uuid::Uuid;

use crate::a2a::{A2AMessage, A2AProtocol, TaskPriority, TaskSpec};
use crate::types::AgentId;
// ─── A2aDelegateTool ───────────────────────────────────────────────────

/// Tool for delegating a task to another agent discovered by capability.
///
/// Usage flow:
/// 1. Agent calls `a2a_delegate` with a description and required capability.
/// 2. The tool queries the AgentCardRegistry for agents with that capability.
/// 3. If found, it delegates the task via A2A and waits for the result.
/// 4. Returns the execution result to the calling agent.
pub struct A2aDelegateTool {
    a2a: Arc<A2AProtocol>,
    my_agent_id: AgentId,
}

impl A2aDelegateTool {
    /// Create a new A2A delegate tool.
    pub fn new(a2a: Arc<A2AProtocol>, agent_id: AgentId) -> Self {
        Self {
            a2a,
            my_agent_id: agent_id,
        }
    }

    /// Create an `A2aDelegateTool` from a [`KernelHandle`].
    ///
    /// Extracts the A2A protocol from the kernel's a2a facade.
    pub fn from_kernel(kernel: &crate::kernel_handle::KernelHandle, agent_id: AgentId) -> Self {
        Self::new(kernel.a2a.protocol().clone(), agent_id)
    }
}

impl std::fmt::Debug for A2aDelegateTool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("A2aDelegateTool").finish()
    }
}

#[async_trait]
impl AgentTool for A2aDelegateTool {
    fn name(&self) -> &str {
        "a2a_delegate"
    }

    fn label(&self) -> &str {
        "A2A Delegate"
    }

    fn description(&self) -> &str {
        "Delegate a task to another agent. Specify a capability (e.g. 'code-review', 'testing') \
         and a description of the work. The system will find a suitable agent, execute the task, \
         and return the result. This is a blocking call — it waits for the delegated agent to complete."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "description": {
                    "type": "string",
                    "description": "Human-readable description of the task to delegate"
                },
                "capability": {
                    "type": "string",
                    "description": "Required capability of the target agent (e.g. 'code-review', 'testing', 'debugging')"
                },
                "payload": {
                    "type": "object",
                    "description": "Structured data for the task (optional)"
                },
                "priority": {
                    "type": "string",
                    "enum": ["low", "normal", "high", "critical"],
                    "description": "Task priority (default: normal)"
                }
            },
            "required": ["description", "capability"]
        })
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: Value,
        _shutdown: Option<tokio::sync::oneshot::Receiver<()>>,
        _ctx: &ToolContext,
    ) -> Result<AgentToolResult, ToolError> {
        let description = params["description"].as_str().unwrap_or("").to_string();
        if description.is_empty() {
            return Ok(AgentToolResult::error(
                "Missing required parameter: description",
            ));
        }

        let capability = params["capability"].as_str().unwrap_or("").to_string();
        if capability.is_empty() {
            return Ok(AgentToolResult::error(
                "Missing required parameter: capability",
            ));
        }

        let payload = params.get("payload").cloned().unwrap_or(json!({}));
        let priority = parse_priority(params["priority"].as_str());

        let my_id = self.my_agent_id;

        // 1. Find agents with the required capability.
        let candidates = match self.a2a.query_capabilities(&capability).await {
            Ok(c) => c,
            Err(e) => {
                return Ok(AgentToolResult::error(format!(
                    "Failed to query capabilities: {e}"
                )))
            }
        };

        if candidates.is_empty() {
            // No agent available — guide the LLM to handle it itself.
            return Ok(AgentToolResult::success(format!(
                "No agents currently available with capability '{}'. You should handle this task yourself.",
                capability
            )));
        }

        // 2. Pick the first available agent (could be smarter — load-balancing later).
        let target = &candidates[0];
        let target_id = target.agent_id;

        tracing::info!(
            from = %my_id,
            to = %target_id,
            target_name = %target.name,
            capability = %capability,
            "A2A delegating task"
        );

        // 3. Create task spec and delegate via A2A.
        let task = TaskSpec::new(&description, payload.clone()).with_priority(priority);
        let task_id = task.task_id;

        // 4. Execute via dispatch handler (blocking — waits for result).
        match self.a2a.execute_delegation(my_id, target_id, task).await {
            Some(Ok(result)) => Ok(AgentToolResult::success(
                serde_json::to_string(&json!({
                    "task_id": task_id.to_string(),
                    "delegated_to": target.name,
                    "delegated_to_id": target_id.to_string(),
                    "status": "completed",
                    "result": result,
                }))
                .unwrap_or_default(),
            )),
            Some(Err(e)) => Ok(AgentToolResult::error(format!(
                "A2A delegation failed: {}",
                e
            ))),
            None => {
                // No handler registered — fall back to fire-and-forget.
                tracing::warn!("No A2A dispatch handler registered, using fire-and-forget");
                match self
                    .a2a
                    .delegate_task(my_id, target_id, TaskSpec::new(&description, payload))
                    .await
                {
                    Ok(_) => Ok(AgentToolResult::success(format!(
                        "Task delegated to '{}' (no handler — fire-and-forget). Task ID: {}",
                        target.name, task_id
                    ))),
                    Err(e) => Ok(AgentToolResult::error(format!("Delegation failed: {e}"))),
                }
            }
        }
    }
}

// ─── A2aSendTool ───────────────────────────────────────────────────────

/// Tool for sending a direct message to a specific agent.
///
/// Unlike `a2a_delegate`, this sends a fire-and-forget message.
/// Use for status updates, notifications, or simple queries.
pub struct A2aSendTool {
    a2a: Arc<A2AProtocol>,
    my_agent_id: AgentId,
}

impl A2aSendTool {
    /// Create a new A2A send tool.
    pub fn new(a2a: Arc<A2AProtocol>, agent_id: AgentId) -> Self {
        Self {
            a2a,
            my_agent_id: agent_id,
        }
    }

    /// Create an `A2aSendTool` from a [`KernelHandle`].
    ///
    /// Extracts the A2A protocol from the kernel's a2a facade.
    pub fn from_kernel(kernel: &crate::kernel_handle::KernelHandle, agent_id: AgentId) -> Self {
        Self::new(kernel.a2a.protocol().clone(), agent_id)
    }
}

impl std::fmt::Debug for A2aSendTool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("A2aSendTool").finish()
    }
}

#[async_trait]
impl AgentTool for A2aSendTool {
    fn name(&self) -> &str {
        "a2a_send"
    }

    fn label(&self) -> &str {
        "A2A Send"
    }

    fn description(&self) -> &str {
        "Send a message to a specific agent by ID. Fire-and-forget — does not wait for a response. \
         Use for status updates, notifications, or sharing information."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "target_agent_id": {
                    "type": "string",
                    "description": "UUID of the target agent"
                },
                "message_type": {
                    "type": "string",
                    "enum": ["status_update", "result_sharing", "handshake"],
                    "description": "Type of message to send (default: status_update)"
                },
                "content": {
                    "type": "string",
                    "description": "The message content"
                },
                "task_id": {
                    "type": "string",
                    "description": "Task UUID this message relates to (for status_update and result_sharing)"
                },
                "payload": {
                    "type": "object",
                    "description": "Structured data to share (optional, for result_sharing)"
                },
                "progress": {
                    "type": "integer",
                    "description": "Progress percentage for status updates (0-100)"
                }
            },
            "required": ["target_agent_id", "message_type", "content"]
        })
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: Value,
        _shutdown: Option<tokio::sync::oneshot::Receiver<()>>,
        _ctx: &ToolContext,
    ) -> Result<AgentToolResult, ToolError> {
        let target_str = params["target_agent_id"].as_str().unwrap_or("");
        let target_id: AgentId = match Uuid::parse_str(target_str) {
            Ok(id) => id,
            Err(e) => {
                return Ok(AgentToolResult::error(format!(
                    "Invalid target_agent_id: {e}"
                )))
            }
        };

        let message_type = params["message_type"].as_str().unwrap_or("status_update");
        let content = params["content"].as_str().unwrap_or("").to_string();
        let payload = params.get("payload").cloned().unwrap_or(json!({}));
        let progress = params["progress"].as_u64().unwrap_or(0) as u8;
        let task_id: Uuid = params["task_id"]
            .as_str()
            .and_then(|s| Uuid::parse_str(s).ok())
            .unwrap_or_else(Uuid::new_v4);

        let my_id = self.my_agent_id;

        let message = match message_type {
            "status_update" => A2AMessage::StatusUpdate {
                task_id,
                progress,
                message: content,
            },
            "result_sharing" => A2AMessage::ResultSharing {
                task_id,
                result: payload,
                summary: content,
            },
            "handshake" => {
                let card = self.a2a.registry().get_agent(my_id).await;
                let (name, capabilities) = card
                    .map(|c| (c.name, c.capabilities))
                    .unwrap_or(("unknown".into(), vec![]));
                A2AMessage::Handshake {
                    agent_id: my_id,
                    name,
                    capabilities,
                }
            }
            _ => {
                return Ok(AgentToolResult::error(format!(
                    "Unknown message_type: {message_type}"
                )))
            }
        };

        match self.a2a.send_message(my_id, target_id, message).await {
            Ok(request_id) => Ok(AgentToolResult::success(
                serde_json::to_string(&json!({
                    "request_id": request_id.to_string(),
                    "sent_to": target_str,
                }))
                .unwrap_or_default(),
            )),
            Err(e) => Ok(AgentToolResult::error(format!("Failed to send: {e}"))),
        }
    }
}

// ─── A2aQueryTool ──────────────────────────────────────────────────────

/// Tool for discovering other agents by capability.
///
/// Returns a list of agent cards matching the requested capability or skill.
pub struct A2aQueryTool {
    a2a: Arc<A2AProtocol>,
}

impl A2aQueryTool {
    /// Create a new A2A query tool.
    pub fn new(a2a: Arc<A2AProtocol>) -> Self {
        Self { a2a }
    }

    /// Create an `A2aQueryTool` from a [`KernelHandle`].
    ///
    /// Extracts the A2A protocol from the kernel's a2a facade.
    pub fn from_kernel(kernel: &crate::kernel_handle::KernelHandle) -> Self {
        Self::new(kernel.a2a.protocol().clone())
    }
}

impl std::fmt::Debug for A2aQueryTool {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("A2aQueryTool").finish()
    }
}

#[async_trait]
impl AgentTool for A2aQueryTool {
    fn name(&self) -> &str {
        "a2a_query"
    }

    fn label(&self) -> &str {
        "A2A Query"
    }

    fn description(&self) -> &str {
        "Discover other agents by capability or skill. Returns a list of available agents \
         with their names, capabilities, and status."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "capability": {
                    "type": "string",
                    "description": "Search for agents with this capability"
                },
                "skill": {
                    "type": "string",
                    "description": "Search for agents with this skill"
                },
                "limit": {
                    "type": "integer",
                    "description": "Maximum number of results (default: 10)"
                }
            }
        })
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: Value,
        _shutdown: Option<tokio::sync::oneshot::Receiver<()>>,
        _ctx: &ToolContext,
    ) -> Result<AgentToolResult, ToolError> {
        let capability = params["capability"].as_str();
        let skill = params["skill"].as_str();
        let limit = params["limit"].as_u64().unwrap_or(10) as usize;

        let agents = if let Some(cap) = capability {
            match self.a2a.query_capabilities(cap).await {
                Ok(a) => a,
                Err(e) => return Ok(AgentToolResult::error(format!("Query failed: {e}"))),
            }
        } else if let Some(sk) = skill {
            match self.a2a.registry().find_agents_by_skill(sk).await {
                Ok(a) => a,
                Err(e) => return Ok(AgentToolResult::error(format!("Query failed: {e}"))),
            }
        } else {
            // No filter — return all agents.
            self.a2a.registry().list_agents().await
        };

        let cards: Vec<Value> = agents
            .into_iter()
            .take(limit)
            .map(|card| {
                json!({
                    "agent_id": card.agent_id.to_string(),
                    "name": card.name,
                    "description": card.description,
                    "capabilities": card.capabilities,
                    "skills": card.skills,
                    "status": format!("{:?}", card.status),
                })
            })
            .collect();

        Ok(AgentToolResult::success(
            serde_json::to_string(&json!({
                "agents": cards,
                "count": cards.len(),
            }))
            .unwrap_or_default(),
        ))
    }
}

// ─── Helpers ────────────────────────────────────────────────────────────

fn parse_priority(s: Option<&str>) -> TaskPriority {
    match s {
        Some("low") => TaskPriority::Low,
        Some("high") => TaskPriority::High,
        Some("critical") => TaskPriority::Critical,
        _ => TaskPriority::Normal,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::event_bus::EventBus;

    fn test_a2a() -> Arc<A2AProtocol> {
        Arc::new(A2AProtocol::new(EventBus::new(256)))
    }

    async fn register_agent_async(a2a: &A2AProtocol, name: &str, caps: &[&str]) -> AgentId {
        let id = Uuid::new_v4();
        let mut card = crate::a2a::AgentCard::new(id, name, format!("Test agent: {name}"));
        for cap in caps {
            card = card.with_capability(*cap);
        }
        a2a.registry().register_agent(card).await.unwrap();
        id
    }

    #[tokio::test]
    async fn test_a2a_query_finds_capability() {
        let a2a = test_a2a();
        register_agent_async(&a2a, "reviewer", &["code-review"]).await;

        let tool = A2aQueryTool::new(a2a.clone());
        let params = json!({"capability": "code-review"});
        let result = tool
            .execute("tc", params, None, &ToolContext::default())
            .await
            .unwrap();
        assert!(result.output.contains("reviewer"));
        assert!(result.output.contains("1"));
    }

    #[tokio::test]
    async fn test_a2a_query_no_match() {
        let a2a = test_a2a();

        let tool = A2aQueryTool::new(a2a.clone());
        let params = json!({"capability": "nonexistent"});
        let result = tool
            .execute("tc", params, None, &ToolContext::default())
            .await
            .unwrap();
        assert!(result.output.contains("0"));
    }

    #[tokio::test]
    async fn test_a2a_query_respects_limit() {
        let a2a = test_a2a();
        register_agent_async(&a2a, "a1", &["test"]).await;
        register_agent_async(&a2a, "a2", &["test"]).await;
        register_agent_async(&a2a, "a3", &["test"]).await;

        let tool = A2aQueryTool::new(a2a.clone());
        let params = json!({"capability": "test", "limit": 2});
        let result = tool
            .execute("tc", params, None, &ToolContext::default())
            .await
            .unwrap();
        assert!(result.output.contains("2"));
    }

    #[tokio::test]
    async fn test_a2a_delegate_no_agents_returns_guidance() {
        let a2a = test_a2a();
        let agent_id = Uuid::new_v4();

        let tool = A2aDelegateTool::new(a2a.clone(), agent_id);
        let params = json!({"description": "review code", "capability": "code-review"});
        let result = tool
            .execute("tc", params, None, &ToolContext::default())
            .await
            .unwrap();
        assert!(result.success);
        assert!(result.output.contains("handle this task yourself"));
    }

    #[tokio::test]
    async fn test_a2a_send_invalid_uuid() {
        let a2a = test_a2a();
        let agent_id = Uuid::new_v4();

        let tool = A2aSendTool::new(a2a.clone(), agent_id);
        let params = json!({"target_agent_id": "not-a-uuid", "message_type": "status_update", "content": "hello"});
        let result = tool
            .execute("tc", params, None, &ToolContext::default())
            .await
            .unwrap();
        assert!(!result.success);
        assert!(result.output.contains("Invalid target_agent_id"));
    }

    #[tokio::test]
    async fn test_a2a_send_handshake() {
        let a2a = test_a2a();
        let my_id = Uuid::new_v4();
        let target_id = Uuid::new_v4();

        // Register self so handshake can look up name.
        let card = crate::a2a::AgentCard::new(my_id, "me", "Test agent").with_capability("test");
        a2a.registry().register_agent(card).await.unwrap();

        let tool = A2aSendTool::new(a2a.clone(), my_id);
        let params = json!({"target_agent_id": target_id.to_string(), "message_type": "handshake", "content": "hello"});
        let result = tool
            .execute("tc", params, None, &ToolContext::default())
            .await
            .unwrap();
        assert!(result.success);

        // Verify message in queue.
        let msgs = a2a.receive_messages(target_id).await;
        assert_eq!(msgs.len(), 1);
    }

    #[test]
    fn test_parse_priority() {
        assert!(matches!(parse_priority(Some("low")), TaskPriority::Low));
        assert!(matches!(parse_priority(Some("high")), TaskPriority::High));
        assert!(matches!(
            parse_priority(Some("critical")),
            TaskPriority::Critical
        ));
        assert!(matches!(parse_priority(None), TaskPriority::Normal));
        assert!(matches!(
            parse_priority(Some("unknown")),
            TaskPriority::Normal
        ));
    }
}