@peopl-health/nexus 2.4.11-logs-msg → 2.4.12

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.
@@ -118,7 +118,7 @@ class TwilioProvider extends MessageProvider {
118
118
  provider: 'twilio',
119
119
  timestamp: new Date(),
120
120
  fromMe: true,
121
- processed: false
121
+ processed: messageData.processed !== undefined ? messageData.processed : false
122
122
  });
123
123
  logger.info('[TwilioProvider] Message persisted successfully', { messageId: result.sid });
124
124
  } catch (storageError) {
@@ -29,7 +29,7 @@ const addInsAssistantController = async (req, res) => {
29
29
  const role = variant === 'responses' ? 'developer' : 'user';
30
30
 
31
31
  const ans = await addInsAssistant(code, instruction, role);
32
- if (ans) await sendMessage({code, body: ans, fileType: 'text'});
32
+ if (ans) await sendMessage({code, body: ans, fileType: 'text', origin: 'assistant'});
33
33
  return res.status(200).send({ message: 'Add instruction to the assistant' });
34
34
  } catch (error) {
35
35
  logger.error(error);
@@ -42,7 +42,7 @@ const addMsgAssistantController = async (req, res) => {
42
42
 
43
43
  try {
44
44
  const ans = await addMsgAssistant(code, messages, role, reply);
45
- if (ans) await sendMessage({code, body: ans, fileType: 'text'});
45
+ if (ans) await sendMessage({code, body: ans, fileType: 'text', origin: 'assistant'});
46
46
  return res.status(200).send({ message: 'Add message to the assistant' });
47
47
  } catch (error) {
48
48
  logger.error(error);
@@ -232,7 +232,8 @@ const getConversationReplyController = async (req, res) => {
232
232
 
233
233
  const messageData = {
234
234
  code: formattedPhoneNumber,
235
- fileType: 'text'
235
+ fileType: 'text',
236
+ _fromConversationReply: true
236
237
  };
237
238
 
238
239
  // Handle template message (contentSid provided)
@@ -279,6 +279,10 @@ class NexusMessaging {
279
279
  throw new Error('No provider initialized');
280
280
  }
281
281
 
282
+ if (messageData._fromConversationReply && messageData.code && (messageData.body || messageData.message || messageData.contentSid)) {
283
+ messageData.processed = true;
284
+ }
285
+
282
286
  const result = await this.provider.sendMessage(messageData);
283
287
 
284
288
  // Store message only if provider does not handle persistence itself
@@ -297,14 +301,24 @@ class NexusMessaging {
297
301
  });
298
302
  }
299
303
 
300
- // Add to thread context for manual sends
301
- if (messageData.origin !== 'assistant' && messageData.code &&
302
- (messageData.body || messageData.message)) {
304
+ // Add to thread context for manual sends (text messages and templates)
305
+ let messageContent = messageData.body || messageData.message;
306
+ if (!messageContent && messageData.contentSid && typeof this.provider.renderTemplate === 'function') {
307
+ try {
308
+ messageContent = await this.provider.renderTemplate(messageData.contentSid, messageData.variables);
309
+ } catch (err) {
310
+ logger.warn(`[NexusMessaging] Failed to render template for thread: ${err.message}`);
311
+ }
312
+ }
313
+
314
+ if (messageData.origin !== 'assistant' && messageData.code && messageContent) {
315
+ const skipSystemMessage = messageData._fromConversationReply === true;
303
316
  await addMsgAssistant(
304
317
  messageData.code,
305
- [messageData.body || messageData.message],
318
+ [messageContent],
306
319
  'assistant',
307
- false
320
+ false,
321
+ skipSystemMessage
308
322
  );
309
323
  }
310
324
 
@@ -30,7 +30,7 @@ const messageSchema = new mongoose.Schema({
30
30
  from_me: { type: Boolean, default: false },
31
31
  origin: {
32
32
  type: String,
33
- enum: ['whatsapp_platform', 'assistant', 'patient'],
33
+ enum: ['whatsapp_platform', 'assistant', 'patient', 'system', 'instruction'],
34
34
  default: 'whatsapp_platform' },
35
35
  tools_executed: [{
36
36
  tool_name: { type: String, required: true },
@@ -7,6 +7,7 @@ const { createProvider } = require('../providers/createProvider');
7
7
 
8
8
  const { Thread } = require('../models/threadModel.js');
9
9
  const { PredictionMetrics } = require('../models/predictionMetricsModel');
10
+ const { insertMessage } = require('../models/messageModel');
10
11
 
11
12
  const { getCurRow } = require('../helpers/assistantHelper.js');
12
13
  const { runAssistantAndWait, runAssistantWithRetries } = require('../helpers/assistantHelper.js');
@@ -186,7 +187,7 @@ const createAssistant = async (code, assistant_id, messages=[], force=false) =>
186
187
  return thread;
187
188
  };
188
189
 
189
- const addMsgAssistant = async (code, inMessages, role = 'user', reply = false) => {
190
+ const addMsgAssistant = async (code, inMessages, role = 'user', reply = false, skipSystemMessage = false) => {
190
191
  try {
191
192
  let thread = await Thread.findOne({ code: code });
192
193
  logger.info(thread);
@@ -204,6 +205,32 @@ const addMsgAssistant = async (code, inMessages, role = 'user', reply = false) =
204
205
  role: role,
205
206
  content: message
206
207
  });
208
+
209
+ // Save system message to database for frontend visibility
210
+ // Skip if message is already saved (e.g., from getConversationReplyController)
211
+ if (!skipSystemMessage) {
212
+ try {
213
+ const message_id = `system_${Date.now()}_${Math.random().toString(36).substring(7)}`;
214
+ await insertMessage({
215
+ nombre_whatsapp: 'System',
216
+ numero: code,
217
+ body: message,
218
+ timestamp: new Date(),
219
+ message_id: message_id,
220
+ is_group: false,
221
+ is_media: false,
222
+ from_me: true,
223
+ processed: true,
224
+ origin: 'system',
225
+ thread_id: thread.getConversationId(),
226
+ assistant_id: thread.getAssistantId(),
227
+ raw: { role: role }
228
+ });
229
+ } catch (err) {
230
+ // Don't throw - we don't want to break the flow if logging fails
231
+ logger.error('[addMsgAssistant] Error saving system message:', err);
232
+ }
233
+ }
207
234
  }
208
235
  },
209
236
  thread,
@@ -263,6 +290,29 @@ const addInstructionCore = async (code, instruction, role = 'user') => {
263
290
  null // no patientReply for instructions
264
291
  );
265
292
 
293
+ // Save instruction to database for frontend visibility
294
+ try {
295
+ const message_id = `instruction_${Date.now()}_${Math.random().toString(36).substring(7)}`;
296
+ await insertMessage({
297
+ nombre_whatsapp: 'Instruction',
298
+ numero: code,
299
+ body: instruction,
300
+ timestamp: new Date(),
301
+ message_id: message_id,
302
+ is_group: false,
303
+ is_media: false,
304
+ from_me: true,
305
+ processed: true,
306
+ origin: 'instruction',
307
+ thread_id: thread.getConversationId(),
308
+ assistant_id: thread.getAssistantId(),
309
+ raw: { role: role }
310
+ });
311
+ } catch (err) {
312
+ // Don't throw - we don't want to break the flow if logging fails
313
+ logger.error('[addInstructionCore] Error saving instruction message:', err);
314
+ }
315
+
266
316
  logger.info('RUN RESPONSE', output);
267
317
  return output;
268
318
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "2.4.11-logs-msg",
3
+ "version": "2.4.12",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",