|
| 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 | +"""Data models for Agent Skills.""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from typing import Optional |
| 20 | + |
| 21 | +from pydantic import BaseModel |
| 22 | + |
| 23 | + |
| 24 | +class Frontmatter(BaseModel): |
| 25 | + """L1 skill content: metadata parsed from SKILL.md frontmatter for skill discovery. |
| 26 | +
|
| 27 | + Attributes: |
| 28 | + name: Skill name in kebab-case (required). |
| 29 | + description: What the skill does and when the model should use it |
| 30 | + (required). |
| 31 | + license: License for the skill (optional). |
| 32 | + compatibility: Compatibility information for the skill (optional). |
| 33 | + allowed_tools: Tool patterns the skill requires (optional, experimental). |
| 34 | + metadata: Key-value pairs for client-specific properties (defaults to |
| 35 | + empty dict). |
| 36 | + """ |
| 37 | + |
| 38 | + name: str |
| 39 | + description: str |
| 40 | + license: Optional[str] = None |
| 41 | + compatibility: Optional[str] = None |
| 42 | + allowed_tools: Optional[str] = None |
| 43 | + metadata: dict[str, str] = {} |
| 44 | + |
| 45 | + |
| 46 | +class Script(BaseModel): |
| 47 | + """Wrapper for script content.""" |
| 48 | + |
| 49 | + src: str |
| 50 | + |
| 51 | + def __str__(self) -> str: |
| 52 | + """Returns the string representation of the script content. |
| 53 | +
|
| 54 | + This ensures that any script type can be converted to a string, which is |
| 55 | + useful for including the script in prompts or saving it to the file system. |
| 56 | + """ |
| 57 | + return self.src |
| 58 | + |
| 59 | + |
| 60 | +class Resources(BaseModel): |
| 61 | + """L3 skill content: additional instructions, assets, and scripts, loaded as needed. |
| 62 | +
|
| 63 | + Attributes: |
| 64 | + references: Additional markdown files with instructions, workflows, or |
| 65 | + guidance. |
| 66 | + assets: Resource materials like database schemas, API documentation, |
| 67 | + templates, or examples. |
| 68 | + scripts: Executable scripts that can be run via bash. |
| 69 | + """ |
| 70 | + |
| 71 | + references: dict[str, str] = {} |
| 72 | + assets: dict[str, str] = {} |
| 73 | + scripts: dict[str, Script] = {} |
| 74 | + |
| 75 | + def get_reference(self, reference_id: str) -> Optional[str]: |
| 76 | + """Get content of a reference file. |
| 77 | +
|
| 78 | + Args: |
| 79 | + reference_id: Unique path or name of the reference file. |
| 80 | +
|
| 81 | + Returns: |
| 82 | + Reference content as string, or None if not found |
| 83 | + """ |
| 84 | + return self.references.get(reference_id) |
| 85 | + |
| 86 | + def get_asset(self, asset_id: str) -> Optional[str]: |
| 87 | + """Get content of an asset file. |
| 88 | +
|
| 89 | + Args: |
| 90 | + asset_id: Unique path or name of the asset file. |
| 91 | +
|
| 92 | + Returns: |
| 93 | + Asset content as string, or None if not found |
| 94 | + """ |
| 95 | + return self.assets.get(asset_id) |
| 96 | + |
| 97 | + def get_script(self, script_id: str) -> Optional[Script]: |
| 98 | + """Get content of a script file. |
| 99 | +
|
| 100 | + Args: |
| 101 | + script_id: Unique path or name of the script file. |
| 102 | +
|
| 103 | + Returns: |
| 104 | + Script object, or None if not found |
| 105 | + """ |
| 106 | + return self.scripts.get(script_id) |
| 107 | + |
| 108 | + def list_references(self) -> list[str]: |
| 109 | + """List all available reference paths.""" |
| 110 | + return list(self.references.keys()) |
| 111 | + |
| 112 | + def list_assets(self) -> list[str]: |
| 113 | + """List all available asset paths.""" |
| 114 | + return list(self.assets.keys()) |
| 115 | + |
| 116 | + def list_scripts(self) -> list[str]: |
| 117 | + """List all available script paths.""" |
| 118 | + return list(self.scripts.keys()) |
| 119 | + |
| 120 | + |
| 121 | +class Skill(BaseModel): |
| 122 | + """Complete skill representation including frontmatter, instructions, and resources. |
| 123 | +
|
| 124 | + A skill combines: |
| 125 | + - L1: Frontmatter for discovery (name, description). |
| 126 | + - L2: Instructions from SKILL.md body, loaded when skill is triggered. |
| 127 | + - L3: Resources including additional instructions, assets, and scripts, |
| 128 | + loaded as needed. |
| 129 | +
|
| 130 | + Attributes: |
| 131 | + frontmatter: Parsed skill frontmatter from SKILL.md. |
| 132 | + instructions: L2 skill content: markdown instruction from SKILL.md body. |
| 133 | + resources: L3 skill content: additional instructions, assets, and scripts. |
| 134 | + """ |
| 135 | + |
| 136 | + frontmatter: Frontmatter |
| 137 | + instructions: str |
| 138 | + resources: Resources = Resources() |
| 139 | + |
| 140 | + @property |
| 141 | + def name(self) -> str: |
| 142 | + """Convenience property to access skill name.""" |
| 143 | + return self.frontmatter.name |
| 144 | + |
| 145 | + @property |
| 146 | + def description(self) -> str: |
| 147 | + """Convenience property to access skill description.""" |
| 148 | + return self.frontmatter.description |
0 commit comments