const DEFAULT_API_BASE_URL = ''; const normalizedBase = (import.meta.env?.VITE_API_BASE_URL ?? DEFAULT_API_BASE_URL).trim(); const API_BASE_URL = normalizedBase ? normalizedBase.replace(/\/+$/, '') : ''; async function request(path, { method = 'GET', params, body } = {}) { const basePath = path.startsWith('/') ? path : `/${path}`; const url = API_BASE_URL ? new URL(`${API_BASE_URL}${basePath}`) : new URL(basePath, window.location.origin); if (params) { Object.entries(params).forEach(([key, value]) => { if (value !== undefined && value !== null) { url.searchParams.set(key, value); } }); } const headers = {}; let payload; if (body !== undefined) { headers['Content-Type'] = 'application/json'; payload = JSON.stringify(body); } const response = await fetch(url.toString(), { method, headers, body: payload, }); if (!response.ok) { let errorMessage = `Request failed with status ${response.status}`; try { const errorBody = await response.json(); errorMessage = errorBody.detail || errorBody.error || errorMessage; } catch { // ignore JSON parsing errors } throw new Error(errorMessage); } return response.json(); } export function fetchProviders() { return request('/api/providers'); } export function fetchSessions() { return request('/api/sessions'); } export function fetchHistory(sessionId) { return request('/api/history', { params: { session_id: sessionId }, }); } export function sendQuery({ sessionId, question, provider }) { return request('/api/query', { method: 'POST', body: { session_id: sessionId, question, provider, }, }); }