// frontend/src/api.js export async function runSearch(query, topK, answerStyle, handlers) { const res = await fetch('/api/search', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ query, top_k: topK, answer_style: answerStyle }), }); const { jobId } = await res.json(); const es = new EventSource(`/api/jobs/${jobId}/events`); es.addEventListener('stage', (e) => handlers.onStage?.(JSON.parse(e.data))); es.addEventListener('papers', (e) => handlers.onPapers?.(JSON.parse(e.data).papers)); es.addEventListener('answer_token', (e) => handlers.onAnswerToken?.(JSON.parse(e.data).text)); es.addEventListener('consensus', (e) => handlers.onConsensus?.(JSON.parse(e.data))); es.addEventListener('blocked', (e) => { handlers.onBlocked?.(JSON.parse(e.data)); es.close(); }); es.addEventListener('result', () => { handlers.onDone?.(); es.close(); }); es.addEventListener('error', (e) => { try { handlers.onError?.(JSON.parse(e.data)); } catch { handlers.onError?.({ message: 'connection lost' }); } es.close(); }); return es; } export async function fetchUmapSample() { const res = await fetch('/api/umap-sample'); return (await res.json()).points; } export async function fetchStats() { const res = await fetch('/api/stats'); return (await res.json()).total; }