@peopl-health/nexus 3.15.6 → 3.16.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.
@@ -9,7 +9,10 @@ const { getBug, VALID_SEVERITIES } = require('../models/bugModel');
9
9
  const { addRecord, getRecordByFilter } = require('../services/airtableService');
10
10
 
11
11
  async function logBugReportToAirtable(reportedBug) {
12
- const { reporter, whatsapp_id, description, severity, messages, server, bugType, project } = reportedBug;
12
+ const {
13
+ reporter, whatsapp_id, description, severity, messages, server, bugType, project,
14
+ clasificacion, status, owner, request_id
15
+ } = reportedBug;
13
16
  try {
14
17
  let conversation = null;
15
18
  if (messages?.length) {
@@ -22,11 +25,13 @@ async function logBugReportToAirtable(reportedBug) {
22
25
  }
23
26
 
24
27
  let patientId = null;
25
- try {
26
- const records = await getRecordByFilter(Logging_ID, 'estado_general', `{whatsapp_id}='${whatsapp_id}'`);
27
- if (records?.length) patientId = records[0].record_logging_id;
28
- } catch (err) {
29
- logger.warn('Could not find patient in estado_general:', { error: err.message });
28
+ if (whatsapp_id) {
29
+ try {
30
+ const records = await getRecordByFilter(Logging_ID, 'estado_general', `{whatsapp_id}='${whatsapp_id}'`);
31
+ if (records?.length) patientId = records[0].record_logging_id;
32
+ } catch (err) {
33
+ logger.warn('Could not find patient in estado_general:', { error: err.message });
34
+ }
30
35
  }
31
36
 
32
37
  const bugRecord = await addRecord(Logging_ID, 'bug_reports', {
@@ -34,6 +39,10 @@ async function logBugReportToAirtable(reportedBug) {
34
39
  ...(project && {...project}),
35
40
  ...(patientId && { patient_id: [patientId] }),
36
41
  ...(conversation && { conversation }),
42
+ ...(clasificacion && { clasificacion }),
43
+ ...(status && { status }),
44
+ ...(owner && { owner }),
45
+ ...(request_id && { request_id }),
37
46
  server
38
47
  });
39
48
  logger.debug('Bug report logged to Airtable successfully');
@@ -148,4 +157,4 @@ const getBugByWhatsappIdController = async (req, res) => {
148
157
  }
149
158
  };
150
159
 
151
- module.exports = { reportBugController, getBugByWhatsappIdController, updateBugController };
160
+ module.exports = { reportBugController, getBugByWhatsappIdController, updateBugController, logBugReportToAirtable };
@@ -0,0 +1,41 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ const KIND_VALUES = ['freeform', 'recovery_template'];
4
+ const STATUS_VALUES = [null, 'queued', 'sending', 'sent', 'delivered', 'undelivered', 'failed', 'read'];
5
+
6
+ const deliveryAttemptSchema = new mongoose.Schema({
7
+ messageId: {
8
+ type: mongoose.Schema.Types.ObjectId,
9
+ ref: 'Message',
10
+ required: true
11
+ },
12
+ kind: {
13
+ type: String,
14
+ enum: KIND_VALUES,
15
+ required: true
16
+ },
17
+ twilioSid: { type: String, default: null },
18
+ contentSid: { type: String, default: null },
19
+ status: {
20
+ type: String,
21
+ enum: STATUS_VALUES,
22
+ default: null
23
+ },
24
+ errorCode: { type: String, default: null },
25
+ errorMessage: { type: String, default: null },
26
+ attemptedAt: { type: Date, default: Date.now, required: true },
27
+ completedAt: { type: Date, default: null },
28
+ raw: { type: mongoose.Schema.Types.Mixed, default: null }
29
+ }, { timestamps: true });
30
+
31
+ deliveryAttemptSchema.index({ messageId: 1, attemptedAt: 1 }, { name: 'message_attempt_history_idx' });
32
+ deliveryAttemptSchema.index({ kind: 1, status: 1, attemptedAt: -1 }, { name: 'analytics_idx' });
33
+ deliveryAttemptSchema.index({ twilioSid: 1 }, { name: 'twilio_sid_idx', sparse: true });
34
+
35
+ const DeliveryAttempt = mongoose.model('DeliveryAttempt', deliveryAttemptSchema);
36
+
37
+ module.exports = {
38
+ DeliveryAttempt,
39
+ DELIVERY_ATTEMPT_KINDS: KIND_VALUES,
40
+ DELIVERY_ATTEMPT_STATUSES: STATUS_VALUES
41
+ };
@@ -8,6 +8,7 @@ const { logger } = require('../utils/logger');
8
8
  const { getClinicalContext } = require('../helpers/patientInformationHelper');
9
9
 
10
10
  const { updateRecordByFilter } = require('../services/airtableService');
11
+ const { DELIVERY_ATTEMPT_STATUSES } = require('./deliveryAttemptModel');
11
12
 
12
13
  const { Thread } = require('./threadModel');
13
14
 
@@ -75,7 +76,7 @@ const messageSchema = new mongoose.Schema({
75
76
  statusInfo: {
76
77
  status: {
77
78
  type: String,
78
- enum: ['queued', 'sending', 'sent', 'delivered', 'undelivered', 'failed', 'read', null],
79
+ enum: DELIVERY_ATTEMPT_STATUSES,
79
80
  default: null
80
81
  },
81
82
  errorCode: { type: String, default: null },
@@ -85,7 +86,12 @@ const messageSchema = new mongoose.Schema({
85
86
  recoveryStartedAt: { type: Date, default: null },
86
87
  recoverySentAt: { type: Date, default: null },
87
88
  recoveryMessageId: { type: String, default: null },
88
- recoveryRejectedAt: { type: Date, default: null }
89
+ recoveryRejectedAt: { type: Date, default: null },
90
+ latestDeliveryStatus: {
91
+ type: String,
92
+ enum: DELIVERY_ATTEMPT_STATUSES,
93
+ default: null
94
+ }
89
95
  },
90
96
  prompt: { type: Object, default: null },
91
97
  preset: { type: Object, default: null },
@@ -1,5 +1,6 @@
1
1
  const { OpenAI } = require('openai');
2
2
 
3
+ const runtimeConfig = require('../config/runtimeConfig');
3
4
  const { retryWithBackoff } = require('../utils/retryUtils');
4
5
  const { logger } = require('../utils/logger');
5
6
  const { getCurrentMexicoDateTime } = require('../utils/dateUtils');
@@ -10,6 +11,7 @@ const { getLastNMessages } = require('../helpers/messageHelper');
10
11
 
11
12
  const { composePrompt, resolveTools } = require('../services/promptComposerService');
12
13
  const { getToolSchemas: getRegistrySchemas } = require('../services/toolRegistryService');
14
+ const { logBugReportToAirtable } = require('../controllers/bugReportController');
13
15
  const { handleFunctionCalls } = require('./OpenAIResponsesProviderTools');
14
16
 
15
17
  const CONVERSATION_PREFIX = 'conv_';
@@ -171,12 +173,40 @@ class OpenAIResponsesProvider {
171
173
  if (result.output && Array.isArray(result.output)) {
172
174
  const messageItems = result.output.filter(item => item && item.type === 'message');
173
175
  if (messageItems.length > 0) {
174
- const lastMessage = messageItems[messageItems.length - 1];
176
+ if (messageItems.length > 1) {
177
+ const hasFunctionCalls = result.output.some(item => item?.type === 'function_call');
178
+ logger.warn('[OpenAIResponsesProvider] Multiple message items in response; keeping the first (likely OpenAI multi-output bug)', {
179
+ discarded: messageItems.length - 1,
180
+ responseId: result.id,
181
+ model: result.model,
182
+ hasFunctionCalls
183
+ });
184
+ logBugReportToAirtable({
185
+ reporter: 'system',
186
+ description: [
187
+ 'OpenAI Responses API returned multiple message items in a single response.',
188
+ 'Suspected upstream bug (model failed to emit stop-of-message token).',
189
+ `Discarded ${messageItems.length - 1} extra message(s); kept the first as the canonical reply.`,
190
+ `Response ID: ${result.id || 'unknown'}`,
191
+ `Model: ${result.model || 'unknown'}`,
192
+ `Function calls present: ${hasFunctionCalls ? 'yes' : 'no'}`
193
+ ].join('\n'),
194
+ severity: 'medium',
195
+ status: 'Open',
196
+ clasificacion: 'alucinaciones',
197
+ bugType: 'backend',
198
+ owner: ['ariana'],
199
+ request_id: result.id || null,
200
+ server: runtimeConfig.get('SERVICE_NAME') || 'nexus'
201
+ }).catch((err) => logger.warn('[OpenAIResponsesProvider] Bug report logger failed', { error: err.message }));
202
+ }
203
+
204
+ const firstMessage = messageItems[0];
175
205
  let text = '';
176
- if (lastMessage.content && Array.isArray(lastMessage.content)) {
177
- text = lastMessage.content.map(c => this._contentPartToText(c)).filter(Boolean).join('');
178
- } else if (typeof lastMessage.content === 'string') {
179
- text = lastMessage.content;
206
+ if (firstMessage.content && Array.isArray(firstMessage.content)) {
207
+ text = firstMessage.content.map(c => this._contentPartToText(c)).filter(Boolean).join('');
208
+ } else if (typeof firstMessage.content === 'string') {
209
+ text = firstMessage.content;
180
210
  }
181
211
  if (text.trim()) return text.trim();
182
212
  }
@@ -253,7 +283,7 @@ class OpenAIResponsesProvider {
253
283
  this.sessionManager?.recordActivity(thread.code);
254
284
 
255
285
  const completed = result.status === 'completed';
256
- const output = this._extractMessageOutput(result);
286
+ const output = result.output_text || this._extractMessageOutput(result);
257
287
  const toolsExecuted = result.tools_executed?.length || 0;
258
288
 
259
289
  logger.info('[executeRun] Complete', {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "3.15.6",
3
+ "version": "3.16.0",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",