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

+42
View File
@@ -401,3 +401,45 @@ def test_readonly_inspect_region_unavailable_without_evidence():
ctx = make_ctx(client=FakeMcpClient())
content, _ = _run_readonly_tool(ctx, "inspect_region", {}, [6])
assert "不可用" in content[0]["text"]
# ---------------------------------------------------------------------------
# 离线源(tools=None)
# ---------------------------------------------------------------------------
def test_review_tools_offline_returns_empty():
ctx = make_ctx(connect_cad=False)
assert ctx.tools is None
assert _review_tools(ctx) == []
def test_readonly_tool_offline_rejected():
ctx = make_ctx(connect_cad=False)
content, budget = _run_readonly_tool(ctx, "query_entities", {}, [3])
assert "离线" in content[0]["text"]
assert budget == [3]
def test_readonly_tool_offline_rejects_inspect_region():
"""离线源连 inspect_region 虚拟工具也不可用(它依赖 CAD 截图)。"""
ctx = make_ctx(connect_cad=False)
content, _ = _run_readonly_tool(ctx, "inspect_region", {}, [3])
assert "离线" in content[0]["text"]
def test_review_frame_offline_never_calls_cad():
"""离线源审查:LLM 即使请求工具也不会碰 CAD,直接收口给出结论。"""
llm = FakeLlmClient(script=[
llm_calls([("query_entities", {})]),
llm_text(json.dumps({"summary": "s", "issues": []})),
])
ctx = make_ctx(llm=llm, connect_cad=False)
out = review_frame(ctx, make_evidence(), "sys", log=lambda *a: None)
assert out["summary"] == "s"
def test_reset_viewport_offline_noop():
from dwg_review.phases.review import _reset_viewport
ctx = make_ctx(connect_cad=False)
ev = make_evidence()
_reset_viewport(ctx, ev) # 不应抛异常