import { createDocSlurpClient, DocSlurpApiError } from "@docslurp/sdk"; // Usage: node watch-run.mjs RUN_ID const runId = process.argv[2]; if (!runId) throw new Error("Pass the run ID printed by your upload."); const client = createDocSlurpClient(); // One budget shared by streaming and polling; fallback does not reset the clock. const signal = AbortSignal.timeout(600_000); try { for await (const event of client.streamRun(runId, { signal, timeoutMs: 60_000 })) { console.error(event.event, JSON.stringify(event.data)); const run = event.data.run; if (run && (run.status === "completed" || run.status === "failed" || ["paused", "cancelled", "dead-letter"].includes(run.controlState ?? ""))) break; } } catch (error) { signal.throwIfAborted(); if (error instanceof DocSlurpApiError && [401, 403, 404].includes(error.status)) throw error; console.error("Live updates unavailable; checking progress by polling."); } // Also covers a cleanly closed stream. Validates completion and throws on failure. const run = await client.waitForRun(runId, { signal, timeoutMs: 600_000 }); console.log(JSON.stringify(run, null, 2));