feat(kpi): expand KPI runtime monitoring, prompt advisory envelopes, and missing field resiliency
This commit is contained in:
+25
-8
@@ -53,11 +53,13 @@ class NodeUsage:
|
||||
bars: List[Dict[str, Any]]
|
||||
dialog_text: str
|
||||
has_redeemed: bool = False
|
||||
stats_loaded: bool = True
|
||||
|
||||
def to_dict(self) -> Dict[str, Any]:
|
||||
return asdict(self)
|
||||
|
||||
|
||||
|
||||
def cdp_send_command(ws: Any, method: str, params: Dict[str, Any], timeout: float = 3.0) -> Optional[Dict[str, Any]]:
|
||||
"""Send a raw CDP command and await response with a unique request ID."""
|
||||
req_id = next(_REQ_COUNTER)
|
||||
@@ -345,9 +347,18 @@ class SettingsRPA:
|
||||
self.close_settings_dialog()
|
||||
|
||||
if not raw:
|
||||
raise RuntimeError(f"Failed to read usage data from Settings dialog on {self.node}")
|
||||
raw = {
|
||||
"dialogText": "",
|
||||
"bars": [],
|
||||
"plan": "Unknown",
|
||||
"resetText": "Unavailable (usage stats did not load)",
|
||||
"tokensLeft": "Unavailable",
|
||||
}
|
||||
|
||||
bars = raw.get("bars", [])
|
||||
dialog_text = raw.get("dialogText", "")
|
||||
stats_loaded = bool(bars or "Weekly limit" in dialog_text or "Additional tokens" in dialog_text or "Free plan" in dialog_text)
|
||||
|
||||
weekly_pct = 0
|
||||
extra_pct = 0
|
||||
extra_status = "Never expires"
|
||||
@@ -362,25 +373,26 @@ class SettingsRPA:
|
||||
|
||||
# Blocked condition: weekly limit 100% and additional tokens 100% or 0 tokens left
|
||||
tokens_left = raw.get("tokensLeft", "")
|
||||
is_blocked = (weekly_pct >= 100 and extra_pct >= 100) or ("0 tokens left" in tokens_left)
|
||||
is_blocked = bool(stats_loaded and ((weekly_pct >= 100 and extra_pct >= 100) or ("0 tokens left" in tokens_left)))
|
||||
|
||||
# has_redeemed binary: If the "Additional tokens" ticker is present in Settings (or "Redeem invite code" entry is absent),
|
||||
# the agent has already redeemed an invite code.
|
||||
has_extra_ticker = any("additional tokens" in b.get("label", "").lower() or "additional" in b.get("raw_text", "").lower() for b in bars)
|
||||
has_redeemed = has_extra_ticker or ("Additional tokens" in raw.get("dialogText", ""))
|
||||
has_redeemed = bool(has_extra_ticker or ("Additional tokens" in dialog_text))
|
||||
|
||||
return NodeUsage(
|
||||
node=self.node,
|
||||
plan=raw.get("plan", "Free plan"),
|
||||
weekly_reset_text=raw.get("resetText", ""),
|
||||
plan=raw.get("plan", "Unknown" if not stats_loaded else "Free plan"),
|
||||
weekly_reset_text=raw.get("resetText", "") or ("Unavailable (stats did not load)" if not stats_loaded else ""),
|
||||
weekly_percent_used=weekly_pct,
|
||||
extra_tokens_status=extra_status,
|
||||
extra_percent_used=extra_pct,
|
||||
extra_tokens_remaining=tokens_left or ("0 tokens left" if extra_pct >= 100 else "Unknown"),
|
||||
extra_tokens_remaining=tokens_left or ("Unavailable" if not stats_loaded else ("0 tokens left" if extra_pct >= 100 else "Unknown")),
|
||||
is_blocked=is_blocked,
|
||||
bars=bars,
|
||||
dialog_text=raw.get("dialogText", ""),
|
||||
dialog_text=dialog_text,
|
||||
has_redeemed=has_redeemed,
|
||||
stats_loaded=stats_loaded,
|
||||
)
|
||||
|
||||
def check_redeem_entrypoint(self) -> Dict[str, Any]:
|
||||
@@ -423,7 +435,12 @@ def main() -> None:
|
||||
if args.json:
|
||||
print(json.dumps(usage.to_dict(), indent=2))
|
||||
else:
|
||||
status_str = "BLOCKED (LIMIT REACHED)" if usage.is_blocked else "ACTIVE"
|
||||
if not usage.stats_loaded:
|
||||
status_str = "UNLOADED (STATS DID NOT RENDER)"
|
||||
elif usage.is_blocked:
|
||||
status_str = "BLOCKED (LIMIT REACHED)"
|
||||
else:
|
||||
status_str = "ACTIVE"
|
||||
print(f"=== Node {usage.node} Usage ===")
|
||||
print(f" Plan: {usage.plan}")
|
||||
print(f" Weekly Reset: {usage.weekly_reset_text} ({usage.weekly_percent_used}% used)")
|
||||
|
||||
Reference in New Issue
Block a user