|
| 1 | +#**************************************************************************** |
| 2 | +#* package_handler_skills.py |
| 3 | +#* |
| 4 | +#* Copyright 2024 Matthew Ballance and Contributors |
| 5 | +#* |
| 6 | +#* Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | +#* not use this file except in compliance with the License. |
| 8 | +#* You may obtain a copy of the License at: |
| 9 | +#* |
| 10 | +#* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +#* |
| 12 | +#* Unless required by applicable law or agreed to in writing, software |
| 13 | +#* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +#* See the License for the specific language governing permissions and |
| 16 | +#* limitations under the License. |
| 17 | +#* |
| 18 | +#**************************************************************************** |
| 19 | +import dataclasses as dc |
| 20 | +import logging |
| 21 | +import os |
| 22 | +import re |
| 23 | +from typing import Dict, Optional, Tuple |
| 24 | +from ..package import Package |
| 25 | +from ..project_ops_info import ProjectUpdateInfo |
| 26 | +from .package_handler import PackageHandler |
| 27 | + |
| 28 | +_logger = logging.getLogger("ivpm.handlers.package_handler_skills") |
| 29 | + |
| 30 | +# Frontmatter is delimited by lines containing only '---' |
| 31 | +_FRONTMATTER_RE = re.compile(r"^---\s*\n(.*?)\n---\s*\n", re.DOTALL) |
| 32 | +_FIELD_RE = re.compile(r"^(\w[\w-]*):\s*(.+)$", re.MULTILINE) |
| 33 | + |
| 34 | + |
| 35 | +def _parse_frontmatter(path: str) -> Optional[Dict[str, str]]: |
| 36 | + """Return a dict of frontmatter fields, or None on failure.""" |
| 37 | + try: |
| 38 | + with open(path) as fh: |
| 39 | + content = fh.read() |
| 40 | + except OSError as exc: |
| 41 | + _logger.warning("Could not read %s: %s", path, exc) |
| 42 | + return None |
| 43 | + |
| 44 | + m = _FRONTMATTER_RE.match(content) |
| 45 | + if not m: |
| 46 | + return None |
| 47 | + |
| 48 | + fields: Dict[str, str] = {} |
| 49 | + for fm in _FIELD_RE.finditer(m.group(1)): |
| 50 | + fields[fm.group(1)] = fm.group(2).strip() |
| 51 | + return fields |
| 52 | + |
| 53 | + |
| 54 | +@dc.dataclass |
| 55 | +class PackageHandlerSkills(PackageHandler): |
| 56 | + name = "skills" |
| 57 | + # package name -> (Package, skill filename, skill name, description, extra fields) |
| 58 | + skill_pkgs: Dict[str, Tuple] = dc.field(default_factory=dict) |
| 59 | + |
| 60 | + def process_pkg(self, pkg: Package): |
| 61 | + """Record packages that provide a SKILLS.md or SKILL.md file.""" |
| 62 | + if not hasattr(pkg, "path") or pkg.path is None: |
| 63 | + return |
| 64 | + if getattr(pkg, "src_type", None) == "pypi": |
| 65 | + return |
| 66 | + |
| 67 | + for candidate in ("SKILLS.md", "SKILL.md"): |
| 68 | + skill_path = os.path.join(pkg.path, candidate) |
| 69 | + if os.path.isfile(skill_path): |
| 70 | + fields = _parse_frontmatter(skill_path) |
| 71 | + if fields is None: |
| 72 | + _logger.warning( |
| 73 | + "Package %s: %s has missing or malformed frontmatter; skipping", |
| 74 | + pkg.name, candidate, |
| 75 | + ) |
| 76 | + break |
| 77 | + skill_name = fields.get("name", "").strip() |
| 78 | + description = fields.get("description", "").strip() |
| 79 | + if not skill_name or not description: |
| 80 | + _logger.warning( |
| 81 | + "Package %s: %s frontmatter missing required 'name' or 'description'; skipping", |
| 82 | + pkg.name, candidate, |
| 83 | + ) |
| 84 | + break |
| 85 | + _logger.debug("Package %s has %s (skill: %s)", pkg.name, candidate, skill_name) |
| 86 | + self.skill_pkgs[pkg.name] = (pkg, candidate, skill_name, description, fields) |
| 87 | + break |
| 88 | + |
| 89 | + def update(self, update_info: ProjectUpdateInfo): |
| 90 | + if not self.skill_pkgs: |
| 91 | + _logger.debug("No packages with skill files; skipping SKILLS.md generation") |
| 92 | + return |
| 93 | + |
| 94 | + deps_dir = update_info.deps_dir |
| 95 | + output_path = os.path.join(deps_dir, "SKILLS.md") |
| 96 | + |
| 97 | + project_name = getattr(update_info, "project_name", None) or "project" |
| 98 | + |
| 99 | + _logger.debug("Writing SKILLS.md to %s", output_path) |
| 100 | + |
| 101 | + with open(output_path, "w") as fp: |
| 102 | + fp.write("---\n") |
| 103 | + fp.write("name: %s-skills\n" % project_name) |
| 104 | + fp.write("description: Aggregated agent skills from %s dependencies.\n" % project_name) |
| 105 | + fp.write("---\n\n") |
| 106 | + fp.write("<!-- Generated by IVPM — do not edit manually -->\n\n") |
| 107 | + fp.write("# Agent Skills\n\n") |
| 108 | + |
| 109 | + for pkg_name, (pkg, filename, skill_name, description, fields) in sorted(self.skill_pkgs.items()): |
| 110 | + fp.write("## %s\n\n" % skill_name) |
| 111 | + fp.write("%s\n\n" % description) |
| 112 | + |
| 113 | + # Propagate optional fields if present |
| 114 | + for field in ("license", "compatibility", "allowed-tools"): |
| 115 | + val = fields.get(field, "").strip() |
| 116 | + if val: |
| 117 | + fp.write("**%s**: %s\n\n" % (field, val)) |
| 118 | + |
| 119 | + fp.write("[Skill source](./%s/%s)\n\n" % (pkg_name, filename)) |
| 120 | + |
| 121 | + from ..utils import note |
| 122 | + note("Generated SKILLS.md with %d skill(s)" % len(self.skill_pkgs)) |
0 commit comments