@peopl-health/nexus 6.4.21 → 6.4.22

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.
@@ -163,6 +163,7 @@ class AssistantProcessor {
163
163
  traceId: ctx?.trace?._id ? String(ctx.trace._id) : null,
164
164
  shouldContinue: runOptions.shouldContinue,
165
165
  sendMessage: this.sendMessage,
166
+ ...(runOptions.contactCode && { contactCode: runOptions.contactCode, contactRole: runOptions.contactRole }),
166
167
  });
167
168
  return bridgeDecision;
168
169
  };
@@ -294,6 +295,7 @@ class AssistantProcessor {
294
295
  outreachCarrier: runOptions.outreachCarrier || null,
295
296
  outreachStartedAt: runOptions.outreachStartedAt || null,
296
297
  triggeredBy: runOptions.triggeredBy || null,
298
+ ...(runOptions.contactCode && { contactCode: runOptions.contactCode, contactRole: runOptions.contactRole }),
297
299
  run,
298
300
  predictionTimeMs,
299
301
  retries: runResult?.retries || 0,
@@ -395,7 +397,7 @@ class AssistantProcessor {
395
397
  const outreachCarrier = result.outreachCarrier
396
398
  ? { ...result.outreachCarrier, utilityBasis: result.signals?.outreachDecision?.utilityBasis || result.outreachCarrier.utilityBasis || null }
397
399
  : null;
398
- sent = await this.sendMessage({ code, body: result.output, processed: true, origin: 'assistant', tools_executed: result.tools_executed, prompt: result.prompt, preset: result.preset, response_id: result.response_id, turnId: result.turnId, traceId: result.traceId, triggeredBy: result.triggeredBy, preemptiveWindowRoute: result.preemptiveWindowRoute, outreachCarrier, outreachStartedAt: result.outreachStartedAt });
400
+ sent = await this.sendMessage({ code, body: result.output, processed: true, origin: 'assistant', tools_executed: result.tools_executed, prompt: result.prompt, preset: result.preset, response_id: result.response_id, turnId: result.turnId, traceId: result.traceId, triggeredBy: result.triggeredBy, preemptiveWindowRoute: result.preemptiveWindowRoute, outreachCarrier, outreachStartedAt: result.outreachStartedAt, ...(result.contactCode && { contactCode: result.contactCode, contactRole: result.contactRole }) });
399
401
  result.delivery = sent || null;
400
402
  } catch (error) {
401
403
  if (directive) {
@@ -32,7 +32,7 @@ function buildDedupNote(bridgeText) {
32
32
  'clínico. El mensaje final es la SEGUNDA burbuja que verá el paciente, no la primera.';
33
33
  }
34
34
 
35
- function maybeSendBridge({ bridgeText = null, hasToolCalls, deliveryOnly = false, code, turnId = null, traceId = null, shouldContinue, sendMessage }) {
35
+ function maybeSendBridge({ bridgeText = null, hasToolCalls, deliveryOnly = false, code, turnId = null, traceId = null, shouldContinue, sendMessage, contactCode = null, contactRole = null }) {
36
36
  const authored = typeof bridgeText === 'string' && bridgeText.trim() ? bridgeText : null;
37
37
 
38
38
  if (!isBridgeEnabled()) return { sent: false, reason: 'disabled', proposed: Boolean(authored) };
@@ -67,7 +67,7 @@ function maybeSendBridge({ bridgeText = null, hasToolCalls, deliveryOnly = false
67
67
  if (entry.clearWhenDelivered && bridgeTimestamps.get(code) === entry) bridgeTimestamps.delete(code);
68
68
  };
69
69
  try {
70
- Promise.resolve(sendMessage({ code, body: text, processed: true, origin: 'assistant', raw: { bridge: true }, turnId, traceId }))
70
+ Promise.resolve(sendMessage({ code, body: text, processed: true, origin: 'assistant', raw: { bridge: true }, turnId, traceId, ...(contactCode && { contactCode, contactRole }) }))
71
71
  .then(delivered)
72
72
  .catch(failed);
73
73
  } catch (err) {
@@ -2,7 +2,6 @@ const moment = require('moment-timezone');
2
2
 
3
3
  const { TRIAGE_RESULT_MARKER } = require('../helpers/triageResultMessageHelper');
4
4
  const { getMessages } = require('../../services/messageService');
5
- const { replyContactFor } = require('../../helpers/caregiverHelper');
6
5
  const { isWithin24HourWindow } = require('../../helpers/conversationWindowHelper');
7
6
  const { checkInsPausedUntil } = require('../../services/threadService');
8
7
  const { DEFAULT_TIMEZONE, isWithinHourWindow } = require('../../utils/scheduleUtils');
@@ -76,7 +75,7 @@ async function recentExchange(code, now) {
76
75
  lastMessage(code, now, false),
77
76
  lastMessage(code, now, true),
78
77
  getMessages({ numero: code, from_me: true, ...OWN_WRITTEN, createdAt: { $gte: startOfDay, $lt: now } }, { select: 'createdAt' }),
79
- replyContactFor(code).then((contact) => isWithin24HourWindow(code, { contactCode: contact?.contactCode || null })),
78
+ isWithin24HourWindow(code),
80
79
  ]);
81
80
  return { patient, own, ownMessagesToday: (ownToday || []).length, sessionOpen };
82
81
  }
@@ -519,10 +519,6 @@ class NexusMessaging {
519
519
 
520
520
  async sendAssistantReply(messageData) {
521
521
  if (await patientIsDeceased(messageData.code)) return NOT_SENT_DECEASED;
522
- if (!messageData.contactCode && (messageData.outreachCarrier?.replyToContact || (!messageData.preemptiveWindowRoute && !messageData.outreachStartedAt))) {
523
- const contact = await replyContactFor(messageData.code);
524
- if (contact) messageData = { ...messageData, ...contact };
525
- }
526
522
  if (messageData.outreachStartedAt) {
527
523
  const patientActivity = await getMessages({
528
524
  numero: messageData.code,
@@ -740,7 +736,7 @@ class NexusMessaging {
740
736
  if (this.batchingManager.isDraining()) return { deferred: true, reason: 'draining' };
741
737
 
742
738
  try {
743
- const result = await this._processMessages(from, () => this.assistantProcessor.process({ code: from, runOptions: { patientTurn: true } }));
739
+ const result = await this._processMessages(from, async () => this.assistantProcessor.process({ code: from, runOptions: { patientTurn: true, ...(await replyContactFor(from)) } }));
744
740
  await this.assistantProcessor.sendResponse(from, result);
745
741
  } catch (error) {
746
742
  logger.error('Error in handleMessageWithAssistant', { error: error.message });
@@ -753,7 +749,7 @@ class NexusMessaging {
753
749
  if (this.batchingManager.isDraining()) return { deferred: true, reason: 'draining' };
754
750
 
755
751
  try {
756
- const result = await this._processMessages(from, () => this.assistantProcessor.process({ code: from, runOptions: { patientTurn: true } }));
752
+ const result = await this._processMessages(from, async () => this.assistantProcessor.process({ code: from, runOptions: { patientTurn: true, ...(await replyContactFor(from)) } }));
757
753
  await this.assistantProcessor.sendResponse(from, result);
758
754
  } catch (error) {
759
755
  logger.error('Error in handleMediaWithAssistant', { error: error.message });
@@ -858,7 +854,7 @@ class NexusMessaging {
858
854
 
859
855
  const result = await this.assistantProcessor.executeLLM(
860
856
  resolved.thread, resolved.assistant,
861
- { prePromptResult: preProcessResult, shouldContinue, runId, signal: this.batchingManager.getAbortSignal(runId) },
857
+ { prePromptResult: preProcessResult, shouldContinue, runId, signal: this.batchingManager.getAbortSignal(runId), ...(await replyContactFor(chatId)) },
862
858
  preProcessed.messages[0]
863
859
  );
864
860
 
@@ -1078,7 +1074,6 @@ class NexusMessaging {
1078
1074
  firstName = null,
1079
1075
  reminderType = 'seguimiento',
1080
1076
  reminderPrefix = null,
1081
- replyToContact = false,
1082
1077
  allowFollowUp = true,
1083
1078
  dryRun = false,
1084
1079
  } = {}) {
@@ -1112,7 +1107,7 @@ class NexusMessaging {
1112
1107
  turnKind: TURN_KIND.PROACTIVE_OUTREACH,
1113
1108
  outreachBrief: brief,
1114
1109
  preemptiveWindowRoute: true,
1115
- outreachCarrier: { reminderType, reminderPrefix, firstName, replyToContact },
1110
+ outreachCarrier: { reminderType, reminderPrefix, firstName },
1116
1111
  outreachAllowsFollowUp: allowFollowUp,
1117
1112
  outreachStartedAt,
1118
1113
  // Stamped on the outbound so a delivered message joins back to the row that surfaced her.
@@ -1250,7 +1245,7 @@ class NexusMessaging {
1250
1245
  const sent = outcome === 'sent' || outcome === 'unknown';
1251
1246
  result = { decision: sent ? 'write' : 'skipped', holdReason: sent ? null : outcome, sent };
1252
1247
  } else {
1253
- const decision = await this.processOutreach(code, { brief: plan.brief, triggeredBy, reason: 'check_in_due', firstName, replyToContact: true });
1248
+ const decision = await this.processOutreach(code, { brief: plan.brief, triggeredBy, reason: 'check_in_due', firstName });
1254
1249
  result = { decision: decision.decision, holdReason: decision.holdReason, nextCheckAt: decision.nextCheckAt, sent: decision.accepted };
1255
1250
  }
1256
1251
 
package/lib/index.d.ts CHANGED
@@ -277,7 +277,7 @@ declare module '@peopl-health/nexus' {
277
277
  getWorkflowRunner(): WorkflowRunner;
278
278
  processInstruction(code: string, instruction: string, role?: string, options?: { triggeredBy?: string; beforeRun?: () => Promise<string | null | undefined> | string | null | undefined; dedupeWithinMs?: number }): Promise<{ outcome: string | null; reason?: string; error?: string }>;
279
279
  processSystemMessage(code: string, messages: string | string[], role?: string, options?: { triggeredBy?: string; reply?: boolean }): Promise<{ delivery: Record<string, unknown> | null; sendThrew: boolean; reply: string | null } | undefined>;
280
- processOutreach(code: string, options?: { brief?: OutreachBrief | null; triggeredBy?: string | null; reason?: string | null; firstName?: string | null; dryRun?: boolean; replyToContact?: boolean; allowFollowUp?: boolean }): Promise<OutreachDecision>;
280
+ processOutreach(code: string, options?: { brief?: OutreachBrief | null; triggeredBy?: string | null; reason?: string | null; firstName?: string | null; dryRun?: boolean; allowFollowUp?: boolean }): Promise<OutreachDecision>;
281
281
  processCheckInDue(code: string, options: { reminderId: string; type: 'contingencia' | 'seguimiento'; question?: string; firstName?: string | null }): Promise<CheckInDueResult>;
282
282
  processScheduledFollowUp(code: string, options: { reminderId: string; firstName: string; now?: Date }): Promise<ScheduledFollowUpResult>;
283
283
  isConnected(): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.4.21",
3
+ "version": "6.4.22",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",