@peopl-health/nexus 1.7.4 → 1.7.6

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.
@@ -154,6 +154,65 @@ class TwilioProvider extends MessageProvider {
154
154
  hasContentSid: Boolean(scheduledMessage.contentSid)
155
155
  });
156
156
 
157
+ const updateStatus = async (status, messageId = null, error = null) => {
158
+ if (!scheduledMessage) return;
159
+
160
+ const now = new Date();
161
+ const errorCode = error?.code || error?.status || error?.errorCode || null;
162
+ const errorMessage = error?.message || error?.statusMessage || null;
163
+ const statusEntry = { status, at: now, errorCode, errorMessage };
164
+ const baseUpdate = {
165
+ status,
166
+ lastStatus: status,
167
+ lastStatusAt: now,
168
+ errorCode,
169
+ errorMessage
170
+ };
171
+
172
+ if (messageId) baseUpdate.wa_id = messageId;
173
+
174
+ const ScheduledMessageModel = (() => {
175
+ if (scheduledMessage.constructor && typeof scheduledMessage.constructor.updateOne === 'function') {
176
+ return scheduledMessage.constructor;
177
+ }
178
+ try {
179
+ return require('../models/agendaMessageModel').ScheduledMessage;
180
+ } catch (e) {
181
+ return null;
182
+ }
183
+ })();
184
+
185
+ if (!ScheduledMessageModel) {
186
+ console.warn('[TwilioProvider] Scheduled message model unavailable for status update');
187
+ return;
188
+ }
189
+
190
+ const query = (() => {
191
+ if (scheduledMessage._id) return { _id: scheduledMessage._id };
192
+ if (messageId) return { wa_id: messageId };
193
+ if (scheduledMessage.wa_id) return { wa_id: scheduledMessage.wa_id };
194
+ return null;
195
+ })();
196
+
197
+ if (!query) {
198
+ console.warn('[TwilioProvider] Scheduled message status update skipped: no identifier', {
199
+ hasId: Boolean(scheduledMessage._id),
200
+ messageId,
201
+ existingWaId: scheduledMessage.wa_id
202
+ });
203
+ return;
204
+ }
205
+
206
+ try {
207
+ await ScheduledMessageModel.updateOne(query, {
208
+ $set: baseUpdate,
209
+ $push: { statusHistory: statusEntry }
210
+ });
211
+ } catch (statusErr) {
212
+ console.warn('[TwilioProvider] Failed to update scheduled message status', statusErr?.message || statusErr);
213
+ }
214
+ };
215
+
157
216
  setTimeout(async () => {
158
217
  try {
159
218
  // Convert Mongoose document to plain object if needed
@@ -164,9 +223,11 @@ class TwilioProvider extends MessageProvider {
164
223
  hasMessage: Boolean(payload.message || payload.body),
165
224
  hasMedia: Boolean(payload.fileUrl)
166
225
  });
167
- await sender(payload);
168
- console.log('Scheduled message sent successfully');
226
+ const response = await sender(payload);
227
+ const messageId = response?.result?.sid || null;
228
+ await updateStatus('sent', messageId);
169
229
  } catch (error) {
230
+ await updateStatus('failed', null, error);
170
231
  console.error(`Scheduled message failed: ${error.message}`);
171
232
  }
172
233
  }, delay);
@@ -7,9 +7,8 @@ const { getLastNMessages } = require('../helpers/assistantHelper');
7
7
  * and supports dynamic tool registration.
8
8
  */
9
9
  class BaseAssistant {
10
- constructor(input = {}) {
11
- const options = this._normalizeOptions(input);
12
-
10
+ constructor(options = {}) {
11
+ console.log('options', options);
13
12
  this.assistantId = options.assistantId || null;
14
13
  this.thread = options.thread || null;
15
14
  this.status = options.status || 'idle';
@@ -53,18 +52,6 @@ class BaseAssistant {
53
52
  }
54
53
  }
55
54
 
56
- _normalizeOptions(input) {
57
- if (!input || typeof input !== 'object') {
58
- return { thread: input };
59
- }
60
-
61
- if (input && input._id && !input.assistantId && !input.client && !input.tools) {
62
- return { thread: input };
63
- }
64
-
65
- return input;
66
- }
67
-
68
55
  _ensureClient() {
69
56
  if (!this.client) {
70
57
  throw new Error('LLM client not configured. Ensure openaiClient is initialized.');
@@ -415,7 +415,8 @@ class NexusMessaging {
415
415
  if (response) {
416
416
  await this.sendMessage({
417
417
  code: from,
418
- message: response
418
+ message: response,
419
+ processed: true
419
420
  });
420
421
  }
421
422
  } catch (error) {
@@ -605,7 +606,8 @@ class NexusMessaging {
605
606
  await this.provider.sendMessage({
606
607
  to: chatId,
607
608
  message: botResponse,
608
- type: 'text'
609
+ type: 'text',
610
+ processed: true
609
611
  });
610
612
  }
611
613
 
package/lib/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  declare module '@peopl-health/nexus' {
2
- import { EventEmitter } from 'events';
3
2
  import mongoose from 'mongoose';
4
3
 
5
4
  // Core Types
@@ -16,7 +16,7 @@ class MessageParser {
16
16
  */
17
17
  parseMessage(rawMessage) {
18
18
  const messageData = {
19
- id: rawMessage.id || rawMessage.key?.id,
19
+ id: rawMessage.id || rawMessage.key?.id || rawMessage.MessageSid,
20
20
  from: this.extractSender(rawMessage),
21
21
  timestamp: rawMessage.timestamp || Date.now(),
22
22
  raw: rawMessage
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "1.7.4",
3
+ "version": "1.7.6",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",