@peopl-health/nexus 2.1.5 → 2.2.0

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.
@@ -121,7 +121,8 @@ class TwilioProvider extends MessageProvider {
121
121
  messageId: result.sid,
122
122
  provider: 'twilio',
123
123
  timestamp: new Date(),
124
- fromMe: true
124
+ fromMe: true,
125
+ processed: true
125
126
  });
126
127
  console.log('[TwilioProvider] Message persisted successfully', { messageId: result.sid });
127
128
  } catch (storageError) {
@@ -3,6 +3,8 @@ const { Thread } = require('../models/threadModel');
3
3
  const { getLastNMessages } = require('../helpers/assistantHelper');
4
4
  const { createProvider } = require('../providers/createProvider');
5
5
 
6
+ const DEFAULT_MAX_HISTORICAL_MESSAGES = parseInt(process.env.MAX_HISTORICAL_MESSAGES || '50', 10);
7
+
6
8
  /**
7
9
  * Flexible base assistant implementation that integrates with OpenAI Threads
8
10
  * and supports dynamic tool registration.
@@ -165,7 +167,7 @@ class BaseAssistant {
165
167
 
166
168
  const whatsappId = context?.whatsapp_id || code;
167
169
  if (whatsappId) {
168
- this.lastMessages = await getLastNMessages(whatsappId, 20);
170
+ this.lastMessages = await getLastNMessages(whatsappId, DEFAULT_MAX_HISTORICAL_MESSAGES);
169
171
  }
170
172
 
171
173
  const provider = createProvider({ variant: process.env.VARIANT || 'assistants' });
@@ -1,5 +1,5 @@
1
1
  const { airtable, getBase } = require('../config/airtableConfig');
2
- const { replyAssistant } = require('../services/assistantService');
2
+ const { addMsgAssistant, replyAssistant } = require('../services/assistantService');
3
3
  const { createProvider } = require('../adapters/registry');
4
4
  const runtimeConfig = require('../config/runtimeConfig');
5
5
  const { hasPreprocessingHandler, invokePreprocessingHandler } = require('../services/preprocessingHooks');
@@ -291,10 +291,22 @@ class NexusMessaging {
291
291
  messageId: result.messageId,
292
292
  provider: result.provider,
293
293
  timestamp: new Date(),
294
- fromMe: true
294
+ fromMe: true,
295
+ processed: true
295
296
  });
296
297
  }
297
298
 
299
+ // Add to thread context for manual sends
300
+ if (messageData.origin !== 'assistant' && messageData.code &&
301
+ (messageData.body || messageData.message)) {
302
+ await addMsgAssistant(
303
+ messageData.code,
304
+ [messageData.body || messageData.message],
305
+ 'assistant',
306
+ false
307
+ );
308
+ }
309
+
298
310
  return result;
299
311
  }
300
312
 
@@ -172,7 +172,7 @@ async function processIndividualMessage(code, reply, provider, thread) {
172
172
  try {
173
173
  const formattedMessage = formatMessage(reply);
174
174
  console.log('[processIndividualMessage] formattedMessage:', formattedMessage);
175
- const isNotAssistant = !reply.from_me;
175
+ const isPatient = reply.origin === 'patient';
176
176
  let messagesChat = [];
177
177
  let url = null;
178
178
 
@@ -247,13 +247,18 @@ async function processIndividualMessage(code, reply, provider, thread) {
247
247
  console.log('[processIndividualMessage] messagesChat', messagesChat);
248
248
 
249
249
  const threadId = thread.getConversationId();
250
- if (isNotAssistant) {
250
+ if (reply.origin === 'whatsapp_platform') {
251
+ await provider.addMessage({
252
+ threadId,
253
+ role: 'assistant',
254
+ content: messagesChat
255
+ });
256
+ } else if (reply.origin === 'patient') {
251
257
  await provider.addMessage({
252
258
  threadId,
253
259
  role: 'user',
254
260
  content: messagesChat
255
261
  });
256
- console.log(`[processIndividualMessage] User message added to thread ${threadId}`);
257
262
  }
258
263
 
259
264
  await Message.updateOne(
@@ -261,7 +266,7 @@ async function processIndividualMessage(code, reply, provider, thread) {
261
266
  { $set: { assistant_id: thread.getAssistantId(), thread_id: threadId } }
262
267
  );
263
268
 
264
- return {isNotAssistant, url};
269
+ return {isPatient, url};
265
270
  } catch (err) {
266
271
  console.log(`Error inside process message ${err}`);
267
272
  } finally {
@@ -230,7 +230,7 @@ const createAssistant = async (code, assistant_id, messages=[], force=false) =>
230
230
  return thread;
231
231
  };
232
232
 
233
- const addMsgAssistant = async (code, inMessages, reply = false) => {
233
+ const addMsgAssistant = async (code, inMessages, role = 'user', reply = false) => {
234
234
  try {
235
235
  const thread = await Thread.findOne({ code: code });
236
236
  console.log(thread);
@@ -241,7 +241,7 @@ const addMsgAssistant = async (code, inMessages, reply = false) => {
241
241
  console.log(message);
242
242
  await provider.addMessage({
243
243
  threadId: thread.getConversationId(),
244
- role: 'assistant',
244
+ role: role,
245
245
  content: message
246
246
  });
247
247
  }
@@ -269,7 +269,7 @@ const addMsgAssistant = async (code, inMessages, reply = false) => {
269
269
  }
270
270
  };
271
271
 
272
- const addInsAssistant = async (code, instruction) => {
272
+ const addInsAssistant = async (code, instruction, role = 'user') => {
273
273
  try {
274
274
  const thread = await Thread.findOne({ code: code });
275
275
  console.log(thread);
@@ -286,7 +286,7 @@ const addInsAssistant = async (code, instruction) => {
286
286
  runConfig: {
287
287
  additionalInstructions: instruction,
288
288
  additionalMessages: [
289
- { role: 'user', content: instruction }
289
+ { role: role, content: instruction }
290
290
  ]
291
291
  }
292
292
  }));
@@ -328,7 +328,7 @@ const getThread = async (code, message = null) => {
328
328
  if (run.status === 'cancelled' || run.status === 'expired' || run.status === 'completed') {
329
329
  await Thread.updateOne({ code: code }, { $set: { run_id: null } });
330
330
  }
331
- thread = await Thread.findOne({ code: code, active: true });
331
+ thread = await Thread.findOne({ code: code });
332
332
  await delay(5000);
333
333
  }
334
334
 
@@ -353,7 +353,7 @@ const replyAssistant = async function (code, message_ = null, thread_ = null, ru
353
353
  try {
354
354
  let thread = thread_ || await getThread(code);
355
355
  console.log('THREAD STOPPED', code, thread?.active);
356
- if (!thread || !thread.active || thread.stopped) return null;
356
+ if (!thread) return null;
357
357
 
358
358
  const patientReply = await getLastMessages(code);
359
359
  if (!patientReply) {
@@ -375,9 +375,9 @@ const replyAssistant = async function (code, message_ = null, thread_ = null, ru
375
375
  let patientMsg = false;
376
376
  let urls = [];
377
377
  for (const reply of patientReply) {
378
- const { isNotAssistant, url } = await processIndividualMessage(code, reply, provider, thread);
379
- console.log(`isNotAssistant ${isNotAssistant} ${url}`);
380
- patientMsg = patientMsg || isNotAssistant;
378
+ const { isPatient, url } = await processIndividualMessage(code, reply, provider, thread);
379
+ console.log(`isPatient ${isPatient} ${url}`);
380
+ patientMsg = patientMsg || isPatient;
381
381
  if (url) urls.push({ 'url': url });
382
382
  }
383
383
 
@@ -395,7 +395,7 @@ const replyAssistant = async function (code, message_ = null, thread_ = null, ru
395
395
  }
396
396
  }
397
397
 
398
- if (!patientMsg) return null;
398
+ if (!patientMsg || thread.stopped) return null;
399
399
 
400
400
  const assistant = getAssistantById(thread.getAssistantId(), thread);
401
401
  assistant.setReplies(patientReply);
@@ -45,6 +45,7 @@ class MongoStorage {
45
45
  const enrichedMessage = await this._enrichTwilioMedia(messageData);
46
46
  console.log('[MongoStorage] Enriched message', enrichedMessage);
47
47
  const values = this.buildMessageValues(enrichedMessage);
48
+ console.log('[MongoStorage] Message values', values);
48
49
  const { insertMessage } = require('../models/messageModel');
49
50
  await insertMessage(values);
50
51
  console.log('[MongoStorage] Message stored');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "2.1.5",
3
+ "version": "2.2.0",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",