import { existsSync } from "node:fs"; import { readFile } from "node:fs/promises"; async function sleep(ms: number) { await new Promise((resolve) => setTimeout(resolve, ms)); } export async function monitorRun(input: { eventsPath: string; resultPath: string; onEvent?: (event: any) => void; pollMs?: number; }) { const pollMs = input.pollMs ?? 50; let offset = 0; while (true) { if (existsSync(input.eventsPath)) { const text = await readFile(input.eventsPath, "utf8"); const next = text.slice(offset); offset = text.length; for (const line of next.split("\n").filter(Boolean)) { input.onEvent?.(JSON.parse(line)); } } if (existsSync(input.resultPath)) { return JSON.parse(await readFile(input.resultPath, "utf8")); } await sleep(pollMs); } }