Rust + Python cross-platform runtime framework with PyO3 packaging.
Project description
面向插件开发和多平台接入的 Rust + Python 消息运行时。
iamai
iamai 是一个 Rust + Python 双层跨平台消息运行时:
- Python 负责插件生态、配置装配、运行编排。
- Rust 负责消息链、协议归一化、配置深度合并等底层能力。
- 使用
PyO3 + maturin打包成一个 Python 包。 - 以
uv作为项目与示例的统一管理器。
设计目标
- 保留 NoneBot 的
Runtime / Adapter / Plugin分层。 - 借鉴 Koishi 的高扩展性与适配器插件化思路。
- 保持 AliceBot / iamai 这种轻量、直接、容易写插件的体验。
- 插件只写 Python,底层数据结构和协议归一化交给 Rust。
当前能力
TerminalAdapter:本地终端交互适配器。OneBot11Adapter:支持ws、ws-reverse、http三种模式。WebhookAdapter:通用 HTTP webhook 适配器。- Python 插件系统:
@command(...)@message_handler(...)@event_handler(...)
- 中间件与依赖注入:
@middleware(phase="before" | "around" | "after" | "error")depends(...) / Depends(...)
- 插件依赖与加载顺序:
requiresoptional_requiresload_afterload_before
- 插件热重载:
- 手动
runtime.reload_plugins() - 基于文件变更轮询的自动热重载
- 手动
- 内置管理插件:
/reload/reload-config/plugins/plugin <name>/plugin-config <name>/adapters/health/sessions/trace last
- 配置文件使用 TOML。
- 示例项目位于:
项目结构
.
├── Cargo.toml
├── pyproject.toml
├── src/lib.rs
├── python/iamai
│ ├── adapters
│ ├── runtime.py
│ ├── plugin.py
│ ├── context.py
│ └── ...
├── docs
└── examples/echo-runtime
开发
根项目:
uv sync
uv run python -m iamai --config examples/echo-runtime/config.terminal.toml
示例项目也作为根 workspace member 启动,不需要进入子目录单独 uv sync:
uv run --package echo-runtime python -m iamai --config examples/echo-runtime/config.terminal.toml
文档:
uv sync --group docs
uv run sphinx-build -b html docs docs/_build/html
质量检查:
uv run ruff check .
uv run pytest
bash scripts/check_example_configs.sh
架构概览
Python plugins
↓
Middleware / Dependency Injection / Context / Runtime / Adapter lifecycle
↓
Rust core (_core)
- message chain
- OneBot11 event normalization
- JSON config deep merge
生态调研
见 docs/ecosystem-and-architecture.md。
新增运行时能力
- 插件热重载:
- 在配置里开启
[runtime.hot_reload] - 修改插件文件后框架会自动重载
- 设置
config = true后,修改当前 TOML 配置文件也会触发配置与插件重载
- 在配置里开启
- CLI 配置检查:
python -m iamai --config config.toml config-checkpython -m iamai --config config.toml config-schema [plugin]config-check会打印高风险配置告警,但不会阻止本地开发态配置通过
- 插件依赖图:
- 加载前会做依赖解析与拓扑排序
- 缺失依赖和循环依赖会在启动时直接报错
- 依赖注入:
- handler / middleware 参数可以直接声明
ctx、runtime、event、adapter、plugin - 也可以用
depends(...)注入派生值
- handler / middleware 参数可以直接声明
- 多轮会话:
- handler 可用
await ctx.wait_for_message(timeout=60)等待同会话下一条消息 - 管理命令
/sessions可查看等待中的会话
- handler 可用
- 事件中间件分层:
before适合预处理和上下文注入around适合包裹 handlerafter适合成功后的收尾error适合统一错误处理,可返回True表示抑制异常
- HTTP/Webhook:
- OneBot11 可用 HTTP webhook 收事件、HTTP API 发动作
- 通用
WebhookAdapter可接任意外部系统 POST 事件
- Agent runtime:
- 内置
LLMClient / ToolRegistry / AgentTrace / Guardrail - examples 里的 ReAct 工具调用已迁移到框架级
ToolRegistry
- 内置
- 持久化状态:
- 插件可声明
state_scope = "persistent" - 配置
[state] backend = "json"后会把插件 state 写入 JSON 文件 - 配置
[state] backend = "sqlite"后会把插件 state 写入 SQLite
- 插件可声明
- Sphinx 文档:
- 使用
readthedocs.io + Furo + rst - API 参考通过 autodoc / autosummary 自动生成
- 提供模块指南与渐进式教程
- 使用
生产建议
WebhookAdapter暴露到公网时,至少同时配置access_token和signature_secret。- 除非上游系统无法配合,否则不要开启
allow_query_token。 - 保持
allow_event_reply_url = false;如果必须开启,配合reply_url_allowlist使用。 - 管理命令只给
runtime.superusers,并保持reload_requires_superuser = true、introspection_requires_superuser = true。 runtime.allow_external_paths = true只建议用于示例或多项目共享代码目录。
工程质量门禁
ruff:静态检查pytest:运行时安全与行为回归config-check:示例配置与插件装配校验sphinx-build:文档与 API 参考可构建性校验
示例
echo-runtime:最小可运行样板,覆盖终端、OneBot11、Webhook 配置模板。arcade-runtime:多插件依赖、before/after/error middleware、leaderboard 和错误恢复。story-runtime:叙事型状态机示例,展示load_before、optional_requires和 message handler。planner-executor-runtime:Planner / Executor agent loop,展示跨插件计划与执行协作。react-runtime:ReAct loop,展示工具调用、observation trace 和错误收敛。supervisor-team-runtime:Supervisor + specialist workers,多角色协作与总结。life-sim-runtime:人生模拟器,展示长期状态、场景生成和结构化选项。group-assistant-runtime:群聊助手,展示房间记忆、总结、TODO 提取和显式 AI 回复。persona-rp-runtime:显式 AI 角色扮演,展示 persona 切换和群聊式对话风格。
LLM examples 默认读取 examples/_shared/.env,各示例只在插件配置里保留 temperature 和 token 限制。
需要临时覆盖时,可以在 shell 里设置:
export OPENAI_API_KEY="sk-..."
export OPENAI_MODEL="xxx"
# Optional:
# export OPENAI_BASE_URL="https://your-compatible-endpoint/v1"
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file iamai-0.2.2.tar.gz.
File metadata
- Download URL: iamai-0.2.2.tar.gz
- Upload date:
- Size: 485.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
932cd93446cfdb0ec8dabc554ba67b7120113bf684dfff3d3974cb2e08b3f46a
|
|
| MD5 |
58a9f79211c507c6e92152a821fff307
|
|
| BLAKE2b-256 |
5861779214f3979fd8b8e83df89767a8b6733d21e34d3479b0c54b151b6f4073
|
File details
Details for the file iamai-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl.
File metadata
- Download URL: iamai-0.2.2-cp312-cp312-macosx_10_12_x86_64.whl
- Upload date:
- Size: 359.6 kB
- Tags: CPython 3.12, macOS 10.12+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5086dd0f5bee160cc6ff899100c53c939f811d8c1fb2817350f6d3fdad592c50
|
|
| MD5 |
c5619d505a3217eafd7d9f81ec4d9e04
|
|
| BLAKE2b-256 |
19a25518ce0ba5ae2a03a9b1b3c3f84458e322e51fdb27a63e5a280c24b9e865
|