const API_BASE = import.meta.env.VITE_API_URL || 'http://localhost:8051'; function authHeaders(): Record { const token = localStorage.getItem('auth_token'); return token ? { Authorization: `Bearer ${token}` } : {}; } export interface ChatResponse { response: string; sources?: Array<{ filename: string; content: string; score: number; }>; thinking_time?: number; } export interface KBStats { document_count: number; chunk_count: number; last_updated?: string; total_size?: number; } export interface Document { filename: string; chunk_count: number; created_at?: string; } export async function chat( kbName: string, query: string, options?: { useHybrid?: boolean; useRerank?: boolean; useAgentic?: boolean } ): Promise { const res = await fetch(`${API_BASE}/api/kb/${kbName}/chat`, { method: 'POST', headers: { 'Content-Type': 'application/json', ...authHeaders() }, body: JSON.stringify({ query, use_hybrid: options?.useHybrid ?? false, use_rerank: options?.useRerank ?? false, use_agentic: options?.useAgentic ?? false, }), }); if (!res.ok) { throw new Error(`Chat failed: ${res.statusText}`); } return res.json(); } export async function getKBStats(kbName: string): Promise { const res = await fetch(`${API_BASE}/api/kb/${kbName}/stats`, { headers: authHeaders() }); if (!res.ok) { throw new Error(`Failed to get KB stats: ${res.statusText}`); } return res.json(); } export async function listDocuments(kbName: string): Promise { const res = await fetch(`${API_BASE}/api/kb/${kbName}/documents`, { headers: authHeaders() }); if (!res.ok) { throw new Error(`Failed to list documents: ${res.statusText}`); } const data = await res.json(); return data.documents || []; } export async function uploadDocument(kbName: string, file: File): Promise { const formData = new FormData(); formData.append('file', file); const res = await fetch(`${API_BASE}/api/kb/${kbName}/upload`, { method: 'POST', headers: authHeaders(), body: formData, }); if (!res.ok) { throw new Error(`Failed to upload document: ${res.statusText}`); } } export async function deleteDocument(kbName: string, filename: string): Promise { const res = await fetch(`${API_BASE}/api/kb/${kbName}/documents/${encodeURIComponent(filename)}`, { method: 'DELETE', headers: authHeaders(), }); if (!res.ok) { throw new Error(`Failed to delete document: ${res.statusText}`); } }