Spaces:
Running
Running
| // βββ Types ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export interface LoginResponse { | |
| status: string; | |
| message: string; | |
| data: { user_id: string; email: string; name: string }; | |
| } | |
| export interface Room { | |
| id: string; | |
| title: string; | |
| created_at: string; | |
| updated_at: string | null; | |
| } | |
| export interface CreateRoomResponse { | |
| status: string; | |
| message: string; | |
| data: Room; | |
| } | |
| export type DocumentStatus = "pending" | "processing" | "completed" | "failed"; | |
| export interface ApiDocument { | |
| id: string; | |
| filename: string; | |
| status: DocumentStatus; | |
| file_size: number; | |
| file_type: string; | |
| created_at: string; | |
| } | |
| export interface UploadDocumentResponse { | |
| status: string; | |
| message: string; | |
| data: { id: string; filename: string; status: DocumentStatus }; | |
| } | |
| export interface ChatSource { | |
| document_id: string; | |
| filename: string; | |
| page_label: string | null; | |
| } | |
| // βββ Base Client ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| const BASE_URL = (import.meta.env.VITE_API_BASE_URL as string) ?? ""; | |
| async function request<T>(path: string, options?: RequestInit): Promise<T> { | |
| const res = await fetch(`${BASE_URL}${path}`, { | |
| headers: { "Content-Type": "application/json", ...options?.headers }, | |
| ...options, | |
| }); | |
| if (!res.ok) { | |
| const err = await res | |
| .json() | |
| .catch(() => ({ detail: `HTTP ${res.status}` })); | |
| throw new Error(err.detail ?? `HTTP ${res.status}`); | |
| } | |
| return res.json() as Promise<T>; | |
| } | |
| // βββ Auth βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export const login = (email: string, password: string) => | |
| request<LoginResponse>("/api/login", { | |
| method: "POST", | |
| body: JSON.stringify({ email, password }), | |
| }); | |
| // βββ Rooms ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export const getRooms = (userId: string) => | |
| request<Room[]>(`/api/v1/rooms/${userId}`); | |
| export const createRoom = (userId: string, title?: string) => | |
| request<CreateRoomResponse>("/api/v1/room/create", { | |
| method: "POST", | |
| body: JSON.stringify({ user_id: userId, title }), | |
| }); | |
| // βββ Documents ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export const getDocuments = (userId: string) => | |
| request<ApiDocument[]>(`/api/v1/documents/${userId}`); | |
| export const uploadDocument = async ( | |
| userId: string, | |
| file: File | |
| ): Promise<UploadDocumentResponse> => { | |
| const form = new FormData(); | |
| form.append("file", file); | |
| const res = await fetch( | |
| `${BASE_URL}/api/v1/document/upload?user_id=${userId}`, | |
| { method: "POST", body: form } | |
| ); | |
| if (!res.ok) { | |
| const err = await res | |
| .json() | |
| .catch(() => ({ detail: `HTTP ${res.status}` })); | |
| throw new Error(err.detail ?? `HTTP ${res.status}`); | |
| } | |
| return res.json() as Promise<UploadDocumentResponse>; | |
| }; | |
| export const processDocument = (userId: string, documentId: string) => | |
| request<{ | |
| status: string; | |
| message: string; | |
| data: { document_id: string; chunks_processed: number }; | |
| }>( | |
| `/api/v1/document/process?document_id=${documentId}&user_id=${userId}`, | |
| { method: "POST" } | |
| ); | |
| export const deleteDocument = (userId: string, documentId: string) => | |
| request<{ status: string; message: string }>( | |
| `/api/v1/document/delete?document_id=${documentId}&user_id=${userId}`, | |
| { method: "DELETE" } | |
| ); | |
| // βββ Chat βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| export const streamChat = ( | |
| userId: string, | |
| roomId: string, | |
| message: string | |
| ): Promise<Response> => | |
| fetch(`${BASE_URL}/api/v1/chat/stream`, { | |
| method: "POST", | |
| headers: { "Content-Type": "application/json" }, | |
| body: JSON.stringify({ user_id: userId, room_id: roomId, message }), | |
| }); | |