Spaces:
Sleeping
Sleeping
Download bot/dashboard_http_helpers.py from JuanCS-Dev/twitch-byte-bot: direct link, hf CLI and curl.
- Browser
- Download file 1.88 kB
-
https://huggingface.co/spaces/JuanCS-Dev/twitch-byte-bot/resolve/main/bot/dashboard_http_helpers.py
- Command line
-
hf download hf://spaces/JuanCS-Dev/twitch-byte-bot/bot/dashboard_http_helpers.py
-
curl -L -o dashboard_http_helpers.py https://huggingface.co/spaces/JuanCS-Dev/twitch-byte-bot/resolve/main/bot/dashboard_http_helpers.py
1.88 kB
| from typing import Any | |
| from urllib.parse import parse_qs, urlparse | |
| from bot.control_plane import control_plane | |
| from bot.runtime_config import TWITCH_CHAT_MODE | |
| def parse_dashboard_request_path(path: str | None) -> tuple[str, dict[str, list[str]]]: | |
| parsed_path = urlparse(path or "/") | |
| return parsed_path.path or "/", parse_qs(parsed_path.query or "") | |
| def send_invalid_request(handler: Any, message: str) -> None: | |
| handler._send_json( | |
| {"ok": False, "error": "invalid_request", "message": str(message)}, | |
| status_code=400, | |
| ) | |
| def require_dashboard_auth(handler: Any) -> bool: | |
| if handler._dashboard_authorized(): | |
| return True | |
| handler._send_forbidden() | |
| return False | |
| def read_json_payload_or_error( | |
| handler: Any, | |
| *, | |
| allow_empty: bool = False, | |
| ) -> dict[str, Any] | None: | |
| try: | |
| return handler._read_json_payload(allow_empty=allow_empty) | |
| except ValueError as error: | |
| send_invalid_request(handler, str(error)) | |
| return None | |
| def require_auth_and_read_payload( | |
| handler: Any, | |
| *, | |
| allow_empty: bool = False, | |
| ) -> dict[str, Any] | None: | |
| if not require_dashboard_auth(handler): | |
| return None | |
| return read_json_payload_or_error(handler, allow_empty=allow_empty) | |
| def build_control_plane_state_payload() -> dict[str, Any]: | |
| config = control_plane.get_config() | |
| runtime = control_plane.runtime_snapshot() | |
| # Flatten structure for TUI compatibility | |
| return { | |
| "ok": True, | |
| "mode": TWITCH_CHAT_MODE, | |
| "config": config, | |
| "autonomy": runtime, | |
| "goals": config.get("goals", []), | |
| "queue": ( | |
| runtime.get("queue", {}).get("items", []) | |
| if isinstance(runtime.get("queue"), dict) | |
| else [] | |
| ), | |
| "capabilities": control_plane.build_capabilities(bot_mode=TWITCH_CHAT_MODE), | |
| } | |