@peopl-health/nexus 5.51.0-dev.7607 → 5.51.0-dev.7627
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/README.md +21 -0
- package/lib/clinical/AssistantProcessor.js +156 -12
- package/lib/clinical/context/ToolRuntimeContext.js +39 -1
- package/lib/clinical/context/turnContext.js +10 -0
- package/lib/clinical/models/supportCandidateModel.js +90 -0
- package/lib/clinical/models/triageRecommendationEventModel.js +29 -0
- package/lib/clinical/providers/BaseLLMProvider.js +45 -5
- package/lib/clinical/services/clinicalAirtableService.js +8 -5
- package/lib/clinical/services/deliverySettlementService.js +39 -0
- package/lib/clinical/services/divergenceService.js +9 -2
- package/lib/clinical/services/supportActionService.js +129 -0
- package/lib/clinical/services/supportCandidateService.js +470 -0
- package/lib/clinical/services/supportRoutingAgentService.js +118 -0
- package/lib/clinical/services/triageRecommendationService.js +456 -251
- package/lib/clinical/tools/deliverPatientMessageTool.js +82 -23
- package/lib/clinical/tools/registerClinicalTools.js +2 -0
- package/lib/clinical/tools/submitNutritionSafetyDecisionTool.js +200 -0
- package/lib/clinical/tools/submitSupportRoutingRecommendationTool.js +121 -0
- package/lib/controllers/dashboardController.js +5 -2
- package/lib/core/BatchingManager.js +20 -9
- package/lib/core/NexusMessaging.js +25 -6
- package/lib/core/ProcessingPipeline.js +32 -3
- package/lib/index.d.ts +325 -6
- package/lib/index.js +22 -0
- package/lib/services/airtableService.js +8 -1
- package/lib/services/dashboard-provider-registry.js +38 -0
- package/lib/services/dashboardService.js +30 -3
- package/lib/services/escalationReviewService.js +7 -7
- package/lib/shared/dtos/NutritionSafetyDecision.js +128 -0
- package/lib/shared/dtos/SupportAction.js +28 -0
- package/lib/shared/dtos/SupportRoutingRecommendation.js +124 -0
- package/package.json +1 -1
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const { readOutputText } = require('../helpers/clinicalMentionHelper');
|
|
2
|
+
const { SupportRoutingRecommendation } = require('../../shared/dtos/SupportRoutingRecommendation');
|
|
3
|
+
const { safeParse } = require('../../utils/jsonUtils');
|
|
4
|
+
|
|
5
|
+
const SUPPORT_ROUTING_SCHEMA = {
|
|
6
|
+
type: 'object',
|
|
7
|
+
additionalProperties: false,
|
|
8
|
+
properties: {
|
|
9
|
+
decisions: {
|
|
10
|
+
type: 'array',
|
|
11
|
+
maxItems: 2,
|
|
12
|
+
items: {
|
|
13
|
+
type: 'object',
|
|
14
|
+
additionalProperties: false,
|
|
15
|
+
properties: {
|
|
16
|
+
service: { type: 'string', enum: ['nutrition', 'psychology'] },
|
|
17
|
+
nutritionRisk: { type: ['string', 'null'], enum: ['green', 'yellow', 'red', 'unknown', null] },
|
|
18
|
+
psychologyRisk: { type: ['string', 'null'], enum: ['green', 'orange', 'red', 'unknown', null] },
|
|
19
|
+
disposition: { type: 'string', enum: ['group', 'individual', 'escalated', 'reviewRequired', 'none'] },
|
|
20
|
+
groupEligible: { type: 'boolean' },
|
|
21
|
+
urgentIntakeEligible: { type: ['boolean', 'null'] },
|
|
22
|
+
priority: { type: 'string', enum: ['routine', 'soon', 'urgent'] },
|
|
23
|
+
confidence: { type: 'number', minimum: 0, maximum: 1 },
|
|
24
|
+
reasonCodes: { type: 'array', maxItems: 20, items: { type: 'string' } },
|
|
25
|
+
evidence: {
|
|
26
|
+
type: 'array',
|
|
27
|
+
minItems: 1,
|
|
28
|
+
maxItems: 20,
|
|
29
|
+
items: {
|
|
30
|
+
type: 'object',
|
|
31
|
+
additionalProperties: false,
|
|
32
|
+
properties: {
|
|
33
|
+
source: { type: 'string', enum: ['triage', 'chat', 'fhir', 'airtable'] },
|
|
34
|
+
sourceId: { type: 'string' },
|
|
35
|
+
code: { type: 'string' },
|
|
36
|
+
summary: { type: ['string', 'null'] },
|
|
37
|
+
confidence: { type: ['number', 'null'], minimum: 0, maximum: 1 },
|
|
38
|
+
},
|
|
39
|
+
required: ['source', 'sourceId', 'code', 'summary', 'confidence'],
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
safety: {
|
|
43
|
+
type: 'object',
|
|
44
|
+
additionalProperties: false,
|
|
45
|
+
properties: {
|
|
46
|
+
suicidalIdeation: { type: 'boolean' },
|
|
47
|
+
immediateRisk: { type: 'boolean' },
|
|
48
|
+
urgentMedicalRisk: { type: 'boolean' },
|
|
49
|
+
needsHumanConfirmation: { type: 'boolean' },
|
|
50
|
+
},
|
|
51
|
+
required: ['suicidalIdeation', 'immediateRisk', 'urgentMedicalRisk', 'needsHumanConfirmation'],
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
required: ['service', 'nutritionRisk', 'psychologyRisk', 'disposition', 'groupEligible', 'urgentIntakeEligible', 'priority', 'confidence', 'reasonCodes', 'evidence', 'safety'],
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
required: ['decisions'],
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const INSTRUCTIONS = `Eres un clasificador clínico de derivación de soporte oncológico. Evalúa únicamente evidencia explícita del caso. Devuelve como máximo una decisión por servicio y omite servicios sin señal relevante.
|
|
62
|
+
|
|
63
|
+
PSICOLOGÍA — política obligatoria:
|
|
64
|
+
- green: aceptación inicial, expresión emocional con autorregulación, expectativas realistas, participación en decisiones, red de apoyo favorecida o comunicación de necesidades. disposition=group y groupEligible=true.
|
|
65
|
+
- orange: ansiedad/miedo a progresión, llanto intermitente, evitación parcial, alteración de autoimagen/identidad/rol, irritabilidad/hipervigilancia, dificultad para hablar del futuro, deficiencia parcial en red de apoyo o cansancio de “ser fuerte”. disposition=individual y groupEligible=false.
|
|
66
|
+
- red: llanto descontrolado, desesperanza/discurso catastrófico, pérdida de interés en continuar tratamiento, enojo/culpa intensa o ideación/plan suicida. disposition=escalated y groupEligible=false.
|
|
67
|
+
- unknown: información insuficiente o contradictoria. disposition=reviewRequired y groupEligible=false. Nunca infieras bienestar por ausencia de datos.
|
|
68
|
+
Clasifica según la señal de mayor riesgo observada: red prevalece sobre orange y green; orange prevalece sobre green.
|
|
69
|
+
Ideación suicida o riesgo inmediato siempre implica psychologyRisk=red, priority=urgent y disposition=escalated.
|
|
70
|
+
|
|
71
|
+
NUTRICIÓN:
|
|
72
|
+
- Señales pertinentes incluyen baja ingesta/apetito, pérdida de peso, disfagia, lesiones orales, alteración del gusto, náusea/vómito, diarrea, estreñimiento, reflujo o dolor abdominal.
|
|
73
|
+
- green: señal actual, manejable y educativa; disposition=group, groupEligible=true y urgentIntakeEligible=false.
|
|
74
|
+
- yellow: requiere resolución individual, pero sin riesgo médico urgente; disposition=individual y urgentIntakeEligible=true. groupEligible es independiente y puede ser true cuando también corresponde una indicación grupal.
|
|
75
|
+
- red con riesgo médico urgente: disposition=escalated, groupEligible=false y urgentIntakeEligible=false.
|
|
76
|
+
- unknown: evidencia insuficiente; disposition=reviewRequired, groupEligible=false y urgentIntakeEligible=false.
|
|
77
|
+
Clasifica según la señal de mayor riesgo observada. Los factores clínicos de base elevan prioridad, pero solos no crean necesidad actual.
|
|
78
|
+
|
|
79
|
+
Cada evidencia debe apuntar al sourceId recibido. No diagnostiques. No copies texto sensible innecesario: usa un resumen breve y reasonCodes estables en snake_case.`;
|
|
80
|
+
|
|
81
|
+
function normalizeDecision(decision, origin, sourceId) {
|
|
82
|
+
const normalized = {
|
|
83
|
+
...decision,
|
|
84
|
+
evidence: decision.evidence.map((item) => ({ ...item, source: origin, sourceId })),
|
|
85
|
+
};
|
|
86
|
+
if (decision.service === 'nutrition') {
|
|
87
|
+
delete normalized.psychologyRisk;
|
|
88
|
+
} else {
|
|
89
|
+
delete normalized.nutritionRisk;
|
|
90
|
+
delete normalized.urgentIntakeEligible;
|
|
91
|
+
}
|
|
92
|
+
return normalized;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
async function evaluateSupportRouting({ provider, patientId, origin, sourceId, context, createdAt = new Date().toISOString() }) {
|
|
96
|
+
if (!provider?.runStructured) throw new Error('support routing provider unavailable');
|
|
97
|
+
const result = await provider.runStructured({
|
|
98
|
+
instructions: INSTRUCTIONS,
|
|
99
|
+
input: [{ role: 'user', content: JSON.stringify({ origin, sourceId, context }) }],
|
|
100
|
+
text: { format: { type: 'json_schema', name: 'support_routing', strict: true, schema: SUPPORT_ROUTING_SCHEMA } },
|
|
101
|
+
metadata: { subAgent: 'support-routing', sourceId },
|
|
102
|
+
});
|
|
103
|
+
const parsed = safeParse(readOutputText(result), null);
|
|
104
|
+
if (!parsed || !Array.isArray(parsed.decisions)) throw new Error('support routing response malformed');
|
|
105
|
+
return new SupportRoutingRecommendation({
|
|
106
|
+
patientId,
|
|
107
|
+
origin,
|
|
108
|
+
sourceId,
|
|
109
|
+
createdAt,
|
|
110
|
+
decisions: parsed.decisions.map((decision) => normalizeDecision(decision, origin, sourceId)),
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
module.exports = {
|
|
115
|
+
INSTRUCTIONS,
|
|
116
|
+
SUPPORT_ROUTING_SCHEMA,
|
|
117
|
+
evaluateSupportRouting,
|
|
118
|
+
};
|