@peopl-health/nexus 5.71.30 → 5.71.31
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/clinical/services/conditionCloseService.js +8 -2
- package/lib/clinical/tools/recordClinicalImpressionTool.js +3 -1
- package/lib/clinical/tools/submitRoutingDecisionTool.js +20 -2
- package/lib/clinical/tools/updateConditionStatusTool.js +3 -1
- package/lib/fhir/index.js +2 -1
- package/lib/fhir/services/routingService.js +51 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { readContingencies, readClusters, storeCluster } = require('../../fhir');
|
|
1
|
+
const { closeRoutingDispositions, readContingencies, readClusters, storeCluster } = require('../../fhir');
|
|
2
2
|
const { RESOLUTION_CLOSE_REASONS } = require('../../shared/dtos/ManagedSymptom');
|
|
3
3
|
const { RESOLVED_REASONS } = require('../../shared/dtos/ContingencySafetyNet');
|
|
4
4
|
const { ClusterImpression } = require('../../shared/dtos/ClusterImpression');
|
|
@@ -58,7 +58,7 @@ function closePatch({ closeReason, resolving, reasoning }) {
|
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
async function runCloseCascade(patientId, caseId, turnId, trace
|
|
61
|
+
async function runCloseCascade(patientId, caseId, turnId, trace, sourceTool, { closeReason }) {
|
|
62
62
|
const autoResolvedPlans = [];
|
|
63
63
|
const prunedClusters = [];
|
|
64
64
|
const dissolvedClusters = [];
|
|
@@ -74,6 +74,12 @@ async function runCloseCascade(patientId, caseId, turnId, trace = null, sourceTo
|
|
|
74
74
|
cascadeErrors.push(`contingency_auto_resolve_failed: ${error?.message || error}`);
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
+
try {
|
|
78
|
+
await closeRoutingDispositions({ patientId, caseId, closeReason, resolving: isResolutionReason(closeReason) });
|
|
79
|
+
} catch (error) {
|
|
80
|
+
cascadeErrors.push(`routing_disposition_close_failed: ${error?.message || error}`);
|
|
81
|
+
}
|
|
82
|
+
|
|
77
83
|
const clusters = await readClusters({ patientId, status: 'active' }).catch((error) => {
|
|
78
84
|
cascadeErrors.push(`cluster_cascade_failed: ${error?.message || error}`);
|
|
79
85
|
return [];
|
|
@@ -321,7 +321,9 @@ async function handler(args = {}, context = {}) {
|
|
|
321
321
|
const dissolvedClusters = [];
|
|
322
322
|
if (event === 'close') {
|
|
323
323
|
try {
|
|
324
|
-
const cascade = await runCloseCascade(patientId, caseId, runtime.turnId, runtime.trace || null, 'recordClinicalImpression'
|
|
324
|
+
const cascade = await runCloseCascade(patientId, caseId, runtime.turnId, runtime.trace || null, 'recordClinicalImpression', {
|
|
325
|
+
closeReason: result.condition.closeReason,
|
|
326
|
+
});
|
|
325
327
|
autoResolvedPlans.push(...cascade.autoResolvedPlans);
|
|
326
328
|
prunedClusters.push(...cascade.prunedClusters);
|
|
327
329
|
dissolvedClusters.push(...cascade.dissolvedClusters);
|
|
@@ -3,7 +3,9 @@ const crypto = require('node:crypto');
|
|
|
3
3
|
const { clinicalIntakeRequiresRisk, hasAlternateTerminalCommit, validateRoutingPrerequisites } = require('../helpers/clinicalWorkflowGuard');
|
|
4
4
|
const { MEDICATION_NAMING_RULE } = require('../helpers/medicationNamingRule');
|
|
5
5
|
const { attachRequiredSkill } = require('../helpers/skillActivationHelper');
|
|
6
|
-
const {
|
|
6
|
+
const { isResolutionReason } = require('../services/conditionCloseService');
|
|
7
|
+
const { closeRoutingDispositions, storeRoutingDecision, readRoutingDecisions, readSymptomCases } = require('../../fhir');
|
|
8
|
+
const { CLOSED_STATUSES } = require('../../shared/dtos/ManagedSymptom');
|
|
7
9
|
const { RoutingDecision } = require('../../shared/dtos/RoutingDecision');
|
|
8
10
|
const { foldAccents } = require('../../utils/formatUtils');
|
|
9
11
|
const { logger } = require('../../utils/logger');
|
|
@@ -49,6 +51,20 @@ const _COMPOSE_SYNONYMS = {
|
|
|
49
51
|
separate: 'separate_if_space',
|
|
50
52
|
};
|
|
51
53
|
|
|
54
|
+
// The case is already closed (often earlier this same turn): its disposition is born closed, as the cascade
|
|
55
|
+
// would have left it.
|
|
56
|
+
async function closeDispositionForClosedCase({ patientId, turnId, caseId, cases }) {
|
|
57
|
+
const condition = caseId ? cases.find((c) => c.condition.conditionId === caseId)?.condition : null;
|
|
58
|
+
if (!CLOSED_STATUSES.includes(condition?.clinicalStatus) || !condition.closeReason) return;
|
|
59
|
+
try {
|
|
60
|
+
await closeRoutingDispositions({
|
|
61
|
+
patientId, caseId, closeReason: condition.closeReason, resolving: isResolutionReason(condition.closeReason),
|
|
62
|
+
});
|
|
63
|
+
} catch (error) {
|
|
64
|
+
logger.warn('[submitRoutingDecision] could not close the disposition of an already-closed case', { patientId, turnId, caseId, error: error?.message });
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
52
68
|
const _ACTION_TO_DISPOSITION_KIND = {
|
|
53
69
|
emergency_escalation: 'task',
|
|
54
70
|
team_escalation: 'task',
|
|
@@ -398,8 +414,9 @@ async function handler(args = {}, context = {}) {
|
|
|
398
414
|
}
|
|
399
415
|
|
|
400
416
|
const namedCaseIds = [...new Set([...traceResult.findings.map((f) => f.caseId), plan.primaryCaseId].filter(Boolean))];
|
|
417
|
+
let cases = [];
|
|
401
418
|
if (namedCaseIds.length) {
|
|
402
|
-
|
|
419
|
+
cases = await readSymptomCases({ patientId });
|
|
403
420
|
const known = new Set(cases.map((c) => c.condition.conditionId));
|
|
404
421
|
const unknown = namedCaseIds.filter((id) => !known.has(id));
|
|
405
422
|
if (unknown.length) {
|
|
@@ -417,6 +434,7 @@ async function handler(args = {}, context = {}) {
|
|
|
417
434
|
|
|
418
435
|
const decision = buildDecision({ patientId, turnId, plan, findings: traceResult.findings, now: new Date().toISOString() });
|
|
419
436
|
await storeRoutingDecision({ patientId, routingDecision: decision });
|
|
437
|
+
await closeDispositionForClosedCase({ patientId, turnId, caseId: plan.primaryCaseId, cases });
|
|
420
438
|
|
|
421
439
|
runtime.trace.setSignals({ branchTaken: 'ai_agent' });
|
|
422
440
|
|
|
@@ -165,7 +165,9 @@ async function handler(args = {}, context = {}) {
|
|
|
165
165
|
const result = applyDormant(existing, reasoning, args, transition === 'resolve');
|
|
166
166
|
if (typeof result === 'string') return result;
|
|
167
167
|
await storeSymptomCase({ patientId, managedSymptom: result });
|
|
168
|
-
const cascade = await runCloseCascade(patientId, result.condition.conditionId, runtime.turnId, runtime.trace || null
|
|
168
|
+
const cascade = await runCloseCascade(patientId, result.condition.conditionId, runtime.turnId, runtime.trace || null, 'updateConditionStatus', {
|
|
169
|
+
closeReason: result.condition.closeReason,
|
|
170
|
+
});
|
|
169
171
|
const data = {
|
|
170
172
|
case_id: result.condition.conditionId,
|
|
171
173
|
status: returnedStatus(result.condition),
|
package/lib/fhir/index.js
CHANGED
|
@@ -18,7 +18,7 @@ const { storeContingency, readContingencies } = require('./services/contingencyS
|
|
|
18
18
|
const { readDispatchedEscalations, readEscalationCases, storeEscalationCase } = require('./services/escalationService');
|
|
19
19
|
const { storeIntervention, readInterventions } = require('./services/interventionService');
|
|
20
20
|
const { storeCluster, readClusters } = require('./services/clusterService');
|
|
21
|
-
const { storeRoutingDecision, readRoutingDecisions } = require('./services/routingService');
|
|
21
|
+
const { closeRoutingDispositions, storeRoutingDecision, readRoutingDecisions } = require('./services/routingService');
|
|
22
22
|
const {
|
|
23
23
|
storePalliativeAssessment,
|
|
24
24
|
readPalliativeAssessments,
|
|
@@ -76,6 +76,7 @@ module.exports = {
|
|
|
76
76
|
readInterventions,
|
|
77
77
|
storeCluster,
|
|
78
78
|
readClusters,
|
|
79
|
+
closeRoutingDispositions,
|
|
79
80
|
storeRoutingDecision,
|
|
80
81
|
readRoutingDecisions,
|
|
81
82
|
storePalliativeAssessment,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
const { codeableConcept } = require('../helpers/elementHelper');
|
|
1
2
|
const { fhirId } = require('../helpers/fhirHelper');
|
|
2
|
-
const { ROUTING_ASSESSMENT_SLUG } = require('../constants/projectionSlugs');
|
|
3
|
+
const { ROUTING_ASSESSMENT_SLUG, ROUTING_DISPOSITION_SLUG } = require('../constants/projectionSlugs');
|
|
3
4
|
const { getFhirStore } = require('../stores/fhirStore');
|
|
4
5
|
const { toFhir, fromFhir } = require('../projections/routingProjection');
|
|
5
6
|
const { storeResources } = require('./fhirService');
|
|
@@ -12,6 +13,54 @@ async function storeRoutingDecision({ patientId, routingDecision }) {
|
|
|
12
13
|
return { count: stored.length, ids: stored };
|
|
13
14
|
}
|
|
14
15
|
|
|
16
|
+
const DISPOSITION_TYPES = ['Task', 'CommunicationRequest', 'RequestOrchestration', 'CarePlan'];
|
|
17
|
+
const OPEN_STATUSES = new Set(['draft', 'requested', 'received', 'accepted', 'ready', 'in-progress', 'on-hold', 'active']);
|
|
18
|
+
// Handing a case to the team carries these actions out; the bot's own follow-ups are withdrawn instead.
|
|
19
|
+
const ESCALATION_ACTIONS = new Set(['team_escalation', 'emergency_escalation', 'palliative_referral']);
|
|
20
|
+
|
|
21
|
+
const caseLinks = (resource) => [...(resource.reason || []), ...(resource.addresses || [])]
|
|
22
|
+
.map((link) => link.reference?.reference)
|
|
23
|
+
.filter(Boolean);
|
|
24
|
+
const primaryActionOf = (resource) => resource.code?.text || resource.action?.[0]?.title || null;
|
|
25
|
+
|
|
26
|
+
function closedStatus(resource, { closeReason, resolving }) {
|
|
27
|
+
if (resolving) return 'completed';
|
|
28
|
+
const done = closeReason === 'escalated_to_team' && ESCALATION_ACTIONS.has(primaryActionOf(resource));
|
|
29
|
+
if (resource.resourceType === 'Task') return done ? 'completed' : 'cancelled';
|
|
30
|
+
return done ? 'completed' : 'revoked';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function closedFields(resource, { closeReason, resolving }, now) {
|
|
34
|
+
const fields = { status: closedStatus(resource, { closeReason, resolving }) };
|
|
35
|
+
if (resource.resourceType === 'Task') {
|
|
36
|
+
fields.statusReason = { concept: codeableConcept(closeReason) };
|
|
37
|
+
fields.lastModified = now;
|
|
38
|
+
}
|
|
39
|
+
if (resource.resourceType === 'CommunicationRequest') fields.statusReason = codeableConcept(closeReason);
|
|
40
|
+
return fields;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function closeRoutingDispositions({ patientId, caseId, closeReason, resolving }) {
|
|
44
|
+
if (!closeReason) throw new Error('closeRoutingDispositions requires the case closeReason');
|
|
45
|
+
const store = getFhirStore();
|
|
46
|
+
const caseRef = `Condition/${fhirId(caseId)}`;
|
|
47
|
+
const dispositions = await store.find({
|
|
48
|
+
patient: `Patient/${fhirId(patientId)}`,
|
|
49
|
+
resourceType: DISPOSITION_TYPES,
|
|
50
|
+
identifierSlug: ROUTING_DISPOSITION_SLUG,
|
|
51
|
+
});
|
|
52
|
+
const open = dispositions.filter((resource) => OPEN_STATUSES.has(resource.status) && caseLinks(resource).includes(caseRef));
|
|
53
|
+
const now = new Date().toISOString();
|
|
54
|
+
const closed = [];
|
|
55
|
+
for (const resource of open) {
|
|
56
|
+
const { stored } = await storeResources([resource], {
|
|
57
|
+
merge: (prior) => (OPEN_STATUSES.has(prior.status) ? { ...prior, ...closedFields(prior, { closeReason, resolving }, now) } : prior),
|
|
58
|
+
});
|
|
59
|
+
closed.push(...stored);
|
|
60
|
+
}
|
|
61
|
+
return { closed };
|
|
62
|
+
}
|
|
63
|
+
|
|
15
64
|
async function readRoutingDecisions({ patientId, turnId = null }) {
|
|
16
65
|
const store = getFhirStore();
|
|
17
66
|
const patient = `Patient/${fhirId(patientId)}`;
|
|
@@ -31,6 +80,7 @@ async function readRoutingDecisions({ patientId, turnId = null }) {
|
|
|
31
80
|
}
|
|
32
81
|
|
|
33
82
|
module.exports = {
|
|
83
|
+
closeRoutingDispositions,
|
|
34
84
|
storeRoutingDecision,
|
|
35
85
|
readRoutingDecisions,
|
|
36
86
|
};
|