feat: layered precise generation for A4 report images

3-phase pipeline to solve LLM prompt overflow from too many OCR elements:
Phase 1 (generate_skeleton): compressed layout schema → skeleton JRXML
Phase 2 (refine_layout): sampled coordinates → pixel-level position tuning
Phase 3 (map_fields): OCR field names → replace $F{field_N} placeholders

Only triggered when layout_schema.total_rows > 0 on initial_generation intent.
Text requests and all other intents are unaffected (zero behavior change).
This commit is contained in:
2026-05-21 08:34:32 +08:00
parent 9bb011e429
commit 43a0542a11
14 changed files with 882 additions and 81 deletions
+40 -1
View File
@@ -160,4 +160,43 @@
---
阶段一立即可做,无外部依赖。阶段二是主要工作量。阶段三是收尾。阶段四是可观测性基础。阶段五是 OCR 智能增强和用户体验改进。
## 阶段六:分层精确生成 (v5) ✓
### 16. 布局 Schema 提取 ✓
- [x] `backend/layout_analyzer.py` — 新增 `extract_layout_schema()` 函数(+107 行)
- [x] X 坐标聚类列检测(avg_width * 0.5 阈值)
- [x] 区域分类:标题/表头/数据/表尾(启发式算法)
- [x] `schema_text` 紧凑中文描述(列定义 + 区域 + 宽度分类)
- [x] 空行/单行/双行边界情况处理
- [x] 单元测试: `tests/test_layered_generation.py::TestExtractLayoutSchema` (9 tests)
### 17. 3 阶段生成管线 ✓
- [x] Phase 1: `generate_skeleton` — 压缩布局 schema → 骨架 JRXML (`$F{field_N}` 占位)
- [x] Phase 2: `refine_layout` — 采样坐标(表头+首行数据+末行)→ 像素级位置精调
- [x] Phase 3: `map_fields` — OCR 字段名 → 替换占位符为真实字段名
- [x] 中间阶段跳过验证(仅最终 mapped 结果进入 validate 循环)
- [x] 流式输出支持(每阶段逐字生成)
- [x] 单元测试: `tests/test_layered_generation.py::TestIntegration` (4 tests)
### 18. 路由与状态 ✓
- [x] `agent/graph.py` — 新增 `route_after_retrieve()` 条件路由
- [x] `layout_schema.total_rows > 0` → 3 阶段,否则 → 原有 1-shot
- [x] `agent/state.py` — 新增 `layout_schema: dict``ocr_elements: list`
- [x] 会话持久化支持(`save_session_node` / `load_session_node`
- [x] 文本请求和其他意图零行为变更
- [x] 单元测试: `tests/test_layered_generation.py::TestRouting` (4 tests)
### 19. Prompt 模板 ✓
- [x] `prompts/skeleton_generation.md` — 骨架生成 prompt
- [x] `prompts/refine_layout.md` — 布局精调 prompt
- [x] `prompts/field_mapping.md` — 字段映射 prompt
- [x] `prompts/loader.py` — 注册 3 个新模板(热重载)
### 20. UI 集成 ✓
- [x] `app.py` — 上传 A4 图片时自动调用 `extract_layout_schema()`
- [x] 新增节点标签:`🏗 生成骨架` / `📐 精调布局` / `🏷 映射字段`
- [x] 3 个新节点的详情渲染
---
阶段一立即可做,无外部依赖。阶段二是主要工作量。阶段三是收尾。阶段四是可观测性基础。阶段五是 OCR 智能增强和用户体验改进。阶段六解决 A4 报表图片 OCR 元素过多(数百个)导致 LLM prompt 超长的问题。