File size: 7,898 Bytes
dd83bc5
2f04655
 
 
dd83bc5
2f04655
 
 
dd83bc5
 
 
 
2f04655
dd83bc5
2f04655
dd83bc5
 
 
 
 
 
2f04655
dd83bc5
 
 
 
 
 
 
 
 
 
 
 
 
 
2f04655
c4ca284
 
 
2f04655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd83bc5
2f04655
dd83bc5
 
 
 
 
 
2f04655
 
 
dd83bc5
2f04655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ca284
 
 
2f04655
 
 
 
c4ca284
 
 
 
 
 
 
 
 
 
 
 
 
dd83bc5
2f04655
 
dd83bc5
c4ca284
2f04655
dd83bc5
 
022a410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ca284
 
 
022a410
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f04655
 
 
 
 
 
 
 
 
 
 
dd83bc5
 
2f04655
 
 
dd83bc5
2f04655
 
 
 
 
 
 
 
 
 
 
 
dd83bc5
2f04655
 
 
dd83bc5
 
2f04655
 
 
dd83bc5
2f04655
 
 
c4ca284
 
 
 
 
2f04655
 
 
 
 
 
dd83bc5
2f04655
 
 
 
 
 
 
 
 
 
 
c4ca284
 
 
 
2f04655
 
 
c4ca284
2f04655
c4ca284
2f04655
 
c4ca284
2f04655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4ca284
 
 
 
2f04655
 
 
dd83bc5
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// web/src/lib/api.ts
// Aligns with api/server.py routes:
// POST /api/login, /api/chat, /api/upload, /api/export, /api/summary, /api/feedback
// GET  /api/memoryline

export type LearningMode = "general" | "concept" | "socratic" | "exam" | "assignment" | "summary";
export type LanguagePref = "Auto" | "English" | "中文";
export type DocType = "Syllabus" | "Lecture Slides / PPT" | "Literature Review / Paper" | "Other Course Document";

const DEFAULT_TIMEOUT_MS = 20000;

function getBaseUrl() {
  // Vite env: VITE_API_BASE can be "", "http://localhost:8000", etc.
  const v = (import.meta as any)?.env?.VITE_API_BASE as string | undefined;
  return v && v.trim() ? v.trim() : "";
}

async function fetchWithTimeout(input: RequestInfo, init?: RequestInit, timeoutMs = DEFAULT_TIMEOUT_MS) {
  const controller = new AbortController();
  const id = setTimeout(() => controller.abort(), timeoutMs);
  try {
    return await fetch(input, { ...init, signal: controller.signal });
  } finally {
    clearTimeout(id);
  }
}

async function parseJsonSafe(res: Response) {
  const text = await res.text();
  try {
    return text ? JSON.parse(text) : null;
  } catch {
    return { _raw: text };
  }
}

function errMsg(data: any, fallback: string) {
  return (data && (data.error || data.detail || data.message))
    ? String(data.error || data.detail || data.message)
    : fallback;
}

// --------------------
// /api/login
// --------------------
export type ApiLoginReq = {
  name: string;
  user_id: string;
};

export type ApiLoginResp =
  | { ok: true; user: { name: string; user_id: string } }
  | { ok: false; error: string };

export async function apiLogin(payload: ApiLoginReq): Promise<ApiLoginResp> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(`${base}/api/login`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  });

  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiLogin failed (${res.status})`));
  return data as ApiLoginResp;
}

// --------------------
// /api/chat
// --------------------
export type ApiChatReq = {
  user_id: string;
  message: string;
  learning_mode: string;        // backend expects string (not strict union)
  language_preference?: string; // "Auto" | "English" | "中文"
  doc_type?: string;            // "Syllabus" | "Lecture Slides / PPT" | ...
};

export type ApiChatRef = { source_file?: string; section?: string };

export type ApiChatResp = {
  reply: string;
  session_status_md: string;
  refs: ApiChatRef[];
  latency_ms: number;

  // ✅ NEW: optional tracing run id returned by backend
  run_id?: string | null;
};

export async function apiChat(payload: ApiChatReq): Promise<ApiChatResp> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(
    `${base}/api/chat`,
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        language_preference: "Auto",
        doc_type: "Syllabus",
        ...payload,
      }),
    },
    60000 // chat can be slow
  );

  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiChat failed (${res.status})`));

  // backend returns { reply, session_status_md, refs, latency_ms, run_id? }
  return data as ApiChatResp;
}

// --------------------
// /api/quiz/start
// --------------------
export type ApiQuizStartReq = {
  user_id: string;
  language_preference?: string; // "Auto" | "English" | "中文"
  doc_type?: string;            // default: "Literature Review / Paper" (backend default ok)
  learning_mode?: string;       // default: "quiz"
};

export type ApiQuizStartResp = {
  reply: string;
  session_status_md: string;
  refs: ApiChatRef[];
  latency_ms: number;

  // ✅ NEW: optional tracing run id returned by backend (if enabled)
  run_id?: string | null;
};

export async function apiQuizStart(payload: ApiQuizStartReq): Promise<ApiQuizStartResp> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(
    `${base}/api/quiz/start`,
    {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        language_preference: "Auto",
        doc_type: "Literature Review / Paper",
        learning_mode: "quiz",
        ...payload,
      }),
    },
    60000
  );

  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiQuizStart failed (${res.status})`));
  return data as ApiQuizStartResp;
}

// --------------------
// /api/upload
// --------------------
export type ApiUploadResp = {
  ok: boolean;
  added_chunks?: number;
  status_md?: string;
  error?: string;
};

export async function apiUpload(args: { user_id: string; doc_type: string; file: File }): Promise<ApiUploadResp> {
  const base = getBaseUrl();
  const fd = new FormData();
  fd.append("user_id", args.user_id);
  fd.append("doc_type", args.doc_type);
  fd.append("file", args.file);

  const res = await fetchWithTimeout(`${base}/api/upload`, { method: "POST", body: fd }, 120000);
  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiUpload failed (${res.status})`));
  return data as ApiUploadResp;
}

// --------------------
// /api/export
// --------------------
export async function apiExport(payload: { user_id: string; learning_mode: string }): Promise<{ markdown: string }> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(`${base}/api/export`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  });

  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiExport failed (${res.status})`));
  return data as { markdown: string };
}

// --------------------
// /api/summary
// --------------------
export async function apiSummary(payload: {
  user_id: string;
  learning_mode: string;
  language_preference?: string;
}): Promise<{ markdown: string }> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(`${base}/api/summary`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ language_preference: "Auto", ...payload }),
  });

  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiSummary failed (${res.status})`));
  return data as { markdown: string };
}

// --------------------
// /api/feedback
// --------------------
export type ApiFeedbackReq = {
  user_id: string;
  rating: "helpful" | "not_helpful";

  // ✅ NEW: run id so backend can attach feedback to tracing run
  run_id?: string | null;

  assistant_message_id?: string;
  assistant_text: string;
  user_text?: string;

  comment?: string;

  tags?: string[];
  refs?: string[];

  learning_mode?: string;
  doc_type?: string;
  timestamp_ms?: number;
};

export async function apiFeedback(payload: ApiFeedbackReq): Promise<{ ok: boolean }> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(`${base}/api/feedback`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify(payload),
  });

  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiFeedback failed (${res.status})`));
  return data as { ok: boolean };
}

// --------------------
// /api/memoryline
// --------------------
export async function apiMemoryline(user_id: string): Promise<{ next_review_label: string; progress_pct: number }> {
  const base = getBaseUrl();
  const res = await fetchWithTimeout(
    `${base}/api/memoryline?user_id=${encodeURIComponent(user_id)}`,
    { method: "GET" }
  );
  const data = await parseJsonSafe(res);
  if (!res.ok) throw new Error(errMsg(data, `apiMemoryline failed (${res.status})`));
  return data as { next_review_label: string; progress_pct: number };
}