Spaces:
Sleeping
Sleeping
File size: 14,435 Bytes
cc20f66 909b61e cc20f66 909b61e cc20f66 909b61e cc20f66 909b61e cc20f66 05b03b0 909b61e cc20f66 909b61e cc20f66 909b61e cc20f66 909b61e cc20f66 909b61e cc20f66 45e7f4a cc20f66 909b61e cc20f66 909b61e cc20f66 909b61e 0c0f5cc 909b61e 0c0f5cc 909b61e cc20f66 49c6642 cc20f66 49c6642 cc20f66 909b61e | 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 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 | import type {
ChatMessageResponse,
ChatSessionResponse,
CourseResponse,
DocumentSource,
} from '@/lib/rag';
import type {
AdminUser,
GeneratedApiKey,
LlmConfig,
RagConfig,
RetrievalConfig,
VectorDbConfig,
} from '@/types/admin';
export type { ChatMessageResponse, ChatSessionResponse, CourseResponse };
export type DocumentResponse = {
id: string;
filename: string;
file_type: string;
file_size: number;
status: 'processing' | 'ready' | 'failed';
course_id: string;
course_name?: string;
uploaded_by: string;
uploader_name?: string;
summary?: string;
chunk_count?: number;
error?: string;
created_at: string;
updated_at: string;
};
export type AuthUser = {
id?: number | string;
name?: string;
email?: string;
role?: string;
is_active?: boolean;
is_superuser?: boolean;
[key: string]: unknown;
};
export class RagApiError extends Error {
constructor(
message: string,
public readonly status: number,
public readonly data?: unknown,
) {
super(message);
this.name = 'RagApiError';
}
}
const BASE_URL =
(import.meta.env.VITE_RAG_API_BASE_URL as string | undefined)?.replace(
/\/$/,
'',
) ?? '';
function getAuthToken(): string | null {
if (typeof document === 'undefined') {
return null;
}
const match = document.cookie.match(
/(?:^|;\s*)sevima_raghub_auth_token=([^;]*)/,
);
return match ? decodeURIComponent(match[1]) : null;
}
function makeHeaders(
extra: Record<string, string> = {},
): Record<string, string> {
const token = getAuthToken();
return {
Accept: 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
...extra,
};
}
async function buildRagError(response: Response): Promise<RagApiError> {
let body: unknown = null;
let message = `${response.status} ${response.statusText}`;
if (response.status === 429) {
message = 'Terlalu banyak percobaan. Silakan tunggu beberapa saat sebelum mencoba lagi.';
}
try {
body = await response.json();
if (body !== null && typeof body === 'object') {
const b = body as Record<string, unknown>;
if (typeof b.detail === 'string' && b.detail) {
message = b.detail;
} else if (typeof b.message === 'string' && b.message) {
message = b.message;
} else if (typeof b.error === 'string' && b.error) {
message = b.error;
}
}
} catch {
// ignore
}
return new RagApiError(message, response.status, body);
}
async function ragGet<T>(path: string): Promise<T> {
const response = await fetch(`${BASE_URL}${path}`, {
headers: makeHeaders(),
});
if (!response.ok) {
throw await buildRagError(response);
}
return response.json() as Promise<T>;
}
async function ragJson<T>(
path: string,
method: string,
body: unknown,
): Promise<T> {
const response = await fetch(`${BASE_URL}${path}`, {
body: JSON.stringify(body),
headers: makeHeaders({ 'Content-Type': 'application/json' }),
method,
});
if (!response.ok) {
throw await buildRagError(response);
}
return response.json() as Promise<T>;
}
async function ragDelete(path: string): Promise<void> {
const response = await fetch(`${BASE_URL}${path}`, {
headers: makeHeaders(),
method: 'DELETE',
});
if (!response.ok) {
throw await buildRagError(response);
}
}
// Courses
export function listCourses(
page = 1,
limit = 100,
): Promise<{ data: CourseResponse[] }> {
return ragGet(`/courses?page=${page}&limit=${limit}`);
}
export function getCourse(courseId: string): Promise<CourseResponse> {
return ragGet(`/courses/${encodeURIComponent(courseId)}`);
}
export function createCourse(payload: {
title: string;
description?: string;
}): Promise<CourseResponse> {
return ragJson('/courses', 'POST', payload);
}
export function updateCourse(
courseId: string,
payload: { title?: string; description?: string },
): Promise<CourseResponse> {
return ragJson(`/courses/${encodeURIComponent(courseId)}`, 'PUT', payload);
}
export function deleteCourse(courseId: string): Promise<void> {
return ragDelete(`/courses/${encodeURIComponent(courseId)}`);
}
export function listCourseDocuments(
courseId: string,
): Promise<{ data: DocumentResponse[] }> {
return ragGet(`/courses/${encodeURIComponent(courseId)}/documents`);
}
export function deleteDocument(
courseId: string,
documentId: string,
): Promise<void> {
return ragDelete(
`/courses/${encodeURIComponent(courseId)}/documents/${encodeURIComponent(documentId)}`,
);
}
export async function uploadDocument(
courseId: string,
file: File,
): Promise<void> {
const token = getAuthToken();
const formData = new FormData();
formData.append('file', file);
formData.append('course_id', courseId);
const response = await fetch(`${BASE_URL}/documents/upload`, {
body: formData,
headers: {
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
method: 'POST',
});
if (!response.ok) {
throw await buildRagError(response);
}
}
// Chat Sessions
export function listChatSessions(
page = 1,
limit = 20,
): Promise<{ data: ChatSessionResponse[] }> {
return ragGet(`/chats/sessions?page=${page}&limit=${limit}`);
}
export function getChatSession(
sessionId: string,
): Promise<ChatSessionResponse> {
return ragGet(`/chats/sessions/${encodeURIComponent(sessionId)}`);
}
export function createChatSession(payload: {
course_id: string;
title: string;
}): Promise<ChatSessionResponse> {
return ragJson('/chats/sessions', 'POST', payload);
}
export function deleteChatSession(sessionId: string): Promise<void> {
return ragDelete(`/chats/sessions/${encodeURIComponent(sessionId)}`);
}
export function getChatHistory(
sessionId: string,
): Promise<{ data: ChatMessageResponse[] }> {
return ragGet(
`/chats/sessions/${encodeURIComponent(sessionId)}/messages`,
);
}
export async function openStreamChatMessage(
sessionId: string,
content: string,
): Promise<Response> {
const token = getAuthToken();
const response = await fetch(
`${BASE_URL}/chats/sessions/${encodeURIComponent(sessionId)}/stream`,
{
body: JSON.stringify({ content }),
headers: {
Accept: 'text/event-stream',
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
},
method: 'POST',
},
);
if (!response.ok) {
throw await buildRagError(response);
}
return response;
}
// Auth
export async function loginUser(
email: string,
password: string,
): Promise<{ token: string; user: AuthUser }> {
return ragJson('/auth/login', 'POST', { email, password });
}
export async function registerUser(
payload: Record<string, unknown>,
): Promise<{ message?: string }> {
return ragJson('/auth/register', 'POST', payload);
}
function setRawCookie(name: string, value: string, remember: boolean): void {
const secure =
typeof window !== 'undefined' &&
window.location.protocol === 'https:';
const maxAge = remember ? 60 * 60 * 24 * 30 : undefined;
const parts = [
`${name}=${encodeURIComponent(value)}`,
'path=/',
'SameSite=Lax',
];
if (secure) {
parts.push('Secure');
}
if (maxAge !== undefined) {
parts.push(`max-age=${maxAge}`);
}
if (typeof document !== 'undefined') {
document.cookie = parts.join('; ');
}
}
export function setAuthCookies(
token: string,
user: AuthUser,
remember: boolean,
): void {
// Token cookie — dibutuhkan middleware server untuk autentikasi
setRawCookie('sevima_raghub_auth_token', token, remember);
// User cookie — hanya simpan role, cukup untuk pengecekan middleware server
// Full user disimpan di localStorage agar tidak membebani request header
setRawCookie('sevima_raghub_auth_user', JSON.stringify({ role: user.role }), remember);
// Full user disimpan di storage lokal (tidak dikirim sebagai cookie/header)
const storage = remember ? window.localStorage : window.sessionStorage;
storage.setItem('sevima_raghub_auth_user', JSON.stringify(user));
}
// Admin: RAG Config
export function getRagConfig(): Promise<RagConfig> {
return ragGet('/admin/rag/config');
}
export function getLlmConfig(): Promise<LlmConfig> {
return ragGet('/admin/rag/config/llm');
}
export function patchLlmConfig(
payload: Record<string, unknown>,
): Promise<LlmConfig> {
return ragJson('/admin/rag/config/llm', 'PATCH', payload);
}
export function getVectorDbConfig(): Promise<VectorDbConfig> {
return ragGet('/admin/rag/config/vector_db');
}
export function patchVectorDbConfig(
payload: Record<string, unknown>,
): Promise<VectorDbConfig> {
return ragJson('/admin/rag/config/vector_db', 'PATCH', payload);
}
export function getRetrievalConfig(): Promise<RetrievalConfig> {
return ragGet('/admin/rag/config/retrieval');
}
export function patchRetrievalConfig(
payload: Record<string, unknown>,
): Promise<RetrievalConfig> {
return ragJson('/admin/rag/config/retrieval', 'PATCH', payload);
}
// Admin: API Keys
export function generateApiKey(): Promise<GeneratedApiKey> {
return ragJson('/auth/api-keys', 'POST', {});
}
// Admin: Users
export function listAdminUsers(
query: Record<string, string> = {},
): Promise<{ founds?: AdminUser[] }> {
const params = new URLSearchParams(query).toString();
const path = params ? `/user?${params}` : '/user';
return ragGet(path);
}
export async function createAdminUser(
payload: Record<string, unknown>,
): Promise<AdminUser> {
const response = await ragJson<{ message: string; user: AdminUser }>(
'/auth/register',
'POST',
payload,
);
return response.user;
}
export function updateAdminUser(
userId: number | string,
payload: Record<string, unknown>,
): Promise<AdminUser> {
return ragJson(`/user/${encodeURIComponent(String(userId))}`, 'PATCH', payload);
}
export function deleteAdminUser(userId: number | string): Promise<void> {
return ragDelete(`/user/${encodeURIComponent(String(userId))}`);
}
// Embed: internal helpers (explicit token, not from cookie)
function makeEmbedHeaders(
token: string,
extra: Record<string, string> = {},
): Record<string, string> {
return {
Accept: 'application/json',
Authorization: `Bearer ${token}`,
...extra,
};
}
async function embedGet<T>(path: string, token: string): Promise<T> {
const response = await fetch(`${BASE_URL}${path}`, {
headers: makeEmbedHeaders(token),
});
if (!response.ok) {
throw await buildRagError(response);
}
return response.json() as Promise<T>;
}
async function embedJson<T>(
path: string,
method: string,
body: unknown,
token: string,
): Promise<T> {
const response = await fetch(`${BASE_URL}${path}`, {
body: JSON.stringify(body),
headers: makeEmbedHeaders(token, { 'Content-Type': 'application/json' }),
method,
});
if (!response.ok) {
throw await buildRagError(response);
}
return response.json() as Promise<T>;
}
async function embedDelete(path: string, token: string): Promise<void> {
const response = await fetch(`${BASE_URL}${path}`, {
headers: makeEmbedHeaders(token),
method: 'DELETE',
});
if (!response.ok) {
throw await buildRagError(response);
}
}
// Embed: Auth
export function loginWithEmbedApiKey(
apiKey: string,
): Promise<{ token: string; user: AuthUser }> {
return ragJson('/auth/login/api-key', 'POST', { api_key: apiKey });
}
// Embed: API
export function listCoursesEmbed(
token: string,
page = 1,
limit = 100,
): Promise<{ data: CourseResponse[] }> {
return embedGet(`/courses?page=${page}&limit=${limit}`, token);
}
export function listChatSessionsEmbed(
token: string,
courseId?: string,
page = 1,
limit = 50,
): Promise<{ data: ChatSessionResponse[] }> {
const params = new URLSearchParams({
limit: String(limit),
page: String(page),
});
if (courseId) {
params.set('course_id', courseId);
}
return embedGet(`/chats/sessions?${params.toString()}`, token);
}
export function getChatHistoryEmbed(
sessionId: string,
token: string,
): Promise<{ data: ChatMessageResponse[] }> {
return embedGet(
`/chats/sessions/${encodeURIComponent(sessionId)}/messages`,
token,
);
}
export function createChatSessionEmbed(
payload: { course_id: string; title: string },
token: string,
): Promise<ChatSessionResponse> {
return embedJson('/chats/sessions', 'POST', payload, token);
}
export function deleteChatSessionEmbed(
sessionId: string,
token: string,
): Promise<void> {
return embedDelete(
`/chats/sessions/${encodeURIComponent(sessionId)}`,
token,
);
}
export function sendRestChatMessage(
sessionId: string,
content: string,
token: string,
): Promise<ChatMessageResponse> {
return embedJson(
`/chats/sessions/${encodeURIComponent(sessionId)}/messages`,
'POST',
{ content },
token,
);
}
export async function queryAiDirect(
courseId: string,
prompt: string,
token: string,
): Promise<ChatMessageResponse> {
type DirectResponse = {
message_id: string;
role?: string;
content: string;
sources?: DocumentSource[];
created_at: string;
};
const data = await embedJson<DirectResponse>(
'/ai/query',
'POST',
{ course_id: courseId, prompt },
token,
);
return {
content: data.content,
created_at: data.created_at,
role: data.role ?? 'assistant',
sources: data.sources ?? [],
uuid_id: data.message_id,
};
}
|