The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(hermes_cli/web_routers): oauth dict-dispatch for status cards and device-code starters; LateState session state
Tekniumec9ebe907cf3
diff --git a/hermes_cli/web_routers/oauth.py b/hermes_cli/web_routers/oauth.pyindex acf6cf7babe..d7ca891fc94 100644--- a/hermes_cli/web_routers/oauth.py+++ b/hermes_cli/web_routers/oauth.py@@ -1,7 +1,7 @@"""OAuth provider dashboard routes: catalog/status, disconnect, and in-browser device-code login flows.Extracted from ``hermes_cli.web_server``; helpers/state that tests monkeypatch on-``web_server`` stay there and are imported lazily at call time (cycle-safe).+``web_server`` stay there and are late-bound (cycle-safe)."""import logging@@ -9,10 +9,10 @@ import asyncioimport sysimport timefrom fastapi import APIRouter-from hermes_cli.web_deps import late+from hermes_cli.web_deps import LateState, latefrom fastapi import HTTPException, Requestfrom hermes_cli.web_models import OAuthSubmitBody-from typing import Any, Dict, Optional+from typing import Any, Callable, Dict, Optionalimport threadingimport osimport secrets@@ -20,7 +20,7 @@ import secrets_log = logging.getLogger("hermes_cli.web_server")router = APIRouter()-# web_server helpers, late-bound so monkeypatch.setattr(web_server, ...) stays authoritative.+# web_server helpers/state, late-bound so monkeypatch.setattr(web_server, ...) stays authoritative._external_process_cli_command = late("_external_process_cli_command")_oauth_profile_name = late("_oauth_profile_name")_profile_scope = late("_profile_scope")@@ -30,6 +30,9 @@ _minimax_poller = late("_minimax_poller")_nous_poller = late("_nous_poller")_truncate_token = late("_truncate_token")_xai_device_poller = late("_xai_device_poller")+_oauth_sessions = LateState("_oauth_sessions")+_oauth_sessions_lock = LateState("_oauth_sessions_lock")+_OAUTH_PROVIDER_CATALOG = LateState("_OAUTH_PROVIDER_CATALOG")def _http_response_error_detail(resp: Any) -> str:@@ -86,14 +89,12 @@ def _new_oauth_session(profile: Optional[str] = None,) -> tuple[str, Dict[str, Any]]:"""Create + register a new OAuth session, return (session_id, session_dict)."""- from hermes_cli.web_server import _oauth_sessions, _oauth_sessions_locksid = secrets.token_urlsafe(16)- profile_name = _oauth_profile_name(profile)sess = {"session_id": sid,"provider": provider_id,"flow": flow,- "profile": profile_name,+ "profile": _oauth_profile_name(profile),"created_at": time.time(),"status": "pending", # pending | approved | denied | expired | error"error_message": None,@@ -103,28 +104,23 @@ def _new_oauth_session(return sid, sess+def _start_poller(target, sid: str) -> None:+ threading.Thread(target=target, args=(sid,), daemon=True, name=f"oauth-poll-{sid[:6]}").start()++def _codex_full_login_worker(session_id: str) -> None:"""Run the complete OpenAI Codex device-code flow.- Codex doesn't use the standard OAuth device-code endpoints; it has its- own ``/api/accounts/deviceauth/usercode`` (JSON body, returns- ``device_auth_id``) and ``/api/accounts/deviceauth/token`` (JSON body- polled until 200). On success the response carries an- ``authorization_code`` + ``code_verifier`` that get exchanged at- CODEX_OAUTH_TOKEN_URL with grant_type=authorization_code.-- The flow is replicated inline (rather than calling- _codex_device_code_login) because that helper prints/blocks/polls in a- single function — we need to surface the user_code to the dashboard the- moment we receive it, well before polling completes.+ Codex has its own ``/api/accounts/deviceauth/usercode`` (returns+ ``device_auth_id``) and ``/api/accounts/deviceauth/token`` (polled until+ 200) endpoints; success yields ``authorization_code`` + ``code_verifier``+ exchanged at CODEX_OAUTH_TOKEN_URL. Replicated inline rather than calling+ ``_codex_device_code_login`` because that helper prints/blocks/polls in one+ function — the dashboard needs the user_code before polling completes."""- from hermes_cli.web_server import _oauth_sessions, _oauth_sessions_locktry:import httpx- from hermes_cli.auth import (- CODEX_OAUTH_CLIENT_ID,- CODEX_OAUTH_TOKEN_URL,- )+ from hermes_cli.auth import CODEX_OAUTH_CLIENT_ID, CODEX_OAUTH_TOKEN_URLissuer = "https://auth.openai.com"# Step 1: request device code@@ -142,13 +138,12 @@ def _codex_full_login_worker(session_id: str) -> None:poll_interval = max(3, int(device_data.get("interval", "5")))if not user_code or not device_auth_id:raise RuntimeError("device-code response missing user_code or device_auth_id")- verification_url = f"{issuer}/codex/device"with _oauth_sessions_lock:sess = _oauth_sessions.get(session_id)if not sess:returnsess["user_code"] = user_code- sess["verification_url"] = verification_url+ sess["verification_url"] = f"{issuer}/codex/device"sess["device_auth_id"] = device_auth_idsess["interval"] = poll_intervalsess["expires_in"] = 15 * 60 # OpenAI's effective limit@@ -219,23 +214,16 @@ def _codex_full_login_worker(session_id: str) -> None:from hermes_cli.auth import _save_codex_tokens- # The cancellation check and the save must be one atomic critical- # section under the same lock cancel_oauth_session() uses. Checking- # "cancelled" and then saving as two separate steps left a window- # where DELETE could flip the flag between them and the worker would- # still persist tokens after the user believed the login was- # aborted. Holding the lock across both closes that window: DELETE- # either lands before this section (worker observes cancelled and- # returns) or blocks until this section (and the save) is done.+ # The cancellation check and the save are one atomic critical section+ # under the lock cancel_oauth_session() uses; otherwise DELETE could+ # flip "cancelled" between the check and the save and tokens would be+ # persisted after the user believed the login was aborted.with _oauth_sessions_lock:if sess.get("cancelled"):_log.info("oauth/device: openai-codex login cancelled before token save (session=%s)", session_id)returnwith _profile_scope(session_profile):- _save_codex_tokens({- "access_token": access_token,- "refresh_token": refresh_token,- })+ _save_codex_tokens({"access_token": access_token, "refresh_token": refresh_token})sess["status"] = "approved"_log.info("oauth/device: openai-codex login completed (session=%s)", session_id)except Exception as e:@@ -247,6 +235,77 @@ def _codex_full_login_worker(session_id: str) -> None:s["error_message"] = str(e)+# Hand-written status card shapes per provider id: (hauth getter name, shaper).+# Providers absent here fall through to the slug-driven ``get_auth_status``.+def _nous_status(raw):+ # Refresh-free snapshot so listing providers never performs an OAuth refresh.+ return {+ "logged_in": bool(raw.get("logged_in")),+ "source": "nous_portal",+ "source_label": raw.get("portal_base_url") or "Nous Portal",+ "token_preview": _truncate_token(raw.get("access_token")),+ "expires_at": raw.get("access_expires_at"),+ "has_refresh_token": bool(raw.get("has_refresh_token")),+ }+++def _codex_status(raw):+ return {+ "logged_in": bool(raw.get("logged_in")),+ "source": raw.get("source") or "openai_codex",+ "source_label": raw.get("auth_mode") or "OpenAI Codex",+ "token_preview": _truncate_token(raw.get("api_key")),+ "expires_at": None,+ "has_refresh_token": False,+ "last_refresh": raw.get("last_refresh"),+ }+++def _qwen_status(raw):+ return {+ "logged_in": bool(raw.get("logged_in")),+ "source": "qwen_cli",+ "source_label": raw.get("auth_store_path") or "Qwen CLI",+ "token_preview": _truncate_token(raw.get("access_token")),+ "expires_at": raw.get("expires_at"),+ "has_refresh_token": bool(raw.get("has_refresh_token")),+ }+++def _minimax_status(raw):+ return {+ "logged_in": bool(raw.get("logged_in")),+ "source": "minimax_oauth",+ "source_label": f"MiniMax ({raw.get('region', 'global')})",+ "token_preview": None,+ "expires_at": raw.get("expires_at"),+ "has_refresh_token": True,+ }+++def _xai_status(raw):+ # source_label is a human-readable origin (auth-store path / credential+ # source), not the internal auth_mode string ("oauth_pkce").+ return {+ "logged_in": bool(raw.get("logged_in")),+ "source": raw.get("source") or "xai_oauth",+ "source_label": raw.get("auth_store") or raw.get("source") or "xAI Grok OAuth",+ "token_preview": _truncate_token(raw.get("api_key")),+ "expires_at": None,+ "has_refresh_token": True,+ "last_refresh": raw.get("last_refresh"),+ }+++_PROVIDER_STATUS: Dict[str, tuple[str, Callable[[dict], dict]]] = {+ "nous": ("get_nous_auth_status_local", _nous_status),+ "openai-codex": ("get_codex_auth_status", _codex_status),+ "qwen-oauth": ("get_qwen_auth_status", _qwen_status),+ "minimax-oauth": ("get_minimax_oauth_auth_status", _minimax_status),+ "xai-oauth": ("get_xai_oauth_auth_status", _xai_status),+}++def _resolve_provider_status(provider_id: str, status_fn) -> Dict[str, Any]:"""Dispatch to the right status helper for an OAuth provider entry."""if status_fn is not None:@@ -256,69 +315,13 @@ def _resolve_provider_status(provider_id: str, status_fn) -> Dict[str, Any]:return {"logged_in": False, "error": str(e)}try:from hermes_cli import auth as hauth- if provider_id == "nous":- # Read-only accounts-tab card: refresh-free snapshot so listing- # providers never performs an OAuth refresh.- raw = hauth.get_nous_auth_status_local()- return {- "logged_in": bool(raw.get("logged_in")),- "source": "nous_portal",- "source_label": raw.get("portal_base_url") or "Nous Portal",- "token_preview": _truncate_token(raw.get("access_token")),- "expires_at": raw.get("access_expires_at"),- "has_refresh_token": bool(raw.get("has_refresh_token")),- }- if provider_id == "openai-codex":- raw = hauth.get_codex_auth_status()- return {- "logged_in": bool(raw.get("logged_in")),- "source": raw.get("source") or "openai_codex",- "source_label": raw.get("auth_mode") or "OpenAI Codex",- "token_preview": _truncate_token(raw.get("api_key")),- "expires_at": None,- "has_refresh_token": False,- "last_refresh": raw.get("last_refresh"),- }- if provider_id == "qwen-oauth":- raw = hauth.get_qwen_auth_status()- return {- "logged_in": bool(raw.get("logged_in")),- "source": "qwen_cli",- "source_label": raw.get("auth_store_path") or "Qwen CLI",- "token_preview": _truncate_token(raw.get("access_token")),- "expires_at": raw.get("expires_at"),- "has_refresh_token": bool(raw.get("has_refresh_token")),- }- if provider_id == "minimax-oauth":- raw = hauth.get_minimax_oauth_auth_status()- return {- "logged_in": bool(raw.get("logged_in")),- "source": "minimax_oauth",- "source_label": f"MiniMax ({raw.get('region', 'global')})",- "token_preview": None,- "expires_at": raw.get("expires_at"),- "has_refresh_token": True,- }- if provider_id == "xai-oauth":- raw = hauth.get_xai_oauth_auth_status()- # source_label is meant to be a human-readable origin (auth-store- # path / credential source), not the internal auth_mode string- # ("oauth_pkce"). Prefer the store path, then the source slug.- return {- "logged_in": bool(raw.get("logged_in")),- "source": raw.get("source") or "xai_oauth",- "source_label": raw.get("auth_store") or raw.get("source") or "xAI Grok OAuth",- "token_preview": _truncate_token(raw.get("api_key")),- "expires_at": None,- "has_refresh_token": True,- "last_refresh": raw.get("last_refresh"),- }- # No hand-written branch for this provider id: fall through to the- # canonical slug-driven dispatcher so accounts-tab providers derived- # from the unified catalog (which carry status_fn=None) still reflect- # real login state instead of rendering permanently logged-out. This- # closes the membership-auto-extends-but-status-doesn't gap: add an- # OAuth/account provider plugin and its card shows the right state.+ entry = _PROVIDER_STATUS.get(provider_id)+ if entry is not None:+ getter, shape = entry+ return shape(getattr(hauth, getter)())+ # Catalog-derived providers (status_fn=None, no hand-written card) still+ # reflect real login state via the canonical slug-driven dispatcher, so+ # a new OAuth/account provider plugin never renders permanently logged-out.raw = hauth.get_auth_status(provider_id)if isinstance(raw, dict) and "logged_in" in raw:return {@@ -332,9 +335,7 @@ def _resolve_provider_status(provider_id: str, status_fn) -> Dict[str, Any]:or raw.get("name")or ""),- "token_preview": _truncate_token(- raw.get("access_token") or raw.get("api_key")- ),+ "token_preview": _truncate_token(raw.get("access_token") or raw.get("api_key")),"expires_at": raw.get("expires_at") or raw.get("access_expires_at"),"has_refresh_token": bool(raw.get("has_refresh_token")),}@@ -343,233 +344,193 @@ def _resolve_provider_status(provider_id: str, status_fn) -> Dict[str, Any]:return {"logged_in": False}+async def _start_nous_device_code(profile: Optional[str]) -> Dict[str, Any]:+ from hermes_cli.auth import _request_device_code, PROVIDER_REGISTRY+ import httpx+ pconfig = PROVIDER_REGISTRY["nous"]+ portal_base_url = (+ os.getenv("HERMES_PORTAL_BASE_URL")+ or os.getenv("NOUS_PORTAL_BASE_URL")+ or pconfig.portal_base_url+ ).rstrip("/")+ client_id = pconfig.client_id+ scope = pconfig.scope++ def _do_nous_device_request():+ with httpx.Client(timeout=httpx.Timeout(15.0), headers={"Accept": "application/json"}) as client:+ return (+ _request_device_code(+ client=client, portal_base_url=portal_base_url, client_id=client_id, scope=scope+ ),+ scope,+ )++ device_data, effective_scope = await asyncio.get_running_loop().run_in_executor(None, _do_nous_device_request)+ sid, sess = _new_oauth_session("nous", "device_code", profile=profile)+ sess.update(+ device_code=str(device_data["device_code"]),+ interval=int(device_data["interval"]),+ expires_at=time.time() + int(device_data["expires_in"]),+ portal_base_url=portal_base_url,+ 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"]),+ }+++async def _start_codex_device_code(profile: Optional[str]) -> Dict[str, Any]:+ # The full Codex helper polls inline, so it runs in a worker thread and+ # proxies user_code + verification_url back via the session dict.+ sid, _ = _new_oauth_session("openai-codex", "device_code", profile=profile)+ threading.Thread(target=_codex_full_login_worker, args=(sid,), daemon=True, name=f"oauth-codex-{sid[:6]}").start()+ # Block briefly until the worker has populated the user_code, OR error.+ deadline = time.monotonic() + 10+ while time.monotonic() < deadline:+ with _oauth_sessions_lock:+ s = _oauth_sessions.get(sid)+ if s and (s.get("user_code") or s["status"] != "pending"):+ break+ await asyncio.sleep(0.1)+ with _oauth_sessions_lock:+ s = _oauth_sessions.get(sid, {})+ if s.get("status") == "error":+ 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),+ }+++async def _start_minimax_device_code(profile: Optional[str]) -> Dict[str, Any]:+ # Device-code flow with a PKCE extension: verifier + challenge from+ # _minimax_pkce_pair bind the token exchange to the original session.+ from hermes_cli.auth import (+ _minimax_pkce_pair, _minimax_request_user_code, MINIMAX_OAUTH_CLIENT_ID, MINIMAX_OAUTH_GLOBAL_BASE,+ )+ import httpx+ verifier, challenge, state = _minimax_pkce_pair()+ portal_base_url = (os.getenv("MINIMAX_PORTAL_BASE_URL") or MINIMAX_OAUTH_GLOBAL_BASE).rstrip("/")++ def _do_minimax_request():+ with httpx.Client(+ timeout=httpx.Timeout(15.0), headers={"Accept": "application/json"}, follow_redirects=True+ ) as client:+ return _minimax_request_user_code(+ client=client, portal_base_url=portal_base_url, client_id=MINIMAX_OAUTH_CLIENT_ID,+ code_challenge=challenge, state=state,+ )++ device_data = await asyncio.get_event_loop().run_in_executor(None, _do_minimax_request)+ sid, sess = _new_oauth_session("minimax-oauth", "device_code", profile=profile)+ # MiniMax's `interval` is in milliseconds (defensive default 2000ms in _minimax_poll_token).+ interval_raw = device_data.get("interval")+ sess.update(+ interval_ms=int(interval_raw) if interval_raw is not None else None,+ user_code=str(device_data["user_code"]),+ code_verifier=verifier,+ state=state,+ portal_base_url=portal_base_url,+ client_id=MINIMAX_OAUTH_CLIENT_ID,+ region="global",+ )+ # `expired_in` is overloaded — a unix-ms timestamp OR seconds-from-now.+ # Mirror the heuristic in _minimax_poll_token; keep the raw value for the+ # poller and derive expires_at + UI-friendly expires_in seconds.+ expired_in_raw = int(device_data["expired_in"])+ sess["expired_in_raw"] = expired_in_raw+ if expired_in_raw > 1_000_000_000_000: # likely unix-ms+ expires_at_ts = expired_in_raw / 1000.0+ expires_in_seconds = max(0, int(expires_at_ts - time.time()))+ else:+ expires_at_ts = time.time() + expired_in_raw+ 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),+ }+++async def _start_xai_device_code(profile: Optional[str]) -> Dict[str, Any]:+ from hermes_cli.auth import _xai_oauth_request_device_code+ import httpx++ def _do_xai_device_request():+ with httpx.Client(timeout=httpx.Timeout(20.0), headers={"Accept": "application/json"}) as client:+ return _xai_oauth_request_device_code(client)++ device_data = await asyncio.get_running_loop().run_in_executor(None, _do_xai_device_request)+ sid, sess = _new_oauth_session("xai-oauth", "device_code", profile=profile)+ sess.update(+ device_code=str(device_data["device_code"]),+ interval=int(device_data["interval"]),+ 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"]),+ }+++_DEVICE_CODE_STARTERS = {+ "nous": _start_nous_device_code,+ "openai-codex": _start_codex_device_code,+ "minimax-oauth": _start_minimax_device_code,+ "xai-oauth": _start_xai_device_code,+}++async def _start_device_code_flow(provider_id: str,profile: Optional[str] = None,) -> Dict[str, Any]:"""Initiate a device-code flow (Nous, OpenAI Codex, MiniMax, or xAI).- Calls the provider's device-auth endpoint via the existing CLI helpers,- then spawns a background poller. Returns the user-facing display fields- so the UI can render the verification page link + user code.+ Calls the provider's device-auth endpoint via the CLI helpers, then spawns+ a background poller. Returns the user-facing display fields (verification+ link + user code)."""- from hermes_cli.web_server import _oauth_sessions, _oauth_sessions_lock- if provider_id == "nous":- from hermes_cli.auth import (- _request_device_code,- PROVIDER_REGISTRY,- )- import httpx- pconfig = PROVIDER_REGISTRY["nous"]- portal_base_url = (- os.getenv("HERMES_PORTAL_BASE_URL")- or os.getenv("NOUS_PORTAL_BASE_URL")- or pconfig.portal_base_url- ).rstrip("/")- client_id = pconfig.client_id- scope = pconfig.scope-- def _do_nous_device_request():- with httpx.Client(- timeout=httpx.Timeout(15.0),- headers={"Accept": "application/json"},- ) as client:- return (- _request_device_code(- client=client,- portal_base_url=portal_base_url,- client_id=client_id,- scope=scope,- ),- scope,- )-- device_data, effective_scope = await asyncio.get_running_loop().run_in_executor(- None, _do_nous_device_request- )- sid, sess = _new_oauth_session("nous", "device_code", profile=profile)- sess["device_code"] = str(device_data["device_code"])- sess["interval"] = int(device_data["interval"])- sess["expires_at"] = time.time() + int(device_data["expires_in"])- sess["portal_base_url"] = portal_base_url- sess["client_id"] = client_id- sess["scope"] = effective_scope- threading.Thread(- target=_nous_poller, args=(sid,), daemon=True, name=f"oauth-poll-{sid[:6]}"- ).start()- 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"]),- }-- if provider_id == "openai-codex":- # Codex uses fixed OpenAI device-auth endpoints; reuse the helper.- sid, _ = _new_oauth_session("openai-codex", "device_code", profile=profile)- # Use the helper but in a thread because it polls inline.- # We can't extract just the start step without refactoring auth.py,- # so we run the full helper in a worker and proxy the user_code +- # verification_url back via the session dict. The helper prints- # to stdout — we capture nothing here, just status.- threading.Thread(- target=_codex_full_login_worker, args=(sid,), daemon=True,- name=f"oauth-codex-{sid[:6]}",- ).start()- # Block briefly until the worker has populated the user_code, OR error.- deadline = time.monotonic() + 10- while time.monotonic() < deadline:- with _oauth_sessions_lock:- s = _oauth_sessions.get(sid)- if s and (s.get("user_code") or s["status"] != "pending"):- break- await asyncio.sleep(0.1)- with _oauth_sessions_lock:- s = _oauth_sessions.get(sid, {})- if s.get("status") == "error":- 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),- }-- if provider_id == "minimax-oauth":- # MiniMax uses a device-code-style flow (verification URI + user- # code + background poll) with a PKCE extension on top. From the- # operator's perspective it's identical to Nous's device-code- # flow; the PKCE bit (verifier + challenge from- # _minimax_pkce_pair) is a security extension that binds the- # token exchange to the original session.- from hermes_cli.auth import (- _minimax_pkce_pair,- _minimax_request_user_code,- MINIMAX_OAUTH_CLIENT_ID,- MINIMAX_OAUTH_GLOBAL_BASE,- )- import httpx- verifier, challenge, state = _minimax_pkce_pair()- portal_base_url = (- os.getenv("MINIMAX_PORTAL_BASE_URL") or MINIMAX_OAUTH_GLOBAL_BASE- ).rstrip("/")- def _do_minimax_request():- with httpx.Client(- timeout=httpx.Timeout(15.0),- headers={"Accept": "application/json"},- follow_redirects=True,- ) as client:- return _minimax_request_user_code(- client=client,- portal_base_url=portal_base_url,- client_id=MINIMAX_OAUTH_CLIENT_ID,- code_challenge=challenge,- state=state,- )- device_data = await asyncio.get_event_loop().run_in_executor(- None, _do_minimax_request- )- sid, sess = _new_oauth_session("minimax-oauth", "device_code", profile=profile)- # The CLI flow names this `interval_ms` because MiniMax's- # `interval` field is in milliseconds (defensive default 2000ms- # in _minimax_poll_token).- interval_raw = device_data.get("interval")- sess["interval_ms"] = (- int(interval_raw) if interval_raw is not None else None- )- sess["user_code"] = str(device_data["user_code"])- sess["code_verifier"] = verifier- sess["state"] = state- sess["portal_base_url"] = portal_base_url- sess["client_id"] = MINIMAX_OAUTH_CLIENT_ID- sess["region"] = "global"- # `expired_in` from MiniMax is overloaded — could be a unix-ms- # timestamp OR a seconds-from-now duration. Mirror the heuristic- # in _minimax_poll_token. Stash the raw value for the poller;- # compute a derived expires_at + UI-friendly expires_in seconds.- expired_in_raw = int(device_data["expired_in"])- sess["expired_in_raw"] = expired_in_raw- if expired_in_raw > 1_000_000_000_000: # likely unix-ms- expires_at_ts = expired_in_raw / 1000.0- expires_in_seconds = max(0, int(expires_at_ts - time.time()))- else:- expires_at_ts = time.time() + expired_in_raw- expires_in_seconds = expired_in_raw- sess["expires_at"] = expires_at_ts- threading.Thread(- target=_minimax_poller,- args=(sid,),- daemon=True,- name=f"oauth-poll-{sid[:6]}",- ).start()- 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),- }-- if provider_id == "xai-oauth":- from hermes_cli.auth import _xai_oauth_request_device_code- import httpx-- def _do_xai_device_request():- with httpx.Client(- timeout=httpx.Timeout(20.0),- headers={"Accept": "application/json"},- ) as client:- return _xai_oauth_request_device_code(client)-- device_data = await asyncio.get_running_loop().run_in_executor(- None, _do_xai_device_request- )- sid, sess = _new_oauth_session("xai-oauth", "device_code", profile=profile)- sess["device_code"] = str(device_data["device_code"])- sess["interval"] = int(device_data["interval"])- sess["expires_at"] = time.time() + int(device_data["expires_in"])- threading.Thread(- target=_xai_device_poller,- args=(sid,),- daemon=True,- name=f"oauth-poll-{sid[:6]}",- ).start()- 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"]),- }-- raise HTTPException(status_code=400, detail=f"Provider {provider_id} does not support device-code flow")+ starter = _DEVICE_CODE_STARTERS.get(provider_id)+ if starter is None:+ raise HTTPException(status_code=400, detail=f"Provider {provider_id} does not support device-code flow")+ return await starter(profile)def _oauth_provider_disconnect_command(provider: Dict[str, Any]) -> Optional[str]:"""Shell command that clears an external provider's credentials.- External providers store their credentials outside Hermes, so the disconnect- API deliberately refuses them (we never delete files another CLI owns on the- user's behalf via a silent API call). For the ones we know how to clear we- instead hand the GUI a command it can *run in the embedded terminal* — the- user sees exactly what executes, and Hermes then stops resolving the token.-- Claude Code has no scriptable logout (only the interactive ``/logout``), so- we remove the credential the same way logout does: the macOS Keychain entry- (``Claude Code-credentials``) and/or the ``~/.claude/.credentials.json``- file — the two sources ``read_claude_code_credentials()`` consults. Returns- None for providers we can't safely clear (the GUI shows a manual hint).+ External providers store credentials outside Hermes, so the disconnect API+ refuses them (never silently delete files another CLI owns); instead the+ GUI gets a command to run in the embedded terminal, so the user sees+ exactly what executes. Claude Code has no scriptable logout, so we remove+ the credential the way logout does: the macOS Keychain entry+ (``Claude Code-credentials``) and/or ``~/.claude/.credentials.json`` — the+ two sources ``read_claude_code_credentials()`` consults. None for providers+ we can't safely clear (the GUI shows a manual hint)."""if provider.get("flow") != "external":return None@@ -583,17 +544,13 @@ def _oauth_provider_disconnect_command(provider: Dict[str, Any]) -> Optional[strdef _oauth_provider_disconnect_hint(provider: Dict[str, Any], status: Dict[str, Any]) -> Optional[str]:"""Return the manual disconnect path when the API cannot clear this provider."""- # "anthropic" is flow == "external" (no in-dashboard OAuth login, see the- # catalog entry) but, unlike other external providers, Hermes still OWNS- # the credential it can show here: the Hermes-managed PKCE file- # (~/.hermes/.anthropic_oauth.json) and its credential-pool entry, both- # written by `hermes auth add anthropic` in the terminal. Those are ours- # to clear via the API, so this provider is excluded from the generic- # "external providers can't be auto-disconnected" rule below.+ # "anthropic" is flow == "external" (no in-dashboard login) but Hermes still+ # OWNS its credential (the PKCE file ~/.hermes/.anthropic_oauth.json and its+ # credential-pool entry, written by `hermes auth add anthropic`), so it is+ # excluded from the "external providers can't be auto-disconnected" rule.if provider.get("flow") == "external" and provider.get("id") != "anthropic":if _oauth_provider_disconnect_command(provider):- # The GUI offers a one-click "run in terminal" path; this hint is the- # fallback wording for surfaces that only show text.+ # Fallback wording for surfaces without the one-click "run in terminal" path.return "Managed outside Hermes — run the disconnect command to remove it."return "Managed by that provider's CLI; remove it there."if status.get("source") == "env_var":@@ -604,34 +561,22 @@ def _oauth_provider_disconnect_hint(provider: Dict[str, Any], status: Dict[str,def _build_oauth_catalog() -> list[Dict[str, Any]]:"""Build the Accounts-tab provider list.- MEMBERSHIP is the union of:- 1. ``_OAUTH_PROVIDER_CATALOG`` — the explicit, hand-tuned cards that carry- bespoke flow / status_fn / cli_command (including the api-key Anthropic- PKCE card and the synthetic claude-code subscription row, which are not- catalog providers), and- 2. every accounts-tab provider in the unified ``provider_catalog()`` (the- ``hermes model`` universe) — so any OAuth/external provider added as a- plugin appears automatically, with sensible defaults, even if no- explicit card was written for it.-- The explicit catalog wins on metadata; the unified catalog guarantees we- never silently drop a provider the CLI picker offers. Order: explicit cards- first (their curated order), then any catalog-only providers appended in- ``hermes model`` order.+ Membership is the union of ``_OAUTH_PROVIDER_CATALOG`` (hand-tuned cards+ with bespoke flow / status_fn / cli_command, incl. the Anthropic PKCE card+ and the synthetic claude-code row) and every accounts-tab provider in the+ unified ``provider_catalog()``, so a plugin-added OAuth/external provider+ appears automatically. Explicit cards win on metadata and come first in+ curated order; catalog-only providers follow in ``hermes model`` order."""- from hermes_cli.web_server import _OAUTH_PROVIDER_CATALOGrows: list[Dict[str, Any]] = []seen: set[str] = set()- # 1. Explicit hand-tuned cards (authoritative metadata + curated order).for entry in _OAUTH_PROVIDER_CATALOG:if entry["id"] in seen:continueseen.add(entry["id"])rows.append(dict(entry))- # 2. Catalog accounts-providers not already covered — keeps the Accounts tab- # in lockstep with the `hermes model` universe (zero-edit for new plugins).try:from hermes_cli.provider_catalog import provider_catalogfor d in provider_catalog():@@ -656,25 +601,11 @@ def _build_oauth_catalog() -> list[Dict[str, Any]]:async def list_oauth_providers(profile: Optional[str] = None):"""Enumerate every OAuth-capable LLM provider with current status.- Response shape (per provider):- id stable identifier (used in DELETE path)- name human label- flow "device_code" | "external"- cli_command fallback CLI command for users to run manually- disconnect_command shell command that clears an external provider's- creds (run in the embedded terminal), else null- docs_url external docs/portal link for the "Learn more" link- status:- logged_in bool — currently has usable creds- source short slug ("hermes_pkce", "claude_code", ...)- source_label human-readable origin (file path, env var name)- token_preview last N chars of the token, never the full token- expires_at ISO timestamp string or null- has_refresh_token bool-- Membership is derived from the unified provider_catalog() so this stays in- sync with the `hermes model` picker; _OAUTH_OVERRIDES supplies per-provider- flow/status/cli metadata.+ Per provider: id (used in the DELETE path), name, flow+ ("device_code" | "external"), cli_command (manual fallback),+ disconnect_command (shell command for external providers, else null),+ docs_url, and status {logged_in, source slug, source_label, token_preview+ (last N chars, never the full token), expires_at, has_refresh_token}."""def _run():with _profile_scope(profile):@@ -698,6 +629,15 @@ async def list_oauth_providers(profile: Optional[str] = None):return await asyncio.to_thread(_run)+def _reject_if_not_disconnectable(provider: Dict[str, Any], status: Dict[str, Any]) -> None:+ disconnect_hint = _oauth_provider_disconnect_hint(provider, status)+ if disconnect_hint:+ raise HTTPException(+ status_code=400,+ detail=f"{provider['name']} cannot be disconnected automatically. {disconnect_hint}",+ )++@router.delete("/api/providers/oauth/{provider_id}")async def disconnect_oauth_provider(provider_id: str,@@ -718,24 +658,14 @@ async def disconnect_oauth_provider(f"Available: {', '.join(sorted(catalog_by_id))}",)- disconnect_hint = _oauth_provider_disconnect_hint(provider, {})- if disconnect_hint:- raise HTTPException(- status_code=400,- detail=f"{provider['name']} cannot be disconnected automatically. {disconnect_hint}",- )-- status = _resolve_provider_status(provider_id, provider.get("status_fn"))- disconnect_hint = _oauth_provider_disconnect_hint(provider, status)- if disconnect_hint:- raise HTTPException(- status_code=400,- detail=f"{provider['name']} cannot be disconnected automatically. {disconnect_hint}",- )+ _reject_if_not_disconnectable(provider, {})+ _reject_if_not_disconnectable(+ provider, _resolve_provider_status(provider_id, provider.get("status_fn"))+ )- # Anthropic clears only the Hermes-managed PKCE file and auth-store entry.- # The separate claude-code catalog row is external/read-only and rejected- # above so we never pretend to remove ~/.claude/* credentials owned by the CLI.+ # Anthropic clears only the Hermes-managed PKCE file and auth-store+ # entry; the external claude-code row was rejected above so we never+ # pretend to remove ~/.claude/* credentials owned by the CLI.if provider_id == "anthropic":cleared = Falsetry:@@ -746,7 +676,6 @@ async def disconnect_oauth_provider(cleared = Trueexcept Exception:pass- # Also clear the credential pool entry if present.try:from hermes_cli.auth import clear_provider_authcleared = clear_provider_auth("anthropic") or cleared@@ -769,43 +698,25 @@ async def disconnect_oauth_provider(return await asyncio.to_thread(_run)-# ----------------------------------------------------------------------------# OAuth Phase 2 — in-browser device-code flows-# ----------------------------------------------------------------------------#-# Anthropic previously had a dashboard-triggered PKCE flow here too (server-# generates a claude.ai/oauth/authorize URL, exchanges the code for tokens at-# the Anthropic token endpoint, persists them). It was removed: an unattended-# HTTP endpoint minting Claude Pro/Max subscription tokens outside Anthropic's-# own client sits on the wrong side of Anthropic's usage policies for OAuth-# credentials. The "anthropic" catalog entry is now flow == "external" and-# points at `hermes auth add anthropic` (terminal PKCE, unaffected) instead.-#-# Device code (Nous, OpenAI Codex):-# 1. POST /api/providers/oauth/{nous|openai-codex}/start-# → server hits provider's device-auth endpoint-# → gets { user_code, verification_url, device_code, interval, expires_in }-# → spawns background poller thread that polls the token endpoint-# every `interval` seconds until approved/expired-# → stores poll status in _oauth_sessions[session_id]-# → returns { session_id, flow: "device_code", user_code,-# verification_url, expires_in, poll_interval }-# 2. UI opens verification_url in a new tab and shows user_code.-# 3. UI polls GET /api/providers/oauth/{provider}/poll/{session_id}-# every 2s until status != "pending".-# 4. On "approved" the background thread has already saved creds; UI-# refreshes the providers list.-#-# Sessions are kept in-memory only (single-process FastAPI) and time out-# after 15 minutes. A periodic cleanup runs on each /start call to GC-# expired sessions so the dict doesn't grow without bound.+# In-browser device-code flows (Nous, OpenAI Codex, MiniMax, xAI):+# 1. POST /api/providers/oauth/{provider}/start hits the provider's+# device-auth endpoint, spawns a poller thread that polls the token+# endpoint every `interval` seconds, and returns+# {session_id, flow, user_code, verification_url, expires_in, poll_interval}.+# 2. UI opens verification_url and shows user_code, then polls+# GET /api/providers/oauth/{provider}/poll/{session_id} until status != "pending".+# 3. On "approved" the poller has already saved creds; UI refreshes the list.+# Anthropic has NO dashboard PKCE flow: an unattended endpoint minting Claude+# subscription tokens outside Anthropic's own client violates its OAuth usage+# policy; that card is flow == "external" (`hermes auth add anthropic`).+# Sessions are in-memory (single-process) and expire after 15 minutes; /start+# GCs expired ones so the dict is bounded._OAUTH_SESSION_TTL_SECONDS = 15 * 60def _gc_oauth_sessions() -> None:"""Drop expired sessions. Called opportunistically on /start."""- from hermes_cli.web_server import _oauth_sessions, _oauth_sessions_lockcutoff = time.time() - _OAUTH_SESSION_TTL_SECONDSwith _oauth_sessions_lock:stale = [sid for sid, sess in _oauth_sessions.items() if sess["created_at"] < cutoff]@@ -826,14 +737,12 @@ async def start_oauth_login(profile: Optional[str] = None,):"""Initiate an OAuth login flow. Token-protected."""- from hermes_cli.web_server import _OAUTH_PROVIDER_CATALOG_require_token(request)_gc_oauth_sessions()_validate_oauth_profile(profile)- valid = {p["id"] for p in _OAUTH_PROVIDER_CATALOG}- if provider_id not in valid:+ catalog_entry = next((p for p in _OAUTH_PROVIDER_CATALOG if p["id"] == provider_id), None)+ if catalog_entry is None:raise HTTPException(status_code=400, detail=f"Unknown provider {provider_id}")- catalog_entry = next(p for p in _OAUTH_PROVIDER_CATALOG if p["id"] == provider_id)if catalog_entry["flow"] == "external":raise HTTPException(status_code=400,@@ -868,13 +777,8 @@ async def poll_oauth_session(session_id: str,profile: Optional[str] = None,):- """Poll a session's status (no auth — read-only state).-- Shared by the device-code flows (Nous, OpenAI Codex, MiniMax, xAI).- Each surfaces progress through the same background-worker-updated- ``status`` field, so a single poll endpoint serves them all.- """- from hermes_cli.web_server import _oauth_sessions, _oauth_sessions_lock+ """Poll a session's status (no auth — read-only state). One endpoint serves+ every device-code flow: all report progress via the worker-updated ``status``."""_validate_oauth_profile(profile)requested_profile = _oauth_profile_name(profile)with _oauth_sessions_lock:@@ -901,13 +805,11 @@ async def cancel_oauth_session():"""Cancel a pending OAuth session. Token-protected.- Marks the session dict ``cancelled`` before popping it so any- background worker still holding a reference to that same dict (e.g.- the Codex device-code poller) observes the cancellation and stops- polling/exchanging/saving instead of completing the login after the- user believed it was aborted.+ Marks the session dict ``cancelled`` before popping it so a background+ worker still holding that dict (e.g. the Codex poller) stops+ polling/exchanging/saving instead of completing the login after the user+ believed it was aborted."""- from hermes_cli.web_server import _oauth_sessions, _oauth_sessions_lock_require_token(request)_validate_oauth_profile(profile)requested_profile = _oauth_profile_name(profile)