@resolveio/server-lib 22.0.21 → 22.0.23

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.
@@ -1665,7 +1665,7 @@ function expandLayoutColumnFilters(filters, selectedFields) {
1665
1665
  }
1666
1666
  function executeReportBuilderAi(payload, context) {
1667
1667
  return __awaiter(this, void 0, void 0, function () {
1668
- var input, prompt, isSuperAdmin, guardrailsEnabled, guardrail, reportType, collectionRoot, collectionJoins, collections, fieldLimit, resolvedFields, usedFallbackFields, derivedFields, trimmedFields, arraySummary, systemPrompt, userPrompt, codexModel, codexClient, runOptions, responseText, usage, parsed, allowMongoRead, mongoPayload, mongoReadResult, error_1, message, mongoContext, usage2, sanitizeContext, patch, summaryNotes, idClient;
1668
+ var input, prompt, isSuperAdmin, guardrailsEnabled, guardrail, reportTypeResolution, reportType, collectionRoot, collectionJoins, idDateField, dateInterval, collections, fieldLimit, resolvedFields, usedFallbackFields, derivedFields, trimmedFields, arraySummary, systemPrompt, userPrompt, codexModel, codexClient, runOptions, responseText, usage, parsed, allowMongoRead, mongoPayload, mongoReadResult, error_1, message, mongoContext, usage2, sanitizeContext, patch, summaryNotes, idClient;
1669
1669
  return __generator(this, function (_a) {
1670
1670
  switch (_a.label) {
1671
1671
  case 0:
@@ -1684,9 +1684,12 @@ function executeReportBuilderAi(payload, context) {
1684
1684
  return [2 /*return*/, guardrail];
1685
1685
  }
1686
1686
  }
1687
- reportType = normalizeReportType(input.report_type);
1687
+ reportTypeResolution = resolveReportTypeForPrompt(input.report_type, prompt);
1688
+ reportType = reportTypeResolution.reportType;
1688
1689
  collectionRoot = normalizeOptionalString(input.collection_root);
1689
1690
  collectionJoins = Array.isArray(input.collection_joins) ? input.collection_joins : [];
1691
+ idDateField = normalizeOptionalString(input.id_date_field);
1692
+ dateInterval = normalizeDateInterval(input.date_interval) || reportTypeResolution.inferredDateInterval;
1690
1693
  collections = sanitizeCollections(input.available_collections || []);
1691
1694
  fieldLimit = normalizeFieldLimit(input.field_limit);
1692
1695
  resolvedFields = sanitizeFields(input.available_fields || []);
@@ -1705,8 +1708,8 @@ function executeReportBuilderAi(payload, context) {
1705
1708
  reportName: normalizeOptionalString(input.report_name),
1706
1709
  collectionRoot: collectionRoot,
1707
1710
  collectionJoins: collectionJoins,
1708
- idDateField: normalizeOptionalString(input.id_date_field),
1709
- dateInterval: normalizeOptionalString(input.date_interval),
1711
+ idDateField: idDateField,
1712
+ dateInterval: dateInterval,
1710
1713
  collections: collections,
1711
1714
  fields: trimmedFields.fields,
1712
1715
  fieldMeta: {
@@ -1755,8 +1758,8 @@ function executeReportBuilderAi(payload, context) {
1755
1758
  reportName: normalizeOptionalString(input.report_name),
1756
1759
  collectionRoot: collectionRoot,
1757
1760
  collectionJoins: collectionJoins,
1758
- idDateField: normalizeOptionalString(input.id_date_field),
1759
- dateInterval: normalizeOptionalString(input.date_interval),
1761
+ idDateField: idDateField,
1762
+ dateInterval: dateInterval,
1760
1763
  collections: collections,
1761
1764
  fields: trimmedFields.fields,
1762
1765
  fieldMeta: {
@@ -1787,11 +1790,16 @@ function executeReportBuilderAi(payload, context) {
1787
1790
  }
1788
1791
  sanitizeContext = buildSanitizerContext(collections, resolvedFields);
1789
1792
  patch = sanitizeReportBuilderPatch(parsed, sanitizeContext);
1793
+ patch.report_type = reportType;
1794
+ if (reportType === 'Dated' && !patch.date_interval && dateInterval) {
1795
+ patch.date_interval = dateInterval;
1796
+ }
1790
1797
  summaryNotes = buildPatchNotes(patch, {
1791
1798
  reportType: reportType,
1792
1799
  truncated: trimmedFields.truncated,
1793
1800
  totalFields: trimmedFields.total,
1794
- usedFields: trimmedFields.fields.length
1801
+ usedFields: trimmedFields.fields.length,
1802
+ inferenceReason: reportTypeResolution.inferenceReason
1795
1803
  });
1796
1804
  return [4 /*yield*/, resolveClientId(input.id_client, context === null || context === void 0 ? void 0 : context.id_user)];
1797
1805
  case 9:
@@ -1852,6 +1860,7 @@ function buildReportBuilderSystemPrompt(reportType) {
1852
1860
  'Use filters with explicit boolean logic: $and for required constraints and $or for alternate matches.',
1853
1861
  'Use custom fields when the request asks for derived metrics (ratios, percentages, margins, computed totals).',
1854
1862
  'If the request specifies a time grain (daily/weekly/monthly/etc), set date_interval and id_date_field to the best matching date field.',
1863
+ 'Treat requests like "over the last X", "by month", "by quarter", and "by <time period>" as Dated report intent.',
1855
1864
  'If you need sample records to confirm field choices, you may return JSON with {"mongo_read": { "collection": "<collection>", "query": {}, "options": {"limit": 5} }, "notes": "why you need data"} .',
1856
1865
  'Only request mongo_read when necessary and only use collections from available_collections.',
1857
1866
  'When mongo_read_result is present in the user context, use it to finalize the patch and do not request another read.',
@@ -1963,6 +1972,60 @@ function getReportBuilderCodexClient() {
1963
1972
  function buildReportBuilderCodexPrompt(systemPrompt, userPrompt) {
1964
1973
  return "System:\n".concat(systemPrompt, "\n\nUser:\n").concat(userPrompt).trim();
1965
1974
  }
1975
+ function resolveReportTypeForPrompt(inputType, prompt) {
1976
+ var requestedType = normalizeReportType(inputType);
1977
+ var inferredInterval = inferDateIntervalFromPrompt(prompt);
1978
+ if (!isDatedPrompt(prompt)) {
1979
+ return {
1980
+ reportType: requestedType,
1981
+ inferenceReason: '',
1982
+ inferredDateInterval: inferredInterval
1983
+ };
1984
+ }
1985
+ var reason = requestedType !== 'Dated'
1986
+ ? 'Auto-inferred Dated report from time-period language in the request.'
1987
+ : '';
1988
+ return {
1989
+ reportType: 'Dated',
1990
+ inferenceReason: reason,
1991
+ inferredDateInterval: inferredInterval
1992
+ };
1993
+ }
1994
+ function isDatedPrompt(prompt) {
1995
+ var text = normalizeOptionalString(prompt).toLowerCase();
1996
+ if (!text) {
1997
+ return false;
1998
+ }
1999
+ var patterns = [
2000
+ /\b(by|per)\s+(day|week|month|quarter|year|hour|minute|second)s?\b/i,
2001
+ /\b(daily|weekly|monthly|quarterly|yearly)\b/i,
2002
+ /\b(last|past|previous|recent)\s+(?:\d+|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)\s+(day|week|month|quarter|year|hour|minute|second)s?\b/i,
2003
+ /\bover\s+the\s+last\s+(?:\d+|one|two|three|four|five|six|seven|eight|nine|ten|eleven|twelve)\s+(day|week|month|quarter|year|hour|minute|second)s?\b/i,
2004
+ /\b(month|quarter|year)\s+over\s+(month|quarter|year)\b/i,
2005
+ /\b(over\s+time|time\s*series|trend(?:ing|s)?)\b/i,
2006
+ /\bby\s+(?:a\s+)?(?:time|date)\s+period\b/i,
2007
+ /\bby\s+period\b/i
2008
+ ];
2009
+ return patterns.some(function (pattern) { return pattern.test(text); });
2010
+ }
2011
+ function inferDateIntervalFromPrompt(prompt) {
2012
+ var text = normalizeOptionalString(prompt).toLowerCase();
2013
+ if (!text) {
2014
+ return '';
2015
+ }
2016
+ var mapping = [
2017
+ { interval: 'Quarterly', pattern: /\b(quarter|quarters|quarterly|qoq)\b/i },
2018
+ { interval: 'Monthly', pattern: /\b(month|months|monthly|mom)\b/i },
2019
+ { interval: 'Weekly', pattern: /\b(week|weeks|weekly|wow)\b/i },
2020
+ { interval: 'Daily', pattern: /\b(day|days|daily)\b/i },
2021
+ { interval: 'Yearly', pattern: /\b(year|years|yearly|yoy)\b/i },
2022
+ { interval: 'Hours', pattern: /\b(hour|hours|hourly)\b/i },
2023
+ { interval: 'Minutes', pattern: /\b(minute|minutes)\b/i },
2024
+ { interval: 'Seconds', pattern: /\b(second|seconds)\b/i }
2025
+ ];
2026
+ var matched = mapping.find(function (entry) { return entry.pattern.test(text); });
2027
+ return (matched === null || matched === void 0 ? void 0 : matched.interval) || '';
2028
+ }
1966
2029
  function normalizeReportType(value) {
1967
2030
  var normalized = normalizeOptionalString(value);
1968
2031
  if (normalized.toLowerCase() === 'group') {
@@ -2586,6 +2649,9 @@ function buildPatchNotes(patch, meta) {
2586
2649
  if (patch === null || patch === void 0 ? void 0 : patch.notes) {
2587
2650
  note += " ".concat(patch.notes);
2588
2651
  }
2652
+ if (meta.inferenceReason) {
2653
+ note += " ".concat(meta.inferenceReason);
2654
+ }
2589
2655
  if (meta.truncated) {
2590
2656
  note += " Field list trimmed to ".concat(meta.usedFields, " of ").concat(meta.totalFields, " available fields.");
2591
2657
  }