@peopl-health/nexus 2.0.16 → 2.0.17
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/basic-usage.js
CHANGED
|
@@ -6,8 +6,16 @@ const app = express();
|
|
|
6
6
|
app.use(express.json());
|
|
7
7
|
|
|
8
8
|
async function startServer() {
|
|
9
|
-
// Initialize Nexus with all services
|
|
10
|
-
const nexus = new Nexus(
|
|
9
|
+
// Initialize Nexus with all services and batching enabled
|
|
10
|
+
const nexus = new Nexus({
|
|
11
|
+
messaging: {
|
|
12
|
+
messageBatching: {
|
|
13
|
+
enabled: true,
|
|
14
|
+
baseWaitTime: 10000, // 10 seconds base wait (shorter for testing)
|
|
15
|
+
randomVariation: 5000 // 0-5 seconds random variation
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
});
|
|
11
19
|
|
|
12
20
|
await nexus.initialize({
|
|
13
21
|
// MongoDB connection
|
|
@@ -34,6 +42,7 @@ async function startServer() {
|
|
|
34
42
|
// Add webhook endpoint for incoming messages
|
|
35
43
|
app.post('/webhook', async (req, res) => {
|
|
36
44
|
try {
|
|
45
|
+
console.log('📨 Received webhook:', JSON.stringify(req.body, null, 2));
|
|
37
46
|
await nexus.processMessage(req.body);
|
|
38
47
|
res.status(200).send('OK');
|
|
39
48
|
} catch (error) {
|
|
@@ -73,11 +82,17 @@ async function startServer() {
|
|
|
73
82
|
console.log(`Server running on port ${PORT}`);
|
|
74
83
|
console.log('Available endpoints:');
|
|
75
84
|
console.log('- POST /webhook - Webhook for incoming messages');
|
|
85
|
+
console.log('- POST /twilio/webhook - Twilio webhook for incoming messages');
|
|
76
86
|
console.log('- GET /status - Connection status');
|
|
77
87
|
console.log('- GET /airtable-test - Test Airtable connection');
|
|
78
88
|
console.log('- GET /media-test - Test AWS media upload configuration');
|
|
79
89
|
console.log('- All Nexus API routes under /api/*');
|
|
80
90
|
console.log('');
|
|
91
|
+
console.log('🔄 Message Batching:');
|
|
92
|
+
console.log(' - Batching is ENABLED with 10-15 second wait times');
|
|
93
|
+
console.log(' - Multiple messages from same sender will be batched');
|
|
94
|
+
console.log(' - Use test-batching.js to test the functionality');
|
|
95
|
+
console.log('');
|
|
81
96
|
console.log('📸 Media Upload Setup:');
|
|
82
97
|
console.log(' Add these to your .env file for media upload:');
|
|
83
98
|
console.log(' - AWS_ACCESS_KEY_ID=your_access_key');
|
|
@@ -325,11 +325,12 @@ class BaseAssistant {
|
|
|
325
325
|
|
|
326
326
|
async close() {
|
|
327
327
|
this.status = 'closed';
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
{
|
|
332
|
-
|
|
328
|
+
const assistantId = process.env.VARIANT === 'responses' ? this.thread?.prompt_id : this.thread?.assistant_id;
|
|
329
|
+
if (this.thread?.code && assistantId) {
|
|
330
|
+
const filter = process.env.VARIANT === 'responses'
|
|
331
|
+
? { code: this.thread.code, prompt_id: assistantId }
|
|
332
|
+
: { code: this.thread.code, assistant_id: assistantId };
|
|
333
|
+
await Thread.updateOne(filter, { $set: { active: false } });
|
|
333
334
|
}
|
|
334
335
|
|
|
335
336
|
if (this.assistantId) {
|
|
@@ -303,9 +303,10 @@ async function processIndividualMessage(code, reply, provider, thread) {
|
|
|
303
303
|
console.log(`[processIndividualMessage] User message added to thread ${threadId}`);
|
|
304
304
|
}
|
|
305
305
|
|
|
306
|
+
const assistantId = process.env.VARIANT === 'responses' ? thread.prompt_id : thread.assistant_id;
|
|
306
307
|
await Message.updateOne(
|
|
307
308
|
{ message_id: reply.message_id, timestamp: reply.timestamp },
|
|
308
|
-
{ $set: { assistant_id:
|
|
309
|
+
{ $set: { assistant_id: assistantId, thread_id: threadId } }
|
|
309
310
|
);
|
|
310
311
|
|
|
311
312
|
return {isNotAssistant, url};
|
|
@@ -197,7 +197,15 @@ const createAssistant = async (code, assistant_id, messages=[], force=false) =>
|
|
|
197
197
|
console.log('[createAssistant] findThread', findThread);
|
|
198
198
|
if ((findThread?.conversation_id && variant === 'responses') || (findThread?.thread_id && variant === 'assistants')) {
|
|
199
199
|
console.log('[createAssistant] Thread already exists');
|
|
200
|
-
|
|
200
|
+
const updateFields = { active: true, stopped: false };
|
|
201
|
+
|
|
202
|
+
if (variant === 'responses') {
|
|
203
|
+
updateFields.prompt_id = assistant_id;
|
|
204
|
+
} else {
|
|
205
|
+
updateFields.assistant_id = assistant_id;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
await Thread.updateOne({ code: code }, { $set: updateFields });
|
|
201
209
|
return findThread;
|
|
202
210
|
}
|
|
203
211
|
|
|
@@ -454,7 +462,16 @@ const switchAssistant = async (code, assistant_id) => {
|
|
|
454
462
|
console.log('Inside thread', thread);
|
|
455
463
|
if (thread === null) return;
|
|
456
464
|
|
|
457
|
-
|
|
465
|
+
const variant = process.env.VARIANT || 'assistants';
|
|
466
|
+
const updateFields = { active: true, stopped: false };
|
|
467
|
+
|
|
468
|
+
if (variant === 'responses') {
|
|
469
|
+
updateFields.prompt_id = assistant_id;
|
|
470
|
+
} else {
|
|
471
|
+
updateFields.assistant_id = assistant_id;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
await Thread.updateOne({ code }, { $set: updateFields });
|
|
458
475
|
} catch (error) {
|
|
459
476
|
console.log(error);
|
|
460
477
|
return null;
|