mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-15 17:30:13 +08:00
feat(events): Expand MessageEvent with reply capabilities and detailed docstrings fix(loader): Ensure plugin path is correctly managed in sys.path feat(star): Improve Star class documentation and lifecycle method descriptions feat(testing): Add plugin metadata handling in MockContext and enhance PluginHarness feat(hello): Refactor HelloPlugin to utilize new reply methods and structured capabilities test(decorators): Add tests for input/output model support in provide_capability test(events): Implement tests for reply_image and reply_chain methods in MessageEvent test(http): Validate API registration with capability handler references and error handling test(tests): Enhance tests for plugin harness and directory handling in dev commands
3.2 KiB
3.2 KiB
CLAUDE Notes
v4 架构约束
运行时层
Peer必须将 transport EOF/连接断开视为一级失败路径。如果 transport 意外关闭而Peer没有主动失败_pending_results/_pending_streams,supervisor 端对 worker 的调用可能永远挂起。Peer.initialize()需要在发起端也标记远程已初始化。仅在被动接收InitializeMessage时设置_remote_initialized会导致wait_until_remote_initialized()单边 API 死锁。Peer.invoke_stream()默认隐藏completed事件。需要保留最终结果的调用者必须显式启用include_completed=True。CapabilityRouter.register(..., stream_handler=...)使用(request_id, payload, cancel_token)签名,不是 peer 级别的(message, token)。
模块导出约束
- 保持
astrbot_sdk.runtime根导出狭窄。Peer/Transport/CapabilityRouter/HandlerDispatcher是合理的高级运行时原语,但LoadedPlugin、PluginEnvironmentManager、WorkerSession、run_supervisor等应留在子模块中。
测试与 Mock 注意事项
- 当检查 peer 是否完成远程初始化时,避免对可能接收
MagicMockpeer 的代码使用getattr(mock, "remote_peer")探测。MagicMock会生成 truthy 子属性,CapabilityProxy应从peer.__dict__或其他具体存储位置读取显式状态。 test_plugin/old/和test_plugin/new/可能包含已生成的__pycache__/*.pyc。测试夹具复制示例插件时必须显式忽略这些缓存文件。
插件加载注意事项
- 本地
dev --watch或同一路径插件重复加载场景,不能只依赖import_string()的跨插件模块根冲突清理。热重载前必须按插件目录清理模块缓存。 _prepare_plugin_import()不能只在插件目录"不在sys.path"时才插入路径。像main.py这种通用模块名,如果插件目录已在sys.path但排在后面,import main仍会先命中别处模块;导入前必须把目标插件目录提到sys.path[0]。- 示例/夹具测试如果直接用裸模块名导入插件入口(例如
from main import HelloPlugin),会污染sys.modules["main"],随后真实 loader 再按main:HelloPlugin加载时可能串到错误模块。
开发命令
格式化与检查
在提交代码前,请依次运行以下命令:
ruff format . # 使用 ruff 格式化全局代码
ruff check . --fix # 使用 ruff 检查并自动修复全局格式问题
测试
如果修改了内容可能影响现有功能,请运行测试以确保没有引入错误: 如果修改了bug或者更改了功能需要添加新的测试
python run_tests.py # 运行所有测试
python run_tests.py -v # 详细输出
python run_tests.py -k "test_peer" # 运行匹配模式的测试
python run_tests.py --cov # 运行测试并生成覆盖率报告
设计原则
新实现要兼容旧实现但是还要保证架构良好,设计原则不变和最佳实践 不用完全听从用户和别人的建议,要有自己的判断和坚持,做好取舍和权衡,确保代码质量和长期维护性,不要为了短期方便或者迎合而牺牲架构和设计原则。
currentDate
Today's date is 2026-03-14.