portal/scripts/skills/validate_skill.py
2026-03-17 12:05:16 +08:00

79 lines
2.4 KiB
Python
Executable File

#!/usr/bin/env python3
"""Minimal ClawHub-style skill validation."""
from __future__ import annotations
import re
import sys
from pathlib import Path
import yaml
ALLOWED_PROPERTIES = {"name", "description", "license", "allowed-tools", "metadata"}
def validate_skill(skill_path: str | Path) -> tuple[bool, str]:
skill_dir = Path(skill_path)
skill_md = skill_dir / "SKILL.md"
if not skill_md.exists():
return False, "SKILL.md not found"
content = skill_md.read_text(encoding="utf-8")
if not content.startswith("---"):
return False, "No YAML frontmatter found"
match = re.match(r"^---\n(.*?)\n---", content, re.DOTALL)
if not match:
return False, "Invalid frontmatter format"
try:
frontmatter = yaml.safe_load(match.group(1))
except yaml.YAMLError as exc:
return False, f"Invalid YAML in frontmatter: {exc}"
if not isinstance(frontmatter, dict):
return False, "Frontmatter must be a YAML dictionary"
unexpected = set(frontmatter.keys()) - ALLOWED_PROPERTIES
if unexpected:
return (
False,
"Unexpected key(s) in SKILL.md frontmatter: "
+ ", ".join(sorted(unexpected))
+ ". Allowed properties are: "
+ ", ".join(sorted(ALLOWED_PROPERTIES)),
)
for key in ("name", "description"):
if key not in frontmatter:
return False, f"Missing '{key}' in frontmatter"
name = str(frontmatter["name"]).strip()
if not re.fullmatch(r"[a-z0-9-]+", name) or name.startswith("-") or name.endswith("-") or "--" in name:
return False, f"Name '{name}' should be hyphen-case (lowercase letters, digits, and hyphens only)"
if len(name) > 64:
return False, f"Name is too long ({len(name)} characters). Maximum is 64 characters."
description = str(frontmatter["description"]).strip()
if "<" in description or ">" in description:
return False, "Description cannot contain angle brackets (< or >)"
if len(description) > 1024:
return False, f"Description is too long ({len(description)} characters). Maximum is 1024 characters."
return True, "Skill is valid!"
def main() -> int:
if len(sys.argv) != 2:
print("Usage: validate_skill.py <skill-directory>")
return 1
valid, message = validate_skill(sys.argv[1])
print(message)
return 0 if valid else 1
if __name__ == "__main__":
raise SystemExit(main())