Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

refactor(plugins): split contribution validation

vastsa26b4223559f2
diff --git a/crates/host-core/src/plugins/validation.rs b/crates/host-core/src/plugins/validation.rs
index c5e78ef93..d01b01c88 100644
--- a/crates/host-core/src/plugins/validation.rs
+++ b/crates/host-core/src/plugins/validation.rs
@@ -1,6 +1,10 @@
use super::*;
+mod composer_transforms;
+mod contribution_ids;
mod provider_catalog;
+use contribution_ids::is_contrib_id;
+
pub(crate) fn validate_contributions(root: &Path, manifest: &PluginManifest) -> Result<()> {
let Some(contributes) = manifest.contributes.as_ref() else {
return Ok(());
@@ -171,40 +175,7 @@ pub(crate) fn validate_contributions(root: &Path, manifest: &PluginManifest) ->
}
}
}
- if let Some(transforms) = map.get("composerTransforms") {
- let entries = array_of(transforms, "contributes.composerTransforms")?;
- if !entries.is_empty() {
- require_permission(manifest, "composer.transform", "composer transforms")?;
- }
- let mut seen: Vec<&str> = Vec::new();
- for entry in entries {
- let obj = entry.as_object().ok_or_else(|| {
- anyhow!("PLUGIN_INVALID: contributes.composerTransforms entry must be an object")
- })?;
- let id = obj
- .get("id")
- .and_then(Value::as_str)
- .filter(|id| is_contrib_id(id))
- .ok_or_else(|| {
- anyhow!("PLUGIN_INVALID: composer transform id is missing or invalid")
- })?;
- if seen.contains(&id) {
- bail!("PLUGIN_INVALID: duplicate composer transform id {id}");
- }
- seen.push(id);
- for field in ["title", "undoTitle"] {
- let Some(value) = obj.get(field) else {
- if field == "title" {
- bail!("PLUGIN_INVALID: composer transform {id} requires a title");
- }
- continue;
- };
- if !is_plugin_label(value) {
- bail!("PLUGIN_INVALID: composer transform {id} has an invalid {field}");
- }
- }
- }
- }
+ composer_transforms::validate(map, manifest)?;
if let Some(skills) = map.get("skills") {
let entries = array_of(skills, "contributes.skills")?;
for entry in entries {
@@ -733,7 +704,7 @@ fn is_shortcut_shape(value: &Value) -> bool {
.all(|part| matches!(*part, "Mod" | "Ctrl" | "Alt" | "Shift"))
}
-fn array_of<'a>(value: &'a Value, field: &str) -> Result<&'a [Value]> {
+pub(super) fn array_of<'a>(value: &'a Value, field: &str) -> Result<&'a [Value]> {
value
.as_array()
.map(|a| a.as_slice())
@@ -863,32 +834,6 @@ fn is_window_background_color(value: &str) -> bool {
.all(|character| character.is_ascii_hexdigit())
}
-fn is_contrib_id(value: &str) -> bool {
- if value.is_empty() || value.len() > 64 {
- return false;
- }
- let mut chars = value.chars();
- match chars.next() {
- Some(c) if c.is_ascii_alphabetic() => {}
- _ => return false,
- }
- chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
-}
-
-/// Host-owned Composer labels accept either a plain string or both contract locales.
-fn is_plugin_label(value: &Value) -> bool {
- if value.as_str().is_some_and(|label| !label.trim().is_empty()) {
- return true;
- }
- value.as_object().is_some_and(|localized| {
- ["en", "zh-CN"].iter().all(|locale| {
- localized
- .get(*locale)
- .and_then(Value::as_str)
- .is_some_and(|label| !label.trim().is_empty())
- })
- })
-}
/// Grammar shared with `contributes.globalShortcuts[].id` in the plugin SDK.
/// Dots are allowed here (unlike `is_contrib_id`): a shortcut id names a
/// namespace inside the plugin, e.g. `voice.pushToTalk`.
diff --git a/crates/host-core/src/plugins/validation/composer_transforms.rs b/crates/host-core/src/plugins/validation/composer_transforms.rs
new file mode 100644
index 000000000..dcfa10e43
--- /dev/null
+++ b/crates/host-core/src/plugins/validation/composer_transforms.rs
@@ -0,0 +1,59 @@
+use anyhow::{anyhow, bail, Result};
+use serde_json::{Map, Value};
+
+use super::{array_of, is_contrib_id, require_permission, PluginManifest};
+
+pub(super) fn validate(map: &Map<String, Value>, manifest: &PluginManifest) -> Result<()> {
+ let Some(transforms) = map.get("composerTransforms") else {
+ return Ok(());
+ };
+ let entries = array_of(transforms, "contributes.composerTransforms")?;
+ if !entries.is_empty() {
+ require_permission(manifest, "composer.transform", "composer transforms")?;
+ }
+
+ let mut seen: Vec<&str> = Vec::new();
+ for entry in entries {
+ let obj = entry.as_object().ok_or_else(|| {
+ anyhow!("PLUGIN_INVALID: contributes.composerTransforms entry must be an object")
+ })?;
+ let id = obj
+ .get("id")
+ .and_then(Value::as_str)
+ .filter(|id| is_contrib_id(id))
+ .ok_or_else(|| {
+ anyhow!("PLUGIN_INVALID: composer transform id is missing or invalid")
+ })?;
+ if seen.contains(&id) {
+ bail!("PLUGIN_INVALID: duplicate composer transform id {id}");
+ }
+ seen.push(id);
+ for field in ["title", "undoTitle"] {
+ let Some(value) = obj.get(field) else {
+ if field == "title" {
+ bail!("PLUGIN_INVALID: composer transform {id} requires a title");
+ }
+ continue;
+ };
+ if !is_plugin_label(value) {
+ bail!("PLUGIN_INVALID: composer transform {id} has an invalid {field}");
+ }
+ }
+ }
+ Ok(())
+}
+
+/// Composer labels accept either a plain string or both contract locales.
+fn is_plugin_label(value: &Value) -> bool {
+ if value.as_str().is_some_and(|label| !label.trim().is_empty()) {
+ return true;
+ }
+ value.as_object().is_some_and(|localized| {
+ ["en", "zh-CN"].iter().all(|locale| {
+ localized
+ .get(*locale)
+ .and_then(Value::as_str)
+ .is_some_and(|label| !label.trim().is_empty())
+ })
+ })
+}
diff --git a/crates/host-core/src/plugins/validation/contribution_ids.rs b/crates/host-core/src/plugins/validation/contribution_ids.rs
new file mode 100644
index 000000000..7f1d6ed08
--- /dev/null
+++ b/crates/host-core/src/plugins/validation/contribution_ids.rs
@@ -0,0 +1,9 @@
+/// Validate the shared grammar for manifest contribution identifiers.
+pub(super) fn is_contrib_id(value: &str) -> bool {
+ if value.is_empty() || value.len() > 64 {
+ return false;
+ }
+ let mut chars = value.chars();
+ matches!(chars.next(), Some(c) if c.is_ascii_alphabetic())
+ && chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
+}