const API = "https://api.webtotable.app/v1";const headers = { Authorization: `Bearer ${process.env.WEBTOTABLE_API_KEY}`, "Content-Type": "application/json",};const get = (path: string) => fetch(API + path, { headers }).then((r) => r.json());const sleep = (seconds: number) => new Promise((done) => setTimeout(done, seconds * 1000)); // 1. Start a run. It returns at once.const run = await fetch(`${API}/research`, { method: "POST", headers, body: JSON.stringify({ prompt: "25 bouldering gyms in Colorado, " + "with day-pass price and opening year", cap: 400, // the most credits this run may use idempotencyKey: "gyms-co-v1", // a retry gets the same run }),}).then((r) => r.json());if (!run.runId) throw new Error(run.error.message); // 2. Wait as long as the server says.let res = run;while (!res.done) { await sleep(res.checkAfterSeconds); res = await get(`/runs/${run.runId}/wait`); if (res.status === "waiting_for_user") { // Ask your user, then POST /v1/runs/{runId}/answer await answer(run.runId, res.question); }}if (res.status !== "completed") throw new Error(res.error); // 3. Store each value with its source.for (const row of res.table.rows) { const pass = row.cells["Day pass (USD)"]; await db.insert(gyms).values({ name: row.cells["Gym"].value, dayPassUsd: pass.value, dayPassSource: pass.sourceUrl, });}