const BASE = ""; let _token = null; export function setToken(token) { _token = token; } function authHeaders() { const headers = { "Content-Type": "application/json" }; if (_token) { headers["Authorization"] = `Bearer ${_token}`; } return headers; } async function post(path, body) { const res = await fetch(`${BASE}${path}`, { method: "POST", headers: authHeaders(), body: JSON.stringify(body), }); if (!res.ok) { const text = await res.text(); throw new Error(`${res.status}: ${text}`); } return res; } async function get(path) { const res = await fetch(`${BASE}${path}`, { headers: authHeaders(), }); if (!res.ok) { const text = await res.text(); throw new Error(`${res.status}: ${text}`); } return res; } export async function computeAnalysis(portfolio) { const res = await post("/analysis/compute", portfolio); return res.json(); } export async function computeFrontier(portfolio) { const res = await post("/analysis/frontier", portfolio); return res.json(); } export async function runAgents(portfolio, userProfile, financialGoal) { const res = await post("/agents/run", { portfolio, user_profile: userProfile, financial_goal: financialGoal, }); return res.json(); } export async function askCIO(question, metrics, inferences, frontierSummary, userProfile, goalProjection) { const res = await post("/agents/cio", { question, metrics, inferences, frontier_summary: frontierSummary, user_profile: userProfile, goal_projection: goalProjection, }); return res.json(); } export async function textToSpeech(text, voice = "Rachel") { const res = await post("/tts/report", { text, voice }); return res.blob(); } export async function loadUserData() { const res = await get("/user/data"); return res.json(); } export async function saveUserData(data) { const res = await post("/user/data", data); return res.json(); } // ── Insights ────────────────────────────────────────────── export async function fetchSectorExposure(tickers, weights) { const res = await post("/insights/sector-exposure", { tickers, weights }); return res.json(); } export async function fetchDividends(tickers, weights, investment_amount = 100000) { const res = await post("/insights/dividends", { tickers, weights, investment_amount }); return res.json(); } export async function fetchRebalancing(tickers, target_weights, portfolio_value = 100000, drift_threshold = 0.05) { const res = await post("/insights/rebalancing", { tickers, target_weights, portfolio_value, drift_threshold }); return res.json(); } export async function fetchMacroCorrelations(tickers, weights, start_date) { const res = await post("/insights/macro-correlations", { tickers, weights, start_date }); return res.json(); } export async function fetchTaxHarvest(tickers, weights, portfolio_value = 100000, cost_basis_date = "2020-01-01", tax_rate = 0.15) { const res = await post("/insights/tax-harvest", { tickers, weights, portfolio_value, cost_basis_date, tax_rate }); return res.json(); } // ── PDF report ────────────────────────────────────────────── export async function generatePDFReport(reportData) { const res = await post("/reports/pdf", reportData); return res.blob(); } // ── Portfolio versions ────────────────────────────────────── export async function savePortfolioSnapshot(tickers, weights, label, performance) { const res = await post("/portfolio/versions/save", { tickers, weights, label, performance }); return res.json(); } export async function listPortfolioSnapshots() { const res = await get("/portfolio/versions/list"); return res.json(); } // ── Alerts ────────────────────────────────────────────────── export async function checkAlerts(portfolio, rules) { const res = await post("/alerts/check", { portfolio, rules }); return res.json(); } // ── SSE streaming agents ──────────────────────────────────── export function streamAgents(portfolio, userProfile, financialGoal, onEvent) { const body = JSON.stringify({ portfolio, user_profile: userProfile, financial_goal: financialGoal, }); fetch(`${BASE}/agents/run-stream`, { method: "POST", headers: authHeaders(), body, }).then(async (response) => { const reader = response.body.getReader(); const decoder = new TextDecoder(); let buffer = ""; while (true) { const { done, value } = await reader.read(); if (done) break; buffer += decoder.decode(value, { stream: true }); const lines = buffer.split("\n"); buffer = lines.pop() || ""; let currentEvent = null; for (const line of lines) { if (line.startsWith("event: ")) { currentEvent = line.slice(7).trim(); } else if (line.startsWith("data: ") && currentEvent) { try { const data = JSON.parse(line.slice(6)); onEvent(currentEvent, data); } catch { /* skip malformed */ } currentEvent = null; } } } }); }