@peopl-health/nexus 2.4.7 → 2.4.8
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 +9 -8
- package/lib/helpers/llmsHelper.js +5 -4
- package/lib/helpers/mediaHelper.js +3 -2
- package/lib/helpers/messageHelper.js +12 -11
- package/lib/helpers/processHelper.js +1 -1
- 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 +18 -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 +20 -20
- 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/sanitizer.js +0 -6
- package/package.json +1 -1
|
@@ -5,28 +5,20 @@ const {
|
|
|
5
5
|
getMediaTypeFromContentType,
|
|
6
6
|
extractTitle
|
|
7
7
|
} = require('./twilioHelper');
|
|
8
|
-
const { validateMedia } = require('../utils/mediaValidator');
|
|
9
8
|
const { generatePresignedUrl } = require('../config/awsConfig');
|
|
10
9
|
const { addLinkedRecord } = require('../services/airtableService');
|
|
11
10
|
const { Monitoreo_ID } = require('../config/airtableConfig');
|
|
12
|
-
|
|
13
|
-
const
|
|
14
|
-
if (!logger) return console;
|
|
15
|
-
const fallback = {};
|
|
16
|
-
for (const level of ['info', 'warn', 'error', 'debug']) {
|
|
17
|
-
fallback[level] = typeof logger[level] === 'function' ? logger[level].bind(logger) : console[level].bind(console);
|
|
18
|
-
}
|
|
19
|
-
return fallback;
|
|
20
|
-
};
|
|
11
|
+
const { validateMedia } = require('../utils/mediaValidator');
|
|
12
|
+
const { logger } = require('../utils/logger');
|
|
21
13
|
|
|
22
14
|
const normalizeMediaType = (type) => {
|
|
23
15
|
if (!type || typeof type !== 'string') return 'document';
|
|
24
16
|
return type.replace(/Message$/i, '').replace(/WithCaption$/i, '').toLowerCase();
|
|
25
17
|
};
|
|
26
18
|
|
|
27
|
-
async function uploadMediaToAirtable(whatsappId, mediaUrl,
|
|
19
|
+
async function uploadMediaToAirtable(whatsappId, mediaUrl, baseID, tableName) {
|
|
28
20
|
if (!baseID) {
|
|
29
|
-
|
|
21
|
+
logger.debug('[uploadMediaToAirtable] Base ID not configured; skipping Airtable upload');
|
|
30
22
|
return;
|
|
31
23
|
}
|
|
32
24
|
|
|
@@ -45,25 +37,23 @@ async function uploadMediaToAirtable(whatsappId, mediaUrl, log, baseID, tableNam
|
|
|
45
37
|
}
|
|
46
38
|
);
|
|
47
39
|
|
|
48
|
-
|
|
40
|
+
logger.debug('[uploadMediaToAirtable] Successfully uploaded media to estudios table', { whatsappId });
|
|
49
41
|
} catch (error) {
|
|
50
|
-
|
|
42
|
+
logger.warn('[uploadMediaToAirtable] Failed to upload media to estudios table', { whatsappId, error: error?.message || error });
|
|
51
43
|
throw error;
|
|
52
44
|
}
|
|
53
45
|
}
|
|
54
46
|
|
|
55
|
-
async function processTwilioMediaMessage(twilioMessage,
|
|
47
|
+
async function processTwilioMediaMessage(twilioMessage, bucketName) {
|
|
56
48
|
if (!twilioMessage) return [];
|
|
57
|
-
|
|
58
|
-
const log = ensureLogger(logger);
|
|
59
49
|
const numMedia = parseInt(twilioMessage.NumMedia || '0', 10);
|
|
60
50
|
if (!numMedia || numMedia <= 0) {
|
|
61
|
-
|
|
51
|
+
logger.debug('[TwilioMedia] No media detected in message');
|
|
62
52
|
return [];
|
|
63
53
|
}
|
|
64
54
|
|
|
65
55
|
if (!bucketName) {
|
|
66
|
-
|
|
56
|
+
logger.warn('[TwilioMedia] AWS bucket not configured; skipping media processing');
|
|
67
57
|
return [];
|
|
68
58
|
}
|
|
69
59
|
|
|
@@ -78,25 +68,25 @@ async function processTwilioMediaMessage(twilioMessage, logger, bucketName) {
|
|
|
78
68
|
const contentType = twilioMessage[`MediaContentType${i}`];
|
|
79
69
|
|
|
80
70
|
if (!mediaUrl || !contentType) {
|
|
81
|
-
|
|
71
|
+
logger.warn('[TwilioMedia] Missing media URL or content type', { index: i });
|
|
82
72
|
continue;
|
|
83
73
|
}
|
|
84
74
|
|
|
85
|
-
|
|
75
|
+
logger.info('[TwilioMedia] Processing media item', { index: i, contentType, mediaUrl });
|
|
86
76
|
|
|
87
77
|
let mediaBuffer;
|
|
88
78
|
try {
|
|
89
|
-
mediaBuffer = await downloadMediaFromTwilio(mediaUrl,
|
|
79
|
+
mediaBuffer = await downloadMediaFromTwilio(mediaUrl, logger);
|
|
90
80
|
} catch (error) {
|
|
91
|
-
|
|
81
|
+
logger.error('[TwilioMedia] Failed to download media', { index: i, error: error?.message || error });
|
|
92
82
|
continue;
|
|
93
83
|
}
|
|
94
84
|
|
|
95
85
|
const validationResult = validateMedia(mediaBuffer, contentType);
|
|
96
86
|
if (!validationResult.valid) {
|
|
97
|
-
|
|
87
|
+
logger.warn('[TwilioMedia] Media validation warning', { index: i, message: validationResult.message });
|
|
98
88
|
} else {
|
|
99
|
-
|
|
89
|
+
logger.debug('[TwilioMedia] Media validation passed', { index: i, message: validationResult.message });
|
|
100
90
|
}
|
|
101
91
|
|
|
102
92
|
const helperMediaType = getMediaTypeFromContentType(contentType);
|
|
@@ -119,7 +109,7 @@ async function processTwilioMediaMessage(twilioMessage, logger, bucketName) {
|
|
|
119
109
|
derivedMediaType
|
|
120
110
|
);
|
|
121
111
|
} catch (error) {
|
|
122
|
-
|
|
112
|
+
logger.error('[TwilioMedia] Failed to upload media to S3', { index: i, error: error?.message || error });
|
|
123
113
|
continue;
|
|
124
114
|
}
|
|
125
115
|
|
|
@@ -146,16 +136,16 @@ async function processTwilioMediaMessage(twilioMessage, logger, bucketName) {
|
|
|
146
136
|
item.metadata.presignedUrl = url;
|
|
147
137
|
|
|
148
138
|
if (derivedMediaType !== 'sticker') {
|
|
149
|
-
await uploadMediaToAirtable(code, url,
|
|
139
|
+
await uploadMediaToAirtable(code, url, Monitoreo_ID, 'estudios');
|
|
150
140
|
}
|
|
151
141
|
} catch (error) {
|
|
152
|
-
|
|
142
|
+
logger.warn('[TwilioMedia] Failed to update Airtable with media reference', { index: i, error: error?.message || error });
|
|
153
143
|
}
|
|
154
144
|
|
|
155
145
|
mediaItems.push(item);
|
|
156
146
|
}
|
|
157
147
|
|
|
158
|
-
|
|
148
|
+
logger.info('[TwilioMedia] Completed processing', {
|
|
159
149
|
from: code,
|
|
160
150
|
itemsStored: mediaItems.length,
|
|
161
151
|
bucket: bucketName
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const moment = require('moment-timezone');
|
|
2
|
+
const { logger } = require('../utils/logger');
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
function delay(ms) {
|
|
@@ -7,7 +8,7 @@ function delay(ms) {
|
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
function formatCode(codeBase) {
|
|
10
|
-
|
|
11
|
+
logger.info(`formatCode ${codeBase}`);
|
|
11
12
|
|
|
12
13
|
const [number, domain] = codeBase.split('@');
|
|
13
14
|
|
|
@@ -50,7 +51,7 @@ function calculateDelay(sendTime, timeZone) {
|
|
|
50
51
|
const delay = sendMoment.diff(now) + randomDelay;
|
|
51
52
|
|
|
52
53
|
// Log the calculated details for debugging
|
|
53
|
-
|
|
54
|
+
logger.info(
|
|
54
55
|
'Scheduled Time:', sendMoment.format(),
|
|
55
56
|
'Current Time:', now.format(),
|
|
56
57
|
'Delay (minutes):', delay / 60000,
|
package/lib/index.js
CHANGED
|
@@ -25,9 +25,9 @@ const {
|
|
|
25
25
|
} = require('./services/preprocessingHooks');
|
|
26
26
|
const {
|
|
27
27
|
requestIdMiddleware,
|
|
28
|
-
getRequestId
|
|
29
|
-
logger
|
|
28
|
+
getRequestId
|
|
30
29
|
} = require('./middleware/requestId');
|
|
30
|
+
const { logger } = require('./utils/logger');
|
|
31
31
|
|
|
32
32
|
/**
|
|
33
33
|
* Main Nexus class that orchestrates all components
|
|
@@ -39,7 +39,7 @@ class Nexus {
|
|
|
39
39
|
try {
|
|
40
40
|
setDefaultInstance(this.messaging);
|
|
41
41
|
} catch (err) {
|
|
42
|
-
|
|
42
|
+
logger.warn('[Nexus] Failed to set default messaging instance:', err?.message || err);
|
|
43
43
|
}
|
|
44
44
|
this.storage = null;
|
|
45
45
|
this.messageParser = null;
|
|
@@ -92,11 +92,10 @@ class Nexus {
|
|
|
92
92
|
}
|
|
93
93
|
}
|
|
94
94
|
} catch (e) {
|
|
95
|
-
|
|
95
|
+
logger.warn('Warning: failed to auto-configure template providers:', e?.message || e);
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
-
|
|
99
|
-
// Convenience: handle mongoUri early (before storage connect)
|
|
98
|
+
// Handle mongoUri early (before storage connect)
|
|
100
99
|
try {
|
|
101
100
|
if (options.mongoUri) {
|
|
102
101
|
if (storage === 'mongo') {
|
|
@@ -104,12 +103,11 @@ class Nexus {
|
|
|
104
103
|
storageConfig.mongoUri = options.mongoUri;
|
|
105
104
|
}
|
|
106
105
|
} else {
|
|
107
|
-
// If not using MongoStorage but a URI is provided, initialize default mongoose connection
|
|
108
106
|
await this.messaging.initializeMongoDB(options.mongoUri);
|
|
109
107
|
}
|
|
110
108
|
}
|
|
111
109
|
} catch (dbErr) {
|
|
112
|
-
|
|
110
|
+
logger.warn('[Nexus] mongo convenience warning:', dbErr?.message || dbErr);
|
|
113
111
|
}
|
|
114
112
|
|
|
115
113
|
// Initialize storage if provided
|
|
@@ -119,9 +117,8 @@ class Nexus {
|
|
|
119
117
|
if (typeof storage === 'string') {
|
|
120
118
|
this.storage = createStorage(storage, storageConfig || {});
|
|
121
119
|
} else if (typeof storage === 'object' && (storage.saveMessage || storage.saveInteractive)) {
|
|
122
|
-
this.storage = storage;
|
|
120
|
+
this.storage = storage;
|
|
123
121
|
} else {
|
|
124
|
-
// default to mongo if truthy but not recognized
|
|
125
122
|
this.storage = createStorage('mongo', storageConfig || {});
|
|
126
123
|
}
|
|
127
124
|
if (this.storage && typeof this.storage.connect === 'function') {
|
|
@@ -129,7 +126,7 @@ class Nexus {
|
|
|
129
126
|
}
|
|
130
127
|
this.messaging.setMessageStorage(this.storage);
|
|
131
128
|
} catch (e) {
|
|
132
|
-
|
|
129
|
+
logger.warn('Warning: storage initialization failed:', e?.message || e);
|
|
133
130
|
}
|
|
134
131
|
}
|
|
135
132
|
|
|
@@ -150,7 +147,7 @@ class Nexus {
|
|
|
150
147
|
llmConfigModule.setOpenAIClient(providerInstance.getClient());
|
|
151
148
|
}
|
|
152
149
|
} catch (err) {
|
|
153
|
-
|
|
150
|
+
logger.warn('[Nexus] Failed to expose OpenAI provider:', err?.message || err);
|
|
154
151
|
}
|
|
155
152
|
}
|
|
156
153
|
|
|
@@ -171,7 +168,7 @@ class Nexus {
|
|
|
171
168
|
runtimeConfig.set('AIRTABLE_API_KEY', options.airtable.apiKey);
|
|
172
169
|
}
|
|
173
170
|
} catch (cfgErr) {
|
|
174
|
-
|
|
171
|
+
logger.warn('[Nexus] convenience config warning:', cfgErr?.message || cfgErr);
|
|
175
172
|
}
|
|
176
173
|
|
|
177
174
|
|
|
@@ -195,7 +192,7 @@ class Nexus {
|
|
|
195
192
|
}
|
|
196
193
|
}
|
|
197
194
|
} catch (e) {
|
|
198
|
-
|
|
195
|
+
logger.warn('Warning: failed to configure assistants:', e?.message || e);
|
|
199
196
|
}
|
|
200
197
|
|
|
201
198
|
this.isInitialized = true;
|
package/lib/interactive/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
|
|
2
2
|
const { toTwilioContent } = require('./twilioMapper');
|
|
3
3
|
const { registerFlow, getFlow, listFlows, registerInteractiveHandler, listInteractiveHandlers } = require('./registry');
|
|
4
|
+
const { logger } = require('../utils/logger');
|
|
4
5
|
|
|
5
6
|
async function sendInteractive(nexusOrMessaging, params) {
|
|
6
7
|
const { code, spec, id, variables } = params || {};
|
|
@@ -28,16 +29,6 @@ async function sendInteractive(nexusOrMessaging, params) {
|
|
|
28
29
|
throw new Error('Interactive/flows not supported for this provider');
|
|
29
30
|
}
|
|
30
31
|
|
|
31
|
-
module.exports = {
|
|
32
|
-
registerFlow,
|
|
33
|
-
getFlow,
|
|
34
|
-
listFlows,
|
|
35
|
-
sendInteractive,
|
|
36
|
-
registerInteractiveHandler,
|
|
37
|
-
attachInteractiveRouter
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
|
|
41
32
|
function _matchInteractive(match, interactive) {
|
|
42
33
|
if (!match) return true;
|
|
43
34
|
if (!interactive) return false;
|
|
@@ -76,7 +67,7 @@ function attachInteractiveRouter(nexusOrMessaging) {
|
|
|
76
67
|
await handler(messageData, messaging);
|
|
77
68
|
}
|
|
78
69
|
} catch (e) {
|
|
79
|
-
|
|
70
|
+
logger.warn('Interactive handler error:', e && e.message || e);
|
|
80
71
|
}
|
|
81
72
|
}
|
|
82
73
|
};
|
|
@@ -84,3 +75,12 @@ function attachInteractiveRouter(nexusOrMessaging) {
|
|
|
84
75
|
bus.on('interactive:received', handler);
|
|
85
76
|
return () => bus.off && bus.off('interactive:received', handler);
|
|
86
77
|
}
|
|
78
|
+
|
|
79
|
+
module.exports = {
|
|
80
|
+
registerFlow,
|
|
81
|
+
getFlow,
|
|
82
|
+
listFlows,
|
|
83
|
+
sendInteractive,
|
|
84
|
+
registerInteractiveHandler,
|
|
85
|
+
attachInteractiveRouter
|
|
86
|
+
};
|
|
@@ -1,41 +1,36 @@
|
|
|
1
1
|
const { AsyncLocalStorage } = require('async_hooks');
|
|
2
2
|
const { v4: uuidv4 } = require('uuid');
|
|
3
|
+
const { setRequestIdGetter } = require('../utils/logger');
|
|
3
4
|
|
|
4
5
|
const requestContext = new AsyncLocalStorage();
|
|
5
6
|
|
|
6
7
|
function getRequestId() {
|
|
7
8
|
const store = requestContext.getStore();
|
|
8
|
-
return store?.requestId ||
|
|
9
|
+
return store?.requestId || null;
|
|
9
10
|
}
|
|
10
11
|
|
|
12
|
+
setRequestIdGetter(getRequestId);
|
|
13
|
+
|
|
11
14
|
function requestIdMiddleware(req, res, next) {
|
|
12
15
|
const requestId = uuidv4().substring(0, 8);
|
|
13
16
|
req.requestId = requestId;
|
|
14
17
|
res.setHeader('X-Request-Id', requestId);
|
|
15
18
|
|
|
16
19
|
requestContext.run({ requestId }, () => {
|
|
17
|
-
console.log(`[${requestId}] → ${req.method} ${req.path}`);
|
|
18
|
-
|
|
19
20
|
const startTime = Date.now();
|
|
21
|
+
|
|
20
22
|
res.on('finish', () => {
|
|
21
23
|
const duration = Date.now() - startTime;
|
|
22
|
-
|
|
24
|
+
if (process.env.LOG_HTTP_REQUESTS === 'true') {
|
|
25
|
+
console.log(`[${requestId}] ${req.method} ${req.path} - ${res.statusCode} (${duration}ms)`);
|
|
26
|
+
}
|
|
23
27
|
});
|
|
24
28
|
|
|
25
29
|
next();
|
|
26
30
|
});
|
|
27
31
|
}
|
|
28
32
|
|
|
29
|
-
const logger = {
|
|
30
|
-
log: (...args) => console.log(`[${getRequestId()}]`, ...args),
|
|
31
|
-
error: (...args) => console.error(`[${getRequestId()}]`, ...args),
|
|
32
|
-
warn: (...args) => console.warn(`[${getRequestId()}]`, ...args),
|
|
33
|
-
info: (...args) => console.info(`[${getRequestId()}]`, ...args),
|
|
34
|
-
debug: (...args) => console.debug(`[${getRequestId()}]`, ...args)
|
|
35
|
-
};
|
|
36
|
-
|
|
37
33
|
module.exports = {
|
|
38
34
|
requestIdMiddleware,
|
|
39
|
-
getRequestId
|
|
40
|
-
logger
|
|
35
|
+
getRequestId
|
|
41
36
|
};
|
|
@@ -2,6 +2,7 @@ const mongoose = require('mongoose');
|
|
|
2
2
|
const moment = require('moment-timezone');
|
|
3
3
|
const { getRecordByFilter } = require('../services/airtableService');
|
|
4
4
|
const { Monitoreo_ID } = require('../config/airtableConfig');
|
|
5
|
+
const { logger } = require('../utils/logger');
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
const messageSchema = new mongoose.Schema({
|
|
@@ -79,7 +80,7 @@ async function getClinicalContext(whatsappId) {
|
|
|
79
80
|
}
|
|
80
81
|
return null;
|
|
81
82
|
} catch (error) {
|
|
82
|
-
|
|
83
|
+
logger.error('Error fetching clinical context from Airtable:', error);
|
|
83
84
|
return null;
|
|
84
85
|
}
|
|
85
86
|
}
|
|
@@ -116,9 +117,9 @@ async function insertMessage(values) {
|
|
|
116
117
|
{ upsert: true, new: true }
|
|
117
118
|
);
|
|
118
119
|
|
|
119
|
-
|
|
120
|
+
logger.info('[MongoStorage] Message inserted or updated successfully');
|
|
120
121
|
} catch (err) {
|
|
121
|
-
|
|
122
|
+
logger.error('[MongoStorage] Error inserting message:', err);
|
|
122
123
|
throw err;
|
|
123
124
|
}
|
|
124
125
|
}
|
|
@@ -173,7 +174,7 @@ async function getContactDisplayName(contactNumber) {
|
|
|
173
174
|
return contactNumber;
|
|
174
175
|
}
|
|
175
176
|
} catch (error) {
|
|
176
|
-
|
|
177
|
+
logger.error('[MongoStorage] Error fetching display name for ${contactNumber}:', error);
|
|
177
178
|
return contactNumber;
|
|
178
179
|
}
|
|
179
180
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { OpenAI } = require('openai');
|
|
2
2
|
const { retryWithBackoff } = require('../utils/retryHelper');
|
|
3
|
+
const { logger } = require('../utils/logger');
|
|
3
4
|
|
|
4
5
|
const DEFAULT_MAX_RETRIES = parseInt(process.env.MAX_RETRIES || '10', 10);
|
|
5
6
|
const PROVIDER_NAME = 'OpenAIAssistantsProvider';
|
|
@@ -274,7 +275,7 @@ class OpenAIAssistantsProvider {
|
|
|
274
275
|
const result = await assistant.executeTool(name, args);
|
|
275
276
|
outputs.push({ tool_call_id: call.id, output: typeof result === 'string' ? result : JSON.stringify(result) });
|
|
276
277
|
} catch (error) {
|
|
277
|
-
|
|
278
|
+
logger.error('[OpenAIAssistantsProvider] Tool execution failed', error);
|
|
278
279
|
outputs.push({
|
|
279
280
|
tool_call_id: call.id,
|
|
280
281
|
output: JSON.stringify({ success: false, error: error?.message || 'Tool execution failed' })
|
|
@@ -294,19 +295,19 @@ class OpenAIAssistantsProvider {
|
|
|
294
295
|
async checkRunStatus(assistant, thread_id, run_id, retryCount = 0, maxRetries = DEFAULT_MAX_RETRIES, actionHandled = false) {
|
|
295
296
|
try {
|
|
296
297
|
const run = await this.getRun({ threadId: thread_id, runId: run_id });
|
|
297
|
-
|
|
298
|
+
logger.info(`Status: ${run.status} ${thread_id} ${run_id} (attempt ${retryCount + 1})`);
|
|
298
299
|
|
|
299
300
|
const failedStatuses = ['failed', 'expired', 'incomplete', 'errored'];
|
|
300
301
|
if (failedStatuses.includes(run.status)) {
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
302
|
+
logger.info(`Run failed. ${run.status} `);
|
|
303
|
+
logger.info('Error:');
|
|
304
|
+
logger.info(run);
|
|
304
305
|
return {run, completed: false};
|
|
305
306
|
} else if (run.status === 'cancelled') {
|
|
306
|
-
|
|
307
|
+
logger.info('cancelled');
|
|
307
308
|
return {run, completed: true};
|
|
308
309
|
} else if (run.status === 'requires_action') {
|
|
309
|
-
|
|
310
|
+
logger.info('requires_action');
|
|
310
311
|
if (retryCount >= maxRetries) {
|
|
311
312
|
return {run, completed: false};
|
|
312
313
|
}
|
|
@@ -325,11 +326,11 @@ class OpenAIAssistantsProvider {
|
|
|
325
326
|
await new Promise(resolve => setTimeout(resolve, 1000));
|
|
326
327
|
return this.checkRunStatus(assistant, thread_id, run_id, retryCount + 1, maxRetries, actionHandled);
|
|
327
328
|
} else {
|
|
328
|
-
|
|
329
|
+
logger.info('Run completed.');
|
|
329
330
|
return {run, completed: true};
|
|
330
331
|
}
|
|
331
332
|
} catch (error) {
|
|
332
|
-
|
|
333
|
+
logger.error('Error checking run status:', error);
|
|
333
334
|
return {run: null, completed: false};
|
|
334
335
|
}
|
|
335
336
|
}
|
|
@@ -5,6 +5,7 @@ const {
|
|
|
5
5
|
handleRequiresAction: handleRequiresActionUtil,
|
|
6
6
|
transformToolsForResponsesAPI: transformToolsForResponsesAPIUtil
|
|
7
7
|
} = require('./OpenAIResponsesProviderTools');
|
|
8
|
+
const { logger } = require('../utils/logger');
|
|
8
9
|
|
|
9
10
|
const CONVERSATION_PREFIX = 'conv_';
|
|
10
11
|
const RESPONSE_PREFIX = 'resp_';
|
|
@@ -81,7 +82,7 @@ class OpenAIResponsesProvider {
|
|
|
81
82
|
: messages;
|
|
82
83
|
|
|
83
84
|
if (messages.length > DEFAULT_MAX_HISTORICAL_MESSAGES) {
|
|
84
|
-
|
|
85
|
+
logger.warn(`[OpenAIResponsesProvider] Capped ${messages.length} → ${DEFAULT_MAX_HISTORICAL_MESSAGES} messages`);
|
|
85
86
|
}
|
|
86
87
|
|
|
87
88
|
const allItems = this._conversationItems(messagesToProcess);
|
|
@@ -108,7 +109,7 @@ class OpenAIResponsesProvider {
|
|
|
108
109
|
const totalBatches = Math.ceil(remainingItems.length / MAX_ITEMS_PER_BATCH);
|
|
109
110
|
|
|
110
111
|
// Concise batch summary
|
|
111
|
-
|
|
112
|
+
logger.info(`[OpenAIResponsesProvider] Batching: ${initialItems.length} (create) + ${remainingItems.length} (${totalBatches} batches) = ${totalItems} total`);
|
|
112
113
|
|
|
113
114
|
const payload = this._cleanObject({
|
|
114
115
|
metadata,
|
|
@@ -125,7 +126,7 @@ class OpenAIResponsesProvider {
|
|
|
125
126
|
return await this._post('/conversations', payload);
|
|
126
127
|
});
|
|
127
128
|
} catch (error) {
|
|
128
|
-
|
|
129
|
+
logger.error('[OpenAIResponsesProvider] Failed to create conversation:', error?.message || error);
|
|
129
130
|
throw error;
|
|
130
131
|
}
|
|
131
132
|
|
|
@@ -134,7 +135,7 @@ class OpenAIResponsesProvider {
|
|
|
134
135
|
try {
|
|
135
136
|
await this._addItemsInBatches(conversation.id, remainingItems);
|
|
136
137
|
} catch (error) {
|
|
137
|
-
|
|
138
|
+
logger.error('[OpenAIResponsesProvider] Failed to add remaining messages. Conversation created with partial history:', error?.message || error);
|
|
138
139
|
}
|
|
139
140
|
}
|
|
140
141
|
|
|
@@ -168,12 +169,12 @@ class OpenAIResponsesProvider {
|
|
|
168
169
|
return await this._post(`/conversations/${id}/items`, { items: batchPayload });
|
|
169
170
|
});
|
|
170
171
|
} catch (error) {
|
|
171
|
-
|
|
172
|
+
logger.error(`[OpenAIResponsesProvider] Batch ${batchNumber}/${totalBatches} failed:`, error?.message || error);
|
|
172
173
|
throw error;
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
|
|
176
|
-
|
|
177
|
+
logger.info(`[OpenAIResponsesProvider] Successfully added ${items.length} messages in ${totalBatches} batches`);
|
|
177
178
|
}
|
|
178
179
|
|
|
179
180
|
/**
|
|
@@ -235,7 +236,7 @@ class OpenAIResponsesProvider {
|
|
|
235
236
|
if (items.length === 0) return;
|
|
236
237
|
|
|
237
238
|
if (deleteAll) {
|
|
238
|
-
|
|
239
|
+
logger.info(`[OpenAIResponsesProvider] Deleting all ${items.length} items from conversation`);
|
|
239
240
|
for (const item of items) {
|
|
240
241
|
await this.conversations.items.delete(item.id, {conversation_id: id});
|
|
241
242
|
}
|
|
@@ -249,12 +250,12 @@ class OpenAIResponsesProvider {
|
|
|
249
250
|
const hasOutput = functionOutputs.some(output => output.call_id === functionCall.call_id);
|
|
250
251
|
|
|
251
252
|
if (!hasOutput) {
|
|
252
|
-
|
|
253
|
+
logger.info(`[OpenAIResponsesProvider] Deleting orphaned function_call: ${functionCall.id} (${functionCall.call_id})`);
|
|
253
254
|
await this.conversations.items.delete(functionCall.id, {conversation_id: id});
|
|
254
255
|
}
|
|
255
256
|
}
|
|
256
257
|
} catch (error) {
|
|
257
|
-
|
|
258
|
+
logger.warn('[OpenAIResponsesProvider] Failed to cleanup conversation:', error?.message);
|
|
258
259
|
}
|
|
259
260
|
}
|
|
260
261
|
|
|
@@ -288,7 +289,7 @@ class OpenAIResponsesProvider {
|
|
|
288
289
|
toolOutputs = pendingOutputs;
|
|
289
290
|
}
|
|
290
291
|
} catch (error) {
|
|
291
|
-
|
|
292
|
+
logger.warn('[OpenAIResponsesProvider] Error checking for pending function calls:', error?.message);
|
|
292
293
|
}
|
|
293
294
|
}
|
|
294
295
|
|
|
@@ -324,7 +325,7 @@ class OpenAIResponsesProvider {
|
|
|
324
325
|
object: response.object || 'response',
|
|
325
326
|
};
|
|
326
327
|
} catch (error) {
|
|
327
|
-
|
|
328
|
+
logger.error('[OpenAIResponsesProvider] Error running conversation:', error);
|
|
328
329
|
throw error;
|
|
329
330
|
}
|
|
330
331
|
}
|
|
@@ -419,7 +420,7 @@ class OpenAIResponsesProvider {
|
|
|
419
420
|
async checkRunStatus(assistant, thread_id, run_id, retryCount = 0, maxRetries = DEFAULT_MAX_RETRIES, actionHandled = false) {
|
|
420
421
|
try {
|
|
421
422
|
let run = await this.getRun({ threadId: thread_id, runId: run_id });
|
|
422
|
-
|
|
423
|
+
logger.info(`Status: ${run.status} ${thread_id} ${run_id} (attempt ${retryCount + 1})`);
|
|
423
424
|
|
|
424
425
|
if (run.status === 'completed') {
|
|
425
426
|
return {run, completed: true};
|
|
@@ -432,12 +433,12 @@ class OpenAIResponsesProvider {
|
|
|
432
433
|
const needsFunctionCall = run.output?.some(item => item.type === 'function_call');
|
|
433
434
|
if (needsFunctionCall && !actionHandled) {
|
|
434
435
|
if (retryCount >= maxRetries) {
|
|
435
|
-
|
|
436
|
+
logger.warn('[OpenAIResponsesProvider] Max retries reached while handling function calls');
|
|
436
437
|
return {run, completed: false};
|
|
437
438
|
}
|
|
438
439
|
|
|
439
440
|
const outputs = await handleRequiresActionUtil(assistant, run);
|
|
440
|
-
|
|
441
|
+
logger.info('[OpenAIResponsesProvider] Function call outputs:', outputs);
|
|
441
442
|
|
|
442
443
|
if (outputs.length > 0) {
|
|
443
444
|
try {
|
|
@@ -451,7 +452,7 @@ class OpenAIResponsesProvider {
|
|
|
451
452
|
|
|
452
453
|
return this.checkRunStatus(assistant, thread_id, run_id, retryCount + 1, maxRetries, true);
|
|
453
454
|
} catch (submitError) {
|
|
454
|
-
|
|
455
|
+
logger.error('[OpenAIResponsesProvider] Error submitting tool outputs:', submitError);
|
|
455
456
|
if (retryCount < maxRetries) {
|
|
456
457
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
457
458
|
return this.checkRunStatus(assistant, thread_id, run_id, retryCount + 1, maxRetries, false);
|
|
@@ -459,7 +460,7 @@ class OpenAIResponsesProvider {
|
|
|
459
460
|
return {run, completed: false};
|
|
460
461
|
}
|
|
461
462
|
} else {
|
|
462
|
-
|
|
463
|
+
logger.warn('[OpenAIResponsesProvider] Function calls detected but no outputs generated');
|
|
463
464
|
return {run, completed: false};
|
|
464
465
|
}
|
|
465
466
|
}
|
|
@@ -471,7 +472,7 @@ class OpenAIResponsesProvider {
|
|
|
471
472
|
|
|
472
473
|
return {run, completed: false};
|
|
473
474
|
} catch (error) {
|
|
474
|
-
|
|
475
|
+
logger.error('[OpenAIResponsesProvider] Error checking run status:', error);
|
|
475
476
|
return {run: null, completed: false};
|
|
476
477
|
}
|
|
477
478
|
}
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
* Tool and function call handling utilities for OpenAIResponsesProvider
|
|
3
|
-
*/
|
|
1
|
+
const { logger } = require('../utils/logger');
|
|
4
2
|
|
|
5
3
|
/**
|
|
6
4
|
* Execute a function call and return the output format
|
|
@@ -19,7 +17,7 @@ async function executeFunctionCall(assistant, call) {
|
|
|
19
17
|
output: typeof result === 'string' ? result : JSON.stringify(result)
|
|
20
18
|
};
|
|
21
19
|
} catch (error) {
|
|
22
|
-
|
|
20
|
+
logger.error('[OpenAIResponsesProvider] Tool execution failed', error);
|
|
23
21
|
return {
|
|
24
22
|
type: 'function_call_output',
|
|
25
23
|
call_id: call.call_id,
|
|
@@ -46,7 +44,7 @@ async function handlePendingFunctionCalls(assistant, conversationItems) {
|
|
|
46
44
|
return [];
|
|
47
45
|
}
|
|
48
46
|
|
|
49
|
-
|
|
47
|
+
logger.info(`[OpenAIResponsesProvider] Found ${orphanedCalls.length} pending function calls, handling them...`);
|
|
50
48
|
const outputs = [];
|
|
51
49
|
for (const call of orphanedCalls) {
|
|
52
50
|
outputs.push(await executeFunctionCall(assistant, call));
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { OpenAIAssistantsProvider } = require('./OpenAIAssistantsProvider');
|
|
2
2
|
const { OpenAIResponsesProvider } = require('./OpenAIResponsesProvider');
|
|
3
|
+
const { logger } = require('../utils/logger');
|
|
3
4
|
|
|
4
5
|
const PROVIDER_VARIANTS = {
|
|
5
6
|
assistants: OpenAIAssistantsProvider,
|
|
@@ -10,7 +11,7 @@ const PROVIDER_VARIANTS = {
|
|
|
10
11
|
* Returns the appropriate OpenAI provider implementation for the requested variant.
|
|
11
12
|
*/
|
|
12
13
|
function createProvider(config = {}) {
|
|
13
|
-
|
|
14
|
+
logger.debug('Creating OpenAI provider', { variant: config.variant || 'assistants' });
|
|
14
15
|
const variant = (config.variant || config.providerVariant || config.llmVariant || 'assistants')
|
|
15
16
|
.toString()
|
|
16
17
|
.toLowerCase();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const { airtable } = require('../config/airtableConfig');
|
|
2
|
+
const { logger } = require('../utils/logger');
|
|
2
3
|
|
|
3
4
|
|
|
4
5
|
async function addRecord(baseID, tableName, fields) {
|
|
@@ -6,10 +7,10 @@ async function addRecord(baseID, tableName, fields) {
|
|
|
6
7
|
if (!airtable) throw new Error('Airtable not configured. Set AIRTABLE_API_KEY');
|
|
7
8
|
const base = airtable.base(baseID);
|
|
8
9
|
const record = await base(tableName).create(fields);
|
|
9
|
-
|
|
10
|
+
logger.info('Record added at', tableName);
|
|
10
11
|
return record;
|
|
11
12
|
} catch (error) {
|
|
12
|
-
|
|
13
|
+
logger.error('Error adding record:', error);
|
|
13
14
|
throw error;
|
|
14
15
|
}
|
|
15
16
|
}
|
|
@@ -30,7 +31,7 @@ async function getRecords(baseID, tableName) {
|
|
|
30
31
|
});
|
|
31
32
|
return records;
|
|
32
33
|
} catch (error) {
|
|
33
|
-
|
|
34
|
+
logger.error('Error fetching records:', error);
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
|
|
@@ -51,7 +52,7 @@ async function getRecordByFilter(baseID, tableName, filter, view = 'Grid view')
|
|
|
51
52
|
});
|
|
52
53
|
return records;
|
|
53
54
|
} catch (error) {
|
|
54
|
-
|
|
55
|
+
logger.error(`Error fetching records by ${filter}:`, error);
|
|
55
56
|
}
|
|
56
57
|
}
|
|
57
58
|
|
|
@@ -74,7 +75,7 @@ async function updateRecordByFilter(baseID, tableName, filter, updateFields) {
|
|
|
74
75
|
|
|
75
76
|
return updatedRecords;
|
|
76
77
|
} catch (error) {
|
|
77
|
-
|
|
78
|
+
logger.error(`Error updating records by ${filter}:`, error);
|
|
78
79
|
}
|
|
79
80
|
}
|
|
80
81
|
|