async function pollResearch(researchId, apiKey, options = {}) {
const {
intervalMs = 5000, // 5 seconds between polls
timeoutMs = 180000, // 3 minute max
onProgress = null,
} = options;
const BASE_URL = 'https://rpiqzfzokrwxavztrpmp.supabase.co/functions/v1';
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
await new Promise(r => setTimeout(r, intervalMs));
const res = await fetch(`${BASE_URL}/research-status/${researchId}`, {
headers: { 'Authorization': `Bearer ${apiKey}` },
});
const data = await res.json();
onProgress?.(data);
if (data.status === 'completed') return data;
if (data.status === 'failed') throw new Error(`Research failed: ${data.error}`);
}
throw new Error('Research timed out after 3 minutes');
}