@resolveio/server-lib 22.3.208 → 22.3.210

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.
@@ -7408,6 +7408,7 @@ function extractAssistantDataIntentCustomerText(message) {
7408
7408
  return '';
7409
7409
  }
7410
7410
  value = value.replace(/\b(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|october|oct|november|nov|december|dec)\s+[0-9]{4}\b/gi, ' ');
7411
+ value = value.replace(/\b(?:last|past|previous|over\s+the\s+last)\s+(?:few|couple|couple\s+of|several)\s+(?:days?|weeks?|months?|years?)\b/gi, ' ');
7411
7412
  value = value.replace(/\b(?:last|past)\s+[0-9]+\s+(?:days?|weeks?|months?)\b/gi, ' ');
7412
7413
  value = value.replace(/\b(?:this|current)\s+(?:week|month|quarter|year)\b/gi, ' ');
7413
7414
  value = value.replace(/\b(?:last|past)\s+(?:week|month|quarter|year)\b/gi, ' ');
@@ -7444,6 +7445,7 @@ function extractAssistantDataIntentLikelyFilterText(message, intent) {
7444
7445
  }
7445
7446
  value = value.replace(/\b(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|october|oct|november|nov|december)\s+[0-9]{4}\b/gi, ' ');
7446
7447
  value = value.replace(/\b(january|jan|february|feb|march|mar|april|apr|may|june|jun|july|jul|august|aug|september|sep|october|oct|november|nov|december)\b/gi, ' ');
7448
+ value = value.replace(/\b(?:last|past|previous|over\s+the\s+last)\s+(?:few|couple|couple\s+of|several)\s+(?:days?|weeks?|months?|years?)\b/gi, ' ');
7447
7449
  value = value.replace(/\b(?:last|past)\s+[0-9]+\s+(?:days?|weeks?|months?)\b/gi, ' ');
7448
7450
  value = value.replace(/\b(?:this|current)\s+(?:week|month|quarter|year)\b/gi, ' ');
7449
7451
  value = value.replace(/\b(?:last|past)\s+(?:week|month|quarter|year)\b/gi, ' ');
@@ -7543,6 +7545,17 @@ function scoreAssistantLikelyFilterCandidate(message, intent, likelyFilterText,
7543
7545
  score += 6;
7544
7546
  }
7545
7547
  }
7548
+ if (__spreadArray([], __read(candidateTerms), false).some(function (term) { return ['division', 'divisions'].includes(term); })) {
7549
+ if (/\bdivisions?\b/i.test(messageText)) {
7550
+ score += 8;
7551
+ }
7552
+ if (new RegExp("\\b".concat(escapeRegexValue(likelyFilterText), "\\b"), 'i').test(messageText)) {
7553
+ score += 3;
7554
+ }
7555
+ if (/\b[a-z0-9&.' -]+\s+divisions?\b/i.test(likelyFilterText)) {
7556
+ score += 8;
7557
+ }
7558
+ }
7546
7559
  if (__spreadArray([], __read(candidateTerms), false).some(function (term) { return ['location', 'locations', 'yard', 'yards', 'warehouse', 'warehouses'].includes(term); })) {
7547
7560
  if (/\b(location|locations|yard|yards|warehouse|warehouses|site|sites)\b/i.test(messageText)) {
7548
7561
  score += 4;
@@ -8005,6 +8018,35 @@ function resolveAssistantDataIntentDateWindow(message) {
8005
8018
  year: now.getUTCFullYear()
8006
8019
  };
8007
8020
  }
8021
+ var approximateLastWindowMatch = text.match(/\b(?:last|past|previous|over\s+the\s+last)\s+(few|couple|couple\s+of|several)\s+(days?|weeks?|months?|years?)\b/);
8022
+ if (approximateLastWindowMatch) {
8023
+ var amountText = approximateLastWindowMatch[1];
8024
+ var amount = amountText.includes('couple')
8025
+ ? 2
8026
+ : amountText === 'several'
8027
+ ? 6
8028
+ : 3;
8029
+ var unit = approximateLastWindowMatch[2];
8030
+ var start = new Date(now.getTime());
8031
+ if (/year/.test(unit)) {
8032
+ start.setUTCFullYear(start.getUTCFullYear() - amount);
8033
+ }
8034
+ else if (/month/.test(unit)) {
8035
+ start.setUTCMonth(start.getUTCMonth() - amount);
8036
+ }
8037
+ else if (/week/.test(unit)) {
8038
+ start.setUTCDate(start.getUTCDate() - (amount * 7));
8039
+ }
8040
+ else {
8041
+ start.setUTCDate(start.getUTCDate() - amount);
8042
+ }
8043
+ start.setUTCHours(0, 0, 0, 0);
8044
+ return {
8045
+ start: start.toISOString(),
8046
+ end: new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 23, 59, 59, 999)).toISOString(),
8047
+ year: now.getUTCFullYear()
8048
+ };
8049
+ }
8008
8050
  var lastWindowMatch = text.match(/\b(?:last|past)\s+([0-9]+)\s+(days?|weeks?|months?)\b/);
8009
8051
  if (lastWindowMatch) {
8010
8052
  var amount = Math.max(1, Math.min(Number(lastWindowMatch[1]) || 1, 36));
@@ -8339,7 +8381,7 @@ function buildAssistantDataIntentTimeDimension(requestedDimensionText, intent) {
8339
8381
  _b[id] = "$_id.".concat(id),
8340
8382
  _b),
8341
8383
  sort: (_c = {},
8342
- _c[id] = 1,
8384
+ _c[id] = -1,
8343
8385
  _c)
8344
8386
  };
8345
8387
  }
@@ -10131,6 +10173,27 @@ function buildAssistantFieldFilterCondition(value, operator, type) {
10131
10173
  }
10132
10174
  return { $regex: escaped, $options: 'i' };
10133
10175
  }
10176
+ function normalizeAssistantFieldAwareFilterValue(value, candidate) {
10177
+ if (candidate.type && candidate.type !== 'string') {
10178
+ return value;
10179
+ }
10180
+ var candidateTerms = __spreadArray(__spreadArray([
10181
+ candidate.id
10182
+ ], __read((candidate.terms || [])), false), __read((candidate.fields || [])), false).map(function (term) { return cleanAssistantRequestedDimensionText(term); })
10183
+ .filter(Boolean);
10184
+ var isDivisionCandidate = candidateTerms.some(function (term) { return term === 'division' || term === 'divisions' || term.endsWith(' division'); });
10185
+ if (!isDivisionCandidate) {
10186
+ return value;
10187
+ }
10188
+ var text = normalizeOptionalString(value);
10189
+ if (!text) {
10190
+ return value;
10191
+ }
10192
+ var normalized = normalizeOptionalString(text
10193
+ .replace(/\bdivision(?:s)?\b/gi, ' ')
10194
+ .replace(/\s+/g, ' '));
10195
+ return normalized || value;
10196
+ }
10134
10197
  function buildAssistantContractFilterClause(spec, candidate, options) {
10135
10198
  var operator = normalizeAssistantContractFilterOperator(spec.operator);
10136
10199
  if (candidate.type === 'number' && spec.values.length === 1) {
@@ -10165,7 +10228,8 @@ function buildAssistantContractFilterClause(spec, candidate, options) {
10165
10228
  fields.forEach(function (field) {
10166
10229
  spec.values.forEach(function (value) {
10167
10230
  var _a;
10168
- var condition = buildAssistantFieldFilterCondition(value, operator === 'in' ? 'eq' : operator, candidate.type);
10231
+ var normalizedValue = normalizeAssistantFieldAwareFilterValue(value, candidate);
10232
+ var condition = buildAssistantFieldFilterCondition(normalizedValue, operator === 'in' ? 'eq' : operator, candidate.type);
10169
10233
  if (condition !== undefined) {
10170
10234
  clauses.push((_a = {}, _a[field] = condition, _a));
10171
10235
  }
@@ -10281,6 +10345,56 @@ function mergeAssistantMatchClauses(existingMatch, clauses) {
10281
10345
  ], __read(cleanClauses), false)
10282
10346
  };
10283
10347
  }
10348
+ function hasAssistantMeaningfulSortStage(pipeline) {
10349
+ return (Array.isArray(pipeline) ? pipeline : []).some(function (stage) { return ((stage === null || stage === void 0 ? void 0 : stage.$sort)
10350
+ && typeof stage.$sort === 'object'
10351
+ && !Array.isArray(stage.$sort)
10352
+ && Object.keys(stage.$sort).length > 0); });
10353
+ }
10354
+ function applyAssistantDefaultRecencySortToPipeline(pipeline) {
10355
+ var _a;
10356
+ var _b;
10357
+ if (!Array.isArray(pipeline) || !pipeline.length || hasAssistantMeaningfulSortStage(pipeline)) {
10358
+ return pipeline;
10359
+ }
10360
+ var next = (0, common_1.deepCopy)(pipeline);
10361
+ var groupIndex = findAggregateGroupIndex(next);
10362
+ if (groupIndex > -1) {
10363
+ var groupStage = (_b = next[groupIndex]) === null || _b === void 0 ? void 0 : _b.$group;
10364
+ if (!groupStage || typeof groupStage !== 'object' || Array.isArray(groupStage)) {
10365
+ return next;
10366
+ }
10367
+ var updatedSortField = '__assistant_sort_updatedAt';
10368
+ var createdSortField = '__assistant_sort_createdAt';
10369
+ if (!Object.prototype.hasOwnProperty.call(groupStage, updatedSortField)) {
10370
+ groupStage[updatedSortField] = { $max: '$updatedAt' };
10371
+ }
10372
+ if (!Object.prototype.hasOwnProperty.call(groupStage, createdSortField)) {
10373
+ groupStage[createdSortField] = { $max: '$createdAt' };
10374
+ }
10375
+ var projectIndex = next.findIndex(function (stage, index) { return index > groupIndex && !!(stage === null || stage === void 0 ? void 0 : stage.$project); });
10376
+ var insertIndex_1 = projectIndex > -1 ? projectIndex : groupIndex + 1;
10377
+ next.splice(insertIndex_1, 0, {
10378
+ $sort: (_a = {},
10379
+ _a[updatedSortField] = -1,
10380
+ _a[createdSortField] = -1,
10381
+ _a)
10382
+ });
10383
+ if (projectIndex === -1) {
10384
+ next.splice(insertIndex_1 + 1, 0, { $unset: [updatedSortField, createdSortField] });
10385
+ }
10386
+ return next;
10387
+ }
10388
+ var insertIndex = next.findIndex(function (stage) { return !!((stage === null || stage === void 0 ? void 0 : stage.$project) || (stage === null || stage === void 0 ? void 0 : stage.$limit) || (stage === null || stage === void 0 ? void 0 : stage.$skip) || (stage === null || stage === void 0 ? void 0 : stage.$count) || (stage === null || stage === void 0 ? void 0 : stage.$facet)); });
10389
+ next.splice(insertIndex > -1 ? insertIndex : next.length, 0, {
10390
+ $sort: {
10391
+ updatedAt: -1,
10392
+ createdAt: -1,
10393
+ _id: -1
10394
+ }
10395
+ });
10396
+ return next;
10397
+ }
10284
10398
  function applyAssistantDataIntentContractFiltersToPipeline(pipeline, intent, requestContract) {
10285
10399
  var _a;
10286
10400
  var filters = Array.isArray(requestContract === null || requestContract === void 0 ? void 0 : requestContract.filters) ? requestContract.filters : [];
@@ -10469,21 +10583,25 @@ function applyAssistantDataIntentDimensionsToPipeline(pipeline, dimensions) {
10469
10583
  }
10470
10584
  });
10471
10585
  }
10472
- var sortIndex = next.findIndex(function (stage, index) { return index > groupIndex && !!(stage === null || stage === void 0 ? void 0 : stage.$sort); });
10473
- if (sortIndex > -1) {
10474
- var combinedSort_1 = {};
10475
- selectedDimensions.forEach(function (dimension) {
10476
- if (dimension.sort && typeof dimension.sort === 'object' && !Array.isArray(dimension.sort)) {
10477
- Object.keys(dimension.sort).forEach(function (key) {
10478
- var _a;
10479
- if (!Object.prototype.hasOwnProperty.call(combinedSort_1, key)) {
10480
- combinedSort_1[key] = (0, common_1.deepCopy)((_a = dimension.sort) === null || _a === void 0 ? void 0 : _a[key]);
10481
- }
10482
- });
10483
- }
10484
- });
10485
- if (Object.keys(combinedSort_1).length) {
10486
- next[sortIndex].$sort = combinedSort_1;
10586
+ var combinedSort = {};
10587
+ selectedDimensions.forEach(function (dimension) {
10588
+ if (dimension.sort && typeof dimension.sort === 'object' && !Array.isArray(dimension.sort)) {
10589
+ Object.keys(dimension.sort).forEach(function (key) {
10590
+ var _a;
10591
+ if (!Object.prototype.hasOwnProperty.call(combinedSort, key)) {
10592
+ combinedSort[key] = (0, common_1.deepCopy)((_a = dimension.sort) === null || _a === void 0 ? void 0 : _a[key]);
10593
+ }
10594
+ });
10595
+ }
10596
+ });
10597
+ if (Object.keys(combinedSort).length) {
10598
+ var sortIndex = next.findIndex(function (stage, index) { return index > groupIndex && !!(stage === null || stage === void 0 ? void 0 : stage.$sort); });
10599
+ if (sortIndex > -1) {
10600
+ next[sortIndex].$sort = combinedSort;
10601
+ }
10602
+ else {
10603
+ var insertIndex = projectIndex > -1 ? projectIndex + 1 : groupIndex + 1;
10604
+ next.splice(insertIndex, 0, { $sort: combinedSort });
10487
10605
  }
10488
10606
  }
10489
10607
  return next;
@@ -10537,7 +10655,7 @@ function buildAssistantAppDataIntentDirective(message, collectionNames, appId, o
10537
10655
  : buildAssistantGenericDataIntentPipeline(intent, customerText, dateWindow);
10538
10656
  var dimensionPipeline = applyAssistantDataIntentDimensionsToPipeline(pipeline, templatedDimensions);
10539
10657
  var filterApplication = applyAssistantDataIntentContractFiltersToPipeline(dimensionPipeline, intent, requestUnderstanding.contract);
10540
- var finalPipeline = filterApplication.pipeline;
10658
+ var finalPipeline = applyAssistantDefaultRecencySortToPipeline(filterApplication.pipeline);
10541
10659
  var planValidationStart = Date.now();
10542
10660
  var droppedBreakdownAmbiguities = resolveAssistantDroppedBreakdownAmbiguities(requestUnderstanding.contract, finalPipeline);
10543
10661
  var droppedFilterAmbiguities = resolveAssistantDroppedFilterAmbiguities(requestUnderstanding.contract, finalPipeline, filterApplication.appliedFilterKeys);
@@ -10861,7 +10979,7 @@ function buildAssistantDatedPivotDisplay(display, expectedMonths) {
10861
10979
  .filter(Boolean)
10862
10980
  : [];
10863
10981
  expectedMonthBuckets.forEach(function (month) { return monthSet.add(month); });
10864
- var months = Array.from(monthSet).sort(function (a, b) { return a.localeCompare(b); });
10982
+ var months = Array.from(monthSet).sort(function (a, b) { return b.localeCompare(a); });
10865
10983
  if (!months.length) {
10866
10984
  return null;
10867
10985
  }
@@ -12319,7 +12437,7 @@ function createAssistantProgressTracker(messageId, initialProgress) {
12319
12437
  if (!next.length) {
12320
12438
  return;
12321
12439
  }
12322
- progress = next;
12440
+ progress = normalizeAssistantProgress(__spreadArray(__spreadArray([], __read(progress), false), __read(next), false));
12323
12441
  progressSource = normalizeOptionalString(source) || progressSource;
12324
12442
  queueProgressUpdate();
12325
12443
  },