fix(agent-subscriptions): handle MiniMax API v2 response shape

MiniMax changed from category_remains[text_generation] with count fields
to model_remains[general] with remaining_percent fields. Now tries v2
first (100 - remaining_percent), falls back to v1 count-based calculation.
This commit is contained in:
Paul O'Reilly
2026-06-03 20:49:20 +12:00
parent b0130bf06b
commit c0a2110ee6

View File

@@ -116,7 +116,12 @@ def probe_anthropic(token: str) -> dict[str, float] | None:
def probe_minimax(api_key: str) -> tuple[dict[str, float] | None, str | None]:
"""Returns (usage_dict, error_reason). On success error_reason is None."""
"""Returns (usage_dict, error_reason). On success error_reason is None.
API v2 shape (current): model_remains[].model_name with remaining_percent fields.
API v1 shape (legacy): category_remains[].category == "text_generation" with count fields.
Tries v2 first; falls back to v1.
"""
try:
req = urllib.request.Request(
"https://www.minimax.io/v1/token_plan/remains",
@@ -131,6 +136,20 @@ def probe_minimax(api_key: str) -> tuple[dict[str, float] | None, str | None]:
if status != 0:
msg = data.get("base_resp", {}).get("status_msg", "unknown")
return None, f"API status_code={status} ({msg})"
# v2: model_remains with remaining_percent fields (general = text)
for entry in data.get("model_remains", []):
if entry.get("model_name") == "general":
result = {}
ih = entry.get("current_interval_remaining_percent")
wh = entry.get("current_weekly_remaining_percent")
if ih is not None:
result["five_hour"] = 100.0 - float(ih)
if wh is not None:
result["seven_day"] = 100.0 - float(wh)
return (result or None), None
# v1 fallback: category_remains with count fields
for cat in data.get("category_remains", []):
if cat.get("category") == "text_generation":
result = {}
@@ -143,7 +162,8 @@ def probe_minimax(api_key: str) -> tuple[dict[str, float] | None, str | None]:
if weekly_total > 0:
result["seven_day"] = float(weekly_used) / float(weekly_total) * 100.0
return (result or None), None
return None, "text_generation category not found in response"
return None, "no usable plan data in response (keys: " + ", ".join(data.keys()) + ")"
except urllib.error.HTTPError as e:
return None, f"HTTP {e.code} {e.reason}"
except urllib.error.URLError as e: