返回 CodeWhale
kimi_import.rs
根目录 / crates / tui / src / commands / groups / plugins / kimi_import.rs
1 //! Explicit local import bridge for Kimi-managed plugins.
2 //!
3 //! Listing is read-only and only considers immediate, canonical child
4 //! directories of `~/.kimi-code/plugins/managed`. Import requires the exact
5 //! content hash shown by listing, then routes the local directory through the
6 //! ordinary reviewed installer. The resulting Codewhale plugin still starts
7 //! disabled and untrusted; this module never launches or probes an external
8 //! Kimi application, daemon, MCP binary, or permission grant.
9 //!
10 //! FEAT-020: the managed-directory scan runs host-side in the TUI adapter;
11 //! the handler consumes the portable `PluginManagedScan` and renders it.
12
13 use super::render::ReviewTextContext;
14 use std::fmt::Write as _;
15 use std::path::Path;
16
17 use codewhale_command_contract::facets::{CommandPluginContext, CommandPresentationContext};
18
19 use crate::commands::CommandResult;
20
21 const LIST_COMMAND: &str = "/plugin import kimi [list]";
22 const APPROVE_COMMAND: &str = "/plugin import kimi approve <name> <content-hash>";
23
24 pub(super) fn usage(presentation: &mut dyn CommandPresentationContext) -> String {
25 presentation
26 .translate(
27 "plugin_kimi_usage",
28 &[
29 ("list_command", LIST_COMMAND),
30 ("approve_command", APPROVE_COMMAND),
31 ],
32 )
33 .unwrap_or_default()
34 }
35
36 pub(super) fn dispatch(
37 presentation: &mut dyn CommandPresentationContext,
38 plugin: &mut dyn CommandPluginContext,
39 words: &[&str],
40 home_override: Option<&Path>,
41 ) -> CommandResult {
42 match words {
43 [] | ["list"] => list(presentation, plugin, home_override),
44 ["approve", name, content_hash] => {
45 approve(presentation, plugin, name, content_hash, home_override)
46 }
47 _ => CommandResult::error(usage(presentation)),
48 }
49 }
50
51 fn list(
52 presentation: &mut dyn CommandPresentationContext,
53 plugin: &dyn CommandPluginContext,
54 home_override: Option<&Path>,
55 ) -> CommandResult {
56 let scan = match plugin.managed_scan(home_override) {
57 Ok(scan) => scan,
58 Err(error) => return CommandResult::error(error),
59 };
60 let root = escape_review_path(&scan.root, ReviewTextContext::Prose);
61 let mut output = presentation
62 .translate("plugin_kimi_managed_root_heading", &[("root", &root)])
63 .unwrap_or_default();
64 output.push('\n');
65 if scan.candidates.is_empty() {
66 output.push_str(" ");
67 output.push_str(
68 &presentation
69 .translate("plugin_kimi_none_found", &[])
70 .unwrap_or_default(),
71 );
72 output.push('\n');
73 }
74 for candidate in &scan.candidates {
75 let name = escape_review_text(&candidate.name, ReviewTextContext::Prose);
76 let version = escape_review_text(&candidate.version, ReviewTextContext::Prose);
77 let license = candidate
78 .license
79 .as_deref()
80 .map(|value| escape_review_text(value, ReviewTextContext::Prose))
81 .unwrap_or_else(|| {
82 presentation
83 .translate("plugin_kimi_license_unspecified", &[])
84 .unwrap_or_default()
85 });
86 let applicability = presentation
87 .translate(
88 if candidate.applicable {
89 "plugin_kimi_applicable"
90 } else {
91 "plugin_kimi_not_applicable"
92 },
93 &[],
94 )
95 .unwrap_or_default();
96 let inventory = escape_review_text(&candidate.inventory, ReviewTextContext::Prose);
97 let summary = presentation
98 .translate(
99 "plugin_kimi_candidate_summary",
100 &[
101 ("name", &name),
102 ("version", &version),
103 ("license", &license),
104 ("applicability", &applicability),
105 ("inventory", &inventory),
106 ],
107 )
108 .unwrap_or_default();
109 let _ = writeln!(output, "\n{summary}");
110
111 let path = escape_review_path(&candidate.canonical_path, ReviewTextContext::Prose);
112 let approve_command = format!(
113 "/plugin import kimi approve {} {}",
114 candidate.name, candidate.content_hash
115 );
116 let details = presentation
117 .translate(
118 "plugin_kimi_candidate_details",
119 &[
120 ("path", &path),
121 ("content_hash", &candidate.content_hash),
122 ("capability_hash", &candidate.capability_hash),
123 ("approve_command", &approve_command),
124 ],
125 )
126 .unwrap_or_default();
127 let _ = writeln!(output, "{details}");
128 }
129 if !scan.rejected.is_empty() {
130 output.push('\n');
131 output.push_str(
132 &presentation
133 .translate("plugin_kimi_rejected_heading", &[])
134 .unwrap_or_default(),
135 );
136 output.push('\n');
137 for rejection in &scan.rejected {
138 let _ = writeln!(output, " - {rejection}");
139 }
140 }
141 output.push('\n');
142 output.push_str(
143 &presentation
144 .translate("plugin_kimi_inspection_footer", &[])
145 .unwrap_or_default(),
146 );
147 CommandResult::message(output)
148 }
149
150 fn approve(
151 presentation: &mut dyn CommandPresentationContext,
152 plugin: &mut dyn CommandPluginContext,
153 name: &str,
154 expected_hash: &str,
155 home_override: Option<&Path>,
156 ) -> CommandResult {
157 let scan = match plugin.managed_scan(home_override) {
158 Ok(scan) => scan,
159 Err(error) => return CommandResult::error(error),
160 };
161 let Some(candidate) = scan
162 .candidates
163 .into_iter()
164 .find(|candidate| candidate.name == name)
165 else {
166 let name = escape_review_text(name, ReviewTextContext::InlineCode);
167 return CommandResult::error(
168 presentation
169 .translate(
170 "plugin_kimi_candidate_missing",
171 &[("name", &name), ("list_command", "/plugin import kimi")],
172 )
173 .unwrap_or_default(),
174 );
175 };
176 if candidate.content_hash != expected_hash {
177 let name = escape_review_text(name, ReviewTextContext::InlineCode);
178 let expected = escape_review_text(expected_hash, ReviewTextContext::Prose);
179 return CommandResult::error(
180 presentation
181 .translate(
182 "plugin_kimi_candidate_changed",
183 &[
184 ("name", &name),
185 ("expected", &expected),
186 ("actual", &candidate.content_hash),
187 ("list_command", "/plugin import kimi"),
188 ],
189 )
190 .unwrap_or_default(),
191 );
192 }
193
194 // The facet revalidates and copies the source through the ordinary local
195 // installer; its result is always rediscovered disabled/untrusted and
196 // presents the post-copy authority review before any activation.
197 super::install_bundle_with_expected_hash(
198 presentation,
199 plugin,
200 &candidate.canonical_path,
201 expected_hash,
202 )
203 }
204
205 pub(super) fn escape_review_path(path: &Path, context: ReviewTextContext) -> String {
206 super::escape_review_path(path, context)
207 }
208
209 pub(super) fn escape_review_text(value: &str, context: ReviewTextContext) -> String {
210 super::escape_review_text(value, context)
211 }
212
212 lines RUST