Files
instantadpay/qa/chatbot-sync.mjs
T
martbost 1e040f21ff 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>
2026-10-20 05:37:09 -05:00

30 lines
2.4 KiB
JavaScript

// Chatbot knowledge guard (Marty, 2026-09-16: "ensure the chatbot is completely current, and keep it that way").
// Compares chatbot.js KNOWLEDGE_DATE with the newest public release note. If a note is newer, the
// chatbot was not brought up to date with that change: fix chatbot.js (canned + prompt), bump the date.
// Since the knowledge base (2026-10-20, kb.js) the assistant also answers from Admin > Knowledge base, so a
// KB change made after a release counts too: the guard takes the later of KNOWLEDGE_DATE and the site's
// newest KB edit by a person (/api/chat/kb-stamp, a Central-time day; seed refreshes from the code do not count). Either way a release must be followed by a
// knowledge update, in the code or in the admin pane.
// node qa/chatbot-sync.mjs (checks the live site's /api/releases)
// LOCAL=http://127.0.0.1:8796 node qa/chatbot-sync.mjs
import fs from 'node:fs';
const B = process.env.LOCAL || 'https://instantadpay.com';
const src = fs.readFileSync(new URL('../chatbot.js', import.meta.url), 'utf8');
const m = /KNOWLEDGE_DATE = '(\d{4}-\d{2}-\d{2})'/.exec(src);
if (!m) { console.log('[bug] chatbot.js has no KNOWLEDGE_DATE constant'); process.exit(1); }
let known = m[1], via = 'chatbot.js KNOWLEDGE_DATE';
try {
const st = await (await fetch(B + '/api/chat/kb-stamp')).json();
if (st && /^\d{4}-\d{2}-\d{2}$/.test(st.updated || '') && st.updated > known) { known = st.updated; via = 'knowledge base (' + st.count + ' entries)'; }
} catch (e) { console.log('[warn] no knowledge-base stamp from ' + B + ' (' + e.message + '), using KNOWLEDGE_DATE only'); }
let notes = [];
try { const r = await (await fetch(B + '/api/releases')).json(); notes = r.notes || []; } catch (e) { console.log('[warn] could not read ' + B + '/api/releases: ' + e.message); process.exit(0); }
const newer = notes.filter(n => (n.date || '') > known);
if (newer.length) {
console.log('[bug] chatbot knowledge date ' + known + ' is older than ' + newer.length + ' release note(s):');
for (const n of newer) console.log(' ' + n.date + ' ' + n.title);
console.log(' -> update chatbot.js (canned answers + systemPrompt) or the entries in Admin > Knowledge base for each, then bump KNOWLEDGE_DATE.');
process.exit(1);
}
console.log('chatbot knowledge date ' + known + ' (' + via + ') covers all ' + notes.length + ' release notes (newest ' + (notes[0] ? notes[0].date : 'none') + ')');