@peopl-health/nexus 2.4.7 → 2.4.9-fix-pdf-processing
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.
- package/examples/assistants/DoctorScheduleAssistant.js +3 -2
- package/lib/adapters/BaileysProvider.js +5 -4
- package/lib/adapters/TwilioProvider.js +20 -19
- package/lib/assistants/BaseAssistant.js +7 -6
- package/lib/config/awsConfig.js +14 -12
- package/lib/config/llmConfig.js +3 -2
- package/lib/config/mongoAuthConfig.js +5 -5
- package/lib/controllers/assistantController.js +12 -11
- package/lib/controllers/bugReportController.js +6 -5
- package/lib/controllers/conversationController.js +72 -71
- package/lib/controllers/interactionController.js +7 -6
- package/lib/controllers/mediaController.js +15 -13
- package/lib/controllers/messageController.js +7 -6
- package/lib/controllers/patientController.js +2 -1
- package/lib/controllers/qualityMessageController.js +5 -4
- package/lib/controllers/templateController.js +11 -9
- package/lib/controllers/uploadController.js +3 -1
- package/lib/core/NexusMessaging.js +18 -18
- package/lib/helpers/assistantHelper.js +8 -9
- package/lib/helpers/baileysHelper.js +4 -3
- package/lib/helpers/filesHelper.js +23 -14
- package/lib/helpers/llmsHelper.js +17 -10
- package/lib/helpers/mediaHelper.js +3 -2
- package/lib/helpers/messageHelper.js +12 -11
- package/lib/helpers/processHelper.js +2 -2
- package/lib/helpers/qrHelper.js +2 -1
- package/lib/helpers/twilioMediaProcessor.js +19 -29
- package/lib/helpers/whatsappHelper.js +3 -2
- package/lib/index.js +11 -14
- package/lib/interactive/index.js +11 -11
- package/lib/middleware/requestId.js +9 -14
- package/lib/models/messageModel.js +5 -4
- package/lib/providers/OpenAIAssistantsProvider.js +10 -9
- package/lib/providers/OpenAIResponsesProvider.js +24 -17
- package/lib/providers/OpenAIResponsesProviderTools.js +3 -5
- package/lib/providers/createProvider.js +2 -1
- package/lib/services/airtableService.js +6 -5
- package/lib/services/assistantService.js +73 -57
- package/lib/services/conversationService.js +16 -16
- package/lib/services/preprocessingHooks.js +3 -1
- package/lib/storage/MongoStorage.js +14 -14
- package/lib/utils/errorHandler.js +3 -1
- package/lib/utils/logger.js +35 -3
- package/lib/utils/mediaValidator.js +18 -14
- package/lib/utils/sanitizer.js +0 -6
- package/lib/utils/tracingDecorator.js +7 -1
- package/package.json +1 -1
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
const runtimeConfig = require('../config/runtimeConfig');
|
|
3
|
+
const { logger } = require('../utils/logger');
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* MongoDB storage interface for messages and interactions
|
|
@@ -34,7 +35,7 @@ class MongoStorage {
|
|
|
34
35
|
useNewUrlParser: true,
|
|
35
36
|
useUnifiedTopology: true
|
|
36
37
|
});
|
|
37
|
-
|
|
38
|
+
logger.info('MongoDB connected successfully');
|
|
38
39
|
} catch (error) {
|
|
39
40
|
throw new Error(`MongoDB connection failed: ${error.message}`);
|
|
40
41
|
}
|
|
@@ -46,10 +47,10 @@ class MongoStorage {
|
|
|
46
47
|
const values = this.buildMessageValues(enrichedMessage);
|
|
47
48
|
const { insertMessage } = require('../models/messageModel');
|
|
48
49
|
await insertMessage(values);
|
|
49
|
-
|
|
50
|
+
logger.info('[MongoStorage] Message stored');
|
|
50
51
|
return values;
|
|
51
52
|
} catch (error) {
|
|
52
|
-
|
|
53
|
+
logger.error('Error saving message:', error);
|
|
53
54
|
throw error;
|
|
54
55
|
}
|
|
55
56
|
}
|
|
@@ -64,23 +65,22 @@ class MongoStorage {
|
|
|
64
65
|
return messageData;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
|
|
68
|
+
logger.info('[MongoStorage] Detected Twilio media message', {
|
|
68
69
|
from: rawMessage.From,
|
|
69
70
|
numMedia
|
|
70
71
|
});
|
|
71
72
|
|
|
72
73
|
const bucketName = runtimeConfig.get('AWS_S3_BUCKET_NAME') || process.env.AWS_S3_BUCKET_NAME;
|
|
73
74
|
if (!bucketName) {
|
|
74
|
-
|
|
75
|
+
logger.warn('[MongoStorage] AWS_S3_BUCKET_NAME not configured. Skipping media upload.');
|
|
75
76
|
return messageData;
|
|
76
77
|
}
|
|
77
78
|
|
|
78
79
|
const { processTwilioMediaMessage } = require('../helpers/twilioMediaProcessor');
|
|
79
|
-
const { logger } = require('../utils/logger');
|
|
80
80
|
|
|
81
|
-
const mediaItems = await processTwilioMediaMessage(rawMessage,
|
|
81
|
+
const mediaItems = await processTwilioMediaMessage(rawMessage, bucketName);
|
|
82
82
|
if (!mediaItems || mediaItems.length === 0) {
|
|
83
|
-
|
|
83
|
+
logger.warn('[MongoStorage] Media processing returned no items');
|
|
84
84
|
return messageData;
|
|
85
85
|
}
|
|
86
86
|
|
|
@@ -91,7 +91,7 @@ class MongoStorage {
|
|
|
91
91
|
|
|
92
92
|
rawMessage.__nexusMediaProcessed = true;
|
|
93
93
|
|
|
94
|
-
|
|
94
|
+
logger.info('[MongoStorage] Media processed successfully', {
|
|
95
95
|
primaryType: mediaPayload.mediaType,
|
|
96
96
|
mediaCount: mediaItems.length,
|
|
97
97
|
s3Key: mediaPayload.key
|
|
@@ -107,7 +107,7 @@ class MongoStorage {
|
|
|
107
107
|
caption: primary.caption || messageData.caption
|
|
108
108
|
};
|
|
109
109
|
} catch (error) {
|
|
110
|
-
|
|
110
|
+
logger.error('[MongoStorage] Failed to enrich Twilio media message:', error);
|
|
111
111
|
return messageData;
|
|
112
112
|
}
|
|
113
113
|
}
|
|
@@ -197,7 +197,7 @@ class MongoStorage {
|
|
|
197
197
|
.sort({ createdAt: -1 })
|
|
198
198
|
.limit(limit);
|
|
199
199
|
} catch (error) {
|
|
200
|
-
|
|
200
|
+
logger.error('Error getting messages:', error);
|
|
201
201
|
throw error;
|
|
202
202
|
}
|
|
203
203
|
}
|
|
@@ -206,7 +206,7 @@ class MongoStorage {
|
|
|
206
206
|
try {
|
|
207
207
|
return await this.schemas.Thread.findOne({ code, active: true });
|
|
208
208
|
} catch (error) {
|
|
209
|
-
|
|
209
|
+
logger.error('Error getting thread:', error);
|
|
210
210
|
throw error;
|
|
211
211
|
}
|
|
212
212
|
}
|
|
@@ -217,7 +217,7 @@ class MongoStorage {
|
|
|
217
217
|
await thread.save();
|
|
218
218
|
return thread;
|
|
219
219
|
} catch (error) {
|
|
220
|
-
|
|
220
|
+
logger.error('Error creating thread:', error);
|
|
221
221
|
throw error;
|
|
222
222
|
}
|
|
223
223
|
}
|
|
@@ -230,7 +230,7 @@ class MongoStorage {
|
|
|
230
230
|
{ new: true }
|
|
231
231
|
);
|
|
232
232
|
} catch (error) {
|
|
233
|
-
|
|
233
|
+
logger.error('Error updating thread:', error);
|
|
234
234
|
throw error;
|
|
235
235
|
}
|
|
236
236
|
}
|
package/lib/utils/logger.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
const pino = require('pino');
|
|
2
|
-
const { trace
|
|
2
|
+
const { trace } = require('@opentelemetry/api');
|
|
3
3
|
|
|
4
4
|
const createLogger = (config = {}) => {
|
|
5
5
|
const {
|
|
@@ -98,12 +98,44 @@ const createObservabilityLogger = (serviceName = 'nexus-assistant', config = {})
|
|
|
98
98
|
};
|
|
99
99
|
|
|
100
100
|
// Default logger instances
|
|
101
|
-
const
|
|
101
|
+
const baseLogger = createLogger();
|
|
102
102
|
const observabilityLogger = createObservabilityLogger();
|
|
103
103
|
|
|
104
|
+
let getRequestId = () => null;
|
|
105
|
+
|
|
106
|
+
function setRequestIdGetter(getter) {
|
|
107
|
+
getRequestId = getter;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const logger = {
|
|
111
|
+
info: (message, meta = {}) => {
|
|
112
|
+
const requestId = getRequestId();
|
|
113
|
+
baseLogger.info(requestId ? { requestId, ...meta } : meta, message);
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
error: (message, meta = {}) => {
|
|
117
|
+
const requestId = getRequestId();
|
|
118
|
+
baseLogger.error(requestId ? { requestId, ...meta } : meta, message);
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
warn: (message, meta = {}) => {
|
|
122
|
+
const requestId = getRequestId();
|
|
123
|
+
baseLogger.warn(requestId ? { requestId, ...meta } : meta, message);
|
|
124
|
+
},
|
|
125
|
+
|
|
126
|
+
debug: (message, meta = {}) => {
|
|
127
|
+
const requestId = getRequestId();
|
|
128
|
+
baseLogger.debug(requestId ? { requestId, ...meta } : meta, message);
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
child: (bindings) => baseLogger.child(bindings),
|
|
132
|
+
level: baseLogger.level
|
|
133
|
+
};
|
|
134
|
+
|
|
104
135
|
module.exports = {
|
|
105
136
|
logger,
|
|
106
137
|
createLogger,
|
|
107
138
|
observabilityLogger,
|
|
108
|
-
createObservabilityLogger
|
|
139
|
+
createObservabilityLogger,
|
|
140
|
+
setRequestIdGetter
|
|
109
141
|
};
|
|
@@ -65,23 +65,27 @@ function getMediaType(contentType) {
|
|
|
65
65
|
}
|
|
66
66
|
|
|
67
67
|
function validateMedia(media, contentType) {
|
|
68
|
+
const fileSize = Buffer.isBuffer(media) ? media.length : media;
|
|
69
|
+
|
|
70
|
+
if (contentType === 'image/webp') {
|
|
71
|
+
const mediaType = fileSize <= MEDIA_LIMITS.sticker ? 'sticker' : 'image';
|
|
72
|
+
const formatValidation = validateMediaFormat(contentType, mediaType);
|
|
73
|
+
if (!formatValidation.valid) return formatValidation;
|
|
74
|
+
|
|
75
|
+
const sizeValidation = validateMediaSize(media, mediaType);
|
|
76
|
+
if (!sizeValidation.valid) return sizeValidation;
|
|
77
|
+
|
|
78
|
+
return { valid: true, mediaType, message: `Media validated successfully as ${mediaType}` };
|
|
79
|
+
}
|
|
80
|
+
|
|
68
81
|
const mediaType = getMediaType(contentType);
|
|
69
82
|
const formatValidation = validateMediaFormat(contentType, mediaType);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
return formatValidation;
|
|
73
|
-
}
|
|
74
|
-
|
|
83
|
+
if (!formatValidation.valid) return formatValidation;
|
|
84
|
+
|
|
75
85
|
const sizeValidation = validateMediaSize(media, mediaType);
|
|
76
|
-
if (!sizeValidation.valid)
|
|
77
|
-
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
return {
|
|
81
|
-
valid: true,
|
|
82
|
-
mediaType,
|
|
83
|
-
message: `Media validated successfully as ${mediaType}`
|
|
84
|
-
};
|
|
86
|
+
if (!sizeValidation.valid) return sizeValidation;
|
|
87
|
+
|
|
88
|
+
return { valid: true, mediaType, message: `Media validated successfully as ${mediaType}` };
|
|
85
89
|
}
|
|
86
90
|
|
|
87
91
|
module.exports = {
|
package/lib/utils/sanitizer.js
CHANGED
|
@@ -39,8 +39,6 @@ function sanitizeLogMetadata(metadata) {
|
|
|
39
39
|
delete safe.text;
|
|
40
40
|
delete safe.content;
|
|
41
41
|
delete safe.body;
|
|
42
|
-
delete safe.phone;
|
|
43
|
-
delete safe.numero;
|
|
44
42
|
|
|
45
43
|
if (safe.message_id) {
|
|
46
44
|
safe.message_id = safe.message_id.length > 8
|
|
@@ -48,10 +46,6 @@ function sanitizeLogMetadata(metadata) {
|
|
|
48
46
|
: '***';
|
|
49
47
|
}
|
|
50
48
|
|
|
51
|
-
if (safe.code && safe.code.length > 8) {
|
|
52
|
-
safe.code = `${safe.code.substring(0, 3)}***${safe.code.slice(-4)}`;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
49
|
return safe;
|
|
56
50
|
}
|
|
57
51
|
|
|
@@ -4,9 +4,10 @@ const { SpanStatusCode } = require('@opentelemetry/api');
|
|
|
4
4
|
/**
|
|
5
5
|
* Usage: const tracedFunction = withTracing(originalFunction, 'operation_name');
|
|
6
6
|
*/
|
|
7
|
-
const withTracing = (fn, spanName, attributeMapper = null) => {
|
|
7
|
+
const withTracing = (fn, spanName, attributeMapper = null, options = {}) => {
|
|
8
8
|
return async function (...args) {
|
|
9
9
|
const span = createSpan(spanName);
|
|
10
|
+
const startTime = Date.now();
|
|
10
11
|
|
|
11
12
|
try {
|
|
12
13
|
if (attributeMapper && typeof attributeMapper === 'function') {
|
|
@@ -16,6 +17,11 @@ const withTracing = (fn, spanName, attributeMapper = null) => {
|
|
|
16
17
|
const result = await fn.apply(this, args);
|
|
17
18
|
|
|
18
19
|
span.setStatus({ code: SpanStatusCode.OK });
|
|
20
|
+
|
|
21
|
+
if (options.returnTiming) {
|
|
22
|
+
const duration = Date.now() - startTime;
|
|
23
|
+
return { result, duration };
|
|
24
|
+
}
|
|
19
25
|
return result;
|
|
20
26
|
|
|
21
27
|
} catch (error) {
|