Add `npm run search` (mnemazine-kb-search.mjs): a recon → fan-out → fan-in swarm over the live vault that writes a Russian "Справка" Markdown report. Recon keyword-scores notes (0 tokens); --deep/MNEMAZINE_DEEP engages a bounded-pool LLM swarm (one agent per shard, untrusted notes fenced), then the orchestrator dedups + synthesizes. Local fallback keeps it 0-token by default, mirroring the rest of the deep pipeline. Self-test included. Wire it into the Telegram Mini App: the vault is Mac-only and the server is on the VPS, so the button only enqueues. Server gains POST /api/search (queue), GET /api/searches (list reports), GET /api/search?name= (read, with path-escape guard). Mini App gets a search card + report list + text viewer (rendered as text — no markdown XSS sink). The Mac poller drains the queue, runs the swarm locally, and rsyncs reports back to the VPS. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
186 lines
8.6 KiB
HTML
186 lines
8.6 KiB
HTML
<!doctype html>
|
||
<html lang="ru">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">
|
||
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'self' https://telegram.org; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' data:; base-uri 'none'">
|
||
<title>Mnemazine</title>
|
||
<script src="https://telegram.org/js/telegram-web-app.js"></script>
|
||
<style>
|
||
:root {
|
||
--bg: var(--tg-theme-bg-color, #fff);
|
||
--text: var(--tg-theme-text-color, #111);
|
||
--hint: var(--tg-theme-hint-color, #888);
|
||
--card: var(--tg-theme-secondary-bg-color, #f4f4f5);
|
||
--accent: var(--tg-theme-button-color, #2481cc);
|
||
--accent-text: var(--tg-theme-button-text-color, #fff);
|
||
--radius: 16px;
|
||
}
|
||
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
|
||
body {
|
||
margin: 0; font: 16px/1.45 -apple-system, system-ui, sans-serif;
|
||
background: var(--bg); color: var(--text);
|
||
padding: 16px calc(16px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
|
||
}
|
||
h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.02em; margin: 4px 0 18px; }
|
||
.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
|
||
.stat { display: flex; align-items: baseline; gap: 8px; }
|
||
.stat b { font-size: 34px; font-weight: 650; letter-spacing: -0.03em; }
|
||
.hint { color: var(--hint); font-size: 13px; }
|
||
ul { list-style: none; margin: 12px 0 0; padding: 0; }
|
||
li { font-size: 14px; padding: 7px 0; border-top: 1px solid color-mix(in srgb, var(--hint) 20%, transparent); display: flex; justify-content: space-between; gap: 10px; }
|
||
li span:last-child { color: var(--hint); flex: none; }
|
||
li:first-child { border-top: none; }
|
||
textarea {
|
||
width: 100%; min-height: 90px; resize: vertical; border: none; border-radius: 12px;
|
||
padding: 12px; font: inherit; background: var(--bg); color: var(--text);
|
||
outline: 1px solid color-mix(in srgb, var(--hint) 25%, transparent);
|
||
}
|
||
button {
|
||
width: 100%; border: none; border-radius: 12px; padding: 14px; margin-top: 10px;
|
||
font: inherit; font-weight: 600; background: var(--accent); color: var(--accent-text);
|
||
cursor: pointer; transition: opacity .15s, transform .05s;
|
||
}
|
||
button:active { transform: scale(.985); }
|
||
button:disabled { opacity: .5; }
|
||
button.ghost { background: transparent; color: var(--accent); outline: 1px solid color-mix(in srgb, var(--accent) 45%, transparent); }
|
||
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 20px; font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; }
|
||
.toast.show { opacity: .94; }
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<h1>Mnemazine</h1>
|
||
|
||
<div class="card">
|
||
<div class="stat"><b id="count">—</b><span class="hint">в буфере</span></div>
|
||
<div class="hint" id="lastrun" style="margin-top:6px">последний прогон: —</div>
|
||
<ul id="recent"></ul>
|
||
</div>
|
||
|
||
<div class="card">
|
||
<textarea id="text" placeholder="Заметка, ссылка, мысль — в inbox Mnemazine…"></textarea>
|
||
<button id="send">В inbox</button>
|
||
</div>
|
||
|
||
<button class="ghost" id="run">Прогнать протокол сейчас</button>
|
||
|
||
<div class="card">
|
||
<div class="hint" style="margin-bottom:8px">🔍 Поиск по базе знаний</div>
|
||
<textarea id="topic" style="min-height:54px" placeholder="Тема — рой агентов соберёт справку…"></textarea>
|
||
<button id="search">Найти</button>
|
||
<ul id="reports"></ul>
|
||
</div>
|
||
|
||
<div class="toast" id="toast"></div>
|
||
|
||
<div class="card" id="viewer" style="display:none">
|
||
<button class="ghost" id="viewerClose" style="margin:0 0 10px">← Назад</button>
|
||
<pre id="viewerBody" style="white-space:pre-wrap;word-break:break-word;font:13px/1.5 ui-monospace,monospace;margin:0"></pre>
|
||
</div>
|
||
|
||
<script>
|
||
const tg = window.Telegram?.WebApp
|
||
tg?.ready(); tg?.expand()
|
||
const initData = tg?.initData || ''
|
||
|
||
const $ = id => document.getElementById(id)
|
||
function toast(msg) { const t = $('toast'); t.textContent = msg; t.classList.add('show'); setTimeout(() => t.classList.remove('show'), 1800) }
|
||
|
||
async function api(path, opts = {}) {
|
||
const res = await fetch('api/' + path, {
|
||
...opts,
|
||
headers: { 'content-type': 'application/json', 'x-telegram-initdata': initData, ...(opts.headers || {}) },
|
||
})
|
||
if (!res.ok) throw new Error((await res.json().catch(() => ({}))).error || res.status)
|
||
return res.json()
|
||
}
|
||
|
||
function fmt(iso) { try { return new Date(iso).toLocaleString('ru-RU', { day: '2-digit', month: '2-digit', hour: '2-digit', minute: '2-digit' }) } catch { return iso } }
|
||
|
||
async function refresh() {
|
||
try {
|
||
const s = await api('status')
|
||
$('count').textContent = s.count
|
||
$('lastrun').textContent = 'последний прогон: ' + (s.lastRun ? fmt(s.lastRun) : 'ещё не было')
|
||
// Build DOM with textContent — file names come from untrusted Telegram
|
||
// uploads; innerHTML here would be a stored-XSS sink.
|
||
const ul = $('recent'); ul.replaceChildren()
|
||
if (!s.recent.length) {
|
||
const li = document.createElement('li'); li.className = 'hint'; li.textContent = 'пусто'; ul.append(li)
|
||
} else for (const r of s.recent) {
|
||
const li = document.createElement('li')
|
||
const name = document.createElement('span'); name.textContent = r.name.replace(/^tg_/, '').replace(/\.md$/, '')
|
||
const at = document.createElement('span'); at.textContent = fmt(r.at)
|
||
li.append(name, at); ul.append(li)
|
||
}
|
||
} catch (e) { toast('Ошибка: ' + e.message) }
|
||
}
|
||
|
||
$('send').onclick = async () => {
|
||
const text = $('text').value.trim()
|
||
if (!text) return toast('Пусто')
|
||
$('send').disabled = true
|
||
try { await api('send', { method: 'POST', body: JSON.stringify({ text }) }); $('text').value = ''; toast('✓ в inbox'); refresh() }
|
||
catch (e) { toast('Ошибка: ' + e.message) }
|
||
finally { $('send').disabled = false }
|
||
}
|
||
|
||
async function doRun() {
|
||
tg?.HapticFeedback?.impactOccurred('medium')
|
||
$('run').disabled = true
|
||
try { await api('run', { method: 'POST' }); toast('✓ протокол поставлен в очередь') }
|
||
catch (e) { toast('Ошибка: ' + e.message) }
|
||
finally { setTimeout(() => $('run').disabled = false, 1500) }
|
||
}
|
||
|
||
$('run').onclick = () => {
|
||
// Confirm before kicking off a real (token-spending) protocol run.
|
||
if (tg?.showConfirm) tg.showConfirm('Запустить прогон протокола сейчас?', ok => { if (ok) doRun() })
|
||
else if (confirm('Запустить прогон протокола сейчас?')) doRun()
|
||
}
|
||
|
||
async function loadReports() {
|
||
try {
|
||
const { reports } = await api('searches')
|
||
const ul = $('reports'); ul.replaceChildren()
|
||
for (const r of reports) {
|
||
const li = document.createElement('li')
|
||
const name = document.createElement('span')
|
||
// textContent — report names are server-generated but stay safe by default.
|
||
name.textContent = r.name.replace(/^search-/, '').replace(/\.md$/, '')
|
||
name.style.cursor = 'pointer'; name.style.color = 'var(--accent)'
|
||
name.onclick = () => openReport(r.name)
|
||
const at = document.createElement('span'); at.textContent = fmt(r.at)
|
||
li.append(name, at); ul.append(li)
|
||
}
|
||
} catch (e) { /* reports dir may be empty before first sync */ }
|
||
}
|
||
|
||
async function openReport(name) {
|
||
try {
|
||
const { markdown } = await api('search?name=' + encodeURIComponent(name))
|
||
$('viewerBody').textContent = markdown // render as text — no markdown sink, no XSS
|
||
$('viewer').style.display = 'block'
|
||
$('viewer').scrollIntoView({ behavior: 'smooth' })
|
||
} catch (e) { toast('Ошибка: ' + e.message) }
|
||
}
|
||
$('viewerClose').onclick = () => { $('viewer').style.display = 'none' }
|
||
|
||
$('search').onclick = async () => {
|
||
const topic = $('topic').value.trim()
|
||
if (!topic) return toast('Пусто')
|
||
tg?.HapticFeedback?.impactOccurred('medium')
|
||
$('search').disabled = true
|
||
try { await api('search', { method: 'POST', body: JSON.stringify({ topic }) }); $('topic').value = ''; toast('✓ поиск в очереди') }
|
||
catch (e) { toast('Ошибка: ' + e.message) }
|
||
finally { setTimeout(() => $('search').disabled = false, 1500) }
|
||
}
|
||
|
||
refresh(); loadReports()
|
||
// Keep status fresh while the app is open and visible.
|
||
setInterval(() => { if (document.visibilityState === 'visible') { refresh(); loadReports() } }, 15000)
|
||
document.addEventListener('visibilitychange', () => { if (document.visibilityState === 'visible') { refresh(); loadReports() } })
|
||
</script>
|
||
</body>
|
||
</html>
|