| 42 | status_url = f"{base_url.rstrip('/')}/v1/tasks/{task_id}/status" |
| 43 | deadline = time.time() + timeout_seconds |
| 44 | |
| 45 | while time.time() < deadline: |
| 46 | response = requests.get(status_url, timeout=15) |
| 47 | if response.status_code != 200: |
| 48 | raise RuntimeError(f"Get task status failed ({response.status_code}): {response.text}") |
| 49 | |
| 50 | status = response.json() |
| 51 | task_status = status.get("status") |
| 52 | print(f"[poll] task_id={task_id}, status={task_status}") |
| 53 | |
| 54 | if task_status == "completed": |
| 55 | return status |
| 56 | if task_status in ("failed", "cancelled"): |
| 57 | raise RuntimeError(f"Task ended with status={task_status}, detail={status.get('error')}") |
| 58 | |
| 59 | time.sleep(poll_interval) |
| 60 | |
| 61 | raise TimeoutError(f"Task {task_id} timeout after {timeout_seconds}s") |
| 62 | |
| 63 | |
| 64 | def download_result(base_url: str, task_id: str, output: str) -> Path: |
| 65 | result_url = f"{base_url.rstrip('/')}/v1/tasks/{task_id}/result" |
| 66 | response = requests.get(result_url, timeout=120) |
| 67 | if response.status_code != 200: |