Skip to content

The agent that grows with you

refactor(hermes_cli/web_routers): shared device-code /start response + custom-endpoint row builder

Teknium83e0ee92290a
diff --git a/hermes_cli/web_routers/config_env.py b/hermes_cli/web_routers/config_env.py
index 4b7aa3fc47a..c8367300340 100644
--- a/hermes_cli/web_routers/config_env.py
+++ b/hermes_cli/web_routers/config_env.py
@@ -352,6 +352,19 @@ def _config_api_key_is_env_ref(endpoint_id: str) -> bool:
return bool(isinstance(raw_key, str) and re.search(r"\$\{[^}]+\}", raw_key))
+def _endpoint_row(
+ endpoint_id: str, name: str, base_url: str, model: str, models: List[str], context_length,
+ discover_models: bool, key_entry: Dict[str, Any], is_current: bool, source: str,
+) -> Dict[str, Any]:
+ has_api_key, api_key_preview = _api_key_display(key_entry)
+ return {
+ "id": endpoint_id, "name": name, "base_url": base_url, "model": model, "models": models,
+ "context_length": context_length, "discover_models": discover_models,
+ "has_api_key": has_api_key, "api_key_preview": api_key_preview,
+ "is_current": is_current, "source": source,
+ }
+
+
def _custom_endpoint_response(cfg: Dict[str, Any]) -> Dict[str, Any]:
model_cfg = cfg.get("model", {}) if isinstance(cfg.get("model"), dict) else {}
current_provider = str(model_cfg.get("provider", "") or "")
@@ -369,26 +382,18 @@ def _custom_endpoint_response(cfg: Dict[str, Any]) -> Dict[str, Any]:
continue
endpoint_id = str(provider_id)
models = _models_from_custom_endpoint_entry(raw_entry)
- endpoint_model = str(raw_entry.get("model") or raw_entry.get("default_model") or (models[0] if models else ""))
- has_api_key, api_key_preview = _api_key_display(raw_entry)
- endpoints.append({
- "id": endpoint_id, "name": str(raw_entry.get("name") or endpoint_id),
- "base_url": base_url, "model": endpoint_model, "models": models,
- "context_length": raw_entry.get("context_length"),
- "discover_models": bool(raw_entry.get("discover_models", True)),
- "has_api_key": has_api_key, "api_key_preview": api_key_preview,
- "is_current": endpoint_id == current_provider, "source": "providers",
- })
+ endpoints.append(_endpoint_row(
+ endpoint_id, str(raw_entry.get("name") or endpoint_id), base_url,
+ str(raw_entry.get("model") or raw_entry.get("default_model") or (models[0] if models else "")),
+ models, raw_entry.get("context_length"), bool(raw_entry.get("discover_models", True)),
+ raw_entry, endpoint_id == current_provider, "providers",
+ ))
if current_provider.lower() == "custom" and current_base_url and not any(e["id"] == "custom" for e in endpoints):
- has_api_key, api_key_preview = _api_key_display(model_cfg)
- endpoints.insert(0, {
- "id": "custom", "name": "Custom", "base_url": current_base_url, "model": current_model,
- "models": [current_model] if current_model else [],
- "context_length": model_cfg.get("context_length"), "discover_models": True,
- "has_api_key": has_api_key, "api_key_preview": api_key_preview, "is_current": True,
- "source": "direct-config",
- })
+ endpoints.insert(0, _endpoint_row(
+ "custom", "Custom", current_base_url, current_model, [current_model] if current_model else [],
+ model_cfg.get("context_length"), True, model_cfg, True, "direct-config",
+ ))
return {
"endpoints": endpoints,
diff --git a/hermes_cli/web_routers/oauth.py b/hermes_cli/web_routers/oauth.py
index 221e343b11c..6e33afc8097 100644
--- a/hermes_cli/web_routers/oauth.py
+++ b/hermes_cli/web_routers/oauth.py
@@ -107,6 +107,14 @@ def _start_poller(target, sid: str) -> None:
threading.Thread(target=target, args=(sid,), daemon=True, name=f"oauth-poll-{sid[:6]}").start()
+def _device_code_started(sid: str, user_code, verification_url, expires_in: int, poll_interval: int) -> Dict[str, Any]:
+ """The /start response shape shared by every device-code flow."""
+ return {
+ "session_id": sid, "flow": "device_code", "user_code": user_code,
+ "verification_url": verification_url, "expires_in": expires_in, "poll_interval": poll_interval,
+ }
+
+
def _codex_full_login_worker(session_id: str) -> None:
"""Run the complete OpenAI Codex device-code flow.
@@ -359,11 +367,10 @@ async def _start_nous_device_code(profile: Optional[str]) -> Dict[str, Any]:
client_id=client_id, scope=effective_scope,
)
_start_poller(_nous_poller, sid)
- return {
- "session_id": sid, "flow": "device_code", "user_code": str(device_data["user_code"]),
- "verification_url": str(device_data["verification_uri_complete"]),
- "expires_in": int(device_data["expires_in"]), "poll_interval": int(device_data["interval"]),
- }
+ return _device_code_started(
+ sid, str(device_data["user_code"]), str(device_data["verification_uri_complete"]),
+ int(device_data["expires_in"]), int(device_data["interval"]),
+ )
async def _start_codex_device_code(profile: Optional[str]) -> Dict[str, Any]:
@@ -385,11 +392,9 @@ async def _start_codex_device_code(profile: Optional[str]) -> Dict[str, Any]:
raise HTTPException(status_code=500, detail=s.get("error_message") or "device-auth failed")
if not s.get("user_code"):
raise HTTPException(status_code=504, detail="device-auth timed out before returning a user code")
- return {
- "session_id": sid, "flow": "device_code", "user_code": s["user_code"],
- "verification_url": s["verification_url"], "expires_in": int(s.get("expires_in") or 900),
- "poll_interval": int(s.get("interval") or 5),
- }
+ return _device_code_started(
+ sid, s["user_code"], s["verification_url"], int(s.get("expires_in") or 900), int(s.get("interval") or 5)
+ )
async def _start_minimax_device_code(profile: Optional[str]) -> Dict[str, Any]:
@@ -433,11 +438,10 @@ async def _start_minimax_device_code(profile: Optional[str]) -> Dict[str, Any]:
expires_in_seconds = expired_in_raw
sess["expires_at"] = expires_at_ts
_start_poller(_minimax_poller, sid)
- return {
- "session_id": sid, "flow": "device_code", "user_code": str(device_data["user_code"]),
- "verification_url": str(device_data["verification_uri"]), "expires_in": expires_in_seconds,
- "poll_interval": max(2, (sess["interval_ms"] or 2000) // 1000),
- }
+ return _device_code_started(
+ sid, str(device_data["user_code"]), str(device_data["verification_uri"]), expires_in_seconds,
+ max(2, (sess["interval_ms"] or 2000) // 1000),
+ )
async def _start_xai_device_code(profile: Optional[str]) -> Dict[str, Any]:
@@ -455,11 +459,11 @@ async def _start_xai_device_code(profile: Optional[str]) -> Dict[str, Any]:
expires_at=time.time() + int(device_data["expires_in"]),
)
_start_poller(_xai_device_poller, sid)
- return {
- "session_id": sid, "flow": "device_code", "user_code": str(device_data["user_code"]),
- "verification_url": str(device_data.get("verification_uri_complete") or device_data["verification_uri"]),
- "expires_in": int(device_data["expires_in"]), "poll_interval": int(device_data["interval"]),
- }
+ return _device_code_started(
+ sid, str(device_data["user_code"]),
+ str(device_data.get("verification_uri_complete") or device_data["verification_uri"]),
+ int(device_data["expires_in"]), int(device_data["interval"]),
+ )
_DEVICE_CODE_STARTERS = {