@resolveio/server-lib 22.2.17 → 22.2.19

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.
@@ -183,6 +183,10 @@ export declare function buildDisplayTable(docs: any[], options?: {
183
183
  export declare function formatDisplayTableMarkdown(display: AiAssistantDisplayTable): string;
184
184
  export declare function resolveAssistantReadDisplayMaxRows(limit: number | undefined, rowCount: number): number;
185
185
  export declare function normalizeAssistantNowExprPlaceholders(value: any): any;
186
+ export declare function repairAssistantPositionalPathSegmentsInPipeline(pipeline: Array<Record<string, any>>): {
187
+ pipeline: Array<Record<string, any>>;
188
+ changed: boolean;
189
+ };
186
190
  export declare function repairAssistantFieldPathReferenceInPipeline(pipeline: Array<Record<string, any>>, fieldPath: string): {
187
191
  pipeline: Array<Record<string, any>>;
188
192
  changed: boolean;
@@ -195,6 +199,19 @@ export declare function stripScopedFieldsFromPipeline(pipeline: any[], fieldsToS
195
199
  export declare function rewriteEmbeddedMatchObjects(query: any): any;
196
200
  export declare function resolveAssistantAppCollectionHintCandidates(message: string, appId?: string): string[];
197
201
  export declare function isAssistantDeterministicHeuristicEnabledForApp(heuristicId: string, appId?: string): boolean;
202
+ export declare function resolveCollectionOverrideWithContextForTesting(params: {
203
+ message: string;
204
+ collectionRanking: Record<string, any> | null;
205
+ requestedCollection: string;
206
+ permissionView?: string;
207
+ collectionNames: string[];
208
+ }): {
209
+ from: string;
210
+ to: string;
211
+ fromScore: number;
212
+ toScore: number;
213
+ reason: string;
214
+ } | null;
198
215
  export declare function resolveAssistantCollectionOverride(collectionRanking: Record<string, any> | null, currentCollection: string): {
199
216
  from: string;
200
217
  to: string;
@@ -229,6 +246,10 @@ export declare function resolveAssistantReportBuilderBridgeCollection(collection
229
246
  mode: 'report-builder' | 'direct-mongo';
230
247
  resolutionPath: string[];
231
248
  }>;
249
+ export declare function expandAggregateDateMatchFallbackForTesting(pipeline: Array<Record<string, any>>, schemaFields?: string[]): {
250
+ pipeline: Array<Record<string, any>>;
251
+ fields: string[];
252
+ } | null;
232
253
  export declare function collectUserViewPermissions(user: any): string[];
233
254
  export declare function userHasInvoiceAccess(user: any): boolean;
234
255
  export declare function resolveAssistantUserAccessTier(user: any): 'super_admin' | 'customer_portal' | 'client_user';
@@ -121,6 +121,7 @@ exports.buildDisplayTable = buildDisplayTable;
121
121
  exports.formatDisplayTableMarkdown = formatDisplayTableMarkdown;
122
122
  exports.resolveAssistantReadDisplayMaxRows = resolveAssistantReadDisplayMaxRows;
123
123
  exports.normalizeAssistantNowExprPlaceholders = normalizeAssistantNowExprPlaceholders;
124
+ exports.repairAssistantPositionalPathSegmentsInPipeline = repairAssistantPositionalPathSegmentsInPipeline;
124
125
  exports.repairAssistantFieldPathReferenceInPipeline = repairAssistantFieldPathReferenceInPipeline;
125
126
  exports.rewriteMatchExpressionsToExpr = rewriteMatchExpressionsToExpr;
126
127
  exports.normalizeAssistantMonthlyCalendarWindowPipeline = normalizeAssistantMonthlyCalendarWindowPipeline;
@@ -130,12 +131,14 @@ exports.stripScopedFieldsFromPipeline = stripScopedFieldsFromPipeline;
130
131
  exports.rewriteEmbeddedMatchObjects = rewriteEmbeddedMatchObjects;
131
132
  exports.resolveAssistantAppCollectionHintCandidates = resolveAssistantAppCollectionHintCandidates;
132
133
  exports.isAssistantDeterministicHeuristicEnabledForApp = isAssistantDeterministicHeuristicEnabledForApp;
134
+ exports.resolveCollectionOverrideWithContextForTesting = resolveCollectionOverrideWithContextForTesting;
133
135
  exports.resolveAssistantCollectionOverride = resolveAssistantCollectionOverride;
134
136
  exports.resolveAssistantCrossCollectionFallbackCandidates = resolveAssistantCrossCollectionFallbackCandidates;
135
137
  exports.resolveAssistantAvailableCrossCollectionFallbacksFromNames = resolveAssistantAvailableCrossCollectionFallbacksFromNames;
136
138
  exports.evaluateAssistantFallbackDocumentsQuality = evaluateAssistantFallbackDocumentsQuality;
137
139
  exports.shouldAcceptAssistantFallbackDocuments = shouldAcceptAssistantFallbackDocuments;
138
140
  exports.resolveAssistantReportBuilderBridgeCollection = resolveAssistantReportBuilderBridgeCollection;
141
+ exports.expandAggregateDateMatchFallbackForTesting = expandAggregateDateMatchFallbackForTesting;
139
142
  exports.collectUserViewPermissions = collectUserViewPermissions;
140
143
  exports.userHasInvoiceAccess = userHasInvoiceAccess;
141
144
  exports.resolveAssistantUserAccessTier = resolveAssistantUserAccessTier;
@@ -4157,7 +4160,7 @@ function executeAiAssistantMongoAggregate(payload, context) {
4157
4160
  : { readPreference: AI_ASSISTANT_READ_PREFERENCE };
4158
4161
  };
4159
4162
  runAggregateWithRepair = function (pipeline, stage) { return __awaiter(_this, void 0, void 0, function () {
4160
- var aggregateOptions, docs, error_9, isDateRepair, isRankSortByRepair, isDottedOutputFieldRepair, repaired, dottedFieldPath, dottedFieldReferenceRepairUsed, pathReferenceRepair, docs;
4163
+ var aggregateOptions, docs, error_9, isDateRepair, isRankSortByRepair, isDottedOutputFieldRepair, isPositionalPathRepair, repaired, dottedFieldPath, dottedFieldReferenceRepairUsed, pathReferenceRepair, docs;
4161
4164
  return __generator(this, function (_a) {
4162
4165
  switch (_a.label) {
4163
4166
  case 0:
@@ -4179,14 +4182,17 @@ function executeAiAssistantMongoAggregate(payload, context) {
4179
4182
  isDateRepair = isAssistantDateArithmeticArgumentError(error_9);
4180
4183
  isRankSortByRepair = isAssistantRankSortByError(error_9);
4181
4184
  isDottedOutputFieldRepair = isAssistantDottedOutputFieldError(error_9);
4182
- if (!isDateRepair && !isRankSortByRepair && !isDottedOutputFieldRepair) {
4185
+ isPositionalPathRepair = isAssistantPositionalPathError(error_9);
4186
+ if (!isDateRepair && !isRankSortByRepair && !isDottedOutputFieldRepair && !isPositionalPathRepair) {
4183
4187
  throw error_9;
4184
4188
  }
4185
4189
  repaired = isDateRepair
4186
4190
  ? repairAssistantDateArithmeticInPipeline(pipeline)
4187
4191
  : isRankSortByRepair
4188
4192
  ? repairAssistantRankSortByInPipeline(pipeline)
4189
- : repairAssistantDottedOutputFieldNamesInPipeline(pipeline);
4193
+ : isDottedOutputFieldRepair
4194
+ ? repairAssistantDottedOutputFieldNamesInPipeline(pipeline)
4195
+ : repairAssistantPositionalPathSegmentsInPipeline(pipeline);
4190
4196
  dottedFieldPath = null;
4191
4197
  dottedFieldReferenceRepairUsed = false;
4192
4198
  if (isDottedOutputFieldRepair) {
@@ -4237,6 +4243,14 @@ function executeAiAssistantMongoAggregate(payload, context) {
4237
4243
  fieldPath: dottedFieldPath || undefined
4238
4244
  };
4239
4245
  }
4246
+ if (isPositionalPathRepair) {
4247
+ fallbackMeta.positionalPath = {
4248
+ attempted: true,
4249
+ used: true,
4250
+ stage: stage,
4251
+ reason: 'removed_positional_path_segments'
4252
+ };
4253
+ }
4240
4254
  return [2 /*return*/, { documents: docs, pipeline: repaired.pipeline }];
4241
4255
  case 5: return [2 /*return*/];
4242
4256
  }
@@ -13465,6 +13479,76 @@ function normalizeAssistantNowExprPlaceholdersDeep(value) {
13465
13479
  function normalizeAssistantNowExprPlaceholders(value) {
13466
13480
  return normalizeAssistantNowExprPlaceholdersDeep(value);
13467
13481
  }
13482
+ function stripAssistantPositionalPathSegments(path) {
13483
+ var normalized = normalizeOptionalString(path);
13484
+ if (!normalized || !normalized.includes('.$')) {
13485
+ return normalized;
13486
+ }
13487
+ return normalized.replace(/\.\$(?=\.|$)/g, '');
13488
+ }
13489
+ function repairAssistantPositionalPathSegmentsDeep(value) {
13490
+ var changed = false;
13491
+ var rewriteValue = function (current) {
13492
+ if (Array.isArray(current)) {
13493
+ return current.map(function (entry) { return rewriteValue(entry); });
13494
+ }
13495
+ if (!current || typeof current !== 'object' || current instanceof Date || current instanceof RegExp || isMongoObjectId(current)) {
13496
+ if (typeof current === 'string' && current.startsWith('$') && !current.startsWith('$$')) {
13497
+ var strippedPath = stripAssistantPositionalPathSegments(current.slice(1));
13498
+ if (strippedPath && strippedPath !== current.slice(1)) {
13499
+ changed = true;
13500
+ return "$".concat(strippedPath);
13501
+ }
13502
+ }
13503
+ return current;
13504
+ }
13505
+ var next = {};
13506
+ Object.keys(current).forEach(function (key) {
13507
+ var nextKey = key;
13508
+ if (!key.startsWith('$')) {
13509
+ var strippedKey = stripAssistantPositionalPathSegments(key);
13510
+ if (strippedKey && strippedKey !== key) {
13511
+ nextKey = strippedKey;
13512
+ changed = true;
13513
+ }
13514
+ }
13515
+ next[nextKey] = rewriteValue(current[key]);
13516
+ });
13517
+ return next;
13518
+ };
13519
+ return {
13520
+ value: rewriteValue(value),
13521
+ changed: changed
13522
+ };
13523
+ }
13524
+ function repairAssistantPositionalPathSegmentsInQuery(query) {
13525
+ var repaired = repairAssistantPositionalPathSegmentsDeep(query);
13526
+ var normalizedQuery = (repaired.value && typeof repaired.value === 'object' && !Array.isArray(repaired.value))
13527
+ ? repaired.value
13528
+ : query;
13529
+ return {
13530
+ query: normalizedQuery,
13531
+ changed: repaired.changed
13532
+ };
13533
+ }
13534
+ function repairAssistantPositionalPathSegmentsInPipeline(pipeline) {
13535
+ var repaired = repairAssistantPositionalPathSegmentsDeep(pipeline);
13536
+ var normalizedPipeline = Array.isArray(repaired.value)
13537
+ ? repaired.value
13538
+ : pipeline;
13539
+ return {
13540
+ pipeline: normalizedPipeline,
13541
+ changed: repaired.changed
13542
+ };
13543
+ }
13544
+ function isAssistantPositionalPathError(error) {
13545
+ var message = normalizeOptionalString(error === null || error === void 0 ? void 0 : error.message).toLowerCase();
13546
+ if (!message) {
13547
+ return false;
13548
+ }
13549
+ return message.includes('positional operator')
13550
+ && message.includes('middle of a path');
13551
+ }
13468
13552
  function isAssistantDateArithmeticArgumentError(error) {
13469
13553
  var message = normalizeOptionalString(error === null || error === void 0 ? void 0 : error.message).toLowerCase();
13470
13554
  if (!message) {
@@ -14490,7 +14574,8 @@ function normalizeAssistantAggregatePipeline(pipeline, collection, originalMessa
14490
14574
  if (next.$setWindowFields && typeof next.$setWindowFields === 'object' && !Array.isArray(next.$setWindowFields)) {
14491
14575
  next.$setWindowFields = normalizeAssistantSetWindowFieldsRankSortBy(next.$setWindowFields);
14492
14576
  }
14493
- return normalizeAssistantNowExprPlaceholdersDeep(next);
14577
+ var nowNormalized = normalizeAssistantNowExprPlaceholdersDeep(next);
14578
+ return repairAssistantPositionalPathSegmentsInQuery(nowNormalized).query;
14494
14579
  });
14495
14580
  var stableSortPipeline = normalizeAssistantSortTieBreakersForLimit(normalizedPipeline);
14496
14581
  var recordLimitedPipeline = normalizeAssistantRecentGroupedLimitBeforeGroup(stableSortPipeline, originalMessage);
@@ -16441,7 +16526,8 @@ function normalizeMongoQuery(query) {
16441
16526
  }
16442
16527
  var rewritten = rewriteEmbeddedMatchObjects(normalized);
16443
16528
  var exprRewritten = rewriteMatchExpressionsToExpr(rewritten);
16444
- var nowNormalized = normalizeAssistantNowExprPlaceholdersDeep(exprRewritten);
16529
+ var positionalRepaired = repairAssistantPositionalPathSegmentsInQuery(exprRewritten).query;
16530
+ var nowNormalized = normalizeAssistantNowExprPlaceholdersDeep(positionalRepaired);
16445
16531
  return applyAssistantNameRegexToQuery(nowNormalized);
16446
16532
  }
16447
16533
  function shouldApplyAssistantNameRegex(field) {
@@ -17523,8 +17609,6 @@ function resolveAssistantConfiguredAppId() {
17523
17609
  var serverConfig = ((_a = resolveio_server_app_1.ResolveIOServer.getServerConfig) === null || _a === void 0 ? void 0 : _a.call(resolveio_server_app_1.ResolveIOServer)) || {};
17524
17610
  var configured = normalizeOptionalString(serverConfig['AI_ASSISTANT_APP_ID']
17525
17611
  || process.env.AI_ASSISTANT_APP_ID
17526
- || serverConfig['AI_CODER_APP_ID']
17527
- || process.env.AI_CODER_APP_ID
17528
17612
  || serverConfig['DATABASE']
17529
17613
  || process.env.DATABASE
17530
17614
  || serverConfig['CLIENT_NAME']
@@ -17927,6 +18011,19 @@ function resolveCollectionOverrideWithContext(params) {
17927
18011
  var allowVersions = shouldAllowVersionCollections(params.message);
17928
18012
  var collectionNames = Array.isArray(params.collectionNames) ? params.collectionNames : [];
17929
18013
  var permissionView = normalizeOptionalString(params.permissionView);
18014
+ var routeHintCollections = permissionView
18015
+ ? resolveCollectionHintsFromRoute(permissionView, collectionNames)
18016
+ : [];
18017
+ var isCollectionInHintFamilies = function (collection, hintCollections) {
18018
+ var candidateFamily = normalizeAssistantCollectionFamilyName(collection);
18019
+ if (!candidateFamily) {
18020
+ return false;
18021
+ }
18022
+ return (hintCollections || []).some(function (hintCollection) {
18023
+ var hintFamily = normalizeAssistantCollectionFamilyName(hintCollection);
18024
+ return !!(hintFamily && hintFamily === candidateFamily);
18025
+ });
18026
+ };
17930
18027
  if (permissionView) {
17931
18028
  var reportPreferred = resolveReportCollectionName(permissionView, collectionNames, requested);
17932
18029
  if (reportPreferred && reportPreferred !== requested) {
@@ -17953,6 +18050,11 @@ function resolveCollectionOverrideWithContext(params) {
17953
18050
  }
17954
18051
  var rankingOverride = resolveAssistantCollectionOverride(params.collectionRanking, requested);
17955
18052
  if (rankingOverride) {
18053
+ var requestedMatchesRouteHints = isCollectionInHintFamilies(requested, routeHintCollections);
18054
+ var overrideMatchesRouteHints = isCollectionInHintFamilies(rankingOverride.to, routeHintCollections);
18055
+ if (requestedMatchesRouteHints && !overrideMatchesRouteHints) {
18056
+ return null;
18057
+ }
17956
18058
  var rankingOverrideTarget = normalizeAssistantCollectionOverrideName(rankingOverride.to);
17957
18059
  var requestedIsWorkOrder = isAssistantWorkOrderCollectionOverrideCandidate(requested);
17958
18060
  if (rankingOverrideTarget === 'orders' && requestedIsWorkOrder && hasAssistantWorkOrderAliasTerms(params.message)) {
@@ -17974,6 +18076,9 @@ function resolveCollectionOverrideWithContext(params) {
17974
18076
  }
17975
18077
  return null;
17976
18078
  }
18079
+ function resolveCollectionOverrideWithContextForTesting(params) {
18080
+ return resolveCollectionOverrideWithContext(params);
18081
+ }
17977
18082
  function isAssistantWorkOrderCollectionOverrideCandidate(value) {
17978
18083
  var normalized = normalizeAssistantCollectionOverrideName(value);
17979
18084
  return normalized === 'work-order-dynamics'
@@ -19607,16 +19712,8 @@ function expandAggregateDateMatchFallback(pipeline, schemaFields) {
19607
19712
  if (!Array.isArray(pipeline)) {
19608
19713
  return null;
19609
19714
  }
19610
- var _loop_13 = function (i) {
19611
- var stage = pipeline[i];
19612
- if (!stage || typeof stage !== 'object' || !stage.$match || typeof stage.$match !== 'object') {
19613
- return "continue";
19614
- }
19615
- var matchStage = stage.$match;
19616
- var orArray = Array.isArray(matchStage.$or) ? matchStage.$or : null;
19617
- if (!orArray || !orArray.length) {
19618
- return "continue";
19619
- }
19715
+ var addedFields = new Set();
19716
+ var expandOrArray = function (orArray) {
19620
19717
  var existingFields = new Set();
19621
19718
  orArray.forEach(function (entry) {
19622
19719
  if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
@@ -19628,8 +19725,8 @@ function expandAggregateDateMatchFallback(pipeline, schemaFields) {
19628
19725
  }
19629
19726
  });
19630
19727
  });
19631
- var expandedOr = __spreadArray([], __read(orArray), false);
19632
- var addedFields = new Set();
19728
+ var changed = false;
19729
+ var expanded = __spreadArray([], __read(orArray), false);
19633
19730
  orArray.forEach(function (entry) {
19634
19731
  if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
19635
19732
  return;
@@ -19641,25 +19738,72 @@ function expandAggregateDateMatchFallback(pipeline, schemaFields) {
19641
19738
  if (existingFields.has(field)) {
19642
19739
  return;
19643
19740
  }
19644
- expandedOr = __spreadArray(__spreadArray([], __read(expandedOr), false), [replaceQueryField(entry, condition.field, field)], false);
19741
+ expanded = __spreadArray(__spreadArray([], __read(expanded), false), [replaceQueryField(entry, condition.field, field)], false);
19645
19742
  existingFields.add(field);
19646
19743
  addedFields.add(field);
19744
+ changed = true;
19647
19745
  });
19648
19746
  });
19649
19747
  });
19650
- if (!addedFields.size) {
19651
- return { value: null };
19748
+ return { value: changed ? expanded : orArray, changed: changed };
19749
+ };
19750
+ var visit = function (value) {
19751
+ if (Array.isArray(value)) {
19752
+ var changed_3 = false;
19753
+ var nextArray = value.map(function (entry) {
19754
+ var visited = visit(entry);
19755
+ if (visited.changed) {
19756
+ changed_3 = true;
19757
+ }
19758
+ return visited.value;
19759
+ });
19760
+ return { value: changed_3 ? nextArray : value, changed: changed_3 };
19652
19761
  }
19653
- var nextMatch = __assign(__assign({}, matchStage), { $or: expandedOr });
19654
- var updated = pipeline.map(function (current, index) { return (index === i ? { $match: nextMatch } : current); });
19655
- return { value: { pipeline: updated, fields: Array.from(addedFields) } };
19762
+ if (!value || typeof value !== 'object') {
19763
+ return { value: value, changed: false };
19764
+ }
19765
+ var changed = false;
19766
+ var next = __assign({}, value);
19767
+ Object.keys(value).forEach(function (key) {
19768
+ if (key === '$or' && Array.isArray(value.$or)) {
19769
+ var nested = visit(value.$or);
19770
+ var expanded = expandOrArray(nested.value);
19771
+ if (nested.changed || expanded.changed) {
19772
+ next.$or = expanded.value;
19773
+ changed = true;
19774
+ }
19775
+ return;
19776
+ }
19777
+ var visited = visit(value[key]);
19778
+ if (visited.changed) {
19779
+ next[key] = visited.value;
19780
+ changed = true;
19781
+ }
19782
+ });
19783
+ return { value: changed ? next : value, changed: changed };
19656
19784
  };
19657
- for (var i = 0; i < pipeline.length; i += 1) {
19658
- var state_9 = _loop_13(i);
19659
- if (typeof state_9 === "object")
19660
- return state_9.value;
19785
+ var pipelineChanged = false;
19786
+ var updated = pipeline.map(function (stage) {
19787
+ if (!stage || typeof stage !== 'object' || !stage.$match || typeof stage.$match !== 'object') {
19788
+ return stage;
19789
+ }
19790
+ var visited = visit(stage.$match);
19791
+ if (!visited.changed) {
19792
+ return stage;
19793
+ }
19794
+ pipelineChanged = true;
19795
+ return __assign(__assign({}, stage), { $match: visited.value });
19796
+ });
19797
+ if (!pipelineChanged || !addedFields.size) {
19798
+ return null;
19661
19799
  }
19662
- return null;
19800
+ return {
19801
+ pipeline: updated,
19802
+ fields: Array.from(addedFields)
19803
+ };
19804
+ }
19805
+ function expandAggregateDateMatchFallbackForTesting(pipeline, schemaFields) {
19806
+ return expandAggregateDateMatchFallback(pipeline, schemaFields);
19663
19807
  }
19664
19808
  function resolveQueryDateFieldFallback(query) {
19665
19809
  var dateField = findQueryDateField(query);
@@ -24442,7 +24586,7 @@ function resolveSingleWorkerTarget(value) {
24442
24586
  }
24443
24587
  function normalizeConversationMode(value) {
24444
24588
  var normalized = normalizeOptionalString(value).toLowerCase();
24445
- if (normalized === 'codex' || normalized === 'aicoder') {
24589
+ if (normalized === 'codex') {
24446
24590
  return 'codex';
24447
24591
  }
24448
24592
  if (normalized === 'openai') {