Merge remote-tracking branch 'origin/v3' into v3

# Conflicts:
#	app/api/endpoints/agent.py
#	app/api/endpoints/anthropic.py
#	app/api/endpoints/openai.py
#	app/chain/__init__.py
#	app/chain/message.py
#	app/chain/site.py
#	app/chain/subscribe.py
#	app/chain/transfer.py
#	app/modules/discord/__init__.py
#	app/modules/qqbot/__init__.py
#	app/modules/slack/__init__.py
#	app/modules/telegram/__init__.py
#	app/modules/wechat/__init__.py
#	app/runtime/extensions/module_manager.py
#	app/runtime/extensions/service_registry.py
#	tests/test_agent_interaction.py
#	tests/test_slash_command_interactions.py
#	tests/test_web_agent_stream.py
This commit is contained in:
jxxghp
2026-08-16 19:44:43 +08:00
232 changed files with 21375 additions and 6683 deletions
+5 -96
View File
@@ -1,17 +1,15 @@
import copy
import json
from typing import Any, Dict, List, Optional, Tuple, Union
from urllib.parse import quote, unquote
from app.domain.context import MediaInfo, Context
from app.runtime.events import eventmanager
from app.application.messaging.agent import (
matches_channel_admin,
register_channel_admin_resolver,
resolve_config_principal_ids,
)
from app.runtime.log import logger
from app.modules import _ModuleBase, _MessageBase
from app.modules._base import _MessageChannelModuleBase
from app.schemas import (
CommandRegisterEventData,
IncomingMessage,
@@ -19,9 +17,8 @@ from app.schemas import (
MessageResponse,
Message,
)
from app.schemas.types import ChainEventType, ModuleType
from app.schemas.types import ModuleType
from app.adapters.network.http import RequestUtils
from app.foundation.collections import DictUtils
try:
from app.modules.discord.discord import Discord
@@ -36,7 +33,9 @@ register_channel_admin_resolver(
)
class DiscordModule(_ModuleBase, _MessageBase[Discord]):
class DiscordModule(_MessageChannelModuleBase[Discord]):
# 管理员配置键,与渠道 resolver 保持一致
_admin_config_key = "DISCORD_ADMINS"
_IMAGE_SUFFIXES = (
".png",
".jpg",
@@ -107,51 +106,9 @@ class DiscordModule(_ModuleBase, _MessageBase[Discord]):
except Exception as err:
logger.error(f"停止Discord模块实例失败:{err}")
def test(self) -> Optional[Tuple[bool, str]]:
"""
测试模块连接性
"""
if not self.get_instances():
return None
for name, client in self.get_instances().items():
state = client.get_state()
if not state:
return False, f"Discord {name} Bot 未就绪"
return True, ""
def init_setting(self) -> Tuple[str, Union[str, bool]]:
pass
@staticmethod
def _get_admins(config: Optional[dict]) -> List[str]:
"""
解析 Discord 管理员配置,兼容逗号分隔和首尾空白。
"""
return [
admin.strip()
for admin in str((config or {}).get("DISCORD_ADMINS") or "").split(",")
if admin.strip()
]
@classmethod
def _should_reject_admin_command(
cls,
config: Optional[dict],
*user_ids: Optional[Union[str, int]],
) -> bool:
"""
判断 Discord 命令或命令型按钮回调是否应因非管理员身份被拒绝。
"""
admins = cls._get_admins(config)
if not admins:
return False
candidates = [
str(user_id).strip()
for user_id in user_ids
if user_id is not None and str(user_id).strip()
]
return not any(candidate in admins for candidate in candidates)
@staticmethod
def _send_admin_denied(
client: Optional[Discord],
@@ -556,54 +513,6 @@ class DiscordModule(_ModuleBase, _MessageBase[Discord]):
return True
return False
def register_commands(self, commands: Dict[str, dict]) -> None:
"""
注册命令,实现这个函数接收系统可用的命令菜单。
:param commands: 命令字典
"""
for client_config in self.get_configs().values():
client = self.get_instance(client_config.name)
if not client:
continue
scoped_commands = copy.deepcopy(commands)
event = eventmanager.send_event(
ChainEventType.CommandRegister,
CommandRegisterEventData(
commands=scoped_commands,
origin="Discord",
service=client_config.name,
),
)
if event and event.event_data:
event_data: CommandRegisterEventData = event.event_data
if event_data.cancel:
client.delete_commands()
logger.debug(
f"Command registration for {client_config.name} canceled by event: {event_data.source}"
)
continue
scoped_commands = event_data.commands or {}
if not scoped_commands:
logger.debug("Filtered commands are empty, skipping registration.")
client.delete_commands()
filtered_scoped_commands = DictUtils.filter_keys_to_subset(
scoped_commands,
commands,
)
if not filtered_scoped_commands:
logger.debug("Filtered commands are empty, skipping registration.")
client.delete_commands()
continue
if filtered_scoped_commands != commands:
logger.debug(
f"Command set has changed, Updating new commands: {filtered_scoped_commands}"
)
client.register_commands(filtered_scoped_commands)
def mark_message_processing_started(
self,
channel: NotificationChannel,
+22
View File
@@ -0,0 +1,22 @@
schema_version = 1
id = "DiscordModule"
kind = "host_module"
entrypoint = "app.modules.discord:DiscordModule"
depends_on = []
[metadata]
name = "Discord"
type = "notification"
subtype = "Discord"
priority = 4
[activation]
policy = "when_configured"
watch = ["Notifications"]
[activation.selector]
kind = "system_config_item"
key = "Notifications"
match_field = "type"
match_value = "discord"
enabled_field = "enabled"