Merge InstantAdPay knowledge-base chatbot + Save as KB entry on support replies

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-10-20 05:52:06 -05:00
12 changed files with 858 additions and 63 deletions
+24
View File
@@ -24,6 +24,7 @@ const drip = require('./drip');
const spaces = require('./spaces'); // DO Spaces video storage (inert unless DO_SPACES_* set)
let QR = null; try { QR = require('qrcode'); } catch (e) { /* optional */ }
const chatbot = require('./chatbot');
const kb = require('./kb'); // the assistant's knowledge base: Admin > Knowledge base, seeded from chatbot.js CANNED + kb-seed.js (2026-10-20)
const fraud = require('./fraud');
const coach = require('./coach'); // coaching view, nudges, digest, prospects, link stats
const tank = require('./tank');
@@ -2175,6 +2176,9 @@ const server = http.createServer(async (req, res) => {
const r = await chatbot.answer(b.message, b.history);
return json(res, r.error ? 400 : 200, r);
}
if (p === '/api/chat/kb-stamp' && req.method === 'GET') { // qa/chatbot-sync.mjs: the newest knowledge-base change (Central day) and the entry count
return json(res, 200, await kb.stamp(), { 'Cache-Control': 'no-store' });
}
// -- help desk, public side (2026-10-16, tickets.js): /support form, the private no-login ticket link, the chatbot hand-off
if (p === '/api/inbound/email' && req.method === 'POST') { // SendGrid Inbound Parse: secret key in the URL, always 200 once it matches
@@ -4719,6 +4723,26 @@ const server = http.createServer(async (req, res) => {
const cur = siteConfig(); cur.supportTemplates = list; fs.writeFileSync(SITE_FILE, JSON.stringify(cur, null, 2));
return json(res, 200, { ok: true, templates: read() });
}
// -- the assistant's knowledge base (2026-10-20, kb.js): Admin > Knowledge base, and "Save as KB entry" on a ticket reply
if (p === '/api/admin/kb' && (req.method === 'GET' || req.method === 'POST')) {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
if (req.method === 'GET') return json(res, 200, { entries: await kb.list(u.searchParams.get('q') || ''), stamp: await kb.stamp() }, { 'Cache-Control': 'no-store' });
const r = await kb.create(await readBody(req));
return json(res, r.error ? 400 : 200, r);
}
if (p === '/api/admin/kb/test' && req.method === 'POST') { // which entry would answer this question, and what would ride along to the AI
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
const b = await readBody(req);
return json(res, 200, await kb.test(b.question));
}
m = /^\/api\/admin\/kb\/([a-z0-9-]{1,64})$/.exec(p);
if (m) {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });
if (req.method === 'GET') { const e = await kb.get(m[1]); return e ? json(res, 200, { entry: e }) : json(res, 404, { error: 'Entry not found.' }); }
if (req.method === 'PUT') { const r = await kb.update(m[1], await readBody(req)); return json(res, r.error ? (r.error === 'Entry not found.' ? 404 : 400) : 200, r); }
if (req.method === 'DELETE') { const r = await kb.remove(m[1]); return json(res, r.error ? 404 : 200, r); }
return json(res, 405, { error: 'method' });
}
m = /^\/api\/admin\/support\/((?:IAP|LS)-\d{4,9})(\/reply)?$/.exec(p);
if (m) {
if (!isAdmin(req)) return json(res, 401, { error: 'auth' });