Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 40 additions & 17 deletions tools/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,41 @@
script = sys.argv[1]
base_path = Path(f"scripts/{script}")

# ---------------------------------------------------
# Load script metadata
# ---------------------------------------------------
model_path = base_path / "model.json"
meta_path = base_path / "meta.json"

if not model_path.exists():
raise RuntimeError(f"Missing model.json in {base_path}")

with open(model_path, "r", encoding="utf-8") as f:
model = json.load(f)

meta = {}
if meta_path.exists():
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)

output_format = meta.get("output_format")


def first_variable_name(model_key, fallback_name):
variables = model.get(model_key) or []
if not variables:
return fallback_name

name = variables[0].get("name")
if not name:
raise RuntimeError(f"First {model_key} entry in model.json is missing a name")

return name


input_name = first_variable_name("inputVariables", "data_in")
output_name = first_variable_name("outputVariables", "data_out")

# ---------------------------------------------------
# Load raw input (format-agnostic)
# ---------------------------------------------------
Expand All @@ -28,21 +63,9 @@
test_input = f.read()

payload = {
"data_in": test_input
input_name: test_input
}

# ---------------------------------------------------
# Load meta data (optional, for format hints)
# ---------------------------------------------------
meta_path = base_path / "meta.json"

meta = {}
if meta_path.exists():
with open(meta_path, "r", encoding="utf-8") as f:
meta = json.load(f)

output_format = meta.get("output_format")

# ---------------------------------------------------
# Headers
# ---------------------------------------------------
Expand Down Expand Up @@ -76,10 +99,10 @@
# ---------------------------------------------------
# Validate output
# ---------------------------------------------------
if "data_out" not in data:
raise RuntimeError(f"No data_out in response: {data}")
if output_name not in data:
raise RuntimeError(f"No {output_name} in response: {data}")

output = data["data_out"]
output = data[output_name]

if output is None or (isinstance(output, str) and not output.strip()):
raise RuntimeError("Script returned empty output")
Expand Down Expand Up @@ -118,4 +141,4 @@
else:
print(str(output)[:500])

print(f"\n>>> Saved as tests/output.{output_format if output_format != 'Base64' else 'bin'}")
print(f"\n>>> Saved as tests/output.{output_format if output_format != 'Base64' else 'bin'}")