fraudTech commited on
Commit
bb2abd9
·
verified ·
1 Parent(s): e110181

Create App.jsx

Browse files
Files changed (1) hide show
  1. src/App.jsx +246 -0
src/App.jsx ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // src/App.jsx
2
+ import React, { useState } from "react";
3
+ import { sendChat } from "./api";
4
+ import "./App.css"; // optional, or remove if you don't use it
5
+
6
+ function App() {
7
+ const [messages, setMessages] = useState([
8
+ {
9
+ role: "assistant",
10
+ text: "Hi, I'm your Prolog + LLM assistant. Ask me something!",
11
+ },
12
+ ]);
13
+ const [input, setInput] = useState("");
14
+ const [strictMode, setStrictMode] = useState(true);
15
+ const [loading, setLoading] = useState(false);
16
+ const [error, setError] = useState(null);
17
+
18
+ async function handleSend(e) {
19
+ if (e) e.preventDefault();
20
+ const trimmed = input.trim();
21
+ if (!trimmed || loading) return;
22
+
23
+ setError(null);
24
+
25
+ const userMsg = { role: "user", text: trimmed };
26
+ const currentMessages = [...messages, userMsg];
27
+
28
+ setMessages(currentMessages);
29
+ setInput("");
30
+ setLoading(true);
31
+
32
+ try {
33
+ // Convert to backend history format
34
+ const historyForBackend = currentMessages.map((m) => ({
35
+ role: m.role === "user" ? "user" : "assistant",
36
+ content: m.text,
37
+ }));
38
+
39
+ const res = await sendChat(trimmed, strictMode, historyForBackend);
40
+
41
+ const assistantMsg = {
42
+ role: "assistant",
43
+ text: res.reply,
44
+ prologInfo: res.prolog_query_info || null,
45
+ };
46
+
47
+ setMessages((prev) => [...prev, assistantMsg]);
48
+ } catch (err) {
49
+ console.error(err);
50
+ setError(err.message || "Something went wrong");
51
+ } finally {
52
+ setLoading(false);
53
+ }
54
+ }
55
+
56
+ return (
57
+ <div className="app-root">
58
+ <header className="app-header">
59
+ <div>
60
+ <h1>🧠 Prolog AI Chatbot</h1>
61
+ <p className="app-subtitle">
62
+ Frontend (React) → Backend (/chat) → Prolog + LLM
63
+ </p>
64
+ </div>
65
+
66
+ <div className="app-controls">
67
+ <label className="switch-label">
68
+ <input
69
+ type="checkbox"
70
+ checked={strictMode}
71
+ onChange={(e) => setStrictMode(e.target.checked)}
72
+ />
73
+ <span>Strict KB mode</span>
74
+ </label>
75
+ </div>
76
+ </header>
77
+
78
+ <main className="app-main">
79
+ <section className="chat-pane">
80
+ <div className="chat-window">
81
+ {messages.map((m, idx) => (
82
+ <ChatMessage key={idx} message={m} />
83
+ ))}
84
+ {loading && (
85
+ <div className="chat-message assistant">
86
+ <div className="bubble typing">
87
+ <span className="dot" />
88
+ <span className="dot" />
89
+ <span className="dot" />
90
+ </div>
91
+ </div>
92
+ )}
93
+ </div>
94
+
95
+ <form className="chat-input-row" onSubmit={handleSend}>
96
+ <textarea
97
+ rows={2}
98
+ placeholder="Ask me anything…"
99
+ value={input}
100
+ onChange={(e) => setInput(e.target.value)}
101
+ disabled={loading}
102
+ />
103
+ <button type="submit" disabled={loading || !input.trim()}>
104
+ {loading ? "Thinking…" : "Send"}
105
+ </button>
106
+ </form>
107
+
108
+ {error && <div className="error-banner">Error: {error}</div>}
109
+ </section>
110
+
111
+ <section className="info-pane">
112
+ <h2>About</h2>
113
+ <p>
114
+ This UI sends your message to{" "}
115
+ <code>/chat</code> on the backend Space. The backend:
116
+ </p>
117
+ <ol>
118
+ <li>Queries Prolog for relevant facts</li>
119
+ <li>Builds a system prompt (strict or normal)</li>
120
+ <li>Calls the LLM once (no streaming)</li>
121
+ <li>Returns the answer + Prolog query metadata</li>
122
+ </ol>
123
+ <p>
124
+ Click{" "}
125
+ <span className="inline-tag">View Prolog info</span> on a reply to
126
+ inspect the query details from <code>prolog_query_info</code>.
127
+ </p>
128
+ </section>
129
+ </main>
130
+ </div>
131
+ );
132
+ }
133
+
134
+ // -----------------
135
+ // Message component
136
+ // -----------------
137
+ function ChatMessage({ message }) {
138
+ const isUser = message.role === "user";
139
+ const [showDebug, setShowDebug] = useState(false);
140
+
141
+ return (
142
+ <div className={`chat-message ${isUser ? "user" : "assistant"}`}>
143
+ <div className="avatar">{isUser ? "🧑" : "🤖"}</div>
144
+ <div className="message-content">
145
+ <div className="bubble">
146
+ <MarkdownText text={message.text} />
147
+ </div>
148
+
149
+ {!isUser && message.prologInfo && (
150
+ <button
151
+ type="button"
152
+ className="debug-toggle"
153
+ onClick={() => setShowDebug((v) => !v)}
154
+ >
155
+ {showDebug ? "Hide Prolog info" : "View Prolog info"}
156
+ </button>
157
+ )}
158
+
159
+ {!isUser && showDebug && message.prologInfo && (
160
+ <div className="debug-panel">
161
+ <DebugPrologInfo info={message.prologInfo} />
162
+ </div>
163
+ )}
164
+ </div>
165
+ </div>
166
+ );
167
+ }
168
+
169
+ // ---------------------
170
+ // Simple Markdown-ish render
171
+ // ---------------------
172
+ function MarkdownText({ text }) {
173
+ // Very small markdown: **bold** and line breaks
174
+ const parts = text.split(/\n/g);
175
+ return (
176
+ <>
177
+ {parts.map((line, i) => (
178
+ <p key={i}>
179
+ {line.split(/(\*\*[^*]+\*\*)/g).map((chunk, j) => {
180
+ if (chunk.startsWith("**") && chunk.endsWith("**")) {
181
+ return (
182
+ <strong key={j}>{chunk.slice(2, chunk.length - 2)}</strong>
183
+ );
184
+ }
185
+ return <span key={j}>{chunk}</span>;
186
+ })}
187
+ </p>
188
+ ))}
189
+ </>
190
+ );
191
+ }
192
+
193
+ // ---------------------
194
+ // Prolog debug viewer
195
+ // ---------------------
196
+ function DebugPrologInfo({ info }) {
197
+ const {
198
+ total_results,
199
+ predicates_queried = [],
200
+ queries_executed = [],
201
+ llm_analysis,
202
+ } = info;
203
+
204
+ return (
205
+ <div className="debug-content">
206
+ <div className="debug-section">
207
+ <h4>Summary</h4>
208
+ <p>
209
+ <strong>Total results:</strong> {total_results ?? 0}
210
+ </p>
211
+ <p>
212
+ <strong>Predicates queried:</strong>{" "}
213
+ {predicates_queried.length > 0
214
+ ? predicates_queried.join(", ")
215
+ : "None"}
216
+ </p>
217
+ </div>
218
+
219
+ {queries_executed.length > 0 && (
220
+ <div className="debug-section">
221
+ <h4>Queries executed</h4>
222
+ <ul>
223
+ {queries_executed.slice(0, 10).map((q, i) => (
224
+ <li key={i}>
225
+ <code>{q.query}</code> — {q.results_count} result
226
+ {q.results_count === 1 ? "" : "s"}
227
+ </li>
228
+ ))}
229
+ </ul>
230
+ {queries_executed.length > 10 && (
231
+ <p>… and {queries_executed.length - 10} more</p>
232
+ )}
233
+ </div>
234
+ )}
235
+
236
+ {llm_analysis && (
237
+ <div className="debug-section">
238
+ <h4>LLM-generated queries (raw)</h4>
239
+ <pre className="llm-analysis">{llm_analysis}</pre>
240
+ </div>
241
+ )}
242
+ </div>
243
+ );
244
+ }
245
+
246
+ export default App;