Spaces:
Running
Running
File size: 5,516 Bytes
e4cff4b 698d302 e4cff4b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | import json
from pathlib import Path
from typing import Any
from agent_core.config import STEP_SUFFIXES, WORKDIR
def resolve_existing_path(path: str | None) -> Path | None:
if not path:
return None
candidate = Path(path)
if not candidate.is_absolute():
candidate = WORKDIR / candidate
candidate = candidate.resolve()
return candidate if candidate.exists() else None
def load_hard_checks(path: Path) -> dict[str, Any]:
if not path.exists():
return {}
raw = json.loads(path.read_text(encoding="utf-8"))
return raw.get("bbox_checks", {})
def _dimension_value(item: dict[str, Any]) -> float:
if "expected" in item:
return float(item["expected"])
return (float(item["min"]) + float(item["max"])) / 2
def _dimension_passes(actual: float, expected: dict[str, Any]) -> bool:
if "expected" in expected:
tolerance = float(expected.get("tolerance", 0))
target = float(expected["expected"])
return abs(actual - target) <= tolerance
return float(expected["min"]) <= actual <= float(expected["max"])
def _load_step_bbox(step_path: Path) -> dict[str, float]:
from build123d import import_step
shape = import_step(step_path)
bbox = shape.bounding_box()
return {
"x": float(bbox.size.X),
"y": float(bbox.size.Y),
"z": float(bbox.size.Z),
}
def evaluate_step_loadable(step_path: Path | None) -> dict[str, Any]:
if not step_path:
return {"passed": False, "reason": "missing_step"}
try:
from build123d import import_step
import_step(step_path)
except Exception as exc:
return {
"passed": False,
"reason": "step_load_failed",
"error_type": type(exc).__name__,
"error": str(exc),
}
return {"passed": True}
def evaluate_bbox(step_path: Path | None, check: dict[str, Any] | None) -> dict[str, Any]:
if not check:
return {"checked": False, "passed": None, "reason": "no_bbox_check"}
if not step_path:
return {"checked": True, "passed": False, "reason": "missing_step"}
try:
actual = _load_step_bbox(step_path)
except Exception as exc:
return {
"checked": True,
"passed": False,
"reason": "step_load_failed",
"error_type": type(exc).__name__,
"error": str(exc),
}
return compare_bbox(actual, check)
def compare_bbox(actual: dict[str, float], check: dict[str, Any] | None) -> dict[str, Any]:
if not check:
return {"checked": False, "passed": None, "reason": "no_bbox_check"}
dimensions = check.get("dimensions_mm", [])
mode = check.get("mode", "unordered")
comparisons = []
if mode == "axis_aligned":
for expected in dimensions:
axis = expected["axis"]
actual_value = actual[axis]
comparisons.append({
"label": expected.get("label", axis),
"axis": axis,
"actual": actual_value,
"expected": expected,
"passed": _dimension_passes(actual_value, expected),
})
else:
expected_sorted = sorted(dimensions, key=_dimension_value)
actual_sorted = sorted(actual.values())
for actual_value, expected in zip(actual_sorted, expected_sorted):
comparisons.append({
"label": expected.get("label"),
"actual": actual_value,
"expected": expected,
"passed": _dimension_passes(actual_value, expected),
})
passed = bool(comparisons) and all(item["passed"] for item in comparisons)
return {
"checked": True,
"passed": passed,
"mode": mode,
"actual_bbox_mm": actual,
"comparisons": comparisons,
}
def evaluate_hard_metrics(case: dict[str, Any], tool_payload: dict[str, Any], hard_checks: dict[str, Any]) -> dict[str, Any]:
output_path = resolve_existing_path(tool_payload.get("output_path"))
preview_path = resolve_existing_path(tool_payload.get("preview_path"))
manifest_path = resolve_existing_path(tool_payload.get("manifest_path"))
step_export = bool(output_path and output_path.suffix.lower() in STEP_SUFFIXES)
stl_preview = bool(preview_path and preview_path.suffix.lower() == ".stl")
manifest_written = manifest_path is not None
manifest_json_ok = False
manifest_generator = None
if manifest_path:
try:
manifest = json.loads(manifest_path.read_text(encoding="utf-8"))
manifest_json_ok = True
manifest_generator = manifest.get("generator")
except Exception:
manifest_json_ok = False
step_load = evaluate_step_loadable(output_path if step_export else None)
bbox = evaluate_bbox(output_path if step_export else None, hard_checks.get(case["id"]))
tool_success = tool_payload.get("ok") is True
return {
"tool_success": tool_success,
"manifest_written": manifest_written,
"manifest_json_ok": manifest_json_ok,
"manifest_generator": manifest_generator,
"step_export": step_export,
"stl_preview": stl_preview,
"step_loadable": step_load["passed"],
"step_load": step_load,
"bbox_check": bbox,
"error_type": tool_payload.get("error_type"),
"error_stage": tool_payload.get("stage"),
"error": tool_payload.get("error"),
}
|