import type { CortexNode, CortexEdge, GraphData, NodeWithConnections, SearchResult, NodeKind, EdgeType, GroupedQueryResult, PromptResult } from './types'; const BASE = '/api'; async function request(path: string, init?: RequestInit): Promise { const res = await fetch(`${BASE}${path}`, { headers: { 'Content-Type': 'application/json' }, ...init, }); if (!res.ok) { const body = await res.json().catch(() => ({})); throw new Error(body.error || `HTTP ${res.status}`); } return res.json(); } export const api = { listNodes: (params?: { kind?: string; status?: string; tags?: string }) => request(`/nodes?${new URLSearchParams(params as any || {})}`), getNode: (id: string) => request(`/nodes/${id}`), addNode: (body: { kind: NodeKind; title: string; content?: string; status?: string; tags?: string[] }) => request('/nodes', { method: 'POST', body: JSON.stringify(body) }), updateNode: (id: string, body: Record) => request(`/nodes/${id}`, { method: 'PATCH', body: JSON.stringify(body) }), deleteNode: (id: string, hard = false) => request<{ ok: boolean }>(`/nodes/${id}?hard=${hard}`, { method: 'DELETE' }), addEdge: (body: { fromId: string; toId: string; type: EdgeType }) => request('/edges', { method: 'POST', body: JSON.stringify(body) }), deleteEdge: (id: string) => request<{ ok: boolean }>(`/edges/${id}`, { method: 'DELETE' }), getGraph: () => request('/graph'), search: (text: string, options?: Record) => request('/search', { method: 'POST', body: JSON.stringify({ text, options }) }), queryOrganized: (text: string) => request('/query/organize', { method: 'POST', body: JSON.stringify({ text }) }), getMaintenanceStatus: () => request>('/maintenance/status'), runMaintenance: () => request>('/maintenance/run', { method: 'POST' }), prompt: (prompt: string) => request('/prompt', { method: 'POST', body: JSON.stringify({ prompt }) }), };