@peopl-health/nexus 2.1.3 → 2.1.5
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.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const mongoose = require('mongoose');
|
|
2
2
|
const { fetchConversationData, processConversations } = require('../services/conversationService');
|
|
3
|
-
const { sendMessage } = require('../core/NexusMessaging');
|
|
3
|
+
const { sendMessage } = require('../core/NexusMessaging');
|
|
4
|
+
const { Thread } = require('../models/threadModel');
|
|
5
|
+
const llmConfig = require('../config/llmConfig');
|
|
4
6
|
|
|
5
7
|
const Message = mongoose.models.Message;
|
|
6
8
|
|
|
@@ -580,6 +582,86 @@ const sendTemplateToNewNumberController = async (req, res) => {
|
|
|
580
582
|
}
|
|
581
583
|
};
|
|
582
584
|
|
|
585
|
+
const getOpenAIThreadMessagesController = async (req, res) => {
|
|
586
|
+
console.log('Starting getOpenAIThreadMessagesController at', new Date().toISOString());
|
|
587
|
+
try {
|
|
588
|
+
const { phoneNumber } = req.params;
|
|
589
|
+
const { limit = 50, order = 'desc', runId } = req.query;
|
|
590
|
+
const variant = process.env.VARIANT || 'assistants';
|
|
591
|
+
|
|
592
|
+
console.log('Fetching OpenAI thread messages for:', phoneNumber);
|
|
593
|
+
console.log('Variant:', variant, 'Limit:', limit, 'Order:', order);
|
|
594
|
+
|
|
595
|
+
if (!phoneNumber) {
|
|
596
|
+
return res.status(400).json({
|
|
597
|
+
success: false,
|
|
598
|
+
error: 'phoneNumber is required'
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const thread = await Thread.findOne({
|
|
603
|
+
code: phoneNumber,
|
|
604
|
+
active: true
|
|
605
|
+
}).sort({ createdAt: -1 });
|
|
606
|
+
|
|
607
|
+
if (!thread) {
|
|
608
|
+
console.log('No active OpenAI thread found for:', phoneNumber);
|
|
609
|
+
return res.status(404).json({
|
|
610
|
+
success: false,
|
|
611
|
+
error: 'No active OpenAI thread found for this phone number'
|
|
612
|
+
});
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
const conversationId = thread.getConversationId();
|
|
616
|
+
console.log('Thread found - Conversation ID:', conversationId);
|
|
617
|
+
|
|
618
|
+
const provider = llmConfig.getOpenAIProvider({ instantiate: true });
|
|
619
|
+
if (!provider) {
|
|
620
|
+
throw new Error('OpenAI provider not initialized');
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
console.log('Using provider variant:', provider.getVariant());
|
|
624
|
+
|
|
625
|
+
const queryParams = {
|
|
626
|
+
threadId: conversationId,
|
|
627
|
+
order,
|
|
628
|
+
limit: parseInt(limit)
|
|
629
|
+
};
|
|
630
|
+
|
|
631
|
+
if (variant === 'assistants' && runId) {
|
|
632
|
+
queryParams.runId = runId;
|
|
633
|
+
console.log('Including runId:', runId);
|
|
634
|
+
}
|
|
635
|
+
|
|
636
|
+
console.log('Calling listMessages with params:', queryParams);
|
|
637
|
+
const messages = await provider.listMessages(queryParams);
|
|
638
|
+
|
|
639
|
+
console.log(`Retrieved ${messages?.data?.length || 0} messages from OpenAI`);
|
|
640
|
+
|
|
641
|
+
res.status(200).json({
|
|
642
|
+
success: true,
|
|
643
|
+
variant,
|
|
644
|
+
phoneNumber,
|
|
645
|
+
threadId: conversationId,
|
|
646
|
+
assistantId: thread.assistant_id,
|
|
647
|
+
messages: messages.data || messages,
|
|
648
|
+
hasMore: messages.has_more || false,
|
|
649
|
+
pagination: {
|
|
650
|
+
limit: parseInt(limit),
|
|
651
|
+
order
|
|
652
|
+
}
|
|
653
|
+
});
|
|
654
|
+
|
|
655
|
+
} catch (error) {
|
|
656
|
+
console.error('Error fetching OpenAI thread messages:', error);
|
|
657
|
+
console.error('Error stack:', error.stack);
|
|
658
|
+
res.status(500).json({
|
|
659
|
+
success: false,
|
|
660
|
+
error: error.message || 'Failed to fetch OpenAI thread messages'
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
};
|
|
664
|
+
|
|
583
665
|
module.exports = {
|
|
584
666
|
getConversationController,
|
|
585
667
|
getConversationMessagesController,
|
|
@@ -588,5 +670,6 @@ module.exports = {
|
|
|
588
670
|
getNewMessagesController,
|
|
589
671
|
markMessagesAsReadController,
|
|
590
672
|
searchConversationsController,
|
|
591
|
-
sendTemplateToNewNumberController
|
|
673
|
+
sendTemplateToNewNumberController,
|
|
674
|
+
getOpenAIThreadMessagesController
|
|
592
675
|
};
|
|
@@ -13,7 +13,7 @@ const getPatientInfoController = async (req, res) => {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
const clinicalContext = records[0]['clinical-context-json'];
|
|
16
|
-
res.status(200).json({ success: true, whatsappId: id, clinicalContext });
|
|
16
|
+
res.status(200).json({ success: true, whatsappId: id, clinicalContext, patientInfo: records[0] });
|
|
17
17
|
} catch (error) {
|
|
18
18
|
console.error('Error fetching patient info:', error);
|
|
19
19
|
res.status(500).json({ success: false, error: error.message });
|
package/lib/routes/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const conversationRouteDefinitions = {
|
|
|
18
18
|
'GET /by-name': 'getConversationsByNameController',
|
|
19
19
|
'GET /:phoneNumber': 'getConversationMessagesController',
|
|
20
20
|
'GET /:phoneNumber/new': 'getNewMessagesController',
|
|
21
|
+
'GET /:phoneNumber/openai': 'getOpenAIThreadMessagesController',
|
|
21
22
|
'POST /reply': 'getConversationReplyController',
|
|
22
23
|
'POST /send-template': 'sendTemplateToNewNumberController',
|
|
23
24
|
'POST /:phoneNumber/read': 'markMessagesAsReadController',
|
|
@@ -110,6 +111,7 @@ const builtInControllers = {
|
|
|
110
111
|
getConversationsByNameController: conversationController.getConversationsByNameController,
|
|
111
112
|
getConversationMessagesController: conversationController.getConversationMessagesController,
|
|
112
113
|
getNewMessagesController: conversationController.getNewMessagesController,
|
|
114
|
+
getOpenAIThreadMessagesController: conversationController.getOpenAIThreadMessagesController,
|
|
113
115
|
getConversationReplyController: conversationController.getConversationReplyController,
|
|
114
116
|
sendTemplateToNewNumberController: conversationController.sendTemplateToNewNumberController,
|
|
115
117
|
markMessagesAsReadController: conversationController.markMessagesAsReadController,
|