@resolveio/server-lib 22.0.18 → 22.0.20

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.
@@ -116,6 +116,7 @@ exports.buildDisplayTable = buildDisplayTable;
116
116
  exports.formatDisplayTableMarkdown = formatDisplayTableMarkdown;
117
117
  exports.normalizeAssistantNowExprPlaceholders = normalizeAssistantNowExprPlaceholders;
118
118
  exports.rewriteMatchExpressionsToExpr = rewriteMatchExpressionsToExpr;
119
+ exports.normalizeAssistantMonthlyCalendarWindowPipeline = normalizeAssistantMonthlyCalendarWindowPipeline;
119
120
  exports.stripQueryFieldPathsDeep = stripQueryFieldPathsDeep;
120
121
  exports.stripScopedFieldsFromPipeline = stripScopedFieldsFromPipeline;
121
122
  exports.rewriteEmbeddedMatchObjects = rewriteEmbeddedMatchObjects;
@@ -408,6 +409,7 @@ var AI_ASSISTANT_REPORT_BUILDER_EXPERT_PLAYBOOK = [
408
409
  '- Use collection joins/lookups when selected fields live across related collections.',
409
410
  '- If multiple arrays are selected, link them by shared identifiers (_id or id_<x>) so rows are aligned and not duplicated by cartesian expansion.',
410
411
  '- For relative dates, include an upper bound <= $$NOW unless user explicitly asks for future ranges.',
412
+ '- For time-based trends over "last/past N" periods, use full completed calendar-period bounds in UTC aligned to the requested grain (day/week/month/quarter/year) unless the user explicitly asks for period-to-date.',
411
413
  '- For invoice revenue, prefer paid_total fallback grand_total and paid/closed states when applicable.',
412
414
  '',
413
415
  '6) Reliability fallback sequence when result is empty.',
@@ -497,6 +499,7 @@ var AI_ASSISTANT_SYSTEM_PROMPT = [
497
499
  '- Do not use /report/* routes as permissionView for data directives; use a module route that matches the collection.',
498
500
  '- For revenue/sales/billing questions, use invoices and sum paid_total (fallback to grand_total) with date_paid and Paid/Closed status when available.',
499
501
  '- For revenue answers, always state the metric/date basis used (paid_total/date_paid vs grand_total/date_invoice). If tool verification warns about ambiguity or partial months, call that out before totals.',
502
+ '- For time-based "last/past N" breakdowns, use full completed calendar-period windows in UTC aligned to the requested grain (day/week/month/quarter/year) by default, and present concrete start/end dates.',
500
503
  '- For relative date ranges (last/past/recent), include an upper bound <= $$NOW unless the user specifies a future end date.',
501
504
  '- Keep queries minimal, read-only, and avoid user/credential data unless the user is a super admin.',
502
505
  '- Assume you are not a super admin unless explicitly told otherwise.',
@@ -512,6 +515,7 @@ var AI_ASSISTANT_SYSTEM_PROMPT = [
512
515
  '- If the user explicitly asks for IDs, set options.includeIds: true.',
513
516
  '- If a data question returns zero results, verify the collection/date field with a tiny read (limit 1-5) or a date field fallback before concluding there is no data.',
514
517
  '- Keep responses concise and use the configured reasoning effort level (default low).',
518
+ '- Never show internal placeholders like $$NOW in user-facing text; always use concrete dates.',
515
519
  '',
516
520
  AI_ASSISTANT_REPORT_BUILDER_EXPERT_PLAYBOOK
517
521
  ].join('\n');
@@ -3147,7 +3151,7 @@ function executeAiAssistantMongoAggregate(payload, context) {
3147
3151
  }
3148
3152
  function verifyAssistantAggregateReliability(params) {
3149
3153
  return __awaiter(this, void 0, void 0, function () {
3150
- var collection, collectionBase, monthKey, amountKey, baseMap, groupIndex, groupPaths, dateField, mergedMatch, dateFieldsInMatch, baseMatchNoDate, primaryDateCondition, verifyPipeline, verifyDocs, _a, verifyMap, comparedMonths, paidCloserCount, grandCloserCount, sumPaidDiffPct, sumGrandDiffPct, maxPaidDiffPct, maxGrandDiffPct, sumPaidGrandGapPct, highVarianceMonths, checks, warnings, avgPaidDiffPct, avgGrandDiffPct, avgPaidGrandGapPct, baselineMetric, rollingWindow, rollingWindowBounds, verificationWindow, rollingStart, rollingEnd, fullMonthStart, fullMonthEndExclusive, normalizedMetric, bounds, fullMonthMatch, fullMonthDocs, fullMonthMap, fullMonthDiff, _b, alternateDateField, alternateDateCondition, bounds, alternateMatch, alternateDocs, alternateMap, alternateDiff, _c;
3154
+ var collection, collectionBase, monthKey, amountKey, baseMap, groupIndex, groupPaths, dateField, mergedMatch, dateFieldsInMatch, baseMatchNoDate, primaryDateCondition, verifyPipeline, verifyDocs, _a, verifyMap, comparedMonths, paidCloserCount, grandCloserCount, sumPaidDiffPct, sumGrandDiffPct, maxPaidDiffPct, maxGrandDiffPct, sumPaidGrandGapPct, highVarianceMonths, checks, warnings, avgPaidDiffPct, avgGrandDiffPct, avgPaidGrandGapPct, usesPaidGrandFallbackMetric, baselineMetric, rollingWindow, fullMonthWindow, rollingWindowBounds, fullMonthWindowBounds, verificationWindow, rollingStart, rollingEnd, fullMonthStart, fullMonthEndExclusive, fullMonthStart, fullMonthEndExclusive, fullMonthEndInclusiveDate, fullMonthEnd, normalizedMetric, bounds, fullMonthMatch, fullMonthDocs, fullMonthMap, fullMonthDiff, _b, alternateDateField, alternateDateCondition, bounds, bounds, alternateMatch, alternateDocs, alternateMap, alternateDiff, _c;
3151
3155
  var _d, _e;
3152
3156
  var _f;
3153
3157
  return __generator(this, function (_g) {
@@ -3225,9 +3229,8 @@ function verifyAssistantAggregateReliability(params) {
3225
3229
  comparedMonths += 1;
3226
3230
  var paidDiff = Math.abs(value - match.paid);
3227
3231
  var grandDiff = Math.abs(value - match.grand);
3228
- var denom = Math.max(Math.abs(value), 1);
3229
- var paidPct = paidDiff / denom;
3230
- var grandPct = grandDiff / denom;
3232
+ var paidPct = computeAssistantRelativeDiffPct(value, match.paid);
3233
+ var grandPct = computeAssistantRelativeDiffPct(value, match.grand);
3231
3234
  sumPaidDiffPct += paidPct;
3232
3235
  sumGrandDiffPct += grandPct;
3233
3236
  maxPaidDiffPct = Math.max(maxPaidDiffPct, paidPct);
@@ -3238,7 +3241,7 @@ function verifyAssistantAggregateReliability(params) {
3238
3241
  else if (grandDiff < paidDiff) {
3239
3242
  grandCloserCount += 1;
3240
3243
  }
3241
- var gapPct = Math.abs(match.paid - match.grand) / Math.max(Math.abs(match.grand), 1);
3244
+ var gapPct = computeAssistantRelativeDiffPct(match.paid, match.grand);
3242
3245
  sumPaidGrandGapPct += gapPct;
3243
3246
  if (Math.min(paidPct, grandPct) >= AI_ASSISTANT_REVENUE_VERIFY_DIFF_WARN_THRESHOLD) {
3244
3247
  highVarianceMonths.push(month);
@@ -3249,6 +3252,7 @@ function verifyAssistantAggregateReliability(params) {
3249
3252
  avgPaidDiffPct = comparedMonths ? (sumPaidDiffPct / comparedMonths) : 0;
3250
3253
  avgGrandDiffPct = comparedMonths ? (sumGrandDiffPct / comparedMonths) : 0;
3251
3254
  avgPaidGrandGapPct = comparedMonths ? (sumPaidGrandGapPct / comparedMonths) : 0;
3255
+ usesPaidGrandFallbackMetric = hasAssistantPaidGrandFallbackExpr(params.pipeline || []);
3252
3256
  baselineMetric = 'unknown';
3253
3257
  if (comparedMonths) {
3254
3258
  if (paidCloserCount > grandCloserCount) {
@@ -3272,10 +3276,12 @@ function verifyAssistantAggregateReliability(params) {
3272
3276
  else {
3273
3277
  checks.push({
3274
3278
  name: 'Metric alignment',
3275
- status: baselineMetric === 'mixed' ? 'warn' : 'pass',
3276
- details: "Result aligns most with ".concat(baselineMetric, ".")
3279
+ status: baselineMetric === 'mixed' && !usesPaidGrandFallbackMetric ? 'warn' : 'pass',
3280
+ details: baselineMetric === 'mixed' && usesPaidGrandFallbackMetric
3281
+ ? 'Result uses explicit paid_total fallback grand_total logic.'
3282
+ : "Result aligns most with ".concat(baselineMetric, ".")
3277
3283
  });
3278
- if (baselineMetric === 'mixed') {
3284
+ if (baselineMetric === 'mixed' && !usesPaidGrandFallbackMetric) {
3279
3285
  warnings.push('Revenue appears to mix definitions across months (paid_total vs grand_total).');
3280
3286
  }
3281
3287
  checks.push({
@@ -3288,7 +3294,9 @@ function verifyAssistantAggregateReliability(params) {
3288
3294
  }
3289
3295
  }
3290
3296
  rollingWindow = detectAssistantRollingMonthWindow(params.pipeline || []);
3297
+ fullMonthWindow = detectAssistantFullMonthWindow(params.pipeline || []);
3291
3298
  rollingWindowBounds = null;
3299
+ fullMonthWindowBounds = null;
3292
3300
  verificationWindow = null;
3293
3301
  if ((rollingWindow === null || rollingWindow === void 0 ? void 0 : rollingWindow.months) && (rollingWindow === null || rollingWindow === void 0 ? void 0 : rollingWindow.upperNow)) {
3294
3302
  rollingWindowBounds = resolveAssistantRollingWindowBounds(rollingWindow.months);
@@ -3312,6 +3320,27 @@ function verifyAssistantAggregateReliability(params) {
3312
3320
  });
3313
3321
  warnings.push("Date window is rolling (".concat(rollingStart, " to ").concat(rollingEnd, " UTC; last ").concat(rollingWindow.months, " months), so first/current months may be partial."));
3314
3322
  }
3323
+ else if ((fullMonthWindow === null || fullMonthWindow === void 0 ? void 0 : fullMonthWindow.months) && (fullMonthWindow === null || fullMonthWindow === void 0 ? void 0 : fullMonthWindow.upperMonthStart)) {
3324
+ fullMonthWindowBounds = resolveAssistantRollingWindowBounds(fullMonthWindow.months);
3325
+ fullMonthStart = formatAssistantIsoDateOnly(fullMonthWindowBounds.startOfWindow);
3326
+ fullMonthEndExclusive = formatAssistantIsoDateOnly(fullMonthWindowBounds.startOfCurrentMonth);
3327
+ fullMonthEndInclusiveDate = new Date(fullMonthWindowBounds.startOfCurrentMonth);
3328
+ fullMonthEndInclusiveDate.setUTCDate(fullMonthEndInclusiveDate.getUTCDate() - 1);
3329
+ fullMonthEnd = formatAssistantIsoDateOnly(fullMonthEndInclusiveDate);
3330
+ verificationWindow = {
3331
+ type: 'full_month_last_n_months',
3332
+ months: fullMonthWindow.months,
3333
+ startDate: fullMonthStart,
3334
+ endDate: fullMonthEnd,
3335
+ endDateExclusive: fullMonthEndExclusive,
3336
+ timezone: 'UTC'
3337
+ };
3338
+ checks.push({
3339
+ name: 'Date window shape',
3340
+ status: 'pass',
3341
+ details: "Query uses full-month calendar bounds (".concat(fullMonthStart, " to ").concat(fullMonthEnd, " UTC; last ").concat(fullMonthWindow.months, " full months).")
3342
+ });
3343
+ }
3315
3344
  else {
3316
3345
  checks.push({
3317
3346
  name: 'Date window shape',
@@ -3345,7 +3374,7 @@ function verifyAssistantAggregateReliability(params) {
3345
3374
  ? "Compared ".concat(fullMonthDiff.comparedMonths, " month(s); avg diff ").concat((0, common_1.round)(fullMonthDiff.avgDiffPct * 100, 2), "%.")
3346
3375
  : 'No overlapping months for full-month cross-check.'
3347
3376
  });
3348
- if (fullMonthDiff.comparedMonths && fullMonthDiff.avgDiffPct >= AI_ASSISTANT_REVENUE_VERIFY_DIFF_WARN_THRESHOLD) {
3377
+ if (fullMonthDiff.comparedMonths >= 2 && fullMonthDiff.avgDiffPct >= AI_ASSISTANT_REVENUE_VERIFY_DIFF_WARN_THRESHOLD) {
3349
3378
  warnings.push("Rolling-window totals differ from full-month totals by ".concat((0, common_1.round)(fullMonthDiff.avgDiffPct * 100, 2), "% on average."));
3350
3379
  }
3351
3380
  if (!fullMonthDiff.comparedMonths) {
@@ -3376,6 +3405,13 @@ function verifyAssistantAggregateReliability(params) {
3376
3405
  $lte: bounds.now
3377
3406
  };
3378
3407
  }
3408
+ else if ((fullMonthWindow === null || fullMonthWindow === void 0 ? void 0 : fullMonthWindow.months) && (fullMonthWindow === null || fullMonthWindow === void 0 ? void 0 : fullMonthWindow.upperMonthStart) && fullMonthWindowBounds) {
3409
+ bounds = fullMonthWindowBounds;
3410
+ alternateDateCondition = {
3411
+ $gte: bounds.startOfWindow,
3412
+ $lt: bounds.startOfCurrentMonth
3413
+ };
3414
+ }
3379
3415
  else if (primaryDateCondition && typeof primaryDateCondition === 'object') {
3380
3416
  alternateDateCondition = primaryDateCondition;
3381
3417
  }
@@ -3398,7 +3434,7 @@ function verifyAssistantAggregateReliability(params) {
3398
3434
  ? "Compared ".concat(alternateDiff.comparedMonths, " month(s); avg diff ").concat((0, common_1.round)(alternateDiff.avgDiffPct * 100, 2), "%.")
3399
3435
  : 'No overlapping months for alternate date-field cross-check.'
3400
3436
  });
3401
- if (alternateDiff.comparedMonths && alternateDiff.avgDiffPct >= AI_ASSISTANT_REVENUE_VERIFY_DIFF_WARN_THRESHOLD) {
3437
+ if (alternateDiff.comparedMonths >= 2 && alternateDiff.avgDiffPct >= AI_ASSISTANT_REVENUE_VERIFY_DIFF_WARN_THRESHOLD) {
3402
3438
  warnings.push("Switching from ".concat(dateField, " to ").concat(alternateDateField, " changes monthly totals by ").concat((0, common_1.round)(alternateDiff.avgDiffPct * 100, 2), "% on average."));
3403
3439
  }
3404
3440
  return [3 /*break*/, 12];
@@ -3519,6 +3555,24 @@ function mapAssistantRevenueByMonth(documents, monthKey, valueKey) {
3519
3555
  });
3520
3556
  return mapped;
3521
3557
  }
3558
+ function hasAssistantPaidGrandFallbackExpr(value) {
3559
+ if (Array.isArray(value)) {
3560
+ return value.some(function (entry) { return hasAssistantPaidGrandFallbackExpr(entry); });
3561
+ }
3562
+ if (!value || typeof value !== 'object') {
3563
+ return false;
3564
+ }
3565
+ var ifNull = value.$ifNull;
3566
+ if (Array.isArray(ifNull) && ifNull.length >= 2) {
3567
+ var first = normalizeOptionalString(ifNull[0]).replace(/^\$/, '');
3568
+ var second = normalizeOptionalString(ifNull[1]).replace(/^\$/, '');
3569
+ var pair = new Set([first, second]);
3570
+ if (pair.has('paid_total') && pair.has('grand_total')) {
3571
+ return true;
3572
+ }
3573
+ }
3574
+ return Object.keys(value).some(function (key) { return hasAssistantPaidGrandFallbackExpr(value[key]); });
3575
+ }
3522
3576
  function mergeAssistantPreGroupMatchStages(pipeline, groupIndex) {
3523
3577
  var matches = [];
3524
3578
  for (var i = 0; i < groupIndex; i += 1) {
@@ -3536,22 +3590,23 @@ function mergeAssistantPreGroupMatchStages(pipeline, groupIndex) {
3536
3590
  return { $and: matches };
3537
3591
  }
3538
3592
  function detectAssistantRollingMonthWindow(pipeline) {
3539
- var raw = JSON.stringify(Array.isArray(pipeline) ? pipeline : []);
3540
- if (!raw) {
3593
+ var detected = detectAssistantMonthWindowFromPipeline(pipeline);
3594
+ if (!detected || detected.mode !== 'rolling') {
3541
3595
  return null;
3542
3596
  }
3543
- var amountMatch = raw.match(/"\$dateSubtract":\{"startDate":"\$\$NOW","unit":"month","amount":([0-9]+)\}/);
3544
- if (!amountMatch) {
3545
- return null;
3546
- }
3547
- var months = Number(amountMatch[1]);
3548
- if (!Number.isFinite(months) || months < 1) {
3597
+ return {
3598
+ months: detected.months,
3599
+ upperNow: true
3600
+ };
3601
+ }
3602
+ function detectAssistantFullMonthWindow(pipeline) {
3603
+ var detected = detectAssistantMonthWindowFromPipeline(pipeline);
3604
+ if (!detected || detected.mode !== 'full_month') {
3549
3605
  return null;
3550
3606
  }
3551
- var nowCount = (raw.match(/\$\$NOW/g) || []).length;
3552
3607
  return {
3553
- months: months,
3554
- upperNow: nowCount >= 2
3608
+ months: detected.months,
3609
+ upperMonthStart: true
3555
3610
  };
3556
3611
  }
3557
3612
  function buildAssistantMonthlyRevenueVerificationPipeline(match, dateField) {
@@ -3622,6 +3677,12 @@ function mapAssistantMetricByMonth(source, metric) {
3622
3677
  });
3623
3678
  return mapped;
3624
3679
  }
3680
+ function computeAssistantRelativeDiffPct(baseValue, compareValue) {
3681
+ var left = Number.isFinite(baseValue) ? baseValue : 0;
3682
+ var right = Number.isFinite(compareValue) ? compareValue : 0;
3683
+ var denom = Math.max(Math.abs(left), Math.abs(right), 1);
3684
+ return Math.abs(left - right) / denom;
3685
+ }
3625
3686
  function compareAssistantMonthlyValueMaps(base, compare) {
3626
3687
  var comparedMonths = 0;
3627
3688
  var sumDiffPct = 0;
@@ -3631,7 +3692,7 @@ function compareAssistantMonthlyValueMaps(base, compare) {
3631
3692
  if (typeof compareValue !== 'number') {
3632
3693
  return;
3633
3694
  }
3634
- var diffPct = Math.abs(baseValue - compareValue) / Math.max(Math.abs(baseValue), 1);
3695
+ var diffPct = computeAssistantRelativeDiffPct(baseValue, compareValue);
3635
3696
  comparedMonths += 1;
3636
3697
  sumDiffPct += diffPct;
3637
3698
  maxDiffPct = Math.max(maxDiffPct, diffPct);
@@ -3888,7 +3949,7 @@ function buildAssistantCodexToolFollowupPrompt(message, attachmentText, historyT
3888
3949
  var trimmedHistory = normalizeOptionalString(historyText);
3889
3950
  var historyBlock = trimmedHistory ? "\n\nConversation so far:\n".concat(trimmedHistory) : '';
3890
3951
  var toolBlock = toolResultText ? "\n\nTool Result:\n".concat(toolResultText) : '';
3891
- var instruction = '\n\nInstruction:\nNow answer the user. Do NOT output another REPORT_BUILDER_* directive. Output plain Markdown. Summarize first, then include a Markdown table. If the Tool Result includes Verification warnings, explicitly include them and call out the metric/date basis used.';
3952
+ var instruction = '\n\nInstruction:\nNow answer the user. Do NOT output another REPORT_BUILDER_* directive. Output plain Markdown. Summarize first, then include a Markdown table. If the Tool Result includes Verification warnings, explicitly include them and call out the metric/date basis used. Include explicit UTC date boundaries and never show placeholders like $$NOW.';
3892
3953
  return "System:\n".concat(AI_ASSISTANT_SYSTEM_PROMPT).concat(contextBlock).concat(historyBlock, "\n\nUser:\n").concat(message).concat(attachmentText || '').concat(toolBlock).concat(instruction).trim();
3893
3954
  }
3894
3955
  function buildAssistantToolFallbackResponse(result) {
@@ -3939,8 +4000,18 @@ function applyAssistantDatedReportWindow(value, toolResult) {
3939
4000
  if (!startDate || !endDate) {
3940
4001
  return content || value || '';
3941
4002
  }
4003
+ var mode = normalizeOptionalString(window === null || window === void 0 ? void 0 : window.mode).toLowerCase();
3942
4004
  var months = normalizeOptionalNumber(window === null || window === void 0 ? void 0 : window.months);
3943
- var monthsText = months && months > 0 ? "; rolling last ".concat((0, common_1.round)(months), " month").concat((0, common_1.round)(months) === 1 ? '' : 's') : '';
4005
+ var monthsText = '';
4006
+ if (months && months > 0) {
4007
+ var count = (0, common_1.round)(months);
4008
+ if (mode === 'full_month') {
4009
+ monthsText = "; last ".concat(count, " full month").concat(count === 1 ? '' : 's');
4010
+ }
4011
+ else if (mode === 'rolling') {
4012
+ monthsText = "; rolling last ".concat(count, " month").concat(count === 1 ? '' : 's');
4013
+ }
4014
+ }
3944
4015
  var line = "Report date range (UTC): ".concat(startDate, " to ").concat(endDate).concat(monthsText, ".");
3945
4016
  var normalizedLower = String(content || '').toLowerCase();
3946
4017
  if (normalizedLower.includes(startDate.toLowerCase()) && normalizedLower.includes(endDate.toLowerCase())) {
@@ -3958,10 +4029,19 @@ function resolveAssistantDatedReportWindow(toolResult) {
3958
4029
  var verificationStart = normalizeOptionalString(verificationWindow === null || verificationWindow === void 0 ? void 0 : verificationWindow.startDate);
3959
4030
  var verificationEnd = normalizeOptionalString(verificationWindow === null || verificationWindow === void 0 ? void 0 : verificationWindow.endDate);
3960
4031
  if (verificationStart && verificationEnd) {
4032
+ var verificationType = normalizeOptionalString(verificationWindow === null || verificationWindow === void 0 ? void 0 : verificationWindow.type).toLowerCase();
4033
+ var mode = void 0;
4034
+ if (verificationType.includes('full_month')) {
4035
+ mode = 'full_month';
4036
+ }
4037
+ else if (verificationType.includes('rolling')) {
4038
+ mode = 'rolling';
4039
+ }
3961
4040
  return {
3962
4041
  startDate: verificationStart,
3963
4042
  endDate: verificationEnd,
3964
- months: normalizeOptionalNumber(verificationWindow === null || verificationWindow === void 0 ? void 0 : verificationWindow.months) || undefined
4043
+ months: normalizeOptionalNumber(verificationWindow === null || verificationWindow === void 0 ? void 0 : verificationWindow.months) || undefined,
4044
+ mode: mode
3965
4045
  };
3966
4046
  }
3967
4047
  var pipelineCandidates = [
@@ -3975,6 +4055,22 @@ function resolveAssistantDatedReportWindow(toolResult) {
3975
4055
  if (!Array.isArray(candidate)) {
3976
4056
  continue;
3977
4057
  }
4058
+ var fullMonthWindow = detectAssistantFullMonthWindow(candidate);
4059
+ if ((fullMonthWindow === null || fullMonthWindow === void 0 ? void 0 : fullMonthWindow.months) && (fullMonthWindow === null || fullMonthWindow === void 0 ? void 0 : fullMonthWindow.upperMonthStart)) {
4060
+ var bounds_1 = resolveAssistantRollingWindowBounds(fullMonthWindow.months);
4061
+ var startDate_1 = formatAssistantIsoDateOnly(bounds_1.startOfWindow);
4062
+ var endDateValue = new Date(bounds_1.startOfCurrentMonth);
4063
+ endDateValue.setUTCDate(endDateValue.getUTCDate() - 1);
4064
+ var endDate_1 = formatAssistantIsoDateOnly(endDateValue);
4065
+ if (startDate_1 && endDate_1) {
4066
+ return {
4067
+ startDate: startDate_1,
4068
+ endDate: endDate_1,
4069
+ months: fullMonthWindow.months,
4070
+ mode: 'full_month'
4071
+ };
4072
+ }
4073
+ }
3978
4074
  var rollingWindow = detectAssistantRollingMonthWindow(candidate);
3979
4075
  if (!(rollingWindow === null || rollingWindow === void 0 ? void 0 : rollingWindow.months) || !(rollingWindow === null || rollingWindow === void 0 ? void 0 : rollingWindow.upperNow)) {
3980
4076
  continue;
@@ -3988,7 +4084,8 @@ function resolveAssistantDatedReportWindow(toolResult) {
3988
4084
  return {
3989
4085
  startDate: startDate,
3990
4086
  endDate: endDate,
3991
- months: rollingWindow.months
4087
+ months: rollingWindow.months,
4088
+ mode: 'rolling'
3992
4089
  };
3993
4090
  }
3994
4091
  }
@@ -5923,9 +6020,13 @@ function isAssistantMarkdownTableRowLine(line) {
5923
6020
  return /^\|.*\|$/.test(trimmed);
5924
6021
  }
5925
6022
  function applyAssistantDisplayTableToResponse(value, display) {
5926
- if (!display || !Array.isArray(display.rows) || !display.rows.length) {
6023
+ if (!display || !Array.isArray(display.rows)) {
5927
6024
  return value;
5928
6025
  }
6026
+ if (!display.rows.length) {
6027
+ // Remove any model-generated placeholder table when tool data has no rows.
6028
+ return stripAssistantMarkdownTables(value);
6029
+ }
5929
6030
  var table = formatDisplayTableMarkdown(display);
5930
6031
  if (!table) {
5931
6032
  return value;
@@ -6381,7 +6482,7 @@ function rewriteMatchExpressionsToExpr(match) {
6381
6482
  return rewriteValue(match);
6382
6483
  }
6383
6484
  function buildInvoiceStatusRegex(value) {
6384
- var normalized = String(value || '').toLowerCase();
6485
+ var normalized = normalizeOptionalString(value).toLowerCase();
6385
6486
  if (!normalized) {
6386
6487
  return null;
6387
6488
  }
@@ -6391,6 +6492,31 @@ function buildInvoiceStatusRegex(value) {
6391
6492
  }
6392
6493
  return /paid|closed/i;
6393
6494
  }
6495
+ function buildInvoiceStatusRegexFromValue(value) {
6496
+ if (typeof value === 'string') {
6497
+ return buildInvoiceStatusRegex(value);
6498
+ }
6499
+ if (Array.isArray(value)) {
6500
+ var normalizedValues = value
6501
+ .filter(function (entry) { return typeof entry === 'string'; })
6502
+ .map(function (entry) { return normalizeOptionalString(entry); })
6503
+ .filter(Boolean);
6504
+ if (!normalizedValues.length) {
6505
+ return null;
6506
+ }
6507
+ return buildInvoiceStatusRegex(normalizedValues.join(' '));
6508
+ }
6509
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
6510
+ var keys = Object.keys(value);
6511
+ if (keys.length === 1 && typeof value.$eq === 'string') {
6512
+ return buildInvoiceStatusRegex(value.$eq);
6513
+ }
6514
+ if (keys.length === 1 && Array.isArray(value.$in)) {
6515
+ return buildInvoiceStatusRegexFromValue(value.$in);
6516
+ }
6517
+ }
6518
+ return null;
6519
+ }
6394
6520
  function normalizeInvoiceStatusMatch(match) {
6395
6521
  var rewriteValue = function (value) {
6396
6522
  if (Array.isArray(value)) {
@@ -6407,8 +6533,8 @@ function normalizeInvoiceStatusMatch(match) {
6407
6533
  return;
6408
6534
  }
6409
6535
  var field = key.split('.').pop() || key;
6410
- if (field === 'status' && typeof entry === 'string') {
6411
- var regex = buildInvoiceStatusRegex(entry);
6536
+ if (field === 'status' || field === 'state') {
6537
+ var regex = buildInvoiceStatusRegexFromValue(entry);
6412
6538
  if (regex) {
6413
6539
  result[key] = regex;
6414
6540
  return;
@@ -6424,12 +6550,299 @@ function normalizeInvoiceStatusMatch(match) {
6424
6550
  };
6425
6551
  return rewriteValue(match);
6426
6552
  }
6553
+ function extractAssistantFieldPathFromOperand(value) {
6554
+ if (typeof value !== 'string') {
6555
+ return '';
6556
+ }
6557
+ if (!value.startsWith('$') || value.startsWith('$$')) {
6558
+ return '';
6559
+ }
6560
+ return value.slice(1);
6561
+ }
6562
+ function createAssistantNowMonthStartExpr() {
6563
+ return {
6564
+ $dateTrunc: {
6565
+ date: '$$NOW',
6566
+ unit: 'month'
6567
+ }
6568
+ };
6569
+ }
6570
+ function isAssistantNowMonthStartExpr(value) {
6571
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
6572
+ return false;
6573
+ }
6574
+ var dateTrunc = value.$dateTrunc;
6575
+ if (!dateTrunc || typeof dateTrunc !== 'object' || Array.isArray(dateTrunc)) {
6576
+ return false;
6577
+ }
6578
+ if (dateTrunc.date !== '$$NOW') {
6579
+ return false;
6580
+ }
6581
+ return normalizeOptionalString(dateTrunc.unit).toLowerCase() === 'month';
6582
+ }
6583
+ function extractAssistantMonthLowerBound(value) {
6584
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
6585
+ return null;
6586
+ }
6587
+ var dateSubtract = value.$dateSubtract;
6588
+ if (!dateSubtract || typeof dateSubtract !== 'object' || Array.isArray(dateSubtract)) {
6589
+ return null;
6590
+ }
6591
+ var unit = normalizeOptionalString(dateSubtract.unit).toLowerCase();
6592
+ var months = normalizeOptionalNumber(dateSubtract.amount);
6593
+ if (unit !== 'month' || !months || months < 1) {
6594
+ return null;
6595
+ }
6596
+ if (dateSubtract.startDate === '$$NOW') {
6597
+ return { months: (0, common_1.round)(months), mode: 'rolling' };
6598
+ }
6599
+ if (isAssistantNowMonthStartExpr(dateSubtract.startDate)) {
6600
+ return { months: (0, common_1.round)(months), mode: 'full_month' };
6601
+ }
6602
+ return null;
6603
+ }
6604
+ function flattenAssistantExprClauses(expr) {
6605
+ if (!expr || typeof expr !== 'object' || Array.isArray(expr)) {
6606
+ return [];
6607
+ }
6608
+ if (Array.isArray(expr.$and)) {
6609
+ return expr.$and;
6610
+ }
6611
+ return [expr];
6612
+ }
6613
+ function detectAssistantMonthWindowInExpr(expr, allowedDateFields) {
6614
+ var e_12, _a;
6615
+ var clauses = flattenAssistantExprClauses(expr);
6616
+ if (!clauses.length) {
6617
+ return null;
6618
+ }
6619
+ var lowerClauses = [];
6620
+ var upperClauses = [];
6621
+ clauses.forEach(function (clause, index) {
6622
+ if (!clause || typeof clause !== 'object' || Array.isArray(clause)) {
6623
+ return;
6624
+ }
6625
+ var keys = Object.keys(clause);
6626
+ if (keys.length !== 1) {
6627
+ return;
6628
+ }
6629
+ var operator = keys[0];
6630
+ if (!['$gte', '$gt', '$lte', '$lt'].includes(operator)) {
6631
+ return;
6632
+ }
6633
+ var operands = clause[operator];
6634
+ if (!Array.isArray(operands) || operands.length !== 2) {
6635
+ return;
6636
+ }
6637
+ var field = extractAssistantFieldPathFromOperand(operands[0]);
6638
+ if (!field) {
6639
+ return;
6640
+ }
6641
+ if ((allowedDateFields === null || allowedDateFields === void 0 ? void 0 : allowedDateFields.size) && !allowedDateFields.has(field)) {
6642
+ return;
6643
+ }
6644
+ if (operator === '$gte' || operator === '$gt') {
6645
+ var lowerBound = extractAssistantMonthLowerBound(operands[1]);
6646
+ if (!lowerBound) {
6647
+ return;
6648
+ }
6649
+ lowerClauses.push({
6650
+ index: index,
6651
+ field: field,
6652
+ months: lowerBound.months,
6653
+ mode: lowerBound.mode
6654
+ });
6655
+ return;
6656
+ }
6657
+ if (operands[1] === '$$NOW') {
6658
+ upperClauses.push({ index: index, field: field, mode: 'rolling' });
6659
+ return;
6660
+ }
6661
+ if (isAssistantNowMonthStartExpr(operands[1])) {
6662
+ upperClauses.push({ index: index, field: field, mode: 'full_month' });
6663
+ }
6664
+ });
6665
+ var _loop_6 = function (lower) {
6666
+ var upper = upperClauses.find(function (candidate) { return (candidate.field === lower.field
6667
+ && candidate.mode === lower.mode); });
6668
+ if (!upper) {
6669
+ return "continue";
6670
+ }
6671
+ return { value: {
6672
+ field: lower.field,
6673
+ months: lower.months,
6674
+ mode: lower.mode,
6675
+ lowerIndex: lower.index,
6676
+ upperIndex: upper.index,
6677
+ clauses: clauses
6678
+ } };
6679
+ };
6680
+ try {
6681
+ for (var lowerClauses_1 = __values(lowerClauses), lowerClauses_1_1 = lowerClauses_1.next(); !lowerClauses_1_1.done; lowerClauses_1_1 = lowerClauses_1.next()) {
6682
+ var lower = lowerClauses_1_1.value;
6683
+ var state_4 = _loop_6(lower);
6684
+ if (typeof state_4 === "object")
6685
+ return state_4.value;
6686
+ }
6687
+ }
6688
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
6689
+ finally {
6690
+ try {
6691
+ if (lowerClauses_1_1 && !lowerClauses_1_1.done && (_a = lowerClauses_1.return)) _a.call(lowerClauses_1);
6692
+ }
6693
+ finally { if (e_12) throw e_12.error; }
6694
+ }
6695
+ return null;
6696
+ }
6697
+ function isAssistantMonthDateFormat(format) {
6698
+ var normalized = normalizeOptionalString(format);
6699
+ if (!normalized) {
6700
+ return false;
6701
+ }
6702
+ if (!/%m/.test(normalized)) {
6703
+ return false;
6704
+ }
6705
+ return !/(%d|%j|%H|%I|%M|%S|%U|%V|%W|%w)/.test(normalized);
6706
+ }
6707
+ function collectAssistantMonthlyGroupDateFields(value, output) {
6708
+ if (Array.isArray(value)) {
6709
+ value.forEach(function (entry) { return collectAssistantMonthlyGroupDateFields(entry, output); });
6710
+ return;
6711
+ }
6712
+ if (!value || typeof value !== 'object') {
6713
+ return;
6714
+ }
6715
+ var dateToString = value.$dateToString;
6716
+ if (dateToString && typeof dateToString === 'object' && !Array.isArray(dateToString)) {
6717
+ var format = normalizeOptionalString(dateToString.format);
6718
+ var field = extractAssistantFieldPathFromOperand(dateToString.date);
6719
+ if (field && isAssistantMonthDateFormat(format)) {
6720
+ output.add(field);
6721
+ }
6722
+ }
6723
+ var dateTrunc = value.$dateTrunc;
6724
+ if (dateTrunc && typeof dateTrunc === 'object' && !Array.isArray(dateTrunc)) {
6725
+ var unit = normalizeOptionalString(dateTrunc.unit).toLowerCase();
6726
+ var field = extractAssistantFieldPathFromOperand(dateTrunc.date);
6727
+ if (field && unit === 'month') {
6728
+ output.add(field);
6729
+ }
6730
+ }
6731
+ var monthExpr = value.$month;
6732
+ if (monthExpr !== undefined) {
6733
+ var field = extractAssistantFieldPathFromOperand(monthExpr);
6734
+ if (field) {
6735
+ output.add(field);
6736
+ }
6737
+ }
6738
+ Object.keys(value).forEach(function (key) {
6739
+ collectAssistantMonthlyGroupDateFields(value[key], output);
6740
+ });
6741
+ }
6742
+ function resolveAssistantMonthlyGroupDateFields(pipeline) {
6743
+ var fields = new Set();
6744
+ var groupIndex = findAggregateGroupIndex(pipeline);
6745
+ if (groupIndex === -1) {
6746
+ return fields;
6747
+ }
6748
+ var groupStage = pipeline[groupIndex];
6749
+ if (!groupStage || typeof groupStage !== 'object' || !groupStage.$group || typeof groupStage.$group !== 'object') {
6750
+ return fields;
6751
+ }
6752
+ collectAssistantMonthlyGroupDateFields(groupStage.$group, fields);
6753
+ return fields;
6754
+ }
6755
+ function detectAssistantMonthWindowFromPipeline(pipeline) {
6756
+ var e_13, _a;
6757
+ var _b;
6758
+ var monthlyFields = resolveAssistantMonthlyGroupDateFields(pipeline);
6759
+ var scanMatch = function (match) {
6760
+ if (!match || typeof match !== 'object' || Array.isArray(match)) {
6761
+ return null;
6762
+ }
6763
+ if (!match.$expr || typeof match.$expr !== 'object') {
6764
+ return null;
6765
+ }
6766
+ return detectAssistantMonthWindowInExpr(match.$expr, monthlyFields);
6767
+ };
6768
+ try {
6769
+ for (var _c = __values((pipeline || [])), _d = _c.next(); !_d.done; _d = _c.next()) {
6770
+ var stage = _d.value;
6771
+ if (!stage || typeof stage !== 'object') {
6772
+ continue;
6773
+ }
6774
+ var directMatch = scanMatch(stage.$match);
6775
+ if (directMatch) {
6776
+ return directMatch;
6777
+ }
6778
+ var geoNearMatch = scanMatch((_b = stage.$geoNear) === null || _b === void 0 ? void 0 : _b.query);
6779
+ if (geoNearMatch) {
6780
+ return geoNearMatch;
6781
+ }
6782
+ }
6783
+ }
6784
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
6785
+ finally {
6786
+ try {
6787
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
6788
+ }
6789
+ finally { if (e_13) throw e_13.error; }
6790
+ }
6791
+ return null;
6792
+ }
6793
+ function normalizeAssistantMonthlyCalendarWindowPipeline(pipeline) {
6794
+ var monthlyFields = resolveAssistantMonthlyGroupDateFields(pipeline);
6795
+ if (!monthlyFields.size) {
6796
+ return pipeline;
6797
+ }
6798
+ return (pipeline || []).map(function (stage) {
6799
+ if (!stage || typeof stage !== 'object' || Array.isArray(stage)) {
6800
+ return stage;
6801
+ }
6802
+ var rewriteMatch = function (match) {
6803
+ if (!match || typeof match !== 'object' || Array.isArray(match) || !match.$expr) {
6804
+ return match;
6805
+ }
6806
+ var detection = detectAssistantMonthWindowInExpr(match.$expr, monthlyFields);
6807
+ if (!detection || detection.mode !== 'rolling') {
6808
+ return match;
6809
+ }
6810
+ var nextClauses = detection.clauses.filter(function (_entry, index) { return (index !== detection.lowerIndex && index !== detection.upperIndex); });
6811
+ nextClauses.push({
6812
+ $gte: [
6813
+ "$".concat(detection.field),
6814
+ {
6815
+ $dateSubtract: {
6816
+ startDate: createAssistantNowMonthStartExpr(),
6817
+ unit: 'month',
6818
+ amount: detection.months
6819
+ }
6820
+ }
6821
+ ]
6822
+ }, {
6823
+ $lt: [
6824
+ "$".concat(detection.field),
6825
+ createAssistantNowMonthStartExpr()
6826
+ ]
6827
+ });
6828
+ return __assign(__assign({}, match), { $expr: nextClauses.length === 1 ? nextClauses[0] : { $and: nextClauses } });
6829
+ };
6830
+ var next = __assign({}, stage);
6831
+ if (next.$match && typeof next.$match === 'object') {
6832
+ next.$match = rewriteMatch(next.$match);
6833
+ }
6834
+ if (next.$geoNear && typeof next.$geoNear === 'object' && next.$geoNear.query) {
6835
+ next.$geoNear = __assign(__assign({}, next.$geoNear), { query: rewriteMatch(next.$geoNear.query) });
6836
+ }
6837
+ return next;
6838
+ });
6839
+ }
6427
6840
  function normalizeAssistantAggregatePipeline(pipeline, collection) {
6428
6841
  if (!Array.isArray(pipeline)) {
6429
6842
  return [];
6430
6843
  }
6431
6844
  var isInvoiceCollection = ['invoice', 'invoices', 'invoice-versions'].includes(String(collection || '').toLowerCase());
6432
- return pipeline
6845
+ var normalizedPipeline = pipeline
6433
6846
  .filter(function (stage) { return stage && typeof stage === 'object' && !Array.isArray(stage); })
6434
6847
  .map(function (stage) {
6435
6848
  var next = __assign({}, stage);
@@ -6447,6 +6860,7 @@ function normalizeAssistantAggregatePipeline(pipeline, collection) {
6447
6860
  }
6448
6861
  return normalizeAssistantNowExprPlaceholdersDeep(next);
6449
6862
  });
6863
+ return normalizeAssistantMonthlyCalendarWindowPipeline(normalizedPipeline);
6450
6864
  }
6451
6865
  function buildAssistantAggregatePipeline(query, pipeline) {
6452
6866
  var _a;
@@ -6575,7 +6989,7 @@ function matchContainsField(value, field) {
6575
6989
  });
6576
6990
  }
6577
6991
  function resolveAggregateCompletionFallback(pipeline) {
6578
- var e_12, _a;
6992
+ var e_14, _a;
6579
6993
  if (!Array.isArray(pipeline)) {
6580
6994
  return null;
6581
6995
  }
@@ -6588,7 +7002,7 @@ function resolveAggregateCompletionFallback(pipeline) {
6588
7002
  }
6589
7003
  var addFields = stage.$addFields;
6590
7004
  try {
6591
- for (var _b = (e_12 = void 0, __values(Object.keys(addFields))), _c = _b.next(); !_c.done; _c = _b.next()) {
7005
+ for (var _b = (e_14 = void 0, __values(Object.keys(addFields))), _c = _b.next(); !_c.done; _c = _b.next()) {
6592
7006
  var key = _c.value;
6593
7007
  if (!isCompletionFieldName(key)) {
6594
7008
  continue;
@@ -6602,12 +7016,12 @@ function resolveAggregateCompletionFallback(pipeline) {
6602
7016
  }
6603
7017
  }
6604
7018
  }
6605
- catch (e_12_1) { e_12 = { error: e_12_1 }; }
7019
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
6606
7020
  finally {
6607
7021
  try {
6608
7022
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6609
7023
  }
6610
- finally { if (e_12) throw e_12.error; }
7024
+ finally { if (e_14) throw e_14.error; }
6611
7025
  }
6612
7026
  if (candidateField) {
6613
7027
  break;
@@ -6763,7 +7177,7 @@ function buildCompletionFallbackSources(field) {
6763
7177
  ]);
6764
7178
  }
6765
7179
  function resolveAggregateCompletionExprFallback(pipeline) {
6766
- var e_13, _a;
7180
+ var e_15, _a;
6767
7181
  if (!Array.isArray(pipeline)) {
6768
7182
  return null;
6769
7183
  }
@@ -6783,7 +7197,7 @@ function resolveAggregateCompletionExprFallback(pipeline) {
6783
7197
  }
6784
7198
  if (!candidateField) {
6785
7199
  try {
6786
- for (var _b = (e_13 = void 0, __values(Object.keys(matchStage))), _c = _b.next(); !_c.done; _c = _b.next()) {
7200
+ for (var _b = (e_15 = void 0, __values(Object.keys(matchStage))), _c = _b.next(); !_c.done; _c = _b.next()) {
6787
7201
  var key = _c.value;
6788
7202
  if (key.startsWith('$')) {
6789
7203
  continue;
@@ -6798,12 +7212,12 @@ function resolveAggregateCompletionExprFallback(pipeline) {
6798
7212
  }
6799
7213
  }
6800
7214
  }
6801
- catch (e_13_1) { e_13 = { error: e_13_1 }; }
7215
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
6802
7216
  finally {
6803
7217
  try {
6804
7218
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
6805
7219
  }
6806
- finally { if (e_13) throw e_13.error; }
7220
+ finally { if (e_15) throw e_15.error; }
6807
7221
  }
6808
7222
  }
6809
7223
  if (!candidateField) {
@@ -6973,7 +7387,7 @@ function isRegexMatchCondition(value) {
6973
7387
  return false;
6974
7388
  }
6975
7389
  function findRegexMatchInMatchObject(match, prefix) {
6976
- var e_14, _a, e_15, _b;
7390
+ var e_16, _a, e_17, _b;
6977
7391
  if (prefix === void 0) { prefix = ''; }
6978
7392
  if (Array.isArray(match)) {
6979
7393
  try {
@@ -6985,12 +7399,12 @@ function findRegexMatchInMatchObject(match, prefix) {
6985
7399
  }
6986
7400
  }
6987
7401
  }
6988
- catch (e_14_1) { e_14 = { error: e_14_1 }; }
7402
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
6989
7403
  finally {
6990
7404
  try {
6991
7405
  if (match_1_1 && !match_1_1.done && (_a = match_1.return)) _a.call(match_1);
6992
7406
  }
6993
- finally { if (e_14) throw e_14.error; }
7407
+ finally { if (e_16) throw e_16.error; }
6994
7408
  }
6995
7409
  return null;
6996
7410
  }
@@ -7021,12 +7435,12 @@ function findRegexMatchInMatchObject(match, prefix) {
7021
7435
  }
7022
7436
  }
7023
7437
  }
7024
- catch (e_15_1) { e_15 = { error: e_15_1 }; }
7438
+ catch (e_17_1) { e_17 = { error: e_17_1 }; }
7025
7439
  finally {
7026
7440
  try {
7027
7441
  if (keys_2_1 && !keys_2_1.done && (_b = keys_2.return)) _b.call(keys_2);
7028
7442
  }
7029
- finally { if (e_15) throw e_15.error; }
7443
+ finally { if (e_17) throw e_17.error; }
7030
7444
  }
7031
7445
  return null;
7032
7446
  }
@@ -7250,7 +7664,7 @@ function collectMatchFieldsByCondition(match, predicate, prefix) {
7250
7664
  return results;
7251
7665
  }
7252
7666
  function findMatchConditionForField(match, targetField, prefix) {
7253
- var e_16, _a, e_17, _b;
7667
+ var e_18, _a, e_19, _b;
7254
7668
  if (prefix === void 0) { prefix = ''; }
7255
7669
  if (!match || typeof match !== 'object') {
7256
7670
  return undefined;
@@ -7265,12 +7679,12 @@ function findMatchConditionForField(match, targetField, prefix) {
7265
7679
  }
7266
7680
  }
7267
7681
  }
7268
- catch (e_16_1) { e_16 = { error: e_16_1 }; }
7682
+ catch (e_18_1) { e_18 = { error: e_18_1 }; }
7269
7683
  finally {
7270
7684
  try {
7271
7685
  if (match_2_1 && !match_2_1.done && (_a = match_2.return)) _a.call(match_2);
7272
7686
  }
7273
- finally { if (e_16) throw e_16.error; }
7687
+ finally { if (e_18) throw e_18.error; }
7274
7688
  }
7275
7689
  return undefined;
7276
7690
  }
@@ -7297,12 +7711,12 @@ function findMatchConditionForField(match, targetField, prefix) {
7297
7711
  }
7298
7712
  }
7299
7713
  }
7300
- catch (e_17_1) { e_17 = { error: e_17_1 }; }
7714
+ catch (e_19_1) { e_19 = { error: e_19_1 }; }
7301
7715
  finally {
7302
7716
  try {
7303
7717
  if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
7304
7718
  }
7305
- finally { if (e_17) throw e_17.error; }
7719
+ finally { if (e_19) throw e_19.error; }
7306
7720
  }
7307
7721
  return undefined;
7308
7722
  }
@@ -7345,7 +7759,7 @@ function detectIdLikeValue(value) {
7345
7759
  return false;
7346
7760
  }
7347
7761
  function detectChemicalIdFromProbe(probeDocs, fields) {
7348
- var e_18, _a, e_19, _b;
7762
+ var e_20, _a, e_21, _b;
7349
7763
  if (!Array.isArray(probeDocs) || !probeDocs.length) {
7350
7764
  return false;
7351
7765
  }
@@ -7357,7 +7771,7 @@ function detectChemicalIdFromProbe(probeDocs, fields) {
7357
7771
  for (var probeDocs_1 = __values(probeDocs), probeDocs_1_1 = probeDocs_1.next(); !probeDocs_1_1.done; probeDocs_1_1 = probeDocs_1.next()) {
7358
7772
  var doc = probeDocs_1_1.value;
7359
7773
  try {
7360
- for (var targets_1 = (e_19 = void 0, __values(targets)), targets_1_1 = targets_1.next(); !targets_1_1.done; targets_1_1 = targets_1.next()) {
7774
+ for (var targets_1 = (e_21 = void 0, __values(targets)), targets_1_1 = targets_1.next(); !targets_1_1.done; targets_1_1 = targets_1.next()) {
7361
7775
  var field = targets_1_1.value;
7362
7776
  var value = getValueAtPath(doc, field);
7363
7777
  if (Array.isArray(value)) {
@@ -7370,21 +7784,21 @@ function detectChemicalIdFromProbe(probeDocs, fields) {
7370
7784
  }
7371
7785
  }
7372
7786
  }
7373
- catch (e_19_1) { e_19 = { error: e_19_1 }; }
7787
+ catch (e_21_1) { e_21 = { error: e_21_1 }; }
7374
7788
  finally {
7375
7789
  try {
7376
7790
  if (targets_1_1 && !targets_1_1.done && (_b = targets_1.return)) _b.call(targets_1);
7377
7791
  }
7378
- finally { if (e_19) throw e_19.error; }
7792
+ finally { if (e_21) throw e_21.error; }
7379
7793
  }
7380
7794
  }
7381
7795
  }
7382
- catch (e_18_1) { e_18 = { error: e_18_1 }; }
7796
+ catch (e_20_1) { e_20 = { error: e_20_1 }; }
7383
7797
  finally {
7384
7798
  try {
7385
7799
  if (probeDocs_1_1 && !probeDocs_1_1.done && (_a = probeDocs_1.return)) _a.call(probeDocs_1);
7386
7800
  }
7387
- finally { if (e_18) throw e_18.error; }
7801
+ finally { if (e_20) throw e_20.error; }
7388
7802
  }
7389
7803
  return false;
7390
7804
  }
@@ -7605,8 +8019,8 @@ function buildChemicalIdFieldCandidates(field) {
7605
8019
  }
7606
8020
  function applyChemicalNameLookupFallbackToQuery(params) {
7607
8021
  return __awaiter(this, void 0, void 0, function () {
7608
- var query, db, dbName, idClient, idCustomer, isSuperAdmin, probeDocs, chemicalFields, targetField, condition, regex, sampleDocs, collectionNames, candidates, _loop_6, candidates_2, candidates_2_1, candidate, state_4, e_20_1;
7609
- var e_20, _a;
8022
+ var query, db, dbName, idClient, idCustomer, isSuperAdmin, probeDocs, chemicalFields, targetField, condition, regex, sampleDocs, collectionNames, candidates, _loop_7, candidates_2, candidates_2_1, candidate, state_5, e_22_1;
8023
+ var e_22, _a;
7610
8024
  var _b;
7611
8025
  return __generator(this, function (_c) {
7612
8026
  switch (_c.label) {
@@ -7630,7 +8044,7 @@ function applyChemicalNameLookupFallbackToQuery(params) {
7630
8044
  case 1:
7631
8045
  collectionNames = _c.sent();
7632
8046
  candidates = resolveIdLookupCandidates('chemical', collectionNames);
7633
- _loop_6 = function (candidate) {
8047
+ _loop_7 = function (candidate) {
7634
8048
  var candidateHasClientScope, _d, candidateProbe, lookup, idFields, baseMatch, orConditions, nextMatch;
7635
8049
  return __generator(this, function (_e) {
7636
8050
  switch (_e.label) {
@@ -7708,25 +8122,25 @@ function applyChemicalNameLookupFallbackToQuery(params) {
7708
8122
  case 3:
7709
8123
  if (!!candidates_2_1.done) return [3 /*break*/, 6];
7710
8124
  candidate = candidates_2_1.value;
7711
- return [5 /*yield**/, _loop_6(candidate)];
8125
+ return [5 /*yield**/, _loop_7(candidate)];
7712
8126
  case 4:
7713
- state_4 = _c.sent();
7714
- if (typeof state_4 === "object")
7715
- return [2 /*return*/, state_4.value];
8127
+ state_5 = _c.sent();
8128
+ if (typeof state_5 === "object")
8129
+ return [2 /*return*/, state_5.value];
7716
8130
  _c.label = 5;
7717
8131
  case 5:
7718
8132
  candidates_2_1 = candidates_2.next();
7719
8133
  return [3 /*break*/, 3];
7720
8134
  case 6: return [3 /*break*/, 9];
7721
8135
  case 7:
7722
- e_20_1 = _c.sent();
7723
- e_20 = { error: e_20_1 };
8136
+ e_22_1 = _c.sent();
8137
+ e_22 = { error: e_22_1 };
7724
8138
  return [3 /*break*/, 9];
7725
8139
  case 8:
7726
8140
  try {
7727
8141
  if (candidates_2_1 && !candidates_2_1.done && (_a = candidates_2.return)) _a.call(candidates_2);
7728
8142
  }
7729
- finally { if (e_20) throw e_20.error; }
8143
+ finally { if (e_22) throw e_22.error; }
7730
8144
  return [7 /*endfinally*/];
7731
8145
  case 9: return [2 /*return*/, null];
7732
8146
  }
@@ -7820,8 +8234,8 @@ function lookupIdsForNameMatch(params) {
7820
8234
  }
7821
8235
  function applyIdLookupFallbackToQuery(params) {
7822
8236
  return __awaiter(this, void 0, void 0, function () {
7823
- var query, db, dbName, idClient, idCustomer, isSuperAdmin, probeDocs, conditions, collectionNames, _a, conditions_1, conditions_1_1, condition, regex, baseToken, targetFieldType, nextValue, localNameField, candidates, candidates_3, candidates_3_1, candidate, candidateHasClientScope, _b, candidateProbe, lookup, targetFieldType, normalizedIds, idsForQuery, e_21_1, e_22_1;
7824
- var e_22, _c, e_21, _d;
8237
+ var query, db, dbName, idClient, idCustomer, isSuperAdmin, probeDocs, conditions, collectionNames, _a, conditions_1, conditions_1_1, condition, regex, baseToken, targetFieldType, nextValue, localNameField, candidates, candidates_3, candidates_3_1, candidate, candidateHasClientScope, _b, candidateProbe, lookup, targetFieldType, normalizedIds, idsForQuery, e_23_1, e_24_1;
8238
+ var e_24, _c, e_23, _d;
7825
8239
  var _e;
7826
8240
  return __generator(this, function (_f) {
7827
8241
  switch (_f.label) {
@@ -7891,7 +8305,7 @@ function applyIdLookupFallbackToQuery(params) {
7891
8305
  _f.label = 5;
7892
8306
  case 5:
7893
8307
  _f.trys.push([5, 14, 15, 16]);
7894
- candidates_3 = (e_21 = void 0, __values(candidates)), candidates_3_1 = candidates_3.next();
8308
+ candidates_3 = (e_23 = void 0, __values(candidates)), candidates_3_1 = candidates_3.next();
7895
8309
  _f.label = 6;
7896
8310
  case 6:
7897
8311
  if (!!candidates_3_1.done) return [3 /*break*/, 13];
@@ -7958,28 +8372,28 @@ function applyIdLookupFallbackToQuery(params) {
7958
8372
  return [3 /*break*/, 6];
7959
8373
  case 13: return [3 /*break*/, 16];
7960
8374
  case 14:
7961
- e_21_1 = _f.sent();
7962
- e_21 = { error: e_21_1 };
8375
+ e_23_1 = _f.sent();
8376
+ e_23 = { error: e_23_1 };
7963
8377
  return [3 /*break*/, 16];
7964
8378
  case 15:
7965
8379
  try {
7966
8380
  if (candidates_3_1 && !candidates_3_1.done && (_d = candidates_3.return)) _d.call(candidates_3);
7967
8381
  }
7968
- finally { if (e_21) throw e_21.error; }
8382
+ finally { if (e_23) throw e_23.error; }
7969
8383
  return [7 /*endfinally*/];
7970
8384
  case 16:
7971
8385
  conditions_1_1 = conditions_1.next();
7972
8386
  return [3 /*break*/, 4];
7973
8387
  case 17: return [3 /*break*/, 20];
7974
8388
  case 18:
7975
- e_22_1 = _f.sent();
7976
- e_22 = { error: e_22_1 };
8389
+ e_24_1 = _f.sent();
8390
+ e_24 = { error: e_24_1 };
7977
8391
  return [3 /*break*/, 20];
7978
8392
  case 19:
7979
8393
  try {
7980
8394
  if (conditions_1_1 && !conditions_1_1.done && (_c = conditions_1.return)) _c.call(conditions_1);
7981
8395
  }
7982
- finally { if (e_22) throw e_22.error; }
8396
+ finally { if (e_24) throw e_24.error; }
7983
8397
  return [7 /*endfinally*/];
7984
8398
  case 20: return [2 /*return*/, null];
7985
8399
  }
@@ -8831,7 +9245,7 @@ function resolveAssistantReportBuilderBridgeCollection(collection) {
8831
9245
  throw new Error('AI assistant report builder bridge: Collection is not configured for report builder.');
8832
9246
  }
8833
9247
  function findQueryDateField(query) {
8834
- var e_23, _a, e_24, _b;
9248
+ var e_25, _a, e_26, _b;
8835
9249
  if (!query || typeof query !== 'object') {
8836
9250
  return null;
8837
9251
  }
@@ -8845,12 +9259,12 @@ function findQueryDateField(query) {
8845
9259
  }
8846
9260
  }
8847
9261
  }
8848
- catch (e_23_1) { e_23 = { error: e_23_1 }; }
9262
+ catch (e_25_1) { e_25 = { error: e_25_1 }; }
8849
9263
  finally {
8850
9264
  try {
8851
9265
  if (query_1_1 && !query_1_1.done && (_a = query_1.return)) _a.call(query_1);
8852
9266
  }
8853
- finally { if (e_23) throw e_23.error; }
9267
+ finally { if (e_25) throw e_25.error; }
8854
9268
  }
8855
9269
  return null;
8856
9270
  }
@@ -8869,12 +9283,12 @@ function findQueryDateField(query) {
8869
9283
  }
8870
9284
  }
8871
9285
  }
8872
- catch (e_24_1) { e_24 = { error: e_24_1 }; }
9286
+ catch (e_26_1) { e_26 = { error: e_26_1 }; }
8873
9287
  finally {
8874
9288
  try {
8875
9289
  if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
8876
9290
  }
8877
- finally { if (e_24) throw e_24.error; }
9291
+ finally { if (e_26) throw e_26.error; }
8878
9292
  }
8879
9293
  return null;
8880
9294
  }
@@ -9030,7 +9444,7 @@ function expandAggregateDateMatchFallback(pipeline) {
9030
9444
  if (!Array.isArray(pipeline)) {
9031
9445
  return null;
9032
9446
  }
9033
- var _loop_7 = function (i) {
9447
+ var _loop_8 = function (i) {
9034
9448
  var stage = pipeline[i];
9035
9449
  if (!stage || typeof stage !== 'object' || !stage.$match || typeof stage.$match !== 'object') {
9036
9450
  return "continue";
@@ -9078,9 +9492,9 @@ function expandAggregateDateMatchFallback(pipeline) {
9078
9492
  return { value: { pipeline: updated, fields: Array.from(addedFields) } };
9079
9493
  };
9080
9494
  for (var i = 0; i < pipeline.length; i += 1) {
9081
- var state_5 = _loop_7(i);
9082
- if (typeof state_5 === "object")
9083
- return state_5.value;
9495
+ var state_6 = _loop_8(i);
9496
+ if (typeof state_6 === "object")
9497
+ return state_6.value;
9084
9498
  }
9085
9499
  return null;
9086
9500
  }
@@ -9096,7 +9510,7 @@ function resolveQueryDateFieldFallback(query) {
9096
9510
  return { from: dateField, to: fallback };
9097
9511
  }
9098
9512
  function containsForbiddenMongoOperators(value) {
9099
- var e_25, _a;
9513
+ var e_27, _a;
9100
9514
  if (!value || typeof value !== 'object') {
9101
9515
  return false;
9102
9516
  }
@@ -9115,12 +9529,12 @@ function containsForbiddenMongoOperators(value) {
9115
9529
  }
9116
9530
  }
9117
9531
  }
9118
- catch (e_25_1) { e_25 = { error: e_25_1 }; }
9532
+ catch (e_27_1) { e_27 = { error: e_27_1 }; }
9119
9533
  finally {
9120
9534
  try {
9121
9535
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
9122
9536
  }
9123
- finally { if (e_25) throw e_25.error; }
9537
+ finally { if (e_27) throw e_27.error; }
9124
9538
  }
9125
9539
  return false;
9126
9540
  }
@@ -9555,8 +9969,8 @@ function applyCodexStreamStatusHandler(runOptions, streamStatusHandler) {
9555
9969
  }
9556
9970
  function waitForCodexWorkerMessage(worker, streamStatusHandler) {
9557
9971
  return __awaiter(this, void 0, void 0, function () {
9558
- var _a, _b, _c, _d, message, payload, status_1, e_26_1;
9559
- var _e, e_26, _f, _g;
9972
+ var _a, _b, _c, _d, message, payload, status_1, e_28_1;
9973
+ var _e, e_28, _f, _g;
9560
9974
  return __generator(this, function (_h) {
9561
9975
  switch (_h.label) {
9562
9976
  case 0:
@@ -9583,8 +9997,8 @@ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
9583
9997
  return [3 /*break*/, 1];
9584
9998
  case 4: return [3 /*break*/, 11];
9585
9999
  case 5:
9586
- e_26_1 = _h.sent();
9587
- e_26 = { error: e_26_1 };
10000
+ e_28_1 = _h.sent();
10001
+ e_28 = { error: e_28_1 };
9588
10002
  return [3 /*break*/, 11];
9589
10003
  case 6:
9590
10004
  _h.trys.push([6, , 9, 10]);
@@ -9595,7 +10009,7 @@ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
9595
10009
  _h.label = 8;
9596
10010
  case 8: return [3 /*break*/, 10];
9597
10011
  case 9:
9598
- if (e_26) throw e_26.error;
10012
+ if (e_28) throw e_28.error;
9599
10013
  return [7 /*endfinally*/];
9600
10014
  case 10: return [7 /*endfinally*/];
9601
10015
  case 11: throw new CodexWorkerBootstrapError('Codex worker exited before completing.');
@@ -10201,7 +10615,7 @@ function resolveAssistantPlannerEnabled() {
10201
10615
  return raw === undefined ? true : raw === true;
10202
10616
  }
10203
10617
  function resolveAssistantPlannerKnownRoutes() {
10204
- var e_27, _a;
10618
+ var e_29, _a;
10205
10619
  var _b;
10206
10620
  var routes = ((_b = resolveio_server_app_1.ResolveIOServer.getClientRoutes) === null || _b === void 0 ? void 0 : _b.call(resolveio_server_app_1.ResolveIOServer)) || [];
10207
10621
  var unique = new Set();
@@ -10214,12 +10628,12 @@ function resolveAssistantPlannerKnownRoutes() {
10214
10628
  }
10215
10629
  }
10216
10630
  }
10217
- catch (e_27_1) { e_27 = { error: e_27_1 }; }
10631
+ catch (e_29_1) { e_29 = { error: e_29_1 }; }
10218
10632
  finally {
10219
10633
  try {
10220
10634
  if (routes_1_1 && !routes_1_1.done && (_a = routes_1.return)) _a.call(routes_1);
10221
10635
  }
10222
- finally { if (e_27) throw e_27.error; }
10636
+ finally { if (e_29) throw e_29.error; }
10223
10637
  }
10224
10638
  return Array.from(unique).slice(0, AI_ASSISTANT_PLANNER_MAX_ROUTES);
10225
10639
  }
@@ -10462,7 +10876,7 @@ function normalizeRouteMatchKey(value) {
10462
10876
  return normalizeRouteKey(value).toLowerCase();
10463
10877
  }
10464
10878
  function buildClientRouteIndex() {
10465
- var e_28, _a;
10879
+ var e_30, _a;
10466
10880
  var _b;
10467
10881
  var routes = ((_b = resolveio_server_app_1.ResolveIOServer.getClientRoutes) === null || _b === void 0 ? void 0 : _b.call(resolveio_server_app_1.ResolveIOServer)) || [];
10468
10882
  var set = new Set();
@@ -10481,12 +10895,12 @@ function buildClientRouteIndex() {
10481
10895
  }
10482
10896
  }
10483
10897
  }
10484
- catch (e_28_1) { e_28 = { error: e_28_1 }; }
10898
+ catch (e_30_1) { e_30 = { error: e_30_1 }; }
10485
10899
  finally {
10486
10900
  try {
10487
10901
  if (routes_2_1 && !routes_2_1.done && (_a = routes_2.return)) _a.call(routes_2);
10488
10902
  }
10489
- finally { if (e_28) throw e_28.error; }
10903
+ finally { if (e_30) throw e_30.error; }
10490
10904
  }
10491
10905
  return { set: set, map: map, size: routes.length };
10492
10906
  }
@@ -10627,7 +11041,7 @@ function sanitizeAssistantResponse(value) {
10627
11041
  return normalizeAssistantRoutes(normalizedCurrency);
10628
11042
  }
10629
11043
  function evaluateAssistantGuardrails(message) {
10630
- var e_29, _a;
11044
+ var e_31, _a;
10631
11045
  var normalized = String(message || '').toLowerCase();
10632
11046
  var patterns = [
10633
11047
  {
@@ -10673,12 +11087,12 @@ function evaluateAssistantGuardrails(message) {
10673
11087
  }
10674
11088
  }
10675
11089
  }
10676
- catch (e_29_1) { e_29 = { error: e_29_1 }; }
11090
+ catch (e_31_1) { e_31 = { error: e_31_1 }; }
10677
11091
  finally {
10678
11092
  try {
10679
11093
  if (patterns_1_1 && !patterns_1_1.done && (_a = patterns_1.return)) _a.call(patterns_1);
10680
11094
  }
10681
- finally { if (e_29) throw e_29.error; }
11095
+ finally { if (e_31) throw e_31.error; }
10682
11096
  }
10683
11097
  return null;
10684
11098
  }
@@ -10793,7 +11207,7 @@ function tokenizeArithmeticExpression(expression) {
10793
11207
  return tokens;
10794
11208
  }
10795
11209
  function evaluateArithmeticExpression(expression) {
10796
- var e_30, _a, e_31, _b;
11210
+ var e_32, _a, e_33, _b;
10797
11211
  var tokens = tokenizeArithmeticExpression(expression);
10798
11212
  if (!tokens || !tokens.length) {
10799
11213
  return null;
@@ -10850,12 +11264,12 @@ function evaluateArithmeticExpression(expression) {
10850
11264
  prevToken = token;
10851
11265
  }
10852
11266
  }
10853
- catch (e_30_1) { e_30 = { error: e_30_1 }; }
11267
+ catch (e_32_1) { e_32 = { error: e_32_1 }; }
10854
11268
  finally {
10855
11269
  try {
10856
11270
  if (tokens_1_1 && !tokens_1_1.done && (_a = tokens_1.return)) _a.call(tokens_1);
10857
11271
  }
10858
- finally { if (e_30) throw e_30.error; }
11272
+ finally { if (e_32) throw e_32.error; }
10859
11273
  }
10860
11274
  while (ops.length) {
10861
11275
  var op = ops.pop();
@@ -10895,12 +11309,12 @@ function evaluateArithmeticExpression(expression) {
10895
11309
  stack.push(Number(token));
10896
11310
  }
10897
11311
  }
10898
- catch (e_31_1) { e_31 = { error: e_31_1 }; }
11312
+ catch (e_33_1) { e_33 = { error: e_33_1 }; }
10899
11313
  finally {
10900
11314
  try {
10901
11315
  if (output_1_1 && !output_1_1.done && (_b = output_1.return)) _b.call(output_1);
10902
11316
  }
10903
- finally { if (e_31) throw e_31.error; }
11317
+ finally { if (e_33) throw e_33.error; }
10904
11318
  }
10905
11319
  if (stack.length !== 1 || Number.isNaN(stack[0])) {
10906
11320
  return null;
@@ -11084,8 +11498,8 @@ function handleCodexUpload(id_conversation, file_name, content_base64, size, con
11084
11498
  }
11085
11499
  function readAttachmentContents(attachments) {
11086
11500
  return __awaiter(this, void 0, void 0, function () {
11087
- var limits, totalBytes, totalChars, chunks, cleaned, attachments_1, attachments_1_1, attachment, localPath, safe, stat, ext, name_1, type, readable, content, _a, e_32_1;
11088
- var e_32, _b;
11501
+ var limits, totalBytes, totalChars, chunks, cleaned, attachments_1, attachments_1_1, attachment, localPath, safe, stat, ext, name_1, type, readable, content, _a, e_34_1;
11502
+ var e_34, _b;
11089
11503
  return __generator(this, function (_c) {
11090
11504
  switch (_c.label) {
11091
11505
  case 0:
@@ -11164,14 +11578,14 @@ function readAttachmentContents(attachments) {
11164
11578
  return [3 /*break*/, 2];
11165
11579
  case 10: return [3 /*break*/, 13];
11166
11580
  case 11:
11167
- e_32_1 = _c.sent();
11168
- e_32 = { error: e_32_1 };
11581
+ e_34_1 = _c.sent();
11582
+ e_34 = { error: e_34_1 };
11169
11583
  return [3 /*break*/, 13];
11170
11584
  case 12:
11171
11585
  try {
11172
11586
  if (attachments_1_1 && !attachments_1_1.done && (_b = attachments_1.return)) _b.call(attachments_1);
11173
11587
  }
11174
- finally { if (e_32) throw e_32.error; }
11588
+ finally { if (e_34) throw e_34.error; }
11175
11589
  return [7 /*endfinally*/];
11176
11590
  case 13: return [2 /*return*/, {
11177
11591
  promptText: chunks.length ? "\n\nAttachments:\n".concat(chunks.join('\n\n')) : '',
@@ -11412,7 +11826,7 @@ function estimateUsage(messages, responseText, model) {
11412
11826
  };
11413
11827
  }
11414
11828
  function evaluateGuardrails(message) {
11415
- var e_33, _a;
11829
+ var e_35, _a;
11416
11830
  var normalized = String(message || '').toLowerCase();
11417
11831
  var patterns = [
11418
11832
  { pattern: /\b(source\s*code|full\s*code|entire\s*code|repo\s*dump|repository|git\s*clone)\b/i, reason: 'Code access is restricted.' },
@@ -11434,12 +11848,12 @@ function evaluateGuardrails(message) {
11434
11848
  }
11435
11849
  }
11436
11850
  }
11437
- catch (e_33_1) { e_33 = { error: e_33_1 }; }
11851
+ catch (e_35_1) { e_35 = { error: e_35_1 }; }
11438
11852
  finally {
11439
11853
  try {
11440
11854
  if (patterns_2_1 && !patterns_2_1.done && (_a = patterns_2.return)) _a.call(patterns_2);
11441
11855
  }
11442
- finally { if (e_33) throw e_33.error; }
11856
+ finally { if (e_35) throw e_35.error; }
11443
11857
  }
11444
11858
  return null;
11445
11859
  }