diff --git a/chatcore.js b/chatcore.js index 88e57be..b1f1493 100644 --- a/chatcore.js +++ b/chatcore.js @@ -116,8 +116,15 @@ async function answer(message, historyText) { async function answerRaw(message, historyText) { const msg = String(message || '').trim().slice(0, 600); if (!msg) return { error: 'Say something first.' }; - let hit = null, kbOk = true; - try { hit = await kb.fastPath(msg); } catch (e) { kbOk = false; console.error('kb fast path', e.message); } + let hit = null, kbOk = true, other = []; + try { + await kb.load(); + // a question about another network site (named, and this site not named) skips this site's instant answers, except a hand-off + other = kb.namesOther(msg, S.hubSite); + if (other.length && new RegExp('\\b' + S.name.replace(/[^A-Za-z0-9]+/g, '\\s*') + '\\b', 'i').test(msg)) other = []; + hit = await kb.fastPath(msg); + if (hit && other.length && !hit.handoff) hit = null; + } catch (e) { kbOk = false; console.error('kb fast path', e.message); } if (hit) return Object.assign({ reply: hit.answer, canned: true, kb: hit.id }, hit.handoff ? { handoff: true } : {}); if (!kbOk) for (const c of S.canned) if (c.re && c.re.test(msg)) return Object.assign({ reply: c.a, canned: true }, c.handoff ? { handoff: true } : {}); let kbBlock = '', kbIds = [], hits = []; diff --git a/kb.js b/kb.js index 63a8c83..1e54cd8 100644 --- a/kb.js +++ b/kb.js @@ -101,6 +101,21 @@ function namedSites(query, idx) { } return out; } +// does the question name a network site other than this one (hub site id `self`)? Then the home fast path stands aside and the +// answer comes from retrieval over the whole network (a "how much" about another site must not get this site's price). +const NAMES = { iap: 'InstantAdPay', linkspin: 'LinkSpin', bannerline: 'Downline Banners', vbb: 'Viral Banner Blitz', viraladblitz: 'ViralAdBlitz', + downlinehydra: 'DownlineHydra', fivex: '5xYourTeam', stackpol: 'Stack POL', earnerscorner: 'Earners Corner' }; +function namesOther(query, self) { + const q = norm(query); const out = []; + const ids = new Set(Object.keys(NAMES).concat(((index && index.sites) || []).map(s => s.site))); + for (const id of ids) { + if (id === self) continue; + const s = ((index && index.sites) || []).find(x => x.site === id) || {}; + const names = [s.name, NAMES[id], id.length > 3 ? id : ''].concat(ALIASES[id] || []).filter(Boolean).map(norm); + if (names.some(n => n.trim() && q.includes(n))) out.push(id); + } + return out; +} function rank(idx, query, k) { if (!idx || !idx.n) return []; const qt = [...new Set(terms(query))]; @@ -270,5 +285,5 @@ async function test(question) { return { fast: fast ? { id: fast.id, title: fast.title } : null, ranked: hits.map(h => ({ id: h.entry.id, site: h.entry.site || '', title: h.entry.title, score: h.score, used: used.has(h.entry.id) })) }; } -module.exports = { init, load, own, fastPath, search, context, promptBlock, list, get, create, update, remove, stamp, test, +module.exports = { init, load, own, namesOther, fastPath, search, context, promptBlock, list, get, create, update, remove, stamp, test, _pure: { words, terms, buildIndex, rank, relevant, validate, seedHash, namedSites, MIN_SCORE, HOME_BOOST, NAMED_BOOST } }; diff --git a/qa/chatbot.test.mjs b/qa/chatbot.test.mjs index d39658c..e8c4b4d 100644 --- a/qa/chatbot.test.mjs +++ b/qa/chatbot.test.mjs @@ -59,7 +59,7 @@ ok(/NEVER promise or estimate income/.test(lastPrompt) && /SafePal or any other 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(/\?$/, ''); +const GENERAL = bot.CANNED.find(c => !['support', 'refund', 'about'].includes(c.id) && (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); @@ -67,6 +67,9 @@ await kb.load(true); { 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 r = await bot.answer(GENERAL + ' on Other Site'); ok(!r.canned, 'a question naming another site skips this site\'s instant answers (' + r.kb + ')'); } +{ const r = await bot.answer('I need a real person, my order on Other Site failed'); ok(r.handoff === true, 'a hand-off still wins when another site is named'); } +{ const r = await bot.answer(bot.CANNED.find(c => c.id === 'about').q[0]); ok(r.canned && r.kb === 'c-about', 'this site\'s own name keeps the instant answer'); } { 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)