Repository navigation
Expand file tree
/
Copy pathanswerFilterAudit.js
More file actions
52 lines (47 loc) · 1.81 KB
/
Copy pathanswerFilterAudit.js
File metadata and controls
52 lines (47 loc) · 1.81 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
// Current-result-only context for the existing reviewer. Never serialize this
// text into agent JSON, history, stage evidence, logs or a durable store.
const audits = new WeakMap();
const MAX_REMOVED_UNITS = 12;
const MAX_REMOVED_CHARS = 6000;
const normalize = text => String(text || '').replace(/\s+/g, ' ').trim().toLowerCase();
function mergeRemovalAudits(...inputs) {
const removed = [];
const seen = new Set();
let chars = 0;
let truncated = false;
for (const input of inputs) {
truncated ||= input?.truncated === true;
for (const value of Array.isArray(input?.removed) ? input.removed : []) {
if (typeof value !== 'string') continue;
const unit = value.trim();
const key = normalize(unit);
if (!key || seen.has(key)) continue;
seen.add(key);
if (removed.length >= MAX_REMOVED_UNITS || chars + unit.length > MAX_REMOVED_CHARS) {
truncated = true;
continue; // Never cut a condition or warning into a misleading fragment.
}
removed.push(unit);
chars += unit.length;
}
}
return { removed, truncated };
}
function removedAnswerUnits(before, after) {
const normalizedAfter = normalize(after);
return mergeRemovalAudits({ removed: String(before || '').trim()
.split(/(?<=[.!?])\s+|\n+/)
.map(unit => unit.trim()).filter(Boolean)
.filter(unit => !normalizedAfter.includes(normalize(unit))) });
}
function rememberRemovalAudit(result, audit) {
if (result && typeof result === 'object') audits.set(result, mergeRemovalAudits(audit));
return result;
}
function getRemovalAudit(result) {
return mergeRemovalAudits(result && typeof result === 'object' ? audits.get(result) : undefined);
}
module.exports = {
MAX_REMOVED_UNITS, MAX_REMOVED_CHARS,
mergeRemovalAudits, removedAnswerUnits, rememberRemovalAudit, getRemovalAudit,
};