-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
356 lines (311 loc) · 14.2 KB
/
Copy pathui.go
File metadata and controls
356 lines (311 loc) · 14.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import "net/http"
func uiHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte(uiHTML))
}
const uiHTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SingleLeaf</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #0a0a0a; color: #e0e0e0; min-height: 100vh; }
.header { text-align: center; padding: 40px 20px 20px; }
.header img { width: 80px; margin-bottom: 12px; }
.header h1 { font-size: 28px; font-weight: 600; color: #fff; }
.header p { color: #888; font-size: 14px; margin-top: 4px; }
.search-box { max-width: 680px; margin: 20px auto; padding: 0 20px; display: flex; gap: 8px; }
.search-box input {
flex: 1; padding: 14px 18px; font-size: 16px; border: 1px solid #333; border-radius: 10px;
background: #141414; color: #fff; outline: none; transition: border-color 0.2s;
}
.search-box input:focus { border-color: #4a9; }
.search-box button {
padding: 14px 28px; font-size: 15px; font-weight: 600; border: none; border-radius: 10px;
background: #2d8a5e; color: #fff; cursor: pointer; transition: background 0.2s;
}
.search-box button:hover { background: #36a06e; }
.search-box button:disabled { background: #333; cursor: not-allowed; }
.tabs { max-width: 680px; margin: 10px auto; padding: 0 20px; display: flex; gap: 4px; }
.tab {
padding: 8px 16px; font-size: 13px; border: 1px solid #333; border-radius: 8px;
background: transparent; color: #888; cursor: pointer; transition: all 0.2s;
}
.tab.active { background: #1a1a1a; color: #4a9; border-color: #4a9; }
.status { max-width: 680px; margin: 16px auto; padding: 0 20px; }
.status-bar {
background: #141414; border: 1px solid #222; border-radius: 10px; padding: 14px 18px;
font-size: 13px; color: #888; display: none;
}
.status-bar.visible { display: block; }
.status-bar .metric { color: #4a9; font-weight: 600; }
.status-bar .separator { color: #333; margin: 0 8px; }
.loader { display: none; text-align: center; padding: 40px; }
.loader.visible { display: block; }
.loader .spinner {
width: 36px; height: 36px; border: 3px solid #222; border-top-color: #4a9;
border-radius: 50%; animation: spin 0.8s linear infinite; margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loader p { color: #666; font-size: 14px; }
.results { max-width: 680px; margin: 0 auto; padding: 0 20px 40px; }
.results-section-label {
font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px;
color: #555; margin: 24px 0 12px; padding-bottom: 8px; border-bottom: 1px solid #1a1a1a;
}
.results-section-label .count { color: #4a9; }
.result-card {
background: #141414; border: 1px solid #222; border-radius: 12px;
padding: 18px; margin-bottom: 12px; transition: border-color 0.2s;
}
.result-card:hover { border-color: #333; }
.result-card.enriched { border-left: 3px solid #2d8a5e; }
.result-card.plain { opacity: 0.75; }
.result-card .rank { font-size: 11px; color: #555; font-weight: 600; margin-bottom: 6px; }
.result-card .title { font-size: 16px; font-weight: 600; margin-bottom: 4px; }
.result-card .title a { color: #5cb88a; text-decoration: none; }
.result-card .title a:hover { text-decoration: underline; }
.result-card .url { font-size: 12px; color: #666; margin-bottom: 8px; word-break: break-all; }
.result-card .snippet { font-size: 14px; color: #aaa; line-height: 1.5; margin-bottom: 10px; }
.result-card .meta { display: flex; flex-wrap: wrap; gap: 8px; font-size: 11px; }
.result-card .tag {
padding: 3px 8px; border-radius: 4px; background: #1a1a1a; color: #888; border: 1px solid #272727;
}
.result-card .tag.source-fetch { color: #4a9; border-color: #2a5a3a; }
.result-card .tag.source-cdp { color: #a94; border-color: #5a4a2a; }
.result-card .tag.error { color: #c55; border-color: #5a2a2a; }
.extracted-info {
margin-top: 10px; padding: 12px; background: #0e1a14; border: 1px solid #1a3a2a;
border-radius: 8px; font-size: 13px; line-height: 1.7;
}
.extracted-info .info-row { display: flex; gap: 8px; margin-bottom: 4px; }
.extracted-info .info-label { color: #4a9; font-weight: 600; min-width: 60px; flex-shrink: 0; }
.extracted-info .info-value { color: #ccc; word-break: break-word; }
.extracted-info .info-value a { color: #5cb88a; text-decoration: none; }
.extracted-info .info-value a:hover { text-decoration: underline; }
.extracted-info .about { color: #999; font-style: italic; margin-top: 6px; }
.synthesis-card {
background: #0e1a14; border: 1px solid #2d8a5e; border-radius: 12px;
padding: 20px; margin-bottom: 20px;
}
.synthesis-card .synth-label {
font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: 1px;
color: #4a9; margin-bottom: 10px;
}
.synthesis-card .synth-body { font-size: 14px; color: #ccc; line-height: 1.7; white-space: pre-wrap; word-break: break-word; }
.synthesis-card .synth-meta { margin-top: 12px; font-size: 11px; color: #555; }
.page-text-toggle {
margin-top: 10px; font-size: 12px; color: #4a9; cursor: pointer;
background: none; border: none; padding: 0;
}
.page-text-toggle:hover { text-decoration: underline; }
.page-text-content {
display: none; margin-top: 8px; padding: 12px; background: #0e0e0e;
border-radius: 8px; font-size: 13px; color: #999; line-height: 1.6;
max-height: 300px; overflow-y: auto; white-space: pre-wrap; word-break: break-word;
}
.page-text-content.visible { display: block; }
.empty { text-align: center; padding: 60px 20px; color: #555; }
.empty p { font-size: 15px; }
</style>
</head>
<body>
<div class="header">
<h1>SingleLeaf</h1>
<p>Privacy-first search with Tor-routed queries</p>
</div>
<div class="search-box">
<input type="text" id="query" placeholder="Search anything..." autofocus>
<button id="searchBtn" onclick="doSearch()">Search</button>
</div>
<div class="tabs">
<button class="tab active" data-mode="deep-search" onclick="setMode(this)">Deep Search</button>
<button class="tab" data-mode="search" onclick="setMode(this)">Quick Search</button>
</div>
<div class="status">
<div class="status-bar" id="statusBar"></div>
</div>
<div class="loader" id="loader">
<div class="spinner"></div>
<p id="loaderText">Searching through Tor...</p>
</div>
<div class="results" id="results"></div>
<script>
let mode = 'deep-search';
function setMode(el) {
document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
el.classList.add('active');
mode = el.dataset.mode;
}
document.getElementById('query').addEventListener('keydown', e => {
if (e.key === 'Enter') doSearch();
});
async function doSearch() {
const q = document.getElementById('query').value.trim();
if (!q) return;
const btn = document.getElementById('searchBtn');
const loader = document.getElementById('loader');
const results = document.getElementById('results');
const statusBar = document.getElementById('statusBar');
const loaderText = document.getElementById('loaderText');
btn.disabled = true;
loader.classList.add('visible');
statusBar.classList.remove('visible');
results.innerHTML = '';
loaderText.textContent = mode === 'deep-search'
? 'Deep searching through Tor + rendering pages...'
: 'Searching through Tor...';
const start = performance.now();
try {
const url = '/' + mode + '?q=' + encodeURIComponent(q);
const resp = await fetch(url);
const data = await resp.json();
const clientMs = Math.round(performance.now() - start);
if (data.error) {
results.innerHTML = '<div class="empty"><p>' + escHtml(data.error) + '</p></div>';
loader.classList.remove('visible');
btn.disabled = false;
return;
}
const isDeep = mode === 'deep-search';
const totalResults = isDeep ? data.total_results : data.number_of_results;
const items = data.results || [];
let statusHtml = '<span class="metric">' + totalResults + '</span> results';
statusHtml += '<span class="separator">|</span>';
statusHtml += '<span class="metric">' + data.elapsed_ms + 'ms</span> server';
statusHtml += '<span class="separator">|</span>';
statusHtml += '<span class="metric">' + clientMs + 'ms</span> total';
if (isDeep) {
statusHtml += '<span class="separator">|</span>';
statusHtml += '<span class="metric">' + data.rendered_results + '/' + items.length + '</span> pages rendered';
}
statusBar.innerHTML = statusHtml;
statusBar.classList.add('visible');
if (items.length === 0) {
results.innerHTML = '<div class="empty"><p>No results found. Tor circuits may be slow — try again.</p></div>';
} else if (isDeep) {
const enriched = items.filter(r => r.page_text && r.page_text.length > 0);
const plain = items.filter(r => !r.page_text || r.page_text.length === 0);
let html = '';
if (data.synthesis && data.synthesis.answer) {
html += renderSynthesis(data.synthesis);
}
if (enriched.length > 0) {
html += '<div class="results-section-label"><span class="count">' + enriched.length + '</span> enriched results</div>';
html += enriched.map((r, i) => renderCard(r, i, true, true)).join('');
}
if (plain.length > 0) {
html += '<div class="results-section-label"><span class="count">' + plain.length + '</span> additional results</div>';
html += plain.map((r, i) => renderCard(r, enriched.length + i, true, false)).join('');
}
results.innerHTML = html;
} else {
results.innerHTML = items.map((r, i) => renderCard(r, i, false, false)).join('');
}
} catch (err) {
results.innerHTML = '<div class="empty"><p>Request failed: ' + escHtml(err.message) + '</p></div>';
}
loader.classList.remove('visible');
btn.disabled = false;
}
function renderCard(r, i, isDeep, isEnriched) {
const hasText = r.page_text && r.page_text.length > 0;
const snippet = r.content || (hasText ? r.page_text.substring(0, 250) : '');
const textLen = r.page_text ? r.page_text.length : 0;
let tags = '';
if (r.engines && r.engines.length) {
tags += r.engines.map(e => '<span class="tag">' + escHtml(e) + '</span>').join('');
}
if (r.score) {
tags += '<span class="tag">score: ' + r.score.toFixed(1) + '</span>';
}
if (r.category) {
tags += '<span class="tag">' + escHtml(r.category) + '</span>';
}
if (isDeep) {
if (r.render_source) {
const cls = r.render_source === 'fetch' ? 'source-fetch' : 'source-cdp';
tags += '<span class="tag ' + cls + '">' + r.render_source + ' ' + r.render_time_ms + 'ms</span>';
}
if (hasText) {
tags += '<span class="tag source-fetch">' + formatBytes(textLen) + ' extracted</span>';
}
if (r.render_error) {
tags += '<span class="tag error">' + escHtml(r.render_error.substring(0, 40)) + '</span>';
}
if (r.blocked) {
tags += '<span class="tag error">blocked</span>';
}
}
let extractedSection = '';
if (r.extracted) {
const ex = r.extracted;
let rows = '';
if (ex.phone && ex.phone.length) {
const phones = ex.phone.map(p => '<a href="tel:' + escAttr(p.replace(/\s/g,'')) + '">' + escHtml(p) + '</a>').join(', ');
rows += '<div class="info-row"><span class="info-label">Phone</span><span class="info-value">' + phones + '</span></div>';
}
if (ex.email && ex.email.length) {
const emails = ex.email.map(e => '<a href="mailto:' + escAttr(e) + '">' + escHtml(e) + '</a>').join(', ');
rows += '<div class="info-row"><span class="info-label">Email</span><span class="info-value">' + emails + '</span></div>';
}
if (ex.address) {
rows += '<div class="info-row"><span class="info-label">Address</span><span class="info-value">' + escHtml(ex.address) + '</span></div>';
}
if (ex.gst) {
rows += '<div class="info-row"><span class="info-label">GST</span><span class="info-value">' + escHtml(ex.gst) + '</span></div>';
}
if (ex.about) {
rows += '<div class="about">' + escHtml(ex.about) + '</div>';
}
if (rows) {
extractedSection = '<div class="extracted-info">' + rows + '</div>';
}
}
let pageTextSection = '';
if (hasText) {
const id = 'pt-' + i;
pageTextSection = '<button class="page-text-toggle" onclick="toggleText(\'' + id + '\', this)">Show raw page text (' + formatBytes(textLen) + ')</button>'
+ '<div class="page-text-content" id="' + id + '">' + escHtml(r.page_text.substring(0, 5000)) + (textLen > 5000 ? '\n\n... [truncated, ' + formatBytes(textLen) + ' total]' : '') + '</div>';
}
const cardClass = isDeep ? (isEnriched ? 'result-card enriched' : 'result-card plain') : 'result-card';
return '<div class="' + cardClass + '">'
+ '<div class="rank">#' + (i + 1) + '</div>'
+ '<div class="title"><a href="' + escAttr(r.url) + '" target="_blank" rel="noopener">' + escHtml(r.title || r.url) + '</a></div>'
+ '<div class="url">' + escHtml(r.url) + '</div>'
+ (snippet ? '<div class="snippet">' + escHtml(snippet.substring(0, 300)) + '</div>' : '')
+ '<div class="meta">' + tags + '</div>'
+ extractedSection
+ pageTextSection
+ '</div>';
}
function renderSynthesis(synth) {
return '<div class="synthesis-card">'
+ '<div class="synth-label">AI Answer</div>'
+ '<div class="synth-body">' + escHtml(synth.answer) + '</div>'
+ '<div class="synth-meta">' + escHtml(synth.model) + ' · ' + synth.elapsed_ms + 'ms · ' + synth.sources.length + ' sources</div>'
+ '</div>';
}
function toggleText(id, btn) {
const el = document.getElementById(id);
const visible = el.classList.toggle('visible');
btn.textContent = visible ? 'Hide extracted text' : btn.textContent.replace('Hide', 'Show');
}
function formatBytes(chars) {
if (chars < 1000) return chars + ' chars';
return (chars / 1000).toFixed(1) + 'K chars';
}
function escHtml(s) {
if (!s) return '';
return s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
}
function escAttr(s) {
if (!s) return '';
return s.replace(/&/g,'&').replace(/"/g,'"').replace(/</g,'<').replace(/>/g,'>');
}
</script>
</body>
</html>`