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.

refactor(dm): unified DM detection with is_dm flag and Feishu DM chat registry

cyh2ddb1f14e534
diff --git a/config/v2_settings.py b/config/v2_settings.py
index 8c5eaa40a..771f0b8cd 100644
--- a/config/v2_settings.py
+++ b/config/v2_settings.py
@@ -76,6 +76,7 @@ class UserSettings:
show_message_types: List[str] = field(default_factory=lambda: DEFAULT_SHOW_MESSAGE_TYPES.copy())
custom_cwd: Optional[str] = None
routing: RoutingSettings = field(default_factory=RoutingSettings)
+ dm_chat_id: str = ""
@dataclass
@@ -214,6 +215,7 @@ class SettingsStore:
show_message_types=normalize_show_message_types(up.get("show_message_types")),
custom_cwd=up.get("custom_cwd"),
routing=_parse_routing(up.get("routing") or {}),
+ dm_chat_id=up.get("dm_chat_id", ""),
)
# --- Bind Codes ---
@@ -264,6 +266,7 @@ class SettingsStore:
"show_message_types": u.show_message_types,
"custom_cwd": u.custom_cwd,
"routing": _routing_to_dict(u.routing),
+ "dm_chat_id": u.dm_chat_id,
}
# Bind codes
@@ -329,7 +332,9 @@ class SettingsStore:
self.save()
return user
- def bind_user_with_code(self, user_id: str, display_name: str, code: str) -> Tuple[bool, bool]:
+ def bind_user_with_code(
+ self, user_id: str, display_name: str, code: str, dm_chat_id: str = ""
+ ) -> Tuple[bool, bool]:
"""Atomically validate code, create user, and consume code.
Returns (success, is_admin).
@@ -358,6 +363,7 @@ class SettingsStore:
is_admin=is_admin,
bound_at=_now_iso(),
enabled=True,
+ dm_chat_id=dm_chat_id,
)
self.settings.users[user_id] = user
diff --git a/core/handlers/command_handlers.py b/core/handlers/command_handlers.py
index 8a861a62b..a9ac268eb 100644
--- a/core/handlers/command_handlers.py
+++ b/core/handlers/command_handlers.py
@@ -453,7 +453,9 @@ class CommandHandlers:
)
# Atomic bind: validate code + create user + consume code in one operation
- success, is_admin = store.bind_user_with_code(context.user_id, display_name, code)
+ success, is_admin = store.bind_user_with_code(
+ context.user_id, display_name, code, dm_chat_id=context.channel_id
+ )
if not success:
# Could be already bound (race) or invalid code
diff --git a/modules/im/feishu.py b/modules/im/feishu.py
index de999b01b..561303afa 100644
--- a/modules/im/feishu.py
+++ b/modules/im/feishu.py
@@ -101,6 +101,7 @@ class FeishuBot(BaseIMClient):
self._cached_token: Optional[str] = None
self._token_expires_at: Optional[float] = None
self._user_info_cache: Dict[str, Dict[str, Any]] = {}
+ self._dm_chat_ids: set = set()
# ------------------------------------------------------------------
# Lifecycle / injection
@@ -113,6 +114,13 @@ class FeishuBot(BaseIMClient):
"""Set the controller reference for handling update button clicks."""
self._controller = controller
+ def _populate_dm_chat_ids(self):
+ """Pre-populate DM chat IDs from bound users for restart survival."""
+ if self.settings_manager:
+ for user_id, user in self.settings_manager.store.settings.users.items():
+ if user.dm_chat_id:
+ self._dm_chat_ids.add(user.dm_chat_id)
+
# ------------------------------------------------------------------
# i18n helpers
# ------------------------------------------------------------------
@@ -1068,6 +1076,8 @@ class FeishuBot(BaseIMClient):
# Require-mention logic (bypass for p2p/DM chats)
chat_type = message.get("chat_type", "")
is_p2p = chat_type == "p2p"
+ if is_p2p and chat_id:
+ self._dm_chat_ids.add(chat_id)
is_thread_reply = bool(root_id)
effective_require_mention = self.config.require_mention
if self.settings_manager:
@@ -1215,24 +1225,13 @@ class FeishuBot(BaseIMClient):
return
# --- Channel authorization ---
- # Feishu card actions don't carry chat_type, so we need heuristics
- # to distinguish DM vs channel contexts.
- is_bound_user = False
- is_dm = False
- if self.settings_manager and user_id:
- store = self.settings_manager.store
- is_bound_user = store.is_bound_user(user_id)
- # If chat_id is NOT a known authorized channel but user is bound,
- # this is likely a DM context
- if is_bound_user and chat_id:
- is_known_channel = chat_id in store.settings.channels
- if not is_known_channel:
- is_dm = True
- if not is_bound_user or not is_dm:
+ # Use positive DM lookup instead of fragile heuristics.
+ # _dm_chat_ids is populated from real p2p messages and persisted
+ # user settings, so it reliably identifies DM chats.
+ is_dm = chat_id in self._dm_chat_ids
+ if not is_dm:
if not chat_id or not await self._is_authorized_channel(chat_id):
- logger.info(
- "Card action from unauthorized/unknown channel %s (bound=%s), ignoring", chat_id, is_bound_user
- )
+ logger.info("Card action from unauthorized/unknown channel %s, ignoring", chat_id)
return
context = MessageContext(
@@ -2608,6 +2607,9 @@ class FeishuBot(BaseIMClient):
# Fetch bot info
await self._fetch_bot_info()
+ # Pre-populate DM chat IDs from persisted user settings
+ self._populate_dm_chat_ids()
+
# Start WebSocket client in a background thread
self._ws_client = lark.ws.Client(
app_id=self.config.app_id,
diff --git a/modules/im/slack.py b/modules/im/slack.py
index 386a9baf9..0075b90fb 100644
--- a/modules/im/slack.py
+++ b/modules/im/slack.py
@@ -937,7 +937,11 @@ class SlackBot(BaseIMClient):
channel_id=channel_id,
thread_id=thread_id, # Always have a thread_id
message_id=event.get("ts"),
- platform_specific={"team_id": payload.get("team_id"), "event": event},
+ platform_specific={
+ "team_id": payload.get("team_id"),
+ "event": event,
+ "is_dm": channel_id.startswith("D"),
+ },
files=file_attachments,
)
@@ -986,7 +990,11 @@ class SlackBot(BaseIMClient):
channel_id=channel_id,
thread_id=thread_id, # Always have a thread_id
message_id=event.get("ts"),
- platform_specific={"team_id": payload.get("team_id"), "event": event},
+ platform_specific={
+ "team_id": payload.get("team_id"),
+ "event": event,
+ "is_dm": channel_id.startswith("D"),
+ },
files=file_attachments,
)
@@ -1083,6 +1091,7 @@ class SlackBot(BaseIMClient):
"command": command,
"text": payload.get("text"),
"payload": payload,
+ "is_dm": is_dm,
},
)
@@ -1193,6 +1202,7 @@ class SlackBot(BaseIMClient):
"response_url": payload.get("response_url"),
"action": action,
"payload": payload,
+ "is_dm": is_dm,
},
)
@@ -1387,7 +1397,10 @@ class SlackBot(BaseIMClient):
user_id=user_id,
channel_id=str(channel_id) if channel_id else "",
thread_id=str(thread_id) if thread_id else None,
- platform_specific={"payload": payload},
+ platform_specific={
+ "payload": payload,
+ "is_dm": isinstance(channel_id, str) and channel_id.startswith("D"),
+ },
)
# Use callback_prefix to route to correct agent
await self.on_callback_query_callback(