|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Utility functions for Agent Skills.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +import pathlib |
| 20 | +from typing import Union |
| 21 | + |
| 22 | +import yaml |
| 23 | + |
| 24 | +from . import models |
| 25 | + |
| 26 | + |
| 27 | +def _load_dir(directory: pathlib.Path) -> dict[str, str]: |
| 28 | + """Recursively load files from a directory into a dictionary. |
| 29 | +
|
| 30 | + Args: |
| 31 | + directory: Path to the directory to load. |
| 32 | +
|
| 33 | + Returns: |
| 34 | + Dictionary mapping relative file paths to their string content. |
| 35 | + """ |
| 36 | + files = {} |
| 37 | + if directory.exists() and directory.is_dir(): |
| 38 | + for file_path in directory.rglob("*"): |
| 39 | + if file_path.is_file(): |
| 40 | + relative_path = file_path.relative_to(directory) |
| 41 | + files[str(relative_path)] = file_path.read_text(encoding="utf-8") |
| 42 | + return files |
| 43 | + |
| 44 | + |
| 45 | +def load_skill_from_dir(skill_dir: Union[str, pathlib.Path]) -> models.Skill: |
| 46 | + """Load a complete skill from a directory. |
| 47 | +
|
| 48 | + Args: |
| 49 | + skill_dir: Path to the skill directory. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + Skill object with all components loaded. |
| 53 | +
|
| 54 | + Raises: |
| 55 | + FileNotFoundError: If the skill directory or SKILL.md is not found. |
| 56 | + ValueError: If SKILL.md is invalid. |
| 57 | + """ |
| 58 | + skill_dir = pathlib.Path(skill_dir).resolve() |
| 59 | + |
| 60 | + if not skill_dir.is_dir(): |
| 61 | + raise FileNotFoundError(f"Skill directory '{skill_dir}' not found.") |
| 62 | + |
| 63 | + skill_md = None |
| 64 | + for name in ("SKILL.md", "skill.md"): |
| 65 | + path = skill_dir / name |
| 66 | + if path.exists(): |
| 67 | + skill_md = path |
| 68 | + break |
| 69 | + |
| 70 | + if skill_md is None: |
| 71 | + raise FileNotFoundError(f"SKILL.md not found in '{skill_dir}'.") |
| 72 | + |
| 73 | + content = skill_md.read_text(encoding="utf-8") |
| 74 | + if not content.startswith("---"): |
| 75 | + raise ValueError("SKILL.md must start with YAML frontmatter (---)") |
| 76 | + |
| 77 | + parts = content.split("---", 2) |
| 78 | + if len(parts) < 3: |
| 79 | + raise ValueError("SKILL.md frontmatter not properly closed with ---") |
| 80 | + |
| 81 | + frontmatter_str = parts[1] |
| 82 | + body = parts[2].strip() |
| 83 | + |
| 84 | + try: |
| 85 | + parsed = yaml.safe_load(frontmatter_str) |
| 86 | + except yaml.YAMLError as e: |
| 87 | + raise ValueError(f"Invalid YAML in frontmatter: {e}") from e |
| 88 | + |
| 89 | + if not isinstance(parsed, dict): |
| 90 | + raise ValueError("SKILL.md frontmatter must be a YAML mapping") |
| 91 | + |
| 92 | + # Frontmatter class handles required field validation |
| 93 | + frontmatter = models.Frontmatter(**parsed) |
| 94 | + |
| 95 | + references = _load_dir(skill_dir / "references") |
| 96 | + assets = _load_dir(skill_dir / "assets") |
| 97 | + raw_scripts = _load_dir(skill_dir / "scripts") |
| 98 | + scripts = { |
| 99 | + name: models.Script(src=content) for name, content in raw_scripts.items() |
| 100 | + } |
| 101 | + |
| 102 | + resources = models.Resources( |
| 103 | + references=references, |
| 104 | + assets=assets, |
| 105 | + scripts=scripts, |
| 106 | + ) |
| 107 | + |
| 108 | + return models.Skill( |
| 109 | + frontmatter=frontmatter, |
| 110 | + instructions=body, |
| 111 | + resources=resources, |
| 112 | + ) |
0 commit comments