File size: 1,182 Bytes
7fd3f6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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