oxios-kernel 1.2.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
//! Calendar tool — wraps `CalendarApi` behind the `AgentTool` interface.
//!
//! Provides agents with calendar event management capabilities.
//! Operations: create, update, delete, list, get, search, freebusy.
//!
//! ## Example
//!
//! ```json
//! { "op": "create", "title": "Team standup", "start": "2026-06-07T09:00:00Z", "end": "2026-06-07T09:30:00Z" }
//! { "op": "list", "from": "2026-06-07T00:00:00Z", "to": "2026-06-14T00:00:00Z" }
//! { "op": "search", "query": "standup" }
//! { "op": "freebusy", "from": "2026-06-07T00:00:00Z", "to": "2026-06-14T00:00:00Z" }
//! { "op": "delete", "uid": "event-uid-here" }
//! ```

use std::sync::Arc;

use async_trait::async_trait;
use oxi_sdk::{AgentTool, AgentToolResult, ToolContext};
use serde_json::{json, Value};
use tokio::sync::oneshot;

use crate::kernel_handle::KernelHandle;
use oxios_calendar::{CalendarEngine, EventDraft, EventPatch, Repeat};

/// Agent tool for calendar event management.
///
/// Wraps the [`CalendarApi`](crate::kernel_handle::CalendarApi) domain. Allows agents
/// to create, update, delete, list, get, search events and query free-busy slots.
///
/// ## Operations
///
/// | Op         | Description               | Required params                     | Optional params                          |
/// |------------|---------------------------|-------------------------------------|------------------------------------------|
/// | `create`   | Create a new event        | `title`, `start`, `end`             | `all_day`, `description`, `location`, `repeat`, `reminder_minutes` |
/// | `update`   | Update an existing event  | `uid`                               | `title`, `start`, `end`, `all_day`, `description`, `location`, `repeat`, `reminder_minutes` |
/// | `delete`   | Delete an event           | `uid`                               | —                                        |
/// | `list`     | List events in range      | `from`, `to`                        | —                                        |
/// | `get`      | Get a single event        | `uid`                               | —                                        |
/// | `search`   | Full-text search events   | `query`                             | —                                        |
/// | `freebusy` | Free/busy slots in range  | `from`, `to`                        | —                                        |
pub struct CalendarTool {
    engine: Arc<CalendarEngine>,
}

impl CalendarTool {
    /// Create a new `CalendarTool` from a `KernelHandle`.
    ///
    /// Returns `None` if calendar is not configured.
    pub fn try_from_kernel(kernel: &KernelHandle) -> Option<Self> {
        kernel.calendar.as_ref().map(|api| Self {
            engine: api.engine.clone(),
        })
    }

    /// Create a new `CalendarTool` from a `KernelHandle` (required).
    ///
    /// Panics if calendar is not configured. Use [`try_from_kernel`] for
    /// the optional variant.
    pub fn from_kernel(kernel: &KernelHandle) -> Self {
        Self::try_from_kernel(kernel).expect("CalendarTool requires calendar to be configured")
    }
}

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

/// Parse an ISO 8601 datetime string into a `chrono::DateTime<chrono::Utc>`.
fn parse_dt(s: &str) -> Result<chrono::DateTime<chrono::Utc>, String> {
    chrono::DateTime::parse_from_rfc3339(s)
        .map(|dt| dt.with_timezone(&chrono::Utc))
        .map_err(|e| {
            format!(
                "Invalid datetime '{s}': {e}. Use ISO 8601 / RFC 3339 format, e.g. \"2026-06-07T09:00:00Z\""
            )
        })
}

/// Extract an optional string parameter.
fn opt_str<'a>(params: &'a Value, key: &str) -> Option<&'a str> {
    params.get(key).and_then(|v| v.as_str())
}

/// Extract an optional boolean parameter.
fn opt_bool(params: &Value, key: &str) -> Option<bool> {
    params.get(key).and_then(|v| v.as_bool())
}

/// Extract an optional `reminder_minutes` array (Vec<u32>).
fn opt_reminder_minutes(params: &Value) -> Option<Vec<u32>> {
    params
        .get("reminder_minutes")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_u64().map(|n| n as u32))
                .collect()
        })
}

/// Extract an optional `repeat` object and parse into a [`Repeat`].
fn opt_repeat(params: &Value) -> Option<Repeat> {
    let obj = params.get("repeat")?.as_object()?;
    let frequency = obj
        .get("frequency")
        .and_then(|v| v.as_str())
        .unwrap_or("daily")
        .to_string();
    let interval = obj.get("interval").and_then(|v| v.as_u64()).unwrap_or(1) as u32;
    let days = obj
        .get("days")
        .and_then(|v| v.as_array())
        .map(|arr| {
            arr.iter()
                .filter_map(|v| v.as_str().map(|s| s.to_string()))
                .collect()
        })
        .unwrap_or_default();
    let until = obj
        .get("until")
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());
    let count = obj.get("count").and_then(|v| v.as_u64()).map(|v| v as u32);

    Some(Repeat {
        frequency,
        days,
        interval,
        until,
        count,
    })
}

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

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

    fn description(&self) -> &'static str {
        "Manage calendar events — create, update, delete, list, search, freebusy. \
         All datetimes use ISO 8601 / RFC 3339 format (e.g. \"2026-06-07T09:00:00Z\")."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "op": {
                    "type": "string",
                    "enum": ["create", "update", "delete", "list", "get", "search", "freebusy"],
                    "description": "Calendar operation to perform"
                },
                "title": {
                    "type": "string",
                    "description": "Event title (required for create, optional for update)"
                },
                "start": {
                    "type": "string",
                    "description": "Event start time (ISO 8601). Required for create, optional for update."
                },
                "end": {
                    "type": "string",
                    "description": "Event end time (ISO 8601). Required for create, optional for update."
                },
                "all_day": {
                    "type": "boolean",
                    "description": "Whether this is an all-day event"
                },
                "description": {
                    "type": "string",
                    "description": "Event description / notes"
                },
                "location": {
                    "type": "string",
                    "description": "Event location"
                },
                "repeat": {
                    "type": "object",
                    "description": "Recurrence rule",
                    "properties": {
                        "frequency": {
                            "type": "string",
                            "enum": ["daily", "weekly", "monthly", "yearly"],
                            "description": "Recurrence frequency"
                        },
                        "interval": {
                            "type": "integer",
                            "description": "Recurrence interval (default: 1)"
                        },
                        "days": {
                            "type": "array",
                            "items": { "type": "string" },
                            "description": "For weekly: ['mon','wed','fri']"
                        },
                        "count": {
                            "type": "integer",
                            "description": "Max number of occurrences"
                        },
                        "until": {
                            "type": "string",
                            "description": "End date for recurrence (ISO date, e.g. '2026-12-31')"
                        }
                    }
                },
                "reminder_minutes": {
                    "type": "array",
                    "items": { "type": "integer" },
                    "description": "Minutes before event to trigger reminders, e.g. [5, 15, 60]"
                },
                "uid": {
                    "type": "string",
                    "description": "Event UID (required for update, delete, get)"
                },
                "from": {
                    "type": "string",
                    "description": "Range start time for list/freebusy (ISO 8601)"
                },
                "to": {
                    "type": "string",
                    "description": "Range end time for list/freebusy (ISO 8601)"
                },
                "query": {
                    "type": "string",
                    "description": "Search query for event title/description (search op)"
                }
            },
            "required": ["op"]
        })
    }

    async fn execute(
        &self,
        _tool_call_id: &str,
        params: Value,
        _signal: Option<oneshot::Receiver<()>>,
        _ctx: &ToolContext,
    ) -> Result<AgentToolResult, String> {
        let op = params
            .get("op")
            .and_then(|v| v.as_str())
            .ok_or_else(|| "Missing required parameter: op".to_string())?;

        match op {
            "create" => self.exec_create(&params).await,
            "update" => self.exec_update(&params).await,
            "delete" => self.exec_delete(&params).await,
            "list" => self.exec_list(&params).await,
            "get" => self.exec_get(&params).await,
            "search" => self.exec_search(&params).await,
            "freebusy" => self.exec_freebusy(&params).await,
            other => Err(format!(
                "Unknown calendar op '{other}'. Valid: create, update, delete, list, get, search, freebusy"
            )),
        }
    }
}

impl CalendarTool {
    async fn exec_create(&self, params: &Value) -> Result<AgentToolResult, String> {
        let title = opt_str(params, "title")
            .ok_or_else(|| "create requires 'title' parameter".to_string())?;
        let start = opt_str(params, "start")
            .ok_or_else(|| "create requires 'start' parameter".to_string())?;
        let end =
            opt_str(params, "end").ok_or_else(|| "create requires 'end' parameter".to_string())?;

        let start_dt = parse_dt(start)?;
        let end_dt = parse_dt(end)?;

        let draft = EventDraft {
            title: title.to_string(),
            start: start_dt,
            end: end_dt,
            all_day: opt_bool(params, "all_day").unwrap_or(false),
            description: opt_str(params, "description").map(|s| s.to_string()),
            location: opt_str(params, "location").map(|s| s.to_string()),
            repeat: opt_repeat(params),
            reminder_minutes: opt_reminder_minutes(params).unwrap_or_default(),
            source: oxios_calendar::EventSource::Agent,
        };

        match self.engine.create(draft).await {
            Ok(result) => Ok(AgentToolResult::success(
                serde_json::to_string_pretty(&json!({
                    "uid": result.uid,
                    "status": "created",
                    "conflicts": result.conflicts,
                    "file": result.file,
                }))
                .unwrap_or_default(),
            )),
            Err(e) => Ok(AgentToolResult::error(format!(
                "Failed to create event: {e}"
            ))),
        }
    }

    async fn exec_update(&self, params: &Value) -> Result<AgentToolResult, String> {
        let uid =
            opt_str(params, "uid").ok_or_else(|| "update requires 'uid' parameter".to_string())?;

        let patch = EventPatch {
            title: opt_str(params, "title").map(|s| s.to_string()),
            start: opt_str(params, "start").and_then(|s| parse_dt(s).ok()),
            end: opt_str(params, "end").and_then(|s| parse_dt(s).ok()),
            all_day: opt_bool(params, "all_day"),
            description: opt_str(params, "description").map(|s| Some(s.to_string())),
            location: opt_str(params, "location").map(|s| Some(s.to_string())),
            repeat: opt_repeat(params).map(Some),
            reminder_minutes: opt_reminder_minutes(params),
        };

        match self.engine.update(uid, patch).await {
            Ok(result) => Ok(AgentToolResult::success(
                serde_json::to_string_pretty(&json!({
                    "uid": result.uid,
                    "status": "updated",
                    "conflicts": result.conflicts,
                }))
                .unwrap_or_default(),
            )),
            Err(e) => Ok(AgentToolResult::error(format!(
                "Failed to update event: {e}"
            ))),
        }
    }

    async fn exec_delete(&self, params: &Value) -> Result<AgentToolResult, String> {
        let uid =
            opt_str(params, "uid").ok_or_else(|| "delete requires 'uid' parameter".to_string())?;

        match self.engine.delete(uid).await {
            Ok(()) => Ok(AgentToolResult::success(format!("Event '{uid}' deleted."))),
            Err(e) => Ok(AgentToolResult::error(format!(
                "Failed to delete event: {e}"
            ))),
        }
    }

    async fn exec_list(&self, params: &Value) -> Result<AgentToolResult, String> {
        let from =
            opt_str(params, "from").ok_or_else(|| "list requires 'from' parameter".to_string())?;
        let to = opt_str(params, "to").ok_or_else(|| "list requires 'to' parameter".to_string())?;

        let from_dt = parse_dt(from)?;
        let to_dt = parse_dt(to)?;

        match self.engine.list(from_dt, to_dt).await {
            Ok(events) => {
                if events.is_empty() {
                    return Ok(AgentToolResult::success("No events in the given range."));
                }
                let display: Vec<Value> = events
                    .iter()
                    .map(|e| {
                        json!({
                            "uid": e.uid,
                            "title": e.title,
                            "start": e.start.to_rfc3339(),
                            "end": e.end.to_rfc3339(),
                            "status": e.status,
                        })
                    })
                    .collect();
                Ok(AgentToolResult::success(
                    serde_json::to_string_pretty(&json!({
                        "events": display,
                        "count": display.len(),
                    }))
                    .unwrap_or_default(),
                ))
            }
            Err(e) => Ok(AgentToolResult::error(format!(
                "Failed to list events: {e}"
            ))),
        }
    }

    async fn exec_get(&self, params: &Value) -> Result<AgentToolResult, String> {
        let uid =
            opt_str(params, "uid").ok_or_else(|| "get requires 'uid' parameter".to_string())?;

        match self.engine.get(uid).await {
            Ok(event) => Ok(AgentToolResult::success(
                serde_json::to_string_pretty(&json!({
                    "uid": event.uid,
                    "title": event.title,
                    "start": event.start.to_rfc3339(),
                    "end": event.end.to_rfc3339(),
                    "all_day": event.all_day,
                    "description": event.description,
                    "location": event.location,
                    "rrule": event.rrule,
                    "status": event.status,
                }))
                .unwrap_or_default(),
            )),
            Err(e) => Ok(AgentToolResult::error(format!("Failed to get event: {e}"))),
        }
    }

    async fn exec_search(&self, params: &Value) -> Result<AgentToolResult, String> {
        let query = opt_str(params, "query")
            .ok_or_else(|| "search requires 'query' parameter".to_string())?;

        match self.engine.search(query).await {
            Ok(events) => {
                if events.is_empty() {
                    return Ok(AgentToolResult::success(format!(
                        "No events matching '{query}'."
                    )));
                }
                let display: Vec<Value> = events
                    .iter()
                    .map(|e| {
                        json!({
                            "uid": e.uid,
                            "title": e.title,
                            "start": e.start.to_rfc3339(),
                            "end": e.end.to_rfc3339(),
                        })
                    })
                    .collect();
                Ok(AgentToolResult::success(
                    serde_json::to_string_pretty(&json!({
                        "events": display,
                        "count": display.len(),
                        "query": query,
                    }))
                    .unwrap_or_default(),
                ))
            }
            Err(e) => Ok(AgentToolResult::error(format!(
                "Failed to search events: {e}"
            ))),
        }
    }

    async fn exec_freebusy(&self, params: &Value) -> Result<AgentToolResult, String> {
        let from = opt_str(params, "from")
            .ok_or_else(|| "freebusy requires 'from' parameter".to_string())?;
        let to =
            opt_str(params, "to").ok_or_else(|| "freebusy requires 'to' parameter".to_string())?;

        let from_dt = parse_dt(from)?;
        let to_dt = parse_dt(to)?;

        match self.engine.freebusy(from_dt, to_dt).await {
            Ok(slots) => Ok(AgentToolResult::success(
                serde_json::to_string_pretty(&json!({
                    "slots": slots,
                    "count": slots.len(),
                }))
                .unwrap_or_default(),
            )),
            Err(e) => Ok(AgentToolResult::error(format!(
                "Failed to compute freebusy: {e}"
            ))),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Datelike;

    #[test]
    fn test_parse_dt_valid() {
        let dt = parse_dt("2026-06-07T09:00:00Z").unwrap();
        assert_eq!(dt.year(), 2026);
        assert_eq!(dt.month(), 6);
        assert_eq!(dt.day(), 7);
    }

    #[test]
    fn test_parse_dt_invalid() {
        assert!(parse_dt("not-a-date").is_err());
    }

    #[test]
    fn test_opt_repeat_basic() {
        let params = json!({
            "repeat": {
                "frequency": "weekly",
                "days": ["mon", "wed"],
                "interval": 2,
                "count": 10
            }
        });
        let rule = opt_repeat(&params).unwrap();
        assert_eq!(rule.frequency, "weekly");
        assert_eq!(rule.days, vec!["mon", "wed"]);
        assert_eq!(rule.interval, 2);
        assert_eq!(rule.count, Some(10));
        assert!(rule.until.is_none());
    }
}