Repository navigation
Expand file tree
/
Copy pathfeedback.js
More file actions
250 lines (233 loc) · 7.89 KB
/
Copy pathfeedback.js
File metadata and controls
250 lines (233 loc) · 7.89 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const {
chunkDocument,
cleanDocument,
formatEvidence,
queryTerms,
rankLocalChunks,
supportQueries,
} = require('./retrieval');
const { redactPublic } = require('./helpcenter');
const SITEMAP = 'https://feedback.omi.me/sitemap.xml';
const CACHE_MS = 60 * 60 * 1000;
const ERROR_CACHE_MS = 5 * 60 * 1000;
const PAGE_CACHE_MS = 15 * 60 * 1000;
let sitemapCache = { at: 0, ok: false, pages: [] };
const pageCache = new Map();
function feedbackPostUrls(xml) {
const urls = [];
for (const match of String(xml || '').matchAll(/<loc>([^<]+)<\/loc>/g)) {
try {
const url = new URL(match[1].replace(/&/g, '&').trim());
if (url.hostname !== 'feedback.omi.me' || !/^\/p\/[^/]+\/?$/.test(url.pathname)) continue;
url.search = '';
url.hash = '';
urls.push(url.toString().replace(/\/$/, ''));
} catch {
// Ignore malformed sitemap entries.
}
}
return [...new Set(urls)];
}
function editDistance(a, b) {
const left = String(a || '');
const right = String(b || '');
if (left === right) return 0;
if (!left.length) return right.length;
if (!right.length) return left.length;
let previous = Array.from({ length: right.length + 1 }, (_, index) => index);
for (let i = 1; i <= left.length; i += 1) {
const current = [i];
for (let j = 1; j <= right.length; j += 1) {
current[j] = Math.min(
current[j - 1] + 1,
previous[j] + 1,
previous[j - 1] + (left[i - 1] === right[j - 1] ? 0 : 1)
);
}
previous = current;
}
return previous[right.length];
}
function slugTerms(url) {
try {
return decodeURIComponent(new URL(url).pathname.split('/').filter(Boolean).at(-1) || '')
.toLowerCase()
.split(/[^\p{L}\p{N}]+/u)
.filter((word) => word.length > 1);
} catch {
return [];
}
}
function termScore(wanted, candidate) {
if (wanted === candidate) return 5;
if (wanted.length >= 5 && candidate.length >= 5 && editDistance(wanted, candidate) === 1) return 4;
if (wanted.length >= 4 && candidate.length >= 4 && (wanted.includes(candidate) || candidate.includes(wanted))) {
return 2;
}
return 0;
}
function rankFeedbackPages(question, urls, limit = 4) {
const generic = new Set(['app', 'is', 'only', 'says', 'use', 'used', 'uses']);
const wanted = queryTerms(question, 30).filter((word) => !generic.has(word));
if (!wanted.length) return [];
return (urls || [])
.map((url) => {
const terms = slugTerms(url);
let score = 0;
let matches = 0;
for (const word of wanted) {
const best = Math.max(0, ...terms.map((term) => termScore(word, term)));
if (best) matches += 1;
score += best;
}
for (let index = 0; index < wanted.length - 1; index += 1) {
for (let position = 0; position < terms.length - 1; position += 1) {
if (
termScore(wanted[index], terms[position]) >= 4 &&
termScore(wanted[index + 1], terms[position + 1]) >= 4
) {
score += 10;
break;
}
}
}
if (matches >= 2) score += 3;
return { url, score, matches };
})
.filter((item) => item.score >= 5)
.sort((a, b) => b.score - a.score || b.matches - a.matches)
.slice(0, limit)
.map((item) => item.url);
}
function nextData(html) {
const match = String(html || '').match(
/<script[^>]*id=["']__NEXT_DATA__["'][^>]*>([\s\S]*?)<\/script>/i
);
if (!match) return null;
try {
return JSON.parse(match[1]);
} catch {
return null;
}
}
function feedbackPostFromHtml(html) {
const data = nextData(html);
const fallback = data?.props?.pageProps?.fallback || {};
const result = fallback['rq:single:/v1/submission']?.data?.results?.[0];
if (!result?.title || !result?.content) return null;
return result;
}
function categoryName(category) {
const value = category?.name;
if (typeof value === 'string') return value;
return String(value?.en || category?.category || '').trim();
}
function feedbackPage(url, post) {
const status = String(post?.postStatus?.name || '').trim();
const category = categoryName(post?.postCategory);
const body = cleanDocument(redactPublic(post?.content || ''), 8_000);
const lines = [
'Customer-submitted feedback report. The report describes a symptom or request; it is not verified product documentation and contains no approved troubleshooting steps.',
status ? `Portal status: ${status}` : '',
category ? `Category: ${category}` : '',
Number.isFinite(Number(post?.upvotes)) ? `Requests/upvotes: ${Number(post.upvotes)}` : '',
post?.date ? `Reported: ${post.date}` : '',
post?.lastModified ? `Portal last updated: ${post.lastModified}` : '',
body ? `Customer report: ${body}` : '',
].filter(Boolean);
return {
url,
title: cleanDocument(redactPublic(post.title), 240) || 'Feedback report',
body: lines.join('\n'),
};
}
function activeStore(store) {
if (store) return store;
if (!process.env.DATABASE_URL) return null;
return require('./db');
}
async function fetchSitemap(fetchFn, useCache) {
const now = Date.now();
const cacheAge = sitemapCache.ok ? CACHE_MS : ERROR_CACHE_MS;
if (useCache && sitemapCache.at && now - sitemapCache.at < cacheAge) {
return sitemapCache.pages;
}
const response = await fetchFn(SITEMAP, {
headers: { 'User-Agent': 'omi-vector-bot' },
signal: AbortSignal.timeout(8_000),
});
if (!response.ok) {
if (useCache) sitemapCache = { at: now, ok: false, pages: [] };
return [];
}
const pages = feedbackPostUrls(await response.text());
if (useCache) sitemapCache = { at: now, ok: true, pages };
return pages;
}
async function fetchPage(url, fetchFn, useCache) {
const now = Date.now();
const cached = pageCache.get(url);
if (useCache && cached && now - cached.at < PAGE_CACHE_MS) return cached.page;
const response = await fetchFn(url, {
headers: { 'User-Agent': 'omi-vector-bot' },
signal: AbortSignal.timeout(8_000),
});
if (!response.ok) return null;
const post = feedbackPostFromHtml(await response.text());
const page = post ? feedbackPage(url, post) : null;
if (useCache && page) pageCache.set(url, { at: now, page });
return page;
}
async function storedFeedback(queries, store) {
if (!store?.searchDocPages) return '';
try {
const sets = await Promise.all(queries.map((query) => store.searchDocPages(query, 4, ['feedback'])));
const chunks = sets.flat();
return formatEvidence(rankLocalChunks(queries, chunks, 4), { maxChars: 8_000, maxPerPage: 1 });
} catch (err) {
console.error('[Feedback] stored lookup failed:', err.message);
return '';
}
}
async function relevantFeedback(question, { fetchImpl, store, queries: plannedQueries = [] } = {}) {
const fetchFn = fetchImpl || fetch;
const useCache = !fetchImpl;
const saved = activeStore(store);
const queries = supportQueries(question, plannedQueries);
try {
const urls = await fetchSitemap(fetchFn, useCache);
const picked = rankFeedbackPages(queries.join(' '), urls);
const pages = (await Promise.all(picked.map((url) => fetchPage(url, fetchFn, useCache)))).filter(Boolean);
if (saved?.saveDocPage) {
for (const page of pages) {
try {
await saved.saveDocPage(page);
} catch (err) {
console.error('[Feedback] save failed:', err.message);
}
}
}
const chunks = pages.flatMap((page) => chunkDocument(page));
const live = formatEvidence(rankLocalChunks(queries, chunks, 4), {
maxChars: 8_000,
maxPerPage: 1,
});
return live || storedFeedback(queries, saved);
} catch (err) {
console.error('[Feedback] lookup failed:', err.message);
return storedFeedback(queries, saved);
}
}
function resetFeedbackCache() {
sitemapCache = { at: 0, ok: false, pages: [] };
pageCache.clear();
}
module.exports = {
SITEMAP,
feedbackPostUrls,
rankFeedbackPages,
feedbackPostFromHtml,
feedbackPage,
relevantFeedback,
resetFeedbackCache,
};