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

+10 -20
View File
@@ -11,7 +11,7 @@ import contextlib
import math
import time
from .. import config, evidence_model, heatmap, tiling
from .. import config, evidence_model, heatmap, sources, tiling
from ..llm_client import image_size_b64
from ..mcp_client import result_images, result_json
@@ -34,8 +34,9 @@ def maximized_drawing_area(ctx):
- 受 `config.DRAWING_AREA_MAXIMIZE` 控制;
- 工具不存在(旧版插件)/ 调用失败 → **静默降级**:照常取证,只是绘图区小一些;
- 退出时一定尝试还原;**还原失败不掩盖业务异常**(不 raise)。
- 离线源(PDF / 图片)无 CAD 连接(`ctx.tools is None`)→ 直接放行。
"""
if not config.DRAWING_AREA_MAXIMIZE:
if not config.DRAWING_AREA_MAXIMIZE or ctx.tools is None:
yield
return
@@ -144,21 +145,8 @@ def _text_boxes(entities, texts, dimensions):
def _content_predicate(boxes):
"""构造「该窗口内是否有实体」谓词;无实体返回 None。
用于剔除空白瓦片:簇 / 象限的 bbox 只是**包围盒**,L 形或斜向分布会含大片空白。
这里直接按**实体 bbox** 判断——比先量化成占用格更精确(格子的跨界膨胀会把
相邻空白瓦片也判成有内容)。接触即算有内容(保守,宁可多拍不漏)。
"""
boxes = [b for b in (boxes or []) if b]
if not boxes:
return None
def _has_content(win):
box = (win["min_x"], win["min_y"], win["max_x"], win["max_y"])
return any(tiling.boxes_intersect(box, b) for b in boxes)
return _has_content
"""构造「该窗口内是否有实体」谓词;无实体返回 None(委托 `tiling.content_predicate`)。"""
return tiling.content_predicate(boxes)
def _apply_keep(regions, keep):
@@ -309,11 +297,13 @@ def collect_frame(ctx, index, frame):
def collect(ctx, index, unit):
"""按审查单元来源分发取证。
当前仅实现 CAD 源;PDF / 图片源预留(未来在对应分支接入离线解析 / 直接读图,
产出与 CAD **同形**的 evidence:window=None、结构化键给空信封、images 带 label)。
CAD 源走 `collect_frame`(CAD 工具 + 截屏);PDF / 图片源走 `sources.collect`
(内存渲染 + 墨迹瓦片),产出与 CAD **同形**的 evidence:`window` 有值、
结构化键给空信封(PDF 另有 best-effort 文本层)、`images` 带 label。
"""
source = evidence_model.source_type_of(unit)
if source == evidence_model.SOURCE_CAD:
return collect_frame(ctx, index, unit)
# TODO(pdf/image): 接入离线解析(DWG/PDF)与直接读图。
if sources.is_file_source(source):
return sources.collect(ctx, index, unit)
return None