|
| 1 | +# SPDX-FileCopyrightText: 2022-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from typing import Any |
| 7 | + |
| 8 | +import yaml |
| 9 | + |
| 10 | +from haystack.core.serialization import default_from_dict, default_to_dict |
| 11 | +from haystack.dataclasses.skill_meta import SkillMeta |
| 12 | +from haystack.skill_stores.types.protocol import SKILL_FILE_NAME |
| 13 | + |
| 14 | + |
| 15 | +def _parse_frontmatter(text: str) -> tuple[dict[str, Any], str]: |
| 16 | + """ |
| 17 | + Split a `SKILL.md` file into its YAML frontmatter and markdown body. |
| 18 | +
|
| 19 | + The frontmatter is the YAML block delimited by leading and trailing `---` lines. If no frontmatter is |
| 20 | + present, an empty mapping and the original text are returned. |
| 21 | +
|
| 22 | + :param text: The full contents of a `SKILL.md` file. |
| 23 | + :returns: A tuple of (frontmatter mapping, body). |
| 24 | + :raises ValueError: If the frontmatter is present but is not a valid YAML mapping. |
| 25 | + """ |
| 26 | + stripped = text.lstrip() |
| 27 | + if not stripped.startswith("---"): |
| 28 | + return {}, text |
| 29 | + |
| 30 | + # Drop the leading '---' line, then split on the closing '---'. |
| 31 | + after_open = stripped[len("---") :].lstrip("\n") |
| 32 | + parts = after_open.split("\n---", 1) |
| 33 | + if len(parts) != 2: |
| 34 | + return {}, text |
| 35 | + |
| 36 | + frontmatter_block, body = parts |
| 37 | + loaded = yaml.safe_load(frontmatter_block) or {} |
| 38 | + if not isinstance(loaded, dict): |
| 39 | + raise ValueError("Skill frontmatter must be a YAML mapping.") # noqa: TRY004 |
| 40 | + return loaded, body.lstrip("\n") |
| 41 | + |
| 42 | + |
| 43 | +class FileSystemSkillStore: |
| 44 | + """ |
| 45 | + SkillStore backed by a directory of skill sub-directories on the local filesystem. |
| 46 | +
|
| 47 | + Expected layout: |
| 48 | +
|
| 49 | + ``` |
| 50 | + skills/ |
| 51 | + pdf-forms/ |
| 52 | + SKILL.md # frontmatter (name, description) + markdown instructions |
| 53 | + reference/forms.md # optional bundled file |
| 54 | + ``` |
| 55 | +
|
| 56 | + Only the frontmatter of each `SKILL.md` is read at construction time (cheap); bodies and bundled |
| 57 | + files are read lazily when the agent calls the corresponding tool. |
| 58 | +
|
| 59 | + :param skills_dir: Root directory that contains one sub-directory per skill. |
| 60 | + :raises ValueError: If `skills_dir` does not exist, is not a directory, a skill is missing a required |
| 61 | + frontmatter field, or two skills share the same name. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, skills_dir: str | Path) -> None: |
| 65 | + self.skills_dir = Path(skills_dir) |
| 66 | + self._skills = self._scan() |
| 67 | + |
| 68 | + def _scan(self) -> dict[str, SkillMeta]: |
| 69 | + if not self.skills_dir.is_dir(): |
| 70 | + raise ValueError(f"Skills directory '{self.skills_dir}' does not exist or is not a directory.") |
| 71 | + |
| 72 | + skills: dict[str, SkillMeta] = {} |
| 73 | + for skill_file in sorted(self.skills_dir.glob(f"*/{SKILL_FILE_NAME}")): |
| 74 | + skill_dir = skill_file.parent |
| 75 | + frontmatter, _ = _parse_frontmatter(skill_file.read_text(encoding="utf-8")) |
| 76 | + |
| 77 | + name = frontmatter.get("name", skill_dir.name) |
| 78 | + description = frontmatter.get("description") |
| 79 | + if not description: |
| 80 | + raise ValueError(f"Skill '{name}' ({skill_file}) is missing a 'description' in its frontmatter.") |
| 81 | + if name in skills: |
| 82 | + raise ValueError(f"Duplicate skill name '{name}' found in '{self.skills_dir}'.") |
| 83 | + |
| 84 | + skills[name] = SkillMeta(name=name, description=description, path=skill_dir) |
| 85 | + return skills |
| 86 | + |
| 87 | + def list_skills(self) -> dict[str, SkillMeta]: |
| 88 | + """Lists all skills available on disk""" |
| 89 | + return self._skills |
| 90 | + |
| 91 | + def load_skill_body(self, name: str) -> str: |
| 92 | + """Loads the skill body from disk""" |
| 93 | + meta = self._skills.get(name) |
| 94 | + if meta is None: |
| 95 | + raise KeyError(name) |
| 96 | + if meta.path is None: |
| 97 | + raise ValueError(f"Skill '{name}' is missing its directory path in metadata.") |
| 98 | + _, body = _parse_frontmatter((meta.path / SKILL_FILE_NAME).read_text(encoding="utf-8")) |
| 99 | + return body |
| 100 | + |
| 101 | + def list_skill_files(self, name: str) -> list[str]: |
| 102 | + """List all files in a skill directory, excluding the SKILL.md file.""" |
| 103 | + meta = self._skills.get(name) |
| 104 | + if meta is None: |
| 105 | + raise KeyError(name) |
| 106 | + if meta.path is None: |
| 107 | + raise ValueError(f"Skill '{name}' is missing its directory path in metadata.") |
| 108 | + return sorted( |
| 109 | + p.relative_to(meta.path).as_posix() |
| 110 | + for p in meta.path.rglob("*") |
| 111 | + if p.is_file() and p.name != SKILL_FILE_NAME |
| 112 | + ) |
| 113 | + |
| 114 | + def read_skill_file(self, name: str, path: str) -> str: |
| 115 | + """read_skill_file implementation that prevents path traversal outside the skill directory.""" |
| 116 | + meta = self._skills.get(name) |
| 117 | + if meta is None: |
| 118 | + raise KeyError(name) |
| 119 | + if meta.path is None: |
| 120 | + raise ValueError(f"Skill '{name}' is missing its directory path in metadata.") |
| 121 | + skill_dir = meta.path.resolve() |
| 122 | + target = (skill_dir / path).resolve() |
| 123 | + if skill_dir != target and skill_dir not in target.parents: |
| 124 | + raise PermissionError(f"path escapes the '{name}' skill directory") |
| 125 | + if not target.is_file(): |
| 126 | + raise FileNotFoundError(f"File '{path}' not found in skill '{name}'") |
| 127 | + return target.read_text(encoding="utf-8") |
| 128 | + |
| 129 | + def to_dict(self) -> dict[str, Any]: |
| 130 | + """Serialize this store to a dictionary for use with :meth:`from_dict`.""" |
| 131 | + return default_to_dict(self, skills_dir=str(self.skills_dir)) |
| 132 | + |
| 133 | + @classmethod |
| 134 | + def from_dict(cls, data: dict[str, Any]) -> "FileSystemSkillStore": |
| 135 | + """Deserialize a FileSystemSkillStore from its dictionary representation.""" |
| 136 | + return default_from_dict(cls, data) |
0 commit comments