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(admin): prevent DM user settings shadowing and fix callback settings_key

cyh5afcbb20eeb0
diff --git a/core/controller.py b/core/controller.py
index 79f6e4bb0..8a250bdc9 100644
--- a/core/controller.py
+++ b/core/controller.py
@@ -386,24 +386,50 @@ class Controller:
if platform == "slack" and context.channel_id.startswith("D"):
return context.user_id
if platform == "discord":
- # Check if this is a DM by looking at platform_specific or channel type
- ps = context.platform_specific or {}
- msg = ps.get("message")
- if msg is not None:
- try:
- import discord
-
- if isinstance(msg.channel, discord.DMChannel) or msg.guild is None:
- return context.user_id
- except Exception:
- pass
+ if self._is_discord_dm(context):
+ return context.user_id
if platform == "lark":
- ps = context.platform_specific or {}
- event = ps.get("event", {})
- if isinstance(event, dict) and event.get("message", {}).get("chat_type") == "p2p":
+ if self._is_lark_dm(context):
return context.user_id
return context.channel_id
+ def _is_discord_dm(self, context: MessageContext) -> bool:
+ """Detect Discord DM from message or interaction context."""
+ ps = context.platform_specific or {}
+ # Explicit is_dm flag set by IM layer (works for all context types)
+ if ps.get("is_dm"):
+ return True
+ try:
+ import discord as discord_lib
+
+ # Message context (normal messages)
+ msg = ps.get("message")
+ if msg is not None:
+ if isinstance(msg.channel, discord_lib.DMChannel) or msg.guild is None:
+ return True
+ # Interaction context (button/slash callbacks)
+ interaction = ps.get("interaction")
+ if interaction is not None:
+ if interaction.guild is None or isinstance(interaction.channel, discord_lib.DMChannel):
+ return True
+ except Exception:
+ pass
+ return False
+
+ def _is_lark_dm(self, context: MessageContext) -> bool:
+ """Detect Lark/Feishu DM from message or callback event context."""
+ ps = context.platform_specific or {}
+ # Explicit is_dm flag set by IM layer (works for all context types)
+ if ps.get("is_dm"):
+ return True
+ event = ps.get("event", {})
+ if isinstance(event, dict):
+ # Message path: event.message.chat_type == "p2p"
+ chat_type = event.get("message", {}).get("chat_type")
+ if chat_type == "p2p":
+ return True
+ return False
+
def _get_target_context(self, context: MessageContext) -> MessageContext:
"""Get target context for sending messages"""
if self.im_client.should_use_thread_for_reply() and context.thread_id:
diff --git a/modules/im/discord.py b/modules/im/discord.py
index c1e3cb783..7364a76fd 100644
--- a/modules/im/discord.py
+++ b/modules/im/discord.py
@@ -506,7 +506,7 @@ class DiscordBot(BaseIMClient):
channel_id=channel_id,
thread_id=thread_id,
message_id=str(message.id),
- platform_specific={"message": message},
+ platform_specific={"message": message, "is_dm": is_dm},
files=files,
)
parts = content.split(maxsplit=1)
@@ -524,7 +524,7 @@ class DiscordBot(BaseIMClient):
channel_id=channel_id,
thread_id=thread_id,
message_id=str(message.id),
- platform_specific={"message": message},
+ platform_specific={"message": message, "is_dm": is_dm},
files=files,
)
await self.on_message_callback(context, "")
@@ -541,7 +541,7 @@ class DiscordBot(BaseIMClient):
channel_id=channel_id,
thread_id=thread_id,
message_id=str(message.id),
- platform_specific={"message": message},
+ platform_specific={"message": message, "is_dm": is_dm},
files=files,
)
@@ -1393,7 +1393,10 @@ class DiscordBot(BaseIMClient):
channel_id=context.channel_id,
thread_id=context.thread_id,
message_id=context.message_id,
- platform_specific={"interaction": submit_interaction},
+ platform_specific={
+ "interaction": submit_interaction,
+ "is_dm": (context.platform_specific or {}).get("is_dm", False),
+ },
)
await self.on_callback_query_callback(ctx, callback_data)
await submit_interaction.response.edit_message(content="✅ Answer submitted.", view=None)
@@ -1502,7 +1505,7 @@ class _PersistentStartView(discord.ui.View):
channel_id=channel_id,
thread_id=thread_id,
message_id=str(interaction.message.id) if interaction.message else None,
- platform_specific={"interaction": interaction},
+ platform_specific={"interaction": interaction, "is_dm": is_dm},
)
if self.outer.on_callback_query_callback:
await self.outer.on_callback_query_callback(context, data)
@@ -1585,7 +1588,7 @@ class _DiscordButtonView(discord.ui.View):
thread_id=thread_id,
message_id=self.base_context.message_id
or (str(interaction.message.id) if interaction.message else None),
- platform_specific={"interaction": interaction},
+ platform_specific={"interaction": interaction, "is_dm": is_dm},
)
if self.outer.on_callback_query_callback:
await self.outer.on_callback_query_callback(context, data)
diff --git a/modules/im/feishu.py b/modules/im/feishu.py
index 04f7b723b..aa0abb710 100644
--- a/modules/im/feishu.py
+++ b/modules/im/feishu.py
@@ -1131,6 +1131,7 @@ class FeishuBot(BaseIMClient):
"event": event_data,
"msg_type": msg_type,
"mentions": mentions,
+ "is_dm": is_p2p,
},
files=file_attachments,
)
@@ -1235,6 +1236,7 @@ class FeishuBot(BaseIMClient):
platform_specific={
"event": event_data,
"action": action,
+ "is_dm": is_bound_dm,
# Provide trigger_id so question_handler.open_question_modal()
# doesn't abort with "Modal UI is not available".
# Feishu doesn't use a real trigger_id (we send cards, not
diff --git a/modules/settings_manager.py b/modules/settings_manager.py
index 1f25582f0..9e54904eb 100644
--- a/modules/settings_manager.py
+++ b/modules/settings_manager.py
@@ -177,6 +177,26 @@ class SettingsManager:
),
)
+ def _sync_to_bound_user(self, user_id: str, settings: UserSettings) -> None:
+ """Sync runtime UserSettings back to the bound-user record in store.settings.users."""
+ bound = self.store.settings.users.get(user_id)
+ if not bound:
+ return
+ routing = settings.channel_routing or ChannelRouting()
+ bound.enabled = settings.enabled
+ bound.show_message_types = self._normalize_show_message_types(settings.show_message_types)
+ bound.custom_cwd = settings.custom_cwd
+ bound.routing = RoutingSettings(
+ agent_backend=routing.agent_backend,
+ opencode_agent=routing.opencode_agent,
+ opencode_model=routing.opencode_model,
+ opencode_reasoning_effort=routing.opencode_reasoning_effort,
+ claude_agent=routing.claude_agent,
+ claude_model=routing.claude_model,
+ codex_model=routing.codex_model,
+ codex_reasoning_effort=routing.codex_reasoning_effort,
+ )
+
def _load_settings(self):
"""Load settings from JSON file"""
self.store = SettingsStore(self.settings_file)
@@ -226,18 +246,33 @@ class SettingsManager:
else:
self._settings_mtime_ns = mtime_ns
+ def _is_bound_user_key(self, key: str) -> bool:
+ """Check if a settings key belongs to a bound DM user (not a channel)."""
+ return key in self.store.settings.users
+
def _save_settings(self):
- """Save settings to JSON file"""
+ """Save settings to JSON file.
+
+ Only writes channel-keyed entries back to ``store.settings.channels``.
+ Bound DM user entries (keyed by user_id present in ``store.settings.users``)
+ are synced back to ``store.settings.users`` instead, preventing them from
+ shadowing the authoritative ``users`` dict on subsequent reloads.
+ """
try:
channels: Dict[str, ChannelSettings] = {}
for settings_key, settings in self.settings.items():
- existing = self.store.settings.channels.get(str(settings_key))
+ sk = str(settings_key)
+ if self._is_bound_user_key(sk):
+ # Sync runtime changes back to the bound-user record
+ self._sync_to_bound_user(sk, settings)
+ continue
+ existing = self.store.settings.channels.get(sk)
channel_settings = self._to_channel_settings(settings)
if existing is not None:
channel_settings.enabled = existing.enabled
# Preserve require_mention setting (it's managed separately)
channel_settings.require_mention = existing.require_mention
- channels[str(settings_key)] = channel_settings
+ channels[sk] = channel_settings
self.store.settings.channels = channels
self.store.save()
try: