-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
83 lines (71 loc) · 1.89 KB
/
utils.js
File metadata and controls
83 lines (71 loc) · 1.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
const GREETINGS = [
'Hey!',
'Hi there!',
'Hey, thanks for reaching out!',
'Hi! Good question.',
'Hey! Let me help with that.',
];
const ESCALATION_KEYWORDS = [
'billing',
'refund',
'privacy',
'shipping',
'charge',
'charged',
'invoice',
'payment',
'cancel subscription',
'delete my data',
'gdpr',
];
const CONFIDENCE_THRESHOLD = 0.72;
const COOLDOWN_MS = 60_000;
// thread_id -> timestamp of last reply
const lastReplyMap = new Map();
function isOnCooldown(threadId) {
const last = lastReplyMap.get(threadId);
if (!last) return false;
return Date.now() - last < COOLDOWN_MS;
}
function markReplied(threadId) {
lastReplyMap.set(threadId, Date.now());
}
// Prevent unbounded growth — prune entries older than 5 minutes every 2 minutes
setInterval(() => {
const cutoff = Date.now() - 5 * 60_000;
for (const [key, ts] of lastReplyMap) {
if (ts < cutoff) lastReplyMap.delete(key);
}
}, 2 * 60_000).unref();
function randomGreeting() {
return GREETINGS[Math.floor(Math.random() * GREETINGS.length)];
}
function containsEscalationKeyword(text) {
const lower = text.toLowerCase();
return ESCALATION_KEYWORDS.some((kw) => lower.includes(kw));
}
function shouldEscalate(aiResponse, userMessage) {
if (aiResponse.confidence < CONFIDENCE_THRESHOLD) return true;
if (aiResponse.escalate === true) return true;
if (containsEscalationKeyword(userMessage)) return true;
return false;
}
function typingDelay() {
// 1–2 second delay to feel human
const ms = 1000 + Math.random() * 1000;
return new Promise((resolve) => setTimeout(resolve, ms));
}
function sanitizeReply(text) {
// Strip any "As an AI" phrasing
return text.replace(/\bas an ai\b/gi, '').replace(/\s{2,}/g, ' ').trim();
}
module.exports = {
isOnCooldown,
markReplied,
randomGreeting,
containsEscalationKeyword,
shouldEscalate,
typingDelay,
sanitizeReply,
CONFIDENCE_THRESHOLD,
};