| 1 | //! `/plugin import dsh`: review, then install, a DeepSeek Harness bundle |
| 2 | //! package through the ordinary reviewed installer. |
| 3 | //! |
| 4 | //! Review converts the package into scratch and shows what converts, what is |
| 5 | //! skipped (each a manual port), the authority the bundle will request, and |
| 6 | //! the content hash. Approval installs only that exact converted bundle; it |
| 7 | //! lands disabled and untrusted like every other install, and the existing |
| 8 | //! trust and enable commands take it from there. |
| 9 | |
| 10 | use super::render::ReviewTextContext; |
| 11 | use std::fmt::Write as _; |
| 12 | use std::path::Path; |
| 13 | |
| 14 | use codewhale_command_contract::facets::{CommandPluginContext, CommandPresentationContext}; |
| 15 | |
| 16 | use super::{escape_review_path, escape_review_text}; |
| 17 | use crate::commands::CommandResult; |
| 18 | |
| 19 | pub(super) const USAGE: &str = |
| 20 | "/plugin import dsh <package-dir>\n/plugin import dsh approve <package-dir> <content-hash>"; |
| 21 | |
| 22 | pub(super) fn dispatch( |
| 23 | presentation: &mut dyn CommandPresentationContext, |
| 24 | plugin: &mut dyn CommandPluginContext, |
| 25 | words: &[&str], |
| 26 | ) -> CommandResult { |
| 27 | match words { |
| 28 | ["approve", path @ .., hash] if !path.is_empty() => { |
| 29 | approve(presentation, plugin, &path.join(" "), hash) |
| 30 | } |
| 31 | path if !path.is_empty() && path[0] != "approve" => review(plugin, &path.join(" ")), |
| 32 | _ => CommandResult::error(format!("Usage:\n{USAGE}")), |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | fn list_line(output: &mut String, label: &str, items: &[String]) { |
| 37 | let shown = if items.is_empty() { |
| 38 | "none".to_string() |
| 39 | } else { |
| 40 | items |
| 41 | .iter() |
| 42 | .map(|item| escape_review_text(item, ReviewTextContext::Prose)) |
| 43 | .collect::<Vec<_>>() |
| 44 | .join(", ") |
| 45 | }; |
| 46 | let _ = writeln!(output, " {label}: {shown}"); |
| 47 | } |
| 48 | |
| 49 | fn review(plugin: &dyn CommandPluginContext, path: &str) -> CommandResult { |
| 50 | let preview = match plugin.dsh_preview(Path::new(path)) { |
| 51 | Ok(preview) => preview, |
| 52 | Err(error) => return CommandResult::error(format!("DSH import refused: {error}")), |
| 53 | }; |
| 54 | let mut output = format!( |
| 55 | "DeepSeek Harness package {}{} → plugin '{}'\n Source: {}\n", |
| 56 | escape_review_text( |
| 57 | preview.source_package.as_deref().unwrap_or("unnamed"), |
| 58 | ReviewTextContext::Prose |
| 59 | ), |
| 60 | preview |
| 61 | .source_version |
| 62 | .as_deref() |
| 63 | .map(|version| format!("@{}", escape_review_text(version, ReviewTextContext::Prose))) |
| 64 | .unwrap_or_default(), |
| 65 | preview.plugin_name, |
| 66 | escape_review_path(&preview.package_path, ReviewTextContext::Prose), |
| 67 | ); |
| 68 | list_line(&mut output, "Skills", &preview.skills); |
| 69 | list_line(&mut output, "Commands", &preview.commands); |
| 70 | list_line(&mut output, "Agents", &preview.agents); |
| 71 | list_line( |
| 72 | &mut output, |
| 73 | "Hooks (run shell commands with your user's authority)", |
| 74 | &preview.hooks, |
| 75 | ); |
| 76 | list_line(&mut output, "Remote MCP servers", &preview.remote_servers); |
| 77 | list_line( |
| 78 | &mut output, |
| 79 | "Local MCP servers (run with your user's authority)", |
| 80 | &preview.local_servers, |
| 81 | ); |
| 82 | list_line( |
| 83 | &mut output, |
| 84 | "Network hosts it will request", |
| 85 | &preview.network_hosts, |
| 86 | ); |
| 87 | if !preview.native_rows.is_empty() { |
| 88 | list_line( |
| 89 | &mut output, |
| 90 | "Native host code (JavaScript; runs in the experimental plugin host)", |
| 91 | &preview.native_rows, |
| 92 | ); |
| 93 | if !codewhale_plugins::activation::extension_host_policy_enabled() { |
| 94 | output.push_str( |
| 95 | " Requires: [features] extension_host = true (off now, so this bundle cannot be enabled)\n", |
| 96 | ); |
| 97 | } |
| 98 | } |
| 99 | if preview.requires_node { |
| 100 | output.push_str(" Requires: node on PATH\n"); |
| 101 | } |
| 102 | if preview.manual_ports.is_empty() { |
| 103 | output.push_str(" Skipped: none\n"); |
| 104 | } else { |
| 105 | let _ = writeln!( |
| 106 | output, |
| 107 | " Skipped ({}; each needs a manual port and is not imported):", |
| 108 | preview.manual_ports.len() |
| 109 | ); |
| 110 | for line in &preview.manual_ports { |
| 111 | let _ = writeln!( |
| 112 | output, |
| 113 | " - {}", |
| 114 | escape_review_text(line, ReviewTextContext::Prose) |
| 115 | ); |
| 116 | } |
| 117 | } |
| 118 | if !preview.diagnostics.is_empty() { |
| 119 | output.push_str(" Diagnostics:\n"); |
| 120 | for line in &preview.diagnostics { |
| 121 | let _ = writeln!( |
| 122 | output, |
| 123 | " - {}", |
| 124 | escape_review_text(line, ReviewTextContext::Prose) |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | let _ = writeln!( |
| 129 | output, |
| 130 | " Content hash: {}\n\nNothing was installed. The full conversion receipt ships in the bundle as CONVERSION.md.\nTo import exactly this bundle (it lands disabled and untrusted):\n /plugin import dsh approve {} {}", |
| 131 | preview.content_hash, |
| 132 | preview.package_path.display(), |
| 133 | preview.content_hash, |
| 134 | ); |
| 135 | CommandResult::message(output) |
| 136 | } |
| 137 | |
| 138 | fn approve( |
| 139 | presentation: &mut dyn CommandPresentationContext, |
| 140 | plugin: &mut dyn CommandPluginContext, |
| 141 | path: &str, |
| 142 | expected_hash: &str, |
| 143 | ) -> CommandResult { |
| 144 | // The installer re-converts and refuses unless the staged bytes match the |
| 145 | // reviewed hash, so a package edited after review installs nothing. |
| 146 | match plugin.install(&format!("dsh:{path}"), Some(expected_hash)) { |
| 147 | Ok(receipt) => { |
| 148 | super::render_install_receipt(presentation, plugin, receipt, Some(expected_hash)) |
| 149 | } |
| 150 | Err(error) => super::action_error(presentation, &format!("DSH import failed: {error}")), |
| 151 | } |
| 152 | } |
| 153 |