@peopl-health/nexus 5.55.0-dev.8344 → 5.55.0-dev.8379

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.
@@ -557,7 +557,7 @@ class NexusMessaging {
557
557
  * INBOUND MESSAGE PROCESSING
558
558
  */
559
559
 
560
- async processIncomingMessage(messageData) {
560
+ async processIncomingMessage(messageData, { onSaved = null } = {}) {
561
561
  const chatId = messageData.code;
562
562
 
563
563
  if (chatId) {
@@ -600,9 +600,20 @@ class NexusMessaging {
600
600
  if (this.batchingConfig.enabled && chatId) return this._handleWithCheckAfter(chatId);
601
601
  return messageData.type === 'media' ? this.handleMedia(messageData) : this.handleMessage(messageData);
602
602
  },
603
+ { onSaved: typeof onSaved === 'function' ? (messageId) => this._notifySaved(onSaved, messageId, chatId) : null },
603
604
  );
604
605
  }
605
606
 
607
+ async _notifySaved(onSaved, messageId, chatId) {
608
+ try {
609
+ await onSaved(messageId);
610
+ } catch (error) {
611
+ logger.warn('[processIncomingMessage] The saved-message callback failed; the turn continues', {
612
+ code: chatId, error: error?.message ?? String(error),
613
+ });
614
+ }
615
+ }
616
+
606
617
  async handleMessage(messageData) {
607
618
  if (this.handlers.onMessage) return await this.handlers.onMessage(messageData, this);
608
619
  return await this.handleMessageWithAssistant(messageData);
@@ -181,10 +181,11 @@ class PhiProcessor {
181
181
  return encoded;
182
182
  }
183
183
 
184
- async inData(messageData, saveFn, handler) {
184
+ async inData(messageData, saveFn, handler, { onSaved } = {}) {
185
185
  const decodedBody = messageData.body;
186
186
  const encodedBody = await this.encodeBody(decodedBody, messageData.code);
187
187
  const saved = await saveFn({ ...messageData, body: encodedBody, plainBody: decodedBody });
188
+ if (saved?._id != null) await onSaved?.(String(saved._id));
188
189
  return handler({ body: encodedBody, _id: saved?._id });
189
190
  }
190
191
 
package/lib/index.d.ts CHANGED
@@ -248,7 +248,7 @@ declare module '@peopl-health/nexus' {
248
248
  setHandlers(handlers: MessageHandlers): void;
249
249
  sendMessage(messageData: MessageData): Promise<any>;
250
250
  sendScheduledMessage(scheduledMessage: ScheduledMessage): Promise<void>;
251
- processIncomingMessage(messageData: MessageData & { type: NonNullable<MessageData['type']> }): Promise<any>;
251
+ processIncomingMessage(messageData: MessageData & { type: NonNullable<MessageData['type']> }, options?: IncomingMessageOptions): Promise<any>;
252
252
  getEventBus(): import('events').EventEmitter;
253
253
  getBatchingManager(): BatchingManager;
254
254
  getPipeline(): ProcessingPipeline;
@@ -322,6 +322,11 @@ declare module '@peopl-health/nexus' {
322
322
  nerTimeoutMs?: number;
323
323
  }
324
324
 
325
+ export interface IncomingMessageOptions {
326
+ /** Called with the saved message's id once it is persisted, before the turn runs. Not called when nothing was persisted. */
327
+ onSaved?: (messageId: string) => void | Promise<void>;
328
+ }
329
+
325
330
  export interface InitializeOptions {
326
331
  provider?: 'twilio' | 'baileys';
327
332
  providerConfig?: TwilioConfig | BaileysConfig;
@@ -360,7 +365,7 @@ declare module '@peopl-health/nexus' {
360
365
  sendMessage(messageData: MessageData): Promise<any>;
361
366
  sendScheduledMessage(scheduledMessage: ScheduledMessage): Promise<void>;
362
367
  processScheduledFollowUp(code: string, options: { reminderId: string; firstName: string; now?: Date }): Promise<ScheduledFollowUpResult>;
363
- processMessage(rawMessage: any): Promise<any>;
368
+ processMessage(rawMessage: any, options?: IncomingMessageOptions): Promise<any>;
364
369
  isConnected(): boolean;
365
370
  beginDrain(): void;
366
371
  isDraining(): boolean;
package/lib/index.js CHANGED
@@ -252,7 +252,7 @@ class Nexus {
252
252
  return await this.messaging.processScheduledFollowUp(code, options);
253
253
  }
254
254
 
255
- async processMessage(rawMessage) {
255
+ async processMessage(rawMessage, { onSaved = null } = {}) {
256
256
  if (!this.isInitialized) {
257
257
  throw new Error('Nexus not initialized. Call initialize() first.');
258
258
  }
@@ -260,7 +260,7 @@ class Nexus {
260
260
  const provider = this.messaging.getProvider();
261
261
  const normalizedMessage = await provider.normalize(rawMessage);
262
262
  const messageData = await this.messageParser.parseMessage(normalizedMessage);
263
- return await this.messaging.processIncomingMessage(messageData);
263
+ return await this.messaging.processIncomingMessage(messageData, { onSaved });
264
264
  }
265
265
 
266
266
  isConnected() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "5.55.0-dev.8344",
3
+ "version": "5.55.0-dev.8379",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",