fraudTech commited on
Commit
ecfe2d7
·
verified ·
1 Parent(s): 0f291c6

Upload 8 files

Browse files
Files changed (8) hide show
  1. README.md +44 -0
  2. package.json +44 -0
  3. public/index.html +17 -0
  4. src/App.css +379 -0
  5. src/App.js +313 -0
  6. src/api.js +136 -0
  7. src/index.css +17 -0
  8. src/index.js +11 -0
README.md ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Prolog AI Frontend
2
+
3
+ React frontend for Prolog AI chatbot with streaming chat and file upload capabilities.
4
+
5
+ ## Features
6
+
7
+ - 🔐 JWT authentication
8
+ - 💬 Streaming chat interface
9
+ - 📁 Upload .pl and .qlf knowledge base files
10
+ - 🔒 Strict mode toggle
11
+ - 📊 KB metadata display
12
+ - 🎨 Modern, responsive UI
13
+
14
+ ## Environment Variables
15
+
16
+ Create `.env` file:
17
+
18
+ ```
19
+ REACT_APP_API_BASE=http://localhost:7861
20
+ ```
21
+
22
+ ## Development
23
+
24
+ ```bash
25
+ npm install
26
+ npm start
27
+ ```
28
+
29
+ Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
30
+
31
+ ## Production Build
32
+
33
+ ```bash
34
+ npm run build
35
+ ```
36
+
37
+ Builds the app for production to the `build` folder.
38
+
39
+ ## Default Credentials
40
+
41
+ - Username: `demo`
42
+ - Password: `demo-password`
43
+
44
+ (Configure in gateway environment variables)
package.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "prolog-ai-frontend",
3
+ "version": "0.1.0",
4
+ "private": true,
5
+ "dependencies": {
6
+ "@testing-library/dom": "^10.4.0",
7
+ "@testing-library/jest-dom": "^6.6.3",
8
+ "@testing-library/react": "^16.3.0",
9
+ "@testing-library/user-event": "^13.5.0",
10
+ "react": "^19.1.0",
11
+ "react-dom": "^19.1.0",
12
+ "react-scripts": "5.0.1",
13
+ "web-vitals": "^2.1.4",
14
+ "react-markdown": "^9.0.1",
15
+ "remark-math": "^6.0.0",
16
+ "remark-gfm": "^4.0.0",
17
+ "rehype-katex": "^7.0.0",
18
+ "katex": "^0.16.10"
19
+ },
20
+ "scripts": {
21
+ "start": "react-scripts start",
22
+ "build": "react-scripts build",
23
+ "test": "react-scripts test",
24
+ "eject": "react-scripts eject"
25
+ },
26
+ "eslintConfig": {
27
+ "extends": [
28
+ "react-app",
29
+ "react-app/jest"
30
+ ]
31
+ },
32
+ "browserslist": {
33
+ "production": [
34
+ ">0.2%",
35
+ "not dead",
36
+ "not op_mini all"
37
+ ],
38
+ "development": [
39
+ "last 1 chrome version",
40
+ "last 1 firefox version",
41
+ "last 1 safari version"
42
+ ]
43
+ }
44
+ }
public/index.html ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1" />
6
+ <meta name="theme-color" content="#667eea" />
7
+ <meta
8
+ name="description"
9
+ content="Prolog-powered AI chatbot with logical reasoning capabilities"
10
+ />
11
+ <title>Prolog AI</title>
12
+ </head>
13
+ <body>
14
+ <noscript>You need to enable JavaScript to run this app.</noscript>
15
+ <div id="root"></div>
16
+ </body>
17
+ </html>
src/App.css ADDED
@@ -0,0 +1,379 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* App.css - Prolog AI Styling */
2
+
3
+ * {
4
+ box-sizing: border-box;
5
+ margin: 0;
6
+ padding: 0;
7
+ }
8
+
9
+ .app-root {
10
+ display: flex;
11
+ flex-direction: column;
12
+ height: 100vh;
13
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
14
+ font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", sans-serif;
15
+ }
16
+
17
+ /* ============================================================
18
+ * HEADER
19
+ * ============================================================ */
20
+ .header {
21
+ background: rgba(255, 255, 255, 0.95);
22
+ backdrop-filter: blur(10px);
23
+ padding: 1rem 2rem;
24
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
25
+ display: flex;
26
+ justify-content: space-between;
27
+ align-items: center;
28
+ }
29
+
30
+ .header h1 {
31
+ color: #667eea;
32
+ font-size: 1.8rem;
33
+ font-weight: 700;
34
+ }
35
+
36
+ .header-controls {
37
+ display: flex;
38
+ gap: 1rem;
39
+ align-items: center;
40
+ }
41
+
42
+ .strict-toggle {
43
+ display: flex;
44
+ align-items: center;
45
+ gap: 0.5rem;
46
+ cursor: pointer;
47
+ font-weight: 500;
48
+ color: #333;
49
+ }
50
+
51
+ .sidebar-toggle,
52
+ .logout-btn {
53
+ padding: 0.5rem 1rem;
54
+ border: none;
55
+ border-radius: 6px;
56
+ background: #667eea;
57
+ color: white;
58
+ font-weight: 600;
59
+ cursor: pointer;
60
+ transition: all 0.2s;
61
+ }
62
+
63
+ .sidebar-toggle:hover,
64
+ .logout-btn:hover {
65
+ background: #764ba2;
66
+ transform: translateY(-2px);
67
+ }
68
+
69
+ /* ============================================================
70
+ * APP LAYOUT
71
+ * ============================================================ */
72
+ .app-layout {
73
+ flex: 1;
74
+ display: flex;
75
+ overflow: hidden;
76
+ }
77
+
78
+ /* ============================================================
79
+ * MAIN CHAT AREA
80
+ * ============================================================ */
81
+ .main {
82
+ flex: 1;
83
+ display: flex;
84
+ flex-direction: column;
85
+ padding: 2rem;
86
+ overflow: hidden;
87
+ }
88
+
89
+ .messages {
90
+ flex: 1;
91
+ overflow-y: auto;
92
+ margin-bottom: 1.5rem;
93
+ display: flex;
94
+ flex-direction: column;
95
+ gap: 1rem;
96
+ }
97
+
98
+ .message {
99
+ display: flex;
100
+ animation: slideIn 0.3s ease-out;
101
+ }
102
+
103
+ @keyframes slideIn {
104
+ from {
105
+ opacity: 0;
106
+ transform: translateY(10px);
107
+ }
108
+ to {
109
+ opacity: 1;
110
+ transform: translateY(0);
111
+ }
112
+ }
113
+
114
+ .message.user {
115
+ justify-content: flex-end;
116
+ }
117
+
118
+ .message.assistant {
119
+ justify-content: flex-start;
120
+ }
121
+
122
+ .bubble {
123
+ max-width: 70%;
124
+ padding: 1rem 1.5rem;
125
+ border-radius: 18px;
126
+ box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
127
+ }
128
+
129
+ .message.user .bubble {
130
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
131
+ color: white;
132
+ }
133
+
134
+ .message.assistant .bubble {
135
+ background: white;
136
+ color: #333;
137
+ }
138
+
139
+ /* ============================================================
140
+ * INPUT ROW
141
+ * ============================================================ */
142
+ .input-row {
143
+ display: flex;
144
+ gap: 1rem;
145
+ background: rgba(255, 255, 255, 0.95);
146
+ backdrop-filter: blur(10px);
147
+ padding: 1rem;
148
+ border-radius: 12px;
149
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
150
+ }
151
+
152
+ .input-row textarea {
153
+ flex: 1;
154
+ padding: 0.75rem;
155
+ border: 2px solid #e0e0e0;
156
+ border-radius: 8px;
157
+ font-size: 1rem;
158
+ resize: none;
159
+ font-family: inherit;
160
+ }
161
+
162
+ .input-row textarea:focus {
163
+ outline: none;
164
+ border-color: #667eea;
165
+ }
166
+
167
+ .input-row button {
168
+ padding: 0.75rem 2rem;
169
+ border: none;
170
+ border-radius: 8px;
171
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
172
+ color: white;
173
+ font-weight: 600;
174
+ cursor: pointer;
175
+ transition: all 0.2s;
176
+ }
177
+
178
+ .input-row button:hover:not(:disabled) {
179
+ transform: translateY(-2px);
180
+ box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
181
+ }
182
+
183
+ .input-row button:disabled {
184
+ opacity: 0.5;
185
+ cursor: not-allowed;
186
+ }
187
+
188
+ /* ============================================================
189
+ * SIDEBAR
190
+ * ============================================================ */
191
+ .sidebar {
192
+ width: 350px;
193
+ background: rgba(255, 255, 255, 0.95);
194
+ backdrop-filter: blur(10px);
195
+ padding: 2rem;
196
+ overflow-y: auto;
197
+ box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);
198
+ }
199
+
200
+ .sidebar h3 {
201
+ color: #667eea;
202
+ margin-bottom: 1rem;
203
+ font-size: 1.3rem;
204
+ }
205
+
206
+ .sidebar h4 {
207
+ color: #764ba2;
208
+ margin-top: 1.5rem;
209
+ margin-bottom: 0.5rem;
210
+ }
211
+
212
+ .upload-section {
213
+ margin-bottom: 1.5rem;
214
+ }
215
+
216
+ #file-input {
217
+ display: none;
218
+ }
219
+
220
+ .upload-btn {
221
+ display: block;
222
+ padding: 0.75rem 1.5rem;
223
+ background: #667eea;
224
+ color: white;
225
+ border-radius: 8px;
226
+ text-align: center;
227
+ cursor: pointer;
228
+ font-weight: 600;
229
+ transition: all 0.2s;
230
+ }
231
+
232
+ .upload-btn:hover {
233
+ background: #764ba2;
234
+ transform: translateY(-2px);
235
+ }
236
+
237
+ .upload-status {
238
+ margin-top: 1rem;
239
+ padding: 0.75rem;
240
+ border-radius: 6px;
241
+ font-size: 0.9rem;
242
+ }
243
+
244
+ .upload-status.success {
245
+ background: #d4edda;
246
+ color: #155724;
247
+ border: 1px solid #c3e6cb;
248
+ }
249
+
250
+ .upload-status.error {
251
+ background: #f8d7da;
252
+ color: #721c24;
253
+ border: 1px solid #f5c6cb;
254
+ }
255
+
256
+ .kb-metadata {
257
+ margin-top: 1.5rem;
258
+ }
259
+
260
+ .kb-metadata pre {
261
+ background: #f8f9fa;
262
+ padding: 1rem;
263
+ border-radius: 6px;
264
+ font-size: 0.85rem;
265
+ overflow-x: auto;
266
+ border: 1px solid #e0e0e0;
267
+ max-height: 300px;
268
+ overflow-y: auto;
269
+ }
270
+
271
+ /* ============================================================
272
+ * LOGIN SCREEN
273
+ * ============================================================ */
274
+ .main.login {
275
+ display: flex;
276
+ align-items: center;
277
+ justify-content: center;
278
+ }
279
+
280
+ .login-form {
281
+ background: rgba(255, 255, 255, 0.95);
282
+ backdrop-filter: blur(10px);
283
+ padding: 3rem;
284
+ border-radius: 16px;
285
+ box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
286
+ width: 100%;
287
+ max-width: 400px;
288
+ }
289
+
290
+ .login-form h2 {
291
+ color: #667eea;
292
+ margin-bottom: 1.5rem;
293
+ text-align: center;
294
+ }
295
+
296
+ .login-form input {
297
+ width: 100%;
298
+ padding: 0.75rem;
299
+ margin-bottom: 1rem;
300
+ border: 2px solid #e0e0e0;
301
+ border-radius: 8px;
302
+ font-size: 1rem;
303
+ }
304
+
305
+ .login-form input:focus {
306
+ outline: none;
307
+ border-color: #667eea;
308
+ }
309
+
310
+ .login-form button {
311
+ width: 100%;
312
+ padding: 0.75rem;
313
+ border: none;
314
+ border-radius: 8px;
315
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
316
+ color: white;
317
+ font-weight: 600;
318
+ font-size: 1rem;
319
+ cursor: pointer;
320
+ transition: all 0.2s;
321
+ }
322
+
323
+ .login-form button:hover {
324
+ transform: translateY(-2px);
325
+ box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
326
+ }
327
+
328
+ /* ============================================================
329
+ * ERROR MESSAGES
330
+ * ============================================================ */
331
+ .error {
332
+ background: #f8d7da;
333
+ color: #721c24;
334
+ padding: 1rem;
335
+ border-radius: 8px;
336
+ margin-top: 1rem;
337
+ border: 1px solid #f5c6cb;
338
+ }
339
+
340
+ /* ============================================================
341
+ * MARKDOWN CONTENT
342
+ * ============================================================ */
343
+ .bubble pre {
344
+ background: #f8f9fa;
345
+ padding: 1rem;
346
+ border-radius: 6px;
347
+ overflow-x: auto;
348
+ margin: 0.5rem 0;
349
+ }
350
+
351
+ .bubble code {
352
+ background: #f8f9fa;
353
+ padding: 0.2rem 0.4rem;
354
+ border-radius: 3px;
355
+ font-family: 'Courier New', monospace;
356
+ }
357
+
358
+ .bubble pre code {
359
+ background: none;
360
+ padding: 0;
361
+ }
362
+
363
+ .bubble ul,
364
+ .bubble ol {
365
+ margin-left: 1.5rem;
366
+ margin-top: 0.5rem;
367
+ margin-bottom: 0.5rem;
368
+ }
369
+
370
+ .bubble p {
371
+ margin: 0.5rem 0;
372
+ }
373
+
374
+ .bubble h1,
375
+ .bubble h2,
376
+ .bubble h3 {
377
+ margin-top: 1rem;
378
+ margin-bottom: 0.5rem;
379
+ }
src/App.js ADDED
@@ -0,0 +1,313 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // src/App.js - Prolog AI Frontend
2
+
3
+ import React, { useState } from "react";
4
+ import { streamChat, login, uploadKB, getKBMetadata } from "./api";
5
+ import "./App.css";
6
+
7
+ import ReactMarkdown from "react-markdown";
8
+ import remarkMath from "remark-math";
9
+ import rehypeKatex from "rehype-katex";
10
+ import "katex/dist/katex.min.css";
11
+ import remarkGfm from "remark-gfm";
12
+
13
+ function App() {
14
+ // Authentication State
15
+ const [token, setToken] = useState(null);
16
+ const [authError, setAuthError] = useState(null);
17
+ const [loginForm, setLoginForm] = useState({
18
+ username: "",
19
+ password: "",
20
+ });
21
+
22
+ // Chat State
23
+ const [messages, setMessages] = useState([
24
+ { role: "assistant", text: "Hello! I'm your Prolog-powered AI assistant. Upload a .pl file to teach me new knowledge!" },
25
+ ]);
26
+ const [input, setInput] = useState("");
27
+ const [strictMode, setStrictMode] = useState(false);
28
+ const [loading, setLoading] = useState(false);
29
+ const [error, setError] = useState(null);
30
+
31
+ // Upload State
32
+ const [uploadStatus, setUploadStatus] = useState(null);
33
+ const [kbMetadata, setKbMetadata] = useState(null);
34
+ const [showSidebar, setShowSidebar] = useState(false);
35
+
36
+ // Auto-scroll on message change
37
+ React.useEffect(() => {
38
+ const container = document.querySelector(".messages");
39
+ if (container) {
40
+ container.scrollTop = container.scrollHeight;
41
+ }
42
+ }, [messages]);
43
+
44
+ // ============================================================
45
+ // LOGIN HANDLER
46
+ // ============================================================
47
+ async function handleLogin(e) {
48
+ e.preventDefault();
49
+ setAuthError(null);
50
+
51
+ try {
52
+ const res = await login(loginForm.username, loginForm.password);
53
+ setToken(res.token);
54
+ setAuthError(null);
55
+
56
+ // Load initial KB metadata
57
+ try {
58
+ const metadata = await getKBMetadata(res.token);
59
+ setKbMetadata(metadata.metadata);
60
+ } catch (err) {
61
+ console.warn("Could not load KB metadata:", err);
62
+ }
63
+ } catch (err) {
64
+ setAuthError(err.message || "Login failed");
65
+ }
66
+ }
67
+
68
+ // ============================================================
69
+ // SEND CHAT MESSAGE
70
+ // ============================================================
71
+ async function handleSend(e) {
72
+ if (e) e.preventDefault();
73
+ if (!input.trim() || loading || !token) return;
74
+
75
+ const userMsg = { role: "user", text: input.trim() };
76
+ const newMessages = [...messages, userMsg];
77
+
78
+ // Create empty assistant bubble for streaming
79
+ const assistantMsg = { role: "assistant", text: "" };
80
+ newMessages.push(assistantMsg);
81
+
82
+ setMessages(newMessages);
83
+ setInput("");
84
+ setError(null);
85
+ setLoading(true);
86
+
87
+ try {
88
+ const historyForBackend = newMessages.map((m) => ({
89
+ role: m.role,
90
+ content: m.text,
91
+ }));
92
+
93
+ await streamChat(
94
+ userMsg.text,
95
+ strictMode,
96
+ historyForBackend,
97
+ token,
98
+ (chunk) => {
99
+ assistantMsg.text += chunk;
100
+ setMessages([...newMessages]);
101
+ }
102
+ );
103
+
104
+ } catch (err) {
105
+ assistantMsg.text += `\n[Error: ${err.message}]`;
106
+ setMessages([...newMessages]);
107
+ } finally {
108
+ setLoading(false);
109
+ }
110
+ }
111
+
112
+ // ============================================================
113
+ // FILE UPLOAD HANDLER
114
+ // ============================================================
115
+ async function handleFileUpload(e) {
116
+ const file = e.target.files[0];
117
+ if (!file) return;
118
+
119
+ // Check file extension
120
+ if (!file.name.endsWith('.pl') && !file.name.endsWith('.qlf')) {
121
+ setUploadStatus({ ok: false, error: "Only .pl and .qlf files allowed" });
122
+ return;
123
+ }
124
+
125
+ setUploadStatus({ ok: false, loading: true });
126
+
127
+ try {
128
+ const result = await uploadKB(file, token);
129
+ setUploadStatus({
130
+ ok: true,
131
+ message: result.message || `Successfully uploaded ${file.name}`,
132
+ });
133
+ setKbMetadata(result.kb_metadata);
134
+
135
+ // Add system message to chat
136
+ setMessages(prev => [
137
+ ...prev,
138
+ {
139
+ role: "assistant",
140
+ text: `✅ **Knowledge Base Updated**\n\nLoaded: ${file.name}\n\nYou can now ask me questions about this knowledge base!`
141
+ }
142
+ ]);
143
+
144
+ // Clear file input
145
+ e.target.value = '';
146
+ } catch (err) {
147
+ setUploadStatus({ ok: false, error: err.message || "Upload failed" });
148
+ }
149
+ }
150
+
151
+ // ============================================================
152
+ // LOGIN SCREEN
153
+ // ============================================================
154
+ if (!token) {
155
+ return (
156
+ <div className="app-root">
157
+ <header className="header">
158
+ <h1>🧠 Prolog AI</h1>
159
+ </header>
160
+
161
+ <main className="main login">
162
+ <form className="login-form" onSubmit={handleLogin}>
163
+ <h2>Sign In</h2>
164
+
165
+ <input
166
+ type="text"
167
+ placeholder="Username"
168
+ value={loginForm.username}
169
+ onChange={(e) =>
170
+ setLoginForm({ ...loginForm, username: e.target.value })
171
+ }
172
+ />
173
+
174
+ <input
175
+ type="password"
176
+ placeholder="Password"
177
+ value={loginForm.password}
178
+ onChange={(e) =>
179
+ setLoginForm({ ...loginForm, password: e.target.value })
180
+ }
181
+ />
182
+
183
+ <button type="submit">Log In</button>
184
+
185
+ {authError && <div className="error">{authError}</div>}
186
+ </form>
187
+ </main>
188
+ </div>
189
+ );
190
+ }
191
+
192
+ // ============================================================
193
+ // CHAT UI
194
+ // ============================================================
195
+ return (
196
+ <div className="app-root">
197
+ <header className="header">
198
+ <h1>🧠 Prolog AI</h1>
199
+
200
+ <div className="header-controls">
201
+ <label className="strict-toggle">
202
+ <input
203
+ type="checkbox"
204
+ checked={strictMode}
205
+ onChange={(e) => setStrictMode(e.target.checked)}
206
+ />{" "}
207
+ Strict Mode
208
+ </label>
209
+
210
+ <button
211
+ className="sidebar-toggle"
212
+ onClick={() => setShowSidebar(!showSidebar)}
213
+ >
214
+ {showSidebar ? "Hide Sidebar" : "Show Sidebar"}
215
+ </button>
216
+
217
+ <button
218
+ className="logout-btn"
219
+ onClick={() => setToken(null)}
220
+ >
221
+ Logout
222
+ </button>
223
+ </div>
224
+ </header>
225
+
226
+ <div className="app-layout">
227
+ <main className="main">
228
+ <div className="messages">
229
+ {messages.map((msg, idx) => (
230
+ <Message key={idx} message={msg} />
231
+ ))}
232
+ </div>
233
+
234
+ {error && <div className="error">{error}</div>}
235
+
236
+ <form className="input-row" onSubmit={handleSend}>
237
+ <textarea
238
+ placeholder="Type your question..."
239
+ value={input}
240
+ onChange={(e) => setInput(e.target.value)}
241
+ disabled={loading}
242
+ rows={3}
243
+ />
244
+ <button type="submit" disabled={loading || !input.trim()}>
245
+ Send
246
+ </button>
247
+ </form>
248
+ </main>
249
+
250
+ {showSidebar && (
251
+ <aside className="sidebar">
252
+ <h3>Upload Knowledge Base</h3>
253
+
254
+ <div className="upload-section">
255
+ <input
256
+ type="file"
257
+ accept=".pl,.qlf"
258
+ onChange={handleFileUpload}
259
+ id="file-input"
260
+ />
261
+ <label htmlFor="file-input" className="upload-btn">
262
+ Choose .pl or .qlf file
263
+ </label>
264
+
265
+ {uploadStatus && (
266
+ <div className={`upload-status ${uploadStatus.ok ? 'success' : 'error'}`}>
267
+ {uploadStatus.loading ? (
268
+ <p>Uploading...</p>
269
+ ) : uploadStatus.ok ? (
270
+ <p>✅ {uploadStatus.message}</p>
271
+ ) : (
272
+ <p>❌ {uploadStatus.error}</p>
273
+ )}
274
+ </div>
275
+ )}
276
+ </div>
277
+
278
+ {kbMetadata && (
279
+ <div className="kb-metadata">
280
+ <h4>Current KB</h4>
281
+ <pre>{kbMetadata}</pre>
282
+ </div>
283
+ )}
284
+ </aside>
285
+ )}
286
+ </div>
287
+ </div>
288
+ );
289
+ }
290
+
291
+ // ============================================================
292
+ // MESSAGE COMPONENT
293
+ // ============================================================
294
+ function Message({ message }) {
295
+ const isUser = message.role === "user";
296
+
297
+ return (
298
+ <div className={`message ${isUser ? "user" : "assistant"}`}>
299
+ <div className="bubble">
300
+ <ReactMarkdown
301
+ remarkPlugins={[remarkMath, remarkGfm]}
302
+ rehypePlugins={[rehypeKatex]}
303
+ skipHtml={false}
304
+ breaks={true}
305
+ >
306
+ {message.text || ""}
307
+ </ReactMarkdown>
308
+ </div>
309
+ </div>
310
+ );
311
+ }
312
+
313
+ export default App;
src/api.js ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // src/api.js - API client for Prolog AI
2
+
3
+ export const API_BASE = process.env.REACT_APP_API_BASE || "http://localhost:7861";
4
+
5
+ export async function login(username, password) {
6
+ const res = await fetch(`${API_BASE}/login`, {
7
+ method: "POST",
8
+ headers: { "Content-Type": "application/json" },
9
+ body: JSON.stringify({ username, password }),
10
+ });
11
+
12
+ let json;
13
+ const text = await res.text().catch(() => "");
14
+
15
+ try {
16
+ json = text ? JSON.parse(text) : {};
17
+ } catch {
18
+ json = {};
19
+ }
20
+
21
+ if (!res.ok) {
22
+ const msg =
23
+ (json && json.error) ||
24
+ text ||
25
+ `Login failed with status ${res.status}`;
26
+ throw new Error(msg);
27
+ }
28
+
29
+ if (!json.ok || !json.token) {
30
+ throw new Error(json.error || "Login failed");
31
+ }
32
+
33
+ return json; // { ok, token }
34
+ }
35
+
36
+ export async function streamChat(message, strictMode, history, token, onToken) {
37
+ const res = await fetch(`${API_BASE}/chat/stream`, {
38
+ method: "POST",
39
+ headers: {
40
+ "Content-Type": "application/json",
41
+ "Authorization": `Bearer ${token}`
42
+ },
43
+ body: JSON.stringify({
44
+ message,
45
+ strict_mode: strictMode,
46
+ history
47
+ }),
48
+ });
49
+
50
+ if (!res.body) {
51
+ throw new Error("Streaming not supported by server");
52
+ }
53
+
54
+ const reader = res.body.getReader();
55
+ const decoder = new TextDecoder();
56
+
57
+ while (true) {
58
+ const { value, done } = await reader.read();
59
+ if (done) break;
60
+
61
+ const chunk = decoder.decode(value);
62
+ onToken(chunk);
63
+ }
64
+ }
65
+
66
+ export async function uploadKB(file, token) {
67
+ const formData = new FormData();
68
+ formData.append('file', file);
69
+
70
+ const res = await fetch(`${API_BASE}/kb/upload`, {
71
+ method: "POST",
72
+ headers: {
73
+ "Authorization": `Bearer ${token}`
74
+ },
75
+ body: formData,
76
+ });
77
+
78
+ if (!res.ok) {
79
+ const text = await res.text().catch(() => "");
80
+ throw new Error(`HTTP ${res.status}: ${text || "Upload failed"}`);
81
+ }
82
+
83
+ const json = await res.json();
84
+ if (!json.ok) {
85
+ throw new Error(json.error || "Upload failed");
86
+ }
87
+
88
+ return json;
89
+ }
90
+
91
+ export async function getKBMetadata(token) {
92
+ const res = await fetch(`${API_BASE}/kb/metadata`, {
93
+ method: "GET",
94
+ headers: {
95
+ "Authorization": `Bearer ${token}`
96
+ },
97
+ });
98
+
99
+ if (!res.ok) {
100
+ const text = await res.text().catch(() => "");
101
+ throw new Error(`HTTP ${res.status}: ${text || "Request failed"}`);
102
+ }
103
+
104
+ const json = await res.json();
105
+ if (!json.ok) {
106
+ throw new Error(json.error || "Request failed");
107
+ }
108
+
109
+ return json;
110
+ }
111
+
112
+ export async function queryProlog(query, token, maxSolutions = 10) {
113
+ const res = await fetch(`${API_BASE}/prolog/query`, {
114
+ method: "POST",
115
+ headers: {
116
+ "Content-Type": "application/json",
117
+ "Authorization": `Bearer ${token}`
118
+ },
119
+ body: JSON.stringify({
120
+ query,
121
+ max_solutions: maxSolutions
122
+ }),
123
+ });
124
+
125
+ if (!res.ok) {
126
+ const text = await res.text().catch(() => "");
127
+ throw new Error(`HTTP ${res.status}: ${text || "Query failed"}`);
128
+ }
129
+
130
+ const json = await res.json();
131
+ if (!json.ok) {
132
+ throw new Error(json.error || "Query failed");
133
+ }
134
+
135
+ return json;
136
+ }
src/index.css ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ margin: 0;
3
+ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4
+ 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5
+ sans-serif;
6
+ -webkit-font-smoothing: antialiased;
7
+ -moz-osx-font-smoothing: grayscale;
8
+ }
9
+
10
+ code {
11
+ font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12
+ monospace;
13
+ }
14
+
15
+ #root {
16
+ height: 100vh;
17
+ }
src/index.js ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import React from 'react';
2
+ import ReactDOM from 'react-dom/client';
3
+ import './index.css';
4
+ import App from './App';
5
+
6
+ const root = ReactDOM.createRoot(document.getElementById('root'));
7
+ root.render(
8
+ <React.StrictMode>
9
+ <App />
10
+ </React.StrictMode>
11
+ );