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
+8
View File
@@ -1,8 +1,13 @@
"""阶段 1:全局规划。
收集图纸概览 + 识别图框,按阅读顺序排序,产出审查计划。
按来源分发:CAD 源用 `detect_drawing_frames` 识别图框;PDF / 图片源直接由文件
结构给出图框(PDF 每页一个、图片整图一个),**保持原始页序**(阅读顺序已由文档
自身决定,不能再按 bbox 重排)。
"""
from .. import sources
from ..mcp_client import result_json
@@ -18,6 +23,9 @@ def _sort_frames(frames):
def plan(ctx):
"""返回 {drawing_info, layers, layouts, frames}。"""
if sources.is_file_source(ctx.source):
return sources.plan_file(ctx)
drawing_info = result_json(ctx.tools.call("get_drawing_info", {}), {})
layers = result_json(ctx.tools.call("get_layers", {"count_entities": True}), {})
layouts = result_json(ctx.tools.call("get_layouts", {}), {})
+1 -1
View File
@@ -19,7 +19,7 @@ _SUMMARY_SYSTEM = (
def format_finding(finding):
"""把一个 findings 元素渲染成给汇总 LLM 的文本块。"""
lines = [f"【图框 {finding.get('frame')}(图幅 {finding.get('sheet')})】"]
lines = [f"【图框 {finding.get('frame')}(图幅 {finding.get('sheet') or '—'})】"]
summary = finding.get("summary")
if summary:
lines.append(str(summary))
+11 -1
View File
@@ -56,7 +56,13 @@ def _run_readonly_tool(ctx, name, args, image_budget, *, evidence=None, state=No
`inspect_region` 是 **agent 侧虚拟工具**(不走 MCP),在只读白名单校验**之前**拦截:
它内部只调白名单内的只读 MCP 工具。
离线源(PDF / 图片,`ctx.tools is None`)没有任何 MCP 工具可用 → 一律拒绝;
`inspect_region` 依赖 CAD 截图,同样不可用。
"""
if ctx.tools is None:
return [{"type": "text", "text": f"当前为离线源,工具 {name} 不可用。"}], image_budget
if name == inspect.TOOL_NAME:
if not config.INSPECT_ENABLED or evidence is None or state is None:
return [{"type": "text", "text": "inspect_region 当前不可用。"}], image_budget
@@ -96,7 +102,11 @@ def _review_tools(ctx):
启用 `inspect_region` 时,用它**取代**三个裸视口工具(`zoom_to_window` /
`zoom_to_entity` / `get_viewport_screenshot`)——一个语义化入口比三个裸工具更好用,
且可读性由 agent 保证;关闭时维持原样(退回旧行为)。
离线源(`ctx.tools is None`)不提供任何工具(也没有 CAD 可 zoom / 截图)。
"""
if ctx.tools is None:
return []
if config.INSPECT_ENABLED:
names = READ_ONLY_TOOLS - _RAW_VIEW_TOOLS
return ctx.tools.openai_tools(names=names) + [inspect.TOOL_SCHEMA]
@@ -106,7 +116,7 @@ def _review_tools(ctx):
def _reset_viewport(ctx, evidence):
"""把视口复位到该图框的整幅视图(审查中的放大不应残留给用户)。"""
win = evidence.get("window")
if not win:
if not win or ctx.tools is None:
return
try:
ctx.tools.call("zoom_to_window", dict(win, margin=0.02))