From c847ab67c6af48869df09415599ab2cc6c8ad89f Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 22:21:52 -0700 Subject: [PATCH] refactor(simplex,teams): AST-neutral bracket layout compaction --- plugins/platforms/simplex/adapter.py | 34 ++++++----------- plugins/platforms/teams/adapter.py | 55 +++++++++------------------- 2 files changed, 29 insertions(+), 60 deletions(-) diff --git a/plugins/platforms/simplex/adapter.py b/plugins/platforms/simplex/adapter.py index 01d2c16828..c504abd161 100644 --- a/plugins/platforms/simplex/adapter.py +++ b/plugins/platforms/simplex/adapter.py @@ -121,8 +121,7 @@ class SimplexAdapter(BasePlatformAdapter): self._pending_text_batch_tasks: Dict[str, asyncio.Task] = {} logger.info( "SimpleX adapter initialized: url=%s auto_accept=%s groups=%s", - self.ws_url, self.auto_accept, "enabled" if self.group_allow_from else "disabled", - ) + self.ws_url, self.auto_accept, "enabled" if self.group_allow_from else "disabled") async def connect(self, *, is_reconnect: bool = False) -> bool: """Connect to the simplex-chat daemon and start the WebSocket listener.""" @@ -372,8 +371,7 @@ class SimplexAdapter(BasePlatformAdapter): source = self.build_source( chat_id=chat_id, chat_name=chat_name, chat_type="group" if is_group else "dm", - user_id=sender_id, user_name=sender_name or sender_id, - ) + user_id=sender_id, user_name=sender_name or sender_id) msg_type = MessageType.TEXT if media_types: if any(mt.startswith("audio/") for mt in media_types): @@ -389,8 +387,7 @@ class SimplexAdapter(BasePlatformAdapter): timestamp = datetime.now(tz=timezone.utc) msg_event = MessageEvent( source=source, text=text or "", message_type=msg_type, media_urls=media_urls, - media_types=media_types, timestamp=timestamp, raw_message=chat_item, - ) + media_types=media_types, timestamp=timestamp, raw_message=chat_item) logger.debug("SimpleX: message from %s in %s: %s", _redact_id(sender_id), chat_id[:20], (text or "")[:50]) if msg_type == MessageType.TEXT and text: # batch rapid-fire text into one combined message self._enqueue_text_event(msg_event) @@ -564,8 +561,7 @@ class SimplexAdapter(BasePlatformAdapter): return png_path, thumb_uri async def send_image( - self, chat_id: str, image_url: str, caption: Optional[str] = None, **kwargs - ) -> SendResult: + self, chat_id: str, image_url: str, caption: Optional[str] = None, **kwargs) -> SendResult: """Send an image. Supports ``file://`` URLs and ``http(s)://`` URLs.""" if image_url.startswith("file://"): file_path = unquote(image_url[7:]) @@ -584,22 +580,19 @@ class SimplexAdapter(BasePlatformAdapter): async def send_image_file( self, chat_id: str, image_path: str, caption: Optional[str] = None, - reply_to: Optional[str] = None, **kwargs, - ) -> SendResult: + reply_to: Optional[str] = None, **kwargs) -> SendResult: """Send a local image file via SimpleX.""" return await self.send_image(chat_id, f"file://{image_path}", caption=caption, **kwargs) async def send_video( self, chat_id: str, video_path: str, caption: Optional[str] = None, - reply_to: Optional[str] = None, **kwargs, - ) -> SendResult: + reply_to: Optional[str] = None, **kwargs) -> SendResult: """Send a video file via SimpleX (as a file attachment).""" return await self.send_document(chat_id, video_path, caption=caption) async def send_document( self, chat_id: str, file_path: str, caption: Optional[str] = None, - filename: Optional[str] = None, **kwargs, - ) -> SendResult: + filename: Optional[str] = None, **kwargs) -> SendResult: """Send a document/file attachment.""" if not Path(file_path).exists(): return SendResult(success=False, error="File not found") @@ -608,8 +601,7 @@ class SimplexAdapter(BasePlatformAdapter): async def send_voice( self, chat_id: str, audio_path: str, caption: Optional[str] = None, - reply_to: Optional[str] = None, duration: int = 0, **kwargs, - ) -> SendResult: + reply_to: Optional[str] = None, duration: int = 0, **kwargs) -> SendResult: """Send an audio file as an inline SimpleX voice note (``msgContent.type == "voice"``).""" if not Path(audio_path).exists(): return SendResult(success=False, error="Voice file not found") @@ -688,8 +680,7 @@ async def _standalone_send( try: payload = { "corrId": f"{_CORR_PREFIX}snd-{int(time.time() * 1000)}", - "cmd": _send_cmd(chat_id, [{"msgContent": {"type": "text", "text": message}}]), - } + "cmd": _send_cmd(chat_id, [{"msgContent": {"type": "text", "text": message}}])} async with _wsclient.connect(ws_url, open_timeout=10, close_timeout=5) as ws: await ws.send(json.dumps(payload)) await asyncio.sleep(0.5) # let the daemon process the command before closing @@ -703,8 +694,7 @@ def interactive_setup() -> None: print( "\nSimpleX Chat setup\n------------------\nRequirements:\n" " 1. simplex-chat daemon running (e.g. `simplex-chat -p 5225`).\n" - " 2. Python package `websockets` installed (`pip install websockets`).\n" - ) + " 2. Python package `websockets` installed (`pip install websockets`).\n") try: from hermes_cli.config import get_env_value, save_env_value except ImportError: @@ -760,6 +750,4 @@ def register(ctx) -> None: "hard message length limit, but keep responses conversational. " "You can attach native images, voice notes, and arbitrary " "files; the adapter handles MEDIA: tags by sending them " - "as inline voice notes (audio extensions) or documents." - ), - ) + "as inline voice notes (audio extensions) or documents.")) diff --git a/plugins/platforms/teams/adapter.py b/plugins/platforms/teams/adapter.py index b3eda47a32..df1c5fe68e 100644 --- a/plugins/platforms/teams/adapter.py +++ b/plugins/platforms/teams/adapter.py @@ -146,8 +146,7 @@ def _credentials(config) -> tuple[str, str, str]: return ( os.getenv("TEAMS_CLIENT_ID") or extra.get("client_id", ""), _get_scoped_secret("TEAMS_CLIENT_SECRET") or extra.get("client_secret", ""), - os.getenv("TEAMS_TENANT_ID") or extra.get("tenant_id", ""), - ) + os.getenv("TEAMS_TENANT_ID") or extra.get("tenant_id", "")) def validate_config(config) -> bool: @@ -253,8 +252,7 @@ _SDK_IMPORTS = { "microsoft_teams.api.models.adaptive_card": ("AdaptiveCardActionCardResponse", "AdaptiveCardActionMessageResponse"), "microsoft_teams.api.models.invoke_response": ("InvokeResponse", "AdaptiveCardInvokeResponse"), "microsoft_teams.apps.http.adapter": ("HttpMethod", "HttpRequest", "HttpResponse", "HttpRouteHandler"), - "microsoft_teams.cards": ("AdaptiveCard", "ExecuteAction", "TextBlock"), -} + "microsoft_teams.cards": ("AdaptiveCard", "ExecuteAction", "TextBlock")} @contextmanager @@ -307,8 +305,7 @@ _CHAT_TYPES = {"personal": "dm", "groupChat": "group", "channel": "channel"} # injection gates strictly on MessageType.DOCUMENT (same precedence as Email/Signal). _MEDIA_KIND_PRECEDENCE = ( ("document", MessageType.DOCUMENT), ("image", MessageType.PHOTO), - ("video", MessageType.VIDEO), ("audio", MessageType.AUDIO), -) + ("video", MessageType.VIDEO), ("audio", MessageType.AUDIO)) _APPROVAL_CHOICES = {"approve_once": "once", "approve_session": "session", "approve_always": "always", "deny": "deny"} _APPROVAL_LABELS = { "once": "✅ Allowed (once)", "session": "✅ Allowed (session)", "always": "✅ Always allowed", "deny": "❌ Denied", @@ -364,8 +361,7 @@ class TeamsAdapter(BasePlatformAdapter): f"microsoft-teams-apps could not be installed. Run: {pip} microsoft-teams-apps"), (not AIOHTTP_AVAILABLE, "MISSING_SDK", f"aiohttp not installed. Run: {pip} aiohttp"), (not self._client_id or not self._client_secret or not self._tenant_id, "MISSING_CREDENTIALS", - "TEAMS_CLIENT_ID, TEAMS_CLIENT_SECRET, and TEAMS_TENANT_ID are all required"), - ): + "TEAMS_CLIENT_ID, TEAMS_CLIENT_SECRET, and TEAMS_TENANT_ID are all required")): if failed: self._set_fatal_error(code, message, retryable=False) return False @@ -377,8 +373,7 @@ class TeamsAdapter(BasePlatformAdapter): self._app = App( client_id=self._client_id, client_secret=self._client_secret, tenant_id=self._tenant_id, http_server_adapter=_AiohttpBridgeAdapter(aiohttp_app), - client=ClientOptions(headers={"User-Agent": "Hermes"}), - ) + client=ClientOptions(headers={"User-Agent": "Hermes"})) # Handlers (ours, then plugin on_* decorators) must be wired before initialize(), # which registers POST /api/messages on aiohttp_app via the bridge's register_route(). @self._app.on_message @@ -401,8 +396,7 @@ class TeamsAdapter(BasePlatformAdapter): self._mark_connected() logger.info( "[teams] Webhook server listening on %s:%d%s", - self._host or "* (all interfaces, IPv4+IPv6)", self._port, _WEBHOOK_PATH, - ) + self._host or "* (all interfaces, IPv4+IPv6)", self._port, _WEBHOOK_PATH) return True except Exception as e: self._set_fatal_error("CONNECT_FAILED", f"Teams connection failed: {e}", retryable=True) @@ -489,8 +483,7 @@ class TeamsAdapter(BasePlatformAdapter): chat_type=_CHAT_TYPES.get(getattr(conv, "conversation_type", None) or "", "dm"), user_id=str(user_id), user_name=getattr(from_account, "name", None) or "", - guild_id=getattr(conv, "tenant_id", None) or self._tenant_id, - ) + guild_id=getattr(conv, "tenant_id", None) or self._tenant_id) media: list = [] # (path, media_type, kind) for att in getattr(activity, "attachments", None) or []: cached = await self._cache_attachment(att) @@ -500,8 +493,7 @@ class TeamsAdapter(BasePlatformAdapter): msg_type = next((t for kind, t in _MEDIA_KIND_PRECEDENCE if kind in media_kinds), MessageType.TEXT) await self.handle_message(MessageEvent( text=text, source=source, message_type=msg_type, message_id=msg_id, - media_urls=[path for path, _, _ in media], media_types=[mt for _, mt, _ in media], - )) + media_urls=[path for path, _, _ in media], media_types=[mt for _, mt, _ in media])) async def _cache_attachment(self, att: Any) -> Optional[tuple]: """Download + cache one inbound attachment → ``(path, media_type, kind)`` or ``None``.""" @@ -544,8 +536,7 @@ class TeamsAdapter(BasePlatformAdapter): if not cached: logger.warning( "[teams] Bot Framework attachment '%s' returned data that failed image validation, skipping", - att_name or content_url, - ) + att_name or content_url) return None return cached.path, cached.media_type, "image" path = await cache_image_from_url(content_url) @@ -606,8 +597,7 @@ class TeamsAdapter(BasePlatformAdapter): if not allowed_csv: logger.warning( "[teams] card action rejected: TEAMS_ALLOWED_USERS not configured " - "and TEAMS_ALLOW_ALL_USERS not set — default deny" - ) + "and TEAMS_ALLOW_ALL_USERS not set — default deny") return self._invoke_message("⛔ Approval buttons require TEAMS_ALLOWED_USERS to be configured.") from_account = ctx.activity.from_ clicker_id = getattr(from_account, "aad_object_id", None) or getattr(from_account, "id", "") @@ -629,8 +619,7 @@ class TeamsAdapter(BasePlatformAdapter): async def send_exec_approval( self, chat_id: str, command: str, session_key: str, description: str = "dangerous command", metadata: Optional[Dict[str, Any]] = None, allow_permanent: bool = True, allow_session: bool = True, - smart_denied: bool = False, - ) -> SendResult: + smart_denied: bool = False) -> SendResult: """Send an Adaptive Card approval prompt with Allow/Deny buttons.""" if not self._app: return SendResult(success=False, error="Teams app not initialized") @@ -722,8 +711,7 @@ class TeamsAdapter(BasePlatformAdapter): async def send_image( self, chat_id: str, image_url: str, caption: Optional[str] = None, reply_to: Optional[str] = None, - metadata: Optional[Dict[str, Any]] = None, - ) -> SendResult: + metadata: Optional[Dict[str, Any]] = None) -> SendResult: return await self._send_media_attachment(chat_id, image_url, "image/png", caption=caption, media_label="image") async def send_image_file( @@ -733,14 +721,12 @@ class TeamsAdapter(BasePlatformAdapter): async def send_video( self, chat_id: str, video_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None, - metadata: Optional[Dict[str, Any]] = None, **kwargs, - ) -> SendResult: + metadata: Optional[Dict[str, Any]] = None, **kwargs) -> SendResult: return await self._send_media_attachment(chat_id, video_path, "video/mp4", caption=caption, media_label="video") async def send_voice( self, chat_id: str, audio_path: str, caption: Optional[str] = None, reply_to: Optional[str] = None, - metadata: Optional[Dict[str, Any]] = None, **kwargs, - ) -> SendResult: + metadata: Optional[Dict[str, Any]] = None, **kwargs) -> SendResult: return await self._send_media_attachment(chat_id, audio_path, "audio/mpeg", caption=caption, media_label="voice") async def send_document( @@ -748,8 +734,7 @@ class TeamsAdapter(BasePlatformAdapter): reply_to: Optional[str] = None, metadata: Optional[Dict[str, Any]] = None, **kwargs, ) -> SendResult: return await self._send_media_attachment( - chat_id, file_path, "application/octet-stream", caption=caption, media_label="document" - ) + chat_id, file_path, "application/octet-stream", caption=caption, media_label="document") async def get_chat_info(self, chat_id: str) -> dict: return {"name": chat_id, "type": "unknown", "chat_id": chat_id} @@ -758,8 +743,7 @@ class TeamsAdapter(BasePlatformAdapter): _SETUP_CREDENTIALS = ( ("Client ID", "TEAMS_CLIENT_ID", {}), ("Client secret", "TEAMS_CLIENT_SECRET", {"password": True}), - ("Tenant ID", "TEAMS_TENANT_ID", {}), -) + ("Tenant ID", "TEAMS_TENANT_ID", {})) _SETUP_INTRO = ( "You'll need the Teams CLI. If you haven't already:", " npm install -g @microsoft/teams.cli@preview", @@ -770,8 +754,7 @@ _SETUP_INTRO = ( ' teams app create --name "Hermes" --endpoint "https:///api/messages"', "", "The CLI will print CLIENT_ID, CLIENT_SECRET, and TENANT_ID. Paste them below.", - "", -) + "") def interactive_setup() -> None: @@ -851,6 +834,4 @@ def register(ctx) -> None: "You are chatting via Microsoft Teams. Teams renders a subset of " "markdown — bold (**text**), italic (*text*), and inline code " "(`code`) work, but complex tables or raw HTML do not. Keep " - "responses clear and professional." - ), - ) + "responses clear and professional."))