File size: 2,731 Bytes
1ed65e9
 
 
 
 
 
799afcf
1ed65e9
 
 
db983c6
1ed65e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
277896e
1ed65e9
 
 
 
 
 
 
 
 
277896e
1ed65e9
 
 
 
 
 
 
 
 
 
 
 
 
 
277896e
db983c6
1ed65e9
 
db983c6
799afcf
 
 
 
 
 
 
 
 
 
 
db983c6
 
 
 
277896e
db983c6
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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),
  });
}