feat(review): agent 侧虚拟工具 inspect_region —— 按可读分辨率放大指定区域
解决三个缺口:① 放大没复用标定/瓦片数学(LLM 一次 zoom 只出一张,宽区域仍读不清);
②「看清没」由 VLM 自评(易过度自信/幻觉);③ 只能按 handle / 手算 bbox 指认,
图上看到但没 handle 的东西指认不了。
核心思路:把「放大」升级成一个语义化工具——LLM 只指认「要看哪里」,由 agent 复用
tiling.py 的标定数学算出该切几张、每张覆盖多大,用构造保证可读。
新增 dwg_review/inspect.py(agent 侧,不编 DLL):
- TOOL_SCHEMA:inspect_region(target, purpose);target 三形态 {handle} / {window} /
{label,cx,cy,span},purpose = text(8px) / line(6px)。
- InspectState:单图框内「截图标签 → 世界可见范围」登记表,支持逐级下钻
(返回的 inspect-N 可继续作为 label 指认)。
- resolve_target / _point_box:label+cx+cy → WCS。cy 是图像坐标(上→下),
世界 y 必须翻转(y = y2 - cy*H)——单测专门钉住。
- plan_tiles:复用 world_per_px + tiles_for_box + subdivide;超 INSPECT_MAX_TILES(4)
则硬失败并给出「缩小 target / 分多次请求」的指引(不静默降级);拿不到比例/像素
则退回单张并如实告知。
关键几何:CAD 的 _.ZOOM _W 是等比适配(不拉伸),故「截图上的归一化坐标」必须按
max(W/ew, H/eh) 反算真实可见范围(新增 tiling.visible_box)——直接用送进去的窗口会偏。
zoom_to_window 返回的 view 是「加过 margin 的窗口」,不是可见范围,不能混用。
证据层配套:evidence_model.make_image(..., window=) 记录每张图的可见世界范围;
capture_images 用 view_px(overall 图原始像素)+ visible_box 逐张登记。
子循环接入:_run_readonly_tool 在只读白名单校验之前拦截 inspect_region(它不在
READ_ONLY_TOOLS 里,内部只调白名单内的只读 MCP 工具);_review_tools 启用时用
inspect_region 取代三个裸视口工具;inspect_region 计入 _VIEW_TOOLS → 结束复位视口。
配置:INSPECT_ENABLED(True) / INSPECT_MAX_TILES(4)。
端到端实测(真实图纸 1 图框):取证与审查两阶段均在最大化态、结束精确还原(4 项 PASS);
LLM 的 11 次取证调用里 2 次是 inspect_region,且都显式给 window + purpose:"text"
(x[20000,115000] y[-6500,3000],即 A0 图框底部横条=标题栏所在)。
验收口径达成:报告报出标题栏乱码 ???????????????,而 get_text_content 对全图返回 0 条文字
(本图文字全在块/自定义实体内)→ 块内文字是被视觉放大读出来的。
pytest 343 passed。
This commit is contained in:
1 parent
3bc64b6c3e
commit
b757892c67
18 files changed
+1003
-45
No files matched your search
@@ -1,5 +1,7 @@
|
||||
"""`dwg_review.tiling` 单元测试(纯函数,不依赖 CAD / LLM / Pillow)。"""
|
||||
|
||||
import pytest
|
||||
|
||||
from dwg_review import tiling as tl
|
||||
|
||||
|
||||
@@ -221,3 +223,60 @@ def test_plan_regions_clamps_to_frame():
|
||||
for w in wins:
|
||||
assert w["min_x"] >= 0 and w["max_x"] <= 1000
|
||||
assert w["min_y"] >= 0 and w["max_y"] <= 500
|
||||
|
||||
|
||||
# --- box / window 互转与可见范围 -------------------------------------------
|
||||
|
||||
def test_box_window_roundtrip():
|
||||
box = (1.0, 2.0, 3.0, 4.0)
|
||||
win = tl.box_to_window(box)
|
||||
assert win == {"min_x": 1.0, "min_y": 2.0, "max_x": 3.0, "max_y": 4.0}
|
||||
assert tl.window_to_box(win) == box
|
||||
|
||||
|
||||
def test_box_to_window_invalid():
|
||||
assert tl.box_to_window(None) is None
|
||||
assert tl.box_to_window([1, 2, 3]) is None
|
||||
|
||||
|
||||
def test_window_to_box_invalid():
|
||||
assert tl.window_to_box(None) is None
|
||||
assert tl.window_to_box({}) is None
|
||||
assert tl.window_to_box({"min_x": 1, "min_y": 2}) is None
|
||||
|
||||
|
||||
def test_expand_box_by_fraction():
|
||||
assert tl.expand_box((0, 0, 100, 50), 0.05) == pytest.approx((-5.0, -2.5, 105.0, 52.5))
|
||||
assert tl.expand_box((0, 0, 100, 50), 0.0) == (0.0, 0.0, 100.0, 50.0)
|
||||
assert tl.expand_box(None, 0.1) is None
|
||||
# 非法 frac 视为 0(不抛异常)
|
||||
assert tl.expand_box((0, 0, 10, 10), "x") == (0.0, 0.0, 10.0, 10.0)
|
||||
|
||||
|
||||
def test_visible_box_widens_to_viewport_aspect():
|
||||
# 1000x500 的窗口在 1280x280(约 4.57:1)视口下等比适配 → 横向露出更多,纵向刚好贴合
|
||||
vis = tl.visible_box((0, 0, 1000, 500), (1280, 280), 0.0)
|
||||
assert vis[0] < 0 and vis[2] > 1000
|
||||
assert vis[1] == pytest.approx(0.0)
|
||||
assert vis[3] == pytest.approx(500.0)
|
||||
assert (vis[0] + vis[2]) / 2 == pytest.approx(500.0) # 中心不变
|
||||
assert (vis[1] + vis[3]) / 2 == pytest.approx(250.0)
|
||||
|
||||
|
||||
def test_visible_box_tall_window_is_fit_by_height():
|
||||
# 100x1000(很窄很高)在 1280x280 视口下按**高**适配 → 纵向刚好贴合,横向大幅露出
|
||||
vis = tl.visible_box((0, 0, 100, 1000), (1280, 280), 0.0)
|
||||
assert vis[1] == pytest.approx(0.0) and vis[3] == pytest.approx(1000.0)
|
||||
assert vis[0] < 0 and vis[2] > 100
|
||||
assert (vis[0] + vis[2]) / 2 == pytest.approx(50.0) # 中心不变
|
||||
|
||||
|
||||
def test_visible_box_without_view_px_falls_back_to_margin():
|
||||
assert tl.visible_box((0, 0, 100, 50), None, 0.1) == pytest.approx(
|
||||
(-10.0, -5.0, 110.0, 55.0)
|
||||
)
|
||||
assert tl.visible_box((0, 0, 100, 50), (0, 0), 0.0) == (0.0, 0.0, 100.0, 50.0)
|
||||
|
||||
|
||||
def test_visible_box_invalid_box():
|
||||
assert tl.visible_box(None, (100, 100), 0.1) is None
|
||||
Reference in new issue
Block a user