Skip to content

Self-improving AI Employees for everyone

refactor(cli_agent): adopt Stripe CLI-managed OAuth pattern

trohitg434b400a97d7
diff --git a/server/config/credential_providers.json b/server/config/credential_providers.json
index 4e787cca..c4e3e76c 100644
--- a/server/config/credential_providers.json
+++ b/server/config/credential_providers.json
@@ -161,12 +161,11 @@
"_abstract": true,
"category": "ai",
"category_label": "AI Providers",
- "kind": "native_cli",
- "_kind_doc": "native_cli: MachinaOs does not store the credential. We trigger the CLI's own login flow (e.g. `claude login`) and detect logged-in state. The CLI handles its own ~/.claude/, ~/.codex/, ~/.gemini/ token storage.",
+ "kind": "oauth",
+ "_kind_doc": "Stripe-style CLI-managed auth: MachinaOs spawns `<provider> login`, the CLI manages its own credentials (~/.claude/, ~/.codex/, ~/.gemini/), and we write a synthetic marker OAuth token on success so the catalogue's `stored` flag flips. OAuthConnect.tsx renders empty fields because `fields=[]` (see OAuthConnect.tsx:42 — the documented Stripe-style fieldless-CLI case).",
"fields": [],
"ws": {
- "login": "cli_login",
- "status": "cli_auth_status"
+ "login": "cli_login"
}
},
@@ -175,15 +174,8 @@
"name": "Claude Code CLI",
"color": "dracula.cyan",
"icon_ref": "lobehub:Claude",
- "ws_provider_arg": "claude",
- "actions": [
- { "key": "login", "label": "Login with Claude", "theme_color": "cyan" },
- { "key": "status", "label": "Check Status", "theme_color": "purple" }
- ],
- "status_rows": [
- { "key": "logged_in", "label": "Logged in", "ok_field": "logged_in", "true_text": "Yes", "false_text": "No" },
- { "key": "available", "label": "CLI installed", "ok_field": "available", "true_text": "Yes", "false_text": "Run `npm install -g @anthropic-ai/claude-code`" }
- ]
+ "status_hook": "claude_code",
+ "ws_provider_arg": "claude"
},
"codex_cli": {
@@ -191,15 +183,8 @@
"name": "Codex CLI",
"color": "dracula.orange",
"icon_ref": "lobehub:OpenAI",
- "ws_provider_arg": "codex",
- "actions": [
- { "key": "login", "label": "Login with OpenAI", "theme_color": "orange" },
- { "key": "status", "label": "Check Status", "theme_color": "purple" }
- ],
- "status_rows": [
- { "key": "logged_in", "label": "Logged in", "ok_field": "logged_in", "true_text": "Yes", "false_text": "No" },
- { "key": "available", "label": "CLI installed", "ok_field": "available", "true_text": "Yes", "false_text": "Run `npm install -g @openai/codex`" }
- ]
+ "status_hook": "codex_cli",
+ "ws_provider_arg": "codex"
},
"whatsapp_personal": {
diff --git a/server/services/cli_agent/__init__.py b/server/services/cli_agent/__init__.py
index 6970b53d..f10d1430 100644
--- a/server/services/cli_agent/__init__.py
+++ b/server/services/cli_agent/__init__.py
@@ -13,7 +13,7 @@ does not need to know about us by name.
from __future__ import annotations
-from services.cli_agent.cli_auth import check_auth, run_native_login
+from services.cli_agent.cli_auth import run_native_login
from services.cli_agent.factory import (
ALL_PROVIDERS,
SUPPORTED_PROVIDERS,
@@ -69,5 +69,4 @@ __all__ = [
"session_result_to_model",
# Native auth helpers (also used by the router for Credentials Modal)
"run_native_login",
- "check_auth",
]
diff --git a/server/services/cli_agent/_handlers.py b/server/services/cli_agent/_handlers.py
index 26151cbb..01dc5393 100644
--- a/server/services/cli_agent/_handlers.py
+++ b/server/services/cli_agent/_handlers.py
@@ -3,16 +3,23 @@
Self-registered into ``services.ws_handler_registry`` from
``services/cli_agent/__init__.py``. The router does not import this
module directly — it discovers handlers via the registry at dispatch
-time, mirroring the telegram / whatsapp plugin pattern.
+time, mirroring the telegram / whatsapp / stripe plugin pattern.
-Two generic handlers (one pair total — dispatched by the ``provider``
-field):
+Adopted from `nodes/stripe/_handlers.py` (CLI-managed OAuth pattern):
-- ``cli_login`` → spawns ``<provider> login``
-- ``cli_auth_status`` → probes whether the CLI is logged in
+ - On successful ``<provider> login``, write a synthetic ``"cli-managed"``
+ marker OAuth token via ``auth_service.store_oauth_tokens()``. The
+ catalogue handler's ``stored`` check (keyed off
+ ``status_hook``) flips ``true`` automatically — the existing
+ ``OAuthConnect.tsx`` primitive renders the modal as Connected.
+ - Broadcast ``credential_catalogue_updated`` so the frontend
+ invalidates its catalogue cache and re-fetches reactively.
+ - No ``cli_auth_status`` handler — redundant once ``stored`` drives
+ the modal.
-Both replace what would have been per-provider OAuth handlers
-(``codex_oauth_login`` etc.).
+Logout: drop the marker token + broadcast.
+
+The actual subprocess spawning lives in ``cli_auth.run_native_login``.
"""
from __future__ import annotations
@@ -21,7 +28,51 @@ from typing import Any, Awaitable, Callable, Dict
from fastapi import WebSocket
-from services.cli_agent.cli_auth import check_auth, run_native_login
+from core.logging import get_logger
+from services.cli_agent.cli_auth import run_native_login
+
+logger = get_logger(__name__)
+
+
+# Synthetic marker stored in `auth_service` after a successful CLI login,
+# matching `nodes/stripe/_handlers.py:_MARKER_TOKEN`. Lets the catalogue's
+# generic `stored` check flip without per-provider code in the catalogue
+# handler.
+_MARKER_TOKEN = "cli-managed"
+
+# Maps `cli_login.provider` field values to the credential-catalogue
+# provider keys that `auth_service` indexes its OAuth tokens under.
+# Aligns with the `status_hook` field in `credential_providers.json`.
+_PROVIDER_TO_CATALOGUE_KEY: Dict[str, str] = {
+ "claude": "claude_code",
+ "codex": "codex_cli",
+ "gemini": "gemini_cli",
+}
+
+
+async def _mark_logged_in(catalogue_key: str) -> None:
+ """Write the marker token via `auth_service.store_oauth_tokens()` —
+ same plumbing Google / Twitter / Stripe use after their OAuth flow."""
+ from core.container import container
+ await container.auth_service().store_oauth_tokens(
+ provider=catalogue_key,
+ access_token=_MARKER_TOKEN,
+ refresh_token=_MARKER_TOKEN,
+ )
+
+
+async def _mark_logged_out(catalogue_key: str) -> None:
+ from core.container import container
+ await container.auth_service().remove_oauth_tokens(catalogue_key)
+
+
+async def _broadcast_catalogue_updated() -> None:
+ """Generic catalogue-invalidation broadcast — same event the Stripe /
+ Google / Twitter handlers fire. Frontend listens in
+ ``WebSocketContext`` (``case 'credential_catalogue_updated'``) and
+ re-fetches the catalogue."""
+ from services.status_broadcaster import get_status_broadcaster
+ await get_status_broadcaster().broadcast({"type": "credential_catalogue_updated"})
async def handle_cli_login(
@@ -31,30 +82,59 @@ async def handle_cli_login(
"""Trigger the CLI's native login flow.
Payload: ``{provider: "claude" | "codex" | "gemini"}``.
- The CLI handles its own credential storage; we only spawn the flow.
+
+ On success: write the marker token via ``auth_service`` (so the
+ catalogue's ``stored`` flag flips) and broadcast
+ ``credential_catalogue_updated`` so the modal refreshes reactively.
+
+ The CLI itself stores its real credentials in ``~/.claude/``,
+ ``~/.codex/``, ``~/.gemini/`` — we never touch them.
"""
provider = (data.get("provider") or "").strip().lower()
if not provider:
return {"success": False, "error": "missing 'provider' field"}
- return await run_native_login(provider)
-
-async def handle_cli_auth_status(
+ result = await run_native_login(provider)
+ if result.get("success"):
+ catalogue_key = _PROVIDER_TO_CATALOGUE_KEY.get(provider)
+ if catalogue_key:
+ try:
+ await _mark_logged_in(catalogue_key)
+ await _broadcast_catalogue_updated()
+ except Exception as exc:
+ # Login itself succeeded; failing to mark/broadcast is a
+ # cosmetic/UI issue, not a user-visible login failure.
+ logger.warning(
+ "[cli_login:%s] mark/broadcast failed: %s", provider, exc,
+ )
+ return result
+
+
+async def handle_cli_logout(
data: Dict[str, Any],
websocket: WebSocket, # noqa: ARG001 — registry signature
) -> Dict[str, Any]:
- """Check whether the CLI is logged in.
+ """Drop the marker token + broadcast catalogue update.
- Payload: ``{provider: "claude" | "codex" | "gemini"}``.
- Returns ``{logged_in, available, hint, exit_code, error}``.
+ The actual CLI auth lives in the CLI's own ~/. config — the user
+ can run ``<provider> logout`` themselves to drop it. We just clear
+ our catalogue marker so the modal flips back to Disconnected.
"""
provider = (data.get("provider") or "").strip().lower()
if not provider:
- return {
- "logged_in": False, "available": False,
- "error": "missing 'provider' field",
- }
- return await check_auth(provider)
+ return {"success": False, "error": "missing 'provider' field"}
+
+ catalogue_key = _PROVIDER_TO_CATALOGUE_KEY.get(provider)
+ if not catalogue_key:
+ return {"success": False, "error": f"unknown provider {provider!r}"}
+
+ try:
+ await _mark_logged_out(catalogue_key)
+ await _broadcast_catalogue_updated()
+ except Exception as exc:
+ logger.warning("[cli_logout:%s] mark/broadcast failed: %s", provider, exc)
+ return {"success": False, "error": str(exc)}
+ return {"success": True}
# Registry payload — `services/cli_agent/__init__.py` registers these
@@ -63,5 +143,5 @@ WSHandler = Callable[[Dict[str, Any], WebSocket], Awaitable[Dict[str, Any]]]
WS_HANDLERS: Dict[str, WSHandler] = {
"cli_login": handle_cli_login,
- "cli_auth_status": handle_cli_auth_status,
+ "cli_logout": handle_cli_logout,
}
diff --git a/server/services/cli_agent/cli_auth.py b/server/services/cli_agent/cli_auth.py
index bca365e3..bae04c74 100644
--- a/server/services/cli_agent/cli_auth.py
+++ b/server/services/cli_agent/cli_auth.py
@@ -1,23 +1,23 @@
-"""Native CLI auth — login + status, no token wrapping.
+"""Native CLI login spawner.
Replaces what would have been per-provider OAuth modules
(``codex_oauth.py``, ``gemini_oauth.py``). Each CLI ships a working
``<provider> login`` flow that handles the browser/device flow and
stores credentials in its native location (``~/.claude/``,
-``~/.codex/``, ``~/.gemini/``). MachinaOs only triggers the flow and
-detects the resulting state.
-
-Two public coroutines:
-
-- :func:`run_native_login` — spawns ``<provider> login`` (interactive
- by default; the user completes the browser/device flow directly).
-- :func:`check_auth` — runs ``<provider> --version`` (or whatever
- ``auth_status_argv()`` returns), feeds output into
- ``provider.detect_auth_error()``, returns a ``{logged_in, available,
- hint, ...}`` payload for the Credentials Modal.
-
-Both share a single ``_resolve_argv`` helper that does the npx-shim
-fallback the providers' own ``binary_path()`` does.
+``~/.codex/``, ``~/.gemini/``). MachinaOs only triggers the flow.
+
+After ``run_native_login()`` succeeds, ``_handlers.py`` writes a
+synthetic marker OAuth token via ``auth_service.store_oauth_tokens()``
+(Stripe-style — see ``nodes/stripe/_handlers.py``). The catalogue's
+generic ``stored`` check then flips ``true`` and the existing
+``OAuthConnect.tsx`` primitive renders the modal as Connected — no
+per-provider status hook needed.
+
+There's no ``check_auth`` here: the marker-token + catalogue-broadcast
+pattern makes a separate status probe redundant. If you actually want to
+verify the CLI is functional (vs just "we marked you as logged in"), the
+first session attempt's stderr matcher (``provider.detect_auth_error``)
+catches it.
"""
from __future__ import annotations
@@ -35,7 +35,7 @@ logger = get_logger(__name__)
# ---------------------------------------------------------------------------
-# Shared argv resolution (binary or npx fallback)
+# Argv resolution (shared between login + the providers' own binary_path)
# ---------------------------------------------------------------------------
def _resolve_argv(
@@ -45,6 +45,12 @@ def _resolve_argv(
"""Resolve ``argv[0]`` to an absolute path or an npx shim.
Returns ``(resolved_argv, error)``. Either field is None.
+
+ NB: For npm-distributed CLIs (Claude / Codex / Gemini), the Stripe
+ GitHub-release auto-installer pattern doesn't apply — there are no
+ standalone binaries on GitHub releases. We fall back to ``npx`` which
+ auto-fetches the npm package on first use, mirroring what
+ ``provider.binary_path()`` already does in the providers themselves.
"""
if not argv:
return None, "empty argv"
@@ -65,19 +71,9 @@ def _build_provider(provider_name: str) -> Tuple[Optional[AICliProvider], Option
try:
return create_cli_provider(provider_name), None
except NotImplementedError as exc:
- return None, {
- "success": False, "logged_in": False, "available": False,
- "exit_code": None, "stderr_tail": "",
- "hint": "Provider deferred to v2",
- "error": str(exc),
- }
+ return None, {"success": False, "exit_code": None, "stderr_tail": "", "error": str(exc)}
except ValueError as exc:
- return None, {
- "success": False, "logged_in": False, "available": False,
- "exit_code": None, "stderr_tail": "",
- "hint": "Unknown provider",
- "error": str(exc),
- }
+ return None, {"success": False, "exit_code": None, "stderr_tail": "", "error": str(exc)}
# ---------------------------------------------------------------------------
@@ -151,102 +147,3 @@ async def run_native_login(
else f"login exited with code {proc.returncode}"
),
}
-
-
-# ---------------------------------------------------------------------------
-# Auth status
-# ---------------------------------------------------------------------------
-
-async def check_auth(
- provider_name: str,
- *,
- timeout_seconds: float = 15.0,
-) -> Dict[str, Any]:
- """Probe whether ``provider_name`` is logged in.
-
- ``available`` is False when the binary isn't installed.
- """
- provider, err = _build_provider(provider_name)
- if err is not None:
- return err
- assert provider is not None
-
- status_argv = provider.auth_status_argv()
- if not status_argv:
- # No cheap probe configured — defer to the first session's stderr.
- return {
- "logged_in": True, # optimistic; real run will surface errors
- "available": True,
- "hint": "",
- "exit_code": None,
- "error": None,
- }
-
- argv, resolve_err = _resolve_argv(provider, list(status_argv))
- if argv is None:
- return {
- "logged_in": False, "available": False,
- "hint": (
- f"Install the CLI: `npm install -g {provider.package_name}`"
- if provider.package_name
- else f"Install the {provider_name} CLI"
- ),
- "exit_code": None,
- "error": resolve_err,
- }
-
- logger.debug("[cli_auth_status:%s] probing %s", provider_name, argv)
-
- try:
- proc = await asyncio.create_subprocess_exec(
- *argv,
- stdout=asyncio.subprocess.PIPE,
- stderr=asyncio.subprocess.PIPE,
- )
- stdout, stderr = await asyncio.wait_for(
- proc.communicate(), timeout=timeout_seconds,
- )
- except asyncio.TimeoutError:
- return {
- "logged_in": False, "available": True,
- "hint": "auth status probe timed out",
- "exit_code": None,
- "error": f"timeout after {timeout_seconds:.0f}s",
- }
- except FileNotFoundError as exc:
- return {
- "logged_in": False, "available": False,
- "hint": f"Install the {provider_name} CLI",
- "exit_code": None,
- "error": str(exc),
- }
-
- stderr_text = stderr.decode("utf-8", errors="replace")
- stdout_text = stdout.decode("utf-8", errors="replace")
- combined = f"{stderr_text}\n{stdout_text}"
- exit_code = proc.returncode if proc.returncode is not None else -1
-
- if provider.detect_auth_error(combined, exit_code):
- login_hint = " ".join(provider.login_argv())
- return {
- "logged_in": False, "available": True,
- "hint": f"Run `{login_hint}` (or click Login in the Credentials Modal)",
- "exit_code": exit_code,
- "error": stderr_text[-500:].strip() or "not authenticated",
- }
-
- if exit_code != 0:
- # Non-zero exit but not an auth error.
- return {
- "logged_in": False, "available": True,
- "hint": f"Status probe failed (exit {exit_code})",
- "exit_code": exit_code,
- "error": stderr_text[-500:].strip() or "non-zero exit",
- }
-
- return {
- "logged_in": True, "available": True,
- "hint": "",
- "exit_code": 0,
- "error": None,
- }