@peopl-health/nexus 5.72.1 → 5.72.3

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.
@@ -133,6 +133,8 @@ async function generateEscalationDraft({
133
133
  return { completed: false, attempted: true, reason: `generation incomplete: status=${result.status}` };
134
134
  }
135
135
 
136
+ // From here on a failure is the model's answer being refused, not the model being unreachable:
137
+ // `rejected` tells the caller that asking again right away is likely to succeed.
136
138
  const parsed = safeParse(readStructuredText(result), null);
137
139
  const patientMessage = trimmed(parsed?.patientMessage);
138
140
  // The schema is not strict, so the model sometimes sends the list JSON-encoded as a string.
@@ -144,15 +146,15 @@ async function generateEscalationDraft({
144
146
  symptomsType: Array.isArray(shape) ? 'array' : typeof shape,
145
147
  symptomsLength: Array.isArray(shape) || typeof shape === 'string' ? shape.length : null,
146
148
  });
147
- return { completed: false, attempted: true, reason: 'the model returned a malformed symptoms list' };
149
+ return { completed: false, attempted: true, rejected: true, reason: 'the model returned a malformed symptoms list' };
148
150
  }
149
151
  if (parsed?.symptoms !== undefined && blocks.some((block) => !trimmed(block?.symptom))) {
150
152
  logger.warn('[escalationDraft] a symptom block came without its symptom', { reviewId });
151
- return { completed: false, attempted: true, reason: 'the model returned a symptom block without its symptom' };
153
+ return { completed: false, attempted: true, rejected: true, reason: 'the model returned a symptom block without its symptom' };
152
154
  }
153
155
  if (blocks.some((block) => !trimmed(block?.characterizationSummary))) {
154
156
  logger.warn('[escalationDraft] generation did not return the sectioned doctor draft', { reviewId });
155
- return { completed: false, attempted: true, reason: 'the model did not return the sectioned doctor draft (characterizationSummary missing)' };
157
+ return { completed: false, attempted: true, rejected: true, reason: 'the model did not return the sectioned doctor draft (characterizationSummary missing)' };
156
158
  }
157
159
  const settled = blocks.map((block) => ({ block, ...settleProposal(block) }));
158
160
  const proposals = settled.map((entry) => entry.proposal).filter(Boolean);
@@ -171,7 +173,7 @@ async function generateEscalationDraft({
171
173
 
172
174
  if (issues.length) {
173
175
  logger.warn('[escalationDraft] the treatment proposal broke the formulary rules', { reviewId, issues });
174
- return { completed: false, attempted: true, reason: `the treatment proposal broke the formulary rules: ${issues.join('; ')}` };
176
+ return { completed: false, attempted: true, rejected: true, reason: `the treatment proposal broke the formulary rules: ${issues.join('; ')}` };
175
177
  }
176
178
 
177
179
  const draft = {
@@ -182,11 +184,11 @@ async function generateEscalationDraft({
182
184
 
183
185
  if (!doctorText || !patientMessage) {
184
186
  logger.warn('[escalationDraft] generation returned no usable text', { reviewId });
185
- return { completed: false, attempted: true, reason: 'the model returned no usable text' };
187
+ return { completed: false, attempted: true, rejected: true, reason: 'the model returned no usable text' };
186
188
  }
187
189
  if (TECHNICAL_TOKEN.test(`${composeDoctorBody({ ...draft, alarm: null })}\n${patientMessage}`)) {
188
190
  logger.warn('[escalationDraft] generation leaked a technical token into the text', { reviewId });
189
- return { completed: false, attempted: true, reason: 'the model wrote a field name or technical token into the doctor or patient text' };
191
+ return { completed: false, attempted: true, rejected: true, reason: 'the model wrote a field name or technical token into the doctor or patient text' };
190
192
  }
191
193
 
192
194
  return {
@@ -1347,7 +1347,7 @@ async function generateBotDraft({ id, lease = null } = {}) {
1347
1347
  }
1348
1348
  if (!draft.completed) {
1349
1349
  logger.warn('[EscalationReview] The automatic draft was not written', { escalationReviewId: String(review._id), reason: draft.reason });
1350
- return { review: null, retry: draft.configured !== false };
1350
+ return { review: null, retry: draft.configured !== false, rejected: draft.rejected === true };
1351
1351
  }
1352
1352
 
1353
1353
  const entry = {
@@ -8,6 +8,8 @@ const { BOOKKEEPING, DOCUMENTING_STATUSES, generateBotDraft } = require('./escal
8
8
  const SWEEP_LIMIT = 1;
9
9
  const DEFAULT_SWEEP_INTERVAL_MS = 10 * 1000;
10
10
  const RETRY_DELAY_MS = 5 * 60 * 1000;
11
+ // A refused answer is a fresh sample away from a good one; only an unreachable model needs the long wait.
12
+ const REJECTED_RETRY_DELAY_MS = 20 * 1000;
11
13
  const MAX_DRAFT_ATTEMPTS = 3;
12
14
  const LEASE_MS = 10 * 60 * 1000;
13
15
  const GENERATION_TIMEOUT_MS = 4 * 60 * 1000;
@@ -26,7 +28,7 @@ async function draftReview(EscalationReview, { _id, draftDueAt, draftAttempts =
26
28
  logger.error('[ReviewDraftSweep] The automatic draft threw', { escalationReviewId: String(_id), error: error.message });
27
29
  outcome = { review: null, retry: true };
28
30
  }
29
- const { review, retry } = outcome;
31
+ const { review, retry, rejected = false } = outcome;
30
32
  if (review) return true;
31
33
  const willRetry = retry && draftAttempts + 1 < MAX_DRAFT_ATTEMPTS;
32
34
  if (retry && !willRetry) {
@@ -45,7 +47,7 @@ async function draftReview(EscalationReview, { _id, draftDueAt, draftAttempts =
45
47
  await EscalationReview.updateOne(
46
48
  { _id, draftDueAt: lease },
47
49
  willRetry
48
- ? { $set: { draftDueAt: new Date(Date.now() + RETRY_DELAY_MS) }, $inc: { draftAttempts: 1 } }
50
+ ? { $set: { draftDueAt: new Date(Date.now() + (rejected ? REJECTED_RETRY_DELAY_MS : RETRY_DELAY_MS)) }, $inc: { draftAttempts: 1 } }
49
51
  : { $set: { draftDueAt: null } },
50
52
  BOOKKEEPING,
51
53
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.72.1",
3
+ "version": "5.72.3",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",