Files
cad-agent/dwg_review/context.py
T

58 lines
2.0 KiB
Python
Raw Normal View History

"""审查运行的共享上下文。"""
from dataclasses import dataclass
from . import config, evidence_model
from .cad_tools import CadTools
from .checklist import load_checklist
from .llm_client import LlmClient
from .mcp_client import McpClient
@dataclass
class ReviewContext:
"""一次审查运行的共享上下文。
`connect_cad=False`(离线模式)时 `client` / `tools` 均为 `None`——供 PDF / 图片
等**纯离线源**使用:这些源没有 CAD 可连,全链路(取证 / 审查 / 批注)都必须
先判 `ctx.tools is None` 再决定是否走 CAD 工具。`source` 记录本次运行的来源类型,
供规划阶段分发(CAD 走 `detect_drawing_frames`,文件源自带图框)。
`source_path` 为文件源(PDF / 图片)的磁盘路径;`source_reader` 是 `sources`
打开的读取器(缓存,避免每页重复打开 PDF)。
"""
client: McpClient
tools: CadTools
llm: LlmClient
checklist: dict
source: str = evidence_model.SOURCE_CAD
source_path: str = None
source_reader: object = None
max_frames: int = 0
frame_limit: int = 200
max_iters: int = 4
annotate: bool = False
dry_run: bool = False
@classmethod
def create(cls, *, need_llm=True, connect_cad=True, source=None, source_path=None,
**overrides):
client = McpClient() if connect_cad else None
# CadTools.__init__ 会立刻 list_tools() 去连 127.0.0.1:8080;离线模式必须跳过。
tools = CadTools(client) if connect_cad else None
llm = LlmClient() if need_llm else None
checklist = load_checklist()
params = dict(
client=client,
tools=tools,
llm=llm,
checklist=checklist,
source=source or evidence_model.SOURCE_CAD,
source_path=source_path,
max_frames=config.MAX_FRAMES,
frame_limit=config.FRAME_ENTITY_LIMIT,
max_iters=config.REVIEW_MAX_ITERS,
)
params.update(overrides)
return cls(**params)