Chat knowledge base: the assistant answers from maintained entries, and support replies become entries
- kb.js: entries { id, title, questions, answer, tags, pattern, handoff, order, source, updated } in MySQL table kb_entries (created on boot) or DATA_DIR/kb.json without DATABASE_URL. BM25 over title, question variants and tags (answer at a low weight) with word bigrams; the top hits (within 30% of the best, score 3+) go into the system prompt as KNOWLEDGE BASE, and when nothing fits the prompt says to answer "I'm not sure, ask support".
- One source: chatbot.js CANNED seeds the KB (ids c-*, its regex becomes the entry's instant-answer pattern, same first-match order), kb-seed.js seeds the home FAQ, Platform Facts, wallets FAQ and the Coinbase support reply (f-*). The fast path now serves the KB entry, so an admin edit changes the instant answer; the in-code CANNED only answers if the KB store fails. Unedited seeds follow code changes, edited ones are left alone, deleted seeds stay deleted.
- Admin > Knowledge base: list, search, filter by source, add, edit, delete, and Try a question (instant answer or what the AI is given). Support tickets: "Save as KB entry" under each sent reply opens a draft with the member's question and the reply (greeting and sign-off trimmed). Em dashes in admin input become commas.
- chatbot.js: OPENROUTER_URL override for tests; solo-ads answer em dash removed; KNOWLEDGE_DATE 2026-10-20.
- qa/chatbot-sync.mjs: takes the later of KNOWLEDGE_DATE and /api/chat/kb-stamp (newest KB edit by a person, Central day; seed refreshes do not count).
- QA: qa/kb-retrieval.cjs (13 sample questions rank the right entry first, off-topic returns nothing), qa/kb-chat.mjs (local copy + mock OpenRouter: Coinbase answered from the KB, fast path without an AI call, admin CRUD, restart keeps edits), bash qa/run.sh kb; walk covers admin#kb and Save as KB entry.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -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');
|
||||
@@ -2162,6 +2163,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
|
||||
@@ -4706,6 +4710,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' });
|
||||
|
||||
Reference in New Issue
Block a user