返回 CodeWhale
codewhale.rs
根目录 / crates / plugins / src / marketplace / parsers / codewhale.rs
1 //! Codewhale native catalog parser.
2 //!
3 //! This is Codewhale's own documented catalog format. It is deliberately
4 //! the smallest of the supported formats: entries carry a `name` and a
5 //! `source` that is exactly the install spec `/plugin install` accepts
6 //! (`github:owner/repo`, `path:dir`, an http(s) tarball URL). A tarball
7 //! entry may add `sha256`; the install refuses archive bytes that differ.
8 //!
9 //! ```json
10 //! {
11 //! "name": "my-catalog",
12 //! "description": "Team plugins",
13 //! "version": "1",
14 //! "plugins": [
15 //! { "name": "formatter", "source": "github:owner/repo", "version": "2.1.0" }
16 //! ]
17 //! }
18 //! ```
19
20 use serde_json::Value;
21
22 use crate::install::PluginInstallSource;
23
24 use super::super::types::{
25 CatalogProvenance, CatalogTier, MarketplaceCandidate, MarketplaceCandidateId,
26 MarketplaceCatalog, MarketplaceDiagnostic, MarketplaceEntryKind, MarketplaceFormat,
27 MarketplaceInstallPlan, MarketplaceSourceSpec,
28 };
29 use super::{MarketplaceDocument, str_array_field, str_field, unknown_fields_warning};
30
31 const TOP_LEVEL_FIELDS: &[&str] = &["name", "description", "version", "plugins"];
32 const ENTRY_FIELDS: &[&str] = &[
33 "name",
34 "source",
35 "kind",
36 "description",
37 "version",
38 "homepage",
39 "keywords",
40 "categories",
41 "display_name",
42 "author",
43 "icon",
44 "platforms",
45 "sha256",
46 ];
47
48 pub fn parse_codewhale_catalog(document: MarketplaceDocument) -> MarketplaceCatalog {
49 let MarketplaceDocument {
50 catalog_id,
51 root,
52 base,
53 ..
54 } = document;
55 let mut diagnostics = Vec::new();
56
57 let Some(obj) = root.as_object() else {
58 return MarketplaceCatalog {
59 id: catalog_id,
60 format: MarketplaceFormat::Codewhale,
61 name: String::new(),
62 display_name: None,
63 description: None,
64 version: None,
65 base,
66 provenance: CatalogProvenance::default(),
67 candidates: Vec::new(),
68 diagnostics: vec![MarketplaceDiagnostic::error(
69 "NOT_AN_OBJECT",
70 "Codewhale catalog must be a JSON object",
71 None,
72 None,
73 )],
74 };
75 };
76
77 if let Some(diag) = unknown_fields_warning(obj, TOP_LEVEL_FIELDS) {
78 diagnostics.push(diag);
79 }
80
81 let (name, bad_name) = str_field(obj, "name");
82 if let Some(diag) = bad_name {
83 diagnostics.push(diag);
84 }
85 let name = name
86 .map(ToString::to_string)
87 .unwrap_or_else(|| catalog_id.as_str().to_string());
88 let (description, bad_desc) = str_field(obj, "description");
89 if let Some(diag) = bad_desc {
90 diagnostics.push(diag);
91 }
92 let (version, bad_version) = str_field(obj, "version");
93 if let Some(diag) = bad_version {
94 diagnostics.push(diag);
95 }
96
97 let Some(entries) = obj.get("plugins").and_then(Value::as_array) else {
98 diagnostics.push(MarketplaceDiagnostic::error(
99 "MISSING_PLUGINS",
100 "Codewhale catalog must contain a `plugins` array",
101 None,
102 None,
103 ));
104 return MarketplaceCatalog {
105 id: catalog_id,
106 format: MarketplaceFormat::Codewhale,
107 name,
108 display_name: None,
109 description: description.map(ToString::to_string),
110 version: version.map(ToString::to_string),
111 base,
112 provenance: CatalogProvenance::default(),
113 candidates: Vec::new(),
114 diagnostics,
115 };
116 };
117
118 let mut candidates = Vec::new();
119 for (index, entry) in entries.iter().enumerate() {
120 if let Some(candidate) = parse_codewhale_entry(&catalog_id, index, entry, &mut diagnostics)
121 {
122 candidates.push(candidate);
123 }
124 }
125
126 MarketplaceCatalog {
127 id: catalog_id,
128 format: MarketplaceFormat::Codewhale,
129 name,
130 display_name: None,
131 description: description.map(ToString::to_string),
132 version: version.map(ToString::to_string),
133 base,
134 provenance: CatalogProvenance {
135 tier: CatalogTier::Community,
136 publisher: None,
137 source_url: None,
138 },
139 candidates,
140 diagnostics,
141 }
142 }
143
144 fn parse_codewhale_entry(
145 catalog_id: &super::super::types::MarketplaceCatalogId,
146 index: usize,
147 entry: &Value,
148 diagnostics: &mut Vec<MarketplaceDiagnostic>,
149 ) -> Option<MarketplaceCandidate> {
150 let Some(obj) = entry.as_object() else {
151 diagnostics.push(MarketplaceDiagnostic::error(
152 "MALFORMED_ENTRY",
153 format!("Codewhale plugin at index {index} must be a JSON object"),
154 None,
155 Some(index),
156 ));
157 return None;
158 };
159
160 let mut entry_diags = Vec::new();
161 if let Some(diag) = unknown_fields_warning(obj, ENTRY_FIELDS) {
162 entry_diags.push(diag);
163 }
164
165 let (name, bad_name) = str_field(obj, "name");
166 if let Some(diag) = bad_name {
167 entry_diags.push(diag);
168 }
169 let Some(name) = name else {
170 diagnostics.push(MarketplaceDiagnostic::error(
171 "MISSING_NAME",
172 format!("Codewhale plugin at index {index} is missing required `name`"),
173 None,
174 Some(index),
175 ));
176 return None;
177 };
178 // The name is the candidate's identity (registry lookup, `/plugin show`,
179 // install), so it is held to the plugin-name rules the other formats
180 // normalize to, rather than stored verbatim with controls or unbounded.
181 if crate::manifest::validate_plugin_name(name).is_err()
182 && !crate::agent_plugin::is_standard_plugin_name(name)
183 {
184 diagnostics.push(MarketplaceDiagnostic::error(
185 "INVALID_NAME",
186 format!(
187 "Codewhale plugin at index {index} has an invalid name {:?}: use lowercase letters, digits, and internal hyphens or dots",
188 name.chars().take(80).collect::<String>()
189 ),
190 None,
191 Some(index),
192 ));
193 return None;
194 }
195 let name = name.to_string();
196
197 let (source, bad_source) = str_field(obj, "source");
198 if let Some(diag) = bad_source {
199 entry_diags.push(diag);
200 }
201 let Some(source) = source else {
202 diagnostics.push(MarketplaceDiagnostic::error(
203 "MISSING_SOURCE",
204 format!("Codewhale plugin `{name}` is missing required `source`"),
205 Some(name.clone()),
206 Some(index),
207 ));
208 return None;
209 };
210
211 let mut install_plan = match PluginInstallSource::parse(source) {
212 Ok(parsed) => {
213 let source_kind = match &parsed {
214 PluginInstallSource::Remote(
215 codewhale_skill_install::install::InstallSource::GitHubRepo(_),
216 ) => "GitHub repository".to_string(),
217 PluginInstallSource::Remote(
218 codewhale_skill_install::install::InstallSource::DirectUrl(_),
219 ) => "Tarball URL".to_string(),
220 PluginInstallSource::LocalPath { .. } => "Local directory".to_string(),
221 PluginInstallSource::Dsh(_) => "DeepSeek Harness bundle package".to_string(),
222 PluginInstallSource::Remote(
223 codewhale_skill_install::install::InstallSource::Registry(_),
224 ) => "Registry".to_string(),
225 };
226 MarketplaceInstallPlan::Supported {
227 spec: source.to_string(),
228 source_kind,
229 }
230 }
231 Err(err) => MarketplaceInstallPlan::Unsupported {
232 reason: format!("invalid Codewhale install spec: {err}"),
233 raw: source.to_string(),
234 },
235 };
236 let mut normalized = normalize_native_source(source);
237 if let Some(pin) = obj.get("sha256").filter(|value| !value.is_null()) {
238 let pinned = pin
239 .as_str()
240 .ok_or_else(|| "sha256 must be a string".to_string())
241 .and_then(|pin| PluginInstallSource::pin_archive_spec(source, pin));
242 match pinned {
243 Ok((pinned_spec, digest)) => {
244 if let MarketplaceInstallPlan::Supported { spec, .. } = &mut install_plan {
245 *spec = pinned_spec;
246 }
247 if let MarketplaceSourceSpec::ArchiveUrl { sha256, .. } = &mut normalized {
248 *sha256 = Some(digest);
249 }
250 }
251 Err(reason) => entry_diags.push(MarketplaceDiagnostic::error(
252 "INVALID_SHA256",
253 reason,
254 Some(name.clone()),
255 Some(index),
256 )),
257 }
258 }
259
260 let (description, bad_desc) = str_field(obj, "description");
261 if let Some(diag) = bad_desc {
262 entry_diags.push(diag);
263 }
264 let (version, bad_version) = str_field(obj, "version");
265 if let Some(diag) = bad_version {
266 entry_diags.push(diag);
267 }
268 let (homepage, bad_home) = str_field(obj, "homepage");
269 if let Some(diag) = bad_home {
270 entry_diags.push(diag);
271 }
272
273 let mut labels = Vec::new();
274 for field in ["display_name", "author", "icon"] {
275 let (value, bad) = str_field(obj, field);
276 if let Some(diag) = bad {
277 entry_diags.push(diag);
278 }
279 let bounded = value.filter(|text| {
280 field == "icon" || (text.chars().count() <= 128 && !text.chars().any(char::is_control))
281 });
282 if value.is_some() && bounded.is_none() {
283 entry_diags.push(MarketplaceDiagnostic::error(
284 "INVALID_IDENTITY",
285 format!("{field} must fit on one line within 128 characters"),
286 Some(name.clone()),
287 Some(index),
288 ));
289 }
290 labels.push(bounded.map(ToString::to_string));
291 }
292 let mut icon = labels.pop().flatten();
293 if let Some(value) = &icon
294 && let Err(reason) = crate::manifest::validate_icon(value)
295 {
296 entry_diags.push(MarketplaceDiagnostic::error(
297 "INVALID_ICON",
298 reason,
299 Some(name.clone()),
300 Some(index),
301 ));
302 icon = None;
303 }
304 let author = labels.pop().flatten();
305 let display_name = labels.pop().flatten();
306 let platforms = obj
307 .get("platforms")
308 .and_then(Value::as_array)
309 .filter(|values| {
310 values.len() <= 3
311 && values
312 .iter()
313 .enumerate()
314 .all(|(i, value)| !values[..i].contains(value))
315 })
316 .and_then(|values| {
317 values
318 .iter()
319 .map(|value| {
320 value
321 .as_str()
322 .filter(|os| ["macos", "linux", "windows"].contains(os))
323 .map(ToString::to_string)
324 })
325 .collect::<Option<Vec<_>>>()
326 });
327 if obj.contains_key("platforms") && platforms.is_none() {
328 entry_diags.push(MarketplaceDiagnostic::error(
329 "INVALID_PLATFORMS",
330 "platforms must be an array of macos, linux or windows",
331 Some(name.clone()),
332 Some(index),
333 ));
334 }
335 // What this entry is. The Codewhale marketplace keeps plugins and
336 // skills in separate top-level directories, so the source path is the
337 // signal; a document may also declare `kind` explicitly. A skill entry
338 // stays installable, but it is not a plugin and is never suggested as
339 // one (#6290 rework).
340 let kind = match obj.get("kind").and_then(Value::as_str) {
341 Some("skill") => MarketplaceEntryKind::Skill,
342 Some("plugin") | None => {
343 let path = source
344 .strip_prefix("path:")
345 .unwrap_or(source)
346 .trim_start_matches("./");
347 if path == "skills" || path.starts_with("skills/") {
348 MarketplaceEntryKind::Skill
349 } else {
350 MarketplaceEntryKind::Plugin
351 }
352 }
353 Some(other) => {
354 entry_diags.push(MarketplaceDiagnostic::warning(
355 "UNKNOWN_ENTRY_KIND",
356 format!("unknown entry kind `{other}`; treated as a plugin"),
357 Some(name.clone()),
358 Some(index),
359 ));
360 MarketplaceEntryKind::Plugin
361 }
362 };
363 let (keywords, bad_keywords) = str_array_field(obj, "keywords");
364 let (categories, bad_categories) = str_array_field(obj, "categories");
365 entry_diags.extend(bad_keywords);
366 entry_diags.extend(bad_categories);
367 Some(MarketplaceCandidate {
368 id: MarketplaceCandidateId::new(catalog_id, &name),
369 catalog_id: catalog_id.clone(),
370 kind,
371 icon,
372 name,
373 display_name,
374 description: description.map(ToString::to_string),
375 version: version.map(ToString::to_string),
376 author,
377 homepage: homepage.map(ToString::to_string),
378 repository: None,
379 license: None,
380 keywords,
381 categories,
382 source: normalized,
383 install_plan,
384 declared_components: None,
385 compatibility: None,
386 provenance: CatalogProvenance {
387 tier: CatalogTier::Community,
388 publisher: None,
389 source_url: None,
390 },
391 when: platforms.map(|os| crate::manifest::PluginWhen {
392 os: Some(os),
393 binaries: None,
394 }),
395 diagnostics: entry_diags,
396 })
397 }
398
399 fn normalize_native_source(spec: &str) -> MarketplaceSourceSpec {
400 match PluginInstallSource::parse(spec) {
401 Ok(PluginInstallSource::LocalPath(path) | PluginInstallSource::Dsh(path)) => {
402 MarketplaceSourceSpec::LocalPath { path }
403 }
404 Ok(PluginInstallSource::Remote(
405 codewhale_skill_install::install::InstallSource::GitHubRepo(repo),
406 )) => {
407 let (owner, name) = repo.split_once('/').unwrap_or((&repo, ""));
408 MarketplaceSourceSpec::GitHub {
409 owner: owner.to_string(),
410 repo: name.to_string(),
411 git_ref: None,
412 sha: None,
413 }
414 }
415 Ok(PluginInstallSource::Remote(
416 codewhale_skill_install::install::InstallSource::DirectUrl(url),
417 )) => MarketplaceSourceSpec::ArchiveUrl { url, sha256: None },
418 Ok(other) => MarketplaceSourceSpec::Invalid {
419 reason: format!("registry source {other:?} is not a marketplace install"),
420 },
421 Err(err) => MarketplaceSourceSpec::Invalid {
422 reason: err.to_string(),
423 },
424 }
425 }
426
426 lines RUST