@peopl-health/nexus 5.75.0-dev.15396 → 5.75.0
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,4 +1,5 @@
|
|
|
1
1
|
const PENDING_INSTRUCTION_WINDOW_MS = 2 * 60 * 1000;
|
|
2
2
|
const DEFAULT_INSTRUCTION_SETTLE_MS = 2500;
|
|
3
|
+
const DUPLICATE_INSTRUCTION_WINDOW_MS = 60 * 60 * 1000;
|
|
3
4
|
|
|
4
|
-
module.exports = { PENDING_INSTRUCTION_WINDOW_MS, DEFAULT_INSTRUCTION_SETTLE_MS };
|
|
5
|
+
module.exports = { PENDING_INSTRUCTION_WINDOW_MS, DEFAULT_INSTRUCTION_SETTLE_MS, DUPLICATE_INSTRUCTION_WINDOW_MS };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const { Config_ID } = require('../config/airtableConfig');
|
|
2
2
|
const runtimeConfig = require('../config/runtimeConfig');
|
|
3
|
+
const { DUPLICATE_INSTRUCTION_WINDOW_MS } = require('../config/instructionConfig');
|
|
3
4
|
|
|
4
5
|
const { logger } = require('../utils/logger');
|
|
5
6
|
const { isArchivedPatientCode } = require('../helpers/twilioHelper');
|
|
@@ -63,7 +64,10 @@ const addInsAssistantController = async (req, res) => {
|
|
|
63
64
|
if (!code) return res.status(400).json({ success: false, error: 'Code is required' });
|
|
64
65
|
|
|
65
66
|
try {
|
|
66
|
-
await processInstruction(code, instruction, 'developer', { triggeredBy });
|
|
67
|
+
const result = await processInstruction(code, instruction, 'developer', { triggeredBy, dedupeWithinMs: DUPLICATE_INSTRUCTION_WINDOW_MS });
|
|
68
|
+
if (result?.outcome === 'duplicate') {
|
|
69
|
+
return res.status(200).json({ success: true, duplicate: true, message: 'Instruction already answered' });
|
|
70
|
+
}
|
|
67
71
|
return res.status(200).json({ success: true, message: 'Instruction added to assistant' });
|
|
68
72
|
} catch (error) {
|
|
69
73
|
logger.error('[AssistantController] Add instruction error', { error: error.message, code });
|
|
@@ -845,7 +845,11 @@ class NexusMessaging {
|
|
|
845
845
|
* batch, and is marked processed when vetoed or refused by a draining instance, so it can only ever
|
|
846
846
|
* run behind its own gate.
|
|
847
847
|
*/
|
|
848
|
-
async processInstruction(code, instruction, role = 'developer', { triggeredBy, beforeRun = null } = {}) {
|
|
848
|
+
async processInstruction(code, instruction, role = 'developer', { triggeredBy, beforeRun = null, dedupeWithinMs = 0 } = {}) {
|
|
849
|
+
if (dedupeWithinMs > 0 && await this._instructionAnsweredRecently(code, instruction, dedupeWithinMs)) {
|
|
850
|
+
logger.info('[processInstruction] Identical instruction already answered; skipping the resend', { code, triggeredBy: triggeredBy || null });
|
|
851
|
+
return { outcome: 'duplicate' };
|
|
852
|
+
}
|
|
849
853
|
const receivedAt = Date.now();
|
|
850
854
|
const messageData = {
|
|
851
855
|
pushName: 'Instruction',
|
|
@@ -882,12 +886,14 @@ class NexusMessaging {
|
|
|
882
886
|
let runId = null;
|
|
883
887
|
let gate = null;
|
|
884
888
|
let alreadyAnswered = false;
|
|
889
|
+
let duplicate = false;
|
|
885
890
|
const answerPending = () => {
|
|
886
891
|
batchIds = [];
|
|
887
892
|
outcome = null;
|
|
888
893
|
runId = null;
|
|
889
894
|
gate = null;
|
|
890
895
|
alreadyAnswered = false;
|
|
896
|
+
duplicate = false;
|
|
891
897
|
return this._executeWithPipeline(code, 'instruction', 'queue',
|
|
892
898
|
async (preProcessResult, shouldContinue, currentRunId) => {
|
|
893
899
|
runId = currentRunId;
|
|
@@ -903,6 +909,12 @@ class NexusMessaging {
|
|
|
903
909
|
return null;
|
|
904
910
|
}
|
|
905
911
|
}
|
|
912
|
+
if (dedupeWithinMs > 0 && await this._instructionAnsweredRecently(code, instruction, dedupeWithinMs, _id)) {
|
|
913
|
+
logger.info('[processInstruction] Identical instruction answered while this one waited; skipping it', { code, triggeredBy: triggeredBy || null });
|
|
914
|
+
duplicate = true;
|
|
915
|
+
await markOwnProcessed();
|
|
916
|
+
return null;
|
|
917
|
+
}
|
|
906
918
|
const batch = await this._pendingInstructionBatch(code, _id, body, receivedAt);
|
|
907
919
|
if (!batch.length) {
|
|
908
920
|
logger.info('[processInstruction] Instruction already answered by an earlier run', { code, runId });
|
|
@@ -958,6 +970,7 @@ class NexusMessaging {
|
|
|
958
970
|
// Only the send outcome goes back: the turn's output was already delivered, and handing it to
|
|
959
971
|
// the caller would invite a second send.
|
|
960
972
|
if (gate) return gate;
|
|
973
|
+
if (duplicate) return { outcome: 'duplicate' };
|
|
961
974
|
return { outcome: outcome === null && alreadyAnswered ? 'already_answered' : outcome };
|
|
962
975
|
},
|
|
963
976
|
);
|
|
@@ -966,6 +979,28 @@ class NexusMessaging {
|
|
|
966
979
|
}
|
|
967
980
|
}
|
|
968
981
|
|
|
982
|
+
async _instructionAnsweredRecently(code, instruction, windowMs, ownId = null) {
|
|
983
|
+
try {
|
|
984
|
+
const rows = await getMessages(
|
|
985
|
+
{
|
|
986
|
+
numero: code,
|
|
987
|
+
origin: 'instruction',
|
|
988
|
+
message_id: /^instruction_/,
|
|
989
|
+
plainBody: instruction,
|
|
990
|
+
processed: true,
|
|
991
|
+
'raw.gated': { $ne: true },
|
|
992
|
+
createdAt: { $gte: new Date(Date.now() - windowMs) },
|
|
993
|
+
...(ownId ? { _id: { $ne: ownId } } : {}),
|
|
994
|
+
},
|
|
995
|
+
{ sort: { _id: -1 }, limit: 1, select: '_id' },
|
|
996
|
+
);
|
|
997
|
+
return Array.isArray(rows) && rows.length > 0;
|
|
998
|
+
} catch (err) {
|
|
999
|
+
logger.error('[processInstruction] Could not check for an answered duplicate', { error: err.message, code });
|
|
1000
|
+
return false;
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
|
|
969
1004
|
async _pendingInstructionBatch(code, ownId, ownBody, receivedAt) {
|
|
970
1005
|
if (!ownId) return [{ _id: null, body: ownBody }];
|
|
971
1006
|
try {
|
package/lib/index.d.ts
CHANGED
|
@@ -272,7 +272,7 @@ declare module '@peopl-health/nexus' {
|
|
|
272
272
|
getLlmMonitor(): { start(options?: { scheduleDaily?: boolean }): Promise<{ enabled: boolean }> } | null;
|
|
273
273
|
initializeLlmMonitor(): Promise<{ enabled: boolean; error?: string } | undefined>;
|
|
274
274
|
getWorkflowRunner(): WorkflowRunner;
|
|
275
|
-
processInstruction(code: string, instruction: string, role?: string, options?: { triggeredBy?: string; beforeRun?: () => Promise<string | null | undefined> | string | null | undefined }): Promise<{ outcome: string | null; reason?: string; error?: string }>;
|
|
275
|
+
processInstruction(code: string, instruction: string, role?: string, options?: { triggeredBy?: string; beforeRun?: () => Promise<string | null | undefined> | string | null | undefined; dedupeWithinMs?: number }): Promise<{ outcome: string | null; reason?: string; error?: string }>;
|
|
276
276
|
processSystemMessage(code: string, messages: string | string[], role?: string, options?: { triggeredBy?: string; reply?: boolean }): Promise<void>;
|
|
277
277
|
processOutreach(code: string, options?: { brief?: OutreachBrief | null; triggeredBy?: string | null; reason?: string | null; firstName?: string | null; dryRun?: boolean; replyToContact?: boolean }): Promise<OutreachDecision>;
|
|
278
278
|
processCheckInDue(code: string, options: { reminderId: string; type: 'contingencia' | 'seguimiento'; question?: string; firstName?: string | null }): Promise<CheckInDueResult>;
|