@peopl-health/nexus 5.45.0-dev.7128 → 5.45.0-dev.7129
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.
|
@@ -6,7 +6,6 @@ const { isAllowedUrl, redactDirectIdentifiers } = require('../utils/sanitizerUti
|
|
|
6
6
|
const { getPatientInformation, updatePatientInformation, logPatientChange } = require('../services/patientService');
|
|
7
7
|
const { getTriageHistory } = require('../services/triageHistoryService');
|
|
8
8
|
const { getCarePlanHistory } = require('../services/carePlanHistoryService');
|
|
9
|
-
const { getEscalationContext } = require('../services/escalationContextService');
|
|
10
9
|
const { getPatientTasks, updatePatientTask } = require('../services/patientTaskService');
|
|
11
10
|
const {
|
|
12
11
|
getRecommendationCarousels,
|
|
@@ -242,25 +241,10 @@ const logPatientChangeController = async (req, res) => {
|
|
|
242
241
|
}
|
|
243
242
|
};
|
|
244
243
|
|
|
245
|
-
const getPatientEscalationContextController = async (req, res) => {
|
|
246
|
-
try {
|
|
247
|
-
const context = await getEscalationContext({ code: req.params.code });
|
|
248
|
-
res.status(200).json({ success: true, context });
|
|
249
|
-
} catch (error) {
|
|
250
|
-
logger.error('Error fetching patient escalation context:', {
|
|
251
|
-
error: redactDirectIdentifiers(String(error.message || '')),
|
|
252
|
-
stack: redactDirectIdentifiers(String(error.stack || '')),
|
|
253
|
-
code: redactDirectIdentifiers(String(req.params?.code || '')),
|
|
254
|
-
});
|
|
255
|
-
res.status(error.statusCode || 500).json({ success: false, error: error.message });
|
|
256
|
-
}
|
|
257
|
-
};
|
|
258
|
-
|
|
259
244
|
module.exports = {
|
|
260
245
|
getPatientInfoController,
|
|
261
246
|
getPatientTriageInfoController,
|
|
262
247
|
getPatientCarePlanController,
|
|
263
|
-
getPatientEscalationContextController,
|
|
264
248
|
getPatientTasksController,
|
|
265
249
|
getPatientRecommendationCarouselsController,
|
|
266
250
|
getPatientRecommendationCarouselImageController,
|
package/lib/routes/index.js
CHANGED
|
@@ -70,7 +70,6 @@ const patientRouteDefinitions = {
|
|
|
70
70
|
'GET /:code': 'getPatientInfoController',
|
|
71
71
|
'GET /triage/:code': 'getPatientTriageInfoController',
|
|
72
72
|
'GET /careplan/:code': 'getPatientCarePlanController',
|
|
73
|
-
'GET /escalation-context/:code': 'getPatientEscalationContextController',
|
|
74
73
|
'GET /tasks/:code': 'getPatientTasksController',
|
|
75
74
|
'PATCH /:code': 'updatePatientController',
|
|
76
75
|
'PATCH /tasks/:code/:kind/:recordId': 'updatePatientTaskController',
|
|
@@ -215,7 +214,6 @@ const builtInControllers = {
|
|
|
215
214
|
getPatientInfoController: patientController.getPatientInfoController,
|
|
216
215
|
getPatientTriageInfoController: patientController.getPatientTriageInfoController,
|
|
217
216
|
getPatientCarePlanController: patientController.getPatientCarePlanController,
|
|
218
|
-
getPatientEscalationContextController: patientController.getPatientEscalationContextController,
|
|
219
217
|
getPatientTasksController: patientController.getPatientTasksController,
|
|
220
218
|
getPatientRecommendationCarouselsController: patientController.getPatientRecommendationCarouselsController,
|
|
221
219
|
getPatientRecommendationCarouselImageController: patientController.getPatientRecommendationCarouselImageController,
|
package/package.json
CHANGED
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
const { isoDay } = require('../utils/dateUtils');
|
|
2
|
-
const { logger } = require('../utils/logger');
|
|
3
|
-
const { PATIENT_CODE, redactDirectIdentifiers } = require('../utils/sanitizerUtils');
|
|
4
|
-
const { withTimeout } = require('../utils/timeoutUtils');
|
|
5
|
-
const { ensureWhatsAppFormat } = require('../helpers/twilioHelper');
|
|
6
|
-
const { assembleHistory } = require('../clinical/services/patientHistoryService');
|
|
7
|
-
const { DefaultMemoryManager } = require('../clinical/memory/DefaultMemoryManager');
|
|
8
|
-
|
|
9
|
-
const { getCarePlanHistory } = require('./carePlanHistoryService');
|
|
10
|
-
const { getPatientInformation } = require('./patientService');
|
|
11
|
-
const { getTriageHistory } = require('./triageHistoryService');
|
|
12
|
-
|
|
13
|
-
const SECTION_TIMEOUT_MS = 10000;
|
|
14
|
-
const TRIAGE_WINDOW_DAYS = 30;
|
|
15
|
-
const CARE_PLAN_LIMIT = 5;
|
|
16
|
-
const HISTORY_SCOPE = ['closed_cases_90d', 'recurrence_patterns', 'recent_medications', 'patient_reported_procedures'];
|
|
17
|
-
|
|
18
|
-
const httpError = (statusCode, message) => Object.assign(new Error(message), { statusCode });
|
|
19
|
-
|
|
20
|
-
function sectionReaders(code, now) {
|
|
21
|
-
const memoryManager = new DefaultMemoryManager();
|
|
22
|
-
const triageStart = isoDay(new Date(now - TRIAGE_WINDOW_DAYS * 86400000).toISOString());
|
|
23
|
-
return {
|
|
24
|
-
agentContext: () => memoryManager.getClinicalData(code),
|
|
25
|
-
conversation: () => memoryManager.buildContext({ thread: { code } }),
|
|
26
|
-
triages: () => getTriageHistory({ code, startDate: triageStart }),
|
|
27
|
-
carePlan: () => getCarePlanHistory({ code, limit: CARE_PLAN_LIMIT }),
|
|
28
|
-
history: () => assembleHistory(code, { scope: HISTORY_SCOPE }),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async function getEscalationContext({ code, now = Date.now(), timeoutMs = SECTION_TIMEOUT_MS } = {}) {
|
|
33
|
-
const patientCode = ensureWhatsAppFormat(String(code ?? '').trim());
|
|
34
|
-
if (!PATIENT_CODE.test(patientCode)) throw httpError(400, 'A valid WhatsApp ID is required');
|
|
35
|
-
|
|
36
|
-
const patient = await withTimeout(getPatientInformation(patientCode), timeoutMs, 'patient lookup');
|
|
37
|
-
if (!patient) throw httpError(404, 'Patient not found');
|
|
38
|
-
|
|
39
|
-
const readers = sectionReaders(patientCode, now);
|
|
40
|
-
const names = Object.keys(readers);
|
|
41
|
-
const outcomes = await Promise.allSettled(names.map((name) => withTimeout(readers[name](), timeoutMs, name)));
|
|
42
|
-
|
|
43
|
-
const context = { patientCode, asOf: new Date(now).toISOString(), degradedSections: [] };
|
|
44
|
-
outcomes.forEach((outcome, index) => {
|
|
45
|
-
const name = names[index];
|
|
46
|
-
if (outcome.status === 'fulfilled' && outcome.value != null) {
|
|
47
|
-
context[name] = outcome.value;
|
|
48
|
-
return;
|
|
49
|
-
}
|
|
50
|
-
context[name] = null;
|
|
51
|
-
context.degradedSections.push(name);
|
|
52
|
-
if (outcome.status === 'rejected') {
|
|
53
|
-
logger.error('[escalationContextService] Section reader failed', {
|
|
54
|
-
code: patientCode,
|
|
55
|
-
section: name,
|
|
56
|
-
error: redactDirectIdentifiers(String(outcome.reason?.message ?? outcome.reason)),
|
|
57
|
-
stack: redactDirectIdentifiers(String(outcome.reason?.stack ?? '')),
|
|
58
|
-
});
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
if (context.conversation) {
|
|
63
|
-
context.conversation = context.conversation
|
|
64
|
-
.filter((item) => item.type === 'message')
|
|
65
|
-
.map(({ role, content }) => ({ role, content }));
|
|
66
|
-
}
|
|
67
|
-
if (context.triages) {
|
|
68
|
-
if (context.triages.pagination?.hasNext) context.degradedSections.push('triages');
|
|
69
|
-
context.triages = context.triages.records;
|
|
70
|
-
}
|
|
71
|
-
if (context.carePlan) {
|
|
72
|
-
const { records, pagination = null, currentState = null } = context.carePlan;
|
|
73
|
-
const { degradedSections: carePlanDegraded = [], ...state } = currentState || {};
|
|
74
|
-
context.carePlan = { records, pagination, currentState: currentState ? state : null };
|
|
75
|
-
for (const section of carePlanDegraded) context.degradedSections.push(`carePlan.${section}`);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return context;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
module.exports = { getEscalationContext };
|