| 1 | import json |
| 2 | from pathlib import Path |
| 3 | |
| 4 | _DIR = Path(__file__).parent.parent |
| 5 | _CFG = None |
| 6 | _TMPL = None |
| 7 | |
| 8 | |
| 9 | def _load_config() -> dict: |
| 10 | global _CFG |
| 11 | if _CFG is not None: |
| 12 | return _CFG |
| 13 | |
| 14 | path = _DIR / "webui" / "plugin-scan-checks.json" |
| 15 | try: |
| 16 | _CFG = json.loads(path.read_text()) |
| 17 | return _CFG |
| 18 | except Exception as e: |
| 19 | raise RuntimeError(f"Unable to load plugin scan checks: {e}") from e |
| 20 | |
| 21 | |
| 22 | def _load_template() -> str: |
| 23 | global _TMPL |
| 24 | if _TMPL is not None: |
| 25 | return _TMPL |
| 26 | |
| 27 | path = _DIR / "webui" / "plugin-scan-prompt.md" |
| 28 | try: |
| 29 | _TMPL = path.read_text() |
| 30 | return _TMPL |
| 31 | except Exception as e: |
| 32 | raise RuntimeError(f"Unable to load plugin scan prompt template: {e}") from e |
| 33 | |
| 34 | |
| 35 | def build_prompt(git_url: str, checks: list | None = None) -> str: |
| 36 | cfg = _load_config() |
| 37 | ratings, all_checks = cfg["ratings"], cfg["checks"] |
| 38 | keys = list(all_checks.keys()) if checks is None else [k for k in checks if k in all_checks] |
| 39 | prompt_template = _load_template() |
| 40 | |
| 41 | subs = { |
| 42 | "GIT_URL": git_url, |
| 43 | "SELECTED_CHECKS": ( |
| 44 | "\n".join(f"- **{all_checks[k]['label']}**" for k in keys) |
| 45 | if keys |
| 46 | else "- (no checks selected)" |
| 47 | ), |
| 48 | "CHECK_DETAILS": ( |
| 49 | "\n\n".join( |
| 50 | f"#### {c['label']}\n{c['detail']}\n\nCriteria:\n" |
| 51 | + "\n".join(f" - {ratings[l]['icon']} {d}" for l, d in c["criteria"].items()) |
| 52 | for c in (all_checks[k] for k in keys) |
| 53 | ) |
| 54 | if keys |
| 55 | else "(no checks selected)" |
| 56 | ), |
| 57 | "STATUS_LEGEND": "\n".join(f"- {r['icon']} **{r['label']}**" for r in ratings.values()), |
| 58 | "RATING_ICONS": "/".join(r["icon"] for r in ratings.values()), |
| 59 | "RATING_PASS": ratings["pass"]["icon"], |
| 60 | "RATING_WARNING": ratings["warning"]["icon"], |
| 61 | "RATING_FAIL": ratings["fail"]["icon"], |
| 62 | } |
| 63 | prompt = prompt_template |
| 64 | for key, val in subs.items(): |
| 65 | prompt = prompt.replace(f"{{{{{key}}}}}", val) |
| 66 | return prompt |