Repository navigation
Expand file tree
/
Copy pathtriage.js
More file actions
129 lines (117 loc) · 4.01 KB
/
Copy pathtriage.js
File metadata and controls
129 lines (117 loc) · 4.01 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
const { classify, looksLikeCaptureFailure, looksLikeTranscription, looksLikeDocs, looksLikeRecordingHow, looksLikeDeviceReset } = require('./router');
const { clipForDiscord } = require('./utils');
const AREAS = ['shop', 'app', 'desktop', 'firmware', 'privacy'];
const LANES = ['shop', 'money', 'privacy', 'firmware', 'tech', 'faq', 'account', 'unknown'];
const LABELS = [
'shop',
'app',
'desktop',
'firmware',
'privacy',
'account',
'money',
'tech',
'shipping',
'faq',
];
function sanitizeTopic(text) {
let s = String(text || '')
.replace(/[\r\n]+/g, ' ')
.replace(/^handoff\s*·\s*/i, '')
.replace(/["*_`#]/g, '')
.replace(/\s{2,}/g, ' ')
.trim();
s = s.replace(/^(hi|hello|hey)[,!.\s]+/i, '').trim();
if (!s || s.length < 8) return '';
if (/^(just got|i just got|i('m| am) getting this error|nintendo kid)\b/i.test(s)) return '';
return clipForDiscord(s, 56);
}
function cleanLabels(list) {
const out = [];
for (const raw of list || []) {
const label = String(raw || '')
.toLowerCase()
.replace(/^needs-human$/, '')
.trim();
if (LABELS.includes(label) && !out.includes(label)) out.push(label);
}
return out;
}
function fallbackTopic(question, route) {
const { threadTopic } = require('./handoff');
return threadTopic(question, route);
}
function merge(route, agent, question) {
const classified = route || classify(question);
const moneyLock = classified.lane === 'money' || classified.lane === 'privacy';
const docsLock =
classified.lane === 'faq' &&
(looksLikeDocs(question) || looksLikeRecordingHow(question) || looksLikeDeviceReset(question));
const bothCaptureAndTranscript =
looksLikeCaptureFailure(question) && looksLikeTranscription(question);
let area = classified.area || 'unknown';
let lane = classified.lane || 'unknown';
if (!moneyLock && !docsLock && !bothCaptureAndTranscript) {
const weak = area === 'unknown' || lane === 'unknown' || lane === 'faq';
if (weak && AREAS.includes(agent?.area)) area = agent.area;
if (weak && LANES.includes(agent?.lane) && agent.lane !== 'unknown') lane = agent.lane;
if (lane === 'account') area = 'shop';
}
const { ticketLabels } = require('./handoff');
let labels = moneyLock
? ticketLabels({ area: classified.area, lane: classified.lane, question })
: cleanLabels(agent?.labels);
const inferred = ticketLabels({ area, lane, question });
for (const label of inferred) {
if (label !== 'needs-human' && !labels.includes(label)) labels.push(label);
}
if (/\b(shipping|tracking|customs)\b/i.test(String(question || '')) && !labels.includes('shipping')) {
labels.push('shipping');
}
labels = labels.filter((label) => label !== 'needs-human');
if (docsLock || (bothCaptureAndTranscript && lane === 'faq')) {
labels = labels.filter((label) => label !== 'shop' && label !== 'account' && label !== 'money');
if (!labels.includes('faq')) labels.unshift('faq');
}
if (!labels.length) labels.push('needs-human');
const topic =
sanitizeTopic(agent?.topic) ||
fallbackTopic(question, { area, lane }) ||
'needs a person';
const resolvedTopic = bothCaptureAndTranscript
? 'Transcription unavailable, device not capturing'
: topic;
const techish =
lane === 'tech' ||
lane === 'firmware' ||
area === 'app' ||
area === 'desktop' ||
area === 'firmware';
const blocked =
moneyLock || lane === 'shop' || lane === 'account' || area === 'shop' || area === 'privacy';
const fileIssue = techish && !blocked;
return {
area,
lane,
labels,
topic: resolvedTopic,
fileIssue,
escalate: docsLock
? Boolean(classified.wantHuman)
: Boolean(classified.escalate || agent?.escalate || moneyLock),
};
}
function wantsShopTicket(merged) {
const lane = merged?.lane;
const area = merged?.area;
if (area === 'privacy' || lane === 'privacy') return false;
return lane === 'shop' || lane === 'money';
}
module.exports = {
AREAS,
LABELS,
sanitizeTopic,
cleanLabels,
merge,
wantsShopTicket,
};