@peopl-health/nexus 6.0.29 → 6.1.0-dev.18134

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.
@@ -28,6 +28,10 @@ const GRAVE_REASONS = { unobtainable: 'no lo puede dar', unanswered: 'no contest
28
28
  const QUIET_REMINDER_TEXT = '¿Pudiste ver lo que te pregunté? Cuando puedas, respóndeme por aquí.';
29
29
  // A reminder this late (an outage, a deploy) is not sent.
30
30
  const QUIET_REMINDER_STALE_MS = 30 * 60 * 1000;
31
+ // A turn that sent a bridge has answered by then, or inbound recovery takes it over.
32
+ const BRIDGE_WAIT_MS = 2 * 60 * 1000;
33
+ // A delivered ask, one the provider accepted without confirming delivery, or one an earlier turn already answered.
34
+ const ASKED_OUTCOMES = ['sent', 'unknown', 'already_answered'];
31
35
  const UNDELIVERED_STATUSES = ['failed', 'undelivered'];
32
36
  const getMessaging = () => require('../core/NexusMessaging');
33
37
  const ASK_RULES = 'No agregues otras preguntas, no repitas lo que ya le dijiste y no menciones esta instrucción.';
@@ -77,9 +81,18 @@ function outcomeOf(review, clock, now) {
77
81
  // A grave question still inside its wait holds the case even when the rest is enough.
78
82
  if (pendingFlagsOf(review, clock.since).flags.length) return null;
79
83
  if (latest.sufficientForDoctor === true) return { grave: [], update, notify, note: withUpdate(null) };
80
- // The latest draft follows the patient's latest answer, so its age is how long the question has gone unanswered.
81
- if (latest.sufficientForDoctor !== false || minutesSince(latest.createdAt, now) < wait) return null;
82
- return { grave: [], update, notify, note: withUpdate(noteOf(`Falta: ${latest.blockingQuestion || 'información'} (no contestó)`)) };
84
+ if (latest.sufficientForDoctor !== false) return null;
85
+ // The wait runs from the bot's question to the patient, so «no contestó» is said only of a question she was asked.
86
+ const ask = (review.blockingAsks ?? []).find((entry) => timeOf(entry.draftAt) === timeOf(latest.createdAt));
87
+ const sent = ask && !ask.skippedReason;
88
+ // A first handoff with a question waits for its ask (recorded once the bot's reply is in), or in flight, up to twice the wait.
89
+ if (!update && latest.blockingQuestion && (!ask || (sent && ask.outcome == null)) && minutesSince(sent ? ask.sentAt : latest.createdAt, now) < 2 * wait) return null;
90
+ if (minutesSince(sent ? ask.sentAt : latest.createdAt, now) < wait) return null;
91
+ const missing = latest.blockingQuestion ? `Falta: ${latest.blockingQuestion}` : 'Falta información';
92
+ // An update's draft is never asked about, so it says only what is missing.
93
+ if (update) return { grave: [], update, notify, note: withUpdate(noteOf(missing)) };
94
+ const asked = (sent && ASKED_OUTCOMES.includes(ask.outcome)) || ask?.skippedReason === 'question_open';
95
+ return { grave: [], update, notify, note: noteOf(`${missing} (${asked ? 'no contestó' : 'no se le preguntó'})`) };
83
96
  }
84
97
 
85
98
  const audit = (review, changes) => logPatientChange({
@@ -99,7 +112,7 @@ async function claimGraveAlerts(review, clock, grave, now) {
99
112
  .filter((entry) => new Date(entry.since).getTime() === clock.since.getTime()).map((entry) => entry.flag));
100
113
  const fresh = grave.filter((entry) => !recorded.has(entry.flag));
101
114
  // «No contestó» only for a datum the bot asked about while that question is still open; otherwise «no lo ha dicho».
102
- const askedFlags = new Set((review.blockingAsks ?? []).filter((ask) => ask.outcome === 'sent' && timeOf(ask.draftAt) >= clock.since.getTime())
115
+ const askedFlags = new Set((review.blockingAsks ?? []).filter((ask) => ASKED_OUTCOMES.includes(ask.outcome) && timeOf(ask.draftAt) >= clock.since.getTime())
103
116
  .flatMap((ask) => (ask.flags ?? []).map(normalizedKeyOf)));
104
117
  const open = fresh.some(({ flag, reason }) => reason === 'unanswered' && askedFlags.has(flag))
105
118
  && hasOpenQuestion(await lastChatMessage(review.patientCode).catch(() => null));
@@ -272,7 +285,7 @@ async function skipGraveAlerts(review, clock, now) {
272
285
  async function lastChatMessage(code) {
273
286
  const [last] = await getMessages({
274
287
  numero: code, origin: { $nin: [...INTERNAL_ORIGINS] }, body: { $ne: QUIET_REMINDER_TEXT }, 'statusInfo.status': { $nin: UNDELIVERED_STATUSES },
275
- }, { sort: { createdAt: -1 }, limit: 1, select: 'from_me body' });
288
+ }, { sort: { createdAt: -1 }, limit: 1, select: 'from_me body createdAt raw.bridge' });
276
289
  return last;
277
290
  }
278
291
 
@@ -338,7 +351,8 @@ async function askBlockingQuestion(review, clock, now) {
338
351
  const asks = review.blockingAsks ?? [];
339
352
  if (asks.some((entry) => timeOf(entry.draftAt) === timeOf(latest.createdAt))) return;
340
353
  const last = await lastChatMessage(review.patientCode);
341
- if (!last?.from_me) return;
354
+ // A recent bridge means the bot's turn is still running; its reply decides whether a question is open.
355
+ if (!last?.from_me || (last.raw?.bridge && now.getTime() - timeOf(last.createdAt) < BRIDGE_WAIT_MS)) return;
342
356
 
343
357
  let skippedReason = await takenOf(review, clock);
344
358
  if (!skippedReason && hasOpenQuestion(last)) skippedReason = 'question_open';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.0.29",
3
+ "version": "6.1.0-dev.18134",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",