Spaces:
Sleeping
Sleeping
Update web/src/lib/api.ts
Browse files- web/src/lib/api.ts +98 -41
web/src/lib/api.ts
CHANGED
|
@@ -1,61 +1,118 @@
|
|
| 1 |
-
//
|
| 2 |
-
export type
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
export interface
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
-
export async function
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
method: 'POST',
|
| 31 |
headers: { 'Content-Type': 'application/json' },
|
| 32 |
body: JSON.stringify({
|
| 33 |
user_id: params.user.email,
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
? params.docType
|
| 52 |
-
: params.docType
|
| 53 |
-
? docTypeMap[params.docType]
|
| 54 |
-
: undefined,
|
| 55 |
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}),
|
| 58 |
});
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
}
|
|
|
|
| 1 |
+
// web/src/lib/api.ts
|
| 2 |
+
export type LearningMode = 'concept' | 'socratic' | 'exam' | 'assignment' | 'summary';
|
| 3 |
+
export type Language = 'auto' | 'en' | 'zh';
|
| 4 |
+
export type FileType = 'syllabus' | 'lecture-slides' | 'literature-review' | 'other';
|
| 5 |
|
| 6 |
+
export interface User {
|
| 7 |
+
name: string;
|
| 8 |
+
email: string; // use email as user_id
|
| 9 |
+
}
|
| 10 |
|
| 11 |
+
const modeMap: Record<LearningMode, string> = {
|
| 12 |
+
concept: 'Concept Explainer',
|
| 13 |
+
socratic: 'Socratic Tutor',
|
| 14 |
+
exam: 'Exam Prep / Quiz',
|
| 15 |
+
assignment: 'Assignment Helper',
|
| 16 |
+
summary: 'Quick Summary',
|
| 17 |
+
};
|
| 18 |
|
| 19 |
+
const langMap: Record<Language, string> = {
|
| 20 |
+
auto: 'Auto',
|
| 21 |
+
en: 'English',
|
| 22 |
+
zh: '简体中文',
|
| 23 |
+
};
|
| 24 |
|
| 25 |
+
const docTypeMap: Record<FileType, string> = {
|
| 26 |
+
syllabus: 'Syllabus',
|
| 27 |
+
'lecture-slides': 'Lecture Slides',
|
| 28 |
+
'literature-review': 'Literature Review / Paper',
|
| 29 |
+
other: 'Other',
|
| 30 |
+
};
|
| 31 |
|
| 32 |
+
async function mustJson<T>(res: Response): Promise<T> {
|
| 33 |
+
if (res.ok) return (await res.json()) as T;
|
| 34 |
+
const txt = await res.text().catch(() => '');
|
| 35 |
+
throw new Error(`${res.status} ${res.statusText}${txt ? ` — ${txt}` : ''}`);
|
| 36 |
+
}
|
| 37 |
|
| 38 |
+
export async function apiLogin(user: User) {
|
| 39 |
+
const res = await fetch('/api/login', {
|
| 40 |
+
method: 'POST',
|
| 41 |
+
headers: { 'Content-Type': 'application/json' },
|
| 42 |
+
body: JSON.stringify({ name: user.name, user_id: user.email }),
|
| 43 |
+
});
|
| 44 |
+
return mustJson<{ ok: boolean; user: { name: string; user_id: string } }>(res);
|
| 45 |
}
|
| 46 |
|
| 47 |
+
export async function apiChat(params: {
|
| 48 |
+
user: User;
|
| 49 |
+
message: string;
|
| 50 |
+
learningMode: LearningMode;
|
| 51 |
+
language: Language;
|
| 52 |
+
docType?: string; // optional override
|
| 53 |
+
}) {
|
| 54 |
+
const res = await fetch('/api/chat', {
|
| 55 |
method: 'POST',
|
| 56 |
headers: { 'Content-Type': 'application/json' },
|
| 57 |
body: JSON.stringify({
|
| 58 |
user_id: params.user.email,
|
| 59 |
+
message: params.message,
|
| 60 |
+
learning_mode: modeMap[params.learningMode],
|
| 61 |
+
language_preference: langMap[params.language],
|
| 62 |
+
doc_type: params.docType ?? 'Syllabus',
|
| 63 |
+
}),
|
| 64 |
+
});
|
| 65 |
|
| 66 |
+
return mustJson<{
|
| 67 |
+
reply: string;
|
| 68 |
+
refs?: Array<{ source_file?: string; section?: string }>;
|
| 69 |
+
latency_ms?: number;
|
| 70 |
+
session_status_md?: string;
|
| 71 |
+
}>(res);
|
| 72 |
+
}
|
| 73 |
|
| 74 |
+
export async function apiUpload(params: {
|
| 75 |
+
user: User;
|
| 76 |
+
file: File;
|
| 77 |
+
fileType: FileType;
|
| 78 |
+
}) {
|
| 79 |
+
const fd = new FormData();
|
| 80 |
+
fd.append('user_id', params.user.email);
|
| 81 |
+
fd.append('doc_type', docTypeMap[params.fileType]);
|
| 82 |
+
fd.append('file', params.file);
|
| 83 |
|
| 84 |
+
const res = await fetch('/api/upload', { method: 'POST', body: fd });
|
| 85 |
+
return mustJson<{ ok: boolean; added_chunks: number; status_md: string }>(res);
|
| 86 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
export async function apiExport(params: {
|
| 89 |
+
user: User;
|
| 90 |
+
learningMode: LearningMode;
|
| 91 |
+
}) {
|
| 92 |
+
const res = await fetch('/api/export', {
|
| 93 |
+
method: 'POST',
|
| 94 |
+
headers: { 'Content-Type': 'application/json' },
|
| 95 |
+
body: JSON.stringify({
|
| 96 |
+
user_id: params.user.email,
|
| 97 |
+
learning_mode: modeMap[params.learningMode],
|
| 98 |
}),
|
| 99 |
});
|
| 100 |
+
return mustJson<{ markdown: string }>(res);
|
| 101 |
+
}
|
| 102 |
|
| 103 |
+
export async function apiSummary(params: {
|
| 104 |
+
user: User;
|
| 105 |
+
learningMode: LearningMode;
|
| 106 |
+
language: Language;
|
| 107 |
+
}) {
|
| 108 |
+
const res = await fetch('/api/summary', {
|
| 109 |
+
method: 'POST',
|
| 110 |
+
headers: { 'Content-Type': 'application/json' },
|
| 111 |
+
body: JSON.stringify({
|
| 112 |
+
user_id: params.user.email,
|
| 113 |
+
learning_mode: modeMap[params.learningMode],
|
| 114 |
+
language_preference: langMap[params.language],
|
| 115 |
+
}),
|
| 116 |
+
});
|
| 117 |
+
return mustJson<{ markdown: string }>(res);
|
| 118 |
}
|