@peopl-health/nexus 4.7.0-dev.449 → 4.7.0-dev.450
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.
|
@@ -5,7 +5,6 @@ const { withTracing } = require('../../utils/tracingDecorator');
|
|
|
5
5
|
const { logger } = require('../../utils/logger');
|
|
6
6
|
|
|
7
7
|
const { getRecordByFilter } = require('../../services/airtableService.js');
|
|
8
|
-
const { withThreadRecovery } = require('./threadRecoveryHelper');
|
|
9
8
|
|
|
10
9
|
function getCurRow(baseID, code) {
|
|
11
10
|
if (code.endsWith('@g.us')) {
|
|
@@ -29,11 +28,7 @@ const runAssistantAndWait = async ({ thread, assistant, message = null, runConfi
|
|
|
29
28
|
const { polling, tools: configTools, ...config } = runConfig;
|
|
30
29
|
const tools = assistant.getToolSchemas?.() || configTools || [];
|
|
31
30
|
|
|
32
|
-
return
|
|
33
|
-
(currentThread = thread) => provider.executeRun({ thread: currentThread, message, assistant, tools, config, polling }),
|
|
34
|
-
thread,
|
|
35
|
-
variant
|
|
36
|
-
);
|
|
31
|
+
return provider.executeRun({ thread, message, assistant, tools, config, polling });
|
|
37
32
|
};
|
|
38
33
|
|
|
39
34
|
const runAssistantWithRetries = async (thread, assistant, runConfig, patientReply = null) => {
|
package/lib/clinical/index.js
CHANGED
|
@@ -15,7 +15,6 @@ const { BaseAssistant } = require('./assistants/BaseAssistant');
|
|
|
15
15
|
const { AssistantProcessor } = require('./AssistantProcessor');
|
|
16
16
|
|
|
17
17
|
const { getCurRow, runAssistantAndWait, runAssistantWithRetries } = require('./helpers/assistantHelper');
|
|
18
|
-
const { withThreadRecovery } = require('./helpers/threadRecoveryHelper');
|
|
19
18
|
const { analyzeImage, buildPrompt } = require('./helpers/llmsHelper');
|
|
20
19
|
|
|
21
20
|
const { getProviderVariant, getAnthropicClient, setOpenAIProvider, getOpenAIProvider, requireOpenAIProvider, configureOpenAIProvider } = require('./config/llmConfig');
|
|
@@ -47,7 +46,6 @@ module.exports = {
|
|
|
47
46
|
getCurRow,
|
|
48
47
|
runAssistantAndWait,
|
|
49
48
|
runAssistantWithRetries,
|
|
50
|
-
withThreadRecovery,
|
|
51
49
|
analyzeImage,
|
|
52
50
|
buildPrompt,
|
|
53
51
|
getProviderVariant,
|
|
@@ -14,7 +14,7 @@ const { fetchConversationData, processConversations, startConversation } = requi
|
|
|
14
14
|
const { getMessages, countMessages, aggregateMessages } = require('../services/messageService');
|
|
15
15
|
|
|
16
16
|
const { sendMessage } = require('../core/NexusMessaging');
|
|
17
|
-
const {
|
|
17
|
+
const { getProviderVariant } = require('../clinical');
|
|
18
18
|
|
|
19
19
|
const Message = mongoose.models.Message;
|
|
20
20
|
|
|
@@ -470,65 +470,45 @@ const markMessagesAsReadController = async (req, res) => {
|
|
|
470
470
|
const getOpenAIThreadMessagesController = async (req, res) => {
|
|
471
471
|
try {
|
|
472
472
|
const { code } = req.params;
|
|
473
|
-
const {
|
|
474
|
-
|
|
475
|
-
|
|
473
|
+
const { order = 'desc' } = req.query;
|
|
474
|
+
|
|
476
475
|
if (!code) {
|
|
477
476
|
return res.status(400).json({
|
|
478
477
|
success: false,
|
|
479
478
|
error: 'Phone number is required'
|
|
480
479
|
});
|
|
481
480
|
}
|
|
482
|
-
|
|
483
|
-
|
|
481
|
+
|
|
482
|
+
const thread = await Thread.findOne({ code: code }).sort({ createdAt: -1 });
|
|
484
483
|
if (!thread) {
|
|
485
484
|
return res.status(404).json({
|
|
486
485
|
success: false,
|
|
487
|
-
error: 'No active
|
|
486
|
+
error: 'No active thread found for this phone number'
|
|
488
487
|
});
|
|
489
488
|
}
|
|
490
|
-
|
|
491
|
-
const
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
threadId: thread.conversation_id,
|
|
496
|
-
order,
|
|
497
|
-
limit: parseInt(limit),
|
|
498
|
-
...(runId && { runId })
|
|
499
|
-
};
|
|
500
|
-
let threadRecreated = false;
|
|
501
|
-
|
|
502
|
-
const messages = await withThreadRecovery(
|
|
503
|
-
async (currentThread = thread) => {
|
|
504
|
-
if (currentThread !== thread) {
|
|
505
|
-
thread = currentThread;
|
|
506
|
-
queryParams.threadId = currentThread.getConversationId();
|
|
507
|
-
threadRecreated = true;
|
|
508
|
-
}
|
|
509
|
-
return await provider.listMessages(queryParams);
|
|
510
|
-
},
|
|
511
|
-
thread,
|
|
512
|
-
variant
|
|
489
|
+
|
|
490
|
+
const parsedLimit = Math.min(Math.max(parseInt(req.query.limit) || 50, 1), 100);
|
|
491
|
+
const messages = await getMessages(
|
|
492
|
+
{ numero: code, group_id: null },
|
|
493
|
+
{ sort: { createdAt: order === 'asc' ? 1 : -1 }, limit: parsedLimit }
|
|
513
494
|
);
|
|
514
|
-
|
|
495
|
+
messages.forEach(msg => { msg.body = msg.plainBody || msg.body; });
|
|
496
|
+
|
|
515
497
|
res.status(200).json({
|
|
516
498
|
success: true,
|
|
517
|
-
variant,
|
|
499
|
+
variant: getProviderVariant(),
|
|
518
500
|
phoneNumber: code,
|
|
519
501
|
code,
|
|
520
502
|
threadId: thread.conversation_id,
|
|
521
503
|
assistantId: thread.assistant_id,
|
|
522
|
-
messages
|
|
523
|
-
|
|
524
|
-
threadRecreated,
|
|
525
|
-
pagination: { limit: parseInt(limit), order }
|
|
504
|
+
messages,
|
|
505
|
+
pagination: { limit: parsedLimit, order }
|
|
526
506
|
});
|
|
527
507
|
} catch (error) {
|
|
528
|
-
logger.error('Error fetching
|
|
529
|
-
res.status(500).json({
|
|
530
|
-
success: false,
|
|
531
|
-
error: error.message || 'Failed to fetch
|
|
508
|
+
logger.error('Error fetching thread messages:', { error: error.message });
|
|
509
|
+
res.status(500).json({
|
|
510
|
+
success: false,
|
|
511
|
+
error: error.message || 'Failed to fetch thread messages'
|
|
532
512
|
});
|
|
533
513
|
}
|
|
534
514
|
};
|
package/package.json
CHANGED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
const { createLLMProvider } = require('../providers/createLLMProvider');
|
|
2
|
-
const { logger } = require('../../utils/logger');
|
|
3
|
-
|
|
4
|
-
const { Thread } = require('../../models/threadModel');
|
|
5
|
-
|
|
6
|
-
const THREAD_ERROR_CODES = ['thread_not_found', 'invalid_thread_id'];
|
|
7
|
-
const THREAD_ERROR_MESSAGES = ['No thread found', 'thread does not exist', 'Invalid thread'];
|
|
8
|
-
|
|
9
|
-
const isThreadNotFoundError = (error) =>
|
|
10
|
-
error?.status === 404 ||
|
|
11
|
-
THREAD_ERROR_CODES.includes(error?.code) ||
|
|
12
|
-
THREAD_ERROR_MESSAGES.some(msg => error?.message?.includes(msg));
|
|
13
|
-
|
|
14
|
-
const recreateThread = async (thread, variant) => {
|
|
15
|
-
const provider = createLLMProvider({ variant });
|
|
16
|
-
const newConversation = await provider.createConversation({
|
|
17
|
-
metadata: { code: thread.code }
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
await Thread.updateOne(
|
|
21
|
-
{ code: thread.code },
|
|
22
|
-
{ $set: { conversation_id: newConversation.id } }
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
logger.info('[threadRecovery] Recreated', {
|
|
26
|
-
oldId: thread.conversation_id,
|
|
27
|
-
newId: newConversation.id,
|
|
28
|
-
code: thread.code
|
|
29
|
-
});
|
|
30
|
-
return Thread.findOne({ code: thread.code });
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const withThreadRecovery = async (operation, thread, variant) => {
|
|
34
|
-
try {
|
|
35
|
-
return await operation();
|
|
36
|
-
} catch (error) {
|
|
37
|
-
if (isThreadNotFoundError(error) && thread) {
|
|
38
|
-
logger.warn('[threadRecovery] Thread not found, recreating', { code: thread.code });
|
|
39
|
-
const recoveredThread = await recreateThread(thread, variant);
|
|
40
|
-
return operation(recoveredThread);
|
|
41
|
-
}
|
|
42
|
-
throw error;
|
|
43
|
-
}
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
module.exports = {
|
|
47
|
-
isThreadNotFoundError,
|
|
48
|
-
recreateThread,
|
|
49
|
-
withThreadRecovery
|
|
50
|
-
};
|