Download selection.py from sqh-1997/WatcherRobot-com.orulink.codex_supervisor: direct link, hf CLI and curl.
- Browser
- Download file 4.99 kB
-
https://huggingface.co/spaces/sqh-1997/WatcherRobot-com.orulink.codex_supervisor/resolve/main/selection.py
- Command line
-
hf download hf://spaces/sqh-1997/WatcherRobot-com.orulink.codex_supervisor/selection.py
-
curl -L -o selection.py https://huggingface.co/spaces/sqh-1997/WatcherRobot-com.orulink.codex_supervisor/resolve/main/selection.py
4.99 kB
| """Display selection shared with the approved circular supervisor UI.""" | |
| from __future__ import annotations | |
| import re | |
| import time | |
| import unicodedata | |
| from pathlib import PurePath | |
| from typing import Any | |
| ROTATION_MS = 4_000 | |
| COMPLETION_NOTICE_MS = ROTATION_MS | |
| HEARTBEAT_MS = 1_000 | |
| VISIBLE_TASKS = 4 | |
| TITLE_MAX_BYTES = 60 | |
| CONTEXT_MAX_BYTES = 32 | |
| def _number(value: Any, default: int = 0) -> int: | |
| return int(value) if isinstance(value, (int, float)) and value >= 0 else default | |
| def _local_day_key(timestamp_ms: int) -> str: | |
| return time.strftime("%Y-%m-%d", time.localtime(timestamp_ms / 1_000)) | |
| def _is_display_eligible(session: dict[str, Any], now_ms: int) -> bool: | |
| today = _local_day_key(now_ms) | |
| if session.get("roundDate") == today and _number(session.get("roundCountToday")) >= 2: | |
| return True | |
| if session.get("status") == "running": | |
| return _local_day_key(_number(session.get("startedAt"))) == today | |
| if session.get("status") == "waiting": | |
| completed_at = _number(session.get("completedAt")) | |
| completion_age = now_ms - completed_at | |
| return ( | |
| completed_at > 0 | |
| and _local_day_key(completed_at) == today | |
| and 0 <= completion_age < COMPLETION_NOTICE_MS | |
| ) | |
| return False | |
| def select_display_sessions(state: dict[str, Any], now_ms: int) -> list[dict[str, Any]]: | |
| sessions = [ | |
| item | |
| for item in state.get("sessions", []) | |
| if isinstance(item, dict) and _is_display_eligible(item, now_ms) | |
| ] | |
| last_event = state.get("lastEvent") if isinstance(state.get("lastEvent"), dict) else {} | |
| action = last_event.get("action") | |
| event_at = _number(last_event.get("at")) | |
| takeover = None | |
| if action in {"stop", "interrupt"} and now_ms - event_at < ROTATION_MS: | |
| takeover_id = last_event.get("sessionId") | |
| takeover = next( | |
| (item for item in sessions if item.get("id") == takeover_id and item.get("status") == "waiting"), | |
| None, | |
| ) | |
| running = sorted( | |
| (item for item in sessions if item.get("status") == "running"), | |
| key=lambda item: _number(item.get("startedAt"), now_ms), | |
| ) | |
| waiting = sorted( | |
| (item for item in sessions if item.get("status") == "waiting"), | |
| key=lambda item: _number(item.get("completedAt")), | |
| reverse=True, | |
| ) | |
| ordered = ([takeover] if takeover is not None else []) + running + waiting | |
| displayed = [] | |
| seen = set() | |
| for item in ordered: | |
| identifier = item.get("id") | |
| if identifier in seen: | |
| continue | |
| seen.add(identifier) | |
| displayed.append(item) | |
| if len(displayed) == VISIBLE_TASKS: | |
| break | |
| # Stable positions across completion / next-round transitions. Membership | |
| # priority stays above; status changes do not rotate existing sectors. | |
| return sorted(displayed, key=lambda item: str(item.get("id", ""))) | |
| def select_display_session(state: dict[str, Any], now_ms: int) -> dict[str, Any] | None: | |
| displayed = select_display_sessions(state, now_ms) | |
| return displayed[0] if displayed else None | |
| def _truncate_utf8(text: str, maximum: int) -> str: | |
| normalized = _sanitize_device_text(text) | |
| encoded = normalized.encode("utf-8") | |
| if len(encoded) <= maximum: | |
| return normalized | |
| suffix = b"..." | |
| return encoded[: maximum - len(suffix)].decode("utf-8", "ignore").rstrip() + "..." | |
| def _sanitize_device_text(text: str) -> str: | |
| """Keep only glyph ranges guaranteed by the device font bundle.""" | |
| safe = [] | |
| for character in unicodedata.normalize("NFKC", text): | |
| codepoint = ord(character) | |
| if character.isspace(): | |
| safe.append(" ") | |
| elif 0x20 <= codepoint <= 0x7E or 0x4E00 <= codepoint <= 0x9FFF: | |
| safe.append(character) | |
| else: | |
| safe.append(" ") | |
| return " ".join("".join(safe).split()) | |
| def _workspace_name(session: dict[str, Any]) -> str: | |
| cwd = session.get("cwd") if isinstance(session.get("cwd"), str) else "" | |
| return PurePath(cwd.replace("\\", "/")).name if cwd else "CODEX" | |
| def _session_suffix(session: dict[str, Any]) -> str: | |
| identifier = str(session.get("id") or "") | |
| return re.sub(r"[^A-Za-z0-9]", "", identifier)[-4:].upper() | |
| def device_title(session: dict[str, Any] | None) -> str: | |
| if not session: | |
| return "CODEX" | |
| conversation = session.get("conversation") if isinstance(session.get("conversation"), str) else "" | |
| title = conversation.strip() or f"{_workspace_name(session)} 任务" | |
| return _truncate_utf8(title, TITLE_MAX_BYTES) or "CODEX" | |
| def device_context(session: dict[str, Any] | None) -> str: | |
| if not session: | |
| return "" | |
| suffix = _session_suffix(session) | |
| suffix_text = f" - {suffix}" if suffix else "" | |
| folder_limit = CONTEXT_MAX_BYTES - len(suffix_text.encode("utf-8")) | |
| folder = _truncate_utf8(_workspace_name(session), max(3, folder_limit)) or "CODEX" | |
| return f"{folder}{suffix_text}" | |