Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(twitter): self-contained plugin folder
trohitg1a2107392bac
diff --git a/server/main.py b/server/main.pyindex 4f0d63db..6200c655 100644--- a/server/main.py+++ b/server/main.py@@ -40,7 +40,7 @@ from core.config import Settingsfrom core.logging import configure_logging, get_logger, setup_websocket_logging, shutdown_websocket_loggingfrom core.tracing import init_tracing_startup_log("Importing routers...")-from routers import workflow, database, maps, nodejs_compat, android, websocket, webhook, auth, twitter, google, credentials, schemas+from routers import workflow, database, maps, nodejs_compat, android, websocket, webhook, auth, google, credentials, schemas_startup_log("All imports complete")# Initialize settings, logging, and tracing.@@ -463,7 +463,8 @@ 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)-app.include_router(twitter.router) # Twitter/X OAuth routes+# 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)# Plugin-registered routers — populated by `nodes/<plugin>/__init__.py`diff --git a/server/nodes/twitter/__init__.py b/server/nodes/twitter/__init__.pyindex d1a2d507..5e4a0f2d 100644--- a/server/nodes/twitter/__init__.py+++ b/server/nodes/twitter/__init__.py@@ -1 +1,30 @@-"""Plugins for the 'twitter' palette group. See ../__init__.py for the package layout."""+"""Plugins for the 'twitter' palette group.++Self-contained plugin folder (Wave 11.H pattern). Owns:++- Action / trigger nodes — ``twitter_send.py``, ``twitter_search.py``,+ ``twitter_user.py``, ``twitter_receive.py`` (auto-registered via+ ``BaseNode.__init_subclass__``).+- ``_credentials.py`` — :class:`TwitterCredential` (OAuth2).+- ``_oauth.py`` — OAuth 2.0 PKCE flow client (formerly+ ``services/twitter_oauth.py``).+- ``_handlers.py`` — 3 WebSocket handlers+ (``twitter_oauth_login`` / ``twitter_oauth_status`` /+ ``twitter_logout``).+- ``_router.py`` — HTTP OAuth callback (``/api/twitter/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++# WebSocket handlers (3 message types) and the OAuth-callback HTTP+# router self-register so ``routers/websocket.py`` and ``main.py``+# stay free of per-plugin imports.+register_ws_handlers(WS_HANDLERS)+register_router(_router.router, name="twitter")diff --git a/server/nodes/twitter/_base.py b/server/nodes/twitter/_base.pyindex 10ec08fb..c5a568d3 100644--- a/server/nodes/twitter/_base.py+++ b/server/nodes/twitter/_base.py@@ -62,7 +62,7 @@ async def get_twitter_client():async def refresh_and_get_client():"""Refresh the OAuth2 token and return a new client."""from core.container import container- from services.twitter_oauth import TwitterOAuth+ from nodes.twitter._oauth import TwitterOAuthfrom xdk import Clientauth_service = container.auth_service()diff --git a/server/nodes/twitter/_credentials.py b/server/nodes/twitter/_credentials.pyindex a059f82f..e3e55a35 100644--- a/server/nodes/twitter/_credentials.py+++ b/server/nodes/twitter/_credentials.py@@ -4,7 +4,7 @@ Used by the four twitter plugins in this folder (twitter_send, twitter_search,twitter_user, twitter_receive).OAuth 2.0 with PKCE. The refresh flow is non-trivial (custom code exchange-in :mod:`services.twitter_oauth`), so :meth:`build_client` returns an+in :mod:`nodes.twitter._oauth`), so :meth:`build_client` returns anauthenticated XDK ``Client`` — that's what the four twitter pluginsactually hand to the XDK API methods. Use :func:`nodes.twitter._base.call_with_retry`on top; it transparently refreshes on 401/403 via this class.diff --git a/server/nodes/twitter/_handlers.py b/server/nodes/twitter/_handlers.pynew file mode 100644index 00000000..bae85323--- /dev/null+++ b/server/nodes/twitter/_handlers.py@@ -0,0 +1,179 @@+"""Twitter / X WebSocket handlers — plugin-owned dispatch table.++OAuth 2.0 PKCE login + status + logout flows. Self-registered into+the central WS dispatcher via ``register_ws_handlers(WS_HANDLERS)``+from this package's ``__init__.py``. ``routers/websocket.py`` knows+nothing about Twitter; the message-type strings are wired here so+renames / additions stay local to the plugin.++Companion: ``_router.py`` owns the OAuth callback endpoint+(``/api/twitter/callback``) — same redirect URI the WS login flow+asks the user to visit. Both share the same ``TwitterOAuth`` client+in ``_oauth.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 TwitterOAuth++logger = get_logger(__name__)+++async def handle_twitter_oauth_login(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Initiate Twitter OAuth 2.0 with PKCE flow.++ Opens the browser to Twitter's authorization page. After the user+ authorises, Twitter redirects to ``/api/twitter/callback`` (owned+ by ``_router.py``) which stores tokens via ``auth_service``.+ """+ # Lazy ``container`` import — same idiom the original handler used+ # in ``routers/websocket.py``. Keeps test monkeypatching simple+ # (the singleton lookup happens at call time, post-patch).+ from core.container import container++ auth_service = container.auth_service()++ # Stored client credentials (configured via Credentials Modal).+ client_id = await auth_service.get_api_key("twitter_client_id")+ client_secret = await auth_service.get_api_key("twitter_client_secret")++ if not client_id:+ return {+ "success": False,+ "error": "Twitter Client ID not configured. Add your Twitter API credentials first.",+ }++ redirect_uri = get_redirect_uri(websocket, "twitter")+ oauth = TwitterOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)+ auth_data = oauth.generate_authorization_url()++ return {+ "success": True,+ "message": "Opening Twitter authorization in browser...",+ "url": auth_data["url"],+ "state": auth_data["state"],+ }+++async def handle_twitter_oauth_status(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Check Twitter OAuth connection status.++ Returns connection status + user info if connected. Tokens live in+ the OAuth system (``EncryptedOAuthToken`` table); never read via+ ``get_api_key("twitter_access_token")`` — that's the wrong table.+ Refreshes silently if the access token is rejected and a refresh+ token exists.+ """+ from core.container import container++ auth_service = container.auth_service()++ tokens = await auth_service.get_oauth_tokens("twitter", customer_id="owner")+ if not tokens or not tokens.get("access_token"):+ return {"connected": False, "username": None, "user_id": None}++ access_token = tokens["access_token"]+ refresh_token = tokens.get("refresh_token", "")+ client_id = await auth_service.get_api_key("twitter_client_id") or ""+ client_secret = await auth_service.get_api_key("twitter_client_secret")+ redirect_uri = get_redirect_uri(websocket, "twitter")++ oauth = TwitterOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)+ user_info = await oauth.get_user_info(access_token)++ if not user_info.get("success") and refresh_token:+ refresh_result = await oauth.refresh_access_token(refresh_token)+ if refresh_result.get("success"):+ await auth_service.store_oauth_tokens(+ provider="twitter",+ access_token=refresh_result["access_token"],+ refresh_token=refresh_result.get("refresh_token") or refresh_token,+ email=tokens.get("email", ""),+ name=tokens.get("name", ""),+ scopes=tokens.get("scopes", ""),+ customer_id="owner",+ )+ user_info = await oauth.get_user_info(refresh_result["access_token"])++ if not user_info.get("success"):+ return {+ "connected": False,+ "username": None,+ "user_id": None,+ "error": user_info.get("error"),+ }++ return {+ "connected": True,+ "username": user_info.get("username"),+ "user_id": user_info.get("id"),+ "name": user_info.get("name"),+ "profile_image_url": user_info.get("profile_image_url"),+ "verified": user_info.get("verified"),+ }+++async def handle_twitter_logout(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:+ """Disconnect Twitter: revoke tokens + clear stored credentials.++ Per RFC 9700 the refresh-token is read directly from the DB+ (``get_oauth_refresh_token``) instead of from the in-memory cache.+ Symmetric ``credential.oauth.disconnected`` broadcast fires the+ catalogue refetch on every connected client.+ """+ from core.container import container++ auth_service = container.auth_service()++ tokens = await auth_service.get_oauth_tokens("twitter", customer_id="owner")+ access_token = tokens.get("access_token") if tokens else None+ refresh_token = (+ await auth_service.get_oauth_refresh_token("twitter", customer_id="owner")+ if tokens+ else None+ )++ client_id = await auth_service.get_api_key("twitter_client_id") or ""+ client_secret = await auth_service.get_api_key("twitter_client_secret")++ if access_token or refresh_token:+ redirect_uri = get_redirect_uri(websocket, "twitter")+ oauth = TwitterOAuth(client_id=client_id, client_secret=client_secret, redirect_uri=redirect_uri)+ if access_token:+ await oauth.revoke_token(access_token, "access_token")+ if refresh_token:+ await oauth.revoke_token(refresh_token, "refresh_token")++ await auth_service.remove_oauth_tokens("twitter", customer_id="owner")++ # Clean up any stale API-key entries from the old (broken) layout+ # where access tokens were mistakenly stored as API keys.+ for key in ("twitter_access_token", "twitter_refresh_token", "twitter_user_info"):+ try:+ await auth_service.remove_api_key(key)+ except Exception:+ pass++ broadcaster = get_status_broadcaster()+ await broadcaster.broadcast_credential_event(+ "credential.oauth.disconnected",+ provider="twitter",+ customer_id="owner",+ )++ return {"success": True, "message": "Twitter disconnected"}+++WS_HANDLERS = {+ "twitter_oauth_login": handle_twitter_oauth_login,+ "twitter_oauth_status": handle_twitter_oauth_status,+ "twitter_logout": handle_twitter_logout,+}diff --git a/server/services/twitter_oauth.py b/server/nodes/twitter/_oauth.pysimilarity index 100%rename from server/services/twitter_oauth.pyrename to server/nodes/twitter/_oauth.pydiff --git a/server/routers/twitter.py b/server/nodes/twitter/_router.pysimilarity index 99%rename from server/routers/twitter.pyrename to server/nodes/twitter/_router.pyindex 6506677f..97ced511 100644--- a/server/routers/twitter.py+++ b/server/nodes/twitter/_router.py@@ -21,7 +21,7 @@ from fastapi.responses import HTMLResponsefrom core.container import containerfrom core.logging import get_logger-from services.twitter_oauth import TwitterOAuth, get_pending_state+from nodes.twitter._oauth import TwitterOAuth, get_pending_statelogger = get_logger(__name__)router = APIRouter(prefix="/api/twitter", tags=["twitter"])diff --git a/server/routers/websocket.py b/server/routers/websocket.pyindex da5d16f4..ac95f44e 100644--- a/server/routers/websocket.py+++ b/server/routers/websocket.py@@ -1371,186 +1371,6 @@ async def handle_claude_oauth_status(data: Dict[str, Any], websocket: WebSocket)# here. See `services/cli_agent/__init__.py`.-# ============================================================================-# Twitter OAuth Handlers-# ============================================================================--@ws_handler()-async def handle_twitter_oauth_login(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:- """- Initiate Twitter OAuth 2.0 with PKCE flow.-- Opens browser to Twitter authorization page. After user authorizes,- Twitter redirects to /api/twitter/callback which stores tokens.- """- from services.twitter_oauth import TwitterOAuth-- auth_service = container.auth_service()-- # Get stored client credentials (configured via Credentials Modal)- client_id = await auth_service.get_api_key("twitter_client_id")- client_secret = await auth_service.get_api_key("twitter_client_secret")-- if not client_id:- return {- "success": False,- "error": "Twitter Client ID not configured. Add your Twitter API credentials first."- }-- # Create OAuth instance and generate authorization URL- from services.oauth_utils import get_redirect_uri- redirect_uri = get_redirect_uri(websocket, "twitter")-- oauth = TwitterOAuth(- client_id=client_id,- client_secret=client_secret,- redirect_uri=redirect_uri,- )-- auth_data = oauth.generate_authorization_url()-- return {- "success": True,- "message": "Opening Twitter authorization in browser...",- "url": auth_data["url"],- "state": auth_data["state"],- }---@ws_handler()-async def handle_twitter_oauth_status(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:- """- Check Twitter OAuth connection status.-- Returns connection status and user info if connected.- Uses OAuth token system (matches REST endpoint in routers/twitter.py).- """- from services.twitter_oauth import TwitterOAuth-- auth_service = container.auth_service()-- # Get tokens from OAuth system (NOT api_key system)- tokens = await auth_service.get_oauth_tokens("twitter", customer_id="owner")-- if not tokens or not tokens.get("access_token"):- return {- "connected": False,- "username": None,- "user_id": None,- }-- access_token = tokens["access_token"]- refresh_token = tokens.get("refresh_token", "")-- # Get client credentials for API calls (these are correctly stored as API keys)- client_id = await auth_service.get_api_key("twitter_client_id") or ""- client_secret = await auth_service.get_api_key("twitter_client_secret")-- from services.oauth_utils import get_redirect_uri- redirect_uri = get_redirect_uri(websocket, "twitter")-- oauth = TwitterOAuth(- client_id=client_id,- client_secret=client_secret,- redirect_uri=redirect_uri,- )-- # Verify token by getting user info- user_info = await oauth.get_user_info(access_token)-- if not user_info.get("success") and refresh_token:- refresh_result = await oauth.refresh_access_token(refresh_token)- if refresh_result.get("success"):- # Store refreshed tokens in OAuth system- await auth_service.store_oauth_tokens(- provider="twitter",- access_token=refresh_result["access_token"],- refresh_token=refresh_result.get("refresh_token") or refresh_token,- email=tokens.get("email", ""),- name=tokens.get("name", ""),- scopes=tokens.get("scopes", ""),- customer_id="owner",- )- # Retry user info- user_info = await oauth.get_user_info(refresh_result["access_token"])-- if not user_info.get("success"):- return {- "connected": False,- "username": None,- "user_id": None,- "error": user_info.get("error"),- }-- return {- "connected": True,- "username": user_info.get("username"),- "user_id": user_info.get("id"),- "name": user_info.get("name"),- "profile_image_url": user_info.get("profile_image_url"),- "verified": user_info.get("verified"),- }---@ws_handler()-async def handle_twitter_logout(data: Dict[str, Any], websocket: WebSocket) -> Dict[str, Any]:- """- Disconnect Twitter by revoking tokens and clearing stored credentials.- Uses OAuth token system (matches REST endpoint in routers/twitter.py).- """- from services.twitter_oauth import TwitterOAuth-- auth_service = container.auth_service()-- # Get tokens from OAuth system. refresh_token is read from DB- # directly (RFC 9700; not cached in memory).- tokens = await auth_service.get_oauth_tokens("twitter", customer_id="owner")- access_token = tokens.get("access_token") if tokens else None- refresh_token = await auth_service.get_oauth_refresh_token(- "twitter", customer_id="owner"- ) if tokens else None-- # Get client credentials (correctly stored as API keys)- client_id = await auth_service.get_api_key("twitter_client_id") or ""- client_secret = await auth_service.get_api_key("twitter_client_secret")-- # Revoke tokens if we have them- if access_token or refresh_token:- from services.oauth_utils import get_redirect_uri- redirect_uri = get_redirect_uri(websocket, "twitter")-- oauth = TwitterOAuth(- client_id=client_id,- client_secret=client_secret,- redirect_uri=redirect_uri,- )-- if access_token:- await oauth.revoke_token(access_token, "access_token")- if refresh_token:- await oauth.revoke_token(refresh_token, "refresh_token")-- # Clear from OAuth system- await auth_service.remove_oauth_tokens("twitter", customer_id="owner")-- # Clean up any stale API key entries (from old broken code)- for key in ["twitter_access_token", "twitter_refresh_token", "twitter_user_info"]:- try:- await auth_service.remove_api_key(key)- except Exception:- pass-- # Symmetric credential broadcast — flips the catalogue's `stored`- # flag for "twitter" on every connected client.- broadcaster = get_status_broadcaster()- await broadcaster.broadcast_credential_event(- "credential.oauth.disconnected",- provider="twitter",- customer_id="owner",- )-- return {"success": True, "message": "Twitter disconnected"}-# ============================================================================# Google Workspace OAuth Handlers@@ -3109,9 +2929,6 @@ MESSAGE_HANDLERS: Dict[str, MessageHandler] = {"claude_oauth_status": handle_claude_oauth_status,# Twitter OAuth operations- "twitter_oauth_login": handle_twitter_oauth_login,- "twitter_oauth_status": handle_twitter_oauth_status,- "twitter_logout": handle_twitter_logout,# Google Workspace OAuth operations"google_oauth_login": handle_google_oauth_login,diff --git a/server/tests/credentials/test_credential_broadcasts.py b/server/tests/credentials/test_credential_broadcasts.pyindex 51ca21e6..1bd95632 100644--- a/server/tests/credentials/test_credential_broadcasts.py+++ b/server/tests/credentials/test_credential_broadcasts.py@@ -98,9 +98,12 @@ class TestCredentialMutationHandlersBroadcast:)def test_twitter_logout_broadcasts(self):- from routers import websocket as ws_module+ # Twitter handler moved to ``nodes/twitter/_handlers.py`` as+ # part of the plugin-extraction migration. The invariant now+ # asserts on the plugin-owned source.+ from nodes.twitter._handlers import handle_twitter_logout- src = _handler_source(ws_module.handle_twitter_logout)+ src = _handler_source(handle_twitter_logout)assert _BROADCAST_PATTERN.search(src), ("handle_twitter_logout must broadcast credential.oauth.disconnected")diff --git a/server/tests/credentials/test_twitter_oauth.py b/server/tests/credentials/test_twitter_oauth.pyindex 2332a3bf..8af2a2f8 100644--- a/server/tests/credentials/test_twitter_oauth.py+++ b/server/tests/credentials/test_twitter_oauth.py@@ -16,8 +16,8 @@ import httpximport pytestimport respx-from services import twitter_oauth-from services.twitter_oauth import (+from nodes.twitter import _oauth as twitter_oauth+from nodes.twitter._oauth import (TwitterOAuth,TOKEN_URL,REVOKE_URL,diff --git a/server/tests/credentials/test_websocket_handlers.py b/server/tests/credentials/test_websocket_handlers.pyindex d2828275..16f2df4d 100644--- a/server/tests/credentials/test_websocket_handlers.py+++ b/server/tests/credentials/test_websocket_handlers.py@@ -219,14 +219,22 @@ class TestDeleteApiKey:class TestTwitterOAuthHandlers:+ # Twitter handlers moved to ``nodes/twitter/_handlers.py`` as part+ # of the plugin-extraction migration. The tests now import+ # directly from the plugin folder; the dispatch contract via+ # ``register_ws_handlers`` is exercised by+ # ``test_plugin_self_containment.py``.+async def test_login_fails_without_client_id(self, patched_container, fake_ws):- result = await _call(ws_module.handle_twitter_oauth_login, {}, fake_ws)+ from nodes.twitter._handlers import handle_twitter_oauth_login+ result = await _call(handle_twitter_oauth_login, {}, fake_ws)assert result["success"] is Falseassert "Client ID" in result["error"]async def test_login_returns_authorization_url(self, patched_container, fake_ws):+ from nodes.twitter._handlers import handle_twitter_oauth_loginawait patched_container.auth.store_api_key("twitter_client_id", "ci-test", models=[])@@ -234,25 +242,28 @@ class TestTwitterOAuthHandlers:"twitter_client_secret", "cs-test", models=[])- result = await _call(ws_module.handle_twitter_oauth_login, {}, fake_ws)+ result = await _call(handle_twitter_oauth_login, {}, fake_ws)assert result["success"] is Trueassert result["url"].startswith("https://x.com/i/oauth2/authorize")assert "state" in resultasync def test_status_when_disconnected(self, patched_container, fake_ws):- result = await _call(ws_module.handle_twitter_oauth_status, {}, fake_ws)+ from nodes.twitter._handlers import handle_twitter_oauth_status+ result = await _call(handle_twitter_oauth_status, {}, fake_ws)assert result["connected"] is Falseassert result["username"] is Noneasync def test_logout_clears_oauth_tokens(self, patched_container, fake_ws):- # Pre-populate OAuth tokens+ from nodes.twitter._handlers import handle_twitter_logoutawait patched_container.auth.store_oauth_tokens("twitter", "access", "refresh")- # Mock the revoke calls so we don't hit the network- with patch("services.twitter_oauth.TwitterOAuth.revoke_token", new=AsyncMock(return_value={"success": True})):- result = await _call(ws_module.handle_twitter_logout, {}, fake_ws)+ with patch(+ "nodes.twitter._oauth.TwitterOAuth.revoke_token",+ new=AsyncMock(return_value={"success": True}),+ ):+ result = await _call(handle_twitter_logout, {}, fake_ws)assert result["success"] is Trueassert await patched_container.auth.get_oauth_tokens("twitter") is Nonediff --git a/server/tests/nodes/test_twitter.py b/server/tests/nodes/test_twitter.pyindex 68aadc50..ffc267ed 100644--- a/server/tests/nodes/test_twitter.py+++ b/server/tests/nodes/test_twitter.py@@ -242,7 +242,7 @@ class TestTwitterSend:with _patch_client(stub_cls), patched_container(auth_oauth_tokens=_ok_tokens()), patched_pricing(), patch(- "services.twitter_oauth.TwitterOAuth"+ "nodes.twitter._oauth.TwitterOAuth") as oauth_cls:oauth_instance = MagicMock()oauth_instance.refresh_access_token = refresh_mock