@peopl-health/nexus 5.48.2 → 5.49.0-dev.7421

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.
@@ -612,17 +612,14 @@ class BaseLLMProvider {
612
612
  } catch (error) {
613
613
  // Keyed on the signal, not the error type: an AbortError from some unrelated inner timeout is not a supersede.
614
614
  const aborted = Boolean(config.signal?.aborted);
615
+ logger.error('[runConversation] Attempt failed', { attempt, error: error.message });
615
616
  if (aborted || isSuperseded(config.shouldContinue)) {
616
- logger.info('[runConversation] Superseded — abandoning after error', { attempt, shadow: config.shadow, aborted, error: error.message });
617
+ logger.info('[runConversation] Superseded — abandoning after error', { attempt, shadow: config.shadow, aborted });
617
618
  const ctx = config?.assistant?.toolRuntimeContext;
618
619
  if (aborted && ctx) ctx.abort = { reason: 'superseded', iteration: ctx.trace ? (ctx.trace.iterations?.length ?? 0) + 1 : null };
619
620
  return { status: 'cancelled', output_text: '', tools_executed: [], retries: 0 };
620
621
  }
621
- if (attempt === maxRetries) {
622
- logger.error('[runConversation] Attempt failed', { attempt, error: error.message });
623
- throw error;
624
- }
625
- logger.warn('[runConversation] Attempt failed, retrying', { attempt, maxRetries, error: error.message });
622
+ if (attempt === maxRetries) throw error;
626
623
  await new Promise(r => setTimeout(r, 500));
627
624
  }
628
625
  }
@@ -5,6 +5,7 @@ const {
5
5
  addCallLog,
6
6
  addDraft,
7
7
  closeEscalationReview,
8
+ getEscalationReviewById,
8
9
  handOffToDoctor,
9
10
  listEscalationReviews,
10
11
  openEscalationReview,
@@ -32,6 +33,15 @@ const openEscalationReviewController = async (req, res) => {
32
33
  }
33
34
  };
34
35
 
36
+ const getEscalationReviewController = async (req, res) => {
37
+ try {
38
+ const { review, isApprover } = await getEscalationReviewById({ id: req.params.id, requestedBy: req.query.requestedBy });
39
+ res.status(200).json({ success: true, review, isApprover });
40
+ } catch (error) {
41
+ respondWithError(res, error, 'reading escalation review', { reviewId: req.params?.id });
42
+ }
43
+ };
44
+
35
45
  const listEscalationReviewsController = async (req, res) => {
36
46
  try {
37
47
  const reviews = await listEscalationReviews({ code: req.params.code });
@@ -133,6 +143,7 @@ module.exports = {
133
143
  addEscalationReviewCallLogController,
134
144
  addEscalationReviewDraftController,
135
145
  closeEscalationReviewController,
146
+ getEscalationReviewController,
136
147
  handOffEscalationReviewController,
137
148
  listEscalationReviewsController,
138
149
  openEscalationReviewController,
@@ -678,7 +678,7 @@ class NexusMessaging {
678
678
 
679
679
  if (storeRunMetrics) await storeRunMetrics(chatId, resolved.thread, result, preProcessed.timings);
680
680
  return { ...result, timings: preProcessed.timings };
681
- }, shouldContinue, runId);
681
+ }, shouldContinue);
682
682
  }
683
683
  );
684
684
  }
@@ -988,16 +988,15 @@ class NexusMessaging {
988
988
  );
989
989
  }
990
990
 
991
- async _processMessages(chatId, processingFn, shouldFinalize = () => true, runId = null) {
991
+ async _processMessages(chatId, processingFn, shouldFinalize = () => true) {
992
992
  const query = { numero: chatId, from_me: false, processed: false };
993
993
  const unprocessed = await getMessages(query, { select: '_id' });
994
994
 
995
995
  try {
996
996
  const result = await processingFn();
997
997
  if (unprocessed.length > 0 && shouldFinalize()) {
998
- const marks = runId ? { processed: true, processedByTurnId: runId } : { processed: true };
999
- await Message.updateMany({ _id: { $in: unprocessed.map(m => m._id) } }, { $set: marks });
1000
- logger.info('[_processMessages] Marked messages as processed', { count: unprocessed.length, chatId, runId });
998
+ await Message.updateMany({ _id: { $in: unprocessed.map(m => m._id) } }, { $set: { processed: true } });
999
+ logger.info('[_processMessages] Marked messages as processed', { count: unprocessed.length, chatId });
1001
1000
  }
1002
1001
  return result;
1003
1002
  } catch (error) {
@@ -84,7 +84,6 @@ const messageSchema = new mongoose.Schema({
84
84
  preset: { type: Object, default: null },
85
85
  response_id: { type: String, default: null },
86
86
  turnId: { type: String },
87
- processedByTurnId: { type: String },
88
87
  traceId: { type: String },
89
88
  triggeredBy: { type: String, default: null }
90
89
  }, { timestamps: true });
@@ -119,6 +119,7 @@ const traceRouteDefinitions = {
119
119
  const escalationReviewRouteDefinitions = {
120
120
  'POST /': 'openEscalationReviewController',
121
121
  'GET /by-patient/:code': 'listEscalationReviewsController',
122
+ 'GET /:id': 'getEscalationReviewController',
122
123
  'POST /:id/call-logs': 'addEscalationReviewCallLogController',
123
124
  'POST /:id/drafts': 'addEscalationReviewDraftController',
124
125
  'POST /:id/doctor-handoffs': 'handOffEscalationReviewController',
@@ -277,6 +278,7 @@ const builtInControllers = {
277
278
  // Escalation review controllers
278
279
  openEscalationReviewController: escalationReviewController.openEscalationReviewController,
279
280
  listEscalationReviewsController: escalationReviewController.listEscalationReviewsController,
281
+ getEscalationReviewController: escalationReviewController.getEscalationReviewController,
280
282
  addEscalationReviewCallLogController: escalationReviewController.addEscalationReviewCallLogController,
281
283
  addEscalationReviewDraftController: escalationReviewController.addEscalationReviewDraftController,
282
284
  handOffEscalationReviewController: escalationReviewController.handOffEscalationReviewController,
@@ -217,6 +217,25 @@ async function openEscalationReview({ code, patientCaseRecordId = null, openedBy
217
217
  return { review, created: true };
218
218
  }
219
219
 
220
+ async function isEscalationApprover(operator) {
221
+ try {
222
+ return Boolean(await getEscalationApprover(operator));
223
+ } catch (error) {
224
+ logger.error('[EscalationReview] Approver lookup failed; serving the review as read only', {
225
+ error: redactedError(error),
226
+ stack: redactedStack(error),
227
+ });
228
+ return false;
229
+ }
230
+ }
231
+
232
+ async function getEscalationReviewById({ id, requestedBy } = {}) {
233
+ const reviewId = reviewIdOf(id);
234
+ const operator = requiredText(requestedBy, 'requestedBy');
235
+ const review = await loadReview(reviewId);
236
+ return { review, isApprover: await isEscalationApprover(operator) };
237
+ }
238
+
220
239
  async function listEscalationReviews({ code } = {}) {
221
240
  const patientCode = patientCodeOf(code);
222
241
  return getEscalationReview().find({ patientCode }).sort({ createdAt: -1 }).limit(RECENT_REVIEW_LIMIT).lean();
@@ -406,6 +425,7 @@ module.exports = {
406
425
  addCallLog,
407
426
  addDraft,
408
427
  closeEscalationReview,
428
+ getEscalationReviewById,
409
429
  handOffToDoctor,
410
430
  listEscalationReviews,
411
431
  openEscalationReview,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.48.2",
3
+ "version": "5.49.0-dev.7421",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",