| import type { |
| UploadResponse, BuildGraphResponse, CausalTriple, |
| GraphData, ChatResponse, AppSettings, |
| } from './types' |
|
|
| const BASE = '/api' |
|
|
| async function req<T>(path: string, options?: RequestInit): Promise<T> { |
| const res = await fetch(BASE + path, options) |
| if (!res.ok) { |
| const text = await res.text() |
| throw new Error(text || `HTTP ${res.status}`) |
| } |
| return res.json() as Promise<T> |
| } |
|
|
| export const api = { |
| health: () => req<{ status: string; neo4j: string }>('/health'), |
|
|
| upload: (file: File): Promise<UploadResponse> => { |
| const fd = new FormData() |
| fd.append('file', file) |
| return req<UploadResponse>('/upload', { method: 'POST', body: fd }) |
| }, |
|
|
| loadSample: (): Promise<UploadResponse> => |
| req<UploadResponse>('/sample'), |
|
|
| buildGraph: (documentId: string, settings: Partial<AppSettings>): Promise<BuildGraphResponse> => |
| req<BuildGraphResponse>('/build-graph', { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify({ |
| document_id: documentId, |
| confidence_threshold: settings.confidenceThreshold ?? 0.5, |
| max_chunks: 50, |
| }), |
| }), |
|
|
| getTriples: (documentId: string): Promise<{ triples: CausalTriple[] }> => |
| req<{ triples: CausalTriple[] }>(`/triples?document_id=${documentId}`), |
|
|
| getGraph: (documentId: string): Promise<GraphData> => |
| req<GraphData>(`/graph?document_id=${documentId}`), |
|
|
| chat: (documentId: string, message: string, settings: Partial<AppSettings>): Promise<ChatResponse> => |
| req<ChatResponse>('/chat', { |
| method: 'POST', |
| headers: { 'Content-Type': 'application/json' }, |
| body: JSON.stringify({ |
| document_id: documentId, |
| message, |
| max_depth: settings.maxDepth ?? 5, |
| top_k_chains: settings.topKChains ?? 5, |
| theme: settings.theme || null, |
| }), |
| }), |
|
|
| reset: () => req<{ status: string }>('/reset', { method: 'POST' }), |
| } |
|
|