@peopl-health/nexus 5.75.0 → 5.75.2
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.
|
@@ -35,6 +35,14 @@ const triageRecommendationEventSchema = new mongoose.Schema({
|
|
|
35
35
|
gateLatencyMs: { type: Number, default: null },
|
|
36
36
|
planLatencyMs: { type: Number, default: null },
|
|
37
37
|
planMaxOutputTokens: { type: Number, default: null },
|
|
38
|
+
planRetryReasons: {
|
|
39
|
+
type: [new mongoose.Schema({
|
|
40
|
+
attempt: { type: Number, required: true },
|
|
41
|
+
reason: { type: String, required: true },
|
|
42
|
+
paths: { type: [String], default: undefined },
|
|
43
|
+
}, { _id: false })],
|
|
44
|
+
default: undefined,
|
|
45
|
+
},
|
|
38
46
|
totalLatencyMs: { type: Number, default: null },
|
|
39
47
|
triageStored: { type: Boolean, default: null },
|
|
40
48
|
unmappedAnswerCount: { type: Number, default: null },
|
|
@@ -60,7 +60,7 @@ const CONVERSATIONAL_REQUEST = 'conversational';
|
|
|
60
60
|
const DELIVERY_NUDGE = 'sistema: el turno no puede cerrar sin un mensaje para el paciente. Llama ahora a DeliverPatientMessage con message_text en español. No repitas herramientas clínicas ya ejecutadas en este turno.';
|
|
61
61
|
const MAX_DELIVERY_NUDGES = 2;
|
|
62
62
|
const OUTREACH_DECISION_TOOL = 'submitOutreachDecision';
|
|
63
|
-
const OUTREACH_NUDGE = 'sistema: este turno de contacto proactivo no puede cerrar incompleto. Si aún no decidiste, llama a submitOutreachDecision. Si ya registraste write, NO repitas esa decisión: llama ahora a DeliverPatientMessage. Un hold
|
|
63
|
+
const OUTREACH_NUDGE = 'sistema: este turno de contacto proactivo no puede cerrar incompleto. Si aún no decidiste, llama a submitOutreachDecision. Si ya registraste write, NO repitas esa decisión: llama ahora a DeliverPatientMessage. Si registraste hold con next_check_at, ese hold no cierra hasta que lo programes: llama ahora a schedulePatientReminder con reminder_date igual a ese next_check_at; si rechaza esa hora, elige una que acepte y registra de nuevo el hold con esa misma hora. Si ya no hace falta volver a preguntar, registra de nuevo el hold con next_check_at null: contingency_already_armed solo si otro plan activo lo cubre; si no, usa el hold_reason que corresponda y, si es una red, resuélvela con setContingencyPlan. No cambies a write solo para poder cerrar. Un hold sin next_check_at sí puede cerrar sin mensaje.';
|
|
64
64
|
const DELIVERY_FALLBACK_MESSAGES = [
|
|
65
65
|
'Gracias por tu mensaje. Si necesitas apoyo, escríbeme cuando quieras.',
|
|
66
66
|
'Gracias por escribirme. Aquí estoy cuando necesites algo.',
|
|
@@ -610,6 +610,7 @@ async function generateTriageRecommendations({ payload, provider, runId = null,
|
|
|
610
610
|
settle.planLatencyMs = Date.now() - planStart;
|
|
611
611
|
settle.attempts = planOutcome.attempts;
|
|
612
612
|
settle.planMaxOutputTokens = planOutcome.planMaxOutputTokens;
|
|
613
|
+
if (planOutcome.retryReasons.length) settle.planRetryReasons = planOutcome.retryReasons;
|
|
613
614
|
if (planOutcome.safetyUncertain) settle.supportSafetyCleared = false;
|
|
614
615
|
if (planOutcome.failure) return finish({ ...planOutcome.failure, autoRepairs: [...gateRepairs, ...(planOutcome.repairs || [])] });
|
|
615
616
|
const { plan, planRepairs, qualityWarnings } = planOutcome;
|
|
@@ -863,12 +864,21 @@ async function runGate({ provider, presetId, casoBase, routeFloor, rid, code, pa
|
|
|
863
864
|
return { attempts, gate, gateRepairs, gateWarnings };
|
|
864
865
|
}
|
|
865
866
|
|
|
867
|
+
// Validator issue paths only (never the problem text), indices collapsed so retries aggregate.
|
|
868
|
+
function issuePaths(issues) {
|
|
869
|
+
return [...new Set(issues.map((issue) => String(issue.path || '').replace(/\[[^\]]*\]/g, '[]')).filter(Boolean))];
|
|
870
|
+
}
|
|
871
|
+
|
|
866
872
|
async function runPlan({ provider, presetId, casoBase, gate, boundary = null, rankContext = {}, rid, code, payload }) {
|
|
867
873
|
const planInput = `${casoBase}\n\n### Gate de seguridad comprometido (PASO 1)\n${JSON.stringify(buildLegacyStep1(gate))}`;
|
|
868
874
|
let lastIssues = [];
|
|
869
875
|
let safetyUncertain = false;
|
|
870
876
|
let maxOutputTokens = null;
|
|
871
|
-
const
|
|
877
|
+
const retryReasons = [];
|
|
878
|
+
const retried = (attempt, reason, issues = null) => {
|
|
879
|
+
retryReasons.push(issues ? { attempt, reason, paths: issuePaths(issues) } : { attempt, reason });
|
|
880
|
+
};
|
|
881
|
+
const outcome = (result) => ({ ...result, safetyUncertain, planMaxOutputTokens: maxOutputTokens, retryReasons });
|
|
872
882
|
const buildPlan = (takeaways, elements) => new RecommendationPlan({
|
|
873
883
|
runId: rid,
|
|
874
884
|
patientId: code,
|
|
@@ -906,13 +916,16 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
906
916
|
} catch {
|
|
907
917
|
lastIssues = [];
|
|
908
918
|
if (last) return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_provider_failed' } });
|
|
919
|
+
retried(attempt, 'plan_provider_failed');
|
|
909
920
|
continue;
|
|
910
921
|
}
|
|
911
922
|
if (result?.status && result.status !== 'completed') {
|
|
912
923
|
lastIssues = [{ path: 'plan', problem: `respuesta incompleta del proveedor (status=${result.status})` }];
|
|
913
924
|
firstEvidence = firstEvidence ?? rawEvidence(result);
|
|
914
925
|
if (last) return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_incomplete', hardRejectIssues: lastIssues, rawExcerpt: firstEvidence } });
|
|
915
|
-
|
|
926
|
+
const isTruncated = hitTokenLimit(result);
|
|
927
|
+
if (isTruncated) maxOutputTokens = truncationBudget(result, maxOutputTokens);
|
|
928
|
+
retried(attempt, isTruncated ? 'plan_truncated' : 'plan_incomplete');
|
|
916
929
|
continue;
|
|
917
930
|
}
|
|
918
931
|
const parsed = safeParse(readStructuredText(result), null);
|
|
@@ -920,6 +933,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
920
933
|
if (!isPlainObject(parsed)) {
|
|
921
934
|
lastIssues = [{ path: 'plan', problem: 'la respuesta no es un objeto JSON válido' }];
|
|
922
935
|
if (last) return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_malformed', rawExcerpt: firstEvidence } });
|
|
936
|
+
retried(attempt, 'plan_malformed');
|
|
923
937
|
continue;
|
|
924
938
|
}
|
|
925
939
|
const rawPlan = isPlainObject(parsed.input) ? parsed.input : parsed;
|
|
@@ -928,7 +942,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
928
942
|
const rawElements = arr(arrayArg(rawPlan.elements));
|
|
929
943
|
if (!takeaways || !rawElements.length) {
|
|
930
944
|
lastIssues = [{ path: 'elements', problem: 'takeaways y elements[] no vacío son obligatorios' }];
|
|
931
|
-
if (attempt < PLAN_MAX_ATTEMPTS) continue;
|
|
945
|
+
if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_empty'); continue; }
|
|
932
946
|
return outcome({ attempts: attempt, failure: { status: 'failed', stage: 'plan', reason: 'plan_empty', rawExcerpt: firstEvidence } });
|
|
933
947
|
}
|
|
934
948
|
|
|
@@ -940,7 +954,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
940
954
|
deniedTargetsStripped = stripped;
|
|
941
955
|
if (!elements.length) {
|
|
942
956
|
lastIssues = [{ path: 'elements', problem: 'ningún elemento nombra un síntoma reportado en este triaje; construye el plan sobre los síntomas que el paciente sí reportó hoy' }];
|
|
943
|
-
if (attempt < PLAN_MAX_ATTEMPTS) continue;
|
|
957
|
+
if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_all_denied'); continue; }
|
|
944
958
|
return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_all_denied', hardRejectIssues: lastIssues } });
|
|
945
959
|
}
|
|
946
960
|
rankReordered = applyRankOrder(elements, gate, repairs, rankContext);
|
|
@@ -948,7 +962,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
948
962
|
const blocking = blockingIssues(issues);
|
|
949
963
|
if (blocking.length) {
|
|
950
964
|
lastIssues = blocking;
|
|
951
|
-
if (attempt < PLAN_MAX_ATTEMPTS) continue;
|
|
965
|
+
if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_invalid', blocking); continue; }
|
|
952
966
|
return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_invalid', hardRejectIssues: blocking } });
|
|
953
967
|
}
|
|
954
968
|
|
|
@@ -957,7 +971,7 @@ async function runPlan({ provider, presetId, casoBase, gate, boundary = null, ra
|
|
|
957
971
|
plan = buildPlan(takeaways, elements);
|
|
958
972
|
} catch (err) {
|
|
959
973
|
lastIssues = [{ path: 'plan', problem: err?.message || 'DTO inválido' }];
|
|
960
|
-
if (attempt < PLAN_MAX_ATTEMPTS) continue;
|
|
974
|
+
if (attempt < PLAN_MAX_ATTEMPTS) { retried(attempt, 'plan_dto_invalid'); continue; }
|
|
961
975
|
return outcome({ attempts: attempt, repairs, failure: { status: 'failed', stage: 'validate', reason: 'plan_invalid', hardRejectIssues: lastIssues } });
|
|
962
976
|
}
|
|
963
977
|
return outcome({
|