Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(im): route workbench delivery through multi client

cyh8173115f138d
diff --git a/core/controller.py b/core/controller.py
index d47db6bd6..9b1fcf698 100644
--- a/core/controller.py
+++ b/core/controller.py
@@ -248,12 +248,16 @@ class Controller:
for platform, client in runtime_clients.items():
client.formatter = self._create_formatter(platform)
self.primary_platform = self._derive_primary_platform(self.config)
- self.im_client = MultiIMClient(dict(runtime_clients), primary_platform=self.primary_platform)
self.im_clients = dict(runtime_clients)
from modules.im.avibe import AvibeBot, AvibeConfig
self.im_clients["avibe"] = AvibeBot(AvibeConfig())
+ self.im_client = MultiIMClient(
+ dict(runtime_clients),
+ primary_platform=self.primary_platform,
+ auxiliary_clients={"avibe": self.im_clients["avibe"]},
+ )
self._removed_im_clients = {}
formatter = self.im_clients.get(self.primary_platform, self.im_clients["avibe"]).formatter
self.claude_client = ClaudeClient(self.config.claude, formatter)
diff --git a/modules/im/multi.py b/modules/im/multi.py
index e7d06629f..fe2e42a40 100644
--- a/modules/im/multi.py
+++ b/modules/im/multi.py
@@ -22,10 +22,16 @@ class IMClientRemovalError(RuntimeError):
class MultiIMClient(BaseIMClient):
"""Delegate inbound/outbound messaging across multiple IM clients."""
- def __init__(self, clients: Dict[str, BaseIMClient], primary_platform: str):
+ def __init__(
+ self,
+ clients: Dict[str, BaseIMClient],
+ primary_platform: str,
+ auxiliary_clients: Optional[Dict[str, BaseIMClient]] = None,
+ ):
if clients and primary_platform not in clients:
raise ValueError(f"Primary platform '{primary_platform}' is not in enabled clients")
self.clients = clients
+ self._auxiliary_clients = auxiliary_clients or {}
self.primary_platform = primary_platform
self._threads: Dict[str, threading.Thread] = {}
self._run_exceptions: Dict[str, BaseException] = {}
@@ -65,6 +71,11 @@ class MultiIMClient(BaseIMClient):
with self._clients_lock:
return dict(self.clients)
+ def set_auxiliary_client(self, platform: str, client: BaseIMClient) -> None:
+ """Register a delivery-only client that is not part of the run loop."""
+ with self._clients_lock:
+ self._auxiliary_clients[platform] = client
+
def _primary_client(self) -> BaseIMClient:
with self._clients_lock:
client = self.clients.get(self.primary_platform)
@@ -92,11 +103,15 @@ class MultiIMClient(BaseIMClient):
return context.platform
ps = context.platform_specific or {}
platform = ps.get("platform")
- if isinstance(platform, str) and platform in self.clients:
+ if isinstance(platform, str) and (platform in self.clients or platform in self._auxiliary_clients):
return platform
return self.primary_platform
def get_client(self, platform: str) -> BaseIMClient:
+ with self._clients_lock:
+ client = self.clients.get(platform) or self._auxiliary_clients.get(platform)
+ if client is not None:
+ return client
try:
return self.clients[platform]
except KeyError as exc:
@@ -121,6 +136,9 @@ class MultiIMClient(BaseIMClient):
def get_client_for_context(self, context: Optional[MessageContext] = None) -> BaseIMClient:
return self.get_client(self._resolve_platform(context))
+ def supports_question_modal(self, context: Optional[MessageContext] = None) -> bool:
+ return callable(getattr(self.get_client_for_context(context), "open_question_modal", None))
+
def get_default_parse_mode(self) -> Optional[str]:
try:
return self._primary_client().get_default_parse_mode()
@@ -390,7 +408,14 @@ class MultiIMClient(BaseIMClient):
pending: Any,
callback_prefix: str = "claude_question",
):
- return await self.get_client_for_context(context).open_question_modal(
+ client = self.get_client_for_context(context)
+ open_modal = getattr(client, "open_question_modal", None)
+ if not callable(open_modal):
+ return await self.send_message(
+ context,
+ "Modal UI is not available. Please reply with a custom message.",
+ )
+ return await open_modal(
trigger_id=trigger_id,
context=context,
pending=pending,
@@ -563,13 +588,13 @@ class MultiIMClient(BaseIMClient):
async def get_user_info(self, user_id: str) -> Dict[str, Any]:
platform, raw_user_id = _split_scoped_key(str(user_id))
platform = platform or _infer_user_platform(user_id)
- client = self.clients.get(platform) or self._primary_client()
+ client = self.clients.get(platform) or self._auxiliary_clients.get(platform) or self._primary_client()
return await client.get_user_info(raw_user_id)
async def get_channel_info(self, channel_id: str) -> Dict[str, Any]:
platform, raw_channel_id = _split_scoped_key(str(channel_id))
platform = platform or _infer_channel_platform(channel_id)
- client = self.clients.get(platform) or self._primary_client()
+ client = self.clients.get(platform) or self._auxiliary_clients.get(platform) or self._primary_client()
return await client.get_channel_info(raw_channel_id)
async def add_reaction(self, context: MessageContext, message_id: str, emoji: str) -> bool:
@@ -593,7 +618,7 @@ class MultiIMClient(BaseIMClient):
async def send_dm(self, user_id: str, text: str, **kwargs):
platform, raw_user_id = _split_scoped_key(str(user_id))
platform = platform or _infer_user_platform(user_id)
- client = self.clients.get(platform) or self._primary_client()
+ client = self.clients.get(platform) or self._auxiliary_clients.get(platform) or self._primary_client()
return await client.send_dm(raw_user_id, text, **kwargs)
def stop(self):
diff --git a/tests/test_controller_platform_reconcile.py b/tests/test_controller_platform_reconcile.py
index 57b8bb821..04357e9cf 100644
--- a/tests/test_controller_platform_reconcile.py
+++ b/tests/test_controller_platform_reconcile.py
@@ -147,9 +147,13 @@ def _controller(config):
clients = {platform: _Client(platform, getattr(config, platform)) for platform in controller.enabled_platforms}
for platform, client in clients.items():
client.formatter = _Formatter()
- controller.im_client = MultiIMClient(dict(clients), controller.primary_platform)
controller.im_clients = dict(clients)
controller.im_clients["avibe"] = _Client("avibe")
+ controller.im_client = MultiIMClient(
+ dict(clients),
+ controller.primary_platform,
+ auxiliary_clients={"avibe": controller.im_clients["avibe"]},
+ )
controller._removed_im_clients = {}
controller.settings_manager = MultiSettingsManager(
Controller._settings_platforms_for(controller.enabled_platforms, controller.primary_platform),
@@ -307,3 +311,31 @@ def test_reconcile_disable_all_keeps_empty_multi_runtime(monkeypatch):
assert controller.im_client.clients == {}
assert "avibe" in controller.im_clients
assert controller.get_im_client_for_context(MessageContext(user_id="u", channel_id="c", platform="avibe")).name == "avibe"
+
+
+def test_reconcile_disable_all_keeps_multi_runtime_avibe_delivery(monkeypatch):
+ monkeypatch.setattr("core.controller.get_platform_descriptor", lambda platform: _Descriptor(platform))
+ controller = _controller(_config(["slack"]))
+ avibe = controller.im_clients["avibe"]
+ deleted: list[tuple[str, str]] = []
+ reactions: list[tuple[str, str, str]] = []
+
+ async def _delete_message(context, message_id):
+ deleted.append((context.platform, message_id))
+ return True
+
+ async def _remove_reaction(context, message_id, emoji):
+ reactions.append((context.platform, message_id, emoji))
+ return True
+
+ avibe.delete_message = _delete_message
+ avibe.remove_reaction = _remove_reaction
+
+ asyncio.run(controller.reconcile_platforms(_config([])))
+ context = MessageContext(user_id="u", channel_id="c", platform="avibe")
+
+ assert controller.im_client.get_client_for_context(context) is avibe
+ assert asyncio.run(controller.im_client.delete_message(context, "ack-1")) is True
+ assert asyncio.run(controller.im_client.remove_reaction(context, "ack-1", "eyes")) is True
+ assert deleted == [("avibe", "ack-1")]
+ assert reactions == [("avibe", "ack-1", "eyes")]
diff --git a/tests/test_multi_platform_runtime.py b/tests/test_multi_platform_runtime.py
index 71ee901a2..5cd89219b 100644
--- a/tests/test_multi_platform_runtime.py
+++ b/tests/test_multi_platform_runtime.py
@@ -111,6 +111,10 @@ class _StubClient(BaseIMClient):
return text
+class _ModalLessClient(_StubClient):
+ open_question_modal = None
+
+
class _SlowStopClient(_StubClient):
def __init__(self, name: str):
super().__init__(name, run_until_stopped=True)
@@ -181,6 +185,18 @@ def test_multi_im_client_delegates_question_modal_by_context_platform():
assert discord.question_modals == [("trigger-1", "discord", pending, "test_question")]
+def test_multi_im_client_question_modal_falls_back_for_modal_less_platform():
+ wechat = _ModalLessClient("wechat")
+ client = MultiIMClient({"wechat": wechat}, primary_platform="wechat")
+ context = MessageContext(user_id="u", channel_id="c", platform="wechat")
+
+ result = asyncio.run(client.open_question_modal("trigger-1", context, {"questions": []}, "test_question"))
+
+ assert wechat.question_modals == []
+ assert result == "wechat"
+ assert wechat.sent == [("wechat", "c", "Modal UI is not available. Please reply with a custom message.")]
+
+
def test_multi_im_client_add_client_registers_callbacks_before_start():
client = MultiIMClient({}, primary_platform="avibe")
added = _StubClient("slack")
diff --git a/tests/test_workbench_only_runtime.py b/tests/test_workbench_only_runtime.py
index ff4f99586..ff1f8c2f3 100644
--- a/tests/test_workbench_only_runtime.py
+++ b/tests/test_workbench_only_runtime.py
@@ -148,6 +148,31 @@ def test_init_modules_uses_empty_multi_runtime_for_workbench_only():
assert "avibe" in controller.agent_router.platform_routes
+def test_workbench_only_multi_runtime_routes_avibe_cleanup_to_avibe_client():
+ controller = _init_controller_modules(_workbench_only_app_config())
+ avibe = controller.im_clients["avibe"]
+ deleted: list[tuple[str, str]] = []
+ reactions: list[tuple[str, str, str]] = []
+
+ async def _delete_message(context, message_id):
+ deleted.append((context.platform, message_id))
+ return True
+
+ async def _remove_reaction(context, message_id, emoji):
+ reactions.append((context.platform, message_id, emoji))
+ return True
+
+ avibe.delete_message = _delete_message
+ avibe.remove_reaction = _remove_reaction
+ context = MessageContext(user_id="u", channel_id="c", platform="avibe")
+
+ assert controller.im_client.get_client_for_context(context) is avibe
+ assert asyncio.run(controller.im_client.delete_message(context, "ack-1")) is True
+ assert asyncio.run(controller.im_client.remove_reaction(context, "ack-1", "eyes")) is True
+ assert deleted == [("avibe", "ack-1")]
+ assert reactions == [("avibe", "ack-1", "eyes")]
+
+
def test_init_modules_boots_single_platform_with_multi_runtime(monkeypatch):
app_config = _boot_app_config(["slack"])
monkeypatch.setattr("core.controller.IMFactory.create_clients", lambda config: {"slack": _BootClient("slack")})