fraudTech commited on
Commit
8b4705e
·
verified ·
1 Parent(s): 4cb037f

Update src/App.js

Browse files
Files changed (1) hide show
  1. src/App.js +84 -213
src/App.js CHANGED
@@ -1,236 +1,107 @@
1
- // src/App.js
2
  import React, { useState } from "react";
3
-
4
- const API_URL = "https://deepthoughtai-prolog-backend.hf.space/prolog/query";
5
-
6
-
7
 
8
  function App() {
9
- const [query, setQuery] = useState("member(X, [1,2,3]).");
10
- const [maxSolutions, setMaxSolutions] = useState(10);
 
 
 
11
  const [loading, setLoading] = useState(false);
12
- const [solutions, setSolutions] = useState([]);
13
- const [rawOutput, setRawOutput] = useState("");
14
- const [error, setError] = useState("");
15
- const [history, setHistory] = useState([]);
16
-
17
- const runQuery = async (e) => {
18
- e.preventDefault();
19
- setLoading(true);
20
- setError("");
21
- setSolutions([]);
22
- setRawOutput("");
23
-
24
- try {
25
- const res = await fetch(API_URL, {
26
- method: "POST",
27
- headers: { "Content-Type": "application/json" },
28
- body: JSON.stringify({
29
- query,
30
- max_solutions: Number(maxSolutions) || 10,
31
- }),
32
- });
33
-
34
- if (!res.ok) throw new Error(`HTTP ${res.status}`);
35
 
36
- const data = await res.json();
 
 
37
 
38
- if (!data.ok) throw new Error(data.error || "Prolog query failed");
 
39
 
40
- setSolutions(data.solutions || []);
41
- setRawOutput(data.raw || "");
 
 
42
 
43
- setHistory((prev) => [
44
- {
45
- id: Date.now(),
46
- query,
47
- solutions: data.solutions || [],
48
- raw: data.raw || "",
49
- },
50
- ...prev.slice(0, 19), // keep last 20
51
- ]);
 
 
 
 
 
 
 
 
 
 
52
  } catch (err) {
53
- console.error(err);
54
  setError(err.message || "Unknown error");
55
  } finally {
56
  setLoading(false);
57
  }
58
- };
59
-
60
- const loadFromHistory = (item) => {
61
- setQuery(item.query);
62
- setSolutions(item.solutions);
63
- setRawOutput(item.raw);
64
- };
65
 
66
  return (
67
  <div className="app-root">
68
- <div className="app-bg-gradient" />
69
-
70
- <main className="app-shell">
71
- {/* Header */}
72
- <header className="app-header">
73
- <div>
74
- <h1>SWI-Prolog Playground</h1>
75
- <p>Experiment with Prolog queries and inspect solutions.</p>
76
- </div>
77
- <span className="app-badge">Beta</span>
78
- </header>
79
-
80
- <div className="app-layout">
81
- {/* Left column */}
82
- <section className="app-main-panel">
83
- {/* Query card */}
84
- <form className="card card-query" onSubmit={runQuery}>
85
- <div className="card-header">
86
- <h2>Query</h2>
87
- <span className="card-subtitle">
88
- Enter a Prolog goal and run it on the backend.
89
- </span>
90
- </div>
91
-
92
- <label className="field-label">Prolog goal</label>
93
- <textarea
94
- className="field-textarea"
95
- value={query}
96
- onChange={(e) => setQuery(e.target.value)}
97
- placeholder="member(X, [1,2,3])."
98
- rows={4}
99
- />
100
-
101
- <div className="field-row">
102
- <div className="field-inline">
103
- <label className="field-label">Max solutions</label>
104
- <input
105
- type="number"
106
- min={1}
107
- max={100}
108
- value={maxSolutions}
109
- onChange={(e) => setMaxSolutions(e.target.value)}
110
- className="field-input small"
111
- />
112
- </div>
113
- <div className="spacer" />
114
- <button
115
- type="submit"
116
- disabled={loading || !query.trim()}
117
- className="button-primary"
118
- >
119
- {loading ? "Running…" : "Run query"}
120
- </button>
121
- </div>
122
-
123
- {error && (
124
- <div className="alert alert-error">
125
- <strong>Error:</strong> {error}
126
- </div>
127
- )}
128
- </form>
129
-
130
- {/* Solutions card */}
131
- <section className="card card-results">
132
- <div className="card-header">
133
- <h2>Solutions</h2>
134
- <span className="card-subtitle">
135
- Returned as structured bindings per solution.
136
- </span>
137
- </div>
138
-
139
- <div className="results-body">
140
- {loading && (
141
- <p className="muted">Running query, please wait…</p>
142
- )}
143
-
144
- {!loading && solutions.length === 0 && !rawOutput && !error && (
145
- <p className="muted">
146
- No results yet. Run a query to see solutions here.
147
- </p>
148
- )}
149
-
150
- {!loading && solutions.length > 0 && (
151
- <ol className="solutions-list">
152
- {solutions.map((sol, i) => (
153
- <li key={i} className="solution-item">
154
- <div className="solution-header">
155
- <span>Solution {i + 1}</span>
156
- </div>
157
- <pre className="solution-json">
158
- {JSON.stringify(sol, null, 2)}
159
- </pre>
160
- </li>
161
- ))}
162
- </ol>
163
- )}
164
- </div>
165
-
166
- <div className="raw-output">
167
- <div className="raw-header">
168
- <span>Raw Prolog output</span>
169
- </div>
170
- <div className="raw-body">
171
- {rawOutput ? (
172
- <pre className="raw-pre">{rawOutput}</pre>
173
- ) : (
174
- <p className="muted">
175
- When the backend returns REPL-style output, it will show
176
- up here (e.g. <code>X = 1 ; X = 2 ; X = 3.</code>).
177
- </p>
178
- )}
179
- </div>
180
- </div>
181
- </section>
182
- </section>
183
-
184
- {/* Right column – history */}
185
- <aside className="app-side-panel">
186
- <section className="card card-history">
187
- <div className="card-header">
188
- <h2>History</h2>
189
- <span className="card-subtitle">
190
- Click a past query to reload it.
191
- </span>
192
- </div>
193
-
194
- <div className="history-list">
195
- {history.length === 0 && (
196
- <p className="muted">
197
- Run some queries to see them appear here.
198
- </p>
199
- )}
200
-
201
- {history.map((item) => (
202
- <button
203
- key={item.id}
204
- type="button"
205
- className="history-item"
206
- onClick={() => loadFromHistory(item)}
207
- >
208
- <div className="history-query" title={item.query}>
209
- {item.query}
210
- </div>
211
- <div className="history-meta">
212
- {item.solutions.length} solution
213
- {item.solutions.length === 1 ? "" : "s"}
214
- </div>
215
- </button>
216
- ))}
217
- </div>
218
- </section>
219
-
220
- <section className="card card-footer">
221
- <p className="footer-text">
222
- Backend expected at{" "}
223
- <code className="code-inline">{API_URL}</code>.
224
- <br />
225
- Update <code className="code-inline">API_URL</code> in{" "}
226
- <code className="code-inline">App.js</code> if needed.
227
- </p>
228
- </section>
229
- </aside>
230
  </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231
  </main>
232
  </div>
233
  );
234
  }
235
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
236
  export default App;
 
 
1
  import React, { useState } from "react";
2
+ import { sendChat } from "./api";
3
+ import "./App.css";
 
 
4
 
5
  function App() {
6
+ const [messages, setMessages] = useState([
7
+ { role: "assistant", text: "Hello! Ask me anything." }
8
+ ]);
9
+ const [input, setInput] = useState("");
10
+ const [strictMode, setStrictMode] = useState(true);
11
  const [loading, setLoading] = useState(false);
12
+ const [error, setError] = useState(null);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
+ async function handleSend(e) {
15
+ if (e) e.preventDefault();
16
+ if (!input.trim() || loading) return;
17
 
18
+ const userMsg = { role: "user", text: input.trim() };
19
+ const newMessages = [...messages, userMsg];
20
 
21
+ setMessages(newMessages);
22
+ setInput("");
23
+ setError(null);
24
+ setLoading(true);
25
 
26
+ try {
27
+ const historyForBackend = newMessages.map((m) => ({
28
+ role: m.role,
29
+ content: m.text,
30
+ }));
31
+
32
+ const response = await sendChat(
33
+ userMsg.text,
34
+ strictMode,
35
+ historyForBackend
36
+ );
37
+
38
+ const assistantMsg = {
39
+ role: "assistant",
40
+ text: response.reply,
41
+ debug: response.prolog_query_info
42
+ };
43
+
44
+ setMessages([...newMessages, assistantMsg]);
45
  } catch (err) {
 
46
  setError(err.message || "Unknown error");
47
  } finally {
48
  setLoading(false);
49
  }
50
+ }
 
 
 
 
 
 
51
 
52
  return (
53
  <div className="app-root">
54
+ <header className="header">
55
+ <h1>🧠 Prolog AI Chatbot</h1>
56
+ <label style={{ fontSize: "0.9rem" }}>
57
+ <input
58
+ type="checkbox"
59
+ checked={strictMode}
60
+ onChange={(e) => setStrictMode(e.target.checked)}
61
+ />{" "}
62
+ Strict Mode
63
+ </label>
64
+ </header>
65
+
66
+ <main className="main">
67
+ <div className="messages">
68
+ {messages.map((msg, idx) => (
69
+ <Message key={idx} message={msg} />
70
+ ))}
71
+ {loading && <div className="assistant bubble">Thinking…</div>}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  </div>
73
+
74
+ {error && <div className="error">Error: {error}</div>}
75
+
76
+ <form className="input-row" onSubmit={handleSend}>
77
+ <textarea
78
+ placeholder="Type your question..."
79
+ value={input}
80
+ onChange={(e) => setInput(e.target.value)}
81
+ disabled={loading}
82
+ />
83
+ <button type="submit" disabled={loading || !input.trim()}>
84
+ Send
85
+ </button>
86
+ </form>
87
  </main>
88
  </div>
89
  );
90
  }
91
 
92
+ function Message({ message }) {
93
+ return (
94
+ <div className={`message ${message.role}`}>
95
+ <div className="bubble">{message.text}</div>
96
+
97
+ {message.role === "assistant" && message.debug && (
98
+ <details className="debug">
99
+ <summary>Show Prolog Debug</summary>
100
+ <pre>{JSON.stringify(message.debug, null, 2)}</pre>
101
+ </details>
102
+ )}
103
+ </div>
104
+ );
105
+ }
106
+
107
  export default App;