@peopl-health/nexus 6.0.0-dev.15840 → 6.0.0-dev.16228

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.
Files changed (31) hide show
  1. package/lib/clinical/flags/clinicalContextFlags.js +5 -0
  2. package/lib/clinical/flags/flagSnapshot.js +4 -1
  3. package/lib/clinical/helpers/chemoContextHelper.js +2 -1
  4. package/lib/clinical/helpers/triageResultMessageHelper.js +30 -0
  5. package/lib/clinical/memory/DefaultMemoryManager.js +4 -1
  6. package/lib/clinical/models/triageRecommendationEventModel.js +1 -0
  7. package/lib/clinical/providers/BaseLLMProvider.js +19 -10
  8. package/lib/clinical/providers/OpenAIResponsesProviderTools.js +6 -1
  9. package/lib/clinical/services/checkInDueService.js +2 -0
  10. package/lib/clinical/services/radiotherapyContextService.js +40 -0
  11. package/lib/clinical/services/radiotherapyPhaseService.js +272 -0
  12. package/lib/clinical/services/triageRecommendationService.js +13 -0
  13. package/lib/clinical/tools/reportMedicalEscalationTool.js +11 -5
  14. package/lib/clinical/tools/schedulePatientReminderTool.js +1 -1
  15. package/lib/controllers/messageController.js +4 -0
  16. package/lib/controllers/patientController.js +21 -13
  17. package/lib/eval/EvalProvider.js +1 -1
  18. package/lib/helpers/airtableArchiveHelper.js +7 -1
  19. package/lib/helpers/patientMessageHelper.js +3 -1
  20. package/lib/index.d.ts +2 -2
  21. package/lib/models/airtableArchiveModel.js +1 -0
  22. package/lib/services/airtableArchivePlanService.js +12 -5
  23. package/lib/services/airtableArchiveReadService.js +31 -6
  24. package/lib/services/airtableArchiveService.js +4 -4
  25. package/lib/services/escalationReviewService.js +45 -4
  26. package/lib/services/recommendationCarouselService.js +58 -20
  27. package/lib/services/teamAlertService.js +13 -7
  28. package/lib/services/triageHistoryService.js +25 -4
  29. package/lib/utils/dateUtils.js +6 -0
  30. package/lib/utils/jsonUtils.js +21 -0
  31. package/package.json +1 -1
@@ -1,11 +1,15 @@
1
1
  const { Symptoms_ID } = require('../config/airtableConfig');
2
2
 
3
3
  const { MapCache } = require('../utils/MapCache');
4
+ const { logger } = require('../utils/logger');
4
5
  const { paginate } = require('../utils/paginationUtils');
6
+ const { firstOf } = require('../utils/coercionUtils');
5
7
  const { fieldEquals, recordIdIn } = require('../helpers/airtableFormulaHelper');
8
+ const { mergeArchivedRows } = require('../helpers/airtableArchiveHelper');
6
9
  const { TRIAGE_FIELDS, SYMPTOM_FIELDS, buildTriageRecord } = require('../helpers/triageRecordHelper');
7
10
 
8
11
  const { getRecordByFilter } = require('./airtableService');
12
+ const { findArchivedByPatient, findArchivedByRecordIds } = require('./airtableArchiveReadService');
9
13
 
10
14
  const DEFAULT_LIMIT = 50;
11
15
  const HISTORY_TTL_MS = 60 * 1000;
@@ -24,11 +28,23 @@ const withinRange = (row, fromDate, toDate) => {
24
28
 
25
29
  const linkedIdsOf = (value) => (Array.isArray(value) ? value.filter(Boolean) : []);
26
30
 
31
+ // Rows deleted from Airtable exist only in the archive; if it cannot be read, the page shows live rows only.
32
+ const readArchived = async (read, params) => {
33
+ try {
34
+ return await read({ baseId: Symptoms_ID, ...params });
35
+ } catch (error) {
36
+ logger.error('[triageHistoryService] Archive read failed, serving live rows only', { table: params.tableName, error: error.message });
37
+ return [];
38
+ }
39
+ };
40
+
27
41
  const readPatientHistory = async (code) => {
28
- const rows = await getRecordByFilter(
29
- Symptoms_ID, 'triaje_symptoms', fieldEquals('whatsapp_id', code), 'Grid view', TRIAGE_FIELDS,
30
- ) || [];
31
- return { rows, symptomsById: new Map() };
42
+ const [live, archived] = await Promise.all([
43
+ getRecordByFilter(Symptoms_ID, 'triaje_symptoms', fieldEquals('whatsapp_id', code), 'Grid view', TRIAGE_FIELDS, true),
44
+ readArchived(findArchivedByPatient, { tableName: 'triaje_symptoms', whatsappId: code, fields: TRIAGE_FIELDS }),
45
+ ]);
46
+ const ownRows = archived.filter((row) => firstOf(row.whatsapp_id) === code);
47
+ return { rows: mergeArchivedRows(live || [], ownRows), symptomsById: new Map() };
32
48
  };
33
49
 
34
50
  const getPatientHistory = (code) => {
@@ -67,6 +83,11 @@ const getTriageHistory = async ({ code, startDate, endDate, limit, page } = {})
67
83
  Symptoms_ID, 'symptoms', recordIdIn(missingIds), null, SYMPTOM_FIELDS, true,
68
84
  ) || [];
69
85
  for (const symptomRow of symptomRows) symptomsById.set(symptomRow._recordId, symptomRow);
86
+ const archivedIds = missingIds.filter((id) => !symptomsById.has(id));
87
+ if (archivedIds.length) {
88
+ const archivedRows = await readArchived(findArchivedByRecordIds, { tableName: 'symptoms', recordIds: archivedIds, fields: SYMPTOM_FIELDS });
89
+ for (const symptomRow of archivedRows) symptomsById.set(symptomRow._recordId, symptomRow);
90
+ }
70
91
  }
71
92
 
72
93
  const records = pageRows.map((row) => buildTriageRecord(
@@ -57,6 +57,10 @@ const mexicoDay = (value) => {
57
57
  // whole days from one YYYY-MM-DD day to another
58
58
  const daysBetween = (from, to) => Math.round((Date.parse(`${to}T00:00:00Z`) - Date.parse(`${from}T00:00:00Z`)) / 86400000);
59
59
 
60
+ const addDays = (day, days) => new Date(Date.parse(`${day}T00:00:00Z`) + days * 86400000).toISOString().slice(0, 10);
61
+
62
+ const formatDmy = (day) => `${day.slice(8, 10)}/${day.slice(5, 7)}/${day.slice(0, 4)}`;
63
+
60
64
  const mexicoIsoWeek = (value) => {
61
65
  if (value === null || value === undefined || value === '') return null;
62
66
  const at = moment.tz(value instanceof Date ? value.toISOString() : value, moment.ISO_8601, MEXICO_TZ);
@@ -101,6 +105,7 @@ const addWorkingMinutes = (start, minutes, { timeZone, days, startHour, endHour
101
105
  };
102
106
 
103
107
  module.exports = {
108
+ addDays,
104
109
  addWorkingMinutes,
105
110
  getCurrentMexicoDateTime,
106
111
  getMexicoDayLabels,
@@ -110,6 +115,7 @@ module.exports = {
110
115
  ageInYears,
111
116
  isoDay,
112
117
  daysBetween,
118
+ formatDmy,
113
119
  mexicoDay,
114
120
  mexicoClock,
115
121
  mexicoIsoWeek,
@@ -9,6 +9,25 @@ function jsonStringifyWithUnicodeEscapes(obj) {
9
9
  });
10
10
  }
11
11
 
12
+ const LITERAL_ESCAPE_RUN = /(?<!\\)(?:\\u[0-9a-fA-F]{4})+/g;
13
+ const LONE_SURROGATE = /[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/;
14
+
15
+ // Models sometimes double-escape non-ASCII inside tool arguments, leaving a literal "\u00e9" after JSON.parse.
16
+ function decodeLiteralUnicodeEscapes(value) {
17
+ if (typeof value !== 'string' || !value.includes('\\u')) return value;
18
+ return value.replace(LITERAL_ESCAPE_RUN, (run) => {
19
+ const decoded = String.fromCharCode(...run.match(/[0-9a-fA-F]{4}/g).map((hex) => parseInt(hex, 16)));
20
+ return LONE_SURROGATE.test(decoded) ? run : decoded;
21
+ });
22
+ }
23
+
24
+ function normalizeModelStrings(value) {
25
+ if (typeof value === 'string') return decodeLiteralUnicodeEscapes(value);
26
+ if (Array.isArray(value)) return value.map(normalizeModelStrings);
27
+ if (isPlainObject(value)) return Object.fromEntries(Object.entries(value).map(([key, val]) => [key, normalizeModelStrings(val)]));
28
+ return value;
29
+ }
30
+
12
31
  function isPlainObject(value) {
13
32
  if (value === null || typeof value !== 'object') return false;
14
33
  const proto = Object.getPrototypeOf(value);
@@ -73,6 +92,8 @@ function stableStringify(value) {
73
92
 
74
93
  module.exports = {
75
94
  jsonStringifyWithUnicodeEscapes,
95
+ decodeLiteralUnicodeEscapes,
96
+ normalizeModelStrings,
76
97
  stableStringify,
77
98
  safeParse,
78
99
  safeParseArray,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peopl-health/nexus",
3
- "version": "6.0.0-dev.15840",
3
+ "version": "6.0.0-dev.16228",
4
4
  "description": "Core messaging and assistant library for WhatsApp communication platforms",
5
5
  "keywords": [
6
6
  "whatsapp",