import type { LoginRequest, LoginResponse, IState, AIEngineResponse, HelperPromptResponse, HelperPromptV2Response, } from "@/types"; // ============================================================ // Base fetch wrapper // ============================================================ async function fetchJSON( url: string, options?: RequestInit ): Promise { const res = await fetch(url, { headers: { "Content-Type": "application/json", ...options?.headers }, ...options, }); if (!res.ok) { let errorMessage = `HTTP ${res.status}`; try { const body = await res.json(); errorMessage = body.message || body.detail || errorMessage; } catch { // ignore parse error } throw new Error(errorMessage); } return res.json() as Promise; } // ============================================================ // Auth API // ============================================================ export async function loginUser(payload: LoginRequest): Promise { return fetchJSON(`/backend/api/login`, { method: "POST", body: JSON.stringify(payload), }); } // ============================================================ // Chat / AI Engine API // ============================================================ export async function callAIEngine(state: IState): Promise { return fetchJSON(`/backend/api/agents/call`, { method: "POST", body: JSON.stringify(state), signal: AbortSignal.timeout(130_000), }); } // ============================================================ // Helper Prompt (Equipment List) API // ============================================================ export async function getHelperPrompt( topic: string, year: number ): Promise { return fetchJSON( `/backend/api/helper_prompt?topic=${encodeURIComponent(topic)}&year=${year}`, { signal: AbortSignal.timeout(40_000) } ); } export async function getHelperPromptV2( topic: string, year: number, withEqm = true ): Promise { return fetchJSON( `/backend/api/v2/helper_prompt?topic=${encodeURIComponent(topic)}&year=${year}&with_eqm=${withEqm}`, { signal: AbortSignal.timeout(40_000) } ); } // ============================================================ // Feedback API // ============================================================ export async function sendFeedback(payload: unknown): Promise<{ status: string }> { return fetchJSON<{ status: string }>(`/backend/api/feedback`, { method: "POST", body: JSON.stringify(payload), signal: AbortSignal.timeout(15_000), }); }