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
//! Marketplace tool — wraps `MarketplaceApi` behind the `AgentTool` interface.
//!
//! Provides agents with multi-registry marketplace capabilities.
//! Registries: ClawHub, Skills.sh (Vercel Labs ecosystem).
//!
//! Actions: search, get, install, update, update_all, check_updates,
//!           skills_sh_search, skills_sh_list, skills_sh_install, skills_sh_detail.
//!
//! ## Example
//!
//! ```json
//! { "action": "search", "query": "code review", "limit": 10 }
//! { "action": "install", "slug": "code-review-helper", "version": "1.2.0" }
//! { "action": "skills_sh_search", "query": "frontend design" }
//! { "action": "skills_sh_install", "skill_id": "vercel-labs/agent-skills/frontend-design" }
//! ```

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 crate::kernel_handle::MarketplaceApi;

/// Agent tool for multi-registry marketplace operations.
///
/// Wraps the `MarketplaceApi` domain of the `KernelHandle`. Allows agents
/// to search, install, and update skills from ClawHub and Skills.sh.
///
/// ## Actions
///
/// | Action              | Description                              | Required params  | Optional params      |
/// |---------------------|------------------------------------------|------------------|----------------------|
/// | `search`            | Search ClawHub for skills                | `query`          | `limit`              |
/// | `get`               | Get skill detail from ClawHub            | `slug`           | —                    |
/// | `install`           | Install a skill from ClawHub             | `slug`           | `version`            |
/// | `update`            | Update a specific installed skill         | `slug`           | —                    |
/// | `update_all`        | Update all installed ClawHub skills       | —                | —                    |
/// | `check_updates`     | Check for available updates              | —                | —                    |
/// | `skills_sh_search`  | Search Skills.sh for skills              | `query`          | `limit`              |
/// | `skills_sh_list`    | List Skills.sh leaderboard               | —                | `view`, `page`, `per_page` |
/// | `skills_sh_install` | Install a skill from Skills.sh           | `skill_id`       | —                    |
/// | `skills_sh_detail`  | Get skill detail from Skills.sh          | `skill_id`       | —                    |
pub struct MarketplaceTool {
    api: MarketplaceApi,
}

impl MarketplaceTool {
    /// Create a new `MarketplaceTool` from a `KernelHandle`.
    pub fn from_kernel(kernel: &KernelHandle) -> Self {
        Self {
            api: kernel.marketplace_api.clone(),
        }
    }
}

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

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

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

    fn description(&self) -> &'static str {
        "Search, install, and update skills from the ClawHub marketplace and Skills.sh registry. \
         ClawHub actions: search, get, install, update, update_all, check_updates. \
         Skills.sh actions: skills_sh_search, skills_sh_list, skills_sh_install, skills_sh_detail."
    }

    fn parameters_schema(&self) -> Value {
        json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["search", "get", "install", "update", "update_all", "check_updates", "skills_sh_search", "skills_sh_list", "skills_sh_install", "skills_sh_detail"],
                    "description": "Marketplace operation to perform"
                },
                "query": {
                    "type": "string",
                    "description": "Search query string (search action)"
                },
                "limit": {
                    "type": "integer",
                    "description": "Maximum number of results to return (search action, default 20)"
                },
                "slug": {
                    "type": "string",
                    "description": "Skill slug — the unique identifier on ClawHub (get, install, update actions)"
                },
                "skill_id": {
                    "type": "string",
                    "description": "Skills.sh skill identifier (format: owner/repo/skill-slug)"
                },
                "version": {
                    "type": "string",
                    "description": "Specific version to install (install action, optional; defaults to latest)"
                },
                "view": {
                    "type": "string",
                    "description": "Skills.sh leaderboard view: all-time, trending, or hot",
                    "default": "all-time"
                },
                "page": {
                    "type": "integer",
                    "description": "Page number for Skills.sh listing (0-indexed)"
                },
                "per_page": {
                    "type": "integer",
                    "description": "Results per page for Skills.sh listing (1-500, default 50)"
                }
            },
            "required": ["action"]
        })
    }

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

        match action {
            "search" => {
                let query = params
                    .get("query")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "search requires 'query' parameter".to_string())?;
                let limit = params["limit"].as_u64().map(|l| l as usize);

                match self.api.search(query, limit).await {
                    Ok(results) => {
                        let display: Vec<Value> = results
                            .into_iter()
                            .map(|r| {
                                json!({
                                    "slug": r.slug,
                                    "displayName": r.display_name,
                                    "summary": r.summary,
                                    "version": r.version,
                                    "score": r.score,
                                })
                            })
                            .collect();
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&json!({
                                "results": display,
                                "count": display.len(),
                            }))
                            .unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Marketplace search failed: {e}"
                    ))),
                }
            }

            "get" => {
                let slug = params
                    .get("slug")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "get requires 'slug' parameter".to_string())?;

                match self.api.get_skill(slug).await {
                    Ok(detail) => {
                        let display = json!({
                            "slug": detail.skill.as_ref().map(|s| &s.slug),
                            "displayName": detail.skill.as_ref().map(|s| &s.display_name),
                            "summary": detail.skill.as_ref().and_then(|s| s.summary.clone()),
                            "latestVersion": detail.latest_version.as_ref().map(|v| &v.version),
                            "changelog": detail.latest_version.as_ref().and_then(|v| v.changelog.clone()),
                            "os": detail.metadata.as_ref().and_then(|m| m.os.clone()),
                            "owner": detail.owner.as_ref().map(|o| {
                                json!({
                                    "handle": o.handle,
                                    "displayName": o.display_name,
                                })
                            }),
                        });
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&display).unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to get skill '{slug}': {e}"
                    ))),
                }
            }

            "install" => {
                let slug = params
                    .get("slug")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "install requires 'slug' parameter".to_string())?;
                let version = params
                    .get("version")
                    .and_then(|v| v.as_str());

                match self.api.install(slug, version).await {
                    Ok(result) => Ok(AgentToolResult::success(
                        serde_json::to_string_pretty(&json!({
                            "ok": result.ok,
                            "slug": result.slug,
                            "version": result.version,
                            "targetDir": result.target_dir.display().to_string(),
                            "changelog": result.changelog,
                        }))
                        .unwrap_or_default(),
                    )),
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to install '{slug}': {e}"
                    ))),
                }
            }

            "update" => {
                let slug = params
                    .get("slug")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "update requires 'slug' parameter".to_string())?;

                match self.api.update(slug).await {
                    Ok(result) => Ok(AgentToolResult::success(
                        serde_json::to_string_pretty(&json!({
                            "ok": result.ok,
                            "slug": result.slug,
                            "previousVersion": result.previous_version,
                            "version": result.version,
                            "changed": result.changed,
                        }))
                        .unwrap_or_default(),
                    )),
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to update '{slug}': {e}"
                    ))),
                }
            }

            "update_all" => {
                match self.api.update_all().await {
                    Ok(results) => {
                        let display: Vec<Value> = results
                            .into_iter()
                            .map(|r| {
                                json!({
                                    "ok": r.ok,
                                    "slug": r.slug,
                                    "previousVersion": r.previous_version,
                                    "version": r.version,
                                    "changed": r.changed,
                                    "error": r.error,
                                })
                            })
                            .collect();
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&json!({
                                "results": display,
                                "count": display.len(),
                            }))
                            .unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to update all skills: {e}"
                    ))),
                }
            }

            "check_updates" => {
                match self.api.check_updates().await {
                    Ok(updates) => {
                        if updates.is_empty() {
                            return Ok(AgentToolResult::success("All skills are up to date."));
                        }
                        let display: Vec<Value> = updates
                            .into_iter()
                            .map(|u| {
                                json!({
                                    "slug": u.slug,
                                    "currentVersion": u.current_version,
                                    "latestVersion": u.latest_version,
                                    "changelog": u.changelog,
                                })
                            })
                            .collect();
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&json!({
                                "updates": display,
                                "count": display.len(),
                            }))
                            .unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to check updates: {e}"
                    ))),
                }
            }

            // ─── Skills.sh Actions ───────────────────────────────────────

            "skills_sh_search" => {
                let query = params
                    .get("query")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "skills_sh_search requires 'query' parameter".to_string())?;
                let limit = params["limit"].as_u64().map(|l| l as usize);

                match self.api.search_skills_sh(query, limit).await {
                    Ok(resp) => {
                        let display: Vec<Value> = resp
                            .data
                            .into_iter()
                            .map(|s| {
                                json!({
                                    "id": s.id,
                                    "name": s.name,
                                    "slug": s.slug,
                                    "source": s.source,
                                    "installs": s.installs,
                                    "sourceType": s.source_type,
                                    "installUrl": s.install_url,
                                })
                            })
                            .collect();
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&json!({
                                "results": display,
                                "count": display.len(),
                                "searchType": resp.search_type,
                            }))
                            .unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Skills.sh search failed: {e}"
                    ))),
                }
            }

            "skills_sh_list" => {
                let view = params.get("view").and_then(|v| v.as_str());
                let page = params["page"].as_i64();
                let per_page = params["per_page"].as_i64();

                match self.api.list_skills_sh(view, page, per_page).await {
                    Ok(resp) => {
                        let display: Vec<Value> = resp
                            .data
                            .into_iter()
                            .map(|s| {
                                json!({
                                    "id": s.id,
                                    "name": s.name,
                                    "slug": s.slug,
                                    "source": s.source,
                                    "installs": s.installs,
                                })
                            })
                            .collect();
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&json!({
                                "results": display,
                                "pagination": {
                                    "page": resp.pagination.page,
                                    "total": resp.pagination.total,
                                    "hasMore": resp.pagination.has_more,
                                },
                            }))
                            .unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Skills.sh list failed: {e}"
                    ))),
                }
            }

            "skills_sh_install" => {
                let skill_id = params
                    .get("skill_id")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "skills_sh_install requires 'skill_id' parameter".to_string())?;

                match self.api.install_skills_sh(skill_id).await {
                    Ok(result) => Ok(AgentToolResult::success(
                        serde_json::to_string_pretty(&json!({
                            "ok": result.ok,
                            "slug": result.slug,
                            "source": result.source,
                            "skillId": result.skill_id,
                            "targetDir": result.target_dir.display().to_string(),
                            "fileCount": result.file_count,
                        }))
                        .unwrap_or_default(),
                    )),
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to install from Skills.sh: {e}"
                    ))),
                }
            }

            "skills_sh_detail" => {
                let skill_id = params
                    .get("skill_id")
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| "skills_sh_detail requires 'skill_id' parameter".to_string())?;

                match self.api.get_skills_sh_skill(skill_id).await {
                    Ok(detail) => {
                        let files: Vec<Value> = detail
                            .files
                            .map(|f| f.into_iter().map(|file| json!({ "path": file.path, "size": file.contents.len() })).collect())
                            .unwrap_or_default();
                        Ok(AgentToolResult::success(
                            serde_json::to_string_pretty(&json!({
                                "id": detail.id,
                                "source": detail.source,
                                "slug": detail.slug,
                                "installs": detail.installs,
                                "hash": detail.hash,
                                "fileCount": files.len(),
                                "files": files,
                            }))
                            .unwrap_or_default(),
                        ))
                    }
                    Err(e) => Ok(AgentToolResult::error(format!(
                        "Failed to get Skills.sh detail: {e}"
                    ))),
                }
            }

            other => Err(format!(
                "Unknown marketplace action '{other}'. Valid: search, get, install, update, update_all, check_updates, skills_sh_search, skills_sh_list, skills_sh_install, skills_sh_detail"
            )),
        }
    }
}

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

    #[test]
    fn test_schema_structure() {
        let schema = json!({
            "type": "object",
            "properties": {
                "action": {
                    "type": "string",
                    "enum": ["search", "get", "install", "update", "update_all", "check_updates",
                             "skills_sh_search", "skills_sh_list", "skills_sh_install", "skills_sh_detail"]
                },
                "query": { "type": "string" },
                "limit": { "type": "integer" },
                "slug": { "type": "string" },
                "skill_id": { "type": "string" },
                "version": { "type": "string" }
            },
            "required": ["action"]
        });

        let actions = schema["properties"]["action"]["enum"].as_array().unwrap();
        assert_eq!(actions.len(), 10);
    }
}