Spaces:
Sleeping
Sleeping
| import type { | |
| LoginRequest, | |
| LoginResponse, | |
| IState, | |
| AIEngineResponse, | |
| HelperPromptResponse, | |
| HelperPromptV2Response, | |
| } from "@/types"; | |
| // ============================================================ | |
| // Base fetch wrapper | |
| // ============================================================ | |
| async function fetchJSON<T>( | |
| url: string, | |
| options?: RequestInit | |
| ): Promise<T> { | |
| 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<T>; | |
| } | |
| // ============================================================ | |
| // Auth API | |
| // ============================================================ | |
| export async function loginUser(payload: LoginRequest): Promise<LoginResponse> { | |
| return fetchJSON<LoginResponse>(`/backend/api/login`, { | |
| method: "POST", | |
| body: JSON.stringify(payload), | |
| }); | |
| } | |
| // ============================================================ | |
| // Chat / AI Engine API | |
| // ============================================================ | |
| export async function callAIEngine(state: IState): Promise<AIEngineResponse> { | |
| return fetchJSON<AIEngineResponse>(`/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<HelperPromptResponse> { | |
| return fetchJSON<HelperPromptResponse>( | |
| `/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<HelperPromptV2Response> { | |
| return fetchJSON<HelperPromptV2Response>( | |
| `/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), | |
| }); | |
| } | |