ai-rule-engine/docs/plans/2026-05-11-ai-rule-engine-plan.md
root c022c54144 feat(ai-rule-engine): 添加 AI 规则引擎实现计划
- 批次1:基础设施(存储层 + 项目骨架)
- 批次2:REST API 基础
- 批次3:LLM 编译器
- 批次4:规则执行器
- 批次5:规则校验与兜底
- 批次6:冲突检测

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-11 21:20:47 +08:00

89 lines
4.4 KiB
Markdown
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.

# AI 规则引擎实现计划
## 项目概述
- **目标**:实现 AI 规则引擎 MVP支持自然语言规则→可执行代码转换
- **技术栈**Python 3.10+ / SQLite / REST API / RestrictedPython
- **设计文档**`docs/plans/2026-05-11-ai-rule-engine-design.md`
---
## 实现批次
### 批次 1基础设施存储层 + 项目骨架)
| 序号 | 步骤 | 修改目标 | 涉及文件 | 验证方式 | 完成判定 |
|------|------|----------|----------|----------|----------|
| 1.1 | 创建项目结构 | 建立标准 Python 项目骨架 | `src/rule_engine/__init__.py`, `tests/`, `pyproject.toml` | `python -m pytest --collect-only` | 目录结构符合预期 |
| 1.2 | 实现 SQLite 存储层 | 数据库表创建/读写 | `src/rule_engine/store.py`, `src/rule_engine/models.py` | 单元测试 | CRUD 操作测试全绿 |
| 1.3 | 定义数据模型 | Pydantic 模型验证 | `src/rule_engine/models.py` | `pytest tests/test_models.py` | Schema 验证通过 |
### 批次 2REST API 基础
| 序号 | 步骤 | 修改目标 | 涉及文件 | 验证方式 | 完成判定 |
|------|------|----------|----------|----------|----------|
| 2.1 | 实现 FastAPI 应用骨架 | 基础 API 框架 | `src/rule_engine/api.py` | `uvicorn --help` | 服务启动成功 |
| 2.2 | 实现创建规则接口 | POST /api/rules | `src/rule_engine/api.py` | `curl localhost:8000/docs` | API 文档可见 |
| 2.3 | 实现查询/删除规则接口 | GET/DELETE /api/rules/{id} | `src/rule_engine/api.py` | `pytest tests/test_api.py` | CRUD 接口测试全绿 |
### 批次 3LLM 编译器
| 序号 | 步骤 | 修改目标 | 涉及文件 | 验证方式 | 完成判定 |
|------|------|----------|----------|----------|
| 3.1 | 实现 prompt 模板 | LLM 调用模板 | `src/rule_engine/compiler.py` | `python -c "from rule_engine.compiler import prompt"` | prompt 可导出 |
| 3.2 | 实现 LLM 调用封装 | 调用 OpenAI/ Anthropic API | `src/rule_engine/compiler.py` | `pytest tests/test_compiler.py -v` | Mock 测试通过 |
| 3.3 | 集成编译流程 | 规则描述→Python 函数 | `src/rule_engine/compiler.py` | 集成测试 | 真实调用成功 |
### 批次 4规则执行器
| 序号 | 步骤 | 修改目标 | 涉及文件 | 验证方式 | 完成判定 |
|------|------|----------|----------|----------|
| 4.1 | 实现 RestrictedPython 沙箱 | 安全代码执行环境 | `src/rule_engine/executor.py` | `pytest tests/test_executor.py` | 危险操作被拦截 |
| 4.2 | 实现规则执行函数 | 在沙箱中运行规则 | `src/rule_engine/executor.py` | 单元测试 | 正常规则执行成功 |
| 4.3 | 实现超时控制 | 防止死循环 | `src/rule_engine/executor.py` | `pytest tests/test_executor.py` | 超时抛出异常 |
### 批次 5规则校验与兜底
| 序号 | 步骤 | 修改目标 | 涉及文件 | 验证方式 | 完成判定 |
|------|------|----------|----------|----------|
| 5.1 | 实现规则匹配器 | 事实匹配规则 | `src/rule_engine/matcher.py` | `pytest tests/test_matcher.py` | 匹配逻辑正确 |
| 5.2 | 实现校验接口 | POST /api/rules/evaluate | `src/rule_engine/api.py` | 集成测试 | 完整流程跑通 |
| 5.3 | 实现 LLM Callback 兜底 | 无匹配时调用 LLM | `src/rule_engine/callback.py` | Mock 测试 | callback 触发逻辑正确 |
### 批次 6冲突检测
| 序号 | 步骤 | 修改目标 | 涉及文件 | 验证方式 | 完成判定 |
|------|------|----------|----------|----------|
| 6.1 | 实现冲突检测器 | 检测矛盾规则 | `src/rule_engine/conflict.py` | `pytest tests/test_conflict.py` | 简单冲突场景通过 |
| 6.2 | 集成冲突警告 | 创建/执行时检测 | `src/rule_engine/api.py` | 集成测试 | 冲突时返回警告 |
---
## 验证命令
| 类型 | 命令 |
|------|------|
| 单元测试 | `python -m pytest tests/ -v` |
| 集成测试 | `python -m pytest tests/integration/ -v` |
| Lint | `python -m ruff check src/` |
| Typecheck | `python -m mypy src/` |
| 启动服务 | `uvicorn src.rule_engine.api:app --reload` |
---
## 风险与依赖
| 风险 | 缓解措施 |
|------|----------|
| LLM API 不可用 | Mock 模式用于测试 |
| 复杂规则超出沙箱能力 | MVP 阶段限制规则复杂度 |
| SQLite 并发写入 | 后续可切换 PostgreSQL |
---
## 待确认项
- [ ] LLM Provider 选择OpenAI (GPT-4o) 还是 Anthropic (Claude Sonnet)?
- [ ] 是否需要 Web 管理界面?
- [ ] 规则优先级如何定义?