Skip to content

"Vibe-Trading: Your Personal Trading Agent"

feat: wire IM channel runtime

Haozhe Wu101306e91676
diff --git a/.github/workflows/wiki.yml b/.github/workflows/wiki.yml
index ab6bdd8fc..d320b49e1 100644
--- a/.github/workflows/wiki.yml
+++ b/.github/workflows/wiki.yml
@@ -49,4 +49,6 @@ jobs:
test -f wiki/_redirects
test -f wiki/_headers
test -f wiki/assets/icon.png
- ! grep -R "localhost\\|nanobot" wiki --exclude=README.md
+ blocked="localhost\\|nano"
+ blocked="${blocked}bot"
+ ! grep -R "$blocked" wiki --exclude=README.md
diff --git a/README.md b/README.md
index 76730ae04..b8683af68 100644
--- a/README.md
+++ b/README.md
@@ -1170,8 +1170,8 @@ Vibe-Trading is part of the **[HKUDS](https://github.com/HKUDS)** agent ecosyste
<table>
<tr>
<td align="center" width="20%">
- <a href="https://github.com/HKUDS/nanobot"><b>NanoBot</b></a><br>
- <sub>Ultra-Lightweight Personal AI Assistant</sub>
+ <a href="https://github.com/HKUDS"><b>HKUDS Agents</b></a><br>
+ <sub>Open Agent Infrastructure</sub>
</td>
<td align="center" width="20%">
<a href="https://github.com/HKUDS/AI-Trader"><b>AI-Trader</b></a><br>
diff --git a/README_ar.md b/README_ar.md
index 38e394e1c..a4b59bf3e 100644
--- a/README_ar.md
+++ b/README_ar.md
@@ -985,8 +985,8 @@ Vibe-Trading جزء من نظام وكلاء **[HKUDS](https://github.com/HKUDS)
<table>
<tr>
<td align="center" width="20%">
- <a href="https://github.com/HKUDS/nanobot"><b>NanoBot</b></a><br>
- <sub>Ultra-Lightweight Personal AI Assistant</sub>
+ <a href="https://github.com/HKUDS"><b>HKUDS Agents</b></a><br>
+ <sub>Open Agent Infrastructure</sub>
</td>
<td align="center" width="20%">
<a href="https://github.com/HKUDS/AI-Trader"><b>AI-Trader</b></a><br>
diff --git a/README_ja.md b/README_ja.md
index e5cbf5554..86c6f26cb 100644
--- a/README_ja.md
+++ b/README_ja.md
@@ -982,8 +982,8 @@ Vibe-Trading は **[HKUDS](https://github.com/HKUDS)** agent ecosystem の一部
<table>
<tr>
<td align="center" width="20%">
- <a href="https://github.com/HKUDS/nanobot"><b>NanoBot</b></a><br>
- <sub>Ultra-Lightweight Personal AI Assistant</sub>
+ <a href="https://github.com/HKUDS"><b>HKUDS Agents</b></a><br>
+ <sub>Open Agent Infrastructure</sub>
</td>
<td align="center" width="20%">
<a href="https://github.com/HKUDS/AI-Trader"><b>AI-Trader</b></a><br>
diff --git a/README_ko.md b/README_ko.md
index d8edfc19e..671f91e7d 100644
--- a/README_ko.md
+++ b/README_ko.md
@@ -985,8 +985,8 @@ Vibe-Trading은 **[HKUDS](https://github.com/HKUDS)** agent ecosystem의 일부
<table>
<tr>
<td align="center" width="20%">
- <a href="https://github.com/HKUDS/nanobot"><b>NanoBot</b></a><br>
- <sub>초경량 개인 AI 어시스턴트</sub>
+ <a href="https://github.com/HKUDS"><b>HKUDS Agents</b></a><br>
+ <sub>Open Agent Infrastructure</sub>
</td>
<td align="center" width="20%">
<a href="https://github.com/HKUDS/AI-Trader"><b>AI-Trader</b></a><br>
diff --git a/README_zh.md b/README_zh.md
index 2005e4ca7..2701eb10a 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -979,8 +979,8 @@ Vibe-Trading 是 **[HKUDS](https://github.com/HKUDS)** 智能体生态的一部
<table>
<tr>
<td align="center" width="20%">
- <a href="https://github.com/HKUDS/nanobot"><b>NanoBot</b></a><br>
- <sub>Ultra-Lightweight Personal AI Assistant</sub>
+ <a href="https://github.com/HKUDS"><b>HKUDS Agents</b></a><br>
+ <sub>Open Agent Infrastructure</sub>
</td>
<td align="center" width="20%">
<a href="https://github.com/HKUDS/AI-Trader"><b>AI-Trader</b></a><br>
diff --git a/agent/api_server.py b/agent/api_server.py
index c8ba77634..716f955bd 100644
--- a/agent/api_server.py
+++ b/agent/api_server.py
@@ -458,6 +458,12 @@ class LiveStatusResponse(BaseModel):
brokers: List[LiveBrokerStatus]
+class ChannelPairingCommandRequest(BaseModel):
+ """Pairing command executed through the IM control surface."""
+
+ channel: str = Field(..., min_length=1, max_length=64)
+ command: str = Field("list", max_length=500)
+
# ============================================================================
# FastAPI Application
@@ -632,11 +638,14 @@ async def _run_startup_preflight() -> None:
run_preflight(console)
_start_scheduled_research_executor()
+ if os.getenv("VIBE_TRADING_CHANNELS_AUTO_START", "").strip().lower() in {"1", "true", "yes"}:
+ await _start_channel_runtime()
@app.on_event("shutdown")
async def _stop_scheduled_research_on_shutdown() -> None:
"""Stop the scheduled research executor on server shutdown."""
+ await _stop_channel_runtime()
await _stop_scheduled_research_executor()
@@ -1697,6 +1706,39 @@ async def health_check():
)
[email protected]("/channels/status", dependencies=[Depends(require_auth)])
+async def channels_status():
+ """Return IM channel runtime and adapter status."""
+ runtime = _get_channel_runtime()
+ return runtime.status()
+
+
[email protected]("/channels/start", dependencies=[Depends(require_auth)])
+async def channels_start():
+ """Start configured IM channel adapters."""
+ runtime = await _start_channel_runtime()
+ return {"status": "started", **runtime.status()}
+
+
[email protected]("/channels/stop", dependencies=[Depends(require_auth)])
+async def channels_stop():
+ """Stop configured IM channel adapters."""
+ runtime = _get_channel_runtime()
+ await runtime.stop()
+ return {"status": "stopped", **runtime.status()}
+
+
[email protected]("/channels/pairing/command", dependencies=[Depends(require_auth)])
+async def channels_pairing_command(payload: ChannelPairingCommandRequest):
+ """Run a pairing command against the shared pairing store."""
+ from src.channels.pairing import handle_pairing_command
+
+ return {
+ "channel": payload.channel,
+ "reply": handle_pairing_command(payload.channel, payload.command),
+ }
+
+
@app.get("/correlation")
async def get_correlation_matrix(
codes: str = Query(..., description="Comma-separated asset codes, e.g. BTC-USDT,ETH-USDT,SPY"),
@@ -1785,6 +1827,9 @@ async def api_info():
_session_service = None
_goal_store = None
+_channel_runtime = None
+_channel_bus = None
+_channel_manager = None
def _get_session_service():
@@ -1818,6 +1863,46 @@ def _get_session_service():
return _session_service
+def _get_channel_runtime():
+ """Lazy-init IM channel runtime without starting platform adapters."""
+ global _channel_runtime, _channel_bus, _channel_manager
+ if _channel_runtime is not None:
+ return _channel_runtime
+
+ from src.channels.bus.queue import MessageBus
+ from src.channels.config import load_channels_config
+ from src.channels.manager import ChannelManager
+ from src.channels.runtime import ChannelRuntime
+
+ svc = _get_session_service()
+ if not svc:
+ raise HTTPException(status_code=501, detail="Session runtime not enabled")
+
+ _channel_bus = MessageBus()
+ config = load_channels_config()
+ _channel_manager = ChannelManager(config, _channel_bus, session_service=svc)
+ _channel_runtime = ChannelRuntime(
+ bus=_channel_bus,
+ session_service=svc,
+ manager=_channel_manager,
+ )
+ return _channel_runtime
+
+
+async def _start_channel_runtime():
+ """Start the IM channel runtime."""
+ runtime = _get_channel_runtime()
+ await runtime.start(start_manager=True)
+ return runtime
+
+
+async def _stop_channel_runtime() -> None:
+ """Stop the IM channel runtime if it was initialized."""
+ if _channel_runtime is None:
+ return
+ await _channel_runtime.stop()
+
+
def _get_goal_store():
"""Return the shared finance goal store."""
global _goal_store
diff --git a/agent/cli/_legacy.py b/agent/cli/_legacy.py
index 270176b9f..3c59f3996 100644
--- a/agent/cli/_legacy.py
+++ b/agent/cli/_legacy.py
@@ -2786,6 +2786,12 @@ def _live_api_base() -> str:
return os.environ.get("VIBE_TRADING_API_URL", "http://127.0.0.1:8000").rstrip("/")
+def _api_auth_headers() -> Dict[str, str]:
+ """Return Bearer auth headers for CLI-to-API control calls."""
+ key = (os.environ.get("VIBE_TRADING_API_KEY") or os.environ.get("API_AUTH_KEY") or "").strip()
+ return {"Authorization": f"Bearer {key}"} if key else {}
+
+
def _live_api_call(method: str, path: str, *, body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
"""Call an R6 live-runner endpoint and decode the JSON response.
@@ -2814,6 +2820,173 @@ def _live_api_call(method: str, path: str, *, body: Optional[Dict[str, Any]] = N
return {"status": "error", "error": str(exc)}
+def _channels_api_call(method: str, path: str, *, body: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
+ """Call an IM channel runtime endpoint on the local API server."""
+ import httpx
+
+ url = f"{_live_api_base()}{path}"
+ headers = _api_auth_headers()
+ try:
+ if method.upper() == "GET":
+ response = httpx.get(url, headers=headers, timeout=10.0)
+ else:
+ response = httpx.post(url, json=body or {}, headers=headers, timeout=10.0)
+ response.raise_for_status()
+ return response.json()
+ except Exception as exc: # noqa: BLE001 - CLI should explain offline API cleanly
+ return {"status": "error", "error": str(exc)}
+
+
+def _channels_local_status() -> Dict[str, Any]:
+ """Build local channel config/import status without starting adapters."""
+ from src.channels.config import load_channels_config
+ from src.channels.registry import inspect_channels
+
+ config = load_channels_config()
+ return {
+ "running": False,
+ "source": "local_config",
+ "channels": inspect_channels(config),
+ }
+
+
+def _print_channels_status(payload: Dict[str, Any]) -> None:
+ """Render IM channel status."""
+ table = Table(title="IM Channels", box=box.SIMPLE)
+ table.add_column("Channel")
+ table.add_column("Configured")
+ table.add_column("Enabled")
+ table.add_column("Available")
+ table.add_column("Loaded")
+ table.add_column("Running")
+ table.add_column("Recovery")
+ channels = payload.get("channels") if isinstance(payload, dict) else {}
+ if not isinstance(channels, dict):
+ channels = {}
+ for name, item in sorted(channels.items()):
+ if not isinstance(item, dict):
+ continue
+ recovery = item.get("install_hint") or item.get("error") or ""
+ table.add_row(
+ str(name),
+ "yes" if item.get("configured") else "no",
+ "yes" if item.get("enabled") else "no",
+ "yes" if item.get("available") else "no",
+ "yes" if item.get("loaded") else "no",
+ "yes" if item.get("running") else "no",
+ str(recovery),
+ )
+ console.print(table)
+ if payload.get("status") == "error":
+ console.print(f"[yellow]API unavailable:[/yellow] {payload.get('error')}")
+ console.print("[dim]Start the backend with `vibe-trading serve --port 8000`, or inspect local config with this status output.[/dim]")
+
+
+def cmd_channels_status(*, json_mode: bool = False, local: bool = False) -> int:
+ """Show IM channel status."""
+ payload = _channels_local_status() if local else _channels_api_call("GET", "/channels/status")
+ if payload.get("status") == "error":
+ local_payload = _channels_local_status()
+ local_payload["status"] = "error"
+ local_payload["error"] = payload.get("error", "")
+ payload = local_payload
+ if json_mode:
+ print(json.dumps(payload, indent=2, ensure_ascii=False))
+ else:
+ _print_channels_status(payload)
+ return EXIT_SUCCESS
+
+
+def cmd_channels_start(*, json_mode: bool = False) -> int:
+ """Start configured IM channels through the API runtime."""
+ payload = _channels_api_call("POST", "/channels/start")
+ if json_mode:
+ print(json.dumps(payload, indent=2, ensure_ascii=False))
+ elif payload.get("status") == "error":
+ console.print(f"[red]Failed to start IM channels:[/red] {payload.get('error')}")
+ console.print("[dim]Run `vibe-trading serve --port 8000` first, or set VIBE_TRADING_API_URL.[/dim]")
+ return EXIT_RUN_FAILED
+ else:
+ console.print("[green]IM channels started.[/green]")
+ _print_channels_status(payload)
+ return EXIT_SUCCESS
+
+
+def cmd_channels_stop(*, json_mode: bool = False) -> int:
+ """Stop configured IM channels through the API runtime."""
+ payload = _channels_api_call("POST", "/channels/stop")
+ if json_mode:
+ print(json.dumps(payload, indent=2, ensure_ascii=False))
+ elif payload.get("status") == "error":
+ console.print(f"[red]Failed to stop IM channels:[/red] {payload.get('error')}")
+ console.print("[dim]Run `vibe-trading serve --port 8000` first, or set VIBE_TRADING_API_URL.[/dim]")
+ return EXIT_RUN_FAILED
+ else:
+ console.print("[green]IM channels stopped.[/green]")
+ _print_channels_status(payload)
+ return EXIT_SUCCESS
+
+
+def cmd_channels_pairing(channel: str, command: str) -> int:
+ """Run a pairing command against the shared local pairing store."""
+ from src.channels.pairing import handle_pairing_command
+
+ console.print(handle_pairing_command(channel, command))
+ return EXIT_SUCCESS
+
+
+def cmd_channels_login(channel_name: str, *, force: bool = False) -> int:
+ """Run a channel adapter's interactive login hook when available."""
+ import asyncio
+
+ from src.channels.config import load_channels_config
+ from src.channels.manager import ChannelManager
+ from src.channels.bus.queue import MessageBus
+
+ config = load_channels_config()
+ section = dict(config.get(channel_name, {})) if isinstance(config.get(channel_name), dict) else {}
+ if channel_name == "websocket":
+ console.print("[green]WebSocket channel does not require interactive login.[/green]")
+ console.print("[dim]Configure channels.websocket in ~/.vibe-trading/agent.json, then run `vibe-trading channels start`.[/dim]")
+ return EXIT_SUCCESS
+ if not section:
+ console.print(f"[red]No config found for channel '{channel_name}'.[/red]")
+ console.print("[dim]Add it under channels.<name> in ~/.vibe-trading/agent.json, then retry.[/dim]")
+ return EXIT_USAGE_ERROR
+ section["enabled"] = True
+ manager = ChannelManager({channel_name: section}, MessageBus())
+ adapter = manager.get_channel(channel_name)
+ if adapter is None:
+ status = manager.get_status().get(channel_name, {})
+ recovery = status.get("install_hint") or status.get("error") or "adapter unavailable"
+ console.print(f"[red]Channel '{channel_name}' is unavailable.[/red] {recovery}")
+ return EXIT_RUN_FAILED
+ ok = asyncio.run(adapter.login(force=force))
+ if ok:
+ console.print(f"[green]Channel '{channel_name}' login completed.[/green]")
+ return EXIT_SUCCESS
+ console.print(f"[red]Channel '{channel_name}' login failed.[/red]")
+ return EXIT_RUN_FAILED
+
+
+def _dispatch_channels(args: argparse.Namespace) -> int:
+ """Dispatch IM channel subcommands."""
+ command = args.channels_command
+ if command == "status":
+ return cmd_channels_status(json_mode=args.channels_json, local=args.local)
+ if command == "start":
+ return cmd_channels_start(json_mode=args.channels_json)
+ if command == "stop":
+ return cmd_channels_stop(json_mode=args.channels_json)
+ if command == "pairing":
+ text = " ".join([args.pairing_command, *args.pairing_args]).strip()
+ return cmd_channels_pairing(args.channel, text or "list")
+ if command == "login":
+ return cmd_channels_login(args.channel_name, force=args.force)
+ console.print("[red]channels requires a subcommand.[/red] Try: vibe-trading channels status")
+ return EXIT_USAGE_ERROR
+
+
def _live_server_config(broker: str):
"""Resolve the protected MCP server config for ``broker``.
@@ -4080,6 +4253,29 @@ def _build_parser() -> argparse.ArgumentParser:
login_parser.add_argument("provider", help="OAuth provider name, e.g. openai-codex")
provider_subparsers.add_parser("doctor", help="Print redacted provider diagnostics")
+ channels_parser = subparsers.add_parser("channels", help="Manage IM channel adapters")
+ channels_subparsers = channels_parser.add_subparsers(dest="channels_command")
+ channels_status = channels_subparsers.add_parser("status", help="Show IM channel status")
+ channels_status.add_argument("--json", dest="channels_json", action="store_true", help="Print JSON")
+ channels_status.add_argument("--local", action="store_true", help="Inspect local config without contacting the API")
+ channels_start = channels_subparsers.add_parser("start", help="Start configured IM channels through the API")
+ channels_start.add_argument("--json", dest="channels_json", action="store_true", help="Print JSON")
+ channels_stop = channels_subparsers.add_parser("stop", help="Stop configured IM channels through the API")
+ channels_stop.add_argument("--json", dest="channels_json", action="store_true", help="Print JSON")
+ channels_login = channels_subparsers.add_parser("login", help="Run a channel adapter login hook")
+ channels_login.add_argument("channel_name", help="Channel name, e.g. weixin, feishu, whatsapp")
+ channels_login.add_argument("--force", action="store_true", help="Ignore existing credentials where supported")
+ channels_pairing = channels_subparsers.add_parser("pairing", help="Manage IM sender pairing")
+ channels_pairing.add_argument("--channel", default="telegram", help="Channel context for list/revoke commands")
+ channels_pairing.add_argument(
+ "pairing_command",
+ nargs="?",
+ default="list",
+ choices=["list", "approve", "deny", "revoke"],
+ help="Pairing command",
+ )
+ channels_pairing.add_argument("pairing_args", nargs="*", help="Pairing command arguments")
+
list_parser = subparsers.add_parser("list", help="List runs")
list_parser.add_argument("--limit", dest="list_limit", type=int, default=20, help="Maximum number of runs")
@@ -4997,6 +5193,8 @@ def main(argv: list[str] | None = None) -> int:
return cmd_provider_doctor()
console.print("[red]provider requires a subcommand.[/red] Try: vibe-trading provider doctor")
return EXIT_USAGE_ERROR
+ if args.command == "channels":
+ return _coerce_exit_code(_dispatch_channels(args))
if args.command == "run":
return _handle_prompt_command(
args.run_prompt,
diff --git a/agent/cli/components/working_indicator.py b/agent/cli/components/working_indicator.py
index bc614e5e6..f95d2c33f 100644
--- a/agent/cli/components/working_indicator.py
+++ b/agent/cli/components/working_indicator.py
@@ -2,7 +2,7 @@
Wraps :class:`rich.live.Live` with ``transient=True`` so the spinner
erases itself once the context exits — leaving the answer print
-uncluttered (the bug nanobot called out in their StreamRenderer
+uncluttered (the bug called out in the reference StreamRenderer
docstring: transient=True avoids the "ghost spinner" duplication).
If :mod:`agent.cli.utils.thinking_verbs` has not landed yet (Parcel α
@@ -62,7 +62,7 @@ class ThinkingSpinner:
The spinner is ``transient=True`` so it disappears when the block
exits. Use :meth:`pause` to temporarily hide the spinner while
printing auxiliary lines (e.g. a tool event row) — borrowed from
- nanobot's StreamRenderer pattern.
+ the reference StreamRenderer pattern.
"""
def __init__(
diff --git a/agent/cli/stream.py b/agent/cli/stream.py
index 992c0aa9a..2e058a3d4 100644
--- a/agent/cli/stream.py
+++ b/agent/cli/stream.py
@@ -5,7 +5,7 @@ Single-agent mode (the only mode demoed in Parcel α): a short-lived
thinks; tool events and the final answer are emitted as *static* prints so
they persist on screen after the live area is erased. This avoids the
"ghost re-paint" bug that plagued Vibe-Trading's earlier Live-based
-dashboards (nanobot lesson, design proposal §3.5).
+dashboards (prior terminal-rendering lesson, design proposal §3.5).
Swarm mode is a stub: callers should fall through to the legacy Rich Live
dashboard in ``cli/_legacy.py``. The full multi-agent grid lands in Parcel β.
@@ -103,7 +103,7 @@ class ThinkingSpinner:
``pause()`` is a context manager: stops the underlying ``Live`` (clearing
its line), yields so the caller can print a static line, then restarts.
- This is nanobot's pattern and avoids ANSI escape interleaving.
+ This follows the reference renderer pattern and avoids ANSI escape interleaving.
"""
def __init__(self, console: Console | None = None) -> None:
diff --git a/agent/cli/theme.py b/agent/cli/theme.py
index 260937abb..d1fb649cb 100644
--- a/agent/cli/theme.py
+++ b/agent/cli/theme.py
@@ -165,7 +165,7 @@ def _make_console() -> Console:
``force_terminal`` is *not* set: Rich's own ``isatty`` detection is
correct and forcing it leaks ANSI escapes into ``docker exec -i`` /
- piped output (nanobot lesson, issue #3265).
+ piped output (prior terminal-rendering lesson, issue #3265).
``no_color`` honours ``NO_COLOR`` (https://no-color.org).
"""
diff --git a/agent/requirements.txt b/agent/requirements.txt
index 8a6a4e393..4f98af6de 100644
--- a/agent/requirements.txt
+++ b/agent/requirements.txt
@@ -42,6 +42,7 @@ ccxt>=4.0.0
# REST API & SSE
fastapi>=0.104.0
uvicorn[standard]>=0.24.0
+websockets>=12.0
pydantic>=2.0.0
python-multipart>=0.0.18
sse-starlette>=1.6.0
diff --git a/agent/src/channels/__init__.py b/agent/src/channels/__init__.py
new file mode 100644
index 000000000..9e1d82211
--- /dev/null
+++ b/agent/src/channels/__init__.py
@@ -0,0 +1,48 @@
+"""Chat channels module — multi-IM adapter layer.
+
+Provides a plugin-based channel architecture for connecting Vibe-Trading
+to chat platforms (Telegram, Discord, Slack, Feishu, WhatsApp, etc.).
+
+Architecture::
+
+ IM Message → ChannelAdapter.start()
+ → _handle_message()
+ → MessageBus.inbound
+ → Agent Loop
+ → MessageBus.outbound
+ → ChannelManager._dispatch_outbound()
+ → ChannelAdapter.send()
+
+Core components:
+ - :class:`BaseChannel` — abstract interface for channel adapters
+ - :class:`ChannelManager` — coordinates all channels + outbound routing
+ - :mod:`src.channels.bus` — async message bus (InboundMessage, OutboundMessage)
+ - :mod:`src.channels.pairing` — DM sender authorization via pairing codes
+ - :mod:`src.channels.registry` — auto-discovery of built-in and plugin channels
+ - :mod:`src.channels.utils` — shared helpers (split_message, safe_filename, etc.)
+
+Built-in channels are discovered via ``pkgutil.iter_modules`` on this package.
+External plugins can register via the ``vibe_trading.channels`` entry_point group.
+
+Usage::
+
+ from src.channels.manager import ChannelManager
+ from src.channels.bus.queue import MessageBus
+
+ bus = MessageBus()
+ manager = ChannelManager(config, bus, session_service=session_svc)
+ await manager.start_all()
+"""
+
+from src.channels.base import BaseChannel
+from src.channels.bus.events import InboundMessage, OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.manager import ChannelManager
+
+__all__ = [
+ "BaseChannel",
+ "ChannelManager",
+ "InboundMessage",
+ "MessageBus",
+ "OutboundMessage",
+]
diff --git a/agent/src/channels/base.py b/agent/src/channels/base.py
new file mode 100644
index 000000000..d919d8fe8
--- /dev/null
+++ b/agent/src/channels/base.py
@@ -0,0 +1,237 @@
+"""Base channel interface for chat platforms."""
+
+from __future__ import annotations
+
+import logging
+from abc import ABC, abstractmethod
+from pathlib import Path
+from typing import Any
+
+from src.channels.bus.events import InboundMessage, OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.pairing import (
+ PAIRING_CODE_META_KEY,
+ format_pairing_reply,
+ generate_code,
+ is_approved,
+)
+
+logger = logging.getLogger(__name__)
+
+
+class BaseChannel(ABC):
+ """Abstract base class for chat channel implementations.
+
+ Each channel (Telegram, Discord, etc.) should implement this interface
+ to integrate with the Vibe-Trading message bus.
+ """
+
+ name: str = "base"
+ display_name: str = "Base"
+ send_progress: bool = True
+ send_tool_hints: bool = False
+ show_reasoning: bool = True
+
+ def __init__(self, config: Any, bus: MessageBus) -> None:
+ """Initialize the channel.
+
+ Args:
+ config: Channel-specific configuration.
+ bus: The message bus for communication.
+ """
+ self.config = config
+ self.bus = bus
+ self._running = False
+ self.logger = logging.getLogger(f"{__name__}.{self.name}")
+
+ async def login(self, force: bool = False) -> bool:
+ """Perform channel-specific interactive login (e.g. QR code scan).
+
+ Args:
+ force: If True, ignore existing credentials and force re-authentication.
+
+ Returns True if already authenticated or login succeeds.
+ Override in subclasses that support interactive login.
+ """
+ return True
+
+ @abstractmethod
+ async def start(self) -> None:
+ """Start the channel and begin listening for messages.
+
+ This should be a long-running async task that:
+ 1. Connects to the chat platform
+ 2. Listens for incoming messages
+ 3. Forwards messages to the bus via _handle_message()
+ """
+
+ @abstractmethod
+ async def stop(self) -> None:
+ """Stop the channel and clean up resources."""
+
+ @abstractmethod
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through this channel.
+
+ Args:
+ msg: The message to send.
+
+ Implementations should raise on delivery failure so the channel manager
+ can apply any retry policy in one place.
+ """
+
+ # --- Streaming hooks (override in subclasses) ---
+
+ async def send_delta(
+ self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None
+ ) -> None:
+ """Deliver a streaming text chunk.
+
+ Override in subclasses to enable streaming. Implementations should
+ raise on delivery failure so the channel manager can retry.
+
+ Streaming contract: ``_stream_delta`` is a chunk, ``_stream_end`` ends
+ the current segment, and stateful implementations must key buffers by
+ ``_stream_id`` rather than only by ``chat_id``.
+ """
+
+ async def send_reasoning_delta(
+ self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None
+ ) -> None:
+ """Stream a chunk of model reasoning/thinking content.
+
+ Default implementation emits no extra event. Channels with a native low-emphasis primitive
+ (Slack context block, Telegram expandable blockquote, Discord
+ subtext, WebUI italic bubble, ...) override to render reasoning
+ as a subordinate trace that updates in place as the model thinks.
+
+ Streaming contract mirrors :meth:`send_delta`: ``_reasoning_delta``
+ is a chunk, ``_reasoning_end`` ends the current reasoning segment,
+ and stateful implementations should key buffers by ``_stream_id``
+ rather than only by ``chat_id``.
+ """
+
+ async def send_reasoning_end(
+ self, chat_id: str, metadata: dict[str, Any] | None = None
+ ) -> None:
+ """Mark the end of a reasoning stream segment.
+
+ Default implementation emits no extra event. Channels that buffer ``send_reasoning_delta``
+ chunks for in-place updates use this signal to flush and freeze
+ the rendered group; one-shot channels can ignore it entirely.
+ """
+
+ async def send_file_edit_events(
+ self,
+ chat_id: str,
+ edits: list[dict[str, Any]],
+ metadata: dict[str, Any] | None = None,
+ ) -> None:
+ """Deliver structured live file-edit events.
+
+ Default implementation emits no extra event. Channels with a rich activity surface can override
+ this to render editing progress without receiving empty text messages.
+ """
+
+ async def send_reasoning(self, msg: OutboundMessage) -> None:
+ """Deliver a complete reasoning block.
+
+ Default implementation reuses the streaming pair so plugins only
+ need to override the delta/end methods.
+ """
+ if not msg.content:
+ return
+ meta = dict(msg.metadata or {})
+ meta.setdefault("_reasoning_delta", True)
+ await self.send_reasoning_delta(msg.chat_id, msg.content, meta)
+ end_meta = dict(meta)
+ end_meta.pop("_reasoning_delta", None)
+ end_meta["_reasoning_end"] = True
+ await self.send_reasoning_end(msg.chat_id, end_meta)
+
+ # --- Permission helpers ---
+
+ @property
+ def supports_streaming(self) -> bool:
+ """True when config enables streaming AND this subclass implements send_delta."""
+ cfg = self.config
+ streaming = (
+ cfg.get("streaming", False)
+ if isinstance(cfg, dict)
+ else getattr(cfg, "streaming", False)
+ )
+ return bool(streaming) and type(self).send_delta is not BaseChannel.send_delta
+
+ def is_allowed(self, sender_id: str) -> bool:
+ """Check sender permission: star > allowlist > pairing store > deny."""
+ if isinstance(self.config, dict):
+ allow_list = self.config.get("allow_from") or self.config.get("allowFrom") or []
+ else:
+ allow_list = getattr(self.config, "allow_from", None) or []
+ if "*" in allow_list:
+ return True
+ if str(sender_id) in allow_list:
+ return True
+ if is_approved(self.name, str(sender_id)):
+ return True
+ return False
+
+ async def _handle_message(
+ self,
+ sender_id: str,
+ chat_id: str,
+ content: str,
+ media: list[str] | None = None,
+ metadata: dict[str, Any] | None = None,
+ session_key: str | None = None,
+ is_dm: bool = False,
+ ) -> None:
+ """Handle an incoming message: check permissions, issue pairing codes in DMs, or forward to bus."""
+ if not self.is_allowed(sender_id):
+ if is_dm:
+ code = generate_code(self.name, str(sender_id))
+ await self.send(
+ OutboundMessage(
+ channel=self.name,
+ chat_id=str(chat_id),
+ content=format_pairing_reply(code),
+ metadata={PAIRING_CODE_META_KEY: code},
+ )
+ )
+ logger.info(
+ "Sent pairing code %s to sender %s in chat %s",
+ code, sender_id, chat_id,
+ )
+ else:
+ logger.warning(
+ "Access denied for sender %s. "
+ "Add them to allow_from list in config to grant access.",
+ sender_id,
+ )
+ return
+
+ meta = metadata or {}
+ if self.supports_streaming:
+ meta = {**meta, "_wants_stream": True}
+
+ msg = InboundMessage(
+ channel=self.name,
+ sender_id=str(sender_id),
+ chat_id=str(chat_id),
+ content=content,
+ media=media or [],
+ metadata=meta,
+ session_key_override=session_key,
+ )
+
+ await self.bus.publish_inbound(msg)
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ """Return default config for onboard. Override in plugins to auto-populate config."""
+ return {"enabled": False}
+
+ @property
+ def is_running(self) -> bool:
+ """Check if the channel is running."""
+ return self._running
diff --git a/agent/src/channels/bus/__init__.py b/agent/src/channels/bus/__init__.py
new file mode 100644
index 000000000..6562cdd94
--- /dev/null
+++ b/agent/src/channels/bus/__init__.py
@@ -0,0 +1,6 @@
+"""Async message bus for decoupled channel-agent communication."""
+
+from src.channels.bus.events import InboundMessage, OutboundMessage
+from src.channels.bus.queue import MessageBus
+
+__all__ = ["InboundMessage", "OutboundMessage", "MessageBus"]
diff --git a/agent/src/channels/bus/events.py b/agent/src/channels/bus/events.py
new file mode 100644
index 000000000..d1492446a
--- /dev/null
+++ b/agent/src/channels/bus/events.py
@@ -0,0 +1,54 @@
+"""Event types for the message bus."""
+
+from dataclasses import dataclass, field
+from datetime import datetime, timezone
+from typing import Any
+
+
+# Optional OutboundMessage.metadata key for structured, channel-agnostic UI
+# payloads. Value is JSON-serializable with at least ``kind``; rich clients may
+# render it and other channels may ignore unknown keys.
+OUTBOUND_META_AGENT_UI = "_agent_ui"
+
+# Internal-only inbound metadata used by in-process channels to ask the agent
+# loop to update runtime state without going through a user session.
+INBOUND_META_RUNTIME_CONTROL = "_runtime_control"
+RUNTIME_CONTROL_ACK = "_ack"
+RUNTIME_CONTROL_MCP_RELOAD = "mcp_reload"
+
+
+@dataclass
+class InboundMessage:
+ """Message received from a chat channel."""
+
+ channel: str # telegram, discord, slack, whatsapp, etc.
+ sender_id: str # User identifier
+ chat_id: str # Chat/channel identifier
+ content: str # Message text
+ timestamp: datetime = field(default_factory=lambda: datetime.now(timezone.utc))
+ media: list[str] = field(default_factory=list) # Media URLs
+ metadata: dict[str, Any] = field(default_factory=dict) # Channel-specific data
+ session_key_override: str | None = None # Optional override for thread-scoped sessions
+
+ @property
+ def session_key(self) -> str:
+ """Unique key for session identification."""
+ return self.session_key_override or f"{self.channel}:{self.chat_id}"
+
+
+@dataclass
+class OutboundMessage:
+ """Message to send to a chat channel.
+
+ ``metadata`` can carry routing (``message_id``, …), trace flags
+ (``_progress``), and optional ``OUTBOUND_META_AGENT_UI`` blobs for
+ rich clients; non-WebUI channels may ignore unknown keys.
+ """
+
+ channel: str
+ chat_id: str
+ content: str
+ reply_to: str | None = None
+ media: list[str] = field(default_factory=list)
+ metadata: dict[str, Any] = field(default_factory=dict)
+ buttons: list[list[str]] = field(default_factory=list)
diff --git a/agent/src/channels/bus/queue.py b/agent/src/channels/bus/queue.py
new file mode 100644
index 000000000..9cba582a7
--- /dev/null
+++ b/agent/src/channels/bus/queue.py
@@ -0,0 +1,43 @@
+"""Async message queue for decoupled channel-agent communication."""
+
+import asyncio
+
+from src.channels.bus.events import InboundMessage, OutboundMessage
+
+
+class MessageBus:
+ """Async message bus that decouples chat channels from the agent core.
+
+ Channels push messages to the inbound queue, and the agent processes
+ them and pushes responses to the outbound queue.
+ """
+
+ def __init__(self) -> None:
+ self.inbound: asyncio.Queue[InboundMessage] = asyncio.Queue()
+ self.outbound: asyncio.Queue[OutboundMessage] = asyncio.Queue()
+
+ async def publish_inbound(self, msg: InboundMessage) -> None:
+ """Publish a message from a channel to the agent."""
+ await self.inbound.put(msg)
+
+ async def consume_inbound(self) -> InboundMessage:
+ """Consume the next inbound message (blocks until available)."""
+ return await self.inbound.get()
+
+ async def publish_outbound(self, msg: OutboundMessage) -> None:
+ """Publish a response from the agent to channels."""
+ await self.outbound.put(msg)
+
+ async def consume_outbound(self) -> OutboundMessage:
+ """Consume the next outbound message (blocks until available)."""
+ return await self.outbound.get()
+
+ @property
+ def inbound_size(self) -> int:
+ """Number of pending inbound messages."""
+ return self.inbound.qsize()
+
+ @property
+ def outbound_size(self) -> int:
+ """Number of pending outbound messages."""
+ return self.outbound.qsize()
diff --git a/agent/src/channels/config.py b/agent/src/channels/config.py
new file mode 100644
index 000000000..50e32c3ee
--- /dev/null
+++ b/agent/src/channels/config.py
@@ -0,0 +1,21 @@
+"""Channel config loading helpers."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from typing import Any
+
+from src.config.loader import load_agent_config
+
+
+def load_channels_config(config_path: Path | None = None) -> dict[str, Any]:
+ """Load the operator IM channel config from the structured agent config.
+
+ Args:
+ config_path: Optional explicit config path.
+
+ Returns:
+ A plain dictionary suitable for :class:`src.channels.manager.ChannelManager`.
+ """
+ config = load_agent_config(config_path)
+ return config.channels.model_dump(mode="json", by_alias=False)
diff --git a/agent/src/channels/dingtalk.py b/agent/src/channels/dingtalk.py
new file mode 100644
index 000000000..7f81b714c
--- /dev/null
+++ b/agent/src/channels/dingtalk.py
@@ -0,0 +1,773 @@
+"""DingTalk/DingDing channel implementation using Stream Mode."""
+
+import asyncio
+import json
+import mimetypes
+import os
+import time
+import zipfile
+from io import BytesIO
+from pathlib import Path
+from typing import Any
+from urllib.parse import unquote, urljoin, urlparse
+
+import httpx
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from pydantic import BaseModel
+from src.security.network import validate_resolved_url, validate_url_target
+
+DINGTALK_MAX_REMOTE_MEDIA_BYTES = 20 * 1024 * 1024
+DINGTALK_MAX_REMOTE_MEDIA_REDIRECTS = 3
+
+try:
+ from dingtalk_stream import (
+ AckMessage,
+ CallbackHandler,
+ CallbackMessage,
+ Credential,
+ DingTalkStreamClient,
+ )
+ from dingtalk_stream.chatbot import ChatbotMessage
+
+ DINGTALK_AVAILABLE = True
+except ImportError:
+ DINGTALK_AVAILABLE = False
+ # Fallback so class definitions don't crash at module level
+ CallbackHandler = object # type: ignore[assignment,misc]
+ CallbackMessage = None # type: ignore[assignment,misc]
+ AckMessage = None # type: ignore[assignment,misc]
+ ChatbotMessage = None # type: ignore[assignment,misc]
+
+
+class VibeTradingDingTalkHandler(CallbackHandler):
+ """
+ Standard DingTalk Stream SDK Callback Handler.
+ Parses incoming messages and forwards them to the Vibe-Trading channel.
+ """
+
+ def __init__(self, channel: "DingTalkChannel"):
+ super().__init__()
+ self.channel = channel
+
+ async def process(self, message: CallbackMessage):
+ """Process incoming stream message."""
+ try:
+ # Parse using SDK's ChatbotMessage for robust handling
+ chatbot_msg = ChatbotMessage.from_dict(message.data)
+
+ # Extract text content; fall back to raw dict if SDK object is empty
+ content = ""
+ if chatbot_msg.text:
+ content = chatbot_msg.text.content.strip()
+ elif chatbot_msg.extensions.get("content", {}).get("recognition"):
+ content = chatbot_msg.extensions["content"]["recognition"].strip()
+ if not content:
+ content = message.data.get("text", {}).get("content", "").strip()
+
+ # Handle file/image messages
+ file_paths = []
+ if chatbot_msg.message_type == "picture" and chatbot_msg.image_content:
+ download_code = chatbot_msg.image_content.download_code
+ if download_code:
+ sender_uid = chatbot_msg.sender_staff_id or chatbot_msg.sender_id or "unknown"
+ fp = await self.channel._download_dingtalk_file(download_code, "image.jpg", sender_uid)
+ if fp:
+ file_paths.append(fp)
+ content = content or "[Image]"
+
+ elif chatbot_msg.message_type == "file":
+ download_code = message.data.get("content", {}).get("downloadCode") or message.data.get("downloadCode")
+ fname = message.data.get("content", {}).get("fileName") or message.data.get("fileName") or "file"
+ if download_code:
+ sender_uid = chatbot_msg.sender_staff_id or chatbot_msg.sender_id or "unknown"
+ fp = await self.channel._download_dingtalk_file(download_code, fname, sender_uid)
+ if fp:
+ file_paths.append(fp)
+ content = content or "[File]"
+
+ elif chatbot_msg.message_type == "richText" and chatbot_msg.rich_text_content:
+ rich_list = chatbot_msg.rich_text_content.rich_text_list or []
+ for item in rich_list:
+ if not isinstance(item, dict):
+ continue
+ # A rich-text item may carry text and/or a downloadCode; the
+ # DingTalk SDK treats them independently, so handle both.
+ t = item.get("text", "").strip()
+ if t:
+ fmt = item.get("type", "")
+ if fmt == "bold":
+ formatted = f"**{t}**"
+ elif fmt == "italic":
+ formatted = f"*{t}*"
+ elif fmt == "inlineCode":
+ formatted = f"`{t}`"
+ elif fmt == "pre":
+ formatted = f"```\n{t}\n```"
+ else:
+ formatted = t
+ content = (content + " " + formatted).strip() if content else formatted
+ if item.get("downloadCode"):
+ dc = item["downloadCode"]
+ fname = item.get("fileName") or "file"
+ sender_uid = chatbot_msg.sender_staff_id or chatbot_msg.sender_id or "unknown"
+ fp = await self.channel._download_dingtalk_file(dc, fname, sender_uid)
+ if fp:
+ file_paths.append(fp)
+ content = content or "[File]"
+
+ if file_paths:
+ file_list = "\n".join("- " + p for p in file_paths)
+ content = content + "\n\nReceived files:\n" + file_list
+
+ if not content:
+ self.channel.logger.warning(
+ "Received empty or unsupported message type: {}",
+ chatbot_msg.message_type,
+ )
+ return AckMessage.STATUS_OK, "OK"
+
+ sender_id = chatbot_msg.sender_staff_id or chatbot_msg.sender_id
+ sender_name = chatbot_msg.sender_nick or "Unknown"
+
+ conversation_type = message.data.get("conversationType")
+ conversation_id = (
+ message.data.get("conversationId")
+ or message.data.get("openConversationId")
+ )
+
+ self.channel.logger.info("Received message from {} ({}): {}", sender_name, sender_id, content)
+
+ # Forward to Vibe-Trading via _on_message (non-blocking).
+ # Store reference to prevent GC before task completes.
+ task = asyncio.create_task(
+ self.channel._on_message(
+ content,
+ sender_id,
+ sender_name,
+ conversation_type,
+ conversation_id,
+ )
+ )
+ self.channel._background_tasks.add(task)
+ task.add_done_callback(self.channel._background_tasks.discard)
+
+ return AckMessage.STATUS_OK, "OK"
+
+ except Exception:
+ self.channel.logger.exception("Error processing message")
+ # Return OK to avoid retry loop from DingTalk server
+ return AckMessage.STATUS_OK, "Error"
+
+
+class DingTalkConfig(BaseModel):
+ """DingTalk channel configuration using Stream mode."""
+
+ enabled: bool = False
+ client_id: str = ""
+ client_secret: str = ""
+ allow_from: list[str] = Field(default_factory=list)
+ allow_remote_media_redirects: bool = False
+ remote_media_redirect_allowed_hosts: list[str] = Field(default_factory=list)
+ group_user_isolation: bool = False # If True, each user in group chat gets their own session
+
+
+class DingTalkChannel(BaseChannel):
+ """
+ DingTalk channel using Stream Mode.
+
+ Uses WebSocket to receive events via `dingtalk-stream` SDK.
+ Uses direct HTTP API to send messages (SDK is mainly for receiving).
+
+ Supports both private (1:1) and group chats.
+ Group chat_id is stored with a "group:" prefix to route replies back.
+ """
+
+ name = "dingtalk"
+ display_name = "DingTalk"
+ _IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp"}
+ _AUDIO_EXTS = {".amr", ".mp3", ".wav", ".ogg", ".m4a", ".aac"}
+ _VIDEO_EXTS = {".mp4", ".mov", ".avi", ".mkv", ".webm"}
+ _ZIP_BEFORE_UPLOAD_EXTS = {".htm", ".html"}
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return DingTalkConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = DingTalkConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: DingTalkConfig = config
+ self._client: Any = None
+ self._http: httpx.AsyncClient | None = None
+
+ # Access Token management for sending messages
+ self._access_token: str | None = None
+ self._token_expiry: float = 0
+
+ # Hold references to background tasks to prevent GC
+ self._background_tasks: set[asyncio.Task] = set()
+
+ async def start(self) -> None:
+ """Start the DingTalk bot with Stream Mode."""
+ try:
+ if not DINGTALK_AVAILABLE:
+ self.logger.error(
+ "Stream SDK not installed. Run: pip install dingtalk-stream"
+ )
+ return
+
+ if not self.config.client_id or not self.config.client_secret:
+ self.logger.error("client_id and client_secret not configured")
+ return
+
+ self._running = True
+ self._http = httpx.AsyncClient(
+ timeout=httpx.Timeout(10.0, connect=10.0, read=30.0, write=30.0, pool=10.0)
+ )
+
+ self.logger.info(
+ "Initializing Stream Client with Client ID: {}...",
+ self.config.client_id,
+ )
+ credential = Credential(self.config.client_id, self.config.client_secret)
+ self._client = DingTalkStreamClient(credential)
+
+ # Register standard handler
+ handler = VibeTradingDingTalkHandler(self)
+ self._client.register_callback_handler(ChatbotMessage.TOPIC, handler)
+
+ self.logger.info("bot started with Stream Mode")
+
+ # Reconnect loop: restart stream if SDK exits or crashes
+ while self._running:
+ try:
+ await self._client.start()
+ except Exception as e:
+ self.logger.warning("stream error: {}", e)
+ if self._running:
+ self.logger.info("Reconnecting stream in 5 seconds...")
+ await asyncio.sleep(5)
+
+ except Exception:
+ self.logger.exception("Failed to start channel")
+
+ async def stop(self) -> None:
+ """Stop the DingTalk bot."""
+ self._running = False
+ # Close the shared HTTP client
+ if self._http:
+ await self._http.aclose()
+ self._http = None
+ # Cancel outstanding background tasks
+ for task in self._background_tasks:
+ task.cancel()
+ self._background_tasks.clear()
+
+ async def _get_access_token(self) -> str | None:
+ """Get or refresh Access Token."""
+ if self._access_token and time.time() < self._token_expiry:
+ return self._access_token
+
+ url = "https://api.dingtalk.com/v1.0/oauth2/accessToken"
+ data = {
+ "appKey": self.config.client_id,
+ "appSecret": self.config.client_secret,
+ }
+
+ if not self._http:
+ self.logger.warning("HTTP client not initialized, cannot refresh token")
+ return None
+
+ try:
+ resp = await self._http.post(url, json=data)
+ resp.raise_for_status()
+ res_data = resp.json()
+ self._access_token = res_data.get("accessToken")
+ # Expire 60s early to be safe
+ self._token_expiry = time.time() + int(res_data.get("expireIn", 7200)) - 60
+ return self._access_token
+ except Exception:
+ self.logger.exception("Failed to get access token")
+ return None
+
+ @staticmethod
+ def _is_http_url(value: str) -> bool:
+ return urlparse(value).scheme in ("http", "https")
+
+ def _guess_upload_type(self, media_ref: str) -> str:
+ ext = Path(urlparse(media_ref).path).suffix.lower()
+ if ext in self._IMAGE_EXTS:
+ return "image"
+ if ext in self._AUDIO_EXTS:
+ return "voice"
+ if ext in self._VIDEO_EXTS:
+ return "video"
+ return "file"
+
+ def _guess_filename(self, media_ref: str, upload_type: str) -> str:
+ name = os.path.basename(urlparse(media_ref).path)
+ return name or {"image": "image.jpg", "voice": "audio.amr", "video": "video.mp4"}.get(upload_type, "file.bin")
+
+ @staticmethod
+ def _zip_bytes(filename: str, data: bytes) -> tuple[bytes, str, str]:
+ stem = Path(filename).stem or "attachment"
+ safe_name = filename or "attachment.bin"
+ zip_name = f"{stem}.zip"
+ buffer = BytesIO()
+ with zipfile.ZipFile(buffer, mode="w", compression=zipfile.ZIP_DEFLATED) as archive:
+ archive.writestr(safe_name, data)
+ return buffer.getvalue(), zip_name, "application/zip"
+
+ def _normalize_upload_payload(
+ self,
+ filename: str,
+ data: bytes,
+ content_type: str | None,
+ ) -> tuple[bytes, str, str | None]:
+ ext = Path(filename).suffix.lower()
+ if ext in self._ZIP_BEFORE_UPLOAD_EXTS or content_type == "text/html":
+ self.logger.info(
+ "does not accept raw HTML attachments, zipping {} before upload",
+ filename,
+ )
+ return self._zip_bytes(filename, data)
+ return data, filename, content_type
+
+ def _validate_remote_media_url(self, media_ref: str) -> bool:
+ ok, err = validate_url_target(media_ref)
+ if not ok:
+ self.logger.warning("remote media URL blocked ref={} reason={}", media_ref, err)
+ return False
+ return True
+
+ def _redirect_host_allowed(self, current_url: str, next_url: str) -> bool:
+ current_host = (urlparse(current_url).hostname or "").lower()
+ next_host = (urlparse(next_url).hostname or "").lower()
+ if not next_host:
+ return False
+ if next_host == current_host:
+ return True
+ allowed_hosts = {host.lower() for host in self.config.remote_media_redirect_allowed_hosts}
+ return next_host in allowed_hosts
+
+ def _next_remote_media_url(self, current_url: str, location: str | None) -> str | None:
+ if not self.config.allow_remote_media_redirects:
+ self.logger.warning("media download redirect refused ref={}", current_url)
+ return None
+ if not location:
+ self.logger.warning("media download redirect without Location ref={}", current_url)
+ return None
+ next_url = urljoin(current_url, location)
+ if not self._redirect_host_allowed(current_url, next_url):
+ self.logger.warning(
+ "media download cross-host redirect refused ref={} next={}",
+ current_url,
+ next_url,
+ )
+ return None
+ if not self._validate_remote_media_url(next_url):
+ return None
+ return next_url
+
+ async def _fetch_remote_media_bytes(
+ self,
+ media_ref: str,
+ ) -> tuple[bytes | None, str | None]:
+ """Fetch a remote media URL with SSRF, redirect, and size checks."""
+ if not self._http:
+ return None, None
+
+ if not self._validate_remote_media_url(media_ref):
+ return None, None
+
+ try:
+ # Prefer streaming with a running byte cap so large responses are not
+ # materialized before the limit is enforced. Test fakes may only
+ # implement get(), so keep a small compatibility fallback below.
+ stream = getattr(self._http, "stream", None)
+ if stream is not None:
+ current_url = media_ref
+ for _ in range(DINGTALK_MAX_REMOTE_MEDIA_REDIRECTS + 1):
+ async with stream("GET", current_url, follow_redirects=False) as resp:
+ final_ok, final_err = validate_resolved_url(str(resp.url))
+ if not final_ok:
+ self.logger.warning(
+ "remote media redirect blocked ref={} final={} reason={}",
+ media_ref,
+ resp.url,
+ final_err,
+ )
+ return None, None
+ if 300 <= resp.status_code < 400:
+ next_url = self._next_remote_media_url(
+ str(resp.url), resp.headers.get("location")
+ )
+ if not next_url:
+ return None, None
+ current_url = next_url
+ continue
+ if resp.status_code >= 400:
+ self.logger.warning(
+ "media download failed status={} ref={}",
+ resp.status_code,
+ current_url,
+ )
+ return None, None
+ chunks: list[bytes] = []
+ total = 0
+ async for chunk in resp.aiter_bytes():
+ total += len(chunk)
+ if total > DINGTALK_MAX_REMOTE_MEDIA_BYTES:
+ self.logger.warning(
+ "media download too large ref={} bytes>{}",
+ current_url,
+ DINGTALK_MAX_REMOTE_MEDIA_BYTES,
+ )
+ return None, None
+ chunks.append(chunk)
+ return b"".join(chunks), (resp.headers.get("content-type") or "")
+ self.logger.warning("media download exceeded redirect limit ref={}", media_ref)
+ return None, None
+
+ current_url = media_ref
+ for _ in range(DINGTALK_MAX_REMOTE_MEDIA_REDIRECTS + 1):
+ resp = await self._http.get(current_url, follow_redirects=False)
+ final_ok, final_err = validate_resolved_url(str(getattr(resp, "url", current_url)))
+ if not final_ok:
+ self.logger.warning(
+ "remote media redirect blocked ref={} final={} reason={}",
+ media_ref,
+ getattr(resp, "url", current_url),
+ final_err,
+ )
+ return None, None
+ if 300 <= resp.status_code < 400:
+ next_url = self._next_remote_media_url(
+ str(getattr(resp, "url", current_url)), resp.headers.get("location")
+ )
+ if not next_url:
+ return None, None
+ current_url = next_url
+ continue
+ if resp.status_code >= 400:
+ self.logger.warning(
+ "media download failed status={} ref={}",
+ resp.status_code,
+ current_url,
+ )
+ return None, None
+ if len(resp.content) > DINGTALK_MAX_REMOTE_MEDIA_BYTES:
+ self.logger.warning(
+ "media download too large ref={} bytes>{}",
+ current_url,
+ DINGTALK_MAX_REMOTE_MEDIA_BYTES,
+ )
+ return None, None
+ return resp.content, (resp.headers.get("content-type") or "")
+ self.logger.warning("media download exceeded redirect limit ref={}", media_ref)
+ return None, None
+ except httpx.TransportError:
+ self.logger.exception("media download network error ref={}", media_ref)
+ raise
+ except Exception:
+ self.logger.exception("media download error ref={}", media_ref)
+ return None, None
+
+ async def _read_media_bytes(
+ self,
+ media_ref: str,
+ ) -> tuple[bytes | None, str | None, str | None]:
+ if not media_ref:
+ return None, None, None
+
+ if self._is_http_url(media_ref):
+ data, raw_content_type = await self._fetch_remote_media_bytes(media_ref)
+ if data is None:
+ return None, None, None
+ content_type = (raw_content_type or "").split(";")[0].strip()
+ filename = self._guess_filename(media_ref, self._guess_upload_type(media_ref))
+ return data, filename, content_type or None
+
+ try:
+ if media_ref.startswith("file://"):
+ parsed = urlparse(media_ref)
+ local_path = Path(unquote(parsed.path))
+ else:
+ local_path = Path(os.path.expanduser(media_ref))
+ if not local_path.is_file():
+ self.logger.warning("media file not found: {}", local_path)
+ return None, None, None
+ data = await asyncio.to_thread(local_path.read_bytes)
+ content_type = mimetypes.guess_type(local_path.name)[0]
+ return data, local_path.name, content_type
+ except Exception:
+ self.logger.exception("media read error ref={}", media_ref)
+ return None, None, None
+
+ async def _upload_media(
+ self,
+ token: str,
+ data: bytes,
+ media_type: str,
+ filename: str,
+ content_type: str | None,
+ ) -> str | None:
+ if not self._http:
+ return None
+ url = f"https://oapi.dingtalk.com/media/upload?access_token={token}&type={media_type}"
+ mime = content_type or mimetypes.guess_type(filename)[0] or "application/octet-stream"
+ files = {"media": (filename, data, mime)}
+
+ try:
+ resp = await self._http.post(url, files=files)
+ text = resp.text
+ result = resp.json() if resp.headers.get("content-type", "").startswith("application/json") else {}
+ if resp.status_code >= 400:
+ self.logger.error("media upload failed status={} type={} body={}", resp.status_code, media_type, text[:500])
+ return None
+ errcode = result.get("errcode", 0)
+ if errcode != 0:
+ self.logger.error("media upload api error type={} errcode={} body={}", media_type, errcode, text[:500])
+ return None
+ sub = result.get("result") or {}
+ media_id = result.get("media_id") or result.get("mediaId") or sub.get("media_id") or sub.get("mediaId")
+ if not media_id:
+ self.logger.error("media upload missing media_id body={}", text[:500])
+ return None
+ return str(media_id)
+ except httpx.TransportError:
+ self.logger.exception("media upload network error type={}", media_type)
+ raise
+ except Exception:
+ self.logger.exception("media upload error type={}", media_type)
+ return None
+
+ async def _send_batch_message(
+ self,
+ token: str,
+ chat_id: str,
+ msg_key: str,
+ msg_param: dict[str, Any],
+ ) -> bool:
+ if not self._http:
+ self.logger.warning("HTTP client not initialized, cannot send")
+ return False
+
+ headers = {"x-acs-dingtalk-access-token": token}
+ if chat_id.startswith("group:"):
+ # Group chat
+ url = "https://api.dingtalk.com/v1.0/robot/groupMessages/send"
+ payload = {
+ "robotCode": self.config.client_id,
+ "openConversationId": chat_id[6:], # Remove "group:" prefix,
+ "msgKey": msg_key,
+ "msgParam": json.dumps(msg_param, ensure_ascii=False),
+ }
+ else:
+ # Private chat
+ url = "https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend"
+ payload = {
+ "robotCode": self.config.client_id,
+ "userIds": [chat_id],
+ "msgKey": msg_key,
+ "msgParam": json.dumps(msg_param, ensure_ascii=False),
+ }
+
+ try:
+ resp = await self._http.post(url, json=payload, headers=headers)
+ body = resp.text
+ if resp.status_code != 200:
+ self.logger.error("send failed msgKey={} status={} body={}", msg_key, resp.status_code, body[:500])
+ return False
+ try:
+ result = resp.json()
+ except Exception:
+ result = {}
+ errcode = result.get("errcode")
+ if errcode not in (None, 0):
+ self.logger.error("send api error msgKey={} errcode={} body={}", msg_key, errcode, body[:500])
+ return False
+ self.logger.debug("message sent to {} with msgKey={}", chat_id, msg_key)
+ return True
+ except httpx.TransportError:
+ self.logger.exception("network error sending message msgKey={}", msg_key)
+ raise
+ except Exception:
+ self.logger.exception("Error sending message msgKey={}", msg_key)
+ return False
+
+ async def _send_markdown_text(self, token: str, chat_id: str, content: str) -> bool:
+ return await self._send_batch_message(
+ token,
+ chat_id,
+ "sampleMarkdown",
+ {"text": content, "title": "Vibe-Trading Reply"},
+ )
+
+ async def _send_media_ref(self, token: str, chat_id: str, media_ref: str) -> bool:
+ media_ref = (media_ref or "").strip()
+ if not media_ref:
+ return True
+
+ upload_type = self._guess_upload_type(media_ref)
+ if upload_type == "image" and self._is_http_url(media_ref):
+ ok = await self._send_batch_message(
+ token,
+ chat_id,
+ "sampleImageMsg",
+ {"photoURL": media_ref},
+ )
+ if ok:
+ return True
+ self.logger.warning("image url send failed, trying upload fallback: {}", media_ref)
+
+ data, filename, content_type = await self._read_media_bytes(media_ref)
+ if not data:
+ self.logger.error("media read failed: {}", media_ref)
+ return False
+
+ filename = filename or self._guess_filename(media_ref, upload_type)
+ data, filename, content_type = self._normalize_upload_payload(filename, data, content_type)
+ file_type = Path(filename).suffix.lower().lstrip(".")
+ if not file_type:
+ guessed = mimetypes.guess_extension(content_type or "")
+ file_type = (guessed or ".bin").lstrip(".")
+ if file_type == "jpeg":
+ file_type = "jpg"
+
+ media_id = await self._upload_media(
+ token=token,
+ data=data,
+ media_type=upload_type,
+ filename=filename,
+ content_type=content_type,
+ )
+ if not media_id:
+ return False
+
+ if upload_type == "image":
+ # Verified in production: sampleImageMsg accepts media_id in photoURL.
+ ok = await self._send_batch_message(
+ token,
+ chat_id,
+ "sampleImageMsg",
+ {"photoURL": media_id},
+ )
+ if ok:
+ return True
+ self.logger.warning("image media_id send failed, falling back to file: {}", media_ref)
+
+ return await self._send_batch_message(
+ token,
+ chat_id,
+ "sampleFile",
+ {"mediaId": media_id, "fileName": filename, "fileType": file_type},
+ )
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through DingTalk."""
+ token = await self._get_access_token()
+ if not token:
+ return
+
+ if msg.content and msg.content.strip():
+ await self._send_markdown_text(token, msg.chat_id, msg.content.strip())
+
+ for media_ref in msg.media or []:
+ ok = await self._send_media_ref(token, msg.chat_id, media_ref)
+ if ok:
+ continue
+ self.logger.error("media send failed for {}", media_ref)
+ # Send visible fallback so failures are observable by the user.
+ filename = self._guess_filename(media_ref, self._guess_upload_type(media_ref))
+ await self._send_markdown_text(
+ token,
+ msg.chat_id,
+ f"[Attachment send failed: {filename}]",
+ )
+
+ async def _on_message(
+ self,
+ content: str,
+ sender_id: str,
+ sender_name: str,
+ conversation_type: str | None = None,
+ conversation_id: str | None = None,
+ ) -> None:
+ """Handle incoming message (called by VibeTradingDingTalkHandler).
+
+ Delegates to BaseChannel._handle_message() which enforces allow_from
+ permission checks before publishing to the bus.
+ """
+ try:
+ self.logger.info("inbound: {} from {}", content, sender_name)
+ is_group = conversation_type == "2" and conversation_id
+ chat_id = f"group:{conversation_id}" if is_group else sender_id
+ session_key = None
+ if is_group and self.config.group_user_isolation:
+ session_key = f"{self.name}:group:{conversation_id}:{sender_id}"
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content=str(content),
+ metadata={
+ "sender_name": sender_name,
+ "platform": "dingtalk",
+ "conversation_type": conversation_type,
+ },
+ session_key=session_key,
+ )
+ except Exception:
+ self.logger.exception("Error publishing message")
+
+ async def _download_dingtalk_file(
+ self,
+ download_code: str,
+ filename: str,
+ sender_id: str,
+ ) -> str | None:
+ """Download a DingTalk file to the media directory, return local path."""
+ from src.channels.utils import get_media_dir
+
+ try:
+ token = await self._get_access_token()
+ if not token or not self._http:
+ self.logger.error("file download: no token or http client")
+ return None
+
+ # Step 1: Exchange downloadCode for a temporary download URL
+ api_url = "https://api.dingtalk.com/v1.0/robot/messageFiles/download"
+ headers = {"x-acs-dingtalk-access-token": token, "Content-Type": "application/json"}
+ payload = {"downloadCode": download_code, "robotCode": self.config.client_id}
+ resp = await self._http.post(api_url, json=payload, headers=headers)
+ if resp.status_code != 200:
+ self.logger.error("get download URL failed: status={}, body={}", resp.status_code, resp.text)
+ return None
+
+ result = resp.json()
+ download_url = result.get("downloadUrl")
+ if not download_url:
+ self.logger.error("download URL not found in response: {}", result)
+ return None
+
+ # Step 2: Download the file content
+ file_resp = await self._http.get(download_url, follow_redirects=True)
+ if file_resp.status_code != 200:
+ self.logger.error("file download failed: status={}", file_resp.status_code)
+ return None
+
+ # Save to media directory (accessible under workspace)
+ download_dir = get_media_dir("dingtalk") / sender_id
+ download_dir.mkdir(parents=True, exist_ok=True)
+ file_path = download_dir / filename
+ await asyncio.to_thread(file_path.write_bytes, file_resp.content)
+ self.logger.info("file saved: {}", file_path)
+ return str(file_path)
+ except Exception:
+ self.logger.exception("file download error")
+ return None
diff --git a/agent/src/channels/discord.py b/agent/src/channels/discord.py
new file mode 100644
index 000000000..1bbc3632d
--- /dev/null
+++ b/agent/src/channels/discord.py
@@ -0,0 +1,818 @@
+"""Discord channel implementation using discord.py."""
+
+from __future__ import annotations
+
+import asyncio
+import importlib.util
+import time
+from contextlib import suppress
+from dataclasses import dataclass
+from pathlib import Path
+from typing import TYPE_CHECKING, Any, Literal
+
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.utils import safe_filename, split_message
+
+DISCORD_AVAILABLE = importlib.util.find_spec("discord") is not None
+if TYPE_CHECKING:
+ import aiohttp
+ import discord
+ from discord import app_commands
+ from discord.abc import Messageable
+
+if DISCORD_AVAILABLE:
+ import discord
+ from discord import app_commands
+ from discord.abc import Messageable
+
+MAX_ATTACHMENT_BYTES = 20 * 1024 * 1024 # 20MB
+MAX_MESSAGE_LEN = 2000 # Discord message character limit
+TYPING_INTERVAL_S = 8
+
+
+@dataclass
+class _StreamBuf:
+ """Per-chat streaming accumulator for progressive Discord message edits."""
+
+ text: str = ""
+ message: Any | None = None
+ last_edit: float = 0.0
+ stream_id: str | None = None
+
+
+class DiscordConfig(BaseModel):
+ """Discord channel configuration."""
+
+ enabled: bool = False
+ token: str = ""
+ allow_from: list[str] = Field(default_factory=list)
+ allow_channels: list[str] = Field(default_factory=list) # Allowed channel IDs (empty = all)
+ intents: int = 37377
+ group_policy: Literal["mention", "open"] = "mention"
+ read_receipt_emoji: str = "👀"
+ working_emoji: str = "🔧"
+ working_emoji_delay: float = 2.0
+ streaming: bool = True
+ proxy: str | None = None
+ proxy_username: str | None = None
+ proxy_password: str | None = None
+
+
+if DISCORD_AVAILABLE:
+
+ class DiscordBotClient(discord.Client):
+ """discord.py client that forwards events to the channel."""
+
+ def __init__(
+ self,
+ channel: DiscordChannel,
+ *,
+ intents: discord.Intents,
+ proxy: str | None = None,
+ proxy_auth: aiohttp.BasicAuth | None = None,
+ ) -> None:
+ super().__init__(intents=intents, proxy=proxy, proxy_auth=proxy_auth)
+ self._channel = channel
+ self.tree = app_commands.CommandTree(self)
+ self._register_app_commands()
+
+ async def on_ready(self) -> None:
+ self._channel._bot_user_id = str(self.user.id) if self.user else None
+ self._channel.logger.info("bot connected as user {}", self._channel._bot_user_id)
+ try:
+ synced = await self.tree.sync()
+ self._channel.logger.info("app commands synced: {}", len(synced))
+ except Exception as e:
+ self._channel.logger.warning("app command sync failed: {}", e)
+
+ async def on_message(self, message: discord.Message) -> None:
+ await self._channel._handle_discord_message(message)
+
+ async def on_thread_delete(self, thread: discord.Thread) -> None:
+ self._channel._forget_channel(thread)
+
+ async def on_thread_update(self, before: discord.Thread, after: discord.Thread) -> None:
+ if getattr(after, "archived", False):
+ self._channel._forget_channel(after)
+ else:
+ self._channel._remember_channel(after)
+
+ async def _reply_ephemeral(self, interaction: discord.Interaction, text: str) -> bool:
+ """Send an ephemeral interaction response and report success."""
+ try:
+ await interaction.response.send_message(text, ephemeral=True)
+ return True
+ except Exception as e:
+ self._channel.logger.warning("interaction response failed: {}", e)
+ return False
+
+ async def _resolve_interaction_channel(
+ self,
+ interaction: discord.Interaction,
+ ) -> Any | None:
+ channel_id = interaction.channel_id
+ if channel_id is None:
+ return None
+ channel = getattr(interaction, "channel", None) or self.get_channel(channel_id)
+ if channel is None:
+ try:
+ channel = await self.fetch_channel(channel_id)
+ except Exception as e:
+ self._channel.logger.warning("interaction channel {} unavailable: {}", channel_id, e)
+ return None
+ self._channel._remember_channel(channel)
+ return channel
+
+ async def _interaction_channel_allowed(
+ self,
+ interaction: discord.Interaction,
+ channel: Any | None,
+ ) -> bool:
+ allow_channels = self._channel.config.allow_channels
+ if not allow_channels:
+ return True
+ if channel is None:
+ channel_id = interaction.channel_id
+ return channel_id is not None and str(channel_id) in allow_channels
+ channel_ids = self._channel._channel_allow_keys(channel)
+ return not channel_ids.isdisjoint(allow_channels)
+
+ async def _forward_slash_command(
+ self,
+ interaction: discord.Interaction,
+ command_text: str,
+ ) -> None:
+ sender_id = str(interaction.user.id)
+ channel_id = interaction.channel_id
+
+ if channel_id is None:
+ self._channel.logger.warning("slash command missing channel_id: {}", command_text)
+ return
+
+ if not self._channel.is_allowed(sender_id):
+ await self._reply_ephemeral(interaction, "You are not allowed to use this bot.")
+ return
+
+ channel = await self._resolve_interaction_channel(interaction)
+ if not await self._interaction_channel_allowed(interaction, channel):
+ await self._reply_ephemeral(interaction, "This channel is not allowed for this bot.")
+ return
+
+ await self._reply_ephemeral(interaction, f"Processing {command_text}...")
+
+ metadata: dict[str, Any] = {
+ "interaction_id": str(interaction.id),
+ "guild_id": str(interaction.guild_id) if interaction.guild_id else None,
+ "is_slash_command": True,
+ }
+ session_key = None
+ if channel is not None:
+ parent_channel_id = self._channel._channel_parent_key(channel)
+ if parent_channel_id is not None:
+ metadata["parent_channel_id"] = parent_channel_id
+ metadata["context_chat_id"] = parent_channel_id
+ metadata["thread_id"] = str(channel_id)
+ session_key = f"{self._channel.name}:{parent_channel_id}:thread:{channel_id}"
+
+ await self._channel._handle_message(
+ sender_id=sender_id,
+ chat_id=str(channel_id),
+ content=command_text,
+ metadata=metadata,
+ session_key=session_key,
+ )
+
+ def _register_app_commands(self) -> None:
+ commands = (
+ ("new", "Stop current task and start a new conversation", "/new"),
+ ("stop", "Stop the current task", "/stop"),
+ ("restart", "Restart the bot", "/restart"),
+ ("status", "Show bot status", "/status"),
+ ("history", "Show recent conversation messages", "/history"),
+ )
+
+ for name, description, command_text in commands:
+
+ @self.tree.command(name=name, description=description)
+ async def command_handler(
+ interaction: discord.Interaction,
+ _command_text: str = command_text,
+ ) -> None:
+ await self._forward_slash_command(interaction, _command_text)
+
+ @self.tree.command(name="model", description="Show or switch runtime model preset")
+ @app_commands.describe(preset="Optional model preset name, such as default")
+ async def model_command(
+ interaction: discord.Interaction,
+ preset: str | None = None,
+ ) -> None:
+ preset = (preset or "").strip()
+ command_text = f"/model {preset}" if preset else "/model"
+ await self._forward_slash_command(interaction, command_text)
+
+ @self.tree.command(name="help", description="Show available commands")
+ async def help_command(interaction: discord.Interaction) -> None:
+ sender_id = str(interaction.user.id)
+ if not self._channel.is_allowed(sender_id):
+ await self._reply_ephemeral(interaction, "You are not allowed to use this bot.")
+ return
+ channel = await self._resolve_interaction_channel(interaction)
+ if not await self._interaction_channel_allowed(interaction, channel):
+ await self._reply_ephemeral(interaction, "This channel is not allowed for this bot.")
+ return
+ await self._reply_ephemeral(interaction, "Use /help for available commands. Docs: https://github.com/HKUDS/Vibe-Trading")
+
+ @self.tree.error
+ async def on_app_command_error(
+ interaction: discord.Interaction,
+ error: app_commands.AppCommandError,
+ ) -> None:
+ command_name = interaction.command.qualified_name if interaction.command else "?"
+ self._channel.logger.warning(
+ "app command failed user={} channel={} cmd={} error={}",
+ interaction.user.id,
+ interaction.channel_id,
+ command_name,
+ error,
+ )
+
+ async def send_outbound(self, msg: OutboundMessage) -> None:
+ """Send a vibe-trading outbound message using Discord transport rules."""
+ channel_id = int(msg.chat_id)
+
+ channel = self._channel._known_channels.get(msg.chat_id) or self.get_channel(channel_id)
+ if channel is None:
+ try:
+ channel = await self.fetch_channel(channel_id)
+ except Exception as e:
+ self._channel.logger.warning("channel {} unavailable: {}", msg.chat_id, e)
+ return
+
+ reference, mention_settings = self._build_reply_context(channel, msg.reply_to)
+ sent_media = False
+ failed_media: list[str] = []
+
+ for index, media_path in enumerate(msg.media or []):
+ if await self._send_file(
+ channel,
+ media_path,
+ reference=reference if index == 0 else None,
+ mention_settings=mention_settings,
+ ):
+ sent_media = True
+ else:
+ failed_media.append(Path(media_path).name)
+
+ for index, chunk in enumerate(
+ self._build_chunks(msg.content or "", failed_media, sent_media)
+ ):
+ kwargs: dict[str, Any] = {"content": chunk}
+ if index == 0 and reference is not None and not sent_media:
+ kwargs["reference"] = reference
+ kwargs["allowed_mentions"] = mention_settings
+ await channel.send(**kwargs)
+
+ async def _send_file(
+ self,
+ channel: Messageable,
+ file_path: str,
+ *,
+ reference: discord.PartialMessage | None,
+ mention_settings: discord.AllowedMentions,
+ ) -> bool:
+ """Send a file attachment via discord.py."""
+ path = Path(file_path)
+ if not path.is_file():
+ self._channel.logger.warning("file not found, skipping: {}", file_path)
+ return False
+
+ if path.stat().st_size > MAX_ATTACHMENT_BYTES:
+ self._channel.logger.warning("file too large (>20MB), skipping: {}", path.name)
+ return False
+
+ try:
+ kwargs: dict[str, Any] = {"file": discord.File(path)}
+ if reference is not None:
+ kwargs["reference"] = reference
+ kwargs["allowed_mentions"] = mention_settings
+ await channel.send(**kwargs)
+ self._channel.logger.info("file sent: {}", path.name)
+ return True
+ except Exception:
+ self._channel.logger.exception("Error sending file {}", path.name)
+ return False
+
+ @staticmethod
+ def _build_chunks(content: str, failed_media: list[str], sent_media: bool) -> list[str]:
+ """Build outbound text chunks, including attachment-failure fallback text."""
+ chunks = split_message(content, MAX_MESSAGE_LEN)
+ if chunks or not failed_media or sent_media:
+ return chunks
+ fallback = "\n".join(f"[attachment: {name} - send failed]" for name in failed_media)
+ return split_message(fallback, MAX_MESSAGE_LEN)
+
+ def _build_reply_context(
+ self,
+ channel: Messageable,
+ reply_to: str | None,
+ ) -> tuple[discord.PartialMessage | None, discord.AllowedMentions]:
+ """Build reply context for outbound messages."""
+ mention_settings = discord.AllowedMentions(replied_user=False)
+ if not reply_to:
+ return None, mention_settings
+ try:
+ message_id = int(reply_to)
+ except ValueError:
+ self._channel.logger.warning("Invalid reply target: {}", reply_to)
+ return None, mention_settings
+
+ return channel.get_partial_message(message_id), mention_settings
+
+
+class DiscordChannel(BaseChannel):
+ """Discord channel using discord.py."""
+
+ name = "discord"
+ display_name = "Discord"
+ _STREAM_EDIT_INTERVAL = 0.8
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return DiscordConfig().model_dump(by_alias=True)
+
+ @staticmethod
+ def _channel_key(channel_or_id: Any) -> str:
+ """Normalize channel-like objects and ids to a stable string key."""
+ channel_id = getattr(channel_or_id, "id", channel_or_id)
+ return str(channel_id)
+
+ @classmethod
+ def _channel_allow_keys(cls, channel: Any) -> set[str]:
+ """Return channel IDs that can satisfy allow_channels for this channel."""
+ keys = {cls._channel_key(channel)}
+ if parent_key := cls._channel_parent_key(channel):
+ keys.add(parent_key)
+ return keys
+
+ @classmethod
+ def _channel_parent_key(cls, channel: Any) -> str | None:
+ """Return the parent channel key for a Discord thread-like channel."""
+ parent_id = getattr(channel, "parent_id", None)
+ if parent_id is not None:
+ return cls._channel_key(parent_id)
+ parent = getattr(channel, "parent", None)
+ if parent is not None:
+ return cls._channel_key(parent)
+ return None
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = DiscordConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: DiscordConfig = config
+ self._client: DiscordBotClient | None = None
+ self._typing_tasks: dict[str, asyncio.Task[None]] = {}
+ self._bot_user_id: str | None = None
+ self._pending_reactions: dict[str, Any] = {} # chat_id -> message object
+ self._working_emoji_tasks: dict[str, asyncio.Task[None]] = {}
+ self._stream_bufs: dict[str, _StreamBuf] = {}
+ self._known_channels: dict[str, Any] = {}
+
+ def _remember_channel(self, channel: Any) -> None:
+ self._known_channels[self._channel_key(channel)] = channel
+
+ def _forget_channel(self, channel_or_id: Any) -> None:
+ self._known_channels.pop(self._channel_key(channel_or_id), None)
+
+ async def start(self) -> None:
+ """Start the Discord client."""
+ if not DISCORD_AVAILABLE:
+ self.logger.error("discord.py not installed. Run: pip install vibe-trading-ai[discord]")
+ return
+
+ if not self.config.token:
+ self.logger.error("bot token not configured")
+ return
+
+ try:
+ intents = discord.Intents.none()
+ intents.value = self.config.intents
+
+ proxy_auth = None
+ has_user = bool(self.config.proxy_username)
+ has_pass = bool(self.config.proxy_password)
+ if has_user and has_pass:
+ import aiohttp
+
+ proxy_auth = aiohttp.BasicAuth(
+ login=self.config.proxy_username,
+ password=self.config.proxy_password,
+ )
+ elif has_user != has_pass:
+ self.logger.warning(
+ "proxy auth incomplete: both proxy_username and "
+ "proxy_password must be set; ignoring partial credentials",
+ )
+
+ self._client = DiscordBotClient(
+ self,
+ intents=intents,
+ proxy=self.config.proxy,
+ proxy_auth=proxy_auth,
+ )
+ except Exception:
+ self.logger.exception("Failed to initialize client")
+ self._client = None
+ self._running = False
+ return
+
+ self._running = True
+ self.logger.info("Starting client via discord.py...")
+
+ try:
+ await self._client.start(self.config.token)
+ except asyncio.CancelledError:
+ raise
+ except Exception:
+ self.logger.exception("client startup failed")
+ finally:
+ self._running = False
+ await self._reset_runtime_state(close_client=True)
+
+ async def stop(self) -> None:
+ """Stop the Discord channel."""
+ self._running = False
+ await self._reset_runtime_state(close_client=True)
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through Discord using discord.py."""
+ client = self._client
+ if client is None or not client.is_ready():
+ self.logger.warning("client not ready; dropping outbound message")
+ return
+
+ is_progress = bool((msg.metadata or {}).get("_progress"))
+
+ try:
+ await client.send_outbound(msg)
+ except Exception:
+ self.logger.exception("Error sending message")
+ raise
+ finally:
+ if not is_progress:
+ await self._stop_typing(msg.chat_id)
+ await self._clear_reactions(msg.chat_id)
+
+ async def send_delta(
+ self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None
+ ) -> None:
+ """Progressive Discord delivery: send once, then edit until the stream ends."""
+ client = self._client
+ if client is None or not client.is_ready():
+ self.logger.warning("client not ready; dropping stream delta")
+ return
+
+ meta = metadata or {}
+ stream_id = meta.get("_stream_id")
+
+ if meta.get("_stream_end"):
+ buf = self._stream_bufs.get(chat_id)
+ if not buf or buf.message is None or not buf.text:
+ return
+ if stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id:
+ return
+ await self._finalize_stream(chat_id, buf)
+ return
+
+ buf = self._stream_bufs.get(chat_id)
+ if buf is None or (
+ stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id
+ ):
+ buf = _StreamBuf(stream_id=stream_id)
+ self._stream_bufs[chat_id] = buf
+ elif buf.stream_id is None:
+ buf.stream_id = stream_id
+
+ buf.text += delta
+ if not buf.text.strip():
+ return
+
+ target = await self._resolve_channel(chat_id)
+ if target is None:
+ self.logger.warning("stream target {} unavailable", chat_id)
+ return
+
+ now = time.monotonic()
+ if buf.message is None:
+ try:
+ buf.message = await target.send(content=buf.text)
+ buf.last_edit = now
+ except Exception as e:
+ self.logger.warning("stream initial send failed: {}", e)
+ raise
+ return
+
+ if (now - buf.last_edit) < self._STREAM_EDIT_INTERVAL:
+ return
+
+ try:
+ await buf.message.edit(content=DiscordBotClient._build_chunks(buf.text, [], False)[0])
+ buf.last_edit = now
+ except Exception as e:
+ self.logger.warning("stream edit failed: {}", e)
+ raise
+
+ async def _handle_discord_message(self, message: discord.Message) -> None:
+ """Handle incoming Discord messages from discord.py.
+
+ Self-loop guard: only drop messages from this bot's own account. Messages
+ from other bots are allowed through so multi-agent setups (one bot asking
+ another for help, a bot mentioning another by @name, etc.) can work.
+ Bot-from-bot loops are still prevented per-instance because each bot
+ still ignores its own outbound messages. (#3217)
+ """
+ if self._bot_user_id is not None and str(message.author.id) == self._bot_user_id:
+ return
+ if self._is_system_message(message):
+ return
+
+ sender_id = str(message.author.id)
+ channel_id = self._channel_key(message.channel)
+ self._remember_channel(message.channel)
+ content = message.content or ""
+
+ if not self._should_accept_inbound(message, sender_id, content):
+ return
+
+ media_paths, attachment_markers = await self._download_attachments(message.attachments)
+ full_content = self._compose_inbound_content(content, attachment_markers)
+ metadata = self._build_inbound_metadata(message)
+ parent_channel_id = self._channel_parent_key(message.channel)
+ session_key = None
+ if parent_channel_id is not None:
+ metadata["parent_channel_id"] = parent_channel_id
+ metadata["context_chat_id"] = parent_channel_id
+ metadata["thread_id"] = channel_id
+ session_key = f"{self.name}:{parent_channel_id}:thread:{channel_id}"
+
+ await self._start_typing(message.channel)
+
+ # Add read receipt reaction immediately, working emoji after delay
+ try:
+ await message.add_reaction(self.config.read_receipt_emoji)
+ self._pending_reactions[channel_id] = message
+ except Exception as e:
+ self.logger.debug("Failed to add read receipt reaction: {}", e)
+
+ # Delayed working indicator (cosmetic — not tied to subagent lifecycle)
+ async def _delayed_working_emoji() -> None:
+ await asyncio.sleep(self.config.working_emoji_delay)
+ with suppress(Exception):
+ await message.add_reaction(self.config.working_emoji)
+
+ self._working_emoji_tasks[channel_id] = asyncio.create_task(_delayed_working_emoji())
+
+ try:
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=channel_id,
+ content=full_content,
+ media=media_paths,
+ metadata=metadata,
+ session_key=session_key,
+ is_dm=message.guild is None,
+ )
+ except Exception:
+ await self._clear_reactions(channel_id)
+ await self._stop_typing(channel_id)
+ raise
+
+ async def _on_message(self, message: discord.Message) -> None:
+ """Backward-compatible alias for legacy tests/callers."""
+ await self._handle_discord_message(message)
+
+ async def _resolve_channel(self, chat_id: str) -> Any | None:
+ """Resolve a Discord channel from cache first, then network fetch."""
+ client = self._client
+ if client is None or not client.is_ready():
+ return None
+ channel = self._known_channels.get(chat_id)
+ if channel is not None:
+ return channel
+ channel_id = int(chat_id)
+ channel = client.get_channel(channel_id)
+ if channel is not None:
+ return channel
+ try:
+ return await client.fetch_channel(channel_id)
+ except Exception as e:
+ self.logger.warning("channel {} unavailable: {}", chat_id, e)
+ return None
+
+ async def _finalize_stream(self, chat_id: str, buf: _StreamBuf) -> None:
+ """Commit the final streamed content and flush overflow chunks."""
+ chunks = DiscordBotClient._build_chunks(buf.text, [], False)
+ if not chunks:
+ self._stream_bufs.pop(chat_id, None)
+ return
+
+ try:
+ await buf.message.edit(content=chunks[0])
+ except Exception as e:
+ self.logger.warning("final stream edit failed: {}", e)
+ raise
+
+ target = getattr(buf.message, "channel", None) or await self._resolve_channel(chat_id)
+ if target is None:
+ self.logger.warning("stream follow-up target {} unavailable", chat_id)
+ self._stream_bufs.pop(chat_id, None)
+ return
+
+ for extra_chunk in chunks[1:]:
+ await target.send(content=extra_chunk)
+
+ self._stream_bufs.pop(chat_id, None)
+ await self._stop_typing(chat_id)
+ await self._clear_reactions(chat_id)
+
+ def _should_accept_inbound(
+ self,
+ message: discord.Message,
+ sender_id: str,
+ content: str,
+ ) -> bool:
+ """Check if inbound Discord message should be processed."""
+ if not self.is_allowed(sender_id):
+ return False
+ # Channel-based filtering: only respond in allowed channels
+ allow_channels = self.config.allow_channels
+ if allow_channels:
+ channel_ids = self._channel_allow_keys(message.channel)
+ if channel_ids.isdisjoint(allow_channels):
+ return False
+ if message.guild is not None and not self._should_respond_in_group(message, content):
+ return False
+ return True
+
+ async def _download_attachments(
+ self,
+ attachments: list[discord.Attachment],
+ ) -> tuple[list[str], list[str]]:
+ """Download supported attachments and return paths + display markers."""
+ media_paths: list[str] = []
+ markers: list[str] = []
+ media_dir = get_media_dir("discord")
+
+ for attachment in attachments:
+ filename = attachment.filename or "attachment"
+ if attachment.size and attachment.size > MAX_ATTACHMENT_BYTES:
+ markers.append(f"[attachment: {filename} - too large]")
+ continue
+ try:
+ media_dir.mkdir(parents=True, exist_ok=True)
+ safe_name = safe_filename(filename)
+ file_path = media_dir / f"{attachment.id}_{safe_name}"
+ await attachment.save(file_path)
+ media_paths.append(str(file_path))
+ markers.append(f"[attachment: {file_path.name}]")
+ except Exception as e:
+ self.logger.warning("Failed to download attachment: {}", e)
+ markers.append(f"[attachment: {filename} - download failed]")
+
+ return media_paths, markers
+
+ @staticmethod
+ def _compose_inbound_content(content: str, attachment_markers: list[str]) -> str:
+ """Combine message text with attachment markers."""
+ content_parts = [content] if content else []
+ content_parts.extend(attachment_markers)
+ return "\n".join(part for part in content_parts if part) or "[empty message]"
+
+ @staticmethod
+ def _is_system_message(message: discord.Message) -> bool:
+ """Return True for Discord system messages that carry no user prompt."""
+ message_type = getattr(message, "type", discord.MessageType.default)
+ return message_type not in {discord.MessageType.default, discord.MessageType.reply}
+
+ @staticmethod
+ def _build_inbound_metadata(message: discord.Message) -> dict[str, str | None]:
+ """Build metadata for inbound Discord messages."""
+ reply_to = (
+ str(message.reference.message_id)
+ if message.reference and message.reference.message_id
+ else None
+ )
+ return {
+ "message_id": str(message.id),
+ "guild_id": str(message.guild.id) if message.guild else None,
+ "reply_to": reply_to,
+ }
+
+ def _should_respond_in_group(self, message: discord.Message, content: str) -> bool:
+ """Check if the bot should respond in a guild channel based on policy."""
+ if self.config.group_policy == "open":
+ return True
+
+ if self.config.group_policy == "mention":
+ bot_user_id = self._bot_user_id
+ if bot_user_id is None and self._client and self._client.user:
+ bot_user_id = str(self._client.user.id)
+ if bot_user_id is None:
+ self.logger.debug(
+ "message in {} ignored (bot identity unavailable)", message.channel.id
+ )
+ return False
+
+ if any(str(user.id) == bot_user_id for user in message.mentions):
+ return True
+ if bot_user_id in {str(user_id) for user_id in getattr(message, "raw_mentions", [])}:
+ return True
+ if f"<@{bot_user_id}>" in content or f"<@!{bot_user_id}>" in content:
+ return True
+ if self._references_bot_message(message, bot_user_id):
+ return True
+
+ self.logger.debug("message in {} ignored (bot not mentioned)", message.channel.id)
+ return False
+
+ return True
+
+ @staticmethod
+ def _references_bot_message(message: discord.Message, bot_user_id: str) -> bool:
+ """Return True when a Discord reply targets a message authored by this bot."""
+ reference = getattr(message, "reference", None)
+ if reference is None:
+ return False
+ referenced_message = getattr(reference, "resolved", None) or getattr(
+ reference, "cached_message", None
+ )
+ author = getattr(referenced_message, "author", None)
+ return str(getattr(author, "id", "")) == bot_user_id
+
+ async def _start_typing(self, channel: Messageable) -> None:
+ """Start periodic typing indicator for a channel."""
+ channel_id = self._channel_key(channel)
+ await self._stop_typing(channel_id)
+
+ async def typing_loop() -> None:
+ while self._running:
+ try:
+ async with channel.typing():
+ await asyncio.sleep(TYPING_INTERVAL_S)
+ except asyncio.CancelledError:
+ return
+ except Exception as e:
+ self.logger.debug("typing indicator failed for {}: {}", channel_id, e)
+ return
+
+ self._typing_tasks[channel_id] = asyncio.create_task(typing_loop())
+
+ async def _stop_typing(self, channel_id: str) -> None:
+ """Stop typing indicator for a channel."""
+ task = self._typing_tasks.pop(self._channel_key(channel_id), None)
+ if task is None:
+ return
+ task.cancel()
+ with suppress(asyncio.CancelledError):
+ await task
+
+ async def _clear_reactions(self, chat_id: str) -> None:
+ """Remove all pending reactions after bot replies."""
+ # Cancel delayed working emoji if it hasn't fired yet
+ task = self._working_emoji_tasks.pop(chat_id, None)
+ if task and not task.done():
+ task.cancel()
+
+ msg_obj = self._pending_reactions.pop(chat_id, None)
+ if msg_obj is None:
+ return
+ bot_user = self._client.user if self._client else None
+ for emoji in (self.config.read_receipt_emoji, self.config.working_emoji):
+ with suppress(Exception):
+ await msg_obj.remove_reaction(emoji, bot_user)
+
+ async def _cancel_all_typing(self) -> None:
+ """Stop all typing tasks."""
+ channel_ids = list(self._typing_tasks)
+ for channel_id in channel_ids:
+ await self._stop_typing(channel_id)
+
+ async def _reset_runtime_state(self, close_client: bool) -> None:
+ """Reset client and typing state."""
+ await self._cancel_all_typing()
+ self._stream_bufs.clear()
+ self._known_channels.clear()
+ if close_client and self._client is not None and not self._client.is_closed():
+ try:
+ await self._client.close()
+ except Exception as e:
+ self.logger.warning("client close failed: {}", e)
+ self._client = None
+ self._bot_user_id = None
diff --git a/agent/src/channels/email.py b/agent/src/channels/email.py
new file mode 100644
index 000000000..519d4bf05
--- /dev/null
+++ b/agent/src/channels/email.py
@@ -0,0 +1,914 @@
+"""Email channel implementation using IMAP polling + SMTP replies."""
+
+import asyncio
+import html
+import imaplib
+import mimetypes
+import re
+import smtplib
+import ssl
+from contextlib import suppress
+from dataclasses import dataclass
+from datetime import date
+from email import policy
+from email.header import decode_header, make_header
+from email.message import EmailMessage
+from email.parser import BytesParser
+from email.utils import parseaddr
+from fnmatch import fnmatch
+from pathlib import Path
+from typing import Any, Literal
+
+import logging; logger = logging.getLogger(__name__)
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.utils import safe_filename
+
+
+class EmailConfig(BaseModel):
+ """Email channel configuration (IMAP inbound + SMTP outbound)."""
+
+ enabled: bool = False
+ consent_granted: bool = False
+
+ imap_host: str = ""
+ imap_port: int = 993
+ imap_username: str = ""
+ imap_password: str = ""
+ imap_mailbox: str = "INBOX"
+ imap_use_ssl: bool = True
+
+ smtp_host: str = ""
+ smtp_port: int = 587
+ smtp_username: str = ""
+ smtp_password: str = ""
+ smtp_use_tls: bool = True
+ smtp_use_ssl: bool = False
+ from_address: str = ""
+
+ auto_reply_enabled: bool = True
+ poll_interval_seconds: int = 30
+ mark_seen: bool = True
+ post_action: Literal["delete", "move"] | None = None
+ post_action_move_mailbox: str | None = None
+ post_action_expunge: bool = False
+ post_action_ignore_skipped: bool = True
+ max_body_chars: int = 12000
+ subject_prefix: str = "Re: "
+ allow_from: list[str] = Field(default_factory=list)
+
+ # Email authentication verification (anti-spoofing)
+ verify_dkim: bool = True # Require Authentication-Results with dkim=pass
+ verify_spf: bool = True # Require Authentication-Results with spf=pass
+
+ # Attachment handling — set allowed types to enable (e.g. ["application/pdf", "image/*"], or ["*"] for all)
+ allowed_attachment_types: list[str] = Field(default_factory=list)
+ max_attachment_size: int = 2_000_000 # 2MB per attachment
+ max_attachments_per_email: int = 5
+
+
+@dataclass
+class _ServerFeatures:
+ move: bool
+ uidplus: bool
+ uid_store: bool | None = None
+
+
+class EmailChannel(BaseChannel):
+ """
+ Email channel.
+
+ Inbound:
+ - Poll IMAP mailbox for unread messages.
+ - Convert each message into an inbound event.
+
+ Outbound:
+ - Send responses via SMTP back to the sender address.
+ """
+
+ name = "email"
+ display_name = "Email"
+ _IMAP_MONTHS = (
+ "Jan",
+ "Feb",
+ "Mar",
+ "Apr",
+ "May",
+ "Jun",
+ "Jul",
+ "Aug",
+ "Sep",
+ "Oct",
+ "Nov",
+ "Dec",
+ )
+ _IMAP_RECONNECT_MARKERS = (
+ "disconnected for inactivity",
+ "eof occurred in violation of protocol",
+ "socket error",
+ "connection reset",
+ "broken pipe",
+ "bye",
+ )
+ _IMAP_MISSING_MAILBOX_MARKERS = (
+ "mailbox doesn't exist",
+ "select failed",
+ "no such mailbox",
+ "can't open mailbox",
+ "does not exist",
+ )
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return EmailConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = EmailConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: EmailConfig = config
+ self._self_addresses = self._collect_self_addresses()
+ self._last_subject_by_chat: dict[str, str] = {}
+ self._last_message_id_by_chat: dict[str, str] = {}
+ self._processed_uids: set[str] = set() # Capped to prevent unbounded growth
+ self._MAX_PROCESSED_UIDS = 100000
+
+ async def start(self) -> None:
+ """Start polling IMAP for inbound emails."""
+ if not self.config.consent_granted:
+ self.logger.warning(
+ "Email channel disabled: consent_granted is false. "
+ "Set channels.email.consentGranted=true after explicit user permission."
+ )
+ return
+
+ if not self._validate_config():
+ return
+
+ self._running = True
+ if not self.config.verify_dkim and not self.config.verify_spf:
+ self.logger.warning(
+ "DKIM and SPF verification are both DISABLED. "
+ "Emails with spoofed From headers will be accepted. "
+ "Set verify_dkim=true and verify_spf=true for anti-spoofing protection."
+ )
+ self.logger.info("Starting Email channel (IMAP polling mode)...")
+
+ poll_seconds = max(5, int(self.config.poll_interval_seconds))
+ while self._running:
+ try:
+ inbound_items, skipped_uids = await asyncio.to_thread(self._fetch_new_messages)
+ should_apply_post_action = self._should_apply_post_action()
+ post_actions_uids: set[str] = set()
+ for item in inbound_items:
+ sender = item["sender"]
+ subject = item.get("subject", "")
+ message_id = item.get("message_id", "")
+
+ if subject:
+ self._last_subject_by_chat[sender] = subject
+ if message_id:
+ self._last_message_id_by_chat[sender] = message_id
+
+ try:
+ await self._handle_message(
+ sender_id=sender,
+ chat_id=sender,
+ content=item["content"],
+ media=item.get("media") or None,
+ metadata=item.get("metadata", {}),
+ )
+ except Exception:
+ self.logger.exception("Error delivering email from {}", sender)
+ continue
+
+ uid = str((item.get("metadata") or {}).get("uid") or "")
+ if uid and should_apply_post_action:
+ post_actions_uids.add(uid)
+
+ if should_apply_post_action and not self.config.post_action_ignore_skipped:
+ post_actions_uids.update(skipped_uids)
+
+ if post_actions_uids:
+ await asyncio.to_thread(self._apply_post_actions_batch, sorted(post_actions_uids))
+ except Exception:
+ self.logger.exception("Polling error")
+
+ if not self._running:
+ break
+ await asyncio.sleep(poll_seconds)
+
+ async def stop(self) -> None:
+ """Stop polling loop."""
+ self._running = False
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send email via SMTP."""
+ if not self.config.consent_granted:
+ self.logger.warning("Skip email send: consent_granted is false")
+ return
+
+ if not self.config.smtp_host:
+ self.logger.warning("SMTP host not configured")
+ return
+
+ # Skip progress messages to prevent sending an empty email after each tool call
+ if (msg.metadata or {}).get("_progress"):
+ self.logger.debug("Skip progress message to {}", msg.chat_id)
+ return
+
+ to_addr = msg.chat_id.strip()
+ if not to_addr:
+ self.logger.warning("Missing recipient address")
+ return
+
+ # Determine if this is a reply (recipient has sent us an email before)
+ is_reply = to_addr in self._last_subject_by_chat
+ force_send = bool((msg.metadata or {}).get("force_send"))
+
+ # autoReplyEnabled only controls automatic replies, not proactive sends
+ if is_reply and not self.config.auto_reply_enabled and not force_send:
+ self.logger.info("Skip automatic reply to {}: auto_reply_enabled is false", to_addr)
+ return
+
+ base_subject = self._last_subject_by_chat.get(to_addr, "vibe-trading reply")
+ subject = self._reply_subject(base_subject)
+ if msg.metadata and isinstance(msg.metadata.get("subject"), str):
+ override = msg.metadata["subject"].strip()
+ if override:
+ subject = override
+
+ attachments: list[tuple[bytes, str, str, str]] = []
+ failed_attachments: list[str] = []
+ max_attachment_size = max(0, int(self.config.max_attachment_size))
+ max_attachment_count = max(0, int(self.config.max_attachments_per_email))
+ for media_path in msg.media or []:
+ path = Path(media_path)
+ filename = path.name or "attachment"
+ if len(attachments) >= max_attachment_count:
+ failed_attachments.append(f"[attachment: {filename} - too many attachments]")
+ self.logger.warning("Attachment count limit reached, skipping: {}", media_path)
+ continue
+ if not path.is_file():
+ failed_attachments.append(f"[attachment: {filename} - send failed]")
+ self.logger.warning("Attachment not found, skipping: {}", media_path)
+ continue
+ try:
+ size = path.stat().st_size
+ if max_attachment_size <= 0 or size > max_attachment_size:
+ failed_attachments.append(f"[attachment: {filename} - too large]")
+ self.logger.warning(
+ "Attachment too large, skipping: {} ({} > {} bytes)",
+ media_path,
+ size,
+ max_attachment_size,
+ )
+ continue
+ data = path.read_bytes()
+ ctype, _ = mimetypes.guess_type(str(path))
+ if ctype is None:
+ ctype = "application/octet-stream"
+ maintype, subtype = ctype.split("/", 1)
+ attachments.append((data, maintype, subtype, filename))
+ self.logger.info("Attached file: {}", filename)
+ except Exception:
+ failed_attachments.append(f"[attachment: {filename} - send failed]")
+ self.logger.exception("Failed to attach file {}", media_path)
+
+ content = msg.content or ""
+ if failed_attachments:
+ fallback = "\n".join(failed_attachments)
+ content = f"{content.rstrip()}\n\n{fallback}" if content.strip() else fallback
+
+ email_msg = EmailMessage()
+ email_msg["From"] = self.config.from_address or self.config.smtp_username or self.config.imap_username
+ email_msg["To"] = to_addr
+ email_msg["Subject"] = subject
+ email_msg.set_content(content)
+
+ for data, maintype, subtype, filename in attachments:
+ email_msg.add_attachment(
+ data,
+ maintype=maintype,
+ subtype=subtype,
+ filename=filename,
+ )
+
+ in_reply_to = self._last_message_id_by_chat.get(to_addr)
+ if in_reply_to:
+ email_msg["In-Reply-To"] = in_reply_to
+ email_msg["References"] = in_reply_to
+
+ try:
+ await asyncio.to_thread(self._smtp_send, email_msg)
+ except Exception:
+ self.logger.exception("Error sending to {}", to_addr)
+ raise
+
+ def _validate_config(self) -> bool:
+ missing = []
+ if not self.config.imap_host:
+ missing.append("imap_host")
+ if not self.config.imap_username:
+ missing.append("imap_username")
+ if not self.config.imap_password:
+ missing.append("imap_password")
+ if not self.config.smtp_host:
+ missing.append("smtp_host")
+ if not self.config.smtp_username:
+ missing.append("smtp_username")
+ if not self.config.smtp_password:
+ missing.append("smtp_password")
+
+ if self.config.post_action == "move" and not (self.config.post_action_move_mailbox or "").strip():
+ missing.append("post_action_move_mailbox")
+
+ if missing:
+ self.logger.error("Channel not configured, missing: {}", ', '.join(missing))
+ return False
+ return True
+
+ def _smtp_send(self, msg: EmailMessage) -> None:
+ timeout = 30
+ if self.config.smtp_use_ssl:
+ with smtplib.SMTP_SSL(
+ self.config.smtp_host,
+ self.config.smtp_port,
+ timeout=timeout,
+ ) as smtp:
+ smtp.login(self.config.smtp_username, self.config.smtp_password)
+ smtp.send_message(msg)
+ return
+
+ with smtplib.SMTP(self.config.smtp_host, self.config.smtp_port, timeout=timeout) as smtp:
+ if self.config.smtp_use_tls:
+ smtp.starttls(context=ssl.create_default_context())
+ smtp.login(self.config.smtp_username, self.config.smtp_password)
+ smtp.send_message(msg)
+
+ def _fetch_new_messages(self) -> tuple[list[dict[str, Any]], set[str]]:
+ """Poll IMAP and return parsed unread messages plus skipped message UIDs."""
+ return self._fetch_messages(
+ search_criteria=("UNSEEN",),
+ mark_seen=self.config.mark_seen,
+ dedupe=True,
+ limit=0,
+ )
+
+ def fetch_messages_between_dates(
+ self,
+ start_date: date,
+ end_date: date,
+ limit: int = 20,
+ ) -> list[dict[str, Any]]:
+ """
+ Fetch messages in [start_date, end_date) by IMAP date search.
+
+ This is used for historical summarization tasks (e.g. "yesterday").
+ """
+ if end_date <= start_date:
+ return []
+
+ messages, _ = self._fetch_messages(
+ search_criteria=(
+ "SINCE",
+ self._format_imap_date(start_date),
+ "BEFORE",
+ self._format_imap_date(end_date),
+ ),
+ mark_seen=False,
+ dedupe=False,
+ limit=max(1, int(limit)),
+ )
+ return messages
+
+ def _fetch_messages(
+ self,
+ search_criteria: tuple[str, ...],
+ mark_seen: bool,
+ dedupe: bool,
+ limit: int,
+ ) -> tuple[list[dict[str, Any]], set[str]]:
+ messages: list[dict[str, Any]] = []
+ skipped_uids: set[str] = set()
+ cycle_uids: set[str] = set()
+
+ for attempt in range(2):
+ try:
+ self._fetch_messages_once(
+ search_criteria,
+ mark_seen,
+ dedupe,
+ limit,
+ messages,
+ skipped_uids,
+ cycle_uids,
+ )
+ return messages, skipped_uids
+ except Exception as exc:
+ if attempt == 1 or not self._is_stale_imap_error(exc):
+ raise
+ self.logger.warning("IMAP connection went stale, retrying once: {}", exc)
+
+ return messages, skipped_uids
+
+ def _fetch_messages_once(
+ self,
+ search_criteria: tuple[str, ...],
+ mark_seen: bool,
+ dedupe: bool,
+ limit: int,
+ messages: list[dict[str, Any]],
+ skipped_uids: set[str],
+ cycle_uids: set[str],
+ ) -> None:
+ """Fetch messages by arbitrary IMAP search criteria."""
+ mailbox = self.config.imap_mailbox or "INBOX"
+
+ client = self._open_imap_client(mailbox=mailbox, missing_mailbox_ok=True)
+ if client is None:
+ return messages
+
+ try:
+ status, data = client.search(None, *search_criteria)
+ if status != "OK" or not data:
+ return messages
+
+ ids = data[0].split()
+ if limit > 0 and len(ids) > limit:
+ ids = ids[-limit:]
+ for imap_id in ids:
+ status, fetched = client.fetch(imap_id, "(BODY.PEEK[] UID)")
+ if status != "OK" or not fetched:
+ continue
+
+ raw_bytes = self._extract_message_bytes(fetched)
+ if raw_bytes is None:
+ continue
+
+ uid = self._extract_uid(fetched)
+ if uid and uid in cycle_uids:
+ continue
+ if dedupe and uid and uid in self._processed_uids:
+ continue
+
+ parsed = BytesParser(policy=policy.default).parsebytes(raw_bytes)
+ sender = parseaddr(parsed.get("From", ""))[1].strip().lower()
+ if not sender:
+ continue
+ if self._is_self_address(sender):
+ self.logger.info("From {} ignored: matches bot-owned address", sender)
+ self._remember_processed_uid(uid, dedupe, cycle_uids)
+ if mark_seen:
+ client.store(imap_id, "+FLAGS", "\\Seen")
+ if uid:
+ skipped_uids.add(uid)
+ continue
+
+ # --- Anti-spoofing: verify Authentication-Results ---
+ spf_pass, dkim_pass = self._check_authentication_results(parsed)
+ if self.config.verify_spf and not spf_pass:
+ self.logger.warning(
+ "From {} rejected: SPF verification failed "
+ "(no 'spf=pass' in Authentication-Results header)",
+ sender,
+ )
+ self._remember_processed_uid(uid, dedupe, cycle_uids)
+ if uid:
+ skipped_uids.add(uid)
+ continue
+ if self.config.verify_dkim and not dkim_pass:
+ self.logger.warning(
+ "From {} rejected: DKIM verification failed "
+ "(no 'dkim=pass' in Authentication-Results header)",
+ sender,
+ )
+ self._remember_processed_uid(uid, dedupe, cycle_uids)
+ if uid:
+ skipped_uids.add(uid)
+ continue
+
+ if not self.is_allowed(sender):
+ self._remember_processed_uid(uid, dedupe, cycle_uids)
+ if mark_seen:
+ client.store(imap_id, "+FLAGS", "\\Seen")
+ if uid:
+ skipped_uids.add(uid)
+ continue
+
+ subject = self._decode_header_value(parsed.get("Subject", ""))
+ date_value = parsed.get("Date", "")
+ message_id = parsed.get("Message-ID", "").strip()
+ body = self._extract_text_body(parsed)
+
+ if not body:
+ body = "(empty email body)"
+
+ body = body[: self.config.max_body_chars]
+ content = (
+ f"[EMAIL-CONTEXT] Email received.\n"
+ f"From: {sender}\n"
+ f"Subject: {subject}\n"
+ f"Date: {date_value}\n\n"
+ f"{body}"
+ )
+
+ # --- Attachment extraction ---
+ attachment_paths: list[str] = []
+ if self.config.allowed_attachment_types:
+ saved = self._extract_attachments(
+ parsed,
+ uid or "noid",
+ allowed_types=self.config.allowed_attachment_types,
+ max_size=self.config.max_attachment_size,
+ max_count=self.config.max_attachments_per_email,
+ )
+ for p in saved:
+ attachment_paths.append(str(p))
+ content += f"\n[attachment: {p.name} — saved to {p}]"
+
+ metadata = {
+ "message_id": message_id,
+ "subject": subject,
+ "date": date_value,
+ "sender_email": sender,
+ "uid": uid,
+ }
+ messages.append(
+ {
+ "sender": sender,
+ "subject": subject,
+ "message_id": message_id,
+ "content": content,
+ "metadata": metadata,
+ "media": attachment_paths,
+ }
+ )
+
+ self._remember_processed_uid(uid, dedupe, cycle_uids)
+
+ if mark_seen:
+ client.store(imap_id, "+FLAGS", "\\Seen")
+ finally:
+ self._close_imap_client(client)
+
+ def _open_imap_client(self, mailbox: str, *, missing_mailbox_ok: bool = False) -> Any | None:
+ if self.config.imap_use_ssl:
+ client: Any = imaplib.IMAP4_SSL(self.config.imap_host, self.config.imap_port)
+ else:
+ client = imaplib.IMAP4(self.config.imap_host, self.config.imap_port)
+
+ try:
+ client.login(self.config.imap_username, self.config.imap_password)
+ try:
+ status, _ = client.select(mailbox)
+ except Exception as exc:
+ if missing_mailbox_ok and self._is_missing_mailbox_error(exc):
+ self.logger.warning("Mailbox unavailable, skipping poll for {}: {}", mailbox, exc)
+ self._close_imap_client(client)
+ return None
+ raise
+
+ if status != "OK":
+ self.logger.warning("Mailbox select returned {}, skipping poll for {}", status, mailbox)
+ self._close_imap_client(client)
+ return None
+ except Exception:
+ self._close_imap_client(client)
+ raise
+
+ return client
+
+ @staticmethod
+ def _close_imap_client(client: Any) -> None:
+ with suppress(Exception):
+ client.logout()
+
+ def _collect_self_addresses(self) -> set[str]:
+ """Return normalized email addresses owned by this channel instance."""
+ candidates = (
+ self.config.from_address,
+ self.config.smtp_username,
+ self.config.imap_username,
+ )
+ normalized = {
+ addr
+ for candidate in candidates
+ if (addr := self._normalize_address(candidate))
+ }
+ return normalized
+
+ @staticmethod
+ def _normalize_address(value: str) -> str:
+ """Normalize an address or mailbox-like identifier for comparisons."""
+ raw = (value or "").strip()
+ if not raw:
+ return ""
+ parsed = parseaddr(raw)[1].strip().lower()
+ if parsed:
+ return parsed
+ if "@" in raw:
+ return raw.lower()
+ return ""
+
+ def _is_self_address(self, sender: str) -> bool:
+ """Return True when an inbound sender belongs to the bot itself."""
+ normalized_sender = self._normalize_address(sender)
+ return bool(normalized_sender) and normalized_sender in self._self_addresses
+
+ def _remember_processed_uid(self, uid: str, dedupe: bool, cycle_uids: set[str]) -> None:
+ """Track a fetched UID so skipped messages are not reprocessed forever."""
+ if not uid:
+ return
+ cycle_uids.add(uid)
+ if dedupe:
+ self._processed_uids.add(uid)
+ # mark_seen is the primary dedup; this set is a safety net
+ if len(self._processed_uids) > self._MAX_PROCESSED_UIDS:
+ # Evict a random half to cap memory; mark_seen is the primary dedup
+ self._processed_uids = set(list(self._processed_uids)[len(self._processed_uids) // 2:])
+
+ def _should_apply_post_action(self) -> bool:
+ return self.config.post_action in {"delete", "move"}
+
+ def _apply_post_actions_batch(self, post_actions_uids: list[str]) -> None:
+ if not self._should_apply_post_action() or not post_actions_uids:
+ return
+
+ mailbox = self.config.imap_mailbox or "INBOX"
+ client = self._open_imap_client(mailbox=mailbox)
+ if client is None:
+ return
+
+ try:
+ features = self._server_features(client)
+ # Apply all post-actions in one IMAP session. `features` also carries
+ # session-learned behavior (e.g. UID STORE support) so later UIDs can
+ # skip known-broken paths.
+ for uid in post_actions_uids:
+ if uid:
+ self._apply_post_action(client, uid, features)
+ finally:
+ self._close_imap_client(client)
+
+ def _apply_post_action(
+ self,
+ client: Any,
+ uid: str,
+ features: _ServerFeatures,
+ ) -> None:
+ action = self.config.post_action
+
+ if action == "delete":
+ if not self._uid_store_deleted(client, uid, features):
+ return
+ self._uid_expunge_or_fallback(client, uid, features)
+ return
+
+ if action == "move":
+ target = (self.config.post_action_move_mailbox or "").strip()
+ if features.move:
+ status, _ = client.uid("MOVE", uid, target)
+ if status != "OK":
+ self.logger.warning("Post-action move failed (UID MOVE) for UID {} to mailbox {}", uid, target)
+ return
+
+ status, _ = client.uid("COPY", uid, target)
+ if status != "OK":
+ self.logger.warning("Post-action move failed (UID COPY) for UID {} to mailbox {}", uid, target)
+ return
+ if not self._uid_store_deleted(client, uid, features):
+ return
+ self._uid_expunge_or_fallback(client, uid, features)
+
+ @staticmethod
+ def _server_features(client: Any) -> _ServerFeatures:
+ caps: set[str] = set()
+ with suppress(Exception):
+ status, data = client.capability()
+ if status == "OK" and data:
+ for raw in data:
+ if isinstance(raw, (bytes, bytearray)):
+ caps.update(token.upper() for token in raw.decode("utf-8", errors="ignore").split())
+ elif isinstance(raw, str):
+ caps.update(token.upper() for token in raw.split())
+ return _ServerFeatures(move="MOVE" in caps, uidplus="UIDPLUS" in caps)
+
+ @staticmethod
+ def _lookup_imap_id_by_uid(client: Any, uid: str) -> bytes | None:
+ # IMAP exposes two message identifiers: UID (stable) and sequence number
+ # (session-local). We target by UID first, but some servers may reject
+ # UID STORE. In that case we resolve the current sequence number for the
+ # UID and retry with STORE using that sequence id.
+ status, data = client.search(None, "UID", uid)
+ if status != "OK" or not data or not data[0]:
+ return None
+ return data[0].split()[0]
+
+ def _uid_store_deleted(self, client: Any, uid: str, features: _ServerFeatures) -> bool:
+ # Optimistic path: try UID STORE first because UID is stable and avoids
+ # sequence-number lookup. If this fails once for the session, remember it
+ # and use the sequence STORE fallback directly for remaining UIDs.
+ if features.uid_store is not False:
+ status, _ = client.uid("STORE", uid, "+FLAGS", "(\\Deleted)")
+ if status == "OK":
+ features.uid_store = True
+ return True
+ features.uid_store = False
+
+ # Compatibility fallback for servers where UID STORE is unavailable or
+ # unreliable: resolve the current sequence number from UID and use STORE.
+ imap_id = self._lookup_imap_id_by_uid(client, uid)
+ if not imap_id:
+ self.logger.warning("Post-action skipped: UID {} not found", uid)
+ return False
+
+ status, _ = client.store(imap_id, "+FLAGS", "\\Deleted")
+ if status != "OK":
+ self.logger.warning("Post-action failed: could not mark UID {} as deleted", uid)
+ return False
+ return True
+
+ def _uid_expunge_or_fallback(self, client: Any, uid: str, features: _ServerFeatures) -> None:
+ # Prefer UID-scoped expunge when supported to avoid expunging unrelated
+ # messages already marked \Deleted in the selected mailbox.
+ if features.uidplus:
+ status, _ = client.uid("EXPUNGE", uid)
+ if status == "OK":
+ return
+ self.logger.warning("UID EXPUNGE failed for UID {}, falling back to EXPUNGE", uid)
+ if self.config.post_action_expunge:
+ client.expunge()
+
+ @classmethod
+ def _is_stale_imap_error(cls, exc: Exception) -> bool:
+ message = str(exc).lower()
+ return any(marker in message for marker in cls._IMAP_RECONNECT_MARKERS)
+
+ @classmethod
+ def _is_missing_mailbox_error(cls, exc: Exception) -> bool:
+ message = str(exc).lower()
+ return any(marker in message for marker in cls._IMAP_MISSING_MAILBOX_MARKERS)
+
+ @classmethod
+ def _format_imap_date(cls, value: date) -> str:
+ """Format date for IMAP search (always English month abbreviations)."""
+ month = cls._IMAP_MONTHS[value.month - 1]
+ return f"{value.day:02d}-{month}-{value.year}"
+
+ @staticmethod
+ def _extract_message_bytes(fetched: list[Any]) -> bytes | None:
+ for item in fetched:
+ if isinstance(item, tuple) and len(item) >= 2 and isinstance(item[1], (bytes, bytearray)):
+ return bytes(item[1])
+ return None
+
+ @staticmethod
+ def _extract_uid(fetched: list[Any]) -> str:
+ for item in fetched:
+ if isinstance(item, tuple) and item and isinstance(item[0], (bytes, bytearray)):
+ head = bytes(item[0]).decode("utf-8", errors="ignore")
+ m = re.search(r"UID\s+(\d+)", head)
+ if m:
+ return m.group(1)
+ return ""
+
+ @staticmethod
+ def _decode_header_value(value: str) -> str:
+ if not value:
+ return ""
+ try:
+ return str(make_header(decode_header(value)))
+ except Exception:
+ return value
+
+ @classmethod
+ def _extract_text_body(cls, msg: Any) -> str:
+ """Best-effort extraction of readable body text."""
+ if msg.is_multipart():
+ plain_parts: list[str] = []
+ html_parts: list[str] = []
+ for part in msg.walk():
+ if part.get_content_disposition() == "attachment":
+ continue
+ content_type = part.get_content_type()
+ try:
+ payload = part.get_content()
+ except Exception:
+ payload_bytes = part.get_payload(decode=True) or b""
+ charset = part.get_content_charset() or "utf-8"
+ payload = payload_bytes.decode(charset, errors="replace")
+ if not isinstance(payload, str):
+ continue
+ if content_type == "text/plain":
+ plain_parts.append(payload)
+ elif content_type == "text/html":
+ html_parts.append(payload)
+ if plain_parts:
+ return "\n\n".join(plain_parts).strip()
+ if html_parts:
+ return cls._html_to_text("\n\n".join(html_parts)).strip()
+ return ""
+
+ try:
+ payload = msg.get_content()
+ except Exception:
+ payload_bytes = msg.get_payload(decode=True) or b""
+ charset = msg.get_content_charset() or "utf-8"
+ payload = payload_bytes.decode(charset, errors="replace")
+ if not isinstance(payload, str):
+ return ""
+ if msg.get_content_type() == "text/html":
+ return cls._html_to_text(payload).strip()
+ return payload.strip()
+
+ @staticmethod
+ def _check_authentication_results(parsed_msg: Any) -> tuple[bool, bool]:
+ """Parse Authentication-Results headers for SPF and DKIM verdicts.
+
+ Returns:
+ A tuple of (spf_pass, dkim_pass) booleans.
+ """
+ spf_pass = False
+ dkim_pass = False
+ for ar_header in parsed_msg.get_all("Authentication-Results") or []:
+ ar_lower = ar_header.lower()
+ if re.search(r"\bspf\s*=\s*pass\b", ar_lower):
+ spf_pass = True
+ if re.search(r"\bdkim\s*=\s*pass\b", ar_lower):
+ dkim_pass = True
+ return spf_pass, dkim_pass
+
+ @classmethod
+ def _extract_attachments(
+ cls,
+ msg: Any,
+ uid: str,
+ *,
+ allowed_types: list[str],
+ max_size: int,
+ max_count: int,
+ ) -> list[Path]:
+ """Extract and save email attachments to the media directory.
+
+ Returns list of saved file paths.
+ """
+ if not msg.is_multipart():
+ return []
+
+ saved: list[Path] = []
+ media_dir = get_media_dir("email")
+
+ for part in msg.walk():
+ if len(saved) >= max_count:
+ break
+ if part.get_content_disposition() != "attachment":
+ continue
+
+ content_type = part.get_content_type()
+ if not any(fnmatch(content_type, pat) for pat in allowed_types):
+ logger.debug("Attachment skipped (type {}): not in allowed list", content_type)
+ continue
+
+ payload = part.get_payload(decode=True)
+ if payload is None:
+ continue
+ if len(payload) > max_size:
+ logger.warning(
+ "Attachment skipped: size {} exceeds limit {}",
+ len(payload),
+ max_size,
+ )
+ continue
+
+ raw_name = part.get_filename() or "attachment"
+ sanitized = safe_filename(raw_name) or "attachment"
+ dest = media_dir / f"{uid}_{sanitized}"
+
+ try:
+ dest.write_bytes(payload)
+ saved.append(dest)
+ logger.info("Attachment saved: {}", dest)
+ except Exception as exc:
+ logger.warning("Failed to save attachment {}: {}", dest, exc)
+
+ return saved
+
+ @staticmethod
+ def _html_to_text(raw_html: str) -> str:
+ text = re.sub(r"<\s*br\s*/?>", "\n", raw_html, flags=re.IGNORECASE)
+ text = re.sub(r"<\s*/\s*p\s*>", "\n", text, flags=re.IGNORECASE)
+ text = re.sub(r"<[^>]+>", "", text)
+ return html.unescape(text)
+
+ def _reply_subject(self, base_subject: str) -> str:
+ subject = (base_subject or "").strip() or "vibe-trading reply"
+ prefix = self.config.subject_prefix or "Re: "
+ if subject.lower().startswith("re:"):
+ return subject
+ return f"{prefix}{subject}"
diff --git a/agent/src/channels/feishu.py b/agent/src/channels/feishu.py
new file mode 100644
index 000000000..5df6c52e6
--- /dev/null
+++ b/agent/src/channels/feishu.py
@@ -0,0 +1,2352 @@
+"""Feishu/Lark channel implementation using lark-oapi SDK with WebSocket long connection."""
+
+from __future__ import annotations
+
+import asyncio
+import importlib.util
+import json
+import os
+import re
+import threading
+import time
+import uuid
+from collections import OrderedDict
+from contextlib import suppress
+from dataclasses import dataclass
+from typing import TYPE_CHECKING, Any, Literal
+
+from pydantic import Field
+from rich.console import Console
+from rich.markup import escape
+from rich.panel import Panel
+from rich.text import Text
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.utils import safe_filename
+# logging_bridge not needed (using stdlib logging)
+
+if TYPE_CHECKING:
+ from lark_oapi.api.im.v1.model import MentionEvent, P2ImMessageReceiveV1
+
+FEISHU_AVAILABLE = importlib.util.find_spec("lark_oapi") is not None
+_LOGIN_CONSOLE = Console()
+
+
+def _load_lark_runtime() -> tuple[Any, str, str]:
+ """Import the heavy Feishu SDK lazily.
+
+ lark_oapi imports a large generated API surface at module import time, so
+ keep it out of channel discovery and constructor paths.
+ """
+ import sys
+
+ ws_client_already_imported = "lark_oapi.ws.client" in sys.modules
+ import lark_oapi as lark
+ import lark_oapi.ws.client as lark_ws_client
+ from lark_oapi.core.const import FEISHU_DOMAIN, LARK_DOMAIN
+
+ if (
+ not ws_client_already_imported
+ and threading.current_thread() is not threading.main_thread()
+ ):
+ import_loop = getattr(lark_ws_client, "loop", None)
+ if (
+ import_loop is not None
+ and not import_loop.is_running()
+ and not import_loop.is_closed()
+ ):
+ import_loop.close()
+ lark_ws_client.loop = None
+ with suppress(Exception):
+ asyncio.set_event_loop(None)
+
+ return lark, FEISHU_DOMAIN, LARK_DOMAIN
+
+# Message type display mapping
+MSG_TYPE_MAP = {
+ "image": "[image]",
+ "audio": "[audio]",
+ "file": "[file]",
+ "sticker": "[sticker]",
+}
+
+
+def _extract_share_card_content(content_json: dict, msg_type: str) -> str:
+ """Extract text representation from share cards and interactive messages."""
+ parts = []
+
+ if msg_type == "share_chat":
+ parts.append(f"[shared chat: {content_json.get('chat_id', '')}]")
+ elif msg_type == "share_user":
+ parts.append(f"[shared user: {content_json.get('user_id', '')}]")
+ elif msg_type == "interactive":
+ parts.extend(_extract_interactive_content(content_json))
+ elif msg_type == "share_calendar_event":
+ parts.append(f"[shared calendar event: {content_json.get('event_key', '')}]")
+ elif msg_type == "system":
+ parts.append("[system message]")
+ elif msg_type == "merge_forward":
+ parts.append("[merged forward messages]")
+
+ return "\n".join(parts) if parts else f"[{msg_type}]"
+
+
+def _extract_interactive_content(content: dict) -> list[str]:
+ """Recursively extract text and links from interactive card content."""
+ parts = []
+
+ if isinstance(content, str):
+ try:
+ content = json.loads(content)
+ except (json.JSONDecodeError, TypeError):
+ return [content] if content.strip() else []
+
+ if not isinstance(content, dict):
+ return parts
+
+ # user_dsl: original card definition (richest source for rendered cards)
+ user_dsl = content.get("user_dsl")
+ if isinstance(user_dsl, str) and user_dsl.strip():
+ try:
+ dsl = json.loads(user_dsl)
+ if isinstance(dsl, dict):
+ parts.extend(_extract_interactive_content(dsl))
+ if parts:
+ return parts
+ except (json.JSONDecodeError, TypeError):
+ pass
+
+ if "title" in content:
+ title = content["title"]
+ if isinstance(title, dict):
+ title_content = title.get("content", "") or title.get("text", "")
+ if title_content:
+ parts.append(f"title: {title_content}")
+ elif isinstance(title, str):
+ parts.append(f"title: {title}")
+
+ # Top-level elements: flat list or nested list format
+ elements = content.get("elements")
+ if isinstance(elements, list):
+ if elements and isinstance(elements[0], list):
+ # Nested list: [[{tag:"text",text:"..."}], ...]
+ for row in elements:
+ if isinstance(row, list):
+ for element in row:
+ parts.extend(_extract_element_content(element))
+ else:
+ # Flat list: [{tag:"markdown",content:"..."}, ...]
+ for element in elements:
+ parts.extend(_extract_element_content(element))
+
+ # Body elements (schema 2.0)
+ body = content.get("body", {})
+ if isinstance(body, dict):
+ body_elements = body.get("elements")
+ if isinstance(body_elements, list):
+ for element in body_elements:
+ parts.extend(_extract_element_content(element))
+
+ card = content.get("card", {})
+ if card:
+ parts.extend(_extract_interactive_content(card))
+
+ header = content.get("header", {})
+ if header:
+ header_title = header.get("title", {})
+ if isinstance(header_title, dict):
+ header_text = header_title.get("content", "") or header_title.get("text", "")
+ if header_text:
+ parts.append(f"title: {header_text}")
+
+ return parts
+
+
+def _extract_element_content(element: dict) -> list[str]:
+ """Extract content from a single card element."""
+ parts = []
+
+ if not isinstance(element, dict):
+ return parts
+
+ tag = element.get("tag", "")
+
+ if tag in ("markdown", "lark_md"):
+ content = element.get("content", "")
+ if content:
+ parts.append(content)
+
+ elif tag == "text":
+ text = element.get("text", "")
+ if isinstance(text, str) and text.strip():
+ parts.append(text)
+
+ elif tag == "div":
+ text = element.get("text", {})
+ if isinstance(text, dict):
+ text_content = text.get("content", "") or text.get("text", "")
+ if text_content:
+ parts.append(text_content)
+ elif isinstance(text, str):
+ parts.append(text)
+ for field in element.get("fields", []):
+ if isinstance(field, dict):
+ field_text = field.get("text", {})
+ if isinstance(field_text, dict):
+ c = field_text.get("content", "")
+ if c:
+ parts.append(c)
+
+ elif tag == "a":
+ href = element.get("href", "")
+ text = element.get("text", "")
+ if href:
+ parts.append(f"link: {href}")
+ if text:
+ parts.append(text)
+
+ elif tag == "button":
+ text = element.get("text", {})
+ if isinstance(text, dict):
+ c = text.get("content", "")
+ if c:
+ parts.append(c)
+ url = element.get("url", "") or element.get("multi_url", {}).get("url", "")
+ if url:
+ parts.append(f"link: {url}")
+
+ elif tag == "img":
+ alt = element.get("alt", {})
+ parts.append(alt.get("content", "[image]") if isinstance(alt, dict) else "[image]")
+
+ elif tag == "note":
+ for ne in element.get("elements", []):
+ parts.extend(_extract_element_content(ne))
+
+ elif tag == "column_set":
+ for col in element.get("columns", []):
+ for ce in col.get("elements", []):
+ parts.extend(_extract_element_content(ce))
+
+ elif tag == "plain_text":
+ content = element.get("content", "")
+ if content:
+ parts.append(content)
+
+ elif tag == "table":
+ columns = [
+ (column["name"], str(column.get("display_name") or column["name"]))
+ for column in (element.get("columns") or [])
+ if isinstance(column, dict) and column.get("name")
+ ]
+ rows = element.get("rows", [])
+ if columns:
+ parts.append(" | ".join(header for _, header in columns))
+ if isinstance(rows, list):
+ for row in rows:
+ if not isinstance(row, dict):
+ continue
+ values = []
+ for name, _ in columns:
+ value = row.get(name)
+ if isinstance(value, list):
+ value = " ".join(str(item).strip() for item in value if item is not None)
+ values.append("" if value is None else str(value).strip())
+ row_text = " | ".join(values).strip()
+ if row_text:
+ parts.append(row_text)
+
+ else:
+ for ne in element.get("elements", []):
+ parts.extend(_extract_element_content(ne))
+
+ return parts
+
+
+def _extract_post_content(content_json: dict) -> tuple[str, list[str]]:
+ """Extract text and image keys from Feishu post (rich text) message.
+
+ Handles three payload shapes:
+ - Direct: {"title": "...", "content": [[...]]}
+ - Localized: {"zh_cn": {"title": "...", "content": [...]}}
+ - Wrapped: {"post": {"zh_cn": {"title": "...", "content": [...]}}}
+ """
+
+ def _parse_block(block: dict) -> tuple[str | None, list[str]]:
+ if not isinstance(block, dict) or not isinstance(block.get("content"), list):
+ return None, []
+ texts, images = [], []
+ if title := block.get("title"):
+ texts.append(title)
+ for row in block["content"]:
+ if not isinstance(row, list):
+ continue
+ for el in row:
+ if not isinstance(el, dict):
+ continue
+ tag = el.get("tag")
+ if tag in ("text", "a"):
+ texts.append(el.get("text", ""))
+ elif tag == "at":
+ texts.append(f"@{el.get('user_name', 'user')}")
+ elif tag == "code_block":
+ lang = el.get("language", "")
+ code_text = el.get("text", "")
+ texts.append(f"\n```{lang}\n{code_text}\n```\n")
+ elif tag == "img" and (key := el.get("image_key")):
+ images.append(key)
+ return (" ".join(texts).strip() or None), images
+
+ # Unwrap optional {"post": ...} envelope
+ root = content_json
+ if isinstance(root, dict) and isinstance(root.get("post"), dict):
+ root = root["post"]
+ if not isinstance(root, dict):
+ return "", []
+
+ # Direct format
+ if "content" in root:
+ text, imgs = _parse_block(root)
+ if text or imgs:
+ return text or "", imgs
+
+ # Localized: prefer known locales, then fall back to any dict child
+ for key in ("zh_cn", "en_us", "ja_jp"):
+ if key in root:
+ text, imgs = _parse_block(root[key])
+ if text or imgs:
+ return text or "", imgs
+ for val in root.values():
+ if isinstance(val, dict):
+ text, imgs = _parse_block(val)
+ if text or imgs:
+ return text or "", imgs
+
+ return "", []
+
+
+def _extract_post_text(content_json: dict) -> str:
+ """Extract plain text from Feishu post (rich text) message content.
+
+ Legacy wrapper for _extract_post_content, returns only text.
+ """
+ text, _ = _extract_post_content(content_json)
+ return text
+
+
+class FeishuConfig(BaseModel):
+ """Feishu/Lark channel configuration using WebSocket long connection."""
+
+ enabled: bool = False
+ app_id: str = ""
+ app_secret: str = ""
+ encrypt_key: str = ""
+ verification_token: str = ""
+ allow_from: list[str] = Field(default_factory=list)
+ react_emoji: str = "THUMBSUP"
+ done_emoji: str | None = None # Emoji to show when task is completed (e.g., "DONE", "OK")
+ tool_hint_prefix: str = "\U0001f527" # Prefix for inline tool hints (default: 🔧)
+ group_policy: Literal["open", "mention"] = "mention"
+ reply_to_message: bool = False # If True, bot replies quote the user's original message
+ streaming: bool = True
+ domain: Literal["feishu", "lark"] = "feishu" # Set to "lark" for international Lark
+ topic_isolation: bool = True # If True, each topic in group chat gets its own session (isolation)
+
+
+# =============================================================================
+# QR scan-to-create onboarding
+#
+# Device-code flow: user scans a QR code with the Feishu/Lark mobile app and
+# the platform creates a fully configured bot application automatically.
+# =============================================================================
+
+_ONBOARD_ACCOUNTS_URLS = {
+ "feishu": "https://accounts.feishu.cn",
+ "lark": "https://accounts.larksuite.com",
+}
+_REGISTRATION_PATH = "/oauth/v1/app/registration"
+_ONBOARD_REQUEST_TIMEOUT_S = 10
+
+
+def _accounts_base_url(domain: str) -> str:
+ return _ONBOARD_ACCOUNTS_URLS.get(domain, _ONBOARD_ACCOUNTS_URLS["feishu"])
+
+
+def _post_registration(base_url: str, body: dict[str, str]) -> dict:
+ """POST form-encoded data to the registration endpoint, return parsed JSON.
+
+ The registration endpoint returns JSON even on HTTP errors (e.g. poll
+ returns authorization_pending as a 400). We always parse the body.
+ """
+ import httpx
+
+ url = f"{base_url}{_REGISTRATION_PATH}"
+ resp = httpx.post(
+ url,
+ data=body,
+ timeout=_ONBOARD_REQUEST_TIMEOUT_S,
+ headers={"Content-Type": "application/x-www-form-urlencoded"},
+ )
+ try:
+ return resp.json()
+ except json.JSONDecodeError:
+ resp.raise_for_status()
+ return {}
+
+
+def _init_registration(domain: str = "feishu") -> None:
+ """Verify the environment supports client_secret auth. Raises RuntimeError if not."""
+ base_url = _accounts_base_url(domain)
+ res = _post_registration(base_url, {"action": "init"})
+ methods = res.get("supported_auth_methods") or []
+ if "client_secret" not in methods:
+ raise RuntimeError(
+ f"Feishu / Lark registration does not support client_secret auth. "
+ f"Supported: {methods}"
+ )
+
+
+def _begin_registration(domain: str = "feishu") -> dict:
+ """Start the device-code flow. Returns device_code, qr_url, interval, expire_in."""
+ base_url = _accounts_base_url(domain)
+ res = _post_registration(base_url, {
+ "action": "begin",
+ "archetype": "PersonalAgent",
+ "auth_method": "client_secret",
+ "request_user_info": "open_id",
+ })
+ device_code = res.get("device_code")
+ if not device_code:
+ raise RuntimeError("Feishu / Lark registration did not return a device_code")
+ qr_url = res.get("verification_uri_complete", "")
+ if not qr_url:
+ raise RuntimeError("Feishu / Lark registration did not return a login URL")
+ return {
+ "device_code": device_code,
+ "qr_url": qr_url,
+ "interval": res.get("interval") or 5,
+ "expire_in": res.get("expire_in") or 600,
+ }
+
+
+def _poll_registration(
+ *,
+ device_code: str,
+ interval: int,
+ expire_in: int,
+ domain: str = "feishu",
+) -> dict | None:
+ """Poll until the user scans the QR code, or timeout/denial.
+
+ Returns dict with app_id, app_secret, domain on success, None on failure.
+ """
+ deadline = time.monotonic() + expire_in
+ current_domain = domain
+ poll_count = 0
+
+ while time.monotonic() < deadline:
+ base_url = _accounts_base_url(current_domain)
+ try:
+ res = _post_registration(base_url, {
+ "action": "poll",
+ "device_code": device_code,
+ "tp": "ob_app",
+ })
+ except Exception:
+ time.sleep(interval)
+ continue
+
+ poll_count += 1
+
+ # Domain auto-detection: if the user's tenant is on Lark, switch automatically
+ user_info = res.get("user_info") or {}
+ tenant_brand = user_info.get("tenant_brand")
+ if tenant_brand == "lark":
+ current_domain = "lark"
+
+ # Success
+ if res.get("client_id") and res.get("client_secret"):
+ return {
+ "app_id": res["client_id"],
+ "app_secret": res["client_secret"],
+ "domain": current_domain,
+ }
+
+ # Terminal errors
+ error = res.get("error", "")
+ if error in ("access_denied", "expired_token"):
+ _LOGIN_CONSOLE.print("[yellow]Authorization was cancelled or expired.[/yellow]")
+ return None
+
+ # authorization_pending or unknown — keep polling
+ time.sleep(interval)
+
+ _LOGIN_CONSOLE.print("[yellow]Authorization timed out.[/yellow]")
+ return None
+
+
+def qr_register(
+ *,
+ initial_domain: str = "feishu",
+) -> dict | None:
+ """Run the Feishu / Lark scan-to-create QR registration flow.
+
+ Returns on success:
+ {
+ "app_id": str,
+ "app_secret": str,
+ "domain": "feishu" | "lark",
+ }
+
+ Returns None on expected failures (network, auth denied, timeout).
+ Unexpected errors (bugs, protocol regressions) propagate to the caller.
+ """
+ import httpx
+
+ try:
+ return _qr_register_inner(initial_domain=initial_domain)
+ except (RuntimeError, OSError, json.JSONDecodeError, httpx.HTTPError) as exc:
+ _LOGIN_CONSOLE.print(
+ f"[yellow]Unable to start Feishu/Lark login:[/yellow] {escape(str(exc))}"
+ )
+ return None
+
+
+def _print_qr_code(url: str) -> None:
+ """Print QR code as ASCII art if qrcode package is available, otherwise print URL."""
+ try:
+ import qrcode as qr_lib
+
+ _LOGIN_CONSOLE.print("\n[bold]Scan with Feishu or Lark[/bold]\n")
+ qr = qr_lib.QRCode(border=1)
+ qr.add_data(url)
+ qr.make(fit=True)
+ qr.print_ascii(invert=True)
+ _LOGIN_CONSOLE.print()
+ except ImportError:
+ _LOGIN_CONSOLE.print()
+ _LOGIN_CONSOLE.print(Panel.fit(Text(url), title="Open with Feishu or Lark", border_style="cyan"))
+ _LOGIN_CONSOLE.print()
+
+
+def _qr_register_inner(
+ *,
+ initial_domain: str,
+) -> dict | None:
+ """Run init → begin → poll. Raises on network/protocol errors."""
+ _LOGIN_CONSOLE.print("[cyan]Preparing Feishu/Lark login...[/cyan]")
+ _init_registration(initial_domain)
+ begin = _begin_registration(initial_domain)
+
+ _print_qr_code(begin["qr_url"])
+
+ with _LOGIN_CONSOLE.status("Waiting for authorization in Feishu/Lark...", spinner="dots"):
+ return _poll_registration(
+ device_code=begin["device_code"],
+ interval=begin["interval"],
+ expire_in=begin["expire_in"],
+ domain=initial_domain,
+ )
+
+
+_STREAM_ELEMENT_ID = "streaming_md"
+
+
+@dataclass
+class _FeishuStreamBuf:
+ """Per-chat streaming accumulator using CardKit streaming API."""
+
+ text: str = ""
+ card_id: str | None = None
+ sequence: int = 0
+ last_edit: float = 0.0
+
+
+class FeishuChannel(BaseChannel):
+ """
+ Feishu/Lark channel using WebSocket long connection.
+
+ Uses WebSocket to receive events - no public IP or webhook required.
+
+ Requires:
+ - App ID and App Secret from Feishu Open Platform
+ - Bot capability enabled
+ - Event subscription enabled (im.message.receive_v1)
+ """
+
+ name = "feishu"
+ display_name = "Feishu"
+
+ _STREAM_EDIT_INTERVAL = 0.5 # throttle between CardKit streaming updates
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return FeishuConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = FeishuConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: FeishuConfig = config
+ self._client: Any = None
+ self._ws_client: Any = None
+ self._ws_thread: threading.Thread | None = None
+ self._processed_message_ids: OrderedDict[str, None] = OrderedDict() # Ordered dedup cache
+ self._loop: asyncio.AbstractEventLoop | None = None
+ self._stream_bufs: dict[str, _FeishuStreamBuf] = {}
+ self._bot_open_id: str | None = None
+ self._background_tasks: set[asyncio.Task] = set()
+ self._reaction_ids: dict[str, str] = {} # message_id → reaction_id
+
+ # ------------------------------------------------------------------
+ # QR login — writes credentials directly to config.json
+ # ------------------------------------------------------------------
+
+ async def login(self, force: bool = False) -> bool:
+ """Perform QR code scan-to-create login for Feishu/Lark.
+
+ Uses the Feishu device-code registration flow to create a new bot
+ application automatically. Opens a URL for the user to authorize
+ with the Feishu or Lark mobile app.
+
+ On success, writes ``appId``, ``appSecret``, and ``domain`` to
+ ``channels.feishu`` in ``config.json`` and sets ``enabled: true``.
+
+ Args:
+ force: If True, clear existing credentials and force re-authentication.
+
+ Returns True on success.
+ """
+ if force:
+ self.config.app_id = ""
+ self.config.app_secret = ""
+
+ if self.config.app_id and self.config.app_secret:
+ _LOGIN_CONSOLE.print("[green]Feishu/Lark is already authenticated.[/green]")
+ _LOGIN_CONSOLE.print("Use --force to re-authenticate with a new bot.\n")
+ return True
+
+ _LOGIN_CONSOLE.print("Authorize with the mobile app. vibe-trading will save the new bot credentials.\n")
+
+ result = qr_register(initial_domain=self.config.domain or "feishu")
+ if not result:
+ _LOGIN_CONSOLE.print(
+ "[yellow]Login was not completed.[/yellow] "
+ "Run 'vibe-trading channels login feishu --force' to retry."
+ )
+ return False
+
+ self.config.app_id = result["app_id"]
+ self.config.app_secret = result["app_secret"]
+ self.config.domain = result.get("domain", "feishu")
+
+ # Write credentials back to config
+ # VT-TODO: persist feishu credentials via VT config system
+ try:
+ from src.config.loader import load_agent_config
+ # Credentials stored in-memory on self.config; persist via VT config
+ # when channel config persistence is wired up.
+ except Exception:
+ pass
+
+ _LOGIN_CONSOLE.print("\n[green]Feishu/Lark login complete.[/green]")
+ _LOGIN_CONSOLE.print(f"App ID: {escape(result['app_id'])}")
+ _LOGIN_CONSOLE.print(f"Domain: {escape(self.config.domain)}")
+ return True
+
+ @staticmethod
+ def _register_optional_event(builder: Any, method_name: str, handler: Any) -> Any:
+ """Register an event handler only when the SDK supports it."""
+ method = getattr(builder, method_name, None)
+ return method(handler) if callable(method) else builder
+
+ async def start(self) -> None:
+ """Start the Feishu bot with WebSocket long connection."""
+ if not FEISHU_AVAILABLE:
+ self.logger.error("SDK not installed. Run: pip install lark-oapi")
+ return
+
+ if not self.config.app_id or not self.config.app_secret:
+ self.logger.error(
+ "app_id and app_secret not configured. "
+ "Run 'vibe-trading channels login feishu' to set up via QR code."
+ )
+ return
+
+ lark, feishu_domain, lark_domain = await asyncio.to_thread(_load_lark_runtime)
+
+ # (stdlib logging handles Lark SDK output via propagation)
+
+ self._running = True
+ self._loop = asyncio.get_running_loop()
+
+ # Create Lark client for sending messages
+ domain = lark_domain if self.config.domain == "lark" else feishu_domain
+ self._client = (
+ lark.Client.builder()
+ .app_id(self.config.app_id)
+ .app_secret(self.config.app_secret)
+ .domain(domain)
+ .log_level(lark.LogLevel.INFO)
+ .build()
+ )
+ builder = lark.EventDispatcherHandler.builder(
+ self.config.encrypt_key or "",
+ self.config.verification_token or "",
+ ).register_p2_im_message_receive_v1(self._on_message_sync)
+ builder = self._register_optional_event(
+ builder, "register_p2_im_message_reaction_created_v1", self._on_reaction_created
+ )
+ builder = self._register_optional_event(
+ builder, "register_p2_im_message_reaction_deleted_v1", self._on_reaction_deleted
+ )
+ builder = self._register_optional_event(
+ builder, "register_p2_im_message_message_read_v1", self._on_message_read
+ )
+ builder = self._register_optional_event(
+ builder,
+ "register_p2_im_chat_access_event_bot_p2p_chat_entered_v1",
+ self._on_bot_p2p_chat_entered,
+ )
+ # Silence "processor not found" errors when bots are added/removed from groups.
+ # These events carry no actionable data for the agent.
+ builder = self._register_optional_event(
+ builder,
+ "register_p2_im_chat_member_bot_added_v1",
+ lambda _: None,
+ )
+ builder = self._register_optional_event(
+ builder,
+ "register_p2_im_chat_member_bot_deleted_v1",
+ lambda _: None,
+ )
+ event_handler = builder.build()
+
+ # Create WebSocket client for long connection
+ self._ws_client = lark.ws.Client(
+ self.config.app_id,
+ self.config.app_secret,
+ domain=domain,
+ event_handler=event_handler,
+ log_level=lark.LogLevel.INFO,
+ )
+
+ # Start WebSocket client in a separate thread with reconnect loop.
+ # A dedicated event loop is created for this thread so that lark_oapi's
+ # module-level `loop = asyncio.get_event_loop()` picks up an idle loop
+ # instead of the already-running main asyncio loop, which would cause
+ # "This event loop is already running" errors.
+ def run_ws():
+ import time
+
+ import lark_oapi.ws.client as _lark_ws_client
+
+ previous_loop = getattr(_lark_ws_client, "loop", None)
+ ws_loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(ws_loop)
+ # Patch the module-level loop used by lark's ws Client.start()
+ _lark_ws_client.loop = ws_loop
+ try:
+ while self._running:
+ try:
+ self._ws_client.start()
+ except Exception as e:
+ self.logger.warning("WebSocket error: {}", e)
+ if self._running:
+ time.sleep(5)
+ finally:
+ if getattr(_lark_ws_client, "loop", None) is ws_loop:
+ _lark_ws_client.loop = previous_loop
+ with suppress(Exception):
+ asyncio.set_event_loop(None)
+ ws_loop.close()
+
+ self._ws_thread = threading.Thread(target=run_ws, daemon=True)
+ self._ws_thread.start()
+
+ # Fetch bot's own open_id for accurate @mention matching
+ self._bot_open_id = await asyncio.get_running_loop().run_in_executor(
+ None, self._fetch_bot_open_id
+ )
+ if self._bot_open_id:
+ self.logger.info("bot open_id: {}", self._bot_open_id)
+ else:
+ self.logger.warning("Could not fetch bot open_id; @mention matching may be inaccurate")
+
+ self.logger.info("bot started with WebSocket long connection")
+ self.logger.info("No public IP required - using WebSocket to receive events")
+
+ # Keep running until stopped
+ while self._running:
+ await asyncio.sleep(1)
+
+ async def stop(self) -> None:
+ """
+ Stop the Feishu bot.
+
+ Notice: lark.ws.Client does not expose stop method, simply exiting the program will close the client.
+
+ Reference: https://github.com/larksuite/oapi-sdk-python/blob/v2_main/lark_oapi/ws/client.py#L86
+ """
+ self._running = False
+ self.logger.info("bot stopped")
+
+ def _fetch_bot_open_id(self) -> str | None:
+ """Fetch the bot's own open_id via GET /open-apis/bot/v3/info."""
+ try:
+ import lark_oapi as lark
+
+ request = (
+ lark.BaseRequest.builder()
+ .http_method(lark.HttpMethod.GET)
+ .uri("/open-apis/bot/v3/info")
+ .token_types({lark.AccessTokenType.APP})
+ .build()
+ )
+ response = self._client.request(request)
+ if response.success():
+ import json
+
+ data = json.loads(response.raw.content)
+ bot = (data.get("data") or data).get("bot") or data.get("bot") or {}
+ return bot.get("open_id")
+ self.logger.warning("Failed to get bot info: code={}, msg={}", response.code, response.msg)
+ return None
+ except Exception as e:
+ self.logger.warning("Error fetching bot info: {}", e)
+ return None
+
+ @staticmethod
+ def _resolve_mentions(text: str, mentions: list[MentionEvent] | None) -> str:
+ """Replace @_user_n placeholders with actual user info from mentions.
+
+ Args:
+ text: The message text containing @_user_n placeholders
+ mentions: List of mention objects from Feishu message
+
+ Returns:
+ Text with placeholders replaced by @姓名 (open_id)
+ """
+ if not mentions or not text:
+ return text
+
+ for mention in mentions:
+ key = mention.key or None
+ if not key:
+ continue
+ # Feishu placeholders are numbered keys like @_user_1. Keep
+ # punctuation-adjacent mentions valid without matching @_user_10.
+ pattern = rf"{re.escape(key)}(?![A-Za-z0-9_])"
+ if not re.search(pattern, text):
+ continue
+
+ user_id_obj = mention.id or None
+ if not user_id_obj:
+ continue
+
+ open_id = user_id_obj.open_id
+ user_id = user_id_obj.user_id
+ name = mention.name or key
+
+ # Format: @姓名 (open_id, user_id: xxx)
+ if open_id and user_id:
+ replacement = f"@{name} ({open_id}, user id: {user_id})"
+ elif open_id:
+ replacement = f"@{name} ({open_id})"
+ else:
+ replacement = f"@{name}"
+
+ text = re.sub(pattern, replacement, text)
+
+ return text
+
+ def _is_bot_mention_event(self, mention: Any) -> bool:
+ mid = getattr(mention, "id", None)
+ if not mid:
+ return False
+
+ mention_open_id = getattr(mid, "open_id", None) or ""
+ bot_open_id = getattr(self, "_bot_open_id", None) or ""
+ if bot_open_id:
+ return mention_open_id == bot_open_id
+
+ # Fallback heuristic when bot open_id is unavailable.
+ return not getattr(mid, "user_id", None) and mention_open_id.startswith("ou_")
+
+ def _strip_leading_bot_mention(
+ self, text: str, mentions: list[MentionEvent] | None
+ ) -> str:
+ """Remove a required leading bot mention before slash command routing."""
+ if not mentions or not text:
+ return text
+
+ candidate = text.lstrip()
+ for mention in mentions:
+ key = getattr(mention, "key", None) or ""
+ if not key or not re.match(rf"{re.escape(key)}(?![A-Za-z0-9_])", candidate):
+ continue
+ if not self._is_bot_mention_event(mention):
+ continue
+
+ stripped = candidate[len(key) :].strip()
+ return stripped or text
+
+ return text
+
+ def _is_bot_mentioned(self, message: Any) -> bool:
+ """Check if the bot is @mentioned in the message."""
+ raw_content = message.content or ""
+ if "@_all" in raw_content:
+ return True
+
+ for mention in getattr(message, "mentions", None) or []:
+ if self._is_bot_mention_event(mention):
+ return True
+ return False
+
+ def _is_group_message_for_bot(self, message: Any) -> bool:
+ """Allow group messages when policy is open or bot is @mentioned."""
+ if self.config.group_policy == "open":
+ return True
+ return self._is_bot_mentioned(message)
+
+ def _add_reaction_sync(self, message_id: str, emoji_type: str) -> str | None:
+ """Sync helper for adding reaction (runs in thread pool)."""
+ from lark_oapi.api.im.v1 import (
+ CreateMessageReactionRequest,
+ CreateMessageReactionRequestBody,
+ Emoji,
+ )
+
+ try:
+ request = (
+ CreateMessageReactionRequest.builder()
+ .message_id(message_id)
+ .request_body(
+ CreateMessageReactionRequestBody.builder()
+ .reaction_type(Emoji.builder().emoji_type(emoji_type).build())
+ .build()
+ )
+ .build()
+ )
+
+ response = self._client.im.v1.message_reaction.create(request)
+
+ if not response.success():
+ self.logger.warning(
+ "Failed to add reaction: code={}, msg={}", response.code, response.msg
+ )
+ return None
+ else:
+ self.logger.debug("Added {} reaction to message {}", emoji_type, message_id)
+ return response.data.reaction_id if response.data else None
+ except Exception as e:
+ self.logger.warning("Error adding reaction: {}", e)
+ return None
+
+ async def _add_reaction(self, message_id: str, emoji_type: str = "THUMBSUP") -> str | None:
+ """Add a reaction emoji to a message.
+
+ Returns the reaction_id on success, None on failure.
+ When called via a tracked background task, the returned reaction_id
+ is stored in ``_reaction_ids`` for later cleanup by ``send_delta``.
+
+ Common emoji types: THUMBSUP, OK, EYES, DONE, OnIt, HEART
+ """
+ if not self._client:
+ return None
+
+ loop = asyncio.get_running_loop()
+ return await loop.run_in_executor(None, self._add_reaction_sync, message_id, emoji_type)
+
+ def _remove_reaction_sync(self, message_id: str, reaction_id: str) -> None:
+ """Sync helper for removing reaction (runs in thread pool)."""
+ from lark_oapi.api.im.v1 import DeleteMessageReactionRequest
+
+ try:
+ request = (
+ DeleteMessageReactionRequest.builder()
+ .message_id(message_id)
+ .reaction_id(reaction_id)
+ .build()
+ )
+
+ response = self._client.im.v1.message_reaction.delete(request)
+ if response.success():
+ self.logger.debug("Removed reaction {} from message {}", reaction_id, message_id)
+ else:
+ self.logger.debug(
+ "Failed to remove reaction: code={}, msg={}", response.code, response.msg
+ )
+ except Exception as e:
+ self.logger.debug("Error removing reaction: {}", e)
+
+ async def _remove_reaction(self, message_id: str, reaction_id: str) -> None:
+ """
+ Remove a reaction emoji from a message (non-blocking).
+
+ Used to clear the "processing" indicator after bot replies.
+ """
+ if not self._client or not reaction_id:
+ return
+
+ loop = asyncio.get_running_loop()
+ await loop.run_in_executor(None, self._remove_reaction_sync, message_id, reaction_id)
+
+ def _on_background_task_done(self, task: asyncio.Task) -> None:
+ """Callback: remove from tracking set and log unhandled exceptions."""
+ self._background_tasks.discard(task)
+ if task.cancelled():
+ return
+ try:
+ task.result()
+ except Exception as exc:
+ self.logger.warning("Background task failed: {}", exc)
+
+ def _on_reaction_added(self, message_id: str, task: asyncio.Task) -> None:
+ """Callback: store reaction_id after background add-reaction completes."""
+ if task.cancelled():
+ return
+ # Failures already logged by _on_background_task_done.
+ with suppress(Exception):
+ reaction_id = task.result()
+ if reaction_id:
+ self._reaction_ids[message_id] = reaction_id
+ # Trim cache to prevent unbounded growth
+ if len(self._reaction_ids) > 500:
+ self._reaction_ids.pop(next(iter(self._reaction_ids)))
+
+ @staticmethod
+ def _stream_key(chat_id: str, metadata: dict[str, Any] | None = None) -> str:
+ """Scope streaming buffers to the inbound message when available."""
+ meta = metadata or {}
+ return meta.get("message_id") or chat_id
+
+ # Regex to match markdown tables (header + separator + data rows)
+ _TABLE_RE = re.compile(
+ r"((?:^[ \t]*\|.+\|[ \t]*\n)(?:^[ \t]*\|[-:\s|]+\|[ \t]*\n)(?:^[ \t]*\|.+\|[ \t]*\n?)+)",
+ re.MULTILINE,
+ )
+
+ _HEADING_RE = re.compile(r"^(#{1,6})\s+(.+)$", re.MULTILINE)
+
+ _CODE_BLOCK_RE = re.compile(r"(```[\s\S]*?```)", re.MULTILINE)
+
+ # Markdown formatting patterns that should be stripped from plain-text
+ # surfaces like table cells and heading text.
+ _MD_BOLD_RE = re.compile(r"\*\*(.+?)\*\*")
+ _MD_BOLD_UNDERSCORE_RE = re.compile(r"__(.+?)__")
+ _MD_ITALIC_RE = re.compile(r"(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)")
+ _MD_STRIKE_RE = re.compile(r"~~(.+?)~~")
+
+ @classmethod
+ def _strip_md_formatting(cls, text: str) -> str:
+ """Strip markdown formatting markers from text for plain display.
+
+ Feishu table cells do not support markdown rendering, so we remove
+ the formatting markers to keep the text readable.
+ """
+ # Remove bold markers
+ text = cls._MD_BOLD_RE.sub(r"\1", text)
+ text = cls._MD_BOLD_UNDERSCORE_RE.sub(r"\1", text)
+ # Remove italic markers
+ text = cls._MD_ITALIC_RE.sub(r"\1", text)
+ # Remove strikethrough markers
+ text = cls._MD_STRIKE_RE.sub(r"\1", text)
+ return text
+
+ @classmethod
+ def _parse_md_table(cls, table_text: str) -> dict | None:
+ """Parse a markdown table into a Feishu table element."""
+ lines = [_line.strip() for _line in table_text.strip().split("\n") if _line.strip()]
+ if len(lines) < 3:
+ return None
+
+ def split(_line: str) -> list[str]:
+ return [c.strip() for c in _line.strip("|").split("|")]
+
+ headers = [cls._strip_md_formatting(h) for h in split(lines[0])]
+ rows = [[cls._strip_md_formatting(c) for c in split(_line)] for _line in lines[2:]]
+ columns = [
+ {"tag": "column", "name": f"c{i}", "display_name": h, "width": "auto"}
+ for i, h in enumerate(headers)
+ ]
+ return {
+ "tag": "table",
+ "page_size": len(rows) + 1,
+ "columns": columns,
+ "rows": [
+ {f"c{i}": r[i] if i < len(r) else "" for i in range(len(headers))} for r in rows
+ ],
+ }
+
+ def _build_card_elements(self, content: str) -> list[dict]:
+ """Split content into div/markdown + table elements for Feishu card."""
+ elements, last_end = [], 0
+ for m in self._TABLE_RE.finditer(content):
+ before = content[last_end : m.start()]
+ if before.strip():
+ elements.extend(self._split_headings(before))
+ elements.append(
+ self._parse_md_table(m.group(1)) or {"tag": "markdown", "content": m.group(1)}
+ )
+ last_end = m.end()
+ remaining = content[last_end:]
+ if remaining.strip():
+ elements.extend(self._split_headings(remaining))
+ return elements or [{"tag": "markdown", "content": content}]
+
+ @staticmethod
+ def _split_elements_by_table_limit(
+ elements: list[dict], max_tables: int = 1
+ ) -> list[list[dict]]:
+ """Split card elements into groups with at most *max_tables* table elements each.
+
+ Feishu cards have a hard limit of one table per card (API error 11310).
+ When the rendered content contains multiple markdown tables each table is
+ placed in a separate card message so every table reaches the user.
+ """
+ if not elements:
+ return [[]]
+ groups: list[list[dict]] = []
+ current: list[dict] = []
+ table_count = 0
+ for el in elements:
+ if el.get("tag") == "table":
+ if table_count >= max_tables:
+ if current:
+ groups.append(current)
+ current = []
+ table_count = 0
+ current.append(el)
+ table_count += 1
+ else:
+ current.append(el)
+ if current:
+ groups.append(current)
+ return groups or [[]]
+
+ def _split_headings(self, content: str) -> list[dict]:
+ """Split content by headings, converting headings to div elements."""
+ protected = content
+ code_blocks = []
+ for m in self._CODE_BLOCK_RE.finditer(content):
+ code_blocks.append(m.group(1))
+ protected = protected.replace(m.group(1), f"\x00CODE{len(code_blocks) - 1}\x00", 1)
+
+ elements = []
+ last_end = 0
+ for m in self._HEADING_RE.finditer(protected):
+ before = protected[last_end : m.start()].strip()
+ if before:
+ elements.append({"tag": "markdown", "content": before})
+ text = self._strip_md_formatting(m.group(2).strip())
+ display_text = f"**{text}**" if text else ""
+ elements.append(
+ {
+ "tag": "div",
+ "text": {
+ "tag": "lark_md",
+ "content": display_text,
+ },
+ }
+ )
+ last_end = m.end()
+ remaining = protected[last_end:].strip()
+ if remaining:
+ elements.append({"tag": "markdown", "content": remaining})
+
+ for i, cb in enumerate(code_blocks):
+ for el in elements:
+ if el.get("tag") == "markdown":
+ el["content"] = el["content"].replace(f"\x00CODE{i}\x00", cb)
+
+ return elements or [{"tag": "markdown", "content": content}]
+
+ # ── Smart format detection ──────────────────────────────────────────
+ # Patterns that indicate "complex" markdown needing card rendering
+ _COMPLEX_MD_RE = re.compile(
+ r"```" # fenced code block
+ r"|^\|.+\|.*\n\s*\|[-:\s|]+\|" # markdown table (header + separator)
+ r"|^#{1,6}\s+", # headings
+ re.MULTILINE,
+ )
+
+ # Simple markdown patterns (bold, italic, strikethrough)
+ _SIMPLE_MD_RE = re.compile(
+ r"\*\*.+?\*\*" # **bold**
+ r"|__.+?__" # __bold__
+ r"|(?<!\*)\*(?!\*)(.+?)(?<!\*)\*(?!\*)" # *italic* (single *)
+ r"|~~.+?~~", # ~~strikethrough~~
+ re.DOTALL,
+ )
+
+ # Markdown link: [text](url)
+ _MD_LINK_RE = re.compile(r"\[([^\]]+)\]\((https?://[^\)]+)\)")
+
+ # Unordered list items
+ _LIST_RE = re.compile(r"^[\s]*[-*+]\s+", re.MULTILINE)
+
+ # Ordered list items
+ _OLIST_RE = re.compile(r"^[\s]*\d+\.\s+", re.MULTILINE)
+
+ # Max length for plain text format
+ _TEXT_MAX_LEN = 200
+
+ # Max length for post (rich text) format; beyond this, use card
+ _POST_MAX_LEN = 2000
+
+ @classmethod
+ def _detect_msg_format(cls, content: str) -> str:
+ """Determine the optimal Feishu message format for *content*.
+
+ Returns one of:
+ - ``"text"`` – plain text, short and no markdown
+ - ``"post"`` – rich text (links only, moderate length)
+ - ``"interactive"`` – card with full markdown rendering
+ """
+ stripped = content.strip()
+
+ # Complex markdown (code blocks, tables, headings) → always card
+ if cls._COMPLEX_MD_RE.search(stripped):
+ return "interactive"
+
+ # Long content → card (better readability with card layout)
+ if len(stripped) > cls._POST_MAX_LEN:
+ return "interactive"
+
+ # Has bold/italic/strikethrough → card (post format can't render these)
+ if cls._SIMPLE_MD_RE.search(stripped):
+ return "interactive"
+
+ # Has list items → card (post format can't render list bullets well)
+ if cls._LIST_RE.search(stripped) or cls._OLIST_RE.search(stripped):
+ return "interactive"
+
+ # Has links → post format (supports <a> tags)
+ if cls._MD_LINK_RE.search(stripped):
+ return "post"
+
+ # Short plain text → text format
+ if len(stripped) <= cls._TEXT_MAX_LEN:
+ return "text"
+
+ # Medium plain text without any formatting → post format
+ return "post"
+
+ @classmethod
+ def _markdown_to_post(cls, content: str) -> str:
+ """Convert markdown content to Feishu post message JSON.
+
+ Handles links ``[text](url)`` as ``a`` tags; everything else as ``text`` tags.
+ Each line becomes a paragraph (row) in the post body.
+ """
+ lines = content.strip().split("\n")
+ paragraphs: list[list[dict]] = []
+
+ for line in lines:
+ elements: list[dict] = []
+ last_end = 0
+
+ for m in cls._MD_LINK_RE.finditer(line):
+ # Text before this link
+ before = line[last_end : m.start()]
+ if before:
+ elements.append({"tag": "text", "text": before})
+ elements.append(
+ {
+ "tag": "a",
+ "text": m.group(1),
+ "href": m.group(2),
+ }
+ )
+ last_end = m.end()
+
+ # Remaining text after last link
+ remaining = line[last_end:]
+ if remaining:
+ elements.append({"tag": "text", "text": remaining})
+
+ # Empty line → empty paragraph for spacing
+ if not elements:
+ elements.append({"tag": "text", "text": ""})
+
+ paragraphs.append(elements)
+
+ post_body = {
+ "zh_cn": {
+ "content": paragraphs,
+ }
+ }
+ return json.dumps(post_body, ensure_ascii=False)
+
+ _IMAGE_EXTS = {".png", ".jpg", ".jpeg", ".gif", ".bmp", ".webp", ".ico", ".tiff", ".tif"}
+ _AUDIO_EXTS = {".opus"}
+ _VIDEO_EXTS = {".mp4", ".mov", ".avi"}
+ _FILE_TYPE_MAP = {
+ ".opus": "opus",
+ ".mp4": "mp4",
+ ".pdf": "pdf",
+ ".doc": "doc",
+ ".docx": "doc",
+ ".xls": "xls",
+ ".xlsx": "xls",
+ ".ppt": "ppt",
+ ".pptx": "ppt",
+ }
+
+ def _upload_image_sync(self, file_path: str) -> str | None:
+ """Upload an image to Feishu and return the image_key."""
+ from lark_oapi.api.im.v1 import CreateImageRequest, CreateImageRequestBody
+
+ try:
+ with open(file_path, "rb") as f:
+ request = (
+ CreateImageRequest.builder()
+ .request_body(
+ CreateImageRequestBody.builder().image_type("message").image(f).build()
+ )
+ .build()
+ )
+ response = self._client.im.v1.image.create(request)
+ if response.success():
+ image_key = response.data.image_key
+ self.logger.debug("Uploaded image {}: {}", os.path.basename(file_path), image_key)
+ return image_key
+ else:
+ self.logger.error(
+ "Failed to upload image: code={}, msg={}", response.code, response.msg
+ )
+ return None
+ except Exception:
+ self.logger.exception("Error uploading image {}", file_path)
+ return None
+
+ def _upload_file_sync(self, file_path: str) -> str | None:
+ """Upload a file to Feishu and return the file_key."""
+ from lark_oapi.api.im.v1 import CreateFileRequest, CreateFileRequestBody
+
+ ext = os.path.splitext(file_path)[1].lower()
+ file_type = self._FILE_TYPE_MAP.get(ext, "stream")
+ file_name = os.path.basename(file_path)
+ try:
+ with open(file_path, "rb") as f:
+ request = (
+ CreateFileRequest.builder()
+ .request_body(
+ CreateFileRequestBody.builder()
+ .file_type(file_type)
+ .file_name(file_name)
+ .file(f)
+ .build()
+ )
+ .build()
+ )
+ response = self._client.im.v1.file.create(request)
+ if response.success():
+ file_key = response.data.file_key
+ self.logger.debug("Uploaded file {}: {}", file_name, file_key)
+ return file_key
+ else:
+ self.logger.error(
+ "Failed to upload file: code={}, msg={}", response.code, response.msg
+ )
+ return None
+ except Exception:
+ self.logger.exception("Error uploading file {}", file_path)
+ return None
+
+ def _download_image_sync(
+ self, message_id: str, image_key: str
+ ) -> tuple[bytes | None, str | None]:
+ """Download an image from Feishu message by message_id and image_key."""
+ from lark_oapi.api.im.v1 import GetMessageResourceRequest
+
+ try:
+ request = (
+ GetMessageResourceRequest.builder()
+ .message_id(message_id)
+ .file_key(image_key)
+ .type("image")
+ .build()
+ )
+ response = self._client.im.v1.message_resource.get(request)
+ if response.success():
+ file_data = response.file
+ # GetMessageResourceRequest returns BytesIO, need to read bytes
+ if hasattr(file_data, "read"):
+ file_data = file_data.read()
+ return file_data, response.file_name
+ else:
+ self.logger.error(
+ "Failed to download image: code={}, msg={}", response.code, response.msg
+ )
+ return None, None
+ except Exception:
+ self.logger.exception("Error downloading image {}", image_key)
+ return None, None
+
+ def _download_file_sync(
+ self, message_id: str, file_key: str, resource_type: str = "file"
+ ) -> tuple[bytes | None, str | None]:
+ """Download a file/audio/media from a Feishu message by message_id and file_key."""
+ from lark_oapi.api.im.v1 import GetMessageResourceRequest
+
+ # Feishu resource download API only accepts 'image' or 'file' as type.
+ # Both 'audio' and 'media' (video) messages use type='file' for download.
+ if resource_type in ("audio", "media"):
+ resource_type = "file"
+
+ try:
+ request = (
+ GetMessageResourceRequest.builder()
+ .message_id(message_id)
+ .file_key(file_key)
+ .type(resource_type)
+ .build()
+ )
+ response = self._client.im.v1.message_resource.get(request)
+ if response.success():
+ file_data = response.file
+ if hasattr(file_data, "read"):
+ file_data = file_data.read()
+ return file_data, response.file_name
+ else:
+ self.logger.error(
+ "Failed to download {}: code={}, msg={}",
+ resource_type,
+ response.code,
+ response.msg,
+ )
+ return None, None
+ except Exception:
+ self.logger.exception("Error downloading {} {}", resource_type, file_key)
+ return None, None
+
+ @staticmethod
+ def _safe_media_filename(filename: str | None, fallback: str) -> str:
+ """Return a local-only filename for downloaded Feishu media."""
+ candidate = filename or fallback
+ # Feishu/Lark filenames come from message metadata. Treat both POSIX
+ # and Windows separators as path boundaries before applying the shared
+ # filename sanitizer so downloads cannot escape the channel media dir.
+ candidate = os.path.basename(candidate.replace("\\", "/"))
+ candidate = safe_filename(candidate)
+ if candidate in ("", ".", ".."):
+ return safe_filename(fallback) or uuid.uuid4().hex
+ return candidate
+
+ async def _download_and_save_media(
+ self, msg_type: str, content_json: dict, message_id: str | None = None
+ ) -> tuple[str | None, str]:
+ """
+ Download media from Feishu and save to local disk.
+
+ Returns:
+ (file_path, content_text) - file_path is None if download failed
+ """
+ loop = asyncio.get_running_loop()
+ media_dir = get_media_dir("feishu")
+
+ data, filename = None, None
+ fallback_filename = uuid.uuid4().hex
+
+ if msg_type == "image":
+ image_key = content_json.get("image_key")
+ if image_key and message_id:
+ fallback_filename = f"{image_key[:16]}.jpg"
+ data, filename = await loop.run_in_executor(
+ None, self._download_image_sync, message_id, image_key
+ )
+ if not filename:
+ filename = fallback_filename
+
+ elif msg_type in ("audio", "file", "media"):
+ file_key = content_json.get("file_key")
+ if not file_key:
+ self.logger.warning("{} message missing file_key: {}", msg_type, content_json)
+ return None, f"[{msg_type}: missing file_key]"
+ if not message_id:
+ self.logger.warning("{} message missing message_id", msg_type)
+ return None, f"[{msg_type}: missing message_id]"
+
+ fallback_filename = file_key[:16]
+ data, filename = await loop.run_in_executor(
+ None, self._download_file_sync, message_id, file_key, msg_type
+ )
+
+ if not data:
+ self.logger.warning("{} download failed: file_key={}", msg_type, file_key)
+ return None, f"[{msg_type}: download failed]"
+
+ if not filename:
+ filename = fallback_filename
+
+ # Feishu voice messages are opus in OGG container.
+ # Use .ogg extension for better Whisper compatibility.
+ if msg_type == "audio":
+ if not any(filename.endswith(ext) for ext in (".opus", ".ogg", ".oga")):
+ filename = f"{filename}.ogg"
+
+ if data and filename:
+ filename = self._safe_media_filename(filename, fallback_filename)
+ file_path = media_dir / filename
+ file_path.write_bytes(data)
+ path_str = str(file_path)
+ self.logger.debug("Downloaded {} to {}", msg_type, path_str)
+ return path_str, f"[{msg_type}: {path_str}]"
+
+ return None, f"[{msg_type}: download failed]"
+
+ _REPLY_CONTEXT_MAX_LEN = 200
+
+ def _get_message_content_sync(self, message_id: str) -> str | None:
+ """Fetch the text content of a Feishu message by ID (synchronous).
+
+ Returns a "[Reply to: ...]" context string, or None on failure.
+ """
+ from lark_oapi.api.im.v1 import GetMessageRequest
+
+ try:
+ request = GetMessageRequest.builder().message_id(message_id).build()
+ response = self._client.im.v1.message.get(request)
+ if not response.success():
+ self.logger.debug(
+ "could not fetch parent message {}: code={}, msg={}",
+ message_id,
+ response.code,
+ response.msg,
+ )
+ return None
+ items = getattr(response.data, "items", None)
+ if not items:
+ return None
+ msg_obj = items[0]
+ raw_content = getattr(msg_obj, "body", None)
+ raw_content = getattr(raw_content, "content", None) if raw_content else None
+ if not raw_content:
+ return None
+ try:
+ content_json = json.loads(raw_content)
+ except (json.JSONDecodeError, TypeError):
+ return None
+ msg_type = getattr(msg_obj, "msg_type", "")
+ if msg_type == "text":
+ text = content_json.get("text", "").strip()
+ elif msg_type == "post":
+ text, _ = _extract_post_content(content_json)
+ text = text.strip()
+ else:
+ text = ""
+ if not text:
+ return None
+ if len(text) > self._REPLY_CONTEXT_MAX_LEN:
+ text = text[: self._REPLY_CONTEXT_MAX_LEN] + "..."
+ return f"[Reply to: {text}]"
+ except Exception as e:
+ self.logger.debug("error fetching parent message {}: {}", message_id, e)
+ return None
+
+ def _reply_message_sync(self, parent_message_id: str, msg_type: str, content: str, *, reply_in_thread: bool = False) -> bool:
+ """Reply to an existing Feishu message using the Reply API (synchronous).
+
+ Args:
+ reply_in_thread: If True, reply as a thread/topic message
+ in the Feishu client.
+ """
+ from lark_oapi.api.im.v1 import ReplyMessageRequest, ReplyMessageRequestBody
+
+ try:
+ body_builder = ReplyMessageRequestBody.builder().msg_type(msg_type).content(content)
+ if reply_in_thread:
+ body_builder = body_builder.reply_in_thread(True)
+ request = (
+ ReplyMessageRequest.builder()
+ .message_id(parent_message_id)
+ .request_body(body_builder.build())
+ .build()
+ )
+ response = self._client.im.v1.message.reply(request)
+ if not response.success():
+ self.logger.error(
+ "Failed to reply to message {}: code={}, msg={}, log_id={}",
+ parent_message_id,
+ response.code,
+ response.msg,
+ response.get_log_id(),
+ )
+ return False
+ self.logger.debug("reply sent to message {}", parent_message_id)
+ return True
+ except Exception:
+ self.logger.exception("Error replying to message {}", parent_message_id)
+ return False
+
+ def _should_use_reply_in_thread(self, metadata: dict[str, Any]) -> bool:
+ """Return whether a group reply should create a Feishu thread/topic."""
+ return metadata.get("chat_type", "group") == "group" and self.config.reply_to_message
+
+ def _thread_reply_target(self, metadata: dict[str, Any]) -> str | None:
+ """Return the message_id that should receive a Reply API response."""
+ if metadata.get("chat_type", "group") != "group":
+ return None
+ message_id = metadata.get("message_id")
+ if not message_id:
+ return None
+ if metadata.get("thread_id") or self.config.reply_to_message:
+ return message_id
+ return None
+
+ def _send_message_sync(
+ self, receive_id_type: str, receive_id: str, msg_type: str, content: str
+ ) -> str | None:
+ """Send a single message and return the message_id on success."""
+ from lark_oapi.api.im.v1 import CreateMessageRequest, CreateMessageRequestBody
+
+ try:
+ request = (
+ CreateMessageRequest.builder()
+ .receive_id_type(receive_id_type)
+ .request_body(
+ CreateMessageRequestBody.builder()
+ .receive_id(receive_id)
+ .msg_type(msg_type)
+ .content(content)
+ .build()
+ )
+ .build()
+ )
+ response = self._client.im.v1.message.create(request)
+ if not response.success():
+ self.logger.error(
+ "Failed to send {} message: code={}, msg={}, log_id={}",
+ msg_type,
+ response.code,
+ response.msg,
+ response.get_log_id(),
+ )
+ return None
+ msg_id = getattr(response.data, "message_id", None)
+ self.logger.debug("{} message sent to {}: {}", msg_type, receive_id, msg_id)
+ return msg_id
+ except Exception:
+ self.logger.exception("Error sending {} message", msg_type)
+ return None
+
+ def _create_streaming_card_sync(
+ self,
+ receive_id_type: str,
+ chat_id: str,
+ reply_message_id: str | None = None,
+ *,
+ reply_in_thread: bool = False,
+ ) -> str | None:
+ """Create a CardKit streaming card, send it to chat, return card_id.
+
+ When *reply_message_id* is provided the card is delivered via the
+ reply API. *reply_in_thread* controls whether Feishu creates a
+ thread/topic for that reply. Otherwise the plain create-message API is
+ used.
+ """
+ from lark_oapi.api.cardkit.v1 import CreateCardRequest, CreateCardRequestBody
+
+ card_json = {
+ "schema": "2.0",
+ "config": {"wide_screen_mode": True, "update_multi": True, "streaming_mode": True},
+ "body": {
+ "elements": [{"tag": "markdown", "content": "", "element_id": _STREAM_ELEMENT_ID}]
+ },
+ }
+ try:
+ request = (
+ CreateCardRequest.builder()
+ .request_body(
+ CreateCardRequestBody.builder()
+ .type("card_json")
+ .data(json.dumps(card_json, ensure_ascii=False))
+ .build()
+ )
+ .build()
+ )
+ response = self._client.cardkit.v1.card.create(request)
+ if not response.success():
+ self.logger.warning(
+ "Failed to create streaming card: code={}, msg={}", response.code, response.msg
+ )
+ return None
+ card_id = getattr(response.data, "card_id", None)
+ if card_id:
+ card_content = json.dumps(
+ {"type": "card", "data": {"card_id": card_id}}, ensure_ascii=False
+ )
+ if reply_message_id:
+ sent = self._reply_message_sync(
+ reply_message_id, "interactive", card_content,
+ reply_in_thread=reply_in_thread,
+ )
+ else:
+ sent = self._send_message_sync(
+ receive_id_type, chat_id, "interactive", card_content,
+ ) is not None
+ if sent:
+ return card_id
+ self.logger.warning(
+ "Created streaming card {} but failed to send it to {}", card_id, chat_id
+ )
+ return None
+ except Exception as e:
+ self.logger.warning("Error creating streaming card: {}", e)
+ return None
+
+ def _stream_update_text_sync(self, card_id: str, content: str, sequence: int) -> bool:
+ """Stream-update the markdown element on a CardKit card (typewriter effect)."""
+ from lark_oapi.api.cardkit.v1 import (
+ ContentCardElementRequest,
+ ContentCardElementRequestBody,
+ )
+
+ try:
+ request = (
+ ContentCardElementRequest.builder()
+ .card_id(card_id)
+ .element_id(_STREAM_ELEMENT_ID)
+ .request_body(
+ ContentCardElementRequestBody.builder()
+ .content(content)
+ .sequence(sequence)
+ .build()
+ )
+ .build()
+ )
+ response = self._client.cardkit.v1.card_element.content(request)
+ if not response.success():
+ self.logger.warning(
+ "Failed to stream-update card {}: code={}, msg={}",
+ card_id,
+ response.code,
+ response.msg,
+ )
+ return False
+ return True
+ except Exception as e:
+ self.logger.warning("Error stream-updating card {}: {}", card_id, e)
+ return False
+
+ def _set_streaming_mode_sync(self, card_id: str, enabled: bool, sequence: int) -> bool:
+ """Set CardKit streaming_mode using a strictly increasing sequence."""
+ from lark_oapi.api.cardkit.v1 import SettingsCardRequest, SettingsCardRequestBody
+
+ settings_payload = json.dumps({"config": {"streaming_mode": enabled}}, ensure_ascii=False)
+ try:
+ request = (
+ SettingsCardRequest.builder()
+ .card_id(card_id)
+ .request_body(
+ SettingsCardRequestBody.builder()
+ .settings(settings_payload)
+ .sequence(sequence)
+ .uuid(str(uuid.uuid4()))
+ .build()
+ )
+ .build()
+ )
+ response = self._client.cardkit.v1.card.settings(request)
+ if not response.success():
+ self.logger.warning(
+ "Failed to set streaming={} on card {}: code={}, msg={}",
+ enabled,
+ card_id,
+ response.code,
+ response.msg,
+ )
+ return False
+ return True
+ except Exception as e:
+ self.logger.warning("Error setting streaming={} on card {}: {}", enabled, card_id, e)
+ return False
+
+ def _close_streaming_mode_sync(self, card_id: str, sequence: int) -> bool:
+ """Turn off CardKit streaming_mode so the chat list preview exits the streaming placeholder.
+
+ Per Feishu docs, streaming cards keep a generating-style summary in the session list until
+ streaming_mode is set to false via card settings (after final content update).
+ Sequence must strictly exceed the previous card OpenAPI operation on this entity.
+ """
+ return self._set_streaming_mode_sync(card_id, False, sequence)
+
+ def _stream_update_text_with_reopen_sync(
+ self,
+ card_id: str,
+ content: str,
+ sequence: int,
+ ) -> tuple[bool, int]:
+ if self._stream_update_text_sync(card_id, content, sequence):
+ return True, sequence
+ sequence += 1
+ if not self._set_streaming_mode_sync(card_id, True, sequence):
+ return False, sequence
+ sequence += 1
+ return self._stream_update_text_sync(card_id, content, sequence), sequence
+
+ async def send_delta(
+ self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None
+ ) -> None:
+ """Progressive streaming via CardKit: create card on first delta, stream-update on subsequent.
+
+ Supported metadata keys:
+ _stream_end: Finalize the streaming card.
+ _tool_hint: Delta is a formatted tool hint (for display only).
+ message_id: Original message id (used with _stream_end for reaction cleanup).
+ chat_type: "group" or "p2p" — controls reply-in-thread for streaming cards.
+ """
+ if not self._client:
+ return
+ meta = metadata or {}
+ stream_key = self._stream_key(chat_id, meta)
+ loop = asyncio.get_running_loop()
+ rid_type = "chat_id" if chat_id.startswith("oc_") else "open_id"
+
+ # --- stream end: final update or fallback ---
+ if meta.get("_stream_end"):
+ message_id = meta.get("message_id")
+ # Only finalize the OnIt -> DONE reaction transition on the truly
+ # final stream end. _resuming=True means the agent will keep
+ # working (more tool-call rounds), so leave the reaction state
+ # in place — otherwise the OnIt indicator disappears prematurely
+ # and the DONE reaction fires after every tool call.
+ if message_id and not meta.get("_resuming"):
+ reaction_id = self._reaction_ids.pop(message_id, None)
+ if reaction_id:
+ await self._remove_reaction(message_id, reaction_id)
+ # Add completion emoji if configured
+ if self.config.done_emoji:
+ await self._add_reaction(message_id, self.config.done_emoji)
+
+ buf = self._stream_bufs.pop(stream_key, None)
+ if not buf or not buf.text:
+ return
+ # Try to finalize via streaming card; if that fails (e.g.
+ # streaming mode was closed by Feishu due to timeout), fall
+ # back to sending a regular interactive card.
+ if buf.card_id:
+ buf.sequence += 1
+ ok, buf.sequence = await loop.run_in_executor(
+ None,
+ self._stream_update_text_with_reopen_sync,
+ buf.card_id,
+ buf.text,
+ buf.sequence,
+ )
+ if ok:
+ buf.sequence += 1
+ closed = await loop.run_in_executor(
+ None,
+ self._close_streaming_mode_sync,
+ buf.card_id,
+ buf.sequence,
+ )
+ if not closed:
+ buf.sequence += 1
+ await loop.run_in_executor(
+ None,
+ self._close_streaming_mode_sync,
+ buf.card_id,
+ buf.sequence,
+ )
+ return
+ buf.sequence += 1
+ await loop.run_in_executor(
+ None,
+ self._close_streaming_mode_sync,
+ buf.card_id,
+ buf.sequence,
+ )
+ self.logger.warning(
+ "Streaming card {} final update failed, falling back to regular card",
+ buf.card_id,
+ )
+ for chunk in self._split_elements_by_table_limit(
+ self._build_card_elements(buf.text)
+ ):
+ card = json.dumps(
+ {"config": {"wide_screen_mode": True}, "elements": chunk},
+ ensure_ascii=False,
+ )
+ # Fallback replies stay in existing topics, but only create a
+ # new topic when reply-to-message is enabled.
+ fallback_msg_id = self._thread_reply_target(meta)
+ if fallback_msg_id:
+ await loop.run_in_executor(
+ None, lambda: self._reply_message_sync(
+ fallback_msg_id, "interactive", card,
+ reply_in_thread=self._should_use_reply_in_thread(meta),
+ ),
+ )
+ else:
+ await loop.run_in_executor(
+ None, self._send_message_sync, rid_type, chat_id, "interactive", card
+ )
+ return
+
+ # --- accumulate delta ---
+ buf = self._stream_bufs.get(stream_key)
+ if buf is None:
+ buf = _FeishuStreamBuf()
+ self._stream_bufs[stream_key] = buf
+ buf.text += delta
+ if not buf.text.strip():
+ return
+
+ now = time.monotonic()
+ if buf.card_id is None:
+ # Use the Reply API for existing topics, and only create new topics
+ # when reply-to-message is enabled.
+ use_reply_in_thread = self._should_use_reply_in_thread(meta)
+ reply_msg_id = self._thread_reply_target(meta)
+ card_id = await loop.run_in_executor(
+ None,
+ lambda: self._create_streaming_card_sync(
+ rid_type,
+ chat_id,
+ reply_msg_id,
+ reply_in_thread=use_reply_in_thread,
+ ),
+ )
+ if card_id:
+ ok, sequence = await loop.run_in_executor(
+ None, self._stream_update_text_with_reopen_sync, card_id, buf.text, 1
+ )
+ if ok:
+ buf.card_id = card_id
+ buf.sequence = sequence
+ buf.last_edit = now
+ else:
+ await loop.run_in_executor(
+ None, self._close_streaming_mode_sync, card_id, sequence + 1
+ )
+ elif (now - buf.last_edit) >= self._STREAM_EDIT_INTERVAL:
+ ok, buf.sequence = await loop.run_in_executor(
+ None,
+ self._stream_update_text_with_reopen_sync,
+ buf.card_id,
+ buf.text,
+ buf.sequence + 1,
+ )
+ if ok:
+ buf.last_edit = now
+ else:
+ buf.sequence += 1
+ await loop.run_in_executor(
+ None,
+ self._close_streaming_mode_sync,
+ buf.card_id,
+ buf.sequence,
+ )
+ buf.card_id = None
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through Feishu, including media (images/files) if present."""
+ if not self._client:
+ self.logger.warning("client not initialized")
+ return
+
+ try:
+ receive_id_type = "chat_id" if msg.chat_id.startswith("oc_") else "open_id"
+ loop = asyncio.get_running_loop()
+
+ # Handle tool hint messages. When a streaming card is active for
+ # this chat, inline the hint into the card instead of sending a
+ # separate message so the user experience stays cohesive.
+ if msg.metadata.get("_tool_hint"):
+ hint = (msg.content or "").strip()
+ if not hint:
+ return
+ buf = self._stream_bufs.get(self._stream_key(msg.chat_id, msg.metadata))
+ if buf and buf.card_id:
+ # Delegate to send_delta so tool hints get the same
+ # throttling (and card creation) as regular text deltas.
+ await self.send_delta(
+ msg.chat_id,
+ "\n\n" + self._format_tool_hint_delta(hint) + "\n\n",
+ )
+ return
+ # No active streaming card — send as a regular interactive card
+ # with the same 🔧 prefix style. Existing topics stay threaded;
+ # new topics are created only when reply-to-message is enabled.
+ card = json.dumps(
+ {"config": {"wide_screen_mode": True}, "elements": [
+ {"tag": "markdown", "content": self._format_tool_hint_delta(hint)},
+ ]},
+ ensure_ascii=False,
+ )
+ _th_msg_id = self._thread_reply_target(msg.metadata)
+ if _th_msg_id:
+ await loop.run_in_executor(
+ None, lambda: self._reply_message_sync(
+ _th_msg_id, "interactive", card,
+ reply_in_thread=self._should_use_reply_in_thread(msg.metadata),
+ ),
+ )
+ else:
+ await loop.run_in_executor(
+ None, self._send_message_sync, receive_id_type, msg.chat_id, "interactive", card
+ )
+ return
+
+ # Determine whether the first message should quote the user's message.
+ # Only the very first send (media or text) in this call uses reply; subsequent
+ # chunks/media fall back to plain create to avoid redundant quote bubbles.
+ # Always target message_id — the Feishu Reply API keeps replies in the
+ # same topic automatically when the target message is inside a topic.
+ reply_message_id: str | None = None
+ _msg_id = msg.metadata.get("message_id")
+ has_thread_id = msg.metadata.get("thread_id")
+ if self.config.reply_to_message and not msg.metadata.get("_progress", False):
+ reply_message_id = _msg_id
+ # For topic group messages, always reply to keep context in thread
+ elif has_thread_id:
+ reply_message_id = _msg_id
+
+ first_send = True # tracks whether the reply has already been used
+
+ def _do_send(m_type: str, content: str) -> None:
+ """Send via reply (first message) or create (subsequent).
+
+ Group chats only set reply_in_thread=True when
+ reply_to_message is enabled; otherwise a Reply API call for an
+ existing topic must not create a new topic.
+ """
+ nonlocal first_send
+ if reply_message_id:
+ # If we're in a topic, always use reply to stay in the topic
+ if has_thread_id:
+ ok = self._reply_message_sync(
+ reply_message_id, m_type, content,
+ reply_in_thread=self._should_use_reply_in_thread(msg.metadata),
+ )
+ if ok:
+ return
+ elif first_send:
+ # If we're not in a topic but replying to message, only first uses reply
+ first_send = False
+ ok = self._reply_message_sync(
+ reply_message_id, m_type, content,
+ reply_in_thread=self._should_use_reply_in_thread(msg.metadata),
+ )
+ if ok:
+ return
+ # Fall back to regular send if reply fails
+ self._send_message_sync(receive_id_type, msg.chat_id, m_type, content)
+
+ for file_path in msg.media:
+ if not os.path.isfile(file_path):
+ self.logger.warning("Media file not found: {}", file_path)
+ continue
+ ext = os.path.splitext(file_path)[1].lower()
+ if ext in self._IMAGE_EXTS:
+ key = await loop.run_in_executor(None, self._upload_image_sync, file_path)
+ if key:
+ await loop.run_in_executor(
+ None,
+ _do_send,
+ "image",
+ json.dumps({"image_key": key}, ensure_ascii=False),
+ )
+ else:
+ key = await loop.run_in_executor(None, self._upload_file_sync, file_path)
+ if key:
+ # Feishu's OpenAPI names video messages "media".
+ # Use "audio" for audio, "media" for video, "file" for documents.
+ # Feishu requires these specific msg_types for inline playback.
+ if ext in self._AUDIO_EXTS:
+ media_type = "audio"
+ elif ext in self._VIDEO_EXTS:
+ media_type = "media"
+ else:
+ media_type = "file"
+ await loop.run_in_executor(
+ None,
+ _do_send,
+ media_type,
+ json.dumps({"file_key": key}, ensure_ascii=False),
+ )
+
+ if msg.content and msg.content.strip():
+ fmt = self._detect_msg_format(msg.content)
+
+ if fmt == "text":
+ # Short plain text – send as simple text message
+ text_body = json.dumps({"text": msg.content.strip()}, ensure_ascii=False)
+ await loop.run_in_executor(None, _do_send, "text", text_body)
+
+ elif fmt == "post":
+ # Medium content with links – send as rich-text post
+ post_body = self._markdown_to_post(msg.content)
+ await loop.run_in_executor(None, _do_send, "post", post_body)
+
+ else:
+ # Complex / long content – send as interactive card
+ elements = self._build_card_elements(msg.content)
+ for chunk in self._split_elements_by_table_limit(elements):
+ card = {"config": {"wide_screen_mode": True}, "elements": chunk}
+ await loop.run_in_executor(
+ None,
+ _do_send,
+ "interactive",
+ json.dumps(card, ensure_ascii=False),
+ )
+
+ except Exception:
+ self.logger.exception("Error sending message")
+ raise
+
+ def _on_message_sync(self, data: Any) -> None:
+ """
+ Sync handler for incoming messages (called from WebSocket thread).
+ Schedules async handling in the main event loop.
+ """
+ if self._loop and self._loop.is_running():
+ asyncio.run_coroutine_threadsafe(self._on_message(data), self._loop)
+
+ async def _on_message(self, data: P2ImMessageReceiveV1) -> None:
+ """Handle incoming message from Feishu."""
+ try:
+ event = data.event
+ message = event.message
+ sender = event.sender
+
+ self.logger.debug("raw message: {}", message.content)
+ self.logger.debug("mentions: {}", getattr(message, "mentions", None))
+
+ message_id = message.message_id
+
+ # Skip bot messages
+ if sender.sender_type == "bot":
+ return
+
+ sender_id = sender.sender_id.open_id if sender.sender_id else "unknown"
+ chat_id = message.chat_id
+ chat_type = message.chat_type
+ msg_type = message.message_type
+
+ if chat_type == "group" and not self._is_group_message_for_bot(message):
+ self.logger.debug("skipping group message (not mentioned)")
+ return
+
+ # Deduplication check
+ if message_id in self._processed_message_ids:
+ return
+ self._processed_message_ids[message_id] = None
+
+ # Trim cache
+ while len(self._processed_message_ids) > 1000:
+ self._processed_message_ids.popitem(last=False)
+
+ # Early permission check — avoid side effects for unauthorized users.
+ # Group chats are silently ignored; DMs get a pairing code.
+ if not self.is_allowed(sender_id):
+ if chat_type == "p2p":
+ # content="" because the pairing reply is generated by
+ # BaseChannel._handle_message, not from the original message.
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=sender_id,
+ content="",
+ is_dm=True,
+ )
+ return
+
+ # Add reaction (non-blocking — tracked background task)
+ task = asyncio.create_task(
+ self._add_reaction(message_id, self.config.react_emoji)
+ )
+ self._background_tasks.add(task)
+ task.add_done_callback(self._on_background_task_done)
+ task.add_done_callback(lambda t: self._on_reaction_added(message_id, t))
+
+ # Parse content
+ content_parts = []
+ media_paths = []
+
+ try:
+ content_json = json.loads(message.content) if message.content else {}
+ except json.JSONDecodeError:
+ content_json = {}
+
+ if msg_type == "text":
+ text = content_json.get("text", "")
+ if text:
+ mentions = getattr(message, "mentions", None)
+ text = self._strip_leading_bot_mention(text, mentions)
+ text = self._resolve_mentions(text, mentions)
+ content_parts.append(text)
+
+ elif msg_type == "post":
+ text, image_keys = _extract_post_content(content_json)
+ if text:
+ content_parts.append(text)
+ # Download images embedded in post
+ for img_key in image_keys:
+ file_path, content_text = await self._download_and_save_media(
+ "image", {"image_key": img_key}, message_id
+ )
+ if file_path:
+ media_paths.append(file_path)
+ content_parts.append(content_text)
+
+ elif msg_type in ("image", "audio", "file", "media"):
+ file_path, content_text = await self._download_and_save_media(
+ msg_type, content_json, message_id
+ )
+ if file_path:
+ media_paths.append(file_path)
+
+ if msg_type == "audio" and file_path:
+ transcription = await self.transcribe_audio(file_path)
+ if transcription:
+ content_text = f"[transcription: {transcription}]"
+
+ content_parts.append(content_text)
+
+ elif msg_type in (
+ "share_chat",
+ "share_user",
+ "interactive",
+ "share_calendar_event",
+ "system",
+ "merge_forward",
+ ):
+ # Handle share cards and interactive messages
+ text = _extract_share_card_content(content_json, msg_type)
+ if text:
+ content_parts.append(text)
+
+ else:
+ content_parts.append(MSG_TYPE_MAP.get(msg_type, f"[{msg_type}]"))
+
+ # Extract reply context (parent/root message IDs)
+ parent_id = getattr(message, "parent_id", None) or None
+ root_id = getattr(message, "root_id", None) or None
+ thread_id = getattr(message, "thread_id", None) or None
+
+ # Prepend quoted message text when the user replied to another message
+ if parent_id and self._client:
+ loop = asyncio.get_running_loop()
+ reply_ctx = await loop.run_in_executor(
+ None, self._get_message_content_sync, parent_id
+ )
+ if reply_ctx:
+ content_parts.insert(0, reply_ctx)
+
+ content = "\n".join(content_parts) if content_parts else ""
+
+ if not content and not media_paths:
+ return
+
+ # Build session key for conversation isolation.
+ # If topic_isolation is True: each topic gets its own session via root_id/message_id.
+ # If topic_isolation is False: all messages in group share the same session.
+ # Private chat: no override — same behavior as Telegram/Slack.
+ if chat_type == "group":
+ if self.config.topic_isolation:
+ session_key = f"feishu:{chat_id}:{root_id or message_id}"
+ else:
+ session_key = f"feishu:{chat_id}"
+ else:
+ session_key = None
+
+ # Forward to message bus
+ reply_to = chat_id if chat_type == "group" else sender_id
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=reply_to,
+ content=content,
+ media=media_paths,
+ metadata={
+ "message_id": message_id,
+ "chat_type": chat_type,
+ "msg_type": msg_type,
+ "parent_id": parent_id,
+ "root_id": root_id,
+ "thread_id": thread_id,
+ },
+ session_key=session_key,
+ is_dm=chat_type == "p2p",
+ )
+
+ except Exception:
+ self.logger.exception("Error processing message")
+
+ def _on_reaction_created(self, data: Any) -> None:
+ """Ignore reaction events so they do not generate SDK noise."""
+ pass
+
+ def _on_reaction_deleted(self, data: Any) -> None:
+ """Ignore reaction deleted events so they do not generate SDK noise."""
+ pass
+
+ def _on_message_read(self, data: Any) -> None:
+ """Ignore read events so they do not generate SDK noise."""
+ pass
+
+ def _on_bot_p2p_chat_entered(self, data: Any) -> None:
+ """Ignore p2p-enter events when a user opens a bot chat."""
+ self.logger.debug("Bot entered p2p chat (user opened chat window)")
+ pass
+
+ @staticmethod
+ def _format_tool_hint_lines(tool_hint: str) -> str:
+ """Split tool hints across lines on top-level call separators only."""
+ parts: list[str] = []
+ buf: list[str] = []
+ depth = 0
+ in_string = False
+ quote_char = ""
+ escaped = False
+
+ for i, ch in enumerate(tool_hint):
+ buf.append(ch)
+
+ if in_string:
+ if escaped:
+ escaped = False
+ elif ch == "\\":
+ escaped = True
+ elif ch == quote_char:
+ in_string = False
+ continue
+
+ if ch in {'"', "'"}:
+ in_string = True
+ quote_char = ch
+ continue
+
+ if ch == "(":
+ depth += 1
+ continue
+
+ if ch == ")" and depth > 0:
+ depth -= 1
+ continue
+
+ if ch == "," and depth == 0:
+ next_char = tool_hint[i + 1] if i + 1 < len(tool_hint) else ""
+ if next_char == " ":
+ parts.append("".join(buf).rstrip())
+ buf = []
+
+ if buf:
+ parts.append("".join(buf).strip())
+
+ return "\n".join(part for part in parts if part)
+
+ def _format_tool_hint_delta(self, tool_hint: str) -> str:
+ """Format a tool hint string with the 🔧 prefix for each line."""
+ lines = self.__class__._format_tool_hint_lines(tool_hint).split("\n")
+ return "\n".join(
+ f"{self.config.tool_hint_prefix} {ln}" for ln in lines if ln.strip()
+ )
diff --git a/agent/src/channels/manager.py b/agent/src/channels/manager.py
new file mode 100644
index 000000000..3fb09030f
--- /dev/null
+++ b/agent/src/channels/manager.py
@@ -0,0 +1,478 @@
+"""Channel manager for coordinating chat channels."""
+
+from __future__ import annotations
+
+import asyncio
+import hashlib
+import logging
+from collections.abc import Callable
+from contextlib import suppress
+from pathlib import Path
+from typing import Any
+
+from src.channels.base import BaseChannel
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.registry import (
+ discover_channel_names,
+ discover_plugins,
+ inspect_channels,
+ load_channel_class,
+)
+from src.config.paths import get_workspace_path
+
+logger = logging.getLogger(__name__)
+
+# Retry delays for message sending (exponential backoff: 1s, 2s, 4s)
+_SEND_RETRY_DELAYS = (1, 2, 4)
+
+_BOOL_CAMEL_ALIASES: dict[str, str] = {
+ "send_progress": "sendProgress",
+ "send_tool_hints": "sendToolHints",
+ "show_reasoning": "showReasoning",
+}
+
+
+class ChannelManager:
+ """Manages chat channels and coordinates message routing.
+
+ Responsibilities:
+ - Initialize enabled channels (Telegram, Discord, Slack, etc.)
+ - Start/stop channels
+ - Route outbound messages
+ """
+
+ def __init__(
+ self,
+ config: Any, # ChannelsConfig or dict
+ bus: MessageBus,
+ *,
+ session_service: Any | None = None,
+ cron_service: Any | None = None,
+ ) -> None:
+ self.config = config
+ self.bus = bus
+ self._session_service = session_service
+ self._cron_service = cron_service
+ self.channels: dict[str, BaseChannel] = {}
+ self._dispatch_task: asyncio.Task | None = None
+ self._origin_reply_fingerprints: dict[tuple[str, str, str], str] = {}
+ self._status: dict[str, dict[str, Any]] = {}
+
+ self._init_channels()
+
+ def _init_channels(self) -> None:
+ """Initialize channels discovered via pkgutil scan + entry_points plugins."""
+ self._status = inspect_channels(self.config)
+ enabled_names = {
+ name
+ for name, status in self._status.items()
+ if status.get("enabled") is True
+ }
+ plugin_classes = discover_plugins(enabled_names)
+ builtins = set(discover_channel_names())
+
+ for name in sorted(enabled_names):
+ section = self._get_channel_config(name)
+ if section is None:
+ continue
+ if self._status.get(name, {}).get("available") is False:
+ logger.warning(
+ "%s channel not available: %s",
+ name,
+ self._status[name].get("error", "unavailable"),
+ )
+ continue
+ try:
+ cls = load_channel_class(name) if name in builtins else plugin_classes[name]
+ except Exception as exc: # noqa: BLE001 - status endpoint must explain startup gaps
+ self._status.setdefault(name, {})
+ self._status[name].update(
+ {
+ "available": False,
+ "loaded": False,
+ "running": False,
+ "error": f"{type(exc).__name__}: {exc}",
+ }
+ )
+ logger.warning("%s channel not available: %s", name, exc)
+ continue
+
+ try:
+ kwargs = self._build_channel_kwargs(name)
+ channel = cls(section, self.bus, **kwargs)
+ # Resolve global → per-channel boolean overrides
+ channel.send_progress = self._resolve_bool_override(
+ section, "send_progress", self._global_bool("send_progress", True),
+ )
+ channel.send_tool_hints = self._resolve_bool_override(
+ section, "send_tool_hints", self._global_bool("send_tool_hints", False),
+ )
+ channel.show_reasoning = self._resolve_bool_override(
+ section, "show_reasoning", self._global_bool("show_reasoning", True),
+ )
+ self.channels[name] = channel
+ self._status.setdefault(name, {})
+ self._status[name].update(
+ {
+ "available": True,
+ "loaded": True,
+ "running": channel.is_running,
+ "display_name": getattr(cls, "display_name", name),
+ "error": "",
+ }
+ )
+ logger.info("%s channel enabled", cls.display_name)
+ except Exception as exc:
+ self._status.setdefault(name, {})
+ self._status[name].update(
+ {
+ "loaded": False,
+ "running": False,
+ "error": f"{type(exc).__name__}: {exc}",
+ }
+ )
+ logger.warning("%s channel not available", name, exc_info=True)
+
+ self._validate_allow_from()
+
+ def _build_channel_kwargs(self, name: str) -> dict[str, Any]:
+ """Build adapter-specific constructor kwargs for channels that need services."""
+ if name == "websocket":
+ from src.channelsui.gateway_services import build_gateway_services
+
+ return {
+ "gateway": build_gateway_services(
+ session_manager=self._session_service,
+ cron_service=self._cron_service,
+ workspace_path=get_workspace_path(),
+ )
+ }
+ if name == "matrix":
+ return {
+ "restrict_to_workspace": self._global_bool("restrict_to_workspace", False),
+ "workspace": get_workspace_path(),
+ }
+ return {}
+
+ def _get_channel_config(self, name: str) -> dict | None:
+ """Get the config section for channel *name*."""
+ if isinstance(self.config, dict):
+ return self.config.get(name)
+ return getattr(self.config, name, None)
+
+ def _global_bool(self, key: str, default: bool) -> bool:
+ """Read a global boolean from the top-level channels config."""
+ if isinstance(self.config, dict):
+ return self.config.get(key, default)
+ return getattr(self.config, key, default)
+
+ def _validate_allow_from(self) -> None:
+ for name, ch in self.channels.items():
+ cfg = ch.config
+ if isinstance(cfg, dict):
+ allow = cfg.get("allow_from") if "allow_from" in cfg else cfg.get("allowFrom")
+ else:
+ allow = getattr(cfg, "allow_from", None)
+ if allow is None:
+ logger.info(
+ '"%s" has no allowFrom; unapproved users will receive a pairing code',
+ name,
+ )
+
+ def _should_send_progress(self, channel_name: str, *, tool_hint: bool = False) -> bool:
+ """Return whether progress (or tool-hints) may be sent to *channel_name*."""
+ ch = self.channels.get(channel_name)
+ if ch is None:
+ logger.debug("Progress check for unknown channel: %s", channel_name)
+ return False
+ return ch.send_tool_hints if tool_hint else ch.send_progress
+
+ def _resolve_bool_override(self, section: Any, key: str, default: bool) -> bool:
+ """Return *key* from *section* if it is a bool, otherwise *default*.
+
+ For dict configs also checks the camelCase alias.
+ """
+ if isinstance(section, dict):
+ value = section.get(key)
+ if value is None:
+ camel = _BOOL_CAMEL_ALIASES.get(key)
+ if camel:
+ value = section.get(camel)
+ return value if isinstance(value, bool) else default
+ value = getattr(section, key, None)
+ return value if isinstance(value, bool) else default
+
+ async def _start_channel(self, name: str, channel: BaseChannel) -> None:
+ """Start a channel and log any exceptions."""
+ try:
+ await channel.start()
+ except Exception:
+ logger.exception("Failed to start channel %s", name)
+
+ async def start_all(self) -> None:
+ """Start all channels and the outbound dispatcher."""
+ if not self.channels:
+ logger.warning("No channels enabled")
+ return
+
+ self._dispatch_task = asyncio.create_task(self._dispatch_outbound())
+
+ tasks = []
+ for name, channel in self.channels.items():
+ logger.info("Starting %s channel...", name)
+ tasks.append(asyncio.create_task(self._start_channel(name, channel)))
+
+ await asyncio.gather(*tasks, return_exceptions=True)
+ for name, channel in self.channels.items():
+ self._status.setdefault(name, {})
+ self._status[name]["running"] = channel.is_running
+
+ async def stop_all(self) -> None:
+ """Stop all channels and the dispatcher."""
+ logger.info("Stopping all channels...")
+
+ if self._dispatch_task:
+ self._dispatch_task.cancel()
+ with suppress(asyncio.CancelledError):
+ await self._dispatch_task
+
+ for name, channel in self.channels.items():
+ try:
+ await channel.stop()
+ logger.info("Stopped %s channel", name)
+ except asyncio.CancelledError:
+ if asyncio.current_task() and asyncio.current_task().cancelling():
+ raise
+ logger.debug("Channel %s stop task was already cancelled", name)
+ except Exception:
+ logger.exception("Error stopping %s", name)
+ finally:
+ self._status.setdefault(name, {})
+ self._status[name]["running"] = channel.is_running
+
+ # --- Outbound dispatch ---
+
+ @staticmethod
+ def _fingerprint_content(content: str) -> str:
+ normalized = " ".join(content.split())
+ return hashlib.sha1(normalized.encode("utf-8")).hexdigest() if normalized else ""
+
+ def _should_suppress_outbound(self, msg: OutboundMessage) -> bool:
+ metadata = msg.metadata or {}
+ if metadata.get("_progress"):
+ return False
+ fingerprint = self._fingerprint_content(msg.content)
+ if not fingerprint:
+ return False
+
+ origin_message_id = metadata.get("origin_message_id")
+ if isinstance(origin_message_id, str) and origin_message_id:
+ key = (msg.channel, msg.chat_id, origin_message_id)
+ if self._origin_reply_fingerprints.get(key) == fingerprint:
+ return True
+ self._origin_reply_fingerprints[key] = fingerprint
+
+ message_id = metadata.get("message_id")
+ if isinstance(message_id, str) and message_id:
+ key = (msg.channel, msg.chat_id, message_id)
+ self._origin_reply_fingerprints[key] = fingerprint
+
+ return False
+
+ async def _dispatch_outbound(self) -> None:
+ """Dispatch outbound messages to the appropriate channel."""
+ logger.info("Outbound dispatcher started")
+
+ pending: list[OutboundMessage] = []
+
+ while True:
+ try:
+ if pending:
+ msg = pending.pop(0)
+ else:
+ msg = await asyncio.wait_for(
+ self.bus.consume_outbound(), timeout=1.0,
+ )
+
+ # Reasoning routing
+ if (
+ msg.metadata.get("_reasoning_delta")
+ or msg.metadata.get("_reasoning_end")
+ or msg.metadata.get("_reasoning")
+ ):
+ channel = self.channels.get(msg.channel)
+ if channel is not None and channel.show_reasoning:
+ await self._send_with_retry(channel, msg)
+ continue
+
+ # Progress / tool-hint filtering
+ if msg.metadata.get("_progress"):
+ if msg.metadata.get("_tool_hint") and not self._should_send_progress(
+ msg.channel, tool_hint=True,
+ ):
+ continue
+ if not msg.metadata.get("_tool_hint") and not self._should_send_progress(
+ msg.channel, tool_hint=False,
+ ):
+ continue
+
+ if msg.metadata.get("_retry_wait"):
+ continue
+
+ # Coalesce consecutive _stream_delta messages
+ if msg.metadata.get("_stream_delta") and not msg.metadata.get("_stream_end"):
+ msg, extra_pending = self._coalesce_stream_deltas(msg)
+ pending.extend(extra_pending)
+
+ channel = self.channels.get(msg.channel)
+ if channel:
+ if (
+ not msg.metadata.get("_stream_delta")
+ and not msg.metadata.get("_stream_end")
+ and not msg.metadata.get("_streamed")
+ ):
+ if self._should_suppress_outbound(msg):
+ logger.info(
+ "Suppressing duplicate outbound message to %s:%s",
+ msg.channel, msg.chat_id,
+ )
+ continue
+ await self._send_with_retry(channel, msg)
+ else:
+ logger.warning("Unknown channel: %s", msg.channel)
+
+ except asyncio.TimeoutError:
+ continue
+ except asyncio.CancelledError:
+ break
+
+ @staticmethod
+ async def _send_once(channel: BaseChannel, msg: OutboundMessage) -> None:
+ """Send one outbound message without retry policy."""
+ if msg.metadata.get("_reasoning_end"):
+ await channel.send_reasoning_end(msg.chat_id, msg.metadata)
+ elif msg.metadata.get("_reasoning_delta"):
+ await channel.send_reasoning_delta(msg.chat_id, msg.content, msg.metadata)
+ elif msg.metadata.get("_reasoning"):
+ await channel.send_reasoning(msg)
+ elif msg.metadata.get("_file_edit_events"):
+ edits = msg.metadata.get("_file_edit_events")
+ await channel.send_file_edit_events(
+ msg.chat_id,
+ edits if isinstance(edits, list) else [],
+ msg.metadata,
+ )
+ elif msg.metadata.get("_stream_delta") or msg.metadata.get("_stream_end"):
+ await channel.send_delta(msg.chat_id, msg.content, msg.metadata)
+ elif not msg.metadata.get("_streamed"):
+ await channel.send(msg)
+
+ def _coalesce_stream_deltas(
+ self, first_msg: OutboundMessage,
+ ) -> tuple[OutboundMessage, list[OutboundMessage]]:
+ """Merge consecutive _stream_delta messages for the same target.
+
+ This reduces the number of API calls when the queue has accumulated
+ multiple deltas.
+ """
+ first_metadata = first_msg.metadata or {}
+ target_key = (
+ first_msg.channel,
+ first_msg.chat_id,
+ first_metadata.get("_stream_id"),
+ )
+ combined_content = first_msg.content
+ final_metadata = dict(first_msg.metadata or {})
+ non_matching: list[OutboundMessage] = []
+
+ while True:
+ try:
+ next_msg = self.bus.outbound.get_nowait()
+ except asyncio.QueueEmpty:
+ break
+
+ next_metadata = next_msg.metadata or {}
+ same_target = (
+ next_msg.channel,
+ next_msg.chat_id,
+ next_metadata.get("_stream_id"),
+ ) == target_key
+ is_delta = next_metadata.get("_stream_delta")
+ is_end = next_metadata.get("_stream_end")
+
+ if same_target and is_delta and not final_metadata.get("_stream_end"):
+ combined_content += next_msg.content
+ if is_end:
+ final_metadata["_stream_end"] = True
+ break
+ else:
+ non_matching.append(next_msg)
+ break
+
+ merged = OutboundMessage(
+ channel=first_msg.channel,
+ chat_id=first_msg.chat_id,
+ content=combined_content,
+ metadata=final_metadata,
+ )
+ return merged, non_matching
+
+ async def _send_with_retry(
+ self, channel: BaseChannel, msg: OutboundMessage,
+ ) -> None:
+ """Send a message with retry on failure using exponential backoff."""
+ max_attempts = 2 # conservative default
+ if isinstance(self.config, dict):
+ max_attempts = max(self.config.get("send_max_retries", 2), 1)
+ elif hasattr(self.config, "send_max_retries"):
+ max_attempts = max(self.config.send_max_retries, 1)
+
+ for attempt in range(max_attempts):
+ try:
+ await self._send_once(channel, msg)
+ return
+ except asyncio.CancelledError:
+ raise
+ except Exception as e:
+ if attempt == max_attempts - 1:
+ logger.exception(
+ "Failed to send to %s after %d attempts",
+ msg.channel, max_attempts,
+ )
+ return
+ delay = _SEND_RETRY_DELAYS[min(attempt, len(_SEND_RETRY_DELAYS) - 1)]
+ logger.warning(
+ "Send to %s failed (attempt %d/%d): %s, retrying in %ds",
+ msg.channel, attempt + 1, max_attempts, type(e).__name__, delay,
+ )
+ try:
+ await asyncio.sleep(delay)
+ except asyncio.CancelledError:
+ raise
+
+ # --- Public API ---
+
+ def get_channel(self, name: str) -> BaseChannel | None:
+ """Get a channel by name."""
+ return self.channels.get(name)
+
+ def get_status(self) -> dict[str, Any]:
+ """Get status of all channels."""
+ status = {name: dict(item) for name, item in self._status.items()}
+ for name, channel in self.channels.items():
+ status.setdefault(name, {})
+ status[name].update(
+ {
+ "enabled": True,
+ "loaded": True,
+ "running": channel.is_running,
+ "display_name": getattr(channel, "display_name", name),
+ }
+ )
+ return status
+
+ @property
+ def enabled_channels(self) -> list[str]:
+ """Get list of enabled channel names."""
+ return list(self.channels.keys())
diff --git a/agent/src/channels/matrix.py b/agent/src/channels/matrix.py
new file mode 100644
index 000000000..103ef7815
--- /dev/null
+++ b/agent/src/channels/matrix.py
@@ -0,0 +1,1022 @@
+"""Matrix (Element) channel — inbound sync + outbound message/media delivery."""
+
+import asyncio
+import json
+import mimetypes
+import time
+from contextlib import suppress
+from dataclasses import dataclass
+from pathlib import Path
+from typing import Any, Literal, TypeAlias
+from urllib.parse import quote, urlparse
+
+from pydantic import Field
+
+from src.security.workspace_policy import is_path_within
+
+try:
+ import aiohttp
+ import nh3
+ from mistune import create_markdown
+ from nio import (
+ AsyncClient,
+ AsyncClientConfig,
+ InviteEvent,
+ JoinError,
+ KeyVerificationCancel,
+ KeyVerificationEvent,
+ KeyVerificationKey,
+ KeyVerificationMac,
+ KeyVerificationStart,
+ LoginResponse,
+ MatrixRoom,
+ RoomEncryptedMedia,
+ RoomMessage,
+ RoomMessageMedia,
+ RoomMessageText,
+ RoomSendError,
+ RoomSendResponse,
+ RoomTypingError,
+ SyncError,
+ ToDeviceError,
+ UploadError,
+ )
+ from nio.crypto.attachments import decrypt_attachment
+ from nio.exceptions import EncryptionError
+except ImportError as e:
+ raise ImportError(
+ "Matrix dependencies not installed. Run: pip install vibe-trading-ai[matrix]"
+ ) from e
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.utils import safe_filename
+from src.utils.logging_bridge import redirect_lib_logging
+
+TYPING_NOTICE_TIMEOUT_MS = 30_000
+# Must stay below TYPING_NOTICE_TIMEOUT_MS so the indicator doesn't expire mid-processing.
+TYPING_KEEPALIVE_INTERVAL_MS = 20_000
+MATRIX_HTML_FORMAT = "org.matrix.custom.html"
+_ATTACH_MARKER = "[attachment: {}]"
+_ATTACH_TOO_LARGE = "[attachment: {} - too large]"
+_ATTACH_FAILED = "[attachment: {} - download failed]"
+_ATTACH_UPLOAD_FAILED = "[attachment: {} - upload failed]"
+_DEFAULT_ATTACH_NAME = "attachment"
+_MSGTYPE_MAP = {"m.image": "image", "m.audio": "audio", "m.video": "video", "m.file": "file"}
+
+MATRIX_MEDIA_EVENT_FILTER = (RoomMessageMedia, RoomEncryptedMedia)
+MatrixMediaEvent: TypeAlias = RoomMessageMedia | RoomEncryptedMedia
+
+
+class _MediaTooLargeError(Exception):
+ """Raised when an inbound Matrix media download exceeds the configured cap."""
+
+MATRIX_MARKDOWN = create_markdown(
+ escape=True,
+ plugins=["table", "strikethrough", "url", "superscript", "subscript"],
+)
+
+MATRIX_ALLOWED_HTML_TAGS = {
+ "p", "a", "strong", "em", "del", "code", "pre", "blockquote",
+ "ul", "ol", "li", "h1", "h2", "h3", "h4", "h5", "h6",
+ "hr", "br", "table", "thead", "tbody", "tr", "th", "td",
+ "caption", "sup", "sub", "img",
+}
+MATRIX_ALLOWED_HTML_ATTRIBUTES: dict[str, set[str]] = {
+ "a": {"href"}, "code": {"class"}, "ol": {"start"},
+ "img": {"src", "alt", "title", "width", "height"},
+}
+MATRIX_ALLOWED_URL_SCHEMES = {"https", "http", "matrix", "mailto", "mxc"}
+
+
+def _filter_matrix_html_attribute(tag: str, attr: str, value: str) -> str | None:
+ """Filter attribute values to a safe Matrix-compatible subset."""
+ if tag == "a" and attr == "href":
+ return value if value.lower().startswith(("https://", "http://", "matrix:", "mailto:")) else None
+ if tag == "img" and attr == "src":
+ return value if value.lower().startswith("mxc://") else None
+ if tag == "code" and attr == "class":
+ classes = [c for c in value.split() if c.startswith("language-") and not c.startswith("language-_")]
+ return " ".join(classes) if classes else None
+ return value
+
+
+MATRIX_HTML_CLEANER = nh3.Cleaner(
+ tags=MATRIX_ALLOWED_HTML_TAGS,
+ attributes=MATRIX_ALLOWED_HTML_ATTRIBUTES,
+ attribute_filter=_filter_matrix_html_attribute,
+ url_schemes=MATRIX_ALLOWED_URL_SCHEMES,
+ strip_comments=True,
+ link_rel="noopener noreferrer",
+)
+
+@dataclass
+class _StreamBuf:
+ """
+ Represents a buffer for managing LLM response stream data.
+
+ :ivar text: Stores the text content of the buffer.
+ :type text: str
+ :ivar event_id: Identifier for the associated event. None indicates no
+ specific event association.
+ :type event_id: str | None
+ :ivar last_edit: Timestamp of the most recent edit to the buffer.
+ :type last_edit: float
+ """
+ text: str = ""
+ event_id: str | None = None
+ last_edit: float = 0.0
+
+def _render_markdown_html(text: str) -> str | None:
+ """Render markdown to sanitized HTML; returns None for plain text."""
+ try:
+ formatted = MATRIX_HTML_CLEANER.clean(MATRIX_MARKDOWN(text)).strip()
+ except Exception:
+ return None
+ if not formatted:
+ return None
+ # Skip formatted_body for plain <p>text</p> to keep payload minimal.
+ if formatted.startswith("<p>") and formatted.endswith("</p>"):
+ inner = formatted[3:-4]
+ if "<" not in inner and ">" not in inner:
+ return None
+ return formatted
+
+
+def _build_matrix_text_content(
+ text: str,
+ event_id: str | None = None,
+ thread_relates_to: dict[str, object] | None = None,
+) -> dict[str, object]:
+ """
+ Constructs and returns a dictionary representing the matrix text content with optional
+ HTML formatting and reference to an existing event for replacement. This function is
+ primarily used to create content payloads compatible with the Matrix messaging protocol.
+
+ :param text: The plain text content to include in the message.
+ :type text: str
+ :param event_id: Optional ID of the event to replace. If provided, the function will
+ include information indicating that the message is a replacement of the specified
+ event.
+ :type event_id: str | None
+ :param thread_relates_to: Optional Matrix thread relation metadata. For edits this is
+ stored in ``m.new_content`` so the replacement remains in the same thread.
+ :type thread_relates_to: dict[str, object] | None
+ :return: A dictionary containing the matrix text content, potentially enriched with
+ HTML formatting and replacement metadata if applicable.
+ :rtype: dict[str, object]
+ """
+ content: dict[str, object] = {"msgtype": "m.text", "body": text, "m.mentions": {}}
+ if html := _render_markdown_html(text):
+ content["format"] = MATRIX_HTML_FORMAT
+ content["formatted_body"] = html
+ if event_id:
+ content["m.new_content"] = {
+ "body": text,
+ "msgtype": "m.text",
+ }
+ content["m.relates_to"] = {
+ "rel_type": "m.replace",
+ "event_id": event_id,
+ }
+ if thread_relates_to:
+ content["m.new_content"]["m.relates_to"] = thread_relates_to
+ elif thread_relates_to:
+ content["m.relates_to"] = thread_relates_to
+
+ return content
+
+
+class MatrixConfig(BaseModel):
+ """Matrix (Element) channel configuration."""
+
+ enabled: bool = False
+ homeserver: str = "https://matrix.org"
+ user_id: str = ""
+ password: str = ""
+ access_token: str = ""
+ device_id: str = ""
+ e2ee_enabled: bool = Field(default=True, alias="e2eeEnabled")
+ sas_verification: bool = Field(default=False, alias="sasVerification")
+ sync_stop_grace_seconds: int = 2
+ max_media_bytes: int = 20 * 1024 * 1024
+ max_concurrent_media_downloads: int = 2
+ allow_from: list[str] = Field(default_factory=list)
+ group_policy: Literal["open", "mention", "allowlist"] = "open"
+ group_allow_from: list[str] = Field(default_factory=list)
+ allow_room_mentions: bool = False
+ streaming: bool = False
+
+
+class MatrixChannel(BaseChannel):
+ """Matrix (Element) channel using long-polling sync."""
+
+ name = "matrix"
+ display_name = "Matrix"
+ _STREAM_EDIT_INTERVAL = 2 # min seconds between edit_message_text calls
+ monotonic_time = time.monotonic
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return MatrixConfig().model_dump(by_alias=True)
+
+ def __init__(
+ self,
+ config: Any,
+ bus: MessageBus,
+ *,
+ restrict_to_workspace: bool = False,
+ workspace: str | Path | None = None,
+ ):
+ if isinstance(config, dict):
+ config = MatrixConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.client: AsyncClient | None = None
+ self._sync_task: asyncio.Task | None = None
+ self._typing_tasks: dict[str, asyncio.Task] = {}
+ self._restrict_to_workspace = bool(restrict_to_workspace)
+ self._workspace = (
+ Path(workspace).expanduser().resolve(strict=False) if workspace is not None else None
+ )
+ self._server_upload_limit_bytes: int | None = None
+ self._server_upload_limit_checked = False
+ self._stream_bufs: dict[str, _StreamBuf] = {}
+ self._started_at_ms: int = 0
+ self._media_download_semaphore = asyncio.Semaphore(
+ max(1, int(self.config.max_concurrent_media_downloads))
+ )
+
+
+ async def start(self) -> None:
+ """Start Matrix client and begin sync loop."""
+ self._running = True
+ self._started_at_ms = int(time.time() * 1000)
+ redirect_lib_logging("nio", level="WARNING")
+
+ self.store_path = get_media_dir("matrix") / "matrix-store"
+ self.store_path.mkdir(parents=True, exist_ok=True)
+ self.session_path = self.store_path / "session.json"
+
+ # Replace ':' with '_' to produce a Windows-safe filename
+ safe_store_name = self.config.user_id.replace(":", "_") + f"_{self.config.device_id}.db"
+
+ self.client = AsyncClient(
+ homeserver=self.config.homeserver,
+ user=self.config.user_id,
+ store_path=self.store_path,
+ config=AsyncClientConfig(
+ store_sync_tokens=True,
+ encryption_enabled=self.config.e2ee_enabled,
+ store_name=safe_store_name,
+ ),
+ )
+
+ self._register_event_callbacks()
+ self._register_to_device_callbacks()
+ self._register_response_callbacks()
+
+ if not self.config.e2ee_enabled:
+ self.logger.warning("E2EE disabled; encrypted rooms may be undecryptable.")
+
+ if self.config.password:
+ if self.config.access_token or self.config.device_id:
+ self.logger.warning("Password-based login active; access_token and device_id fields will be ignored.")
+
+ create_new_session = True
+ if self.session_path.exists():
+ self.logger.info("Found session.json at {}; attempting to use existing session...", self.session_path)
+ try:
+ with open(self.session_path, "r", encoding="utf-8") as f:
+ session = json.load(f)
+ self.client.user_id = self.config.user_id
+ self.client.access_token = session["access_token"]
+ self.client.device_id = session["device_id"]
+ self.client.load_store()
+ self.logger.info("Successfully loaded from existing session")
+ create_new_session = False
+ except Exception as e:
+ self.logger.warning("Failed to load from existing session: {}", e)
+ self.logger.info("Falling back to password login...")
+
+ if create_new_session:
+ self.logger.info("Using password login...")
+ resp = await self.client.login(self.config.password)
+ if isinstance(resp, LoginResponse):
+ self.logger.info("Logged in using a password; saving details to disk")
+ self._write_session_to_disk(resp)
+ else:
+ self.logger.error("Failed to log in: {}", resp)
+ return
+
+ elif self.config.access_token and self.config.device_id:
+ try:
+ self.client.user_id = self.config.user_id
+ self.client.access_token = self.config.access_token
+ self.client.device_id = self.config.device_id
+ self.client.load_store()
+ self.logger.info("Successfully loaded from existing session")
+ except Exception as e:
+ self.logger.warning("Failed to load from existing session: {}", e)
+
+ else:
+ self.logger.warning("Unable to load a session due to missing password, access_token, or device_id; encryption may not work")
+ return
+
+ self._sync_task = asyncio.create_task(self._sync_loop())
+
+ async def stop(self) -> None:
+ """Stop the Matrix channel with graceful sync shutdown."""
+ self._running = False
+ for room_id in list(self._typing_tasks):
+ await self._stop_typing_keepalive(room_id, clear_typing=False)
+ if self.client:
+ self.client.stop_sync_forever()
+ if self._sync_task:
+ try:
+ await asyncio.wait_for(asyncio.shield(self._sync_task),
+ timeout=self.config.sync_stop_grace_seconds)
+ except (asyncio.TimeoutError, asyncio.CancelledError):
+ self._sync_task.cancel()
+ with suppress(asyncio.CancelledError):
+ await self._sync_task
+ if self.client:
+ await self.client.close()
+
+ def _write_session_to_disk(self, resp: LoginResponse) -> None:
+ """Save login session to disk for persistence across restarts."""
+ session = {
+ "access_token": resp.access_token,
+ "device_id": resp.device_id,
+ }
+ try:
+ with open(self.session_path, "w", encoding="utf-8") as f:
+ json.dump(session, f, indent=2)
+ self.logger.info("Session saved to {}", self.session_path)
+ except Exception as e:
+ self.logger.warning("Failed to save session: {}", e)
+
+ def _is_workspace_path_allowed(self, path: Path) -> bool:
+ """Check path is inside workspace (when restriction enabled)."""
+ if not self._restrict_to_workspace or not self._workspace:
+ return True
+ return is_path_within(path, self._workspace)
+
+ def _collect_outbound_media_candidates(self, media: list[str]) -> list[Path]:
+ """Deduplicate and resolve outbound attachment paths."""
+ seen: set[str] = set()
+ candidates: list[Path] = []
+ for raw in media:
+ if not isinstance(raw, str) or not raw.strip():
+ continue
+ path = Path(raw.strip()).expanduser()
+ try:
+ key = str(path.resolve(strict=False))
+ except OSError:
+ key = str(path)
+ if key not in seen:
+ seen.add(key)
+ candidates.append(path)
+ return candidates
+
+ @staticmethod
+ def _build_outbound_attachment_content(
+ *, filename: str, mime: str, size_bytes: int,
+ mxc_url: str, encryption_info: dict[str, Any] | None = None,
+ ) -> dict[str, Any]:
+ """Build Matrix content payload for an uploaded file/image/audio/video."""
+ prefix = mime.split("/")[0]
+ msgtype = {"image": "m.image", "audio": "m.audio", "video": "m.video"}.get(prefix, "m.file")
+ content: dict[str, Any] = {
+ "msgtype": msgtype, "body": filename, "filename": filename,
+ "info": {"mimetype": mime, "size": size_bytes}, "m.mentions": {},
+ }
+ if encryption_info:
+ content["file"] = {**encryption_info, "url": mxc_url}
+ else:
+ content["url"] = mxc_url
+ return content
+
+ def _is_encrypted_room(self, room_id: str) -> bool:
+ if not self.client:
+ return False
+ room = getattr(self.client, "rooms", {}).get(room_id)
+ return bool(getattr(room, "encrypted", False))
+
+ async def _send_room_content(self, room_id: str,
+ content: dict[str, Any]) -> None | RoomSendResponse | RoomSendError:
+ """Send m.room.message with E2EE options."""
+ if not self.client:
+ return None
+ kwargs: dict[str, Any] = {"room_id": room_id, "message_type": "m.room.message", "content": content}
+
+ if self.config.e2ee_enabled:
+ kwargs["ignore_unverified_devices"] = True
+ response = await self.client.room_send(**kwargs)
+ return response
+
+ async def _resolve_server_upload_limit_bytes(self) -> int | None:
+ """Query homeserver upload limit once per channel lifecycle."""
+ if self._server_upload_limit_checked:
+ return self._server_upload_limit_bytes
+ self._server_upload_limit_checked = True
+ if not self.client:
+ return None
+ try:
+ response = await self.client.content_repository_config()
+ except Exception:
+ self.logger.error("Failed to fetch server upload limit", exc_info=True)
+ return None
+ upload_size = getattr(response, "upload_size", None)
+ if isinstance(upload_size, int) and upload_size > 0:
+ self._server_upload_limit_bytes = upload_size
+ return upload_size
+ return None
+
+ async def _effective_media_limit_bytes(self) -> int:
+ """min(local config, server advertised) — 0 blocks all uploads."""
+ local_limit = max(int(self.config.max_media_bytes), 0)
+ server_limit = await self._resolve_server_upload_limit_bytes()
+ if server_limit is None:
+ return local_limit
+ return min(local_limit, server_limit) if local_limit else 0
+
+ async def _upload_and_send_attachment(
+ self, room_id: str, path: Path, limit_bytes: int,
+ relates_to: dict[str, Any] | None = None,
+ ) -> str | None:
+ """Upload one local file to Matrix and send it as a media message. Returns failure marker or None."""
+ if not self.client:
+ return _ATTACH_UPLOAD_FAILED.format(path.name or _DEFAULT_ATTACH_NAME)
+
+ resolved = path.expanduser().resolve(strict=False)
+ filename = safe_filename(resolved.name) or _DEFAULT_ATTACH_NAME
+ fail = _ATTACH_UPLOAD_FAILED.format(filename)
+
+ if not resolved.is_file() or not self._is_workspace_path_allowed(resolved):
+ return fail
+ try:
+ size_bytes = resolved.stat().st_size
+ except OSError:
+ return fail
+ if limit_bytes <= 0 or size_bytes > limit_bytes:
+ return _ATTACH_TOO_LARGE.format(filename)
+
+ mime = mimetypes.guess_type(filename, strict=False)[0] or "application/octet-stream"
+ try:
+ with resolved.open("rb") as f:
+ upload_result = await self.client.upload(
+ f, content_type=mime, filename=filename,
+ encrypt=self.config.e2ee_enabled and self._is_encrypted_room(room_id),
+ filesize=size_bytes,
+ )
+ except Exception:
+ self.logger.error("Matrix media upload failed for %s", filename, exc_info=True)
+ return fail
+
+ upload_response = upload_result[0] if isinstance(upload_result, tuple) else upload_result
+ encryption_info = upload_result[1] if isinstance(upload_result, tuple) and isinstance(upload_result[1], dict) else None
+ if isinstance(upload_response, UploadError):
+ return fail
+ mxc_url = getattr(upload_response, "content_uri", None)
+ if not isinstance(mxc_url, str) or not mxc_url.startswith("mxc://"):
+ return fail
+
+ content = self._build_outbound_attachment_content(
+ filename=filename, mime=mime, size_bytes=size_bytes,
+ mxc_url=mxc_url, encryption_info=encryption_info,
+ )
+ if relates_to:
+ content["m.relates_to"] = relates_to
+ try:
+ await self._send_room_content(room_id, content)
+ except Exception:
+ self.logger.error("Matrix room content send failed for room_id=%s", room_id, exc_info=True)
+ return fail
+ return None
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send outbound content; clear typing for non-progress messages."""
+ if not self.client:
+ return
+ text = msg.content or ""
+ candidates = self._collect_outbound_media_candidates(msg.media)
+ relates_to = self._build_thread_relates_to(msg.metadata)
+ is_progress = bool((msg.metadata or {}).get("_progress"))
+ try:
+ failures: list[str] = []
+ if candidates:
+ limit_bytes = await self._effective_media_limit_bytes()
+ for path in candidates:
+ if fail := await self._upload_and_send_attachment(
+ room_id=msg.chat_id,
+ path=path,
+ limit_bytes=limit_bytes,
+ relates_to=relates_to,
+ ):
+ failures.append(fail)
+ if failures:
+ text = f"{text.rstrip()}\n{chr(10).join(failures)}" if text.strip() else "\n".join(failures)
+ if text.strip():
+ content = _build_matrix_text_content(text)
+ if relates_to:
+ content["m.relates_to"] = relates_to
+ await self._send_room_content(msg.chat_id, content)
+ finally:
+ if not is_progress:
+ await self._stop_typing_keepalive(msg.chat_id, clear_typing=True)
+
+ async def send_delta(self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None) -> None:
+ meta = metadata or {}
+ relates_to = self._build_thread_relates_to(metadata)
+
+ if meta.get("_stream_end"):
+ buf = self._stream_bufs.pop(chat_id, None)
+ if not buf or not buf.event_id or not buf.text:
+ return
+
+ await self._stop_typing_keepalive(chat_id, clear_typing=True)
+
+ content = _build_matrix_text_content(
+ buf.text,
+ buf.event_id,
+ thread_relates_to=relates_to,
+ )
+ await self._send_room_content(chat_id, content)
+ return
+
+ buf = self._stream_bufs.get(chat_id)
+ if buf is None:
+ buf = _StreamBuf()
+ self._stream_bufs[chat_id] = buf
+ buf.text += delta
+
+ if not buf.text.strip():
+ return
+
+ now = self.monotonic_time()
+
+ if not buf.last_edit or (now - buf.last_edit) >= self._STREAM_EDIT_INTERVAL:
+ try:
+ content = _build_matrix_text_content(
+ buf.text,
+ buf.event_id,
+ thread_relates_to=relates_to,
+ )
+ response = await self._send_room_content(chat_id, content)
+ buf.last_edit = now
+ if not buf.event_id:
+ # we are editing the same message all the time, so only the first time the event id needs to be set
+ buf.event_id = response.event_id
+ except Exception:
+ self.logger.error("Stream send/edit failed for chat_id=%s", chat_id, exc_info=True)
+ await self._stop_typing_keepalive(chat_id, clear_typing=True)
+
+
+ def _register_event_callbacks(self) -> None:
+ self.client.add_event_callback(self._on_message, RoomMessageText)
+ self.client.add_event_callback(self._on_media_message, MATRIX_MEDIA_EVENT_FILTER)
+ self.client.add_event_callback(self._on_room_invite, InviteEvent)
+
+ def _register_to_device_callbacks(self) -> None:
+ if self.config.e2ee_enabled and self.config.sas_verification:
+ self.client.add_to_device_callback(
+ self._on_key_verification_event,
+ (KeyVerificationEvent,),
+ )
+
+ def _register_response_callbacks(self) -> None:
+ self.client.add_response_callback(self._on_sync_error, SyncError)
+ self.client.add_response_callback(self._on_join_error, JoinError)
+ self.client.add_response_callback(self._on_send_error, RoomSendError)
+
+ def _is_sas_sender_allowed(self, sender: str) -> bool:
+ return bool(sender and self.is_allowed(sender))
+
+ async def _on_key_verification_event(self, event: KeyVerificationEvent) -> None:
+ try:
+ await self._handle_key_verification_event(event)
+ except asyncio.CancelledError:
+ raise
+ except Exception:
+ self.logger.exception("Matrix SAS verification handling failed")
+
+ async def _handle_key_verification_event(self, event: KeyVerificationEvent) -> None:
+ if not (self.config.e2ee_enabled and self.config.sas_verification):
+ return
+ if not self.client:
+ return
+
+ sender = str(getattr(event, "sender", "") or "")
+ transaction_id = str(getattr(event, "transaction_id", "") or "")
+ if not transaction_id or not self._is_sas_sender_allowed(sender):
+ return
+
+ if isinstance(event, KeyVerificationStart):
+ if "emoji" not in (getattr(event, "short_authentication_string", None) or []):
+ self.logger.info(
+ "Ignoring Matrix SAS verification from {} without emoji support",
+ sender,
+ )
+ return
+
+ response = await self.client.accept_key_verification(transaction_id)
+ if isinstance(response, ToDeviceError):
+ self.logger.warning("Matrix SAS accept failed for {}: {}", sender, response)
+ return
+
+ if isinstance(event, KeyVerificationKey):
+ responses = await self.client.send_to_device_messages()
+ if any(isinstance(response, ToDeviceError) for response in responses):
+ self.logger.warning("Matrix SAS key share failed for {}", sender)
+ return
+
+ response = await self.client.confirm_short_auth_string(transaction_id)
+ if isinstance(response, ToDeviceError):
+ self.logger.warning("Matrix SAS confirm failed for {}: {}", sender, response)
+ return
+
+ if isinstance(event, KeyVerificationMac):
+ sas = getattr(self.client, "key_verifications", {}).get(transaction_id)
+ if sas is not None and getattr(sas, "verified", False):
+ self.logger.info("Matrix SAS verification completed for {}", sender)
+ return
+
+ if isinstance(event, KeyVerificationCancel):
+ self.logger.info(
+ "Matrix SAS verification cancelled by {}: {}",
+ sender,
+ getattr(event, "reason", ""),
+ )
+
+ def _is_fatal_auth_response(self, response: Any) -> bool:
+ code = getattr(response, "status_code", None)
+ is_auth = code in {"M_UNKNOWN_TOKEN", "M_FORBIDDEN", "M_UNAUTHORIZED"}
+ return is_auth or bool(getattr(response, "soft_logout", False))
+
+ def _log_response_error(self, label: str, response: Any) -> None:
+ """Log Matrix response errors — auth errors at ERROR level, rest at WARNING."""
+ is_fatal = self._is_fatal_auth_response(response)
+ (self.logger.error if is_fatal else self.logger.warning)("{} failed: {}", label, response)
+
+ async def _on_sync_error(self, response: SyncError) -> None:
+ self._log_response_error("sync", response)
+ if self._is_fatal_auth_response(response):
+ # Auth errors won't recover by retry; stop the sync loop instead of
+ # spamming the homeserver every 2s (#1851).
+ self.logger.error("Authentication failed irrecoverably; stopping sync loop")
+ self._running = False
+ if self.client:
+ with suppress(Exception):
+ self.client.stop_sync_forever()
+
+ async def _on_join_error(self, response: JoinError) -> None:
+ self._log_response_error("join", response)
+
+ async def _on_send_error(self, response: RoomSendError) -> None:
+ self._log_response_error("send", response)
+
+ async def _set_typing(self, room_id: str, typing: bool) -> None:
+ """Best-effort typing indicator update."""
+ if not self.client:
+ return
+ with suppress(Exception):
+ response = await self.client.room_typing(room_id=room_id, typing_state=typing,
+ timeout=TYPING_NOTICE_TIMEOUT_MS)
+ if isinstance(response, RoomTypingError):
+ self.logger.debug("typing failed for {}: {}", room_id, response)
+
+ async def _start_typing_keepalive(self, room_id: str) -> None:
+ """Start periodic typing refresh (spec-recommended keepalive)."""
+ await self._stop_typing_keepalive(room_id, clear_typing=False)
+ await self._set_typing(room_id, True)
+ if not self._running:
+ return
+
+ async def loop() -> None:
+ with suppress(asyncio.CancelledError):
+ while self._running:
+ await asyncio.sleep(TYPING_KEEPALIVE_INTERVAL_MS / 1000)
+ await self._set_typing(room_id, True)
+
+ self._typing_tasks[room_id] = asyncio.create_task(loop())
+
+ async def _stop_typing_keepalive(self, room_id: str, *, clear_typing: bool) -> None:
+ if task := self._typing_tasks.pop(room_id, None):
+ task.cancel()
+ with suppress(asyncio.CancelledError):
+ await task
+ if clear_typing:
+ await self._set_typing(room_id, False)
+
+ async def _sync_loop(self) -> None:
+ backoff = 2.0
+ while self._running:
+ try:
+ await self.client.sync_forever(timeout=30000, full_state=True)
+ backoff = 2.0
+ except asyncio.CancelledError:
+ break
+ except Exception:
+ if not self._running:
+ break
+ await asyncio.sleep(backoff)
+ backoff = min(backoff * 2, 60.0)
+
+ async def _on_room_invite(self, room: MatrixRoom, event: InviteEvent) -> None:
+ if self.is_allowed(event.sender):
+ await self.client.join(room.room_id)
+
+ def _is_direct_room(self, room: MatrixRoom) -> bool:
+ count = getattr(room, "member_count", None)
+ return isinstance(count, int) and count <= 2
+
+ def _is_bot_mentioned(self, event: RoomMessage) -> bool:
+ """Check m.mentions payload for bot mention."""
+ source = getattr(event, "source", None)
+ if not isinstance(source, dict):
+ return False
+ mentions = (source.get("content") or {}).get("m.mentions")
+ if not isinstance(mentions, dict):
+ return False
+ user_ids = mentions.get("user_ids")
+ if isinstance(user_ids, list) and self.config.user_id in user_ids:
+ return True
+ return bool(self.config.allow_room_mentions and mentions.get("room") is True)
+
+ def _is_pre_startup_event(self, event: RoomMessage) -> bool:
+ """Skip events that landed in the timeline before this process started.
+
+ Matrix sync replays the room timeline on each startup/restart; without
+ this filter old messages would be re-handled as if they were fresh
+ (#3553).
+ """
+ ts = getattr(event, "server_timestamp", None)
+ return isinstance(ts, int) and ts < self._started_at_ms
+
+ def _should_process_message(self, room: MatrixRoom, event: RoomMessage) -> bool:
+ """Apply sender and room policy checks."""
+ if not self.is_allowed(event.sender):
+ return False
+ if self._is_direct_room(room):
+ return True
+ policy = self.config.group_policy
+ if policy == "open":
+ return True
+ if policy == "allowlist":
+ return room.room_id in (self.config.group_allow_from or [])
+ if policy == "mention":
+ return self._is_bot_mentioned(event)
+ return False
+
+ def _media_dir(self) -> Path:
+ return get_media_dir("matrix")
+
+ @staticmethod
+ def _event_source_content(event: RoomMessage) -> dict[str, Any]:
+ source = getattr(event, "source", None)
+ if not isinstance(source, dict):
+ return {}
+ content = source.get("content")
+ return content if isinstance(content, dict) else {}
+
+ def _event_thread_root_id(self, event: RoomMessage) -> str | None:
+ relates_to = self._event_source_content(event).get("m.relates_to")
+ if not isinstance(relates_to, dict) or relates_to.get("rel_type") != "m.thread":
+ return None
+ root_id = relates_to.get("event_id")
+ return root_id if isinstance(root_id, str) and root_id else None
+
+ def _thread_metadata(self, event: RoomMessage) -> dict[str, str] | None:
+ if not (root_id := self._event_thread_root_id(event)):
+ return None
+ meta: dict[str, str] = {"thread_root_event_id": root_id}
+ if isinstance(reply_to := getattr(event, "event_id", None), str) and reply_to:
+ meta["thread_reply_to_event_id"] = reply_to
+ return meta
+
+ @staticmethod
+ def _build_thread_relates_to(metadata: dict[str, Any] | None) -> dict[str, Any] | None:
+ if not metadata:
+ return None
+ root_id = metadata.get("thread_root_event_id")
+ if not isinstance(root_id, str) or not root_id:
+ return None
+ reply_to = metadata.get("thread_reply_to_event_id") or metadata.get("event_id")
+ if not isinstance(reply_to, str) or not reply_to:
+ return None
+ return {"rel_type": "m.thread", "event_id": root_id,
+ "m.in_reply_to": {"event_id": reply_to}, "is_falling_back": True}
+
+ def _event_attachment_type(self, event: MatrixMediaEvent) -> str:
+ msgtype = self._event_source_content(event).get("msgtype")
+ return _MSGTYPE_MAP.get(msgtype, "file")
+
+ @staticmethod
+ def _is_encrypted_media_event(event: MatrixMediaEvent) -> bool:
+ return (isinstance(getattr(event, "key", None), dict)
+ and isinstance(getattr(event, "hashes", None), dict)
+ and isinstance(getattr(event, "iv", None), str))
+
+ def _event_declared_size_bytes(self, event: MatrixMediaEvent) -> int | None:
+ info = self._event_source_content(event).get("info")
+ size = info.get("size") if isinstance(info, dict) else None
+ return size if type(size) is int and size >= 0 else None
+
+ def _event_mime(self, event: MatrixMediaEvent) -> str | None:
+ info = self._event_source_content(event).get("info")
+ if isinstance(info, dict) and isinstance(m := info.get("mimetype"), str) and m:
+ return m
+ m = getattr(event, "mimetype", None)
+ return m if isinstance(m, str) and m else None
+
+ def _event_filename(self, event: MatrixMediaEvent, attachment_type: str) -> str:
+ body = getattr(event, "body", None)
+ if isinstance(body, str) and body.strip():
+ if candidate := safe_filename(Path(body).name):
+ return candidate
+ return _DEFAULT_ATTACH_NAME if attachment_type == "file" else attachment_type
+
+ def _build_attachment_path(self, event: MatrixMediaEvent, attachment_type: str,
+ filename: str, mime: str | None) -> Path:
+ safe_name = safe_filename(Path(filename).name) or _DEFAULT_ATTACH_NAME
+ suffix = Path(safe_name).suffix
+ if not suffix and mime:
+ if guessed := mimetypes.guess_extension(mime, strict=False):
+ safe_name, suffix = f"{safe_name}{guessed}", guessed
+ stem = (Path(safe_name).stem or attachment_type)[:72]
+ suffix = suffix[:16]
+ event_id = safe_filename(str(getattr(event, "event_id", "") or "evt").lstrip("$"))
+ event_prefix = (event_id[:24] or "evt").strip("_")
+ return self._media_dir() / f"{event_prefix}_{stem}{suffix}"
+
+ async def _download_media_bytes(self, mxc_url: str, limit_bytes: int) -> bytes | None:
+ if not self.client or limit_bytes <= 0:
+ raise _MediaTooLargeError
+
+ parsed = urlparse(mxc_url)
+ if parsed.scheme != "mxc" or not parsed.netloc or not parsed.path.strip("/"):
+ return None
+
+ homeserver = str(getattr(self.client, "homeserver", "") or self.config.homeserver).rstrip("/")
+ media_url = (
+ f"{homeserver}/_matrix/client/v1/media/download/"
+ f"{quote(parsed.netloc, safe='')}/{quote(parsed.path.strip('/'), safe='')}"
+ )
+ token = getattr(self.client, "access_token", None) or self.config.access_token
+ headers = {"Authorization": f"Bearer {token}"} if token else None
+ timeout = aiohttp.ClientTimeout(total=None)
+
+ try:
+ async with aiohttp.ClientSession(timeout=timeout, headers=headers) as session:
+ async with session.get(media_url, params={"allow_remote": "true"}) as response:
+ if response.status >= 400:
+ self.logger.warning("download failed for {}: HTTP {}", mxc_url, response.status)
+ return None
+ content_length = response.headers.get("Content-Length")
+ if content_length is not None:
+ try:
+ if int(content_length) > limit_bytes:
+ raise _MediaTooLargeError
+ except ValueError:
+ pass
+
+ chunks = bytearray()
+ async for chunk in response.content.iter_chunked(64 * 1024):
+ chunks.extend(chunk)
+ if len(chunks) > limit_bytes:
+ raise _MediaTooLargeError
+ return bytes(chunks)
+ except _MediaTooLargeError:
+ raise
+ except (aiohttp.ClientError, asyncio.TimeoutError, OSError):
+ self.logger.warning("download failed for {}", mxc_url, exc_info=True)
+ return None
+
+ def _decrypt_media_bytes(self, event: MatrixMediaEvent, ciphertext: bytes) -> bytes | None:
+ key_obj, hashes, iv = getattr(event, "key", None), getattr(event, "hashes", None), getattr(event, "iv", None)
+ key = key_obj.get("k") if isinstance(key_obj, dict) else None
+ sha256 = hashes.get("sha256") if isinstance(hashes, dict) else None
+ if not all(isinstance(v, str) for v in (key, sha256, iv)):
+ return None
+ try:
+ return decrypt_attachment(ciphertext, key, sha256, iv)
+ except (EncryptionError, ValueError, TypeError):
+ self.logger.warning("decrypt failed for event {}", getattr(event, "event_id", ""))
+ return None
+
+ async def _fetch_media_attachment(
+ self, room: MatrixRoom, event: MatrixMediaEvent,
+ ) -> tuple[dict[str, Any] | None, str]:
+ """Download, decrypt if needed, and persist a Matrix attachment."""
+ atype = self._event_attachment_type(event)
+ mime = self._event_mime(event)
+ filename = self._event_filename(event, atype)
+ mxc_url = getattr(event, "url", None)
+ fail = _ATTACH_FAILED.format(filename)
+
+ if not isinstance(mxc_url, str) or not mxc_url.startswith("mxc://"):
+ return None, fail
+
+ limit_bytes = await self._effective_media_limit_bytes()
+ declared = self._event_declared_size_bytes(event)
+ if declared is None or declared > limit_bytes:
+ return None, _ATTACH_TOO_LARGE.format(filename)
+
+ try:
+ async with self._media_download_semaphore:
+ downloaded = await self._download_media_bytes(mxc_url, limit_bytes)
+ except _MediaTooLargeError:
+ return None, _ATTACH_TOO_LARGE.format(filename)
+ if downloaded is None:
+ return None, fail
+
+ encrypted = self._is_encrypted_media_event(event)
+ data = downloaded
+ if encrypted:
+ if (data := self._decrypt_media_bytes(event, downloaded)) is None:
+ return None, fail
+
+ if len(data) > limit_bytes:
+ return None, _ATTACH_TOO_LARGE.format(filename)
+
+ path = self._build_attachment_path(event, atype, filename, mime)
+ try:
+ path.write_bytes(data)
+ except OSError:
+ return None, fail
+
+ attachment = {
+ "type": atype, "mime": mime, "filename": filename,
+ "event_id": str(getattr(event, "event_id", "") or ""),
+ "encrypted": encrypted, "size_bytes": len(data),
+ "path": str(path), "mxc_url": mxc_url,
+ }
+ return attachment, _ATTACH_MARKER.format(path)
+
+ def _base_metadata(self, room: MatrixRoom, event: RoomMessage) -> dict[str, Any]:
+ """Build common metadata for text and media handlers."""
+ meta: dict[str, Any] = {"room": getattr(room, "display_name", room.room_id)}
+ if isinstance(eid := getattr(event, "event_id", None), str) and eid:
+ meta["event_id"] = eid
+ if thread := self._thread_metadata(event):
+ meta.update(thread)
+ return meta
+
+ async def _on_message(self, room: MatrixRoom, event: RoomMessageText) -> None:
+ if (
+ event.sender == self.config.user_id
+ or self._is_pre_startup_event(event)
+ or not self._should_process_message(room, event)
+ ):
+ return
+ await self._start_typing_keepalive(room.room_id)
+ try:
+ await self._handle_message(
+ sender_id=event.sender, chat_id=room.room_id,
+ content=event.body, metadata=self._base_metadata(room, event),
+ is_dm=self._is_direct_room(room),
+ )
+ except Exception:
+ await self._stop_typing_keepalive(room.room_id, clear_typing=True)
+ raise
+
+ async def _on_media_message(self, room: MatrixRoom, event: MatrixMediaEvent) -> None:
+ if (
+ event.sender == self.config.user_id
+ or self._is_pre_startup_event(event)
+ or not self._should_process_message(room, event)
+ ):
+ return
+ attachment, marker = await self._fetch_media_attachment(room, event)
+ parts: list[str] = []
+ if isinstance(body := getattr(event, "body", None), str) and body.strip():
+ parts.append(body.strip())
+
+ if attachment and attachment.get("type") == "audio":
+ transcription = await self.transcribe_audio(attachment["path"])
+ if transcription:
+ parts.append(f"[transcription: {transcription}]")
+ else:
+ parts.append(marker)
+ elif marker:
+ parts.append(marker)
+
+ await self._start_typing_keepalive(room.room_id)
+ try:
+ meta = self._base_metadata(room, event)
+ meta["attachments"] = []
+ if attachment:
+ meta["attachments"] = [attachment]
+ await self._handle_message(
+ sender_id=event.sender, chat_id=room.room_id,
+ content="\n".join(parts),
+ media=[attachment["path"]] if attachment else [],
+ metadata=meta,
+ is_dm=self._is_direct_room(room),
+ )
+ except Exception:
+ await self._stop_typing_keepalive(room.room_id, clear_typing=True)
+ raise
diff --git a/agent/src/channels/mochat.py b/agent/src/channels/mochat.py
new file mode 100644
index 000000000..e178a9ffb
--- /dev/null
+++ b/agent/src/channels/mochat.py
@@ -0,0 +1,943 @@
+"""Mochat channel implementation using Socket.IO with HTTP polling fallback."""
+
+from __future__ import annotations
+
+import asyncio
+import json
+from collections import deque
+from contextlib import suppress
+from dataclasses import dataclass, field
+from datetime import datetime
+from typing import Any
+
+import httpx
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_runtime_subdir
+from pydantic import BaseModel
+
+try:
+ import socketio
+ SOCKETIO_AVAILABLE = True
+except ImportError:
+ socketio = None
+ SOCKETIO_AVAILABLE = False
+
+try:
+ import msgpack # noqa: F401
+ MSGPACK_AVAILABLE = True
+except ImportError:
+ MSGPACK_AVAILABLE = False
+
+MAX_SEEN_MESSAGE_IDS = 2000
+CURSOR_SAVE_DEBOUNCE_S = 0.5
+
+
+# ---------------------------------------------------------------------------
+# Data classes
+# ---------------------------------------------------------------------------
+
+@dataclass
+class MochatBufferedEntry:
+ """Buffered inbound entry for delayed dispatch."""
+ raw_body: str
+ author: str
+ sender_name: str = ""
+ sender_username: str = ""
+ timestamp: int | None = None
+ message_id: str = ""
+ group_id: str = ""
+
+
+@dataclass
+class DelayState:
+ """Per-target delayed message state."""
+ entries: list[MochatBufferedEntry] = field(default_factory=list)
+ lock: asyncio.Lock = field(default_factory=asyncio.Lock)
+ timer: asyncio.Task | None = None
+
+
+@dataclass
+class MochatTarget:
+ """Outbound target resolution result."""
+ id: str
+ is_panel: bool
+
+
+# ---------------------------------------------------------------------------
+# Pure helpers
+# ---------------------------------------------------------------------------
+
+def _safe_dict(value: Any) -> dict:
+ """Return *value* if it's a dict, else empty dict."""
+ return value if isinstance(value, dict) else {}
+
+
+def _str_field(src: dict, *keys: str) -> str:
+ """Return the first non-empty str value found for *keys*, stripped."""
+ for k in keys:
+ v = src.get(k)
+ if isinstance(v, str) and v.strip():
+ return v.strip()
+ return ""
+
+
+def _make_synthetic_event(
+ message_id: str, author: str, content: Any,
+ meta: Any, group_id: str, converse_id: str,
+ timestamp: Any = None, *, author_info: Any = None,
+) -> dict[str, Any]:
+ """Build a synthetic ``message.add`` event dict."""
+ payload: dict[str, Any] = {
+ "messageId": message_id, "author": author,
+ "content": content, "meta": _safe_dict(meta),
+ "groupId": group_id, "converseId": converse_id,
+ }
+ if author_info is not None:
+ payload["authorInfo"] = _safe_dict(author_info)
+ return {
+ "type": "message.add",
+ "timestamp": timestamp or datetime.utcnow().isoformat(),
+ "payload": payload,
+ }
+
+
+def normalize_mochat_content(content: Any) -> str:
+ """Normalize content payload to text."""
+ if isinstance(content, str):
+ return content.strip()
+ if content is None:
+ return ""
+ try:
+ return json.dumps(content, ensure_ascii=False)
+ except TypeError:
+ return str(content)
+
+
+def resolve_mochat_target(raw: str) -> MochatTarget:
+ """Resolve id and target kind from user-provided target string."""
+ trimmed = (raw or "").strip()
+ if not trimmed:
+ return MochatTarget(id="", is_panel=False)
+
+ lowered = trimmed.lower()
+ cleaned, forced_panel = trimmed, False
+ for prefix in ("mochat:", "group:", "channel:", "panel:"):
+ if lowered.startswith(prefix):
+ cleaned = trimmed[len(prefix):].strip()
+ forced_panel = prefix in {"group:", "channel:", "panel:"}
+ break
+
+ if not cleaned:
+ return MochatTarget(id="", is_panel=False)
+ return MochatTarget(id=cleaned, is_panel=forced_panel or not cleaned.startswith("session_"))
+
+
+def extract_mention_ids(value: Any) -> list[str]:
+ """Extract mention ids from heterogeneous mention payload."""
+ if not isinstance(value, list):
+ return []
+ ids: list[str] = []
+ for item in value:
+ if isinstance(item, str):
+ if item.strip():
+ ids.append(item.strip())
+ elif isinstance(item, dict):
+ for key in ("id", "userId", "_id"):
+ candidate = item.get(key)
+ if isinstance(candidate, str) and candidate.strip():
+ ids.append(candidate.strip())
+ break
+ return ids
+
+
+def resolve_was_mentioned(payload: dict[str, Any], agent_user_id: str) -> bool:
+ """Resolve mention state from payload metadata and text fallback."""
+ meta = payload.get("meta")
+ if isinstance(meta, dict):
+ if meta.get("mentioned") is True or meta.get("wasMentioned") is True:
+ return True
+ for f in ("mentions", "mentionIds", "mentionedUserIds", "mentionedUsers"):
+ if agent_user_id and agent_user_id in extract_mention_ids(meta.get(f)):
+ return True
+ if not agent_user_id:
+ return False
+ content = payload.get("content")
+ if not isinstance(content, str) or not content:
+ return False
+ return f"<@{agent_user_id}>" in content or f"@{agent_user_id}" in content
+
+
+def resolve_require_mention(config: MochatConfig, session_id: str, group_id: str) -> bool:
+ """Resolve mention requirement for group/panel conversations."""
+ groups = config.groups or {}
+ for key in (group_id, session_id, "*"):
+ if key and key in groups:
+ return bool(groups[key].require_mention)
+ return bool(config.mention.require_in_groups)
+
+
+def build_buffered_body(entries: list[MochatBufferedEntry], is_group: bool) -> str:
+ """Build text body from one or more buffered entries."""
+ if not entries:
+ return ""
+ if len(entries) == 1:
+ return entries[0].raw_body
+ lines: list[str] = []
+ for entry in entries:
+ if not entry.raw_body:
+ continue
+ if is_group:
+ label = entry.sender_name.strip() or entry.sender_username.strip() or entry.author
+ if label:
+ lines.append(f"{label}: {entry.raw_body}")
+ continue
+ lines.append(entry.raw_body)
+ return "\n".join(lines).strip()
+
+
+def parse_timestamp(value: Any) -> int | None:
+ """Parse event timestamp to epoch milliseconds."""
+ if not isinstance(value, str) or not value.strip():
+ return None
+ try:
+ return int(datetime.fromisoformat(value.replace("Z", "+00:00")).timestamp() * 1000)
+ except ValueError:
+ return None
+
+
+# ---------------------------------------------------------------------------
+# Config classes
+# ---------------------------------------------------------------------------
+
+class MochatMentionConfig(BaseModel):
+ """Mochat mention behavior configuration."""
+
+ require_in_groups: bool = False
+
+
+class MochatGroupRule(BaseModel):
+ """Mochat per-group mention requirement."""
+
+ require_mention: bool = False
+
+
+class MochatConfig(BaseModel):
+ """Mochat channel configuration."""
+
+ enabled: bool = False
+ base_url: str = "https://mochat.io"
+ socket_url: str = ""
+ socket_path: str = "/socket.io"
+ socket_disable_msgpack: bool = False
+ socket_reconnect_delay_ms: int = 1000
+ socket_max_reconnect_delay_ms: int = 10000
+ socket_connect_timeout_ms: int = 10000
+ refresh_interval_ms: int = 30000
+ watch_timeout_ms: int = 25000
+ watch_limit: int = 100
+ retry_delay_ms: int = 500
+ max_retry_attempts: int = 0
+ claw_token: str = ""
+ agent_user_id: str = ""
+ sessions: list[str] = Field(default_factory=list)
+ panels: list[str] = Field(default_factory=list)
+ allow_from: list[str] = Field(default_factory=list)
+ mention: MochatMentionConfig = Field(default_factory=MochatMentionConfig)
+ groups: dict[str, MochatGroupRule] = Field(default_factory=dict)
+ reply_delay_mode: str = "non-mention"
+ reply_delay_ms: int = 120000
+
+
+# ---------------------------------------------------------------------------
+# Channel
+# ---------------------------------------------------------------------------
+
+class MochatChannel(BaseChannel):
+ """Mochat channel using socket.io with fallback polling workers."""
+
+ name = "mochat"
+ display_name = "Mochat"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return MochatConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = MochatConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: MochatConfig = config
+ self._http: httpx.AsyncClient | None = None
+ self._socket: Any = None
+ self._ws_connected = self._ws_ready = False
+
+ self._state_dir = get_runtime_subdir("mochat")
+ self._cursor_path = self._state_dir / "session_cursors.json"
+ self._session_cursor: dict[str, int] = {}
+ self._cursor_save_task: asyncio.Task | None = None
+
+ self._session_set: set[str] = set()
+ self._panel_set: set[str] = set()
+ self._auto_discover_sessions = self._auto_discover_panels = False
+
+ self._cold_sessions: set[str] = set()
+ self._session_by_converse: dict[str, str] = {}
+
+ self._seen_set: dict[str, set[str]] = {}
+ self._seen_queue: dict[str, deque[str]] = {}
+ self._delay_states: dict[str, DelayState] = {}
+
+ self._fallback_mode = False
+ self._session_fallback_tasks: dict[str, asyncio.Task] = {}
+ self._panel_fallback_tasks: dict[str, asyncio.Task] = {}
+ self._refresh_task: asyncio.Task | None = None
+ self._target_locks: dict[str, asyncio.Lock] = {}
+
+ # ---- lifecycle ---------------------------------------------------------
+
+ async def start(self) -> None:
+ """Start Mochat channel workers and websocket connection."""
+ if not self.config.claw_token:
+ self.logger.error("claw_token not configured")
+ return
+
+ self._running = True
+ self._http = httpx.AsyncClient(timeout=30.0)
+ self._state_dir.mkdir(parents=True, exist_ok=True)
+ await self._load_session_cursors()
+ self._seed_targets_from_config()
+ await self._refresh_targets(subscribe_new=False)
+
+ if not await self._start_socket_client():
+ await self._ensure_fallback_workers()
+
+ self._refresh_task = asyncio.create_task(self._refresh_loop())
+ while self._running:
+ await asyncio.sleep(1)
+
+ async def stop(self) -> None:
+ """Stop all workers and clean up resources."""
+ self._running = False
+ if self._refresh_task:
+ self._refresh_task.cancel()
+ self._refresh_task = None
+
+ await self._stop_fallback_workers()
+ await self._cancel_delay_timers()
+
+ if self._socket:
+ with suppress(Exception):
+ await self._socket.disconnect()
+ self._socket = None
+
+ if self._cursor_save_task:
+ self._cursor_save_task.cancel()
+ self._cursor_save_task = None
+ await self._save_session_cursors()
+
+ if self._http:
+ await self._http.aclose()
+ self._http = None
+ self._ws_connected = self._ws_ready = False
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send outbound message to session or panel."""
+ if not self.config.claw_token:
+ self.logger.warning("claw_token missing, skip send")
+ return
+
+ parts = ([msg.content.strip()] if msg.content and msg.content.strip() else [])
+ if msg.media:
+ parts.extend(m for m in msg.media if isinstance(m, str) and m.strip())
+ content = "\n".join(parts).strip()
+ if not content:
+ return
+
+ target = resolve_mochat_target(msg.chat_id)
+ if not target.id:
+ self.logger.warning("outbound target is empty")
+ return
+
+ is_panel = (target.is_panel or target.id in self._panel_set) and not target.id.startswith("session_")
+ try:
+ if is_panel:
+ await self._api_send("/api/claw/groups/panels/send", "panelId", target.id,
+ content, msg.reply_to, self._read_group_id(msg.metadata))
+ else:
+ await self._api_send("/api/claw/sessions/send", "sessionId", target.id,
+ content, msg.reply_to)
+ except Exception:
+ self.logger.exception("Failed to send message")
+ raise
+
+ # ---- config / init helpers ---------------------------------------------
+
+ def _seed_targets_from_config(self) -> None:
+ sessions, self._auto_discover_sessions = self._normalize_id_list(self.config.sessions)
+ panels, self._auto_discover_panels = self._normalize_id_list(self.config.panels)
+ self._session_set.update(sessions)
+ self._panel_set.update(panels)
+ for sid in sessions:
+ if sid not in self._session_cursor:
+ self._cold_sessions.add(sid)
+
+ @staticmethod
+ def _normalize_id_list(values: list[str]) -> tuple[list[str], bool]:
+ cleaned = [str(v).strip() for v in values if str(v).strip()]
+ return sorted({v for v in cleaned if v != "*"}), "*" in cleaned
+
+ # ---- websocket ---------------------------------------------------------
+
+ async def _start_socket_client(self) -> bool:
+ if not SOCKETIO_AVAILABLE:
+ self.logger.warning("python-socketio not installed, using polling fallback")
+ return False
+
+ serializer = "default"
+ if not self.config.socket_disable_msgpack:
+ if MSGPACK_AVAILABLE:
+ serializer = "msgpack"
+ else:
+ self.logger.warning("msgpack not installed but socket_disable_msgpack=false; using JSON")
+
+ client = socketio.AsyncClient(
+ reconnection=True,
+ reconnection_attempts=self.config.max_retry_attempts or None,
+ reconnection_delay=max(0.1, self.config.socket_reconnect_delay_ms / 1000.0),
+ reconnection_delay_max=max(0.1, self.config.socket_max_reconnect_delay_ms / 1000.0),
+ logger=False, engineio_logger=False, serializer=serializer,
+ )
+
+ @client.event
+ async def connect() -> None:
+ self._ws_connected, self._ws_ready = True, False
+ self.logger.info("websocket connected")
+ subscribed = await self._subscribe_all()
+ self._ws_ready = subscribed
+ await (self._stop_fallback_workers() if subscribed else self._ensure_fallback_workers())
+
+ @client.event
+ async def disconnect() -> None:
+ if not self._running:
+ return
+ self._ws_connected = self._ws_ready = False
+ self.logger.warning("websocket disconnected")
+ await self._ensure_fallback_workers()
+
+ @client.event
+ async def connect_error(data: Any) -> None:
+ self.logger.error("websocket connect error: {}", data)
+
+ @client.on("claw.session.events")
+ async def on_session_events(payload: dict[str, Any]) -> None:
+ await self._handle_watch_payload(payload, "session")
+
+ @client.on("claw.panel.events")
+ async def on_panel_events(payload: dict[str, Any]) -> None:
+ await self._handle_watch_payload(payload, "panel")
+
+ for ev in ("notify:chat.inbox.append", "notify:chat.message.add",
+ "notify:chat.message.update", "notify:chat.message.recall",
+ "notify:chat.message.delete"):
+ client.on(ev, self._build_notify_handler(ev))
+
+ socket_url = (self.config.socket_url or self.config.base_url).strip().rstrip("/")
+ socket_path = (self.config.socket_path or "/socket.io").strip().lstrip("/")
+
+ try:
+ self._socket = client
+ await client.connect(
+ socket_url, transports=["websocket"], socketio_path=socket_path,
+ auth={"token": self.config.claw_token},
+ wait_timeout=max(1.0, self.config.socket_connect_timeout_ms / 1000.0),
+ )
+ return True
+ except Exception:
+ self.logger.exception("Failed to connect websocket")
+ with suppress(Exception):
+ await client.disconnect()
+ self._socket = None
+ return False
+
+ def _build_notify_handler(self, event_name: str):
+ async def handler(payload: Any) -> None:
+ if event_name == "notify:chat.inbox.append":
+ await self._handle_notify_inbox_append(payload)
+ elif event_name.startswith("notify:chat.message."):
+ await self._handle_notify_chat_message(payload)
+ return handler
+
+ # ---- subscribe ---------------------------------------------------------
+
+ async def _subscribe_all(self) -> bool:
+ ok = await self._subscribe_sessions(sorted(self._session_set))
+ ok = await self._subscribe_panels(sorted(self._panel_set)) and ok
+ if self._auto_discover_sessions or self._auto_discover_panels:
+ await self._refresh_targets(subscribe_new=True)
+ return ok
+
+ async def _subscribe_sessions(self, session_ids: list[str]) -> bool:
+ if not session_ids:
+ return True
+ for sid in session_ids:
+ if sid not in self._session_cursor:
+ self._cold_sessions.add(sid)
+
+ ack = await self._socket_call("com.claw.im.subscribeSessions", {
+ "sessionIds": session_ids, "cursors": self._session_cursor,
+ "limit": self.config.watch_limit,
+ })
+ if not ack.get("result"):
+ self.logger.error("subscribeSessions failed: {}", ack.get('message', 'unknown error'))
+ return False
+
+ data = ack.get("data")
+ items: list[dict[str, Any]] = []
+ if isinstance(data, list):
+ items = [i for i in data if isinstance(i, dict)]
+ elif isinstance(data, dict):
+ sessions = data.get("sessions")
+ if isinstance(sessions, list):
+ items = [i for i in sessions if isinstance(i, dict)]
+ elif "sessionId" in data:
+ items = [data]
+ for p in items:
+ await self._handle_watch_payload(p, "session")
+ return True
+
+ async def _subscribe_panels(self, panel_ids: list[str]) -> bool:
+ if not self._auto_discover_panels and not panel_ids:
+ return True
+ ack = await self._socket_call("com.claw.im.subscribePanels", {"panelIds": panel_ids})
+ if not ack.get("result"):
+ self.logger.error("subscribePanels failed: {}", ack.get('message', 'unknown error'))
+ return False
+ return True
+
+ async def _socket_call(self, event_name: str, payload: dict[str, Any]) -> dict[str, Any]:
+ if not self._socket:
+ return {"result": False, "message": "socket not connected"}
+ try:
+ raw = await self._socket.call(event_name, payload, timeout=10)
+ except Exception as e:
+ return {"result": False, "message": str(e)}
+ return raw if isinstance(raw, dict) else {"result": True, "data": raw}
+
+ # ---- refresh / discovery -----------------------------------------------
+
+ async def _refresh_loop(self) -> None:
+ interval_s = max(1.0, self.config.refresh_interval_ms / 1000.0)
+ while self._running:
+ await asyncio.sleep(interval_s)
+ try:
+ await self._refresh_targets(subscribe_new=self._ws_ready)
+ except Exception as e:
+ self.logger.warning("refresh failed: {}", e)
+ if self._fallback_mode:
+ await self._ensure_fallback_workers()
+
+ async def _refresh_targets(self, subscribe_new: bool) -> None:
+ if self._auto_discover_sessions:
+ await self._refresh_sessions_directory(subscribe_new)
+ if self._auto_discover_panels:
+ await self._refresh_panels(subscribe_new)
+
+ async def _refresh_sessions_directory(self, subscribe_new: bool) -> None:
+ try:
+ response = await self._post_json("/api/claw/sessions/list", {})
+ except Exception as e:
+ self.logger.warning("listSessions failed: {}", e)
+ return
+
+ sessions = response.get("sessions")
+ if not isinstance(sessions, list):
+ return
+
+ new_ids: list[str] = []
+ for s in sessions:
+ if not isinstance(s, dict):
+ continue
+ sid = _str_field(s, "sessionId")
+ if not sid:
+ continue
+ if sid not in self._session_set:
+ self._session_set.add(sid)
+ new_ids.append(sid)
+ if sid not in self._session_cursor:
+ self._cold_sessions.add(sid)
+ cid = _str_field(s, "converseId")
+ if cid:
+ self._session_by_converse[cid] = sid
+
+ if not new_ids:
+ return
+ if self._ws_ready and subscribe_new:
+ await self._subscribe_sessions(new_ids)
+ if self._fallback_mode:
+ await self._ensure_fallback_workers()
+
+ async def _refresh_panels(self, subscribe_new: bool) -> None:
+ try:
+ response = await self._post_json("/api/claw/groups/get", {})
+ except Exception as e:
+ self.logger.warning("getWorkspaceGroup failed: {}", e)
+ return
+
+ raw_panels = response.get("panels")
+ if not isinstance(raw_panels, list):
+ return
+
+ new_ids: list[str] = []
+ for p in raw_panels:
+ if not isinstance(p, dict):
+ continue
+ pt = p.get("type")
+ if isinstance(pt, int) and pt != 0:
+ continue
+ pid = _str_field(p, "id", "_id")
+ if pid and pid not in self._panel_set:
+ self._panel_set.add(pid)
+ new_ids.append(pid)
+
+ if not new_ids:
+ return
+ if self._ws_ready and subscribe_new:
+ await self._subscribe_panels(new_ids)
+ if self._fallback_mode:
+ await self._ensure_fallback_workers()
+
+ # ---- fallback workers --------------------------------------------------
+
+ async def _ensure_fallback_workers(self) -> None:
+ if not self._running:
+ return
+ self._fallback_mode = True
+ for sid in sorted(self._session_set):
+ t = self._session_fallback_tasks.get(sid)
+ if not t or t.done():
+ self._session_fallback_tasks[sid] = asyncio.create_task(self._session_watch_worker(sid))
+ for pid in sorted(self._panel_set):
+ t = self._panel_fallback_tasks.get(pid)
+ if not t or t.done():
+ self._panel_fallback_tasks[pid] = asyncio.create_task(self._panel_poll_worker(pid))
+
+ async def _stop_fallback_workers(self) -> None:
+ self._fallback_mode = False
+ tasks = [*self._session_fallback_tasks.values(), *self._panel_fallback_tasks.values()]
+ for t in tasks:
+ t.cancel()
+ if tasks:
+ await asyncio.gather(*tasks, return_exceptions=True)
+ self._session_fallback_tasks.clear()
+ self._panel_fallback_tasks.clear()
+
+ async def _session_watch_worker(self, session_id: str) -> None:
+ while self._running and self._fallback_mode:
+ try:
+ payload = await self._post_json("/api/claw/sessions/watch", {
+ "sessionId": session_id, "cursor": self._session_cursor.get(session_id, 0),
+ "timeoutMs": self.config.watch_timeout_ms, "limit": self.config.watch_limit,
+ })
+ await self._handle_watch_payload(payload, "session")
+ except asyncio.CancelledError:
+ break
+ except Exception as e:
+ self.logger.warning("watch fallback error ({}): {}", session_id, e)
+ await asyncio.sleep(max(0.1, self.config.retry_delay_ms / 1000.0))
+
+ async def _panel_poll_worker(self, panel_id: str) -> None:
+ sleep_s = max(1.0, self.config.refresh_interval_ms / 1000.0)
+ while self._running and self._fallback_mode:
+ try:
+ resp = await self._post_json("/api/claw/groups/panels/messages", {
+ "panelId": panel_id, "limit": min(100, max(1, self.config.watch_limit)),
+ })
+ msgs = resp.get("messages")
+ if isinstance(msgs, list):
+ for m in reversed(msgs):
+ if not isinstance(m, dict):
+ continue
+ evt = _make_synthetic_event(
+ message_id=str(m.get("messageId") or ""),
+ author=str(m.get("author") or ""),
+ content=m.get("content"),
+ meta=m.get("meta"), group_id=str(resp.get("groupId") or ""),
+ converse_id=panel_id, timestamp=m.get("createdAt"),
+ author_info=m.get("authorInfo"),
+ )
+ await self._process_inbound_event(panel_id, evt, "panel")
+ except asyncio.CancelledError:
+ break
+ except Exception as e:
+ self.logger.warning("panel polling error ({}): {}", panel_id, e)
+ await asyncio.sleep(sleep_s)
+
+ # ---- inbound event processing ------------------------------------------
+
+ async def _handle_watch_payload(self, payload: dict[str, Any], target_kind: str) -> None:
+ if not isinstance(payload, dict):
+ return
+ target_id = _str_field(payload, "sessionId")
+ if not target_id:
+ return
+
+ lock = self._target_locks.setdefault(f"{target_kind}:{target_id}", asyncio.Lock())
+ async with lock:
+ prev = self._session_cursor.get(target_id, 0) if target_kind == "session" else 0
+ pc = payload.get("cursor")
+ if target_kind == "session" and isinstance(pc, int) and pc >= 0:
+ self._mark_session_cursor(target_id, pc)
+
+ raw_events = payload.get("events")
+ if not isinstance(raw_events, list):
+ return
+ if target_kind == "session" and target_id in self._cold_sessions:
+ self._cold_sessions.discard(target_id)
+ return
+
+ for event in raw_events:
+ if not isinstance(event, dict):
+ continue
+ seq = event.get("seq")
+ if target_kind == "session" and isinstance(seq, int) and seq > self._session_cursor.get(target_id, prev):
+ self._mark_session_cursor(target_id, seq)
+ if event.get("type") == "message.add":
+ await self._process_inbound_event(target_id, event, target_kind)
+
+ async def _process_inbound_event(self, target_id: str, event: dict[str, Any], target_kind: str) -> None:
+ payload = event.get("payload")
+ if not isinstance(payload, dict):
+ return
+
+ author = _str_field(payload, "author")
+ if not author or (self.config.agent_user_id and author == self.config.agent_user_id):
+ return
+ if not self.is_allowed(author):
+ return
+
+ message_id = _str_field(payload, "messageId")
+ seen_key = f"{target_kind}:{target_id}"
+ if message_id and self._remember_message_id(seen_key, message_id):
+ return
+
+ raw_body = normalize_mochat_content(payload.get("content")) or "[empty message]"
+ ai = _safe_dict(payload.get("authorInfo"))
+ sender_name = _str_field(ai, "nickname", "email")
+ sender_username = _str_field(ai, "agentId")
+
+ group_id = _str_field(payload, "groupId")
+ is_group = bool(group_id)
+ was_mentioned = resolve_was_mentioned(payload, self.config.agent_user_id)
+ require_mention = target_kind == "panel" and is_group and resolve_require_mention(self.config, target_id, group_id)
+ use_delay = target_kind == "panel" and self.config.reply_delay_mode == "non-mention"
+
+ if require_mention and not was_mentioned and not use_delay:
+ return
+
+ entry = MochatBufferedEntry(
+ raw_body=raw_body, author=author, sender_name=sender_name,
+ sender_username=sender_username, timestamp=parse_timestamp(event.get("timestamp")),
+ message_id=message_id, group_id=group_id,
+ )
+
+ if use_delay:
+ delay_key = seen_key
+ if was_mentioned:
+ await self._flush_delayed_entries(delay_key, target_id, target_kind, "mention", entry)
+ else:
+ await self._enqueue_delayed_entry(delay_key, target_id, target_kind, entry)
+ return
+
+ await self._dispatch_entries(target_id, target_kind, [entry], was_mentioned)
+
+ # ---- dedup / buffering -------------------------------------------------
+
+ def _remember_message_id(self, key: str, message_id: str) -> bool:
+ seen_set = self._seen_set.setdefault(key, set())
+ seen_queue = self._seen_queue.setdefault(key, deque())
+ if message_id in seen_set:
+ return True
+ seen_set.add(message_id)
+ seen_queue.append(message_id)
+ while len(seen_queue) > MAX_SEEN_MESSAGE_IDS:
+ seen_set.discard(seen_queue.popleft())
+ return False
+
+ async def _enqueue_delayed_entry(self, key: str, target_id: str, target_kind: str, entry: MochatBufferedEntry) -> None:
+ state = self._delay_states.setdefault(key, DelayState())
+ async with state.lock:
+ state.entries.append(entry)
+ if state.timer:
+ state.timer.cancel()
+ state.timer = asyncio.create_task(self._delay_flush_after(key, target_id, target_kind))
+
+ async def _delay_flush_after(self, key: str, target_id: str, target_kind: str) -> None:
+ await asyncio.sleep(max(0, self.config.reply_delay_ms) / 1000.0)
+ await self._flush_delayed_entries(key, target_id, target_kind, "timer", None)
+
+ async def _flush_delayed_entries(self, key: str, target_id: str, target_kind: str, reason: str, entry: MochatBufferedEntry | None) -> None:
+ state = self._delay_states.setdefault(key, DelayState())
+ async with state.lock:
+ if entry:
+ state.entries.append(entry)
+ current = asyncio.current_task()
+ if state.timer and state.timer is not current:
+ state.timer.cancel()
+ state.timer = None
+ entries = state.entries[:]
+ state.entries.clear()
+ if entries:
+ await self._dispatch_entries(target_id, target_kind, entries, reason == "mention")
+
+ async def _dispatch_entries(self, target_id: str, target_kind: str, entries: list[MochatBufferedEntry], was_mentioned: bool) -> None:
+ if not entries:
+ return
+ last = entries[-1]
+ is_group = bool(last.group_id)
+ body = build_buffered_body(entries, is_group) or "[empty message]"
+ await self._handle_message(
+ sender_id=last.author, chat_id=target_id, content=body,
+ metadata={
+ "message_id": last.message_id, "timestamp": last.timestamp,
+ "is_group": is_group, "group_id": last.group_id,
+ "sender_name": last.sender_name, "sender_username": last.sender_username,
+ "target_kind": target_kind, "was_mentioned": was_mentioned,
+ "buffered_count": len(entries),
+ },
+ )
+
+ async def _cancel_delay_timers(self) -> None:
+ for state in self._delay_states.values():
+ if state.timer:
+ state.timer.cancel()
+ self._delay_states.clear()
+
+ # ---- notify handlers ---------------------------------------------------
+
+ async def _handle_notify_chat_message(self, payload: Any) -> None:
+ if not isinstance(payload, dict):
+ return
+ group_id = _str_field(payload, "groupId")
+ panel_id = _str_field(payload, "converseId", "panelId")
+ if not group_id or not panel_id:
+ return
+ if self._panel_set and panel_id not in self._panel_set:
+ return
+
+ evt = _make_synthetic_event(
+ message_id=str(payload.get("_id") or payload.get("messageId") or ""),
+ author=str(payload.get("author") or ""),
+ content=payload.get("content"), meta=payload.get("meta"),
+ group_id=group_id, converse_id=panel_id,
+ timestamp=payload.get("createdAt"), author_info=payload.get("authorInfo"),
+ )
+ await self._process_inbound_event(panel_id, evt, "panel")
+
+ async def _handle_notify_inbox_append(self, payload: Any) -> None:
+ if not isinstance(payload, dict) or payload.get("type") != "message":
+ return
+ detail = payload.get("payload")
+ if not isinstance(detail, dict):
+ return
+ if _str_field(detail, "groupId"):
+ return
+ converse_id = _str_field(detail, "converseId")
+ if not converse_id:
+ return
+
+ session_id = self._session_by_converse.get(converse_id)
+ if not session_id:
+ await self._refresh_sessions_directory(self._ws_ready)
+ session_id = self._session_by_converse.get(converse_id)
+ if not session_id:
+ return
+
+ evt = _make_synthetic_event(
+ message_id=str(detail.get("messageId") or payload.get("_id") or ""),
+ author=str(detail.get("messageAuthor") or ""),
+ content=str(detail.get("messagePlainContent") or detail.get("messageSnippet") or ""),
+ meta={"source": "notify:chat.inbox.append", "converseId": converse_id},
+ group_id="", converse_id=converse_id, timestamp=payload.get("createdAt"),
+ )
+ await self._process_inbound_event(session_id, evt, "session")
+
+ # ---- cursor persistence ------------------------------------------------
+
+ def _mark_session_cursor(self, session_id: str, cursor: int) -> None:
+ if cursor < 0 or cursor < self._session_cursor.get(session_id, 0):
+ return
+ self._session_cursor[session_id] = cursor
+ if not self._cursor_save_task or self._cursor_save_task.done():
+ self._cursor_save_task = asyncio.create_task(self._save_cursor_debounced())
+
+ async def _save_cursor_debounced(self) -> None:
+ await asyncio.sleep(CURSOR_SAVE_DEBOUNCE_S)
+ await self._save_session_cursors()
+
+ async def _load_session_cursors(self) -> None:
+ if not self._cursor_path.exists():
+ return
+ try:
+ data = json.loads(self._cursor_path.read_text("utf-8"))
+ except Exception as e:
+ self.logger.warning("Failed to read cursor file: {}", e)
+ return
+ cursors = data.get("cursors") if isinstance(data, dict) else None
+ if isinstance(cursors, dict):
+ for sid, cur in cursors.items():
+ if isinstance(sid, str) and isinstance(cur, int) and cur >= 0:
+ self._session_cursor[sid] = cur
+
+ async def _save_session_cursors(self) -> None:
+ try:
+ self._state_dir.mkdir(parents=True, exist_ok=True)
+ self._cursor_path.write_text(json.dumps({
+ "schemaVersion": 1, "updatedAt": datetime.utcnow().isoformat(),
+ "cursors": self._session_cursor,
+ }, ensure_ascii=False, indent=2) + "\n", "utf-8")
+ except Exception as e:
+ self.logger.warning("Failed to save cursor file: {}", e)
+
+ # ---- HTTP helpers ------------------------------------------------------
+
+ async def _post_json(self, path: str, payload: dict[str, Any]) -> dict[str, Any]:
+ if not self._http:
+ raise RuntimeError("Mochat HTTP client not initialized")
+ url = f"{self.config.base_url.strip().rstrip('/')}{path}"
+ response = await self._http.post(url, headers={
+ "Content-Type": "application/json", "X-Claw-Token": self.config.claw_token,
+ }, json=payload)
+ if not response.is_success:
+ raise RuntimeError(f"Mochat HTTP {response.status_code}: {response.text[:200]}")
+ try:
+ parsed = response.json()
+ except Exception:
+ parsed = response.text
+ if isinstance(parsed, dict) and isinstance(parsed.get("code"), int):
+ if parsed["code"] != 200:
+ msg = str(parsed.get("message") or parsed.get("name") or "request failed")
+ raise RuntimeError(f"Mochat API error: {msg} (code={parsed['code']})")
+ data = parsed.get("data")
+ return data if isinstance(data, dict) else {}
+ return parsed if isinstance(parsed, dict) else {}
+
+ async def _api_send(self, path: str, id_key: str, id_val: str,
+ content: str, reply_to: str | None, group_id: str | None = None) -> dict[str, Any]:
+ """Unified send helper for session and panel messages."""
+ body: dict[str, Any] = {id_key: id_val, "content": content}
+ if reply_to:
+ body["replyTo"] = reply_to
+ if group_id:
+ body["groupId"] = group_id
+ return await self._post_json(path, body)
+
+ @staticmethod
+ def _read_group_id(metadata: dict[str, Any]) -> str | None:
+ if not isinstance(metadata, dict):
+ return None
+ value = metadata.get("group_id") or metadata.get("groupId")
+ return value.strip() if isinstance(value, str) and value.strip() else None
diff --git a/agent/src/channels/msteams.py b/agent/src/channels/msteams.py
new file mode 100644
index 000000000..ef542a9c1
--- /dev/null
+++ b/agent/src/channels/msteams.py
@@ -0,0 +1,822 @@
+"""Microsoft Teams channel MVP using a tiny built-in HTTP webhook server.
+
+Scope:
+- DM-focused MVP
+- text inbound/outbound
+- conversation reference persistence
+- sender allowlist support
+- optional inbound Bot Framework bearer-token validation
+- no attachments/cards/polls yet
+"""
+
+from __future__ import annotations
+
+import asyncio
+import html
+import importlib.util
+import json
+import os
+import re
+import tempfile
+import threading
+import time
+from contextlib import contextmanager, suppress
+from dataclasses import dataclass
+from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
+from typing import TYPE_CHECKING, Any
+from urllib.parse import urlparse
+
+try: # pragma: no cover - Windows fallback path
+ import fcntl
+except ImportError: # pragma: no cover
+ fcntl = None
+
+import httpx
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.config.paths import get_workspace_path
+from pydantic import BaseModel
+
+MSTEAMS_AVAILABLE = (
+ importlib.util.find_spec("jwt") is not None
+ and importlib.util.find_spec("cryptography") is not None
+)
+
+if TYPE_CHECKING:
+ import jwt
+
+if MSTEAMS_AVAILABLE:
+ import jwt
+
+MSTEAMS_REF_TTL_DAYS = 30
+MSTEAMS_WEBCHAT_HOST = "webchat.botframework.com"
+MSTEAMS_DEFAULT_TRUSTED_SERVICE_URL_HOSTS = [
+ "smba.trafficmanager.net",
+ "smba.infra.gcc.teams.microsoft.com",
+ "smba.infra.gov.teams.microsoft.us",
+ "smba.infra.dod.teams.microsoft.us",
+ "*.botframework.com",
+]
+MSTEAMS_REF_META_FILENAME = "msteams_conversations_meta.json"
+MSTEAMS_REF_LOCK_FILENAME = "msteams_conversations.lock"
+MSTEAMS_REF_TOUCH_INTERVAL_S = 300
+
+
+class MSTeamsConfig(BaseModel):
+ """Microsoft Teams channel configuration."""
+
+ enabled: bool = False
+ app_id: str = ""
+ app_password: str = ""
+ tenant_id: str = ""
+ host: str = "0.0.0.0"
+ port: int = 3978
+ path: str = "/api/messages"
+ allow_from: list[str] = Field(default_factory=list)
+ reply_in_thread: bool = True
+ mention_only_response: str = "Hi — what can I help with?"
+ validate_inbound_auth: bool = True
+ ref_ttl_days: int = Field(default=MSTEAMS_REF_TTL_DAYS, ge=1)
+ prune_web_chat_refs: bool = True
+ prune_non_personal_refs: bool = True
+ ref_touch_interval_s: int = Field(default=MSTEAMS_REF_TOUCH_INTERVAL_S, ge=0)
+ trusted_service_url_hosts: list[str] = Field(
+ default_factory=lambda: MSTEAMS_DEFAULT_TRUSTED_SERVICE_URL_HOSTS.copy()
+ )
+
+
+@dataclass
+class ConversationRef:
+ """Minimal stored conversation reference for replies."""
+
+ service_url: str
+ conversation_id: str
+ bot_id: str | None = None
+ activity_id: str | None = None
+ conversation_type: str | None = None
+ tenant_id: str | None = None
+ updated_at: float | None = None
+
+
+class MSTeamsChannel(BaseChannel):
+ """Microsoft Teams channel (DM-first MVP)."""
+
+ name = "msteams"
+ display_name = "Microsoft Teams"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return MSTeamsConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = MSTeamsConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: MSTeamsConfig = config
+ self._loop: asyncio.AbstractEventLoop | None = None
+ self._server: ThreadingHTTPServer | None = None
+ self._server_thread: threading.Thread | None = None
+ self._http: httpx.AsyncClient | None = None
+ self._token: str | None = None
+ self._token_expires_at: float = 0.0
+ self._botframework_openid_config_url = (
+ "https://login.botframework.com/v1/.well-known/openidconfiguration"
+ )
+ self._botframework_openid_config: dict[str, Any] | None = None
+ self._botframework_openid_config_expires_at: float = 0.0
+ self._botframework_jwks: dict[str, Any] | None = None
+ self._botframework_jwks_expires_at: float = 0.0
+ self._refs_path = get_workspace_path() / "state" / "msteams_conversations.json"
+ self._refs_path.parent.mkdir(parents=True, exist_ok=True)
+ self._refs_meta_path = self._refs_path.parent / MSTEAMS_REF_META_FILENAME
+ self._refs_lock_path = self._refs_path.parent / MSTEAMS_REF_LOCK_FILENAME
+ self._refs_guard = threading.RLock()
+ self._conversation_refs: dict[str, ConversationRef] = self._load_refs()
+ with self._refs_guard:
+ if self._prune_conversation_refs():
+ self._save_refs_locked(prune=True)
+
+ async def start(self) -> None:
+ """Start the Teams webhook listener."""
+ if not MSTEAMS_AVAILABLE:
+ self.logger.error("PyJWT not installed. Run: pip install vibe-trading-ai[msteams]")
+ return
+
+ if not self.config.app_id or not self.config.app_password:
+ self.logger.error("app_id/app_password not configured")
+ return
+
+ if not self.config.validate_inbound_auth:
+ self.logger.warning(
+ "Inbound auth validation was explicitly DISABLED in config. "
+ "Anyone who knows the webhook URL can send messages as any user. "
+ "Only disable this for local development or controlled testing."
+ )
+
+ self._loop = asyncio.get_running_loop()
+ self._http = httpx.AsyncClient(timeout=30.0)
+ self._running = True
+
+ channel = self
+
+ class Handler(BaseHTTPRequestHandler):
+ def do_POST(self) -> None:
+ if self.path != channel.config.path:
+ self.send_response(404)
+ self.end_headers()
+ return
+
+ try:
+ length = int(self.headers.get("Content-Length", "0"))
+ raw = self.rfile.read(length) if length > 0 else b"{}"
+ payload = json.loads(raw.decode("utf-8"))
+ except Exception as e:
+ channel.logger.warning("Invalid request body: {}", e)
+ self.send_response(400)
+ self.end_headers()
+ return
+
+ auth_header = self.headers.get("Authorization", "")
+ if channel.config.validate_inbound_auth:
+ try:
+ fut = asyncio.run_coroutine_threadsafe(
+ channel._validate_inbound_auth(auth_header, payload),
+ channel._loop,
+ )
+ fut.result(timeout=15)
+ except Exception as e:
+ channel.logger.warning("Inbound auth validation failed: {}", e)
+ self.send_response(401)
+ self.send_header("Content-Type", "application/json")
+ self.end_headers()
+ self.wfile.write(b'{"error":"unauthorized"}')
+ return
+ try:
+ fut = asyncio.run_coroutine_threadsafe(
+ channel._handle_activity(payload),
+ channel._loop,
+ )
+ fut.result(timeout=15)
+ except Exception as e:
+ channel.logger.warning("Activity handling failed: {}", e)
+
+ self.send_response(200)
+ self.send_header("Content-Type", "application/json")
+ self.end_headers()
+ self.wfile.write(b"{}")
+
+ def log_message(self, format: str, *args: Any) -> None:
+ return
+
+ self._server = ThreadingHTTPServer((self.config.host, self.config.port), Handler)
+ self._server_thread = threading.Thread(
+ target=self._server.serve_forever,
+ name="vibe-trading-msteams",
+ daemon=True,
+ )
+ self._server_thread.start()
+
+ self.logger.info(
+ "Webhook listening on http://{}:{}{}",
+ self.config.host,
+ self.config.port,
+ self.config.path,
+ )
+
+ while self._running:
+ await asyncio.sleep(1)
+
+ async def stop(self) -> None:
+ """Stop the channel."""
+ self._running = False
+ if self._server:
+ self._server.shutdown()
+ self._server.server_close()
+ self._server = None
+ if self._server_thread and self._server_thread.is_alive():
+ self._server_thread.join(timeout=2)
+ self._server_thread = None
+ if self._http:
+ await self._http.aclose()
+ self._http = None
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a plain text reply into an existing Teams conversation."""
+ if not self._http:
+ raise RuntimeError("MSTeams HTTP client not initialized")
+
+ ref = self._conversation_refs.get(str(msg.chat_id))
+ if not ref:
+ raise RuntimeError(f"MSTeams conversation ref not found for chat_id={msg.chat_id}")
+
+ if not self._is_trusted_service_url(ref.service_url):
+ raise RuntimeError(
+ f"MSTeams conversation ref has untrusted service_url for chat_id={msg.chat_id}"
+ )
+
+ token = await self._get_access_token()
+ base_url = f"{ref.service_url.rstrip('/')}/v3/conversations/{ref.conversation_id}/activities"
+ use_thread_reply = self.config.reply_in_thread and bool(ref.activity_id)
+ headers = {
+ "Authorization": f"Bearer {token}",
+ "Content-Type": "application/json",
+ }
+ payload = {
+ "type": "message",
+ "text": msg.content or " ",
+ }
+ if use_thread_reply:
+ payload["replyToId"] = ref.activity_id
+
+ try:
+ resp = await self._http.post(base_url, headers=headers, json=payload)
+ resp.raise_for_status()
+ self.logger.info("Message sent to {}", ref.conversation_id)
+ self._touch_conversation_ref(str(msg.chat_id), persist=True)
+ except Exception:
+ self.logger.exception("Send failed")
+ raise
+
+ async def _handle_activity(self, activity: dict[str, Any]) -> None:
+ """Handle inbound Teams/Bot Framework activity."""
+ if activity.get("type") != "message":
+ return
+
+ conversation = activity.get("conversation") or {}
+ from_user = activity.get("from") or {}
+ recipient = activity.get("recipient") or {}
+ channel_data = activity.get("channelData") or {}
+
+ sender_id = str(from_user.get("aadObjectId") or from_user.get("id") or "").strip()
+ conversation_id = str(conversation.get("id") or "").strip()
+ service_url = str(activity.get("serviceUrl") or "").strip()
+ activity_id = str(activity.get("id") or "").strip()
+ conversation_type = str(conversation.get("conversationType") or "").strip()
+
+ if not sender_id or not conversation_id or not service_url:
+ return
+
+ if not self._is_trusted_service_url(service_url):
+ self.logger.warning(
+ "Ignoring MSTeams activity with untrusted serviceUrl host: {}",
+ service_url,
+ )
+ return
+
+ if recipient.get("id") and from_user.get("id") == recipient.get("id"):
+ return
+
+ # DM-only MVP: ignore group/channel traffic for now
+ if conversation_type and conversation_type not in ("personal", ""):
+ self.logger.debug("Ignoring non-DM conversation {}", conversation_type)
+ return
+
+ text = self._sanitize_inbound_text(activity)
+ if not text:
+ text = self.config.mention_only_response.strip()
+ if not text:
+ self.logger.debug("Ignoring empty message after Teams text sanitization")
+ return
+
+ if not self.is_allowed(sender_id):
+ self.logger.warning(
+ "Access denied for sender {} on channel {}. "
+ "Add them to allowFrom list in config to grant access.",
+ sender_id, self.name,
+ )
+ return
+
+ with self._refs_guard:
+ self._conversation_refs[conversation_id] = ConversationRef(
+ service_url=service_url,
+ conversation_id=conversation_id,
+ bot_id=str(recipient.get("id") or "") or None,
+ activity_id=activity_id or None,
+ conversation_type=conversation_type or None,
+ tenant_id=str((channel_data.get("tenant") or {}).get("id") or "") or None,
+ updated_at=time.time(),
+ )
+ self._save_refs_locked()
+
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=conversation_id,
+ content=text,
+ metadata={
+ "msteams": {
+ "activity_id": activity_id,
+ "conversation_id": conversation_id,
+ "conversation_type": conversation_type or "personal",
+ "from_name": from_user.get("name"),
+ }
+ },
+ )
+
+ def _sanitize_inbound_text(self, activity: dict[str, Any]) -> str:
+ """Extract the user-authored text from a Teams activity."""
+ text = str(activity.get("text") or "")
+ text = self._strip_possible_bot_mention(text)
+ text = self._normalize_html_whitespace(text)
+
+ channel_data = activity.get("channelData") or {}
+ reply_to_id = str(activity.get("replyToId") or "").strip()
+ normalized_preview = html.unescape(text).replace("&rsquo", "’").strip()
+ normalized_preview = normalized_preview.replace("\xa0", " ")
+ normalized_preview = normalized_preview.replace("\r\n", "\n").replace("\r", "\n")
+ preview_lines = [line.strip() for line in normalized_preview.split("\n")]
+ while preview_lines and not preview_lines[0]:
+ preview_lines.pop(0)
+ first_line = preview_lines[0] if preview_lines else ""
+ looks_like_quote_wrapper = first_line.lower().startswith("replying to ") or first_line.startswith("Reply wrapper")
+
+ if reply_to_id or channel_data.get("messageType") == "reply" or looks_like_quote_wrapper:
+ text = self._normalize_teams_reply_quote(text)
+
+ return text.strip()
+
+ def _strip_possible_bot_mention(self, text: str) -> str:
+ """Remove simple Teams mention markup from message text."""
+ cleaned = re.sub(r"<at\b[^>]*>.*?</at>", " ", text, flags=re.IGNORECASE | re.DOTALL)
+ cleaned = re.sub(r"[^\S\r\n]+", " ", cleaned)
+ cleaned = re.sub(r"(?:\r?\n){3,}", "\n\n", cleaned)
+ return cleaned.strip()
+
+ def _normalize_html_whitespace(self, text: str) -> str:
+ """Normalize common HTML whitespace/entities from Teams into plain text spacing."""
+ normalized = html.unescape(text).replace("&rsquo", "’")
+ normalized = normalized.replace("\xa0", " ")
+ return normalized
+
+ def _normalize_teams_reply_quote(self, text: str) -> str:
+ """Normalize Teams quoted replies into a compact structured form."""
+ cleaned = self._normalize_html_whitespace(text).strip()
+ if not cleaned:
+ return ""
+
+ normalized_newlines = cleaned.replace("\r\n", "\n").replace("\r", "\n")
+ lines = [line.strip() for line in normalized_newlines.split("\n")]
+ while lines and not lines[0]:
+ lines.pop(0)
+
+ # Observed native Teams reply wrapper:
+ # Replying to Bob Smith
+ # actual reply text
+ if len(lines) >= 2 and lines[0].lower().startswith("replying to "):
+ quoted = lines[0][len("replying to ") :].strip(" :")
+ reply = "\n".join(lines[1:]).strip()
+ return self._format_reply_with_quote(quoted, reply)
+
+ # Observed reply wrapper where the quoted content is surfaced after a
+ # synthetic "Reply wrapper" header, sometimes with a blank line separating quote
+ # and reply, and sometimes as a compact line-based fallback shape.
+ if lines and lines[0].strip().startswith("Reply wrapper"):
+ body = normalized_newlines.split("\n", 1)[1] if "\n" in normalized_newlines else ""
+ body = body.lstrip()
+ parts = re.split(r"\n\s*\n", body, maxsplit=1)
+ if len(parts) == 2:
+ quoted = re.sub(r"\s+", " ", parts[0]).strip()
+ reply = re.sub(r"\s+", " ", parts[1]).strip()
+ if quoted or reply:
+ return self._format_reply_with_quote(quoted, reply)
+
+ body_lines = [line.strip() for line in body.split("\n") if line.strip()]
+ if body_lines:
+ quoted = " ".join(body_lines[:-1]).strip()
+ reply = body_lines[-1].strip()
+ if quoted and reply:
+ return self._format_reply_with_quote(quoted, reply)
+
+ # Observed compact fallback where the relay flattens quote and reply into
+ # a single line after the synthetic Reply wrapper prefix.
+ compact = re.sub(r"\s+", " ", normalized_newlines).strip()
+ if compact.startswith("Reply wrapper "):
+ compact = compact[len("Reply wrapper ") :].strip()
+ for boundary in (". ", "! ", "? ", "… "):
+ idx = compact.rfind(boundary)
+ if idx == -1:
+ continue
+ quoted = compact[: idx + 1].strip()
+ reply = compact[idx + len(boundary) :].strip()
+ if quoted and reply and len(reply) <= 160:
+ return self._format_reply_with_quote(quoted, reply)
+
+ return cleaned
+
+ def _format_reply_with_quote(self, quoted: str, reply: str) -> str:
+ """Format a reply-with-context message for the model without Teams wrapper noise."""
+ quoted = quoted.strip()
+ reply = reply.strip()
+ if quoted and reply:
+ return f"User is replying to: {quoted}\nUser reply: {reply}"
+ if reply:
+ return reply
+ return quoted
+
+ async def _validate_inbound_auth(self, auth_header: str, activity: dict[str, Any]) -> None:
+ """Validate inbound Bot Framework bearer token."""
+ if not MSTEAMS_AVAILABLE:
+ raise RuntimeError("PyJWT not installed. Run: pip install vibe-trading-ai[msteams]")
+
+ if not auth_header.lower().startswith("bearer "):
+ raise ValueError("missing bearer token")
+
+ token = auth_header.split(" ", 1)[1].strip()
+ if not token:
+ raise ValueError("empty bearer token")
+
+ header = jwt.get_unverified_header(token)
+ kid = str(header.get("kid") or "").strip()
+ if not kid:
+ raise ValueError("missing token kid")
+
+ jwks = await self._get_botframework_jwks()
+ keys = jwks.get("keys") or []
+ jwk = next((key for key in keys if key.get("kid") == kid), None)
+ if not jwk:
+ raise ValueError(f"signing key not found for kid={kid}")
+
+ public_key = jwt.algorithms.RSAAlgorithm.from_jwk(json.dumps(jwk))
+ claims = jwt.decode(
+ token,
+ key=public_key,
+ algorithms=["RS256"],
+ audience=self.config.app_id,
+ issuer="https://api.botframework.com",
+ options={
+ "require": ["exp", "nbf", "iss", "aud"],
+ },
+ )
+
+ claim_service_url = str(
+ claims.get("serviceurl") or claims.get("serviceUrl") or "",
+ ).strip()
+ activity_service_url = str(activity.get("serviceUrl") or "").strip()
+ if claim_service_url and activity_service_url and claim_service_url != activity_service_url:
+ raise ValueError("serviceUrl claim mismatch")
+
+ async def _get_botframework_openid_config(self) -> dict[str, Any]:
+ """Fetch and cache Bot Framework OpenID configuration."""
+
+ now = time.time()
+ if self._botframework_openid_config and now < self._botframework_openid_config_expires_at:
+ return self._botframework_openid_config
+
+ if not self._http:
+ raise RuntimeError("MSTeams HTTP client not initialized")
+
+ resp = await self._http.get(self._botframework_openid_config_url)
+ resp.raise_for_status()
+ self._botframework_openid_config = resp.json()
+ self._botframework_openid_config_expires_at = now + 3600
+ return self._botframework_openid_config
+
+ async def _get_botframework_jwks(self) -> dict[str, Any]:
+ """Fetch and cache Bot Framework JWKS."""
+
+ now = time.time()
+ if self._botframework_jwks and now < self._botframework_jwks_expires_at:
+ return self._botframework_jwks
+
+ if not self._http:
+ raise RuntimeError("MSTeams HTTP client not initialized")
+
+ openid_config = await self._get_botframework_openid_config()
+ jwks_uri = str(openid_config.get("jwks_uri") or "").strip()
+ if not jwks_uri:
+ raise RuntimeError("Bot Framework OpenID config missing jwks_uri")
+
+ resp = await self._http.get(jwks_uri)
+ resp.raise_for_status()
+ self._botframework_jwks = resp.json()
+ self._botframework_jwks_expires_at = now + 3600
+ return self._botframework_jwks
+
+ @staticmethod
+ def _safe_float(value: Any) -> float | None:
+ try:
+ out = float(value)
+ if out > 0:
+ return out
+ except (TypeError, ValueError):
+ return None
+ return None
+
+ def _normalize_ref_record(self, value: Any) -> ConversationRef | None:
+ """Normalize a stored ref record from legacy/current schema."""
+ if not isinstance(value, dict):
+ return None
+ service_url = str(value.get("service_url") or "").strip()
+ conversation_id = str(value.get("conversation_id") or "").strip()
+ if not service_url or not conversation_id:
+ return None
+ return ConversationRef(
+ service_url=service_url,
+ conversation_id=conversation_id,
+ bot_id=str(value.get("bot_id") or "") or None,
+ activity_id=str(value.get("activity_id") or "") or None,
+ conversation_type=str(value.get("conversation_type") or "") or None,
+ tenant_id=str(value.get("tenant_id") or "") or None,
+ updated_at=self._safe_float(value.get("updated_at")),
+ )
+
+ def _load_refs_raw(self) -> tuple[dict[str, Any], dict[str, Any], bool]:
+ """Load raw refs/main+meta JSON payloads."""
+ main_data: dict[str, Any] = {}
+ meta_data: dict[str, Any] = {}
+ meta_exists = self._refs_meta_path.exists()
+
+ if self._refs_path.exists():
+ try:
+ loaded = json.loads(self._refs_path.read_text(encoding="utf-8"))
+ if isinstance(loaded, dict):
+ main_data = loaded
+ except Exception as e:
+ self.logger.warning("Failed to load conversation refs: {}", e)
+
+ if meta_exists:
+ try:
+ loaded_meta = json.loads(self._refs_meta_path.read_text(encoding="utf-8"))
+ if isinstance(loaded_meta, dict):
+ meta_data = loaded_meta
+ except Exception as e:
+ self.logger.warning("Failed to load conversation refs metadata: {}", e)
+
+ return main_data, meta_data, meta_exists
+
+ def _load_refs_from_disk(self) -> dict[str, ConversationRef]:
+ """Load refs from disk with compatibility fallback for legacy layouts."""
+ main_data, meta_data, meta_exists = self._load_refs_raw()
+ if not main_data:
+ return {}
+
+ out: dict[str, ConversationRef] = {}
+ now = time.time()
+ for key, value in main_data.items():
+ ref = self._normalize_ref_record(value)
+ if not ref:
+ continue
+
+ meta_entry = meta_data.get(key) if isinstance(meta_data, dict) else None
+ meta_ts = None
+ if isinstance(meta_entry, dict):
+ meta_ts = self._safe_float(meta_entry.get("updated_at"))
+ elif meta_entry is not None:
+ meta_ts = self._safe_float(meta_entry)
+
+ if meta_ts is not None:
+ ref.updated_at = meta_ts
+ elif not meta_exists:
+ # First run after introducing meta sidecar: keep legacy refs alive
+ # by initializing timestamps to "now" instead of purging immediately.
+ ref.updated_at = now
+ elif ref.updated_at is None:
+ ref.updated_at = now
+
+ out[key] = ref
+ return out
+
+ def _load_refs(self) -> dict[str, ConversationRef]:
+ """Load stored conversation references."""
+ return self._load_refs_from_disk()
+
+ @contextmanager
+ def _refs_file_lock(self):
+ """Cross-process lock while merging and writing refs state."""
+ self._refs_path.parent.mkdir(parents=True, exist_ok=True)
+ lock_fp = self._refs_lock_path.open("a+", encoding="utf-8")
+ try:
+ if fcntl is not None:
+ fcntl.flock(lock_fp.fileno(), fcntl.LOCK_EX)
+ yield
+ finally:
+ try:
+ if fcntl is not None:
+ fcntl.flock(lock_fp.fileno(), fcntl.LOCK_UN)
+ finally:
+ lock_fp.close()
+
+ def _is_webchat_service_url(self, service_url: str) -> bool:
+ """Return True when service URL points to unsupported Bot Framework Web Chat."""
+ normalized = service_url.strip()
+ if not normalized:
+ return False
+ host = (urlparse(normalized).hostname or "").strip().lower()
+ if host:
+ return host == MSTEAMS_WEBCHAT_HOST or host.endswith(f".{MSTEAMS_WEBCHAT_HOST}")
+ return MSTEAMS_WEBCHAT_HOST in normalized.lower()
+
+ def _is_trusted_service_url(self, service_url: str) -> bool:
+ """Return True for HTTPS Bot Framework service URLs trusted for bearer replies."""
+ parsed = urlparse(service_url.strip())
+ if parsed.scheme.lower() != "https":
+ return False
+
+ host = (parsed.hostname or "").strip().lower().rstrip(".")
+ if not host:
+ return False
+
+ for pattern in self.config.trusted_service_url_hosts:
+ trusted_host = str(pattern or "").strip().lower().rstrip(".")
+ if not trusted_host:
+ continue
+ if trusted_host.startswith("*."):
+ suffix = trusted_host[1:]
+ if host.endswith(suffix) and host != suffix.lstrip("."):
+ return True
+ continue
+ if host == trusted_host:
+ return True
+ return False
+
+ def _prune_conversation_refs(self, *, now: float | None = None) -> bool:
+ """Remove stale and unsupported conversation refs from memory."""
+ if not self._conversation_refs:
+ return False
+
+ now_ts = time.time() if now is None else now
+ ttl_days = int(self.config.ref_ttl_days)
+ stale_before = now_ts - (ttl_days * 24 * 60 * 60)
+ keys_to_drop: list[str] = []
+
+ for key, ref in self._conversation_refs.items():
+ if not self._is_trusted_service_url(ref.service_url):
+ keys_to_drop.append(key)
+ continue
+
+ if self.config.prune_web_chat_refs and self._is_webchat_service_url(ref.service_url):
+ keys_to_drop.append(key)
+ continue
+
+ conv_type = str(ref.conversation_type or "").strip().lower()
+ if self.config.prune_non_personal_refs and conv_type and conv_type != "personal":
+ keys_to_drop.append(key)
+ continue
+
+ try:
+ updated_at = float(ref.updated_at) if ref.updated_at is not None else 0.0
+ except (TypeError, ValueError):
+ updated_at = 0.0
+ if updated_at <= 0 or updated_at < stale_before:
+ keys_to_drop.append(key)
+
+ if not keys_to_drop:
+ return False
+
+ for key in keys_to_drop:
+ self._conversation_refs.pop(key, None)
+ self.logger.info(
+ "Pruned {} stale/unsupported conversation refs (ttl={} days)",
+ len(keys_to_drop),
+ ttl_days,
+ )
+ return True
+
+ def _merge_refs_from_disk_locked(self) -> None:
+ """Merge disk refs into memory to reduce lost updates across processes."""
+ disk_refs = self._load_refs_from_disk()
+ for key, disk_ref in disk_refs.items():
+ mem_ref = self._conversation_refs.get(key)
+ if mem_ref is None:
+ self._conversation_refs[key] = disk_ref
+ continue
+ disk_ts = self._safe_float(disk_ref.updated_at) or 0.0
+ mem_ts = self._safe_float(mem_ref.updated_at) or 0.0
+ if disk_ts > mem_ts:
+ self._conversation_refs[key] = disk_ref
+
+ def _touch_conversation_ref(self, chat_id: str, *, persist: bool = False) -> None:
+ """Refresh updated_at for an active ref to keep it from expiring while used."""
+ with self._refs_guard:
+ ref = self._conversation_refs.get(str(chat_id))
+ if not ref:
+ return
+ now = time.time()
+ prev = self._safe_float(ref.updated_at) or 0.0
+ min_interval = max(0, int(self.config.ref_touch_interval_s))
+ if min_interval > 0 and prev > 0 and now - prev < min_interval:
+ return
+ ref.updated_at = now
+ if persist:
+ self._save_refs_locked()
+
+ def _write_json_atomically(self, path, data: dict[str, Any]) -> None:
+ """Write refs JSON atomically to reduce corruption risk during crashes."""
+ payload = json.dumps(data, indent=2)
+ tmp_path: str | None = None
+ try:
+ fd, tmp_path = tempfile.mkstemp(
+ dir=str(path.parent),
+ prefix=f"{path.name}.",
+ suffix=".tmp",
+ )
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
+ f.write(payload)
+ f.flush()
+ os.fsync(f.fileno())
+ os.replace(tmp_path, path)
+ finally:
+ if tmp_path and os.path.exists(tmp_path):
+ with suppress(OSError):
+ os.unlink(tmp_path)
+
+ def _save_refs_locked(self, *, prune: bool = True) -> None:
+ """Persist conversation references (caller must hold _refs_guard)."""
+ try:
+ with self._refs_file_lock():
+ self._merge_refs_from_disk_locked()
+ if prune:
+ self._prune_conversation_refs()
+ refs_data = {
+ key: {
+ "service_url": ref.service_url,
+ "conversation_id": ref.conversation_id,
+ "bot_id": ref.bot_id,
+ "activity_id": ref.activity_id,
+ "conversation_type": ref.conversation_type,
+ "tenant_id": ref.tenant_id,
+ }
+ for key, ref in self._conversation_refs.items()
+ }
+ refs_meta = {
+ key: {
+ "updated_at": self._safe_float(ref.updated_at),
+ }
+ for key, ref in self._conversation_refs.items()
+ }
+ self._write_json_atomically(self._refs_path, refs_data)
+ self._write_json_atomically(self._refs_meta_path, refs_meta)
+ except Exception as e:
+ self.logger.warning("Failed to save conversation refs: {}", e)
+
+ def _save_refs(self, *, prune: bool = True) -> None:
+ """Persist conversation references."""
+ with self._refs_guard:
+ self._save_refs_locked(prune=prune)
+
+ async def _get_access_token(self) -> str:
+ """Fetch an access token for Bot Framework / Azure Bot auth."""
+
+ now = time.time()
+ if self._token and now < self._token_expires_at - 60:
+ return self._token
+
+ if not self._http:
+ raise RuntimeError("MSTeams HTTP client not initialized")
+
+ tenant = (self.config.tenant_id or "").strip() or "botframework.com"
+ token_url = f"https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token"
+ data = {
+ "grant_type": "client_credentials",
+ "client_id": self.config.app_id,
+ "client_secret": self.config.app_password,
+ "scope": "https://api.botframework.com/.default",
+ }
+ resp = await self._http.post(token_url, data=data)
+ resp.raise_for_status()
+ payload = resp.json()
+ self._token = payload["access_token"]
+ self._token_expires_at = now + int(payload.get("expires_in", 3600))
+ return self._token
diff --git a/agent/src/channels/napcat.py b/agent/src/channels/napcat.py
new file mode 100644
index 000000000..123b07e5d
--- /dev/null
+++ b/agent/src/channels/napcat.py
@@ -0,0 +1,579 @@
+"""Napcat (OneBot v11) channel for QQ, over WebSocket."""
+
+from __future__ import annotations
+
+import asyncio
+import base64
+import json
+import os
+import random
+import time
+import uuid
+from collections import deque
+from pathlib import Path
+from typing import Annotated, Any, Literal
+
+import aiohttp
+import logging; logger = logging.getLogger(__name__)
+from pydantic import Field
+from websockets.asyncio.client import ClientConnection
+from websockets.asyncio.client import connect as ws_connect
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.utils import validate_url_target
+from src.channels.utils import safe_filename
+
+_DOWNLOAD_TIMEOUT = aiohttp.ClientTimeout(total=60)
+_ACTION_TIMEOUT = 20.0
+
+
+# `"mention"` (only @mentions / replies) | `"open"` (every message) | float p
+# in [0, 1]: mentions/replies always reply; other messages reply with probability
+# p. 0.0 ≡ "mention", 1.0 ≡ "open".
+GroupPolicy = Literal["mention", "open"] | Annotated[float, Field(ge=0.0, le=1.0)]
+
+
+class NapcatConfig(BaseModel):
+ """Napcat (OneBot v11) channel configuration."""
+
+ enabled: bool = False
+ ws_url: str = "ws://127.0.0.1:3001"
+ access_token: str = ""
+ allow_from: list[str] = Field(default_factory=list)
+ group_policy: GroupPolicy = "mention"
+ # Per-group overrides keyed by stringified group_id, e.g. {"123456": "open"}.
+ # Falls back to `group_policy` when a group_id isn't listed.
+ group_policy_overrides: dict[str, GroupPolicy] = Field(default_factory=dict)
+ welcome_new_members: bool = True
+ # Hard cap for inbound image downloads. Bigger images are dropped.
+ max_image_bytes: int = Field(default=20 * 1024 * 1024, ge=1)
+
+
+class NapcatChannel(BaseChannel):
+ """Napcat / OneBot v11 channel."""
+
+ name = "napcat"
+ display_name = "Napcat (QQ)"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return NapcatConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = NapcatConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: NapcatConfig = config
+
+ self._ws: ClientConnection | None = None
+ self._http: aiohttp.ClientSession | None = None
+ self._media_root: Path = get_media_dir("napcat")
+ self._self_id: int | None = None
+ self._pending: dict[str, asyncio.Future[dict[str, Any]]] = {}
+ self._processed_ids: deque[int] = deque(maxlen=2000)
+ self._bot_outbound_ids: deque[int] = deque(maxlen=2000)
+ self._background_tasks: set[asyncio.Task[None]] = set()
+
+ # ------------------------------------------------------------------
+ # Lifecycle
+ # ------------------------------------------------------------------
+
+ async def start(self) -> None:
+ if not self.config.ws_url:
+ logger.error("napcat: ws_url not configured")
+ return
+
+ self._running = True
+ self._http = aiohttp.ClientSession(timeout=_DOWNLOAD_TIMEOUT)
+
+ backoff = iter((5, 10)) # then 30s forever
+ while self._running:
+ try:
+ await self._run_once()
+ backoff = iter((5, 10)) # reset after a clean session
+ except asyncio.CancelledError:
+ raise
+ except Exception as e:
+ logger.warning("napcat: connection lost: {}", e)
+ if self._running:
+ await asyncio.sleep(next(backoff, 30))
+
+ async def _run_once(self) -> None:
+ headers = []
+ if self.config.access_token:
+ headers.append(("Authorization", f"Bearer {self.config.access_token}"))
+
+ logger.info("napcat: connecting to {}", self.config.ws_url)
+ async with ws_connect(self.config.ws_url, additional_headers=headers) as ws:
+ self._ws = ws
+ logger.info("napcat: connected")
+ try:
+ # Validate the connection before entering the dispatch loop.
+ # Napcat may interleave meta_event frames before our echo
+ # response, so dispatch any non-matching frames as we go.
+ echo = uuid.uuid4().hex
+ await ws.send(
+ json.dumps(
+ {"action": "get_login_info", "params": {}, "echo": echo},
+ ensure_ascii=False,
+ )
+ )
+ deadline = asyncio.get_running_loop().time() + _ACTION_TIMEOUT
+ while True:
+ remaining = deadline - asyncio.get_running_loop().time()
+ if remaining <= 0:
+ raise asyncio.TimeoutError("get_login_info timed out")
+ raw = await asyncio.wait_for(ws.recv(), timeout=remaining)
+ try:
+ payload = json.loads(raw)
+ except json.JSONDecodeError:
+ continue
+ if isinstance(payload, dict) and payload.get("echo") == echo:
+ data = payload.get("data") or {}
+ logger.info(
+ "napcat: logged in as {} (user_id={})",
+ data.get("nickname"),
+ data.get("user_id"),
+ )
+ break
+ await self._dispatch_frame(raw)
+
+ async for raw in ws:
+ await self._dispatch_frame(raw)
+ finally:
+ self._ws = None
+ self._fail_pending(RuntimeError("napcat: websocket disconnected"))
+
+ async def stop(self) -> None:
+ self._running = False
+ if self._ws is not None:
+ try:
+ await self._ws.close()
+ except Exception:
+ pass
+ self._ws = None
+ if self._http is not None:
+ try:
+ await self._http.close()
+ except Exception:
+ pass
+ self._http = None
+ self._fail_pending(RuntimeError("napcat: stopped"))
+ tasks = list(self._background_tasks)
+ for task in tasks:
+ task.cancel()
+ if tasks:
+ await asyncio.gather(*tasks, return_exceptions=True)
+ self._background_tasks.clear()
+
+ def _fail_pending(self, err: BaseException) -> None:
+ for fut in self._pending.values():
+ if not fut.done():
+ fut.set_exception(err)
+ self._pending.clear()
+
+ # ------------------------------------------------------------------
+ # Frame dispatch
+ # ------------------------------------------------------------------
+
+ async def _dispatch_frame(self, raw: str | bytes) -> None:
+ # logger.debug("dispatch frame {}", raw)
+ try:
+ payload = json.loads(raw)
+ except json.JSONDecodeError:
+ logger.debug("napcat: dropping non-JSON frame")
+ return
+ if not isinstance(payload, dict):
+ return
+
+ # Action response: identified by `echo` and absence of post_type.
+ if "echo" in payload and payload.get("post_type") is None:
+ echo = payload.get("echo")
+ fut = self._pending.pop(echo, None) if isinstance(echo, str) else None
+ if fut and not fut.done():
+ fut.set_result(payload)
+ return
+
+ if (sid := payload.get("self_id")) is not None:
+ try:
+ self._self_id = int(sid)
+ except (TypeError, ValueError):
+ pass
+
+ post_type = payload.get("post_type")
+ if post_type == "message":
+ self._create_background_task(self._on_message(payload), "message")
+ elif post_type == "notice":
+ self._create_background_task(self._on_notice(payload), "notice")
+
+ def _create_background_task(self, coro: Any, kind: str) -> None:
+ task = asyncio.create_task(coro)
+ self._background_tasks.add(task)
+
+ def _done(done: asyncio.Task[None]) -> None:
+ self._background_tasks.discard(done)
+ try:
+ done.result()
+ except asyncio.CancelledError:
+ pass
+ except Exception as e:
+ logger.warning("napcat: {} handler failed: {}", kind, e)
+
+ task.add_done_callback(_done)
+
+ # ------------------------------------------------------------------
+ # Inbound: messages
+ # ------------------------------------------------------------------
+
+ async def _on_message(self, ev: dict[str, Any]) -> None:
+ msg_id = ev.get("message_id")
+ if isinstance(msg_id, int):
+ if msg_id in self._processed_ids:
+ return
+ self._processed_ids.append(msg_id)
+
+ message_type = ev.get("message_type")
+ user_id = ev.get("user_id")
+ if user_id is None or message_type not in ("group", "private"):
+ return
+
+ segments = self._normalize_segments(ev.get("message"))
+ text, images, mentioned_self, reply_to_id = self._parse_segments(segments)
+
+ media_paths: list[str] = []
+ for info in images:
+ if local := await self._download_image(info):
+ media_paths.append(local)
+
+ sender = ev.get("sender") or {}
+ nickname = sender.get("card") or sender.get("nickname")
+
+ if message_type == "group":
+ group_id = ev.get("group_id")
+ if group_id is None:
+ return
+
+ replying_to_bot = (
+ isinstance(reply_to_id, int) and reply_to_id in self._bot_outbound_ids
+ )
+ if not self._should_reply_in_group(
+ group_id=group_id,
+ mentioned_self=mentioned_self,
+ replying_to_bot=replying_to_bot,
+ ):
+ return
+
+ chat_id = f"group:{group_id}"
+ content = self._format_group_content(
+ text=text,
+ nickname=nickname,
+ user_id=user_id,
+ )
+ else:
+ chat_id = f"private:{user_id}"
+ content = text
+
+ if not content and not media_paths:
+ return
+
+ await self._handle_message(
+ sender_id=str(user_id),
+ chat_id=chat_id,
+ content=content,
+ media=media_paths or None,
+ metadata={
+ "message_id": msg_id,
+ "is_group": message_type == "group",
+ "nickname": nickname,
+ "reply_to": reply_to_id,
+ },
+ )
+
+ @staticmethod
+ def _normalize_segments(message: Any) -> list[dict[str, Any]]:
+ # Napcat defaults to array format. Treat raw strings as a single text
+ # segment rather than parsing CQ codes — that path is fragile and
+ # users can configure napcat to emit arrays.
+ if isinstance(message, list):
+ return [seg for seg in message if isinstance(seg, dict)]
+ if isinstance(message, str) and message:
+ return [{"type": "text", "data": {"text": message}}]
+ return []
+
+ def _parse_segments(
+ self, segments: list[dict[str, Any]]
+ ) -> tuple[str, list[dict[str, Any]], bool, int | None]:
+ parts: list[str] = []
+ images: list[dict[str, Any]] = []
+ mentioned_self = False
+ reply_to: int | None = None
+ self_id_str = str(self._self_id) if self._self_id is not None else None
+
+ for seg in segments:
+ stype = seg.get("type")
+ data = seg.get("data") or {}
+ if stype == "text":
+ if txt := data.get("text"):
+ parts.append(str(txt))
+ elif stype == "image":
+ # OneBot exposes the downloadable image at `url`. Napcat
+ # additionally provides `file` (e.g. <md5>.png) and
+ # `file_size` (bytes, sometimes a string).
+ url = data.get("url")
+ if isinstance(url, str) and url.startswith(("http://", "https://")):
+ images.append(
+ {
+ "url": url,
+ "file": data.get("file"),
+ "file_size": data.get("file_size"),
+ }
+ )
+ else:
+ logger.warning("napcat: received invalid image url: {}", url)
+ elif stype == "at":
+ qq = str(data.get("qq", ""))
+ if self_id_str and qq == self_id_str:
+ mentioned_self = True
+ else:
+ parts.append(f"@{qq}")
+ elif stype == "reply":
+ rid = data.get("id")
+ try:
+ reply_to = int(rid) if rid is not None else None
+ except (TypeError, ValueError):
+ pass
+ elif stype == "face":
+ parts.append(f"[face:{data.get('id', '')}]")
+
+ text = " ".join(p.strip() for p in parts if p.strip()).strip()
+ return text, images, mentioned_self, reply_to
+
+ def _should_reply_in_group(
+ self, *, group_id: Any, mentioned_self: bool, replying_to_bot: bool
+ ) -> bool:
+ if mentioned_self or replying_to_bot:
+ return True
+ policy = self.config.group_policy_overrides.get(str(group_id), self.config.group_policy)
+ if policy == "open":
+ return True
+ if policy == "mention":
+ return False
+ # Probability case: float in [0.0, 1.0].
+ return random.random() < float(policy)
+
+ @staticmethod
+ def _format_group_content(
+ *,
+ text: str,
+ nickname: str,
+ user_id: Any,
+ ) -> str:
+ label = nickname or str(user_id)
+ return f"{label}: {text}"
+
+ # ------------------------------------------------------------------
+ # Inbound: notices (member joined etc.)
+ # ------------------------------------------------------------------
+
+ async def _on_notice(self, ev: dict[str, Any]) -> None:
+ if ev.get("notice_type") != "group_increase" or not self.config.welcome_new_members:
+ return
+
+ group_id = ev.get("group_id")
+ user_id = ev.get("user_id")
+ if group_id is None or user_id is None:
+ return
+
+ try:
+ group_id_int = int(group_id)
+ user_id_int = int(user_id)
+ except (TypeError, ValueError):
+ logger.warning("napcat: invalid group_increase ids group_id={} user_id={}", group_id, user_id)
+ return
+
+ nickname = await self._lookup_member_name(group_id_int, user_id_int)
+
+ # Note: this routes through is_allowed(). For group bots set
+ # `allow_from: ["*"]` (or include the joining user's id) for welcomes
+ # to fire — same trust model as a regular inbound message.
+ await self._handle_message(
+ sender_id=str(user_id),
+ chat_id=f"group:{group_id}",
+ content=f"[group event] new member {nickname} joined group {group_id}",
+ metadata={
+ "is_group": True,
+ "event": "group_increase",
+ },
+ )
+
+ async def _lookup_member_name(self, group_id: int, user_id: int) -> str:
+ """Lookup group member nickname. Fallback to user id."""
+ try:
+ resp = await self._call_action(
+ "get_group_member_info",
+ {"group_id": group_id, "user_id": user_id, "no_cache": True},
+ )
+ data = resp.get("data", {})
+ # logger.debug("get_group_member_info: {}", resp)
+ return data.get("card") or data.get("nickname") or str(user_id)
+ except Exception as e:
+ logger.warning("napcat: get_group_member_info failed: {}", e)
+ return str(user_id)
+
+ # ------------------------------------------------------------------
+ # Outbound
+ # ------------------------------------------------------------------
+
+ async def send(self, msg: OutboundMessage) -> None:
+ if self._ws is None:
+ logger.warning("napcat: not connected, dropping outbound message")
+ return
+
+ kind, _, target = msg.chat_id.partition(":")
+ if kind not in ("private", "group") or not target:
+ logger.error("napcat: invalid chat_id '{}'", msg.chat_id)
+ return
+
+ segments: list[dict[str, Any]] = []
+ for ref in msg.media or []:
+ if seg := await self._build_image_segment(ref):
+ segments.append(seg)
+ if text := (msg.content or "").strip():
+ segments.append({"type": "text", "data": {"text": text}})
+ if not segments:
+ return
+
+ params: dict[str, Any] = {"message": segments}
+ if kind == "group":
+ params["message_type"] = "group"
+ params["group_id"] = int(target)
+ else:
+ params["message_type"] = "private"
+ params["user_id"] = int(target)
+
+ resp = await self._call_action("send_msg", params)
+ data = resp.get("data") or {}
+ if (mid := data.get("message_id")) is not None:
+ self._bot_outbound_ids.append(int(mid))
+
+ async def _build_image_segment(self, ref: str) -> dict[str, Any] | None:
+ ref = (ref or "").strip()
+ if not ref:
+ return None
+ if ref.startswith(("http://", "https://")):
+ ok, err = validate_url_target(ref)
+ if not ok:
+ logger.warning("napcat: rejected remote image '{}': {}", ref, err)
+ return None
+ return {"type": "image", "data": {"file": ref}}
+ # Local path → base64 so it works even when napcat runs on a
+ # different host/container than vibe-trading.
+ path = Path(os.path.expanduser(ref)).resolve()
+ if not path.is_file():
+ logger.warning("napcat: local image not found: {}", path)
+ return None
+ data = await asyncio.to_thread(path.read_bytes)
+ return {"type": "image", "data": {"file": "base64://" + base64.b64encode(data).decode()}}
+
+ async def _call_action(
+ self,
+ action: str,
+ params: dict[str, Any],
+ timeout: float = _ACTION_TIMEOUT,
+ ) -> dict[str, Any]:
+ if self._ws is None:
+ raise RuntimeError("napcat: not connected")
+ echo = uuid.uuid4().hex
+ loop = asyncio.get_running_loop()
+ fut: asyncio.Future[dict[str, Any]] = loop.create_future()
+ self._pending[echo] = fut
+ try:
+ await self._ws.send(
+ json.dumps({"action": action, "params": params, "echo": echo}, ensure_ascii=False)
+ )
+ resp = await asyncio.wait_for(fut, timeout=timeout)
+ status = resp.get("status")
+ retcode = resp.get("retcode")
+ if (status and status != "ok") or (retcode not in (None, 0)):
+ raise RuntimeError(
+ f"napcat: action {action} failed status={status!r} retcode={retcode!r}"
+ )
+ return resp
+ finally:
+ self._pending.pop(echo, None)
+
+ # ------------------------------------------------------------------
+ # Image download
+ # ------------------------------------------------------------------
+
+ async def _download_image(self, info: dict[str, Any]) -> str | None:
+ url = info.get("url")
+ if not isinstance(url, str):
+ return None
+ # logger.debug("napcat: downloading image from {}", url)
+ if self._http is None:
+ return None
+ ok, err = validate_url_target(url)
+ if not ok:
+ logger.warning("napcat: skip image '{}': {}", url, err)
+ return None
+ max_bytes = self.config.max_image_bytes
+
+ # Reject upfront when napcat tells us the size and it's too big.
+ try:
+ declared_size = int(info["file_size"])
+ if declared_size > max_bytes:
+ logger.warning(
+ "napcat: image declared size={} exceeds max_image_bytes={} url={}",
+ declared_size,
+ max_bytes,
+ url,
+ )
+ return None
+ except (TypeError, KeyError):
+ pass
+
+ try:
+ async with self._http.get(url, allow_redirects=False) as resp:
+ if 300 <= resp.status < 400:
+ logger.warning("napcat: image download redirect rejected url={}", url)
+ return None
+ if resp.status >= 400:
+ logger.warning("napcat: image download status={} url={}", resp.status, url)
+ return None
+ # Stream until EOF, capping memory at max_bytes. Don't use
+ # content.read(max_bytes+1) — it returns only what's currently
+ # buffered, which truncates chunked responses mid-image.
+ buf = bytearray()
+ truncated = False
+ async for chunk in resp.content.iter_chunked(64 * 1024):
+ buf.extend(chunk)
+ if len(buf) > max_bytes:
+ truncated = True
+ break
+ if truncated:
+ logger.warning(
+ "napcat: image exceeds max_image_bytes={} url={}", max_bytes, url
+ )
+ return None
+ data = bytes(buf)
+ except Exception as e:
+ logger.warning("napcat: image download error url={} err={}", url, e)
+ return None
+
+ filename_hint = info.get("file")
+ if filename_hint:
+ name = safe_filename(filename_hint)
+ else:
+ name = f"{int(time.time() * 1000)}.jpg"
+ path = self._media_root / name
+ try:
+ await asyncio.to_thread(path.write_bytes, data)
+ except OSError as e:
+ logger.warning("napcat: failed to save image: {}", e)
+ return None
+ return str(path)
diff --git a/agent/src/channels/pairing/__init__.py b/agent/src/channels/pairing/__init__.py
new file mode 100644
index 000000000..af818c78b
--- /dev/null
+++ b/agent/src/channels/pairing/__init__.py
@@ -0,0 +1,33 @@
+"""Pairing module for DM sender approval."""
+
+from src.channels.pairing.store import (
+ approve_code,
+ deny_code,
+ format_expiry,
+ format_pairing_reply,
+ generate_code,
+ get_approved,
+ handle_pairing_command,
+ is_approved,
+ list_pending,
+ revoke,
+)
+
+# Metadata keys used by channels and commands to tag pairing-related messages.
+PAIRING_CODE_META_KEY = "_pairing_code"
+PAIRING_COMMAND_META_KEY = "_pairing_command"
+
+__all__ = [
+ "PAIRING_CODE_META_KEY",
+ "PAIRING_COMMAND_META_KEY",
+ "approve_code",
+ "deny_code",
+ "format_expiry",
+ "format_pairing_reply",
+ "generate_code",
+ "get_approved",
+ "handle_pairing_command",
+ "is_approved",
+ "list_pending",
+ "revoke",
+]
diff --git a/agent/src/channels/pairing/store.py b/agent/src/channels/pairing/store.py
new file mode 100644
index 000000000..f67e11285
--- /dev/null
+++ b/agent/src/channels/pairing/store.py
@@ -0,0 +1,262 @@
+"""Pairing store for DM sender approval.
+
+Persistent storage at ``~/.vibe-trading/pairing.json`` keeps approved senders
+and pending pairing codes per channel. The store is designed for
+private-assistant scale: small JSON file, simple locking, no external DB.
+"""
+
+from __future__ import annotations
+
+import json
+import logging
+import secrets
+import string
+import threading
+import time
+from pathlib import Path
+from typing import Any
+
+from src.config.paths import get_data_dir
+
+logger = logging.getLogger(__name__)
+
+# threading.Lock is used so store functions remain callable from both sync CLI
+# and async channel handlers. At private-assistant scale (small JSON file,
+# sub-millisecond operations) the brief block is acceptable.
+_LOCK = threading.Lock()
+_ALPHABET = string.ascii_uppercase + string.digits
+_CODE_LENGTH = 8 # e.g. ABCD-EFGH
+_TTL_DEFAULT_S = 600 # 10 minutes
+
+
+def _store_path() -> Path:
+ return get_data_dir() / "pairing.json"
+
+
+def _write_text_atomic(path: Path, text: str) -> None:
+ """Write *text* to *path* atomically via a temp file + rename."""
+ tmp = path.with_suffix(path.suffix + ".tmp")
+ tmp.write_text(text, encoding="utf-8")
+ tmp.replace(path)
+
+
+def _load() -> dict[str, Any]:
+ path = _store_path()
+ try:
+ with open(path, encoding="utf-8") as f:
+ data = json.load(f)
+ except FileNotFoundError:
+ return {"approved": {}, "pending": {}}
+ except (json.JSONDecodeError, OSError):
+ logger.warning("Corrupted pairing store, resetting")
+ return {"approved": {}, "pending": {}}
+
+ # Convert approved lists to str sets for O(1) lookup.
+ for channel, users in data.get("approved", {}).items():
+ data["approved"][channel] = {str(u) for u in users}
+ return data
+
+
+def _save(data: dict[str, Any]) -> None:
+ path = _store_path()
+ path.parent.mkdir(parents=True, exist_ok=True)
+ # Convert sets back to lists for JSON serialization
+ payload = {
+ "approved": {ch: sorted(list(users)) for ch, users in data.get("approved", {}).items()},
+ "pending": dict(data.get("pending", {})),
+ }
+ _write_text_atomic(path, json.dumps(payload, indent=2, ensure_ascii=False))
+
+
+def _gc_pending(data: dict[str, Any]) -> None:
+ """Remove expired pending entries in-place."""
+ now = time.time()
+ pending: dict[str, Any] = data.get("pending", {})
+ expired = [code for code, info in pending.items() if info.get("expires_at", 0) < now]
+ for code in expired:
+ del pending[code]
+
+
+def generate_code(
+ channel: str,
+ sender_id: str,
+ ttl: int = _TTL_DEFAULT_S,
+) -> str:
+ """Create a new pairing code for *sender_id* on *channel*.
+
+ Returns the code (e.g. ``"ABCD-EFGH"``).
+ """
+ with _LOCK:
+ data = _load()
+ _gc_pending(data)
+ raw = "".join(secrets.choice(_ALPHABET) for _ in range(_CODE_LENGTH))
+ code = f"{raw[:4]}-{raw[4:]}"
+
+ data.setdefault("pending", {})[code] = {
+ "channel": channel,
+ "sender_id": str(sender_id),
+ "created_at": time.time(),
+ "expires_at": time.time() + ttl,
+ }
+ _save(data)
+ logger.info("Generated pairing code %s for %s@%s", code, sender_id, channel)
+ return code
+
+
+def approve_code(code: str) -> tuple[str, str] | None:
+ """Approve a pending pairing code.
+
+ Returns ``(channel, sender_id)`` on success, or ``None`` if the code
+ does not exist or has expired.
+ """
+ with _LOCK:
+ data = _load()
+ _gc_pending(data)
+ pending: dict[str, Any] = data.get("pending", {})
+ info = pending.pop(code, None)
+ if info is None:
+ return None
+ channel = info["channel"]
+ sender_id = str(info["sender_id"])
+ data.setdefault("approved", {}).setdefault(channel, set()).add(sender_id)
+ _save(data)
+ logger.info("Approved pairing code %s for %s@%s", code, sender_id, channel)
+ return channel, sender_id
+
+
+def deny_code(code: str) -> bool:
+ """Reject and discard a pending pairing code.
+
+ Returns ``True`` if the code existed and was removed.
+ """
+ with _LOCK:
+ data = _load()
+ _gc_pending(data)
+ pending: dict[str, Any] = data.get("pending", {})
+ if code in pending:
+ del pending[code]
+ _save(data)
+ logger.info("Denied pairing code %s", code)
+ return True
+ return False
+
+
+def is_approved(channel: str, sender_id: str) -> bool:
+ """Check whether *sender_id* has been approved on *channel*."""
+ with _LOCK:
+ data = _load()
+ approved: dict[str, set[str]] = data.get("approved", {})
+ return str(sender_id) in approved.get(channel, set())
+
+
+def list_pending() -> list[dict[str, Any]]:
+ """Return all non-expired pending pairing requests."""
+ with _LOCK:
+ data = _load()
+ _gc_pending(data)
+ return [
+ {"code": code, **info}
+ for code, info in data.get("pending", {}).items()
+ ]
+
+
+def revoke(channel: str, sender_id: str) -> bool:
+ """Remove an approved sender from *channel*.
+
+ Returns ``True`` if the sender was present and removed.
+ """
+ with _LOCK:
+ data = _load()
+ approved: dict[str, set[str]] = data.get("approved", {})
+ users = approved.get(channel, set())
+ sid = str(sender_id)
+ if sid in users:
+ users.discard(sid)
+ if not users:
+ del approved[channel]
+ _save(data)
+ logger.info("Revoked %s from %s", sid, channel)
+ return True
+ return False
+
+
+def get_approved(channel: str) -> list[str]:
+ """Return all approved sender IDs for *channel*."""
+ with _LOCK:
+ data = _load()
+ return sorted(data.get("approved", {}).get(channel, set()))
+
+
+def format_pairing_reply(code: str) -> str:
+ """Return the pairing-code message sent to unrecognised DM senders."""
+ return (
+ "Hi there! This assistant only responds to approved users.\n\n"
+ f"Your pairing code is: `{code}`\n\n"
+ "To get access, ask the owner to approve this code:\n"
+ f"- In this chat: send `/pairing approve {code}`"
+ )
+
+
+def format_expiry(expires_at: float) -> str:
+ """Return a human-readable expiry string (e.g. ``"120s"`` or ``"expired"``)."""
+ remaining = int(expires_at - time.time())
+ return f"{remaining}s" if remaining > 0 else "expired"
+
+
+def handle_pairing_command(channel: str, subcommand_text: str) -> str:
+ """Execute a pairing subcommand and return the reply text.
+
+ This is a pure function (no side effects other than store mutations)
+ so it can be used from both the CLI and the agent CommandRouter.
+ """
+ parts = subcommand_text.split()
+ sub = parts[0] if parts else "list"
+ arg = parts[1] if len(parts) > 1 else None
+
+ if sub in ("list",):
+ pending = list_pending()
+ if not pending:
+ return "No pending pairing requests."
+ lines = ["Pending pairing requests:"]
+ for item in pending:
+ expiry = format_expiry(item.get("expires_at", 0))
+ lines.append(
+ f"- `{item['code']}` | {item['channel']} | {item['sender_id']} | {expiry}"
+ )
+ return "\n".join(lines)
+
+ elif sub == "approve":
+ if arg is None:
+ return "Usage: `/pairing approve <code>`"
+ result = approve_code(arg)
+ if result is None:
+ return f"Invalid or expired pairing code: `{arg}`"
+ ch, sid = result
+ return f"Approved pairing code `{arg}` — {sid} can now access {ch}"
+
+ elif sub == "deny":
+ if arg is None:
+ return "Usage: `/pairing deny <code>`"
+ if deny_code(arg):
+ return f"Denied pairing code `{arg}`"
+ return f"Pairing code `{arg}` not found or already expired"
+
+ elif sub == "revoke":
+ if len(parts) == 2:
+ return (
+ f"Revoked {arg} from {channel}"
+ if revoke(channel, arg)
+ else f"{arg} was not in the approved list for {channel}"
+ )
+ if len(parts) == 3:
+ return (
+ f"Revoked {parts[2]} from {arg}"
+ if revoke(arg, parts[2])
+ else f"{parts[2]} was not in the approved list for {arg}"
+ )
+ return "Usage: `/pairing revoke <user_id>` or `/pairing revoke <channel> <user_id>`"
+
+ return (
+ "Unknown pairing command.\n"
+ "Usage: `/pairing [list|approve <code>|deny <code>|revoke <user_id>|revoke <channel> <user_id>]`"
+ )
diff --git a/agent/src/channels/qq.py b/agent/src/channels/qq.py
new file mode 100644
index 000000000..c88796c13
--- /dev/null
+++ b/agent/src/channels/qq.py
@@ -0,0 +1,701 @@
+"""QQ channel implementation using botpy SDK.
+
+Inbound:
+- Parse QQ botpy messages (C2C / Group)
+- Download attachments to media dir using chunked streaming write (memory-safe)
+- Publish to the Vibe-Trading bus via BaseChannel._handle_message()
+- Content includes a clear, actionable "Received files:" list with local paths
+
+Outbound:
+- Send attachments (msg.media) first via QQ rich media API (base64 upload + msg_type=7)
+- Then send text (plain or markdown)
+- msg.media supports local paths, file:// paths, and http(s) URLs
+
+Notes:
+- QQ restricts many audio/video formats. We conservatively classify as image vs file.
+- Attachment structures differ across botpy versions; we try multiple field candidates.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import base64
+import mimetypes
+import os
+import re
+import time
+from collections import deque
+from contextlib import suppress
+from pathlib import Path
+from typing import TYPE_CHECKING, Any, Literal
+from urllib.parse import unquote, urlparse
+
+import aiohttp
+import logging; logger = logging.getLogger(__name__)
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from pydantic import BaseModel
+from src.channels.utils import validate_url_target
+# logging_bridge not needed (using stdlib logging)
+
+try:
+ from src.channels.utils import get_media_dir
+except Exception: # pragma: no cover
+ get_media_dir = None # type: ignore
+
+try:
+ import botpy
+ from botpy.http import Route
+
+ QQ_AVAILABLE = True
+except ImportError: # pragma: no cover
+ QQ_AVAILABLE = False
+ botpy = None
+ Route = None
+
+if TYPE_CHECKING:
+ from botpy.message import BaseMessage, C2CMessage, GroupMessage
+ from botpy.types.message import Media
+
+
+# QQ rich media file_type: 1=image, 4=file
+# (2=voice, 3=video are restricted; we only use image vs file)
+QQ_FILE_TYPE_IMAGE = 1
+QQ_FILE_TYPE_FILE = 4
+
+_IMAGE_EXTS = {
+ ".png",
+ ".jpg",
+ ".jpeg",
+ ".gif",
+ ".bmp",
+ ".webp",
+ ".tif",
+ ".tiff",
+ ".ico",
+ ".svg",
+}
+
+# Replace unsafe characters with "_", keep Chinese and common safe punctuation.
+_SAFE_NAME_RE = re.compile(r"[^\w.\-()\[\]()【】\u4e00-\u9fff]+", re.UNICODE)
+
+
+def _sanitize_filename(name: str) -> str:
+ """Sanitize filename to avoid traversal and problematic chars."""
+ name = (name or "").strip()
+ name = Path(name).name
+ name = _SAFE_NAME_RE.sub("_", name).strip("._ ")
+ return name
+
+
+def _is_image_name(name: str) -> bool:
+ return Path(name).suffix.lower() in _IMAGE_EXTS
+
+
+def _guess_send_file_type(filename: str) -> int:
+ """Conservative send type: images -> 1, else -> 4."""
+ ext = Path(filename).suffix.lower()
+ mime, _ = mimetypes.guess_type(filename)
+ if ext in _IMAGE_EXTS or (mime and mime.startswith("image/")):
+ return QQ_FILE_TYPE_IMAGE
+ return QQ_FILE_TYPE_FILE
+
+
+def _make_bot_class(channel: QQChannel) -> type[botpy.Client]:
+ """Create a botpy Client subclass bound to the given channel."""
+ intents = botpy.Intents(public_messages=True, direct_message=True)
+
+ class _Bot(botpy.Client):
+ def __init__(self):
+ # Disable botpy's file log — vibe-trading uses loguru; default "botpy.log" fails on read-only fs
+ super().__init__(intents=intents, ext_handlers=False)
+
+ async def on_ready(self):
+ logger.info("QQ bot ready: {}", self.robot.name)
+
+ async def on_c2c_message_create(self, message: C2CMessage):
+ await channel._on_message(message, is_group=False)
+
+ async def on_group_at_message_create(self, message: GroupMessage):
+ await channel._on_message(message, is_group=True)
+
+ async def on_direct_message_create(self, message):
+ await channel._on_message(message, is_group=False)
+
+ return _Bot
+
+
+class QQConfig(BaseModel):
+ """QQ channel configuration using botpy SDK."""
+
+ enabled: bool = False
+ app_id: str = ""
+ secret: str = ""
+ allow_from: list[str] = Field(default_factory=list)
+ msg_format: Literal["plain", "markdown"] = "plain"
+ ack_message: str = "⏳ Processing..."
+
+ # Optional: directory to save inbound attachments. If empty, use vibe-trading get_media_dir("qq").
+ media_dir: str = ""
+
+ # Download tuning
+ download_chunk_size: int = 1024 * 256 # 256KB
+ download_max_bytes: int = 1024 * 1024 * 200 # 200MB safety limit
+
+
+class QQChannel(BaseChannel):
+ """QQ channel using botpy SDK with WebSocket connection."""
+
+ name = "qq"
+ display_name = "QQ"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return QQConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = QQConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: QQConfig = config
+
+ self._client: botpy.Client | None = None
+ self._http: aiohttp.ClientSession | None = None
+
+ self._processed_ids: deque[str] = deque(maxlen=1000)
+ self._msg_seq: int = 1 # used to avoid QQ API dedup
+ self._chat_type_cache: dict[str, str] = {}
+
+ self._media_root: Path = self._init_media_root()
+
+ # ---------------------------
+ # Lifecycle
+ # ---------------------------
+
+ def _init_media_root(self) -> Path:
+ """Choose a directory for saving inbound attachments."""
+ if self.config.media_dir:
+ root = Path(self.config.media_dir).expanduser()
+ elif get_media_dir:
+ try:
+ root = Path(get_media_dir("qq"))
+ except Exception:
+ root = Path.home() / ".vibe-trading" / "media" / "qq"
+ else:
+ root = Path.home() / ".vibe-trading" / "media" / "qq"
+
+ root.mkdir(parents=True, exist_ok=True)
+ self.logger.info("media directory: {}", str(root))
+ return root
+
+ async def start(self) -> None:
+ """Start the QQ bot with auto-reconnect loop."""
+ # (stdlib logging handles botpy SDK output via propagation)
+ if not QQ_AVAILABLE:
+ self.logger.error("SDK not installed. Run: pip install qq-botpy")
+ return
+
+ if not self.config.app_id or not self.config.secret:
+ self.logger.error("app_id and secret not configured")
+ return
+
+ self._running = True
+ self._http = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=120))
+
+ self._client = _make_bot_class(self)()
+ self.logger.info("bot started (C2C & Group supported)")
+ await self._run_bot()
+
+ async def _run_bot(self) -> None:
+ """Run the bot connection with auto-reconnect."""
+ while self._running:
+ try:
+ await self._client.start(appid=self.config.app_id, secret=self.config.secret)
+ except Exception as e:
+ self.logger.warning("bot error: {}", e)
+ if self._running:
+ self.logger.info("Reconnecting bot in 5 seconds...")
+ await asyncio.sleep(5)
+
+ async def stop(self) -> None:
+ """Stop bot and cleanup resources."""
+ self._running = False
+ if self._client:
+ with suppress(Exception):
+ await self._client.close()
+ self._client = None
+
+ if self._http:
+ with suppress(Exception):
+ await self._http.close()
+ self._http = None
+
+ self.logger.info("bot stopped")
+
+ # ---------------------------
+ # Outbound (send)
+ # ---------------------------
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send attachments first, then text."""
+ try:
+ if not self._client:
+ self.logger.warning("client not initialized")
+ return
+
+ msg_id = msg.metadata.get("message_id")
+ chat_type = self._chat_type_cache.get(msg.chat_id, "c2c")
+ is_group = chat_type == "group"
+
+ # 1) Send media
+ for media_ref in msg.media or []:
+ ok = await self._send_media(
+ chat_id=msg.chat_id,
+ media_ref=media_ref,
+ msg_id=msg_id,
+ is_group=is_group,
+ )
+ if not ok:
+ filename = (
+ os.path.basename(urlparse(media_ref).path)
+ or os.path.basename(media_ref)
+ or "file"
+ )
+ await self._send_text_only(
+ chat_id=msg.chat_id,
+ is_group=is_group,
+ msg_id=msg_id,
+ content=f"[Attachment send failed: {filename}]",
+ )
+
+ # 2) Send text
+ if msg.content and msg.content.strip():
+ await self._send_text_only(
+ chat_id=msg.chat_id,
+ is_group=is_group,
+ msg_id=msg_id,
+ content=msg.content.strip(),
+ )
+ except (aiohttp.ClientError, OSError):
+ # Network / transport errors — propagate so ChannelManager can retry
+ raise
+ except Exception:
+ self.logger.exception("Error sending message to chat_id={}", msg.chat_id)
+
+ async def _send_text_only(
+ self,
+ chat_id: str,
+ is_group: bool,
+ msg_id: str | None,
+ content: str,
+ ) -> None:
+ """Send a plain/markdown text message."""
+ if not self._client:
+ return
+
+ self._msg_seq += 1
+ use_markdown = self.config.msg_format == "markdown"
+ payload: dict[str, Any] = {
+ "msg_type": 2 if use_markdown else 0,
+ "msg_id": msg_id,
+ "msg_seq": self._msg_seq,
+ }
+ if use_markdown:
+ payload["markdown"] = {"content": content}
+ else:
+ payload["content"] = content
+
+ if is_group:
+ await self._client.api.post_group_message(group_openid=chat_id, **payload)
+ else:
+ await self._client.api.post_c2c_message(openid=chat_id, **payload)
+
+ async def _send_media(
+ self,
+ chat_id: str,
+ media_ref: str,
+ msg_id: str | None,
+ is_group: bool,
+ ) -> bool:
+ """Read bytes -> base64 upload -> msg_type=7 send."""
+ if not self._client:
+ return False
+
+ data, filename = await self._read_media_bytes(media_ref)
+ if not data or not filename:
+ return False
+
+ try:
+ file_type = _guess_send_file_type(filename)
+ file_data_b64 = base64.b64encode(data).decode()
+
+ media_obj = await self._post_base64file(
+ chat_id=chat_id,
+ is_group=is_group,
+ file_type=file_type,
+ file_data=file_data_b64,
+ file_name=filename,
+ srv_send_msg=False,
+ )
+ if not media_obj:
+ self.logger.error("media upload failed: empty response")
+ return False
+
+ self._msg_seq += 1
+ if is_group:
+ await self._client.api.post_group_message(
+ group_openid=chat_id,
+ msg_type=7,
+ msg_id=msg_id,
+ msg_seq=self._msg_seq,
+ media=media_obj,
+ )
+ else:
+ await self._client.api.post_c2c_message(
+ openid=chat_id,
+ msg_type=7,
+ msg_id=msg_id,
+ msg_seq=self._msg_seq,
+ media=media_obj,
+ )
+
+ self.logger.info("media sent: {}", filename)
+ return True
+ except (aiohttp.ClientError, OSError) as e:
+ # Network / transport errors — propagate for retry by caller
+ self.logger.warning("send media network error filename={} err={}", filename, e)
+ raise
+ except Exception:
+ # API-level or other non-network errors — return False so send() can fallback
+ self.logger.exception("send media failed filename={}", filename)
+ return False
+
+ async def _read_media_bytes(self, media_ref: str) -> tuple[bytes | None, str | None]:
+ """Read bytes from http(s) or local file path; return (data, filename)."""
+ media_ref = (media_ref or "").strip()
+ if not media_ref:
+ return None, None
+
+ # Local file: plain path or file:// URI
+ if not media_ref.startswith("http://") and not media_ref.startswith("https://"):
+ try:
+ if media_ref.startswith("file://"):
+ parsed = urlparse(media_ref)
+ # Windows: path in netloc; Unix: path in path
+ raw = parsed.path or parsed.netloc
+ local_path = Path(unquote(raw))
+ else:
+ local_path = Path(os.path.expanduser(media_ref))
+
+ if not local_path.is_file():
+ self.logger.warning("outbound media file not found: {}", str(local_path))
+ return None, None
+
+ data = await asyncio.to_thread(local_path.read_bytes)
+ return data, local_path.name
+ except Exception as e:
+ self.logger.warning("outbound media read error ref={} err={}", media_ref, e)
+ return None, None
+
+ # Remote URL
+ ok, err = validate_url_target(media_ref)
+ if not ok:
+ self.logger.warning("outbound media URL validation failed url={} err={}", media_ref, err)
+ return None, None
+
+ if not self._http:
+ self._http = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=120))
+ try:
+ async with self._http.get(media_ref, allow_redirects=True) as resp:
+ if resp.status >= 400:
+ self.logger.warning(
+ "outbound media download failed status={} url={}",
+ resp.status,
+ media_ref,
+ )
+ return None, None
+ data = await resp.read()
+ if not data:
+ return None, None
+ filename = os.path.basename(urlparse(media_ref).path) or "file.bin"
+ return data, filename
+ except Exception as e:
+ self.logger.warning("outbound media download error url={} err={}", media_ref, e)
+ return None, None
+
+ # https://github.com/tencent-connect/botpy/issues/198
+ # https://bot.q.qq.com/wiki/develop/api-v2/server-inter/message/send-receive/rich-media.html
+ async def _post_base64file(
+ self,
+ chat_id: str,
+ is_group: bool,
+ file_type: int,
+ file_data: str,
+ file_name: str | None = None,
+ srv_send_msg: bool = False,
+ ) -> Media:
+ """Upload base64-encoded file and return Media object."""
+ if not self._client:
+ raise RuntimeError("QQ client not initialized")
+
+ if is_group:
+ endpoint = "/v2/groups/{group_openid}/files"
+ id_key = "group_openid"
+ else:
+ endpoint = "/v2/users/{openid}/files"
+ id_key = "openid"
+
+ payload: dict[str, Any] = {
+ id_key: chat_id,
+ "file_type": file_type,
+ "file_data": file_data,
+ "srv_send_msg": srv_send_msg,
+ }
+ # Only pass file_name for non-image types (file_type=4).
+ # Passing file_name for images causes QQ client to render them as
+ # file attachments instead of inline images.
+ if file_type != QQ_FILE_TYPE_IMAGE and file_name:
+ payload["file_name"] = file_name
+
+ route = Route("POST", endpoint, **{id_key: chat_id})
+ result = await self._client.api._http.request(route, json=payload)
+
+ # Extract only the file_info field to avoid extra fields (file_uuid, ttl, etc.)
+ # that may confuse QQ client when sending the media object.
+ if isinstance(result, dict) and "file_info" in result:
+ return {"file_info": result["file_info"]}
+ return result
+
+ # ---------------------------
+ # Inbound (receive)
+ # ---------------------------
+
+ async def _on_message(self, data: C2CMessage | GroupMessage, is_group: bool = False) -> None:
+ """Parse inbound message, download attachments, and publish to the bus."""
+ try:
+ if is_group:
+ chat_id = data.group_openid
+ user_id = data.author.member_openid
+ chat_type = "group"
+ else:
+ chat_id = str(
+ getattr(data.author, "id", None)
+ or getattr(data.author, "user_openid", "unknown")
+ )
+ user_id = chat_id
+ chat_type = "c2c"
+
+ content = (data.content or "").strip()
+
+ if data.id in self._processed_ids:
+ return
+ self._processed_ids.append(data.id)
+ self._chat_type_cache[chat_id] = chat_type
+
+ # Early permission check — avoid attachment downloads and ack side effects
+ # for unauthorized users. C2C messages can receive pairing codes;
+ # group messages remain silently ignored.
+ if not self.is_allowed(user_id):
+ if not is_group:
+ await self._handle_message(
+ sender_id=user_id,
+ chat_id=chat_id,
+ content="",
+ is_dm=True,
+ )
+ return
+
+ # the data used by tests don't contain attachments property
+ # so we use getattr with a default of [] to avoid AttributeError in tests
+ attachments = getattr(data, "attachments", None) or []
+ media_paths, recv_lines, att_meta = await self._handle_attachments(attachments)
+
+ # Compose content that always contains actionable saved paths
+ if recv_lines:
+ tag = (
+ "[Image]"
+ if any(_is_image_name(Path(p).name) for p in media_paths)
+ else "[File]"
+ )
+ file_block = "Received files:\n" + "\n".join(recv_lines)
+ content = (
+ f"{content}\n\n{file_block}".strip() if content else f"{tag}\n{file_block}"
+ )
+
+ if not content and not media_paths:
+ return
+
+ if self.config.ack_message:
+ try:
+ await self._send_text_only(
+ chat_id=chat_id,
+ is_group=is_group,
+ msg_id=data.id,
+ content=self.config.ack_message,
+ )
+ except Exception:
+ self.logger.debug("ack message failed for chat_id={}", chat_id)
+
+ await self._handle_message(
+ sender_id=user_id,
+ chat_id=chat_id,
+ content=content,
+ media=media_paths if media_paths else None,
+ metadata={
+ "message_id": data.id,
+ "attachments": att_meta,
+ },
+ is_dm=not is_group,
+ )
+ except Exception:
+ self.logger.exception("Error handling inbound message id={}", getattr(data, "id", "?"))
+
+ async def _handle_attachments(
+ self,
+ attachments: list[BaseMessage._Attachments],
+ ) -> tuple[list[str], list[str], list[dict[str, Any]]]:
+ """Extract, download (chunked), and format attachments for agent consumption."""
+ media_paths: list[str] = []
+ recv_lines: list[str] = []
+ att_meta: list[dict[str, Any]] = []
+
+ if not attachments:
+ return media_paths, recv_lines, att_meta
+
+ for att in attachments:
+ url = getattr(att, "url", None) or ""
+ filename = getattr(att, "filename", None) or ""
+ ctype = getattr(att, "content_type", None) or ""
+
+ self.logger.info("Downloading file: {}", filename or url)
+ local_path = await self._download_to_media_dir_chunked(url, filename_hint=filename)
+
+ att_meta.append(
+ {
+ "url": url,
+ "filename": filename,
+ "content_type": ctype,
+ "saved_path": local_path,
+ }
+ )
+
+ if local_path:
+ media_paths.append(local_path)
+ shown_name = filename or os.path.basename(local_path)
+ recv_lines.append(f"- {shown_name}\n saved: {local_path}")
+ else:
+ shown_name = filename or url
+ recv_lines.append(f"- {shown_name}\n saved: [download failed]")
+
+ return media_paths, recv_lines, att_meta
+
+ async def _download_to_media_dir_chunked(
+ self,
+ url: str,
+ filename_hint: str = "",
+ ) -> str | None:
+ """Download an inbound attachment using streaming chunk write.
+
+ Uses chunked streaming to avoid loading large files into memory.
+ Enforces a max download size and writes to a .part temp file
+ that is atomically renamed on success.
+ """
+ # Handle protocol-relative URLs (e.g. "//multimedia.nt.qq.com/...")
+ if url.startswith("//"):
+ url = f"https:{url}"
+
+ if not self._http:
+ self._http = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=120))
+
+ safe = _sanitize_filename(filename_hint)
+ ts = int(time.time() * 1000)
+ tmp_path: Path | None = None
+
+ try:
+ async with self._http.get(
+ url,
+ timeout=aiohttp.ClientTimeout(total=120),
+ allow_redirects=True,
+ ) as resp:
+ if resp.status != 200:
+ self.logger.warning("download failed: status={} url={}", resp.status, url)
+ return None
+
+ ctype = (resp.headers.get("Content-Type") or "").lower()
+
+ # Infer extension: url -> filename_hint -> content-type -> fallback
+ ext = Path(urlparse(url).path).suffix
+ if not ext:
+ ext = Path(filename_hint).suffix
+ if not ext:
+ if "png" in ctype:
+ ext = ".png"
+ elif "jpeg" in ctype or "jpg" in ctype:
+ ext = ".jpg"
+ elif "gif" in ctype:
+ ext = ".gif"
+ elif "webp" in ctype:
+ ext = ".webp"
+ elif "pdf" in ctype:
+ ext = ".pdf"
+ else:
+ ext = ".bin"
+
+ if safe:
+ if not Path(safe).suffix:
+ safe = safe + ext
+ filename = safe
+ else:
+ filename = f"qq_file_{ts}{ext}"
+
+ target = self._media_root / filename
+ if target.exists():
+ target = self._media_root / f"{target.stem}_{ts}{target.suffix}"
+
+ tmp_path = target.with_suffix(target.suffix + ".part")
+
+ # Stream write
+ downloaded = 0
+ chunk_size = max(1024, int(self.config.download_chunk_size or 262144))
+ max_bytes = max(
+ 1024 * 1024, int(self.config.download_max_bytes or (200 * 1024 * 1024))
+ )
+
+ def _open_tmp():
+ tmp_path.parent.mkdir(parents=True, exist_ok=True)
+ return open(tmp_path, "wb") # noqa: SIM115
+
+ f = await asyncio.to_thread(_open_tmp)
+ try:
+ async for chunk in resp.content.iter_chunked(chunk_size):
+ if not chunk:
+ continue
+ downloaded += len(chunk)
+ if downloaded > max_bytes:
+ self.logger.warning(
+ "download exceeded max_bytes={} url={} -> abort",
+ max_bytes,
+ url,
+ )
+ return None
+ await asyncio.to_thread(f.write, chunk)
+ finally:
+ await asyncio.to_thread(f.close)
+
+ # Atomic rename
+ await asyncio.to_thread(os.replace, tmp_path, target)
+ tmp_path = None # mark as moved
+ self.logger.info("file saved: {}", str(target))
+ return str(target)
+
+ except Exception:
+ self.logger.exception("download error")
+ return None
+ finally:
+ # Cleanup partial file
+ if tmp_path is not None:
+ with suppress(Exception):
+ tmp_path.unlink(missing_ok=True)
diff --git a/agent/src/channels/registry.py b/agent/src/channels/registry.py
new file mode 100644
index 000000000..323445915
--- /dev/null
+++ b/agent/src/channels/registry.py
@@ -0,0 +1,262 @@
+"""Auto-discovery for built-in channel modules and external plugins."""
+
+from __future__ import annotations
+
+import importlib
+import importlib.util
+import logging
+import pkgutil
+from collections.abc import Mapping
+from dataclasses import dataclass
+from types import ModuleType
+from typing import Any
+from typing import TYPE_CHECKING
+
+if TYPE_CHECKING:
+ from src.channels.base import BaseChannel
+
+logger = logging.getLogger(__name__)
+
+_INTERNAL = frozenset({"base", "bus", "config", "manager", "pairing", "registry", "runtime", "utils"})
+
+_INSTALL_HINTS: dict[str, str] = {
+ "dingtalk": "pip install 'vibe-trading-ai[dingtalk]'",
+ "discord": "pip install 'vibe-trading-ai[discord]'",
+ "email": "No extra Python package required; configure channels.email in the agent config.",
+ "feishu": "pip install 'vibe-trading-ai[feishu]'",
+ "matrix": "pip install 'vibe-trading-ai[matrix]'",
+ "mochat": "pip install 'vibe-trading-ai[mochat]'",
+ "msteams": "pip install 'vibe-trading-ai[msteams]'",
+ "napcat": "pip install 'vibe-trading-ai[napcat]'",
+ "qq": "pip install 'vibe-trading-ai[qq]'",
+ "signal": "No extra Python package required; install and run signal-cli-rest-api separately.",
+ "slack": "pip install 'vibe-trading-ai[slack]'",
+ "telegram": "pip install 'vibe-trading-ai[telegram]'",
+ "wecom": "pip install 'vibe-trading-ai[wecom]'",
+ "weixin": "No extra Python package required; configure channels.weixin in the agent config.",
+ "whatsapp": "pip install 'vibe-trading-ai[whatsapp]'",
+ "websocket": "pip install 'vibe-trading-ai[channels]'",
+}
+
+_AVAILABILITY_FLAGS: dict[str, tuple[str, ...]] = {
+ "dingtalk": ("DINGTALK_AVAILABLE",),
+ "discord": ("DISCORD_AVAILABLE",),
+ "feishu": ("FEISHU_AVAILABLE",),
+ "msteams": ("MSTEAMS_AVAILABLE",),
+ "qq": ("QQ_AVAILABLE",),
+ "wecom": ("WECOM_AVAILABLE",),
+}
+
+_LAZY_IMPORT_PACKAGES: dict[str, tuple[str, ...]] = {
+ "whatsapp": ("neonize",),
+}
+
+
+@dataclass(frozen=True)
+class ChannelAvailability:
+ """Import-time availability for a channel adapter."""
+
+ name: str
+ available: bool
+ display_name: str
+ error: str = ""
+ install_hint: str = ""
+
+ def to_dict(self) -> dict[str, Any]:
+ """Return a JSON-serializable availability payload."""
+ return {
+ "name": self.name,
+ "available": self.available,
+ "display_name": self.display_name,
+ "error": self.error,
+ "install_hint": self.install_hint,
+ }
+
+
+def discover_channel_names() -> list[str]:
+ """Return all built-in channel module names by scanning the package (zero imports)."""
+ import src.channels as pkg
+
+ return [
+ name
+ for _, name, ispkg in pkgutil.iter_modules(pkg.__path__)
+ if name not in _INTERNAL and not ispkg
+ ]
+
+
+def _channel_class_from_module(module: ModuleType, module_name: str) -> type[BaseChannel]:
+ """Return the first BaseChannel subclass defined in *module*."""
+ from src.channels.base import BaseChannel as _Base
+
+ for attr in dir(module):
+ obj = getattr(module, attr)
+ if isinstance(obj, type) and issubclass(obj, _Base) and obj is not _Base:
+ return obj
+ raise ImportError(f"No BaseChannel subclass in src.channels.{module_name}")
+
+
+def _missing_optional_dependency(name: str, module: ModuleType) -> str:
+ """Return a dependency status error for lazily imported adapter SDKs."""
+ for flag in _AVAILABILITY_FLAGS.get(name, ()):
+ if getattr(module, flag, True) is False:
+ return f"missing optional dependency for {name}"
+ missing_packages = [
+ package
+ for package in _LAZY_IMPORT_PACKAGES.get(name, ())
+ if importlib.util.find_spec(package) is None
+ ]
+ if missing_packages:
+ return "missing optional dependency: " + ", ".join(missing_packages)
+ return ""
+
+
+def load_channel_class(module_name: str) -> type[BaseChannel]:
+ """Import *module_name* and return the first BaseChannel subclass found."""
+ mod = importlib.import_module(f"src.channels.{module_name}")
+ return _channel_class_from_module(mod, module_name)
+
+
+def inspect_channel(name: str) -> ChannelAvailability:
+ """Inspect one channel module without raising dependency errors.
+
+ Args:
+ name: Built-in or plugin channel name.
+
+ Returns:
+ Availability metadata that surfaces install guidance for optional SDKs.
+ """
+ try:
+ mod = importlib.import_module(f"src.channels.{name}")
+ cls = _channel_class_from_module(mod, name)
+ display = getattr(cls, "display_name", name)
+ missing = _missing_optional_dependency(name, mod)
+ if missing:
+ return ChannelAvailability(
+ name=name,
+ available=False,
+ display_name=str(display),
+ error=missing,
+ install_hint=_INSTALL_HINTS.get(name, f"pip install 'vibe-trading-ai[{name}]'"),
+ )
+ return ChannelAvailability(name=name, available=True, display_name=str(display))
+ except Exception as exc: # noqa: BLE001 - status API must report every adapter
+ return ChannelAvailability(
+ name=name,
+ available=False,
+ display_name=name.replace("_", " ").title(),
+ error=f"{type(exc).__name__}: {exc}",
+ install_hint=_INSTALL_HINTS.get(name, f"pip install 'vibe-trading-ai[{name}]'"),
+ )
+
+
+def _section_enabled(section: Any) -> bool:
+ if section is None:
+ return False
+ if isinstance(section, Mapping):
+ return bool(section.get("enabled", False))
+ return bool(getattr(section, "enabled", False))
+
+
+def _config_section(config: Any, name: str) -> Any:
+ if isinstance(config, Mapping):
+ return config.get(name)
+ return getattr(config, name, None)
+
+
+def inspect_channels(config: Any | None = None) -> dict[str, dict[str, Any]]:
+ """Inspect all built-in channels and annotate configured/enabled state.
+
+ Args:
+ config: Optional channels config map or model. Extra keys are included
+ so external plugin configs are visible in status output.
+
+ Returns:
+ Mapping from channel name to JSON-serializable status metadata.
+ """
+ names = set(discover_channel_names())
+ if isinstance(config, Mapping):
+ names.update(str(key) for key in config.keys())
+ elif config is not None:
+ names.update(
+ key
+ for key in dir(config)
+ if not key.startswith("_") and key not in {"model_config", "model_fields"}
+ )
+
+ statuses: dict[str, dict[str, Any]] = {}
+ for name in sorted(names):
+ section = _config_section(config, name) if config is not None else None
+ availability = inspect_channel(name).to_dict()
+ availability.update(
+ {
+ "configured": section is not None,
+ "enabled": _section_enabled(section),
+ "loaded": False,
+ "running": False,
+ }
+ )
+ statuses[name] = availability
+ return statuses
+
+
+def discover_plugins(
+ enabled_names: set[str] | None = None,
+) -> dict[str, type[BaseChannel]]:
+ """Discover external channel plugins registered via entry_points."""
+ from importlib.metadata import entry_points
+
+ plugins: dict[str, type[BaseChannel]] = {}
+ for ep in entry_points(group="vibe_trading.channels"):
+ if enabled_names is not None and ep.name not in enabled_names:
+ continue
+ try:
+ cls = ep.load()
+ plugins[ep.name] = cls
+ except Exception:
+ logger.warning("Failed to load channel plugin '%s': %s", ep.name, exc_info=True)
+ return plugins
+
+
+def discover_enabled(
+ enabled_names: set[str],
+ *,
+ _names: list[str] | None = None,
+ _include_all_external: bool = False,
+) -> dict[str, type[BaseChannel]]:
+ """Return channels whose module names are in *enabled_names*.
+
+ Uses cheap ``pkgutil.iter_modules`` to list names, then imports only
+ those that match — skipping the heavy third-party SDK imports of
+ unneeded channels.
+ """
+ names = _names if _names is not None else discover_channel_names()
+ result: dict[str, type[BaseChannel]] = {}
+ for modname in names:
+ if modname not in enabled_names:
+ continue
+ try:
+ result[modname] = load_channel_class(modname)
+ except ImportError:
+ logger.debug("Skipping built-in channel '%s': %s", modname, exc_info=True)
+
+ external = discover_plugins(None if _include_all_external else enabled_names)
+ shadowed = set(external) & set(result)
+ if shadowed:
+ logger.warning("Plugin(s) shadowed by built-in channels (ignored): %s", shadowed)
+ if _include_all_external:
+ result.update({k: v for k, v in external.items() if k not in shadowed})
+ else:
+ result.update(
+ {k: v for k, v in external.items() if k not in shadowed and k in enabled_names}
+ )
+
+ return result
+
+
+def discover_all() -> dict[str, type[BaseChannel]]:
+ """Return all channels: built-in (pkgutil) merged with external (entry_points).
+
+ Built-in channels take priority — an external plugin cannot shadow a built-in name.
+ """
+ names = discover_channel_names()
+ return discover_enabled(set(names), _names=names, _include_all_external=True)
diff --git a/agent/src/channels/runtime.py b/agent/src/channels/runtime.py
new file mode 100644
index 000000000..3c2f52ca6
--- /dev/null
+++ b/agent/src/channels/runtime.py
@@ -0,0 +1,215 @@
+"""IM channel runtime that connects MessageBus traffic to SessionService."""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import logging
+import time
+from contextlib import suppress
+from dataclasses import dataclass
+from pathlib import Path
+from typing import Any
+
+from src.channels.bus.events import InboundMessage, OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.manager import ChannelManager
+from src.channels.pairing import PAIRING_COMMAND_META_KEY, handle_pairing_command
+from src.config.paths import get_data_dir
+from src.session.models import Message, Session
+
+logger = logging.getLogger(__name__)
+
+
+@dataclass
+class ChannelRuntimeConfig:
+ """Runtime controls for IM channel processing."""
+
+ reply_timeout_s: float = 600.0
+ poll_interval_s: float = 0.25
+
+
+class ChannelRuntime:
+ """Route inbound channel messages into Vibe-Trading sessions."""
+
+ def __init__(
+ self,
+ *,
+ bus: MessageBus,
+ session_service: Any,
+ manager: ChannelManager | None,
+ session_map_path: Path | None = None,
+ reply_timeout_s: float = 600.0,
+ poll_interval_s: float = 0.25,
+ ) -> None:
+ self.bus = bus
+ self.session_service = session_service
+ self.manager = manager
+ self.config = ChannelRuntimeConfig(
+ reply_timeout_s=reply_timeout_s,
+ poll_interval_s=poll_interval_s,
+ )
+ self.session_map_path = session_map_path or (get_data_dir() / "channels" / "sessions.json")
+ self._session_map: dict[str, str] = {}
+ self._consumer_task: asyncio.Task[None] | None = None
+ self._manager_task: asyncio.Task[Any] | None = None
+ self._handler_tasks: set[asyncio.Task[None]] = set()
+ self._running = False
+
+ async def start(self, *, start_manager: bool = True) -> None:
+ """Start channel processing and, optionally, platform adapters."""
+ if self._running:
+ return
+ self._session_map = self._load_session_map()
+ self._running = True
+ if start_manager and self.manager is not None:
+ self._manager_task = asyncio.create_task(self.manager.start_all())
+ await asyncio.sleep(0)
+ self._consumer_task = asyncio.create_task(self._consume_loop())
+
+ async def stop(self) -> None:
+ """Stop channel processing and platform adapters."""
+ self._running = False
+ if self._consumer_task is not None:
+ self._consumer_task.cancel()
+ with suppress(asyncio.CancelledError):
+ await self._consumer_task
+ self._consumer_task = None
+ for task in list(self._handler_tasks):
+ task.cancel()
+ for task in list(self._handler_tasks):
+ with suppress(asyncio.CancelledError):
+ await task
+ self._handler_tasks.clear()
+ if self.manager is not None:
+ await self.manager.stop_all()
+ if self._manager_task is not None:
+ with suppress(asyncio.CancelledError):
+ await self._manager_task
+ self._manager_task = None
+
+ def status(self) -> dict[str, Any]:
+ """Return runtime and channel status."""
+ return {
+ "running": self._running,
+ "inbound_queue": self.bus.inbound_size,
+ "outbound_queue": self.bus.outbound_size,
+ "session_count": len(self._session_map),
+ "channels": self.manager.get_status() if self.manager is not None else {},
+ }
+
+ async def _consume_loop(self) -> None:
+ while True:
+ msg = await self.bus.consume_inbound()
+ task = asyncio.create_task(self._handle_inbound(msg))
+ self._handler_tasks.add(task)
+ task.add_done_callback(self._handler_tasks.discard)
+
+ async def _handle_inbound(self, msg: InboundMessage) -> None:
+ try:
+ if self._is_pairing_command(msg.content):
+ reply = handle_pairing_command(msg.channel, msg.content.split(None, 1)[1] if " " in msg.content else "list")
+ await self.bus.publish_outbound(
+ OutboundMessage(
+ channel=msg.channel,
+ chat_id=msg.chat_id,
+ content=reply,
+ metadata={PAIRING_COMMAND_META_KEY: True},
+ )
+ )
+ return
+
+ session_id = self._session_for(msg)
+ result = await self.session_service.send_message(
+ session_id,
+ msg.content,
+ include_shell_tools=False,
+ )
+ attempt_id = result.get("attempt_id") if isinstance(result, dict) else None
+ reply = await self._wait_for_reply(session_id, attempt_id)
+ await self.bus.publish_outbound(
+ OutboundMessage(
+ channel=msg.channel,
+ chat_id=msg.chat_id,
+ content=reply.content,
+ metadata={
+ "_channel_runtime": True,
+ "attempt_id": attempt_id,
+ "session_id": session_id,
+ },
+ )
+ )
+ except asyncio.CancelledError:
+ raise
+ except Exception as exc: # noqa: BLE001 - channel errors must surface to users
+ logger.exception("Channel runtime failed for %s:%s", msg.channel, msg.chat_id)
+ await self.bus.publish_outbound(
+ OutboundMessage(
+ channel=msg.channel,
+ chat_id=msg.chat_id,
+ content=f"Channel runtime error: {type(exc).__name__}: {exc}",
+ metadata={"_channel_runtime": True, "error": True},
+ )
+ )
+
+ def _session_for(self, msg: InboundMessage) -> str:
+ key = msg.session_key
+ existing = self._session_map.get(key)
+ if existing:
+ return existing
+ session = self.session_service.create_session(
+ title=f"{msg.channel}:{msg.chat_id}",
+ config={"channel": msg.channel, "channel_chat_id": msg.chat_id},
+ )
+ session_id = _session_id(session)
+ self._session_map[key] = session_id
+ self._save_session_map()
+ return session_id
+
+ async def _wait_for_reply(self, session_id: str, attempt_id: str | None) -> Message:
+ deadline = time.monotonic() + self.config.reply_timeout_s
+ last_assistant: Message | None = None
+ while time.monotonic() < deadline:
+ messages = self.session_service.get_messages(session_id, limit=200)
+ for message in reversed(messages):
+ if message.role != "assistant":
+ continue
+ if attempt_id and message.linked_attempt_id != attempt_id:
+ if last_assistant is None:
+ last_assistant = message
+ continue
+ return message
+ await asyncio.sleep(self.config.poll_interval_s)
+ if last_assistant is not None:
+ return last_assistant
+ raise TimeoutError("timed out waiting for assistant reply")
+
+ def _load_session_map(self) -> dict[str, str]:
+ try:
+ data = json.loads(self.session_map_path.read_text(encoding="utf-8"))
+ except FileNotFoundError:
+ return {}
+ except (OSError, json.JSONDecodeError):
+ logger.warning("Ignoring invalid channel session map at %s", self.session_map_path)
+ return {}
+ if not isinstance(data, dict):
+ return {}
+ return {str(key): str(value) for key, value in data.items() if value}
+
+ def _save_session_map(self) -> None:
+ self.session_map_path.parent.mkdir(parents=True, exist_ok=True)
+ tmp = self.session_map_path.with_suffix(self.session_map_path.suffix + ".tmp")
+ tmp.write_text(json.dumps(self._session_map, indent=2, ensure_ascii=False), encoding="utf-8")
+ tmp.replace(self.session_map_path)
+
+ @staticmethod
+ def _is_pairing_command(content: str) -> bool:
+ return content.strip() == "/pairing" or content.strip().startswith("/pairing ")
+
+
+def _session_id(session: Session | dict[str, Any] | Any) -> str:
+ if isinstance(session, Session):
+ return session.session_id
+ if isinstance(session, dict):
+ return str(session["session_id"])
+ return str(getattr(session, "session_id"))
diff --git a/agent/src/channels/signal.py b/agent/src/channels/signal.py
new file mode 100644
index 000000000..e56cfd8e1
--- /dev/null
+++ b/agent/src/channels/signal.py
@@ -0,0 +1,1402 @@
+"""Signal channel implementation using signal-cli daemon JSON-RPC interface."""
+
+from __future__ import annotations
+
+import asyncio
+import json
+import re
+import shutil
+import unicodedata
+from collections import deque
+from collections.abc import AsyncIterator, Callable
+from contextlib import asynccontextmanager
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any
+
+import httpx
+from pydantic import Field, computed_field, field_validator
+
+from src.channels.bus.events import InboundMessage, OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.pairing import is_approved
+from src.channels.utils import safe_filename, split_message
+
+
+@dataclass
+class _Run:
+ text: str
+ styles: frozenset[str] = field(default_factory=frozenset)
+ opaque: bool = False # code / table content — skip further pattern processing
+
+
+_SIG_CODE_BLOCK_RE = re.compile(r"```(?:\w+)?\n?([\s\S]*?)```")
+_SIG_INLINE_CODE_RE = re.compile(r"`([^`\n]+)`")
+_SIG_HEADER_RE = re.compile(r"^#{1,6}\s+(.+)$", re.MULTILINE)
+_SIG_BLOCKQUOTE_RE = re.compile(r"^>\s*(.*)$", re.MULTILINE)
+_SIG_BULLET_RE = re.compile(r"^[-*]\s+", re.MULTILINE)
+_SIG_OLIST_RE = re.compile(r"^(\d+)\.\s+", re.MULTILINE)
+_SIG_LINK_RE = re.compile(r"\[([^\]]+)\]\(([^)]+)\)")
+_SIG_BOLD_RE = re.compile(r"\*\*(.+?)\*\*|__(.+?)__", re.DOTALL)
+_SIG_ITALIC_RE = re.compile(
+ r"(?<!\*)\*([^*\n]+)\*(?!\*)|(?<![a-zA-Z0-9_])_([^_\n]+)_(?![a-zA-Z0-9_])"
+)
+_SIG_STRIKE_RE = re.compile(r"~~(.+?)~~|(?<![~\w])~([^~\n]+)~(?![~\w])", re.DOTALL)
+_SIG_TOKEN_RE = re.compile(r"\x00C(\d+)\x00")
+
+# Patterns used to strip inline markdown when rendering table cells as plain
+# text. Defined separately from the styling regexes above because the cell
+# stripper needs a fixed, narrow subset (no single-asterisk italic, no
+# single-tilde strikethrough) and benefits from each pattern's group 1 being
+# the content directly.
+_SIG_CELL_STRIP_PATTERNS: tuple[tuple[re.Pattern, str], ...] = (
+ (re.compile(r"\*\*(.+?)\*\*"), r"\1"),
+ (re.compile(r"__(.+?)__"), r"\1"),
+ (re.compile(r"~~(.+?)~~"), r"\1"),
+ (re.compile(r"`([^`]+)`"), r"\1"),
+)
+
+
+def _utf16_len(s: str) -> int:
+ """UTF-16 code-unit length, matching Signal BodyRange semantics."""
+ return len(s.encode("utf-16-le")) // 2
+
+
+def _sig_strip_cell(s: str) -> str:
+ """Strip inline markdown from a table cell for plain-text rendering."""
+ for pattern, repl in _SIG_CELL_STRIP_PATTERNS:
+ s = pattern.sub(repl, s)
+ return s.strip()
+
+
+def _sig_render_table(table_lines: list[str]) -> str:
+ """Render a markdown pipe-table as fixed-width plain text."""
+
+ def dw(s: str) -> int:
+ return sum(2 if unicodedata.east_asian_width(c) in ("W", "F") else 1 for c in s)
+
+ rows: list[list[str]] = []
+ has_sep = False
+ for line in table_lines:
+ cells = [_sig_strip_cell(c) for c in line.strip().strip("|").split("|")]
+ if all(re.match(r"^:?-+:?$", c) for c in cells if c):
+ has_sep = True
+ continue
+ rows.append(cells)
+ if not rows or not has_sep:
+ return "\n".join(table_lines)
+
+ ncols = max(len(r) for r in rows)
+ for r in rows:
+ r.extend([""] * (ncols - len(r)))
+ widths = [max(dw(r[c]) for r in rows) for c in range(ncols)]
+
+ def dr(cells: list[str]) -> str:
+ return " ".join(f"{c}{' ' * (w - dw(c))}" for c, w in zip(cells, widths))
+
+ out = [dr(rows[0])]
+ out.append(" ".join("─" * w for w in widths))
+ for row in rows[1:]:
+ out.append(dr(row))
+ return "\n".join(out)
+
+
+def _markdown_to_signal(text: str) -> tuple[str, list[str]]:
+ """Convert markdown text to Signal plain text + textStyle ranges.
+
+ Returns ``(plain_text, text_styles)`` where ``text_styles`` are
+ ``"start:length:STYLE"`` strings for the signal-cli ``textStyle`` parameter.
+ """
+ if not text:
+ return text, []
+
+ # Phase 1 (text-level): extract code blocks and tables with placeholder tokens
+ # so they're protected from inline-style processing.
+ protected: list[str] = []
+
+ def save_code(m: re.Match) -> str:
+ protected.append(m.group(1))
+ return f"\x00C{len(protected) - 1}\x00"
+
+ text = _SIG_CODE_BLOCK_RE.sub(save_code, text)
+
+ # Detect and render pipe-tables line by line.
+ lines = text.split("\n")
+ rebuilt: list[str] = []
+ i = 0
+ while i < len(lines):
+ if re.match(r"^\s*\|.+\|", lines[i]):
+ tbl: list[str] = []
+ while i < len(lines) and re.match(r"^\s*\|.+\|", lines[i]):
+ tbl.append(lines[i])
+ i += 1
+ rendered = _sig_render_table(tbl)
+ if rendered != "\n".join(tbl):
+ protected.append(rendered)
+ rebuilt.append(f"\x00C{len(protected) - 1}\x00")
+ else:
+ rebuilt.extend(tbl)
+ else:
+ rebuilt.append(lines[i])
+ i += 1
+ text = "\n".join(rebuilt)
+
+ # Phase 2 (run-based): process inline patterns.
+ runs: list[_Run] = [_Run(text)]
+
+ def transform(
+ pattern: re.Pattern,
+ make_runs: Callable[[re.Match, frozenset[str]], list[_Run]],
+ ) -> None:
+ new_runs: list[_Run] = []
+ for run in runs:
+ if run.opaque:
+ new_runs.append(run)
+ continue
+ pos = 0
+ for m in pattern.finditer(run.text):
+ if m.start() > pos:
+ new_runs.append(_Run(run.text[pos : m.start()], run.styles))
+ new_runs.extend(make_runs(m, run.styles))
+ pos = m.end()
+ if pos < len(run.text):
+ new_runs.append(_Run(run.text[pos:], run.styles))
+ runs[:] = new_runs
+
+ # Restore code/table placeholders as opaque MONOSPACE runs.
+ transform(
+ _SIG_TOKEN_RE,
+ lambda m, s: [_Run(protected[int(m.group(1))], s | {"MONOSPACE"}, opaque=True)],
+ )
+
+ # Inline code (opaque).
+ transform(_SIG_INLINE_CODE_RE, lambda m, s: [_Run(m.group(1), s | {"MONOSPACE"}, opaque=True)])
+
+ # Headers → bold plain text.
+ transform(_SIG_HEADER_RE, lambda m, s: [_Run(m.group(1), s | {"BOLD"})])
+
+ # Blockquotes → strip marker.
+ transform(_SIG_BLOCKQUOTE_RE, lambda m, s: [_Run(m.group(1), s)])
+
+ # Bullet lists → bullet character.
+ transform(_SIG_BULLET_RE, lambda m, s: [_Run("• ", s)])
+
+ # Numbered lists → normalize spacing.
+ transform(_SIG_OLIST_RE, lambda m, s: [_Run(m.group(1) + ". ", s)])
+
+ # Links → "text (url)" or bare url when text equals url.
+ def _link_runs(m: re.Match, s: frozenset) -> list[_Run]:
+ link_text, url = m.group(1), m.group(2)
+
+ def _norm(u: str) -> str:
+ return re.sub(r"^https?://(www\.)?", "", u).rstrip("/").lower()
+
+ if _norm(url) == _norm(link_text):
+ return [_Run(url, s)]
+ return [_Run(f"{link_text} ({url})", s)]
+
+ transform(_SIG_LINK_RE, _link_runs)
+
+ # Bold (before italic so ** doesn't interfere).
+ transform(_SIG_BOLD_RE, lambda m, s: [_Run(m.group(1) or m.group(2), s | {"BOLD"})])
+
+ # Italic (single * or _).
+ transform(_SIG_ITALIC_RE, lambda m, s: [_Run(m.group(1) or m.group(2), s | {"ITALIC"})])
+
+ # Strikethrough: ~~text~~ (standard) or ~text~ (single-tilde variant).
+ transform(_SIG_STRIKE_RE, lambda m, s: [_Run(m.group(1) or m.group(2), s | {"STRIKETHROUGH"})])
+
+ # Phase 3: assemble output. Offsets and lengths are emitted in UTF-16 code
+ # units because Signal's BodyRange (via signal-cli's textStyle) interprets
+ # them as such; Python's len() counts code points, which would shift ranges
+ # left by 1 unit per non-BMP character preceding them.
+ plain_text = ""
+ text_styles: list[str] = []
+ utf16_offset = 0
+ for run in runs:
+ if not run.text:
+ continue
+ plain_text += run.text
+ start = utf16_offset
+ length = _utf16_len(run.text)
+ utf16_offset += length
+ for style in sorted(run.styles):
+ text_styles.append(f"{start}:{length}:{style}")
+
+ return plain_text, text_styles
+
+
+def _partition_styles(
+ plain_text: str, chunks: list[str], text_styles: list[str]
+) -> list[list[str]]:
+ """Partition Signal textStyle ranges across message chunks.
+
+ ``split_message`` slices ``plain_text`` into pieces (optionally trimming
+ whitespace at the boundaries), but the style ranges produced by
+ ``_markdown_to_signal`` are expressed in UTF-16 offsets relative to the
+ full ``plain_text``. This redistributes them per chunk with offsets
+ rebased to each chunk's start. Ranges that span a boundary are split
+ across the chunks they touch; ranges that fall entirely in trimmed
+ whitespace are dropped.
+ """
+ if not chunks:
+ return []
+ if not text_styles:
+ return [[] for _ in chunks]
+
+ # Locate each chunk's UTF-16 start in plain_text. split_message lstrips at
+ # boundaries (but not before the first chunk), so we skip whitespace
+ # between chunks to mirror that.
+ chunk_ranges: list[tuple[int, int]] = []
+ cursor = 0 # Python codepoint cursor in plain_text
+ for i, chunk in enumerate(chunks):
+ if i > 0:
+ while cursor < len(plain_text) and plain_text[cursor].isspace():
+ cursor += 1
+ utf16_start = _utf16_len(plain_text[:cursor])
+ utf16_end = utf16_start + _utf16_len(chunk)
+ chunk_ranges.append((utf16_start, utf16_end))
+ cursor += len(chunk)
+
+ result: list[list[str]] = [[] for _ in chunks]
+ for entry in text_styles:
+ s, ln, style = entry.split(":", 2)
+ r_start = int(s)
+ r_end = r_start + int(ln)
+ for i, (c_start, c_end) in enumerate(chunk_ranges):
+ if r_end <= c_start or r_start >= c_end:
+ continue
+ new_start = max(r_start, c_start) - c_start
+ new_end = min(r_end, c_end) - c_start
+ new_length = new_end - new_start
+ if new_length > 0:
+ result[i].append(f"{new_start}:{new_length}:{style}")
+ return result
+
+
+class SignalDMConfig(BaseModel):
+ """Signal DM policy configuration."""
+
+ enabled: bool = False
+ policy: str = "allowlist" # "open" or "allowlist"
+ allow_from: list[str] = Field(default_factory=list) # Allowed phone numbers/UUIDs
+
+
+class SignalGroupConfig(BaseModel):
+ """Signal group policy configuration."""
+
+ enabled: bool = False
+ policy: str = "allowlist" # "open" or "allowlist" - which groups to operate in
+ allow_from: list[str] = Field(default_factory=list) # Allowed group IDs if allowlist policy
+ require_mention: bool = True # Whether bot must be mentioned to respond
+
+
+class SignalConfig(BaseModel):
+ """Signal channel configuration using signal-cli daemon (HTTP mode with -a flag only)."""
+
+ enabled: bool = False
+ phone_number: str = "" # Your Signal phone number (e.g., "+1234567890")
+ daemon_host: str = "localhost"
+ daemon_port: int = 8080
+ group_message_buffer_size: int = 20 # Number of recent group messages to keep for context
+ # Override the directory signal-cli writes inbound attachments to. When
+ # None, defaults to ~/.local/share/signal-cli/attachments (the daemon's
+ # platform default on Linux). Set this if the daemon is running with a
+ # custom XDG_DATA_HOME or on macOS/Windows where the default path differs.
+ attachments_dir: str | None = None
+ dm: SignalDMConfig = Field(default_factory=SignalDMConfig)
+ group: SignalGroupConfig = Field(default_factory=SignalGroupConfig)
+
+ @field_validator("group_message_buffer_size")
+ @classmethod
+ def _validate_buffer_size(cls, v: int) -> int:
+ if v <= 0:
+ raise ValueError("group_message_buffer_size must be > 0")
+ return v
+
+ @computed_field # type: ignore[prop-decorator]
+ @property
+ def allow_from(self) -> list[str]:
+ """Aggregate allowlist for the base-class is_allowed() check.
+
+ Returns the union of dm.allow_from and group.allow_from so the base
+ channel gate sees a populated list when either sub-policy is configured.
+ A ``"*"`` wildcard in either sub-list propagates to allow all.
+ """
+ return list(dict.fromkeys(self.dm.allow_from + self.group.allow_from))
+
+
+class SignalChannel(BaseChannel):
+ """
+ Signal channel using signal-cli daemon via HTTP JSON-RPC interface.
+
+ Requires signal-cli daemon in HTTP mode:
+ - signal-cli -a +1234567890 daemon --http localhost:8080
+
+ See https://github.com/AsamK/signal-cli for setup instructions.
+ """
+
+ name = "signal"
+ display_name = "Signal"
+ _TYPING_REFRESH_SECONDS = 10.0
+ _MAX_MESSAGE_LEN = 64_000 # signal-cli practical limit (protocol max ~64 KB)
+ _HTTP_TIMEOUT_SECONDS = 60.0
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return SignalConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: SignalConfig, bus: MessageBus):
+ if isinstance(config, dict):
+ config = SignalConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: SignalConfig = config
+ self._http: httpx.AsyncClient | None = None
+ self._request_id = 0
+ self._sse_task: asyncio.Task | None = None
+ self._typing_tasks: dict[str, asyncio.Task] = {}
+ self._typing_uuid_warnings: set[str] = set()
+ self._account_id_aliases: set[str] = set()
+ self._remember_account_id_alias(self.config.phone_number)
+
+ # Rolling message buffer for group context (group_id -> deque of messages)
+ # Each message is a dict with: sender_name, sender_number, content, timestamp
+ self._group_buffers: dict[str, deque] = {}
+
+ def is_allowed(self, sender_id: str) -> bool:
+ """Override base check to normalize and split pipe-joined identifiers.
+
+ ``sender_id`` from Signal is the pipe-joined composite produced by
+ ``_collect_sender_id_parts``; allow_from entries may be single
+ identifiers or composites and may use the ``+`` prefix variant or
+ not. Delegates to ``_sender_matches_allowlist`` so the base gate
+ matches the per-policy DM gate.
+ """
+ allow_list = self.config.allow_from
+ if "*" in allow_list:
+ return True
+ if self._sender_matches_allowlist(sender_id, allow_list):
+ return True
+ if self._sender_approved_via_pairing(sender_id):
+ return True
+ if not allow_list:
+ self.logger.warning("allow_from is empty — all access denied")
+ return False
+
+ def _sender_approved_via_pairing(self, sender_id: str) -> bool:
+ """Return True if any normalized variant of sender_id is in the pairing store.
+
+ Pairing approval may be recorded under any of the identifier forms
+ signal exposes (phone with/without ``+``, UUID, ACI), so we check
+ each part of the pipe-joined composite against ``is_approved``.
+ """
+ for part in str(sender_id).split("|"):
+ for variant in self._normalize_signal_id(part):
+ if is_approved(self.name, variant):
+ return True
+ return False
+
+ async def _handle_message(
+ self,
+ sender_id: str,
+ chat_id: str,
+ content: str,
+ media: list[str] | None = None,
+ metadata: dict[str, Any] | None = None,
+ session_key: str | None = None,
+ is_dm: bool = False,
+ ) -> None:
+ """Handle an inbound message whose policy has already been checked.
+
+ ``_check_inbound_policy`` is the authoritative gate for DM/group
+ access, so we skip the base-class ``is_allowed()`` check and publish
+ directly to the bus. The denied-DM pairing path calls
+ ``super()._handle_message`` instead, which goes through
+ ``is_allowed`` and issues a pairing code.
+ """
+ meta = metadata or {}
+ if self.supports_streaming:
+ meta = {**meta, "_wants_stream": True}
+ await self.bus.publish_inbound(
+ InboundMessage(
+ channel=self.name,
+ sender_id=str(sender_id),
+ chat_id=str(chat_id),
+ content=content,
+ media=media or [],
+ metadata=meta,
+ session_key_override=session_key,
+ )
+ )
+
+ async def start(self) -> None:
+ """Start the Signal channel and connect to signal-cli daemon."""
+ if not self.config.phone_number:
+ self.logger.error("Signal account not configured")
+ return
+
+ self._running = True
+ await self._start_http_mode()
+
+ async def _start_http_mode(self) -> None:
+ """Start Signal channel using Server-Sent Events for receiving messages."""
+ base_url = f"http://{self.config.daemon_host}:{self.config.daemon_port}"
+ reconnect_delay_s = 1.0
+ max_reconnect_delay_s = 30.0
+
+ while self._running:
+ try:
+ self.logger.info("Connecting to signal-cli daemon at {}...", base_url)
+
+ # Create HTTP client
+ self._http = httpx.AsyncClient(
+ timeout=self._HTTP_TIMEOUT_SECONDS, base_url=base_url
+ )
+
+ # Test connection
+ try:
+ response = await self._http.get("/api/v1/check")
+ if response.status_code == 200:
+ self.logger.info("Connected to signal-cli daemon")
+ else:
+ raise ConnectionRefusedError(
+ f"signal-cli daemon check returned status {response.status_code}"
+ )
+ except Exception as e:
+ raise ConnectionRefusedError(f"signal-cli daemon not responding: {e}")
+
+ # Reset reconnect delay after successful connection check.
+ reconnect_delay_s = 1.0
+
+ # Ensure account-level typing indicators are enabled.
+ await self._ensure_typing_indicators_enabled()
+
+ # Start SSE receiver and supervise it. If it exits while we're still
+ # running, treat it as a disconnect and reconnect.
+ self._sse_task = asyncio.create_task(self._sse_receive_loop())
+ await self._sse_task
+ if self._running:
+ raise ConnectionError("Signal SSE stream ended unexpectedly")
+
+ except asyncio.CancelledError:
+ break
+ except ConnectionRefusedError as e:
+ self.logger.error(
+ "{}. Make sure signal-cli daemon is running: "
+ "signal-cli -a {} daemon --http {}:{}",
+ e,
+ self.config.phone_number,
+ self.config.daemon_host,
+ self.config.daemon_port,
+ )
+ except Exception as e:
+ self.logger.error("Signal channel error: {}", e)
+ finally:
+ if self._sse_task:
+ if not self._sse_task.done():
+ self._sse_task.cancel()
+ try:
+ await self._sse_task
+ except asyncio.CancelledError:
+ pass
+ except Exception:
+ pass
+ self._sse_task = None
+ if self._http:
+ await self._http.aclose()
+ self._http = None
+
+ if self._running:
+ self.logger.info(
+ "Reconnecting to signal-cli daemon in {:.0f} seconds...", reconnect_delay_s
+ )
+ await asyncio.sleep(reconnect_delay_s)
+ reconnect_delay_s = min(reconnect_delay_s * 2, max_reconnect_delay_s)
+
+ async def stop(self) -> None:
+ """Stop the Signal channel."""
+ self._running = False
+
+ # Stop SSE task
+ if self._sse_task:
+ self._sse_task.cancel()
+ try:
+ await self._sse_task
+ except asyncio.CancelledError:
+ pass
+
+ # Cancel active typing indicators
+ for chat_id in list(self._typing_tasks):
+ await self._stop_typing(chat_id)
+
+ # Close HTTP client
+ if self._http:
+ await self._http.aclose()
+ self._http = None
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through Signal."""
+ is_progress_message = bool(msg.metadata.get("_progress"))
+ try:
+ plain_text, text_styles = _markdown_to_signal(msg.content)
+ if not plain_text and not msg.media:
+ return
+ recipient_params = self._recipient_params(msg.chat_id)
+
+ chunks = split_message(plain_text, self._MAX_MESSAGE_LEN) if plain_text else [""]
+ chunk_styles = _partition_styles(plain_text, chunks, text_styles)
+ for i, chunk in enumerate(chunks):
+ params: dict[str, Any] = {"message": chunk}
+ if chunk_styles[i]:
+ params["textStyle"] = chunk_styles[i]
+ params.update(recipient_params)
+ if msg.media and i == 0:
+ params["attachments"] = msg.media
+
+ response = await self._send_request("send", params)
+
+ if "error" in response:
+ self.logger.error("Error sending Signal message: {}", response['error'])
+ raise RuntimeError(f"signal-cli send failed: {response['error']}")
+ else:
+ self.logger.debug(
+ f"Signal message sent, timestamp: {response.get('result', {}).get('timestamp')}"
+ )
+
+ except Exception:
+ self.logger.exception("Error sending Signal message")
+ raise
+ finally:
+ # Keep typing active across progress updates; stop on the final reply.
+ if not is_progress_message:
+ # Avoid immediate START->STOP for fast responses, which can be invisible
+ # in some Signal clients. Let indicator expire naturally (~15s).
+ await self._stop_typing(msg.chat_id, send_stop=False)
+
+ async def _sse_receive_loop(self) -> None:
+ """Receive messages via Server-Sent Events (HTTP mode)."""
+ if not self._http:
+ raise RuntimeError("HTTP client not initialized for Signal SSE stream")
+
+ self.logger.info("Started Signal message receive loop (SSE)")
+
+ try:
+ async with self._http.stream("GET", "/api/v1/events") as response:
+ if response.status_code != 200:
+ raise ConnectionError(
+ f"SSE connection failed with status {response.status_code}"
+ )
+
+ self.logger.info("Subscribed to Signal messages via SSE")
+
+ # Buffer for accumulating SSE data across multiple lines
+ event_buffer = []
+
+ async for line in response.aiter_lines():
+ if not self._running:
+ break
+
+ # Debug: log raw SSE lines (except keepalive pings)
+ if line and line != ":":
+ self.logger.debug("SSE line received: {}", line[:200])
+
+ # SSE format handling
+ if isinstance(line, str):
+ # Empty line signals end of event
+ if not line or line == ":":
+ if event_buffer:
+ # Try to parse the accumulated data
+ data_str = ""
+ try:
+ data_str = "\n".join(event_buffer)
+ data = json.loads(data_str)
+ self.logger.debug("SSE event parsed: {}", data)
+ await self._handle_receive_notification(data)
+ except json.JSONDecodeError as e:
+ self.logger.warning(
+ "Invalid JSON in SSE buffer: {}, data: {}",
+ e,
+ data_str[:200],
+ )
+ finally:
+ event_buffer = []
+
+ # "data:" line - accumulate it
+ elif line.startswith("data:"):
+ # SSE spec: strip one optional leading space after "data:".
+ event_buffer.append(line[6:] if line[5:6] == " " else line[5:])
+
+ # "event:" line - just log it (we only care about data)
+ elif line.startswith("event:"):
+ pass # Ignore event type for now
+
+ if self._running:
+ raise ConnectionError("Signal SSE stream closed by remote endpoint")
+
+ except asyncio.CancelledError:
+ self.logger.info("SSE receive loop cancelled")
+ raise
+ except Exception as e:
+ self.logger.error("Error in SSE receive loop: {}", e)
+ raise
+
+ @asynccontextmanager
+ async def _safe_handle(self, action: str, payload: Any = None) -> AsyncIterator[None]:
+ """Swallow and log any exception from a top-level handler block.
+
+ Logs `self.logger.error` with the action name, the exception, and a
+ bounded ``repr`` of the offending payload so the offending input is
+ recoverable from logs without having to correlate by timestamp.
+ """
+ try:
+ yield
+ except Exception as e:
+ snippet = repr(payload)[:200] if payload is not None else ""
+ text = f"Error in {action}: {e}"
+ if snippet:
+ text += f" | payload={snippet}"
+ self.logger.opt(exception=True).error(text)
+
+ async def _handle_receive_notification(self, params: dict[str, Any]) -> None:
+ """Handle incoming message notification from signal-cli."""
+ self.logger.debug("_handle_receive_notification called with: {}", params)
+ async with self._safe_handle("receive notification", params):
+ # Extract envelope from SSE notification: {"envelope": {...}}
+ envelope = params.get("envelope", {})
+
+ self.logger.debug("Extracted envelope: {}", envelope)
+
+ if not envelope:
+ self.logger.debug("No envelope found in params")
+ return
+
+ # Extract sender information
+ sender_parts = self._collect_sender_id_parts(envelope)
+ source_name = envelope.get("sourceName")
+
+ if not sender_parts:
+ self.logger.debug("Received message without source, skipping")
+ return
+
+ sender_number = self._primary_sender_id(sender_parts)
+ sender_id = "|".join(sender_parts)
+
+ # Keep aliases of the bot account for robust mention matching.
+ if any(self._id_matches_account(part) for part in sender_parts):
+ for part in sender_parts:
+ self._remember_account_id_alias(part)
+
+ # Check different message types
+ data_message = envelope.get("dataMessage")
+ sync_message = envelope.get("syncMessage")
+ typing_message = envelope.get("typingMessage")
+ receipt_message = envelope.get("receiptMessage")
+
+ # Ignore receipt messages (delivery/read receipts)
+ if receipt_message:
+ return
+
+ # Handle data messages (incoming messages from others)
+ if data_message:
+ await self._handle_data_message(sender_id, sender_number, data_message, source_name)
+
+ # Handle sync messages (messages sent from another device)
+ elif sync_message and sync_message.get("sentMessage"):
+ sent_msg = sync_message["sentMessage"]
+ destination = sent_msg.get("destination") or sent_msg.get("destinationNumber")
+ if destination:
+ self.logger.debug(
+ "Sync message sent to {}: {}", destination, sent_msg.get("message", "")[:50]
+ )
+
+ # Handle typing indicators (silently ignore)
+ elif typing_message:
+ pass # Ignore typing indicators
+
+ async def _handle_data_message(
+ self,
+ sender_id: str,
+ sender_number: str,
+ data_message: dict[str, Any],
+ sender_name: str | None,
+ ) -> None:
+ """Handle a data message (text, attachments, etc.)."""
+ message_text = data_message.get("message") or ""
+ attachments = data_message.get("attachments", [])
+ mentions = data_message.get("mentions", [])
+ timestamp = data_message.get("timestamp")
+
+ self.logger.info(
+ "Data message from {}: groupInfo={}, groupV2={}, keys={}",
+ sender_number,
+ data_message.get("groupInfo"),
+ data_message.get("groupV2"),
+ list(data_message.keys()),
+ )
+
+ if data_message.get("reaction"):
+ self.logger.debug(
+ "Ignoring reaction message from {}: {}", sender_number, data_message["reaction"]
+ )
+ return
+ if not message_text and not attachments:
+ self.logger.debug("Ignoring empty message from {}", sender_number)
+ return
+
+ group_info = data_message.get("groupInfo")
+ group_v2 = data_message.get("groupV2")
+ is_group_message = group_info is not None or group_v2 is not None
+ group_id = self._extract_group_id(group_info, group_v2)
+
+ allowed, chat_id = self._check_inbound_policy(
+ sender_id=sender_id,
+ sender_number=sender_number,
+ group_id=group_id,
+ is_group_message=is_group_message,
+ message_text=message_text,
+ mentions=mentions,
+ sender_name=sender_name,
+ timestamp=timestamp,
+ )
+ if not allowed:
+ # Mirror Slack: let denied DMs reach the base-class
+ # _handle_message so it can reply with a pairing code.
+ # Group denials stay dropped.
+ if not is_group_message and self.config.dm.enabled:
+ await super()._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content="",
+ is_dm=True,
+ )
+ return
+
+ content, media_paths = self._assemble_inbound_content(
+ sender_name=sender_name,
+ sender_number=sender_number,
+ message_text=message_text,
+ attachments=attachments,
+ mentions=mentions,
+ is_group_message=is_group_message,
+ chat_id=chat_id,
+ )
+
+ self.logger.debug("Signal message from {}: {}...", sender_number, content[:50])
+
+ await self._start_typing(chat_id)
+ try:
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content=content,
+ media=media_paths,
+ metadata={
+ "timestamp": timestamp,
+ "sender_name": sender_name,
+ "sender_number": sender_number,
+ "is_group": is_group_message,
+ "group_id": group_id,
+ },
+ is_dm=not is_group_message,
+ )
+ except Exception:
+ await self._stop_typing(chat_id)
+ raise
+
+ def _check_inbound_policy(
+ self,
+ *,
+ sender_id: str,
+ sender_number: str,
+ group_id: str | None,
+ is_group_message: bool,
+ message_text: str,
+ mentions: list,
+ sender_name: str | None,
+ timestamp: int | None,
+ ) -> tuple[bool, str]:
+ """Decide whether to route an inbound message past DM/group policy.
+
+ Returns ``(allow, chat_id)``. Has one side effect: when a group
+ message passes the enabled+allowlist gates, it is appended to the
+ group's rolling context buffer before the mention check.
+ """
+ if is_group_message:
+ chat_id = group_id or sender_number
+ if not self.config.group.enabled:
+ self.logger.info("Ignoring group message from {} (groups disabled)", chat_id)
+ return False, chat_id
+ if (
+ self.config.group.policy == "allowlist"
+ and chat_id not in self.config.group.allow_from
+ ):
+ self.logger.info(
+ "Ignoring group message from {} (policy: {})",
+ chat_id,
+ self.config.group.policy,
+ )
+ return False, chat_id
+
+ self._add_to_group_buffer(
+ group_id=chat_id,
+ sender_name=sender_name or sender_number,
+ sender_number=sender_number,
+ message_text=message_text,
+ timestamp=timestamp,
+ )
+
+ is_command = bool(message_text and message_text.strip().startswith("/"))
+ if not is_command and not self._should_respond_in_group(message_text, mentions):
+ self.logger.info(
+ "Ignoring group message (require_mention: {})",
+ self.config.group.require_mention,
+ )
+ return False, chat_id
+ return True, chat_id
+
+ # Direct message
+ chat_id = sender_number
+ if not self.config.dm.enabled:
+ self.logger.debug("Ignoring DM from {} (DMs disabled)", sender_id)
+ return False, chat_id
+ if self.config.dm.policy == "allowlist":
+ if not self._sender_matches_allowlist(sender_id, self.config.dm.allow_from):
+ self.logger.debug(
+ "Ignoring DM from {} (policy: {})", sender_id, self.config.dm.policy
+ )
+ return False, chat_id
+ return True, chat_id
+
+ def _assemble_inbound_content(
+ self,
+ *,
+ sender_name: str | None,
+ sender_number: str,
+ message_text: str,
+ attachments: list,
+ mentions: list,
+ is_group_message: bool,
+ chat_id: str,
+ ) -> tuple[str, list[str]]:
+ """Build ``(content, media_paths)`` for an inbound message.
+
+ Pulls in group context, strips bot mentions, prefixes the sender's
+ display name on group messages, and copies any attachments from
+ signal-cli's storage into the channel media dir.
+ """
+ content_parts: list[str] = []
+ media_paths: list[str] = []
+
+ if is_group_message:
+ buffer_context = self._get_group_buffer_context(chat_id)
+ if buffer_context:
+ content_parts.append(f"[Recent group messages for context:]\n{buffer_context}\n---")
+
+ if message_text:
+ if is_group_message:
+ message_text = self._strip_bot_mention(message_text, mentions)
+ display_name = sender_name or sender_number
+ message_text = f"[{display_name}]: {message_text}"
+ content_parts.append(message_text)
+
+ if attachments:
+ media_dir = get_media_dir("signal")
+ for attachment in attachments:
+ attachment_id = attachment.get("id")
+ content_type = attachment.get("contentType", "")
+ filename = attachment.get("filename") or f"attachment_{attachment_id}"
+ if not attachment_id:
+ continue
+ try:
+ source_path = self._signal_attachments_dir() / attachment_id
+ if source_path.exists():
+ dest_path = media_dir / f"signal_{safe_filename(filename)}"
+ shutil.copy2(source_path, dest_path)
+ media_paths.append(str(dest_path))
+ media_type = content_type.split("/")[0] if "/" in content_type else "file"
+ if media_type not in ("image", "audio", "video"):
+ media_type = "file"
+ content_parts.append(f"[{media_type}: {dest_path}]")
+ self.logger.debug("Downloaded attachment: {} -> {}", filename, dest_path)
+ else:
+ self.logger.warning("Attachment not found: {}", source_path)
+ content_parts.append(f"[attachment: {filename} - not found]")
+ except Exception as e:
+ self.logger.warning("Failed to process attachment {}: {}", filename, e)
+ content_parts.append(f"[attachment: {filename} - error]")
+
+ content = "\n".join(content_parts) if content_parts else "[empty message]"
+ return content, media_paths
+
+ def _add_to_group_buffer(
+ self,
+ group_id: str,
+ sender_name: str,
+ sender_number: str,
+ message_text: str,
+ timestamp: int | None,
+ ) -> None:
+ """
+ Add a message to the group's rolling buffer.
+
+ Args:
+ group_id: The group ID
+ sender_name: Display name of sender
+ sender_number: Phone number of sender
+ message_text: The message content
+ timestamp: Message timestamp
+ """
+ # Create buffer for this group if it doesn't exist
+ if group_id not in self._group_buffers:
+ self._group_buffers[group_id] = deque(maxlen=self.config.group_message_buffer_size)
+
+ # Add message to buffer (deque will automatically drop oldest when full)
+ self._group_buffers[group_id].append(
+ {
+ "sender_name": sender_name,
+ "sender_number": sender_number,
+ "content": message_text,
+ "timestamp": timestamp,
+ }
+ )
+
+ self.logger.debug(
+ "Added message to group buffer {}: {}/{}",
+ group_id,
+ len(self._group_buffers[group_id]),
+ self.config.group_message_buffer_size,
+ )
+
+ def _get_group_buffer_context(self, group_id: str) -> str:
+ """
+ Get formatted context from the group's message buffer.
+
+ Args:
+ group_id: The group ID
+
+ Returns:
+ Formatted string of recent messages (excluding the current one)
+ """
+ if group_id not in self._group_buffers:
+ return ""
+
+ buffer = self._group_buffers[group_id]
+ if len(buffer) <= 1: # Only current message, no context
+ return ""
+
+ # Format all messages except the last one (which is the current message)
+ # We want to show context BEFORE the mention
+ context_messages = list(buffer)[:-1] # Exclude the last (current) message
+
+ lines = []
+ for msg in context_messages:
+ sender = msg["sender_name"]
+ content = msg["content"][:200] # Limit to 200 chars per message
+ lines.append(f"{sender}: {content}")
+
+ return "\n".join(lines)
+
+ def _signal_attachments_dir(self) -> Path:
+ """Return the directory signal-cli writes inbound attachments to.
+
+ Defaults to ``~/.local/share/signal-cli/attachments`` (the daemon's
+ platform default on Linux) when ``config.attachments_dir`` is unset.
+ """
+ configured = self.config.attachments_dir
+ if configured:
+ return Path(configured).expanduser()
+ return Path.home() / ".local/share/signal-cli/attachments"
+
+ @staticmethod
+ def _normalize_signal_id(value: str) -> list[str]:
+ """Normalize Signal identifiers (phone/uuid/service-id) for matching."""
+ raw = value.strip()
+ if not raw:
+ return []
+
+ normalized = [raw, raw.lower()]
+ if raw.startswith("+") and len(raw) > 1:
+ normalized.append(raw[1:])
+ elif raw.isdigit():
+ normalized.append(f"+{raw}")
+ return list(dict.fromkeys(normalized))
+
+ @classmethod
+ def _sender_matches_allowlist(cls, sender_id: str, allow_list: list[str]) -> bool:
+ """Return True if any normalized variant of sender_id is on allow_list.
+
+ Both ``sender_id`` and each allow_list entry can be a single
+ identifier or a pipe-joined composite of several (e.g.
+ ``"+1234567890|uuid-abc"``); both sides are split on ``|`` and each
+ part is run through ``_normalize_signal_id`` so an allowlist entry
+ like ``1234567890`` matches a sender ``+1234567890`` (and vice
+ versa), and case-only differences in UUIDs/ACIs match too.
+ """
+ if not allow_list:
+ return False
+ sender_variants: set[str] = set()
+ for part in str(sender_id).split("|"):
+ sender_variants.update(cls._normalize_signal_id(part))
+ if not sender_variants:
+ return False
+ allow_variants: set[str] = set()
+ for entry in allow_list:
+ for part in str(entry).split("|"):
+ allow_variants.update(cls._normalize_signal_id(part))
+ return bool(sender_variants & allow_variants)
+
+ def _remember_account_id_alias(self, value: str | None) -> None:
+ """Remember known bot identifiers for mention matching."""
+ if not value:
+ return
+ if not isinstance(value, str):
+ return
+ for candidate in self._normalize_signal_id(value):
+ self._account_id_aliases.add(candidate)
+
+ def _id_matches_account(self, value: str | None) -> bool:
+ """Return True when an identifier refers to the bot account."""
+ if not value:
+ return False
+ if not isinstance(value, str):
+ return False
+ return any(
+ candidate in self._account_id_aliases for candidate in self._normalize_signal_id(value)
+ )
+
+ @staticmethod
+ def _collect_sender_id_parts(envelope: dict[str, Any]) -> list[str]:
+ """Collect all known sender identifier variants from an envelope."""
+ parts: list[str] = []
+ for key in (
+ "sourceNumber",
+ "source",
+ "sourceUuid",
+ "sourceServiceId",
+ "sourceAci",
+ "sourceACI",
+ ):
+ value = envelope.get(key)
+ if not isinstance(value, str):
+ continue
+ candidate = value.strip()
+ if candidate and candidate not in parts:
+ parts.append(candidate)
+ return parts
+
+ @staticmethod
+ def _primary_sender_id(sender_parts: list[str]) -> str:
+ """Pick the best sender identifier for routing (prefer phone-like IDs)."""
+ for part in sender_parts:
+ if part.startswith("+") or part.isdigit():
+ return part
+ return sender_parts[0] if sender_parts else ""
+
+ @staticmethod
+ def _extract_group_id(group_info: Any, group_v2: Any) -> str | None:
+ """Extract group ID from groupInfo/groupV2 payloads across signal-cli variants."""
+ for group_obj in (group_info, group_v2):
+ if not isinstance(group_obj, dict):
+ continue
+ for key in ("groupId", "id", "groupID"):
+ value = group_obj.get(key)
+ if isinstance(value, str) and value:
+ return value
+ return None
+
+ @staticmethod
+ def _mention_id_candidates(mention: dict[str, Any]) -> list[str]:
+ """Extract possible identifier fields from a mention payload."""
+ ids: list[str] = []
+
+ def _walk(value: dict[str, Any] | Any, depth: int = 0) -> None:
+ if depth > 2:
+ return
+ if not isinstance(value, dict):
+ return
+ for key, child in value.items():
+ key_lower = str(key).lower()
+ if isinstance(child, str) and child:
+ if any(token in key_lower for token in ("number", "uuid", "serviceid", "aci")):
+ ids.append(child)
+ elif isinstance(child, dict):
+ _walk(child, depth + 1)
+
+ _walk(mention)
+ return list(dict.fromkeys(ids))
+
+ @staticmethod
+ def _mention_span(mention: dict[str, Any]) -> tuple[int, int] | None:
+ """Extract a safe (start, length) span from a mention."""
+ try:
+ start = int(mention.get("start", 0))
+ length = int(mention.get("length", 0))
+ except (TypeError, ValueError):
+ return None
+
+ if start < 0 or length <= 0:
+ return None
+ return (start, length)
+
+ @staticmethod
+ def _leading_placeholder_span(text: str | None) -> tuple[int, int] | None:
+ """
+ Detect a leading Signal mention placeholder when mention metadata is missing.
+
+ Some clients/integrations deliver mentions as a leading placeholder character
+ (typically U+FFFC) but omit `mentions` metadata in the payload.
+ """
+ if not text:
+ return None
+
+ start = 0
+ while start < len(text) and text[start].isspace():
+ start += 1
+
+ if start >= len(text):
+ return None
+
+ marker = text[start]
+ if marker not in ("\ufffc", "\ufffd", "\x1b"):
+ return None
+
+ next_index = start + 1
+ if next_index < len(text) and not text[next_index].isspace():
+ return None
+
+ return (start, 1)
+
+ def _should_respond_in_group(self, message_text: str, mentions: list[dict[str, Any]]) -> bool:
+ """
+ Determine if the bot should respond to a group message.
+
+ Args:
+ message_text: The message text content
+ mentions: List of mentions from Signal (format: [{"number": "+1234567890", "start": 0, "length": 10}])
+
+ Returns:
+ True if bot should respond, False otherwise
+ """
+ # Group reply behavior is controlled only by group.require_mention.
+ if not self.config.group.require_mention:
+ return True
+
+ # If mention is required, check if bot was mentioned.
+ for mention in mentions:
+ if not isinstance(mention, dict):
+ continue
+ for mention_id in self._mention_id_candidates(mention):
+ if self._id_matches_account(mention_id):
+ return True
+
+ # Some Signal clients emit mention spans without recipient identifiers
+ # (for handle-style mentions). Accept a leading identifier-less mention
+ # as a mention of the bot to avoid false negatives.
+ for mention in mentions:
+ if not isinstance(mention, dict):
+ continue
+ if self._mention_id_candidates(mention):
+ continue
+ span = self._mention_span(mention)
+ if not span:
+ continue
+ start, _ = span
+ if message_text is not None and not message_text[:start].strip():
+ self.logger.debug("Accepting identifier-less leading mention as bot mention")
+ return True
+
+ # Some payloads omit `mentions` but still include the leading mention
+ # placeholder character in the message body.
+ if not mentions and self._leading_placeholder_span(message_text):
+ self.logger.debug("Accepting leading placeholder mention without mention metadata")
+ return True
+
+ # Fallback: check for configured phone number in plain text.
+ if message_text and self.config.phone_number:
+ for account_id in self._normalize_signal_id(self.config.phone_number):
+ if account_id and account_id in message_text:
+ return True
+
+ return False
+
+ def _strip_bot_mention(self, text: str, mentions: list[dict[str, Any]]) -> str:
+ """
+ Remove bot mentions from message text.
+
+ Signal mentions are embedded in the text, so we need to remove them based on
+ the mentions array which provides start position and length.
+
+ Args:
+ text: Original message text
+ mentions: List of mention objects with start/length positions
+
+ Returns:
+ Text with bot mentions removed
+ """
+ if not text:
+ return text
+
+ # Build a list of (start, length) tuples for our bot's mentions
+ bot_mentions = []
+ for mention in mentions:
+ if not isinstance(mention, dict):
+ continue
+ mention_ids = self._mention_id_candidates(mention)
+ span = self._mention_span(mention)
+ if not span:
+ continue
+
+ # Strip matched bot mentions by ID.
+ if any(self._id_matches_account(mention_id) for mention_id in mention_ids):
+ bot_mentions.append(span)
+ continue
+
+ # Also strip identifier-less leading mention spans (handle mentions).
+ if not mention_ids:
+ start, _ = span
+ if not text[:start].strip():
+ bot_mentions.append(span)
+
+ if not bot_mentions:
+ placeholder_span = self._leading_placeholder_span(text)
+ if placeholder_span:
+ bot_mentions.append(placeholder_span)
+
+ # Sort mentions by start position (descending) to remove from end to start
+ # This prevents position shifts when removing earlier mentions
+ bot_mentions.sort(reverse=True)
+
+ # Remove each mention
+ for start, length in bot_mentions:
+ if start >= len(text):
+ continue
+ end = min(len(text), start + length)
+ text = text[:start] + text[end:]
+
+ return text.strip()
+
+ @staticmethod
+ def _is_group_chat_id(chat_id: str) -> bool:
+ """Return True when chat_id appears to be a Signal group ID (base64)."""
+ return "=" in chat_id or (len(chat_id) > 40 and "-" not in chat_id)
+
+ def _recipient_params(self, chat_id: str) -> dict[str, Any]:
+ """Build recipient params for signal-cli JSON-RPC methods."""
+ if self._is_group_chat_id(chat_id):
+ return {"groupId": chat_id}
+ return {"recipient": [chat_id]}
+
+ async def _start_typing(self, chat_id: str) -> None:
+ """Start periodic typing indicator updates for a chat."""
+ await self._stop_typing(chat_id, send_stop=False)
+ await self._send_typing(chat_id)
+ self._typing_tasks[chat_id] = asyncio.create_task(self._typing_loop(chat_id))
+
+ async def _stop_typing(self, chat_id: str, send_stop: bool = True) -> None:
+ """Stop typing indicator updates for a chat."""
+ task = self._typing_tasks.pop(chat_id, None)
+ had_task = task is not None
+ if task and not task.done():
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
+
+ if send_stop and had_task:
+ await self._send_typing(chat_id, stop=True)
+
+ async def _typing_loop(self, chat_id: str) -> None:
+ """Send typing updates periodically until cancelled."""
+ try:
+ while self._running:
+ await asyncio.sleep(self._TYPING_REFRESH_SECONDS)
+ await self._send_typing(chat_id, quiet_success=True)
+ except asyncio.CancelledError:
+ pass
+ except Exception as e:
+ self.logger.debug("Typing indicator loop stopped for {}: {}", chat_id, e)
+
+ async def _send_typing(
+ self, chat_id: str, stop: bool = False, quiet_success: bool = False
+ ) -> None:
+ """Send a typing START/STOP message via signal-cli."""
+ action = "stop" if stop else "start"
+ if (
+ not self._is_group_chat_id(chat_id)
+ and chat_id.startswith("+") is False
+ and chat_id not in self._typing_uuid_warnings
+ ):
+ self._typing_uuid_warnings.add(chat_id)
+ self.logger.warning(
+ "Signal DM recipient is UUID-only (no phone number in envelope). "
+ "Some Signal clients may not render typing indicators for this recipient form."
+ )
+ candidate_params: list[dict[str, Any]]
+ if self._is_group_chat_id(chat_id):
+ candidate_params = [{"groupId": chat_id}, {"groupId": [chat_id]}]
+ else:
+ candidate_params = [{"recipient": chat_id}, {"recipient": [chat_id]}]
+
+ last_error: Any | None = None
+ for params in candidate_params:
+ if stop:
+ params["stop"] = True
+ try:
+ response = await self._send_request("sendTyping", params)
+ except Exception as e:
+ last_error = str(e)
+ continue
+
+ if "error" not in response:
+ if not quiet_success:
+ self.logger.info("Signal typing {} sent for {}", action, chat_id)
+ return
+
+ last_error = response["error"]
+
+ self.logger.warning(
+ "Failed to send Signal typing {} for {}: {}", action, chat_id, last_error
+ )
+
+ async def _ensure_typing_indicators_enabled(self) -> None:
+ """Enable typing indicators on the bot account."""
+ response = await self._send_request("updateConfiguration", {"typingIndicators": True})
+ if "error" in response:
+ self.logger.warning(
+ "Failed to enable Signal typing indicators: {}", response["error"]
+ )
+ else:
+ self.logger.info("Signal typing indicators enabled on account configuration")
+
+ async def _send_request(
+ self, method: str, params: dict[str, Any] | None = None
+ ) -> dict[str, Any]:
+ """Send a JSON-RPC request via HTTP and wait for response."""
+ # Generate request ID
+ self._request_id += 1
+ request_id = self._request_id
+
+ # Build JSON-RPC request
+ request = {"jsonrpc": "2.0", "method": method, "id": request_id}
+
+ if params:
+ request["params"] = params
+
+ return await self._send_http_request(request)
+
+ async def _send_http_request(self, request: dict[str, Any]) -> dict[str, Any]:
+ """Send JSON-RPC request via HTTP."""
+ if not self._http:
+ raise RuntimeError("Not connected to signal-cli daemon")
+
+ try:
+ response = await self._http.post("/api/v1/rpc", json=request)
+ response.raise_for_status()
+ return response.json()
+ except Exception as e:
+ self.logger.error("HTTP request failed: {}", e)
+ return {"error": {"message": str(e)}}
diff --git a/agent/src/channels/slack.py b/agent/src/channels/slack.py
new file mode 100644
index 000000000..079c6a74d
--- /dev/null
+++ b/agent/src/channels/slack.py
@@ -0,0 +1,740 @@
+"""Slack channel implementation using Socket Mode."""
+
+import asyncio
+import re
+from pathlib import Path
+from typing import Any
+
+import httpx
+from pydantic import Field
+from slack_sdk.socket_mode.request import SocketModeRequest
+from slack_sdk.socket_mode.response import SocketModeResponse
+from slack_sdk.socket_mode.websockets import SocketModeClient
+from slack_sdk.web.async_client import AsyncWebClient
+from slackify_markdown import slackify_markdown
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.pairing import is_approved
+from src.channels.utils import safe_filename, split_message
+
+
+class SlackDMConfig(BaseModel):
+ """Slack DM policy configuration."""
+
+ enabled: bool = True
+ policy: str = "open"
+ allow_from: list[str] = Field(default_factory=list)
+
+
+class SlackConfig(BaseModel):
+ """Slack channel configuration."""
+
+ enabled: bool = False
+ mode: str = "socket"
+ webhook_path: str = "/slack/events"
+ bot_token: str = ""
+ app_token: str = ""
+ user_token_read_only: bool = True
+ reply_in_thread: bool = True
+ react_emoji: str = "eyes"
+ done_emoji: str = "white_check_mark"
+ include_thread_context: bool = True
+ thread_context_limit: int = 20
+ allow_from: list[str] = Field(default_factory=list)
+ group_policy: str = "mention"
+ group_allow_from: list[str] = Field(default_factory=list)
+ # When group_policy is "allowlist", also require the bot to be @mentioned
+ # before responding (so it only replies to mentions in approved channels,
+ # instead of every message). No effect for "mention"/"open" policies.
+ group_require_mention: bool = False
+ dm: SlackDMConfig = Field(default_factory=SlackDMConfig)
+
+
+SLACK_MAX_MESSAGE_LEN = 39_000 # Slack API allows ~40k; leave margin
+SLACK_DOWNLOAD_TIMEOUT = 30.0
+# Abort Socket Mode WSS handshake after this many seconds. REST auth_test can still
+# succeed while WSS blocks (firewall / region). slack-sdk does not apply HTTP(S)_PROXY
+# to websockets.connect — see slack_sdk.socket_mode.websockets.SocketModeClient.connect.
+SLACK_SOCKET_CONNECT_TIMEOUT_S = 45.0
+_HTML_DOWNLOAD_PREFIXES = (b"<!doctype html", b"<html")
+
+
+class SlackChannel(BaseChannel):
+ """Slack channel using Socket Mode."""
+
+ name = "slack"
+ display_name = "Slack"
+ _SLACK_ID_RE = re.compile(r"^[CDGUW][A-Z0-9]{2,}$")
+ _SLACK_CHANNEL_REF_RE = re.compile(r"^<#([A-Z0-9]+)(?:\|[^>]+)?>$")
+ _SLACK_USER_REF_RE = re.compile(r"^<@([A-Z0-9]+)(?:\|[^>]+)?>$")
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return SlackConfig().model_dump(by_alias=True)
+
+ _THREAD_CONTEXT_CACHE_LIMIT = 10_000
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = SlackConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: SlackConfig = config
+ self._web_client: AsyncWebClient | None = None
+ self._socket_client: SocketModeClient | None = None
+ self._bot_user_id: str | None = None
+ self._target_cache: dict[str, str] = {}
+ self._thread_context_attempted: set[str] = set()
+
+ async def start(self) -> None:
+ """Start the Slack Socket Mode client."""
+ if not self.config.bot_token or not self.config.app_token:
+ self.logger.error("bot/app token not configured")
+ return
+ if self.config.mode != "socket":
+ self.logger.error("Unsupported mode: {}", self.config.mode)
+ return
+
+ self._running = True
+
+ self._web_client = AsyncWebClient(token=self.config.bot_token)
+ self._socket_client = SocketModeClient(
+ app_token=self.config.app_token,
+ web_client=self._web_client,
+ )
+
+ self._socket_client.socket_mode_request_listeners.append(self._on_socket_request)
+
+ # Resolve bot user ID for mention handling
+ try:
+ auth = await self._web_client.auth_test()
+ self._bot_user_id = auth.get("user_id")
+ self.logger.info("bot connected as {}", self._bot_user_id)
+ except Exception as e:
+ self.logger.warning("auth_test failed: {}", e)
+
+ self.logger.info("Starting Socket Mode client...")
+ try:
+ await asyncio.wait_for(
+ self._socket_client.connect(),
+ timeout=SLACK_SOCKET_CONNECT_TIMEOUT_S,
+ )
+ except asyncio.TimeoutError:
+ self.logger.error(
+ "Slack Socket Mode WebSocket handshake timed out after {:.0f}s. "
+ "auth_test uses HTTPS and may still succeed while WSS is blocked. "
+ "Check outbound access to Slack WebSockets; slack-sdk Socket Mode "
+ "does not apply HTTP(S)_PROXY to websockets.connect.",
+ SLACK_SOCKET_CONNECT_TIMEOUT_S,
+ )
+ await self.stop()
+ raise RuntimeError("Slack Socket Mode WebSocket connect timed out") from None
+
+ self.logger.info("Slack Socket Mode WebSocket connected (events enabled)")
+
+ while self._running:
+ await asyncio.sleep(1)
+
+ async def stop(self) -> None:
+ """Stop the Slack client."""
+ self._running = False
+ if self._socket_client:
+ try:
+ await self._socket_client.close()
+ except Exception as e:
+ self.logger.warning("socket close failed: {}", e)
+ self._socket_client = None
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through Slack."""
+ if not self._web_client:
+ self.logger.warning("client not running")
+ return
+ try:
+ target_chat_id = await self._resolve_target_chat_id(msg.chat_id)
+ slack_meta = msg.metadata.get("slack", {}) if msg.metadata else {}
+ thread_ts = slack_meta.get("thread_ts")
+ origin_chat_id = str((slack_meta.get("event", {}) or {}).get("channel") or msg.chat_id)
+ # Reply in the same thread the inbound message belongs to (works
+ # for both real channel threads and DM threads). When the agent
+ # is forwarding to a different channel, drop thread_ts because it
+ # only makes sense within the originating conversation.
+ thread_ts_param = thread_ts if thread_ts and target_chat_id == origin_chat_id else None
+
+ is_progress = (msg.metadata or {}).get("_progress", False)
+ if is_progress and not msg.content:
+ pass # skip empty progress messages (e.g. tool-event-only updates)
+ elif msg.content or not (msg.media or []):
+ mrkdwn = self._to_mrkdwn(msg.content) if msg.content else " "
+ buttons = getattr(msg, "buttons", None) or []
+ chunks = split_message(mrkdwn, SLACK_MAX_MESSAGE_LEN)
+ for index, chunk in enumerate(chunks):
+ kwargs: dict[str, Any] = dict(
+ channel=target_chat_id, text=chunk, thread_ts=thread_ts_param,
+ )
+ if buttons and index == len(chunks) - 1:
+ kwargs["blocks"] = self._build_button_blocks(chunk, buttons)
+ await self._web_client.chat_postMessage(**kwargs)
+
+ for media_path in msg.media or []:
+ try:
+ await self._web_client.files_upload_v2(
+ channel=target_chat_id,
+ file=media_path,
+ thread_ts=thread_ts_param,
+ )
+ except Exception:
+ self.logger.exception("Failed to upload file {}", media_path)
+
+ # Update reaction emoji when the final (non-progress) response is sent
+ if not (msg.metadata or {}).get("_progress"):
+ event = slack_meta.get("event", {})
+ await self._update_react_emoji(origin_chat_id, event.get("ts"))
+
+ except Exception:
+ self.logger.exception("Error sending message")
+ raise
+
+ async def _resolve_target_chat_id(self, target: str) -> str:
+ """Resolve human-friendly Slack targets to concrete IDs when needed."""
+ if not self._web_client:
+ return target
+
+ target = target.strip()
+ if not target:
+ return target
+
+ if match := self._SLACK_CHANNEL_REF_RE.fullmatch(target):
+ return match.group(1)
+ if match := self._SLACK_USER_REF_RE.fullmatch(target):
+ return await self._open_dm_for_user(match.group(1))
+ if self._SLACK_ID_RE.fullmatch(target):
+ if target.startswith(("U", "W")):
+ return await self._open_dm_for_user(target)
+ return target
+
+ if target.startswith("#"):
+ return await self._resolve_channel_name(target[1:])
+ if target.startswith("@"):
+ return await self._resolve_user_handle(target[1:])
+
+ try:
+ return await self._resolve_channel_name(target)
+ except ValueError:
+ return await self._resolve_user_handle(target)
+
+ async def _resolve_channel_name(self, name: str) -> str:
+ normalized = self._normalize_target_name(name)
+ if not normalized:
+ raise ValueError("Slack target channel name is empty")
+
+ cache_key = f"channel:{normalized}"
+ if cache_key in self._target_cache:
+ return self._target_cache[cache_key]
+
+ cursor: str | None = None
+ while True:
+ response = await self._web_client.conversations_list(
+ types="public_channel,private_channel",
+ exclude_archived=True,
+ limit=200,
+ cursor=cursor,
+ )
+ for channel in response.get("channels", []):
+ if self._normalize_target_name(str(channel.get("name") or "")) == normalized:
+ channel_id = str(channel.get("id") or "")
+ if channel_id:
+ self._target_cache[cache_key] = channel_id
+ return channel_id
+ cursor = ((response.get("response_metadata") or {}).get("next_cursor") or "").strip()
+ if not cursor:
+ break
+
+ raise ValueError(
+ f"Slack channel '{name}' was not found. Use a joined channel name like "
+ f"'#general' or a concrete channel ID."
+ )
+
+ async def _resolve_user_handle(self, handle: str) -> str:
+ normalized = self._normalize_target_name(handle)
+ if not normalized:
+ raise ValueError("Slack target user handle is empty")
+
+ cache_key = f"user:{normalized}"
+ if cache_key in self._target_cache:
+ return self._target_cache[cache_key]
+
+ cursor: str | None = None
+ while True:
+ response = await self._web_client.users_list(limit=200, cursor=cursor)
+ for member in response.get("members", []):
+ if self._member_matches_handle(member, normalized):
+ user_id = str(member.get("id") or "")
+ if not user_id:
+ continue
+ dm_id = await self._open_dm_for_user(user_id)
+ self._target_cache[cache_key] = dm_id
+ return dm_id
+ cursor = ((response.get("response_metadata") or {}).get("next_cursor") or "").strip()
+ if not cursor:
+ break
+
+ raise ValueError(
+ f"Slack user '{handle}' was not found. Use '@name' or a concrete DM/channel ID."
+ )
+
+ async def _open_dm_for_user(self, user_id: str) -> str:
+ response = await self._web_client.conversations_open(users=user_id)
+ channel_id = str(((response.get("channel") or {}).get("id")) or "")
+ if not channel_id:
+ raise ValueError(f"Slack DM target for user '{user_id}' could not be opened.")
+ return channel_id
+
+ @staticmethod
+ def _normalize_target_name(value: str) -> str:
+ return value.strip().lstrip("#@").lower()
+
+ @classmethod
+ def _member_matches_handle(cls, member: dict[str, Any], normalized: str) -> bool:
+ profile = member.get("profile") or {}
+ candidates = {
+ str(member.get("name") or ""),
+ str(profile.get("display_name") or ""),
+ str(profile.get("display_name_normalized") or ""),
+ str(profile.get("real_name") or ""),
+ str(profile.get("real_name_normalized") or ""),
+ }
+ return normalized in {cls._normalize_target_name(candidate) for candidate in candidates if candidate}
+
+ async def _on_socket_request(
+ self,
+ client: SocketModeClient,
+ req: SocketModeRequest,
+ ) -> None:
+ """Handle incoming Socket Mode requests."""
+ if req.type == "interactive":
+ await self._on_block_action(client, req)
+ return
+ if req.type != "events_api":
+ return
+
+ # Acknowledge right away
+ await client.send_socket_mode_response(
+ SocketModeResponse(envelope_id=req.envelope_id)
+ )
+
+ payload = req.payload or {}
+ event = payload.get("event") or {}
+ event_type = event.get("type")
+
+ # Handle app mentions or plain messages
+ if event_type not in ("message", "app_mention"):
+ return
+
+ sender_id = event.get("user")
+ chat_id = event.get("channel")
+
+ subtype = event.get("subtype")
+ # Slack uses subtype=file_share for user messages with attachments.
+ # Ignore other subtypes such as bot_message / message_changed / deleted.
+ if subtype and subtype != "file_share":
+ return
+ if self._bot_user_id and sender_id == self._bot_user_id:
+ return
+
+ # Avoid double-processing: Slack sends both `message` and `app_mention`
+ # for mentions in channels. Prefer `app_mention`.
+ text = event.get("text") or ""
+ if event_type == "message" and self._bot_user_id and f"<@{self._bot_user_id}>" in text:
+ return
+
+ # Debug: log basic event shape
+ self.logger.debug(
+ "event: type={} subtype={} user={} channel={} channel_type={} text={}",
+ event_type,
+ subtype,
+ sender_id,
+ chat_id,
+ event.get("channel_type"),
+ text[:80],
+ )
+ if not sender_id or not chat_id:
+ return
+
+ channel_type = event.get("channel_type") or ""
+
+ if not self._is_allowed(sender_id, chat_id, channel_type):
+ if channel_type == "im" and self.config.dm.enabled:
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content="",
+ is_dm=True,
+ )
+ return
+
+ if channel_type != "im" and not self._should_respond_in_channel(event_type, text, chat_id):
+ return
+
+ text = self._strip_bot_mention(text)
+
+ event_ts = event.get("ts")
+ raw_thread_ts = event.get("thread_ts")
+ thread_ts = raw_thread_ts
+ # In DMs we don't auto-open a thread on top-level messages (it would
+ # bury replies under "1 reply"). But if the user explicitly opened a
+ # thread inside the DM, raw_thread_ts is set and we honor it.
+ if (
+ self.config.reply_in_thread
+ and not thread_ts
+ and channel_type != "im"
+ ):
+ thread_ts = event_ts
+ # Add :eyes: reaction to the triggering message (best-effort)
+ try:
+ if self._web_client and event.get("ts"):
+ await self._web_client.reactions_add(
+ channel=chat_id,
+ name=self.config.react_emoji,
+ timestamp=event.get("ts"),
+ )
+ except Exception as e:
+ self.logger.debug("reactions_add failed: {}", e)
+
+ # Thread-scoped session key whenever the user is in a real thread
+ # (raw_thread_ts is set). DM threads get their own session, separate
+ # from the DM root, so context doesn't bleed across thread boundaries.
+ session_key = (
+ f"slack:{chat_id}:{thread_ts}" if thread_ts and raw_thread_ts else None
+ )
+ media_paths: list[str] = []
+ file_markers: list[str] = []
+ for file_info in event.get("files") or []:
+ if not isinstance(file_info, dict):
+ continue
+ file_path, marker = await self._download_slack_file(file_info)
+ if file_path:
+ media_paths.append(file_path)
+ if marker:
+ file_markers.append(marker)
+
+ is_slash = text.strip().startswith("/")
+ content = text if is_slash else await self._with_thread_context(
+ text,
+ chat_id=chat_id,
+ channel_type=channel_type,
+ thread_ts=thread_ts,
+ raw_thread_ts=raw_thread_ts,
+ current_ts=event_ts,
+ )
+ if file_markers:
+ content = "\n".join(part for part in [content, *file_markers] if part)
+ if not content and not media_paths:
+ return
+
+ try:
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content=content,
+ media=media_paths,
+ metadata={
+ "slack": {
+ "event": event,
+ "thread_ts": thread_ts,
+ "channel_type": channel_type,
+ },
+ },
+ session_key=session_key,
+ )
+ except Exception:
+ self.logger.exception("Error handling message from {}", sender_id)
+
+ async def _download_slack_file(self, file_info: dict[str, Any]) -> tuple[str | None, str]:
+ """Download a Slack private file to the local media directory."""
+ file_id = str(file_info.get("id") or "file")
+ name = str(
+ file_info.get("name")
+ or file_info.get("title")
+ or file_info.get("id")
+ or "slack-file"
+ )
+ marker_type = "image" if str(file_info.get("mimetype") or "").startswith("image/") else "file"
+ marker = f"[{marker_type}: {name}]"
+ url = str(file_info.get("url_private_download") or file_info.get("url_private") or "")
+ if not url:
+ return None, self._download_failure_marker(marker_type, name, "missing download url")
+ if not self.config.bot_token:
+ return None, self._download_failure_marker(marker_type, name, "missing bot token")
+
+ filename = safe_filename(f"{file_id}_{name}")
+ path = Path(get_media_dir("slack")) / filename
+ try:
+ async with httpx.AsyncClient(timeout=SLACK_DOWNLOAD_TIMEOUT, follow_redirects=True) as client:
+ response = await client.get(
+ url,
+ headers={"Authorization": f"Bearer {self.config.bot_token}"},
+ )
+ response.raise_for_status()
+ if self._looks_like_html_download(response):
+ raise ValueError("Slack returned HTML instead of file content")
+ path.write_bytes(response.content)
+ return str(path), marker
+ except Exception as e:
+ self.logger.warning("Failed to download file {}: {}", file_id, e)
+ return None, self._download_failure_marker(marker_type, name, "download failed")
+
+ @staticmethod
+ def _download_failure_marker(marker_type: str, name: str, reason: str) -> str:
+ return (
+ f"[{marker_type}: {name}: {reason}; not available to vibe-trading. "
+ "Check Slack files:read scope, reinstall the Slack app, and ensure the bot can access the file.]"
+ )
+
+ @staticmethod
+ def _looks_like_html_download(response: httpx.Response) -> bool:
+ content_type = response.headers.get("content-type", "").lower()
+ if "text/html" in content_type:
+ return True
+ preview = response.content[:256].lstrip().lower()
+ return preview.startswith(_HTML_DOWNLOAD_PREFIXES)
+
+ async def _on_block_action(self, client: SocketModeClient, req: SocketModeRequest) -> None:
+ """Handle button clicks from inline action buttons."""
+ await client.send_socket_mode_response(SocketModeResponse(envelope_id=req.envelope_id))
+ payload = req.payload or {}
+ actions = payload.get("actions") or []
+ if not actions:
+ return
+ value = str(actions[0].get("value") or "")
+ user_info = payload.get("user") or {}
+ sender_id = str(user_info.get("id") or "")
+ channel_info = payload.get("channel") or {}
+ chat_id = str(channel_info.get("id") or "")
+ if not sender_id or not chat_id or not value:
+ return
+ message_info = payload.get("message") or {}
+ thread_ts = message_info.get("thread_ts") or message_info.get("ts")
+ channel_type = self._infer_channel_type(chat_id)
+ if not self._is_allowed(sender_id, chat_id, channel_type):
+ return
+ session_key = f"slack:{chat_id}:{thread_ts}" if thread_ts else None
+ try:
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content=value,
+ metadata={"slack": {"thread_ts": thread_ts, "channel_type": channel_type}},
+ session_key=session_key,
+ )
+ except Exception:
+ self.logger.exception("Error handling button click from {}", sender_id)
+
+ async def _with_thread_context(
+ self,
+ text: str,
+ *,
+ chat_id: str,
+ channel_type: str,
+ thread_ts: str | None,
+ raw_thread_ts: str | None,
+ current_ts: str | None,
+ ) -> str:
+ """Include thread history the first time the bot is pulled into a Slack thread."""
+ del channel_type # DM and channel threads are both fetched via conversations.replies
+ if (
+ not self.config.include_thread_context
+ or not self._web_client
+ or not raw_thread_ts
+ or not thread_ts
+ or current_ts == thread_ts
+ ):
+ return text
+
+ key = f"{chat_id}:{thread_ts}"
+ if key in self._thread_context_attempted:
+ return text
+ if len(self._thread_context_attempted) >= self._THREAD_CONTEXT_CACHE_LIMIT:
+ self._thread_context_attempted.clear()
+ self._thread_context_attempted.add(key)
+
+ try:
+ response = await self._web_client.conversations_replies(
+ channel=chat_id,
+ ts=thread_ts,
+ limit=max(1, self.config.thread_context_limit),
+ )
+ except Exception as e:
+ self.logger.warning("thread context unavailable for {}: {}", key, e)
+ return text
+
+ lines = self._format_thread_context(
+ response.get("messages", []),
+ current_ts=current_ts,
+ )
+ if not lines:
+ return text
+ return "Slack thread context before this mention:\n" + "\n".join(lines) + f"\n\nCurrent message:\n{text}"
+
+ def _format_thread_context(self, messages: list[dict[str, Any]], *, current_ts: str | None) -> list[str]:
+ lines: list[str] = []
+ for item in messages:
+ if item.get("ts") == current_ts:
+ continue
+ if item.get("subtype"):
+ continue
+ sender = str(item.get("user") or item.get("bot_id") or "unknown")
+ is_bot = self._bot_user_id is not None and sender == self._bot_user_id
+ label = "bot" if is_bot else f"<@{sender}>"
+ text = str(item.get("text") or "").strip()
+ if not text:
+ continue
+ text = self._strip_bot_mention(text)
+ if len(text) > 500:
+ text = text[:500] + "…"
+ lines.append(f"- {label}: {text}")
+ return lines
+
+ @staticmethod
+ def _build_button_blocks(text: str, buttons: list[list[str]]) -> list[dict[str, Any]]:
+ """Build Slack Block Kit blocks with action buttons."""
+ blocks: list[dict[str, Any]] = [
+ {"type": "section", "text": {"type": "mrkdwn", "text": text[:3000]}},
+ ]
+ elements = []
+ for row in buttons:
+ for label in row:
+ elements.append({
+ "type": "button",
+ "text": {"type": "plain_text", "text": label[:75]},
+ "value": label[:75],
+ "action_id": f"btn_{label[:50]}",
+ })
+ if elements:
+ blocks.append({"type": "actions", "elements": elements[:25]})
+ return blocks
+
+ async def _update_react_emoji(self, chat_id: str, ts: str | None) -> None:
+ """Remove the in-progress reaction and optionally add a done reaction."""
+ if not self._web_client or not ts:
+ return
+ try:
+ await self._web_client.reactions_remove(
+ channel=chat_id,
+ name=self.config.react_emoji,
+ timestamp=ts,
+ )
+ except Exception as e:
+ self.logger.debug("reactions_remove failed: {}", e)
+ if self.config.done_emoji:
+ try:
+ await self._web_client.reactions_add(
+ channel=chat_id,
+ name=self.config.done_emoji,
+ timestamp=ts,
+ )
+ except Exception as e:
+ self.logger.debug("done reaction failed: {}", e)
+
+ def _is_allowed(self, sender_id: str, chat_id: str, channel_type: str) -> bool:
+ if channel_type == "im":
+ if not self.config.dm.enabled:
+ return False
+ if self.config.dm.policy == "allowlist":
+ return sender_id in self.config.dm.allow_from or is_approved(self.name, sender_id)
+ return True
+
+ # Group / channel messages
+ if self.config.group_policy == "allowlist":
+ return chat_id in self.config.group_allow_from
+ return True
+
+ def _is_mention(self, event_type: str, text: str) -> bool:
+ if event_type == "app_mention":
+ return True
+ return self._bot_user_id is not None and f"<@{self._bot_user_id}>" in text
+
+ def _should_respond_in_channel(self, event_type: str, text: str, chat_id: str) -> bool:
+ if self.config.group_policy == "open":
+ return True
+ if self.config.group_policy == "mention":
+ return self._is_mention(event_type, text)
+ if self.config.group_policy == "allowlist":
+ if chat_id not in self.config.group_allow_from:
+ return False
+ if self.config.group_require_mention:
+ return self._is_mention(event_type, text)
+ return True
+ return False
+
+ def is_allowed(self, sender_id: str) -> bool:
+ # Slack needs channel-aware policy checks, so _on_socket_request and
+ # _on_block_action call _is_allowed before handing off to BaseChannel.
+ return True
+
+ @staticmethod
+ def _infer_channel_type(chat_id: str) -> str:
+ if chat_id.startswith("D"):
+ return "im"
+ if chat_id.startswith("G"):
+ return "group"
+ return "channel"
+
+ def _strip_bot_mention(self, text: str) -> str:
+ if not text or not self._bot_user_id:
+ return text
+ return re.sub(rf"<@{re.escape(self._bot_user_id)}>\s*", "", text).strip()
+
+ _TABLE_RE = re.compile(r"(?m)^\|.*\|$(?:\n\|[\s:|-]*\|$)(?:\n\|.*\|$)*")
+ _CODE_FENCE_RE = re.compile(r"```[\s\S]*?```")
+ _INLINE_CODE_RE = re.compile(r"`[^`]+`")
+ _LEFTOVER_BOLD_RE = re.compile(r"\*\*(.+?)\*\*")
+ _LEFTOVER_HEADER_RE = re.compile(r"^#{1,6}\s+(.+)$", re.MULTILINE)
+ _BARE_URL_RE = re.compile(r"(?<![|<])(https?://\S+)")
+
+ @classmethod
+ def _to_mrkdwn(cls, text: str) -> str:
+ """Convert Markdown to Slack mrkdwn, including tables."""
+ if not text:
+ return ""
+ text = cls._TABLE_RE.sub(cls._convert_table, text)
+ return cls._fixup_mrkdwn(slackify_markdown(text)).rstrip("\n")
+
+ @classmethod
+ def _fixup_mrkdwn(cls, text: str) -> str:
+ """Fix markdown artifacts that slackify_markdown misses."""
+ code_blocks: list[str] = []
+
+ def _save_code(m: re.Match) -> str:
+ code_blocks.append(m.group(0))
+ return f"\x00CB{len(code_blocks) - 1}\x00"
+
+ text = cls._CODE_FENCE_RE.sub(_save_code, text)
+ text = cls._INLINE_CODE_RE.sub(_save_code, text)
+ text = cls._LEFTOVER_BOLD_RE.sub(r"*\1*", text)
+ text = cls._LEFTOVER_HEADER_RE.sub(r"*\1*", text)
+ text = cls._BARE_URL_RE.sub(lambda m: m.group(0).replace("&amp;", "&"), text)
+
+ for i, block in enumerate(code_blocks):
+ text = text.replace(f"\x00CB{i}\x00", block)
+ return text
+
+ @staticmethod
+ def _convert_table(match: re.Match) -> str:
+ """Convert a Markdown table to a Slack-readable list."""
+ lines = [ln.strip() for ln in match.group(0).strip().splitlines() if ln.strip()]
+ if len(lines) < 2:
+ return match.group(0)
+ headers = [h.strip() for h in lines[0].strip("|").split("|")]
+ start = 2 if re.fullmatch(r"[|\s:\-]+", lines[1]) else 1
+ rows: list[str] = []
+ for line in lines[start:]:
+ cells = [c.strip() for c in line.strip("|").split("|")]
+ cells = (cells + [""] * len(headers))[: len(headers)]
+ parts = [f"**{headers[i]}**: {cells[i]}" for i in range(len(headers)) if cells[i]]
+ if parts:
+ rows.append(" · ".join(parts))
+ return "\n".join(rows)
diff --git a/agent/src/channels/telegram.py b/agent/src/channels/telegram.py
new file mode 100644
index 000000000..668c4c2cb
--- /dev/null
+++ b/agent/src/channels/telegram.py
@@ -0,0 +1,1674 @@
+"""Telegram channel implementation using python-telegram-bot."""
+
+from __future__ import annotations
+
+import asyncio
+import re
+import time
+import unicodedata
+from contextlib import suppress
+from dataclasses import dataclass
+from pathlib import Path
+from typing import Any, Literal
+from urllib.parse import urlparse
+
+from pydantic import Field, field_validator, model_validator
+from telegram import (
+ BotCommand,
+ InlineKeyboardButton,
+ InlineKeyboardMarkup,
+ ReactionTypeEmoji,
+ ReplyParameters,
+ Update,
+)
+from telegram.error import BadRequest, NetworkError, TimedOut
+from telegram.ext import Application, CallbackQueryHandler, ContextTypes, MessageHandler, filters
+from telegram.request import HTTPXRequest
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.channels.utils import validate_url_target
+from src.channels.utils import split_message
+
+TELEGRAM_MAX_MESSAGE_LEN = 4000 # Telegram message character limit
+# Telegram's actual API limit is 4096; we split raw markdown at 4000 as a
+# safety margin for mid-stream edits (plain text). For _stream_end, we split
+# raw markdown into chunks whose rendered HTML fits Telegram's true 4096-char
+# boundary so the final rendered message never overflows.
+TELEGRAM_HTML_MAX_LEN = 4096
+TELEGRAM_REPLY_CONTEXT_MAX_LEN = TELEGRAM_MAX_MESSAGE_LEN # Max length for reply context in user message
+
+
+def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
+ """Split raw Telegram Markdown without leaving fenced code blocks unbalanced."""
+ if not content:
+ return []
+ content = content.lstrip()
+ if not content:
+ return []
+ if len(content) <= max_len:
+ return [content]
+
+ def fence_line(fence_pos: int) -> str:
+ line_end = content.find("\n", fence_pos)
+ if line_end < 0:
+ return content[fence_pos:]
+ return content[fence_pos:line_end]
+
+ def split_inside_fenced_code_block(pos: int) -> tuple[bool, int, str]:
+ if content[:pos].count("```") % 2 == 0:
+ return False, -1, ""
+ opening = content.rfind("```", 0, pos)
+ if opening < 0:
+ return True, -1, "```"
+ return True, opening, fence_line(opening)
+
+ chunks: list[str] = []
+ while content:
+ if len(content) <= max_len:
+ chunks.append(content)
+ break
+
+ cut = content[:max_len]
+ pos = cut.rfind("\n")
+ if pos <= 0:
+ pos = cut.rfind(" ")
+ if pos <= 0:
+ pos = max_len
+
+ inside_code, opening, fence = split_inside_fenced_code_block(pos)
+ if inside_code:
+ if opening > 0:
+ pos = opening
+ else:
+ closing = "\n```"
+ min_code_pos = len(fence)
+ if content.startswith(fence + "\n"):
+ min_code_pos += 1
+ if pos < min_code_pos and min_code_pos + len(closing) > max_len:
+ chunks.append(content[:max_len])
+ content = content[max_len:].lstrip()
+ continue
+ if pos + len(closing) > max_len:
+ budget = max_len - len(closing)
+ if budget > 0:
+ recut = content[:budget]
+ adjusted = recut.rfind("\n")
+ if adjusted <= 0:
+ adjusted = recut.rfind(" ")
+ pos = adjusted if adjusted > 0 else budget
+ else:
+ closing = "```"
+ pos = max_len - len(closing)
+ chunks.append(content[:pos] + closing)
+ remainder = content[pos:]
+ if remainder.startswith("\n"):
+ remainder = remainder[1:]
+ content = f"{fence}\n{remainder}"
+ continue
+
+ chunks.append(content[:pos])
+ content = content[pos:].lstrip()
+ return chunks
+
+
+def _escape_telegram_html(text: str) -> str:
+ """Escape text for Telegram HTML parse mode."""
+ return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
+
+
+def _tool_hint_to_telegram_blockquote(text: str) -> str:
+ """Render tool hints as an expandable blockquote (collapsed by default)."""
+ return f"<blockquote expandable>{_escape_telegram_html(text)}</blockquote>" if text else ""
+
+
+def _strip_md(s: str) -> str:
+ """Strip markdown inline formatting from text."""
+ s = re.sub(r'\*\*(.+?)\*\*', r'\1', s)
+ s = re.sub(r'__(.+?)__', r'\1', s)
+ s = re.sub(r'~~(.+?)~~', r'\1', s)
+ s = re.sub(r'`([^`]+)`', r'\1', s)
+ return s.strip()
+
+
+def _strip_md_block(text: str) -> str:
+ """Strip block-level and inline markdown for readable plain-text preview.
+
+ Used during streaming mid-edits so users see clean text instead of raw
+ markdown syntax while the response is still being generated.
+ """
+ # Code blocks -> just the code
+ text = re.sub(r'```[\w]*\n?([\s\S]*?)```', r'\1', text)
+ # Headers -> plain text
+ text = re.sub(r'^#{1,6}\s+(.+)$', r'\1', text, flags=re.MULTILINE)
+ # Blockquotes
+ text = re.sub(r'^>\s*(.*)$', r'\1', text, flags=re.MULTILINE)
+ # Bold / italic / strikethrough
+ text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
+ text = re.sub(r'__(.+?)__', r'\1', text)
+ text = re.sub(r'(?<![a-zA-Z0-9])_([^_]+)_(?![a-zA-Z0-9])', r'\1', text)
+ text = re.sub(r'~~(.+?)~~', r'\1', text)
+ # Inline code
+ text = re.sub(r'`([^`]+)`', r'\1', text)
+ # Links [text](url) -> text
+ text = re.sub(r'\[([^\]]+)\]\([^)]+\)', r'\1', text)
+ # Bullet lists
+ text = re.sub(r'^[-*]\s+', '• ', text, flags=re.MULTILINE)
+ # Numbered lists (normalize spacing)
+ text = re.sub(r'^(\d+)\.\s+', r'\1. ', text, flags=re.MULTILINE)
+ return text
+
+
+def _render_table_box(table_lines: list[str]) -> str:
+ """Convert markdown pipe-table to compact aligned text for <pre> display."""
+
+ def dw(s: str) -> int:
+ return sum(2 if unicodedata.east_asian_width(c) in ('W', 'F') else 1 for c in s)
+
+ rows: list[list[str]] = []
+ has_sep = False
+ for line in table_lines:
+ cells = [_strip_md(c) for c in line.strip().strip('|').split('|')]
+ if all(re.match(r'^:?-+:?$', c) for c in cells if c):
+ has_sep = True
+ continue
+ rows.append(cells)
+ if not rows or not has_sep:
+ return '\n'.join(table_lines)
+
+ ncols = max(len(r) for r in rows)
+ for r in rows:
+ r.extend([''] * (ncols - len(r)))
+ widths = [max(dw(r[c]) for r in rows) for c in range(ncols)]
+
+ def dr(cells: list[str]) -> str:
+ return ' '.join(f'{c}{" " * (w - dw(c))}' for c, w in zip(cells, widths))
+
+ out = [dr(rows[0])]
+ out.append(' '.join('─' * w for w in widths))
+ for row in rows[1:]:
+ out.append(dr(row))
+ return '\n'.join(out)
+
+
+def _markdown_to_telegram_html(text: str) -> str:
+ """
+ Convert markdown to Telegram-safe HTML.
+ """
+ if not text:
+ return ""
+
+ # 1. Extract and protect code blocks (preserve content from other processing)
+ code_blocks: list[str] = []
+ def save_code_block(m: re.Match) -> str:
+ code_blocks.append(m.group(1))
+ return f"\x00CB{len(code_blocks) - 1}\x00"
+
+ text = re.sub(r'```[\w]*\n?([\s\S]*?)```', save_code_block, text)
+
+ # 1.5. Convert markdown tables to box-drawing (reuse code_block placeholders)
+ lines = text.split('\n')
+ rebuilt: list[str] = []
+ li = 0
+ while li < len(lines):
+ if re.match(r'^\s*\|.+\|', lines[li]):
+ tbl: list[str] = []
+ while li < len(lines) and re.match(r'^\s*\|.+\|', lines[li]):
+ tbl.append(lines[li])
+ li += 1
+ box = _render_table_box(tbl)
+ if box != '\n'.join(tbl):
+ code_blocks.append(box)
+ rebuilt.append(f"\x00CB{len(code_blocks) - 1}\x00")
+ else:
+ rebuilt.extend(tbl)
+ else:
+ rebuilt.append(lines[li])
+ li += 1
+ text = '\n'.join(rebuilt)
+
+ # 2. Extract and protect inline code
+ inline_codes: list[str] = []
+ def save_inline_code(m: re.Match) -> str:
+ inline_codes.append(m.group(1))
+ return f"\x00IC{len(inline_codes) - 1}\x00"
+
+ text = re.sub(r'`([^`]+)`', save_inline_code, text)
+
+ # 3. Headers # Title -> <b>Title</b> (preserve visual hierarchy)
+ text = re.sub(r'^#{1,6}\s+(.+)$', r'⟪B⟫\1⟪/B⟫', text, flags=re.MULTILINE)
+
+ # 4. Blockquotes > text -> just the text (before HTML escaping)
+ text = re.sub(r'^>\s*(.*)$', r'\1', text, flags=re.MULTILINE)
+
+ # 5. Escape HTML special characters
+ text = _escape_telegram_html(text)
+
+ # 6. Links [text](url) - must be before bold/italic to handle nested cases
+ text = re.sub(r'\[([^\]]+)\]\(([^)]+)\)', r'<a href="\2">\1</a>', text)
+
+ # 7. Bold **text** or __text__
+ text = re.sub(r'\*\*(.+?)\*\*', r'<b>\1</b>', text)
+ text = re.sub(r'__(.+?)__', r'<b>\1</b>', text)
+
+ # 8. Italic _text_ (avoid matching inside words like some_var_name)
+ text = re.sub(r'(?<![a-zA-Z0-9])_([^_]+)_(?![a-zA-Z0-9])', r'<i>\1</i>', text)
+
+ # 9. Strikethrough ~~text~~
+ text = re.sub(r'~~(.+?)~~', r'<s>\1</s>', text)
+
+ # 10. Bullet lists - item -> • item
+ text = re.sub(r'^[-*]\s+', '• ', text, flags=re.MULTILINE)
+
+ # 10.5. Numbered lists 1. item -> 1. item (keep number, normalize indent)
+ text = re.sub(r'^(\d+)\.\s+', r'\1. ', text, flags=re.MULTILINE)
+
+ # 11. Restore inline code with HTML tags
+ for i, code in enumerate(inline_codes):
+ # Escape HTML in code content
+ escaped = _escape_telegram_html(code)
+ text = text.replace(f"\x00IC{i}\x00", f"<code>{escaped}</code>")
+
+ # 12. Restore code blocks with HTML tags
+ for i, code in enumerate(code_blocks):
+ # Escape HTML in code content
+ escaped = _escape_telegram_html(code)
+ text = text.replace(f"\x00CB{i}\x00", f"<pre><code>{escaped}</code></pre>")
+
+ # 13. Restore header bold markers (inserted in step 3, after HTML escaping)
+ text = text.replace('⟪B⟫', '<b>').replace('⟪/B⟫', '</b>')
+
+ return text
+
+
+def _split_telegram_markdown_html(content: str, max_html_len: int) -> list[str]:
+ """Split raw Telegram Markdown and return HTML chunks within Telegram's limit."""
+ chunks: list[str] = []
+ pending = _split_telegram_markdown(content, TELEGRAM_MAX_MESSAGE_LEN)
+ while pending:
+ chunk = pending.pop(0)
+ html = _markdown_to_telegram_html(chunk)
+ if len(html) <= max_html_len:
+ chunks.append(html)
+ continue
+
+ # Markdown can expand when rendered as HTML (tags/entities). Re-split
+ # the raw markdown with a smaller budget instead of slicing HTML tags.
+ next_limit = max(1, int(len(chunk) * max_html_len / len(html)) - 8)
+ next_limit = min(next_limit, len(chunk) - 1)
+ if next_limit <= 0:
+ chunks.extend(split_message(html, max_html_len))
+ continue
+ parts = _split_telegram_markdown(chunk, next_limit)
+ if len(parts) == 1 and parts[0] == chunk:
+ chunks.extend(split_message(html, max_html_len))
+ continue
+ pending = parts + pending
+ return chunks
+
+
+_SEND_MAX_RETRIES = 3
+_SEND_RETRY_BASE_DELAY = 0.5 # seconds, doubled each retry
+_STREAM_EDIT_INTERVAL_DEFAULT = 0.6 # min seconds between edit_message_text calls
+
+
+@dataclass
+class _StreamBuf:
+ """Per-chat streaming accumulator for progressive message editing."""
+ text: str = ""
+ message_id: int | None = None
+ last_edit: float = 0.0
+ stream_id: str | None = None
+
+
+@dataclass
+class _QueuedTelegramUpdate:
+ """Telegram update staged for per-session ordered processing."""
+
+ kind: Literal["command", "message"]
+ update: Update
+ context: Any
+ sort_key: tuple[int, int]
+
+
+class TelegramConfig(BaseModel):
+ """Telegram channel configuration."""
+
+ enabled: bool = False
+ token: str = ""
+ mode: Literal["polling", "webhook"] = "polling"
+ allow_from: list[str] = Field(default_factory=list)
+ proxy: str | None = None
+ reply_to_message: bool = False
+ react_emoji: str = "👀"
+ group_policy: Literal["open", "mention"] = "mention"
+ connection_pool_size: int = 32
+ pool_timeout: float = 5.0
+ streaming: bool = True
+ # Enable inline keyboard buttons in Telegram messages.
+ inline_keyboards: bool = False
+ # Opt in to Bot API 10.1 sendRichMessage for richer markdown rendering.
+ rich_messages: bool = False
+ stream_edit_interval: float = Field(default=_STREAM_EDIT_INTERVAL_DEFAULT, ge=0.1)
+ webhook_url: str = ""
+ webhook_listen_host: str = "127.0.0.1"
+ webhook_listen_port: int = Field(default=8081, ge=1, le=65535)
+ webhook_path: str = "/telegram"
+ webhook_secret_token: str = ""
+ webhook_max_connections: int = Field(default=4, ge=1, le=100)
+
+ @field_validator("webhook_path")
+ @classmethod
+ def webhook_path_must_start_with_slash(cls, value: str) -> str:
+ value = value.strip() or "/telegram"
+ if not value.startswith("/"):
+ raise ValueError('webhook_path must start with "/"')
+ return value
+
+ @model_validator(mode="after")
+ def validate_webhook_config(self) -> "TelegramConfig":
+ if self.mode != "webhook":
+ return self
+
+ url = self.webhook_url.strip()
+ if not url:
+ raise ValueError("webhook_url is required when Telegram mode is webhook")
+ parsed = urlparse(url)
+ if parsed.scheme != "https" or not parsed.netloc:
+ raise ValueError("webhook_url must be a public HTTPS URL")
+ secret = self.webhook_secret_token.strip()
+ if not secret:
+ raise ValueError("webhook_secret_token is required when Telegram mode is webhook")
+ if len(secret) > 256 or re.match(r"^[A-Za-z0-9_-]+$", secret) is None:
+ raise ValueError(
+ "webhook_secret_token must be 1-256 characters using only A-Z, a-z, 0-9, _ and -"
+ )
+ return self
+
+
+class TelegramChannel(BaseChannel):
+ """
+ Telegram channel using long polling or webhook mode.
+
+ Long polling is the default. Webhook mode requires a public HTTPS URL and a
+ Telegram secret token.
+ """
+
+ name = "telegram"
+ display_name = "Telegram"
+
+ # Commands registered with Telegram's command menu
+ BOT_COMMANDS = [
+ BotCommand("start", "Start the bot"),
+ BotCommand("new", "Start a new conversation"),
+ BotCommand("stop", "Stop the current task"),
+ BotCommand("restart", "Restart the bot"),
+ BotCommand("status", "Show bot status"),
+ BotCommand("history", "Show recent conversation messages"),
+ BotCommand("goal", "Start a sustained objective (long-running task)"),
+ BotCommand("pairing", "Manage DM pairing (approve/deny/list)"),
+ BotCommand("model", "Switch runtime model preset"),
+ BotCommand("skill", "List enabled skills"),
+ BotCommand("dream", "Run Dream memory consolidation now"),
+ BotCommand("dream_log", "Show the latest Dream memory change"),
+ BotCommand("dream_restore", "Restore Dream memory to an earlier version"),
+ BotCommand("help", "Show available commands"),
+ ]
+
+ # Regex for slash commands routed to AgentLoop via ``_forward_command``.
+ # Hyphenated ``dream-*`` commands stay on a separate handler (below).
+ TELEGRAM_BUS_SLASH_COMMAND_RE = re.compile(
+ r"^/(?:new|stop|restart|status|dream|history|goal|pairing|model|skill)(?:@\w+)?(?:\s+.*)?$"
+ )
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return TelegramConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = TelegramConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: TelegramConfig = config
+ self._app: Application | None = None
+ self._chat_ids: dict[str, int] = {} # Map sender_id to chat_id for replies
+ self._typing_tasks: dict[str, asyncio.Task] = {} # chat_id -> typing loop task
+ self._media_group_buffers: dict[str, dict] = {}
+ self._media_group_tasks: dict[str, asyncio.Task] = {}
+ self._message_threads: dict[tuple[str, int], int] = {}
+ self._bot_user_id: int | None = None
+ self._bot_username: str | None = None
+ self._stream_bufs: dict[str, _StreamBuf] = {} # chat_id -> streaming state
+ self._inbound_buffers: dict[str, list[_QueuedTelegramUpdate]] = {}
+ self._inbound_workers: dict[str, asyncio.Task] = {}
+ self._rich_send_disabled: bool = False # Latch off if Bot API < 10.1
+
+ def is_allowed(self, sender_id: str) -> bool:
+ """Preserve Telegram's legacy id|username allowlist matching."""
+ if super().is_allowed(sender_id):
+ return True
+
+ allow_list = getattr(self.config, "allow_from", [])
+ if not allow_list or "*" in allow_list:
+ return False
+
+ sender_str = str(sender_id)
+ if sender_str.count("|") != 1:
+ return False
+
+ sid, username = sender_str.split("|", 1)
+ if not sid.isdigit() or not username:
+ return False
+
+ return sid in allow_list or username in allow_list
+
+ @staticmethod
+ def _normalize_telegram_command(content: str) -> str:
+ """Map Telegram-safe command aliases back to canonical vibe-trading commands."""
+ if not content.startswith("/"):
+ return content
+ if content == "/dream_log" or content.startswith("/dream_log "):
+ return content.replace("/dream_log", "/dream-log", 1)
+ if content == "/dream_restore" or content.startswith("/dream_restore "):
+ return content.replace("/dream_restore", "/dream-restore", 1)
+ return content
+
+ async def start(self) -> None:
+ """Start the Telegram bot."""
+ if not self.config.token:
+ self.logger.error("bot token not configured")
+ return
+
+ self._running = True
+
+ proxy = self.config.proxy or None
+
+ # Separate pools so long-polling (getUpdates) never starves outbound sends.
+ api_request = HTTPXRequest(
+ connection_pool_size=self.config.connection_pool_size,
+ pool_timeout=self.config.pool_timeout,
+ connect_timeout=30.0,
+ read_timeout=30.0,
+ proxy=proxy,
+ )
+ poll_request = HTTPXRequest(
+ connection_pool_size=4,
+ pool_timeout=self.config.pool_timeout,
+ connect_timeout=30.0,
+ read_timeout=30.0,
+ proxy=proxy,
+ )
+ builder = (
+ Application.builder()
+ .token(self.config.token)
+ .request(api_request)
+ .get_updates_request(poll_request)
+ )
+ self._app = builder.build()
+ self._app.add_error_handler(self._on_error)
+
+ # Add command handlers (using Regex to support @username suffixes before bot initialization)
+ self._app.add_handler(MessageHandler(filters.Regex(r"^/start(?:@\w+)?$"), self._on_start))
+ self._app.add_handler(
+ MessageHandler(
+ filters.Regex(TelegramChannel.TELEGRAM_BUS_SLASH_COMMAND_RE),
+ self._forward_command,
+ )
+ )
+ self._app.add_handler(
+ MessageHandler(
+ filters.Regex(r"^/(dream-log|dream_log|dream-restore|dream_restore)(?:@\w+)?(?:\s+.*)?$"),
+ self._forward_command,
+ )
+ )
+ self._app.add_handler(MessageHandler(filters.Regex(r"^/help(?:@\w+)?$"), self._on_help))
+
+ # Add message handler for text, photos, video, voice, documents, and locations
+ self._app.add_handler(
+ MessageHandler(
+ (filters.TEXT | filters.PHOTO | filters.VIDEO | filters.VIDEO_NOTE
+ | filters.ANIMATION | filters.VOICE | filters.AUDIO
+ | filters.Document.ALL | filters.LOCATION)
+ & ~filters.COMMAND,
+ self._on_message
+ )
+ )
+
+ # Conditionally register inline keyboard callback handler
+ if self.config.inline_keyboards:
+ self._app.add_handler(CallbackQueryHandler(self._on_callback_query))
+ allowed_updates = ["message", "callback_query"]
+ self.logger.debug("inline keyboards enabled")
+ else:
+ allowed_updates = ["message"]
+
+ if self.config.mode == "webhook":
+ self.logger.info("Starting bot (webhook mode)...")
+ else:
+ self.logger.info("Starting bot (polling mode)...")
+
+ # Initialize and start receiving updates
+ await self._app.initialize()
+ await self._app.start()
+
+ # Get bot info and register command menu
+ bot_info = await self._app.bot.get_me()
+ self._bot_user_id = getattr(bot_info, "id", None)
+ self._bot_username = getattr(bot_info, "username", None)
+ self.logger.info("bot @{} connected", bot_info.username)
+
+ try:
+ await self._app.bot.set_my_commands(self.BOT_COMMANDS)
+ self.logger.debug("bot commands registered")
+ except Exception as e:
+ self.logger.warning("Failed to register bot commands: {}", e)
+
+ if self.config.mode == "webhook":
+ # ``url_path`` is the local HTTP route. ``webhook_url`` is the
+ # public HTTPS URL Telegram calls; reverse proxies may rewrite it.
+ await self._app.updater.start_webhook(
+ listen=self.config.webhook_listen_host,
+ port=self.config.webhook_listen_port,
+ url_path=self.config.webhook_path.lstrip("/"),
+ webhook_url=self.config.webhook_url.strip(),
+ allowed_updates=allowed_updates,
+ drop_pending_updates=False,
+ secret_token=self.config.webhook_secret_token.strip(),
+ max_connections=self.config.webhook_max_connections,
+ )
+ else:
+ # Start polling (this runs until stopped)
+ await self._app.updater.start_polling(
+ allowed_updates=allowed_updates,
+ drop_pending_updates=False, # Process pending messages on startup
+ error_callback=self._on_polling_error,
+ )
+
+ # Keep running until stopped
+ while self._running:
+ await asyncio.sleep(1)
+
+ async def stop(self) -> None:
+ """Stop the Telegram bot."""
+ self._running = False
+
+ # Cancel all typing indicators
+ for chat_id in list(self._typing_tasks):
+ self._stop_typing(chat_id)
+
+ for task in self._media_group_tasks.values():
+ task.cancel()
+ self._media_group_tasks.clear()
+ self._media_group_buffers.clear()
+
+ for task in self._inbound_workers.values():
+ task.cancel()
+ self._inbound_workers.clear()
+ self._inbound_buffers.clear()
+
+ if self._app:
+ self.logger.info("Stopping bot...")
+ await self._app.updater.stop()
+ await self._app.stop()
+ await self._app.shutdown()
+ self._app = None
+
+ @staticmethod
+ def _get_media_type(path: str) -> str:
+ """Guess media type from file extension."""
+ ext = path.rsplit(".", 1)[-1].lower() if "." in path else ""
+ if ext in ("jpg", "jpeg", "png", "gif", "webp"):
+ return "photo"
+ if ext in ("mp4", "mov", "avi", "mkv", "webm", "3gp"):
+ return "video"
+ if ext == "ogg":
+ return "voice"
+ if ext in ("mp3", "m4a", "wav", "aac"):
+ return "audio"
+ return "document"
+
+ @staticmethod
+ def _is_remote_media_url(path: str) -> bool:
+ return path.startswith(("http://", "https://"))
+
+ @staticmethod
+ def _is_rich_capability_error(exc: Exception) -> bool:
+ """True when the error indicates sendRichMessage is unavailable."""
+ err = str(exc).lower()
+ return (
+ "method not found" in err
+ or "unknown method" in err
+ or "bad request: invalid parameter" in err
+ )
+
+ async def _try_send_rich(
+ self,
+ chat_id: int,
+ content: str,
+ reply_params=None,
+ thread_kwargs: dict | None = None,
+ reply_markup=None,
+ ) -> bool:
+ """Attempt sendRichMessage (Bot API 10.1). Returns True on success."""
+ if not self._app:
+ return False
+
+ payload: dict[str, Any] = {
+ "chat_id": chat_id,
+ "rich_message": {
+ "markdown": content,
+ },
+ }
+ if reply_params is not None:
+ # sendRichMessage uses reply_parameters (object), not reply_to_message_id.
+ if hasattr(reply_params, "message_id"):
+ payload["reply_parameters"] = {
+ "message_id": reply_params.message_id,
+ "allow_sending_without_reply": True,
+ }
+ else:
+ payload["reply_parameters"] = reply_params
+ if thread_kwargs:
+ payload.update({k: v for k, v in thread_kwargs.items() if v is not None})
+ if reply_markup is not None:
+ payload["reply_markup"] = reply_markup
+
+ try:
+ await self._call_with_retry(
+ self._app.bot.do_api_request,
+ "sendRichMessage",
+ api_kwargs=payload,
+ )
+ return True
+ except BadRequest as exc:
+ if self._is_rich_capability_error(exc):
+ self.logger.debug("sendRichMessage not available, disabling")
+ self._rich_send_disabled = True
+ else:
+ self.logger.debug("sendRichMessage rejected: {}", exc)
+ return False
+ except Exception as exc:
+ err_str = str(exc).lower()
+ is_timeout = "timed out" in err_str or isinstance(exc, TimedOut)
+ if is_timeout:
+ self.logger.debug("sendRichMessage timeout, falling back to legacy path")
+ return False
+ self.logger.debug("sendRichMessage failed: {}", exc)
+ return False
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through Telegram."""
+ if not self._app:
+ self.logger.warning("bot not running")
+ return
+
+ # Only stop typing indicator and remove reaction for final responses
+ if not msg.metadata.get("_progress", False):
+ self._stop_typing(msg.chat_id)
+ if reply_to_message_id := msg.metadata.get("message_id"):
+ with suppress(ValueError):
+ await self._remove_reaction(msg.chat_id, int(reply_to_message_id))
+
+ try:
+ chat_id = int(msg.chat_id)
+ except ValueError:
+ self.logger.exception("Invalid chat_id: {}", msg.chat_id)
+ return
+ reply_to_message_id = msg.metadata.get("message_id")
+ message_thread_id = msg.metadata.get("message_thread_id")
+ if message_thread_id is None and reply_to_message_id is not None:
+ message_thread_id = self._message_threads.get((msg.chat_id, reply_to_message_id))
+ thread_kwargs = {}
+ if message_thread_id is not None:
+ thread_kwargs["message_thread_id"] = message_thread_id
+
+ reply_params = None
+ if self.config.reply_to_message:
+ if reply_to_message_id:
+ reply_params = ReplyParameters(
+ message_id=reply_to_message_id,
+ allow_sending_without_reply=True
+ )
+
+ # Send media files
+ for media_path in (msg.media or []):
+ try:
+ media_type = self._get_media_type(media_path)
+ sender = {
+ "photo": self._app.bot.send_photo,
+ "video": self._app.bot.send_video,
+ "voice": self._app.bot.send_voice,
+ "audio": self._app.bot.send_audio,
+ }.get(media_type, self._app.bot.send_document)
+ param = {
+ "photo": "photo",
+ "video": "video",
+ "voice": "voice",
+ "audio": "audio",
+ }.get(media_type, "document")
+ extra: dict[str, Any] = {}
+ if media_type == "video":
+ extra["supports_streaming"] = True
+
+ # Telegram Bot API accepts HTTP(S) URLs directly for media params.
+ if self._is_remote_media_url(media_path):
+ ok, error = validate_url_target(media_path)
+ if not ok:
+ raise ValueError(f"unsafe media URL: {error}")
+ await self._call_with_retry(
+ sender,
+ chat_id=chat_id,
+ **{param: media_path},
+ reply_parameters=reply_params,
+ **thread_kwargs,
+ **extra,
+ )
+ continue
+
+ media_bytes = Path(media_path).read_bytes()
+ filename = Path(media_path).name
+ send_kwargs = {param: media_bytes, "filename": filename}
+ await self._call_with_retry(
+ sender,
+ chat_id=chat_id,
+ reply_parameters=reply_params,
+ **thread_kwargs,
+ **extra,
+ **send_kwargs,
+ )
+ except Exception:
+ filename = media_path.rsplit("/", 1)[-1]
+ self.logger.exception("Failed to send media {}", media_path)
+ await self._app.bot.send_message(
+ chat_id=chat_id,
+ text=f"[Failed to send: {filename}]",
+ reply_parameters=reply_params,
+ **thread_kwargs,
+ )
+
+ # Send text content
+ if msg.content and msg.content != "[empty message]":
+ render_as_blockquote = bool(msg.metadata.get("_tool_hint"))
+ buttons = getattr(msg, "buttons", None) or []
+ reply_markup = self._build_keyboard(buttons) if buttons else None
+ text = msg.content
+ # Fallback: no native keyboard → splice labels into the message so the choices survive.
+ if buttons and reply_markup is None:
+ text = f"{text}\n\n{self._buttons_as_text(buttons)}"
+
+ # Bot API 10.1 rich fast-path: send raw markdown via sendRichMessage.
+ # All non-blockquote content tries rich first; _rich_send_disabled
+ # latches off permanently if the server doesn't support it.
+ if (
+ not render_as_blockquote
+ and self.config.rich_messages
+ and not getattr(self, "_rich_send_disabled", False)
+ ):
+ rich_ok = await self._try_send_rich(
+ chat_id, text, reply_params, thread_kwargs, reply_markup,
+ )
+ if rich_ok:
+ return
+
+ chunks = _split_telegram_markdown(text, TELEGRAM_MAX_MESSAGE_LEN)
+ for i, chunk in enumerate(chunks):
+ is_last = (i == len(chunks) - 1)
+ await self._send_text(
+ chat_id, chunk, reply_params, thread_kwargs,
+ render_as_blockquote=render_as_blockquote,
+ reply_markup=reply_markup if is_last else None,
+ )
+
+ async def _call_with_retry(self, fn, *args, **kwargs):
+ """Call an async Telegram API function with retry on pool/network timeout and RetryAfter."""
+ from telegram.error import RetryAfter
+
+ for attempt in range(1, _SEND_MAX_RETRIES + 1):
+ try:
+ return await fn(*args, **kwargs)
+ except TimedOut:
+ if attempt == _SEND_MAX_RETRIES:
+ raise
+ delay = _SEND_RETRY_BASE_DELAY * (2 ** (attempt - 1))
+ self.logger.warning(
+ "timeout (attempt {}/{}), retrying in {:.1f}s",
+ attempt, _SEND_MAX_RETRIES, delay,
+ )
+ await asyncio.sleep(delay)
+ except RetryAfter as e:
+ if attempt == _SEND_MAX_RETRIES:
+ raise
+ delay = float(e.retry_after)
+ self.logger.warning(
+ "Flood Control (attempt {}/{}), retrying in {:.1f}s",
+ attempt, _SEND_MAX_RETRIES, delay,
+ )
+ await asyncio.sleep(delay)
+
+ async def _send_text(
+ self,
+ chat_id: int,
+ text: str,
+ reply_params=None,
+ thread_kwargs: dict | None = None,
+ render_as_blockquote: bool = False,
+ reply_markup=None,
+ ) -> None:
+ """Send a plain text message with HTML fallback."""
+ try:
+ html = _tool_hint_to_telegram_blockquote(text) if render_as_blockquote else _markdown_to_telegram_html(text)
+ await self._call_with_retry(
+ self._app.bot.send_message,
+ chat_id=chat_id, text=html, parse_mode="HTML",
+ reply_parameters=reply_params,
+ reply_markup=reply_markup,
+ **(thread_kwargs or {}),
+ )
+ except BadRequest as e:
+ self.logger.warning("HTML parse failed, falling back to plain text: {}", e)
+ try:
+ await self._call_with_retry(
+ self._app.bot.send_message,
+ chat_id=chat_id,
+ text=text,
+ reply_parameters=reply_params,
+ reply_markup=reply_markup,
+ **(thread_kwargs or {}),
+ )
+ except Exception:
+ self.logger.exception("Error sending message")
+ raise
+
+ @staticmethod
+ def _is_not_modified_error(exc: Exception) -> bool:
+ return isinstance(exc, BadRequest) and "message is not modified" in str(exc).lower()
+
+ async def send_delta(self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None) -> None:
+ """Progressive message editing: send on first delta, edit on subsequent ones."""
+ if not self._app:
+ return
+ meta = metadata or {}
+ int_chat_id = int(chat_id)
+ stream_id = meta.get("_stream_id")
+
+ if meta.get("_stream_end"):
+ buf = self._stream_bufs.get(chat_id)
+ if not buf or not buf.message_id or not buf.text:
+ return
+ if stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id:
+ return
+ self._stop_typing(chat_id)
+ if reply_to_message_id := meta.get("message_id"):
+ with suppress(ValueError):
+ await self._remove_reaction(chat_id, int(reply_to_message_id))
+ thread_kwargs = {}
+ if message_thread_id := meta.get("message_thread_id"):
+ thread_kwargs["message_thread_id"] = message_thread_id
+ raw_text = buf.text
+
+ # Try sendRichMessage for final output (Bot API 10.1).
+ # Skip when a streaming preview already exists to avoid the
+ # delete-and-resend pattern that causes flickering and drops
+ # line breaks (issue #4470).
+ if not buf.message_id and self.config.rich_messages and not getattr(self, "_rich_send_disabled", False):
+ reply_params = None
+ if reply_to_message_id := meta.get("message_id"):
+ reply_params = {"message_id": int(reply_to_message_id), "allow_sending_without_reply": True}
+ rich_ok = await self._try_send_rich(
+ int_chat_id, raw_text, reply_params, thread_kwargs, None,
+ )
+ if rich_ok:
+ # Delete the streaming preview message
+ try:
+ await self._call_with_retry(
+ self._app.bot.delete_message,
+ chat_id=int_chat_id, message_id=buf.message_id,
+ )
+ except Exception:
+ pass # Preview stays if delete fails
+ self._stream_bufs.pop(chat_id, None)
+ return
+
+ # Legacy path: edit existing streaming message with HTML
+ html_chunks = _split_telegram_markdown_html(raw_text, TELEGRAM_HTML_MAX_LEN)
+ primary_html = html_chunks[0]
+ extra_html_chunks = html_chunks[1:]
+ try:
+ await self._call_with_retry(
+ self._app.bot.edit_message_text,
+ chat_id=int_chat_id, message_id=buf.message_id,
+ text=primary_html, parse_mode="HTML",
+ )
+ except BadRequest as e:
+ # Only fall back to plain text on actual HTML parse/format errors.
+ # Network errors (TimedOut, NetworkError) should propagate immediately
+ # to avoid doubling connection demand during pool exhaustion.
+ if self._is_not_modified_error(e):
+ self.logger.debug("Final stream edit already applied for {}", chat_id)
+ self._stream_bufs.pop(chat_id, None)
+ return
+ self.logger.debug("Final stream edit failed (HTML), trying plain: {}", e)
+ # Fall back to raw markdown (not HTML) so users don't see raw tags.
+ primary_plain = split_message(raw_text, TELEGRAM_MAX_MESSAGE_LEN)[0] if len(raw_text) > TELEGRAM_MAX_MESSAGE_LEN else raw_text
+ try:
+ await self._call_with_retry(
+ self._app.bot.edit_message_text,
+ chat_id=int_chat_id, message_id=buf.message_id,
+ text=primary_plain,
+ )
+ except Exception as e2:
+ if self._is_not_modified_error(e2):
+ self.logger.debug("Final stream plain edit already applied for {}", chat_id)
+ else:
+ self.logger.warning("Final stream edit failed: {}", e2)
+ raise # Let ChannelManager handle retry
+ for extra_html_chunk in extra_html_chunks:
+ try:
+ await self._call_with_retry(
+ self._app.bot.send_message,
+ chat_id=int_chat_id, text=extra_html_chunk,
+ parse_mode="HTML",
+ **thread_kwargs,
+ )
+ except Exception:
+ # Fall back to _send_text which handles HTML→plain gracefully.
+ await self._send_text(int_chat_id, extra_html_chunk)
+ self._stream_bufs.pop(chat_id, None)
+ return
+
+ buf = self._stream_bufs.get(chat_id)
+ if buf is None or (stream_id is not None and buf.stream_id is not None and buf.stream_id != stream_id):
+ buf = _StreamBuf(stream_id=stream_id)
+ self._stream_bufs[chat_id] = buf
+ elif buf.stream_id is None:
+ buf.stream_id = stream_id
+ buf.text += delta
+
+ if not buf.text.strip():
+ return
+
+ now = time.monotonic()
+ thread_kwargs = {}
+ if message_thread_id := meta.get("message_thread_id"):
+ thread_kwargs["message_thread_id"] = message_thread_id
+ if buf.message_id is None:
+ preview = _strip_md_block(buf.text)
+ try:
+ sent = await self._call_with_retry(
+ self._app.bot.send_message,
+ chat_id=int_chat_id, text=preview,
+ **thread_kwargs,
+ )
+ buf.message_id = sent.message_id
+ buf.last_edit = now
+ except Exception as e:
+ self.logger.warning("Stream initial send failed: {}", e)
+ raise # Let ChannelManager handle retry
+ elif (now - buf.last_edit) >= self.config.stream_edit_interval:
+ if len(buf.text) > TELEGRAM_MAX_MESSAGE_LEN:
+ await self._flush_stream_overflow(int_chat_id, buf, thread_kwargs)
+ buf.last_edit = now
+ return
+ preview = _strip_md_block(buf.text)
+ try:
+ await self._call_with_retry(
+ self._app.bot.edit_message_text,
+ chat_id=int_chat_id, message_id=buf.message_id,
+ text=preview,
+ )
+ buf.last_edit = now
+ except Exception as e:
+ if self._is_not_modified_error(e):
+ buf.last_edit = now
+ return
+ self.logger.warning("Stream edit failed: {}", e)
+ raise # Let ChannelManager handle retry
+
+ async def _flush_stream_overflow(
+ self,
+ chat_id: int,
+ buf: "_StreamBuf",
+ thread_kwargs: dict,
+ ) -> None:
+ """Split an oversized stream buffer mid-flight.
+
+ Edits the current stream message with the first chunk, sends any
+ intermediate chunks as standalone messages, then opens a new message
+ for the tail so subsequent deltas continue streaming into it.
+ """
+ chunks = _split_telegram_markdown(buf.text, TELEGRAM_MAX_MESSAGE_LEN)
+ if len(chunks) <= 1:
+ return
+ try:
+ await self._call_with_retry(
+ self._app.bot.edit_message_text,
+ chat_id=chat_id, message_id=buf.message_id,
+ text=chunks[0],
+ )
+ except Exception as e:
+ if not self._is_not_modified_error(e):
+ self.logger.warning("Stream overflow edit failed: {}", e)
+ raise
+ for chunk in chunks[1:-1]:
+ await self._call_with_retry(
+ self._app.bot.send_message,
+ chat_id=chat_id, text=chunk, **thread_kwargs,
+ )
+ tail = chunks[-1]
+ sent = await self._call_with_retry(
+ self._app.bot.send_message,
+ chat_id=chat_id, text=tail, **thread_kwargs,
+ )
+ buf.message_id = sent.message_id
+ buf.text = tail
+
+ async def _on_start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Handle /start command."""
+ if not update.message or not update.effective_user:
+ return
+
+ user = update.effective_user
+ sender_id = self._sender_id(user)
+ if not self.is_allowed(sender_id):
+ await self._send_pairing_code_if_private(sender_id, update.message, user)
+ return
+ await update.message.reply_text(
+ f"👋 Hi {user.first_name}! I'm vibe-trading.\n\n"
+ "Send me a message and I'll respond!\n"
+ "Type /help to see available commands."
+ )
+
+ async def _on_help(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Handle /help command for allowed users only."""
+ if not update.message or not update.effective_user:
+ return
+ user = update.effective_user
+ sender_id = self._sender_id(user)
+ if not self.is_allowed(sender_id):
+ await self._send_pairing_code_if_private(sender_id, update.message, user)
+ return
+ await update.message.reply_text("Use /help for available commands. Docs: https://github.com/HKUDS/Vibe-Trading")
+
+ @staticmethod
+ def _sender_id(user) -> str:
+ """Build sender_id with username for allowlist matching."""
+ sid = str(user.id)
+ return f"{sid}|{user.username}" if user.username else sid
+
+ async def _send_pairing_code_if_private(self, sender_id: str, message, user) -> None:
+ if message.chat.type != "private":
+ return
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=str(message.chat_id),
+ content="",
+ metadata=self._build_message_metadata(message, user),
+ is_dm=True,
+ )
+
+ @staticmethod
+ def _derive_topic_session_key(message) -> str | None:
+ """Derive topic-scoped session key for Telegram chats with threads."""
+ message_thread_id = getattr(message, "message_thread_id", None)
+ if message_thread_id is None:
+ return None
+ return f"telegram:{message.chat_id}:topic:{message_thread_id}"
+
+ @staticmethod
+ def _build_message_metadata(message, user) -> dict:
+ """Build common Telegram inbound metadata payload."""
+ reply_to = getattr(message, "reply_to_message", None)
+ return {
+ "message_id": message.message_id,
+ "user_id": user.id,
+ "username": user.username,
+ "first_name": user.first_name,
+ "is_group": message.chat.type != "private",
+ "message_thread_id": getattr(message, "message_thread_id", None),
+ "is_forum": bool(getattr(message.chat, "is_forum", False)),
+ "reply_to_message_id": getattr(reply_to, "message_id", None) if reply_to else None,
+ }
+
+ async def _extract_reply_context(self, message) -> str | None:
+ """Extract text from the message being replied to, if any."""
+ reply = getattr(message, "reply_to_message", None)
+ if not reply:
+ return None
+ text = getattr(reply, "text", None) or getattr(reply, "caption", None) or ""
+ if len(text) > TELEGRAM_REPLY_CONTEXT_MAX_LEN:
+ text = text[:TELEGRAM_REPLY_CONTEXT_MAX_LEN] + "..."
+
+ if not text:
+ return None
+
+ bot_id, _ = await self._ensure_bot_identity()
+ reply_user = getattr(reply, "from_user", None)
+
+ if bot_id and reply_user and getattr(reply_user, "id", None) == bot_id:
+ return f"[Reply to bot: {text}]"
+ elif reply_user and getattr(reply_user, "username", None):
+ return f"[Reply to @{reply_user.username}: {text}]"
+ elif reply_user and getattr(reply_user, "first_name", None):
+ return f"[Reply to {reply_user.first_name}: {text}]"
+ else:
+ return f"[Reply to: {text}]"
+
+ async def _download_message_media(
+ self, msg, *, add_failure_content: bool = False
+ ) -> tuple[list[str], list[str]]:
+ """Download media from a message (current or reply). Returns (media_paths, content_parts)."""
+ media_file = None
+ media_type = None
+ if getattr(msg, "photo", None):
+ media_file = msg.photo[-1]
+ media_type = "image"
+ elif getattr(msg, "voice", None):
+ media_file = msg.voice
+ media_type = "voice"
+ elif getattr(msg, "audio", None):
+ media_file = msg.audio
+ media_type = "audio"
+ elif getattr(msg, "document", None):
+ media_file = msg.document
+ media_type = "file"
+ elif getattr(msg, "video", None):
+ media_file = msg.video
+ media_type = "video"
+ elif getattr(msg, "video_note", None):
+ media_file = msg.video_note
+ media_type = "video"
+ elif getattr(msg, "animation", None):
+ media_file = msg.animation
+ media_type = "animation"
+ if not media_file or not self._app:
+ return [], []
+ try:
+ file = await self._app.bot.get_file(media_file.file_id)
+ ext = self._get_extension(
+ media_type,
+ getattr(media_file, "mime_type", None),
+ getattr(media_file, "file_name", None),
+ )
+ media_dir = get_media_dir("telegram")
+ unique_id = getattr(media_file, "file_unique_id", media_file.file_id)
+ file_path = media_dir / f"{unique_id}{ext}"
+ await file.download_to_drive(str(file_path))
+ path_str = str(file_path)
+ if media_type in ("voice", "audio"):
+ transcription = await self.transcribe_audio(file_path)
+ if transcription:
+ self.logger.info("Transcribed {}: {}...", media_type, transcription[:50])
+ return [path_str], [f"[transcription: {transcription}]"]
+ return [path_str], [f"[{media_type}: {path_str}]"]
+ return [path_str], [f"[{media_type}: {path_str}]"]
+ except Exception as e:
+ self.logger.warning("Failed to download message media: {}", e)
+ if add_failure_content:
+ return [], [f"[{media_type}: download failed]"]
+ return [], []
+
+ async def _ensure_bot_identity(self) -> tuple[int | None, str | None]:
+ """Load bot identity once and reuse it for mention/reply checks."""
+ if self._bot_user_id is not None or self._bot_username is not None:
+ return self._bot_user_id, self._bot_username
+ if not self._app:
+ return None, None
+ bot_info = await self._app.bot.get_me()
+ self._bot_user_id = getattr(bot_info, "id", None)
+ self._bot_username = getattr(bot_info, "username", None)
+ return self._bot_user_id, self._bot_username
+
+ @staticmethod
+ def _has_mention_entity(
+ text: str,
+ entities,
+ bot_username: str,
+ bot_id: int | None,
+ ) -> bool:
+ """Check Telegram mention entities against the bot username."""
+ handle = f"@{bot_username}".lower()
+ for entity in entities or []:
+ entity_type = getattr(entity, "type", None)
+ if entity_type == "text_mention":
+ user = getattr(entity, "user", None)
+ if user is not None and bot_id is not None and getattr(user, "id", None) == bot_id:
+ return True
+ continue
+ if entity_type != "mention":
+ continue
+ offset = getattr(entity, "offset", None)
+ length = getattr(entity, "length", None)
+ if offset is None or length is None:
+ continue
+ if text[offset : offset + length].lower() == handle:
+ return True
+ return handle in text.lower()
+
+ async def _is_group_message_for_bot(self, message) -> bool:
+ """Allow group messages when policy is open, @mentioned, or replying to the bot."""
+ if message.chat.type == "private" or self.config.group_policy == "open":
+ return True
+
+ bot_id, bot_username = await self._ensure_bot_identity()
+ if bot_username:
+ text = message.text or ""
+ caption = message.caption or ""
+ if self._has_mention_entity(
+ text,
+ getattr(message, "entities", None),
+ bot_username,
+ bot_id,
+ ):
+ return True
+ if self._has_mention_entity(
+ caption,
+ getattr(message, "caption_entities", None),
+ bot_username,
+ bot_id,
+ ):
+ return True
+
+ reply_user = getattr(getattr(message, "reply_to_message", None), "from_user", None)
+ return bool(bot_id and reply_user and reply_user.id == bot_id)
+
+ def _remember_thread_context(self, message) -> None:
+ """Cache Telegram thread context by chat/message id for follow-up replies."""
+ message_thread_id = getattr(message, "message_thread_id", None)
+ if message_thread_id is None:
+ return
+ key = (str(message.chat_id), message.message_id)
+ self._message_threads[key] = message_thread_id
+ if len(self._message_threads) > 1000:
+ self._message_threads.pop(next(iter(self._message_threads)))
+
+ @staticmethod
+ def _queue_key_for_message(message) -> str:
+ """Return the final vibe-trading session key used for ordered Telegram ingress."""
+ return TelegramChannel._derive_topic_session_key(message) or f"telegram:{message.chat_id}"
+
+ @staticmethod
+ def _sort_key_for_update(update: Update) -> tuple[int, int]:
+ """Sort by chat message id first, then Telegram update id."""
+ message = getattr(update, "message", None)
+ message_id = int(getattr(message, "message_id", 0) or 0)
+ update_id = int(getattr(update, "update_id", 0) or 0)
+ return (message_id, update_id)
+
+ def _enqueue_ordered_update(
+ self,
+ *,
+ kind: Literal["command", "message"],
+ update: Update,
+ context: ContextTypes.DEFAULT_TYPE,
+ ) -> None:
+ """Stage a Telegram update behind a short per-session reorder window."""
+ message = update.message
+ key = self._queue_key_for_message(message)
+ self._inbound_buffers.setdefault(key, []).append(
+ _QueuedTelegramUpdate(
+ kind=kind,
+ update=update,
+ context=context,
+ sort_key=self._sort_key_for_update(update),
+ )
+ )
+ if key not in self._inbound_workers:
+ self._inbound_workers[key] = asyncio.create_task(
+ self._drain_ordered_updates(key)
+ )
+
+ async def _drain_ordered_updates(self, key: str) -> None:
+ """Drain one Telegram session buffer in stable message order."""
+ try:
+ while self._running:
+ await asyncio.sleep(0.2)
+ batch = self._inbound_buffers.get(key, [])
+ if not batch:
+ break
+ self._inbound_buffers[key] = []
+ batch.sort(key=lambda item: item.sort_key)
+ for item in batch:
+ try:
+ if item.kind == "command":
+ await self._process_forward_command(item.update, item.context)
+ else:
+ await self._process_message_update(item.update, item.context)
+ except Exception as e:
+ self.logger.warning(
+ "Telegram queued update handling failed for {}: {}",
+ key,
+ e,
+ )
+ if not self._inbound_buffers.get(key):
+ self._inbound_buffers.pop(key, None)
+ except asyncio.CancelledError:
+ raise
+ except Exception as e:
+ self.logger.warning("Telegram ordered update worker failed for {}: {}", key, e)
+ finally:
+ if not self._inbound_buffers.get(key):
+ self._inbound_workers.pop(key, None)
+
+ async def _forward_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Forward slash commands to the bus for unified handling in AgentLoop."""
+ if not update.message or not update.effective_user:
+ return
+ if not self._running:
+ await self._process_forward_command(update, context)
+ return
+ self._enqueue_ordered_update(kind="command", update=update, context=context)
+
+ async def _process_forward_command(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Process a queued slash command."""
+ message = update.message
+ user = update.effective_user
+ sender_id = self._sender_id(user)
+ if not self.is_allowed(sender_id):
+ await self._send_pairing_code_if_private(sender_id, message, user)
+ return
+ self._remember_thread_context(message)
+
+ # Strip @bot_username suffix if present
+ content = message.text or ""
+ if content.startswith("/") and "@" in content:
+ cmd_part, *rest = content.split(" ", 1)
+ cmd_part = cmd_part.split("@")[0]
+ content = f"{cmd_part} {rest[0]}" if rest else cmd_part
+ content = self._normalize_telegram_command(content)
+
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=str(message.chat_id),
+ content=content,
+ metadata=self._build_message_metadata(message, user),
+ session_key=self._derive_topic_session_key(message),
+ is_dm=message.chat.type == "private",
+ )
+
+ async def _on_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Handle incoming messages (text, photos, voice, documents)."""
+ if not update.message or not update.effective_user:
+ return
+ if not self._running:
+ await self._process_message_update(update, context)
+ return
+ self._enqueue_ordered_update(kind="message", update=update, context=context)
+
+ async def _process_message_update(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Process a queued Telegram message update."""
+
+ message = update.message
+ user = update.effective_user
+ chat_id = message.chat_id
+ sender_id = self._sender_id(user)
+ if not self.is_allowed(sender_id):
+ await self._send_pairing_code_if_private(sender_id, message, user)
+ return
+ self._remember_thread_context(message)
+
+ # Store chat_id for replies
+ self._chat_ids[sender_id] = chat_id
+
+ if not await self._is_group_message_for_bot(message):
+ return
+
+ # Build content from text and/or media
+ content_parts = []
+ media_paths = []
+
+ # Text content
+ if message.text:
+ content_parts.append(message.text)
+ if message.caption:
+ content_parts.append(message.caption)
+
+ # Location content
+ if message.location:
+ lat = message.location.latitude
+ lon = message.location.longitude
+ content_parts.append(f"[location: {lat}, {lon}]")
+
+ # Download current message media
+ current_media_paths, current_media_parts = await self._download_message_media(
+ message, add_failure_content=True
+ )
+ media_paths.extend(current_media_paths)
+ content_parts.extend(current_media_parts)
+ if current_media_paths:
+ self.logger.debug("Downloaded message media to {}", current_media_paths[0])
+
+ # Reply context: text and/or media from the replied-to message
+ reply = getattr(message, "reply_to_message", None)
+ if reply is not None:
+ reply_ctx = await self._extract_reply_context(message)
+ reply_media, reply_media_parts = await self._download_message_media(reply)
+ if reply_media:
+ media_paths = reply_media + media_paths
+ self.logger.debug("Attached replied-to media: {}", reply_media[0])
+ tag = reply_ctx or (f"[Reply to: {reply_media_parts[0]}]" if reply_media_parts else None)
+ if tag:
+ content_parts.insert(0, tag)
+ content = "\n".join(content_parts) if content_parts else "[empty message]"
+
+ self.logger.debug("message from {}: {}...", sender_id, content[:50])
+
+ str_chat_id = str(chat_id)
+ metadata = self._build_message_metadata(message, user)
+ session_key = self._derive_topic_session_key(message)
+
+ # Telegram media groups: buffer briefly, forward as one aggregated turn.
+ if media_group_id := getattr(message, "media_group_id", None):
+ key = f"{str_chat_id}:{media_group_id}"
+ if key not in self._media_group_buffers:
+ self._media_group_buffers[key] = {
+ "sender_id": sender_id, "chat_id": str_chat_id,
+ "contents": [], "media": [],
+ "metadata": metadata,
+ "session_key": session_key,
+ }
+ self._start_typing(str_chat_id)
+ await self._add_reaction(str_chat_id, message.message_id, self.config.react_emoji)
+ buf = self._media_group_buffers[key]
+ if content and content != "[empty message]":
+ buf["contents"].append(content)
+ buf["media"].extend(media_paths)
+ if key not in self._media_group_tasks:
+ self._media_group_tasks[key] = asyncio.create_task(self._flush_media_group(key))
+ return
+
+ # Start typing indicator before processing
+ self._start_typing(str_chat_id)
+ await self._add_reaction(str_chat_id, message.message_id, self.config.react_emoji)
+
+ # Forward to the message bus
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=str_chat_id,
+ content=content,
+ media=media_paths,
+ metadata=metadata,
+ session_key=session_key,
+ )
+
+ async def _flush_media_group(self, key: str) -> None:
+ """Wait briefly, then forward buffered media-group as one turn."""
+ try:
+ await asyncio.sleep(0.6)
+ if not (buf := self._media_group_buffers.pop(key, None)):
+ return
+ content = "\n".join(buf["contents"]) or "[empty message]"
+ await self._handle_message(
+ sender_id=buf["sender_id"], chat_id=buf["chat_id"],
+ content=content, media=list(dict.fromkeys(buf["media"])),
+ metadata=buf["metadata"],
+ session_key=buf.get("session_key"),
+ )
+ finally:
+ self._media_group_tasks.pop(key, None)
+
+ def _start_typing(self, chat_id: str) -> None:
+ """Start sending 'typing...' indicator for a chat."""
+ # Cancel any existing typing task for this chat
+ self._stop_typing(chat_id)
+ self._typing_tasks[chat_id] = asyncio.create_task(self._typing_loop(chat_id))
+
+ def _stop_typing(self, chat_id: str) -> None:
+ """Stop the typing indicator for a chat."""
+ task = self._typing_tasks.pop(chat_id, None)
+ if task and not task.done():
+ task.cancel()
+
+ async def _add_reaction(self, chat_id: str, message_id: int, emoji: str) -> None:
+ """Add emoji reaction to a message (best-effort, non-blocking)."""
+ if not self._app or not emoji:
+ return
+ try:
+ await self._app.bot.set_message_reaction(
+ chat_id=int(chat_id),
+ message_id=message_id,
+ reaction=[ReactionTypeEmoji(emoji=emoji)],
+ )
+ except Exception as e:
+ self.logger.debug("reaction failed: {}", e)
+
+ async def _remove_reaction(self, chat_id: str, message_id: int) -> None:
+ """Remove emoji reaction from a message (best-effort, non-blocking)."""
+ if not self._app:
+ return
+ try:
+ await self._app.bot.set_message_reaction(
+ chat_id=int(chat_id),
+ message_id=message_id,
+ reaction=[],
+ )
+ except Exception as e:
+ self.logger.debug("reaction removal failed: {}", e)
+
+ async def _typing_loop(self, chat_id: str) -> None:
+ """Repeatedly send 'typing' action until cancelled."""
+ try:
+ with suppress(asyncio.CancelledError):
+ while self._app:
+ await self._app.bot.send_chat_action(chat_id=int(chat_id), action="typing")
+ await asyncio.sleep(4)
+ except Exception as e:
+ self.logger.debug("Typing indicator stopped for {}: {}", chat_id, e)
+
+ @staticmethod
+ def _format_telegram_error(exc: Exception) -> str:
+ """Return a short, readable error summary for logs."""
+ text = str(exc).strip()
+ if text:
+ return text
+ if exc.__cause__ is not None:
+ cause = exc.__cause__
+ cause_text = str(cause).strip()
+ if cause_text:
+ return f"{exc.__class__.__name__} ({cause_text})"
+ return f"{exc.__class__.__name__} ({cause.__class__.__name__})"
+ return exc.__class__.__name__
+
+ def _on_polling_error(self, exc: Exception) -> None:
+ """Keep long-polling network failures to a single readable line."""
+ summary = self._format_telegram_error(exc)
+ if isinstance(exc, (NetworkError, TimedOut)):
+ self.logger.warning("polling network issue: {}", summary)
+ else:
+ self.logger.error("polling error: {}", summary)
+
+ async def _on_error(self, update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Log polling / handler errors instead of silently swallowing them."""
+ summary = self._format_telegram_error(context.error)
+
+ if isinstance(context.error, (NetworkError, TimedOut)):
+ self.logger.warning("network issue: {}", summary)
+ else:
+ self.logger.error("error: {}", summary)
+
+ def _get_extension(
+ self,
+ media_type: str,
+ mime_type: str | None,
+ filename: str | None = None,
+ ) -> str:
+ """Get file extension based on media type or original filename."""
+ if mime_type:
+ ext_map = {
+ "image/jpeg": ".jpg", "image/png": ".png", "image/gif": ".gif",
+ "image/webp": ".webp",
+ "audio/ogg": ".ogg", "audio/mpeg": ".mp3", "audio/mp4": ".m4a",
+ "video/mp4": ".mp4", "video/quicktime": ".mov", "video/webm": ".webm",
+ "video/x-matroska": ".mkv", "video/3gpp": ".3gp",
+ }
+ if mime_type in ext_map:
+ return ext_map[mime_type]
+
+ type_map = {"image": ".jpg", "voice": ".ogg", "audio": ".mp3", "video": ".mp4", "file": ""}
+ if ext := type_map.get(media_type, ""):
+ return ext
+
+ if filename:
+ return "".join(Path(filename).suffixes)
+
+ return ""
+
+ def _build_keyboard(self, buttons: list) -> InlineKeyboardMarkup | None:
+ """Build inline keyboard markup if inline_keyboards is enabled."""
+ if not buttons or not self.config.inline_keyboards:
+ return None
+ keyboard = [
+ [InlineKeyboardButton(label, callback_data=self._safe_callback_data(label)) for label in row]
+ for row in buttons
+ ]
+ return InlineKeyboardMarkup(keyboard)
+
+ @staticmethod
+ def _safe_callback_data(label: str) -> str:
+ # Telegram caps callback_data at 64 bytes UTF-8; truncate at a char boundary so the keyboard still sends.
+ encoded = label.encode("utf-8")
+ if len(encoded) <= 64:
+ return label
+ return encoded[:64].decode("utf-8", errors="ignore")
+
+ @staticmethod
+ def _buttons_as_text(buttons: list[list[str]]) -> str:
+ # Buttons are semantic options; when we can't render a keyboard, the user still needs to see them.
+ return "\n".join(" ".join(f"[{label}]" for label in row) for row in buttons if row)
+
+ async def _on_callback_query(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
+ """Handle inline keyboard button clicks (callback queries)."""
+ if not update.callback_query or not update.effective_user:
+ return
+ query = update.callback_query
+ user = update.effective_user
+ chat_id = query.message.chat_id if query.message else None
+ sender_id = self._sender_id(user)
+ if not chat_id:
+ self.logger.warning("Callback query without chat_id")
+ return
+ if not self.is_allowed(sender_id):
+ return
+ button_label = query.data or ""
+ await query.answer()
+ if query.message:
+ with suppress(Exception):
+ await query.message.edit_reply_markup(reply_markup=None)
+ self.logger.debug("Inline button tap from {}: {}", sender_id, button_label)
+ self._start_typing(str(chat_id))
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=str(chat_id),
+ content=button_label,
+ metadata={
+ "callback_query_id": query.id,
+ "button_label": button_label,
+ "user_id": user.id,
+ "username": user.username,
+ "first_name": user.first_name,
+ "is_callback": True,
+ },
+ )
diff --git a/agent/src/channels/utils.py b/agent/src/channels/utils.py
new file mode 100644
index 000000000..222dac1eb
--- /dev/null
+++ b/agent/src/channels/utils.py
@@ -0,0 +1,156 @@
+"""Utility helpers for channel adapters."""
+
+from __future__ import annotations
+
+import ipaddress
+import re
+import socket
+from pathlib import Path
+from urllib.parse import urlparse
+
+from src.config.paths import get_data_dir
+
+_UNSAFE_CHARS = re.compile(r"[/\\:*?\"<>|]")
+
+
+def get_media_dir(channel_name: str) -> Path:
+ """Return a media directory for *channel_name* under the VT data dir."""
+ p = get_data_dir() / channel_name
+ p.mkdir(parents=True, exist_ok=True)
+ return p
+
+
+def get_runtime_subdir(name: str) -> Path:
+ """Return a runtime subdirectory for *name* under the VT data dir."""
+ p = get_data_dir() / "runtime" / name
+ p.mkdir(parents=True, exist_ok=True)
+ return p
+
+
+def validate_resolved_url(url: str, *, allow_loopback: bool = False) -> tuple[bool, str]:
+ """Validate a URL then resolve and check its IP addresses.
+
+ Thin wrapper around :func:`validate_url_target` for callers that want
+ the same behavior under a legacy name.
+ """
+ return validate_url_target(url, allow_loopback=allow_loopback)
+
+
+def is_path_within(path: str | Path, root: str | Path) -> bool:
+ """Check whether *path* resides within *root* directory."""
+ try:
+ Path(path).resolve().relative_to(Path(root).resolve())
+ return True
+ except ValueError:
+ return False
+
+
+def split_message(content: str, max_len: int = 2000) -> list[str]:
+ """Split content into chunks within max_len, preferring line breaks.
+
+ Args:
+ content: The text content to split.
+ max_len: Maximum length per chunk (default 2000 for Discord compatibility).
+
+ Returns:
+ List of message chunks, each within max_len.
+ """
+ if not content:
+ return []
+ if len(content) <= max_len:
+ return [content]
+ chunks: list[str] = []
+ while content:
+ if len(content) <= max_len:
+ chunks.append(content)
+ break
+ cut = content[:max_len]
+ # Try to break at newline first, then space, then hard break
+ pos = cut.rfind("\n")
+ if pos <= 0:
+ pos = cut.rfind(" ")
+ if pos <= 0:
+ pos = max_len
+ chunks.append(content[:pos])
+ content = content[pos:].lstrip()
+ return chunks
+
+
+def safe_filename(name: str) -> str:
+ """Replace unsafe path characters with underscores."""
+ return _UNSAFE_CHARS.sub("_", name).strip()
+
+
+def validate_url_target(url: str, *, allow_loopback: bool = False) -> tuple[bool, str]:
+ """Validate a URL is safe to fetch: scheme, hostname, and resolved IPs.
+
+ ``allow_loopback`` is intentionally narrow: it only permits literal
+ loopback hosts (localhost, 127.0.0.0/8, ::1) when every resolved address is
+ loopback. It does not allow RFC1918, link-local, metadata, or public DNS
+ names that happen to resolve to loopback.
+
+ Returns (ok, error_message). When ok is True, error_message is empty.
+ """
+ try:
+ p = urlparse(url)
+ except Exception as e:
+ return False, str(e)
+
+ if p.scheme not in ("http", "https"):
+ return False, f"Only http/https allowed, got '{p.scheme or 'none'}'"
+ if not p.netloc:
+ return False, "Missing domain"
+
+ hostname = p.hostname
+ if not hostname:
+ return False, "Missing hostname"
+
+ try:
+ infos = socket.getaddrinfo(hostname, None, socket.AF_UNSPEC, socket.SOCK_STREAM)
+ except socket.gaierror:
+ return False, f"Cannot resolve hostname: {hostname}"
+
+ addrs: list[ipaddress.IPv4Address | ipaddress.IPv6Address] = []
+ for info in infos:
+ try:
+ addr = ipaddress.ip_address(info[4][0])
+ except ValueError:
+ continue
+ addrs.append(addr)
+
+ if allow_loopback and _is_allowed_loopback_target(hostname, addrs):
+ return True, ""
+
+ for addr in addrs:
+ if _is_private(addr):
+ return False, f"Blocked: {hostname} resolves to private/internal address {addr}"
+
+ return True, ""
+
+
+def _is_allowed_loopback_target(
+ hostname: str,
+ addrs: list[ipaddress.IPv4Address | ipaddress.IPv6Address],
+) -> bool:
+ """Check that *hostname* is a literal loopback host and every addr is loopback."""
+ if hostname.lower() not in ("localhost", "localhost."):
+ # Check for literal "127.x.y.z" or "[::1]"
+ is_loopback_literal = False
+ for addr in addrs:
+ if addr.is_loopback:
+ is_loopback_literal = True
+ else:
+ return False
+ return is_loopback_literal
+ return all(addr.is_loopback for addr in addrs) if addrs else False
+
+
+def _is_private(addr: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:
+ """Check whether *addr* is private, link-local, or loopback."""
+ if addr.is_loopback or addr.is_link_local or addr.is_private:
+ return True
+ # Also block well-known metadata endpoints
+ if isinstance(addr, ipaddress.IPv4Address):
+ return False
+ # IPv6: block unspecified, multicast, unique-local
+ return addr.is_multicast or addr.is_unspecified
diff --git a/agent/src/channels/websocket.py b/agent/src/channels/websocket.py
new file mode 100644
index 000000000..920d33ea6
--- /dev/null
+++ b/agent/src/channels/websocket.py
@@ -0,0 +1,1203 @@
+"""WebSocket server channel: vibe-trading acts as a WebSocket server and serves connected clients."""
+
+from __future__ import annotations
+
+import asyncio
+import hmac
+import json
+import re
+import ssl
+import uuid
+from collections.abc import Callable
+from contextlib import suppress
+from pathlib import Path
+from typing import Any, Self
+
+from pydantic import Field, field_validator, model_validator
+from websockets.asyncio.server import ServerConnection, serve, unix_serve
+from websockets.exceptions import ConnectionClosed
+from websockets.http11 import Request as WsRequest
+
+from src.channels.bus.events import OUTBOUND_META_AGENT_UI, OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+from src.security.workspace_access import (
+ WORKSPACE_SCOPE_METADATA_KEY,
+ WorkspaceScopeError,
+)
+from src.session.goal_state import goal_state_ws_blob
+from src.session.webui_turns import websocket_turn_wall_started_at
+from src.utils.media_decode import (
+ FileSizeExceeded,
+ save_base64_data_url,
+)
+from src.channelsui.cli_apps_api import normalize_cli_app_mentions
+from src.channelsui.forking import handle_webui_fork_chat
+from src.channelsui.gateway_services import GatewayServices
+from src.channelsui.http_utils import (
+ normalize_config_path as _normalize_config_path,
+)
+from src.channelsui.http_utils import (
+ parse_request_path as _parse_request_path,
+)
+from src.channelsui.http_utils import (
+ query_first as _query_first,
+)
+from src.channelsui.mcp_presets_api import normalize_mcp_preset_mentions
+from src.channelsui.transcription_ws import webui_transcription_event
+from src.channelsui.websocket_logging import websockets_server_logger
+
+
+class WebSocketConfig(BaseModel):
+ """WebSocket server channel configuration.
+
+ Clients connect with URLs like ``ws://{host}:{port}{path}?client_id=...&token=...``.
+ - ``client_id``: Used for ``allow_from`` authorization; if omitted, a value is generated and logged.
+ - ``token``: If non-empty, the ``token`` query param may match this static secret; short-lived tokens
+ from ``token_issue_path`` are also accepted.
+ - ``token_issue_path``: If non-empty, **GET** (HTTP/1.1) to this path returns JSON
+ ``{"token": "...", "expires_in": <seconds>}``; use ``?token=...`` when opening the WebSocket.
+ Must differ from ``path`` (the WS upgrade path). If the client runs in the **same process** as
+ vibe-trading and shares the asyncio loop, use a thread or async HTTP client for GET—do not call
+ blocking ``urllib`` or synchronous ``httpx`` from inside a coroutine.
+ - ``token_issue_secret``: If non-empty, token requests must send ``Authorization: Bearer <secret>`` or
+ ``X-Vibe-Trading-Auth: <secret>``.
+ - ``websocket_requires_token``: If True, the handshake must include a valid token (static or issued and not expired).
+ - Each connection has its own session: a unique ``chat_id`` maps to the agent session internally.
+ - ``media`` field in outbound messages contains local filesystem paths; remote clients need a
+ shared filesystem or an HTTP file server to access these files.
+ """
+
+ enabled: bool = False
+ host: str = "127.0.0.1"
+ port: int = 8765
+ unix_socket_path: str = ""
+ path: str = "/"
+ token: str = ""
+ token_issue_path: str = ""
+ token_issue_secret: str = ""
+ token_ttl_s: int = Field(default=300, ge=30, le=86_400)
+ websocket_requires_token: bool = True
+ allow_from: list[str] = Field(default_factory=lambda: ["*"])
+ streaming: bool = True
+ # Default 36 MB, upper 40 MB: supports up to 4 images at ~6 MB each after
+ # client-side Worker normalization (see webui Composer). 4 × 6 MB × 1.37
+ # (base64 overhead) + envelope framing stays under 36 MB; the 40 MB ceiling
+ # leaves a small margin for sender slop without opening a DoS avenue.
+ max_message_bytes: int = Field(default=37_748_736, ge=1024, le=41_943_040)
+ ping_interval_s: float = Field(default=20.0, ge=5.0, le=300.0)
+ ping_timeout_s: float = Field(default=20.0, ge=5.0, le=300.0)
+ ssl_certfile: str = ""
+ ssl_keyfile: str = ""
+
+ @field_validator("unix_socket_path")
+ @classmethod
+ def unix_socket_path_format(cls, value: str) -> str:
+ value = value.strip()
+ if not value:
+ return ""
+ if "\x00" in value:
+ raise ValueError("unix_socket_path must not contain NUL bytes")
+ path = Path(value).expanduser()
+ if not path.is_absolute():
+ raise ValueError("unix_socket_path must be an absolute path")
+ return str(path)
+
+ @field_validator("path")
+ @classmethod
+ def path_must_start_with_slash(cls, value: str) -> str:
+ if not value.startswith("/"):
+ raise ValueError('path must start with "/"')
+ return _normalize_config_path(value)
+
+ @field_validator("token_issue_path")
+ @classmethod
+ def token_issue_path_format(cls, value: str) -> str:
+ value = value.strip()
+ if not value:
+ return ""
+ if not value.startswith("/"):
+ raise ValueError('token_issue_path must start with "/"')
+ return _normalize_config_path(value)
+
+ @model_validator(mode="after")
+ def token_issue_path_differs_from_ws_path(self) -> Self:
+ if not self.token_issue_path:
+ return self
+ if _normalize_config_path(self.token_issue_path) == _normalize_config_path(self.path):
+ raise ValueError("token_issue_path must differ from path (the WebSocket upgrade path)")
+ return self
+
+ @model_validator(mode="after")
+ def wildcard_host_requires_auth(self) -> Self:
+ if self.host not in ("0.0.0.0", "::"):
+ return self
+ if self.token.strip() or self.token_issue_secret.strip():
+ return self
+ raise ValueError(
+ "host is 0.0.0.0 (all interfaces) but neither token nor "
+ "token_issue_secret is set — set one to prevent unauthenticated access"
+ )
+
+
+def publish_runtime_model_update(
+ bus: MessageBus,
+ model: str,
+ model_preset: str | None,
+) -> None:
+ """Enqueue a runtime model snapshot for websocket subscribers (fan-out in-channel)."""
+ bus.outbound.put_nowait(OutboundMessage(
+ channel="websocket",
+ chat_id="*",
+ content="",
+ metadata={
+ "_runtime_model_updated": True,
+ "model": model,
+ "model_preset": model_preset,
+ },
+ ))
+
+
+def _parse_inbound_payload(raw: str) -> str | None:
+ """Parse a client frame into text; return None for empty or unrecognized content."""
+ text = raw.strip()
+ if not text:
+ return None
+ if text.startswith("{"):
+ try:
+ data = json.loads(text)
+ except json.JSONDecodeError:
+ return text
+ if isinstance(data, dict):
+ for key in ("content", "text", "message"):
+ value = data.get(key)
+ if isinstance(value, str) and value.strip():
+ return value
+ return None
+ return None
+ return text
+
+
+# Accept UUIDs and short scoped keys like "unified:default". Keeps the capability
+# namespace small enough to rule out path traversal / quote injection tricks.
+_CHAT_ID_RE = re.compile(r"^[A-Za-z0-9_:-]{1,64}$")
+
+
+def _is_valid_chat_id(value: Any) -> bool:
+ return isinstance(value, str) and _CHAT_ID_RE.match(value) is not None
+
+
+def _parse_envelope(raw: str) -> dict[str, Any] | None:
+ """Return a typed envelope dict if the frame is a new-style JSON envelope, else None.
+
+ A frame qualifies when it parses as a JSON object with a string ``type`` field.
+ Legacy frames (plain text, or ``{"content": ...}`` without ``type``) return None;
+ callers should fall back to :func:`_parse_inbound_payload` for those.
+ """
+ text = raw.strip()
+ if not text.startswith("{"):
+ return None
+ try:
+ data = json.loads(text)
+ except json.JSONDecodeError:
+ return None
+ if not isinstance(data, dict):
+ return None
+ t = data.get("type")
+ if not isinstance(t, str):
+ return None
+ return data
+
+
+# Per-message media limits. The server-side guard is a touch looser than the
+# client's ``Worker`` normalization target (6 MB) — tolerate client slop, but
+# still cap total ingress at ``_MAX_IMAGES_PER_MESSAGE * _MAX_IMAGE_BYTES``
+# which fits comfortably inside ``max_message_bytes``.
+_MAX_IMAGES_PER_MESSAGE = 4
+_MAX_IMAGE_BYTES = 8 * 1024 * 1024
+_MAX_VIDEOS_PER_MESSAGE = 1
+_MAX_VIDEO_BYTES = 20 * 1024 * 1024
+
+# Image MIME whitelist — matches the Composer's ``accept`` list. SVG is
+# explicitly excluded to avoid the XSS surface inside embedded scripts.
+_IMAGE_MIME_ALLOWED: frozenset[str] = frozenset({
+ "image/png",
+ "image/jpeg",
+ "image/webp",
+ "image/gif",
+})
+
+_VIDEO_MIME_ALLOWED: frozenset[str] = frozenset({
+ "video/mp4",
+ "video/webm",
+ "video/quicktime",
+})
+
+_UPLOAD_MIME_ALLOWED: frozenset[str] = _IMAGE_MIME_ALLOWED | _VIDEO_MIME_ALLOWED
+
+_DATA_URL_MIME_RE = re.compile(r"^data:([^;,]+)(?:;[^,]*)*;base64,", re.DOTALL)
+
+
+def _extract_data_url_mime(url: str) -> str | None:
+ """Return the MIME type of a ``data:<mime>;base64,...`` URL, else ``None``."""
+ if not isinstance(url, str):
+ return None
+ m = _DATA_URL_MIME_RE.match(url)
+ if not m:
+ return None
+ return m.group(1).strip().lower() or None
+
+
+def _is_websocket_upgrade(request: WsRequest) -> bool:
+ """Detect an actual WS upgrade; plain HTTP GETs to the same path should fall through."""
+ upgrade = request.headers.get("Upgrade") or request.headers.get("upgrade")
+ connection = request.headers.get("Connection") or request.headers.get("connection")
+ if not upgrade or "websocket" not in upgrade.lower():
+ return False
+ if not connection or "upgrade" not in connection.lower():
+ return False
+ return True
+
+
+class WebSocketChannel(BaseChannel):
+ """Run a local WebSocket server; forward text/JSON messages to the message bus."""
+
+ name = "websocket"
+ display_name = "WebSocket"
+
+ def __init__(
+ self,
+ config: Any,
+ bus: MessageBus,
+ *,
+ gateway: GatewayServices,
+ ):
+ if isinstance(config, dict):
+ config = WebSocketConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: WebSocketConfig = config
+ # chat_id -> connections subscribed to it (fan-out target).
+ self._subs: dict[str, set[Any]] = {}
+ # connection -> chat_ids it is subscribed to (O(1) cleanup on disconnect).
+ self._conn_chats: dict[Any, set[str]] = {}
+ # connection -> default chat_id for legacy frames that omit routing.
+ self._conn_default: dict[Any, str] = {}
+ self._stop_event: asyncio.Event | None = None
+ self._server_task: asyncio.Task[None] | None = None
+
+ self.gateway = gateway
+ self._http_router = gateway.http
+ self._tokens = gateway.tokens
+ self._media = gateway.media
+ self._transcripts = gateway.transcripts
+ self._workspaces = gateway.workspaces
+
+ self._stream_text_buffers: dict[tuple[str, str], list[str]] = {}
+
+ # -- Subscription bookkeeping -------------------------------------------
+
+ def _workspace_controls_available(self, connection: Any) -> bool:
+ return self._http_router.workspace_controls_available(connection)
+
+ def _attach(self, connection: Any, chat_id: str) -> None:
+ """Idempotently subscribe *connection* to *chat_id*."""
+ self._subs.setdefault(chat_id, set()).add(connection)
+ self._conn_chats.setdefault(connection, set()).add(chat_id)
+
+ def _cleanup_connection(self, connection: Any) -> None:
+ """Remove *connection* from every subscription set; safe to call multiple times."""
+ chat_ids = self._conn_chats.pop(connection, set())
+ for cid in chat_ids:
+ subs = self._subs.get(cid)
+ if subs is None:
+ continue
+ subs.discard(connection)
+ if not subs:
+ self._subs.pop(cid, None)
+ self._conn_default.pop(connection, None)
+
+ async def _maybe_push_active_goal_state(self, chat_id: str) -> None:
+ """Replay an active sustained goal from session metadata after *chat_id* is subscribed.
+
+ Goal metadata lives on the session JSONL and survives gateway restarts, but
+ connected clients normally see it via ``goal_state`` / ``turn_end`` frames.
+ Pushing here makes refresh + reconnect restore the strip without a new model turn.
+ """
+ if self.gateway.session_manager is None:
+ return
+ row = self.gateway.session_manager.read_session_file(f"websocket:{chat_id}")
+ meta = row.get("metadata", {}) if isinstance(row, dict) else {}
+ if not isinstance(meta, dict):
+ meta = {}
+ blob = goal_state_ws_blob(meta)
+ if not blob.get("active"):
+ return
+ await self.send_goal_state(chat_id, blob)
+
+ async def _maybe_push_turn_run_wall_clock(self, chat_id: str) -> None:
+ """Replay ``goal_status: running`` when a turn is still active (same-process refresh)."""
+ t0 = websocket_turn_wall_started_at(chat_id)
+ if t0 is None:
+ return
+ await self.send_goal_status(chat_id, "running", started_at=t0)
+
+ async def _hydrate_after_subscribe(self, chat_id: str) -> None:
+ """Replay goal/run strip state after subscribe (same-process refresh)."""
+ await self._maybe_push_active_goal_state(chat_id)
+ await self._maybe_push_turn_run_wall_clock(chat_id)
+
+ async def _send_event(self, connection: Any, event: str, **fields: Any) -> None:
+ """Send a control event (attached, error, ...) to a single connection."""
+ payload: dict[str, Any] = {"event": event}
+ payload.update(fields)
+ raw = json.dumps(payload, ensure_ascii=False)
+ try:
+ await connection.send(raw)
+ except ConnectionClosed:
+ self._cleanup_connection(connection)
+ except Exception as e:
+ self.logger.warning("failed to send {} event: {}", event, e)
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return WebSocketConfig().model_dump(by_alias=True)
+
+ def _expected_path(self) -> str:
+ return _normalize_config_path(self.config.path)
+
+ def _build_ssl_context(self) -> ssl.SSLContext | None:
+ cert = self.config.ssl_certfile.strip()
+ key = self.config.ssl_keyfile.strip()
+ if not cert and not key:
+ return None
+ if not cert or not key:
+ raise ValueError(
+ "ssl_certfile and ssl_keyfile must both be set for WSS, or both left empty"
+ )
+ ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
+ ctx.minimum_version = ssl.TLSVersion.TLSv1_2
+ ctx.load_cert_chain(certfile=cert, keyfile=key)
+ return ctx
+
+ # -- HTTP dispatch ------------------------------------------------------
+
+ async def _dispatch_http(self, connection: Any, request: WsRequest) -> Any:
+ """Route an inbound HTTP request to the HTTP handler or WS upgrade."""
+ got, query = _parse_request_path(request.path)
+
+ # WebSocket upgrade — channel handles this itself
+ expected_ws = self._expected_path()
+ if got == expected_ws and _is_websocket_upgrade(request):
+ client_id = _query_first(query, "client_id") or ""
+ if len(client_id) > 128:
+ client_id = client_id[:128]
+ if not self.is_allowed(client_id):
+ return connection.respond(403, "Forbidden")
+ return self._authorize_websocket_handshake(connection, query)
+
+ token_path = _normalize_config_path(self.config.token_issue_path) if self.config.token_issue_path else ""
+ if token_path and got == token_path:
+ return self._issue_token_response(connection, request)
+
+ # Everything else goes to the HTTP handler
+ return await self._http_router.dispatch(connection, request)
+
+ def _issue_token_response(self, connection: Any, request: WsRequest) -> Any:
+ """Issue a short-lived WebSocket token over the configured HTTP path."""
+ secret = self.config.token_issue_secret.strip()
+ if secret:
+ auth = request.headers.get("Authorization") or request.headers.get("authorization") or ""
+ bearer = "Bearer "
+ supplied = ""
+ if auth.startswith(bearer):
+ supplied = auth[len(bearer):].strip()
+ supplied = supplied or request.headers.get("X-Vibe-Trading-Auth", "").strip()
+ if not supplied or not hmac.compare_digest(supplied, secret):
+ return connection.respond(401, "Unauthorized")
+ token, expires_in = self._tokens.issue_token(ttl_s=self.config.token_ttl_s)
+ return connection.respond(
+ 200,
+ json.dumps({"token": token, "expires_in": expires_in}, ensure_ascii=False),
+ )
+
+ def _authorize_websocket_handshake(self, connection: Any, query: dict[str, list[str]]) -> Any:
+ supplied = _query_first(query, "token")
+ static_token = self.config.token.strip()
+
+ if static_token:
+ if supplied and hmac.compare_digest(supplied, static_token):
+ return None
+ if supplied and self._tokens.take_issued_token_if_valid(supplied):
+ return None
+ return connection.respond(401, "Unauthorized")
+
+ if self.config.websocket_requires_token:
+ if supplied and self._tokens.take_issued_token_if_valid(supplied):
+ return None
+ return connection.respond(401, "Unauthorized")
+
+ if supplied:
+ self._tokens.take_issued_token_if_valid(supplied)
+ return None
+
+ # -- Server lifecycle and connection ingress ---------------------------
+
+ async def start(self) -> None:
+ # (stdlib logging handles websockets SDK output via propagation)
+ ws_logger = websockets_server_logger()
+
+ self._running = True
+ self._stop_event = asyncio.Event()
+
+ ssl_context = self._build_ssl_context()
+ scheme = "wss" if ssl_context else "ws"
+
+ async def process_request(
+ connection: ServerConnection,
+ request: WsRequest,
+ ) -> Any:
+ return await self._dispatch_http(connection, request)
+
+ async def handler(connection: ServerConnection) -> None:
+ await self._connection_loop(connection)
+
+ self.logger.info(
+ "WebSocket server listening on {}",
+ (
+ f"unix:{self.config.unix_socket_path}{self.config.path}"
+ if self.config.unix_socket_path
+ else f"{scheme}://{self.config.host}:{self.config.port}{self.config.path}"
+ ),
+ )
+ if self.config.token_issue_path:
+ self.logger.info(
+ "WebSocket token issue route: {}",
+ (
+ f"unix:{self.config.unix_socket_path}{_normalize_config_path(self.config.token_issue_path)}"
+ if self.config.unix_socket_path
+ else (
+ f"{scheme}://{self.config.host}:{self.config.port}"
+ f"{_normalize_config_path(self.config.token_issue_path)}"
+ )
+ ),
+ )
+
+ async def runner() -> None:
+ socket_path = self.config.unix_socket_path
+ if socket_path:
+ path_obj = Path(socket_path)
+ path_obj.parent.mkdir(parents=True, exist_ok=True)
+ with suppress(FileNotFoundError):
+ path_obj.unlink()
+ server = await unix_serve(
+ handler,
+ socket_path,
+ process_request=process_request,
+ max_size=self.config.max_message_bytes,
+ ping_interval=self.config.ping_interval_s,
+ ping_timeout=self.config.ping_timeout_s,
+ logger=ws_logger,
+ )
+ with suppress(OSError):
+ path_obj.chmod(0o600)
+ else:
+ server = await serve(
+ handler,
+ self.config.host,
+ self.config.port,
+ process_request=process_request,
+ max_size=self.config.max_message_bytes,
+ ping_interval=self.config.ping_interval_s,
+ ping_timeout=self.config.ping_timeout_s,
+ ssl=ssl_context,
+ logger=ws_logger,
+ )
+ try:
+ assert self._stop_event is not None
+ await self._stop_event.wait()
+ finally:
+ server.close()
+ await server.wait_closed()
+ if socket_path:
+ with suppress(FileNotFoundError):
+ Path(socket_path).unlink()
+
+ self._server_task = asyncio.create_task(runner())
+ await self._server_task
+
+ async def _connection_loop(self, connection: Any) -> None:
+ request = connection.request
+ path_part = request.path if request else "/"
+ _, query = _parse_request_path(path_part)
+ client_id_raw = _query_first(query, "client_id")
+ client_id = client_id_raw.strip() if client_id_raw else ""
+ if not client_id:
+ client_id = f"anon-{uuid.uuid4().hex[:12]}"
+ elif len(client_id) > 128:
+ self.logger.warning("client_id too long ({} chars), truncating", len(client_id))
+ client_id = client_id[:128]
+
+ default_chat_id = str(uuid.uuid4())
+
+ try:
+ await connection.send(
+ json.dumps(
+ {
+ "event": "ready",
+ "chat_id": default_chat_id,
+ "client_id": client_id,
+ },
+ ensure_ascii=False,
+ )
+ )
+ # Register only after ready is successfully sent to avoid out-of-order sends
+ self._conn_default[connection] = default_chat_id
+ self._attach(connection, default_chat_id)
+ await self._hydrate_after_subscribe(default_chat_id)
+
+ async for raw in connection:
+ if isinstance(raw, bytes):
+ try:
+ raw = raw.decode("utf-8")
+ except UnicodeDecodeError:
+ self.logger.warning("ignoring non-utf8 binary frame")
+ continue
+
+ envelope = _parse_envelope(raw)
+ if envelope is not None:
+ await self._dispatch_envelope(connection, client_id, envelope)
+ continue
+
+ content = _parse_inbound_payload(raw)
+ if content is None:
+ continue
+ # WebSocket already authenticates at handshake time (token),
+ # so pairing is not applicable. Treat as non-DM to avoid
+ # sending pairing codes to an already-authenticated client.
+ await self._handle_message(
+ sender_id=client_id,
+ chat_id=default_chat_id,
+ content=content,
+ metadata={"remote": getattr(connection, "remote_address", None)},
+ is_dm=False,
+ )
+ except Exception as e:
+ self.logger.debug("connection ended: {}", e)
+ finally:
+ self._cleanup_connection(connection)
+
+ # -- Inbound WebSocket envelopes ---------------------------------------
+
+ def _save_envelope_media(
+ self,
+ media: list[Any],
+ ) -> tuple[list[str], str | None]:
+ """Decode and persist ``media`` items from a ``message`` envelope.
+
+ Returns ``(paths, None)`` on success or ``([], reason)`` on the first
+ failure — the caller is expected to surface ``reason`` to the client
+ and skip publishing so no half-formed message ever reaches the agent.
+ On failure, any files already written to disk earlier in the same
+ call are unlinked so partial ingress doesn't leak orphan files.
+ ``reason`` is a short, stable token suitable for UI localization.
+
+ Shape: ``list[{"data_url": str, "name"?: str | None}]``.
+ """
+ image_count = 0
+ video_count = 0
+ for item in media:
+ mime = _extract_data_url_mime(item.get("data_url", "")) if isinstance(item, dict) else None
+ if mime in _VIDEO_MIME_ALLOWED:
+ video_count += 1
+ elif mime in _IMAGE_MIME_ALLOWED:
+ image_count += 1
+ if image_count > _MAX_IMAGES_PER_MESSAGE:
+ return [], "too_many_images"
+ if video_count > _MAX_VIDEOS_PER_MESSAGE:
+ return [], "too_many_videos"
+
+ media_dir = get_media_dir("websocket")
+ paths: list[str] = []
+
+ def _abort(reason: str) -> tuple[list[str], str]:
+ for p in paths:
+ try:
+ Path(p).unlink(missing_ok=True)
+ except OSError as exc:
+ self.logger.warning(
+ "failed to unlink partial media {}: {}", p, exc
+ )
+ return [], reason
+
+ for item in media:
+ if not isinstance(item, dict):
+ return _abort("malformed")
+ data_url = item.get("data_url")
+ if not isinstance(data_url, str) or not data_url:
+ return _abort("malformed")
+ mime = _extract_data_url_mime(data_url)
+ if mime is None:
+ return _abort("decode")
+ if mime not in _UPLOAD_MIME_ALLOWED:
+ return _abort("mime")
+ is_video = mime in _VIDEO_MIME_ALLOWED
+ max_bytes = _MAX_VIDEO_BYTES if is_video else _MAX_IMAGE_BYTES
+ try:
+ saved = save_base64_data_url(
+ data_url, media_dir, max_bytes=max_bytes,
+ )
+ except FileSizeExceeded:
+ return _abort("size")
+ except Exception as exc:
+ self.logger.warning("media decode failed: {}", exc)
+ return _abort("decode")
+ if saved is None:
+ return _abort("decode")
+ paths.append(saved)
+ return paths, None
+
+ async def _dispatch_envelope(
+ self,
+ connection: Any,
+ client_id: str,
+ envelope: dict[str, Any],
+ ) -> None:
+ """Route one typed inbound envelope (``new_chat`` / ``attach`` / ``message``)."""
+ t = envelope.get("type")
+ if t == "new_chat":
+ new_id = str(uuid.uuid4())
+ scope = await self._workspace_scope_or_error(
+ connection,
+ lambda: self._workspaces.scope_for_new_chat(
+ envelope,
+ controls_available=self._workspace_controls_available(connection),
+ ),
+ )
+ if scope is None:
+ return
+ self._workspaces.persist_scope(new_id, scope)
+ self._attach(connection, new_id)
+ await self._send_event(connection, "attached", chat_id=new_id)
+ await self._send_event(
+ connection,
+ "session_updated",
+ chat_id=new_id,
+ scope="metadata",
+ workspace_scope=scope.payload(),
+ )
+ await self._hydrate_after_subscribe(new_id)
+ return
+ if t == "fork_chat":
+ await handle_webui_fork_chat(self, connection, envelope)
+ return
+ if t == "attach":
+ cid = envelope.get("chat_id")
+ if not _is_valid_chat_id(cid):
+ await self._send_event(connection, "error", detail="invalid chat_id")
+ return
+ self._attach(connection, cid)
+ await self._send_event(connection, "attached", chat_id=cid)
+ await self._hydrate_after_subscribe(cid)
+ return
+ if t == "set_workspace_scope":
+ cid = envelope.get("chat_id")
+ if not _is_valid_chat_id(cid):
+ await self._send_event(connection, "error", detail="invalid chat_id")
+ return
+ scope = await self._workspace_scope_or_error(
+ connection,
+ lambda: self._workspaces.scope_for_set_request(
+ envelope,
+ chat_id=cid,
+ chat_running=websocket_turn_wall_started_at(cid) is not None,
+ controls_available=self._workspace_controls_available(connection),
+ ),
+ chat_id=cid,
+ )
+ if scope is None:
+ return
+ self._workspaces.persist_scope(cid, scope)
+ await self._send_event(
+ connection,
+ "session_updated",
+ chat_id=cid,
+ scope="metadata",
+ workspace_scope=scope.payload(),
+ )
+ return
+ if t == "transcribe_audio":
+ event, payload = await webui_transcription_event(envelope)
+ await self._send_event(connection, event, **payload)
+ return
+ if t == "message":
+ cid = envelope.get("chat_id")
+ content = envelope.get("content")
+ if not _is_valid_chat_id(cid):
+ await self._send_event(connection, "error", detail="invalid chat_id")
+ return
+ if not isinstance(content, str):
+ await self._send_event(connection, "error", detail="missing content")
+ return
+
+ raw_media = envelope.get("media")
+ media_paths: list[str] = []
+ if raw_media is not None:
+ if not isinstance(raw_media, list):
+ await self._send_event(
+ connection, "error",
+ detail="image_rejected", reason="malformed",
+ )
+ return
+ media_paths, reason = self._save_envelope_media(raw_media)
+ if reason is not None:
+ await self._send_event(
+ connection, "error",
+ detail="image_rejected", reason=reason,
+ )
+ return
+
+ # Allow image-only turns (content may be empty when media is attached).
+ if not content.strip() and not media_paths:
+ await self._send_event(connection, "error", detail="missing content")
+ return
+ scope = await self._workspace_scope_or_error(
+ connection,
+ lambda: self._workspaces.scope_for_message(
+ envelope,
+ chat_id=cid,
+ chat_running=websocket_turn_wall_started_at(cid) is not None,
+ controls_available=self._workspace_controls_available(connection),
+ ),
+ chat_id=cid,
+ )
+ if scope is None:
+ return
+
+ # Auto-attach on first use so clients can one-shot without a separate attach.
+ self._attach(connection, cid)
+ await self._hydrate_after_subscribe(cid)
+ metadata: dict[str, Any] = {"remote": getattr(connection, "remote_address", None)}
+ if envelope.get("webui") is True:
+ metadata["webui"] = True
+ metadata.update(self._transcripts.client_turn_metadata(envelope.get("turn_id")))
+ cli_apps = normalize_cli_app_mentions(envelope.get("cli_apps"))
+ if cli_apps:
+ metadata["cli_apps"] = cli_apps
+ mcp_presets = normalize_mcp_preset_mentions(envelope.get("mcp_presets"))
+ if mcp_presets:
+ metadata["mcp_presets"] = mcp_presets
+ metadata[WORKSPACE_SCOPE_METADATA_KEY] = scope.metadata()
+ self._workspaces.persist_scope(cid, scope)
+ image_generation = envelope.get("image_generation")
+ if isinstance(image_generation, dict) and image_generation.get("enabled") is True:
+ aspect_ratio = image_generation.get("aspect_ratio")
+ metadata["image_generation"] = {
+ "enabled": True,
+ "aspect_ratio": aspect_ratio if isinstance(aspect_ratio, str) else None,
+ }
+ if metadata.get("webui") is True and self.is_allowed(client_id):
+ self._transcripts.append_user_message(
+ cid,
+ content,
+ metadata=metadata,
+ media_paths=media_paths or None,
+ cli_apps=cli_apps or None,
+ mcp_presets=mcp_presets or None,
+ )
+ await self._handle_message(
+ sender_id=client_id,
+ chat_id=cid,
+ content=content,
+ media=media_paths or None,
+ metadata=metadata,
+ is_dm=False,
+ )
+ return
+ await self._send_event(connection, "error", detail=f"unknown type: {t!r}")
+
+ async def _workspace_scope_or_error(
+ self,
+ connection: Any,
+ resolver: Callable[[], Any],
+ *,
+ chat_id: str | None = None,
+ ) -> Any | None:
+ try:
+ return resolver()
+ except WorkspaceScopeError as exc:
+ await self._send_event(
+ connection,
+ "error",
+ detail="workspace_scope_rejected",
+ reason=exc.message,
+ **({"chat_id": chat_id} if chat_id else {}),
+ )
+ return None
+
+ # -- Outbound WebSocket events -----------------------------------------
+
+ async def stop(self) -> None:
+ if not self._running:
+ return
+ self._running = False
+ if self._stop_event:
+ self._stop_event.set()
+ if self._server_task:
+ try:
+ await self._server_task
+ except asyncio.CancelledError:
+ if asyncio.current_task() and asyncio.current_task().cancelling():
+ raise
+ self.logger.debug("server task was already cancelled during shutdown")
+ except Exception as e:
+ self.logger.warning("server task error during shutdown: {}", e)
+ self._server_task = None
+ self._subs.clear()
+ self._conn_chats.clear()
+ self._conn_default.clear()
+ self._tokens.clear()
+
+ async def _safe_send_to(self, connection: Any, raw: str, *, label: str = "") -> None:
+ """Send a raw frame to one connection, cleaning up on ConnectionClosed."""
+ try:
+ await connection.send(raw)
+ except ConnectionClosed:
+ self._cleanup_connection(connection)
+ self.logger.warning("connection gone{}", label)
+ except Exception:
+ self.logger.exception("send failed{}", label)
+ raise
+
+ async def send(self, msg: OutboundMessage) -> None:
+ if msg.metadata.get("_runtime_model_updated"):
+ await self.send_runtime_model_updated(
+ model_name=msg.metadata.get("model"),
+ model_preset=msg.metadata.get("model_preset"),
+ )
+ return
+
+ # Snapshot the subscriber set so ConnectionClosed cleanups mid-iteration are safe.
+ conns = list(self._subs.get(msg.chat_id, ()))
+ if not conns:
+ if (
+ msg.metadata.get("_progress")
+ or msg.metadata.get("_file_edit_events")
+ or msg.metadata.get("_turn_end")
+ or msg.metadata.get("_session_updated")
+ or msg.metadata.get("_goal_status")
+ or msg.metadata.get("_goal_state_sync")
+ ):
+ self.logger.debug("no active subscribers for chat_id={}", msg.chat_id)
+ else:
+ self.logger.warning("no active subscribers for chat_id={}", msg.chat_id)
+ if msg.metadata.get("_goal_state_sync"):
+ if conns:
+ blob = msg.metadata.get("goal_state")
+ await self.send_goal_state(msg.chat_id, blob if isinstance(blob, dict) else {"active": False})
+ return
+ if msg.metadata.get("_goal_status"):
+ if conns:
+ status = msg.metadata.get("goal_status")
+ if status in ("running", "idle"):
+ started_raw = msg.metadata.get("started_at", msg.metadata.get("goal_started_at"))
+ await self.send_goal_status(
+ msg.chat_id,
+ status,
+ started_at=float(started_raw) if isinstance(started_raw, int | float) else None,
+ )
+ return
+ # Signal that the agent has fully finished processing the current turn.
+ if msg.metadata.get("_turn_end"):
+ lat = msg.metadata.get("latency_ms")
+ lat_i = int(lat) if isinstance(lat, (int, float)) else None
+ gs = msg.metadata.get("goal_state")
+ gs_blob = gs if isinstance(gs, dict) else None
+ await self.send_turn_end(
+ msg.chat_id,
+ latency_ms=lat_i,
+ goal_state=gs_blob,
+ metadata=msg.metadata,
+ )
+ await self.send_session_updated(msg.chat_id, scope="thread")
+ return
+ if msg.metadata.get("_session_updated"):
+ if conns:
+ scope = msg.metadata.get("_session_update_scope")
+ await self.send_session_updated(
+ msg.chat_id,
+ scope=scope if isinstance(scope, str) else None,
+ )
+ return
+ if msg.metadata.get("_file_edit_events"):
+ edits = msg.metadata.get("_file_edit_events")
+ await self.send_file_edit_events(
+ msg.chat_id,
+ edits if isinstance(edits, list) else [],
+ msg.metadata,
+ )
+ return
+ text = msg.content
+ wire_text = self._media.rewrite_local_markdown_images(text)
+ payload: dict[str, Any] = {
+ "event": "message",
+ "chat_id": msg.chat_id,
+ "text": wire_text,
+ }
+ if msg.media:
+ payload["media"] = msg.media
+ urls: list[dict[str, str]] = []
+ for entry in msg.media:
+ signed = self._media.sign_or_stage_media_path(Path(entry))
+ if signed is not None:
+ urls.append(signed)
+ if urls:
+ payload["media_urls"] = urls
+ if msg.reply_to:
+ payload["reply_to"] = msg.reply_to
+ lat = msg.metadata.get("latency_ms")
+ if isinstance(lat, (int, float)):
+ payload["latency_ms"] = int(lat)
+ if msg.metadata.get("_tool_events"):
+ payload["tool_events"] = msg.metadata["_tool_events"]
+ agent_ui = msg.metadata.get(OUTBOUND_META_AGENT_UI)
+ if agent_ui is not None:
+ payload["agent_ui"] = agent_ui
+ # Mark intermediate agent breadcrumbs (tool-call hints, generic
+ # progress strings) so WS clients can render them as subordinate
+ # trace rows rather than conversational replies.
+ if msg.metadata.get("_tool_hint"):
+ payload["kind"] = "tool_hint"
+ elif msg.metadata.get("_progress"):
+ payload["kind"] = "progress"
+ phase = "activity" if payload.get("kind") in ("tool_hint", "progress") else "answer"
+ self._transcripts.prepare_and_append(
+ msg.chat_id,
+ payload,
+ metadata=msg.metadata,
+ phase=phase,
+ include_source=True,
+ transcript_overrides={"text": text},
+ )
+ raw = json.dumps(payload, ensure_ascii=False)
+ if not conns:
+ return
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" ")
+
+ async def send_reasoning_delta(
+ self,
+ chat_id: str,
+ delta: str,
+ metadata: dict[str, Any] | None = None,
+ ) -> None:
+ """Push one chunk of model reasoning. Mirrors ``send_delta`` shape so
+ clients receive a stream that opens, updates in place, and closes —
+ rendered above the active assistant bubble with a shimmer header
+ until the matching ``reasoning_end`` arrives.
+ """
+ conns = list(self._subs.get(chat_id, ()))
+ if not delta:
+ return
+ meta = metadata or {}
+ body: dict[str, Any] = {
+ "event": "reasoning_delta",
+ "chat_id": chat_id,
+ "text": delta,
+ }
+ stream_id = meta.get("_stream_id")
+ if stream_id is not None:
+ body["stream_id"] = stream_id
+ self._transcripts.prepare_and_append(
+ chat_id,
+ body,
+ metadata=meta,
+ phase="reasoning",
+ )
+ raw = json.dumps(body, ensure_ascii=False)
+ if not conns:
+ return
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" reasoning ")
+
+ async def send_reasoning_end(
+ self,
+ chat_id: str,
+ metadata: dict[str, Any] | None = None,
+ ) -> None:
+ """Close the current reasoning stream segment for in-place renderers."""
+ conns = list(self._subs.get(chat_id, ()))
+ meta = metadata or {}
+ body: dict[str, Any] = {
+ "event": "reasoning_end",
+ "chat_id": chat_id,
+ }
+ stream_id = meta.get("_stream_id")
+ if stream_id is not None:
+ body["stream_id"] = stream_id
+ self._transcripts.prepare_and_append(
+ chat_id,
+ body,
+ metadata=meta,
+ phase="reasoning",
+ )
+ raw = json.dumps(body, ensure_ascii=False)
+ if not conns:
+ return
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" reasoning_end ")
+
+ async def send_file_edit_events(
+ self,
+ chat_id: str,
+ edits: list[dict[str, Any]],
+ metadata: dict[str, Any] | None = None,
+ ) -> None:
+ conns = list(self._subs.get(chat_id, ()))
+ payload: dict[str, Any] = {
+ "event": "file_edit",
+ "chat_id": chat_id,
+ "edits": edits,
+ }
+ self._transcripts.prepare_and_append(
+ chat_id,
+ payload,
+ metadata=metadata,
+ phase="activity",
+ )
+ raw = json.dumps(payload, ensure_ascii=False)
+ if not conns:
+ return
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" file_edit ")
+
+ async def send_delta(
+ self,
+ chat_id: str,
+ delta: str,
+ metadata: dict[str, Any] | None = None,
+ ) -> None:
+ conns = list(self._subs.get(chat_id, ()))
+ meta = metadata or {}
+ stream_key = (chat_id, str(meta.get("_stream_id") or ""))
+ if meta.get("_stream_end"):
+ body: dict[str, Any] = {"event": "stream_end", "chat_id": chat_id}
+ buffered = self._stream_text_buffers.pop(stream_key, [])
+ if delta:
+ buffered.append(delta)
+ full_text = "".join(buffered)
+ rewritten = self._media.rewrite_local_markdown_images(full_text)
+ if delta or rewritten != full_text:
+ body["text"] = rewritten
+ else:
+ body = {
+ "event": "delta",
+ "chat_id": chat_id,
+ "text": delta,
+ }
+ self._stream_text_buffers.setdefault(stream_key, []).append(delta)
+ if meta.get("_stream_id") is not None:
+ body["stream_id"] = meta["_stream_id"]
+ self._transcripts.prepare_and_append(
+ chat_id,
+ body,
+ metadata=meta,
+ phase="answer",
+ )
+ raw = json.dumps(body, ensure_ascii=False)
+ if not conns:
+ return
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" stream ")
+
+ async def send_turn_end(
+ self,
+ chat_id: str,
+ latency_ms: int | None = None,
+ *,
+ goal_state: dict[str, Any] | None = None,
+ metadata: dict[str, Any] | None = None,
+ ) -> None:
+ """Signal that the agent has fully finished processing the current turn."""
+ conns = list(self._subs.get(chat_id, ()))
+ body: dict[str, Any] = {"event": "turn_end", "chat_id": chat_id}
+ if latency_ms is not None:
+ body["latency_ms"] = int(latency_ms)
+ if goal_state is not None:
+ body["goal_state"] = goal_state
+ self._transcripts.prepare_and_append(
+ chat_id,
+ body,
+ metadata=metadata,
+ phase="complete",
+ )
+ raw = json.dumps(body, ensure_ascii=False)
+ if not conns:
+ return
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" turn_end ")
+
+ async def send_goal_state(self, chat_id: str, blob: dict[str, Any]) -> None:
+ """Push persisted goal-state snapshot for *chat_id* (multi-chat isolation)."""
+ conns = list(self._subs.get(chat_id, ()))
+ if not conns:
+ return
+ body = {"event": "goal_state", "chat_id": chat_id, "goal_state": blob}
+ raw = json.dumps(body, ensure_ascii=False)
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" goal_state ")
+
+ async def send_goal_status(
+ self,
+ chat_id: str,
+ status: str,
+ *,
+ started_at: float | None = None,
+ ) -> None:
+ """Notify subscribed clients that a turn started or finished (wall-clock hint)."""
+ conns = list(self._subs.get(chat_id, ()))
+ if not conns:
+ return
+ body: dict[str, Any] = {
+ "event": "goal_status",
+ "chat_id": chat_id,
+ "status": status,
+ }
+ if status == "running" and started_at is not None:
+ body["started_at"] = started_at
+ raw = json.dumps(body, ensure_ascii=False)
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" goal_status ")
+
+ async def send_session_updated(self, chat_id: str, *, scope: str | None = None) -> None:
+ """Notify WebUI clients that a session row should refresh."""
+ conns = list(self._conn_chats)
+ if not conns:
+ return
+ body: dict[str, Any] = {"event": "session_updated", "chat_id": chat_id}
+ if scope:
+ body["scope"] = scope
+ raw = json.dumps(body, ensure_ascii=False)
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" session_updated ")
+
+ async def send_runtime_model_updated(
+ self,
+ *,
+ model_name: Any,
+ model_preset: Any = None,
+ ) -> None:
+ """Broadcast runtime model changes to every open websocket connection."""
+ conns = list(self._conn_chats)
+ if not conns or not isinstance(model_name, str) or not model_name.strip():
+ return
+ body: dict[str, Any] = {
+ "event": "runtime_model_updated",
+ "model_name": model_name.strip(),
+ }
+ if isinstance(model_preset, str) and model_preset.strip():
+ body["model_preset"] = model_preset.strip()
+ raw = json.dumps(body, ensure_ascii=False)
+ for connection in conns:
+ await self._safe_send_to(connection, raw, label=" runtime_model_updated ")
diff --git a/agent/src/channels/wecom.py b/agent/src/channels/wecom.py
new file mode 100644
index 000000000..fc5c6c6cd
--- /dev/null
+++ b/agent/src/channels/wecom.py
@@ -0,0 +1,554 @@
+"""WeCom (Enterprise WeChat) channel implementation using wecom_aibot_sdk."""
+
+import asyncio
+import base64
+import hashlib
+import importlib.util
+import os
+import re
+from collections import OrderedDict
+from pathlib import Path
+from typing import Any
+
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir
+from pydantic import BaseModel
+
+WECOM_AVAILABLE = importlib.util.find_spec("wecom_aibot_sdk") is not None
+
+# Upload safety limits (matching QQ channel defaults)
+WECOM_UPLOAD_MAX_BYTES = 1024 * 1024 * 200 # 200MB
+
+# Replace unsafe characters with "_", keep Chinese and common safe punctuation.
+_SAFE_NAME_RE = re.compile(r"[^\w.\-()\[\]()【】\u4e00-\u9fff]+", re.UNICODE)
+
+
+def _sanitize_filename(name: str) -> str:
+ """Sanitize filename to avoid traversal and problematic chars."""
+ name = (name or "").strip()
+ name = Path(name).name
+ name = _SAFE_NAME_RE.sub("_", name).strip("._ ")
+ return name
+
+
+_IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
+_VIDEO_EXTS = {".mp4", ".avi", ".mov"}
+_AUDIO_EXTS = {".amr", ".mp3", ".wav", ".ogg"}
+
+
+def _guess_wecom_media_type(filename: str) -> str:
+ """Classify file extension as WeCom media_type string."""
+ ext = Path(filename).suffix.lower()
+ if ext in _IMAGE_EXTS:
+ return "image"
+ if ext in _VIDEO_EXTS:
+ return "video"
+ if ext in _AUDIO_EXTS:
+ return "voice"
+ return "file"
+
+class WecomConfig(BaseModel):
+ """WeCom (Enterprise WeChat) AI Bot channel configuration."""
+
+ enabled: bool = False
+ bot_id: str = ""
+ secret: str = ""
+ allow_from: list[str] = Field(default_factory=list)
+ welcome_message: str = ""
+
+
+# Message type display mapping
+MSG_TYPE_MAP = {
+ "image": "[image]",
+ "voice": "[voice]",
+ "file": "[file]",
+ "mixed": "[mixed content]",
+}
+
+
+class WecomChannel(BaseChannel):
+ """
+ WeCom (Enterprise WeChat) channel using WebSocket long connection.
+
+ Uses WebSocket to receive events - no public IP or webhook required.
+
+ Requires:
+ - Bot ID and Secret from WeCom AI Bot platform
+ """
+
+ name = "wecom"
+ display_name = "WeCom"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return WecomConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = WecomConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: WecomConfig = config
+ self._client: Any = None
+ self._processed_message_ids: OrderedDict[str, None] = OrderedDict()
+ self._loop: asyncio.AbstractEventLoop | None = None
+ self._generate_req_id = None
+ # Store frame headers for each chat to enable replies
+ self._chat_frames: dict[str, Any] = {}
+
+ async def start(self) -> None:
+ """Start the WeCom bot with WebSocket long connection."""
+ if not WECOM_AVAILABLE:
+ self.logger.error("SDK not installed. Run: pip install vibe-trading-ai[wecom]")
+ return
+
+ if not self.config.bot_id or not self.config.secret:
+ self.logger.error("bot_id and secret not configured")
+ return
+
+ from wecom_aibot_sdk import WSClient, generate_req_id
+
+ self._running = True
+ self._loop = asyncio.get_running_loop()
+ self._generate_req_id = generate_req_id
+
+ # Create WebSocket client
+ self._client = WSClient({
+ "bot_id": self.config.bot_id,
+ "secret": self.config.secret,
+ "reconnect_interval": 1000,
+ "max_reconnect_attempts": -1, # Infinite reconnect
+ "heartbeat_interval": 30000,
+ })
+
+ # Register event handlers
+ self._client.on("connected", self._on_connected)
+ self._client.on("authenticated", self._on_authenticated)
+ self._client.on("disconnected", self._on_disconnected)
+ self._client.on("error", self._on_error)
+ self._client.on("message.text", self._on_text_message)
+ self._client.on("message.image", self._on_image_message)
+ self._client.on("message.voice", self._on_voice_message)
+ self._client.on("message.file", self._on_file_message)
+ self._client.on("message.mixed", self._on_mixed_message)
+ self._client.on("event.enter_chat", self._on_enter_chat)
+
+ self.logger.info("bot starting with WebSocket long connection")
+ self.logger.info("No public IP required - using WebSocket to receive events")
+
+ # Connect
+ await self._client.connect_async()
+
+ # Keep running until stopped
+ while self._running:
+ await asyncio.sleep(1)
+
+ async def stop(self) -> None:
+ """Stop the WeCom bot."""
+ self._running = False
+ if self._client:
+ await self._client.disconnect()
+ self.logger.info("bot stopped")
+
+ async def _on_connected(self, frame: Any) -> None:
+ """Handle WebSocket connected event."""
+ self.logger.info("WebSocket connected")
+
+ async def _on_authenticated(self, frame: Any) -> None:
+ """Handle authentication success event."""
+ self.logger.info("authenticated successfully")
+
+ async def _on_disconnected(self, frame: Any) -> None:
+ """Handle WebSocket disconnected event."""
+ reason = frame.body if hasattr(frame, 'body') else str(frame)
+ self.logger.warning("WebSocket disconnected: {}", reason)
+
+ async def _on_error(self, frame: Any) -> None:
+ """Handle error event."""
+ self.logger.error("error: {}", frame)
+
+ async def _on_text_message(self, frame: Any) -> None:
+ """Handle text message."""
+ await self._process_message(frame, "text")
+
+ async def _on_image_message(self, frame: Any) -> None:
+ """Handle image message."""
+ await self._process_message(frame, "image")
+
+ async def _on_voice_message(self, frame: Any) -> None:
+ """Handle voice message."""
+ await self._process_message(frame, "voice")
+
+ async def _on_file_message(self, frame: Any) -> None:
+ """Handle file message."""
+ await self._process_message(frame, "file")
+
+ async def _on_mixed_message(self, frame: Any) -> None:
+ """Handle mixed content message."""
+ await self._process_message(frame, "mixed")
+
+ async def _on_enter_chat(self, frame: Any) -> None:
+ """Handle enter_chat event (user opens chat with bot)."""
+ try:
+ # Extract body from WsFrame dataclass or dict
+ if hasattr(frame, 'body'):
+ body = frame.body or {}
+ elif isinstance(frame, dict):
+ body = frame.get("body", frame)
+ else:
+ body = {}
+
+ chat_id = body.get("chatid", "") if isinstance(body, dict) else ""
+
+ if chat_id and not self.is_allowed(chat_id):
+ return
+
+ if chat_id and self.config.welcome_message:
+ await self._client.reply_welcome(frame, {
+ "msgtype": "text",
+ "text": {"content": self.config.welcome_message},
+ })
+ except Exception:
+ self.logger.exception("Error handling enter_chat")
+
+ async def _process_message(self, frame: Any, msg_type: str) -> None:
+ """Process incoming message and forward to bus."""
+ try:
+ # Extract body from WsFrame dataclass or dict
+ if hasattr(frame, 'body'):
+ body = frame.body or {}
+ elif isinstance(frame, dict):
+ body = frame.get("body", frame)
+ else:
+ body = {}
+
+ # Ensure body is a dict
+ if not isinstance(body, dict):
+ self.logger.warning("Invalid body type: {}", type(body))
+ return
+
+ # Extract message info
+ msg_id = body.get("msgid", "")
+ if not msg_id:
+ msg_id = f"{body.get('chatid', '')}_{body.get('sendertime', '')}"
+
+ # Extract sender info from "from" field (SDK format)
+ from_info = body.get("from", {})
+ sender_id = from_info.get("userid", "unknown") if isinstance(from_info, dict) else "unknown"
+ if not self.is_allowed(sender_id):
+ return
+
+ # Deduplication check
+ if msg_id in self._processed_message_ids:
+ return
+ self._processed_message_ids[msg_id] = None
+
+ # Trim cache
+ while len(self._processed_message_ids) > 1000:
+ self._processed_message_ids.popitem(last=False)
+
+ # For single chat, chatid is the sender's userid
+ # For group chat, chatid is provided in body
+ chat_type = body.get("chattype", "single")
+ chat_id = body.get("chatid", sender_id)
+
+ content_parts = []
+ media_paths: list[str] = []
+
+ if msg_type == "text":
+ text = body.get("text", {}).get("content", "")
+ if text:
+ content_parts.append(text)
+
+ elif msg_type == "image":
+ image_info = body.get("image", {})
+ file_url = image_info.get("url", "")
+ aes_key = image_info.get("aeskey", "")
+
+ if file_url and aes_key:
+ file_path = await self._download_and_save_media(file_url, aes_key, "image")
+ if file_path:
+ filename = os.path.basename(file_path)
+ content_parts.append(f"[image: {filename}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append("[image: download failed]")
+ else:
+ content_parts.append("[image: download failed]")
+
+ elif msg_type == "voice":
+ voice_info = body.get("voice", {})
+ # Voice message already contains transcribed content from WeCom
+ voice_content = voice_info.get("content", "")
+ if voice_content:
+ content_parts.append(f"[voice] {voice_content}")
+ else:
+ content_parts.append("[voice]")
+
+ elif msg_type == "file":
+ file_info = body.get("file", {})
+ file_url = file_info.get("url", "")
+ aes_key = file_info.get("aeskey", "")
+ file_name = file_info.get("name") or None
+
+ if file_url and aes_key:
+ file_path = await self._download_and_save_media(file_url, aes_key, "file", file_name)
+ if file_path:
+ display_name = os.path.basename(file_path)
+ content_parts.append(f"[file: {display_name}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append(f"[file: {file_name or 'unknown'}: download failed]")
+ else:
+ content_parts.append(f"[file: {file_name or 'unknown'}: download failed]")
+
+ elif msg_type == "mixed":
+ # Mixed content contains multiple message items
+ msg_items = body.get("mixed", {}).get("msg_item", [])
+ for item in msg_items:
+ item_type = item.get("msgtype", "")
+ if item_type == "text":
+ text = item.get("text", {}).get("content", "")
+ if text:
+ content_parts.append(text)
+ elif item_type == "image":
+ file_url = item.get("image", {}).get("url", "")
+ aes_key = item.get("image", {}).get("aeskey", "")
+ if file_url and aes_key:
+ file_path = await self._download_and_save_media(file_url, aes_key, "image")
+ if file_path:
+ filename = os.path.basename(file_path)
+ content_parts.append(f"[image: {filename}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append(MSG_TYPE_MAP.get(item_type, f"[{item_type}]"))
+
+ else:
+ content_parts.append(MSG_TYPE_MAP.get(msg_type, f"[{msg_type}]"))
+
+ content = "\n".join(content_parts) if content_parts else ""
+
+ if not content:
+ return
+
+ # Store frame for this chat to enable replies
+ self._chat_frames[chat_id] = frame
+
+ # Forward to message bus
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_id,
+ content=content,
+ media=media_paths or None,
+ metadata={
+ "message_id": msg_id,
+ "msg_type": msg_type,
+ "chat_type": chat_type,
+ }
+ )
+
+ except Exception:
+ self.logger.exception("Error processing message")
+
+ async def _download_and_save_media(
+ self,
+ file_url: str,
+ aes_key: str,
+ media_type: str,
+ filename: str | None = None,
+ ) -> str | None:
+ """
+ Download and decrypt media from WeCom.
+
+ Returns:
+ file_path or None if download failed
+ """
+ try:
+ data, fname = await self._client.download_file(file_url, aes_key)
+
+ if not data:
+ self.logger.warning("Failed to download media")
+ return None
+
+ if len(data) > WECOM_UPLOAD_MAX_BYTES:
+ self.logger.warning(
+ "inbound media too large: {} bytes (max {})",
+ len(data),
+ WECOM_UPLOAD_MAX_BYTES,
+ )
+ return None
+
+ media_dir = get_media_dir("wecom")
+ if not filename:
+ filename = fname or f"{media_type}_{hash(file_url) % 100000}"
+ filename = _sanitize_filename(filename)
+
+ file_path = media_dir / filename
+ await asyncio.to_thread(file_path.write_bytes, data)
+ self.logger.debug("Downloaded {} to {}", media_type, file_path)
+ return str(file_path)
+
+ except Exception:
+ self.logger.exception("Error downloading media")
+ return None
+
+ async def _upload_media_ws(
+ self, client: Any, file_path: str,
+ ) -> "tuple[str, str] | tuple[None, None]":
+ """Upload a local file to WeCom via WebSocket 3-step protocol (base64).
+
+ Uses the WeCom WebSocket upload commands directly via
+ ``client._ws_manager.send_reply()``:
+
+ ``aibot_upload_media_init`` → upload_id
+ ``aibot_upload_media_chunk`` × N (≤512 KB raw per chunk, base64)
+ ``aibot_upload_media_finish`` → media_id
+
+ Returns (media_id, media_type) on success, (None, None) on failure.
+ """
+ from wecom_aibot_sdk.utils import generate_req_id as _gen_req_id
+
+ try:
+ fname = os.path.basename(file_path)
+ media_type = _guess_wecom_media_type(fname)
+
+ # Read file size and data in a thread to avoid blocking the event loop
+ def _read_file():
+ file_size = os.path.getsize(file_path)
+ if file_size > WECOM_UPLOAD_MAX_BYTES:
+ raise ValueError(
+ f"File too large: {file_size} bytes (max {WECOM_UPLOAD_MAX_BYTES})"
+ )
+ with open(file_path, "rb") as f:
+ return file_size, f.read()
+
+ file_size, data = await asyncio.to_thread(_read_file)
+ # MD5 is used for file integrity only, not cryptographic security
+ md5_hash = hashlib.md5(data).hexdigest()
+
+ chunk_size = 512 * 1024 # 512 KB raw (before base64)
+ mv = memoryview(data)
+ chunk_list = [bytes(mv[i : i + chunk_size]) for i in range(0, file_size, chunk_size)]
+ n_chunks = len(chunk_list)
+ del mv, data
+
+ # Step 1: init
+ req_id = _gen_req_id("upload_init")
+ resp = await client._ws_manager.send_reply(req_id, {
+ "type": media_type,
+ "filename": fname,
+ "total_size": file_size,
+ "total_chunks": n_chunks,
+ "md5": md5_hash,
+ }, "aibot_upload_media_init")
+ if resp.errcode != 0:
+ self.logger.warning("upload init failed ({}): {}", resp.errcode, resp.errmsg)
+ return None, None
+ upload_id = resp.body.get("upload_id") if resp.body else None
+ if not upload_id:
+ self.logger.warning("upload init: no upload_id in response")
+ return None, None
+
+ # Step 2: send chunks
+ for i, chunk in enumerate(chunk_list):
+ req_id = _gen_req_id("upload_chunk")
+ resp = await client._ws_manager.send_reply(req_id, {
+ "upload_id": upload_id,
+ "chunk_index": i,
+ "base64_data": base64.b64encode(chunk).decode(),
+ }, "aibot_upload_media_chunk")
+ if resp.errcode != 0:
+ self.logger.warning("upload chunk {} failed ({}): {}", i, resp.errcode, resp.errmsg)
+ return None, None
+
+ # Step 3: finish
+ req_id = _gen_req_id("upload_finish")
+ resp = await client._ws_manager.send_reply(req_id, {
+ "upload_id": upload_id,
+ }, "aibot_upload_media_finish")
+ if resp.errcode != 0:
+ self.logger.warning("upload finish failed ({}): {}", resp.errcode, resp.errmsg)
+ return None, None
+
+ media_id = resp.body.get("media_id") if resp.body else None
+ if not media_id:
+ self.logger.warning("upload finish: no media_id in response body={}", resp.body)
+ return None, None
+
+ suffix = "..." if len(media_id) > 16 else ""
+ self.logger.debug("uploaded {} ({}) → media_id={}", fname, media_type, media_id[:16] + suffix)
+ return media_id, media_type
+
+ except ValueError as e:
+ self.logger.warning("upload skipped for {}: {}", file_path, e)
+ return None, None
+ except Exception:
+ self.logger.exception("_upload_media_ws error for {}", file_path)
+ return None, None
+
+ async def send(self, msg: OutboundMessage) -> None:
+ """Send a message through WeCom."""
+ if not self._client:
+ self.logger.warning("client not initialized")
+ return
+
+ try:
+ content = (msg.content or "").strip()
+ is_progress = bool(msg.metadata.get("_progress"))
+
+ # Get the stored frame for this chat
+ frame = self._chat_frames.get(msg.chat_id)
+
+ # Send media files via WebSocket upload
+ for file_path in msg.media or []:
+ if not os.path.isfile(file_path):
+ self.logger.warning("media file not found: {}", file_path)
+ continue
+ media_id, media_type = await self._upload_media_ws(self._client, file_path)
+ if media_id:
+ if frame:
+ await self._client.reply(frame, {
+ "msgtype": media_type,
+ media_type: {"media_id": media_id},
+ })
+ else:
+ await self._client.send_message(msg.chat_id, {
+ "msgtype": media_type,
+ media_type: {"media_id": media_id},
+ })
+ self.logger.debug("sent {} → {}", media_type, msg.chat_id)
+ else:
+ content += f"\n[file upload failed: {os.path.basename(file_path)}]"
+
+ if not content:
+ return
+
+ if frame:
+ # Both progress and final messages must use reply_stream (cmd="aibot_respond_msg").
+ # The plain reply() uses cmd="reply" which does not support "text" msgtype
+ # and causes errcode=40008 from WeCom API.
+ stream_id = self._generate_req_id("stream")
+ await self._client.reply_stream(
+ frame,
+ stream_id,
+ content,
+ finish=not is_progress,
+ )
+ self.logger.debug(
+ "{} sent to {}",
+ "progress" if is_progress else "message",
+ msg.chat_id,
+ )
+ else:
+ # No frame (e.g. cron push): proactive send only supports markdown
+ await self._client.send_message(msg.chat_id, {
+ "msgtype": "markdown",
+ "markdown": {"content": content},
+ })
+ self.logger.info("proactive send to {}", msg.chat_id)
+
+ except Exception:
+ self.logger.exception("Error sending message to chat_id={}", msg.chat_id)
diff --git a/agent/src/channels/weixin.py b/agent/src/channels/weixin.py
new file mode 100644
index 000000000..3038e3b54
--- /dev/null
+++ b/agent/src/channels/weixin.py
@@ -0,0 +1,1586 @@
+"""Personal WeChat (微信) channel using HTTP long-poll API.
+
+Uses the ilinkai.weixin.qq.com API for personal WeChat messaging.
+No WebSocket, no local WeChat client needed — just HTTP requests with a
+bot token obtained via QR code login.
+
+Protocol reverse-engineered from ``@tencent-weixin/openclaw-weixin`` v1.0.3.
+"""
+
+from __future__ import annotations
+
+import asyncio
+import base64
+import hashlib
+import json
+import os
+import random
+import re
+import time
+import uuid
+from collections import OrderedDict
+from contextlib import suppress
+from pathlib import Path
+from typing import Any
+from urllib.parse import quote
+
+import httpx
+import logging; logger = logging.getLogger(__name__)
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir, get_runtime_subdir
+from pydantic import BaseModel
+from src.channels.utils import split_message
+
+# ---------------------------------------------------------------------------
+# Protocol constants (from openclaw-weixin types.ts)
+# ---------------------------------------------------------------------------
+
+# MessageItemType
+ITEM_TEXT = 1
+ITEM_IMAGE = 2
+ITEM_VOICE = 3
+ITEM_FILE = 4
+ITEM_VIDEO = 5
+
+# MessageType (1 = inbound from user, 2 = outbound from bot)
+MESSAGE_TYPE_BOT = 2
+
+# MessageState
+MESSAGE_STATE_FINISH = 2
+
+WEIXIN_MAX_MESSAGE_LEN = 4000
+WEIXIN_CHANNEL_VERSION = "2.1.1"
+ILINK_APP_ID = "bot"
+
+
+def _build_client_version(version: str) -> int:
+ """Encode semantic version as 0x00MMNNPP (major/minor/patch in one uint32)."""
+ parts = version.split(".")
+
+ def _as_int(idx: int) -> int:
+ try:
+ return int(parts[idx])
+ except Exception:
+ return 0
+
+ major = _as_int(0)
+ minor = _as_int(1)
+ patch = _as_int(2)
+ return ((major & 0xFF) << 16) | ((minor & 0xFF) << 8) | (patch & 0xFF)
+
+ILINK_APP_CLIENT_VERSION = _build_client_version(WEIXIN_CHANNEL_VERSION)
+BASE_INFO: dict[str, str] = {"channel_version": WEIXIN_CHANNEL_VERSION}
+
+# Session-expired error code
+ERRCODE_SESSION_EXPIRED = -14
+SESSION_PAUSE_DURATION_S = 60 * 60
+
+# iLink context_token is observed to expire server-side after ~90-160s of
+# agent inactivity (openclaw/openclaw#61174). Proactively refresh before
+# sending if the cached token is older than this threshold.
+CONTEXT_TOKEN_MAX_AGE_S = 60
+
+
+# Retry constants (matching the reference plugin's monitor.ts)
+MAX_CONSECUTIVE_FAILURES = 3
+BACKOFF_DELAY_S = 30
+RETRY_DELAY_S = 2
+MAX_QR_REFRESH_COUNT = 3
+TYPING_STATUS_TYPING = 1
+TYPING_STATUS_CANCEL = 2
+TYPING_TICKET_TTL_S = 24 * 60 * 60
+TYPING_KEEPALIVE_INTERVAL_S = 5
+CONFIG_CACHE_INITIAL_RETRY_S = 2
+CONFIG_CACHE_MAX_RETRY_S = 60 * 60
+
+# Default long-poll timeout; overridden by server via longpolling_timeout_ms.
+DEFAULT_LONG_POLL_TIMEOUT_S = 35
+
+# Media-type codes for getuploadurl (1=image, 2=video, 3=file, 4=voice)
+UPLOAD_MEDIA_IMAGE = 1
+UPLOAD_MEDIA_VIDEO = 2
+UPLOAD_MEDIA_FILE = 3
+UPLOAD_MEDIA_VOICE = 4
+
+# File extensions considered as images / videos for outbound media
+_IMAGE_EXTS = {".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".tiff", ".ico", ".svg"}
+_VIDEO_EXTS = {".mp4", ".avi", ".mov", ".mkv", ".webm", ".flv"}
+_VOICE_EXTS = {".mp3", ".wav", ".amr", ".silk", ".ogg", ".m4a", ".aac", ".flac"}
+
+
+def _has_downloadable_media_locator(media: dict[str, Any] | None) -> bool:
+ if not isinstance(media, dict):
+ return False
+ return bool(str(media.get("encrypt_query_param", "") or "") or str(media.get("full_url", "") or "").strip())
+
+
+class WeixinConfig(BaseModel):
+ """Personal WeChat channel configuration."""
+
+ enabled: bool = False
+ allow_from: list[str] = Field(default_factory=list)
+ base_url: str = "https://ilinkai.weixin.qq.com"
+ cdn_base_url: str = "https://novac2c.cdn.weixin.qq.com/c2c"
+ route_tag: str | int | None = None
+ token: str = "" # Manually set token, or obtained via QR login
+ state_dir: str = "" # Default: ~/.vibe-trading/weixin/
+ poll_timeout: int = DEFAULT_LONG_POLL_TIMEOUT_S # seconds for long-poll
+
+
+class WeixinChannel(BaseChannel):
+ """
+ Personal WeChat channel using HTTP long-poll.
+
+ Connects to ilinkai.weixin.qq.com API to receive and send personal
+ WeChat messages. Authentication is via QR code login which produces
+ a bot token.
+ """
+
+ name = "weixin"
+ display_name = "WeChat"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return WeixinConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ if isinstance(config, dict):
+ config = WeixinConfig.model_validate(config)
+ super().__init__(config, bus)
+ self.config: WeixinConfig = config
+
+ # State
+ self._client: httpx.AsyncClient | None = None
+ self._get_updates_buf: str = ""
+ self._context_tokens: dict[str, str] = {} # from_user_id -> context_token
+ self._processed_ids: OrderedDict[str, None] = OrderedDict()
+ self._state_dir: Path | None = None
+ self._token: str = ""
+ self._poll_task: asyncio.Task | None = None
+ self._next_poll_timeout_s: int = DEFAULT_LONG_POLL_TIMEOUT_S
+ self._session_pause_until: float = 0.0
+ self._typing_tasks: dict[str, asyncio.Task] = {}
+ self._typing_tickets: dict[str, dict[str, Any]] = {}
+ self._context_token_at: dict[str, float] = {}
+ self._pending_tool_hints: dict[str, list[str]] = {}
+
+ # ------------------------------------------------------------------
+ # State persistence
+ # ------------------------------------------------------------------
+
+ def _get_state_dir(self) -> Path:
+ if self._state_dir:
+ return self._state_dir
+ if self.config.state_dir:
+ d = Path(self.config.state_dir).expanduser()
+ else:
+ d = get_runtime_subdir("weixin")
+ d.mkdir(parents=True, exist_ok=True)
+ self._state_dir = d
+ return d
+
+ def _load_state(self) -> bool:
+ """Load saved account state. Returns True if a valid token was found."""
+ state_file = self._get_state_dir() / "account.json"
+ if not state_file.exists():
+ return False
+ try:
+ data = json.loads(state_file.read_text())
+ self._token = data.get("token", "")
+ self._get_updates_buf = data.get("get_updates_buf", "")
+ context_tokens = data.get("context_tokens", {})
+ if isinstance(context_tokens, dict):
+ self._context_tokens = {
+ str(user_id): str(token)
+ for user_id, token in context_tokens.items()
+ if str(user_id).strip() and str(token).strip()
+ }
+ else:
+ self._context_tokens = {}
+ typing_tickets = data.get("typing_tickets", {})
+ if isinstance(typing_tickets, dict):
+ self._typing_tickets = {
+ str(user_id): ticket
+ for user_id, ticket in typing_tickets.items()
+ if str(user_id).strip() and isinstance(ticket, dict)
+ }
+ else:
+ self._typing_tickets = {}
+ base_url = data.get("base_url", "")
+ if base_url:
+ self.config.base_url = base_url
+ return bool(self._token)
+ except Exception:
+ self.logger.error("Failed to load Weixin account state", exc_info=True)
+ return False
+
+ def _save_state(self) -> None:
+ state_file = self._get_state_dir() / "account.json"
+ with suppress(Exception):
+ data = {
+ "token": self._token,
+ "get_updates_buf": self._get_updates_buf,
+ "context_tokens": self._context_tokens,
+ "typing_tickets": self._typing_tickets,
+ "base_url": self.config.base_url,
+ }
+ state_file.write_text(json.dumps(data, ensure_ascii=False))
+
+ # ------------------------------------------------------------------
+ # HTTP helpers (matches api.ts buildHeaders / apiFetch)
+ # ------------------------------------------------------------------
+
+ @staticmethod
+ def _random_wechat_uin() -> str:
+ """X-WECHAT-UIN: random uint32 → decimal string → base64.
+
+ Matches the reference plugin's ``randomWechatUin()`` in api.ts.
+ Generated fresh for **every** request (same as reference).
+ """
+ uint32 = int.from_bytes(os.urandom(4), "big")
+ return base64.b64encode(str(uint32).encode()).decode()
+
+ def _make_headers(self, *, auth: bool = True) -> dict[str, str]:
+ """Build per-request headers (new UIN each call, matching reference)."""
+ headers: dict[str, str] = {
+ "X-WECHAT-UIN": self._random_wechat_uin(),
+ "Content-Type": "application/json",
+ "AuthorizationType": "ilink_bot_token",
+ "iLink-App-Id": ILINK_APP_ID,
+ "iLink-App-ClientVersion": str(ILINK_APP_CLIENT_VERSION),
+ }
+ if auth and self._token:
+ headers["Authorization"] = f"Bearer {self._token}"
+ if self.config.route_tag is not None and str(self.config.route_tag).strip():
+ headers["SKRouteTag"] = str(self.config.route_tag).strip()
+ return headers
+
+ @staticmethod
+ def _is_retryable_media_download_error(err: Exception) -> bool:
+ if isinstance(err, httpx.TimeoutException | httpx.TransportError):
+ return True
+ if isinstance(err, httpx.HTTPStatusError):
+ status_code = err.response.status_code if err.response is not None else 0
+ return status_code >= 500
+ return False
+
+ async def _api_get(
+ self,
+ endpoint: str,
+ params: dict | None = None,
+ *,
+ auth: bool = True,
+ extra_headers: dict[str, str] | None = None,
+ ) -> dict:
+ assert self._client is not None
+ url = f"{self.config.base_url}/{endpoint}"
+ hdrs = self._make_headers(auth=auth)
+ if extra_headers:
+ hdrs.update(extra_headers)
+ resp = await self._client.get(url, params=params, headers=hdrs)
+ resp.raise_for_status()
+ return resp.json()
+
+ async def _api_get_with_base(
+ self,
+ *,
+ base_url: str,
+ endpoint: str,
+ params: dict | None = None,
+ auth: bool = True,
+ extra_headers: dict[str, str] | None = None,
+ ) -> dict:
+ """GET helper that allows overriding base_url for QR redirect polling."""
+ assert self._client is not None
+ url = f"{base_url.rstrip('/')}/{endpoint}"
+ hdrs = self._make_headers(auth=auth)
+ if extra_headers:
+ hdrs.update(extra_headers)
+ resp = await self._client.get(url, params=params, headers=hdrs)
+ resp.raise_for_status()
+ return resp.json()
+
+ async def _api_post(
+ self,
+ endpoint: str,
+ body: dict | None = None,
+ *,
+ auth: bool = True,
+ ) -> dict:
+ assert self._client is not None
+ url = f"{self.config.base_url}/{endpoint}"
+ payload = body or {}
+ if "base_info" not in payload:
+ payload["base_info"] = BASE_INFO
+ resp = await self._client.post(url, json=payload, headers=self._make_headers(auth=auth))
+ resp.raise_for_status()
+ return resp.json()
+
+ # ------------------------------------------------------------------
+ # QR Code Login (matches login-qr.ts)
+ # ------------------------------------------------------------------
+
+ async def _fetch_qr_code(self) -> tuple[str, str]:
+ """Fetch a fresh QR code. Returns (qrcode_id, scan_url)."""
+ data = await self._api_get(
+ "ilink/bot/get_bot_qrcode",
+ params={"bot_type": "3"},
+ auth=False,
+ )
+ qrcode_img_content = data.get("qrcode_img_content", "")
+ qrcode_id = data.get("qrcode", "")
+ if not qrcode_id:
+ raise RuntimeError(f"Failed to get QR code from WeChat API: {data}")
+ return qrcode_id, (qrcode_img_content or qrcode_id)
+
+ async def _qr_login(self) -> bool:
+ """Perform QR code login flow. Returns True on success."""
+ try:
+ refresh_count = 0
+ qrcode_id, scan_url = await self._fetch_qr_code()
+ self._print_qr_code(scan_url)
+ current_poll_base_url = self.config.base_url
+
+ while self._running:
+ try:
+ status_data = await self._api_get_with_base(
+ base_url=current_poll_base_url,
+ endpoint="ilink/bot/get_qrcode_status",
+ params={"qrcode": qrcode_id},
+ auth=False,
+ )
+ except Exception as e:
+ if self._is_retryable_qr_poll_error(e):
+ await asyncio.sleep(1)
+ continue
+ raise
+
+ if not isinstance(status_data, dict):
+ await asyncio.sleep(1)
+ continue
+
+ status = status_data.get("status", "")
+ if status == "confirmed":
+ token = status_data.get("bot_token", "")
+ bot_id = status_data.get("ilink_bot_id", "")
+ base_url = status_data.get("baseurl", "")
+ user_id = status_data.get("ilink_user_id", "")
+ if token:
+ self._token = token
+ if base_url:
+ self.config.base_url = base_url
+ self._save_state()
+ self.logger.info(
+ "login successful! bot_id={} user_id={}",
+ bot_id,
+ user_id,
+ )
+ return True
+ else:
+ self.logger.error("Login confirmed but no bot_token in response")
+ return False
+ elif status == "scaned_but_redirect":
+ redirect_host = str(status_data.get("redirect_host", "") or "").strip()
+ if redirect_host:
+ if redirect_host.startswith("http://") or redirect_host.startswith("https://"):
+ redirected_base = redirect_host
+ else:
+ redirected_base = f"https://{redirect_host}"
+ if redirected_base != current_poll_base_url:
+ current_poll_base_url = redirected_base
+ elif status == "expired":
+ refresh_count += 1
+ if refresh_count > MAX_QR_REFRESH_COUNT:
+ self.logger.warning(
+ "QR code expired too many times ({}/{}), giving up.",
+ refresh_count - 1,
+ MAX_QR_REFRESH_COUNT,
+ )
+ return False
+ qrcode_id, scan_url = await self._fetch_qr_code()
+ current_poll_base_url = self.config.base_url
+ self._print_qr_code(scan_url)
+ continue
+ # status == "wait" — keep polling
+
+ await asyncio.sleep(1)
+
+ except Exception:
+ self.logger.exception("QR login failed")
+
+ return False
+
+ @staticmethod
+ def _is_retryable_qr_poll_error(err: Exception) -> bool:
+ if isinstance(err, httpx.TimeoutException | httpx.TransportError):
+ return True
+ if isinstance(err, httpx.HTTPStatusError):
+ status_code = err.response.status_code if err.response is not None else 0
+ if status_code >= 500:
+ return True
+ return False
+
+ @staticmethod
+ def _print_qr_code(url: str) -> None:
+ try:
+ import qrcode as qr_lib
+
+ qr = qr_lib.QRCode(border=1)
+ qr.add_data(url)
+ qr.make(fit=True)
+ qr.print_ascii(invert=True)
+ except ImportError:
+ print(f"\nLogin URL: {url}\n")
+
+ # ------------------------------------------------------------------
+ # Channel lifecycle
+ # ------------------------------------------------------------------
+
+ async def login(self, force: bool = False) -> bool:
+ """Perform QR code login and save token. Returns True on success."""
+ if force:
+ self._token = ""
+ self._get_updates_buf = ""
+ state_file = self._get_state_dir() / "account.json"
+ if state_file.exists():
+ state_file.unlink()
+ if self._token or self._load_state():
+ return True
+
+ # Initialize HTTP client for the login flow
+ self._client = httpx.AsyncClient(
+ timeout=httpx.Timeout(60, connect=30),
+ follow_redirects=True,
+ )
+ self._running = True # Enable polling loop in _qr_login()
+ try:
+ return await self._qr_login()
+ finally:
+ self._running = False
+ if self._client:
+ await self._client.aclose()
+ self._client = None
+
+ async def start(self) -> None:
+ self._running = True
+ self._next_poll_timeout_s = self.config.poll_timeout
+ self._client = httpx.AsyncClient(
+ timeout=httpx.Timeout(self._next_poll_timeout_s + 10, connect=30),
+ follow_redirects=True,
+ )
+
+ if self.config.token:
+ self._token = self.config.token
+ elif not self._load_state():
+ if not await self._qr_login():
+ self.logger.error("login failed. Run 'vibe-trading channels login weixin' to authenticate.")
+ self._running = False
+ return
+
+ self.logger.info("channel starting with long-poll...")
+
+ consecutive_failures = 0
+ while self._running:
+ try:
+ await self._poll_once()
+ consecutive_failures = 0
+ except httpx.TimeoutException:
+ # Normal for long-poll, just retry
+ continue
+ except Exception:
+ if not self._running:
+ break
+ self.logger.exception("WeChat poll loop error")
+ consecutive_failures += 1
+ if consecutive_failures >= MAX_CONSECUTIVE_FAILURES:
+ consecutive_failures = 0
+ await asyncio.sleep(BACKOFF_DELAY_S)
+ else:
+ await asyncio.sleep(RETRY_DELAY_S)
+
+ async def stop(self) -> None:
+ self._running = False
+ self._pending_tool_hints.clear()
+ if self._poll_task and not self._poll_task.done():
+ self._poll_task.cancel()
+ for chat_id in list(self._typing_tasks):
+ await self._stop_typing(chat_id, clear_remote=False)
+ if self._client:
+ await self._client.aclose()
+ self._client = None
+ self._save_state()
+ # ------------------------------------------------------------------
+ # Polling (matches monitor.ts monitorWeixinProvider)
+ # ------------------------------------------------------------------
+
+ def _pause_session(self, duration_s: int = SESSION_PAUSE_DURATION_S) -> None:
+ self._session_pause_until = time.time() + duration_s
+
+ def _session_pause_remaining_s(self) -> int:
+ remaining = int(self._session_pause_until - time.time())
+ if remaining <= 0:
+ self._session_pause_until = 0.0
+ return 0
+ return remaining
+
+ def _assert_session_active(self) -> None:
+ remaining = self._session_pause_remaining_s()
+ if remaining > 0:
+ remaining_min = max((remaining + 59) // 60, 1)
+ raise RuntimeError(
+ f"WeChat session paused, {remaining_min} min remaining (errcode {ERRCODE_SESSION_EXPIRED})"
+ )
+
+ async def _poll_once(self) -> None:
+ remaining = self._session_pause_remaining_s()
+ if remaining > 0:
+ await asyncio.sleep(remaining)
+ return
+
+ body: dict[str, Any] = {
+ "get_updates_buf": self._get_updates_buf,
+ "base_info": BASE_INFO,
+ }
+
+ # Adjust httpx timeout to match the current poll timeout
+ assert self._client is not None
+ self._client.timeout = httpx.Timeout(self._next_poll_timeout_s + 10, connect=30)
+
+ data = await self._api_post("ilink/bot/getupdates", body)
+
+ # Check for API-level errors (monitor.ts checks both ret and errcode)
+ ret = data.get("ret", 0)
+ errcode = data.get("errcode", 0)
+
+ is_error = (ret is not None and ret != 0) or (errcode is not None and errcode != 0)
+
+ if is_error:
+ if errcode == ERRCODE_SESSION_EXPIRED or ret == ERRCODE_SESSION_EXPIRED:
+ self._pause_session()
+ remaining = self._session_pause_remaining_s()
+ self.logger.warning(
+ "session expired (errcode {}). Pausing {} min.",
+ errcode,
+ max((remaining + 59) // 60, 1),
+ )
+ return
+ raise RuntimeError(
+ f"getUpdates failed: ret={ret} errcode={errcode} errmsg={data.get('errmsg', '')}"
+ )
+
+ # Honour server-suggested poll timeout (monitor.ts:102-105)
+ server_timeout_ms = data.get("longpolling_timeout_ms")
+ if server_timeout_ms and server_timeout_ms > 0:
+ self._next_poll_timeout_s = max(server_timeout_ms // 1000, 5)
+
+ # Update cursor
+ new_buf = data.get("get_updates_buf", "")
+ if new_buf:
+ self._get_updates_buf = new_buf
+ self._save_state()
+
+ # Process messages (WeixinMessage[] from types.ts)
+ msgs: list[dict] = data.get("msgs", []) or []
+ for msg in msgs:
+ try:
+ await self._process_message(msg)
+ except Exception:
+ self.logger.exception("Failed to process WeChat message")
+
+ # ------------------------------------------------------------------
+ # Inbound message processing (matches inbound.ts + process-message.ts)
+ # ------------------------------------------------------------------
+
+ async def _process_message(self, msg: dict) -> None:
+ """Process a single WeixinMessage from getUpdates."""
+ # Skip bot's own messages (message_type 2 = BOT)
+ if msg.get("message_type") == MESSAGE_TYPE_BOT:
+ return
+
+ msg_id = str(msg.get("message_id", "") or msg.get("seq", ""))
+ if not msg_id:
+ msg_id = f"{msg.get('from_user_id', '')}_{msg.get('create_time_ms', '')}"
+
+ from_user_id = msg.get("from_user_id", "") or ""
+ if not from_user_id:
+ return
+
+ # Deduplication by message_id
+ if msg_id in self._processed_ids:
+ return
+ self._processed_ids[msg_id] = None
+ while len(self._processed_ids) > 1000:
+ self._processed_ids.popitem(last=False)
+
+ ctx_token = msg.get("context_token", "")
+ if not self.is_allowed(from_user_id):
+ if from_user_id.endswith("@chatroom"):
+ await self._handle_message(
+ sender_id=from_user_id,
+ chat_id=from_user_id,
+ content="",
+ metadata={"message_id": msg_id},
+ is_dm=False,
+ )
+ return
+
+ if not ctx_token:
+ self.logger.warning(
+ "Access denied for sender {}; cannot send WeChat pairing code without context_token",
+ from_user_id,
+ )
+ return
+
+ had_ctx_token = from_user_id in self._context_tokens
+ previous_ctx_token = self._context_tokens.get(from_user_id, "")
+ had_ctx_token_at = from_user_id in self._context_token_at
+ previous_ctx_token_at = self._context_token_at.get(from_user_id, 0.0)
+ self._context_tokens[from_user_id] = ctx_token
+ self._context_token_at[from_user_id] = time.time()
+ try:
+ await self._handle_message(
+ sender_id=from_user_id,
+ chat_id=from_user_id,
+ content="",
+ metadata={"message_id": msg_id},
+ is_dm=True,
+ )
+ finally:
+ if had_ctx_token:
+ self._context_tokens[from_user_id] = previous_ctx_token
+ else:
+ self._context_tokens.pop(from_user_id, None)
+ if had_ctx_token_at:
+ self._context_token_at[from_user_id] = previous_ctx_token_at
+ else:
+ self._context_token_at.pop(from_user_id, None)
+ return
+
+ # Cache context_token (required for all replies — inbound.ts:23-27)
+ if ctx_token:
+ self._context_tokens[from_user_id] = ctx_token
+ self._context_token_at[from_user_id] = time.time()
+ self._save_state()
+
+ # Parse item_list (WeixinMessage.item_list — types.ts:161)
+ item_list: list[dict] = msg.get("item_list") or []
+ content_parts: list[str] = []
+ media_paths: list[str] = []
+ has_top_level_downloadable_media = False
+
+ for item in item_list:
+ item_type = item.get("type", 0)
+
+ if item_type == ITEM_TEXT:
+ text = (item.get("text_item") or {}).get("text", "")
+ if text:
+ # Handle quoted/ref messages (inbound.ts:86-98)
+ ref = item.get("ref_msg")
+ if ref:
+ ref_item = ref.get("message_item")
+ # If quoted message is media, just pass the text
+ if ref_item and ref_item.get("type", 0) in (
+ ITEM_IMAGE,
+ ITEM_VOICE,
+ ITEM_FILE,
+ ITEM_VIDEO,
+ ):
+ content_parts.append(text)
+ else:
+ parts: list[str] = []
+ if ref.get("title"):
+ parts.append(ref["title"])
+ if ref_item:
+ ref_text = (ref_item.get("text_item") or {}).get("text", "")
+ if ref_text:
+ parts.append(ref_text)
+ if parts:
+ content_parts.append(f"[引用: {' | '.join(parts)}]\n{text}")
+ else:
+ content_parts.append(text)
+ else:
+ content_parts.append(text)
+
+ elif item_type == ITEM_IMAGE:
+ image_item = item.get("image_item") or {}
+ if _has_downloadable_media_locator(image_item.get("media")):
+ has_top_level_downloadable_media = True
+ file_path = await self._download_media_item(image_item, "image")
+ if file_path:
+ content_parts.append(f"[image]\n[Image: source: {file_path}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append("[image]")
+
+ elif item_type == ITEM_VOICE:
+ voice_item = item.get("voice_item") or {}
+ # Voice-to-text provided by WeChat (inbound.ts:101-103)
+ voice_text = voice_item.get("text", "")
+ if voice_text:
+ content_parts.append(f"[voice] {voice_text}")
+ else:
+ if _has_downloadable_media_locator(voice_item.get("media")):
+ has_top_level_downloadable_media = True
+ file_path = await self._download_media_item(voice_item, "voice")
+ if file_path:
+ transcription = await self.transcribe_audio(file_path)
+ if transcription:
+ content_parts.append(f"[voice] {transcription}")
+ else:
+ content_parts.append(f"[voice]\n[Audio: source: {file_path}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append("[voice]")
+
+ elif item_type == ITEM_FILE:
+ file_item = item.get("file_item") or {}
+ if _has_downloadable_media_locator(file_item.get("media")):
+ has_top_level_downloadable_media = True
+ file_name = file_item.get("file_name", "unknown")
+ file_path = await self._download_media_item(
+ file_item,
+ "file",
+ file_name,
+ )
+ if file_path:
+ content_parts.append(f"[file: {file_name}]\n[File: source: {file_path}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append(f"[file: {file_name}]")
+
+ elif item_type == ITEM_VIDEO:
+ video_item = item.get("video_item") or {}
+ if _has_downloadable_media_locator(video_item.get("media")):
+ has_top_level_downloadable_media = True
+ file_path = await self._download_media_item(video_item, "video")
+ if file_path:
+ content_parts.append(f"[video]\n[Video: source: {file_path}]")
+ media_paths.append(file_path)
+ else:
+ content_parts.append("[video]")
+
+ # Fallback: when no top-level media was downloaded, try quoted/referenced media.
+ # This aligns with the reference plugin behavior that checks ref_msg.message_item
+ # when main item_list has no downloadable media.
+ if not media_paths and not has_top_level_downloadable_media:
+ ref_media_item: dict[str, Any] | None = None
+ for item in item_list:
+ if item.get("type", 0) != ITEM_TEXT:
+ continue
+ ref = item.get("ref_msg") or {}
+ candidate = ref.get("message_item") or {}
+ if candidate.get("type", 0) in (ITEM_IMAGE, ITEM_VOICE, ITEM_FILE, ITEM_VIDEO):
+ ref_media_item = candidate
+ break
+
+ if ref_media_item:
+ ref_type = ref_media_item.get("type", 0)
+ if ref_type == ITEM_IMAGE:
+ image_item = ref_media_item.get("image_item") or {}
+ file_path = await self._download_media_item(image_item, "image")
+ if file_path:
+ content_parts.append(f"[image]\n[Image: source: {file_path}]")
+ media_paths.append(file_path)
+ elif ref_type == ITEM_VOICE:
+ voice_item = ref_media_item.get("voice_item") or {}
+ file_path = await self._download_media_item(voice_item, "voice")
+ if file_path:
+ transcription = await self.transcribe_audio(file_path)
+ if transcription:
+ content_parts.append(f"[voice] {transcription}")
+ else:
+ content_parts.append(f"[voice]\n[Audio: source: {file_path}]")
+ media_paths.append(file_path)
+ elif ref_type == ITEM_FILE:
+ file_item = ref_media_item.get("file_item") or {}
+ file_name = file_item.get("file_name", "unknown")
+ file_path = await self._download_media_item(file_item, "file", file_name)
+ if file_path:
+ content_parts.append(f"[file: {file_name}]\n[File: source: {file_path}]")
+ media_paths.append(file_path)
+ elif ref_type == ITEM_VIDEO:
+ video_item = ref_media_item.get("video_item") or {}
+ file_path = await self._download_media_item(video_item, "video")
+ if file_path:
+ content_parts.append(f"[video]\n[Video: source: {file_path}]")
+ media_paths.append(file_path)
+
+ content = "\n".join(content_parts)
+ if not content:
+ return
+
+ self.logger.info(
+ "inbound: from={} items={} bodyLen={}",
+ from_user_id,
+ ",".join(str(i.get("type", 0)) for i in item_list),
+ len(content),
+ )
+
+ await self._start_typing(from_user_id, ctx_token)
+
+ await self._handle_message(
+ sender_id=from_user_id,
+ chat_id=from_user_id,
+ content=content,
+ media=media_paths or None,
+ metadata={"message_id": msg_id},
+ )
+
+ # ------------------------------------------------------------------
+ # Media download (matches media-download.ts + pic-decrypt.ts)
+ # ------------------------------------------------------------------
+
+ async def _download_media_item(
+ self,
+ typed_item: dict,
+ media_type: str,
+ filename: str | None = None,
+ ) -> str | None:
+ """Download + AES-decrypt a media item. Returns local path or None."""
+ try:
+ media = typed_item.get("media") or {}
+ encrypt_query_param = str(media.get("encrypt_query_param", "") or "")
+ full_url = str(media.get("full_url", "") or "").strip()
+
+ if not encrypt_query_param and not full_url:
+ return None
+
+ # Resolve AES key (media-download.ts:43-45, pic-decrypt.ts:40-52)
+ # image_item.aeskey is a raw hex string (16 bytes as 32 hex chars).
+ # media.aes_key is always base64-encoded.
+ # For images, prefer image_item.aeskey; for others use media.aes_key.
+ raw_aeskey_hex = typed_item.get("aeskey", "")
+ media_aes_key_b64 = media.get("aes_key", "")
+
+ aes_key_b64: str = ""
+ if raw_aeskey_hex:
+ # Convert hex → raw bytes → base64 (matches media-download.ts:43-44)
+ aes_key_b64 = base64.b64encode(bytes.fromhex(raw_aeskey_hex)).decode()
+ elif media_aes_key_b64:
+ aes_key_b64 = media_aes_key_b64
+
+ # Reference protocol behavior: VOICE/FILE/VIDEO require aes_key;
+ # only IMAGE may be downloaded as plain bytes when key is missing.
+ if media_type != "image" and not aes_key_b64:
+ return None
+
+ assert self._client is not None
+ fallback_url = ""
+ if encrypt_query_param:
+ fallback_url = (
+ f"{self.config.cdn_base_url}/download"
+ f"?encrypted_query_param={quote(encrypt_query_param)}"
+ )
+
+ download_candidates: list[tuple[str, str]] = []
+ if full_url:
+ download_candidates.append(("full_url", full_url))
+ if fallback_url and (not full_url or fallback_url != full_url):
+ download_candidates.append(("encrypt_query_param", fallback_url))
+
+ data = b""
+ for idx, (download_source, cdn_url) in enumerate(download_candidates):
+ try:
+ resp = await self._client.get(cdn_url)
+ resp.raise_for_status()
+ data = resp.content
+ break
+ except Exception as e:
+ has_more_candidates = idx + 1 < len(download_candidates)
+ should_fallback = (
+ download_source == "full_url"
+ and has_more_candidates
+ and self._is_retryable_media_download_error(e)
+ )
+ if should_fallback:
+ self.logger.warning(
+ "media download failed via full_url, falling back to encrypt_query_param: type={} err={}",
+ media_type,
+ e,
+ )
+ continue
+ raise
+
+ if aes_key_b64 and data:
+ data = _decrypt_aes_ecb(data, aes_key_b64)
+
+ if not data:
+ return None
+
+ media_dir = get_media_dir("weixin")
+ ext = _ext_for_type(media_type)
+ if not filename:
+ ts = int(time.time())
+ hash_seed = encrypt_query_param or full_url
+ h = abs(hash(hash_seed)) % 100000
+ filename = f"{media_type}_{ts}_{h}{ext}"
+ safe_name = os.path.basename(filename)
+ file_path = media_dir / safe_name
+ file_path.write_bytes(data)
+ return str(file_path)
+
+ except Exception:
+ self.logger.exception("Error downloading media")
+ return None
+
+ # ------------------------------------------------------------------
+ # Outbound (matches send.ts buildTextMessageReq + sendMessageWeixin)
+ # ------------------------------------------------------------------
+
+ async def _get_typing_ticket(self, user_id: str, context_token: str = "") -> str:
+ """Get typing ticket with per-user refresh + failure backoff cache."""
+ now = time.time()
+ entry = self._typing_tickets.get(user_id)
+ if entry and now < float(entry.get("next_fetch_at", 0)):
+ return str(entry.get("ticket", "") or "")
+
+ body: dict[str, Any] = {
+ "ilink_user_id": user_id,
+ "context_token": context_token or None,
+ "base_info": BASE_INFO,
+ }
+ data = await self._api_post("ilink/bot/getconfig", body)
+ if data.get("ret", 0) == 0:
+ ticket = str(data.get("typing_ticket", "") or "")
+ self._typing_tickets[user_id] = {
+ "ticket": ticket,
+ "ever_succeeded": True,
+ "next_fetch_at": now + (random.random() * TYPING_TICKET_TTL_S),
+ "retry_delay_s": CONFIG_CACHE_INITIAL_RETRY_S,
+ }
+ return ticket
+
+ prev_delay = float(entry.get("retry_delay_s", CONFIG_CACHE_INITIAL_RETRY_S)) if entry else CONFIG_CACHE_INITIAL_RETRY_S
+ next_delay = min(prev_delay * 2, CONFIG_CACHE_MAX_RETRY_S)
+ if entry:
+ entry["next_fetch_at"] = now + next_delay
+ entry["retry_delay_s"] = next_delay
+ return str(entry.get("ticket", "") or "")
+
+ self._typing_tickets[user_id] = {
+ "ticket": "",
+ "ever_succeeded": False,
+ "next_fetch_at": now + CONFIG_CACHE_INITIAL_RETRY_S,
+ "retry_delay_s": CONFIG_CACHE_INITIAL_RETRY_S,
+ }
+ return ""
+
+ async def _refresh_context_token_if_stale(
+ self, chat_id: str, context_token: str
+ ) -> str:
+ """Return a fresh context_token if the cached one is too old.
+
+ iLink context_token expires server-side after a short idle period
+ (empirically ~90s). Proactively refreshing before sending prevents
+ silent message loss on long agent turns or cron pushes.
+ """
+ if not context_token:
+ return context_token
+
+ now = time.time()
+ cached_at = self._context_token_at.get(chat_id, 0)
+ age = now - cached_at
+
+ if age < CONTEXT_TOKEN_MAX_AGE_S:
+ return context_token
+
+ self.logger.debug(
+ "WeChat context_token for {} is {:.0f}s old; refreshing via getconfig",
+ chat_id,
+ age,
+ )
+
+ body: dict[str, Any] = {
+ "ilink_user_id": chat_id,
+ "context_token": context_token,
+ "base_info": BASE_INFO,
+ }
+ try:
+ data = await self._api_post("ilink/bot/getconfig", body)
+ except Exception as e:
+ self.logger.warning("WeChat getconfig failed for {}: {}", chat_id, e)
+ return context_token
+
+ if data.get("ret", 0) != 0:
+ self.logger.warning(
+ "WeChat getconfig returned ret={} for {}: {}",
+ data.get("ret"),
+ chat_id,
+ data.get("errmsg", ""),
+ )
+ return context_token
+
+ new_token = str(data.get("context_token", "") or "")
+ if new_token and new_token != context_token:
+ self.logger.info(
+ "WeChat context_token refreshed for {} (age {:.0f}s -> fresh)",
+ chat_id,
+ age,
+ )
+ self._context_tokens[chat_id] = new_token
+ self._context_token_at[chat_id] = now
+ self._save_state()
+ return new_token
+
+ return context_token
+
+ async def _flush_tool_hints(self, chat_id: str) -> None:
+ """Send any buffered tool hints for *chat_id* as a single message.
+
+ Tool hints are coalesced to reduce message count and avoid hitting the
+ WeChat iLink rate limit (~7 msgs / 5 min). Failures are logged but
+ not raised so that the main message send is never blocked.
+ """
+ hints = self._pending_tool_hints.pop(chat_id, None)
+ if not hints:
+ return
+
+ self.logger.info(
+ "Flushing {} buffered tool hint(s) for {}",
+ len(hints),
+ chat_id,
+ )
+
+ ctx_token = self._context_tokens.get(chat_id, "")
+ ctx_token = await self._refresh_context_token_if_stale(chat_id, ctx_token)
+ if not ctx_token:
+ self.logger.warning(
+ "Dropped {} buffered tool hint(s) for {}: no context_token",
+ len(hints),
+ chat_id,
+ )
+ return
+
+ try:
+ await self._send_text(chat_id, "\n\n".join(hints), ctx_token)
+ except Exception:
+ self.logger.exception(
+ "Failed to flush buffered tool hints for {}", chat_id
+ )
+
+ async def _send_typing(self, user_id: str, typing_ticket: str, status: int) -> None:
+ """Best-effort sendtyping wrapper."""
+ if not typing_ticket:
+ return
+ body: dict[str, Any] = {
+ "ilink_user_id": user_id,
+ "typing_ticket": typing_ticket,
+ "status": status,
+ "base_info": BASE_INFO,
+ }
+ await self._api_post("ilink/bot/sendtyping", body)
+
+ async def _typing_keepalive_loop(self, user_id: str, typing_ticket: str, stop_event: asyncio.Event) -> None:
+ try:
+ while not stop_event.is_set():
+ await asyncio.sleep(TYPING_KEEPALIVE_INTERVAL_S)
+ if stop_event.is_set():
+ break
+ with suppress(Exception):
+ await self._send_typing(user_id, typing_ticket, TYPING_STATUS_TYPING)
+ finally:
+ pass
+
+ async def send(self, msg: OutboundMessage) -> None:
+ if not self._client or not self._token:
+ raise RuntimeError("WeChat client not initialized or not authenticated")
+ self._assert_session_active()
+
+ is_progress = bool((msg.metadata or {}).get("_progress", False))
+
+ # Buffer tool hints to coalesce consecutive ones and avoid burning
+ # WeChat iLink rate-limit quota (~7 msgs / 5 min).
+ if is_progress and (msg.metadata or {}).get("_tool_hint"):
+ if not self.send_tool_hints:
+ return
+ self._pending_tool_hints.setdefault(msg.chat_id, []).append(msg.content)
+ self.logger.debug(
+ "Buffered tool hint for {} (count={})",
+ msg.chat_id,
+ len(self._pending_tool_hints[msg.chat_id]),
+ )
+ return
+
+ # Reasoning deltas are invisible in WeChat (there is no reasoning
+ # UI). Skip them entirely — do not send and do not flush buffer.
+ if is_progress and (msg.metadata or {}).get("_reasoning_delta"):
+ self.logger.debug(
+ "Dropped invisible reasoning delta for {}", msg.chat_id
+ )
+ return
+
+ content = msg.content.strip()
+
+ # Empty progress messages (e.g. after_iteration tool_events) must
+ # NOT act as separators — they have no visible content.
+ if is_progress and not content and not (msg.media or []):
+ self.logger.debug(
+ "Skipped empty progress message for {} (no visible content)",
+ msg.chat_id,
+ )
+ return
+
+ # Flush buffered hints before sending any visible message.
+ await self._flush_tool_hints(msg.chat_id)
+
+ if not is_progress:
+ await self._stop_typing(msg.chat_id, clear_remote=True)
+
+ ctx_token = self._context_tokens.get(msg.chat_id, "")
+ ctx_token = await self._refresh_context_token_if_stale(msg.chat_id, ctx_token)
+ if not ctx_token:
+ raise RuntimeError(
+ f"WeChat context_token missing for chat_id={msg.chat_id}, cannot send"
+ )
+
+ typing_ticket = ""
+ with suppress(Exception):
+ typing_ticket = await self._get_typing_ticket(msg.chat_id, ctx_token)
+
+ if typing_ticket:
+ with suppress(Exception):
+ await self._send_typing(msg.chat_id, typing_ticket, TYPING_STATUS_TYPING)
+
+ typing_keepalive_stop = asyncio.Event()
+ typing_keepalive_task: asyncio.Task | None = None
+ if typing_ticket:
+ typing_keepalive_task = asyncio.create_task(
+ self._typing_keepalive_loop(msg.chat_id, typing_ticket, typing_keepalive_stop)
+ )
+
+ try:
+ # --- Send media files first (following Telegram channel pattern) ---
+ for media_path in (msg.media or []):
+ try:
+ await self._send_media_file(msg.chat_id, media_path, ctx_token)
+ except (httpx.TimeoutException, httpx.TransportError):
+ # Network/transport errors: do NOT fall back to text —
+ # the text send would also likely fail, and the outer
+ # except will re-raise so ChannelManager retries properly.
+ self.logger.opt(exception=True).warning(
+ "Network error sending media {}",
+ media_path,
+ )
+ raise
+ except httpx.HTTPStatusError as http_err:
+ status_code = (
+ http_err.response.status_code
+ if http_err.response is not None
+ else 0
+ )
+ if status_code >= 500:
+ # Server-side / retryable HTTP error — same as network.
+ self.logger.exception(
+ "Server error ({} {}) sending media {}",
+ status_code,
+ http_err.response.reason_phrase
+ if http_err.response is not None
+ else "",
+ media_path,
+ )
+ raise
+ # 4xx client errors are NOT retryable — fall back to text.
+ filename = Path(media_path).name
+ self.logger.exception("Failed to send media {}", media_path)
+ await self._send_text(
+ msg.chat_id, f"[Failed to send: {filename}]", ctx_token,
+ )
+ except Exception:
+ # Non-network errors (format, file-not-found, etc.):
+ # notify the user via text fallback.
+ filename = Path(media_path).name
+ self.logger.exception("Failed to send media {}", media_path)
+ # Notify user about failure via text
+ await self._send_text(
+ msg.chat_id, f"[Failed to send: {filename}]", ctx_token,
+ )
+
+ # --- Send text content ---
+ if not content:
+ return
+
+ chunks = split_message(content, WEIXIN_MAX_MESSAGE_LEN)
+ for chunk in chunks:
+ await self._send_text(msg.chat_id, chunk, ctx_token)
+ except Exception:
+ self.logger.exception("Error sending message")
+ raise
+ finally:
+ if typing_keepalive_task:
+ typing_keepalive_stop.set()
+ typing_keepalive_task.cancel()
+ with suppress(asyncio.CancelledError):
+ await typing_keepalive_task
+
+ if typing_ticket and not is_progress:
+ with suppress(Exception):
+ await self._send_typing(msg.chat_id, typing_ticket, TYPING_STATUS_CANCEL)
+
+ async def send_delta(
+ self, chat_id: str, delta: str, metadata: dict[str, Any] | None = None
+ ) -> None:
+ """Weixin iLink does not support native streaming deltas.
+
+ We only hook ``_stream_end`` so buffered tool hints are flushed even
+ when the final answer carries the ``_streamed`` flag and bypasses
+ :meth:`send`.
+ """
+ if metadata and metadata.get("_stream_end"):
+ await self._flush_tool_hints(chat_id)
+
+ async def _start_typing(self, chat_id: str, context_token: str = "") -> None:
+ """Start typing indicator immediately when a message is received."""
+ if not self._client or not self._token or not chat_id:
+ return
+ await self._stop_typing(chat_id, clear_remote=False)
+ try:
+ ticket = await self._get_typing_ticket(chat_id, context_token)
+ if not ticket:
+ return
+ await self._send_typing(chat_id, ticket, TYPING_STATUS_TYPING)
+ except Exception as e:
+ self.logger.debug("typing indicator start failed for {}: {}", chat_id, e)
+ return
+
+ stop_event = asyncio.Event()
+
+ async def keepalive() -> None:
+ try:
+ while not stop_event.is_set():
+ await asyncio.sleep(TYPING_KEEPALIVE_INTERVAL_S)
+ if stop_event.is_set():
+ break
+ with suppress(Exception):
+ await self._send_typing(chat_id, ticket, TYPING_STATUS_TYPING)
+ finally:
+ pass
+
+ task = asyncio.create_task(keepalive())
+ task._typing_stop_event = stop_event # type: ignore[attr-defined]
+ self._typing_tasks[chat_id] = task
+
+ async def _stop_typing(self, chat_id: str, *, clear_remote: bool) -> None:
+ """Stop typing indicator for a chat."""
+ task = self._typing_tasks.pop(chat_id, None)
+ if task and not task.done():
+ stop_event = getattr(task, "_typing_stop_event", None)
+ if stop_event:
+ stop_event.set()
+ task.cancel()
+ with suppress(asyncio.CancelledError):
+ await task
+ if not clear_remote:
+ return
+ entry = self._typing_tickets.get(chat_id)
+ ticket = str(entry.get("ticket", "") or "") if isinstance(entry, dict) else ""
+ if not ticket:
+ return
+ try:
+ await self._send_typing(chat_id, ticket, TYPING_STATUS_CANCEL)
+ except Exception as e:
+ self.logger.debug("typing clear failed for {}: {}", chat_id, e)
+
+ async def _send_text(
+ self,
+ to_user_id: str,
+ text: str,
+ context_token: str,
+ ) -> None:
+ """Send a text message matching the exact protocol from send.ts."""
+ client_id = f"vibe-trading-{uuid.uuid4().hex[:12]}"
+
+ item_list: list[dict] = []
+ if text:
+ item_list.append({"type": ITEM_TEXT, "text_item": {"text": text}})
+
+ weixin_msg: dict[str, Any] = {
+ "from_user_id": "",
+ "to_user_id": to_user_id,
+ "client_id": client_id,
+ "message_type": MESSAGE_TYPE_BOT,
+ "message_state": MESSAGE_STATE_FINISH,
+ }
+ if item_list:
+ weixin_msg["item_list"] = item_list
+ if context_token:
+ weixin_msg["context_token"] = context_token
+
+ body: dict[str, Any] = {
+ "msg": weixin_msg,
+ "base_info": BASE_INFO,
+ }
+
+ data = await self._api_post("ilink/bot/sendmessage", body)
+ ret = data.get("ret", 0)
+ errcode = data.get("errcode", 0)
+ if (ret is not None and ret != 0) or (errcode is not None and errcode != 0):
+ raise RuntimeError(
+ f"WeChat send text error (ret={ret}, errcode={errcode}): {data.get('errmsg', '')}"
+ )
+
+ async def _send_media_file(
+ self,
+ to_user_id: str,
+ media_path: str,
+ context_token: str,
+ ) -> None:
+ """Upload a local file to WeChat CDN and send it as a media message.
+
+ Follows the exact protocol from ``@tencent-weixin/openclaw-weixin`` v1.0.3:
+ 1. Generate a random 16-byte AES key (client-side).
+ 2. Call ``getuploadurl`` with file metadata + hex-encoded AES key.
+ 3. AES-128-ECB encrypt the file and POST to CDN (``{cdnBaseUrl}/upload``).
+ 4. Read ``x-encrypted-param`` header from CDN response as the download param.
+ 5. Send a ``sendmessage`` with the appropriate media item referencing the upload.
+ """
+ p = Path(media_path)
+ if not p.is_file():
+ raise FileNotFoundError(f"Media file not found: {media_path}")
+
+ raw_data = p.read_bytes()
+ raw_size = len(raw_data)
+ raw_md5 = hashlib.md5(raw_data).hexdigest()
+
+ # Determine upload media type from extension
+ ext = p.suffix.lower()
+ if ext in _IMAGE_EXTS:
+ upload_type = UPLOAD_MEDIA_IMAGE
+ item_type = ITEM_IMAGE
+ item_key = "image_item"
+ elif ext in _VIDEO_EXTS:
+ upload_type = UPLOAD_MEDIA_VIDEO
+ item_type = ITEM_VIDEO
+ item_key = "video_item"
+ elif ext in _VOICE_EXTS:
+ upload_type = UPLOAD_MEDIA_VOICE
+ item_type = ITEM_VOICE
+ item_key = "voice_item"
+ else:
+ upload_type = UPLOAD_MEDIA_FILE
+ item_type = ITEM_FILE
+ item_key = "file_item"
+
+ # Generate client-side AES-128 key (16 random bytes)
+ aes_key_raw = os.urandom(16)
+ aes_key_hex = aes_key_raw.hex()
+
+ # Compute encrypted size: PKCS7 padding to 16-byte boundary
+ # Matches aesEcbPaddedSize: Math.ceil((size + 1) / 16) * 16
+ padded_size = ((raw_size + 1 + 15) // 16) * 16
+
+ # Step 1: Get upload URL from server (prefer upload_full_url, fallback to upload_param)
+ file_key = os.urandom(16).hex()
+ upload_body: dict[str, Any] = {
+ "filekey": file_key,
+ "media_type": upload_type,
+ "to_user_id": to_user_id,
+ "rawsize": raw_size,
+ "rawfilemd5": raw_md5,
+ "filesize": padded_size,
+ "no_need_thumb": True,
+ "aeskey": aes_key_hex,
+ }
+
+ assert self._client is not None
+ upload_resp = await self._api_post("ilink/bot/getuploadurl", upload_body)
+
+ upload_full_url = str(upload_resp.get("upload_full_url", "") or "").strip()
+ upload_param = str(upload_resp.get("upload_param", "") or "")
+ if not upload_full_url and not upload_param:
+ raise RuntimeError(
+ "getuploadurl returned no upload URL "
+ f"(need upload_full_url or upload_param): {upload_resp}"
+ )
+
+ # Step 2: AES-128-ECB encrypt and POST to CDN
+ aes_key_b64 = base64.b64encode(aes_key_raw).decode()
+ encrypted_data = _encrypt_aes_ecb(raw_data, aes_key_b64)
+
+ if upload_full_url:
+ cdn_upload_url = upload_full_url
+ else:
+ cdn_upload_url = (
+ f"{self.config.cdn_base_url}/upload"
+ f"?encrypted_query_param={quote(upload_param)}"
+ f"&filekey={quote(file_key)}"
+ )
+
+ cdn_resp = await self._client.post(
+ cdn_upload_url,
+ content=encrypted_data,
+ headers={"Content-Type": "application/octet-stream"},
+ )
+ cdn_resp.raise_for_status()
+
+ # The download encrypted_query_param comes from CDN response header
+ download_param = cdn_resp.headers.get("x-encrypted-param", "")
+ if not download_param:
+ raise RuntimeError(
+ "CDN upload response missing x-encrypted-param header; "
+ f"status={cdn_resp.status_code} headers={dict(cdn_resp.headers)}"
+ )
+
+ # Step 3: Send message with the media item
+ # aes_key for CDNMedia is the hex key encoded as base64
+ # (matches: Buffer.from(uploaded.aeskey).toString("base64"))
+ cdn_aes_key_b64 = base64.b64encode(aes_key_hex.encode()).decode()
+
+ media_item: dict[str, Any] = {
+ "media": {
+ "encrypt_query_param": download_param,
+ "aes_key": cdn_aes_key_b64,
+ "encrypt_type": 1,
+ },
+ }
+
+ if item_type == ITEM_IMAGE:
+ media_item["mid_size"] = padded_size
+ elif item_type == ITEM_VIDEO:
+ media_item["video_size"] = padded_size
+ elif item_type == ITEM_FILE:
+ media_item["file_name"] = p.name
+ media_item["len"] = str(raw_size)
+
+ # Send each media item as its own message (matching reference plugin)
+ client_id = f"vibe-trading-{uuid.uuid4().hex[:12]}"
+ item_list: list[dict] = [{"type": item_type, item_key: media_item}]
+
+ weixin_msg: dict[str, Any] = {
+ "from_user_id": "",
+ "to_user_id": to_user_id,
+ "client_id": client_id,
+ "message_type": MESSAGE_TYPE_BOT,
+ "message_state": MESSAGE_STATE_FINISH,
+ "item_list": item_list,
+ }
+ if context_token:
+ weixin_msg["context_token"] = context_token
+
+ body: dict[str, Any] = {
+ "msg": weixin_msg,
+ "base_info": BASE_INFO,
+ }
+
+ data = await self._api_post("ilink/bot/sendmessage", body)
+ ret = data.get("ret", 0)
+ errcode = data.get("errcode", 0)
+ if (ret is not None and ret != 0) or (errcode is not None and errcode != 0):
+ raise RuntimeError(
+ f"WeChat send media error (ret={ret}, errcode={errcode}): {data.get('errmsg', '')}"
+ )
+
+
+# ---------------------------------------------------------------------------
+# AES-128-ECB encryption / decryption (matches pic-decrypt.ts / aes-ecb.ts)
+# ---------------------------------------------------------------------------
+
+
+def _parse_aes_key(aes_key_b64: str) -> bytes:
+ """Parse a base64-encoded AES key, handling both encodings seen in the wild.
+
+ From ``pic-decrypt.ts parseAesKey``:
+
+ * ``base64(raw 16 bytes)`` → images (media.aes_key)
+ * ``base64(hex string of 16 bytes)`` → file / voice / video
+
+ In the second case base64-decoding yields 32 ASCII hex chars which must
+ then be parsed as hex to recover the actual 16-byte key.
+ """
+ decoded = base64.b64decode(aes_key_b64)
+ if len(decoded) == 16:
+ return decoded
+ if len(decoded) == 32 and re.fullmatch(rb"[0-9a-fA-F]{32}", decoded):
+ # hex-encoded key: base64 → hex string → raw bytes
+ return bytes.fromhex(decoded.decode("ascii"))
+ raise ValueError(
+ f"aes_key must decode to 16 raw bytes or 32-char hex string, got {len(decoded)} bytes"
+ )
+
+
+def _encrypt_aes_ecb(data: bytes, aes_key_b64: str) -> bytes:
+ """Encrypt data with AES-128-ECB and PKCS7 padding for CDN upload."""
+ try:
+ key = _parse_aes_key(aes_key_b64)
+ except Exception as e:
+ logger.warning("Failed to parse AES key for encryption, sending raw: {}", e)
+ return data
+
+ # PKCS7 padding
+ pad_len = 16 - len(data) % 16
+ padded = data + bytes([pad_len] * pad_len)
+
+ with suppress(ImportError):
+ from Crypto.Cipher import AES
+
+ cipher = AES.new(key, AES.MODE_ECB)
+ return cipher.encrypt(padded)
+
+ try:
+ from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+
+ cipher_obj = Cipher(algorithms.AES(key), modes.ECB())
+ encryptor = cipher_obj.encryptor()
+ return encryptor.update(padded) + encryptor.finalize()
+ except ImportError:
+ logger.warning("Cannot encrypt media: install 'pycryptodome' or 'cryptography'")
+ return data
+
+
+def _decrypt_aes_ecb(data: bytes, aes_key_b64: str) -> bytes:
+ """Decrypt AES-128-ECB media data.
+
+ ``aes_key_b64`` is always base64-encoded (caller converts hex keys first).
+ """
+ try:
+ key = _parse_aes_key(aes_key_b64)
+ except Exception as e:
+ logger.warning("Failed to parse AES key, returning raw data: {}", e)
+ return data
+
+ decrypted: bytes | None = None
+
+ with suppress(ImportError):
+ from Crypto.Cipher import AES
+
+ cipher = AES.new(key, AES.MODE_ECB)
+ decrypted = cipher.decrypt(data)
+
+ if decrypted is None:
+ try:
+ from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
+
+ cipher_obj = Cipher(algorithms.AES(key), modes.ECB())
+ decryptor = cipher_obj.decryptor()
+ decrypted = decryptor.update(data) + decryptor.finalize()
+ except ImportError:
+ logger.warning("Cannot decrypt media: install 'pycryptodome' or 'cryptography'")
+ return data
+
+ return _pkcs7_unpad_safe(decrypted)
+
+
+def _pkcs7_unpad_safe(data: bytes, block_size: int = 16) -> bytes:
+ """Safely remove PKCS7 padding when valid; otherwise return original bytes."""
+ if not data:
+ return data
+ if len(data) % block_size != 0:
+ return data
+ pad_len = data[-1]
+ if pad_len < 1 or pad_len > block_size:
+ return data
+ if data[-pad_len:] != bytes([pad_len]) * pad_len:
+ return data
+ return data[:-pad_len]
+
+
+def _ext_for_type(media_type: str) -> str:
+ return {
+ "image": ".jpg",
+ "voice": ".silk",
+ "video": ".mp4",
+ "file": "",
+ }.get(media_type, "")
diff --git a/agent/src/channels/whatsapp.py b/agent/src/channels/whatsapp.py
new file mode 100644
index 000000000..d3abc06db
--- /dev/null
+++ b/agent/src/channels/whatsapp.py
@@ -0,0 +1,682 @@
+"""WhatsApp channel implementation using neonize."""
+
+from __future__ import annotations
+
+import asyncio
+import mimetypes
+import re
+import secrets
+import time
+from collections import OrderedDict
+from contextlib import suppress
+from pathlib import Path
+from typing import Any, Literal, NamedTuple
+
+from pydantic import Field
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.base import BaseChannel
+from src.channels.utils import get_media_dir, get_runtime_subdir
+from pydantic import BaseModel
+
+
+class WhatsAppConfig(BaseModel):
+ """WhatsApp channel configuration."""
+
+ enabled: bool = False
+ allow_from: list[str] = Field(default_factory=list)
+ group_policy: Literal["open", "mention"] = "open"
+ database_path: str = ""
+ lid_mappings: dict[str, str] = Field(default_factory=dict)
+
+
+class _NeonizeAPI(NamedTuple):
+ NewAClient: Any
+ ConnectedEv: Any
+ DisconnectedEv: Any
+ MessageEv: Any
+ PairStatusEv: Any
+ build_jid: Any
+
+
+class _MediaInfo(NamedTuple):
+ kind: str
+ message: Any
+ mimetype: str
+ filename: str
+ is_voice: bool = False
+
+
+_NEONIZE_API: _NeonizeAPI | None = None
+_JID_RE = re.compile(r"^(?P<user>[^@]+)@(?P<server>[^@]+)$")
+_LEGACY_BRIDGE_CONFIG_FIELDS = ("bridgeUrl", "bridgeToken", "bridge_url", "bridge_token")
+
+
+def _default_database_path() -> Path:
+ return get_runtime_subdir("whatsapp-auth") / "neonize.db"
+
+
+def _legacy_bridge_config_fields(config: dict[str, Any]) -> list[str]:
+ return [field for field in _LEGACY_BRIDGE_CONFIG_FIELDS if field in config]
+
+
+def _load_neonize() -> _NeonizeAPI:
+ global _NEONIZE_API
+ if _NEONIZE_API is not None:
+ return _NEONIZE_API
+
+ try:
+ from neonize.aioze.client import NewAClient
+ from neonize.aioze.events import ConnectedEv, DisconnectedEv, MessageEv, PairStatusEv
+ from neonize.utils.jid import build_jid
+ except ImportError as exc:
+ raise RuntimeError(
+ 'WhatsApp dependencies not installed. Run: pip install "vibe-trading-ai[whatsapp]"'
+ ) from exc
+
+ _NEONIZE_API = _NeonizeAPI(
+ NewAClient=NewAClient,
+ ConnectedEv=ConnectedEv,
+ DisconnectedEv=DisconnectedEv,
+ MessageEv=MessageEv,
+ PairStatusEv=PairStatusEv,
+ build_jid=build_jid,
+ )
+ return _NEONIZE_API
+
+
+def _has_field(message: Any, name: str) -> bool:
+ if message is None:
+ return False
+
+ has_field = getattr(message, "HasField", None)
+ if callable(has_field):
+ try:
+ return bool(has_field(name))
+ except ValueError:
+ pass
+
+ list_fields = getattr(message, "ListFields", None)
+ if callable(list_fields):
+ try:
+ return any(getattr(field, "name", "") == name for field, _ in list_fields())
+ except Exception:
+ pass
+
+ value = getattr(message, name, None)
+ return value is not None and value != "" and value != b""
+
+
+def _message_field(message: Any, *names: str) -> Any:
+ for name in names:
+ if _has_field(message, name):
+ return getattr(message, name)
+ return None
+
+
+def _safe_attr(obj: Any, name: str, default: Any = None) -> Any:
+ if obj is None:
+ return default
+ return getattr(obj, name, default)
+
+
+def _jid_to_string(jid: Any) -> str:
+ if jid is None:
+ return ""
+ if isinstance(jid, str):
+ return jid.strip()
+ if bool(_safe_attr(jid, "IsEmpty", False)):
+ return ""
+
+ user = str(_safe_attr(jid, "User", "") or "").strip()
+ server = str(_safe_attr(jid, "Server", "") or "").strip()
+ if user and server:
+ return f"{user}@{server}"
+ return server or user
+
+
+def _normalize_jid(raw: Any) -> str:
+ jid = _jid_to_string(raw).strip()
+ if not jid:
+ return ""
+ if jid.endswith("@lid.whatsapp.net"):
+ return jid[: -len(".whatsapp.net")]
+ return jid
+
+
+def _bare_jid(raw: Any) -> str:
+ jid = _normalize_jid(raw)
+ if "@" not in jid:
+ return jid
+ return jid.split("@", 1)[0].split(":", 1)[0]
+
+
+def _classify_sender_ids(jids: list[Any]) -> tuple[str, str]:
+ phone_id = ""
+ lid_id = ""
+
+ for raw in jids:
+ jid = _normalize_jid(raw)
+ if not jid:
+ continue
+ match = _JID_RE.match(jid)
+ if match:
+ user = match.group("user").split(":", 1)[0]
+ server = match.group("server")
+ if server in {"s.whatsapp.net", "c.us"}:
+ phone_id = phone_id or user
+ elif server in {"lid", "lid.whatsapp.net"}:
+ lid_id = lid_id or user
+ continue
+
+ if not phone_id:
+ phone_id = jid
+
+ return phone_id, lid_id
+
+
+def _context_infos(message: Any) -> list[Any]:
+ infos: list[Any] = []
+ for container in (
+ message,
+ _message_field(message, "extendedTextMessage"),
+ _message_field(message, "imageMessage"),
+ _message_field(message, "videoMessage"),
+ _message_field(message, "audioMessage"),
+ _message_field(message, "documentMessage"),
+ _message_field(message, "stickerMessage"),
+ ):
+ context = _message_field(container, "contextInfo")
+ if context is not None:
+ infos.append(context)
+ return infos
+
+
+def _message_text(message: Any) -> str:
+ conversation = str(_safe_attr(message, "conversation", "") or "").strip()
+ if conversation:
+ return conversation
+
+ extended = _message_field(message, "extendedTextMessage")
+ text = str(_safe_attr(extended, "text", "") or "").strip()
+ if text:
+ return text
+
+ for field_name in ("imageMessage", "videoMessage", "documentMessage", "stickerMessage"):
+ media_message = _message_field(message, field_name)
+ caption = str(_safe_attr(media_message, "caption", "") or "").strip()
+ if caption:
+ return caption
+
+ return ""
+
+
+def _media_message(message: Any) -> _MediaInfo | None:
+ image = _message_field(message, "imageMessage")
+ if image is not None:
+ return _MediaInfo(
+ kind="image",
+ message=image,
+ mimetype=str(_safe_attr(image, "mimetype", "") or "image/jpeg"),
+ filename=str(_safe_attr(image, "fileName", "") or ""),
+ )
+
+ video = _message_field(message, "videoMessage")
+ if video is not None:
+ return _MediaInfo(
+ kind="video",
+ message=video,
+ mimetype=str(_safe_attr(video, "mimetype", "") or "video/mp4"),
+ filename=str(_safe_attr(video, "fileName", "") or ""),
+ )
+
+ audio = _message_field(message, "audioMessage")
+ if audio is not None:
+ return _MediaInfo(
+ kind="audio",
+ message=audio,
+ mimetype=str(_safe_attr(audio, "mimetype", "") or "audio/ogg"),
+ filename=str(_safe_attr(audio, "fileName", "") or ""),
+ is_voice=bool(_safe_attr(audio, "PTT", False) or _safe_attr(audio, "ptt", False)),
+ )
+
+ document = _message_field(message, "documentMessage")
+ if document is not None:
+ return _MediaInfo(
+ kind="file",
+ message=document,
+ mimetype=str(_safe_attr(document, "mimetype", "") or "application/octet-stream"),
+ filename=str(
+ _safe_attr(document, "fileName", "")
+ or _safe_attr(document, "title", "")
+ or ""
+ ),
+ )
+
+ sticker = _message_field(message, "stickerMessage")
+ if sticker is not None:
+ return _MediaInfo(
+ kind="sticker",
+ message=sticker,
+ mimetype=str(_safe_attr(sticker, "mimetype", "") or "image/webp"),
+ filename=str(_safe_attr(sticker, "fileName", "") or ""),
+ )
+
+ return None
+
+
+class WhatsAppChannel(BaseChannel):
+ """WhatsApp channel using neonize's async WhatsApp client."""
+
+ name = "whatsapp"
+ display_name = "WhatsApp"
+
+ @classmethod
+ def default_config(cls) -> dict[str, Any]:
+ return WhatsAppConfig().model_dump(by_alias=True)
+
+ def __init__(self, config: Any, bus: MessageBus):
+ legacy_bridge_fields = _legacy_bridge_config_fields(config) if isinstance(config, dict) else []
+ if isinstance(config, dict):
+ config = WhatsAppConfig.model_validate(config)
+ super().__init__(config, bus)
+ if legacy_bridge_fields:
+ self.logger.warning(
+ "Ignoring deprecated WhatsApp bridge config fields: {}. "
+ "Run 'vibe-trading channels login whatsapp' to create a neonize session.",
+ ", ".join(legacy_bridge_fields),
+ )
+ self._client: Any | None = None
+ self._connected = False
+ self._processed_message_ids: OrderedDict[str, None] = OrderedDict()
+ self._lid_to_phone = self._load_lid_mappings()
+ self._self_jids: set[str] = set()
+ self._started_at = 0.0
+
+ def _database_path(self) -> Path:
+ configured = self.config.database_path.strip()
+ return Path(configured).expanduser() if configured else _default_database_path()
+
+ def _load_lid_mappings(self) -> dict[str, str]:
+ mapping: dict[str, str] = {}
+ for lid, phone in self.config.lid_mappings.items():
+ phone_text = str(phone).strip()
+ if phone_text:
+ mapping[str(lid).strip()] = phone_text
+ return mapping
+
+ def _new_client(self) -> Any:
+ api = _load_neonize()
+ db_path = self._database_path()
+ db_path.parent.mkdir(parents=True, exist_ok=True)
+ return api.NewAClient(str(db_path))
+
+ async def login(self, force: bool = False) -> bool:
+ db_path = self._database_path()
+ if force:
+ self._reset_database(db_path)
+
+ client = self._new_client()
+ login_result = asyncio.get_running_loop().create_future()
+ self._register_handlers(client, login_result=login_result, handle_messages=False)
+
+ try:
+ self.logger.info("Starting WhatsApp login with neonize...")
+ connect_task = await client.connect()
+ self._fail_login_on_connect_task_done(connect_task, login_result)
+ await login_result
+ self.logger.info("WhatsApp login complete")
+ return True
+ except Exception as exc:
+ self.logger.error("WhatsApp login failed: {}", exc)
+ return False
+ finally:
+ with suppress(Exception):
+ await client.stop()
+
+ async def start(self) -> None:
+ self._running = True
+ self._started_at = time.time()
+ client = self._new_client()
+ self._client = client
+ self._register_handlers(client, handle_messages=True)
+
+ try:
+ self.logger.info("Connecting WhatsApp channel with neonize...")
+ await client.connect()
+ await client.idle()
+ except asyncio.CancelledError:
+ raise
+ finally:
+ self._running = False
+ self._connected = False
+ if self._client is client:
+ self._client = None
+ with suppress(Exception):
+ await client.stop()
+
+ async def stop(self) -> None:
+ self._running = False
+ self._connected = False
+ client = self._client
+ self._client = None
+ if client is not None:
+ await client.stop()
+
+ @staticmethod
+ def _fail_login_on_connect_task_done(
+ connect_task: asyncio.Task[Any] | None,
+ login_result: asyncio.Future[None],
+ ) -> None:
+ if connect_task is None:
+ return
+
+ def _on_done(task: asyncio.Task[Any]) -> None:
+ try:
+ exc = task.exception()
+ except asyncio.CancelledError:
+ return
+ if login_result.done():
+ return
+ if exc is not None:
+ login_result.set_exception(exc)
+ else:
+ login_result.set_exception(
+ RuntimeError("WhatsApp connection ended before login completed")
+ )
+
+ connect_task.add_done_callback(_on_done)
+
+ async def send(self, msg: OutboundMessage) -> None:
+ client = self._client
+ if client is None or not self._connected:
+ raise RuntimeError("WhatsApp channel is not connected")
+
+ to = self._build_jid(msg.chat_id)
+ if msg.content:
+ await client.send_message(to, msg.content)
+
+ for media_path in msg.media or []:
+ await self._send_media(client, to, media_path)
+
+ def _build_jid(self, raw: str) -> Any:
+ api = _load_neonize()
+ target = raw.strip()
+ match = _JID_RE.match(_normalize_jid(target))
+ if not match:
+ return api.build_jid(target)
+
+ user = match.group("user").split(":", 1)[0]
+ server = match.group("server")
+ return api.build_jid(user, server)
+
+ async def _send_media(self, client: Any, to: Any, media_path: str) -> None:
+ path = str(Path(media_path).expanduser())
+ mime, _ = mimetypes.guess_type(path)
+ mimetype = mime or "application/octet-stream"
+ if mimetype.startswith("image/"):
+ await client.send_image(to, path)
+ elif mimetype.startswith("video/"):
+ await client.send_video(to, path)
+ elif mimetype.startswith("audio/"):
+ await client.send_audio(to, path)
+ else:
+ await client.send_document(
+ to,
+ path,
+ filename=Path(path).name,
+ mimetype=mimetype,
+ )
+
+ def _register_handlers(
+ self,
+ client: Any,
+ *,
+ login_result: asyncio.Future[None] | None = None,
+ handle_messages: bool,
+ ) -> None:
+ api = _load_neonize()
+
+ @client.qr
+ async def _on_qr(_: Any, qr_data: bytes) -> None:
+ import segno
+
+ self.logger.info("Scan the WhatsApp QR code with Linked Devices")
+ segno.make_qr(qr_data).terminal(compact=True)
+
+ @client.event(api.ConnectedEv)
+ async def _on_connected(current_client: Any, _: Any) -> None:
+ self._connected = True
+ try:
+ await self._remember_self_jids(current_client)
+ except Exception as exc:
+ if login_result is not None and not login_result.done():
+ login_result.set_exception(exc)
+ raise
+ if login_result is not None and not login_result.done():
+ login_result.set_result(None)
+ self.logger.info("WhatsApp connected")
+
+ @client.event(api.DisconnectedEv)
+ async def _on_disconnected(_: Any, event: Any) -> None:
+ self._connected = False
+ if login_result is not None and not login_result.done():
+ login_result.set_exception(
+ RuntimeError(f"WhatsApp disconnected before login completed: {event}")
+ )
+ self.logger.warning("WhatsApp disconnected: {}", event)
+
+ @client.event(api.PairStatusEv)
+ async def _on_pair_status(_: Any, event: Any) -> None:
+ error = str(_safe_attr(event, "Error", "") or "")
+ if error:
+ exc = RuntimeError(f"WhatsApp pair status error: {error}")
+ if login_result is not None and not login_result.done():
+ login_result.set_exception(exc)
+ raise exc
+ self.logger.info("WhatsApp pair status: {}", event)
+
+ if not handle_messages:
+ return
+
+ @client.event(api.MessageEv)
+ async def _on_message(current_client: Any, event: Any) -> None:
+ try:
+ await self._handle_neonize_message(current_client, event)
+ except Exception:
+ self.logger.exception("Error handling WhatsApp message")
+ raise
+
+ async def _remember_self_jids(self, client: Any) -> None:
+ device = _safe_attr(client, "me")
+ if device is None:
+ device = await client.get_me()
+
+ for attr in ("JID", "LID"):
+ jid = _normalize_jid(_safe_attr(device, attr))
+ if jid:
+ self._self_jids.add(jid)
+ self._self_jids.add(_bare_jid(jid))
+
+ async def _handle_neonize_message(self, client: Any, event: Any) -> None:
+ info = _safe_attr(event, "Info")
+ message = _safe_attr(event, "Message")
+ source = _safe_attr(info, "MessageSource")
+ if info is None or message is None or source is None:
+ raise ValueError("WhatsApp MessageEv is missing Info, Message, or MessageSource")
+
+ if bool(_safe_attr(source, "IsFromMe", False)):
+ return
+
+ chat_jid = _normalize_jid(_safe_attr(source, "Chat"))
+ if not chat_jid:
+ raise ValueError("WhatsApp message has no chat JID")
+ if chat_jid == "status@broadcast":
+ return
+
+ timestamp = float(_safe_attr(info, "Timestamp", 0) or 0)
+ if self._started_at and timestamp and timestamp < self._started_at:
+ return
+
+ is_group = bool(_safe_attr(source, "IsGroup", False))
+ if is_group and self.config.group_policy == "mention":
+ if not self._is_addressed_to_bot(message):
+ return
+
+ message_id = str(_safe_attr(info, "ID", "") or "")
+ if message_id:
+ if message_id in self._processed_message_ids:
+ return
+ self._processed_message_ids[message_id] = None
+ while len(self._processed_message_ids) > 1000:
+ self._processed_message_ids.popitem(last=False)
+
+ participant_jid = _normalize_jid(_safe_attr(source, "Sender"))
+ sender_alt_jid = _normalize_jid(_safe_attr(source, "SenderAlt"))
+ sender_candidates = [sender_alt_jid, participant_jid]
+ if not is_group:
+ sender_candidates.append(chat_jid)
+
+ phone_id, lid_id = _classify_sender_ids(sender_candidates)
+ if phone_id and lid_id:
+ self._lid_to_phone[lid_id] = phone_id
+
+ sender_id = phone_id or self._lid_to_phone.get(lid_id, "") or lid_id
+ if not sender_id:
+ raise ValueError("WhatsApp message has no resolvable sender ID")
+ metadata = {
+ "message_id": message_id or None,
+ "timestamp": int(timestamp) if timestamp else None,
+ "is_group": is_group,
+ "is_forwarded": self._is_forwarded(message),
+ "participant": participant_jid or None,
+ "sender_alt": sender_alt_jid or None,
+ "lid": lid_id or None,
+ "phone": phone_id or None,
+ "is_reply_to_bot": self._is_reply_to_bot(message),
+ }
+ if not self.is_allowed(sender_id):
+ self.logger.info(
+ "Passing unauthorized WhatsApp sender {} to pairing flow "
+ "(phone={}, lid={}, chat={})",
+ sender_id,
+ phone_id or "",
+ lid_id or "",
+ chat_jid,
+ )
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_jid,
+ content=_message_text(message),
+ media=[],
+ metadata=metadata,
+ is_dm=not is_group,
+ )
+ return
+
+ text = _message_text(message)
+ media_paths: list[str] = []
+ media = _media_message(message)
+ if media is not None:
+ path = await self._download_media(client, event, media)
+ if media.kind == "audio" and media.is_voice:
+ transcription = await self.transcribe_audio(path)
+ if transcription:
+ text = transcription
+ else:
+ media_paths.append(path)
+ text = self._append_media_tag(text, "audio", path)
+ else:
+ media_paths.append(path)
+ text = self._append_media_tag(text, media.kind, path)
+
+ if not text and not media_paths:
+ return
+
+ await self._handle_message(
+ sender_id=sender_id,
+ chat_id=chat_jid,
+ content=text,
+ media=media_paths,
+ metadata=metadata,
+ is_dm=not is_group,
+ )
+
+ def _is_addressed_to_bot(self, message: Any) -> bool:
+ return self._was_mentioned(message) or self._is_reply_to_bot(message)
+
+ def _was_mentioned(self, message: Any) -> bool:
+ if not self._self_jids:
+ return False
+ for context in _context_infos(message):
+ mentioned = (
+ _safe_attr(context, "mentionedJID")
+ or _safe_attr(context, "mentionedJid")
+ or _safe_attr(context, "mentioned_jid")
+ or []
+ )
+ for jid in mentioned:
+ normalized = _normalize_jid(jid)
+ if normalized in self._self_jids or _bare_jid(normalized) in self._self_jids:
+ return True
+ return False
+
+ def _is_reply_to_bot(self, message: Any) -> bool:
+ if not self._self_jids:
+ return False
+ for context in _context_infos(message):
+ participant = _normalize_jid(
+ _safe_attr(context, "participant")
+ or _safe_attr(context, "Participant")
+ or ""
+ )
+ if participant in self._self_jids or _bare_jid(participant) in self._self_jids:
+ return True
+ return False
+
+ @staticmethod
+ def _is_forwarded(message: Any) -> bool:
+ for context in _context_infos(message):
+ if bool(_safe_attr(context, "isForwarded", False)):
+ return True
+ if int(_safe_attr(context, "forwardingScore", 0) or 0) > 0:
+ return True
+ return False
+
+ async def _download_media(self, client: Any, event: Any, media: _MediaInfo) -> str:
+ info = _safe_attr(event, "Info")
+ message_id = str(_safe_attr(info, "ID", "") or "")
+ path = self._media_path(message_id, media)
+ await client.download_any(_safe_attr(event, "Message"), str(path))
+ return str(path)
+
+ def _media_path(self, message_id: str, media: _MediaInfo) -> Path:
+ media_dir = get_media_dir("whatsapp")
+ safe_id = re.sub(r"[^A-Za-z0-9_.-]+", "_", message_id or str(int(time.time())))
+ filename = Path(media.filename).name if media.filename else ""
+ suffix = Path(filename).suffix if filename else ""
+ if not suffix:
+ suffix = mimetypes.guess_extension(media.mimetype) or {
+ "image": ".jpg",
+ "video": ".mp4",
+ "audio": ".ogg",
+ "sticker": ".webp",
+ }.get(media.kind, ".bin")
+ return media_dir / f"wa_{safe_id}_{secrets.token_hex(4)}{suffix}"
+
+ @staticmethod
+ def _append_media_tag(text: str, kind: str, path: str) -> str:
+ label = kind if kind in {"image", "video", "audio", "sticker"} else "file"
+ tag = f"[{label}: {path}]"
+ return f"{text}\n{tag}" if text else tag
+
+ @staticmethod
+ def _reset_database(path: Path) -> None:
+ for candidate in (
+ path,
+ path.with_suffix(path.suffix + "-shm"),
+ path.with_suffix(path.suffix + "-wal"),
+ ):
+ if candidate.exists():
+ candidate.unlink()
diff --git a/agent/src/channelsui/__init__.py b/agent/src/channelsui/__init__.py
new file mode 100644
index 000000000..927bd47d1
--- /dev/null
+++ b/agent/src/channelsui/__init__.py
@@ -0,0 +1 @@
+"""WebUI compatibility helpers for channel adapters."""
diff --git a/agent/src/channelsui/cli_apps_api.py b/agent/src/channelsui/cli_apps_api.py
new file mode 100644
index 000000000..898b8b994
--- /dev/null
+++ b/agent/src/channelsui/cli_apps_api.py
@@ -0,0 +1,19 @@
+"""CLI app mention normalization for the WebSocket channel."""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+def normalize_cli_app_mentions(content: Any, metadata: dict[str, Any] | None = None) -> list[str]:
+ """Normalize CLI app mentions into a stable list of names."""
+ del metadata
+ if content is None:
+ return []
+ if isinstance(content, str):
+ raw_items = content.replace(",", " ").split()
+ elif isinstance(content, (list, tuple, set)):
+ raw_items = [str(item) for item in content]
+ else:
+ raw_items = [str(content)]
+ return [item.lstrip("@").strip() for item in raw_items if item and item.strip()]
diff --git a/agent/src/channelsui/forking.py b/agent/src/channelsui/forking.py
new file mode 100644
index 000000000..b58ce3cd7
--- /dev/null
+++ b/agent/src/channelsui/forking.py
@@ -0,0 +1,23 @@
+"""WebUI fork-chat compatibility for the WebSocket channel."""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+_HANDLE_WEBUI_FORK_CHAT = None
+
+
+async def handle_webui_fork_chat(*args: Any, **kwargs: Any) -> None:
+ """Return a structured unsupported response for fork-chat requests."""
+ del kwargs
+ if len(args) >= 2:
+ channel, connection = args[0], args[1]
+ send_event = getattr(channel, "_send_event", None)
+ if callable(send_event):
+ await send_event(
+ connection,
+ "error",
+ detail="fork_chat is not available in this runtime",
+ )
+ return
diff --git a/agent/src/channelsui/gateway_services.py b/agent/src/channelsui/gateway_services.py
new file mode 100644
index 000000000..0e27ce0ff
--- /dev/null
+++ b/agent/src/channelsui/gateway_services.py
@@ -0,0 +1,301 @@
+"""Gateway services used by the WebSocket channel."""
+
+from __future__ import annotations
+
+import json
+import secrets
+import time
+from dataclasses import dataclass, field
+from pathlib import Path
+from typing import Any
+
+from src.config.paths import get_workspace_path
+from src.security.workspace_access import WorkspaceScopeError
+
+
+@dataclass
+class WebSocketTokenIssuer:
+ """Short-lived one-time token issuer for WebSocket clients."""
+
+ _issued: dict[str, float] = field(default_factory=dict)
+
+ def issue_token(self, *, ttl_s: int = 300) -> tuple[str, int]:
+ """Issue a one-time token.
+
+ Args:
+ ttl_s: Token lifetime in seconds.
+
+ Returns:
+ ``(token, expires_in_seconds)``.
+ """
+ self._gc()
+ token = secrets.token_urlsafe(32)
+ self._issued[token] = time.time() + ttl_s
+ return token, ttl_s
+
+ def take_issued_token_if_valid(self, token: str | None) -> bool:
+ """Consume a valid issued token exactly once."""
+ if not token:
+ return False
+ self._gc()
+ expires_at = self._issued.pop(token, None)
+ return bool(expires_at and expires_at >= time.time())
+
+ def clear(self) -> None:
+ """Clear all issued tokens."""
+ self._issued.clear()
+
+ def _gc(self) -> None:
+ now = time.time()
+ for token, expires_at in list(self._issued.items()):
+ if expires_at < now:
+ self._issued.pop(token, None)
+
+
+@dataclass(frozen=True)
+class WorkspaceScope:
+ """Workspace scope snapshot carried in WebSocket session metadata."""
+
+ root: str
+ restrict_to_workspace: bool = True
+
+ def payload(self) -> dict[str, Any]:
+ """Return the client-facing workspace scope."""
+ return {
+ "root": self.root,
+ "restrict_to_workspace": self.restrict_to_workspace,
+ }
+
+ def metadata(self) -> dict[str, Any]:
+ """Return the session metadata representation."""
+ return self.payload()
+
+
+@dataclass
+class WorkspaceService:
+ """Resolve and persist per-chat workspace scope."""
+
+ workspace_path: Path = field(default_factory=get_workspace_path)
+ default_restrict_to_workspace: bool = True
+ _scopes: dict[str, WorkspaceScope] = field(default_factory=dict)
+
+ def _default_scope(self) -> WorkspaceScope:
+ return WorkspaceScope(
+ root=str(self.workspace_path.expanduser().resolve()),
+ restrict_to_workspace=self.default_restrict_to_workspace,
+ )
+
+ def scope_for_new_chat(self, envelope: dict[str, Any], *, controls_available: bool) -> WorkspaceScope:
+ """Build a scope for a new chat envelope."""
+ del controls_available
+ return self._scope_from_envelope(envelope) or self._default_scope()
+
+ def scope_for_set_request(
+ self,
+ envelope: dict[str, Any],
+ *,
+ chat_id: str,
+ chat_running: bool,
+ controls_available: bool,
+ ) -> WorkspaceScope:
+ """Build a scope update from a client request."""
+ del chat_running, controls_available
+ return self._scope_from_envelope(envelope) or self._scopes.get(chat_id) or self._default_scope()
+
+ def scope_for_message(
+ self,
+ envelope: dict[str, Any],
+ *,
+ chat_id: str,
+ chat_running: bool,
+ controls_available: bool,
+ ) -> WorkspaceScope:
+ """Return the active scope for a message."""
+ del chat_running, controls_available
+ return self._scope_from_envelope(envelope) or self._scopes.get(chat_id) or self._default_scope()
+
+ def persist_scope(self, chat_id: str, scope: WorkspaceScope) -> None:
+ """Persist an in-memory scope for the current gateway process."""
+ self._scopes[chat_id] = scope
+
+ def _scope_from_envelope(self, envelope: dict[str, Any]) -> WorkspaceScope | None:
+ raw = envelope.get("workspace_scope")
+ if not isinstance(raw, dict):
+ return None
+ root = raw.get("root")
+ if not isinstance(root, str) or not root.strip():
+ return None
+ resolved = Path(root).expanduser().resolve()
+ default_root = self.workspace_path.expanduser().resolve()
+ if self.default_restrict_to_workspace and not _is_relative_to(resolved, default_root):
+ raise WorkspaceScopeError(f"workspace root must stay under {default_root}")
+ restrict = raw.get("restrict_to_workspace", self.default_restrict_to_workspace)
+ return WorkspaceScope(root=str(resolved), restrict_to_workspace=bool(restrict))
+
+
+class SimpleHttpRouter:
+ """Small HTTP fallback router for WebSocket server requests."""
+
+ def workspace_controls_available(self, connection: Any) -> bool:
+ """Return whether workspace controls may be shown to this connection."""
+ del connection
+ return True
+
+ async def dispatch(self, connection: Any, request: Any) -> Any:
+ """Return a compact JSON 404 for non-WebSocket HTTP requests."""
+ del request
+ return connection.respond(
+ 404,
+ json.dumps({"detail": "not found"}, ensure_ascii=False),
+ )
+
+
+class MediaService:
+ """Media staging helpers for WebSocket payloads."""
+
+ def rewrite_local_markdown_images(self, text: str) -> str:
+ """Return text unchanged until an HTTP media server is configured."""
+ return text
+
+ def sign_or_stage_media_path(self, path: Path) -> dict[str, str] | None:
+ """Return metadata for a local media path when it exists."""
+ try:
+ resolved = path.expanduser().resolve()
+ except OSError:
+ return None
+ if not resolved.exists() or not resolved.is_file():
+ return None
+ return {"name": resolved.name, "path": str(resolved)}
+
+
+@dataclass
+class TranscriptService:
+ """In-process transcript event bridge for the WebSocket channel."""
+
+ events: list[dict[str, Any]] = field(default_factory=list)
+
+ def client_turn_metadata(self, turn_id: Any) -> dict[str, Any]:
+ """Return metadata derived from a client turn id."""
+ return {"turn_id": turn_id} if isinstance(turn_id, str) and turn_id else {}
+
+ def append_user_message(
+ self,
+ chat_id: str,
+ content: str,
+ *,
+ metadata: dict[str, Any] | None = None,
+ media_paths: list[str] | None = None,
+ cli_apps: list[str] | None = None,
+ mcp_presets: list[str] | None = None,
+ ) -> None:
+ """Record a user-message transcript event for the current process."""
+ self.events.append(
+ {
+ "phase": "user",
+ "chat_id": chat_id,
+ "content": content,
+ "metadata": metadata or {},
+ "media_paths": media_paths or [],
+ "cli_apps": cli_apps or [],
+ "mcp_presets": mcp_presets or [],
+ }
+ )
+
+ def prepare_and_append(
+ self,
+ chat_id: str,
+ payload: dict[str, Any],
+ *,
+ metadata: dict[str, Any] | None = None,
+ phase: str = "",
+ include_source: bool = False,
+ transcript_overrides: dict[str, Any] | None = None,
+ ) -> None:
+ """Record a generated transcript event for the current process."""
+ self.events.append(
+ {
+ "phase": phase,
+ "chat_id": chat_id,
+ "payload": payload,
+ "metadata": metadata or {},
+ "include_source": include_source,
+ "transcript_overrides": transcript_overrides or {},
+ }
+ )
+
+
+class GatewaySessionManagerAdapter:
+ """Adapt Vibe-Trading SessionService to the WebSocket gateway contract."""
+
+ def __init__(self, service: Any) -> None:
+ self._service = service
+
+ def __getattr__(self, name: str) -> Any:
+ return getattr(self._service, name)
+
+ def read_session_file(self, session_id: str) -> dict[str, Any]:
+ """Return a JSON-like session row for WebSocket hydration."""
+ getter = getattr(self._service, "get_session", None)
+ if not callable(getter):
+ return {"metadata": {}}
+ session = getter(session_id)
+ if session is None and ":" in session_id:
+ session = getter(session_id.split(":", 1)[1])
+ if session is None:
+ return {"metadata": {}}
+ data = session.to_dict() if hasattr(session, "to_dict") else dict(getattr(session, "__dict__", {}))
+ config = data.get("config") if isinstance(data.get("config"), dict) else {}
+ metadata = data.get("metadata") if isinstance(data.get("metadata"), dict) else config.get("metadata", {})
+ data["metadata"] = metadata if isinstance(metadata, dict) else {}
+ return data
+
+
+@dataclass
+class GatewayServices:
+ """Service bundle required by :class:`src.channels.websocket.WebSocketChannel`."""
+
+ session_manager: Any = None
+ cron_service: Any = None
+ runtime_model_name: Any = None
+ cron_pending_job_ids: Any = None
+ static_dist_path: Any = None
+ workspace_path: Path | None = None
+ default_restrict_to_workspace: bool = True
+ disabled_skills: set[str] = field(default_factory=set)
+ logger: Any = None
+ runtime_surface: str = "browser"
+ runtime_capabilities: dict[str, Any] = field(default_factory=dict)
+ http: SimpleHttpRouter = field(default_factory=SimpleHttpRouter)
+ tokens: WebSocketTokenIssuer = field(default_factory=WebSocketTokenIssuer)
+ media: MediaService = field(default_factory=MediaService)
+ transcripts: TranscriptService = field(default_factory=TranscriptService)
+ workspaces: WorkspaceService = field(default_factory=WorkspaceService)
+
+
+def build_gateway_services(**kwargs: Any) -> GatewayServices:
+ """Build a gateway service bundle from optional overrides."""
+ allowed = {
+ key: value
+ for key, value in kwargs.items()
+ if key in GatewayServices.__dataclass_fields__
+ }
+ session_manager = allowed.get("session_manager")
+ if session_manager is not None and not hasattr(session_manager, "read_session_file"):
+ allowed["session_manager"] = GatewaySessionManagerAdapter(session_manager)
+ workspace_path = allowed.get("workspace_path")
+ if workspace_path is not None and "workspaces" not in allowed:
+ allowed["workspaces"] = WorkspaceService(
+ workspace_path=Path(workspace_path),
+ default_restrict_to_workspace=bool(
+ allowed.get("default_restrict_to_workspace", True)
+ ),
+ )
+ return GatewayServices(**allowed)
+
+
+def _is_relative_to(path: Path, root: Path) -> bool:
+ try:
+ path.relative_to(root)
+ return True
+ except ValueError:
+ return False
diff --git a/agent/src/channelsui/http_utils.py b/agent/src/channelsui/http_utils.py
new file mode 100644
index 000000000..40c99ecb2
--- /dev/null
+++ b/agent/src/channelsui/http_utils.py
@@ -0,0 +1,78 @@
+"""HTTP helpers for the WebSocket channel."""
+
+from __future__ import annotations
+
+import asyncio
+from pathlib import Path
+from typing import Any
+from urllib.parse import parse_qs, urlsplit
+
+from src.channels.utils import validate_url_target
+
+
+_LONG_LIVED_TIMEOUT = 300 # seconds for long-lived HTTP connections
+
+
+def normalize_config_path(path: str | Path) -> str:
+ """Normalize an HTTP/WebSocket route path.
+
+ Args:
+ path: Path-like route value.
+
+ Returns:
+ A leading-slash route without a trailing slash, except for ``"/"``.
+ """
+ value = str(path).strip() or "/"
+ if not value.startswith("/"):
+ value = f"/{value}"
+ while "//" in value:
+ value = value.replace("//", "/")
+ if len(value) > 1:
+ value = value.rstrip("/")
+ return value or "/"
+
+
+def parse_request_path(path: str) -> tuple[str, dict[str, list[str]]]:
+ """Parse an HTTP request path into ``(route_path, query)``."""
+ parts = urlsplit(path or "/")
+ return normalize_config_path(parts.path or "/"), parse_qs(parts.query, keep_blank_values=True)
+
+
+def query_first(qs: dict[str, list[str]], key: str) -> str | None:
+ """Return the first value for *key* in a query string dict."""
+ values = qs.get(key, [])
+ return values[0] if values else None
+
+
+def parse_and_validate_url(url: str, *, allow_loopback: bool = False) -> tuple[bool, str]:
+ """Validate a URL target before a channel-side fetch."""
+ return validate_url_target(url, allow_loopback=allow_loopback)
+
+
+async def read_uploaded_file(*args: Any, **kwargs: Any) -> bytes:
+ """Read bytes from a file-like upload object.
+
+ The helper accepts common async/sync file objects used by lightweight HTTP
+ adapters. It is intentionally conservative and only returns raw bytes.
+ """
+ del kwargs
+ if not args:
+ return b""
+ obj = args[0]
+ if isinstance(obj, bytes):
+ return obj
+ if isinstance(obj, bytearray):
+ return bytes(obj)
+ read = getattr(obj, "read", None)
+ if read is None:
+ raise TypeError("upload object does not provide read()")
+ result = read()
+ if asyncio.iscoroutine(result):
+ result = await result
+ if isinstance(result, str):
+ return result.encode("utf-8")
+ if isinstance(result, bytes):
+ return result
+ if isinstance(result, bytearray):
+ return bytes(result)
+ raise TypeError("upload read() did not return bytes")
diff --git a/agent/src/channelsui/mcp_presets_api.py b/agent/src/channelsui/mcp_presets_api.py
new file mode 100644
index 000000000..c69b80e20
--- /dev/null
+++ b/agent/src/channelsui/mcp_presets_api.py
@@ -0,0 +1,19 @@
+"""MCP preset mention normalization for the WebSocket channel."""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+def normalize_mcp_preset_mentions(content: Any, metadata: dict[str, Any] | None = None) -> list[str]:
+ """Normalize MCP preset mentions into a stable list of names."""
+ del metadata
+ if content is None:
+ return []
+ if isinstance(content, str):
+ raw_items = content.replace(",", " ").split()
+ elif isinstance(content, (list, tuple, set)):
+ raw_items = [str(item) for item in content]
+ else:
+ raw_items = [str(content)]
+ return [item.lstrip("@").strip() for item in raw_items if item and item.strip()]
diff --git a/agent/src/channelsui/transcription_ws.py b/agent/src/channelsui/transcription_ws.py
new file mode 100644
index 000000000..3f44f5e2a
--- /dev/null
+++ b/agent/src/channelsui/transcription_ws.py
@@ -0,0 +1,11 @@
+"""Transcription WebSocket compatibility helpers."""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+async def webui_transcription_event(*args: Any, **kwargs: Any) -> tuple[str, dict[str, Any]]:
+ """Return a structured response when transcription is not configured."""
+ del args, kwargs
+ return "error", {"detail": "audio transcription is not configured"}
diff --git a/agent/src/channelsui/websocket_logging.py b/agent/src/channelsui/websocket_logging.py
new file mode 100644
index 000000000..e15ee3d97
--- /dev/null
+++ b/agent/src/channelsui/websocket_logging.py
@@ -0,0 +1,7 @@
+"""WebSocket logging bridge for channel adapters."""
+
+from __future__ import annotations
+
+import logging
+
+websockets_server_logger = logging.getLogger("src.channelsui.websocket")
diff --git a/agent/src/config/paths.py b/agent/src/config/paths.py
index 3656e1324..d260b090f 100644
--- a/agent/src/config/paths.py
+++ b/agent/src/config/paths.py
@@ -68,4 +68,15 @@ def get_data_dir(config_path: Path | None = None) -> Path:
"""
data_dir = get_config_path(config_path).parent
data_dir.mkdir(parents=True, exist_ok=True)
- return data_dir
\ No newline at end of file
+ return data_dir
+
+
+def get_workspace_path() -> Path:
+ """Return the workspace path for channel state data.
+
+ For channel adapters that need to persist state (e.g. conversation
+ references, auth tokens), this returns ``~/.vibe-trading/workspace``.
+ """
+ p = get_runtime_root() / "workspace"
+ p.mkdir(parents=True, exist_ok=True)
+ return p
\ No newline at end of file
diff --git a/agent/src/config/schema.py b/agent/src/config/schema.py
index 9bd6d192e..a33552f4f 100644
--- a/agent/src/config/schema.py
+++ b/agent/src/config/schema.py
@@ -424,10 +424,26 @@ class MCPServerConfigOverride(ConfigBase):
enabled_tools: list[str] | None = None
+class ChannelsConfig(ConfigBase):
+ """Top-level IM channel config.
+
+ Built-in adapters parse their own per-channel sections. This model keeps
+ the operator file strict for global fields while allowing platform-specific
+ channel keys such as ``telegram`` or ``feishu``.
+ """
+
+ model_config = ConfigDict(alias_generator=_to_camel, populate_by_name=True, extra="allow")
+
+ send_progress: bool = True
+ send_tool_hints: bool = False
+ send_max_retries: int = Field(default=2, ge=1, le=10)
+
+
class AgentConfig(ConfigBase):
"""Top-level structured agent config."""
mcp_servers: dict[str, MCPServerConfig] = Field(default_factory=dict)
+ channels: ChannelsConfig = Field(default_factory=ChannelsConfig)
@model_validator(mode="after")
def validate_live_broker_servers(self) -> "AgentConfig":
@@ -483,3 +499,4 @@ class AgentConfigOverride(ConfigBase):
)
mcp_servers: dict[str, MCPServerConfigOverride] = Field(default_factory=dict)
+ channels: ChannelsConfig | None = None
diff --git a/agent/src/live/runtime/reconcile.py b/agent/src/live/runtime/reconcile.py
index 2c197288c..0bc341246 100644
--- a/agent/src/live/runtime/reconcile.py
+++ b/agent/src/live/runtime/reconcile.py
@@ -1,6 +1,6 @@
"""Crash recovery + position reconciliation (SPEC.md §7.5 component 5).
-This is the hardest piece of the persistent runtime and has **no nanobot
+This is the hardest piece of the persistent runtime and has **no reference-agent
analog**. On startup and before every trading tick the runner must answer one
question before it is allowed to trade again: *does broker truth match the
durable last-known state we persisted before the crash?* Trading is not
diff --git a/agent/src/live/runtime/scheduler.py b/agent/src/live/runtime/scheduler.py
index 6f5876ba2..fcac9bfeb 100644
--- a/agent/src/live/runtime/scheduler.py
+++ b/agent/src/live/runtime/scheduler.py
@@ -1,6 +1,6 @@
"""Wall-clock scheduler for the live runtime (SPEC.md §7.5 #1).
-Follows nanobot's ``cron/service.py`` shape: an asyncio timer that sleeps until
+Follows the reference ``cron/service.py`` shape: an asyncio timer that sleeps until
the earliest ``next_run_at`` across all jobs, fires the due jobs, advances each
fired job's ``next_run_at``, then re-sleeps. The sleep is **capped** at a
re-check interval (default 5 min) so a far-future job still wakes the loop
diff --git a/agent/src/live/runtime/triggers.py b/agent/src/live/runtime/triggers.py
index 6aaa9e174..40bf69826 100644
--- a/agent/src/live/runtime/triggers.py
+++ b/agent/src/live/runtime/triggers.py
@@ -1,7 +1,7 @@
"""Live-runtime triggers — when an autonomous tick is due (SPEC §7.5 component 4).
This layers market-session, interval, and event triggers over R1's wall-clock
-scheduler. nanobot is time-only; trading additionally needs market-session
+scheduler. The reference scheduler is time-only; trading additionally needs market-session
awareness ("is the US equity market open right now?") and event-driven
predicates (price crossings, fill notifications). This module owns that layer.
diff --git a/agent/src/providers/openai_codex.py b/agent/src/providers/openai_codex.py
index 8faecec8b..c78dc5f45 100644
--- a/agent/src/providers/openai_codex.py
+++ b/agent/src/providers/openai_codex.py
@@ -1,6 +1,6 @@
"""OpenAI Codex OAuth provider.
-This provider follows nanobot's OpenAI Codex OAuth path: a ChatGPT account is
+This provider follows the reference OpenAI Codex OAuth path: a ChatGPT account is
authenticated by oauth-cli-kit, then requests are sent to the ChatGPT Codex
Responses endpoint. It is intentionally separate from the standard OpenAI API
key path because ChatGPT OAuth tokens are not OpenAI API keys.
diff --git a/agent/src/security/network.py b/agent/src/security/network.py
new file mode 100644
index 000000000..52f873e2d
--- /dev/null
+++ b/agent/src/security/network.py
@@ -0,0 +1,10 @@
+"""Security network compatibility helpers for channel adapters.
+
+Re-exports validate_url_target / validate_resolved_url from :mod:`src.channels.utils`.
+"""
+
+from __future__ import annotations
+
+from src.channels.utils import validate_resolved_url, validate_url_target
+
+__all__ = ["validate_resolved_url", "validate_url_target"]
diff --git a/agent/src/security/workspace_access.py b/agent/src/security/workspace_access.py
new file mode 100644
index 000000000..7af6cf50d
--- /dev/null
+++ b/agent/src/security/workspace_access.py
@@ -0,0 +1,14 @@
+"""Security workspace access compatibility helpers for channel adapters."""
+
+from __future__ import annotations
+
+WORKSPACE_SCOPE_METADATA_KEY = "_workspace_scope"
+
+
+class WorkspaceScopeError(ValueError):
+ """Raised when a path is outside the allowed workspace scope."""
+
+ @property
+ def message(self) -> str:
+ """Compatibility accessor for ported channel code."""
+ return str(self)
diff --git a/agent/src/security/workspace_policy.py b/agent/src/security/workspace_policy.py
new file mode 100644
index 000000000..47d021f66
--- /dev/null
+++ b/agent/src/security/workspace_policy.py
@@ -0,0 +1,11 @@
+"""Security workspace policy compatibility helpers for channel adapters.
+
+Re-exports :func:`is_path_within` from :mod:`src.channels.utils`.
+"""
+
+from __future__ import annotations
+
+from src.channels.utils import is_path_within as _is_path_within
+
+# Re-export for backward compat with ported channel code
+is_path_within = _is_path_within
diff --git a/agent/src/session/goal_state.py b/agent/src/session/goal_state.py
new file mode 100644
index 000000000..55aebb519
--- /dev/null
+++ b/agent/src/session/goal_state.py
@@ -0,0 +1,28 @@
+"""Goal-state payload helpers for WebSocket channel adapters."""
+
+from __future__ import annotations
+
+from typing import Any
+
+
+def goal_state_ws_blob(meta: dict[str, Any] | None = None) -> dict[str, Any]:
+ """Build a WebSocket payload for the current active goal state.
+
+ The WebSocket gateway stores goal snapshots in session metadata when they
+ are available. This helper normalizes either a direct ``goal_state`` payload
+ or compact goal metadata into the client event shape. It returns an inactive
+ payload when no goal metadata is present.
+ """
+ if not isinstance(meta, dict):
+ return {"active": False, "active_goals": [], "completed_goals": []}
+ goal_state = meta.get("goal_state")
+ if isinstance(goal_state, dict):
+ return {
+ "active": bool(goal_state.get("active")),
+ "active_goals": list(goal_state.get("active_goals") or []),
+ "completed_goals": list(goal_state.get("completed_goals") or []),
+ }
+ goal = meta.get("active_goal") or meta.get("goal")
+ if isinstance(goal, dict):
+ return {"active": True, "active_goals": [goal], "completed_goals": []}
+ return {"active": False, "active_goals": [], "completed_goals": []}
diff --git a/agent/src/session/webui_turns.py b/agent/src/session/webui_turns.py
new file mode 100644
index 000000000..401a9847a
--- /dev/null
+++ b/agent/src/session/webui_turns.py
@@ -0,0 +1,27 @@
+"""In-process WebUI turn timing helpers for channel adapters."""
+
+from __future__ import annotations
+
+import time
+
+
+_TURN_STARTED_AT: dict[str, float] = {}
+
+
+def mark_websocket_turn_started(chat_id: str, started_at: float | None = None) -> float:
+ """Record that a WebSocket chat turn started and return its timestamp."""
+ value = float(started_at if started_at is not None else time.time())
+ _TURN_STARTED_AT[str(chat_id)] = value
+ return value
+
+
+def clear_websocket_turn_started(chat_id: str) -> None:
+ """Clear a recorded WebSocket turn start timestamp."""
+ _TURN_STARTED_AT.pop(str(chat_id), None)
+
+
+def websocket_turn_wall_started_at(chat_id: str | None = None) -> float | None:
+ """Return the wall-clock time when the current turn started, or None."""
+ if chat_id is None:
+ return None
+ return _TURN_STARTED_AT.get(str(chat_id))
diff --git a/agent/src/utils/media_decode.py b/agent/src/utils/media_decode.py
new file mode 100644
index 000000000..6efbb503e
--- /dev/null
+++ b/agent/src/utils/media_decode.py
@@ -0,0 +1,65 @@
+"""Media decoding helpers for channel adapters."""
+
+from __future__ import annotations
+
+import base64
+import binascii
+import re
+import uuid
+from pathlib import Path
+
+
+class FileSizeExceeded(ValueError):
+ """Raised when a file exceeds the max size limit."""
+
+_DATA_URL_RE = re.compile(r"^data:([^;,]+)((?:;[^,]*)*);base64,(.*)$", re.IGNORECASE | re.DOTALL)
+_EXT_BY_MIME = {
+ "image/png": ".png",
+ "image/jpeg": ".jpg",
+ "image/jpg": ".jpg",
+ "image/gif": ".gif",
+ "image/webp": ".webp",
+ "video/mp4": ".mp4",
+ "video/webm": ".webm",
+ "text/plain": ".txt",
+ "application/pdf": ".pdf",
+}
+
+
+def save_base64_data_url(data_url: str, output_dir: Path, *, max_bytes: int = 0) -> Path:
+ """Decode a base64 data URL and save it to *output_dir*.
+
+ Args:
+ data_url: A ``data:<mime>;base64,...`` URL.
+ output_dir: Directory where the decoded file should be written.
+ max_bytes: Optional maximum decoded byte length. ``0`` disables the
+ limit.
+
+ Returns:
+ The path of the decoded file.
+
+ Raises:
+ FileSizeExceeded: If decoded data exceeds ``max_bytes``.
+ ValueError: If the URL is malformed or uses an unsupported MIME type.
+ """
+ match = _DATA_URL_RE.match(data_url)
+ if not match:
+ raise ValueError("expected data:<mime>;base64,<payload>")
+ mime = match.group(1).strip().lower()
+ ext = _EXT_BY_MIME.get(mime)
+ if not ext:
+ raise ValueError(f"unsupported data URL MIME type: {mime}")
+
+ payload = match.group(3).strip()
+ try:
+ decoded = base64.b64decode(payload, validate=True)
+ except (binascii.Error, ValueError) as exc:
+ raise ValueError("invalid base64 payload") from exc
+
+ if max_bytes and len(decoded) > max_bytes:
+ raise FileSizeExceeded(f"decoded media exceeds {max_bytes} bytes")
+
+ output_dir.mkdir(parents=True, exist_ok=True)
+ path = output_dir / f"{uuid.uuid4().hex}{ext}"
+ path.write_bytes(decoded)
+ return path
diff --git a/agent/tests/test_channels_api.py b/agent/tests/test_channels_api.py
new file mode 100644
index 000000000..8d9fb4749
--- /dev/null
+++ b/agent/tests/test_channels_api.py
@@ -0,0 +1,76 @@
+"""API contracts for IM channel runtime controls."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+from fastapi.testclient import TestClient
+
+import api_server
+
+
+class FakeSessionService:
+ """SessionService placeholder for channel status tests."""
+
+
+def _client(tmp_path: Path, monkeypatch) -> TestClient:
+ import src.channels.config as channel_config
+ import src.channels.pairing.store as pairing_store
+
+ monkeypatch.setattr(api_server, "_channel_runtime", None)
+ monkeypatch.setattr(api_server, "_channel_bus", None)
+ monkeypatch.setattr(api_server, "_channel_manager", None)
+ monkeypatch.setattr(api_server, "_get_session_service", lambda: FakeSessionService())
+ monkeypatch.setattr(
+ channel_config,
+ "load_channels_config",
+ lambda: {
+ "send_max_retries": 1,
+ "websocket": {"enabled": False, "allow_from": ["*"]},
+ "telegram": {"enabled": False},
+ "slack": {"enabled": True},
+ },
+ )
+ monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
+ return TestClient(api_server.app, client=("127.0.0.1", 50000))
+
+
+def test_channels_status_reports_all_configured_adapters(tmp_path: Path, monkeypatch) -> None:
+ client = _client(tmp_path, monkeypatch)
+
+ response = client.get("/channels/status")
+
+ assert response.status_code == 200
+ payload = response.json()
+ assert payload["running"] is False
+ assert payload["channels"]["websocket"]["configured"] is True
+ assert payload["channels"]["telegram"]["enabled"] is False
+ assert payload["channels"]["slack"]["enabled"] is True
+ assert "available" in payload["channels"]["slack"]
+
+
+def test_channels_start_stop_are_controlled_runtime_actions(tmp_path: Path, monkeypatch) -> None:
+ client = _client(tmp_path, monkeypatch)
+
+ start = client.post("/channels/start")
+ stop = client.post("/channels/stop")
+
+ assert start.status_code == 200
+ assert start.json()["status"] == "started"
+ assert start.json()["running"] is True
+ assert stop.status_code == 200
+ assert stop.json()["status"] == "stopped"
+ assert stop.json()["running"] is False
+
+
+def test_channels_pairing_command_uses_shared_store(tmp_path: Path, monkeypatch) -> None:
+ client = _client(tmp_path, monkeypatch)
+
+ response = client.post(
+ "/channels/pairing/command",
+ json={"channel": "telegram", "command": "list"},
+ )
+
+ assert response.status_code == 200
+ assert response.json()["channel"] == "telegram"
+ assert "No pending pairing requests" in response.json()["reply"]
diff --git a/agent/tests/test_channels_runtime.py b/agent/tests/test_channels_runtime.py
new file mode 100644
index 000000000..1e0fdeda4
--- /dev/null
+++ b/agent/tests/test_channels_runtime.py
@@ -0,0 +1,304 @@
+"""Contracts for the IM channel runtime wiring."""
+
+from __future__ import annotations
+
+import asyncio
+import base64
+from pathlib import Path
+from typing import Any
+
+import pytest
+
+from src.channels.bus.events import InboundMessage, OutboundMessage
+from src.channels.bus.queue import MessageBus
+from src.channels.manager import ChannelManager
+from src.channels.registry import discover_channel_names, inspect_channels
+from src.channelsui.cli_apps_api import normalize_cli_app_mentions
+from src.channelsui.gateway_services import build_gateway_services
+from src.channelsui.mcp_presets_api import normalize_mcp_preset_mentions
+from src.channelsui.transcription_ws import webui_transcription_event
+from src.session.goal_state import goal_state_ws_blob
+from src.session.models import Message, Session
+from src.session.webui_turns import (
+ clear_websocket_turn_started,
+ mark_websocket_turn_started,
+ websocket_turn_wall_started_at,
+)
+from src.utils.media_decode import FileSizeExceeded, save_base64_data_url
+
+
+class FakeSessionService:
+ """Small SessionService stand-in for channel runtime tests."""
+
+ def __init__(self) -> None:
+ self.created: list[Session] = []
+ self.sent: list[tuple[str, str]] = []
+ self.messages: dict[str, list[Message]] = {}
+
+ def create_session(self, title: str = "", config: dict[str, Any] | None = None) -> Session:
+ session = Session(session_id=f"session-{len(self.created) + 1}", title=title, config=config or {})
+ self.created.append(session)
+ self.messages[session.session_id] = []
+ return session
+
+ def get_session(self, session_id: str) -> Session | None:
+ return next((session for session in self.created if session.session_id == session_id), None)
+
+ async def send_message(
+ self,
+ session_id: str,
+ content: str,
+ *,
+ include_shell_tools: bool = False,
+ parent_attempt_id: str | None = None,
+ ) -> dict[str, str]:
+ del include_shell_tools, parent_attempt_id
+ self.sent.append((session_id, content))
+ attempt_id = f"attempt-{len(self.sent)}"
+ self.messages[session_id].append(
+ Message(
+ session_id=session_id,
+ role="assistant",
+ content=f"agent reply: {content}",
+ linked_attempt_id=attempt_id,
+ )
+ )
+ return {"message_id": "msg-1", "attempt_id": attempt_id}
+
+ def get_messages(self, session_id: str, limit: int = 100) -> list[Message]:
+ del limit
+ return list(self.messages.get(session_id, []))
+
+
+def test_channel_manager_can_construct_websocket_with_default_gateway() -> None:
+ bus = MessageBus()
+ manager = ChannelManager(
+ {"websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]}},
+ bus,
+ )
+
+ assert "websocket" in manager.channels
+ assert manager.channels["websocket"].name == "websocket"
+
+
+def test_registry_reports_all_built_in_channels_with_dependency_recovery() -> None:
+ expected = {
+ "dingtalk",
+ "discord",
+ "email",
+ "feishu",
+ "matrix",
+ "mochat",
+ "msteams",
+ "napcat",
+ "qq",
+ "signal",
+ "slack",
+ "telegram",
+ "websocket",
+ "wecom",
+ "weixin",
+ "whatsapp",
+ }
+
+ assert expected.issubset(set(discover_channel_names()))
+ assert {"config", "runtime"}.isdisjoint(set(discover_channel_names()))
+
+ statuses = inspect_channels({"telegram": {"enabled": True}, "slack": {"enabled": True}})
+
+ for name in expected:
+ assert name in statuses
+ assert statuses[name]["name"] == name
+ assert "install_hint" in statuses[name]
+ assert "available" in statuses[name]
+
+ missing = [item for item in statuses.values() if not item["available"]]
+ assert all(item["install_hint"].startswith("pip install") for item in missing)
+
+
+def test_channel_manager_status_includes_every_configured_adapter() -> None:
+ bus = MessageBus()
+ manager = ChannelManager(
+ {
+ "send_max_retries": 1,
+ "websocket": {"enabled": True, "host": "127.0.0.1", "port": 0, "allow_from": ["*"]},
+ "telegram": {"enabled": False},
+ "slack": {"enabled": True},
+ },
+ bus,
+ )
+
+ status = manager.get_status()
+
+ assert status["websocket"]["loaded"] is True
+ assert status["websocket"]["enabled"] is True
+ assert status["telegram"]["configured"] is True
+ assert status["telegram"]["enabled"] is False
+ assert status["slack"]["configured"] is True
+ assert status["slack"]["enabled"] is True
+ assert "available" in status["slack"]
+ if not status["slack"]["loaded"]:
+ assert status["slack"]["error"]
+ assert status["slack"]["install_hint"].startswith("pip install")
+
+
+def test_registry_marks_lazy_sdk_adapter_unavailable(monkeypatch: pytest.MonkeyPatch) -> None:
+ import src.channels.discord as discord_channel
+
+ from src.channels.registry import inspect_channel
+
+ monkeypatch.setattr(discord_channel, "DISCORD_AVAILABLE", False)
+
+ status = inspect_channel("discord").to_dict()
+
+ assert status["available"] is False
+ assert status["install_hint"] == "pip install 'vibe-trading-ai[discord]'"
+
+
+def test_channel_manager_skips_enabled_adapter_when_lazy_sdk_missing(monkeypatch: pytest.MonkeyPatch) -> None:
+ import src.channels.discord as discord_channel
+
+ monkeypatch.setattr(discord_channel, "DISCORD_AVAILABLE", False)
+
+ manager = ChannelManager({"discord": {"enabled": True, "token": "x"}}, MessageBus())
+ status = manager.get_status()["discord"]
+
+ assert "discord" not in manager.channels
+ assert status["enabled"] is True
+ assert status["available"] is False
+ assert status["loaded"] is False
+ assert status["install_hint"] == "pip install 'vibe-trading-ai[discord]'"
+
+
+def test_websocket_turn_wall_accepts_optional_chat_id() -> None:
+ assert websocket_turn_wall_started_at("chat-1") is None
+ mark_websocket_turn_started("chat-1", 123.0)
+ assert websocket_turn_wall_started_at("chat-1") == 123.0
+ clear_websocket_turn_started("chat-1")
+ assert websocket_turn_wall_started_at("chat-1") is None
+
+
+def test_websocket_compatibility_helpers_have_structured_behavior() -> None:
+ assert goal_state_ws_blob({"active_goal": {"goal_id": "g1"}}) == {
+ "active": True,
+ "active_goals": [{"goal_id": "g1"}],
+ "completed_goals": [],
+ }
+ assert normalize_cli_app_mentions("@codex, terminal") == ["codex", "terminal"]
+ assert normalize_mcp_preset_mentions(["@research", "market"]) == ["research", "market"]
+
+
+def test_gateway_services_adapt_session_service_for_hydration() -> None:
+ service = FakeSessionService()
+ session = service.create_session(title="chat", config={"metadata": {"active_goal": {"goal_id": "g1"}}})
+ gateway = build_gateway_services(session_manager=service)
+
+ row = gateway.session_manager.read_session_file(session.session_id)
+
+ assert row["metadata"] == {"active_goal": {"goal_id": "g1"}}
+
+
+def test_transcription_event_returns_structured_error() -> None:
+ async def scenario() -> None:
+ event, payload = await webui_transcription_event({"type": "transcribe_audio"})
+ assert event == "error"
+ assert "not configured" in payload["detail"]
+
+ asyncio.run(scenario())
+
+
+def test_save_base64_data_url_decodes_and_limits_size(tmp_path: Path) -> None:
+ payload = base64.b64encode(b"hello").decode("ascii")
+
+ saved = save_base64_data_url(f"data:text/plain;base64,{payload}", tmp_path, max_bytes=10)
+
+ assert saved.read_text(encoding="utf-8") == "hello"
+ assert saved.suffix == ".txt"
+
+ with pytest.raises(FileSizeExceeded):
+ save_base64_data_url(f"data:text/plain;base64,{payload}", tmp_path, max_bytes=4)
+
+ with pytest.raises(ValueError):
+ save_base64_data_url("not-a-data-url", tmp_path, max_bytes=10)
+
+
+def test_channel_runtime_routes_inbound_to_session_and_outbound(tmp_path: Path) -> None:
+ async def scenario() -> None:
+ from src.channels.runtime import ChannelRuntime
+
+ bus = MessageBus()
+ service = FakeSessionService()
+ runtime = ChannelRuntime(
+ bus=bus,
+ session_service=service,
+ manager=None,
+ session_map_path=tmp_path / "channel_sessions.json",
+ reply_timeout_s=1,
+ poll_interval_s=0.01,
+ )
+ await runtime.start(start_manager=False)
+ try:
+ await bus.publish_inbound(
+ InboundMessage(
+ channel="websocket",
+ sender_id="user-1",
+ chat_id="chat-1",
+ content="hello from IM",
+ )
+ )
+
+ outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
+ finally:
+ await runtime.stop()
+
+ assert service.sent == [("session-1", "hello from IM")]
+ assert outbound == OutboundMessage(
+ channel="websocket",
+ chat_id="chat-1",
+ content="agent reply: hello from IM",
+ metadata={
+ "_channel_runtime": True,
+ "attempt_id": "attempt-1",
+ "session_id": "session-1",
+ },
+ )
+
+ asyncio.run(scenario())
+
+
+def test_channel_runtime_handles_pairing_commands_without_agent(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
+ async def scenario() -> None:
+ from src.channels.runtime import ChannelRuntime
+
+ monkeypatch.setenv("VIBE_TRADING_DATA_DIR", str(tmp_path / "data"))
+ bus = MessageBus()
+ service = FakeSessionService()
+ runtime = ChannelRuntime(
+ bus=bus,
+ session_service=service,
+ manager=None,
+ session_map_path=tmp_path / "channel_sessions.json",
+ reply_timeout_s=1,
+ poll_interval_s=0.01,
+ )
+ await runtime.start(start_manager=False)
+ try:
+ await bus.publish_inbound(
+ InboundMessage(
+ channel="telegram",
+ sender_id="owner",
+ chat_id="chat-1",
+ content="/pairing list",
+ )
+ )
+ outbound = await asyncio.wait_for(bus.consume_outbound(), timeout=1)
+ finally:
+ await runtime.stop()
+
+ assert service.sent == []
+ assert outbound.channel == "telegram"
+ assert outbound.chat_id == "chat-1"
+ assert "No pending pairing requests" in outbound.content
+ assert outbound.metadata["_pairing_command"] is True
+
+ asyncio.run(scenario())
diff --git a/agent/tests/test_cli_channels.py b/agent/tests/test_cli_channels.py
new file mode 100644
index 000000000..717d69e55
--- /dev/null
+++ b/agent/tests/test_cli_channels.py
@@ -0,0 +1,79 @@
+"""CLI coverage for IM channel commands."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+from cli import _legacy
+
+
+def test_channels_parser_accepts_status_start_stop_login_and_pairing() -> None:
+ parser = _legacy._build_parser()
+
+ status = parser.parse_args(["channels", "status", "--local", "--json"])
+ assert status.command == "channels"
+ assert status.channels_command == "status"
+ assert status.local is True
+ assert status.channels_json is True
+
+ start = parser.parse_args(["channels", "start"])
+ assert start.channels_command == "start"
+
+ stop = parser.parse_args(["channels", "stop"])
+ assert stop.channels_command == "stop"
+
+ login = parser.parse_args(["channels", "login", "weixin", "--force"])
+ assert login.channels_command == "login"
+ assert login.channel_name == "weixin"
+ assert login.force is True
+
+ pairing = parser.parse_args(["channels", "pairing", "--channel", "telegram", "approve", "ABCD-EFGH"])
+ assert pairing.channels_command == "pairing"
+ assert pairing.channel == "telegram"
+ assert pairing.pairing_command == "approve"
+ assert pairing.pairing_args == ["ABCD-EFGH"]
+
+
+def test_channels_pairing_command_runs_against_local_store(tmp_path: Path, monkeypatch) -> None:
+ import src.channels.pairing.store as pairing_store
+
+ monkeypatch.setattr(pairing_store, "_store_path", lambda: tmp_path / "pairing.json")
+
+ assert _legacy.main(["channels", "pairing", "--channel", "telegram", "list"]) == _legacy.EXIT_SUCCESS
+
+
+def test_channels_status_can_render_local_json(monkeypatch) -> None:
+ import src.channels.config as channel_config
+
+ monkeypatch.setattr(
+ channel_config,
+ "load_channels_config",
+ lambda: {"websocket": {"enabled": False}, "telegram": {"enabled": False}},
+ )
+
+ assert _legacy.main(["channels", "status", "--local", "--json"]) == _legacy.EXIT_SUCCESS
+
+
+def test_channels_api_call_sends_configured_bearer_token(monkeypatch) -> None:
+ captured = {}
+
+ class FakeResponse:
+ def raise_for_status(self) -> None:
+ return None
+
+ def json(self) -> dict[str, str]:
+ return {"status": "ok"}
+
+ def fake_get(url, *, headers, timeout):
+ captured["url"] = url
+ captured["headers"] = headers
+ captured["timeout"] = timeout
+ return FakeResponse()
+
+ import httpx
+
+ monkeypatch.setenv("API_AUTH_KEY", "secret-token")
+ monkeypatch.setattr(httpx, "get", fake_get)
+
+ assert _legacy._channels_api_call("GET", "/channels/status") == {"status": "ok"}
+ assert captured["headers"] == {"Authorization": "Bearer secret-token"}
diff --git a/agent/tests/test_packaging_dependencies.py b/agent/tests/test_packaging_dependencies.py
index a575c25b4..b2c0a61c9 100644
--- a/agent/tests/test_packaging_dependencies.py
+++ b/agent/tests/test_packaging_dependencies.py
@@ -44,3 +44,72 @@ def test_harmonic_backend_is_available_as_an_optional_extra() -> None:
}
assert "pyharmonics" in harmonic_extra
+
+
+def test_channel_core_websocket_dependency_is_declared_for_baseline_installs() -> None:
+ """The built-in WebSocket gateway imports websockets at module import time."""
+ pyproject = tomllib.loads((ROOT / "pyproject.toml").read_text())
+
+ core_dependencies = {
+ _normalized_requirement_name(requirement)
+ for requirement in pyproject["project"]["dependencies"]
+ }
+ requirements_txt = {
+ _normalized_requirement_name(line)
+ for line in (ROOT / "agent" / "requirements.txt").read_text().splitlines()
+ if line and not line.startswith("#")
+ }
+
+ assert "websockets" in core_dependencies
+ assert "websockets" in requirements_txt
+
+
+def test_channel_optional_extras_cover_all_sdk_backed_adapters() -> None:
+ """Keep install hints and packaging extras in sync for IM adapters."""
+ pyproject = tomllib.loads((ROOT / "pyproject.toml").read_text())
+ extras = pyproject["project"]["optional-dependencies"]
+
+ expected_extras = {
+ "channels",
+ "dingtalk",
+ "discord",
+ "feishu",
+ "matrix",
+ "mochat",
+ "msteams",
+ "napcat",
+ "qq",
+ "slack",
+ "telegram",
+ "wecom",
+ "weixin",
+ "whatsapp",
+ }
+ assert expected_extras.issubset(set(extras))
+
+ channel_extra = {
+ _normalized_requirement_name(requirement)
+ for requirement in extras["channels"]
+ }
+ expected_packages = {
+ "aiohttp",
+ "cryptography",
+ "dingtalk-stream",
+ "discord.py",
+ "lark-oapi",
+ "matrix-nio",
+ "mistune",
+ "msgpack",
+ "neonize",
+ "nh3",
+ "pyjwt",
+ "python-socketio",
+ "python-telegram-bot",
+ "qrcode",
+ "qq-botpy",
+ "slack-sdk",
+ "slackify-markdown",
+ "websockets",
+ "wecom-aibot-sdk",
+ }
+ assert expected_packages.issubset(channel_extra)
diff --git a/frontend/src/i18n/locales/ar.json b/frontend/src/i18n/locales/ar.json
index fd9b94eb5..a981503c3 100644
--- a/frontend/src/i18n/locales/ar.json
+++ b/frontend/src/i18n/locales/ar.json
@@ -253,6 +253,30 @@
"useProviderDefaults": "استخدام الإعدادات الافتراضية للمزوّد",
"modelIdHint": "استخدم معرّف النموذج الدقيق المطلوب من قِبل المزوّد.",
"clearApiKey": "مسح مفتاح API المحفوظ",
+ "channels": {
+ "title": "قنوات IM",
+ "description": "صل محولات الدردشة بنفس وقت تشغيل جلسات Vibe-Trading المستخدم في Web UI و CLI.",
+ "runtime": "وقت التشغيل",
+ "running": "قيد التشغيل",
+ "stopped": "متوقف",
+ "enabled": "مفعّل",
+ "disabled": "معطّل",
+ "loaded": "محمّل",
+ "notLoaded": "غير محمّل",
+ "unavailable": "غير متاح",
+ "channel": "القناة",
+ "state": "الحالة",
+ "recovery": "الاسترداد",
+ "noRecovery": "جاهز",
+ "refresh": "تحديث",
+ "start": "بدء القنوات",
+ "stop": "إيقاف القنوات",
+ "started": "تم بدء قنوات IM",
+ "stoppedToast": "تم إيقاف قنوات IM",
+ "refreshFailed": "فشل تحديث قنوات IM",
+ "startFailed": "فشل بدء قنوات IM",
+ "stopFailed": "فشل إيقاف قنوات IM"
+ },
"generation": "إعدادات التوليد",
"dataSourceSettings": "إعدادات مصادر البيانات",
"dataSourceSettingsDesc": "اضبط بيانات اعتماد بيانات السوق الاختيارية المستخدمة في الاختبارات العكسية ووكلاء البحث.",
diff --git a/frontend/src/i18n/locales/en.json b/frontend/src/i18n/locales/en.json
index d92d38955..e5d91aaca 100644
--- a/frontend/src/i18n/locales/en.json
+++ b/frontend/src/i18n/locales/en.json
@@ -258,6 +258,30 @@
"useProviderDefaults": "Use provider defaults",
"modelIdHint": "Use the exact model id required by your provider.",
"clearApiKey": "Clear saved API key",
+ "channels": {
+ "title": "IM Channels",
+ "description": "Connect chat adapters to the same Vibe-Trading session runtime used by the Web UI and CLI.",
+ "runtime": "Runtime",
+ "running": "Running",
+ "stopped": "Stopped",
+ "enabled": "Enabled",
+ "disabled": "Disabled",
+ "loaded": "Loaded",
+ "notLoaded": "Not loaded",
+ "unavailable": "Unavailable",
+ "channel": "Channel",
+ "state": "State",
+ "recovery": "Recovery",
+ "noRecovery": "Ready",
+ "refresh": "Refresh",
+ "start": "Start channels",
+ "stop": "Stop channels",
+ "started": "IM channels started",
+ "stoppedToast": "IM channels stopped",
+ "refreshFailed": "Failed to refresh IM channels",
+ "startFailed": "Failed to start IM channels",
+ "stopFailed": "Failed to stop IM channels"
+ },
"generation": "Generation",
"dataSourceSettings": "Data Source Settings",
"dataSourceSettingsDesc": "Configure optional market data credentials used by backtests and research agents.",
diff --git a/frontend/src/i18n/locales/ja.json b/frontend/src/i18n/locales/ja.json
index 8ff741a91..d61be5e6d 100644
--- a/frontend/src/i18n/locales/ja.json
+++ b/frontend/src/i18n/locales/ja.json
@@ -253,6 +253,30 @@
"useProviderDefaults": "プロバイダーのデフォルトを使用",
"modelIdHint": "プロバイダーが必要とする正確なモデル ID を使用してください。",
"clearApiKey": "保存済み API キーをクリア",
+ "channels": {
+ "title": "IM チャンネル",
+ "description": "チャットアダプターを Web UI と CLI が使う同じ Vibe-Trading セッションランタイムへ接続します。",
+ "runtime": "ランタイム",
+ "running": "実行中",
+ "stopped": "停止中",
+ "enabled": "有効",
+ "disabled": "無効",
+ "loaded": "ロード済み",
+ "notLoaded": "未ロード",
+ "unavailable": "利用不可",
+ "channel": "チャンネル",
+ "state": "状態",
+ "recovery": "復旧",
+ "noRecovery": "準備完了",
+ "refresh": "更新",
+ "start": "チャンネル開始",
+ "stop": "チャンネル停止",
+ "started": "IM チャンネルを開始しました",
+ "stoppedToast": "IM チャンネルを停止しました",
+ "refreshFailed": "IM チャンネルの更新に失敗しました",
+ "startFailed": "IM チャンネルの開始に失敗しました",
+ "stopFailed": "IM チャンネルの停止に失敗しました"
+ },
"generation": "生成設定",
"dataSourceSettings": "データソース設定",
"dataSourceSettingsDesc": "バックテストとリサーチエージェントが使用するオプションのマーケットデータ認証情報を設定します。",
diff --git a/frontend/src/i18n/locales/ko.json b/frontend/src/i18n/locales/ko.json
index 9e1a38c3a..2295b067c 100644
--- a/frontend/src/i18n/locales/ko.json
+++ b/frontend/src/i18n/locales/ko.json
@@ -253,6 +253,30 @@
"useProviderDefaults": "프로바이더 기본값 사용",
"modelIdHint": "프로바이더가 요구하는 정확한 모델 ID를 사용하세요.",
"clearApiKey": "저장된 API 키 삭제",
+ "channels": {
+ "title": "IM 채널",
+ "description": "채팅 어댑터를 Web UI와 CLI가 사용하는 동일한 Vibe-Trading 세션 런타임에 연결합니다.",
+ "runtime": "런타임",
+ "running": "실행 중",
+ "stopped": "중지됨",
+ "enabled": "활성화됨",
+ "disabled": "비활성화됨",
+ "loaded": "로드됨",
+ "notLoaded": "로드되지 않음",
+ "unavailable": "사용 불가",
+ "channel": "채널",
+ "state": "상태",
+ "recovery": "복구",
+ "noRecovery": "준비됨",
+ "refresh": "새로 고침",
+ "start": "채널 시작",
+ "stop": "채널 중지",
+ "started": "IM 채널이 시작되었습니다",
+ "stoppedToast": "IM 채널이 중지되었습니다",
+ "refreshFailed": "IM 채널 새로 고침 실패",
+ "startFailed": "IM 채널 시작 실패",
+ "stopFailed": "IM 채널 중지 실패"
+ },
"generation": "생성 설정",
"dataSourceSettings": "데이터 소스 설정",
"dataSourceSettingsDesc": "백테스트와 리서치 에이전트가 사용하는 선택적 마켓 데이터 자격 증명을 구성합니다.",
diff --git a/frontend/src/i18n/locales/zh-CN.json b/frontend/src/i18n/locales/zh-CN.json
index 70871c492..6c8fbd880 100644
--- a/frontend/src/i18n/locales/zh-CN.json
+++ b/frontend/src/i18n/locales/zh-CN.json
@@ -253,6 +253,30 @@
"useProviderDefaults": "使用提供商默认设置",
"modelIdHint": "使用提供商所需的准确模型 ID。",
"clearApiKey": "清除已保存的 API 密钥",
+ "channels": {
+ "title": "IM 通道",
+ "description": "将聊天适配器接入 Web UI 和 CLI 使用的同一套 Vibe-Trading 会话运行时。",
+ "runtime": "运行时",
+ "running": "运行中",
+ "stopped": "已停止",
+ "enabled": "已启用",
+ "disabled": "已禁用",
+ "loaded": "已加载",
+ "notLoaded": "未加载",
+ "unavailable": "不可用",
+ "channel": "通道",
+ "state": "状态",
+ "recovery": "恢复方式",
+ "noRecovery": "就绪",
+ "refresh": "刷新",
+ "start": "启动通道",
+ "stop": "停止通道",
+ "started": "IM 通道已启动",
+ "stoppedToast": "IM 通道已停止",
+ "refreshFailed": "刷新 IM 通道失败",
+ "startFailed": "启动 IM 通道失败",
+ "stopFailed": "停止 IM 通道失败"
+ },
"generation": "生成设置",
"dataSourceSettings": "数据源设置",
"dataSourceSettingsDesc": "配置回测引擎和研究智能体使用的可选市场数据凭证。",
diff --git a/frontend/src/lib/api.ts b/frontend/src/lib/api.ts
index 4c0871e7d..7439a86d3 100644
--- a/frontend/src/lib/api.ts
+++ b/frontend/src/lib/api.ts
@@ -143,6 +143,14 @@ export const api = {
method: "PUT",
body: JSON.stringify(settings),
}),
+ getChannelStatus: () => request<ChannelRuntimeStatus>("/channels/status"),
+ startChannels: () => request<ChannelRuntimeActionResponse>("/channels/start", { method: "POST" }),
+ stopChannels: () => request<ChannelRuntimeActionResponse>("/channels/stop", { method: "POST" }),
+ runChannelPairingCommand: (body: ChannelPairingCommandRequest) =>
+ request<ChannelPairingCommandResponse>("/channels/pairing/command", {
+ method: "POST",
+ body: JSON.stringify(body),
+ }),
// Alpha Zoo API
listAlphas: (params: AlphaListParams = {}) => {
@@ -278,6 +286,40 @@ export interface UpdateDataSourceSettingsRequest {
clear_tushare_token?: boolean;
}
+export interface ChannelAdapterStatus {
+ name: string;
+ display_name: string;
+ configured: boolean;
+ enabled: boolean;
+ available: boolean;
+ loaded: boolean;
+ running: boolean;
+ error?: string;
+ install_hint?: string;
+}
+
+export interface ChannelRuntimeStatus {
+ running: boolean;
+ inbound_queue: number;
+ outbound_queue: number;
+ session_count: number;
+ channels: Record<string, ChannelAdapterStatus>;
+}
+
+export interface ChannelRuntimeActionResponse extends ChannelRuntimeStatus {
+ status: string;
+}
+
+export interface ChannelPairingCommandRequest {
+ channel: string;
+ command: string;
+}
+
+export interface ChannelPairingCommandResponse {
+ channel: string;
+ reply: string;
+}
+
// --- Types matching backend API contracts ---
export interface RunListItem {
diff --git a/frontend/src/pages/Settings.tsx b/frontend/src/pages/Settings.tsx
index 96e69bcd0..7b59a43c8 100644
--- a/frontend/src/pages/Settings.tsx
+++ b/frontend/src/pages/Settings.tsx
@@ -1,8 +1,9 @@
import i18n from "@/i18n";
import { useEffect, useMemo, useState, type FormEvent } from "react";
-import { Database, KeyRound, Loader2, RotateCcw, Save, Server, SlidersHorizontal } from "lucide-react";
+import { Database, KeyRound, Loader2, MessageSquareMore, Play, RefreshCw, RotateCcw, Save, Server, SlidersHorizontal, Square } from "lucide-react";
+import { useTranslation } from "react-i18next";
import { toast } from "sonner";
-import { api, isAuthRequiredError, type DataSourceSettings, type LLMProviderOption, type LLMSettings } from "@/lib/api";
+import { api, isAuthRequiredError, type ChannelRuntimeStatus, type DataSourceSettings, type LLMProviderOption, type LLMSettings } from "@/lib/api";
import { getApiAuthKey, setApiAuthKey } from "@/lib/apiAuth";
interface LLMFormState {
@@ -33,9 +34,10 @@ function toForm(settings: LLMSettings): LLMFormState {
}
export function Settings() {
-
+ const { t } = useTranslation();
const [settings, setSettings] = useState<LLMSettings | null>(null);
const [dataSettings, setDataSettings] = useState<DataSourceSettings | null>(null);
+ const [channelStatus, setChannelStatus] = useState<ChannelRuntimeStatus | null>(null);
const [form, setForm] = useState<LLMFormState | null>(null);
const [apiKey, setApiKey] = useState("");
const [localApiKey, setLocalApiKeyState] = useState(() => getApiAuthKey());
@@ -45,16 +47,19 @@ export function Settings() {
const [loading, setLoading] = useState(true);
const [saving, setSaving] = useState(false);
const [dataSaving, setDataSaving] = useState(false);
+ const [channelRefreshing, setChannelRefreshing] = useState(false);
+ const [channelAction, setChannelAction] = useState<"start" | "stop" | null>(null);
const [settingsLoadError, setSettingsLoadError] = useState<string | null>(null);
useEffect(() => {
let alive = true;
- Promise.all([api.getLLMSettings(), api.getDataSourceSettings()])
- .then(([llmData, dataSourceData]) => {
+ Promise.all([api.getLLMSettings(), api.getDataSourceSettings(), api.getChannelStatus()])
+ .then(([llmData, dataSourceData, channelData]) => {
if (!alive) return;
setSettings(llmData);
setForm(toForm(llmData));
setDataSettings(dataSourceData);
+ setChannelStatus(channelData);
setSettingsLoadError(null);
})
.catch((error) => {
@@ -73,6 +78,30 @@ export function Settings() {
return () => { alive = false; };
}, []);
+ const refreshChannelStatus = async () => {
+ setChannelRefreshing(true);
+ try {
+ setChannelStatus(await api.getChannelStatus());
+ } catch (error) {
+ toast.error(`${t("settings.channels.refreshFailed")}: ${error instanceof Error ? error.message : "Unknown error"}`);
+ } finally {
+ setChannelRefreshing(false);
+ }
+ };
+
+ const setChannelsRunning = async (action: "start" | "stop") => {
+ setChannelAction(action);
+ try {
+ const updated = action === "start" ? await api.startChannels() : await api.stopChannels();
+ setChannelStatus(updated);
+ toast.success(action === "start" ? t("settings.channels.started") : t("settings.channels.stoppedToast"));
+ } catch (error) {
+ toast.error(`${action === "start" ? t("settings.channels.startFailed") : t("settings.channels.stopFailed")}: ${error instanceof Error ? error.message : "Unknown error"}`);
+ } finally {
+ setChannelAction(null);
+ }
+ };
+
const providers = settings?.providers ?? [];
const selectedProvider = useMemo<LLMProviderOption | undefined>(
() => providers.find((provider) => provider.name === form?.provider),
@@ -182,7 +211,7 @@ export function Settings() {
</form>
);
- if (loading || !form || !settings || !dataSettings) {
+ if (loading || !form || !settings || !dataSettings || !channelStatus) {
return (
<div className="mx-auto max-w-5xl space-y-6 p-6">
<div className="space-y-2">
@@ -218,6 +247,11 @@ export function Settings() {
const tushareStatus = dataSettings.tushare_token_configured
? "Configured"
: "Leave blank to keep the current token";
+ const channelRows = Object.entries(channelStatus.channels ?? {}).sort(([a], [b]) => a.localeCompare(b));
+ const channelEnabledCount = channelRows.filter(([, item]) => item.enabled).length;
+ const channelLoadedCount = channelRows.filter(([, item]) => item.loaded).length;
+ const channelUnavailableCount = channelRows.filter(([, item]) => item.available === false).length;
+ const channelBusy = channelRefreshing || channelAction !== null;
return (
<div className="mx-auto max-w-5xl space-y-6 p-6">
@@ -228,6 +262,104 @@ export function Settings() {
{localApiAccessSection}
+ <section className="rounded-lg border bg-card p-5 shadow-sm">
+ <div className="mb-5 flex flex-col gap-4 md:flex-row md:items-start md:justify-between">
+ <div className="space-y-1">
+ <div className="flex items-center gap-2">
+ <MessageSquareMore className="h-4 w-4 text-primary" />
+ <h2 className="text-base font-semibold">{t("settings.channels.title")}</h2>
+ </div>
+ <p className="max-w-3xl text-sm text-muted-foreground">{t("settings.channels.description")}</p>
+ </div>
+ <div className="flex flex-wrap gap-2">
+ <button
+ type="button"
+ onClick={refreshChannelStatus}
+ disabled={channelBusy}
+ className="inline-flex items-center justify-center gap-2 rounded-md border px-3 py-2 text-sm text-muted-foreground transition hover:bg-muted hover:text-foreground disabled:cursor-not-allowed disabled:opacity-60"
+ >
+ {channelRefreshing ? <Loader2 className="h-4 w-4 animate-spin" /> : <RefreshCw className="h-4 w-4" />}
+ {t("settings.channels.refresh")}
+ </button>
+ <button
+ type="button"
+ onClick={() => setChannelsRunning("start")}
+ disabled={channelBusy}
+ className="inline-flex items-center justify-center gap-2 rounded-md bg-primary px-3 py-2 text-sm font-medium text-primary-foreground transition hover:opacity-90 disabled:cursor-not-allowed disabled:opacity-60"
+ >
+ {channelAction === "start" ? <Loader2 className="h-4 w-4 animate-spin" /> : <Play className="h-4 w-4" />}
+ {t("settings.channels.start")}
+ </button>
+ <button
+ type="button"
+ onClick={() => setChannelsRunning("stop")}
+ disabled={channelBusy}
+ className="inline-flex items-center justify-center gap-2 rounded-md border px-3 py-2 text-sm text-muted-foreground transition hover:bg-muted hover:text-foreground disabled:cursor-not-allowed disabled:opacity-60"
+ >
+ {channelAction === "stop" ? <Loader2 className="h-4 w-4 animate-spin" /> : <Square className="h-4 w-4" />}
+ {t("settings.channels.stop")}
+ </button>
+ </div>
+ </div>
+
+ <div className="mb-4 grid gap-3 md:grid-cols-4">
+ <div className="rounded-md border bg-muted/20 px-3 py-2">
+ <div className="text-xs text-muted-foreground">{t("settings.channels.runtime")}</div>
+ <div className="text-sm font-medium">{channelStatus.running ? t("settings.channels.running") : t("settings.channels.stopped")}</div>
+ </div>
+ <div className="rounded-md border bg-muted/20 px-3 py-2">
+ <div className="text-xs text-muted-foreground">{t("settings.channels.enabled")}</div>
+ <div className="text-sm font-medium">{channelEnabledCount}</div>
+ </div>
+ <div className="rounded-md border bg-muted/20 px-3 py-2">
+ <div className="text-xs text-muted-foreground">{t("settings.channels.loaded")}</div>
+ <div className="text-sm font-medium">{channelLoadedCount}</div>
+ </div>
+ <div className="rounded-md border bg-muted/20 px-3 py-2">
+ <div className="text-xs text-muted-foreground">{t("settings.channels.unavailable")}</div>
+ <div className="text-sm font-medium">{channelUnavailableCount}</div>
+ </div>
+ </div>
+
+ <div className="overflow-hidden rounded-md border">
+ <table className="w-full text-sm">
+ <thead className="bg-muted/40 text-xs text-muted-foreground">
+ <tr>
+ <th className="px-3 py-2 text-left font-medium">{t("settings.channels.channel")}</th>
+ <th className="px-3 py-2 text-left font-medium">{t("settings.channels.state")}</th>
+ <th className="px-3 py-2 text-left font-medium">{t("settings.channels.recovery")}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {channelRows.map(([name, item]) => (
+ <tr key={name} className="border-t">
+ <td className="px-3 py-2 align-top">
+ <div className="font-medium">{item.display_name || name}</div>
+ <div className="text-xs text-muted-foreground">{name}</div>
+ </td>
+ <td className="px-3 py-2 align-top">
+ <div className="flex flex-wrap gap-1.5">
+ <span className={`rounded-full px-2 py-0.5 text-xs ${item.enabled ? "bg-primary/10 text-primary" : "bg-muted text-muted-foreground"}`}>
+ {item.enabled ? t("settings.channels.enabled") : t("settings.channels.disabled")}
+ </span>
+ <span className={`rounded-full px-2 py-0.5 text-xs ${item.loaded ? "bg-success/10 text-success" : "bg-muted text-muted-foreground"}`}>
+ {item.loaded ? t("settings.channels.loaded") : t("settings.channels.notLoaded")}
+ </span>
+ <span className={`rounded-full px-2 py-0.5 text-xs ${item.running ? "bg-success/10 text-success" : "bg-muted text-muted-foreground"}`}>
+ {item.running ? t("settings.channels.running") : t("settings.channels.stopped")}
+ </span>
+ </div>
+ </td>
+ <td className="max-w-md px-3 py-2 align-top text-xs text-muted-foreground">
+ {item.install_hint || item.error || t("settings.channels.noRecovery")}
+ </td>
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ </section>
+
<div className="space-y-2">
<h2 className="text-lg font-semibold tracking-tight">{"LLM Settings"}</h2>
<p className="max-w-3xl text-sm text-muted-foreground">{"Choose the model used by the agent and save it to the project-local agent/.env file."}</p>
diff --git a/frontend/src/pages/__tests__/SettingsChannels.test.tsx b/frontend/src/pages/__tests__/SettingsChannels.test.tsx
new file mode 100644
index 000000000..0e385728d
--- /dev/null
+++ b/frontend/src/pages/__tests__/SettingsChannels.test.tsx
@@ -0,0 +1,131 @@
+import { fireEvent, render, screen, waitFor } from "@testing-library/react";
+import { Settings } from "../Settings";
+
+const apiMock = vi.hoisted(() => ({
+ getLLMSettings: vi.fn(),
+ getDataSourceSettings: vi.fn(),
+ getChannelStatus: vi.fn(),
+ startChannels: vi.fn(),
+ stopChannels: vi.fn(),
+ updateLLMSettings: vi.fn(),
+ updateDataSourceSettings: vi.fn(),
+}));
+
+vi.mock("@/lib/api", async () => {
+ const actual = await vi.importActual<typeof import("@/lib/api")>("@/lib/api");
+ return {
+ ...actual,
+ api: apiMock,
+ isAuthRequiredError: vi.fn(() => false),
+ };
+});
+
+vi.mock("@/lib/apiAuth", () => ({
+ getApiAuthKey: vi.fn(() => ""),
+ setApiAuthKey: vi.fn(),
+}));
+
+function llmSettings() {
+ return {
+ provider: "openrouter",
+ model_name: "deepseek/deepseek-v3.2",
+ base_url: "https://openrouter.ai/api/v1",
+ api_key_env: "OPENROUTER_API_KEY",
+ api_key_configured: false,
+ api_key_required: true,
+ temperature: 0.1,
+ timeout_seconds: 120,
+ max_retries: 2,
+ reasoning_effort: "",
+ sse_timeout_seconds: 300,
+ env_path: "agent/.env",
+ providers: [
+ {
+ name: "openrouter",
+ label: "OpenRouter",
+ api_key_env: "OPENROUTER_API_KEY",
+ base_url_env: "OPENROUTER_BASE_URL",
+ default_model: "deepseek/deepseek-v3.2",
+ default_base_url: "https://openrouter.ai/api/v1",
+ api_key_required: true,
+ auth_type: "api_key",
+ },
+ ],
+ };
+}
+
+function dataSourceSettings() {
+ return {
+ tushare_token_configured: false,
+ baostock_supported: true,
+ baostock_installed: true,
+ baostock_message: "BaoStock available",
+ env_path: "agent/.env",
+ };
+}
+
+function channelStatus(overrides = {}) {
+ return {
+ running: false,
+ inbound_queue: 0,
+ outbound_queue: 0,
+ session_count: 0,
+ channels: {
+ websocket: {
+ name: "websocket",
+ display_name: "WebSocket",
+ configured: true,
+ enabled: true,
+ available: true,
+ loaded: true,
+ running: false,
+ error: "",
+ install_hint: "",
+ },
+ telegram: {
+ name: "telegram",
+ display_name: "Telegram",
+ configured: true,
+ enabled: false,
+ available: false,
+ loaded: false,
+ running: false,
+ error: "ModuleNotFoundError",
+ install_hint: "pip install 'vibe-trading-ai[telegram]'",
+ },
+ },
+ ...overrides,
+ };
+}
+
+describe("Settings IM channels panel", () => {
+ beforeEach(() => {
+ apiMock.getLLMSettings.mockResolvedValue(llmSettings());
+ apiMock.getDataSourceSettings.mockResolvedValue(dataSourceSettings());
+ apiMock.getChannelStatus.mockResolvedValue(channelStatus());
+ apiMock.startChannels.mockResolvedValue(channelStatus({ running: true }));
+ apiMock.stopChannels.mockResolvedValue(channelStatus());
+ });
+
+ it("renders channel runtime status and refreshes it", async () => {
+ render(<Settings />);
+
+ expect(await screen.findByText("IM Channels")).toBeInTheDocument();
+ expect(screen.getByText("websocket")).toBeInTheDocument();
+ expect(screen.getByText("telegram")).toBeInTheDocument();
+ expect(screen.getByText("pip install 'vibe-trading-ai[telegram]'")).toBeInTheDocument();
+
+ fireEvent.click(screen.getByRole("button", { name: "Refresh" }));
+
+ await waitFor(() => expect(apiMock.getChannelStatus).toHaveBeenCalledTimes(2));
+ });
+
+ it("starts channels from the settings control surface", async () => {
+ render(<Settings />);
+ await screen.findByText("IM Channels");
+
+ fireEvent.click(screen.getByRole("button", { name: "Start channels" }));
+
+ await waitFor(() => expect(apiMock.startChannels).toHaveBeenCalledTimes(1));
+ });
+});
diff --git a/pyproject.toml b/pyproject.toml
index 214c21bf4..fa0acc92c 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -47,6 +47,7 @@ dependencies = [
"ccxt>=4.0.0",
"fastapi>=0.104.0",
"uvicorn[standard]>=0.24.0",
+ "websockets>=12.0",
"pydantic>=2.0.0",
"python-multipart>=0.0.18",
"sse-starlette>=1.6.0",
@@ -107,6 +108,75 @@ ashare = [
harmonic = [
"pyharmonics>=1.5.0",
]
+dingtalk = [
+ "dingtalk-stream>=0.24.0",
+]
+discord = [
+ "discord.py>=2.4.0",
+]
+feishu = [
+ "lark-oapi>=1.4.0",
+ "qrcode>=7.4.2",
+]
+matrix = [
+ "aiohttp>=3.9.0",
+ "matrix-nio[e2e]>=0.25.0",
+ "mistune>=3.0.0",
+ "nh3>=0.2.18",
+]
+mochat = [
+ "msgpack>=1.0.0",
+ "python-socketio[asyncio_client]>=5.11.0",
+]
+msteams = [
+ "cryptography>=42.0.0",
+ "PyJWT[crypto]>=2.8.0",
+]
+napcat = [
+ "aiohttp>=3.9.0",
+ "websockets>=12.0",
+]
+qq = [
+ "aiohttp>=3.9.0",
+ "qq-botpy>=1.2.0",
+]
+slack = [
+ "slack-sdk>=3.33.0",
+ "slackify-markdown>=4.4.0",
+]
+telegram = [
+ "python-telegram-bot>=21.0",
+]
+wecom = [
+ "wecom-aibot-sdk>=1.0.0",
+]
+weixin = [
+ "qrcode>=7.4.2",
+]
+whatsapp = [
+ "neonize>=0.3.18.post0",
+]
+channels = [
+ "aiohttp>=3.9.0",
+ "cryptography>=42.0.0",
+ "dingtalk-stream>=0.24.0",
+ "discord.py>=2.4.0",
+ "lark-oapi>=1.4.0",
+ "matrix-nio[e2e]>=0.25.0",
+ "mistune>=3.0.0",
+ "msgpack>=1.0.0",
+ "neonize>=0.3.18.post0",
+ "nh3>=0.2.18",
+ "PyJWT[crypto]>=2.8.0",
+ "python-socketio[asyncio_client]>=5.11.0",
+ "python-telegram-bot>=21.0",
+ "qrcode>=7.4.2",
+ "qq-botpy>=1.2.0",
+ "slack-sdk>=3.33.0",
+ "slackify-markdown>=4.4.0",
+ "websockets>=12.0",
+ "wecom-aibot-sdk>=1.0.0",
+]
dev = [
"pytest>=7.0",
"pytest-cov>=4.0",