Site assistant (Marty 2026-11-15): chat widget in the Stack POL look on every page, CANNED + FACTS from this repo (the on-page FAQ, programs.js, the 5-minute indexer, opt-in member pages, holds nothing, no income promise), retrieval over the shared network knowledge base on the hub (netkb.js as hub site stackpol; another site named = its answer, with its tracked LinkSpin link), reasoning off, account problems hand off to network support. Static server now serves .js/.css; Dockerfile copies the chat modules. chatbot.test 125 pass

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-15 13:11:04 -06:00
parent 3a6ac0a81e
commit 7446df2f4f
12 changed files with 916 additions and 3 deletions
+8 -1
View File
@@ -157,7 +157,7 @@ function sitemap() {
}
const robots = () => 'User-agent: *\nAllow: /\nDisallow: /api/\n\nSitemap: ' + SITE + '/sitemap.xml\n';
const TYPES = { '.jpg': 'image/jpeg', '.png': 'image/png', '.svg': 'image/svg+xml', '.mp4': 'video/mp4', '.webmanifest': 'application/manifest+json', '.ico': 'image/x-icon', '.txt': 'text/plain; charset=utf-8', '.xml': 'application/xml' };
const TYPES = { '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8', '.jpg': 'image/jpeg', '.png': 'image/png', '.svg': 'image/svg+xml', '.mp4': 'video/mp4', '.webmanifest': 'application/manifest+json', '.ico': 'image/x-icon', '.txt': 'text/plain; charset=utf-8', '.xml': 'application/xml' };
function send(req, res, code, type, body, cache) {
const h = { 'Content-Type': type, 'Cache-Control': cache || 'public, max-age=60', 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'strict-origin-when-cross-origin' };
if (/gzip/.test(req.headers['accept-encoding'] || '') && /text|xml|json|javascript/.test(type) && body.length > 1024) { h['Content-Encoding'] = 'gzip'; h.Vary = 'Accept-Encoding'; body = zlib.gzipSync(body); }
@@ -178,6 +178,10 @@ function staticFile(req, res, p) {
return true;
}
// the site assistant (2026-11-15): chatcore.js + kb.js + the network knowledge base (netkb.js); no admin editing on Stack POL
const chatbot = require('./chatbot');
const chatJson = (res, code, obj, extra) => { res.writeHead(code, Object.assign({ 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff' }, extra || {})); res.end(JSON.stringify(obj)); };
const chatBody = req => new Promise(resolve => { const ch = []; let n = 0; req.on('data', c => { n += c.length; if (n <= 32768) ch.push(c); }); req.on('end', () => { try { resolve(n && n <= 32768 ? JSON.parse(Buffer.concat(ch).toString('utf8')) : {}); } catch (e) { resolve({}); } }); req.on('error', () => resolve({})); });
const server = http.createServer((req, res) => {
let u; try { u = new URL(req.url, SITE); } catch (e) { res.writeHead(400); return res.end(); }
const p = decodeURIComponent(u.pathname);
@@ -190,6 +194,8 @@ const server = http.createServer((req, res) => {
req.on('end', () => { try { const j = JSON.parse(Buffer.concat(chunks).toString('utf8')); if (!j || !j.network || !j.programs) throw new Error('shape'); j.at = j.at || Date.now(); IDX = j; fs.writeFile(SNAP, JSON.stringify(j), () => {}); send(req, res, 200, 'application/json', JSON.stringify({ ok: true, at: j.at }), 'no-store'); } catch (e) { send(req, res, 400, 'application/json', JSON.stringify({ error: e.message }), 'no-store'); } });
return;
}
if (p.startsWith('/api/chat') || p.startsWith('/api/admin/kb')) { chatbot.handle(req, res, p, u, { json: chatJson, readBody: chatBody, isAdmin: () => false })
.then(done => { if (!done) chatJson(res, 404, { error: 'Not found' }); }).catch(e => { console.error('[stackpol] chat', e.message); if (!res.headersSent) chatJson(res, 500, { error: 'Server error' }); }); return; }
if (req.method !== 'GET' && req.method !== 'HEAD') { res.writeHead(405); return res.end(); }
if (p === '/api/totals') return send(req, res, 200, 'application/json', JSON.stringify({ at: IDX ? IDX.at : null, network: net(), programs: (IDX && IDX.programs) || {} }), 'public, max-age=120');
if (p === '/health') return send(req, res, 200, 'text/plain', 'ok', 'no-store');
@@ -221,4 +227,5 @@ const server = http.createServer((req, res) => {
return html(req, res, 404, notFound());
} catch (e) { console.error('[stackpol]', p, e); html(req, res, 500, notFound('Something went wrong on our side. Try again in a minute.'), 'no-store'); }
});
chatbot.init();
server.listen(PORT, () => console.log('Stack POL on :' + PORT + ' | snapshot ' + (IDX ? new Date(IDX.at).toISOString() : 'none yet') + ' | posts ' + blog.listPublished().length + ' | ingest ' + (KEY ? 'on' : 'OFF (no STACK_INGEST_KEY)')));