@peopl-health/nexus 3.13.4 → 3.13.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.
- package/lib/helpers/threadHelper.js +26 -20
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { Config_ID, Monitoreo_ID } = require('../config/airtableConfig.js');
|
|
1
|
+
const { Config_ID, Monitoreo_ID, Historial_Clinico_ID } = require('../config/airtableConfig.js');
|
|
2
2
|
const runtimeConfig = require('../config/runtimeConfig.js');
|
|
3
3
|
|
|
4
4
|
const { logger } = require('../utils/logger');
|
|
@@ -49,30 +49,36 @@ const setThreadPromptId = async (code, promptId) => {
|
|
|
49
49
|
.catch(err => logger.error('[setThreadPromptId] Failed to update message_monitor', { code, error: err.message }));
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
+
const createThread = async (code, { assistantId = null, fallbackName = null }) => {
|
|
53
|
+
const nodeEnv = runtimeConfig.get('NODE_ENV');
|
|
54
|
+
const assistantStatus = nodeEnv === 'production' ? 'prod' : nodeEnv === 'development' ? 'dev' : nodeEnv;
|
|
55
|
+
const configRecord = await getRecordByFilter(Config_ID, 'responses', `AND({code}="PIPO_ASST", {status}="${assistantStatus}")`, undefined, ['prompt_id']);
|
|
56
|
+
const patientRecord = await getRecordByFilter(Historial_Clinico_ID, 'estado_general', `{whatsapp_id} = "${code}"`, undefined, ['name', 'record_id']);
|
|
57
|
+
|
|
58
|
+
const promptId = assistantId || configRecord?.[0]?.['prompt_id'] || null;
|
|
59
|
+
const patientName = patientRecord?.[0]?.['name'] || patientRecord?.[0]?.['whatsapp_name'] || fallbackName;
|
|
60
|
+
const patientId = patientRecord?.[0]?.['record_id'] || null;
|
|
61
|
+
|
|
62
|
+
const thread = {
|
|
63
|
+
code,
|
|
64
|
+
...(patientName && { nombre: patientName }),
|
|
65
|
+
patient_id: patientId
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const condition = { code };
|
|
69
|
+
const options = { upsert: true, new: true };
|
|
70
|
+
await Thread.findOneAndUpdate( condition, { ...thread }, options);
|
|
71
|
+
await switchThreadStoppedStatus(code, true);
|
|
72
|
+
await setThreadPromptId(code, promptId);
|
|
73
|
+
return await Thread.findOne({ code });
|
|
74
|
+
};
|
|
75
|
+
|
|
52
76
|
const createPlaceholderThread = async (code) => {
|
|
53
77
|
try {
|
|
54
78
|
const existing = await Thread.findOne({ code });
|
|
55
79
|
if (existing?.stopped !== undefined) return existing;
|
|
56
80
|
|
|
57
|
-
|
|
58
|
-
const assistantStatus = nodeEnv === 'production' ? 'prod' : nodeEnv === 'development' ? 'dev' : nodeEnv;
|
|
59
|
-
const record = await getRecordByFilter(Config_ID, 'responses', `AND({code}="PIPO_ASST", {status}="${assistantStatus}")`, undefined, ['prompt_id']);
|
|
60
|
-
const prompt_id = record?.[0]?.['prompt_id'] || null;
|
|
61
|
-
|
|
62
|
-
const thread = new Thread({ code, assistant_id: null, conversation_id: null });
|
|
63
|
-
await thread.save();
|
|
64
|
-
await switchThreadStoppedStatus(code, true);
|
|
65
|
-
await setThreadPromptId(code, prompt_id);
|
|
66
|
-
logger.info('[createPlaceholderThread] Created', { code, prompt_id });
|
|
67
|
-
|
|
68
|
-
if (prompt_id) {
|
|
69
|
-
// Imported here to avoid circular dependency
|
|
70
|
-
const { createAssistant } = require('../services/assistantService.js');
|
|
71
|
-
await createAssistant(code, prompt_id, [], true);
|
|
72
|
-
logger.info('[createPlaceholderThread] Assistant created', { code, prompt_id });
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return thread;
|
|
81
|
+
return await createThread(code, { assistantId: null, fallbackName: null });
|
|
76
82
|
} catch (error) {
|
|
77
83
|
logger.error('[createPlaceholderThread] Error', { code, error: error.message });
|
|
78
84
|
return null;
|