@peopl-health/nexus 5.56.35 → 5.56.37
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/controllers/assistantController.js +31 -2
- package/lib/helpers/pausedThreadHelper.js +13 -1
- package/lib/models/threadModel.js +1 -0
- package/lib/routes/index.js +4 -1
- package/lib/services/inboundRecoveryService.js +10 -4
- package/lib/services/recommendationCarouselService.js +2 -2
- package/lib/services/threadService.js +3 -0
- package/package.json +1 -1
|
@@ -3,17 +3,19 @@ const runtimeConfig = require('../config/runtimeConfig');
|
|
|
3
3
|
|
|
4
4
|
const { logger } = require('../utils/logger');
|
|
5
5
|
|
|
6
|
-
const { getThreadInfo, switchThreadStoppedStatus } = require('../services/threadService');
|
|
6
|
+
const { getThreadInfo, setThreadDoNotAlert, switchThreadStoppedStatus } = require('../services/threadService');
|
|
7
7
|
|
|
8
8
|
const { getRecordByFilter } = require('../services/airtableService');
|
|
9
9
|
const { sendMessage, processInstruction, processSystemMessage } = require('../core/NexusMessaging');
|
|
10
10
|
const { createAssistant, switchAssistant } = require('../clinical');
|
|
11
11
|
|
|
12
|
+
const asBoolean = (value) => (typeof value === 'string' ? value === 'true' : !!value);
|
|
13
|
+
|
|
12
14
|
const stopAssistantController = async (req, res) => {
|
|
13
15
|
const { code, stop } = req.body;
|
|
14
16
|
if (!code) return res.status(400).json({ success: false, error: 'Code is required' });
|
|
15
17
|
|
|
16
|
-
const stopped =
|
|
18
|
+
const stopped = asBoolean(stop);
|
|
17
19
|
try {
|
|
18
20
|
await switchThreadStoppedStatus(code, stopped, { byOperator: true });
|
|
19
21
|
return res.status(200).json({ success: true, message: stopped ? 'Assistant stopped' : 'Assistant resumed' });
|
|
@@ -23,6 +25,32 @@ const stopAssistantController = async (req, res) => {
|
|
|
23
25
|
}
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
const observedSilence = (through) => {
|
|
29
|
+
if (through === undefined || through === null || through === '') return null;
|
|
30
|
+
const at = new Date(through);
|
|
31
|
+
if (Number.isNaN(at.getTime()) || at.getTime() > Date.now()) return undefined;
|
|
32
|
+
return at;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const doNotAlertController = async (req, res) => {
|
|
36
|
+
const { code, doNotAlert, through } = req.body;
|
|
37
|
+
if (!code) return res.status(400).json({ success: false, error: 'Code is required' });
|
|
38
|
+
|
|
39
|
+
const observed = observedSilence(through);
|
|
40
|
+
if (observed === undefined) return res.status(400).json({ success: false, error: 'through must be a past timestamp' });
|
|
41
|
+
|
|
42
|
+
const muted = asBoolean(doNotAlert);
|
|
43
|
+
try {
|
|
44
|
+
const result = await setThreadDoNotAlert(code, muted, observed);
|
|
45
|
+
if (!result.matchedCount) return res.status(404).json({ success: false, error: 'No conversation with that code' });
|
|
46
|
+
logger.info('[AssistantController] Alerts for a contact were switched', { code, doNotAlert: muted, through: observed });
|
|
47
|
+
return res.status(200).json({ success: true, message: muted ? 'Alerts muted for this contact' : 'Alerts restored for this contact' });
|
|
48
|
+
} catch (error) {
|
|
49
|
+
logger.error('[AssistantController] Failed to switch alerts for a contact', { error: error.message, code });
|
|
50
|
+
return res.status(500).json({ success: false, error: 'Failed to switch alerts for this contact' });
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
|
|
26
54
|
const addInsAssistantController = async (req, res) => {
|
|
27
55
|
const { code, instruction, triggeredBy } = req.body;
|
|
28
56
|
if (!code) return res.status(400).json({ success: false, error: 'Code is required' });
|
|
@@ -113,6 +141,7 @@ module.exports = {
|
|
|
113
141
|
addInsAssistantController,
|
|
114
142
|
addMsgAssistantController,
|
|
115
143
|
createAssistantController,
|
|
144
|
+
doNotAlertController,
|
|
116
145
|
getInfoAssistantController,
|
|
117
146
|
listAssistantController,
|
|
118
147
|
switchAssistantController,
|
|
@@ -12,9 +12,21 @@ async function botIsPaused(code, { label = '[pausedThread]' } = {}) {
|
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
async function alertsMutedFor(code, inboundAt, { label = '[pausedThread]' } = {}) {
|
|
16
|
+
if (!code || !inboundAt) return false;
|
|
17
|
+
try {
|
|
18
|
+
const thread = await Thread.findOne({ code }, { doNotAlertThrough: 1 }).lean();
|
|
19
|
+
if (!thread?.doNotAlertThrough) return false;
|
|
20
|
+
return new Date(inboundAt).getTime() <= new Date(thread.doNotAlertThrough).getTime();
|
|
21
|
+
} catch (error) {
|
|
22
|
+
logger.warn(`${label} could not read whether this silence was waved off; alerting anyway`, { code, error: error.message });
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
15
27
|
async function pausedSince(code, since) {
|
|
16
28
|
const paused = await Thread.findOne({ code, stopped: true, pausedAt: { $gt: since } }, { _id: 1 }).lean();
|
|
17
29
|
return Boolean(paused);
|
|
18
30
|
}
|
|
19
31
|
|
|
20
|
-
module.exports = { botIsPaused, pausedSince };
|
|
32
|
+
module.exports = { alertsMutedFor, botIsPaused, pausedSince };
|
|
@@ -11,6 +11,7 @@ const threadSchema = new mongoose.Schema({
|
|
|
11
11
|
nombre: { type: String, default: null },
|
|
12
12
|
stopped: { type: Boolean, default: false },
|
|
13
13
|
pausedAt: { type: Date, default: null },
|
|
14
|
+
doNotAlertThrough: { type: Date, default: null },
|
|
14
15
|
review: { type: Boolean, default: false },
|
|
15
16
|
version: { type: Number, default: null },
|
|
16
17
|
preset_id: { type: String, default: null },
|
package/lib/routes/index.js
CHANGED
|
@@ -7,7 +7,8 @@ const assistantRouteDefinitions = {
|
|
|
7
7
|
'POST /info': 'getInfoAssistantController',
|
|
8
8
|
'GET /list': 'listAssistantController',
|
|
9
9
|
'POST /switch': 'switchAssistantController',
|
|
10
|
-
'POST /stop': 'stopAssistantController'
|
|
10
|
+
'POST /stop': 'stopAssistantController',
|
|
11
|
+
'POST /do-not-alert': 'doNotAlertController'
|
|
11
12
|
};
|
|
12
13
|
|
|
13
14
|
const contingencyRouteDefinitions = {
|
|
@@ -183,6 +184,7 @@ const builtInControllers = {
|
|
|
183
184
|
listAssistantController: assistantController.listAssistantController,
|
|
184
185
|
switchAssistantController: assistantController.switchAssistantController,
|
|
185
186
|
stopAssistantController: assistantController.stopAssistantController,
|
|
187
|
+
doNotAlertController: assistantController.doNotAlertController,
|
|
186
188
|
|
|
187
189
|
// Contingency controllers
|
|
188
190
|
contingencyEventController: contingencyController.contingencyEventController,
|
|
@@ -340,6 +342,7 @@ module.exports = {
|
|
|
340
342
|
traceRoutes: traceRouteDefinitions,
|
|
341
343
|
escalationReviewRoutes: escalationReviewRouteDefinitions,
|
|
342
344
|
|
|
345
|
+
builtInControllers,
|
|
343
346
|
createRouter,
|
|
344
347
|
setupDefaultRoutes
|
|
345
348
|
};
|
|
@@ -3,7 +3,7 @@ const { logger } = require('../utils/logger');
|
|
|
3
3
|
const { isWithinHourWindow } = require('../utils/scheduleUtils');
|
|
4
4
|
const { createIntervalSweeper } = require('../utils/intervalSweeper');
|
|
5
5
|
const { Thread } = require('../models/threadModel');
|
|
6
|
-
const { botIsPaused } = require('../helpers/pausedThreadHelper');
|
|
6
|
+
const { alertsMutedFor, botIsPaused } = require('../helpers/pausedThreadHelper');
|
|
7
7
|
const { isWithin24HourWindow } = require('../helpers/conversationWindowHelper');
|
|
8
8
|
const { coldInboundMode, inboundRecoveryLive } = require('../clinical/flags/inboundRecoveryFlags');
|
|
9
9
|
const { formatOpsAlert, recordUnresolvedRequest } = require('../clinical/services/clinicalAirtableService');
|
|
@@ -41,6 +41,7 @@ async function unansweredThreads(now, { minAgeMs = MIN_AGE_MS, maxAgeMs = MAX_AG
|
|
|
41
41
|
stopped: false,
|
|
42
42
|
lastMessageFromMe: false,
|
|
43
43
|
lastMessageAt: { $gte: new Date(now.getTime() - maxAgeMs), $lte: new Date(now.getTime() - minAgeMs) },
|
|
44
|
+
$or: [{ doNotAlertThrough: null }, { $expr: { $lt: ['$doNotAlertThrough', '$lastMessageAt'] } }],
|
|
44
45
|
},
|
|
45
46
|
{ code: 1, lastMessageAt: 1 },
|
|
46
47
|
).sort({ lastMessageAt: 1 }).limit(limit).lean();
|
|
@@ -68,6 +69,7 @@ async function candidateCodes(now, { threadBand = null, failedMaxAgeMs = MAX_AGE
|
|
|
68
69
|
}
|
|
69
70
|
|
|
70
71
|
const threadIsOwnedByAHuman = (code) => botIsPaused(code, { label: '[inboundRecovery]' });
|
|
72
|
+
const silenceWasWavedOff = (code, inboundAt) => alertsMutedFor(code, inboundAt, { label: '[inboundRecovery]' });
|
|
71
73
|
|
|
72
74
|
async function stillUnanswered(code, now = new Date(), { minAgeMs = MIN_AGE_MS, maxAgeMs = MAX_AGE_MS } = {}) {
|
|
73
75
|
const [newest] = await getMessages(
|
|
@@ -113,6 +115,7 @@ async function sweepUnansweredInbound({
|
|
|
113
115
|
claims = recoveryClaims,
|
|
114
116
|
ticketGate = ticketClaims,
|
|
115
117
|
isPaused = threadIsOwnedByAHuman,
|
|
118
|
+
isMuted = silenceWasWavedOff,
|
|
116
119
|
alert = recordUnresolvedRequest,
|
|
117
120
|
} = {}) {
|
|
118
121
|
let codes;
|
|
@@ -128,7 +131,7 @@ async function sweepUnansweredInbound({
|
|
|
128
131
|
if (isProcessing(code)) continue;
|
|
129
132
|
try {
|
|
130
133
|
const inbound = await stillUnanswered(code, now);
|
|
131
|
-
if (inbound) candidates.push({ code, inbound });
|
|
134
|
+
if (inbound && !await isMuted(code, inbound.createdAt)) candidates.push({ code, inbound });
|
|
132
135
|
} catch (error) {
|
|
133
136
|
logger.error('[inboundRecovery] could not read one conversation; leaving it for the next pass', {
|
|
134
137
|
code, error: error.message,
|
|
@@ -154,7 +157,7 @@ async function sweepUnansweredInbound({
|
|
|
154
157
|
const key = String(inbound._id);
|
|
155
158
|
const ageMs = now.getTime() - new Date(inbound.createdAt).getTime();
|
|
156
159
|
|
|
157
|
-
if (await isPaused(code)) { skipped += 1; continue; }
|
|
160
|
+
if (await isPaused(code) || await isMuted(code, inbound.createdAt)) { skipped += 1; continue; }
|
|
158
161
|
|
|
159
162
|
const claim = await claims.acquire(key, 'handled');
|
|
160
163
|
if (claim === 'unavailable') {
|
|
@@ -224,6 +227,7 @@ async function sweepColdUnansweredInbound({
|
|
|
224
227
|
mode = coldInboundMode(),
|
|
225
228
|
isProcessing = () => false,
|
|
226
229
|
isPaused = threadIsOwnedByAHuman,
|
|
230
|
+
isMuted = silenceWasWavedOff,
|
|
227
231
|
ticketGate = ticketClaims,
|
|
228
232
|
coldGate = coldTicketClaims,
|
|
229
233
|
alert = recordUnresolvedRequest,
|
|
@@ -246,7 +250,7 @@ async function sweepColdUnansweredInbound({
|
|
|
246
250
|
if (isProcessing(code)) continue;
|
|
247
251
|
try {
|
|
248
252
|
const inbound = await stillUnanswered(code, now, COLD_BAND);
|
|
249
|
-
if (inbound && !await isPaused(code)) waiting.push({ code, inbound });
|
|
253
|
+
if (inbound && !await isPaused(code) && !await isMuted(code, inbound.createdAt)) waiting.push({ code, inbound });
|
|
250
254
|
} catch (error) {
|
|
251
255
|
logger.error('[inboundRecovery] could not read one cold conversation; leaving it for the next pass', {
|
|
252
256
|
code, error: error.message,
|
|
@@ -277,6 +281,7 @@ async function sweepColdUnansweredInbound({
|
|
|
277
281
|
for (const { code, inbound } of waiting) {
|
|
278
282
|
if ((await spent() ?? COLD_TICKETS_PER_DAY) >= COLD_TICKETS_PER_DAY) break;
|
|
279
283
|
const key = String(inbound._id);
|
|
284
|
+
if (await isMuted(code, inbound.createdAt)) continue;
|
|
280
285
|
if (await ticketGate.has(key) !== false) continue;
|
|
281
286
|
if (await coldGate.acquire(key, 'cold') !== 'acquired') continue;
|
|
282
287
|
if (await fileTicket(code, alert, now.getTime() - new Date(inbound.createdAt).getTime())) alerted += 1;
|
|
@@ -331,6 +336,7 @@ module.exports = {
|
|
|
331
336
|
DEFAULT_SWEEP_INTERVAL_MS,
|
|
332
337
|
stillUnanswered,
|
|
333
338
|
threadIsOwnedByAHuman,
|
|
339
|
+
silenceWasWavedOff,
|
|
334
340
|
_claims: recoveryClaims,
|
|
335
341
|
_ticketClaims: ticketClaims,
|
|
336
342
|
_coldTicketClaims: coldTicketClaims,
|
|
@@ -10,7 +10,6 @@ const CACHE_TTL_MS = 60 * 1000;
|
|
|
10
10
|
|
|
11
11
|
const CAROUSEL_DELIVERY_FIELDS = [
|
|
12
12
|
'twilio_sent',
|
|
13
|
-
'first_attmp_send',
|
|
14
13
|
'delivered',
|
|
15
14
|
'date_delivered_at',
|
|
16
15
|
'twilio_error',
|
|
@@ -56,7 +55,8 @@ function deliveryStatus(row) {
|
|
|
56
55
|
if (delivered === 'read' || delivered === 'leído' || delivered === 'leido') return 'read';
|
|
57
56
|
if (delivered === 'failed' || delivered === 'undelivered') return 'failed';
|
|
58
57
|
if (delivered) return 'delivered';
|
|
59
|
-
|
|
58
|
+
// pending/queued, or an attempt Twilio never confirmed, has not left yet.
|
|
59
|
+
if (String(firstOf(row.twilio_sent) || '').trim().toLowerCase() === 'sent') return 'sent';
|
|
60
60
|
return 'queued';
|
|
61
61
|
}
|
|
62
62
|
|
|
@@ -45,6 +45,8 @@ const switchThreadStoppedStatus = async (code, stopped, { byOperator = false } =
|
|
|
45
45
|
return result;
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
+
const setThreadDoNotAlert = async (code, doNotAlert, through = null) => Thread.updateMany({ code }, { $set: { doNotAlertThrough: doNotAlert ? (through || new Date()) : null } });
|
|
49
|
+
|
|
48
50
|
const setThreadPromptId = async (code, promptId) => {
|
|
49
51
|
const updateFields = {};
|
|
50
52
|
Thread.setAssistantId(updateFields, promptId);
|
|
@@ -103,5 +105,6 @@ module.exports = {
|
|
|
103
105
|
createPlaceholderThread,
|
|
104
106
|
ensureThreadExists,
|
|
105
107
|
switchThreadStoppedStatus,
|
|
108
|
+
setThreadDoNotAlert,
|
|
106
109
|
setThreadPromptId
|
|
107
110
|
};
|