Files
AstrBot/src-new/astrbot_sdk/star.py
whatevertogo cdf72b75a4 feat: 实现 v4 API 兼容层,支持旧版插件导入路径
新增模块:
- api/basic/: AstrBotConfig, BaseConversationManager, Conversation
- api/event/: AstrMessageEvent, AstrBotMessage, EventType, MessageType,
  MessageSession, EventResult 等核心事件类型
- api/message/: MessageChain 及所有消息组件 (Plain, At, Image, File 等)
- api/platform/: PlatformMetadata 平台元数据类型
- api/provider/: LLMResponse 提供者响应实体
- api/star/star.py: StarMetadata 插件元数据类型

更新模块:
- api/__init__.py: 导出所有子模块
- api/components/: 扩展 Command 兼容性
- api/event/filter.py: 增强 filter 装饰器兼容性
- context.py, decorators.py, events.py: 顶层兼容入口

测试更新:
- test_api_event_filter.py: 验证 filter 兼容性
- test_api_modules.py: 验证新模块可导入
- test_handler_dispatcher.py: 验证处理器分发

文档更新:
- AGENTS.md/CLAUDE.md: 添加兼容层设计说明,避免重复造轮子

设计原则:
- 兼容层通过 thin re-export 方式暴露旧版 API
- 不复制独立运行时逻辑,保持架构清晰
- 新版推荐使用顶层模块导入路径
2026-03-13 03:17:03 +08:00

54 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""v4 原生插件基类。
旧版 ``StarMetadata`` 等兼容数据类型保留在 ``astrbot_sdk.api.star``
这里仅承载新版插件生命周期与 handler 收集逻辑。
"""
from __future__ import annotations
import traceback
from typing import Any
from loguru import logger
from .errors import AstrBotError
class Star:
__handlers__: tuple[str, ...] = ()
def __init_subclass__(cls, **kwargs: Any) -> None:
super().__init_subclass__(**kwargs)
from .decorators import get_handler_meta
handlers: dict[str, None] = {}
for base in reversed(cls.__mro__):
for name, attr in getattr(base, "__dict__", {}).items():
func = getattr(attr, "__func__", attr)
meta = get_handler_meta(func)
if meta is not None and meta.trigger is not None:
handlers[name] = None
cls.__handlers__ = tuple(handlers.keys())
async def on_start(self, ctx: Any | None = None) -> None:
return None
async def on_stop(self, ctx: Any | None = None) -> None:
return None
async def on_error(self, error: Exception, event, ctx) -> None:
if isinstance(error, AstrBotError):
if error.retryable:
await event.reply("请求失败,请稍后重试")
elif error.hint:
await event.reply(error.hint)
else:
await event.reply(error.message)
else:
await event.reply("出了点问题,请联系插件作者")
logger.error("handler 执行失败\n{}", traceback.format_exc())
@classmethod
def __astrbot_is_new_star__(cls) -> bool:
return True