|
| 1 | +import os |
| 2 | +import yaml |
| 3 | +import pathlib |
| 4 | +from typing import Dict, Any, Optional |
| 5 | + |
| 6 | +class AgentLoader: |
| 7 | + def __init__(self): |
| 8 | + self.agents: Dict[str, Dict[str, Any]] = {} |
| 9 | + self.load_all_agents() |
| 10 | + |
| 11 | + def load_all_agents(self): |
| 12 | + # We search in ~/.gemini/config/skills/ and ~/.gemini/antigravity/skills/ |
| 13 | + search_dirs = [ |
| 14 | + os.path.expanduser("~/.gemini/config/skills"), |
| 15 | + os.path.expanduser("~/.gemini/antigravity/skills") |
| 16 | + ] |
| 17 | + |
| 18 | + for base_dir in search_dirs: |
| 19 | + if not os.path.isdir(base_dir): |
| 20 | + continue |
| 21 | + |
| 22 | + p = pathlib.Path(base_dir) |
| 23 | + for skill_dir in p.iterdir(): |
| 24 | + if skill_dir.is_dir() and skill_dir.name.startswith("agency-"): |
| 25 | + skill_md_path = skill_dir / "SKILL.md" |
| 26 | + if skill_md_path.exists(): |
| 27 | + self.parse_skill_file(skill_dir.name, skill_md_path) |
| 28 | + |
| 29 | + def parse_skill_file(self, folder_name: str, file_path: pathlib.Path): |
| 30 | + try: |
| 31 | + with open(file_path, "r", encoding="utf-8") as f: |
| 32 | + content = f.read() |
| 33 | + |
| 34 | + # Parse YAML frontmatter |
| 35 | + # Example format: |
| 36 | + # --- |
| 37 | + # name: agency-software-architect |
| 38 | + # description: ... |
| 39 | + # --- |
| 40 | + # Body... |
| 41 | + parts = content.split("---") |
| 42 | + if len(parts) >= 3: |
| 43 | + frontmatter_str = parts[1] |
| 44 | + body_str = "---".join(parts[2:]) |
| 45 | + |
| 46 | + try: |
| 47 | + metadata = yaml.safe_load(frontmatter_str) |
| 48 | + except Exception: |
| 49 | + metadata = {} |
| 50 | + |
| 51 | + name = metadata.get("name", folder_name) |
| 52 | + description = metadata.get("description", "") |
| 53 | + |
| 54 | + self.agents[name] = { |
| 55 | + "name": name, |
| 56 | + "description": description, |
| 57 | + "system_prompt": body_str.strip(), |
| 58 | + "folder_name": folder_name, |
| 59 | + "path": str(file_path) |
| 60 | + } |
| 61 | + except Exception as e: |
| 62 | + print(f"Error parsing skill file {file_path}: {e}") |
| 63 | + |
| 64 | + def get_agent(self, name: str) -> Optional[Dict[str, Any]]: |
| 65 | + # Match by name or folder name (e.g. "agency-reality-checker") |
| 66 | + if name in self.agents: |
| 67 | + return self.agents[name] |
| 68 | + |
| 69 | + # Try match by suffix |
| 70 | + for k, v in self.agents.items(): |
| 71 | + if k.endswith(name) or v["folder_name"] == name: |
| 72 | + return v |
| 73 | + return None |
| 74 | + |
| 75 | + def list_available_agents(self) -> Dict[str, str]: |
| 76 | + return {name: info["description"] for name, info in self.agents.items()} |
0 commit comments