@peopl-health/nexus 6.0.15 → 6.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.
|
@@ -83,7 +83,7 @@ const createAssistantCore = async (code, assistant_id, _messages = [], force = f
|
|
|
83
83
|
};
|
|
84
84
|
Thread.setConversationId(thread, initialThread.id);
|
|
85
85
|
|
|
86
|
-
const condition = {
|
|
86
|
+
const condition = { code };
|
|
87
87
|
const options = { new: true, upsert: true };
|
|
88
88
|
const updatedThread = await Thread.findOneAndUpdate(condition, { ...thread }, options);
|
|
89
89
|
await setThreadPromptId(code, assistant_id);
|
|
@@ -119,6 +119,16 @@ messageSchema.index({ triggeredBy: 1, createdAt: -1 }, { name: 'triggered_by_idx
|
|
|
119
119
|
|
|
120
120
|
const Message = mongoose.model('Message', messageSchema);
|
|
121
121
|
|
|
122
|
+
const INTERNAL_ORIGINS = new Set(['system', 'instruction']);
|
|
123
|
+
|
|
124
|
+
// A stored text message carries a media object of schema defaults, so only a populated one counts as media.
|
|
125
|
+
const threadPreview = (message, at) => ({
|
|
126
|
+
lastMessageAt: at,
|
|
127
|
+
lastMessageBody: message.plainBody || '',
|
|
128
|
+
lastMessageFromMe: !!message.from_me,
|
|
129
|
+
lastMessageMedia: message.media && Object.values(message.media).some((value) => value != null) ? message.media : null,
|
|
130
|
+
});
|
|
131
|
+
|
|
122
132
|
function getMessageValues(message, content) {
|
|
123
133
|
return {
|
|
124
134
|
nombre_whatsapp: message.pushName,
|
|
@@ -132,5 +142,7 @@ function getMessageValues(message, content) {
|
|
|
132
142
|
|
|
133
143
|
module.exports = {
|
|
134
144
|
Message,
|
|
145
|
+
INTERNAL_ORIGINS,
|
|
146
|
+
threadPreview,
|
|
135
147
|
getMessageValues,
|
|
136
148
|
};
|
|
@@ -30,7 +30,7 @@ const threadSchema = new mongoose.Schema({
|
|
|
30
30
|
lastMessageMedia: { type: Object, default: null }
|
|
31
31
|
}, { timestamps: true });
|
|
32
32
|
|
|
33
|
-
threadSchema.index({ code: 1 });
|
|
33
|
+
threadSchema.index({ code: 1 }, { unique: true });
|
|
34
34
|
threadSchema.index({ thread_id: 1 });
|
|
35
35
|
threadSchema.index({ conversation_id: 1 });
|
|
36
36
|
threadSchema.index({ lastMessageAt: -1 }, { name: 'last_message_at_idx', sparse: true });
|
|
@@ -2,15 +2,13 @@ const { Monitoreo_ID } = require('../config/airtableConfig');
|
|
|
2
2
|
|
|
3
3
|
const { logger } = require('../utils/logger');
|
|
4
4
|
|
|
5
|
-
const { Message } = require('../models/messageModel');
|
|
5
|
+
const { Message, INTERNAL_ORIGINS, threadPreview } = require('../models/messageModel');
|
|
6
6
|
const { Thread } = require('../models/threadModel');
|
|
7
7
|
|
|
8
8
|
const { getClinicalContext } = require('../helpers/patientInformationHelper');
|
|
9
9
|
|
|
10
10
|
const { updateRecordByFilter } = require('../services/airtableService');
|
|
11
11
|
|
|
12
|
-
const INTERNAL_ORIGINS = new Set(['system', 'instruction']);
|
|
13
|
-
|
|
14
12
|
async function getMessages(query, options = {}) {
|
|
15
13
|
let cursor = Message.find(query);
|
|
16
14
|
if (options.select) cursor = cursor.select(options.select);
|
|
@@ -61,12 +59,7 @@ async function insertMessage(values) {
|
|
|
61
59
|
Thread.findOneAndUpdate(
|
|
62
60
|
{ code: values.numero },
|
|
63
61
|
{
|
|
64
|
-
$set:
|
|
65
|
-
lastMessageAt: new Date(),
|
|
66
|
-
lastMessageBody: values.plainBody || '',
|
|
67
|
-
lastMessageFromMe: !!values.from_me,
|
|
68
|
-
lastMessageMedia: values.media || null
|
|
69
|
-
},
|
|
62
|
+
$set: threadPreview(values, new Date()),
|
|
70
63
|
$inc: {
|
|
71
64
|
messageCount: 1,
|
|
72
65
|
...(!values.from_me ? { unreadCount: 1 } : {})
|
|
@@ -2,7 +2,7 @@ const { logger } = require('../utils/logger');
|
|
|
2
2
|
const { normalize } = require('../utils/tokenMapUtils');
|
|
3
3
|
const { mapStringsDeep } = require('../utils/sanitizerUtils');
|
|
4
4
|
|
|
5
|
-
const { Message } = require('../models/messageModel');
|
|
5
|
+
const { Message, INTERNAL_ORIGINS, threadPreview } = require('../models/messageModel');
|
|
6
6
|
const { Thread } = require('../models/threadModel');
|
|
7
7
|
const { getTokenMap } = require('../models/tokenMapModel');
|
|
8
8
|
const { ScheduledMessage } = require('../models/agendaMessageModel');
|
|
@@ -173,15 +173,32 @@ const rewriteOps = (docs, setFor) => docs.map((doc) => ({
|
|
|
173
173
|
updateOne: { filter: { _id: doc._id }, update: { $set: setFor(doc) } },
|
|
174
174
|
}));
|
|
175
175
|
|
|
176
|
+
// The new number may write mid-migration and get its own thread; the source thread is then archived like a merge.
|
|
177
|
+
const renameThread = (thread, target) => Thread.updateOne({ _id: thread._id }, { $set: { code: target } }).then(() => true, (error) => {
|
|
178
|
+
if (error.code !== 11000) throw error;
|
|
179
|
+
return false;
|
|
180
|
+
});
|
|
181
|
+
|
|
176
182
|
async function moveThreads(source, target, state, migrationId) {
|
|
177
|
-
const archived = `archived:${source}`;
|
|
178
183
|
const [newest, ...rest] = [...state.sourceThreads].sort((a, b) => new Date(b.createdAt || 0) - new Date(a.createdAt || 0));
|
|
179
|
-
const
|
|
180
|
-
|
|
184
|
+
const renamed = !state.targetThread && !!newest && await renameThread(newest, target);
|
|
185
|
+
const toArchive = renamed ? rest : state.sourceThreads;
|
|
181
186
|
if (toArchive.length) {
|
|
182
|
-
await Thread.
|
|
187
|
+
await Thread.bulkWrite(rewriteOps(toArchive, (doc) => ({ code: `archived:${source}:${doc._id}`, stopped: true })));
|
|
183
188
|
}
|
|
184
|
-
logger.debug('[migratePatientCode] Threads moved', { migrationId, renamed
|
|
189
|
+
logger.debug('[migratePatientCode] Threads moved', { migrationId, renamed, archived: toArchive.length });
|
|
190
|
+
return renamed;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async function countMovedMessages(target, messages) {
|
|
194
|
+
const counted = messages.filter((doc) => !INTERNAL_ORIGINS.has(doc.origin));
|
|
195
|
+
if (!counted.length) return;
|
|
196
|
+
const latest = await Message.findOne({ _id: { $in: counted.map((doc) => doc._id) } }, null, { sort: { createdAt: -1 } }).lean();
|
|
197
|
+
await Thread.updateOne({ code: target }, { $inc: { messageCount: counted.length } });
|
|
198
|
+
await Thread.updateOne(
|
|
199
|
+
{ code: target, $or: [{ lastMessageAt: null }, { lastMessageAt: { $lt: latest.createdAt } }] },
|
|
200
|
+
{ $set: threadPreview(latest, latest.createdAt) },
|
|
201
|
+
);
|
|
185
202
|
}
|
|
186
203
|
|
|
187
204
|
async function migratePatientCode({ from, to, asCaregiver = false, apply = false, operator = null } = {}) {
|
|
@@ -233,7 +250,8 @@ async function migratePatientCode({ from, to, asCaregiver = false, apply = false
|
|
|
233
250
|
await getPatientMemory().bulkWrite(rewriteOps(state.memories, (doc) => ({ ...retokenizedFields(doc, mapping), numero: target })));
|
|
234
251
|
}
|
|
235
252
|
|
|
236
|
-
await moveThreads(source, target, state, migrationId);
|
|
253
|
+
const renamed = await moveThreads(source, target, state, migrationId);
|
|
254
|
+
if (!renamed) await countMovedMessages(target, state.messages);
|
|
237
255
|
|
|
238
256
|
if (state.scheduled.length && !asCaregiver) {
|
|
239
257
|
await ScheduledMessage.bulkWrite(rewriteOps(state.scheduled, (doc) => ({ code: sameFormat(doc.code, target) })));
|