|
| 1 | +/** |
| 2 | + * Unified Backend API Client & SSE Stream Handler |
| 3 | + */ |
| 4 | + |
| 5 | +export async function fetchSystemStatus() { |
| 6 | + const res = await fetch('/api/status'); |
| 7 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 8 | + return await res.json(); |
| 9 | +} |
| 10 | + |
| 11 | +export async function fetchDocuments() { |
| 12 | + const res = await fetch('/api/documents'); |
| 13 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 14 | + return await res.json(); |
| 15 | +} |
| 16 | + |
| 17 | +export async function deleteDocument(name) { |
| 18 | + const res = await fetch(`/api/documents/${encodeURIComponent(name)}`, { |
| 19 | + method: 'DELETE' |
| 20 | + }); |
| 21 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 22 | + return await res.json(); |
| 23 | +} |
| 24 | + |
| 25 | +export async function clearWorkspace() { |
| 26 | + const res = await fetch('/api/workspace/clear', { |
| 27 | + method: 'POST' |
| 28 | + }); |
| 29 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 30 | + return await res.json(); |
| 31 | +} |
| 32 | + |
| 33 | +export async function uploadFiles(fileList) { |
| 34 | + const formData = new FormData(); |
| 35 | + for (let i = 0; i < fileList.length; i++) { |
| 36 | + formData.append('files', fileList[i]); |
| 37 | + } |
| 38 | + const res = await fetch('/api/upload', { |
| 39 | + method: 'POST', |
| 40 | + body: formData |
| 41 | + }); |
| 42 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 43 | + return await res.json(); |
| 44 | +} |
| 45 | + |
| 46 | +export async function syncKnowledgeBase(parserSettings) { |
| 47 | + const res = await fetch('/api/sync', { |
| 48 | + method: 'POST', |
| 49 | + headers: { 'Content-Type': 'application/json' }, |
| 50 | + body: JSON.stringify({ |
| 51 | + parser: parserSettings.parser, |
| 52 | + llamaparse_key: parserSettings.llamaparseKey, |
| 53 | + unstructured_key: parserSettings.unstructuredKey |
| 54 | + }) |
| 55 | + }); |
| 56 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 57 | + return await res.json(); |
| 58 | +} |
| 59 | + |
| 60 | +export async function fetchExistingJsonLd(fileName) { |
| 61 | + const res = await fetch(`/api/jsonld/${encodeURIComponent(fileName)}`); |
| 62 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 63 | + return await res.json(); |
| 64 | +} |
| 65 | + |
| 66 | +export async function extractJsonLdStream(params, signal, onEvent) { |
| 67 | + const response = await fetch('/api/extract-jsonld-stream', { |
| 68 | + method: 'POST', |
| 69 | + headers: { 'Content-Type': 'application/json' }, |
| 70 | + signal: signal, |
| 71 | + body: JSON.stringify({ |
| 72 | + file_name: params.fileName, |
| 73 | + llm_provider: params.provider, |
| 74 | + llm_model: params.model, |
| 75 | + api_key: params.apiKey, |
| 76 | + base_url: params.baseUrl |
| 77 | + }) |
| 78 | + }); |
| 79 | + |
| 80 | + if (!response.ok) { |
| 81 | + throw new Error(`Server returned status ${response.status}`); |
| 82 | + } |
| 83 | + |
| 84 | + const reader = response.body.getReader(); |
| 85 | + const decoder = new TextDecoder('utf-8'); |
| 86 | + let buffer = ''; |
| 87 | + |
| 88 | + while (true) { |
| 89 | + const { value, done } = await reader.read(); |
| 90 | + if (done) break; |
| 91 | + |
| 92 | + buffer += decoder.decode(value, { stream: true }); |
| 93 | + const lines = buffer.split(/\r?\n/); |
| 94 | + buffer = lines.pop(); |
| 95 | + |
| 96 | + for (const line of lines) { |
| 97 | + const trimmed = line.trim(); |
| 98 | + if (trimmed.startsWith('data:')) { |
| 99 | + const jsonStr = trimmed.replace(/^data:\s*/, '').trim(); |
| 100 | + if (jsonStr) { |
| 101 | + try { |
| 102 | + const event = JSON.parse(jsonStr); |
| 103 | + onEvent(event); |
| 104 | + } catch (err) { |
| 105 | + console.warn('Failed to parse SSE JSON:', jsonStr, err); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if (buffer && buffer.trim().startsWith('data:')) { |
| 113 | + try { |
| 114 | + const event = JSON.parse(buffer.trim().replace(/^data:\s*/, '')); |
| 115 | + onEvent(event); |
| 116 | + } catch (e) {} |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +export async function sendChatMessage(params) { |
| 121 | + const res = await fetch('/api/chat', { |
| 122 | + method: 'POST', |
| 123 | + headers: { 'Content-Type': 'application/json' }, |
| 124 | + body: JSON.stringify({ |
| 125 | + query: params.query, |
| 126 | + file_name: params.fileName, |
| 127 | + llm_provider: params.provider, |
| 128 | + llm_model: params.model, |
| 129 | + api_key: params.apiKey, |
| 130 | + base_url: params.baseUrl |
| 131 | + }) |
| 132 | + }); |
| 133 | + if (!res.ok) throw new Error(`HTTP error ${res.status}`); |
| 134 | + return await res.json(); |
| 135 | +} |
| 136 | + |
| 137 | +export async function testLlmConnection(params) { |
| 138 | + const res = await fetch('/api/diagnostics/llm/test', { |
| 139 | + method: 'POST', |
| 140 | + headers: { 'Content-Type': 'application/json' }, |
| 141 | + body: JSON.stringify(params) |
| 142 | + }); |
| 143 | + return await res.json(); |
| 144 | +} |
| 145 | + |
| 146 | +export async function testParserService(params) { |
| 147 | + const res = await fetch('/api/diagnostics/parser/test', { |
| 148 | + method: 'POST', |
| 149 | + headers: { 'Content-Type': 'application/json' }, |
| 150 | + body: JSON.stringify(params) |
| 151 | + }); |
| 152 | + return await res.json(); |
| 153 | +} |
| 154 | + |
| 155 | +export async function testGraphdbConnection(params) { |
| 156 | + const res = await fetch('/api/enterprise/graphdb/test', { |
| 157 | + method: 'POST', |
| 158 | + headers: { 'Content-Type': 'application/json' }, |
| 159 | + body: JSON.stringify(params) |
| 160 | + }); |
| 161 | + return await res.json(); |
| 162 | +} |
| 163 | + |
| 164 | +export async function syncGraphdb(params) { |
| 165 | + const res = await fetch('/api/enterprise/graphdb/sync', { |
| 166 | + method: 'POST', |
| 167 | + headers: { 'Content-Type': 'application/json' }, |
| 168 | + body: JSON.stringify(params) |
| 169 | + }); |
| 170 | + return await res.json(); |
| 171 | +} |
| 172 | + |
| 173 | +export function getExportUrl(format, docName) { |
| 174 | + const encoded = encodeURIComponent(docName); |
| 175 | + if (format === 'ttl') return `/api/export/ttl/${encoded}`; |
| 176 | + if (format === 'bibtex') return `/api/export/bibtex/${encoded}`; |
| 177 | + if (format === 'ris') return `/api/export/ris/${encoded}`; |
| 178 | + if (format === 'csl') return `/api/export/csl/${encoded}`; |
| 179 | + if (format === 'cypher') return `/api/export/cypher/${encoded}`; |
| 180 | + if (format === 'graph') return `/api/export/graph/${encoded}`; |
| 181 | + return `/api/export/${encoded}`; |
| 182 | +} |
0 commit comments