Repository navigation
Expand file tree
/
Copy pathanswerPipeline.js
More file actions
205 lines (191 loc) · 10.3 KB
/
Copy pathanswerPipeline.js
File metadata and controls
205 lines (191 loc) · 10.3 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
const {
sanitizeReply,
stripFalseCertainty,
stripPingNarration,
formatDiscordReply,
DISCORD_REPLY_MAX,
} = require('./utils');
const { stripHowtoBleed, stripShopBleed, stripUnsupportedClaims } = require('./honesty');
const { SOURCE_LABEL_NAME, SOURCE_LABEL_SEPARATOR } = require('./supportSteps');
const { publicReplyPrivacy } = require('./publicReplyPrivacy');
const { recordStage } = require('./answerStageEvidence');
const EMPTY_ANSWER_FALLBACK = 'I could not verify a safe answer here. A person needs to check this.';
const UNSYNCED_DATA_WARNING = 'Recordings may still be unsynced. Do not reinstall the app, log out, or clear Pending/All storage until a person checks; those actions could erase local recordings.';
function ensureNonEmptyAnswer(answer) {
return String(answer || '').trim() || EMPTY_ANSWER_FALLBACK;
}
// Filter the draft, and separately show the reviewer the original sentences
// removed by those filters. Only the reviewer may restore supported details.
function prepareDraftForReviewWithAudit(answer, lane, question) {
recordStage('draft_input', answer);
const original = String(answer || '').trim();
const draft = stripFalseCertainty(
stripUnsupportedClaims(
stripShopBleed(stripHowtoBleed(sanitizeReply(answer), lane), lane),
lane,
question
)
);
const normalizedDraft = draft.replace(/\s+/g, ' ').toLowerCase();
const removed = original
.split(/(?<=[.!?])\s+|\n+/)
.map((sentence) => sentence.trim())
.filter(Boolean)
.filter((sentence) => !normalizedDraft.includes(sentence.replace(/\s+/g, ' ').toLowerCase()))
.slice(0, 12);
recordStage('draft_safety', draft);
return { draft, removed };
}
function prepareDraftForReview(answer, lane, question) {
return prepareDraftForReviewWithAudit(answer, lane, question).draft;
}
// The closing source line: a label, then at least one https link, up to the end of the answer.
const TRAILING_SOURCE = new RegExp(
`\\n\\s*${SOURCE_LABEL_NAME}\\s*${SOURCE_LABEL_SEPARATOR}\\s*https:\\/\\/[^\\n]*$`,
'iu'
);
// Reply items are atomic: a wrapped bullet is one item, not a series of lines.
// Prose can stop at a complete sentence; an oversized first list item cannot.
// Keep this separate from the raw question/title clipper used elsewhere.
function clipCompleteReplyUnits(answer, max) {
const text = String(answer || '').trim();
if (text.length <= max) return text;
const bodyBudget = Math.max(0, max - 2); // newline plus the truncation marker
const units = [];
let current;
let offset = 0;
const finish = () => {
if (current) units.push(current);
current = undefined;
};
for (const line of text.split('\n')) {
const listItem = /^\s*(?:[-*+•]|\d+[.)])\s+\S/u.test(line);
if (!line.trim()) {
finish();
} else if (listItem || !current) {
finish();
current = { start: offset, end: offset + line.length, listItem };
} else {
current.end = offset + line.length;
}
offset += line.length + 1;
}
finish();
let end = 0;
for (const unit of units) {
if (unit.end <= bodyBudget) {
end = unit.end;
continue;
}
if (!unit.listItem) {
const prefix = text.slice(unit.start, bodyBudget);
for (const sentenceEnd of prefix.matchAll(/[.!?。!?]["'”’\])]*(?=\s|$)/gu)) {
const candidate = unit.start + sentenceEnd.index + sentenceEnd[0].length;
// The prefix boundary itself is not a sentence boundary if the next
// original character continues a URL, word or punctuation group.
if (!text[candidate] || /\s/u.test(text[candidate])) end = candidate;
}
}
break;
}
const body = text.slice(0, end).trim();
if (body) return `${body}\n…`;
const note = 'This reply was too long to display in full.';
return note.length <= max ? note : '';
}
// Clip at a complete reply item and keep the source line whole. A link cut
// short opens nothing, including on multi-part answers with several sources.
function clipKeepingSource(answer, max = DISCORD_REPLY_MAX) {
const text = String(answer || '').trim();
if (text.length <= max) return text;
const sourceAt = text.search(TRAILING_SOURCE);
if (sourceAt < 0) return clipCompleteReplyUnits(text, max);
const tail = text.slice(sourceAt);
const gap = /^\n\s*\n/.test(tail) ? '\n\n' : '\n';
const sourceLine = tail.trim();
const bodyBudget = max - sourceLine.length - gap.length;
if (bodyBudget < 0) return EMPTY_ANSWER_FALLBACK; // never publish a severed source URL
const body = clipCompleteReplyUnits(text.slice(0, sourceAt), bodyBudget);
return body ? `${body}${gap}${sourceLine}` : sourceLine;
}
function presentReviewedAnswer(answer, opts = {}) {
recordStage('presentation_input', answer);
const privacy = publicReplyPrivacy(stripPingNarration(answer), opts);
opts.onPrivacy?.(privacy);
recordStage('presentation_filtered', privacy.text);
return recordStage('presentation_output', ensureNonEmptyAnswer(fitPublicReply({ ...privacy, text: formatDiscordReply(privacy.text) })));
}
// Late delivery/privacy copy must fit too. Reserve the complete custody warning,
// application-owned next steps and scoped links before optional prose.
function fitPublicReply(privacy, max = DISCORD_REPLY_MAX) {
const text = String(privacy.text || '').trim();
recordStage('clip_input', text);
if (text.length <= max) return recordStage('clip_output', text);
const tail = [text.includes(UNSYNCED_DATA_WARNING) ? UNSYNCED_DATA_WARNING : '',
privacy.privateNextStep, ...new Set(privacy.channelLinks || [])].filter(Boolean);
if (!tail.length) return recordStage('clip_output', clipKeepingSource(text, max));
let body = text;
for (const item of tail) body = body.split(item).join('');
const sources = [];
const sourceHeading = new RegExp(`^\\s*${SOURCE_LABEL_NAME}\\s*${SOURCE_LABEL_SEPARATOR}\\s*https:\\/\\/`, 'iu');
body = body.split('\n').filter(line => {
if (!sourceHeading.test(line)) return true;
sources.push(line.trim()); return false;
}).join('\n').trim();
body = [body, ...sources].filter(Boolean).join('\n\n');
const ending = tail.join('\n\n');
const budget = Math.max(0, max - ending.length - (ending ? 2 : 0));
return recordStage('clip_output', [clipKeepingSource(body, budget), ending].filter(Boolean).join('\n\n'));
}
function stripUnverifiedOrderClaims(answer, { verifiedLookup = false } = {}) {
if (verifiedLookup) return String(answer || '').trim();
const assertedStatus = /\b(?:(?:your|this|the)\s+(?:order|package|parcel|shipment|delivery|omi|device|necklace|glasses)|it)\s+(?:(?:is|was|has|had|hasn['’]?t|will|would|should|may|might|appears?|seems?|expected|likely)\s+){1,3}(?:(?:been|be|to|have|not|already|currently|probably)\s+){0,3}(?:(?:(?:marked|showing|listed)\s+as\s+)?(?:deliver\w*|shipp\w*|dispatch\w*|arriv\w*|in\s+transit|out\s+for\s+delivery|on\s+its\s+way)|located|at\s+(?:the|a|your)\b|sitting\s+(?:at|with)\s+(?:the\s+)?(?:depot|carrier|customs)\b)/i;
const pronounStatus = /\b(?:it(?:['’]s|\s+(?:is|has))|they(?:['’]re|\s+(?:are|have)))\s+(?:been\s+)?(?:on\s+its\s+way|out\s+for\s+delivery|delivered|in\s+transit|shipped)\b/i;
const locationClaim = /\b(?:(?:your|this|the)\s+(?:order|package|parcel|shipment|delivery)|it|they)(?:['’](?:s|re)|\s+(?:is|are|was|were|has|have|had))\s+(?:been\s+)?(?:stuck|held|sitting)\s+(?:at|in|with)\s+(?:(?:a|the)\s+)?(?:customs|depot|sorting\s+facility|warehouse|carrier)\b/i;
const carrierClaim = /\b(?:the|your|a)\s+carrier\s+(?:has\s+|had\s+)?(?:deliver\w*|shipp\w*|dispatch\w*)\s+(?:it|your\s+(?:order|package|parcel|shipment))\b/i;
return String(answer || '')
.split('\n')
.map((line) => line.split(/(?<=[.!?])\s+/)
.filter((sentence) => {
if (/^\s*(?:source|fuente|quelle|fonte|источник|来源|출처)\s*:/i.test(sentence)) return true;
const claim = assertedStatus.exec(sentence) || pronounStatus.exec(sentence) || locationClaim.exec(sentence) || carrierClaim.exec(sentence);
if (!claim) return true;
return /\b(?:if|when|in case)\b/i.test(sentence.slice(0, claim.index));
})
.join(' ').trim())
.filter(Boolean)
.join('\n')
.trim();
}
function hasUnsyncedDataRisk(question) {
const text = String(question || '');
const data = /\b(?:sync\w*|record\w*|audio|meetings?|conversations?|memories?|transcri\w*|dictat\w*|voice)\b/i.test(text);
const loss = /\b(?:gone|vanish\w*|disappear\w*|lost|losing|missing|nothing\s+sync\w*|never\s+showed\s+up|not\s+sync\w*|didn['’]?t\s+sync\w*|sync\w*\s+(?:is\s+)?(?:stuck|stall\w*|fail\w*)|(?:stuck|stall\w*|fail\w*)\s+sync\w*)\b/i.test(text);
const emptyOutput = /\b(?:(?:no|zero|empty|blank)\s+(?:transcripts?|recordings?|audio)|(?:transcripts?|recordings?|audio)\s+(?:is|was|are|were|came(?:\s+back)?)?\s*(?:empty|blank|zero|missing)|nothing\s+(?:was\s+)?(?:recorded|transcribed|saved))\b/i.test(text);
const happened = /\b(?:recorded|captured|finished|spoke|talked|said|dictated|yesterday|last\s+(?:night|week|meeting|call)|(?:\d+|an?|one|two|three|four|several)\s+(?:hours?|minutes?)|(?:a|the|my)\s+whole\s+(?:meeting|lecture|call|day)|all\s+day|during\s+(?:my|the|a)\s+(?:call|meeting|lecture)|(?:my|the)\s+(?:recording|audio|transcript))\b/i.test(text);
return data && (loss || (emptyOutput && happened));
}
function addUnsyncedDataWarning(answer, question, { dataLossRisk = false, language = 'en' } = {}) {
const current = String(answer || '').trim();
if (!(dataLossRisk || hasUnsyncedDataRisk(question))) return current;
if (String(language || 'en').toLowerCase().split('-')[0] !== 'en') return current;
if (/do not reinstall/i.test(current) && /log out/i.test(current) && /clear Pending\/All/i.test(current)) return current;
const warning = UNSYNCED_DATA_WARNING;
const sourceAt = current.search(/\n(?:Sources?):\s*https:\/\//i);
const sourceLine = sourceAt < 0 ? '' : current.slice(sourceAt).trim();
const body = sourceAt < 0 ? current : current.slice(0, sourceAt).trim();
const bodyBudget = Math.max(200, 1_750 - warning.length - sourceLine.length - 4);
return [clipCompleteReplyUnits(body, bodyBudget), warning, sourceLine].filter(Boolean).join('\n\n');
}
module.exports = {
EMPTY_ANSWER_FALLBACK,
UNSYNCED_DATA_WARNING,
ensureNonEmptyAnswer,
prepareDraftForReview,
prepareDraftForReviewWithAudit,
presentReviewedAnswer,
fitPublicReply,
stripUnverifiedOrderClaims,
addUnsyncedDataWarning,
hasUnsyncedDataRisk,
};