Compare commits

...

11 Commits

Author SHA1 Message Date
Soulter 8abaf1015d chore: bump version to 4.17.0 2026-02-15 21:51:00 +08:00
Soulter 9a0c814fd4 feat: add SSL configuration options for WebUI and update related logging (#5117) 2026-02-15 17:43:36 +08:00
Soulter c64e1b42a4 feat: replace colorlog with loguru for enhanced logging support (#5115) 2026-02-15 17:11:03 +08:00
Soulter 2d23c36067 feat: add Afdian support card to resources section in WelcomePage 2026-02-15 16:20:34 +08:00
Soulter 754144ad99 feat: add fallback chat model chain in tool loop runner (#5109)
* feat: implement fallback provider support for chat models and update configuration

* feat: enhance provider selection display with count and chips for selected providers

* feat: update fallback chat providers to use provider settings and add warning for non-list fallback models
2026-02-15 11:51:34 +08:00
Waterwzy 0faf109c2a feat: support hot reload after plugin load failure (#5043)
* add :Support hot reload after plugin load failure

* Apply suggestions from code review

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>

* fix:reformat code

* fix:reformat code

---------

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
2026-02-13 18:37:20 +08:00
evpeople 7d1eff3ec4 fix #5089: add uv lock step in Dockerfile before export (#5091)
Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com>
2026-02-13 18:34:26 +08:00
Soulter e295c470a5 fix: remove unnecessary frozen flag from requirements export in Dockerfile
fixes: #5089
2026-02-13 18:09:49 +08:00
Li-shi-ling 935168c024 fix-correct-FIRST_NOTICE.md-locale-path-resolution (#5083) (#5082)
* fix:修改配置文件目录

* fix:添加备选的FIRST_NOTICE.zh-CN.md用于兼容
2026-02-13 13:15:08 +08:00
Soulter f44961d065 feat: add LINE platform support with adapter and configuration (#5085) 2026-02-13 13:01:48 +08:00
Soulter 0c7a95ccd8 chore: bump version to 4.16.0 (#5074) 2026-02-12 22:55:42 +08:00
35 changed files with 2020 additions and 352 deletions
+1
View File
@@ -22,6 +22,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
RUN python -m pip install uv \
&& echo "3.12" > .python-version \
&& uv lock \
&& uv export --format requirements.txt --output-file requirements.txt --frozen \
&& uv pip install -r requirements.txt --no-cache-dir --system \
&& uv pip install socksio uv pilk --no-cache-dir --system
+1 -1
View File
@@ -1 +1 @@
__version__ = "4.15.0"
__version__ = "4.17.0"
@@ -91,6 +91,7 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
custom_token_counter: TokenCounter | None = None,
custom_compressor: ContextCompressor | None = None,
tool_schema_mode: str | None = "full",
fallback_providers: list[Provider] | None = None,
**kwargs: T.Any,
) -> None:
self.req = request
@@ -120,6 +121,17 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
self.context_manager = ContextManager(self.context_config)
self.provider = provider
self.fallback_providers: list[Provider] = []
seen_provider_ids: set[str] = {str(provider.provider_config.get("id", ""))}
for fallback_provider in fallback_providers or []:
fallback_id = str(fallback_provider.provider_config.get("id", ""))
if fallback_provider is provider:
continue
if fallback_id and fallback_id in seen_provider_ids:
continue
self.fallback_providers.append(fallback_provider)
if fallback_id:
seen_provider_ids.add(fallback_id)
self.final_llm_resp = None
self._state = AgentState.IDLE
self.tool_executor = tool_executor
@@ -166,16 +178,19 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
self.stats = AgentStats()
self.stats.start_time = time.time()
async def _iter_llm_responses(self) -> T.AsyncGenerator[LLMResponse, None]:
async def _iter_llm_responses(
self, *, include_model: bool = True
) -> T.AsyncGenerator[LLMResponse, None]:
"""Yields chunks *and* a final LLMResponse."""
payload = {
"contexts": self.run_context.messages, # list[Message]
"func_tool": self.req.func_tool,
"model": self.req.model, # NOTE: in fact, this arg is None in most cases
"session_id": self.req.session_id,
"extra_user_content_parts": self.req.extra_user_content_parts, # list[ContentPart]
}
if include_model:
# For primary provider we keep explicit model selection if provided.
payload["model"] = self.req.model
if self.streaming:
stream = self.provider.text_chat_stream(**payload)
async for resp in stream: # type: ignore
@@ -183,6 +198,77 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
else:
yield await self.provider.text_chat(**payload)
async def _iter_llm_responses_with_fallback(
self,
) -> T.AsyncGenerator[LLMResponse, None]:
"""Wrap _iter_llm_responses with provider fallback handling."""
candidates = [self.provider, *self.fallback_providers]
total_candidates = len(candidates)
last_exception: Exception | None = None
last_err_response: LLMResponse | None = None
for idx, candidate in enumerate(candidates):
candidate_id = candidate.provider_config.get("id", "<unknown>")
is_last_candidate = idx == total_candidates - 1
if idx > 0:
logger.warning(
"Switched from %s to fallback chat provider: %s",
self.provider.provider_config.get("id", "<unknown>"),
candidate_id,
)
self.provider = candidate
has_stream_output = False
try:
async for resp in self._iter_llm_responses(include_model=idx == 0):
if resp.is_chunk:
has_stream_output = True
yield resp
continue
if (
resp.role == "err"
and not has_stream_output
and (not is_last_candidate)
):
last_err_response = resp
logger.warning(
"Chat Model %s returns error response, trying fallback to next provider.",
candidate_id,
)
break
yield resp
return
if has_stream_output:
return
except Exception as exc: # noqa: BLE001
last_exception = exc
logger.warning(
"Chat Model %s request error: %s",
candidate_id,
exc,
exc_info=True,
)
continue
if last_err_response:
yield last_err_response
return
if last_exception:
yield LLMResponse(
role="err",
completion_text=(
"All chat models failed: "
f"{type(last_exception).__name__}: {last_exception}"
),
)
return
yield LLMResponse(
role="err",
completion_text="All available chat models are unavailable.",
)
def _simple_print_message_role(self, tag: str = ""):
roles = []
for message in self.run_context.messages:
@@ -215,7 +301,7 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
)
self._simple_print_message_role("[AftCompact]")
async for llm_response in self._iter_llm_responses():
async for llm_response in self._iter_llm_responses_with_fallback():
if llm_response.is_chunk:
# update ttft
if self.stats.time_to_first_token == 0:
+38
View File
@@ -870,6 +870,41 @@ def _get_compress_provider(
return provider
def _get_fallback_chat_providers(
provider: Provider, plugin_context: Context, provider_settings: dict
) -> list[Provider]:
fallback_ids = provider_settings.get("fallback_chat_models", [])
if not isinstance(fallback_ids, list):
logger.warning(
"fallback_chat_models setting is not a list, skip fallback providers."
)
return []
provider_id = str(provider.provider_config.get("id", ""))
seen_provider_ids: set[str] = {provider_id} if provider_id else set()
fallbacks: list[Provider] = []
for fallback_id in fallback_ids:
if not isinstance(fallback_id, str) or not fallback_id:
continue
if fallback_id in seen_provider_ids:
continue
fallback_provider = plugin_context.get_provider_by_id(fallback_id)
if fallback_provider is None:
logger.warning("Fallback chat provider `%s` not found, skip.", fallback_id)
continue
if not isinstance(fallback_provider, Provider):
logger.warning(
"Fallback chat provider `%s` is invalid type: %s, skip.",
fallback_id,
type(fallback_provider),
)
continue
fallbacks.append(fallback_provider)
seen_provider_ids.add(fallback_id)
return fallbacks
async def build_main_agent(
*,
event: AstrMessageEvent,
@@ -1093,6 +1128,9 @@ async def build_main_agent(
truncate_turns=config.dequeue_context_length,
enforce_max_turns=config.max_context_length,
tool_schema_mode=config.tool_schema_mode,
fallback_providers=_get_fallback_chat_providers(
provider, plugin_context, config.provider_settings
),
)
if apply_reset:
+59 -3
View File
@@ -5,7 +5,7 @@ from typing import Any, TypedDict
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
VERSION = "4.15.0"
VERSION = "4.17.0"
DB_PATH = os.path.join(get_astrbot_data_path(), "data_v4.db")
WEBHOOK_SUPPORTED_PLATFORMS = [
@@ -15,6 +15,7 @@ WEBHOOK_SUPPORTED_PLATFORMS = [
"wecom_ai_bot",
"slack",
"lark",
"line",
]
# 默认配置
@@ -67,6 +68,7 @@ DEFAULT_CONFIG = {
"provider_settings": {
"enable": True,
"default_provider_id": "",
"fallback_chat_models": [],
"default_image_caption_provider_id": "",
"image_caption_prompt": "Please describe the image using Chinese.",
"provider_pool": ["*"], # "*" 表示使用所有可用的提供者
@@ -194,6 +196,12 @@ DEFAULT_CONFIG = {
"host": "0.0.0.0",
"port": 6185,
"disable_access_log": True,
"ssl": {
"enable": False,
"cert_file": "",
"key_file": "",
"ca_certs": "",
},
},
"platform": [],
"platform_specific": {
@@ -415,6 +423,7 @@ CONFIG_METADATA_2 = {
"slack_webhook_port": 6197,
"slack_webhook_path": "/astrbot-slack-webhook/callback",
},
# LINE's config is located in line_adapter.py
"Satori": {
"id": "satori",
"type": "satori",
@@ -2205,6 +2214,10 @@ CONFIG_METADATA_2 = {
"default_provider_id": {
"type": "string",
},
"fallback_chat_models": {
"type": "list",
"items": {"type": "string"},
},
"wake_prefix": {
"type": "string",
},
@@ -2399,6 +2412,19 @@ CONFIG_METADATA_2 = {
"type": "string",
"options": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
},
"dashboard.ssl.enable": {"type": "bool"},
"dashboard.ssl.cert_file": {
"type": "string",
"condition": {"dashboard.ssl.enable": True},
},
"dashboard.ssl.key_file": {
"type": "string",
"condition": {"dashboard.ssl.enable": True},
},
"dashboard.ssl.ca_certs": {
"type": "string",
"condition": {"dashboard.ssl.enable": True},
},
"log_file_enable": {"type": "bool"},
"log_file_path": {"type": "string", "condition": {"log_file_enable": True}},
"log_file_max_mb": {"type": "int", "condition": {"log_file_enable": True}},
@@ -2502,15 +2528,22 @@ CONFIG_METADATA_3 = {
},
"ai": {
"description": "模型",
"hint": "当使用非内置 Agent 执行器时,默认聊天模型和默认图片转述模型可能会无效,但某些插件会依赖此配置项来调用 AI 能力。",
"hint": "当使用非内置 Agent 执行器时,默认对话模型和默认图片转述模型可能会无效,但某些插件会依赖此配置项来调用 AI 能力。",
"type": "object",
"items": {
"provider_settings.default_provider_id": {
"description": "默认聊天模型",
"description": "默认对话模型",
"type": "string",
"_special": "select_provider",
"hint": "留空时使用第一个模型",
},
"provider_settings.fallback_chat_models": {
"description": "回退对话模型列表",
"type": "list",
"items": {"type": "string"},
"_special": "select_providers",
"hint": "主聊天模型请求失败时,按顺序切换到这些模型。",
},
"provider_settings.default_image_caption_provider_id": {
"description": "默认图片转述模型",
"type": "string",
@@ -3406,6 +3439,29 @@ CONFIG_METADATA_3_SYSTEM = {
"hint": "控制台输出日志的级别。",
"options": ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
},
"dashboard.ssl.enable": {
"description": "启用 WebUI HTTPS",
"type": "bool",
"hint": "启用后,WebUI 将直接使用 HTTPS 提供服务。",
},
"dashboard.ssl.cert_file": {
"description": "SSL 证书文件路径",
"type": "string",
"hint": "证书文件路径(PEM)。支持绝对路径和相对路径(相对于当前工作目录)。",
"condition": {"dashboard.ssl.enable": True},
},
"dashboard.ssl.key_file": {
"description": "SSL 私钥文件路径",
"type": "string",
"hint": "私钥文件路径(PEM)。支持绝对路径和相对路径(相对于当前工作目录)。",
"condition": {"dashboard.ssl.enable": True},
},
"dashboard.ssl.ca_certs": {
"description": "SSL CA 证书文件路径",
"type": "string",
"hint": "可选。用于指定 CA 证书文件路径。",
"condition": {"dashboard.ssl.enable": True},
},
"log_file_enable": {
"description": "启用文件日志",
"type": "bool",
+261 -309
View File
@@ -1,24 +1,4 @@
"""日志系统, 用于支持核心组件和插件的日志记录, 提供了日志订阅功能
const:
CACHED_SIZE: 日志缓存大小, 用于限制缓存的日志数量
log_color_config: 日志颜色配置, 定义了不同日志级别的颜色
class:
LogBroker: 日志代理类, 用于缓存和分发日志消息
LogQueueHandler: 日志处理器, 用于将日志消息发送到 LogBroker
LogManager: 日志管理器, 用于创建和配置日志记录器
function:
is_plugin_path: 检查文件路径是否来自插件目录
get_short_level_name: 将日志级别名称转换为四个字母的缩写
工作流程:
1. 通过 LogManager.GetLogger() 获取日志器, 配置了控制台输出和多个格式化过滤器
2. 通过 set_queue_handler() 设置日志处理器, 将日志消息发送到 LogBroker
3. logBroker 维护一个订阅者列表, 负责将日志分发给所有订阅者
4. 订阅者可以使用 register() 方法注册到 LogBroker, 订阅日志流
"""
"""日志系统,统一将标准 logging 输出转发到 loguru。"""
import asyncio
import logging
@@ -27,54 +7,59 @@ import sys
import time
from asyncio import Queue
from collections import deque
from logging.handlers import RotatingFileHandler
from typing import TYPE_CHECKING
import colorlog
from loguru import logger as _raw_loguru_logger
from astrbot.core.config.default import VERSION
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
# 日志缓存大小
CACHED_SIZE = 500
# 日志颜色配置
log_color_config = {
"DEBUG": "green",
"INFO": "bold_cyan",
"WARNING": "bold_yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
"RESET": "reset",
"asctime": "green",
}
if TYPE_CHECKING:
from loguru import Record
def is_plugin_path(pathname):
"""检查文件路径是否来自插件目录
class _RecordEnricherFilter(logging.Filter):
"""为 logging.LogRecord 注入 AstrBot 日志字段。"""
Args:
pathname (str): 文件路径
def filter(self, record: logging.LogRecord) -> bool:
record.plugin_tag = "[Plug]" if _is_plugin_path(record.pathname) else "[Core]"
record.short_levelname = _get_short_level_name(record.levelname)
record.astrbot_version_tag = (
f" [v{VERSION}]" if record.levelno >= logging.WARNING else ""
)
record.source_file = _build_source_file(record.pathname)
record.source_line = record.lineno
record.is_trace = record.name == "astrbot.trace"
return True
Returns:
bool: 如果路径来自插件目录,则返回 True,否则返回 False
"""
class _QueueAnsiColorFilter(logging.Filter):
"""Attach ANSI color prefix for WebUI console rendering."""
_LEVEL_COLOR = {
"DEBUG": "\u001b[1;34m",
"INFO": "\u001b[1;36m",
"WARNING": "\u001b[1;33m",
"ERROR": "\u001b[31m",
"CRITICAL": "\u001b[1;31m",
}
def filter(self, record: logging.LogRecord) -> bool:
record.ansi_prefix = self._LEVEL_COLOR.get(record.levelname, "\u001b[0m")
record.ansi_reset = "\u001b[0m"
return True
def _is_plugin_path(pathname: str | None) -> bool:
if not pathname:
return False
norm_path = os.path.normpath(pathname)
return ("data/plugins" in norm_path) or ("astrbot/builtin_stars/" in norm_path)
def get_short_level_name(level_name):
"""将日志级别名称转换为四个字母的缩写
Args:
level_name (str): 日志级别名称, 如 "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"
Returns:
str: 四个字母的日志级别缩写
"""
def _get_short_level_name(level_name: str) -> str:
level_map = {
"DEBUG": "DBUG",
"INFO": "INFO",
@@ -85,44 +70,75 @@ def get_short_level_name(level_name):
return level_map.get(level_name, level_name[:4].upper())
class LogBroker:
"""日志代理类, 用于缓存和分发日志消息
def _build_source_file(pathname: str | None) -> str:
if not pathname:
return "unknown"
dirname = os.path.dirname(pathname)
return (
os.path.basename(dirname) + "." + os.path.basename(pathname).replace(".py", "")
)
发布-订阅模式
"""
def _patch_record(record: "Record") -> None:
extra = record["extra"]
extra.setdefault("plugin_tag", "[Core]")
extra.setdefault("short_levelname", _get_short_level_name(record["level"].name))
level_no = record["level"].no
extra.setdefault("astrbot_version_tag", f" [v{VERSION}]" if level_no >= 30 else "")
extra.setdefault("source_file", _build_source_file(record["file"].path))
extra.setdefault("source_line", record["line"])
extra.setdefault("is_trace", False)
_loguru = _raw_loguru_logger.patch(_patch_record)
class _LoguruInterceptHandler(logging.Handler):
"""将 logging 记录转发到 loguru。"""
def emit(self, record: logging.LogRecord) -> None:
try:
level: str | int = _loguru.level(record.levelname).name
except ValueError:
level = record.levelno
payload = {
"plugin_tag": getattr(record, "plugin_tag", "[Core]"),
"short_levelname": getattr(
record,
"short_levelname",
_get_short_level_name(record.levelname),
),
"astrbot_version_tag": getattr(record, "astrbot_version_tag", ""),
"source_file": getattr(
record, "source_file", _build_source_file(record.pathname)
),
"source_line": getattr(record, "source_line", record.lineno),
"is_trace": getattr(record, "is_trace", record.name == "astrbot.trace"),
}
_loguru.bind(**payload).opt(exception=record.exc_info).log(
level,
record.getMessage(),
)
class LogBroker:
"""日志代理类,用于缓存和分发日志消息。"""
def __init__(self) -> None:
self.log_cache = deque(maxlen=CACHED_SIZE) # 环形缓冲区, 保存最近的日志
self.subscribers: list[Queue] = [] # 订阅者列表
self.log_cache = deque(maxlen=CACHED_SIZE)
self.subscribers: list[Queue] = []
def register(self) -> Queue:
"""注册新的订阅者, 并给每个订阅者返回一个带有日志缓存的队列
Returns:
Queue: 订阅者的队列, 可用于接收日志消息
"""
q = Queue(maxsize=CACHED_SIZE + 10)
self.subscribers.append(q)
return q
def unregister(self, q: Queue) -> None:
"""取消订阅
Args:
q (Queue): 需要取消订阅的队列
"""
self.subscribers.remove(q)
def publish(self, log_entry: dict) -> None:
"""发布新日志到所有订阅者, 使用非阻塞方式投递, 避免一个订阅者阻塞整个系统
Args:
log_entry (dict): 日志消息, 包含日志级别和日志内容.
example: {"level": "INFO", "data": "This is a log message.", "time": "2023-10-01 12:00:00"}
"""
self.log_cache.append(log_entry)
for q in self.subscribers:
try:
@@ -132,23 +148,13 @@ class LogBroker:
class LogQueueHandler(logging.Handler):
"""日志处理器, 用于将日志消息发送到 LogBroker
继承自 logging.Handler
"""
"""日志处理器用于将日志消息发送到 LogBroker"""
def __init__(self, log_broker: LogBroker) -> None:
super().__init__()
self.log_broker = log_broker
def emit(self, record) -> None:
"""日志处理的入口方法, 接受一个日志记录, 转换为字符串后由 LogBroker 发布
这个方法会在每次日志记录时被调用
Args:
record (logging.LogRecord): 日志记录对象, 包含日志信息
"""
def emit(self, record: logging.LogRecord) -> None:
log_entry = self.format(record)
self.log_broker.publish(
{
@@ -160,117 +166,16 @@ class LogQueueHandler(logging.Handler):
class LogManager:
"""日志管理器, 用于创建和配置日志记录器
_LOGGER_HANDLER_FLAG = "_astrbot_loguru_handler"
_ENRICH_FILTER_FLAG = "_astrbot_enrich_filter"
提供了获取默认日志记录器logger和设置队列处理器的方法
"""
_FILE_HANDLER_FLAG = "_astrbot_file_handler"
_TRACE_FILE_HANDLER_FLAG = "_astrbot_trace_file_handler"
@classmethod
def GetLogger(cls, log_name: str = "default"):
"""获取指定名称的日志记录器logger
Args:
log_name (str): 日志记录器的名称, 默认为 "default"
Returns:
logging.Logger: 返回配置好的日志记录器
"""
logger = logging.getLogger(log_name)
# 检查该logger或父级logger是否已经有处理器, 如果已经有处理器, 直接返回该logger, 避免重复配置
if logger.hasHandlers():
return logger
# 如果logger没有处理器
console_handler = logging.StreamHandler(
sys.stdout,
) # 创建一个StreamHandler用于控制台输出
console_handler.setLevel(
logging.DEBUG,
) # 将日志级别设置为DEBUG(最低级别, 显示所有日志), *如果插件没有设置级别, 默认为DEBUG
# 创建彩色日志格式化器, 输出日志格式为: [时间] [插件标签] [日志级别] [文件名:行号]: 日志消息
console_formatter = colorlog.ColoredFormatter(
fmt="%(log_color)s [%(asctime)s] %(plugin_tag)s [%(short_levelname)-4s]%(astrbot_version_tag)s [%(filename)s:%(lineno)d]: %(message)s %(reset)s",
datefmt="%H:%M:%S",
log_colors=log_color_config,
)
class PluginFilter(logging.Filter):
"""插件过滤器类, 用于标记日志来源是插件还是核心组件"""
def filter(self, record) -> bool:
record.plugin_tag = (
"[Plug]" if is_plugin_path(record.pathname) else "[Core]"
)
return True
class FileNameFilter(logging.Filter):
"""文件名过滤器类, 用于修改日志记录的文件名格式
例如: 将文件路径 /path/to/file.py 转换为 file.<file> 格式
"""
# 获取这个文件和父文件夹的名字:<folder>.<file> 并且去除 .py
def filter(self, record) -> bool:
dirname = os.path.dirname(record.pathname)
record.filename = (
os.path.basename(dirname)
+ "."
+ os.path.basename(record.pathname).replace(".py", "")
)
return True
class LevelNameFilter(logging.Filter):
"""短日志级别名称过滤器类, 用于将日志级别名称转换为四个字母的缩写"""
# 添加短日志级别名称
def filter(self, record) -> bool:
record.short_levelname = get_short_level_name(record.levelname)
return True
class AstrBotVersionTagFilter(logging.Filter):
"""在 WARNING 及以上级别日志后追加当前 AstrBot 版本号。"""
def filter(self, record) -> bool:
if record.levelno >= logging.WARNING:
record.astrbot_version_tag = f" [v{VERSION}]"
else:
record.astrbot_version_tag = ""
return True
console_handler.setFormatter(console_formatter) # 设置处理器的格式化器
logger.addFilter(PluginFilter()) # 添加插件过滤器
logger.addFilter(FileNameFilter()) # 添加文件名过滤器
logger.addFilter(LevelNameFilter()) # 添加级别名称过滤器
logger.addFilter(AstrBotVersionTagFilter()) # 追加版本号(WARNING 及以上)
logger.setLevel(logging.DEBUG) # 设置日志级别为DEBUG
logger.addHandler(console_handler) # 添加处理器到logger
return logger
@classmethod
def set_queue_handler(cls, logger: logging.Logger, log_broker: LogBroker) -> None:
"""设置队列处理器, 用于将日志消息发送到 LogBroker
Args:
logger (logging.Logger): 日志记录器
log_broker (LogBroker): 日志代理类, 用于缓存和分发日志消息
"""
handler = LogQueueHandler(log_broker)
handler.setLevel(logging.DEBUG)
if logger.handlers:
handler.setFormatter(logger.handlers[0].formatter)
else:
# 为队列处理器设置相同格式的formatter
handler.setFormatter(
logging.Formatter(
"[%(asctime)s] [%(short_levelname)s] %(plugin_tag)s[%(filename)s:%(lineno)d]: %(message)s",
),
)
logger.addHandler(handler)
_configured = False
_console_sink_id: int | None = None
_file_sink_id: int | None = None
_trace_sink_id: int | None = None
_NOISY_LOGGER_LEVELS: dict[str, int] = {
"aiosqlite": logging.WARNING,
}
@classmethod
def _default_log_path(cls) -> str:
@@ -285,79 +190,145 @@ class LogManager:
return os.path.join(get_astrbot_data_path(), configured_path)
@classmethod
def _get_file_handlers(cls, logger: logging.Logger) -> list[logging.Handler]:
return [
handler
for handler in logger.handlers
if getattr(handler, cls._FILE_HANDLER_FLAG, False)
]
def _setup_loguru(cls) -> None:
if cls._configured:
return
@classmethod
def _get_trace_file_handlers(cls, logger: logging.Logger) -> list[logging.Handler]:
return [
handler
for handler in logger.handlers
if getattr(handler, cls._TRACE_FILE_HANDLER_FLAG, False)
]
@classmethod
def _remove_file_handlers(cls, logger: logging.Logger) -> None:
for handler in cls._get_file_handlers(logger):
logger.removeHandler(handler)
try:
handler.close()
except Exception:
pass
@classmethod
def _remove_trace_file_handlers(cls, logger: logging.Logger) -> None:
for handler in cls._get_trace_file_handlers(logger):
logger.removeHandler(handler)
try:
handler.close()
except Exception:
pass
@classmethod
def _add_file_handler(
cls,
logger: logging.Logger,
file_path: str,
max_mb: int | None = None,
backup_count: int = 3,
trace: bool = False,
) -> None:
os.makedirs(os.path.dirname(file_path) or ".", exist_ok=True)
max_bytes = 0
if max_mb and max_mb > 0:
max_bytes = max_mb * 1024 * 1024
if max_bytes > 0:
file_handler = RotatingFileHandler(
file_path,
maxBytes=max_bytes,
backupCount=backup_count,
encoding="utf-8",
)
else:
file_handler = logging.FileHandler(file_path, encoding="utf-8")
file_handler.setLevel(logger.level)
if trace:
formatter = logging.Formatter(
"[%(asctime)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
else:
formatter = logging.Formatter(
"[%(asctime)s] %(plugin_tag)s [%(short_levelname)s]%(astrbot_version_tag)s [%(filename)s:%(lineno)d]: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
file_handler.setFormatter(formatter)
setattr(
file_handler,
cls._TRACE_FILE_HANDLER_FLAG if trace else cls._FILE_HANDLER_FLAG,
True,
_loguru.remove()
cls._console_sink_id = _loguru.add(
sys.stdout,
level="DEBUG",
colorize=True,
filter=lambda record: not record["extra"].get("is_trace", False),
format=(
"<green>[{time:HH:mm:ss.SSS}]</green> {extra[plugin_tag]} "
"<level>[{extra[short_levelname]}]</level>{extra[astrbot_version_tag]} "
"[{extra[source_file]}:{extra[source_line]}]: <level>{message}</level>"
),
)
cls._configured = True
@classmethod
def _setup_root_bridge(cls) -> None:
root_logger = logging.getLogger()
has_handler = any(
getattr(handler, cls._LOGGER_HANDLER_FLAG, False)
for handler in root_logger.handlers
)
if not has_handler:
handler = _LoguruInterceptHandler()
setattr(handler, cls._LOGGER_HANDLER_FLAG, True)
root_logger.addHandler(handler)
root_logger.setLevel(logging.DEBUG)
for name, level in cls._NOISY_LOGGER_LEVELS.items():
logging.getLogger(name).setLevel(level)
@classmethod
def _ensure_logger_enricher_filter(cls, logger: logging.Logger) -> None:
has_filter = any(
getattr(existing_filter, cls._ENRICH_FILTER_FLAG, False)
for existing_filter in logger.filters
)
if not has_filter:
enrich_filter = _RecordEnricherFilter()
setattr(enrich_filter, cls._ENRICH_FILTER_FLAG, True)
logger.addFilter(enrich_filter)
@classmethod
def _ensure_logger_intercept_handler(cls, logger: logging.Logger) -> None:
has_handler = any(
getattr(handler, cls._LOGGER_HANDLER_FLAG, False)
for handler in logger.handlers
)
if not has_handler:
handler = _LoguruInterceptHandler()
setattr(handler, cls._LOGGER_HANDLER_FLAG, True)
logger.addHandler(handler)
@classmethod
def GetLogger(cls, log_name: str = "default") -> logging.Logger:
cls._setup_loguru()
cls._setup_root_bridge()
logger = logging.getLogger(log_name)
cls._ensure_logger_enricher_filter(logger)
cls._ensure_logger_intercept_handler(logger)
logger.setLevel(logging.DEBUG)
logger.propagate = False
return logger
@classmethod
def set_queue_handler(cls, logger: logging.Logger, log_broker: LogBroker) -> None:
cls._ensure_logger_enricher_filter(logger)
for handler in logger.handlers:
if isinstance(handler, LogQueueHandler):
return
handler = LogQueueHandler(log_broker)
handler.setLevel(logging.DEBUG)
handler.addFilter(_QueueAnsiColorFilter())
handler.setFormatter(
logging.Formatter(
"%(ansi_prefix)s[%(asctime)s.%(msecs)03d] %(plugin_tag)s [%(short_levelname)s]%(astrbot_version_tag)s "
"[%(source_file)s:%(source_line)d]: %(message)s%(ansi_reset)s",
datefmt="%Y-%m-%d %H:%M:%S",
),
)
logger.addHandler(handler)
@classmethod
def _remove_sink(cls, sink_id: int | None) -> None:
if sink_id is None:
return
try:
_loguru.remove(sink_id)
except ValueError:
pass
@classmethod
def _add_file_sink(
cls,
*,
file_path: str,
level: int,
max_mb: int | None,
backup_count: int,
trace: bool,
) -> int:
os.makedirs(os.path.dirname(file_path) or ".", exist_ok=True)
rotation = f"{max_mb} MB" if max_mb and max_mb > 0 else None
retention = f"{backup_count} files" if rotation else None
if trace:
return _loguru.add(
file_path,
level="INFO",
format="[{time:YYYY-MM-DD HH:mm:ss.SSS}] {message}",
encoding="utf-8",
rotation=rotation,
retention=retention,
enqueue=True,
filter=lambda record: record["extra"].get("is_trace", False),
)
logging_level_name = logging.getLevelName(level)
if isinstance(logging_level_name, int):
logging_level_name = "INFO"
return _loguru.add(
file_path,
level=logging_level_name,
format=(
"[{time:YYYY-MM-DD HH:mm:ss.SSS}] {extra[plugin_tag]} "
"[{extra[short_levelname]}]{extra[astrbot_version_tag]} "
"[{extra[source_file]}:{extra[source_line]}]: {message}"
),
encoding="utf-8",
rotation=rotation,
retention=retention,
enqueue=True,
filter=lambda record: not record["extra"].get("is_trace", False),
)
logger.addHandler(file_handler)
@classmethod
def configure_logger(
@@ -366,13 +337,6 @@ class LogManager:
config: dict | None,
override_level: str | None = None,
) -> None:
"""根据配置设置日志级别和文件日志。
Args:
logger: 需要配置的 logger
config: 配置字典
override_level: 若提供,将覆盖配置中的日志级别
"""
if not config:
return
@@ -383,7 +347,6 @@ class LogManager:
except Exception:
logger.setLevel(logging.INFO)
# 兼容旧版嵌套配置
if "log_file" in config:
file_conf = config.get("log_file") or {}
enable_file = bool(file_conf.get("enable", False))
@@ -394,27 +357,22 @@ class LogManager:
file_path = config.get("log_file_path")
max_mb = config.get("log_file_max_mb")
file_path = cls._resolve_log_path(file_path)
cls._remove_sink(cls._file_sink_id)
cls._file_sink_id = None
existing = cls._get_file_handlers(logger)
if not enable_file:
cls._remove_file_handlers(logger)
return
# 如果已有文件处理器且路径一致,则仅同步级别
if existing:
handler = existing[0]
base = getattr(handler, "baseFilename", "")
if base and os.path.abspath(base) == os.path.abspath(file_path):
handler.setLevel(logger.level)
return
cls._remove_file_handlers(logger)
cls._add_file_handler(logger, file_path, max_mb=max_mb)
cls._file_sink_id = cls._add_file_sink(
file_path=cls._resolve_log_path(file_path),
level=logger.level,
max_mb=max_mb,
backup_count=3,
trace=False,
)
@classmethod
def configure_trace_logger(cls, config: dict | None) -> None:
"""为 trace 事件配置独立的文件日志,不向控制台输出。"""
if not config:
return
@@ -429,28 +387,22 @@ class LogManager:
path = path or legacy.get("trace_path")
max_mb = max_mb or legacy.get("trace_max_mb")
if not enable:
trace_logger = logging.getLogger("astrbot.trace")
cls._remove_trace_file_handlers(trace_logger)
return
file_path = cls._resolve_log_path(path or "logs/astrbot.trace.log")
trace_logger = logging.getLogger("astrbot.trace")
cls._ensure_logger_enricher_filter(trace_logger)
cls._ensure_logger_intercept_handler(trace_logger)
trace_logger.setLevel(logging.INFO)
trace_logger.propagate = False
existing = cls._get_trace_file_handlers(trace_logger)
if existing:
handler = existing[0]
base = getattr(handler, "baseFilename", "")
if base and os.path.abspath(base) == os.path.abspath(file_path):
handler.setLevel(trace_logger.level)
return
cls._remove_trace_file_handlers(trace_logger)
cls._remove_sink(cls._trace_sink_id)
cls._trace_sink_id = None
cls._add_file_handler(
trace_logger,
file_path,
if not enable:
return
cls._trace_sink_id = cls._add_file_sink(
file_path=cls._resolve_log_path(path or "logs/astrbot.trace.log"),
level=logging.INFO,
max_mb=max_mb,
backup_count=3,
trace=True,
)
+4
View File
@@ -176,6 +176,10 @@ class PlatformManager:
from .sources.satori.satori_adapter import (
SatoriPlatformAdapter, # noqa: F401
)
case "line":
from .sources.line.line_adapter import (
LinePlatformAdapter, # noqa: F401
)
except (ImportError, ModuleNotFoundError) as e:
logger.error(
f"加载平台适配器 {platform_config['type']} 失败,原因:{e}。请检查依赖库是否安装。提示:可以在 管理面板->平台日志->安装Pip库 中安装依赖库。",
@@ -0,0 +1,474 @@
import asyncio
import mimetypes
import time
import uuid
from pathlib import Path
from typing import Any, cast
from astrbot.api import logger
from astrbot.api.event import MessageChain
from astrbot.api.message_components import At, File, Image, Plain, Record, Video
from astrbot.api.platform import (
AstrBotMessage,
Group,
MessageMember,
MessageType,
Platform,
PlatformMetadata,
)
from astrbot.core.platform.astr_message_event import MessageSesion
from astrbot.core.utils.astrbot_path import get_astrbot_temp_path
from astrbot.core.utils.webhook_utils import log_webhook_info
from ...register import register_platform_adapter
from .line_api import LineAPIClient
from .line_event import LineMessageEvent
LINE_CONFIG_METADATA = {
"channel_access_token": {
"description": "LINE Channel Access Token",
"type": "string",
"hint": "LINE Messaging API 的 channel access token。",
},
"channel_secret": {
"description": "LINE Channel Secret",
"type": "string",
"hint": "用于校验 LINE Webhook 签名。",
},
}
LINE_I18N_RESOURCES = {
"zh-CN": {
"channel_access_token": {
"description": "LINE Channel Access Token",
"hint": "LINE Messaging API 的 channel access token。",
},
"channel_secret": {
"description": "LINE Channel Secret",
"hint": "用于校验 LINE Webhook 签名。",
},
},
"en-US": {
"channel_access_token": {
"description": "LINE Channel Access Token",
"hint": "Channel access token for LINE Messaging API.",
},
"channel_secret": {
"description": "LINE Channel Secret",
"hint": "Used to verify LINE webhook signatures.",
},
},
}
@register_platform_adapter(
"line",
"LINE Messaging API 适配器",
support_streaming_message=False,
default_config_tmpl={
"id": "line",
"type": "line",
"enable": False,
"channel_access_token": "",
"channel_secret": "",
"unified_webhook_mode": True,
"webhook_uuid": "",
},
config_metadata=LINE_CONFIG_METADATA,
i18n_resources=LINE_I18N_RESOURCES,
)
class LinePlatformAdapter(Platform):
def __init__(
self,
platform_config: dict,
platform_settings: dict,
event_queue: asyncio.Queue,
) -> None:
super().__init__(platform_config, event_queue)
self.config["unified_webhook_mode"] = True
self.destination = "unknown"
self.settings = platform_settings
self._event_id_timestamps: dict[str, float] = {}
self.shutdown_event = asyncio.Event()
channel_access_token = str(platform_config.get("channel_access_token", ""))
channel_secret = str(platform_config.get("channel_secret", ""))
if not channel_access_token or not channel_secret:
raise ValueError(
"LINE 适配器需要 channel_access_token 和 channel_secret。",
)
self.line_api = LineAPIClient(
channel_access_token=channel_access_token,
channel_secret=channel_secret,
)
async def send_by_session(
self,
session: MessageSesion,
message_chain: MessageChain,
) -> None:
messages = await LineMessageEvent.build_line_messages(message_chain)
if messages:
await self.line_api.push_message(session.session_id, messages)
await super().send_by_session(session, message_chain)
def meta(self) -> PlatformMetadata:
return PlatformMetadata(
name="line",
description="LINE Messaging API 适配器",
id=cast(str, self.config.get("id", "line")),
support_streaming_message=False,
)
async def run(self) -> None:
webhook_uuid = self.config.get("webhook_uuid")
if webhook_uuid:
log_webhook_info(f"{self.meta().id}(LINE)", webhook_uuid)
else:
logger.warning("[LINE] webhook_uuid 为空,统一 Webhook 可能无法接收消息。")
await self.shutdown_event.wait()
async def terminate(self) -> None:
self.shutdown_event.set()
await self.line_api.close()
async def webhook_callback(self, request: Any) -> Any:
raw_body = await request.get_data()
signature = request.headers.get("x-line-signature")
if not self.line_api.verify_signature(raw_body, signature):
logger.warning("[LINE] invalid webhook signature")
return "invalid signature", 400
try:
payload = await request.get_json(force=True, silent=False)
except Exception as e:
logger.warning("[LINE] invalid webhook body: %s", e)
return "bad request", 400
if not isinstance(payload, dict):
return "bad request", 400
await self.handle_webhook_event(payload)
return "ok", 200
async def handle_webhook_event(self, payload: dict[str, Any]) -> None:
destination = str(payload.get("destination", "")).strip()
if destination:
self.destination = destination
events = payload.get("events")
if not isinstance(events, list):
return
for event in events:
if not isinstance(event, dict):
continue
event_id = str(event.get("webhookEventId", ""))
if event_id and self._is_duplicate_event(event_id):
logger.debug("[LINE] duplicate event skipped: %s", event_id)
continue
abm = await self.convert_message(event)
if abm is None:
continue
await self.handle_msg(abm)
async def convert_message(self, event: dict[str, Any]) -> AstrBotMessage | None:
if str(event.get("type", "")) != "message":
return None
if str(event.get("mode", "active")) == "standby":
return None
source = event.get("source", {})
if not isinstance(source, dict):
return None
message = event.get("message", {})
if not isinstance(message, dict):
return None
source_type = str(source.get("type", ""))
user_id = str(source.get("userId", "")).strip()
group_id = str(source.get("groupId", "")).strip()
room_id = str(source.get("roomId", "")).strip()
abm = AstrBotMessage()
abm.self_id = self.destination or self.meta().id
abm.message = []
abm.raw_message = event
abm.message_id = str(
message.get("id")
or event.get("webhookEventId")
or event.get("deliveryContext", {}).get("deliveryId", "")
or uuid.uuid4().hex
)
event_timestamp = event.get("timestamp")
if isinstance(event_timestamp, int):
abm.timestamp = (
event_timestamp // 1000
if event_timestamp > 1_000_000_000_000
else event_timestamp
)
else:
abm.timestamp = int(time.time())
if source_type in {"group", "room"}:
abm.type = MessageType.GROUP_MESSAGE
container_id = group_id or room_id
abm.group = Group(group_id=container_id, group_name=container_id)
abm.session_id = container_id
sender_id = user_id or container_id
elif source_type == "user":
abm.type = MessageType.FRIEND_MESSAGE
abm.session_id = user_id
sender_id = user_id
else:
abm.type = MessageType.OTHER_MESSAGE
abm.session_id = user_id or group_id or room_id or "unknown"
sender_id = abm.session_id
abm.sender = MessageMember(user_id=sender_id, nickname=sender_id[:8])
components = await self._parse_line_message_components(message)
if not components:
return None
abm.message = components
abm.message_str = self._build_message_str(components)
return abm
async def _parse_line_message_components(
self,
message: dict[str, Any],
) -> list:
msg_type = str(message.get("type", ""))
message_id = str(message.get("id", "")).strip()
if msg_type == "text":
text = str(message.get("text", ""))
mention = message.get("mention")
if isinstance(mention, dict):
return self._parse_text_with_mentions(text, mention)
return [Plain(text=text)] if text else []
if msg_type == "image":
image_component = await self._build_image_component(message_id, message)
return [image_component] if image_component else [Plain(text="[image]")]
if msg_type == "video":
video_component = await self._build_video_component(message_id, message)
return [video_component] if video_component else [Plain(text="[video]")]
if msg_type == "audio":
audio_component = await self._build_audio_component(message_id, message)
return [audio_component] if audio_component else [Plain(text="[audio]")]
if msg_type == "file":
file_component = await self._build_file_component(message_id, message)
return [file_component] if file_component else [Plain(text="[file]")]
if msg_type == "sticker":
return [Plain(text="[sticker]")]
return [Plain(text=f"[{msg_type}]")]
def _parse_text_with_mentions(self, text: str, mention_obj: dict[str, Any]) -> list:
mentions = mention_obj.get("mentionees", [])
if not isinstance(mentions, list) or not mentions:
return [Plain(text=text)] if text else []
normalized = []
for item in mentions:
if not isinstance(item, dict):
continue
start = item.get("index")
length = item.get("length")
if not isinstance(start, int) or not isinstance(length, int):
continue
normalized.append((start, length, item))
normalized.sort(key=lambda x: x[0])
ret = []
cursor = 0
for start, length, item in normalized:
if start > cursor:
part = text[cursor:start]
if part:
ret.append(Plain(text=part))
label = text[start : start + length] or "@user"
mention_type = str(item.get("type", ""))
if mention_type == "user":
target_id = str(item.get("userId", "")).strip()
ret.append(At(qq=target_id, name=label.lstrip("@")))
else:
ret.append(Plain(text=label))
cursor = max(cursor, start + length)
if cursor < len(text):
tail = text[cursor:]
if tail:
ret.append(Plain(text=tail))
return ret
async def _build_image_component(
self,
message_id: str,
message: dict[str, Any],
) -> Image | None:
external_url = self._get_external_content_url(message)
if external_url:
return Image.fromURL(external_url)
content = await self.line_api.get_message_content(message_id)
if not content:
return None
content_bytes, _, _ = content
return Image.fromBytes(content_bytes)
async def _build_video_component(
self,
message_id: str,
message: dict[str, Any],
) -> Video | None:
external_url = self._get_external_content_url(message)
if external_url:
return Video.fromURL(external_url)
content = await self.line_api.get_message_content(message_id)
if not content:
return None
content_bytes, content_type, _ = content
suffix = self._guess_suffix(content_type, ".mp4")
file_path = self._store_temp_content("video", message_id, content_bytes, suffix)
return Video(file=file_path, path=file_path)
async def _build_audio_component(
self,
message_id: str,
message: dict[str, Any],
) -> Record | None:
external_url = self._get_external_content_url(message)
if external_url:
return Record.fromURL(external_url)
content = await self.line_api.get_message_content(message_id)
if not content:
return None
content_bytes, content_type, _ = content
suffix = self._guess_suffix(content_type, ".m4a")
file_path = self._store_temp_content("audio", message_id, content_bytes, suffix)
return Record(file=file_path, url=file_path)
async def _build_file_component(
self,
message_id: str,
message: dict[str, Any],
) -> File | None:
content = await self.line_api.get_message_content(message_id)
if not content:
return None
content_bytes, content_type, filename = content
default_name = str(message.get("fileName", "")).strip() or f"{message_id}.bin"
suffix = Path(default_name).suffix or self._guess_suffix(content_type, ".bin")
final_name = filename or default_name
file_path = self._store_temp_content(
"file",
message_id,
content_bytes,
suffix,
original_name=final_name,
)
return File(name=final_name, file=file_path, url=file_path)
@staticmethod
def _get_external_content_url(message: dict[str, Any]) -> str:
provider = message.get("contentProvider")
if not isinstance(provider, dict):
return ""
if str(provider.get("type", "")) != "external":
return ""
return str(provider.get("originalContentUrl", "")).strip()
@staticmethod
def _guess_suffix(content_type: str | None, fallback: str) -> str:
if not content_type:
return fallback
base_type = content_type.split(";", 1)[0].strip().lower()
guessed = mimetypes.guess_extension(base_type)
if guessed:
return guessed
return fallback
@staticmethod
def _store_temp_content(
content_type: str,
message_id: str,
content: bytes,
suffix: str,
original_name: str = "",
) -> str:
temp_dir = Path(get_astrbot_temp_path())
temp_dir.mkdir(parents=True, exist_ok=True)
name_prefix = f"line_{content_type}"
if original_name:
safe_stem = Path(original_name).stem.strip()
safe_stem = "".join(
ch if ch.isalnum() or ch in ("-", "_", ".") else "_" for ch in safe_stem
)
safe_stem = safe_stem.strip("._")
if safe_stem:
name_prefix = safe_stem[:64]
file_path = temp_dir / f"{name_prefix}_{message_id}_{uuid.uuid4().hex[:6]}"
file_path = file_path.with_suffix(suffix)
file_path.write_bytes(content)
return str(file_path.resolve())
@staticmethod
def _build_message_str(components: list) -> str:
parts: list[str] = []
for comp in components:
if isinstance(comp, Plain):
parts.append(comp.text)
elif isinstance(comp, At):
parts.append(f"@{comp.name or comp.qq}")
elif isinstance(comp, Image):
parts.append("[image]")
elif isinstance(comp, Video):
parts.append("[video]")
elif isinstance(comp, Record):
parts.append("[audio]")
elif isinstance(comp, File):
parts.append(str(comp.name or "[file]"))
else:
parts.append(f"[{comp.type}]")
return " ".join(i for i in parts if i).strip()
def _clean_expired_events(self) -> None:
current = time.time()
expired = [
event_id
for event_id, ts in self._event_id_timestamps.items()
if current - ts > 1800
]
for event_id in expired:
del self._event_id_timestamps[event_id]
def _is_duplicate_event(self, event_id: str) -> bool:
self._clean_expired_events()
if event_id in self._event_id_timestamps:
return True
self._event_id_timestamps[event_id] = time.time()
return False
async def handle_msg(self, abm: AstrBotMessage) -> None:
event = LineMessageEvent(
message_str=abm.message_str,
message_obj=abm,
platform_meta=self.meta(),
session_id=abm.session_id,
line_api=self.line_api,
)
self._event_queue.put_nowait(event)
@@ -0,0 +1,203 @@
import asyncio
import base64
import hmac
import json
from hashlib import sha256
from typing import Any
from urllib.parse import unquote
import aiohttp
from astrbot.api import logger
class LineAPIClient:
def __init__(
self,
*,
channel_access_token: str,
channel_secret: str,
timeout_seconds: int = 30,
) -> None:
self.channel_access_token = channel_access_token.strip()
self.channel_secret = channel_secret.strip()
self.timeout = aiohttp.ClientTimeout(total=timeout_seconds)
self._session: aiohttp.ClientSession | None = None
async def _get_session(self) -> aiohttp.ClientSession:
if self._session is None or self._session.closed:
self._session = aiohttp.ClientSession(timeout=self.timeout)
return self._session
async def close(self) -> None:
if self._session and not self._session.closed:
await self._session.close()
def verify_signature(self, raw_body: bytes, signature: str | None) -> bool:
if not signature:
return False
digest = hmac.new(
self.channel_secret.encode("utf-8"),
raw_body,
sha256,
).digest()
expected = base64.b64encode(digest).decode("utf-8")
return hmac.compare_digest(expected, signature.strip())
@property
def _auth_headers(self) -> dict[str, str]:
return {"Authorization": f"Bearer {self.channel_access_token}"}
async def reply_message(
self,
reply_token: str,
messages: list[dict[str, Any]],
*,
notification_disabled: bool = False,
) -> bool:
payload = {
"replyToken": reply_token,
"messages": messages[:5],
"notificationDisabled": notification_disabled,
}
return await self._post_json(
"https://api.line.me/v2/bot/message/reply",
payload=payload,
op_name="reply",
)
async def push_message(
self,
to: str,
messages: list[dict[str, Any]],
*,
notification_disabled: bool = False,
) -> bool:
payload = {
"to": to,
"messages": messages[:5],
"notificationDisabled": notification_disabled,
}
return await self._post_json(
"https://api.line.me/v2/bot/message/push",
payload=payload,
op_name="push",
)
async def _post_json(
self,
url: str,
*,
payload: dict[str, Any],
op_name: str,
) -> bool:
session = await self._get_session()
headers = {
**self._auth_headers,
"Content-Type": "application/json",
}
try:
async with session.post(url, json=payload, headers=headers) as resp:
if resp.status < 400:
return True
body = await resp.text()
logger.error(
"[LINE] %s message failed: status=%s body=%s",
op_name,
resp.status,
body,
)
return False
except Exception as e:
logger.error("[LINE] %s message request failed: %s", op_name, e)
return False
async def get_message_content(
self,
message_id: str,
) -> tuple[bytes, str | None, str | None] | None:
session = await self._get_session()
url = f"https://api-data.line.me/v2/bot/message/{message_id}/content"
headers = self._auth_headers
async with session.get(url, headers=headers) as resp:
if resp.status == 202:
if not await self._wait_for_transcoding(message_id):
return None
async with session.get(url, headers=headers) as retry_resp:
if retry_resp.status != 200:
body = await retry_resp.text()
logger.warning(
"[LINE] get content retry failed: message_id=%s status=%s body=%s",
message_id,
retry_resp.status,
body,
)
return None
return await self._read_content_response(retry_resp)
if resp.status != 200:
body = await resp.text()
logger.warning(
"[LINE] get content failed: message_id=%s status=%s body=%s",
message_id,
resp.status,
body,
)
return None
return await self._read_content_response(resp)
async def _read_content_response(
self,
resp: aiohttp.ClientResponse,
) -> tuple[bytes, str | None, str | None]:
content = await resp.read()
content_type = resp.headers.get("Content-Type")
disposition = resp.headers.get("Content-Disposition")
filename = self._extract_filename_from_disposition(disposition)
return content, content_type, filename
def _extract_filename_from_disposition(self, disposition: str | None) -> str | None:
if not disposition:
return None
for part in disposition.split(";"):
token = part.strip()
if token.startswith("filename*="):
val = token.split("=", 1)[1].strip().strip('"')
if val.lower().startswith("utf-8''"):
val = val[7:]
return unquote(val)
if token.startswith("filename="):
return token.split("=", 1)[1].strip().strip('"')
return None
async def _wait_for_transcoding(
self,
message_id: str,
*,
max_attempts: int = 10,
interval_seconds: float = 1.0,
) -> bool:
session = await self._get_session()
url = (
f"https://api-data.line.me/v2/bot/message/{message_id}/content/transcoding"
)
headers = self._auth_headers
for _ in range(max_attempts):
try:
async with session.get(url, headers=headers) as resp:
if resp.status != 200:
await asyncio.sleep(interval_seconds)
continue
body = await resp.text()
data = json.loads(body)
status = str(data.get("status", "")).lower()
if status == "succeeded":
return True
if status == "failed":
return False
except Exception:
pass
await asyncio.sleep(interval_seconds)
return False
@@ -0,0 +1,285 @@
import asyncio
import os
import re
import uuid
from collections.abc import AsyncGenerator
from pathlib import Path
from astrbot.api import logger
from astrbot.api.event import AstrMessageEvent, MessageChain
from astrbot.api.message_components import (
At,
BaseMessageComponent,
File,
Image,
Plain,
Record,
Video,
)
from astrbot.core.utils.astrbot_path import get_astrbot_temp_path
from astrbot.core.utils.media_utils import get_media_duration
from .line_api import LineAPIClient
class LineMessageEvent(AstrMessageEvent):
def __init__(
self,
message_str,
message_obj,
platform_meta,
session_id,
line_api: LineAPIClient,
) -> None:
super().__init__(message_str, message_obj, platform_meta, session_id)
self.line_api = line_api
@staticmethod
async def _component_to_message_object(
segment: BaseMessageComponent,
) -> dict | None:
if isinstance(segment, Plain):
text = segment.text.strip()
if not text:
return None
return {"type": "text", "text": text[:5000]}
if isinstance(segment, At):
name = str(segment.name or segment.qq or "").strip()
if not name:
return None
return {"type": "text", "text": f"@{name}"[:5000]}
if isinstance(segment, Image):
image_url = await LineMessageEvent._resolve_image_url(segment)
if not image_url:
return None
return {
"type": "image",
"originalContentUrl": image_url,
"previewImageUrl": image_url,
}
if isinstance(segment, Record):
audio_url = await LineMessageEvent._resolve_record_url(segment)
if not audio_url:
return None
duration = await LineMessageEvent._resolve_record_duration(segment)
return {
"type": "audio",
"originalContentUrl": audio_url,
"duration": duration,
}
if isinstance(segment, Video):
video_url = await LineMessageEvent._resolve_video_url(segment)
if not video_url:
return None
preview_url = await LineMessageEvent._resolve_video_preview_url(segment)
if not preview_url:
return None
return {
"type": "video",
"originalContentUrl": video_url,
"previewImageUrl": preview_url,
}
if isinstance(segment, File):
file_url = await LineMessageEvent._resolve_file_url(segment)
if not file_url:
return None
file_name = str(segment.name or "").strip() or "file.bin"
file_size = await LineMessageEvent._resolve_file_size(segment)
if file_size <= 0:
return None
return {
"type": "file",
"fileName": file_name,
"fileSize": file_size,
"originalContentUrl": file_url,
}
return None
@staticmethod
async def _resolve_image_url(segment: Image) -> str:
candidate = (segment.url or segment.file or "").strip()
if candidate.startswith("http://") or candidate.startswith("https://"):
return candidate
try:
return await segment.register_to_file_service()
except Exception as e:
logger.debug("[LINE] resolve image url failed: %s", e)
return ""
@staticmethod
async def _resolve_record_url(segment: Record) -> str:
candidate = (segment.url or segment.file or "").strip()
if candidate.startswith("http://") or candidate.startswith("https://"):
return candidate
try:
return await segment.register_to_file_service()
except Exception as e:
logger.debug("[LINE] resolve record url failed: %s", e)
return ""
@staticmethod
async def _resolve_record_duration(segment: Record) -> int:
try:
file_path = await segment.convert_to_file_path()
duration_ms = await get_media_duration(file_path)
if isinstance(duration_ms, int) and duration_ms > 0:
return duration_ms
except Exception as e:
logger.debug("[LINE] resolve record duration failed: %s", e)
return 1000
@staticmethod
async def _resolve_video_url(segment: Video) -> str:
candidate = (segment.file or "").strip()
if candidate.startswith("http://") or candidate.startswith("https://"):
return candidate
try:
return await segment.register_to_file_service()
except Exception as e:
logger.debug("[LINE] resolve video url failed: %s", e)
return ""
@staticmethod
async def _resolve_video_preview_url(segment: Video) -> str:
cover_candidate = (segment.cover or "").strip()
if cover_candidate.startswith("http://") or cover_candidate.startswith(
"https://"
):
return cover_candidate
if cover_candidate:
try:
cover_seg = Image(file=cover_candidate)
return await cover_seg.register_to_file_service()
except Exception as e:
logger.debug("[LINE] resolve video cover failed: %s", e)
try:
video_path = await segment.convert_to_file_path()
temp_dir = Path(get_astrbot_temp_path())
temp_dir.mkdir(parents=True, exist_ok=True)
thumb_path = temp_dir / f"line_video_preview_{uuid.uuid4().hex}.jpg"
process = await asyncio.create_subprocess_exec(
"ffmpeg",
"-y",
"-ss",
"00:00:01",
"-i",
video_path,
"-frames:v",
"1",
str(thumb_path),
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)
await process.communicate()
if process.returncode != 0 or not thumb_path.exists():
return ""
cover_seg = Image.fromFileSystem(str(thumb_path))
return await cover_seg.register_to_file_service()
except Exception as e:
logger.debug("[LINE] generate video preview failed: %s", e)
return ""
@staticmethod
async def _resolve_file_url(segment: File) -> str:
if segment.url and segment.url.startswith(("http://", "https://")):
return segment.url
try:
return await segment.register_to_file_service()
except Exception as e:
logger.debug("[LINE] resolve file url failed: %s", e)
return ""
@staticmethod
async def _resolve_file_size(segment: File) -> int:
try:
file_path = await segment.get_file(allow_return_url=False)
if file_path and os.path.exists(file_path):
return int(os.path.getsize(file_path))
except Exception as e:
logger.debug("[LINE] resolve file size failed: %s", e)
return 0
@classmethod
async def build_line_messages(cls, message_chain: MessageChain) -> list[dict]:
messages: list[dict] = []
for segment in message_chain.chain:
obj = await cls._component_to_message_object(segment)
if obj:
messages.append(obj)
if not messages:
return []
if len(messages) > 5:
logger.warning(
"[LINE] message count exceeds 5, extra segments will be dropped."
)
messages = messages[:5]
return messages
async def send(self, message: MessageChain) -> None:
messages = await self.build_line_messages(message)
if not messages:
return
raw = self.message_obj.raw_message
reply_token = ""
if isinstance(raw, dict):
reply_token = str(raw.get("replyToken") or "")
sent = False
if reply_token:
sent = await self.line_api.reply_message(reply_token, messages)
if not sent:
target_id = self.get_group_id() or self.get_sender_id()
if target_id:
await self.line_api.push_message(target_id, messages)
await super().send(message)
async def send_streaming(
self,
generator: AsyncGenerator,
use_fallback: bool = False,
):
if not use_fallback:
buffer = None
async for chain in generator:
if not buffer:
buffer = chain
else:
buffer.chain.extend(chain.chain)
if not buffer:
return None
buffer.squash_plain()
await self.send(buffer)
return await super().send_streaming(generator, use_fallback)
buffer = ""
pattern = re.compile(r"[^。?!~…]+[。?!~…]+")
async for chain in generator:
if isinstance(chain, MessageChain):
for comp in chain.chain:
if isinstance(comp, Plain):
buffer += comp.text
if any(p in buffer for p in "。?!~…"):
buffer = await self.process_buffer(buffer, pattern)
else:
await self.send(MessageChain(chain=[comp]))
await asyncio.sleep(1.5)
if buffer.strip():
await self.send(MessageChain([Plain(buffer)]))
return await super().send_streaming(generator, use_fallback)
@@ -20,6 +20,7 @@ class PlatformAdapterType(enum.Flag):
WEIXIN_OFFICIAL_ACCOUNT = enum.auto()
SATORI = enum.auto()
MISSKEY = enum.auto()
LINE = enum.auto()
ALL = (
AIOCQHTTP
| QQOFFICIAL
@@ -34,6 +35,7 @@ class PlatformAdapterType(enum.Flag):
| WEIXIN_OFFICIAL_ACCOUNT
| SATORI
| MISSKEY
| LINE
)
@@ -51,6 +53,7 @@ ADAPTER_NAME_2_TYPE = {
"weixin_official_account": PlatformAdapterType.WEIXIN_OFFICIAL_ACCOUNT,
"satori": PlatformAdapterType.SATORI,
"misskey": PlatformAdapterType.MISSKEY,
"line": PlatformAdapterType.LINE,
}
+30
View File
@@ -62,6 +62,9 @@ class PluginManager:
self._pm_lock = asyncio.Lock()
"""StarManager操作互斥锁"""
self.failed_plugin_dict = {}
"""加载失败插件的信息,用于后续可能的热重载"""
self.failed_plugin_info = ""
if os.getenv("ASTRBOT_RELOAD", "0") == "1":
asyncio.create_task(self._watch_plugins_changes())
@@ -327,6 +330,28 @@ class PluginManager:
except KeyError:
logger.warning(f"模块 {module_name} 未载入")
async def reload_failed_plugin(self, dir_name):
"""
重新加载未注册(加载失败)的插件
Args:
dir_name (str): 要重载的特定插件名称。
Returns:
tuple: 返回 load() 方法的结果,包含 (success, error_message)
- success (bool): 重载是否成功
- error_message (str|None): 错误信息,成功时为 None
"""
async with self._pm_lock:
if dir_name in self.failed_plugin_dict:
success, error = await self.load(specified_dir_name=dir_name)
if success:
self.failed_plugin_dict.pop(dir_name, None)
if not self.failed_plugin_dict:
self.failed_plugin_info = ""
return success, None
else:
return False, error
return False, "插件不存在于失败列表中"
async def reload(self, specified_plugin_name=None):
"""重新加载插件
@@ -663,6 +688,11 @@ class PluginManager:
logger.error(f"| {line}")
logger.error("----------------------------------")
fail_rec += f"加载 {root_dir_name} 插件时出现问题,原因 {e!s}\n"
self.failed_plugin_dict[root_dir_name] = {
"error": str(e),
"traceback": errors,
}
# 记录注册失败的插件名称,以便后续重载插件
# 清除 pip.main 导致的多余的 logging handlers
for handler in logging.root.handlers[:]:
+17 -1
View File
@@ -1,3 +1,4 @@
import os
import uuid
from astrbot.core import astrbot_config, logger
@@ -20,6 +21,20 @@ def _get_dashboard_port() -> int:
return 6185
def _is_dashboard_ssl_enabled() -> bool:
env_ssl = os.environ.get("DASHBOARD_SSL_ENABLE") or os.environ.get(
"ASTRBOT_DASHBOARD_SSL_ENABLE"
)
if env_ssl is not None:
return env_ssl.strip().lower() in {"1", "true", "yes", "on"}
try:
return bool(astrbot_config.get("dashboard", {}).get("ssl", {}).get("enable"))
except Exception as e:
logger.error(f"获取 dashboard SSL 配置失败: {e!s}")
return False
def log_webhook_info(platform_name: str, webhook_uuid: str) -> None:
"""打印美观的 webhook 信息日志
@@ -38,12 +53,13 @@ def log_webhook_info(platform_name: str, webhook_uuid: str) -> None:
callback_base = callback_base.rstrip("/")
webhook_url = f"{callback_base}/api/platform/webhook/{webhook_uuid}"
scheme = "https" if _is_dashboard_ssl_enabled() else "http"
display_log = (
"\n====================\n"
f"🔗 机器人平台 {platform_name} 已启用统一 Webhook 模式\n"
f"📍 Webhook 回调地址: \n"
f"http://<your-ip>:{_get_dashboard_port()}/api/platform/webhook/{webhook_uuid}\n"
f"{scheme}://<your-ip>:{_get_dashboard_port()}/api/platform/webhook/{webhook_uuid}\n"
f"{webhook_url}\n"
"====================\n"
)
+33
View File
@@ -54,11 +54,13 @@ class PluginRoute(Route):
"/plugin/market_list": ("GET", self.get_online_plugins),
"/plugin/off": ("POST", self.off_plugin),
"/plugin/on": ("POST", self.on_plugin),
"/plugin/reload-failed": ("POST", self.reload_failed_plugins),
"/plugin/reload": ("POST", self.reload_plugins),
"/plugin/readme": ("GET", self.get_plugin_readme),
"/plugin/changelog": ("GET", self.get_plugin_changelog),
"/plugin/source/get": ("GET", self.get_custom_source),
"/plugin/source/save": ("POST", self.save_custom_source),
"/plugin/source/get-failed-plugins": ("GET", self.get_failed_plugins),
}
self.core_lifecycle = core_lifecycle
self.plugin_manager = plugin_manager
@@ -75,6 +77,33 @@ class PluginRoute(Route):
self._logo_cache = {}
async def reload_failed_plugins(self):
if DEMO_MODE:
return (
Response()
.error("You are not permitted to do this operation in demo mode")
.__dict__
)
try:
data = await request.get_json()
dir_name = data.get("dir_name") # 这里拿的是目录名,不是插件名
if not dir_name:
return Response().error("缺少插件目录名").__dict__
# 调用 star_manager.py 中的函数
# 注意:传入的是目录名
success, err = await self.plugin_manager.reload_failed_plugin(dir_name)
if success:
return Response().ok(None, f"插件 {dir_name} 重载成功。").__dict__
else:
return Response().error(f"重载失败: {err}").__dict__
except Exception as e:
logger.error(f"/api/plugin/reload-failed: {traceback.format_exc()}")
return Response().error(str(e)).__dict__
async def reload_plugins(self):
if DEMO_MODE:
return (
@@ -334,6 +363,10 @@ class PluginRoute(Route):
.__dict__
)
async def get_failed_plugins(self):
"""专门获取加载失败的插件列表(字典格式)"""
return Response().ok(self.plugin_manager.failed_plugin_dict).__dict__
async def get_plugin_handlers_info(self, handler_full_names: list[str]):
"""解析插件行为"""
handlers = []
+2 -1
View File
@@ -295,14 +295,15 @@ class StatRoute(Route):
if locale:
candidates.append(base_path / f"FIRST_NOTICE.{locale}.md")
if locale.lower().startswith("zh"):
candidates.append(base_path / "FIRST_NOTICE.md")
candidates.append(base_path / "FIRST_NOTICE.zh-CN.md")
elif locale.lower().startswith("en"):
candidates.append(base_path / "FIRST_NOTICE.en-US.md")
candidates.extend(
[
base_path / "FIRST_NOTICE.en-US.md",
base_path / "FIRST_NOTICE.md",
base_path / "FIRST_NOTICE.en-US.md",
],
)
+60 -9
View File
@@ -2,6 +2,7 @@ import asyncio
import logging
import os
import socket
from pathlib import Path
from typing import Protocol, cast
import jwt
@@ -36,6 +37,12 @@ class _AddrWithPort(Protocol):
APP: Quart
def _parse_env_bool(value: str | None, default: bool) -> bool:
if value is None:
return default
return value.strip().lower() in {"1", "true", "yes", "on"}
class AstrBotDashboard:
def __init__(
self,
@@ -201,23 +208,33 @@ class AstrBotDashboard:
def run(self):
ip_addr = []
dashboard_config = self.core_lifecycle.astrbot_config.get("dashboard", {})
port = (
os.environ.get("DASHBOARD_PORT")
or os.environ.get("ASTRBOT_DASHBOARD_PORT")
or self.core_lifecycle.astrbot_config["dashboard"].get("port", 6185)
or dashboard_config.get("port", 6185)
)
host = (
os.environ.get("DASHBOARD_HOST")
or os.environ.get("ASTRBOT_DASHBOARD_HOST")
or self.core_lifecycle.astrbot_config["dashboard"].get("host", "0.0.0.0")
or dashboard_config.get("host", "0.0.0.0")
)
enable = self.core_lifecycle.astrbot_config["dashboard"].get("enable", True)
enable = dashboard_config.get("enable", True)
ssl_config = dashboard_config.get("ssl", {})
if not isinstance(ssl_config, dict):
ssl_config = {}
ssl_enable = _parse_env_bool(
os.environ.get("DASHBOARD_SSL_ENABLE")
or os.environ.get("ASTRBOT_DASHBOARD_SSL_ENABLE"),
bool(ssl_config.get("enable", False)),
)
scheme = "https" if ssl_enable else "http"
if not enable:
logger.info("WebUI 已被禁用")
return None
logger.info(f"正在启动 WebUI, 监听地址: http://{host}:{port}")
logger.info(f"正在启动 WebUI, 监听地址: {scheme}://{host}:{port}")
if host == "0.0.0.0":
logger.info(
"提示: WebUI 将监听所有网络接口,请注意安全。(可在 data/cmd_config.json 中配置 dashboard.host 以修改 host",
@@ -245,9 +262,9 @@ class AstrBotDashboard:
raise Exception(f"端口 {port} 已被占用")
parts = [f"\n ✨✨✨\n AstrBot v{VERSION} WebUI 已启动,可访问\n\n"]
parts.append(f" ➜ 本地: http://localhost:{port}\n")
parts.append(f" ➜ 本地: {scheme}://localhost:{port}\n")
for ip in ip_addr:
parts.append(f" ➜ 网络: http://{ip}:{port}\n")
parts.append(f" ➜ 网络: {scheme}://{ip}:{port}\n")
parts.append(" ➜ 默认用户名和密码: astrbot\n ✨✨✨\n")
display = "".join(parts)
@@ -261,11 +278,45 @@ class AstrBotDashboard:
# 配置 Hypercorn
config = HyperConfig()
config.bind = [f"{host}:{port}"]
if ssl_enable:
cert_file = (
os.environ.get("DASHBOARD_SSL_CERT")
or os.environ.get("ASTRBOT_DASHBOARD_SSL_CERT")
or ssl_config.get("cert_file", "")
)
key_file = (
os.environ.get("DASHBOARD_SSL_KEY")
or os.environ.get("ASTRBOT_DASHBOARD_SSL_KEY")
or ssl_config.get("key_file", "")
)
ca_certs = (
os.environ.get("DASHBOARD_SSL_CA_CERTS")
or os.environ.get("ASTRBOT_DASHBOARD_SSL_CA_CERTS")
or ssl_config.get("ca_certs", "")
)
cert_path = Path(cert_file).expanduser()
key_path = Path(key_file).expanduser()
if not cert_file or not key_file:
raise ValueError(
"dashboard.ssl.enable 为 true 时,必须配置 cert_file 和 key_file。",
)
if not cert_path.is_file():
raise ValueError(f"SSL 证书文件不存在: {cert_path}")
if not key_path.is_file():
raise ValueError(f"SSL 私钥文件不存在: {key_path}")
config.certfile = str(cert_path.resolve())
config.keyfile = str(key_path.resolve())
if ca_certs:
ca_path = Path(ca_certs).expanduser()
if not ca_path.is_file():
raise ValueError(f"SSL CA 证书文件不存在: {ca_path}")
config.ca_certs = str(ca_path.resolve())
# 根据配置决定是否禁用访问日志
disable_access_log = self.core_lifecycle.astrbot_config.get(
"dashboard", {}
).get("disable_access_log", True)
disable_access_log = dashboard_config.get("disable_access_log", True)
if disable_access_log:
config.accesslog = None
else:
+62
View File
@@ -0,0 +1,62 @@
## What's Changed
### 新增
- QQ 官方机器人平台支持主动推送消息,私聊场景支持接收文件 ([#5066](https://github.com/AstrBotDevs/AstrBot/issues/5066))
- 为 Telegram 平台适配器新增等待 AI 回复时自动展示 “正在输入”、“正在上传图片” 等状态的功能 ([#5037](https://github.com/AstrBotDevs/AstrBot/issues/5037))
- 为飞书适配器增加接收文件、读取引用消息的内容(包括引用的图片、视频、文件、文字等) ([#5018](https://github.com/AstrBotDevs/AstrBot/issues/5018))
- 新增自定义平台适配器 i18n 支持 ([#5045](https://github.com/AstrBotDevs/AstrBot/issues/5045))
- 新增临时文件处理能力,可在系统配置中限制 data/temp 目录的最大大小。 ([#5026](https://github.com/AstrBotDevs/AstrBot/issues/5026))
- 增加首次启动公告功能,支持多语言与 WebUI 集成
### 修复
- 修复 OpenRouter DeepSeek 场景下的 chunk 错误 ([#5069](https://github.com/AstrBotDevs/AstrBot/issues/5069))
- 修复备份时人格文件夹映射缺失问题 ([#5042](https://github.com/AstrBotDevs/AstrBot/issues/5042))
- 修复更新日志与官方文档弹窗双滚动条问题 ([#5060](https://github.com/AstrBotDevs/AstrBot/issues/5060))
- 修复 provider 额外参数弹窗 key 显示异常
- 修复连接失败时错误日志提示不准确的问题
- 修复提前返回时未等待 reset 协程导致的资源清理问题 ([#5033](https://github.com/AstrBotDevs/AstrBot/issues/5033))
- 提升打包版桌面端启动稳定性并优化插件依赖处理 ([#5031](https://github.com/AstrBotDevs/AstrBot/issues/5031))
- 为 Electron 与后端日志增加按大小轮转 ([#5029](https://github.com/AstrBotDevs/AstrBot/issues/5029))
- 加固冻结运行时(frozen app runtime)插件依赖加载流程 ([#5015](https://github.com/AstrBotDevs/AstrBot/issues/5015))
### 优化
- 完善合并消息、引用解析与图片回退,并支持配置化控制 ([#5054](https://github.com/AstrBotDevs/AstrBot/issues/5054))
- 配置页面支持通过侧边栏子项切换普通配置/系统配置,并补充相关路由修复
- 优化分段回复间隔时间初始化逻辑 ([#5068](https://github.com/AstrBotDevs/AstrBot/issues/5068))
### 文档与维护
- 同步并修正 README 文档内容与拼写 ([#5055](https://github.com/AstrBotDevs/AstrBot/issues/5055), [#5014](https://github.com/AstrBotDevs/AstrBot/issues/5014))
- 新增 AUR 安装方式说明 ([#4879](https://github.com/AstrBotDevs/AstrBot/issues/4879))
- 执行代码格式化(ruff
## What's Changed (EN)
### New Features
- Added proactive message push and private-chat file receiving support for the QQ official bot adapter ([#5066](https://github.com/AstrBotDevs/AstrBot/issues/5066))
- Added automatic "typing..." and "uploading image..." status display while waiting for AI response in the Telegram adapter ([#5037](https://github.com/AstrBotDevs/AstrBot/issues/5037))
- Added file receiving and quoted message content reading (including quoted images, videos, files, text, etc.) for the Feishu adapter ([#5018](https://github.com/AstrBotDevs/AstrBot/issues/5018))
- Added i18n support for custom platform adapters ([#5045](https://github.com/AstrBotDevs/AstrBot/issues/5045))
- Introduced temporary file handling and `TempDirCleaner` ([#5026](https://github.com/AstrBotDevs/AstrBot/issues/5026))
- Added a first-launch notice feature with multilingual content and WebUI integration
### Fixes
- Added sidebar child-tab switching for normal/system config and fixed related routing behavior on the config page
- Fixed chunk errors when using OpenRouter DeepSeek ([#5069](https://github.com/AstrBotDevs/AstrBot/issues/5069))
- Improved forwarded-quote parsing and image fallback with configurable controls ([#5054](https://github.com/AstrBotDevs/AstrBot/issues/5054))
- Fixed missing persona-folder mapping in backup exports ([#5042](https://github.com/AstrBotDevs/AstrBot/issues/5042))
- Fixed double scrollbar issue in changelog and official docs dialogs ([#5060](https://github.com/AstrBotDevs/AstrBot/issues/5060))
- Fixed key rendering issues in the provider extra-params dialog
- Improved error log wording for connection failures
- Fixed unawaited reset coroutine cleanup on early returns ([#5033](https://github.com/AstrBotDevs/AstrBot/issues/5033))
- Improved packaged desktop startup stability and plugin dependency handling ([#5031](https://github.com/AstrBotDevs/AstrBot/issues/5031))
- Added size-based log rotation for Electron and backend logs ([#5029](https://github.com/AstrBotDevs/AstrBot/issues/5029))
- Hardened plugin dependency loading in frozen app runtime ([#5015](https://github.com/AstrBotDevs/AstrBot/issues/5015))
### Improvements
- Optimized initialization logic for segmented-reply interval timing ([#5068](https://github.com/AstrBotDevs/AstrBot/issues/5068))
### Docs & Maintenance
- Synced and fixed README docs and typos ([#5055](https://github.com/AstrBotDevs/AstrBot/issues/5055), [#5014](https://github.com/AstrBotDevs/AstrBot/issues/5014))
- Added AUR installation instructions ([#4879](https://github.com/AstrBotDevs/AstrBot/issues/4879))
- Applied code formatting with ruff
+29
View File
@@ -0,0 +1,29 @@
## What's Changed
### 新增
- 新增 LINE 平台适配器与相关配置支持 ([#5085](https://github.com/AstrBotDevs/AstrBot/issues/5085))
- 新增备用回退聊天模型列表,当主模型报错时自动切换到备用模型 ([#5109](https://github.com/AstrBotDevs/AstrBot/issues/5109))
- 新增插件加载失败后的热重载支持,便于插件修复后快速恢复 ([#5043](https://github.com/AstrBotDevs/AstrBot/issues/5043))
- WebUI 新增 SSL 配置选项并同步更新相关日志行为 ([#5117](https://github.com/AstrBotDevs/AstrBot/issues/5117))
### 修复
- 修复 Dockerfile 中依赖导出流程,增加 `uv lock` 步骤并移除不必要的 `--frozen` 参数,提升构建稳定性 ([#5091](https://github.com/AstrBotDevs/AstrBot/issues/5091), [#5089](https://github.com/AstrBotDevs/AstrBot/issues/5089))
- 修复首次启动公告 `FIRST_NOTICE.md` 的本地化路径解析问题,补充兼容路径处理 ([#5083](https://github.com/AstrBotDevs/AstrBot/issues/5083), [#5082](https://github.com/AstrBotDevs/AstrBot/issues/5082))
### 优化
- 日志系统由 `colorlog` 切换为 `loguru`,增强日志输出与展示能力 ([#5115](https://github.com/AstrBotDevs/AstrBot/issues/5115))
## What's Changed (EN)
### New Features
- Added LINE platform adapter support with related configuration options ([#5085](https://github.com/AstrBotDevs/AstrBot/issues/5085))
- Added fallback chat model chain support in tool loop runner, with corresponding config and improved provider selection display ([#5109](https://github.com/AstrBotDevs/AstrBot/issues/5109))
- Added hot reload support after plugin load failure for faster recovery during plugin development and maintenance ([#5043](https://github.com/AstrBotDevs/AstrBot/issues/5043))
- Added SSL configuration options for WebUI and updated related logging behavior ([#5117](https://github.com/AstrBotDevs/AstrBot/issues/5117))
### Fixes
- Fixed Dockerfile dependency export flow by adding a `uv lock` step and removing unnecessary `--frozen` flag to improve build stability ([#5091](https://github.com/AstrBotDevs/AstrBot/issues/5091), [#5089](https://github.com/AstrBotDevs/AstrBot/issues/5089))
- Fixed locale path resolution for `FIRST_NOTICE.md` and added compatible fallback handling ([#5083](https://github.com/AstrBotDevs/AstrBot/issues/5083), [#5082](https://github.com/AstrBotDevs/AstrBot/issues/5082))
### Improvements
- Replaced `colorlog` with `loguru` to improve logging capabilities and console display ([#5115](https://github.com/AstrBotDevs/AstrBot/issues/5115))
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

@@ -10,6 +10,14 @@
<template v-else-if="itemMeta?._special === 'select_provider_tts'">
<ProviderSelector :model-value="modelValue" @update:model-value="emitUpdate" :provider-type="'text_to_speech'" />
</template>
<template v-else-if="itemMeta?._special === 'select_providers'">
<ProviderSelector
:model-value="modelValue"
@update:model-value="emitUpdate"
:provider-type="'chat_completion'"
:multiple="true"
/>
</template>
<template v-else-if="getSpecialName(itemMeta?._special) === 'select_agent_runner_provider'">
<ProviderSelector
:model-value="modelValue"
@@ -27,7 +27,7 @@ export default {
return {
autoScroll: true,
logColorAnsiMap: {
'\u001b[1;34m': 'color: #0000FF; font-weight: bold;',
'\u001b[1;34m': 'color: #39C5BB; font-weight: bold;',
'\u001b[1;36m': 'color: #00FFFF; font-weight: bold;',
'\u001b[1;33m': 'color: #FFFF00; font-weight: bold;',
'\u001b[31m': 'color: #FF0000;',
@@ -1,16 +1,35 @@
<template>
<div class="d-flex align-center justify-space-between">
<span v-if="!modelValue" style="color: rgb(var(--v-theme-primaryText));">
<span v-if="!hasSelection" style="color: rgb(var(--v-theme-primaryText));">
{{ tm('providerSelector.notSelected') }}
</span>
<span v-else class="provider-name-text">
{{ modelValue }}
<template v-if="multiple">
{{ tm('providerSelector.selectedCount', { count: selectedProviders.length }) }}
</template>
<template v-else>
{{ modelValue }}
</template>
</span>
<v-btn size="small" color="primary" variant="tonal" @click="openDialog">
{{ buttonText || tm('providerSelector.buttonText') }}
</v-btn>
</div>
<div v-if="multiple && selectedProviders.length > 0" class="selected-preview mt-2">
<v-chip
v-for="providerId in selectedProviders"
:key="`preview-${providerId}`"
size="x-small"
color="primary"
variant="tonal"
class="mr-1 mb-1"
label
>
{{ providerId }}
</v-chip>
</div>
<!-- Provider Selection Dialog -->
<v-dialog v-model="dialog" max-width="600px">
<v-card>
@@ -32,10 +51,52 @@
<v-card-text class="pa-0" style="max-height: 400px; overflow-y: auto;">
<v-progress-linear v-if="loading" indeterminate color="primary"></v-progress-linear>
<div v-if="multiple && selectedProviders.length > 0" class="pa-3">
<div class="text-caption text-medium-emphasis mb-2">
{{ tm('providerSelector.selectedCount', { count: selectedProviders.length }) }}
</div>
<v-list density="compact" class="selected-order-list">
<v-list-item
v-for="(providerId, index) in selectedProviders"
:key="`selected-${providerId}-${index}`"
rounded="md"
class="ma-1"
>
<v-list-item-title>{{ providerId }}</v-list-item-title>
<template #append>
<div class="d-flex ga-1">
<v-btn
icon="mdi-arrow-up"
size="x-small"
variant="text"
:disabled="index === 0"
@click.stop="moveSelected(index, -1)"
/>
<v-btn
icon="mdi-arrow-down"
size="x-small"
variant="text"
:disabled="index === selectedProviders.length - 1"
@click.stop="moveSelected(index, 1)"
/>
<v-btn
icon="mdi-close"
size="x-small"
variant="text"
@click.stop="removeSelected(providerId)"
/>
</div>
</template>
</v-list-item>
</v-list>
<v-divider class="ma-1"></v-divider>
</div>
<v-list v-if="!loading && providerList.length > 0" density="compact">
<!-- 不选择选项 -->
<v-list-item
v-if="!multiple"
key="none"
value=""
@click="selectProvider({ id: '' })"
@@ -57,7 +118,7 @@
:key="provider.id"
:value="provider.id"
@click="selectProvider(provider)"
:active="selectedProvider === provider.id"
:active="isProviderSelected(provider.id)"
rounded="md"
class="ma-1">
<v-list-item-title>{{ provider.id }}</v-list-item-title>
@@ -67,7 +128,7 @@
</v-list-item-subtitle>
<template v-slot:append>
<v-icon v-if="selectedProvider === provider.id" color="primary">mdi-check-circle</v-icon>
<v-icon v-if="isProviderSelected(provider.id)" color="primary">mdi-check-circle</v-icon>
</template>
</v-list-item>
</v-list>
@@ -121,7 +182,7 @@ import ProviderPage from '@/views/ProviderPage.vue'
const props = defineProps({
modelValue: {
type: String,
type: [String, Array],
default: ''
},
providerType: {
@@ -135,6 +196,10 @@ const props = defineProps({
buttonText: {
type: String,
default: ''
},
multiple: {
type: Boolean,
default: false
}
})
@@ -145,8 +210,16 @@ const dialog = ref(false)
const providerList = ref([])
const loading = ref(false)
const selectedProvider = ref('')
const selectedProviders = ref([])
const providerDrawer = ref(false)
const hasSelection = computed(() => {
if (props.multiple) {
return selectedProviders.value.length > 0
}
return Boolean(props.modelValue)
})
const defaultTab = computed(() => {
if (props.providerType === 'agent_runner' && props.providerSubtype) {
return `select_agent_runner_provider:${props.providerSubtype}`
@@ -156,7 +229,13 @@ const defaultTab = computed(() => {
// 监听 modelValue 变化,同步到 selectedProvider
watch(() => props.modelValue, (newValue) => {
selectedProvider.value = newValue || ''
if (props.multiple) {
selectedProviders.value = Array.isArray(newValue)
? [...newValue.filter((v) => typeof v === 'string' && v)]
: []
return
}
selectedProvider.value = typeof newValue === 'string' ? newValue : ''
}, { immediate: true })
watch(providerDrawer, (isOpen, wasOpen) => {
@@ -166,7 +245,13 @@ watch(providerDrawer, (isOpen, wasOpen) => {
})
async function openDialog() {
selectedProvider.value = props.modelValue || ''
if (props.multiple) {
selectedProviders.value = Array.isArray(props.modelValue)
? [...props.modelValue.filter((v) => typeof v === 'string' && v)]
: []
} else {
selectedProvider.value = typeof props.modelValue === 'string' ? props.modelValue : ''
}
dialog.value = true
await loadProviders()
}
@@ -205,19 +290,72 @@ function matchesProviderSubtype(provider, subtype) {
}
function selectProvider(provider) {
if (props.multiple) {
if (!provider.id) {
selectedProviders.value = []
return
}
const idx = selectedProviders.value.indexOf(provider.id)
if (idx >= 0) {
selectedProviders.value.splice(idx, 1)
} else {
selectedProviders.value.push(provider.id)
}
return
}
selectedProvider.value = provider.id
}
function confirmSelection() {
emit('update:modelValue', selectedProvider.value)
if (props.multiple) {
emit('update:modelValue', [...selectedProviders.value])
} else {
emit('update:modelValue', selectedProvider.value)
}
dialog.value = false
}
function cancelSelection() {
selectedProvider.value = props.modelValue || ''
if (props.multiple) {
selectedProviders.value = Array.isArray(props.modelValue)
? [...props.modelValue.filter((v) => typeof v === 'string' && v)]
: []
} else {
selectedProvider.value = typeof props.modelValue === 'string' ? props.modelValue : ''
}
dialog.value = false
}
function isProviderSelected(providerId) {
if (props.multiple) {
return selectedProviders.value.includes(providerId)
}
return selectedProvider.value === providerId
}
function removeSelected(providerId) {
const idx = selectedProviders.value.indexOf(providerId)
if (idx >= 0) {
selectedProviders.value.splice(idx, 1)
}
}
function moveSelected(index, delta) {
const targetIndex = index + delta
if (
targetIndex < 0
|| targetIndex >= selectedProviders.value.length
|| index < 0
|| index >= selectedProviders.value.length
) {
return
}
const copied = [...selectedProviders.value]
const [item] = copied.splice(index, 1)
copied.splice(targetIndex, 0, item)
selectedProviders.value = copied
}
function openProviderDrawer() {
providerDrawer.value = true
}
@@ -236,6 +374,16 @@ function closeProviderDrawer() {
display: inline-block;
}
.selected-preview {
width: 100%;
max-width: 100%;
}
.selected-order-list {
background: rgba(var(--v-theme-surface-variant), 0.15);
border-radius: 10px;
}
.v-list-item {
transition: all 0.2s ease;
}
@@ -45,7 +45,8 @@
"unknownType": "Unknown type",
"createProvider": "Create Provider",
"manageProviders": "Provider Management",
"selectProviderPool": "Select Provider Pool..."
"selectProviderPool": "Select Provider Pool...",
"selectedCount": "{count} provider(s) selected"
},
"personaSelector": {
"notSelected": "Not selected",
@@ -37,6 +37,10 @@
"description": "Default Chat Model",
"hint": "Uses the first model when left empty"
},
"fallback_chat_models": {
"description": "Fallback chat model IDs",
"hint": "When the primary chat model request fails, fallback to these chat models in order."
},
"default_image_caption_provider_id": {
"description": "Default Image Caption Model",
"hint": "Leave empty to disable; useful for non-multimodal models"
@@ -869,6 +873,26 @@
"description": "Externally Accessible Callback API Address",
"hint": "External services may access AstrBot's backend through callback links generated by AstrBot (such as file download links). Since AstrBot cannot automatically determine the externally accessible host address in the deployment environment, this configuration item is needed to explicitly specify how external services should access AstrBot's address. Examples: [http://localhost:6185](http://localhost:6185), [https://example.com](https://example.com), etc."
},
"dashboard": {
"ssl": {
"enable": {
"description": "Enable WebUI HTTPS",
"hint": "When enabled, WebUI serves directly over HTTPS."
},
"cert_file": {
"description": "SSL Certificate File Path",
"hint": "Certificate file path (PEM). Supports absolute and relative paths (relative to current working directory)."
},
"key_file": {
"description": "SSL Private Key File Path",
"hint": "Private key file path (PEM). Supports absolute and relative paths (relative to current working directory)."
},
"ca_certs": {
"description": "SSL CA Certificate File Path",
"hint": "Optional. Path to CA certificate file."
}
}
},
"timezone": {
"description": "Timezone",
"hint": "Timezone setting. Please enter an IANA timezone name, such as Asia/Shanghai. Uses system default timezone when empty. For all timezones, see: [https://data.iana.org/time-zones/tzdb-2021a/zone1970.tab](https://data.iana.org/time-zones/tzdb-2021a/zone1970.tab)"
@@ -27,6 +27,8 @@
"title": "Resources",
"githubDesc": "Give us a Star!",
"docsTitle": "Documentation",
"docsDesc": "Read the official AstrBot documentation."
"docsDesc": "Read the official AstrBot documentation.",
"afdianTitle": "Afdian",
"afdianDesc": "Support the AstrBot team on Afdian."
}
}
@@ -45,7 +45,8 @@
"unknownType": "未知类型",
"createProvider": "创建提供商",
"manageProviders": "提供商管理",
"selectProviderPool": "选择提供商池..."
"selectProviderPool": "选择提供商池...",
"selectedCount": "已选择 {count} 个提供商"
},
"personaSelector": {
"notSelected": "未选择",
@@ -31,12 +31,16 @@
},
"ai": {
"description": "模型",
"hint": "当使用非内置 Agent 执行器时,默认聊天模型和默认图片转述模型可能会无效,但某些插件会依赖此配置项来调用 AI 能力。",
"hint": "当使用非内置 Agent 执行器时,默认对话模型和默认图片转述模型可能会无效,但某些插件会依赖此配置项来调用 AI 能力。",
"provider_settings": {
"default_provider_id": {
"description": "默认聊天模型",
"description": "默认对话模型",
"hint": "留空时使用第一个模型"
},
"fallback_chat_models": {
"description": "回退对话模型列表",
"hint": "主对话模型请求失败时,按顺序切换到这些对话模型。"
},
"default_image_caption_provider_id": {
"description": "默认图片转述模型",
"hint": "留空代表不使用,可用于非多模态模型"
@@ -872,6 +876,26 @@
"description": "对外可达的回调接口地址",
"hint": "外部服务可能会通过 AstrBot 生成的回调链接(如文件下载链接)访问 AstrBot 后端。由于 AstrBot 无法自动判断部署环境中对外可达的主机地址(host),因此需要通过此配置项显式指定外部服务如何访问 AstrBot 的地址。如 [http://localhost:6185](http://localhost:6185),[https://example.com](https://example.com) 等。"
},
"dashboard": {
"ssl": {
"enable": {
"description": "启用 WebUI HTTPS",
"hint": "启用后,WebUI 将直接使用 HTTPS 提供服务。"
},
"cert_file": {
"description": "SSL 证书文件路径",
"hint": "证书文件路径(PEM)。支持绝对路径和相对路径(相对于当前工作目录)。"
},
"key_file": {
"description": "SSL 私钥文件路径",
"hint": "私钥文件路径(PEM)。支持绝对路径和相对路径(相对于当前工作目录)。"
},
"ca_certs": {
"description": "SSL CA 证书文件路径",
"hint": "可选。用于指定 CA 证书文件路径。"
}
}
},
"timezone": {
"description": "时区",
"hint": "时区设置。请填写 IANA 时区名称, 如 Asia/Shanghai, 为空时使用系统默认时区。所有时区请查看: [https://data.iana.org/time-zones/tzdb-2021a/zone1970.tab](https://data.iana.org/time-zones/tzdb-2021a/zone1970.tab)"
@@ -27,6 +27,8 @@
"title": "相关资源",
"githubDesc": "给 AstrBot 点个 Star 吧!",
"docsTitle": "文档",
"docsDesc": "查阅 AstrBot 的官方文档。"
"docsDesc": "查阅 AstrBot 的官方文档。",
"afdianTitle": "爱发电",
"afdianDesc": "通过爱发电支持 AstrBot 团队。"
}
}
+2
View File
@@ -34,6 +34,8 @@ export function getPlatformIcon(name) {
return new URL('@/assets/images/platform_logos/satori.png', import.meta.url).href
} else if (name === 'misskey') {
return new URL('@/assets/images/platform_logos/misskey.png', import.meta.url).href
} else if (name === 'line') {
return new URL('@/assets/images/platform_logos/line.png', import.meta.url).href
}
}
+46 -1
View File
@@ -357,11 +357,17 @@ const onLoadingDialogResult = (statusCode, result, timeToClose = 2000) => {
setTimeout(resetLoadingDialog, timeToClose);
};
const failedPluginsDict = ref({});
const getExtensions = async () => {
loading_.value = true;
try {
const res = await axios.get("/api/plugin/get");
const res = await axios.get("/api/plugin/get");
Object.assign(extension_data, res.data);
const failRes = await axios.get("/api/plugin/source/get-failed-plugins");
failedPluginsDict.value = failRes.data.data || {};
checkUpdate();
} catch (err) {
toast(err, "error");
@@ -370,6 +376,36 @@ const getExtensions = async () => {
}
};
const handleReloadAllFailed = async () => {
const dirNames = Object.keys(failedPluginsDict.value);
if (dirNames.length === 0) {
toast("没有需要重载的失败插件", "info");
return;
}
loading_.value = true;
try {
const promises = dirNames.map(dir =>
axios.post("/api/plugin/reload-failed", { dir_name: dir })
);
await Promise.all(promises);
toast("已尝试重载所有失败插件", "success");
// message
extension_data.message = "";
//
await getExtensions();
} catch (e) {
console.error("重载失败:", e);
toast("批量重载过程中出现错误", "error");
} finally {
loading_.value = false;
}
};
const checkUpdate = () => {
const onlinePluginsMap = new Map();
const onlinePluginsNameMap = new Map();
@@ -1273,6 +1309,15 @@ watch(activeTab, (newTab) => {
</p>
</v-card-text>
<v-card-actions>
<v-btn
color="error"
variant="tonal"
prepend-icon="mdi-refresh"
@click="handleReloadAllFailed"
>
尝试一键重载修复
</v-btn>
<v-spacer></v-spacer>
<v-btn
color="primary"
+16 -2
View File
@@ -70,7 +70,7 @@
{{ tm('resources.title') }}
</div>
<v-row>
<v-col cols="12" sm="6">
<v-col cols="12" sm="4">
<!-- GitHub Card -->
<v-card variant="outlined" class="h-100 pa-4 d-flex flex-column"
href="https://github.com/AstrBotDevs/AstrBot/" target="_blank">
@@ -84,7 +84,7 @@
</v-card>
</v-col>
<v-col cols="12" sm="6">
<v-col cols="12" sm="4">
<!-- Docs Card -->
<v-card variant="outlined" class="h-100 pa-4 d-flex flex-column" href="https://docs.astrbot.app"
target="_blank">
@@ -98,6 +98,20 @@
</v-card>
</v-col>
<v-col cols="12" sm="4">
<!-- Afdian Card -->
<v-card variant="outlined" class="h-100 pa-4 d-flex flex-column"
href="https://afdian.com/a/astrbot_team" target="_blank">
<div class="d-flex align-center mb-3">
<v-icon size="32" class="mr-3">mdi-hand-heart</v-icon>
<span class="text-h6 font-weight-bold">{{ tm('resources.afdianTitle') }}</span>
</div>
<p class="text-body-2 text-medium-emphasis mb-0">
{{ tm('resources.afdianDesc') }}
</p>
</v-card>
</v-col>
</v-row>
</v-card>
</v-col>
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "astrbot-desktop",
"version": "4.15.0",
"version": "4.17.0",
"description": "AstrBot desktop wrapper",
"private": true,
"main": "main.js",
+2 -2
View File
@@ -1,6 +1,6 @@
[project]
name = "AstrBot"
version = "4.15.0"
version = "4.17.0"
description = "Easy-to-use multi-platform LLM chatbot and development framework"
readme = "README.md"
requires-python = ">=3.12"
@@ -17,7 +17,7 @@ dependencies = [
"beautifulsoup4>=4.13.4",
"certifi>=2025.4.26",
"chardet~=5.1.0",
"colorlog>=6.9.0",
"loguru>=0.7.2",
"cryptography>=44.0.3",
"dashscope>=1.23.2",
"defusedxml>=0.7.1",
+2 -2
View File
@@ -10,7 +10,7 @@ apscheduler>=3.11.0
beautifulsoup4>=4.13.4
certifi>=2025.4.26
chardet~=5.1.0
colorlog>=6.9.0
loguru>=0.7.2
cryptography>=44.0.3
dashscope>=1.23.2
defusedxml>=0.7.1
@@ -53,4 +53,4 @@ jieba>=0.42.1
markitdown-no-magika[docx,xls,xlsx]>=0.1.2
xinference-client
tenacity>=9.1.2
shipyard-python-sdk>=0.2.4
shipyard-python-sdk>=0.2.4
+73
View File
@@ -90,6 +90,21 @@ class MockToolExecutor:
return generator()
class MockFailingProvider(MockProvider):
async def text_chat(self, **kwargs) -> LLMResponse:
self.call_count += 1
raise RuntimeError("primary provider failed")
class MockErrProvider(MockProvider):
async def text_chat(self, **kwargs) -> LLMResponse:
self.call_count += 1
return LLMResponse(
role="err",
completion_text="primary provider returned error",
)
class MockHooks(BaseAgentRunHooks):
"""模拟钩子函数"""
@@ -321,6 +336,64 @@ async def test_hooks_called_with_max_step(
assert mock_hooks.tool_end_called, "on_tool_end应该被调用"
@pytest.mark.asyncio
async def test_fallback_provider_used_when_primary_raises(
runner, provider_request, mock_tool_executor, mock_hooks
):
primary_provider = MockFailingProvider()
fallback_provider = MockProvider()
fallback_provider.should_call_tools = False
await runner.reset(
provider=primary_provider,
request=provider_request,
run_context=ContextWrapper(context=None),
tool_executor=mock_tool_executor,
agent_hooks=mock_hooks,
streaming=False,
fallback_providers=[fallback_provider],
)
async for _ in runner.step_until_done(5):
pass
final_resp = runner.get_final_llm_resp()
assert final_resp is not None
assert final_resp.role == "assistant"
assert final_resp.completion_text == "这是我的最终回答"
assert primary_provider.call_count == 1
assert fallback_provider.call_count == 1
@pytest.mark.asyncio
async def test_fallback_provider_used_when_primary_returns_err(
runner, provider_request, mock_tool_executor, mock_hooks
):
primary_provider = MockErrProvider()
fallback_provider = MockProvider()
fallback_provider.should_call_tools = False
await runner.reset(
provider=primary_provider,
request=provider_request,
run_context=ContextWrapper(context=None),
tool_executor=mock_tool_executor,
agent_hooks=mock_hooks,
streaming=False,
fallback_providers=[fallback_provider],
)
async for _ in runner.step_until_done(5):
pass
final_resp = runner.get_final_llm_resp()
assert final_resp is not None
assert final_resp.role == "assistant"
assert final_resp.completion_text == "这是我的最终回答"
assert primary_provider.call_count == 1
assert fallback_provider.call_count == 1
if __name__ == "__main__":
# 运行测试
pytest.main([__file__, "-v"])