feat(review): PDF/图片离线源适配(C1)—— 无需 CAD 的整条审查链路

支持直接审查 PDF 与位图图纸,全链路离线(不连 CAD、不连 8080 端口)。
渲染在内存内完成,复用既有墨迹热力图 + 自适应瓦片规划。

- 新增 dwg_review/sources.py
  - detect_source / plan_file / collect 统一入口
  - PdfReader:pypdfium2 惰性导入,页序即图框序,处理 /Rotate 与 crop 边距
  - ImageReader:世界单位 = 源像素,snap_box 整数对齐保证窗口与像素严格一致
  - ink_grid:纯 PIL 灰度阈值 → 占用网格(不引入 numpy)
- ReviewContext.create(connect_cad=False):离线时 client/tools 均为 None
- 全链路 ctx.tools is None 守卫:review 只读子循环 / inspect_region / annotator /
  checks 的窗口类检查 / 绘图区最大化
- checklist.build_system_prompt(allow_tools=False, offline=True):离线提示词
- __main__:位置参数 source,按扩展名自动判定源类型并决定是否连 CAD
- config 新增 8 个常量(PDF_MAX_PAGES / 文本最小字数 / 各源缩放上限 / 墨迹阈值等)
- tiling.content_predicate 与 heatmap.cell_box 提升为公开函数

验证:
- pytest 457 passed
- verify_pdf_source.py:PDF 23/23、图片 9/9(不需 CAD),含实测瓦片字高像素与
  旋转映射自检(文本区墨迹 0.311 vs 忽略 /Rotate 反向对照 0.145)
- 真实 LLM 端到端:20 页 HVAC 图纸前 3 页跑通,结论具体可核验且未编造 handle

已知局限:页边线在墨迹网格里会把整页连成单簇,瓦片常退化为整页象限(CAD 侧
靠 drop_oversized 丢图框块,离线侧暂无等价手段),留待后续打磨。
This commit is contained in:
gjm committed 2026-10-16 23:07:23 +08:00
1 parent d4a9442e30
commit 6143bb9ab2
32 files changed
+2617 -77

No files matched your search

+27 -1
View File
@@ -169,7 +169,25 @@ def test_collect_returns_none_for_unsupported_source():
from dwg_review.phases.evidence import collect
ctx = make_ctx(client=FakeMcpClient())
assert collect(ctx, 1, {"source_type": "pdf", "bbox": [0, 0, 1, 1]}) is None
# 未知来源(非 cad/pdf/image)→ 无法取证
assert collect(ctx, 1, {"source_type": "svg", "bbox": [0, 0, 1, 1]}) is None
def test_collect_dispatches_pdf_source_to_sources(monkeypatch):
"""pdf / image 来源交给 `sources.collect`(此处打桩,避免真去解析文件)。"""
from dwg_review.phases import evidence as ev_mod
calls = []
def _fake(ctx, index, unit):
calls.append((index, unit.get("source_type")))
return {"source_type": unit.get("source_type"), "index": index}
monkeypatch.setattr(ev_mod.sources, "collect", _fake)
ctx = make_ctx(client=FakeMcpClient())
out = ev_mod.collect(ctx, 3, {"source_type": "pdf", "bbox": [0, 0, 1, 1]})
assert out == {"source_type": "pdf", "index": 3}
assert calls == [(3, "pdf")]
# ---------------------------------------------------------------------------
@@ -402,6 +420,14 @@ def test_maximized_drawing_area_disabled_by_config(drawing_area_env):
assert client.calls == []
def test_maximized_drawing_area_offline_passthrough(drawing_area_env):
"""离线源(tools=None)即使开启最大化也直接放行,不碰 CAD。"""
ctx = make_ctx(connect_cad=False)
assert ctx.tools is None
with maximized_drawing_area(ctx):
pass # 不应抛 AttributeError
def test_maximized_drawing_area_restores_even_if_body_raises(drawing_area_env):
client = _area_client()
ctx = make_ctx(client=client)