|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Run example samples and report results. |
| 3 | +
|
| 4 | +Usage (from repo root): |
| 5 | + uv run examples/run-samples.py # text-only samples |
| 6 | + uv run examples/run-samples.py --image # also run image samples |
| 7 | + uv run examples/run-samples.py --video # also run video samples |
| 8 | + uv run examples/run-samples.py --all # run everything |
| 9 | + uv run examples/run-samples.py --parallel # run in parallel |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import concurrent.futures |
| 14 | +import dataclasses |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +REPO = Path(__file__).resolve().parent.parent |
| 21 | +SAMPLES = REPO / "examples" / "samples" |
| 22 | + |
| 23 | + |
| 24 | +@dataclasses.dataclass |
| 25 | +class Sample: |
| 26 | + name: str |
| 27 | + _: dataclasses.KW_ONLY |
| 28 | + stdin: str | None = None |
| 29 | + |
| 30 | + |
| 31 | +TEXT_SAMPLES = [ |
| 32 | + Sample("stream.py"), |
| 33 | + Sample("stream_all.py"), |
| 34 | + Sample("structured_output.py"), |
| 35 | + Sample("tools_schema.py"), |
| 36 | + Sample("agent_simple.py"), |
| 37 | + Sample("agent_custom_loop.py"), |
| 38 | + Sample("agent_nested.py"), |
| 39 | + Sample("streaming_tool.py"), |
| 40 | + Sample("explicit_client.py"), |
| 41 | + Sample("middleware_simple.py"), |
| 42 | + Sample("multimodal_input.py"), |
| 43 | + Sample("check_connection.py"), |
| 44 | +] |
| 45 | + |
| 46 | +IMAGE_SAMPLES = [ |
| 47 | + Sample("image_generation.py"), |
| 48 | + Sample("image_edit.py"), |
| 49 | + Sample("inline_image.py"), |
| 50 | +] |
| 51 | + |
| 52 | +VIDEO_SAMPLES = [ |
| 53 | + Sample("video_generation.py"), |
| 54 | +] |
| 55 | + |
| 56 | +# Broken! |
| 57 | +HOOKS_SAMPLES = [ |
| 58 | + Sample("agent_hooks.py", stdin="y\n"), |
| 59 | + Sample("agent_hooks_serverless.py"), |
| 60 | +] |
| 61 | + |
| 62 | +# Broken! |
| 63 | +MCP_SAMPLES = [ |
| 64 | + Sample("mcp_tools.py"), |
| 65 | +] |
| 66 | + |
| 67 | + |
| 68 | +def _sample_cmd(sample: Sample) -> list[str]: |
| 69 | + return [ |
| 70 | + "uv", |
| 71 | + "run", |
| 72 | + "--frozen", |
| 73 | + "--with-editable", |
| 74 | + str(REPO), |
| 75 | + "python", |
| 76 | + str(SAMPLES / sample.name), |
| 77 | + ] |
| 78 | + |
| 79 | + |
| 80 | +_env = {k: v for k, v in os.environ.items() if k != "VIRTUAL_ENV"} |
| 81 | + |
| 82 | + |
| 83 | +def run_sample(sample: Sample) -> bool: |
| 84 | + print(f"{'=' * 20} {sample.name} {'=' * 20}") |
| 85 | + sys.stdout.flush() |
| 86 | + result = subprocess.run( |
| 87 | + _sample_cmd(sample), |
| 88 | + env=_env, |
| 89 | + timeout=120, |
| 90 | + input=sample.stdin, |
| 91 | + text=True, |
| 92 | + ) |
| 93 | + print() |
| 94 | + sys.stdout.flush() |
| 95 | + return result.returncode == 0 |
| 96 | + |
| 97 | + |
| 98 | +def run_sample_quiet(sample: Sample) -> tuple[str, bool, str]: |
| 99 | + try: |
| 100 | + result = subprocess.run( |
| 101 | + _sample_cmd(sample), |
| 102 | + env=_env, |
| 103 | + timeout=120, |
| 104 | + capture_output=True, |
| 105 | + text=True, |
| 106 | + input=sample.stdin, |
| 107 | + ) |
| 108 | + output = result.stdout + result.stderr |
| 109 | + return sample.name, result.returncode == 0, output |
| 110 | + except subprocess.TimeoutExpired: |
| 111 | + return sample.name, False, "TIMEOUT after 120s" |
| 112 | + |
| 113 | + |
| 114 | +def main() -> None: |
| 115 | + parser = argparse.ArgumentParser(description="Run example samples.") |
| 116 | + parser.add_argument("--text", action="store_true", help="include text samples") |
| 117 | + parser.add_argument("--image", action="store_true", help="include image samples") |
| 118 | + parser.add_argument("--video", action="store_true", help="include video samples") |
| 119 | + parser.add_argument("--hooks", action="store_true", help="include hook samples") |
| 120 | + parser.add_argument("--mcp", action="store_true", help="include MCP samples") |
| 121 | + parser.add_argument("--all", action="store_true", help="run all samples") |
| 122 | + parser.add_argument( |
| 123 | + "--parallel", action="store_true", help="run samples in parallel" |
| 124 | + ) |
| 125 | + args = parser.parse_args() |
| 126 | + |
| 127 | + has_category = args.text or args.image or args.video or args.hooks or args.mcp |
| 128 | + |
| 129 | + samples: list[Sample] = [] |
| 130 | + if args.text or args.all or not has_category: |
| 131 | + samples.extend(TEXT_SAMPLES) |
| 132 | + if args.image or args.all: |
| 133 | + samples.extend(IMAGE_SAMPLES) |
| 134 | + if args.video or args.all: |
| 135 | + samples.extend(VIDEO_SAMPLES) |
| 136 | + if args.hooks or args.all: |
| 137 | + samples.extend(HOOKS_SAMPLES) |
| 138 | + if args.mcp or args.all: |
| 139 | + samples.extend(MCP_SAMPLES) |
| 140 | + |
| 141 | + results: list[tuple[str, bool]] = [] |
| 142 | + |
| 143 | + if args.parallel: |
| 144 | + outputs: dict[str, str] = {} |
| 145 | + with concurrent.futures.ThreadPoolExecutor() as pool: |
| 146 | + futures = {pool.submit(run_sample_quiet, s): s for s in samples} |
| 147 | + for future in concurrent.futures.as_completed(futures): |
| 148 | + name, ok, output = future.result() |
| 149 | + status = "PASS" if ok else "FAIL" |
| 150 | + print(f" {status} {name}") |
| 151 | + sys.stdout.flush() |
| 152 | + outputs[name] = output |
| 153 | + results.append((name, ok)) |
| 154 | + |
| 155 | + passed = sorted(name for name, ok in results if ok) |
| 156 | + failed = sorted(name for name, ok in results if not ok) |
| 157 | + |
| 158 | + print() |
| 159 | + for name in [*passed, *failed]: |
| 160 | + print(f"{'=' * 20} {name} {'=' * 20}") |
| 161 | + if outputs[name].strip(): |
| 162 | + print(outputs[name].rstrip()) |
| 163 | + print() |
| 164 | + |
| 165 | + if failed: |
| 166 | + sys.exit(1) |
| 167 | + else: |
| 168 | + for sample in samples: |
| 169 | + try: |
| 170 | + ok = run_sample(sample) |
| 171 | + except subprocess.TimeoutExpired: |
| 172 | + print(" TIMEOUT after 120s\n") |
| 173 | + ok = False |
| 174 | + results.append((sample.name, ok)) |
| 175 | + |
| 176 | + print("=" * 60) |
| 177 | + print("Summary:") |
| 178 | + any_failed = False |
| 179 | + for name, ok in results: |
| 180 | + status = "PASS" if ok else "FAIL" |
| 181 | + print(f" {status} {name}") |
| 182 | + if not ok: |
| 183 | + any_failed = True |
| 184 | + print() |
| 185 | + |
| 186 | + if any_failed: |
| 187 | + sys.exit(1) |
| 188 | + |
| 189 | + |
| 190 | +if __name__ == "__main__": |
| 191 | + main() |
0 commit comments