nolgia-cli 0.2.5

CLI for the Nolgia generative media platform (image, audio, video)
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
//! Marketplace skills: registry-backed skills served by nolgia-api.
//! Distinct from `nolgia skills` (SKILL.md packs bundled in the binary):
//! marketplace skills are published by Nolgia, installed per agent, and
//! materialized onto the agent pod by `nolgia skill sync` (or the chart's
//! sync initContainer, which does the same thing).

use anyhow::{Context, Result, bail};
use base64::Engine as _;
use base64::engine::general_purpose::STANDARD as BASE64;
use clap::{Args, Subcommand};
use nolgia_client::types::{
    PublishSkillRequest, PublishSkillRequestMinTier, PublishSkillRequestName,
    PublishSkillRequestSlug, PublishSkillRequestVersion, Skill, SkillVisibility,
};
use std::{
    fs,
    io::Read as _,
    path::{Path, PathBuf},
};

use crate::output::{OutputFormat, print_json};

use super::CommandContext;

/// Sync marker inside each materialized skill dir; records the synced
/// version. Dirs without it (hand-authored skills) are never touched.
const MARKER: &str = ".nolgia-skill.json";

#[derive(Subcommand, Debug)]
pub enum SkillCommand {
    /// List the marketplace catalog visible to this account
    List,
    /// Show one marketplace skill
    Show(SlugArgs),
    /// List skills installed for this account's agent
    Installed,
    /// Install a marketplace skill for this account's agent
    Install(SlugArgs),
    /// Uninstall a marketplace skill from this account's agent
    Uninstall(SlugArgs),
    /// Materialize installed skills into a skills directory (what the agent
    /// pod's initContainer runs on boot)
    Sync(SyncArgs),
    /// Publish a skill package directory to the marketplace (admin only)
    Publish(PublishArgs),
}

#[derive(Args, Debug)]
pub struct SlugArgs {
    pub slug: String,
}

#[derive(Args, Debug)]
pub struct SyncArgs {
    /// Target skills directory (default: $HERMES_HOME/skills, HERMES_HOME
    /// defaulting to /opt/data)
    #[arg(long)]
    pub dir: Option<PathBuf>,
}

#[derive(Args, Debug)]
pub struct PublishArgs {
    /// Skill package directory containing skill.json + SKILL.md
    pub dir: PathBuf,
}

pub async fn run(command: SkillCommand, ctx: &CommandContext) -> Result<()> {
    match command {
        SkillCommand::List => list(ctx).await,
        SkillCommand::Show(args) => show(args, ctx).await,
        SkillCommand::Installed => installed(ctx).await,
        SkillCommand::Install(args) => install(args, ctx).await,
        SkillCommand::Uninstall(args) => uninstall(args, ctx).await,
        SkillCommand::Sync(args) => sync(args, ctx).await,
        SkillCommand::Publish(args) => publish(args, ctx).await,
    }
}

async fn list(ctx: &CommandContext) -> Result<()> {
    let skills = ctx
        .client()
        .list_skills()
        .send()
        .await
        .context("listing marketplace skills")?
        .into_inner();
    match ctx.format() {
        OutputFormat::Json => print_json(&skills),
        OutputFormat::Text => {
            for skill in &skills {
                print_skill_line(skill);
            }
            Ok(())
        }
    }
}

async fn show(args: SlugArgs, ctx: &CommandContext) -> Result<()> {
    let skill = ctx
        .client()
        .get_skill()
        .slug(&args.slug)
        .send()
        .await
        .context("fetching skill")?
        .into_inner();
    match ctx.format() {
        OutputFormat::Json => print_json(&skill),
        OutputFormat::Text => {
            println!("{} v{}  {}", skill.slug, skill.latest_version, skill.name);
            println!("  {}", skill.description);
            if !skill.min_tier.is_empty() {
                println!(
                    "  requires plan: {} (entitled: {})",
                    skill.min_tier, skill.entitled
                );
            }
            if !skill.credit_cost_hint.is_empty() {
                println!("  credits: {}", skill.credit_cost_hint);
            }
            if !skill.required_env.is_empty() {
                println!("  env: {}", skill.required_env.join(", "));
            }
            Ok(())
        }
    }
}

async fn installed(ctx: &CommandContext) -> Result<()> {
    let skills = ctx
        .client()
        .list_agent_skills()
        .send()
        .await
        .context("listing installed skills")?
        .into_inner();
    match ctx.format() {
        OutputFormat::Json => print_json(&skills),
        OutputFormat::Text => {
            for skill in &skills {
                println!(
                    "{:28} v{:10} {}",
                    skill.slug, skill.latest_version, skill.name
                );
            }
            Ok(())
        }
    }
}

async fn install(args: SlugArgs, ctx: &CommandContext) -> Result<()> {
    let skill = ctx
        .client()
        .install_agent_skill()
        .slug(&args.slug)
        .send()
        .await
        .context("installing skill")?
        .into_inner();
    match ctx.format() {
        OutputFormat::Json => print_json(&skill),
        OutputFormat::Text => {
            println!(
                "installed {} v{} — it lands on the agent pod on its next restart or `nolgia skill sync`",
                skill.slug, skill.latest_version
            );
            Ok(())
        }
    }
}

async fn uninstall(args: SlugArgs, ctx: &CommandContext) -> Result<()> {
    ctx.client()
        .uninstall_agent_skill()
        .slug(&args.slug)
        .send()
        .await
        .context("uninstalling skill")?;
    match ctx.format() {
        OutputFormat::Json => print_json(&serde_json::json!({ "uninstalled": args.slug })),
        OutputFormat::Text => {
            println!("uninstalled {}", args.slug);
            Ok(())
        }
    }
}

// --- sync ---------------------------------------------------------------

#[derive(serde::Serialize)]
struct SyncResult {
    slug: String,
    version: String,
    action: &'static str, // "synced" | "current" | "removed" | "skipped"
}

async fn sync(args: SyncArgs, ctx: &CommandContext) -> Result<()> {
    let root = args.dir.unwrap_or_else(|| {
        let home = std::env::var("HERMES_HOME").unwrap_or_else(|_| "/opt/data".into());
        PathBuf::from(home).join("skills")
    });
    fs::create_dir_all(&root).with_context(|| format!("creating {}", root.display()))?;

    let installed = ctx
        .client()
        .list_agent_skills()
        .send()
        .await
        .context("listing installed skills")?
        .into_inner();

    let mut results = Vec::new();
    for skill in &installed {
        let dir = root.join(&skill.slug);
        if marker_version(&dir).as_deref() == Some(skill.latest_version.as_str()) {
            results.push(SyncResult {
                slug: skill.slug.clone(),
                version: skill.latest_version.clone(),
                action: "current",
            });
            continue;
        }
        let content = match ctx
            .client()
            .get_skill_content()
            .slug(&skill.slug)
            .send()
            .await
        {
            Ok(response) => response.into_inner(),
            Err(err) => {
                // Entitlement/visibility is enforced server-side per download;
                // skip rather than fail the whole sync.
                eprintln!("skipping {}: {}", skill.slug, err);
                results.push(SyncResult {
                    slug: skill.slug.clone(),
                    version: skill.latest_version.clone(),
                    action: "skipped",
                });
                continue;
            }
        };
        let raw = BASE64
            .decode(&content.content_base64)
            .with_context(|| format!("decoding content for {}", skill.slug))?;
        materialize(&root, &skill.slug, &content.version, &raw)?;
        results.push(SyncResult {
            slug: skill.slug.clone(),
            version: content.version.clone(),
            action: "synced",
        });
    }

    // Remove marketplace-managed dirs no longer in the install list.
    let keep: Vec<&str> = installed.iter().map(|s| s.slug.as_str()).collect();
    for entry in fs::read_dir(&root)? {
        let entry = entry?;
        let name = entry.file_name().to_string_lossy().into_owned();
        let dir = entry.path();
        if dir.is_dir()
            && !keep.contains(&name.as_str())
            && let Some(version) = marker_version(&dir)
        {
            fs::remove_dir_all(&dir)
                .with_context(|| format!("removing stale skill {}", dir.display()))?;
            results.push(SyncResult {
                slug: name,
                version,
                action: "removed",
            });
        }
    }

    match ctx.format() {
        OutputFormat::Json => print_json(&results),
        OutputFormat::Text => {
            for r in &results {
                println!("{:8} {} v{}", r.action, r.slug, r.version);
            }
            println!("skills dir: {}", root.display());
            Ok(())
        }
    }
}

fn marker_version(dir: &Path) -> Option<String> {
    let raw = fs::read_to_string(dir.join(MARKER)).ok()?;
    let value: serde_json::Value = serde_json::from_str(&raw).ok()?;
    Some(value.get("version")?.as_str()?.to_string())
}

/// Extract a skill tarball into a staging dir and atomically swap it in.
fn materialize(root: &Path, slug: &str, version: &str, targz: &[u8]) -> Result<()> {
    let staging = root.join(format!(".{slug}.syncing"));
    let _ = fs::remove_dir_all(&staging);
    fs::create_dir_all(&staging)?;

    let decoder = flate2::read::GzDecoder::new(targz);
    let mut archive = tar::Archive::new(decoder);
    // tar-rs refuses path traversal and absolute paths on unpack.
    archive
        .unpack(&staging)
        .with_context(|| format!("extracting {slug}"))?;

    fs::write(
        staging.join(MARKER),
        serde_json::to_string_pretty(&serde_json::json!({ "slug": slug, "version": version }))?
            + "\n",
    )?;

    let target = root.join(slug);
    let _ = fs::remove_dir_all(&target);
    fs::rename(&staging, &target)
        .with_context(|| format!("activating {} -> {}", staging.display(), target.display()))?;
    Ok(())
}

// --- publish ------------------------------------------------------------

async fn publish(args: PublishArgs, ctx: &CommandContext) -> Result<()> {
    let manifest_path = args.dir.join("skill.json");
    let manifest_raw = fs::read_to_string(&manifest_path)
        .with_context(|| format!("reading {}", manifest_path.display()))?;
    let manifest: serde_json::Map<String, serde_json::Value> =
        serde_json::from_str(&manifest_raw).context("parsing skill.json")?;
    if !args.dir.join("SKILL.md").is_file() {
        bail!(
            "{} has no SKILL.md — not a skill package",
            args.dir.display()
        );
    }

    let field = |key: &str| -> Result<String> {
        manifest
            .get(key)
            .and_then(|v| v.as_str())
            .map(str::to_string)
            .with_context(|| format!("skill.json is missing required string field {key:?}"))
    };
    let slug: PublishSkillRequestSlug = field("slug")?.parse().context("invalid slug")?;
    let name: PublishSkillRequestName = field("name")?.parse().context("invalid name")?;
    let version: PublishSkillRequestVersion =
        field("version")?.parse().context("invalid version")?;
    let description = manifest
        .get("description")
        .and_then(|v| v.as_str())
        .map(|d| d.parse())
        .transpose()
        .context("invalid description")?;
    let credit_cost_hint = manifest
        .get("credit_cost_hint")
        .and_then(|v| v.as_str())
        .map(|h| h.parse())
        .transpose()
        .context("invalid credit_cost_hint")?;
    let required_env: Vec<String> = manifest
        .get("required_env")
        .and_then(|v| v.as_array())
        .map(|a| {
            a.iter()
                .filter_map(|v| v.as_str().map(str::to_string))
                .collect()
        })
        .unwrap_or_default();
    let min_tier: Option<PublishSkillRequestMinTier> = manifest
        .get("min_tier")
        .and_then(|v| v.as_str())
        .map(|t| serde_json::from_value(serde_json::Value::String(t.to_string())))
        .transpose()
        .context("skill.json min_tier is not a valid subscription tier")?;
    let visibility: Option<SkillVisibility> = manifest
        .get("visibility")
        .and_then(|v| v.as_str())
        .map(|t| serde_json::from_value(serde_json::Value::String(t.to_string())))
        .transpose()
        .context("skill.json visibility must be \"public\" or \"private\"")?;

    let content_base64 = BASE64.encode(pack(&args.dir)?);

    let body = PublishSkillRequest {
        slug,
        name,
        version,
        description,
        credit_cost_hint,
        required_env,
        min_tier,
        visibility,
        manifest,
        content_base64,
    };
    let skill = ctx
        .client()
        .publish_skill()
        .body(body)
        .send()
        .await
        .context("publishing skill (admin only)")?
        .into_inner();
    match ctx.format() {
        OutputFormat::Json => print_json(&skill),
        OutputFormat::Text => {
            println!(
                "published {} v{} ({}, min_tier: {})",
                skill.slug,
                skill.latest_version,
                skill.visibility,
                if skill.min_tier.is_empty() {
                    "free"
                } else {
                    &skill.min_tier
                }
            );
            Ok(())
        }
    }
}

/// Tar+gzip the package directory (contents at the archive root).
fn pack(dir: &Path) -> Result<Vec<u8>> {
    let encoder = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default());
    let mut builder = tar::Builder::new(encoder);
    append_dir(&mut builder, dir, Path::new(""))?;
    Ok(builder.into_inner()?.finish()?)
}

fn append_dir<W: std::io::Write>(
    builder: &mut tar::Builder<W>,
    dir: &Path,
    prefix: &Path,
) -> Result<()> {
    let mut entries: Vec<_> = fs::read_dir(dir)?.collect::<std::io::Result<_>>()?;
    entries.sort_by_key(|e| e.file_name());
    for entry in entries {
        let name = entry.file_name();
        if name.to_string_lossy() == "__pycache__" {
            continue;
        }
        let path = entry.path();
        let archived = prefix.join(&name);
        if path.is_dir() {
            append_dir(builder, &path, &archived)?;
        } else {
            let mut file = fs::File::open(&path)?;
            let mut buf = Vec::new();
            file.read_to_end(&mut buf)?;
            let mut header = tar::Header::new_gnu();
            header.set_size(buf.len() as u64);
            header.set_mode(0o644);
            header.set_cksum();
            builder.append_data(&mut header, &archived, buf.as_slice())?;
        }
    }
    Ok(())
}

fn print_skill_line(skill: &Skill) {
    let mut tags = Vec::new();
    if skill.visibility == SkillVisibility::Private {
        tags.push("private".to_string());
    }
    if !skill.min_tier.is_empty() {
        tags.push(format!("requires {}", skill.min_tier));
    }
    if !skill.entitled {
        tags.push("not entitled".to_string());
    }
    let suffix = if tags.is_empty() {
        String::new()
    } else {
        format!("  [{}]", tags.join(", "))
    };
    println!(
        "{:28} v{:10} {}{}",
        skill.slug, skill.latest_version, skill.name, suffix
    );
}

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

    #[test]
    fn pack_and_materialize_roundtrip() {
        let base = std::env::temp_dir().join(format!("nolgia-skill-test-{}", std::process::id()));
        let src = base.join("src/my-skill");
        fs::create_dir_all(src.join("nested")).unwrap();
        fs::write(src.join("SKILL.md"), "---\nname: my-skill\n---\n").unwrap();
        fs::write(src.join("nested/tool.py"), "print('hi')\n").unwrap();
        fs::create_dir_all(src.join("__pycache__")).unwrap();
        fs::write(src.join("__pycache__/junk.pyc"), "x").unwrap();

        let targz = pack(&src).unwrap();
        let root = base.join("skills");
        fs::create_dir_all(&root).unwrap();
        materialize(&root, "my-skill", "1.2.3", &targz).unwrap();

        let installed = root.join("my-skill");
        assert!(installed.join("SKILL.md").is_file());
        assert!(installed.join("nested/tool.py").is_file());
        assert!(!installed.join("__pycache__").exists());
        assert_eq!(marker_version(&installed).as_deref(), Some("1.2.3"));

        // Re-materializing a new version replaces the dir atomically.
        materialize(&root, "my-skill", "1.3.0", &targz).unwrap();
        assert_eq!(marker_version(&installed).as_deref(), Some("1.3.0"));

        fs::remove_dir_all(&base).ok();
    }
}