Compare commits

..

1 Commits

Author SHA1 Message Date
Soulter fcd18503cb chore: bump version to 4.16.0 2026-02-12 22:55:10 +08:00
52 changed files with 416 additions and 2403 deletions
+1 -1
View File
@@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.12'
python-version: '3.10'
- name: Install UV
run: pip install uv
-1
View File
@@ -22,7 +22,6 @@ 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
-4
View File
@@ -81,10 +81,6 @@ uv tool install astrbot
astrbot
```
#### 启动器一键部署(AstrBot Launcher
进入 [AstrBot Launcher](https://github.com/Raven95676/astrbot-launcher) 仓库,在 Releases 页最新版本下找到对应的系统安装包安装即可。
#### 宝塔面板部署
AstrBot 与宝塔面板合作,已上架至宝塔面板。
+1 -1
View File
@@ -1 +1 @@
__version__ = "4.17.3"
__version__ = "4.16.0"
@@ -91,7 +91,6 @@ 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
@@ -121,17 +120,6 @@ 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
@@ -178,19 +166,16 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
self.stats = AgentStats()
self.stats.start_time = time.time()
async def _iter_llm_responses(
self, *, include_model: bool = True
) -> T.AsyncGenerator[LLMResponse, None]:
async def _iter_llm_responses(self) -> 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
@@ -198,77 +183,6 @@ 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:
@@ -301,7 +215,7 @@ class ToolLoopAgentRunner(BaseAgentRunner[TContext]):
)
self._simple_print_message_role("[AftCompact]")
async for llm_response in self._iter_llm_responses_with_fallback():
async for llm_response in self._iter_llm_responses():
if llm_response.is_chunk:
# update ttft
if self.stats.time_to_first_token == 0:
-47
View File
@@ -42,7 +42,6 @@ from astrbot.core.message.components import File, Image, Reply
from astrbot.core.platform.astr_message_event import AstrMessageEvent
from astrbot.core.provider import Provider
from astrbot.core.provider.entities import ProviderRequest
from astrbot.core.provider.manager import llm_tools
from astrbot.core.skills.skill_manager import SkillManager, build_skills_prompt
from astrbot.core.star.context import Context
from astrbot.core.star.star_handler import star_map
@@ -770,14 +769,6 @@ def _plugin_tool_fix(event: AstrMessageEvent, req: ProviderRequest) -> None:
if plugin.name in event.plugins_name or plugin.reserved:
new_tool_set.add_tool(tool)
req.func_tool = new_tool_set
else:
# mcp tools
tool_set = req.func_tool
if not tool_set:
tool_set = ToolSet()
for tool in llm_tools.func_list:
if isinstance(tool, MCPTool):
tool_set.add_tool(tool)
async def _handle_webchat(
@@ -879,41 +870,6 @@ 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,
@@ -1137,9 +1093,6 @@ 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:
+6 -13
View File
@@ -5,9 +5,8 @@ import mcp
from astrbot.api import FunctionTool
from astrbot.core.agent.run_context import ContextWrapper
from astrbot.core.agent.tool import ToolExecResult
from astrbot.core.astr_agent_context import AstrAgentContext, AstrMessageEvent
from astrbot.core.astr_agent_context import AstrAgentContext
from astrbot.core.computer.computer_client import get_booter, get_local_booter
from astrbot.core.message.message_event_result import MessageChain
param_schema = {
"type": "object",
@@ -26,7 +25,7 @@ param_schema = {
}
async def handle_result(result: dict, event: AstrMessageEvent) -> ToolExecResult:
def handle_result(result: dict) -> ToolExecResult:
data = result.get("data", {})
output = data.get("output", {})
error = data.get("error", "")
@@ -45,9 +44,6 @@ async def handle_result(result: dict, event: AstrMessageEvent) -> ToolExecResult
type="image", data=img["image/png"], mimeType="image/png"
)
)
if event.get_platform_name() == "webchat":
await event.send(message=MessageChain().base64_image(img["image/png"]))
if text:
resp.content.append(mcp.types.TextContent(type="text", text=text))
@@ -72,7 +68,7 @@ class PythonTool(FunctionTool):
)
try:
result = await sb.python.exec(code, silent=silent)
return await handle_result(result, context.context.event)
return handle_result(result)
except Exception as e:
return f"Error executing code: {str(e)}"
@@ -88,14 +84,11 @@ class LocalPythonTool(FunctionTool):
self, context: ContextWrapper[AstrAgentContext], code: str, silent: bool = False
) -> ToolExecResult:
if context.context.event.role != "admin":
return (
"error: Permission denied. Local Python execution is only allowed for admin users. "
"Tell user to set admins in AstrBot WebUI by adding their user ID to the admins list if they need this feature."
f"User's ID is: {context.context.event.get_sender_id()}. User's ID can be found by using /sid command."
)
return "error: Permission denied. Local Python execution is only allowed for admin users. Tell user to set admins in AstrBot WebUI."
sb = get_local_booter()
try:
result = await sb.python.exec(code, silent=silent)
return await handle_result(result, context.context.event)
return handle_result(result)
except Exception as e:
return f"Error executing code: {str(e)}"
+1 -5
View File
@@ -47,11 +47,7 @@ class ExecuteShellTool(FunctionTool):
env: dict = {},
) -> ToolExecResult:
if context.context.event.role != "admin":
return (
"error: Permission denied. Local shell execution is only allowed for admin users. "
"Tell user to set admins in AstrBot WebUI by adding their user ID to the admins list if they need this feature."
f"User's ID is: {context.context.event.get_sender_id()}. User's ID can be found by using /sid command."
)
return "error: Permission denied. Shell execution is only allowed for admin users. Tell user to Set admins in AstrBot WebUI."
if self.is_local:
sb = get_local_booter()
+3 -71
View File
@@ -5,7 +5,7 @@ from typing import Any, TypedDict
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
VERSION = "4.17.3"
VERSION = "4.16.0"
DB_PATH = os.path.join(get_astrbot_data_path(), "data_v4.db")
WEBHOOK_SUPPORTED_PLATFORMS = [
@@ -15,7 +15,6 @@ WEBHOOK_SUPPORTED_PLATFORMS = [
"wecom_ai_bot",
"slack",
"lark",
"line",
]
# 默认配置
@@ -68,7 +67,6 @@ 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": ["*"], # "*" 表示使用所有可用的提供者
@@ -196,12 +194,6 @@ 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": {
@@ -423,7 +415,6 @@ 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",
@@ -1029,18 +1020,6 @@ CONFIG_METADATA_2 = {
"proxy": "",
"custom_headers": {},
},
"NVIDIA": {
"id": "nvidia",
"provider": "nvidia",
"type": "openai_chat_completion",
"provider_type": "chat_completion",
"enable": True,
"key": [],
"api_base": "https://integrate.api.nvidia.com/v1",
"timeout": 120,
"proxy": "",
"custom_headers": {},
},
"Azure OpenAI": {
"id": "azure_openai",
"provider": "azure",
@@ -2226,10 +2205,6 @@ CONFIG_METADATA_2 = {
"default_provider_id": {
"type": "string",
},
"fallback_chat_models": {
"type": "list",
"items": {"type": "string"},
},
"wake_prefix": {
"type": "string",
},
@@ -2424,19 +2399,6 @@ 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}},
@@ -2540,22 +2502,15 @@ 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",
@@ -3451,29 +3406,6 @@ 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",
+295 -252
View File
@@ -1,4 +1,24 @@
"""日志系统,统一将标准 logging 输出转发到 loguru。"""
"""日志系统, 用于支持核心组件和插件的日志记录, 提供了日志订阅功能
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, 订阅日志流
"""
import asyncio
import logging
@@ -7,59 +27,54 @@ import sys
import time
from asyncio import Queue
from collections import deque
from typing import TYPE_CHECKING
from logging.handlers import RotatingFileHandler
from loguru import logger as _raw_loguru_logger
import colorlog
from astrbot.core.config.default import VERSION
from astrbot.core.utils.astrbot_path import get_astrbot_data_path
# 日志缓存大小
CACHED_SIZE = 500
if TYPE_CHECKING:
from loguru import Record
# 日志颜色配置
log_color_config = {
"DEBUG": "green",
"INFO": "bold_cyan",
"WARNING": "bold_yellow",
"ERROR": "red",
"CRITICAL": "bold_red",
"RESET": "reset",
"asctime": "green",
}
class _RecordEnricherFilter(logging.Filter):
"""为 logging.LogRecord 注入 AstrBot 日志字段。"""
def is_plugin_path(pathname):
"""检查文件路径是否来自插件目录
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
Args:
pathname (str): 文件路径
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: str) -> str:
def get_short_level_name(level_name):
"""将日志级别名称转换为四个字母的缩写
Args:
level_name (str): 日志级别名称, 如 "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"
Returns:
str: 四个字母的日志级别缩写
"""
level_map = {
"DEBUG": "DBUG",
"INFO": "INFO",
@@ -70,75 +85,44 @@ def _get_short_level_name(level_name: str) -> str:
return level_map.get(level_name, level_name[:4].upper())
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:
@@ -148,13 +132,23 @@ class LogBroker:
class LogQueueHandler(logging.Handler):
"""日志处理器用于将日志消息发送到 LogBroker"""
"""日志处理器, 用于将日志消息发送到 LogBroker
继承自 logging.Handler
"""
def __init__(self, log_broker: LogBroker) -> None:
super().__init__()
self.log_broker = log_broker
def emit(self, record: logging.LogRecord) -> None:
def emit(self, record) -> None:
"""日志处理的入口方法, 接受一个日志记录, 转换为字符串后由 LogBroker 发布
这个方法会在每次日志记录时被调用
Args:
record (logging.LogRecord): 日志记录对象, 包含日志信息
"""
log_entry = self.format(record)
self.log_broker.publish(
{
@@ -166,16 +160,117 @@ class LogQueueHandler(logging.Handler):
class LogManager:
_LOGGER_HANDLER_FLAG = "_astrbot_loguru_handler"
_ENRICH_FILTER_FLAG = "_astrbot_enrich_filter"
"""日志管理器, 用于创建和配置日志记录器
_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,
}
提供了获取默认日志记录器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)
@classmethod
def _default_log_path(cls) -> str:
@@ -190,147 +285,79 @@ class LogManager:
return os.path.join(get_astrbot_data_path(), configured_path)
@classmethod
def _setup_loguru(cls) -> None:
if cls._configured:
return
_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)
def _get_file_handlers(cls, logger: logging.Logger) -> list[logging.Handler]:
return [
handler
for handler in logger.handlers
)
if not has_handler:
handler = _LoguruInterceptHandler()
setattr(handler, cls._LOGGER_HANDLER_FLAG, True)
logger.addHandler(handler)
if getattr(handler, cls._FILE_HANDLER_FLAG, False)
]
@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
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 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)
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_sink(cls, sink_id: int | None) -> None:
if sink_id is None:
return
try:
_loguru.remove(sink_id)
except ValueError:
pass
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_sink(
def _add_file_handler(
cls,
*,
logger: logging.Logger,
file_path: str,
level: int,
max_mb: int | None,
backup_count: int,
trace: bool,
) -> int:
max_mb: int | None = None,
backup_count: int = 3,
trace: bool = False,
) -> None:
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 = (
backup_count if rotation and backup_count and backup_count > 0 else None
)
if trace:
return _loguru.add(
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,
level="INFO",
format="[{time:YYYY-MM-DD HH:mm:ss.SSS}] {message}",
maxBytes=max_bytes,
backupCount=backup_count,
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),
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,
)
logger.addHandler(file_handler)
@classmethod
def configure_logger(
@@ -339,6 +366,13 @@ class LogManager:
config: dict | None,
override_level: str | None = None,
) -> None:
"""根据配置设置日志级别和文件日志。
Args:
logger: 需要配置的 logger
config: 配置字典
override_level: 若提供,将覆盖配置中的日志级别
"""
if not config:
return
@@ -349,6 +383,7 @@ 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))
@@ -359,25 +394,27 @@ class LogManager:
file_path = config.get("log_file_path")
max_mb = config.get("log_file_max_mb")
cls._remove_sink(cls._file_sink_id)
cls._file_sink_id = None
file_path = cls._resolve_log_path(file_path)
existing = cls._get_file_handlers(logger)
if not enable_file:
cls._remove_file_handlers(logger)
return
try:
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,
)
except Exception as e:
logger.error(f"Failed to add file sink: {e}")
# 如果已有文件处理器且路径一致,则仅同步级别
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)
@classmethod
def configure_trace_logger(cls, config: dict | None) -> None:
"""为 trace 事件配置独立的文件日志,不向控制台输出。"""
if not config:
return
@@ -392,22 +429,28 @@ 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
cls._remove_sink(cls._trace_sink_id)
cls._trace_sink_id = None
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)
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,
cls._add_file_handler(
trace_logger,
file_path,
max_mb=max_mb,
backup_count=3,
trace=True,
)
+22 -26
View File
@@ -25,14 +25,10 @@ import asyncio
import base64
import json
import os
import sys
import uuid
from enum import Enum
if sys.version_info >= (3, 14):
from pydantic import BaseModel
else:
from pydantic.v1 import BaseModel
from pydantic.v1 import BaseModel
from astrbot.core import astrbot_config, file_token_service, logger
from astrbot.core.utils.astrbot_path import get_astrbot_temp_path
@@ -89,7 +85,7 @@ class BaseMessageComponent(BaseModel):
class Plain(BaseMessageComponent):
type: ComponentType = ComponentType.Plain
type = ComponentType.Plain
text: str
convert: bool | None = True
@@ -104,7 +100,7 @@ class Plain(BaseMessageComponent):
class Face(BaseMessageComponent):
type: ComponentType = ComponentType.Face
type = ComponentType.Face
id: int
def __init__(self, **_) -> None:
@@ -112,7 +108,7 @@ class Face(BaseMessageComponent):
class Record(BaseMessageComponent):
type: ComponentType = ComponentType.Record
type = ComponentType.Record
file: str | None = ""
magic: bool | None = False
url: str | None = ""
@@ -219,7 +215,7 @@ class Record(BaseMessageComponent):
class Video(BaseMessageComponent):
type: ComponentType = ComponentType.Video
type = ComponentType.Video
file: str
cover: str | None = ""
c: int | None = 2
@@ -305,7 +301,7 @@ class Video(BaseMessageComponent):
class At(BaseMessageComponent):
type: ComponentType = ComponentType.At
type = ComponentType.At
qq: int | str # 此处str为all时代表所有人
name: str | None = ""
@@ -327,28 +323,28 @@ class AtAll(At):
class RPS(BaseMessageComponent): # TODO
type: ComponentType = ComponentType.RPS
type = ComponentType.RPS
def __init__(self, **_) -> None:
super().__init__(**_)
class Dice(BaseMessageComponent): # TODO
type: ComponentType = ComponentType.Dice
type = ComponentType.Dice
def __init__(self, **_) -> None:
super().__init__(**_)
class Shake(BaseMessageComponent): # TODO
type: ComponentType = ComponentType.Shake
type = ComponentType.Shake
def __init__(self, **_) -> None:
super().__init__(**_)
class Share(BaseMessageComponent):
type: ComponentType = ComponentType.Share
type = ComponentType.Share
url: str
title: str
content: str | None = ""
@@ -359,7 +355,7 @@ class Share(BaseMessageComponent):
class Contact(BaseMessageComponent): # TODO
type: ComponentType = ComponentType.Contact
type = ComponentType.Contact
_type: str # type 字段冲突
id: int | None = 0
@@ -368,7 +364,7 @@ class Contact(BaseMessageComponent): # TODO
class Location(BaseMessageComponent): # TODO
type: ComponentType = ComponentType.Location
type = ComponentType.Location
lat: float
lon: float
title: str | None = ""
@@ -379,7 +375,7 @@ class Location(BaseMessageComponent): # TODO
class Music(BaseMessageComponent):
type: ComponentType = ComponentType.Music
type = ComponentType.Music
_type: str
id: int | None = 0
url: str | None = ""
@@ -396,7 +392,7 @@ class Music(BaseMessageComponent):
class Image(BaseMessageComponent):
type: ComponentType = ComponentType.Image
type = ComponentType.Image
file: str | None = ""
_type: str | None = ""
subType: int | None = 0
@@ -511,7 +507,7 @@ class Image(BaseMessageComponent):
class Reply(BaseMessageComponent):
type: ComponentType = ComponentType.Reply
type = ComponentType.Reply
id: str | int
"""所引用的消息 ID"""
chain: list["BaseMessageComponent"] | None = []
@@ -547,7 +543,7 @@ class Poke(BaseMessageComponent):
class Forward(BaseMessageComponent):
type: ComponentType = ComponentType.Forward
type = ComponentType.Forward
id: str
def __init__(self, **_) -> None:
@@ -557,7 +553,7 @@ class Forward(BaseMessageComponent):
class Node(BaseMessageComponent):
"""群合并转发消息"""
type: ComponentType = ComponentType.Node
type = ComponentType.Node
id: int | None = 0 # 忽略
name: str | None = "" # qq昵称
uin: str | None = "0" # qq号
@@ -609,7 +605,7 @@ class Node(BaseMessageComponent):
class Nodes(BaseMessageComponent):
type: ComponentType = ComponentType.Nodes
type = ComponentType.Nodes
nodes: list[Node]
def __init__(self, nodes: list[Node], **_) -> None:
@@ -635,7 +631,7 @@ class Nodes(BaseMessageComponent):
class Json(BaseMessageComponent):
type: ComponentType = ComponentType.Json
type = ComponentType.Json
data: dict
def __init__(self, data: str | dict, **_) -> None:
@@ -645,14 +641,14 @@ class Json(BaseMessageComponent):
class Unknown(BaseMessageComponent):
type: ComponentType = ComponentType.Unknown
type = ComponentType.Unknown
text: str
class File(BaseMessageComponent):
"""文件消息段"""
type: ComponentType = ComponentType.File
type = ComponentType.File
name: str | None = "" # 名字
file_: str | None = "" # 本地路径
url: str | None = "" # url
@@ -787,7 +783,7 @@ class File(BaseMessageComponent):
class WechatEmoji(BaseMessageComponent):
type: ComponentType = ComponentType.WechatEmoji
type = ComponentType.WechatEmoji
md5: str | None = ""
md5_len: int | None = 0
cdnurl: str | None = ""
-4
View File
@@ -176,10 +176,6 @@ 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库 中安装依赖库。",
@@ -1,474 +0,0 @@
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)
@@ -1,203 +0,0 @@
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
@@ -1,285 +0,0 @@
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)
+3 -85
View File
@@ -323,8 +323,7 @@ class ProviderOpenAIOfficial(Provider):
llm_response.reasoning_content = reasoning
_y = True
if delta.content:
# Don't strip streaming chunks to preserve spaces between words
completion_text = self._normalize_content(delta.content, strip=False)
completion_text = delta.content
llm_response.result_chain = MessageChain(
chain=[Comp.Plain(completion_text)],
)
@@ -372,86 +371,6 @@ class ProviderOpenAIOfficial(Provider):
output=completion_tokens,
)
@staticmethod
def _normalize_content(raw_content: Any, strip: bool = True) -> str:
"""Normalize content from various formats to plain string.
Some LLM providers return content as list[dict] format
like [{'type': 'text', 'text': '...'}] instead of
plain string. This method handles both formats.
Args:
raw_content: The raw content from LLM response, can be str, list, or other.
strip: Whether to strip whitespace from the result. Set to False for
streaming chunks to preserve spaces between words.
Returns:
Normalized plain text string.
"""
if isinstance(raw_content, list):
# Check if this looks like OpenAI content-part format
# Only process if at least one item has {'type': 'text', 'text': ...} structure
has_content_part = any(
isinstance(part, dict) and part.get("type") == "text"
for part in raw_content
)
if has_content_part:
text_parts = []
for part in raw_content:
if isinstance(part, dict) and part.get("type") == "text":
text_val = part.get("text", "")
# Coerce to str in case text is null or non-string
text_parts.append(str(text_val) if text_val is not None else "")
return "".join(text_parts)
# Not content-part format, return string representation
return str(raw_content)
if isinstance(raw_content, str):
content = raw_content.strip() if strip else raw_content
# Check if the string is a JSON-encoded list (e.g., "[{'type': 'text', ...}]")
# This can happen when streaming concatenates content that was originally list format
# Only check if it looks like a complete JSON array (requires strip for check)
check_content = raw_content.strip()
if (
check_content.startswith("[")
and check_content.endswith("]")
and len(check_content) < 8192
):
try:
# First try standard JSON parsing
parsed = json.loads(check_content)
except json.JSONDecodeError:
# If that fails, try parsing as Python literal (handles single quotes)
# This is safer than blind replace("'", '"') which corrupts apostrophes
try:
import ast
parsed = ast.literal_eval(check_content)
except (ValueError, SyntaxError):
parsed = None
if isinstance(parsed, list):
# Only convert if it matches OpenAI content-part schema
# i.e., at least one item has {'type': 'text', 'text': ...}
has_content_part = any(
isinstance(part, dict) and part.get("type") == "text"
for part in parsed
)
if has_content_part:
text_parts = []
for part in parsed:
if isinstance(part, dict) and part.get("type") == "text":
text_val = part.get("text", "")
# Coerce to str in case text is null or non-string
text_parts.append(
str(text_val) if text_val is not None else ""
)
if text_parts:
return "".join(text_parts)
return content
return str(raw_content)
async def _parse_openai_completion(
self, completion: ChatCompletion, tools: ToolSet | None
) -> LLMResponse:
@@ -464,7 +383,8 @@ class ProviderOpenAIOfficial(Provider):
# parse the text completion
if choice.message.content is not None:
completion_text = self._normalize_content(choice.message.content)
# text completion
completion_text = str(choice.message.content).strip()
# specially, some providers may set <think> tags around reasoning content in the completion text,
# we use regex to remove them, and store then in reasoning_content field
reasoning_pattern = re.compile(r"<think>(.*?)</think>", re.DOTALL)
@@ -474,8 +394,6 @@ class ProviderOpenAIOfficial(Provider):
[match.strip() for match in matches],
)
completion_text = reasoning_pattern.sub("", completion_text).strip()
# Also clean up orphan </think> tags that may leak from some models
completion_text = re.sub(r"</think>\s*$", "", completion_text).strip()
llm_response.result_chain = MessageChain().message(completion_text)
# parse the reasoning content if any
@@ -20,7 +20,6 @@ class PlatformAdapterType(enum.Flag):
WEIXIN_OFFICIAL_ACCOUNT = enum.auto()
SATORI = enum.auto()
MISSKEY = enum.auto()
LINE = enum.auto()
ALL = (
AIOCQHTTP
| QQOFFICIAL
@@ -35,7 +34,6 @@ class PlatformAdapterType(enum.Flag):
| WEIXIN_OFFICIAL_ACCOUNT
| SATORI
| MISSKEY
| LINE
)
@@ -53,7 +51,6 @@ ADAPTER_NAME_2_TYPE = {
"weixin_official_account": PlatformAdapterType.WEIXIN_OFFICIAL_ACCOUNT,
"satori": PlatformAdapterType.SATORI,
"misskey": PlatformAdapterType.MISSKEY,
"line": PlatformAdapterType.LINE,
}
+11 -44
View File
@@ -62,9 +62,6 @@ 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())
@@ -330,28 +327,6 @@ 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):
"""重新加载插件
@@ -513,16 +488,6 @@ class PluginManager:
)
logger.info(metadata)
metadata.config = plugin_config
p_name = (metadata.name or "unknown").lower().replace("/", "_")
p_author = (metadata.author or "unknown").lower().replace("/", "_")
plugin_id = f"{p_author}/{p_name}"
# 在实例化前注入类属性,保证插件 __init__ 可读取这些值
if metadata.star_cls_type:
setattr(metadata.star_cls_type, "name", p_name)
setattr(metadata.star_cls_type, "author", p_author)
setattr(metadata.star_cls_type, "plugin_id", plugin_id)
if path not in inactivated_plugins:
# 只有没有禁用插件时才实例化插件类
if plugin_config and metadata.star_cls_type:
@@ -540,10 +505,17 @@ class PluginManager:
context=self.context,
)
if metadata.star_cls:
setattr(metadata.star_cls, "name", p_name)
setattr(metadata.star_cls, "author", p_author)
setattr(metadata.star_cls, "plugin_id", plugin_id)
p_name = (metadata.name or "unknown").lower().replace("/", "_")
p_author = (
(metadata.author or "unknown").lower().replace("/", "_")
)
setattr(metadata.star_cls, "name", p_name)
setattr(metadata.star_cls, "author", p_author)
setattr(
metadata.star_cls,
"plugin_id",
f"{p_author}/{p_name}",
)
else:
logger.info(f"插件 {metadata.name} 已被禁用。")
@@ -691,11 +663,6 @@ 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[:]:
+2 -2
View File
@@ -148,8 +148,8 @@ class AstrBotUpdator(RepoZipUpdator):
update_data = await self.fetch_release_info(self.ASTRBOT_RELEASE_API, latest)
file_url = None
if os.environ.get("ASTRBOT_CLI") or os.environ.get("ASTRBOT_LAUNCHER"):
raise Exception("不支持更新此方式启动的AstrBot") # 避免版本管理混乱
if os.environ.get("ASTRBOT_CLI"):
raise Exception("不支持更新CLI启动的AstrBot") # 避免版本管理混乱
if latest:
latest_version = update_data[0]["tag_name"]
+1 -17
View File
@@ -1,4 +1,3 @@
import os
import uuid
from astrbot.core import astrbot_config, logger
@@ -21,20 +20,6 @@ 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 信息日志
@@ -53,13 +38,12 @@ 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"{scheme}://<your-ip>:{_get_dashboard_port()}/api/platform/webhook/{webhook_uuid}\n"
f"http://<your-ip>:{_get_dashboard_port()}/api/platform/webhook/{webhook_uuid}\n"
f"{webhook_url}\n"
"====================\n"
)
-33
View File
@@ -54,13 +54,11 @@ 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
@@ -77,33 +75,6 @@ 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 (
@@ -363,10 +334,6 @@ 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 = []
+1 -2
View File
@@ -295,15 +295,14 @@ 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.md",
base_path / "FIRST_NOTICE.en-US.md",
base_path / "FIRST_NOTICE.md",
],
)
+9 -60
View File
@@ -2,7 +2,6 @@ import asyncio
import logging
import os
import socket
from pathlib import Path
from typing import Protocol, cast
import jwt
@@ -37,12 +36,6 @@ 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,
@@ -208,33 +201,23 @@ 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 dashboard_config.get("port", 6185)
or self.core_lifecycle.astrbot_config["dashboard"].get("port", 6185)
)
host = (
os.environ.get("DASHBOARD_HOST")
or os.environ.get("ASTRBOT_DASHBOARD_HOST")
or dashboard_config.get("host", "0.0.0.0")
or self.core_lifecycle.astrbot_config["dashboard"].get("host", "0.0.0.0")
)
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"
enable = self.core_lifecycle.astrbot_config["dashboard"].get("enable", True)
if not enable:
logger.info("WebUI 已被禁用")
return None
logger.info(f"正在启动 WebUI, 监听地址: {scheme}://{host}:{port}")
logger.info(f"正在启动 WebUI, 监听地址: http://{host}:{port}")
if host == "0.0.0.0":
logger.info(
"提示: WebUI 将监听所有网络接口,请注意安全。(可在 data/cmd_config.json 中配置 dashboard.host 以修改 host",
@@ -262,9 +245,9 @@ class AstrBotDashboard:
raise Exception(f"端口 {port} 已被占用")
parts = [f"\n ✨✨✨\n AstrBot v{VERSION} WebUI 已启动,可访问\n\n"]
parts.append(f" ➜ 本地: {scheme}://localhost:{port}\n")
parts.append(f" ➜ 本地: http://localhost:{port}\n")
for ip in ip_addr:
parts.append(f" ➜ 网络: {scheme}://{ip}:{port}\n")
parts.append(f" ➜ 网络: http://{ip}:{port}\n")
parts.append(" ➜ 默认用户名和密码: astrbot\n ✨✨✨\n")
display = "".join(parts)
@@ -278,45 +261,11 @@ 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 = dashboard_config.get("disable_access_log", True)
disable_access_log = self.core_lifecycle.astrbot_config.get(
"dashboard", {}
).get("disable_access_log", True)
if disable_access_log:
config.accesslog = None
else:
-29
View File
@@ -1,29 +0,0 @@
## 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))
-34
View File
@@ -1,34 +0,0 @@
## What's Changed
hotfix of 4.17.0
- 修复:当开启了 “启用文件日志” 后,无法启动 AstrBot,报错 `ValueError: Invalid unit value while parsing duration: 'files'`。这是由于日志轮转设置中保留配置错误导致的,已通过根据备份数量正确设置保留参数进行修复。
- fix: When "Enable file logging" is turned on, AstrBot fails to start with error `ValueError: Invalid unit value while parsing duration: 'files'`. This is due to an incorrect retention configuration in the log rotation setup, which has been fixed by properly setting the retention parameter based on backup count.
### 新增
- 新增 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))
-8
View File
@@ -1,8 +0,0 @@
## What's Changed
hotfix of 4.17.0
- 修复:MCP 服务器的 Tools 没有被正确添加到上下文中。
- 修复:Electron 桌面应用部署时,系统自带插件未被正确加载的问题。
- fix: Tools from MCP server were not properly added to context.
- fix: built-in plugins were not properly loaded in Electron desktop application deployment.
-27
View File
@@ -1,27 +0,0 @@
## What's Changed
### 修复
- ‼️ 修复 Python 3.14 环境下 `'Plain' object has no attribute 'text'` 报错问题 ([#5154](https://github.com/AstrBotDevs/AstrBot/issues/5154))。
- ‼️ 修复插件元数据处理流程:在实例化前注入必要属性,避免初始化阶段元数据缺失 ([#5155](https://github.com/AstrBotDevs/AstrBot/issues/5155))。
- 修复桌面端后端构建中 AstrBot 内置插件运行时依赖未打包的问题 ([#5146](https://github.com/AstrBotDevs/AstrBot/issues/5146))。
- 修复通过 AstrBot Launcher 启动时仍被检测并触发更新的问题。
### 优化
- Webchat 下,使用 `astrbot_execute_ipython` 工具如果返回了图片,会自动将图片发送到聊天中。
### 其他
- 执行 `ruff format` 代码格式整理。
## What's Changed (EN)
### Fixes
- ‼️ Fixed plugin metadata handling by injecting required attributes before instantiation to avoid missing metadata during initialization ([#5155](https://github.com/AstrBotDevs/AstrBot/issues/5155)).
- ‼️ Fixed `'Plain' object has no attribute 'text'` error when using Python 3.14 ([#5154](https://github.com/AstrBotDevs/AstrBot/issues/5154)).
- Fixed missing runtime dependencies for built-in plugins in desktop backend builds ([#5146](https://github.com/AstrBotDevs/AstrBot/issues/5146)).
- Fixed update checks being triggered when AstrBot is launched via AstrBot Launcher.
### Improvements
- In Webchat, when using the `astrbot_execute_ipython` tool, if an image is returned, it will automatically be sent to the chat.
### Others
- Applied `ruff format` code formatting.
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 KiB

@@ -14,13 +14,8 @@
</div>
<!-- 选择对话框 -->
<v-dialog
v-model="dialog"
:max-width="$vuetify.display.smAndDown ? undefined : '1000px'"
:min-width="$vuetify.display.smAndDown ? undefined : '800px'"
scrollable
>
<v-card class="selector-dialog-card" :class="{ 'selector-dialog-card-mobile': $vuetify.display.smAndDown }">
<v-dialog v-model="dialog" max-width="1000px" min-width="800px">
<v-card class="selector-dialog-card">
<v-card-title class="dialog-title d-flex align-center py-4 px-5">
<v-icon class="mr-3" color="primary">mdi-account-circle</v-icon>
<span>{{ labels.dialogTitle || '选择项目' }}</span>
@@ -28,7 +23,7 @@
<v-divider />
<v-card-text class="selector-dialog-content pa-0">
<v-card-text class="pa-0" style="height: 600px; max-height: 80vh; overflow: hidden;">
<div class="selector-layout">
<!-- 左侧文件夹树 -->
<div class="folder-sidebar">
@@ -151,7 +146,7 @@
</div>
</v-card-text>
<v-card-actions class="selector-dialog-actions pa-4">
<v-card-actions class="pa-4">
<v-btn v-if="showCreateButton" variant="text" color="primary" prepend-icon="mdi-plus"
@click="$emit('create')">
{{ labels.createButton || '新建' }}
@@ -411,12 +406,6 @@ export default defineComponent({
overflow: hidden;
}
.selector-dialog-content {
height: 600px;
max-height: 80vh;
overflow: hidden;
}
.dialog-title {
font-size: 1.25rem;
font-weight: 500;
@@ -529,44 +518,21 @@ export default defineComponent({
}
@media (max-width: 600px) {
.selector-dialog-card-mobile {
border-radius: 0;
}
.selector-dialog-content {
height: calc(100vh - 132px);
max-height: none;
}
.dialog-title {
font-size: 1.05rem;
padding: 12px 16px !important;
}
.selector-dialog-actions {
padding: 12px 16px !important;
gap: 8px;
}
.selector-dialog-actions .v-btn {
min-width: 0;
}
.selector-layout {
flex-direction: column;
height: 100%;
max-height: none;
height: auto;
max-height: 500px;
}
.folder-sidebar {
width: 100%;
border-right: none;
border-bottom: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
max-height: 35vh;
max-height: 150px;
}
.items-list {
max-height: none;
max-height: 300px;
}
}
</style>
@@ -4,7 +4,7 @@
<div class="d-flex align-center ga-2">
<h3 class="mb-0">{{ tm('providerSources.title') }}</h3>
</div>
<StyledMenu>
<v-menu>
<template #activator="{ props }">
<v-btn
v-bind="props"
@@ -17,61 +17,19 @@
{{ tm('providerSources.add') }}
</v-btn>
</template>
<v-list-item
v-for="sourceType in availableSourceTypes"
:key="sourceType.value"
class="styled-menu-item"
@click="emitAddSource(sourceType.value)"
>
<template #prepend>
<v-avatar size="18" rounded="0" class="me-2">
<v-img v-if="sourceType.icon" :src="sourceType.icon" alt="provider icon" cover></v-img>
<v-icon v-else size="16">mdi-shape-outline</v-icon>
</v-avatar>
</template>
<v-list-item-title>{{ sourceType.label }}</v-list-item-title>
</v-list-item>
</StyledMenu>
<v-list density="compact">
<v-list-item
v-for="sourceType in availableSourceTypes"
:key="sourceType.value"
@click="emitAddSource(sourceType.value)"
>
<v-list-item-title>{{ sourceType.label }}</v-list-item-title>
</v-list-item>
</v-list>
</v-menu>
</div>
<div v-if="isMobile && displayedProviderSources.length > 0" class="px-4 pb-3">
<div class="d-flex align-center ga-2">
<v-select
:model-value="selectedId"
:items="mobileSourceItems"
item-title="label"
item-value="value"
:label="tm('providerSources.selectCreated')"
variant="solo-filled"
density="comfortable"
flat
hide-details
class="mobile-source-select"
@update:model-value="onMobileSourceChange"
>
<template #item="{ props: itemProps, item }">
<v-list-item v-bind="itemProps">
<template #prepend>
<v-avatar size="18" rounded="0" class="me-2">
<v-img v-if="item.raw.icon" :src="item.raw.icon" alt="provider icon" cover></v-img>
<v-icon v-else size="16">mdi-shape-outline</v-icon>
</v-avatar>
</template>
</v-list-item>
</template>
</v-select>
<v-btn
v-if="selectedProviderSource"
icon="mdi-delete"
variant="text"
size="small"
color="error"
@click.stop="emitDeleteSource(selectedProviderSource)"
></v-btn>
</div>
</div>
<div v-else-if="displayedProviderSources.length > 0">
<div v-if="displayedProviderSources.length > 0">
<v-list class="provider-source-list" nav density="compact" lines="two">
<v-list-item
v-for="source in displayedProviderSources"
@@ -88,7 +46,7 @@
<v-icon v-else size="32">mdi-creation</v-icon>
</v-avatar>
</template>
<v-list-item-title class="font-weight-bold mb-1" style="font-family: Arial, Helvetica, sans-serif; font-size: 16px;">{{ getSourceDisplayName(source) }}</v-list-item-title>
<v-list-item-title class="font-weight-bold">{{ getSourceDisplayName(source) }}</v-list-item-title>
<v-list-item-subtitle class="text-truncate">{{ source.api_base || 'N/A' }}</v-list-item-subtitle>
<template #append>
<div class="d-flex align-center ga-1">
@@ -114,8 +72,6 @@
<script setup>
import { computed } from 'vue'
import { useDisplay } from 'vuetify'
import StyledMenu from '@/components/shared/StyledMenu.vue'
const props = defineProps({
displayedProviderSources: {
@@ -150,30 +106,13 @@ const emit = defineEmits([
'delete-provider-source'
])
const { smAndDown } = useDisplay()
const selectedId = computed(() => props.selectedProviderSource?.id || null)
const isMobile = computed(() => smAndDown.value)
const mobileSourceItems = computed(() =>
(props.displayedProviderSources || []).map((source) => ({
value: source.id,
label: props.getSourceDisplayName(source),
icon: props.resolveSourceIcon(source),
source
}))
)
const isActive = (source) => {
if (source.isPlaceholder) return false
return selectedId.value !== null && selectedId.value === source.id
}
const onMobileSourceChange = (sourceId) => {
const matched = mobileSourceItems.value.find((item) => item.value === sourceId)
if (matched?.source) {
emitSelectSource(matched.source)
}
}
const emitAddSource = (type) => emit('add-provider-source', type)
const emitSelectSource = (source) => emit('select-provider-source', source)
const emitDeleteSource = (source) => emit('delete-provider-source', source)
@@ -10,14 +10,6 @@
<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: #39C5BB; font-weight: bold;',
'\u001b[1;34m': 'color: #0000FF; font-weight: bold;',
'\u001b[1;36m': 'color: #00FFFF; font-weight: bold;',
'\u001b[1;33m': 'color: #FFFF00; font-weight: bold;',
'\u001b[31m': 'color: #FF0000;',
@@ -1,15 +1,11 @@
<template>
<v-dialog
v-model="showDialog"
:max-width="$vuetify.display.smAndDown ? undefined : '760px'"
scrollable
>
<v-card class="persona-form-card" :class="{ 'persona-form-card-mobile': $vuetify.display.smAndDown }">
<v-card-title class="persona-form-title text-h2">
<v-dialog v-model="showDialog" max-width="500px">
<v-card>
<v-card-title class="text-h2">
{{ editingPersona ? tm('dialog.edit.title') : tm('dialog.create.title') }}
</v-card-title>
<v-card-text class="persona-form-content">
<v-card-text>
<!-- 创建位置提示 -->
<v-alert
v-if="!editingPersona"
@@ -55,7 +51,7 @@
</v-radio>
</v-radio-group>
<div v-if="toolSelectValue === '1'" class="mt-3 selected-config-area">
<div v-if="toolSelectValue === '1'" class="mt-3 ml-8">
<!-- 工具搜索 -->
<v-text-field v-model="toolSearch" :label="tm('form.searchTools')"
@@ -182,7 +178,7 @@
<v-radio :label="tm('form.skillsSelectSpecific')" value="1"></v-radio>
</v-radio-group>
<div v-if="skillSelectValue === '1'" class="mt-3 selected-config-area">
<div v-if="skillSelectValue === '1'" class="mt-3 ml-8">
<v-text-field v-model="skillSearch" :label="tm('form.searchSkills')"
prepend-inner-icon="mdi-magnify" variant="outlined" density="compact"
hide-details clearable class="mb-3" />
@@ -292,7 +288,7 @@
</v-form>
</v-card-text>
<v-card-actions class="persona-form-actions">
<v-card-actions>
<v-btn v-if="editingPersona" color="error" variant="text" @click="deletePersona">
{{ tm('buttons.delete') }}
</v-btn>
@@ -803,32 +799,6 @@ export default {
</script>
<style scoped>
.persona-form-card {
border-radius: 12px;
overflow: hidden;
}
.persona-form-content {
max-height: min(78vh, 760px);
overflow-y: auto;
}
.persona-form-title {
line-height: 1.3;
}
.persona-form-actions {
position: sticky;
bottom: 0;
z-index: 2;
background: rgb(var(--v-theme-surface));
border-top: 1px solid rgba(var(--v-border-color), var(--v-border-opacity));
}
.selected-config-area {
margin-left: 32px;
}
.tools-selection {
max-height: 300px;
overflow-y: auto;
@@ -842,38 +812,4 @@ export default {
.v-virtual-scroll {
padding-bottom: 16px;
}
@media (max-width: 600px) {
.persona-form-card-mobile {
border-radius: 0;
}
.persona-form-content {
max-height: calc(100vh - 128px);
padding: 16px !important;
}
.persona-form-title {
font-size: 1.15rem !important;
padding: 12px 16px !important;
}
.selected-config-area {
margin-left: 0;
}
.tools-selection,
.skills-selection {
max-height: 38vh;
}
.persona-form-actions {
padding: 12px 16px !important;
gap: 8px;
}
.persona-form-actions .v-btn {
min-width: 0;
}
}
</style>
@@ -1,35 +1,16 @@
<template>
<div class="d-flex align-center justify-space-between">
<span v-if="!hasSelection" style="color: rgb(var(--v-theme-primaryText));">
<span v-if="!modelValue" style="color: rgb(var(--v-theme-primaryText));">
{{ tm('providerSelector.notSelected') }}
</span>
<span v-else class="provider-name-text">
<template v-if="multiple">
{{ tm('providerSelector.selectedCount', { count: selectedProviders.length }) }}
</template>
<template v-else>
{{ modelValue }}
</template>
{{ modelValue }}
</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>
@@ -51,52 +32,10 @@
<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: '' })"
@@ -118,7 +57,7 @@
:key="provider.id"
:value="provider.id"
@click="selectProvider(provider)"
:active="isProviderSelected(provider.id)"
:active="selectedProvider === provider.id"
rounded="md"
class="ma-1">
<v-list-item-title>{{ provider.id }}</v-list-item-title>
@@ -128,7 +67,7 @@
</v-list-item-subtitle>
<template v-slot:append>
<v-icon v-if="isProviderSelected(provider.id)" color="primary">mdi-check-circle</v-icon>
<v-icon v-if="selectedProvider === provider.id" color="primary">mdi-check-circle</v-icon>
</template>
</v-list-item>
</v-list>
@@ -182,7 +121,7 @@ import ProviderPage from '@/views/ProviderPage.vue'
const props = defineProps({
modelValue: {
type: [String, Array],
type: String,
default: ''
},
providerType: {
@@ -196,10 +135,6 @@ const props = defineProps({
buttonText: {
type: String,
default: ''
},
multiple: {
type: Boolean,
default: false
}
})
@@ -210,16 +145,8 @@ 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}`
@@ -229,13 +156,7 @@ const defaultTab = computed(() => {
// modelValue selectedProvider
watch(() => props.modelValue, (newValue) => {
if (props.multiple) {
selectedProviders.value = Array.isArray(newValue)
? [...newValue.filter((v) => typeof v === 'string' && v)]
: []
return
}
selectedProvider.value = typeof newValue === 'string' ? newValue : ''
selectedProvider.value = newValue || ''
}, { immediate: true })
watch(providerDrawer, (isOpen, wasOpen) => {
@@ -245,13 +166,7 @@ watch(providerDrawer, (isOpen, wasOpen) => {
})
async function openDialog() {
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 : ''
}
selectedProvider.value = props.modelValue || ''
dialog.value = true
await loadProviders()
}
@@ -290,72 +205,19 @@ 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() {
if (props.multiple) {
emit('update:modelValue', [...selectedProviders.value])
} else {
emit('update:modelValue', selectedProvider.value)
}
emit('update:modelValue', selectedProvider.value)
dialog.value = false
}
function cancelSelection() {
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 : ''
}
selectedProvider.value = 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
}
@@ -374,16 +236,6 @@ 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;
}
@@ -81,14 +81,10 @@ export function useProviderSources(options: UseProviderSourcesOptions) {
return []
}
const types: Array<{ value: string; label: string; icon: string }> = []
const types: Array<{ value: string; label: string }> = []
for (const [templateName, template] of Object.entries(providerTemplates.value)) {
if (template.provider_type === selectedProviderType.value) {
types.push({
value: templateName,
label: templateName,
icon: getProviderIcon(template.provider)
})
types.push({ value: templateName, label: templateName })
}
}
@@ -45,8 +45,7 @@
"unknownType": "Unknown type",
"createProvider": "Create Provider",
"manageProviders": "Provider Management",
"selectProviderPool": "Select Provider Pool...",
"selectedCount": "{count} provider(s) selected"
"selectProviderPool": "Select Provider Pool..."
},
"personaSelector": {
"notSelected": "Not selected",
@@ -37,10 +37,6 @@
"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"
@@ -873,26 +869,6 @@
"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)"
@@ -94,7 +94,6 @@
"add": "Add",
"empty": "No provider sources",
"selectHint": "Please select a provider source",
"selectCreated": "Select created provider source",
"save": "Save Configuration",
"saveAndFetchModels": "Save and Fetch Models",
"fetchModels": "Fetch Model List",
@@ -147,4 +146,4 @@
"modelId": "Model ID"
}
}
}
}
@@ -27,8 +27,6 @@
"title": "Resources",
"githubDesc": "Give us a Star!",
"docsTitle": "Documentation",
"docsDesc": "Read the official AstrBot documentation.",
"afdianTitle": "Afdian",
"afdianDesc": "Support the AstrBot team on Afdian."
"docsDesc": "Read the official AstrBot documentation."
}
}
@@ -45,8 +45,7 @@
"unknownType": "未知类型",
"createProvider": "创建提供商",
"manageProviders": "提供商管理",
"selectProviderPool": "选择提供商池...",
"selectedCount": "已选择 {count} 个提供商"
"selectProviderPool": "选择提供商池..."
},
"personaSelector": {
"notSelected": "未选择",
@@ -31,16 +31,12 @@
},
"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": "留空代表不使用,可用于非多模态模型"
@@ -876,26 +872,6 @@
"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)"
@@ -95,7 +95,6 @@
"add": "新增",
"empty": "暂无提供商源",
"selectHint": "请选择一个提供商源",
"selectCreated": "选择已创建的提供商源",
"save": "保存配置",
"saveAndFetchModels": "保存并获取模型",
"fetchModels": "获取模型列表",
@@ -148,4 +147,4 @@
"modelId": "模型 ID"
}
}
}
}
@@ -27,8 +27,6 @@
"title": "相关资源",
"githubDesc": "给 AstrBot 点个 Star 吧!",
"docsTitle": "文档",
"docsDesc": "查阅 AstrBot 的官方文档。",
"afdianTitle": "爱发电",
"afdianDesc": "通过爱发电支持 AstrBot 团队。"
"docsDesc": "查阅 AstrBot 的官方文档。"
}
}
-2
View File
@@ -34,8 +34,6 @@ 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
}
}
-1
View File
@@ -18,7 +18,6 @@ export function getProviderIcon(type) {
'deepseek': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/deepseek.svg',
'modelscope': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/modelscope.svg',
'zhipu': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/zhipu.svg',
'nvidia': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/nvidia-color.svg',
'siliconflow': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/siliconcloud.svg',
'moonshot': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/kimi.svg',
'ppio': 'https://registry.npmmirror.com/@lobehub/icons-static-svg/latest/files/icons/ppio.svg',
+1 -46
View File
@@ -357,17 +357,11 @@ 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");
@@ -376,36 +370,6 @@ 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();
@@ -1309,15 +1273,6 @@ 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"
+2 -16
View File
@@ -70,7 +70,7 @@
{{ tm('resources.title') }}
</div>
<v-row>
<v-col cols="12" sm="4">
<v-col cols="12" sm="6">
<!-- 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="4">
<v-col cols="12" sm="6">
<!-- Docs Card -->
<v-card variant="outlined" class="h-100 pa-4 d-flex flex-column" href="https://docs.astrbot.app"
target="_blank">
@@ -98,20 +98,6 @@
</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.17.3",
"version": "4.16.0",
"description": "AstrBot desktop wrapper",
"private": true,
"main": "main.js",
-16
View File
@@ -16,8 +16,6 @@ const kbStopwordsSrc = path.join(
'hit_stopwords.txt',
);
const kbStopwordsDest = 'astrbot/core/knowledge_base/retrieval';
const builtinStarsSrc = path.join(rootDir, 'astrbot', 'builtin_stars');
const builtinStarsDest = 'astrbot/builtin_stars';
const args = [
'run',
@@ -35,25 +33,11 @@ const args = [
'aiosqlite',
'--collect-all',
'pip',
'--collect-all',
'bs4',
'--collect-all',
'readability',
'--collect-all',
'lxml',
'--collect-all',
'lxml_html_clean',
'--collect-all',
'rfc3987_syntax',
'--collect-submodules',
'astrbot.api',
'--collect-submodules',
'astrbot.builtin_stars',
'--collect-data',
'certifi',
'--add-data',
`${builtinStarsSrc}${dataSeparator}${builtinStarsDest}`,
'--add-data',
`${kbStopwordsSrc}${dataSeparator}${kbStopwordsDest}`,
'--distpath',
outputDir,
+2 -2
View File
@@ -1,6 +1,6 @@
[project]
name = "AstrBot"
version = "4.17.3"
version = "4.16.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",
"loguru>=0.7.2",
"colorlog>=6.9.0",
"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
loguru>=0.7.2
colorlog>=6.9.0
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,21 +90,6 @@ 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):
"""模拟钩子函数"""
@@ -336,64 +321,6 @@ 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"])