Skip to content

Self-improving AI Employees for everyone

refactor(google): self-contained Workspace plugin folder

trohitg231d54e3ccd3
diff --git a/server/main.py b/server/main.py
index 6200c655..434bdc96 100644
--- a/server/main.py
+++ b/server/main.py
@@ -40,7 +40,7 @@ from core.config import Settings
from core.logging import configure_logging, get_logger, setup_websocket_logging, shutdown_websocket_logging
from core.tracing import init_tracing
_startup_log("Importing routers...")
-from routers import workflow, database, maps, nodejs_compat, android, websocket, webhook, auth, google, credentials, schemas
+from routers import workflow, database, maps, nodejs_compat, android, websocket, webhook, auth, credentials, schemas
_startup_log("All imports complete")
# Initialize settings, logging, and tracing.
@@ -463,9 +463,9 @@ app.include_router(schemas.router) # Per-node output schema endpoint (GET /api/
app.include_router(maps.router)
app.include_router(android.router)
app.include_router(webhook.router)
-# Twitter OAuth callback router moved to nodes/twitter/_router.py and
-# is mounted via the plugin-registered loop below.
-app.include_router(google.router) # Google Workspace OAuth routes (Gmail, Calendar, Drive, Sheets, Tasks, Contacts)
+# Twitter + Google OAuth callback routers moved to
+# nodes/twitter/_router.py and nodes/google/_router.py and are mounted
+# via the plugin-registered loop below.
# Plugin-registered routers — populated by `nodes/<plugin>/__init__.py`
# at import time via `register_router(...)`. Plugins are imported during
diff --git a/server/nodes/google/__init__.py b/server/nodes/google/__init__.py
index 4d59201b..b7fd63f9 100644
--- a/server/nodes/google/__init__.py
+++ b/server/nodes/google/__init__.py
@@ -1 +1,33 @@
-"""Plugins for the 'google' palette group. See ../__init__.py for the package layout."""
+"""Plugins for the 'google' palette group.
+
+Self-contained plugin folder (Wave 11.H pattern). One folder owns
+the entire Google Workspace surface:
+
+- 6 service plugins — ``gmail.py``, ``calendar.py``, ``drive.py``,
+ ``sheets.py``, ``tasks.py``, ``contacts.py``, plus
+ ``gmail_receive.py`` for the polling trigger.
+- ``_credentials.py`` — :class:`GoogleCredential` (OAuth2). The
+ 6 service plugins all reference this single credential class.
+- ``_oauth.py`` — OAuth 2.0 client (formerly
+ ``services/google_oauth.py``).
+- ``_auth_helper.py`` — shared credential builder + proactive
+ refresh (formerly ``services/handlers/google_auth.py``). Used by
+ every Workspace plugin to obtain a refresh-aware ``Credentials``
+ object.
+- ``_handlers.py`` — 3 WebSocket handlers
+ (``google_oauth_login`` / ``google_oauth_status`` /
+ ``google_logout``).
+- ``_router.py`` — HTTP OAuth callback (``/api/google/callback``).
+
+Two self-registration calls below — the central WS dispatcher and
+the FastAPI app pick up the plugin's surface without ever importing
+this module by name.
+"""
+
+from services.ws_handler_registry import register_router, register_ws_handlers
+
+from . import _router
+from ._handlers import WS_HANDLERS
+
+register_ws_handlers(WS_HANDLERS)
+register_router(_router.router, name="google")
diff --git a/server/services/handlers/google_auth.py b/server/nodes/google/_auth_helper.py
similarity index 100%
rename from server/services/handlers/google_auth.py
rename to server/nodes/google/_auth_helper.py
diff --git a/server/nodes/google/_credentials.py b/server/nodes/google/_credentials.py
index 262d997f..393faf38 100644
--- a/server/nodes/google/_credentials.py
+++ b/server/nodes/google/_credentials.py
@@ -7,7 +7,7 @@ to hand to ``googleapiclient.discovery.build``), so :meth:`build_credentials`
returns that object instead of an httpx request.
OAuth flow (authorization_url / token_url / scopes) is owned by
-:mod:`services.google_oauth`; this class is the plugin-facing interface
+:mod:`nodes.google._oauth`; this class is the plugin-facing interface
+ Credentials-modal metadata.
"""
@@ -30,7 +30,7 @@ class GoogleCredential(OAuth2Credential):
docs_url = "https://developers.google.com/workspace"
# Scope union across all 6 Google plugins. Single source of truth:
- # :data:`services.google_oauth.GOOGLE_WORKSPACE_SCOPES`, exposed lazily
+ # :data:`nodes.google._oauth.GOOGLE_WORKSPACE_SCOPES`, exposed lazily
# to avoid import-cycle risk during credential auto-discovery.
scopes: ClassVar[tuple] = ()
@@ -38,7 +38,7 @@ class GoogleCredential(OAuth2Credential):
def get_scopes(cls) -> tuple:
"""Return the live scope list (lazy import)."""
if not cls.scopes:
- from services.google_oauth import GOOGLE_WORKSPACE_SCOPES
+ from nodes.google._oauth import GOOGLE_WORKSPACE_SCOPES
cls.scopes = tuple(GOOGLE_WORKSPACE_SCOPES)
return cls.scopes
@@ -50,12 +50,12 @@ class GoogleCredential(OAuth2Credential):
):
"""Return a ``google.oauth2.credentials.Credentials`` object.
- Thin wrapper over :func:`services.handlers.google_auth.get_google_credentials`
+ Thin wrapper over :func:`nodes.google._auth_helper.get_google_credentials`
— the existing helper already handles owner-vs-customer token
sources + proactive refresh. Keeping the implementation there
(plus this indirection) means the plugin file's import surface
stays flat and there's a single place to evolve the refresh
policy.
"""
- from services.handlers.google_auth import get_google_credentials
+ from nodes.google._auth_helper import get_google_credentials
return await get_google_credentials(parameters, context)
diff --git a/server/nodes/google/_handlers.py b/server/nodes/google/_handlers.py
new file mode 100644
index 00000000..9cdf91cc
--- /dev/null
+++ b/server/nodes/google/_handlers.py
@@ -0,0 +1,149 @@
+"""Google Workspace WebSocket handlers — plugin-owned dispatch table.
+
+OAuth 2.0 flow for the 6 Google Workspace plugins (Gmail, Calendar,
+Drive, Sheets, Tasks, Contacts) — they share a single OAuth grant and
+a single ``GoogleCredential`` class. The HTTP callback that completes
+the flow lives in :mod:`._router` (mounted via ``register_router``);
+the live-state probe + logout flows are WebSocket handlers
+self-registered into the central WS dispatcher via
+:func:`register_ws_handlers` from this package's ``__init__.py``.
+"""
+
+from __future__ import annotations
+
+from typing import Any, Dict
+
+from fastapi import WebSocket
+
+from core.logging import get_logger
+from services.oauth_utils import get_redirect_uri
+from services.status_broadcaster import get_status_broadcaster
+
+from ._oauth import GoogleOAuth
+
+logger = get_logger(__name__)
+
+
+async def handle_google_oauth_login(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
+ """Initiate the Google Workspace OAuth 2.0 flow.
+
+ Opens the browser to the Google authorisation page; after consent
+ Google redirects to ``/api/google/callback`` (owned by
+ :mod:`._router`) which stores tokens via ``auth_service``. One
+ grant covers all 6 Workspace services.
+ """
+ from core.container import container
+
+ auth_service = container.auth_service()
+ client_id = await auth_service.get_api_key("google_client_id")
+ client_secret = await auth_service.get_api_key("google_client_secret")
+
+ if not client_id or not client_secret:
+ return {
+ "success": False,
+ "error": (
+ "Google Workspace Client ID and Secret not configured. "
+ "Add your Google API credentials first."
+ ),
+ }
+
+ redirect_uri = get_redirect_uri(websocket, "google")
+ oauth = GoogleOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)
+ auth_data = oauth.generate_authorization_url()
+
+ return {
+ "success": True,
+ "message": "Opening Google authorization in browser...",
+ "url": auth_data["url"],
+ "state": auth_data["state"],
+ }
+
+
+async def handle_google_oauth_status(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
+ """Check Google Workspace OAuth connection status.
+
+ Returns connection state + user info; proactively refreshes the
+ access token if a refresh token is on file (RFC 9700 — refresh
+ tokens are read directly from the DB, not the in-memory cache).
+ Mirrors the result into the broadcaster's status snapshot so
+ reconnecting clients pick up the current state.
+ """
+ from core.container import container
+
+ auth_service = container.auth_service()
+ broadcaster = get_status_broadcaster()
+
+ tokens = await auth_service.get_oauth_tokens("google", customer_id="owner")
+ if not tokens or not tokens.get("access_token"):
+ status = {"connected": False, "email": None, "name": None}
+ broadcaster._status["google"] = status
+ return status
+
+ email = tokens.get("email")
+ name = tokens.get("name")
+ refresh_token = await auth_service.get_oauth_refresh_token("google", customer_id="owner")
+
+ try:
+ client_id = await auth_service.get_api_key("google_client_id") or ""
+ client_secret = await auth_service.get_api_key("google_client_secret") or ""
+
+ if refresh_token and client_id and client_secret:
+ refreshed = GoogleOAuth.refresh_credentials(
+ refresh_token=refresh_token,
+ client_id=client_id,
+ client_secret=client_secret,
+ )
+ if refreshed.get("success") and refreshed.get("access_token"):
+ await auth_service.store_oauth_tokens(
+ provider="google",
+ access_token=refreshed["access_token"],
+ refresh_token=refresh_token,
+ email=email,
+ name=name,
+ customer_id="owner",
+ )
+
+ status = {"connected": True, "email": email, "name": name}
+ broadcaster._status["google"] = status
+ await broadcaster.broadcast({"type": "google_status", "data": status})
+ return status
+ except Exception as e: # noqa: BLE001 — log and return failure envelope
+ logger.warning(f"Google token validation failed: {e}")
+ status = {"connected": False, "email": None, "name": None, "error": str(e)}
+ broadcaster._status["google"] = {"connected": False, "email": None, "name": None}
+ return status
+
+
+async def handle_google_logout(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
+ """Disconnect Google Workspace and clear stored credentials.
+
+ Symmetric ``credential.oauth.disconnected`` broadcast fires the
+ catalogue refetch on every connected client (uniform contract with
+ twitter / telegram disconnect paths). The legacy
+ ``google_status`` broadcast is kept too so existing FE listeners
+ continue to update.
+ """
+ from core.container import container
+
+ auth_service = container.auth_service()
+ broadcaster = get_status_broadcaster()
+
+ await auth_service.remove_oauth_tokens("google", customer_id="owner")
+
+ cleared = {"connected": False, "email": None, "name": None}
+ broadcaster._status["google"] = cleared
+ await broadcaster.broadcast({"type": "google_status", "data": cleared})
+
+ await broadcaster.broadcast_credential_event(
+ "credential.oauth.disconnected",
+ provider="google",
+ customer_id="owner",
+ )
+ return {"success": True, "message": "Google Workspace disconnected"}
+
+
+WS_HANDLERS = {
+ "google_oauth_login": handle_google_oauth_login,
+ "google_oauth_status": handle_google_oauth_status,
+ "google_logout": handle_google_logout,
+}
diff --git a/server/services/google_oauth.py b/server/nodes/google/_oauth.py
similarity index 100%
rename from server/services/google_oauth.py
rename to server/nodes/google/_oauth.py
diff --git a/server/routers/google.py b/server/nodes/google/_router.py
similarity index 99%
rename from server/routers/google.py
rename to server/nodes/google/_router.py
index 8a595692..0b22f265 100644
--- a/server/routers/google.py
+++ b/server/nodes/google/_router.py
@@ -24,7 +24,7 @@ from fastapi.responses import HTMLResponse, RedirectResponse
from core.container import container
from core.logging import get_logger
-from services.google_oauth import GoogleOAuth, get_pending_state
+from nodes.google._oauth import GoogleOAuth, get_pending_state
logger = get_logger(__name__)
router = APIRouter(prefix="/api/google", tags=["google"])
diff --git a/server/routers/websocket.py b/server/routers/websocket.py
index ac95f44e..12d61f91 100644
--- a/server/routers/websocket.py
+++ b/server/routers/websocket.py
@@ -1372,148 +1372,6 @@ async def handle_claude_oauth_status(data: Dict[str, Any], websocket: WebSocket)
-# ============================================================================
-# Google Workspace OAuth Handlers
-# ============================================================================
-
-@ws_handler()
-async def handle_google_oauth_login(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
- """
- Initiate Google Workspace OAuth 2.0 flow.
-
- Opens browser to Google authorization page. After user authorizes,
- Google redirects to /api/google/callback which stores tokens.
- Grants access to all Google Workspace services (Gmail, Calendar, Drive, etc).
- """
- from services.google_oauth import GoogleOAuth
-
- auth_service = container.auth_service()
-
- client_id = await auth_service.get_api_key("google_client_id")
- client_secret = await auth_service.get_api_key("google_client_secret")
-
- if not client_id or not client_secret:
- return {
- "success": False,
- "error": "Google Workspace Client ID and Secret not configured. Add your Google API credentials first."
- }
-
- from services.oauth_utils import get_redirect_uri
- redirect_uri = get_redirect_uri(websocket, "google")
-
- oauth = GoogleOAuth(
- client_id=client_id,
- client_secret=client_secret,
- redirect_uri=redirect_uri,
- )
-
- auth_data = oauth.generate_authorization_url()
-
- return {
- "success": True,
- "message": "Opening Google authorization in browser...",
- "url": auth_data["url"],
- "state": auth_data["state"],
- }
-
-
-@ws_handler()
-async def handle_google_oauth_status(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
- """
- Check Google Workspace OAuth connection status.
-
- Returns connection status and user info if connected.
- Also updates broadcaster status so all clients stay in sync.
- """
- from services.google_oauth import GoogleOAuth
- from services.status_broadcaster import get_status_broadcaster
-
- auth_service = container.auth_service()
- broadcaster = get_status_broadcaster()
-
- tokens = await auth_service.get_oauth_tokens("google", customer_id="owner")
-
- if not tokens or not tokens.get("access_token"):
- status = {"connected": False, "email": None, "name": None}
- broadcaster._status["google"] = status
- return status
-
- email = tokens.get("email")
- name = tokens.get("name")
- # refresh_token is read from DB directly (RFC 9700; not cached).
- refresh_token = await auth_service.get_oauth_refresh_token(
- "google", customer_id="owner"
- )
-
- # Try to refresh token proactively
- try:
- client_id = await auth_service.get_api_key("google_client_id") or ""
- client_secret = await auth_service.get_api_key("google_client_secret") or ""
-
- if refresh_token and client_id and client_secret:
- refreshed = GoogleOAuth.refresh_credentials(
- refresh_token=refresh_token,
- client_id=client_id,
- client_secret=client_secret,
- )
- if refreshed.get("success") and refreshed.get("access_token"):
- await auth_service.store_oauth_tokens(
- provider="google",
- access_token=refreshed["access_token"],
- refresh_token=refresh_token,
- email=email,
- name=name,
- customer_id="owner",
- )
-
- status = {"connected": True, "email": email, "name": name}
- broadcaster._status["google"] = status
- # Broadcast so all connected clients update
- await broadcaster.broadcast({
- "type": "google_status",
- "data": status,
- })
- return status
- except Exception as e:
- logger.warning(f"Google token validation failed: {e}")
- status = {"connected": False, "email": None, "name": None, "error": str(e)}
- broadcaster._status["google"] = {"connected": False, "email": None, "name": None}
- return status
-
-
-@ws_handler()
-async def handle_google_logout(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
- """
- Disconnect Google Workspace by clearing stored credentials.
- """
- from services.status_broadcaster import get_status_broadcaster
-
- auth_service = container.auth_service()
- broadcaster = get_status_broadcaster()
-
- await auth_service.remove_oauth_tokens("google", customer_id="owner")
-
- # Clear broadcaster status and notify all clients
- broadcaster._status["google"] = {"connected": False, "email": None, "name": None}
- await broadcaster.broadcast({
- "type": "google_status",
- "data": {"connected": False, "email": None, "name": None},
- })
-
- # Symmetric credential broadcast — flips the catalogue's `stored`
- # flag for "google" on every connected client. The google_status
- # event above also triggers invalidateCatalogue on the frontend
- # ([WebSocketContext.tsx:722]) but the credential event keeps the
- # contract uniform with twitter / telegram disconnect paths.
- await broadcaster.broadcast_credential_event(
- "credential.oauth.disconnected",
- provider="google",
- customer_id="owner",
- )
-
- return {"success": True, "message": "Google Workspace disconnected"}
-
-
@ws_handler("url")
async def handle_test_ai_proxy(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:
"""Test connectivity to an AI proxy server."""
@@ -2931,9 +2789,6 @@ MESSAGE_HANDLERS: Dict[str, MessageHandler] = {
# Twitter OAuth operations
# Google Workspace OAuth operations
- "google_oauth_login": handle_google_oauth_login,
- "google_oauth_status": handle_google_oauth_status,
- "google_logout": handle_google_logout,
# Android operations
"get_android_devices": handle_get_android_devices,
diff --git a/server/services/node_option_loaders/google_loaders.py b/server/services/node_option_loaders/google_loaders.py
index 36dc1d1f..a0d4b2df 100644
--- a/server/services/node_option_loaders/google_loaders.py
+++ b/server/services/node_option_loaders/google_loaders.py
@@ -2,7 +2,7 @@
Wave 6 Phase 4b. Adds dynamic-option loaders for the Gmail label
picker and Calendar list picker. Reuses
-services/handlers/google_auth.get_google_credentials() so the OAuth
+nodes/google/_auth_helper.get_google_credentials() so the OAuth
dance is identical to the workflow-execution path.
Adding more Google loaders (e.g. Drive folder picker, Sheets
@@ -21,7 +21,7 @@ async def _google_service(api: str, version: str, params: dict[str, Any]):
from googleapiclient.discovery import build
- from services.handlers.google_auth import get_google_credentials
+ from nodes.google._auth_helper import get_google_credentials
creds = await get_google_credentials(params, {})
loop = asyncio.get_event_loop()
diff --git a/server/services/oauth_utils.py b/server/services/oauth_utils.py
index fe0dfdbd..4b0ca711 100644
--- a/server/services/oauth_utils.py
+++ b/server/services/oauth_utils.py
@@ -11,7 +11,7 @@ base URL (scheme + host + port) comes from the connection itself.
from urllib.parse import urlparse
-from services.google_oauth import get_callback_paths
+from nodes.google._oauth import get_callback_paths
def get_base_url(connection) -> str:
diff --git a/server/tests/credentials/test_credential_broadcasts.py b/server/tests/credentials/test_credential_broadcasts.py
index 1bd95632..1e377c9a 100644
--- a/server/tests/credentials/test_credential_broadcasts.py
+++ b/server/tests/credentials/test_credential_broadcasts.py
@@ -109,9 +109,9 @@ class TestCredentialMutationHandlersBroadcast:
)
def test_google_logout_broadcasts(self):
- from routers import websocket as ws_module
+ from nodes.google._handlers import handle_google_logout
- src = _handler_source(ws_module.handle_google_logout)
+ src = _handler_source(handle_google_logout)
assert _BROADCAST_PATTERN.search(src), (
"handle_google_logout must broadcast credential.oauth.disconnected"
)
diff --git a/server/tests/credentials/test_google_oauth.py b/server/tests/credentials/test_google_oauth.py
index dce1a2f1..8015f577 100644
--- a/server/tests/credentials/test_google_oauth.py
+++ b/server/tests/credentials/test_google_oauth.py
@@ -11,8 +11,8 @@ from urllib.parse import parse_qs, urlparse
import pytest
-from services import google_oauth
-from services.google_oauth import GoogleOAuth, get_callback_paths
+from nodes.google import _oauth as google_oauth
+from nodes.google._oauth import GoogleOAuth, get_callback_paths
pytestmark = pytest.mark.credentials
diff --git a/server/tests/credentials/test_oauth_utils.py b/server/tests/credentials/test_oauth_utils.py
index e0dffdc8..e5c0ba99 100644
--- a/server/tests/credentials/test_oauth_utils.py
+++ b/server/tests/credentials/test_oauth_utils.py
@@ -80,7 +80,7 @@ class TestGetRedirectUri:
def test_paths_loaded_from_config_not_hardcoded(self):
"""Smoke test: the real config must expose google + twitter paths."""
- from services.google_oauth import get_callback_paths
+ from nodes.google._oauth import get_callback_paths
paths = get_callback_paths()
assert "google" in paths
diff --git a/server/tests/credentials/test_websocket_handlers.py b/server/tests/credentials/test_websocket_handlers.py
index 16f2df4d..a863efd9 100644
--- a/server/tests/credentials/test_websocket_handlers.py
+++ b/server/tests/credentials/test_websocket_handlers.py
@@ -270,16 +270,23 @@ class TestTwitterOAuthHandlers:
class TestGoogleOAuthHandlers:
+ # Google OAuth handlers moved to ``nodes/google/_handlers.py`` as
+ # part of the plugin-extraction migration. Tests now import from
+ # the plugin folder; the dispatch contract is exercised by
+ # ``test_plugin_self_containment.py``.
+
async def test_login_fails_without_client_credentials(
self, patched_container, fake_ws
):
- result = await _call(ws_module.handle_google_oauth_login, {}, fake_ws)
+ from nodes.google._handlers import handle_google_oauth_login
+ result = await _call(handle_google_oauth_login, {}, fake_ws)
assert result["success"] is False
assert "Client ID" in result["error"]
async def test_login_returns_authorization_url(
self, patched_container, fake_ws
):
+ from nodes.google._handlers import handle_google_oauth_login
await patched_container.auth.store_api_key(
"google_client_id", "ci.apps.googleusercontent.com", models=[]
)
@@ -287,7 +294,7 @@ class TestGoogleOAuthHandlers:
"google_client_secret", "cs-test", models=[]
)
- result = await _call(ws_module.handle_google_oauth_login, {}, fake_ws)
+ result = await _call(handle_google_oauth_login, {}, fake_ws)
assert result["success"] is True
assert result["url"].startswith("https://accounts.google.com/o/oauth2/auth")
@@ -295,15 +302,16 @@ class TestGoogleOAuthHandlers:
assert "prompt=consent" in result["url"]
async def test_status_when_disconnected(self, patched_container, fake_ws):
- result = await _call(ws_module.handle_google_oauth_status, {}, fake_ws)
- # success may be auto-injected by the decorator; check connected=False
+ from nodes.google._handlers import handle_google_oauth_status
+ result = await _call(handle_google_oauth_status, {}, fake_ws)
assert result["connected"] is False
assert result["email"] is None
async def test_logout_removes_oauth_tokens(self, patched_container, fake_ws):
+ from nodes.google._handlers import handle_google_logout
await patched_container.auth.store_oauth_tokens(
"google", "access", "refresh", email="[email protected]"
)
- result = await _call(ws_module.handle_google_logout, {}, fake_ws)
+ result = await _call(handle_google_logout, {}, fake_ws)
assert result["success"] is True
assert await patched_container.auth.get_oauth_tokens("google") is None
diff --git a/server/tests/nodes/test_google_workspace.py b/server/tests/nodes/test_google_workspace.py
index a7f259dc..a73550d3 100644
--- a/server/tests/nodes/test_google_workspace.py
+++ b/server/tests/nodes/test_google_workspace.py
@@ -42,10 +42,10 @@ def _patch_creds(module_name: str, creds_return=None, side_effect=None):
By default returns a MagicMock that stands in for a Credentials instance.
Pass `side_effect=ValueError(...)` to simulate missing credentials.
"""
- # Scaling-branch: shared helper lives at services.handlers.google_auth.
+ # Scaling-branch: shared helper lives at nodes.google._auth_helper.
# `module_name` is retained for API symmetry but not part of the patch path.
_ = module_name
- target = "services.handlers.google_auth.get_google_credentials"
+ target = "nodes.google._auth_helper.get_google_credentials"
kwargs = {}
if side_effect is not None:
kwargs["side_effect"] = side_effect