from __future__ import annotations import json from pathlib import Path from typing import Any, Dict, List TEMPLATE_DIR = Path(__file__).resolve().parent / "trainer_templates" def list_trainer_templates() -> List[Dict[str, Any]]: TEMPLATE_DIR.mkdir(parents=True, exist_ok=True) out: List[Dict[str, Any]] = [] for p in sorted(TEMPLATE_DIR.glob("*.json")): try: cfg = json.loads(p.read_text(encoding="utf-8")) except Exception: continue template_id = cfg.get("template_id") or cfg.get("form_id") or p.stem name = cfg.get("name") or cfg.get("form_id") or template_id out.append({ "template_id": template_id, "name": name, # optional: trainer config itself (don’t spam prompt if huge) "has_config": True, }) return out def save_trainer_template(template_id: str, cfg: Dict[str, Any]) -> Path: TEMPLATE_DIR.mkdir(parents=True, exist_ok=True) cfg = dict(cfg) cfg["template_id"] = template_id # enforce path = TEMPLATE_DIR / f"{template_id}.json" path.write_text(json.dumps(cfg, indent=2), encoding="utf-8") return path