@peopl-health/nexus 5.56.17 → 5.56.18
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.
|
@@ -38,6 +38,8 @@ const triageRecommendationEventSchema = new mongoose.Schema({
|
|
|
38
38
|
droppedComboCount: { type: Number, default: null },
|
|
39
39
|
rankReordered: { type: Number, default: null },
|
|
40
40
|
classifierPending: { type: Boolean, default: null },
|
|
41
|
+
classifierOutcome: { type: String, enum: ['ready', 'timeout', 'no_free_text', null], default: null },
|
|
42
|
+
classifierWaitMs: { type: Number, default: null },
|
|
41
43
|
deniedCardsDropped: { type: Number, default: null },
|
|
42
44
|
deniedTargetsStripped: { type: Number, default: null },
|
|
43
45
|
adherenceRecorded: { type: Number, default: null },
|
|
@@ -72,6 +72,9 @@ const GATE_MAX_ATTEMPTS = 2;
|
|
|
72
72
|
const PLAN_MAX_ATTEMPTS = 2;
|
|
73
73
|
const DEFAULT_PROVIDER_RETRY_BACKOFF_MS = [15000, 45000];
|
|
74
74
|
const DEFAULT_SETTLE_WRITE_BACKOFF_MS = [1000, 5000];
|
|
75
|
+
const DEFAULT_CLASSIFIER_WAIT_MS = [15000, 30000, 45000, 60000, 90000];
|
|
76
|
+
const CLASSIFIER_WINDOW_MS = 4 * 60 * 1000;
|
|
77
|
+
const DEFAULT_CLASSIFIER_DEADLINE_MS = 5 * 60 * 1000;
|
|
75
78
|
const RAW_EXCERPT_MAX = 2000;
|
|
76
79
|
const PRIOR_TRIAGE_RX = /^rec[A-Za-z0-9]{14,}$/;
|
|
77
80
|
const PATIENT_LOOKUP_FIELD = 'patient_id (from triaje_symptoms) (from cluster_recommendations)';
|
|
@@ -247,15 +250,20 @@ async function generateTriageRecommendations({ payload, provider, runId = null,
|
|
|
247
250
|
const code = payload.patientCode;
|
|
248
251
|
const settle = { runId: rid };
|
|
249
252
|
|
|
250
|
-
const
|
|
253
|
+
const catalog = getCatalog();
|
|
254
|
+
const { triageResponse, unmappedQuestionIds } = buildTriageResponse(catalog, payload);
|
|
255
|
+
settle.unmappedAnswerCount = unmappedQuestionIds.length;
|
|
256
|
+
|
|
257
|
+
const hasFreeText = Boolean(triageResponse.otherSymptomsText || triageResponse.otherSymptomsNotProctcae.length
|
|
258
|
+
|| triageResponse.pain?.otherText);
|
|
259
|
+
const classifier = await awaitFreeTextClassifier(payload.triageRecordId, code, { hasFreeText });
|
|
251
260
|
if (classifier === null) {
|
|
252
261
|
return { ...settle, status: 'failed', stage: 'assemble', reason: 'triage_patient_mismatch' };
|
|
253
262
|
}
|
|
254
263
|
const freeTextDerivedSymptoms = classifier.terms;
|
|
255
264
|
settle.classifierPending = classifier.pending;
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
settle.unmappedAnswerCount = unmappedQuestionIds.length;
|
|
265
|
+
settle.classifierOutcome = classifier.outcome;
|
|
266
|
+
settle.classifierWaitMs = classifier.waitMs;
|
|
259
267
|
|
|
260
268
|
let supportContext = { triage: triageResponse, freeTextDerivedSymptoms };
|
|
261
269
|
let supportPromise;
|
|
@@ -536,7 +544,7 @@ async function generateTriageRecommendations({ payload, provider, runId = null,
|
|
|
536
544
|
affirmedKeys: reportedKeys,
|
|
537
545
|
riskAssessment,
|
|
538
546
|
riskReadFailed: riskRead.failed,
|
|
539
|
-
freeTextPending:
|
|
547
|
+
freeTextPending: classifier.outcome === 'timeout',
|
|
540
548
|
};
|
|
541
549
|
|
|
542
550
|
supportContext = { ...supportContext, case: casoBaseFor('support-routing') };
|
|
@@ -961,13 +969,47 @@ function stripOtcOffers(elements, repairs) {
|
|
|
961
969
|
|
|
962
970
|
// auna calls handleTriageCompleted directly, so the classifier's rows are resolved here rather
|
|
963
971
|
// than in the controller: 989 of 1002 production runs never pass through it.
|
|
972
|
+
async function awaitFreeTextClassifier(triageRecordId, code, { hasFreeText }) {
|
|
973
|
+
const startedAt = Date.now();
|
|
974
|
+
let read = await readFreeTextDerivedSymptoms(triageRecordId, code);
|
|
975
|
+
if (!hasFreeText) return read && { ...read, outcome: 'no_free_text', waitMs: 0 };
|
|
976
|
+
const schedule = backoffSchedule('TRIAGE_CLASSIFIER_WAIT_MS', DEFAULT_CLASSIFIER_WAIT_MS);
|
|
977
|
+
const deadlineMs = Number(runtimeConfig.get('TRIAGE_CLASSIFIER_DEADLINE_MS')) || DEFAULT_CLASSIFIER_DEADLINE_MS;
|
|
978
|
+
for (let tries = 0; read && !classifierSettled(read) && tries < schedule.length; tries += 1) {
|
|
979
|
+
if (Date.now() - startedAt + schedule[tries] > deadlineMs) break;
|
|
980
|
+
await sleep(schedule[tries], { keepAlive: true });
|
|
981
|
+
try {
|
|
982
|
+
read = await readFreeTextDerivedSymptoms(triageRecordId, code);
|
|
983
|
+
} catch (err) {
|
|
984
|
+
logger.warn('[triageRecommendation] free-text classifier re-read failed; keeping the last read', {
|
|
985
|
+
code, triageRecordId, error: err?.message,
|
|
986
|
+
});
|
|
987
|
+
}
|
|
988
|
+
}
|
|
989
|
+
if (!read) return read;
|
|
990
|
+
const outcome = classifierSettled(read) ? 'ready' : 'timeout';
|
|
991
|
+
if (outcome === 'timeout') {
|
|
992
|
+
logger.warn('[triageRecommendation] free-text classifier did not settle; cards built without its symptoms', {
|
|
993
|
+
code, triageRecordId, pending: read.pending, derived: read.derived,
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
return { ...read, outcome, waitMs: Date.now() - startedAt };
|
|
997
|
+
}
|
|
998
|
+
|
|
999
|
+
function classifierSettled(read) {
|
|
1000
|
+
if (read.pending !== false) return false;
|
|
1001
|
+
if (read.derived > 0) return true;
|
|
1002
|
+
const createdMs = Date.parse(read.createdAt || '');
|
|
1003
|
+
return !Number.isFinite(createdMs) || Date.now() - createdMs >= CLASSIFIER_WINDOW_MS;
|
|
1004
|
+
}
|
|
1005
|
+
|
|
964
1006
|
async function readFreeTextDerivedSymptoms(triageRecordId, code) {
|
|
965
|
-
if (!/^rec[A-Za-z0-9]+$/.test(triageRecordId || '')) return { terms: [], pending: null };
|
|
1007
|
+
if (!/^rec[A-Za-z0-9]+$/.test(triageRecordId || '')) return { terms: [], pending: null, derived: 0, createdAt: null };
|
|
966
1008
|
const rows = await getRecordByFilter(Symptoms_ID, 'triaje_symptoms', `RECORD_ID()='${triageRecordId}'`);
|
|
967
1009
|
const row = Array.isArray(rows) ? rows[0] : rows;
|
|
968
1010
|
if (!row) {
|
|
969
1011
|
logger.warn('[triageRecommendation] triage row unreadable; no classifier anchors', { code, triageRecordId });
|
|
970
|
-
return { terms: [], pending: null };
|
|
1012
|
+
return { terms: [], pending: null, derived: 0, createdAt: null };
|
|
971
1013
|
}
|
|
972
1014
|
const owner = Array.isArray(row.whatsapp_id) ? row.whatsapp_id[0] : row.whatsapp_id;
|
|
973
1015
|
if (owner && owner !== code) {
|
|
@@ -976,19 +1018,18 @@ async function readFreeTextDerivedSymptoms(triageRecordId, code) {
|
|
|
976
1018
|
}
|
|
977
1019
|
// The classifier writes its rows ~96 s after the triage: an unfinished one is not an empty one.
|
|
978
1020
|
const pending = row.symptoms_processed !== true;
|
|
1021
|
+
const createdAt = row.Created || null;
|
|
979
1022
|
const ids = (Array.isArray(row.symptoms) ? row.symptoms : []).filter((id) => /^rec[A-Za-z0-9]+$/.test(id || ''));
|
|
980
|
-
if (!ids.length) {
|
|
981
|
-
if (pending) logger.warn('[triageRecommendation] classifier had not finished; free-text symptoms unavailable', { code, triageRecordId });
|
|
982
|
-
return { terms: [], pending };
|
|
983
|
-
}
|
|
1023
|
+
if (!ids.length) return { terms: [], pending, derived: 0, createdAt };
|
|
984
1024
|
const formula = `OR(${ids.map((id) => `RECORD_ID()='${id}'`).join(',')})`;
|
|
985
1025
|
const symptoms = await getRecordByFilter(Symptoms_ID, 'symptoms', formula, null, FREE_TEXT_SYMPTOM_FIELDS, true);
|
|
986
|
-
const
|
|
987
|
-
|
|
1026
|
+
const derived = (Array.isArray(symptoms) ? symptoms : []).filter((symptom) => symptom.freetext_derived === true);
|
|
1027
|
+
const terms = derived
|
|
1028
|
+
.filter((symptom) => Number(symptom.grade_num) >= 1)
|
|
988
1029
|
.map((symptom) => (Array.isArray(symptom.corresponding_CTCAE) ? symptom.corresponding_CTCAE[0] : symptom.corresponding_CTCAE))
|
|
989
1030
|
.map((term) => String(term ?? '').trim())
|
|
990
1031
|
.filter(Boolean);
|
|
991
|
-
return { terms, pending };
|
|
1032
|
+
return { terms, pending, derived: derived.length, createdAt };
|
|
992
1033
|
}
|
|
993
1034
|
|
|
994
1035
|
async function readPriorResolutions(adherence = [], patientRecordId = null) {
|