codewhale-tui 0.8.63

Terminal UI for open-source and open-weight coding models
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
//! Table-driven registries for `codewhale remote-setup`.
//!
//! Mirrors the `ProviderKind`/`provider::Provider` registry pattern in
//! `crates/config/src/lib.rs`: adding a cloud or a bridge is one row of data,
//! not a new control-flow branch. The wizard in [`super`] iterates these tables
//! rather than hard-coding clouds/bridges, so the matrix grows by data.
//!
//! - [`BridgeSpec`] — pure transport between a chat app and the local runtime.
//! - [`CloudTarget`] — where the agent runs and where its secrets live.
//! - The provider dimension is *not* duplicated here: it reads the existing
//!   `codewhale_config::provider` registry (see [`super::bundle::ProviderInfo`]).

/// Where a cloud target stores the runtime/provider secrets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SecretStore {
    /// Secrets live in `/etc/codewhale/*.env` files on the host.
    EnvFile,
    /// Secrets live in a managed vault (e.g. Azure Key Vault), read at boot.
    KeyVault,
}

impl SecretStore {
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            SecretStore::EnvFile => "EnvFile (/etc/codewhale/*.env)",
            SecretStore::KeyVault => "Key Vault (managed identity at boot)",
        }
    }
}

/// How the runtime + bridge are installed on the host.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InstallMethod {
    /// Native `cargo install` + systemd units (mirrors deploy/tencent-lighthouse).
    NativeSystemd,
    /// Container image pulled and run under systemd / a container runtime.
    Docker,
}

impl InstallMethod {
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            InstallMethod::NativeSystemd => "native + systemd",
            InstallMethod::Docker => "Docker image",
        }
    }
}

/// A single provisioning step expressed as **data**, never a shell string.
///
/// Commands are returned as `(program, args)` so the confirmation gate can print
/// every command before running anything, secrets are fed via stdin/temp files
/// (never argv or shell history — `secret_args` lists arg indexes to redact when
/// printing), and `--apply` simply executes the already-printed plan. In the
/// generate-only MVP these steps are only *rendered into the RUNBOOK*; nothing
/// is executed.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProvisionStep {
    /// Human-readable description shown in the plan / RUNBOOK.
    pub description: String,
    /// Program to run (e.g. `az`, `doctl`).
    pub program: String,
    /// Arguments, in order.
    pub args: Vec<String>,
    /// Indexes into `args` whose values are secret and must be redacted when
    /// the plan is printed. (Empty for the data-only RUNBOOK rows here.)
    pub secret_args: Vec<usize>,
}

impl ProvisionStep {
    pub fn new(description: impl Into<String>, program: impl Into<String>, args: &[&str]) -> Self {
        Self {
            description: description.into(),
            program: program.into(),
            args: args.iter().map(|a| (*a).to_string()).collect(),
            secret_args: Vec::new(),
        }
    }

    /// Render the command for display, redacting any secret arg positions.
    #[must_use]
    pub fn display_command(&self) -> String {
        let mut parts = Vec::with_capacity(self.args.len() + 1);
        parts.push(self.program.clone());
        for (idx, arg) in self.args.iter().enumerate() {
            if self.secret_args.contains(&idx) {
                parts.push("<redacted>".to_string());
            } else {
                parts.push(arg.clone());
            }
        }
        parts.join(" ")
    }
}

/// Inputs collected by the wizard that a cloud `plan()` reads.
///
/// Deliberately minimal and side-effect-free: a `plan()` turns these into an
/// ordered list of [`ProvisionStep`]s. Secret *values* are never placed here;
/// the plan references where they will be read from (env file / vault), so this
/// struct stays safe to print and to construct in tests.
#[derive(Debug, Clone)]
pub struct DeployInputs {
    /// Bridge slug, e.g. `"telegram"`.
    pub bridge_slug: String,
    /// Provider slug, e.g. `"deepseek"`.
    pub provider_slug: String,
    /// Cloud region / location (default per cloud).
    pub region: String,
    /// Logical instance / resource name.
    pub instance_name: String,
    /// Container image used by Docker installs.
    pub image: String,
}

impl Default for DeployInputs {
    fn default() -> Self {
        Self {
            bridge_slug: "telegram".to_string(),
            provider_slug: "deepseek".to_string(),
            region: String::new(),
            instance_name: "codewhale-remote".to_string(),
            image: "ghcr.io/hmbown/codewhale:latest".to_string(),
        }
    }
}

/// A chat bridge: pure transport between a chat app and `127.0.0.1:7878`.
#[derive(Debug, Clone, Copy)]
pub struct BridgeSpec {
    /// Stable slug used on the CLI and in paths, e.g. `"telegram"`.
    pub slug: &'static str,
    /// Human-readable label.
    pub display: &'static str,
    /// Package directory (relative to repo root), e.g. `"integrations/telegram-bridge"`.
    pub package_dir: &'static str,
    /// Systemd unit filename for the bridge.
    pub service_unit: &'static str,
    /// Repo-relative path of the reference env template shipped with deploy/.
    pub env_template: &'static str,
    /// Bridge-specific secret env keys the wizard prompts for (token(s), etc.).
    pub secret_keys: &'static [&'static str],
    /// One-liner shown before prompting (where to get the bridge credentials).
    pub setup_hint: &'static str,
    /// systemd `WorkingDirectory` the unit expects the bridge to be installed at.
    pub install_dir: &'static str,
}

/// A cloud target: where the agent runs and where secrets live.
#[derive(Debug, Clone, Copy)]
pub struct CloudTarget {
    /// Stable slug used on the CLI and in paths, e.g. `"azure"`.
    pub slug: &'static str,
    /// Human-readable label.
    pub display: &'static str,
    /// Where runtime/provider secrets are stored.
    pub secret_store: SecretStore,
    /// How the runtime + bridge are installed.
    pub install: InstallMethod,
    /// Default region/location for this cloud.
    pub default_region: &'static str,
    /// Cloud CLI used by the (stubbed) auto-provision path, e.g. `"az"`.
    pub cli_tool: &'static str,
    /// Builds the ordered provisioning plan as data. In the generate-only MVP
    /// this is only rendered into the RUNBOOK; `--apply` is not implemented.
    pub plan: fn(&DeployInputs) -> Vec<ProvisionStep>,
}

// ---------------------------------------------------------------------------
// Bridge registry
// ---------------------------------------------------------------------------

/// Telegram bridge — long-poll transport, secret is the BotFather token.
pub const TELEGRAM: BridgeSpec = BridgeSpec {
    slug: "telegram",
    display: "Telegram",
    package_dir: "integrations/telegram-bridge",
    service_unit: "codewhale-telegram-bridge.service",
    env_template: "deploy/tencent-lighthouse/examples/telegram-bridge.env.example",
    secret_keys: &["TELEGRAM_BOT_TOKEN"],
    setup_hint: "Create a bot with @BotFather in Telegram and copy the HTTP API token.",
    install_dir: "/opt/codewhale/telegram-bridge",
};

/// Feishu/Lark bridge — app id + secret are the bridge credentials.
pub const FEISHU: BridgeSpec = BridgeSpec {
    slug: "feishu",
    display: "Feishu/Lark",
    package_dir: "integrations/feishu-bridge",
    service_unit: "codewhale-feishu-bridge.service",
    env_template: "deploy/tencent-lighthouse/examples/feishu-bridge.env.example",
    secret_keys: &["FEISHU_APP_ID", "FEISHU_APP_SECRET"],
    setup_hint: "Create a custom app in the Feishu/Lark Open Platform; copy its App ID and App Secret.",
    install_dir: "/opt/codewhale/bridge",
};

/// All registered bridges. Adding a bridge is one row here.
pub const BRIDGES: &[BridgeSpec] = &[FEISHU, TELEGRAM];

/// Look up a bridge by slug.
#[must_use]
pub fn bridge_by_slug(slug: &str) -> Option<&'static BridgeSpec> {
    BRIDGES.iter().find(|b| b.slug.eq_ignore_ascii_case(slug))
}

// ---------------------------------------------------------------------------
// Cloud registry
// ---------------------------------------------------------------------------

/// Tencent Lighthouse — native systemd, env-file secrets, CNB-driven deploy.
pub const LIGHTHOUSE: CloudTarget = CloudTarget {
    slug: "lighthouse",
    display: "Tencent Lighthouse",
    secret_store: SecretStore::EnvFile,
    install: InstallMethod::NativeSystemd,
    default_region: "ap-hongkong",
    cli_tool: "cnb",
    plan: lighthouse_plan,
};

/// Azure VM — Docker image + Key Vault secrets via managed identity.
pub const AZURE: CloudTarget = CloudTarget {
    slug: "azure",
    display: "Azure VM",
    secret_store: SecretStore::KeyVault,
    install: InstallMethod::Docker,
    default_region: "eastus",
    cli_tool: "az",
    plan: azure_plan,
};

/// DigitalOcean Droplet — native systemd, env-file secrets, cloud-init + doctl.
///
/// Hunter-requested target. Modeled like Azure/Lighthouse: secrets in
/// `/etc/codewhale/*.env`, native+systemd install driven by a cloud-init
/// user-data file, and `doctl` for the create/destroy commands. The `plan()`
/// returns `doctl` `ProvisionStep` data, but since `--apply` is stubbed in the
/// MVP the plan is only printed inside the generated RUNBOOK.
pub const DIGITALOCEAN: CloudTarget = CloudTarget {
    slug: "digitalocean",
    display: "DigitalOcean Droplet",
    secret_store: SecretStore::EnvFile,
    install: InstallMethod::NativeSystemd,
    default_region: "sfo3",
    cli_tool: "doctl",
    plan: digitalocean_plan,
};

/// All registered cloud targets. Adding a cloud is one row here.
pub const CLOUD_TARGETS: &[CloudTarget] = &[LIGHTHOUSE, AZURE, DIGITALOCEAN];

/// Look up a cloud target by slug.
#[must_use]
pub fn cloud_by_slug(slug: &str) -> Option<&'static CloudTarget> {
    CLOUD_TARGETS
        .iter()
        .find(|c| c.slug.eq_ignore_ascii_case(slug))
}

// ---------------------------------------------------------------------------
// Cloud plans (data only — never executed in the MVP)
// ---------------------------------------------------------------------------

fn lighthouse_plan(inputs: &DeployInputs) -> Vec<ProvisionStep> {
    // Lighthouse provisioning is driven by the existing CNB pipeline
    // (deploy/tencent-lighthouse/cnb/*). The "plan" here is the CNB trigger plus
    // the host-side service install the RUNBOOK walks the user through.
    let restart_bridge = format!("codewhale-{}-bridge", inputs.bridge_slug);
    vec![
        ProvisionStep::new(
            "Render and commit the CNB pipeline (cnb.yml + tag_deploy.yml) for this deploy",
            "git",
            &["add", ".cnb.yml", ".cnb/tag_deploy.yml"],
        ),
        ProvisionStep::new(
            "Trigger the CNB `web_trigger_lighthouse` button to build + ship to the host",
            "cnb",
            &["trigger", "web_trigger_lighthouse"],
        ),
        ProvisionStep::new(
            "On the host: install both systemd units and start the runtime + bridge",
            "bash",
            &["scripts/tencent-lighthouse/install-services.sh"],
        ),
        ProvisionStep::new(
            format!("Restart the bridge service after the deploy ({restart_bridge})"),
            "systemctl",
            &["restart", &restart_bridge],
        ),
    ]
}

fn azure_plan(inputs: &DeployInputs) -> Vec<ProvisionStep> {
    let rg = format!("{}-rg", inputs.instance_name);
    let vault = format!("{}-kv", inputs.instance_name);
    let provider_secret = format!("codewhale-{}-key", inputs.provider_slug);
    vec![
        ProvisionStep::new(
            "Create the resource group",
            "az",
            &[
                "group",
                "create",
                "--name",
                &rg,
                "--location",
                &inputs.region,
            ],
        ),
        ProvisionStep::new(
            "Create the Key Vault that holds the provider key + runtime token",
            "az",
            &[
                "keyvault",
                "create",
                "--name",
                &vault,
                "--resource-group",
                &rg,
                "--location",
                &inputs.region,
            ],
        ),
        ProvisionStep::new(
            format!(
                "Store the {} provider key in Key Vault (value piped via stdin, not argv)",
                inputs.provider_slug
            ),
            "az",
            &[
                "keyvault",
                "secret",
                "set",
                "--vault-name",
                &vault,
                "--name",
                &provider_secret,
            ],
        ),
        ProvisionStep::new(
            format!(
                "Create the VM from {} with cloud-init custom-data + a system-assigned identity",
                inputs.image
            ),
            "az",
            &[
                "vm",
                "create",
                "--resource-group",
                &rg,
                "--name",
                &inputs.instance_name,
                "--custom-data",
                "cloud-init.yaml",
                "--assign-identity",
            ],
        ),
        ProvisionStep::new(
            "Scope the NSG to SSH (22) from the caller IP only; 7878 stays on 127.0.0.1",
            "az",
            &[
                "vm",
                "open-port",
                "--resource-group",
                &rg,
                "--name",
                &inputs.instance_name,
                "--port",
                "22",
            ],
        ),
    ]
}

fn digitalocean_plan(inputs: &DeployInputs) -> Vec<ProvisionStep> {
    // A Droplet stood up from a cloud-init user-data file, then the host-side
    // service install. doctl is the cloud CLI; commands are data only here.
    vec![
        ProvisionStep::new(
            "Create the Droplet from the generated cloud-init user-data (native + systemd)",
            "doctl",
            &[
                "compute",
                "droplet",
                "create",
                &inputs.instance_name,
                "--region",
                &inputs.region,
                "--image",
                "ubuntu-24-04-x64",
                "--size",
                "s-2vcpu-4gb",
                "--user-data-file",
                "cloud-init.yaml",
                "--ssh-keys",
                "<your-ssh-key-fingerprint>",
                "--wait",
            ],
        ),
        ProvisionStep::new(
            "Read the Droplet's public IPv4 for the SSH step below",
            "doctl",
            &[
                "compute",
                "droplet",
                "get",
                &inputs.instance_name,
                "--format",
                "PublicIPv4",
                "--no-header",
            ],
        ),
        ProvisionStep::new(
            "On the Droplet: write /etc/codewhale/*.env, install both systemd units, enable --now",
            "bash",
            &["scripts/tencent-lighthouse/install-services.sh"],
        ),
    ]
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashSet;
    use std::path::{Path, PathBuf};

    /// Repo root, resolved from this crate's manifest dir (`crates/tui`).
    fn repo_root() -> PathBuf {
        Path::new(env!("CARGO_MANIFEST_DIR"))
            .parent()
            .and_then(Path::parent)
            .expect("crates/tui has a two-level parent (repo root)")
            .to_path_buf()
    }

    #[test]
    fn bridge_slugs_are_unique() {
        let mut seen = HashSet::new();
        for b in BRIDGES {
            assert!(seen.insert(b.slug), "duplicate bridge slug: {}", b.slug);
        }
        assert_eq!(seen.len(), BRIDGES.len());
    }

    #[test]
    fn cloud_slugs_are_unique() {
        let mut seen = HashSet::new();
        for c in CLOUD_TARGETS {
            assert!(seen.insert(c.slug), "duplicate cloud slug: {}", c.slug);
        }
        assert_eq!(seen.len(), CLOUD_TARGETS.len());
    }

    #[test]
    fn digitalocean_is_registered() {
        // Hunter explicitly wants DigitalOcean in the matrix.
        assert!(
            cloud_by_slug("digitalocean").is_some(),
            "DigitalOcean must be a registered cloud target"
        );
        let r#do = cloud_by_slug("digitalocean").unwrap();
        assert_eq!(r#do.secret_store, SecretStore::EnvFile);
        assert_eq!(r#do.install, InstallMethod::NativeSystemd);
        assert_eq!(r#do.cli_tool, "doctl");
    }

    #[test]
    fn every_bridge_references_existing_files() {
        let root = repo_root();
        for b in BRIDGES {
            let pkg = root.join(b.package_dir);
            assert!(
                pkg.is_dir(),
                "bridge {} package_dir missing: {}",
                b.slug,
                pkg.display()
            );
            let unit = root
                .join("deploy/tencent-lighthouse/systemd")
                .join(b.service_unit);
            assert!(
                unit.is_file(),
                "bridge {} service_unit missing: {}",
                b.slug,
                unit.display()
            );
            let template = root.join(b.env_template);
            assert!(
                template.is_file(),
                "bridge {} env_template missing: {}",
                b.slug,
                template.display()
            );
            assert!(
                !b.secret_keys.is_empty(),
                "bridge {} must declare at least one secret key",
                b.slug
            );
        }
    }

    #[test]
    fn lookup_helpers_are_case_insensitive() {
        assert_eq!(bridge_by_slug("TELEGRAM").map(|b| b.slug), Some("telegram"));
        assert_eq!(cloud_by_slug("Azure").map(|c| c.slug), Some("azure"));
        assert!(bridge_by_slug("nope").is_none());
        assert!(cloud_by_slug("nope").is_none());
    }

    #[test]
    fn cloud_plans_return_ordered_steps_without_executing() {
        // Build (never run) a plan for each cloud and assert on program+args.
        let inputs = DeployInputs::default();
        for c in CLOUD_TARGETS {
            let steps = (c.plan)(&inputs);
            assert!(!steps.is_empty(), "cloud {} produced an empty plan", c.slug);
            // First step's program is the cloud's own tooling or a host script.
            assert!(
                steps
                    .iter()
                    .all(|s| !s.program.is_empty() && !s.description.is_empty()),
                "cloud {} has a malformed step",
                c.slug
            );
        }

        // DigitalOcean specifically drives doctl.
        let do_steps = (DIGITALOCEAN.plan)(&inputs);
        assert!(
            do_steps.iter().any(|s| s.program == "doctl"),
            "DigitalOcean plan must use doctl"
        );
        // Azure specifically drives az.
        let az_steps = (AZURE.plan)(&inputs);
        assert!(
            az_steps.iter().any(|s| s.program == "az"),
            "Azure plan must use az"
        );
    }

    #[test]
    fn display_command_redacts_secret_args() {
        let mut step =
            ProvisionStep::new("set secret", "az", &["keyvault", "secret", "set", "VALUE"]);
        step.secret_args = vec![3];
        let rendered = step.display_command();
        assert!(rendered.contains("<redacted>"));
        assert!(!rendered.contains("VALUE"));
    }
}