import { supabase } from "./lib/supabase"; import type { Evaluation, Objection, ScenarioBrief, ScenarioListItem, StartSessionResponse, TranscriptTurn, } from "./types"; // Empty base => same-origin (Vite proxy handles it in dev). const BASE = import.meta.env.VITE_API_BASE ?? ""; // Attach the current user's Supabase access token so the backend can identify // them on protected routes. Returns {} when logged out (public routes still work). async function authHeaders(): Promise> { const { data } = await supabase.auth.getSession(); const token = data.session?.access_token; return token ? { Authorization: `Bearer ${token}` } : {}; } async function getJSON(path: string): Promise { const res = await fetch(`${BASE}${path}`, { headers: await authHeaders() }); if (!res.ok) throw new Error(`${res.status} ${res.statusText} on ${path}`); return res.json() as Promise; } // Smoke test for auth wiring: returns the signed-in user's id/email/profile. export function me() { return getJSON<{ id: string; email: string; profile: unknown }>("/me"); } // Book a practice slot via the backend (so it can send a confirmation email). export async function scheduleSession( scheduledAtISO: string, scenarioId: string | null, note: string | null ): Promise<{ id: string }> { const res = await fetch(`${BASE}/scheduled`, { method: "POST", headers: { "Content-Type": "application/json", ...(await authHeaders()) }, body: JSON.stringify({ scheduled_at: scheduledAtISO, scenario_id: scenarioId, note, }), }); if (!res.ok) throw new Error(`Schedule failed: ${res.status}`); return res.json(); } // Permanently delete the signed-in user's account (needs the server's admin key). export async function deleteAccount(): Promise { const res = await fetch(`${BASE}/me`, { method: "DELETE", headers: await authHeaders(), }); if (!res.ok) throw new Error(`Delete account failed: ${res.status}`); } export function listScenarios(level?: number, persona?: string, lang = "en") { const q = new URLSearchParams(); if (level) q.set("level", String(level)); if (persona) q.set("persona", persona); if (lang && lang !== "en") q.set("lang", lang); const qs = q.toString(); return getJSON(`/scenarios${qs ? `?${qs}` : ""}`); } export function getBrief(id: string, lang = "en") { const qs = lang && lang !== "en" ? `?lang=${lang}` : ""; return getJSON(`/scenarios/${id}/brief${qs}`); } export function listObjections(lang = "en") { const qs = lang && lang !== "en" ? `?lang=${lang}` : ""; return getJSON(`/objections${qs}`); } export async function startSession( scenarioId: string, language: "en" | "de" = "en" ): Promise { const res = await fetch(`${BASE}/sessions`, { method: "POST", headers: { "Content-Type": "application/json", ...(await authHeaders()) }, body: JSON.stringify({ scenario_id: scenarioId, language }), }); if (!res.ok) throw new Error(`Start session failed: ${res.status}`); return res.json(); } export async function endSession(sessionId: string): Promise { await fetch(`${BASE}/sessions/${sessionId}`, { method: "DELETE", headers: await authHeaders(), }); } // End a call and get it scored + saved. Returns the evaluation to display. export async function finishSession( sessionId: string, transcript: TranscriptTurn[] ): Promise { const res = await fetch(`${BASE}/sessions/${sessionId}/finish`, { method: "POST", headers: { "Content-Type": "application/json", ...(await authHeaders()) }, body: JSON.stringify({ transcript }), }); if (!res.ok) throw new Error(`Finish session failed: ${res.status}`); return res.json(); } // Build the absolute ws:// URL for the control socket from a relative path. export function controlWsUrl(path: string): string { if (BASE) return BASE.replace(/^http/, "ws") + path; const proto = location.protocol === "https:" ? "wss" : "ws"; return `${proto}://${location.host}${path}`; }