|
1 | 1 | # AstrBot SDK |
2 | 2 |
|
3 | | -AstrBot 插件开发 SDK,提供 v4 runtime、worker protocol 和插件工具链。 |
| 3 | +> 优雅、安全、强大的机器人插件开发框架 |
4 | 4 |
|
5 | | -## 安装 |
| 5 | +AstrBot SDK 是一个基于 Python 3.12+ 的插件开发框架,采用**进程隔离**和**能力路由**架构,让插件崩溃不影响系统稳定,支持流式 LLM、语义记忆、跨平台消息收发。 |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +## 核心特性 |
| 10 | + |
| 11 | +| 特性 | 说明 | |
| 12 | +|------|------| |
| 13 | +| **进程隔离** | 每个插件独立进程运行,单个插件崩溃不影响其他插件 | |
| 14 | +| **能力路由** | 显式声明能力(Capability),支持 JSON Schema 验证和流式调用 | |
| 15 | +| **内置 AI** | 集成 LLM 对话、语义记忆、知识库等 AI 能力 | |
| 16 | +| **跨平台** | 统一 API 支持多平台消息收发(QQ、微信等) | |
| 17 | +| **类型安全** | Pydantic 模型 + 类型注解,IDE 友好 | |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 分层架构 |
| 22 | + |
| 23 | +``` |
| 24 | +┌────────────────────────────────────────────────────────────┐ |
| 25 | +│ 插件开发者 API │ |
| 26 | +│ Star, Context, MessageEvent, 装饰器, 过滤器 │ |
| 27 | +└──────────────────────┬─────────────────────────────────────┘ |
| 28 | + │ |
| 29 | +┌──────────────────────▼─────────────────────────────────────┐ |
| 30 | +│ 高层客户端 API │ |
| 31 | +│ ctx.llm.chat() / ctx.memory.save() / ctx.db.set() │ |
| 32 | +└──────────────────────┬─────────────────────────────────────┘ |
| 33 | + │ CapabilityProxy |
| 34 | +┌──────────────────────▼─────────────────────────────────────┐ |
| 35 | +│ 运行时 & 协议层 │ |
| 36 | +│ HandlerDispatcher / CapabilityRouter / Peer / Transport │ |
| 37 | +└──────────────────────┬─────────────────────────────────────┘ |
| 38 | + │ |
| 39 | +┌──────────────────────▼─────────────────────────────────────┐ |
| 40 | +│ AstrBot Core (Supervisor) │ |
| 41 | +└────────────────────────────────────────────────────────────┘ |
| 42 | +``` |
| 43 | + |
| 44 | +**关键概念**: |
| 45 | +- **Star**: 插件基类,所有插件都继承它 |
| 46 | +- **Context**: 运行时上下文,提供 `llm`、`memory`、`db`、`platform` 等客户端 |
| 47 | +- **Capability**: 能力,插件能做的事情(如 `llm.chat`、`platform.send`) |
| 48 | +- **Handler**: 处理器,响应命令/消息的函数 |
| 49 | + |
| 50 | +--- |
| 51 | + |
| 52 | +## 快速开始 |
| 53 | + |
| 54 | +### 安装 |
6 | 55 |
|
7 | 56 | ```bash |
8 | 57 | pip install astrbot-sdk |
9 | 58 | ``` |
10 | 59 |
|
11 | | -## 开发安装 |
| 60 | +### 初始化插件 |
12 | 61 |
|
13 | 62 | ```bash |
14 | | -# 克隆仓库后 |
15 | | -pip install -e . |
| 63 | +# 生成插件骨架 |
| 64 | +astr init my-plugin |
16 | 65 |
|
17 | | -# 或使用 uv |
18 | | -uv sync |
| 66 | +# 生成骨架 + AI 辅助开发配置(可选) |
| 67 | +astr init my-plugin --agents claude,codex |
19 | 68 | ``` |
20 | 69 |
|
21 | | -## 初始化插件 |
| 70 | +### 第一个插件 |
22 | 71 |
|
23 | | -```bash |
24 | | -astr init demo-plugin |
25 | | -astr init demo-plugin --agents claude,codex,opencode |
| 72 | +```python |
| 73 | +# main.py |
| 74 | +from astrbot_sdk import Star, Context, MessageEvent |
| 75 | +from astrbot_sdk.decorators import on_command, on_message |
| 76 | + |
| 77 | +class MyPlugin(Star): |
| 78 | + @on_command("hello", aliases=["hi"]) |
| 79 | + async def hello(self, event: MessageEvent, ctx: Context): |
| 80 | + """打招呼命令""" |
| 81 | + await event.reply(f"你好,{event.sender_name}!") |
| 82 | + |
| 83 | + @on_message(keywords=["帮助"]) |
| 84 | + async def help_handler(self, event: MessageEvent, ctx: Context): |
| 85 | + """关键词触发""" |
| 86 | + await event.reply("可用命令: /hello") |
| 87 | +``` |
| 88 | + |
| 89 | +```yaml |
| 90 | +# plugin.yaml |
| 91 | +_schema_version: 2 |
| 92 | +name: my_plugin |
| 93 | +author: your_name |
| 94 | +version: 1.0.0 |
| 95 | +components: |
| 96 | + - class: main:MyPlugin |
26 | 97 | ``` |
27 | 98 |
|
28 | | -`astr init <name>` 会继续按原样生成插件骨架。传入 `--agents` 时,会在新插件目录下额外生成对应的项目级 agent 目录: |
| 99 | +### 常用功能 |
29 | 100 |
|
30 | | -- Claude Code: `.claude/skills/astrbot-plugin-dev/` |
31 | | -- Codex: `.agents/skills/astrbot-plugin-dev/` |
32 | | -- OpenCode: `.opencode/skills/astrbot-plugin-dev/` |
| 101 | +```python |
| 102 | +# LLM 对话 |
| 103 | +reply = await ctx.llm.chat("你好") |
| 104 | +async for chunk in ctx.llm.stream_chat("讲故事"): |
| 105 | + print(chunk) |
33 | 106 |
|
34 | | -`--agents` 仅支持 `claude`、`codex`、`opencode`,使用逗号分隔;重复值会去重,非法值会直接报错。 |
| 107 | +# 数据存储 |
| 108 | +await ctx.db.set("user:123", {"name": "Alice"}) |
| 109 | +data = await ctx.db.get("user:123") |
35 | 110 |
|
36 | | -## 目录结构 |
| 111 | +# 语义记忆(需要 embedding provider) |
| 112 | +await ctx.memory.save("pref", {"theme": "dark"}) |
| 113 | +results = await ctx.memory.search("用户喜欢什么") |
37 | 114 |
|
| 115 | +# 发送消息 |
| 116 | +await ctx.platform.send(event.session_id, "消息内容") |
| 117 | +await ctx.platform.send_image(event.session_id, "https://example.com/img.jpg") |
38 | 118 | ``` |
39 | | -astrbot-sdk/ |
40 | | -├── src/ |
41 | | -│ └── astrbot_sdk/ # SDK 主包 |
42 | | -├── pyproject.toml |
43 | | -└── README.md |
| 119 | + |
| 120 | +--- |
| 121 | + |
| 122 | +## 我该看哪篇文档? |
| 123 | + |
| 124 | +| 你的目标 | 推荐文档 | |
| 125 | +|----------|----------| |
| 126 | +| 刚开始写第一个插件 | [事件与组件](docs/02_event_and_components.md) → [装饰器](docs/03_decorators.md) | |
| 127 | +| 想了解所有可用的客户端方法 | [常用客户端速查](docs/05_clients.md) | |
| 128 | +| 插件需要生命周期钩子(启动/停止) | [Star 生命周期](docs/04_star_lifecycle.md) | |
| 129 | +| 处理错误、调试问题 | [错误处理与调试](docs/06_error_handling.md) | |
| 130 | +| 并发、性能、安全最佳实践 | [高级主题](docs/07_advanced_topics.md) | |
| 131 | +| 写测试 | [测试指南](docs/08_testing_guide.md) | |
| 132 | +| 从旧版迁移 | [迁移指南](docs/10_migration_guide.md) | |
| 133 | +| 查完整 API 签名 | [客户端 API 参考](docs/api/clients.md) | |
| 134 | +| 了解架构设计 | [架构文档](docs/PROJECT_ARCHITECTURE.md) | |
| 135 | + |
| 136 | +--- |
| 137 | + |
| 138 | +## 文档目录 |
| 139 | + |
| 140 | +### 入门(初级) |
| 141 | + |
| 142 | +| 文档 | 内容 | |
| 143 | +|------|------| |
| 144 | +| [Context API](docs/01_context_api.md) | Context 类完整参考 | |
| 145 | +| [事件与组件](docs/02_event_and_components.md) | MessageEvent 和消息组件使用 | |
| 146 | +| [装饰器](docs/03_decorators.md) | 所有装饰器详细说明 | |
| 147 | +| [Star 生命周期](docs/04_star_lifecycle.md) | 插件基类和生命周期钩子 | |
| 148 | +| [常用客户端](docs/05_clients.md) | LLM/Memory/DB/Platform 快速上手 | |
| 149 | + |
| 150 | +### 进阶(中级) |
| 151 | + |
| 152 | +| 文档 | 内容 | |
| 153 | +|------|------| |
| 154 | +| [错误处理](docs/06_error_handling.md) | 错误体系、处理模式、调试技巧 | |
| 155 | +| [高级主题](docs/07_advanced_topics.md) | 并发、性能、安全、架构模式 | |
| 156 | +| [测试指南](docs/08_testing_guide.md) | 单元测试、集成测试、Mock 使用 | |
| 157 | + |
| 158 | +### 参考(高级) |
| 159 | + |
| 160 | +| 文档 | 内容 | |
| 161 | +|------|------| |
| 162 | +| [API 索引](docs/09_api_reference.md) | 所有导出类和函数入口 | |
| 163 | +| [客户端 API](docs/api/clients.md) | 17 个客户端完整签名和示例 | |
| 164 | +| [迁移指南](docs/10_migration_guide.md) | 从旧版本迁移 | |
| 165 | +| [安全清单](docs/11_security_checklist.md) | 安全开发检查清单 | |
| 166 | +| [架构文档](docs/PROJECT_ARCHITECTURE.md) | SDK 架构设计 | |
| 167 | + |
| 168 | +--- |
| 169 | + |
| 170 | +## 开发 |
| 171 | + |
| 172 | +```bash |
| 173 | +# 开发安装 |
| 174 | +pip install -e . |
| 175 | + |
| 176 | +# 运行测试 |
| 177 | +python -m pytest tests -q |
| 178 | + |
| 179 | +# 代码格式化 |
| 180 | +ruff format . |
| 181 | +ruff check . --fix |
44 | 182 | ``` |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## 相关资源 |
| 187 | + |
| 188 | +- **AstrBot 主项目**: https://github.com/AstrBotDevs/AstrBot |
| 189 | +- **Python 版本**: >= 3.12 |
| 190 | +- **SDK 版本**: v4.0 |
| 191 | +- **协议版本**: P0.6 |
0 commit comments