Files
ai-exchange/scripts/run_visual_review.py
2026-05-12 08:11:16 +08:00

89 lines
3.3 KiB
Python

from __future__ import annotations
from pathlib import Path
import json
import os
import sys
ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
sys.path.insert(0, str(ROOT))
from src.services.visual_watch import VisualReviewService
def main(env=None) -> int:
env = os.environ if env is None else env
output_json = _get_bool(env, "VISUAL_REVIEW_OUTPUT_JSON", False)
service = VisualReviewService()
watch_run_id = _get_optional_int(env, "VISUAL_REVIEW_WATCH_RUN_ID")
if watch_run_id is not None:
payload = service.review_watch_run(
watch_run_id=watch_run_id,
snapshot_id=_get_optional_int(env, "VISUAL_REVIEW_SNAPSHOT_ID"),
image_path=_get_optional_value(env, "VISUAL_REVIEW_IMAGE_PATH"),
source_name=_get_optional_value(env, "VISUAL_REVIEW_SOURCE_NAME"),
visual_state=_get_optional_value(env, "VISUAL_REVIEW_STATE"),
visual_confidence=_get_optional_float(env, "VISUAL_REVIEW_CONFIDENCE"),
visual_notes=_get_optional_value(env, "VISUAL_REVIEW_NOTES") or "",
)
else:
payload = service.review_pending_watch_runs(
limit=_get_optional_int(env, "VISUAL_REVIEW_LIMIT") or 1,
instrument_id=_get_optional_int(env, "VISUAL_REVIEW_INSTRUMENT_ID"),
trigger_type=_get_optional_value(env, "VISUAL_REVIEW_TRIGGER_TYPE"),
image_path=_get_optional_value(env, "VISUAL_REVIEW_IMAGE_PATH"),
source_name=_get_optional_value(env, "VISUAL_REVIEW_SOURCE_NAME"),
)
if output_json:
print(json.dumps(payload, ensure_ascii=False, sort_keys=True, default=str))
else:
print(f"Visual review status: {payload.get('status')}")
if payload.get("skip_reason"):
print(f"Skip reason: {payload.get('skip_reason')}")
print(f"Attempted: {payload.get('attempted', 1)} / Reviewed: {payload.get('reviewed', 1)}")
if payload.get("trigger_type_counts"):
print(f"Trigger types: {json.dumps(payload.get('trigger_type_counts'), ensure_ascii=False, sort_keys=True)}")
for item in payload.get("items") or []:
print(
f"- watch_run_id={item.get('watch_run_id')} "
f"state={item.get('visual_state')} snapshot_id={item.get('snapshot_id')}"
)
if item.get("snapshot_image_path"):
print(f" image_path={item.get('snapshot_image_path')}")
if item.get("snapshot_artifact_path"):
print(f" artifact_path={item.get('snapshot_artifact_path')}")
return 0 if payload.get("status") not in {"failed"} else 2
def _get_bool(env, key: str, default: bool = False) -> bool:
value = env.get(key)
if value is None or value == "":
return default
return str(value).strip().lower() in {"1", "true", "yes", "on"}
def _get_optional_int(env, key: str):
value = env.get(key)
if value is None or value == "":
return None
return int(value)
def _get_optional_float(env, key: str):
value = env.get(key)
if value is None or value == "":
return None
return float(value)
def _get_optional_value(env, key: str):
value = env.get(key)
if value is None or value == "":
return None
return value
if __name__ == "__main__":
raise SystemExit(main())