from __future__ import annotations import json import os import shlex import shutil import subprocess from dataclasses import dataclass from pathlib import Path from packages.adapters.protocols import NotConfiguredError from packages.core.files import atomic_write_text from packages.schemas.models import TaskSpec EMBODIEDGEN_VERSION = "v2.0.0" @dataclass(frozen=True, slots=True) class EmbodiedGenSettings: command: tuple[str, ...] asset_catalog: Path | None = None timeout_s: int = 3600 @classmethod def from_env(cls) -> EmbodiedGenSettings: command = tuple(shlex.split(os.environ.get("EMBODIEDGEN_COMMAND", ""))) catalog = os.environ.get("EMBODIEDGEN_ASSET_CATALOG") timeout_s = int(os.environ.get("EMBODIEDGEN_TIMEOUT_S", "3600")) if timeout_s <= 0: raise ValueError("EMBODIEDGEN_TIMEOUT_S must be positive") return cls(command, Path(catalog) if catalog else None, timeout_s) def probe_embodiedgen(settings: EmbodiedGenSettings | None = None) -> dict[str, object]: settings = settings or EmbodiedGenSettings.from_env() executable = settings.command[0] if settings.command else "" command_ready = bool(executable) and ( Path(executable).is_file() or shutil.which(executable) is not None ) catalog_ready = settings.asset_catalog is None or settings.asset_catalog.is_file() return { "configured": bool(settings.command), "ready": command_ready and catalog_ready, "command": list(settings.command), "command_ready": command_ready, "asset_catalog": str(settings.asset_catalog) if settings.asset_catalog else None, "asset_catalog_ready": catalog_ready, "timeout_s": settings.timeout_s, "version": EMBODIEDGEN_VERSION, } class EmbodiedGenAdapter: """File-protocol task/affordance adapter for the isolated upstream environment.""" def __init__( self, command: list[str] | tuple[str, ...] | None = None, asset_catalog: Path | None = None, timeout_s: int | None = None, ) -> None: if command is None: settings = EmbodiedGenSettings.from_env() self.command = list(settings.command) self.asset_catalog = asset_catalog or settings.asset_catalog self.timeout_s = timeout_s or settings.timeout_s else: self.command = list(command) self.asset_catalog = asset_catalog self.timeout_s = timeout_s or 3600 def version(self) -> str: return EMBODIEDGEN_VERSION def compile(self, task: TaskSpec, output_dir: Path) -> Path: if not self.command: raise NotConfiguredError("EmbodiedGen command is not configured") output_dir.mkdir(parents=True, exist_ok=True) request = output_dir / "embodiedgen_request.json" atomic_write_text(request, task.model_dump_json(indent=2)) command = [*self.command, "--request", str(request), "--output", str(output_dir)] if self.asset_catalog is not None: if not self.asset_catalog.is_file(): raise FileNotFoundError(self.asset_catalog) command.extend(["--asset-catalog", str(self.asset_catalog.resolve())]) result = subprocess.run( command, check=False, capture_output=True, text=True, timeout=self.timeout_s, ) manifest = output_dir / "task_manifest.json" if result.returncode != 0: raise RuntimeError(f"EmbodiedGen failed ({result.returncode}): {result.stderr[-2000:]}") if not manifest.exists(): raise RuntimeError("EmbodiedGen did not produce task_manifest.json") payload = json.loads(manifest.read_text(encoding="utf-8")) if payload.get("status") != "complete": raise RuntimeError("EmbodiedGen task stage did not report complete status") if payload.get("backend") != "embodiedgen-v2": raise RuntimeError("EmbodiedGen task stage reported an unexpected backend") if payload.get("task_id") != task.task_id: raise RuntimeError("EmbodiedGen task stage task_id does not match the request") return manifest