Repository navigation
Expand file tree
/
Copy pathknowledge.js
More file actions
214 lines (192 loc) · 7.46 KB
/
Copy pathknowledge.js
File metadata and controls
214 lines (192 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
const { looksLikeStaffLie } = require('./honesty');
const { clipForDiscord } = require('./utils');
const MAX_SNIPPET = 500;
const MAX_STORED = 50;
const SEARCH_LIMIT = 5;
const snippets = [];
// Denials observed in this process, never an authority/publication cache. This
// lets an optional-registry outage preserve ordinary notes without knowingly
// reviving a governed copy. Remote governance is not discovered while off.
const knownGovernedHashes = new Map();
function parseFaqCommand(text) {
const raw = String(text || '').replace(/<@!?\d+>/g, '').trim();
const match = raw.match(/^faq:\s*([\s\S]*)$/i);
if (!match) return null;
return clipForDiscord(match[1].trim(), MAX_SNIPPET);
}
function resetKnowledge() {
snippets.length = 0;
knownGovernedHashes.clear();
}
function listSnippets() {
return [...snippets];
}
function addSnippet(text) {
const snippet = clipForDiscord(String(text || '').trim(), MAX_SNIPPET);
if (!snippet) return { ok: false, reason: 'empty' };
if (looksLikeStaffLie(snippet)) return { ok: false, reason: 'lie' };
try {
const shopify = require('./shopify');
if (shopify.isConfigured() && /cannot see shopify/i.test(snippet)) {
return { ok: false, reason: 'stale' };
}
} catch {
/* shopify optional */
}
if (snippets[0] === snippet) return { ok: true, snippet, duplicate: true };
snippets.unshift(snippet);
if (snippets.length > MAX_STORED) snippets.length = MAX_STORED;
return { ok: true, snippet };
}
async function persistSnippet(snippet) {
if (!process.env.DATABASE_URL) return;
try {
const db = require('./db');
await db.addKnowledge(snippet);
} catch (err) {
console.error('[Knowledge] db save failed:', err.message);
}
}
function searchWords(query) {
return String(query || '')
.toLowerCase()
.replace(/[^a-z0-9\s]/g, ' ')
.split(/\s+/)
.filter((w) => w.length > 3)
.slice(0, 5);
}
const HOWTO_FACT = /\b(pair|pairing|bluetooth|led\b|teal|orange =|dev kit|cv1|center button)\b/i;
const DEVICE_FACT = /\b(necklace|blue light|teal|pairing|bluetooth|recording|disconnected)\b/i;
const SHOP_FACT = /\b(shopify|order lookups?|order and tracking|order status|tracking number|where is (?:my|the) order|\/order)\b/i;
function isHowtoFact(text) {
return HOWTO_FACT.test(String(text || ''));
}
function isShopFact(text) {
return SHOP_FACT.test(String(text || ''));
}
function filterSnippetsForLane(snippets, lane) {
const list = (snippets || []).map((s) => String(s || '').trim()).filter(Boolean);
const keepShop = lane === 'shop' || lane === 'money';
if (!lane || lane === 'faq' || lane === 'unknown') {
return keepShop ? list : list.filter((s) => !isShopFact(s));
}
let filtered = list.filter((s) => !isHowtoFact(s));
if (lane === 'shop' || lane === 'money' || lane === 'privacy') {
filtered = filtered.filter((s) => !DEVICE_FACT.test(s));
}
if (!keepShop) filtered = filtered.filter((s) => !isShopFact(s));
return filtered;
}
function search(query, limit = SEARCH_LIMIT) {
const words = searchWords(query);
if (!words.length) return [];
const pool = snippets.filter((s) => {
const lower = s.toLowerCase();
return words.some((w) => lower.includes(w));
});
return pool.slice(0, limit);
}
async function searchAll(query, limit = SEARCH_LIMIT) {
if (!searchWords(query).length) return [];
const local = search(query, limit);
if (!process.env.DATABASE_URL) return local;
try {
const db = require('./db');
const remote = await db.searchKnowledge(query, limit);
const seen = new Set(local.map((s) => s.toLowerCase()));
const merged = [...local];
for (const item of remote) {
const key = String(item || '').toLowerCase();
if (!key || seen.has(key)) continue;
seen.add(key);
merged.push(item);
if (merged.length >= limit) break;
}
return merged.slice(0, limit);
} catch (err) {
console.error('[Knowledge] db search failed:', err.message);
return local;
}
}
async function searchForAnswers(query, { guildId, store, enabled = process.env.SUPPORT_FIX_INBOX === '1', now = Date.now(), limit = SEARCH_LIMIT } = {}) {
const local = search(query, limit);
const { hashContent } = require('./knowledgeReview');
const localFallback = () => local.filter(text => !knownGovernedHashes.get(guildId)?.has(hashContent(text)));
// Disabled means no optional database construction, registry check or fact
// lookup on the answering path. The existing staff-note behavior remains.
if (enabled !== true) return localFallback();
try {
if (!store && process.env.DATABASE_URL) store = require('./knowledgeReviewStore').createPostgresStore(require('./db').pool);
if (!store) return localFallback();
if (!guildId) return [];
const governed = await store.managedHashes(guildId, local.map(hashContent), local);
const known = knownGovernedHashes.get(guildId) || new Set();
for (const hash of governed) known.add(hash);
if (known.size) knownGovernedHashes.set(guildId, known);
const reviewed = await store.search(guildId, query, now, limit);
const notes = reviewed.map(row => `${row.content}\nReviewed product note (${row.productVersion}). Official sources: ${row.sources.join(' ')}`);
return [...new Set([...notes, ...localFallback()])].slice(0, limit);
} catch {
// Do not publish a cached reviewed fact or claim remote state is current.
// Keep pre-existing local notes, minus governance already observed here.
console.error('[KnowledgeReview] lookup unavailable; using local notes with known-governed exclusions');
return localFallback();
}
}
function collectFaqFromMessages(messages, allowedStaffIds = []) {
const found = [];
const allowed = new Set(allowedStaffIds.map(String));
for (const m of messages || []) {
if (m?.author?.bot) continue;
if (!allowed.has(String(m?.author?.id || ''))) continue;
const snippet = parseFaqCommand(m.content);
if (snippet) found.push(snippet);
}
return found;
}
async function hydrateFromDiscord(client, allowedStaffIds = []) {
if (!allowedStaffIds.length) return 0;
const channelId = process.env.VECTOR_TEST_CHANNEL_ID;
if (!channelId || !client?.channels?.fetch) return 0;
const channel = await client.channels.fetch(channelId);
if (!channel?.threads?.fetchActive) return 0;
const threads = [];
const active = await channel.threads.fetchActive();
if (active?.threads) threads.push(...active.threads.values());
try {
const archived = await channel.threads.fetchArchived({ limit: 20 });
if (archived?.threads) threads.push(...archived.threads.values());
} catch (err) {
console.error('[Knowledge] archived threads:', err.message);
}
let added = 0;
const seen = new Set();
for (const thread of threads) {
if (!/^Handoff\b/i.test(thread.name || '')) continue;
if (seen.has(thread.id)) continue;
seen.add(thread.id);
const fetched = await thread.messages.fetch({ limit: 50 });
for (const snippet of collectFaqFromMessages([...fetched.values()].reverse(), allowedStaffIds)) {
const saved = addSnippet(snippet);
if (saved.ok && !saved.duplicate) added += 1;
}
}
return added;
}
module.exports = {
MAX_SNIPPET,
parseFaqCommand,
resetKnowledge,
listSnippets,
addSnippet,
persistSnippet,
search,
searchAll,
searchForAnswers,
createReviewService: require('./knowledgeReview').createReviewService,
createReviewedMemoryStore: require('./knowledgeReviewStore').createMemoryStore,
collectFaqFromMessages,
hydrateFromDiscord,
filterSnippetsForLane,
isHowtoFact,
};