Site assistant (Marty 2026-11-15): chat widget in the ViralAdBlitz look on every page, CANNED + FACTS from this repo's code (Boost $5/500 views, 10% x 5 + 50% company, 20 places, 30-day delivery, ad limits, verified views), retrieval over the shared network knowledge base on the hub (netkb.js publishes this site's entries and reads the others', home entries boosted), OpenRouter deepseek-v4-flash with reasoning off, account problems hand off to network support. kb.json is a MySQL document. qa/chatbot.test.mjs 195 pass, smoke 198/198

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
martbost
2026-11-15 12:40:33 -06:00
parent 56316c0139
commit d6e36a65bc
24 changed files with 950 additions and 3 deletions
+22
View File
@@ -0,0 +1,22 @@
{
"site": "viraladblitz",
"home": [
{ "q": "What is ViralAdBlitz?", "kb": "about", "must": ["text ad", "five", "Boost"] },
{ "q": "How long can my headline be?", "kb": "free-ad", "must": ["60", "90", "no link shorteners"] },
{ "q": "How much does a Boost cost and what do I get?", "kb": "boost", "must": ["\\$5", "500", "not income"] },
{ "q": "Where does my $5 go when I boost?", "kb": "split", "must": ["10%", "50%", "passed over", "20 places"] },
{ "q": "How do I withdraw my earnings?", "kb": "withdraw", "must": ["nothing to withdraw", "same|inside the buyer"] },
{ "q": "Do my own views count?", "kb": "views", "must": ["never count", "24 hours"] },
{ "q": "Why is someone skipped on my page?", "kb": "spread", "must": ["LEVEL 1 to LEVEL 5", "next person up"] },
{ "q": "Which wallet should I use?", "kb": "wallet", "must": ["SafePal or any other crypto wallet", "POL"] },
{ "q": "Can I get a refund on my Boost?", "kb": "refund", "must": ["final"] },
{ "q": "Is this a pyramid scheme?", "kb": "safe", "must": ["contract", "not income"] }
],
"cross": [
{ "q": "What is Downline Banners?", "site": "bannerline" },
{ "q": "How much is Pro on DownlineHydra?", "site": "downlinehydra" },
{ "q": "How does the 5xYourTeam draft work?", "site": "fivex" },
{ "q": "What is a spin link on LinkSpin?", "site": "linkspin" },
{ "q": "How do InstantAdPay ad packages work?", "site": "iap" }
]
}
+84
View File
@@ -0,0 +1,84 @@
// Site assistant checks (2026-11-15): node qa/chatbot.test.mjs
// No network: a stand-in OpenRouter and a stand-in hub. Checks the instant answers route every example question to its own entry,
// the house rules (no em dashes, no MetaMask, never RM Circle, ad-credits line where credits come up, no income promises),
// the network merge (home boosted, a named site boosted), the support hand-off, and the site's scripted Q&A (qa/chat-qa.json:
// home questions must land on the named entry and contain the expected facts).
// The SAME file ships in every rebuilt network site.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import http from 'node:http';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
const ROOT = path.resolve(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, '$1')), '..');
const DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'chat-'));
process.env.DATA_DIR = DIR; delete process.env.DOCSTORE_URL; process.env.NETKB = '0'; delete process.env.OPENROUTER_API_KEY;
let pass = 0, fail = 0; const bad = [];
const ok = (c, m) => { if (c) pass++; else { fail++; bad.push(m); console.log('FAIL', m); } };
// stand-in OpenRouter: answers with what the system prompt says about the first KB entry, so we can see the prompt
let lastPrompt = '', reply = 'Fine.';
const or = http.createServer((req, res) => { let b = ''; req.on('data', c => { b += c; }); req.on('end', () => {
const j = JSON.parse(b); lastPrompt = j.messages[0].content; ok(j.reasoning && j.reasoning.enabled === false, 'reasoning is switched off');
res.writeHead(200, { 'Content-Type': 'application/json' }); res.end('\n\n' + JSON.stringify({ choices: [{ message: { content: reply } }] })); }); });
await new Promise(r => or.listen(0, r));
process.env.OPENROUTER_URL = 'http://127.0.0.1:' + or.address().port + '/v1/chat/completions';
const cfg = require(path.join(ROOT, 'config.js')); if (cfg.reset) cfg.reset();
const bot = require(path.join(ROOT, 'chatbot.js'));
const core = require(path.join(ROOT, 'chatcore.js'));
const kb = require(path.join(ROOT, 'kb.js'));
const netkb = require(path.join(ROOT, 'netkb.js'));
const dataDir = bot.SITE.dataDir;
await bot.init();
// 1. seeds and the fast path: every example question of a canned entry lands on that entry (first match wins)
const seeds = core.seeds();
ok(seeds.length >= 12, 'at least 12 seeded entries (' + seeds.length + ')');
for (const c of bot.CANNED) {
ok(c.re instanceof RegExp, c.id + ' has a pattern');
for (const q of c.q || []) { const hit = await kb.fastPath(q); ok(hit && hit.id === 'c-' + c.id, 'example "' + q + '" -> ' + c.id + ' (got ' + (hit && hit.id) + ')'); }
}
// 2. house rules in every seeded answer
for (const s of seeds) {
ok(!/[—―]/.test(s.answer + s.title), s.id + ': no em dashes');
ok(!/metamask/i.test(s.answer), s.id + ': never recommends MetaMask');
ok(!/rm ?circle/i.test(s.answer), s.id + ': never RM Circle');
ok(!/(guarantee[ds]? (income|earnings|profit)|you will (earn|make)|passive income|get rich|up to \$?\d[\d,.]* (a|per) (day|week|month))/i.test(s.answer), s.id + ': no income promise');
if (/\bad credits?\b/i.test(s.answer)) ok(/Ad credits buy advertising, not income\./.test(s.answer), s.id + ': ad credits line');
if (/wallet/i.test(s.answer) && /(which|use|recommend)/i.test(s.answer) && /safepal/i.test(s.answer)) ok(/SafePal or any other crypto wallet/i.test(s.answer), s.id + ': wallet wording');
}
// 3. the engine: scrub, support hand-off, the AI path
ok(core.scrub('A — B, use MetaMask or SafePal') === 'A, B, use SafePal', 'scrub: em dash and MetaMask (' + core.scrub('A — B, use MetaMask or SafePal') + ')');
{ const r = await bot.answer('I need to talk to a real person about my account'); ok(r.handoff === true, 'asking for a person hands off'); }
{ const r = await bot.answer(''); ok(!!r.error, 'empty question refused'); }
fs.writeFileSync(path.join(dataDir || DIR, 'openrouter.key'), 'test-key');
reply = 'Sure — try RM Circle'; { const r = await bot.answer('zzqx unrelated words nobody wrote down'); ok(!/rm ?circle/i.test(r.reply) && !/—/.test(r.reply), 'an RM Circle or em dash reply never leaves (' + r.reply + ')'); }
ok(/NEVER promise or estimate income/.test(lastPrompt) && /SafePal or any other crypto wallet/.test(lastPrompt) && /Ad credits buy advertising, not income\./.test(lastPrompt), 'house rules in the system prompt');
reply = 'Fine.';
// 4. the network merge: home boosted, a named site boosted, labelled with the site and its link
const GENERAL = bot.CANNED.find(c => c.id !== 'support' && c.id !== 'refund' && (c.q || []).length).q[0].replace(/\?$/, '');
netkb._set({ version: 'v1', sites: { othersite: { name: 'Other Site', url: 'https://other.example', count: 1 }, [process.env.HUB_SITE || bot.SITE.hubSite]: { name: bot.SITE.name, url: bot.SITE.url } },
entries: [{ site: 'othersite', id: 'about', title: 'What is Other Site?', questions: ['What is Other Site?', GENERAL + ' on Other Site'], answer: 'Other Site is a test site with its own rules.', tags: ['test'] }] });
await kb.load(true);
{ const hits = await kb.search(GENERAL, 8); const top = hits[0] && hits[0].entry; ok(top && !top.site, 'home entries first for a general question (top ' + (top && top.id) + ')'); }
{ const hits = await kb.search(GENERAL + ' on Other Site', 8); const top = hits[0] && hits[0].entry; ok(top && top.site === 'othersite', 'a named site boosts its own entry (top ' + (top && top.id) + ')'); }
{ const block = kb.promptBlock(await kb.context('What is Other Site', 6)); ok(/about Other Site, another network site; send people to https:\/\/other\.example/.test(block), 'remote entry labelled with site and link'); }
{ const fp = await kb.fastPath('What is Other Site?'); ok(!fp || !String(fp.id).startsWith('othersite:'), 'other sites never take the fast path'); }
{ const own = await kb.own(); ok(own.length === seeds.length && own.every(e => !e.site), 'publish set = this site\'s own entries only'); }
// 5. the scripted Q&A (home questions)
const qaFile = path.join(ROOT, 'qa', 'chat-qa.json');
if (fs.existsSync(qaFile)) {
const qa = JSON.parse(fs.readFileSync(qaFile, 'utf8'));
for (const t of qa.home || []) {
const r = await bot.answer(t.q);
if (t.kb) ok(r.kb === 'c-' + t.kb || (Array.isArray(r.kb) && r.kb.includes('c-' + t.kb)), 'Q&A "' + t.q + '" -> ' + t.kb + ' (got ' + JSON.stringify(r.kb) + ')');
for (const m of t.must || []) ok(new RegExp(m, 'i').test(r.reply) || new RegExp(m, 'i').test(lastPrompt), 'Q&A "' + t.q + '" mentions /' + m + '/');
}
}
or.close();
console.log((fail ? 'FAILED ' : 'ok ') + pass + ' passed, ' + fail + ' failed');
process.exit(fail ? 1 : 0);
+8 -1
View File
@@ -127,7 +127,7 @@ const ENV = Object.assign({}, process.env, { PORT: String(PORT), DATA_DIR: DATA,
VISIT_SECONDS: '2', VISITS_PER_IP_DAY: '2', LINK_PROBE: '0', NETWORK_LOOKUP_URL: 'http://127.0.0.1:' + LOOKUP_PORT + '/api/network/member',
NETWORK_SSO_SECRET: SSO_SECRET, IAP_URL: 'https://iap.invalid', LS_URL: 'https://ls.invalid', NETWORK_SITES_HUB: '0',
CONTRACT_ADDRESS: SHOP, CHAIN_RPC: 'http://127.0.0.1:' + CHAIN_PORT, KEEPER_KEY: KEEPER_PK, SHOP_START_BLOCK: '1', SHOP_CONFIRMATIONS: '0', SHOP_POLL_MS: '200', EXPLORER: 'https://explorer.invalid',
HUB_ENABLED: '1', HUB_URL: 'http://127.0.0.1:' + HUB_PORT, HUB_SECRET, HUB_SITE: 'viraladblitz', HUB_TICK_MS: '300', HUB_DOWN_MS: '300', HUB_CACHE_MS: '200',
HUB_ENABLED: '1', NETKB: '0', HUB_URL: 'http://127.0.0.1:' + HUB_PORT, HUB_SECRET, HUB_SITE: 'viraladblitz', HUB_TICK_MS: '300', HUB_DOWN_MS: '300', HUB_CACHE_MS: '200',
LS_TRACK_URL: 'http://127.0.0.1:' + LS_PORT, LS_SWEEP_FIRST_MS: '300', LS_SWEEP_MS: '300', LS_RETRY_MS: '300', LS_CACHE_MS: '100',
TG_TOKEN: 'standin-token', TG_CHAT: '-100123', TG_PAY_TOPIC: '632242', TG_API: 'http://127.0.0.1:' + TG_PORT });
process.on('exit', () => { try { srv && srv.kill(); } catch (e) {} });
@@ -208,6 +208,13 @@ async function main() {
ok(/script-src[^;]*https:\/\/linkspin\.app/.test(r.headers.get('content-security-policy') || ''), 'CSP admits linkspin.app (network zones)');
r = await anon.req('/nobody-at-all'); ok(r.status === 404 && /Back to the wire/.test(await r.text()), 'a username the hub does not know: 404');
r = await anon.req('/api/my'); ok(r.status === 401, '/api/my needs sign-in');
// the site assistant (2026-11-15): widget on every page, canned answers without AI, admin KB behind the token
ok(/<script src="\/assets\/chat\.js\?v=\d+" defer><\/script>/.test(home) && (await anon.req('/assets/chat.js')).status === 200 && (await anon.req('/assets/chat.css')).status === 200, 'chat widget on the page, its script and styles served');
{ const cr = await anon.req('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'What is a Boost?' }) }); const cj = await cr.json();
ok(cr.status === 200 && cj.canned && /\$5/.test(cj.reply) && /500/.test(cj.reply) && /A Boost buys advertising, not income\./.test(cj.reply), 'chat: canned Boost answer', cj); }
{ const cr = await anon.req('/api/chat', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: 'my boost did not show up' }) }); const cj = await cr.json();
ok(cj.handoff === true && /^https:\/\/instantadpay\.com\/support\?from=viraladblitz/.test(cj.support || ''), 'chat: account problem hands off to network support', cj); }
ok((await anon.req('/api/admin/kb')).status === 401, 'admin KB needs the token');
console.log('4. promo kit');
{ const kr = await anon.req('/api/promo-kit?u=smokeleader'); const k = await kr.json();