Spaces:
Runtime error
Runtime error
Download frontend/src/services/api.ts from adithaf7/intelligent-data-room: direct link, hf CLI and curl.
- Browser
- Download file 2.74 kB
-
https://huggingface.co/spaces/adithaf7/intelligent-data-room/resolve/main/frontend/src/services/api.ts
- Command line
-
hf download hf://spaces/adithaf7/intelligent-data-room/frontend/src/services/api.ts
-
curl -L -o api.ts https://huggingface.co/spaces/adithaf7/intelligent-data-room/resolve/main/frontend/src/services/api.ts
2.74 kB
| import type { FileUploadResponse, QueryRequest, QueryResponse, Message } from '../types'; | |
| // API base URL - will be set from environment variable | |
| const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:8000'; | |
| /** | |
| * Custom error class for API errors | |
| */ | |
| class ApiError extends Error { | |
| statusCode: number; | |
| constructor(statusCode: number, message: string) { | |
| super(message); | |
| this.name = 'ApiError'; | |
| this.statusCode = statusCode; | |
| } | |
| } | |
| /** | |
| * Generic fetch wrapper with error handling | |
| */ | |
| async function fetchApi<T>( | |
| endpoint: string, | |
| options: RequestInit = {} | |
| ): Promise<T> { | |
| const url = `${API_BASE_URL}${endpoint}`; | |
| try { | |
| const response = await fetch(url, { | |
| ...options, | |
| headers: { | |
| ...options.headers, | |
| }, | |
| }); | |
| if (!response.ok) { | |
| const errorData = await response.json().catch(() => ({})); | |
| throw new ApiError( | |
| response.status, | |
| errorData.detail || `HTTP error! status: ${response.status}` | |
| ); | |
| } | |
| return await response.json(); | |
| } catch (error) { | |
| if (error instanceof ApiError) { | |
| throw error; | |
| } | |
| throw new ApiError(500, 'Network error. Please check your connection.'); | |
| } | |
| } | |
| /** | |
| * Upload a CSV/XLSX file to the backend | |
| * @param file - The file to upload | |
| * @param sessionId - Current session ID | |
| * @returns File metadata including URL and columns | |
| */ | |
| export async function uploadFile( | |
| file: File, | |
| sessionId: string | |
| ): Promise<FileUploadResponse> { | |
| const formData = new FormData(); | |
| formData.append('file', file); | |
| formData.append('session_id', sessionId); | |
| return fetchApi<FileUploadResponse>('/api/upload', { | |
| method: 'POST', | |
| body: formData, | |
| }); | |
| } | |
| /** | |
| * Send a query to the multi-agent system | |
| * @param request - Query request with session_id, question, and file_url | |
| * @returns Answer, plan, and optional chart configuration | |
| */ | |
| export async function sendQuery(request: QueryRequest): Promise<QueryResponse> { | |
| return fetchApi<QueryResponse>('/api/query', { | |
| method: 'POST', | |
| headers: { | |
| 'Content-Type': 'application/json', | |
| }, | |
| body: JSON.stringify(request), | |
| }); | |
| } | |
| /** | |
| * Get chat history for a session | |
| * @param sessionId - Session ID to fetch history for | |
| * @returns Array of messages | |
| */ | |
| export async function getChatHistory(sessionId: string): Promise<Message[]> { | |
| return fetchApi<Message[]>(`/api/history/${sessionId}`); | |
| } | |
| /** | |
| * Health check endpoint | |
| * @returns Health status | |
| */ | |
| export async function healthCheck(): Promise<{ status: string }> { | |
| return fetchApi<{ status: string }>('/health'); | |
| } | |
| /** | |
| * Generate a unique session ID | |
| * @returns UUID string | |
| */ | |
| export function generateSessionId(): string { | |
| return crypto.randomUUID(); | |
| } | |