@resolveio/server-lib 22.3.208 → 22.3.209

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, ' ');
@@ -8005,6 +8007,35 @@ function resolveAssistantDataIntentDateWindow(message) {
8005
8007
  year: now.getUTCFullYear()
8006
8008
  };
8007
8009
  }
8010
+ 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/);
8011
+ if (approximateLastWindowMatch) {
8012
+ var amountText = approximateLastWindowMatch[1];
8013
+ var amount = amountText.includes('couple')
8014
+ ? 2
8015
+ : amountText === 'several'
8016
+ ? 6
8017
+ : 3;
8018
+ var unit = approximateLastWindowMatch[2];
8019
+ var start = new Date(now.getTime());
8020
+ if (/year/.test(unit)) {
8021
+ start.setUTCFullYear(start.getUTCFullYear() - amount);
8022
+ }
8023
+ else if (/month/.test(unit)) {
8024
+ start.setUTCMonth(start.getUTCMonth() - amount);
8025
+ }
8026
+ else if (/week/.test(unit)) {
8027
+ start.setUTCDate(start.getUTCDate() - (amount * 7));
8028
+ }
8029
+ else {
8030
+ start.setUTCDate(start.getUTCDate() - amount);
8031
+ }
8032
+ start.setUTCHours(0, 0, 0, 0);
8033
+ return {
8034
+ start: start.toISOString(),
8035
+ end: new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), 23, 59, 59, 999)).toISOString(),
8036
+ year: now.getUTCFullYear()
8037
+ };
8038
+ }
8008
8039
  var lastWindowMatch = text.match(/\b(?:last|past)\s+([0-9]+)\s+(days?|weeks?|months?)\b/);
8009
8040
  if (lastWindowMatch) {
8010
8041
  var amount = Math.max(1, Math.min(Number(lastWindowMatch[1]) || 1, 36));
@@ -8339,7 +8370,7 @@ function buildAssistantDataIntentTimeDimension(requestedDimensionText, intent) {
8339
8370
  _b[id] = "$_id.".concat(id),
8340
8371
  _b),
8341
8372
  sort: (_c = {},
8342
- _c[id] = 1,
8373
+ _c[id] = -1,
8343
8374
  _c)
8344
8375
  };
8345
8376
  }
@@ -10281,6 +10312,56 @@ function mergeAssistantMatchClauses(existingMatch, clauses) {
10281
10312
  ], __read(cleanClauses), false)
10282
10313
  };
10283
10314
  }
10315
+ function hasAssistantMeaningfulSortStage(pipeline) {
10316
+ return (Array.isArray(pipeline) ? pipeline : []).some(function (stage) { return ((stage === null || stage === void 0 ? void 0 : stage.$sort)
10317
+ && typeof stage.$sort === 'object'
10318
+ && !Array.isArray(stage.$sort)
10319
+ && Object.keys(stage.$sort).length > 0); });
10320
+ }
10321
+ function applyAssistantDefaultRecencySortToPipeline(pipeline) {
10322
+ var _a;
10323
+ var _b;
10324
+ if (!Array.isArray(pipeline) || !pipeline.length || hasAssistantMeaningfulSortStage(pipeline)) {
10325
+ return pipeline;
10326
+ }
10327
+ var next = (0, common_1.deepCopy)(pipeline);
10328
+ var groupIndex = findAggregateGroupIndex(next);
10329
+ if (groupIndex > -1) {
10330
+ var groupStage = (_b = next[groupIndex]) === null || _b === void 0 ? void 0 : _b.$group;
10331
+ if (!groupStage || typeof groupStage !== 'object' || Array.isArray(groupStage)) {
10332
+ return next;
10333
+ }
10334
+ var updatedSortField = '__assistant_sort_updatedAt';
10335
+ var createdSortField = '__assistant_sort_createdAt';
10336
+ if (!Object.prototype.hasOwnProperty.call(groupStage, updatedSortField)) {
10337
+ groupStage[updatedSortField] = { $max: '$updatedAt' };
10338
+ }
10339
+ if (!Object.prototype.hasOwnProperty.call(groupStage, createdSortField)) {
10340
+ groupStage[createdSortField] = { $max: '$createdAt' };
10341
+ }
10342
+ var projectIndex = next.findIndex(function (stage, index) { return index > groupIndex && !!(stage === null || stage === void 0 ? void 0 : stage.$project); });
10343
+ var insertIndex_1 = projectIndex > -1 ? projectIndex : groupIndex + 1;
10344
+ next.splice(insertIndex_1, 0, {
10345
+ $sort: (_a = {},
10346
+ _a[updatedSortField] = -1,
10347
+ _a[createdSortField] = -1,
10348
+ _a)
10349
+ });
10350
+ if (projectIndex === -1) {
10351
+ next.splice(insertIndex_1 + 1, 0, { $unset: [updatedSortField, createdSortField] });
10352
+ }
10353
+ return next;
10354
+ }
10355
+ 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)); });
10356
+ next.splice(insertIndex > -1 ? insertIndex : next.length, 0, {
10357
+ $sort: {
10358
+ updatedAt: -1,
10359
+ createdAt: -1,
10360
+ _id: -1
10361
+ }
10362
+ });
10363
+ return next;
10364
+ }
10284
10365
  function applyAssistantDataIntentContractFiltersToPipeline(pipeline, intent, requestContract) {
10285
10366
  var _a;
10286
10367
  var filters = Array.isArray(requestContract === null || requestContract === void 0 ? void 0 : requestContract.filters) ? requestContract.filters : [];
@@ -10469,21 +10550,25 @@ function applyAssistantDataIntentDimensionsToPipeline(pipeline, dimensions) {
10469
10550
  }
10470
10551
  });
10471
10552
  }
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;
10553
+ var combinedSort = {};
10554
+ selectedDimensions.forEach(function (dimension) {
10555
+ if (dimension.sort && typeof dimension.sort === 'object' && !Array.isArray(dimension.sort)) {
10556
+ Object.keys(dimension.sort).forEach(function (key) {
10557
+ var _a;
10558
+ if (!Object.prototype.hasOwnProperty.call(combinedSort, key)) {
10559
+ combinedSort[key] = (0, common_1.deepCopy)((_a = dimension.sort) === null || _a === void 0 ? void 0 : _a[key]);
10560
+ }
10561
+ });
10562
+ }
10563
+ });
10564
+ if (Object.keys(combinedSort).length) {
10565
+ var sortIndex = next.findIndex(function (stage, index) { return index > groupIndex && !!(stage === null || stage === void 0 ? void 0 : stage.$sort); });
10566
+ if (sortIndex > -1) {
10567
+ next[sortIndex].$sort = combinedSort;
10568
+ }
10569
+ else {
10570
+ var insertIndex = projectIndex > -1 ? projectIndex + 1 : groupIndex + 1;
10571
+ next.splice(insertIndex, 0, { $sort: combinedSort });
10487
10572
  }
10488
10573
  }
10489
10574
  return next;
@@ -10537,7 +10622,7 @@ function buildAssistantAppDataIntentDirective(message, collectionNames, appId, o
10537
10622
  : buildAssistantGenericDataIntentPipeline(intent, customerText, dateWindow);
10538
10623
  var dimensionPipeline = applyAssistantDataIntentDimensionsToPipeline(pipeline, templatedDimensions);
10539
10624
  var filterApplication = applyAssistantDataIntentContractFiltersToPipeline(dimensionPipeline, intent, requestUnderstanding.contract);
10540
- var finalPipeline = filterApplication.pipeline;
10625
+ var finalPipeline = applyAssistantDefaultRecencySortToPipeline(filterApplication.pipeline);
10541
10626
  var planValidationStart = Date.now();
10542
10627
  var droppedBreakdownAmbiguities = resolveAssistantDroppedBreakdownAmbiguities(requestUnderstanding.contract, finalPipeline);
10543
10628
  var droppedFilterAmbiguities = resolveAssistantDroppedFilterAmbiguities(requestUnderstanding.contract, finalPipeline, filterApplication.appliedFilterKeys);
@@ -10861,7 +10946,7 @@ function buildAssistantDatedPivotDisplay(display, expectedMonths) {
10861
10946
  .filter(Boolean)
10862
10947
  : [];
10863
10948
  expectedMonthBuckets.forEach(function (month) { return monthSet.add(month); });
10864
- var months = Array.from(monthSet).sort(function (a, b) { return a.localeCompare(b); });
10949
+ var months = Array.from(monthSet).sort(function (a, b) { return b.localeCompare(a); });
10865
10950
  if (!months.length) {
10866
10951
  return null;
10867
10952
  }
@@ -12319,7 +12404,7 @@ function createAssistantProgressTracker(messageId, initialProgress) {
12319
12404
  if (!next.length) {
12320
12405
  return;
12321
12406
  }
12322
- progress = next;
12407
+ progress = normalizeAssistantProgress(__spreadArray(__spreadArray([], __read(progress), false), __read(next), false));
12323
12408
  progressSource = normalizeOptionalString(source) || progressSource;
12324
12409
  queueProgressUpdate();
12325
12410
  },