SarahXia0405 commited on
Commit
c4ca284
·
verified ·
1 Parent(s): 941d25c

Update web/src/lib/api.ts

Browse files
Files changed (1) hide show
  1. web/src/lib/api.ts +39 -17
web/src/lib/api.ts CHANGED
@@ -35,7 +35,9 @@ async function parseJsonSafe(res: Response) {
35
  }
36
 
37
  function errMsg(data: any, fallback: string) {
38
- return (data && (data.error || data.detail || data.message)) ? String(data.error || data.detail || data.message) : fallback;
 
 
39
  }
40
 
41
  // --------------------
@@ -81,28 +83,34 @@ export type ApiChatResp = {
81
  session_status_md: string;
82
  refs: ApiChatRef[];
83
  latency_ms: number;
 
 
 
84
  };
85
 
86
  export async function apiChat(payload: ApiChatReq): Promise<ApiChatResp> {
87
  const base = getBaseUrl();
88
- const res = await fetchWithTimeout(`${base}/api/chat`, {
89
- method: "POST",
90
- headers: { "Content-Type": "application/json" },
91
- body: JSON.stringify({
92
- language_preference: "Auto",
93
- doc_type: "Syllabus",
94
- ...payload,
95
- }),
96
- }, 60000); // chat can be slow
 
 
 
 
97
 
98
  const data = await parseJsonSafe(res);
99
  if (!res.ok) throw new Error(errMsg(data, `apiChat failed (${res.status})`));
100
 
101
- // backend returns { reply, session_status_md, refs, latency_ms }
102
  return data as ApiChatResp;
103
  }
104
 
105
-
106
  // --------------------
107
  // /api/quiz/start
108
  // --------------------
@@ -118,6 +126,9 @@ export type ApiQuizStartResp = {
118
  session_status_md: string;
119
  refs: ApiChatRef[];
120
  latency_ms: number;
 
 
 
121
  };
122
 
123
  export async function apiQuizStart(payload: ApiQuizStartReq): Promise<ApiQuizStartResp> {
@@ -142,9 +153,6 @@ export async function apiQuizStart(payload: ApiQuizStartReq): Promise<ApiQuizSta
142
  return data as ApiQuizStartResp;
143
  }
144
 
145
-
146
-
147
-
148
  // --------------------
149
  // /api/upload
150
  // --------------------
@@ -187,7 +195,11 @@ export async function apiExport(payload: { user_id: string; learning_mode: strin
187
  // --------------------
188
  // /api/summary
189
  // --------------------
190
- export async function apiSummary(payload: { user_id: string; learning_mode: string; language_preference?: string }): Promise<{ markdown: string }> {
 
 
 
 
191
  const base = getBaseUrl();
192
  const res = await fetchWithTimeout(`${base}/api/summary`, {
193
  method: "POST",
@@ -206,12 +218,19 @@ export async function apiSummary(payload: { user_id: string; learning_mode: stri
206
  export type ApiFeedbackReq = {
207
  user_id: string;
208
  rating: "helpful" | "not_helpful";
 
 
 
 
209
  assistant_message_id?: string;
210
  assistant_text: string;
211
  user_text?: string;
 
212
  comment?: string;
 
213
  tags?: string[];
214
  refs?: string[];
 
215
  learning_mode?: string;
216
  doc_type?: string;
217
  timestamp_ms?: number;
@@ -235,7 +254,10 @@ export async function apiFeedback(payload: ApiFeedbackReq): Promise<{ ok: boolea
235
  // --------------------
236
  export async function apiMemoryline(user_id: string): Promise<{ next_review_label: string; progress_pct: number }> {
237
  const base = getBaseUrl();
238
- const res = await fetchWithTimeout(`${base}/api/memoryline?user_id=${encodeURIComponent(user_id)}`, { method: "GET" });
 
 
 
239
  const data = await parseJsonSafe(res);
240
  if (!res.ok) throw new Error(errMsg(data, `apiMemoryline failed (${res.status})`));
241
  return data as { next_review_label: string; progress_pct: number };
 
35
  }
36
 
37
  function errMsg(data: any, fallback: string) {
38
+ return (data && (data.error || data.detail || data.message))
39
+ ? String(data.error || data.detail || data.message)
40
+ : fallback;
41
  }
42
 
43
  // --------------------
 
83
  session_status_md: string;
84
  refs: ApiChatRef[];
85
  latency_ms: number;
86
+
87
+ // ✅ NEW: optional tracing run id returned by backend
88
+ run_id?: string | null;
89
  };
90
 
91
  export async function apiChat(payload: ApiChatReq): Promise<ApiChatResp> {
92
  const base = getBaseUrl();
93
+ const res = await fetchWithTimeout(
94
+ `${base}/api/chat`,
95
+ {
96
+ method: "POST",
97
+ headers: { "Content-Type": "application/json" },
98
+ body: JSON.stringify({
99
+ language_preference: "Auto",
100
+ doc_type: "Syllabus",
101
+ ...payload,
102
+ }),
103
+ },
104
+ 60000 // chat can be slow
105
+ );
106
 
107
  const data = await parseJsonSafe(res);
108
  if (!res.ok) throw new Error(errMsg(data, `apiChat failed (${res.status})`));
109
 
110
+ // backend returns { reply, session_status_md, refs, latency_ms, run_id? }
111
  return data as ApiChatResp;
112
  }
113
 
 
114
  // --------------------
115
  // /api/quiz/start
116
  // --------------------
 
126
  session_status_md: string;
127
  refs: ApiChatRef[];
128
  latency_ms: number;
129
+
130
+ // ✅ NEW: optional tracing run id returned by backend (if enabled)
131
+ run_id?: string | null;
132
  };
133
 
134
  export async function apiQuizStart(payload: ApiQuizStartReq): Promise<ApiQuizStartResp> {
 
153
  return data as ApiQuizStartResp;
154
  }
155
 
 
 
 
156
  // --------------------
157
  // /api/upload
158
  // --------------------
 
195
  // --------------------
196
  // /api/summary
197
  // --------------------
198
+ export async function apiSummary(payload: {
199
+ user_id: string;
200
+ learning_mode: string;
201
+ language_preference?: string;
202
+ }): Promise<{ markdown: string }> {
203
  const base = getBaseUrl();
204
  const res = await fetchWithTimeout(`${base}/api/summary`, {
205
  method: "POST",
 
218
  export type ApiFeedbackReq = {
219
  user_id: string;
220
  rating: "helpful" | "not_helpful";
221
+
222
+ // ✅ NEW: run id so backend can attach feedback to tracing run
223
+ run_id?: string | null;
224
+
225
  assistant_message_id?: string;
226
  assistant_text: string;
227
  user_text?: string;
228
+
229
  comment?: string;
230
+
231
  tags?: string[];
232
  refs?: string[];
233
+
234
  learning_mode?: string;
235
  doc_type?: string;
236
  timestamp_ms?: number;
 
254
  // --------------------
255
  export async function apiMemoryline(user_id: string): Promise<{ next_review_label: string; progress_pct: number }> {
256
  const base = getBaseUrl();
257
+ const res = await fetchWithTimeout(
258
+ `${base}/api/memoryline?user_id=${encodeURIComponent(user_id)}`,
259
+ { method: "GET" }
260
+ );
261
  const data = await parseJsonSafe(res);
262
  if (!res.ok) throw new Error(errMsg(data, `apiMemoryline failed (${res.status})`));
263
  return data as { next_review_label: string; progress_pct: number };