nemo-relay-cli 0.5.0

Coding-agent gateway CLI for NeMo Relay observability.
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
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

//! Host CLI discovery and marketplace registration commands.

use std::env;
use std::path::{Path, PathBuf};
use std::process::Command;

use serde_json::Value;

#[cfg(test)]
use serde_json::json;

use crate::config::PluginHost;

use super::state::{PluginInstallOptions, PluginLayout};
use super::{MARKETPLACE_NAME, PLUGIN_NAME, RELAY_COMMAND, host_cli};

pub(super) fn run_host_marketplace_registration(
    host: PluginHost,
    layout: &PluginLayout,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    run_command(
        host_cli(host),
        &[
            "plugin".into(),
            "marketplace".into(),
            "add".into(),
            layout.marketplace_root.display().to_string(),
        ],
        options,
        runner,
    )
}

pub(super) fn run_host_plugin_registration(
    host: PluginHost,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    match host {
        PluginHost::Codex => run_command(
            host_cli(host),
            &[
                "plugin".into(),
                "add".into(),
                format!("{PLUGIN_NAME}@{MARKETPLACE_NAME}"),
            ],
            options,
            runner,
        ),
        PluginHost::ClaudeCode => run_command(
            host_cli(host),
            &[
                "plugin".into(),
                "install".into(),
                format!("{PLUGIN_NAME}@{MARKETPLACE_NAME}"),
                "--scope".into(),
                "user".into(),
            ],
            options,
            runner,
        ),
        PluginHost::All => unreachable!("all is expanded before host registration"),
    }
}

pub(super) fn run_host_plugin_removal(
    host: PluginHost,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    match host {
        PluginHost::Codex => run_command(
            host_cli(host),
            &[
                "plugin".into(),
                "remove".into(),
                format!("{PLUGIN_NAME}@{MARKETPLACE_NAME}"),
            ],
            options,
            runner,
        )?,
        PluginHost::ClaudeCode => run_command(
            host_cli(host),
            &["plugin".into(), "uninstall".into(), PLUGIN_NAME.into()],
            options,
            runner,
        )?,
        PluginHost::All => unreachable!("all is expanded before host unregistration"),
    }
    Ok(())
}

pub(super) fn run_host_marketplace_removal(
    host: PluginHost,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    run_command(
        host_cli(host),
        &[
            "plugin".into(),
            "marketplace".into(),
            "remove".into(),
            MARKETPLACE_NAME.into(),
        ],
        options,
        runner,
    )
}

#[derive(Debug, Clone)]
pub(super) struct HostRegistrationReport {
    pub(super) host_plugin_registered: bool,
    pub(super) host_marketplace_registered: bool,
}

impl HostRegistrationReport {
    pub(super) fn ok(&self) -> bool {
        self.host_plugin_registered && self.host_marketplace_registered
    }

    #[cfg(test)]
    pub(super) fn to_json(&self) -> Value {
        json!({
            "ok": self.ok(),
            "host_plugin_registered": self.host_plugin_registered,
            "host_marketplace_registered": self.host_marketplace_registered
        })
    }
}

#[cfg(test)]
pub(super) fn validate_host_registration(
    host: PluginHost,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<HostRegistrationReport, String> {
    let report = host_registration_report(host, options, runner)?;
    if report.ok() {
        Ok(report)
    } else {
        let mut missing = Vec::new();
        if !report.host_plugin_registered {
            missing.push(format!("{PLUGIN_NAME}@{MARKETPLACE_NAME} host plugin"));
        }
        if !report.host_marketplace_registered {
            missing.push(format!("{MARKETPLACE_NAME} host marketplace"));
        }
        Err(format!(
            "{} plugin host registration is incomplete: missing {}",
            host_cli(host),
            missing.join(", ")
        ))
    }
}

pub(super) fn host_registration_report(
    host: PluginHost,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<HostRegistrationReport, String> {
    if options.dry_run {
        return Ok(HostRegistrationReport {
            host_plugin_registered: true,
            host_marketplace_registered: true,
        });
    }
    require_host_cli(host, options, runner)?;
    Ok(match host {
        PluginHost::ClaudeCode => HostRegistrationReport {
            host_plugin_registered: claude_plugin_registered(options, runner)?,
            host_marketplace_registered: claude_marketplace_registered(options, runner)?,
        },
        PluginHost::Codex => HostRegistrationReport {
            host_plugin_registered: codex_plugin_registered(options, runner)?,
            host_marketplace_registered: codex_marketplace_registered(options, runner)?,
        },
        PluginHost::All => unreachable!("all is expanded before host registration checks"),
    })
}

fn claude_plugin_registered(
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<bool, String> {
    let output = run_capture_command(
        "claude",
        &["plugin".into(), "list".into(), "--json".into()],
        options,
        runner,
    )?;
    let plugins = parse_json_command_output("claude plugin list --json", output)?;
    Ok(plugins
        .as_array()
        .is_some_and(|plugins| plugins.iter().any(plugin_entry_matches)))
}

fn claude_marketplace_registered(
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<bool, String> {
    let output = run_capture_command(
        "claude",
        &[
            "plugin".into(),
            "marketplace".into(),
            "list".into(),
            "--json".into(),
        ],
        options,
        runner,
    )?;
    let marketplaces = parse_json_command_output("claude plugin marketplace list --json", output)?;
    Ok(marketplaces
        .as_array()
        .is_some_and(|marketplaces| marketplaces.iter().any(marketplace_entry_matches)))
}

fn codex_plugin_registered(
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<bool, String> {
    // Codex `plugin list` has no `--json` flag (unlike Claude Code).
    let output = run_capture_command("codex", &["plugin".into(), "list".into()], options, runner)?;
    let plugin_id = format!("{PLUGIN_NAME}@{MARKETPLACE_NAME}");
    Ok(output
        .stdout
        .lines()
        .any(|line| codex_plugin_line_installed(line, &plugin_id)))
}

fn codex_plugin_line_installed(line: &str, plugin_id: &str) -> bool {
    let mut columns = line.split_whitespace();
    if columns.next() != Some(plugin_id) {
        return false;
    }
    columns
        .next()
        .is_some_and(|status| status.starts_with("installed"))
}

fn codex_marketplace_registered(
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<bool, String> {
    let output = run_capture_command(
        "codex",
        &["plugin".into(), "marketplace".into(), "list".into()],
        options,
        runner,
    )?;
    Ok(output
        .stdout
        .lines()
        .filter_map(|line| line.split_whitespace().next())
        .any(|name| name == MARKETPLACE_NAME))
}

fn plugin_entry_matches(entry: &Value) -> bool {
    let plugin_id = format!("{PLUGIN_NAME}@{MARKETPLACE_NAME}");
    string_field(entry, "id") == Some(plugin_id.as_str())
        || string_field(entry, "pluginId") == Some(plugin_id.as_str())
        || (string_field(entry, "name") == Some(PLUGIN_NAME)
            && string_field(entry, "marketplaceName") == Some(MARKETPLACE_NAME))
}

fn marketplace_entry_matches(entry: &Value) -> bool {
    string_field(entry, "name") == Some(MARKETPLACE_NAME)
        || string_field(entry, "id") == Some(MARKETPLACE_NAME)
}

fn string_field<'a>(value: &'a Value, key: &str) -> Option<&'a str> {
    value.get(key).and_then(Value::as_str)
}

fn parse_json_command_output(command: &str, output: CommandOutput) -> Result<Value, String> {
    serde_json::from_str::<Value>(&output.stdout)
        .map_err(|error| format!("failed to parse `{command}` output as JSON: {error}"))
}

pub(super) fn require_relay(
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<PathBuf, String> {
    if options.dry_run {
        return Ok(PathBuf::from(RELAY_COMMAND));
    }
    runner
        .resolve_executable(RELAY_COMMAND)?
        .ok_or_else(|| "required `nemo-relay` executable was not found on PATH".into())
}

pub(super) fn validate_relay_plugin_shim(
    relay: &Path,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    if options.dry_run {
        return Ok(());
    }
    let args = ["plugin-shim".into(), "hook".into(), "--help".into()];
    let status = runner.run_quiet(relay, &args)?;
    if status == 0 {
        Ok(())
    } else {
        Err(format!(
            "{} failed with exit code {status}; installed hooks require `nemo-relay plugin-shim hook` support",
            format_command(&relay.display().to_string(), &args)
        ))
    }
}

pub(super) fn require_host_cli(
    host: PluginHost,
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    if options.dry_run {
        return Ok(());
    }
    let cli = host_cli(host);
    runner
        .resolve_executable(cli)?
        .map(|_| ())
        .ok_or_else(|| format!("required `{cli}` CLI was not found on PATH"))
}

pub(super) fn run_command(
    program: &str,
    args: &[String],
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    if options.dry_run {
        println!("{}", format_command(program, args));
        return Ok(());
    }
    let resolved = runner
        .resolve_executable(program)?
        .ok_or_else(|| format!("required `{program}` executable was not found on PATH"))?;
    run_path_command(&resolved, args, options, runner)
}

pub(super) fn run_path_command(
    program: &Path,
    args: &[String],
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<(), String> {
    if options.dry_run {
        println!("{}", format_command(&program.display().to_string(), args));
        return Ok(());
    }
    let status = runner.run(program, args)?;
    if status == 0 {
        Ok(())
    } else {
        Err(format!(
            "{} failed with exit code {status}",
            format_command(&program.display().to_string(), args)
        ))
    }
}

pub(super) fn run_capture_command(
    program: &str,
    args: &[String],
    options: &PluginInstallOptions,
    runner: &dyn CommandRunner,
) -> Result<CommandOutput, String> {
    if options.dry_run {
        println!("{}", format_command(program, args));
        // Keep dry-run capture output syntactically valid for future callers that parse stdout.
        return Ok(CommandOutput::success("null\n".into()));
    }
    let resolved = runner
        .resolve_executable(program)?
        .ok_or_else(|| format!("required `{program}` executable was not found on PATH"))?;
    let output = runner.run_capture(&resolved, args)?;
    if output.status == 0 {
        Ok(output)
    } else {
        let stderr = output.stderr.trim();
        let detail = if stderr.is_empty() {
            String::new()
        } else {
            format!(": {stderr}")
        };
        Err(format!(
            "{} failed with exit code {}{detail}",
            format_command(&resolved.display().to_string(), args),
            output.status
        ))
    }
}

pub(super) fn format_command(program: &str, args: &[String]) -> String {
    let mut parts = vec![program.to_string()];
    parts.extend(args.iter().cloned());
    format!(
        "$ {}",
        parts
            .iter()
            .map(|part| shell_quote(part))
            .collect::<Vec<_>>()
            .join(" ")
    )
}

fn shell_quote(raw: &str) -> String {
    if raw.chars().all(|ch| {
        ch.is_ascii_alphanumeric()
            || matches!(ch, '/' | '\\' | ':' | '.' | '_' | '-' | '=' | '@' | '+')
    }) {
        raw.into()
    } else {
        let mut escaped = String::new();
        for ch in raw.chars() {
            if matches!(ch, '"' | '\\' | '$' | '`') {
                escaped.push('\\');
            }
            escaped.push(ch);
        }
        format!("\"{escaped}\"")
    }
}

#[derive(Debug, Clone)]
pub(super) struct CommandOutput {
    pub(super) status: i32,
    pub(super) stdout: String,
    pub(super) stderr: String,
}

impl CommandOutput {
    pub(super) fn success(stdout: String) -> Self {
        Self {
            status: 0,
            stdout,
            stderr: String::new(),
        }
    }
}

pub(super) trait CommandRunner {
    fn resolve_executable(&self, command: &str) -> Result<Option<PathBuf>, String>;
    fn run(&self, program: &Path, args: &[String]) -> Result<i32, String>;
    fn run_quiet(&self, program: &Path, args: &[String]) -> Result<i32, String>;
    fn run_capture(&self, program: &Path, args: &[String]) -> Result<CommandOutput, String>;
}

pub(super) struct RealCommandRunner;

impl CommandRunner for RealCommandRunner {
    fn resolve_executable(&self, command: &str) -> Result<Option<PathBuf>, String> {
        Ok(find_executable(command))
    }

    fn run(&self, program: &Path, args: &[String]) -> Result<i32, String> {
        #[cfg(windows)]
        if is_windows_command_script(program) {
            let status = Command::new(env::var_os("COMSPEC").unwrap_or_else(|| "cmd.exe".into()))
                .args(["/d", "/s", "/c"])
                .arg(windows_command_line(program, args))
                .status()
                .map_err(|error| format!("failed to run {}: {error}", program.display()))?;
            return Ok(status.code().unwrap_or(1));
        }

        let status = Command::new(program)
            .args(args)
            .status()
            .map_err(|error| format!("failed to run {}: {error}", program.display()))?;
        Ok(status.code().unwrap_or(1))
    }

    fn run_quiet(&self, program: &Path, args: &[String]) -> Result<i32, String> {
        #[cfg(windows)]
        if is_windows_command_script(program) {
            let status = Command::new(env::var_os("COMSPEC").unwrap_or_else(|| "cmd.exe".into()))
                .args(["/d", "/s", "/c"])
                .arg(windows_command_line(program, args))
                .stdin(std::process::Stdio::null())
                .stdout(std::process::Stdio::null())
                .stderr(std::process::Stdio::null())
                .status()
                .map_err(|error| format!("failed to run {}: {error}", program.display()))?;
            return Ok(status.code().unwrap_or(1));
        }

        let status = Command::new(program)
            .args(args)
            .stdin(std::process::Stdio::null())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null())
            .status()
            .map_err(|error| format!("failed to run {}: {error}", program.display()))?;
        Ok(status.code().unwrap_or(1))
    }

    fn run_capture(&self, program: &Path, args: &[String]) -> Result<CommandOutput, String> {
        #[cfg(windows)]
        if is_windows_command_script(program) {
            let output = Command::new(env::var_os("COMSPEC").unwrap_or_else(|| "cmd.exe".into()))
                .args(["/d", "/s", "/c"])
                .arg(windows_command_line(program, args))
                .output()
                .map_err(|error| format!("failed to run {}: {error}", program.display()))?;
            return Ok(command_output(output));
        }

        let output = Command::new(program)
            .args(args)
            .output()
            .map_err(|error| format!("failed to run {}: {error}", program.display()))?;
        Ok(command_output(output))
    }
}

fn command_output(output: std::process::Output) -> CommandOutput {
    CommandOutput {
        status: output.status.code().unwrap_or(1),
        stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
        stderr: String::from_utf8_lossy(&output.stderr).into_owned(),
    }
}

fn find_executable(command: &str) -> Option<PathBuf> {
    let path = env::var_os("PATH")?;
    let candidates = env::split_paths(&path);
    let extensions = executable_extensions(command);
    for dir in candidates {
        for extension in &extensions {
            let candidate = dir.join(format!("{command}{extension}"));
            if candidate.is_file() {
                return Some(candidate);
            }
        }
    }
    None
}

fn executable_extensions(command: &str) -> Vec<String> {
    if cfg!(windows) && Path::new(command).extension().is_none() {
        env::var("PATHEXT")
            .unwrap_or_else(|_| ".EXE;.CMD;.BAT;.COM".into())
            .split(';')
            .map(str::to_string)
            .collect()
    } else {
        vec![String::new()]
    }
}

#[cfg(windows)]
fn is_windows_command_script(program: &Path) -> bool {
    program
        .extension()
        .and_then(|extension| extension.to_str())
        .is_some_and(|extension| {
            extension.eq_ignore_ascii_case("cmd") || extension.eq_ignore_ascii_case("bat")
        })
}

#[cfg(windows)]
fn windows_command_line(program: &Path, args: &[String]) -> String {
    std::iter::once(windows_command_argument(&program.display().to_string()))
        .chain(args.iter().map(|arg| windows_command_argument(arg)))
        .collect::<Vec<_>>()
        .join(" ")
}

#[cfg(windows)]
fn windows_command_argument(argument: &str) -> String {
    format!("\"{}\"", argument.replace('"', "\\\""))
}