/** * API utilities: axios instance and SSE helper. */ export const API_BASE = '' export const api = axios.create({ baseURL: '/api' }) /** * Consume a Server-Sent Events stream from a POST endpoint. * @param {string} url - The URL to POST to (e.g. '/api/train') * @param {object} body - JSON body to send * @param {function} onMessage - Callback invoked with each parsed SSE message object * @returns {Promise} */ export async function fetchSSE(url, body, onMessage) { const resp = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body), }) const reader = resp.body.getReader() const dec = new TextDecoder() let buf = '' while (true) { const { done, value } = await reader.read() if (done) break buf += dec.decode(value, { stream: true }) const parts = buf.split('\n\n') buf = parts.pop() for (const part of parts) { const line = part.trim() if (!line.startsWith('data:')) continue const msg = JSON.parse(line.slice(5).trim()) onMessage(msg) } } }