@resolveio/server-lib 22.3.79 → 22.3.80

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.
@@ -149,6 +149,7 @@ type AssistantMongoDirective = {
149
149
  payload: Record<string, any> | null;
150
150
  cleaned: string;
151
151
  rawLine: string;
152
+ metadata?: Record<string, any>;
152
153
  };
153
154
  type AssistantRequestType = 'data' | 'navigation' | 'feature_info' | 'bug_issue' | 'mixed' | 'unknown';
154
155
  type AssistantRequestTypeSource = 'heuristic' | 'planner' | 'merged';
@@ -1901,7 +1901,7 @@ function executeAiAssistantCodexRun(payload, context) {
1901
1901
  insertResult = _d.sent();
1902
1902
  assistantMessageId = (insertResult === null || insertResult === void 0 ? void 0 : insertResult._id) || (insertResult === null || insertResult === void 0 ? void 0 : insertResult.insertedId);
1903
1903
  enqueueAssistantCodexRun(function () { return __awaiter(_this, void 0, void 0, function () {
1904
- var runStart, fastModeEnabled, alwaysAllowProbeEnabled, plannerEnabled, runBudgetMs, assistantMongoMaxTimeMs, assistantSystemPrompt, runDeadlineMs, steps, recordStep, progressTracker, streamProgress, remainingRunBudgetMs, hasRunBudget, applyCodexStageBudget, assistantContent, toolResult, assistantDebug, directiveSource, requestClassification, dataQuestion, lastDirective, heuristicDirectivePrecomputed, usedDeterministicHeuristicFastPath, requestedTimeGrain, requestedBreakdownDimensions, enforceDatedDirective, enforceGroupedDirective, datedDirectiveRetryUsed, datedDirectiveResolved, toolResponseDebug, toolError, toolTelemetry, termHints, collectionHints, fieldHints, schemaHints, methodHints, publicationHints, collectionTokenization, collectionRanking, collectionSelection, collectionOverride, collectionNames, plannerUsed, plannerSkipReason, plannerOutput, plannerRaw, timingBreakdown, codexUsage, accumulateCodexUsage, contextRoute, contextMode, hintSeed, termExpansion, hintText, baseTokens, expandedTokens, baseWeights, expandedWeights, dbName, db, surfaceHints, _a, allowedRoutes, rankedAllowedRoutes, routeHints, appCollectionHints, rankedCollectionHints, rankedCollections, hintCollections, schemaHintCollections, assistantContext, hasDeterministicHeuristicFastPath, prompt_1, workspaceRoot, codexConfig, runOptions, plannerRunOptions, shouldRunPlanner, plannerBudgetAvailable, plannerPrompt, plannerStart, _b, preferListDirective, directiveStyleHint, directivePromptMode, responseText, directiveText, directive, heuristicDirectiveFastPath, directivePrompt, directiveStart, forcedDirective, _c, initialStart, extractedDirective, error_2, directivePrompt, forcedStart, forcedDirective, _d, strictDirectivePrompt, strictStart, strictDirectiveText, strictDirective, strictDirectiveIsDated, shouldUseStrictDirective, _e, guardDirectivePrompt, guardStart, guardDirectiveText, guardDirective, _f, groupedDirectivePrompt, groupedStart, groupedDirectiveText, groupedDirective, _g, heuristicDirective, requestedCollection, allowCollectionOverride, cleanedResponseText, deniedModuleByIntent, permissionLabel, effectiveDirective, rankedCollections_1, directiveHintsBase_1, buildToolRequestForDirective, executionDirective, probeFieldHints, aggregateProbeDisabled, probeEnabled, probeBudgetAllowed, probeDirective, probeRequest, probeStart, probeResponse, _h, probeDurationMs, probeRowCount, probeRewrite, probeError_1, probeDurationMs, probeErrorMessage, toolRequest, requestedToolCollection, toolStart, toolResponse, _j, toolDurationMs, toolPayload, resolvedToolCollection, zeroRowResult, followupCodexEnabled, skipFollowupCodex, followupPrompt, followupStart, followupText, _k, error_3, errorMessage, existingFinal, error_4, runBudgetExceeded, includeToolExecutionTelemetry, toolExecutionTelemetry, finishedAt, finalNow, finishedAt, codexMs, draftingMs, finalMetadata, finalUsage, usageClientId, usageError_1, finalAssistantDoc, setPayload, finalizeError_1, failedAt, fallbackContent, failureMetadata, persistError_1, _l;
1904
+ var runStart, fastModeEnabled, alwaysAllowProbeEnabled, plannerEnabled, runBudgetMs, assistantMongoMaxTimeMs, assistantSystemPrompt, runDeadlineMs, steps, recordStep, requestClassification, dataQuestion, progressTracker, streamProgress, remainingRunBudgetMs, hasRunBudget, applyCodexStageBudget, assistantContent, toolResult, assistantDebug, directiveSource, lastDirective, heuristicDirectivePrecomputed, usedDeterministicHeuristicFastPath, requestedTimeGrain, requestedBreakdownDimensions, enforceDatedDirective, enforceGroupedDirective, datedDirectiveRetryUsed, datedDirectiveResolved, toolResponseDebug, toolError, toolTelemetry, termHints, collectionHints, fieldHints, schemaHints, methodHints, publicationHints, collectionTokenization, collectionRanking, collectionSelection, collectionOverride, collectionNames, plannerUsed, plannerSkipReason, plannerOutput, plannerRaw, timingBreakdown, codexUsage, accumulateCodexUsage, contextRoute, contextMode, hintSeed, termExpansion, hintText, baseTokens, expandedTokens, baseWeights, expandedWeights, dbName, db, surfaceHints, _a, allowedRoutes, rankedAllowedRoutes, routeHints, appCollectionHints, rankedCollectionHints, rankedCollections, hintCollections, schemaHintCollections, assistantContext, hasDeterministicHeuristicFastPath, prompt_1, workspaceRoot, codexConfig, runOptions, plannerRunOptions, shouldRunPlanner, plannerBudgetAvailable, plannerPrompt, plannerStart, _b, preferListDirective, directiveStyleHint, directivePromptMode, responseText, directiveText, directive, heuristicDirectiveFastPath, directivePrompt, directiveStart, forcedDirective, _c, initialStart, extractedDirective, error_2, directivePrompt, forcedStart, forcedDirective, _d, strictDirectivePrompt, strictStart, strictDirectiveText, strictDirective, strictDirectiveIsDated, shouldUseStrictDirective, _e, guardDirectivePrompt, guardStart, guardDirectiveText, guardDirective, _f, groupedDirectivePrompt, groupedStart, groupedDirectiveText, groupedDirective, _g, heuristicDirective, requestedCollection, allowCollectionOverride, cleanedResponseText, deniedModuleByIntent, permissionLabel, effectiveDirective, effectiveDirectiveMetadata, directiveAcknowledgementText, directiveProgressMessages, deterministicDataIntentDirective, rankedCollections_1, directiveHintsBase_1, buildToolRequestForDirective, executionDirective, probeFieldHints, aggregateProbeDisabled, probeEnabled, probeBudgetAllowed, probeDirective, probeRequest, probeStart, probeResponse, _h, probeDurationMs, probeRowCount, probeRewrite, probeError_1, probeDurationMs, probeErrorMessage, toolRequest, requestedToolCollection, toolStart, toolResponse, _j, toolDurationMs, toolPayload, resolvedToolCollection, zeroRowResult, followupCodexEnabled, skipFollowupCodex, followupPrompt, followupStart, followupText, _k, error_3, errorMessage, existingFinal, error_4, runBudgetExceeded, includeToolExecutionTelemetry, toolExecutionTelemetry, finishedAt, finalNow, finishedAt, codexMs, draftingMs, finalMetadata, finalUsage, usageClientId, usageError_1, finalAssistantDoc, setPayload, finalizeError_1, failedAt, fallbackContent, failureMetadata, persistError_1, _l;
1905
1905
  var _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16, _17, _18, _19, _20, _21;
1906
1906
  return __generator(this, function (_22) {
1907
1907
  switch (_22.label) {
@@ -1941,8 +1941,10 @@ function executeAiAssistantCodexRun(payload, context) {
1941
1941
  details: details
1942
1942
  });
1943
1943
  };
1944
+ requestClassification = classifyAssistantRequestType(message);
1945
+ dataQuestion = requestClassification.dataQuestion;
1944
1946
  progressTracker = createAssistantProgressTracker(assistantMessageId, initialProgress);
1945
- streamProgress = createAssistantStreamProgressHandler(progressTracker);
1947
+ streamProgress = createAssistantStreamProgressHandler(progressTracker, function () { return dataQuestion; });
1946
1948
  remainingRunBudgetMs = function () {
1947
1949
  if (!runDeadlineMs) {
1948
1950
  return null;
@@ -1977,8 +1979,6 @@ function executeAiAssistantCodexRun(payload, context) {
1977
1979
  toolResult = null;
1978
1980
  assistantDebug = null;
1979
1981
  directiveSource = 'none';
1980
- requestClassification = classifyAssistantRequestType(message);
1981
- dataQuestion = requestClassification.dataQuestion;
1982
1982
  lastDirective = null;
1983
1983
  heuristicDirectivePrecomputed = null;
1984
1984
  usedDeterministicHeuristicFastPath = false;
@@ -2609,6 +2609,22 @@ function executeAiAssistantCodexRun(payload, context) {
2609
2609
  if (!((directive === null || directive === void 0 ? void 0 : directive.payload) && AI_ASSISTANT_TOOL_MAX_STEPS > 0)) return [3 /*break*/, 66];
2610
2610
  effectiveDirective = collectionOverride
2611
2611
  ? __assign(__assign({}, directive), { payload: __assign(__assign({}, (directive.payload || {})), { collection: collectionOverride.to }) }) : directive;
2612
+ effectiveDirectiveMetadata = effectiveDirective.metadata && typeof effectiveDirective.metadata === 'object'
2613
+ ? effectiveDirective.metadata
2614
+ : {};
2615
+ directiveAcknowledgementText = normalizeOptionalString(effectiveDirectiveMetadata.acknowledgementText);
2616
+ directiveProgressMessages = Array.isArray(effectiveDirectiveMetadata.progress)
2617
+ ? effectiveDirectiveMetadata.progress.map(function (entry) { return normalizeOptionalString(entry); }).filter(Boolean)
2618
+ : [];
2619
+ deterministicDataIntentDirective = isAssistantAppDataIntentHeuristicDirective(effectiveDirective);
2620
+ if (deterministicDataIntentDirective) {
2621
+ toolTelemetry.intentId = normalizeOptionalString(effectiveDirectiveMetadata.intentId) || undefined;
2622
+ toolTelemetry.recipeUsed = normalizeOptionalString(effectiveDirectiveMetadata.recipeUsed) || undefined;
2623
+ toolTelemetry.acknowledgementText = directiveAcknowledgementText || undefined;
2624
+ if (!collectionOverride) {
2625
+ toolTelemetry.overrideBlockedReason = 'deterministic_data_intent_preserved_collection';
2626
+ }
2627
+ }
2612
2628
  rankedCollections_1 = Array.isArray(collectionRanking === null || collectionRanking === void 0 ? void 0 : collectionRanking.ranked)
2613
2629
  ? collectionRanking.ranked
2614
2630
  .map(function (entry) { return normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.name); })
@@ -2628,9 +2644,18 @@ function executeAiAssistantCodexRun(payload, context) {
2628
2644
  deadlineMs: runDeadlineMs || undefined,
2629
2645
  maxTimeMS: assistantMongoMaxTimeMs
2630
2646
  };
2647
+ if (isAssistantAppDataIntentHeuristicDirective(value)) {
2648
+ request.__assistantRetryState = __assign(__assign({}, (request.__assistantRetryState || {})), { disableCrossCollectionRetry: true, rootCollection: normalizeOptionalString(request.collection) || undefined });
2649
+ }
2631
2650
  return request;
2632
2651
  };
2633
- progressTracker.push('Grabbing Data');
2652
+ if (directiveAcknowledgementText) {
2653
+ progressTracker.push(directiveAcknowledgementText);
2654
+ }
2655
+ else {
2656
+ progressTracker.push(deterministicDataIntentDirective ? 'Pulling the requested data' : 'Grabbing Data');
2657
+ }
2658
+ directiveProgressMessages.slice(0, 2).forEach(function (messageText) { return progressTracker.push(messageText); });
2634
2659
  executionDirective = effectiveDirective;
2635
2660
  probeFieldHints = [];
2636
2661
  _22.label = 39;
@@ -2648,7 +2673,9 @@ function executeAiAssistantCodexRun(payload, context) {
2648
2673
  };
2649
2674
  return [3 /*break*/, 63];
2650
2675
  case 40:
2651
- aggregateProbeDisabled = executionDirective.type === 'aggregate' && !alwaysAllowProbeEnabled;
2676
+ aggregateProbeDisabled = executionDirective.type === 'aggregate'
2677
+ && !alwaysAllowProbeEnabled
2678
+ && !isAssistantAppDataIntentHeuristicDirective(executionDirective);
2652
2679
  probeEnabled = AI_ASSISTANT_TOOL_MAX_STEPS > 1 && !aggregateProbeDisabled;
2653
2680
  probeBudgetAllowed = alwaysAllowProbeEnabled
2654
2681
  || hasRunBudget('Grabbing Data: probe', AI_ASSISTANT_MONGO_MIN_STAGE_BUDGET_MS);
@@ -6010,6 +6037,10 @@ function buildAssistantToolExecutionTelemetrySnapshot(telemetry) {
6010
6037
  mode: (telemetry === null || telemetry === void 0 ? void 0 : telemetry.mode) || 'single_step',
6011
6038
  always_allow_probe: (telemetry === null || telemetry === void 0 ? void 0 : telemetry.alwaysAllowProbe) === true,
6012
6039
  permission_denied: (telemetry === null || telemetry === void 0 ? void 0 : telemetry.permissionDenied) === true ? true : undefined,
6040
+ intentId: normalizeOptionalString(telemetry === null || telemetry === void 0 ? void 0 : telemetry.intentId) || undefined,
6041
+ acknowledgementText: normalizeOptionalString(telemetry === null || telemetry === void 0 ? void 0 : telemetry.acknowledgementText) || undefined,
6042
+ recipeUsed: normalizeOptionalString(telemetry === null || telemetry === void 0 ? void 0 : telemetry.recipeUsed) || undefined,
6043
+ overrideBlockedReason: normalizeOptionalString(telemetry === null || telemetry === void 0 ? void 0 : telemetry.overrideBlockedReason) || undefined,
6013
6044
  probe: probe ? {
6014
6045
  attempted: probe.attempted === true,
6015
6046
  succeeded: probe.succeeded === true ? true : undefined,
@@ -6650,15 +6681,253 @@ function isAssistantSchemaHoursHeuristicDirective(directive) {
6650
6681
  var rawLine = normalizeOptionalString(directive === null || directive === void 0 ? void 0 : directive.rawLine).toLowerCase();
6651
6682
  return rawLine.includes('heuristic_agg(schema-hours-user-time)');
6652
6683
  }
6684
+ function isAssistantAppDataIntentHeuristicDirective(directive) {
6685
+ var rawLine = normalizeOptionalString(directive === null || directive === void 0 ? void 0 : directive.rawLine).toLowerCase();
6686
+ return rawLine.includes('heuristic_agg(app-data-intent:') || rawLine.includes('heuristic_read(app-data-intent:');
6687
+ }
6688
+ function scoreAssistantAppDataIntent(messageText, intent) {
6689
+ if ((intent.excludeTerms || []).some(function (term) { return containsAssistantHeuristicTerm(messageText, term); })) {
6690
+ return 0;
6691
+ }
6692
+ return (intent.terms || []).reduce(function (score, term) {
6693
+ if (!containsAssistantHeuristicTerm(messageText, term)) {
6694
+ return score;
6695
+ }
6696
+ return score + (term.includes(' ') ? 4 : 1);
6697
+ }, 0);
6698
+ }
6699
+ function selectAssistantAppDataIntent(message, collectionNames, appId) {
6700
+ var text = normalizeOptionalString(message).toLowerCase();
6701
+ if (!text) {
6702
+ return null;
6703
+ }
6704
+ var availableNames = new Set((Array.isArray(collectionNames) ? collectionNames : []).map(function (value) { return normalizeOptionalString(value); }).filter(Boolean));
6705
+ var profile = resolveAssistantAppHeuristicProfile(appId);
6706
+ var selected = null;
6707
+ profile.dataIntents.forEach(function (intent) {
6708
+ if (!(intent === null || intent === void 0 ? void 0 : intent.id) || !intent.collection) {
6709
+ return;
6710
+ }
6711
+ if (availableNames.size && !availableNames.has(intent.collection)) {
6712
+ return;
6713
+ }
6714
+ var score = scoreAssistantAppDataIntent(text, intent);
6715
+ if (score <= 0) {
6716
+ return;
6717
+ }
6718
+ if (!selected || score > selected.score) {
6719
+ selected = { intent: intent, score: score };
6720
+ }
6721
+ });
6722
+ return selected ? selected.intent : null;
6723
+ }
6724
+ function extractAssistantDataIntentCustomerText(message) {
6725
+ var value = normalizeOptionalString(message);
6726
+ if (!value) {
6727
+ return '';
6728
+ }
6729
+ value = value.replace(/\b(summarize|summary|show|list|give|pull|find|tell|me|about|of|the|a|an|and|amount|quantity|qty|total|totals|for|to|them|their|by)\b/gi, ' ');
6730
+ value = value.replace(/\b(work\s*orders?|wos?|jobs?|invoices?|invoice|billed|billing|revenue|charged|chemicals?|chemical|sent|shipped|shipment|shipments|delivered|planned|inventory|on\s+hand|stock|this\s+year|ytd|year\s+to\s+date|current\s+year)\b/gi, ' ');
6731
+ value = value.replace(/\b(last|past|current|completed|reviewed|closed)\b/gi, ' ');
6732
+ value = value.replace(/[^a-z0-9&.' -]+/gi, ' ');
6733
+ return normalizeOptionalString(value.replace(/\s+/g, ' '));
6734
+ }
6735
+ function resolveAssistantDataIntentDateWindow(message) {
6736
+ var text = normalizeOptionalString(message).toLowerCase();
6737
+ if (!/\b(this\s+year|ytd|year\s+to\s+date|current\s+year)\b/.test(text)) {
6738
+ return {};
6739
+ }
6740
+ var now = new Date();
6741
+ var year = now.getUTCFullYear();
6742
+ return {
6743
+ start: new Date(Date.UTC(year, 0, 1, 0, 0, 0, 0)).toISOString(),
6744
+ end: new Date(Date.UTC(year, now.getUTCMonth(), now.getUTCDate(), 23, 59, 59, 999)).toISOString(),
6745
+ year: year
6746
+ };
6747
+ }
6748
+ function buildAssistantDataIntentAcknowledgement(intent, customerText, dateWindow) {
6749
+ var customer = customerText || 'the requested customer';
6750
+ var yearText = dateWindow.year ? " for ".concat(dateWindow.year) : '';
6751
+ if (intent.id === 'snf_job_chemical_shipments') {
6752
+ return "I understand you want a summary of chemicals sent to ".concat(customer, " jobs").concat(yearText, ". Let me pull the work-order shipment data for you.");
6753
+ }
6754
+ if (intent.id === 'snf_invoice_chemical_quantities') {
6755
+ return "I understand you want invoice and billed chemical details for ".concat(customer).concat(yearText, ". Let me pull the invoice data for you.");
6756
+ }
6757
+ if (intent.id === 'snf_planned_job_chemicals') {
6758
+ return "I understand you want planned chemicals for ".concat(customer, " jobs").concat(yearText, ". Let me pull the job setup data for you.");
6759
+ }
6760
+ return 'I understand the data you want. Let me pull the matching records for you.';
6761
+ }
6762
+ function buildAssistantDataIntentMatchStage(customerText, dateField, dateWindow, customerPaths) {
6763
+ var _a;
6764
+ var and = [];
6765
+ if (customerText) {
6766
+ and.push({
6767
+ $or: customerPaths.map(function (path) {
6768
+ var _a;
6769
+ return (_a = {},
6770
+ _a[path] = {
6771
+ $regex: customerText,
6772
+ $options: 'i'
6773
+ },
6774
+ _a);
6775
+ })
6776
+ });
6777
+ }
6778
+ if (dateField && dateWindow.start && dateWindow.end) {
6779
+ and.push((_a = {},
6780
+ _a[dateField] = {
6781
+ $gte: dateWindow.start,
6782
+ $lte: dateWindow.end
6783
+ },
6784
+ _a));
6785
+ }
6786
+ return and.length ? { $match: { $and: and } } : { $match: {} };
6787
+ }
6788
+ function buildAssistantAppDataIntentDirective(message, collectionNames, appId) {
6789
+ if (collectionNames === void 0) { collectionNames = []; }
6790
+ var intent = selectAssistantAppDataIntent(message, collectionNames, appId);
6791
+ if (!intent) {
6792
+ return null;
6793
+ }
6794
+ var customerText = extractAssistantDataIntentCustomerText(message);
6795
+ var dateWindow = resolveAssistantDataIntentDateWindow(message);
6796
+ var acknowledgementText = buildAssistantDataIntentAcknowledgement(intent, customerText, dateWindow);
6797
+ var progress = intent.progress.map(function (entry) {
6798
+ return entry
6799
+ .replace(/\{\{customer\}\}/g, customerText || 'the requested customer')
6800
+ .replace(/\{\{year\}\}/g, dateWindow.year ? String(dateWindow.year) : 'the requested date range');
6801
+ });
6802
+ var metadata = {
6803
+ intentId: intent.id,
6804
+ recipeUsed: intent.id,
6805
+ acknowledgementText: acknowledgementText,
6806
+ progress: progress,
6807
+ assumptions: intent.assumptions,
6808
+ fallbackCollections: intent.fallbackCollections
6809
+ };
6810
+ var basePayload = function (pipeline) { return ({
6811
+ collection: intent.collection,
6812
+ permissionView: intent.permissionView || resolveDefaultAssistantPermissionView(intent.collection),
6813
+ pipeline: pipeline,
6814
+ options: {
6815
+ allowDiskUse: true,
6816
+ limit: 20
6817
+ }
6818
+ }); };
6819
+ if (intent.id === 'snf_job_chemical_shipments') {
6820
+ var pipeline = [
6821
+ buildAssistantDataIntentMatchStage(customerText, intent.dateField || 'date_completed', dateWindow, ['customer', 'job.customer']),
6822
+ { $match: { status: { $in: ['Completed', 'Reviewed', 'Closed'] } } },
6823
+ { $unwind: '$chemicals' },
6824
+ { $match: { 'chemicals.shipped.quantity': { $gt: 0 } } },
6825
+ {
6826
+ $group: {
6827
+ _id: {
6828
+ id_job: '$job.id_job',
6829
+ job: '$job.job',
6830
+ chemical: '$chemicals.chemical',
6831
+ unit: '$chemicals.unit'
6832
+ },
6833
+ qty_sent: { $sum: '$chemicals.shipped.quantity' },
6834
+ work_order_ids: { $addToSet: '$_id' }
6835
+ }
6836
+ },
6837
+ {
6838
+ $project: {
6839
+ _id: 0,
6840
+ id_job: '$_id.id_job',
6841
+ job: '$_id.job',
6842
+ chemical: '$_id.chemical',
6843
+ unit: '$_id.unit',
6844
+ qty_sent: 1,
6845
+ work_order_count: { $size: '$work_order_ids' }
6846
+ }
6847
+ },
6848
+ { $sort: { qty_sent: -1, job: 1, chemical: 1 } }
6849
+ ];
6850
+ return { type: 'aggregate', payload: basePayload(pipeline), cleaned: '', rawLine: "HEURISTIC_AGG(app-data-intent:".concat(intent.id, ")"), metadata: metadata };
6851
+ }
6852
+ if (intent.id === 'snf_invoice_chemical_quantities') {
6853
+ var pipeline = [
6854
+ buildAssistantDataIntentMatchStage(customerText, intent.dateField || 'date_created', dateWindow, ['customer', 'job.customer']),
6855
+ { $unwind: '$items_chemicals' },
6856
+ {
6857
+ $group: {
6858
+ _id: {
6859
+ id_job: '$id_job',
6860
+ job: '$job',
6861
+ chemical: '$items_chemicals.chemical',
6862
+ unit: '$items_chemicals.unit'
6863
+ },
6864
+ quantity: { $sum: '$items_chemicals.quantity' },
6865
+ price_total: { $sum: '$items_chemicals.price_total' },
6866
+ invoice_ids: { $addToSet: '$_id' }
6867
+ }
6868
+ },
6869
+ {
6870
+ $project: {
6871
+ _id: 0,
6872
+ id_job: '$_id.id_job',
6873
+ job: '$_id.job',
6874
+ chemical: '$_id.chemical',
6875
+ unit: '$_id.unit',
6876
+ quantity: 1,
6877
+ price_total: 1,
6878
+ invoice_count: { $size: '$invoice_ids' }
6879
+ }
6880
+ },
6881
+ { $sort: { price_total: -1, quantity: -1, job: 1 } }
6882
+ ];
6883
+ return { type: 'aggregate', payload: basePayload(pipeline), cleaned: '', rawLine: "HEURISTIC_AGG(app-data-intent:".concat(intent.id, ")"), metadata: metadata };
6884
+ }
6885
+ if (intent.id === 'snf_planned_job_chemicals') {
6886
+ var pipeline = [
6887
+ buildAssistantDataIntentMatchStage(customerText, intent.dateField || 'date_job_created', dateWindow, ['customer', 'jca.customer']),
6888
+ { $unwind: '$planned_chemicals' },
6889
+ {
6890
+ $group: {
6891
+ _id: {
6892
+ id_job: '$_id',
6893
+ job: '$job',
6894
+ chemical: '$planned_chemicals.chemical',
6895
+ unit: '$planned_chemicals.unit'
6896
+ },
6897
+ planned_quantity: { $sum: '$planned_chemicals.quantity' }
6898
+ }
6899
+ },
6900
+ {
6901
+ $project: {
6902
+ _id: 0,
6903
+ id_job: '$_id.id_job',
6904
+ job: '$_id.job',
6905
+ chemical: '$_id.chemical',
6906
+ unit: '$_id.unit',
6907
+ planned_quantity: 1
6908
+ }
6909
+ },
6910
+ { $sort: { planned_quantity: -1, job: 1 } }
6911
+ ];
6912
+ return { type: 'aggregate', payload: basePayload(pipeline), cleaned: '', rawLine: "HEURISTIC_AGG(app-data-intent:".concat(intent.id, ")"), metadata: metadata };
6913
+ }
6914
+ return null;
6915
+ }
6653
6916
  function isAssistantDeterministicHeuristicDirective(directive) {
6654
- return isAssistantSchemaHoursHeuristicDirective(directive);
6917
+ return isAssistantSchemaHoursHeuristicDirective(directive)
6918
+ || isAssistantAppDataIntentHeuristicDirective(directive);
6655
6919
  }
6656
6920
  function buildAssistantHeuristicDirective(message, collectionHints, collectionNames, appId) {
6657
6921
  if (collectionNames === void 0) { collectionNames = []; }
6658
6922
  if (!normalizeOptionalString(message)) {
6659
6923
  return null;
6660
6924
  }
6661
- void collectionNames;
6925
+ var appDataIntentDirective = resolveAssistantDeterministicHeuristicEnabled(AI_ASSISTANT_HEURISTIC_ID_APP_DATA_INTENT, appId)
6926
+ ? buildAssistantAppDataIntentDirective(message, collectionNames, appId)
6927
+ : null;
6928
+ if (appDataIntentDirective) {
6929
+ return appDataIntentDirective;
6930
+ }
6662
6931
  var schemaHoursDirective = resolveAssistantDeterministicHeuristicEnabled(AI_ASSISTANT_HEURISTIC_ID_SCHEMA_HOURS_USER_TIME, appId)
6663
6932
  ? buildAssistantGenericHoursDirective(message, collectionHints, appId)
6664
6933
  : null;
@@ -7346,6 +7615,8 @@ function buildAssistantToolResultPayload(directive, toolResponse, requestMessage
7346
7615
  var verification = (toolResponse === null || toolResponse === void 0 ? void 0 : toolResponse.verification) && typeof toolResponse.verification === 'object'
7347
7616
  ? toolResponse.verification
7348
7617
  : undefined;
7618
+ var directiveMetadata = directive.metadata && typeof directive.metadata === 'object' ? directive.metadata : {};
7619
+ var mergedDebug = __assign(__assign(__assign(__assign(__assign({}, ((toolResponse === null || toolResponse === void 0 ? void 0 : toolResponse.debug) && typeof toolResponse.debug === 'object' ? toolResponse.debug : {})), (normalizeOptionalString(directiveMetadata.intentId) ? { intentId: normalizeOptionalString(directiveMetadata.intentId) } : {})), (normalizeOptionalString(directiveMetadata.acknowledgementText) ? { acknowledgementText: normalizeOptionalString(directiveMetadata.acknowledgementText) } : {})), (normalizeOptionalString(directiveMetadata.recipeUsed) ? { recipeUsed: normalizeOptionalString(directiveMetadata.recipeUsed) } : {})), (Array.isArray(directiveMetadata.assumptions) && directiveMetadata.assumptions.length ? { assumptions: directiveMetadata.assumptions } : {}));
7349
7620
  var result = {
7350
7621
  type: directive.type === 'aggregate' ? 'mongo_agg' : 'mongo_read',
7351
7622
  input: directivePayload,
@@ -7357,7 +7628,7 @@ function buildAssistantToolResultPayload(directive, toolResponse, requestMessage
7357
7628
  columns: responseDisplay.columns,
7358
7629
  truncated: responseDisplay.truncated,
7359
7630
  verification: verification,
7360
- debug: (toolResponse === null || toolResponse === void 0 ? void 0 : toolResponse.debug) && typeof toolResponse.debug === 'object' ? toolResponse.debug : undefined
7631
+ debug: Object.keys(mergedDebug).length ? mergedDebug : undefined
7361
7632
  }
7362
7633
  };
7363
7634
  return {
@@ -7541,9 +7812,13 @@ function buildAssistantToolFallbackResponse(result) {
7541
7812
  var _a, _b;
7542
7813
  var rowCount = Math.max(0, Number(result.output.rowCount || 0));
7543
7814
  var source = normalizeOptionalString(result.output.collection);
7815
+ var debug = result.output.debug && typeof result.output.debug === 'object' ? result.output.debug : {};
7816
+ var intentId = normalizeOptionalString(debug.intentId);
7544
7817
  var lines = rowCount > 0
7545
7818
  ? ["I found ".concat(rowCount === 1 ? '1 matching row' : "".concat(rowCount, " matching rows")).concat(source ? " from ".concat(source) : '', ".")]
7546
- : ["The query ran successfully".concat(source ? " against ".concat(source) : '', ", but no matching rows were returned.")];
7819
+ : [intentId
7820
+ ? "I checked the ".concat(source || 'selected', " data for that request, but I did not find matching rows for the filters I used.")
7821
+ : "I checked the data".concat(source ? " in ".concat(source) : '', ", but no matching rows were returned.")];
7547
7822
  if (source) {
7548
7823
  lines.push("- Source: ".concat(source));
7549
7824
  }
@@ -7551,6 +7826,22 @@ function buildAssistantToolFallbackResponse(result) {
7551
7826
  if (typeof result.output.total === 'number') {
7552
7827
  lines.push("- Total: ".concat(result.output.total));
7553
7828
  }
7829
+ if (rowCount <= 0) {
7830
+ var assumptions = Array.isArray(debug.assumptions)
7831
+ ? debug.assumptions.map(function (entry) { return normalizeOptionalString(entry); }).filter(Boolean)
7832
+ : [];
7833
+ if (assumptions.length) {
7834
+ lines.push("- Assumptions: ".concat(assumptions.slice(0, 3).join('; ')));
7835
+ }
7836
+ if (intentId === 'snf_job_chemical_shipments') {
7837
+ lines.push('- I treated "sent" as shipped chemical quantity from completed/reviewed/closed work orders.');
7838
+ lines.push('- If you meant billed quantities or dollars, ask for invoiced/billed chemicals instead.');
7839
+ }
7840
+ else if (intentId === 'snf_invoice_chemical_quantities') {
7841
+ lines.push('- I treated the request as billed invoice chemical quantities and invoice totals.');
7842
+ lines.push('- If you meant field shipments, ask for chemicals shipped/sent on work orders.');
7843
+ }
7844
+ }
7554
7845
  var leadSummary = buildAssistantDisplayLeadSummary(result.output.display);
7555
7846
  if (leadSummary) {
7556
7847
  lines.push('');
@@ -8096,11 +8387,29 @@ function deriveAssistantStreamStatus(event) {
8096
8387
  }
8097
8388
  return null;
8098
8389
  }
8099
- function createAssistantStreamProgressHandler(progressTracker) {
8390
+ function normalizeAssistantStreamProgressStatus(status, dataMode) {
8391
+ if (dataMode === void 0) { dataMode = false; }
8392
+ var trimmed = normalizeOptionalString(status);
8393
+ if (!trimmed || !dataMode) {
8394
+ return trimmed;
8395
+ }
8396
+ var normalized = trimmed.toLowerCase();
8397
+ if (normalized === 'planning') {
8398
+ return 'Understanding your request';
8399
+ }
8400
+ if (normalized === 'drafting response') {
8401
+ return 'Preparing the summary';
8402
+ }
8403
+ if (normalized === 'searching files' || normalized === 'finding files' || normalized === 'opening files' || normalized === 'scanning files') {
8404
+ return 'Checking available data';
8405
+ }
8406
+ return trimmed;
8407
+ }
8408
+ function createAssistantStreamProgressHandler(progressTracker, isDataMode) {
8100
8409
  var lastStatus = '';
8101
8410
  var lastSentAt = 0;
8102
8411
  return function (status) {
8103
- var trimmed = normalizeOptionalString(status);
8412
+ var trimmed = normalizeAssistantStreamProgressStatus(status, (isDataMode === null || isDataMode === void 0 ? void 0 : isDataMode()) === true);
8104
8413
  if (!trimmed) {
8105
8414
  return;
8106
8415
  }
@@ -15255,6 +15564,7 @@ function mergeAssistantHintValues() {
15255
15564
  return result;
15256
15565
  }
15257
15566
  var AI_ASSISTANT_HEURISTIC_ID_SCHEMA_HOURS_USER_TIME = 'schema_hours_user_time';
15567
+ var AI_ASSISTANT_HEURISTIC_ID_APP_DATA_INTENT = 'app_data_intent';
15258
15568
  var AI_ASSISTANT_DEFAULT_ENABLED_DETERMINISTIC_HEURISTICS = new Set();
15259
15569
  var aiAssistantAppHeuristicRegistryCache = null;
15260
15570
  var aiAssistantAppHeuristicRegistryWarningKey = '';
@@ -15263,6 +15573,7 @@ function createAssistantEmptyHeuristicProfile() {
15263
15573
  aliases: {},
15264
15574
  aliasOptions: {},
15265
15575
  collectionTermHints: [],
15576
+ dataIntents: [],
15266
15577
  enabledDeterministicHeuristics: new Set(),
15267
15578
  disabledDeterministicHeuristics: new Set()
15268
15579
  };
@@ -15281,6 +15592,21 @@ function cloneAssistantHeuristicProfile(profile) {
15281
15592
  collections: mergeAssistantHintValues((entry === null || entry === void 0 ? void 0 : entry.collections) || [])
15282
15593
  }); })
15283
15594
  : [],
15595
+ dataIntents: Array.isArray(profile === null || profile === void 0 ? void 0 : profile.dataIntents)
15596
+ ? profile.dataIntents.map(function (entry) { return ({
15597
+ id: normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.id),
15598
+ terms: mergeAssistantHintValues((entry === null || entry === void 0 ? void 0 : entry.terms) || []).map(function (term) { return term.toLowerCase(); }),
15599
+ excludeTerms: mergeAssistantHintValues((entry === null || entry === void 0 ? void 0 : entry.excludeTerms) || []).map(function (term) { return term.toLowerCase(); }),
15600
+ collection: normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.collection),
15601
+ permissionView: normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.permissionView) || undefined,
15602
+ dateField: normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.dateField) || undefined,
15603
+ quantityField: normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.quantityField) || undefined,
15604
+ metricField: normalizeOptionalString(entry === null || entry === void 0 ? void 0 : entry.metricField) || undefined,
15605
+ fallbackCollections: mergeAssistantHintValues((entry === null || entry === void 0 ? void 0 : entry.fallbackCollections) || []),
15606
+ progress: mergeAssistantHintValues((entry === null || entry === void 0 ? void 0 : entry.progress) || []),
15607
+ assumptions: mergeAssistantHintValues((entry === null || entry === void 0 ? void 0 : entry.assumptions) || [])
15608
+ }); }).filter(function (entry) { return !!entry.id && !!entry.collection; })
15609
+ : [],
15284
15610
  enabledDeterministicHeuristics: new Set(Array.from((profile === null || profile === void 0 ? void 0 : profile.enabledDeterministicHeuristics) || [])),
15285
15611
  disabledDeterministicHeuristics: new Set(Array.from((profile === null || profile === void 0 ? void 0 : profile.disabledDeterministicHeuristics) || [])),
15286
15612
  displayDateFormat: profile === null || profile === void 0 ? void 0 : profile.displayDateFormat
@@ -15308,6 +15634,7 @@ function mergeAssistantHeuristicProfiles(base, override) {
15308
15634
  merged.aliasOptions[normalizedKey] = values;
15309
15635
  });
15310
15636
  merged.collectionTermHints = __spreadArray(__spreadArray([], __read(merged.collectionTermHints), false), __read((Array.isArray(override.collectionTermHints) ? override.collectionTermHints : [])), false);
15637
+ merged.dataIntents = __spreadArray(__spreadArray([], __read(merged.dataIntents), false), __read((Array.isArray(override.dataIntents) ? override.dataIntents : [])), false);
15311
15638
  Array.from(override.enabledDeterministicHeuristics || []).forEach(function (value) { return merged.enabledDeterministicHeuristics.add(value); });
15312
15639
  Array.from(override.disabledDeterministicHeuristics || []).forEach(function (value) { return merged.disabledDeterministicHeuristics.add(value); });
15313
15640
  if (override.displayDateFormat) {
@@ -15390,6 +15717,35 @@ function normalizeAssistantCollectionTermHints(value) {
15390
15717
  });
15391
15718
  return hints;
15392
15719
  }
15720
+ function normalizeAssistantAppDataIntents(value) {
15721
+ var entries = Array.isArray(value) ? value : [];
15722
+ var intents = [];
15723
+ entries.forEach(function (entry) {
15724
+ if (!entry || typeof entry !== 'object' || Array.isArray(entry)) {
15725
+ return;
15726
+ }
15727
+ var id = normalizeOptionalString(entry.id || entry.intent_id || entry.intentId);
15728
+ var collection = normalizeOptionalString(entry.collection || entry.primary_collection || entry.primaryCollection);
15729
+ var terms = normalizeAssistantHeuristicStringList(entry.terms || entry.trigger_terms || entry.triggerTerms || entry.phrases || []).map(function (term) { return term.toLowerCase(); });
15730
+ if (!id || !collection || !terms.length) {
15731
+ return;
15732
+ }
15733
+ intents.push({
15734
+ id: id,
15735
+ terms: terms,
15736
+ excludeTerms: normalizeAssistantHeuristicStringList(entry.exclude_terms || entry.excludeTerms || []).map(function (term) { return term.toLowerCase(); }),
15737
+ collection: collection,
15738
+ permissionView: normalizeOptionalString(entry.permission_view || entry.permissionView) || undefined,
15739
+ dateField: normalizeOptionalString(entry.date_field || entry.dateField) || undefined,
15740
+ quantityField: normalizeOptionalString(entry.quantity_field || entry.quantityField || entry.metric_field || entry.metricField) || undefined,
15741
+ metricField: normalizeOptionalString(entry.metric_field || entry.metricField) || undefined,
15742
+ fallbackCollections: normalizeAssistantHeuristicStringList(entry.fallback_collections || entry.fallbackCollections || []),
15743
+ progress: mergeAssistantHintValues(Array.isArray(entry.progress) ? entry.progress : []),
15744
+ assumptions: mergeAssistantHintValues(Array.isArray(entry.assumptions) ? entry.assumptions : [])
15745
+ });
15746
+ });
15747
+ return intents;
15748
+ }
15393
15749
  function normalizeAssistantAppHeuristicProfile(value) {
15394
15750
  var profile = createAssistantEmptyHeuristicProfile();
15395
15751
  if (!value || typeof value !== 'object' || Array.isArray(value)) {
@@ -15412,6 +15768,9 @@ function normalizeAssistantAppHeuristicProfile(value) {
15412
15768
  || value.collectionTermHints
15413
15769
  || value.term_hints
15414
15770
  || value.termHints);
15771
+ profile.dataIntents = normalizeAssistantAppDataIntents(value.data_intents
15772
+ || value.dataIntents
15773
+ || value.intents);
15415
15774
  profile.displayDateFormat = normalizeAssistantDisplayDateFormat(value.display_date_format
15416
15775
  || value.displayDateFormat
15417
15776
  || value.date_display_format
@@ -15458,6 +15817,7 @@ function normalizeAssistantAppHeuristicRegistry(value) {
15458
15817
  if (Object.keys(standaloneProfile.aliases).length
15459
15818
  || Object.keys(standaloneProfile.aliasOptions).length
15460
15819
  || standaloneProfile.collectionTermHints.length
15820
+ || standaloneProfile.dataIntents.length
15461
15821
  || standaloneProfile.enabledDeterministicHeuristics.size
15462
15822
  || standaloneProfile.disabledDeterministicHeuristics.size
15463
15823
  || normalizeOptionalString(standaloneProfile.appId)) {
@@ -15893,6 +16253,9 @@ function resolveCollectionOverrideWithContext(params) {
15893
16253
  && !isAssistantCollectionInHintFamilies(requested, [routePreferred])) {
15894
16254
  var routePreferredName = normalizeAssistantCollectionOverrideName(routePreferred);
15895
16255
  var requestedIsWorkOrder = isAssistantWorkOrderCollectionOverrideCandidate(requested);
16256
+ if (shouldPreserveAssistantJobDomainCollectionForRoute(params.message, permissionView, routePreferredName, requested)) {
16257
+ return null;
16258
+ }
15896
16259
  if (!(routePreferredName === 'orders' && requestedIsWorkOrder && hasAssistantWorkOrderAliasTerms(params.message))) {
15897
16260
  return {
15898
16261
  from: requested,
@@ -15992,6 +16355,27 @@ function resolveCollectionOverrideWithContext(params) {
15992
16355
  function resolveCollectionOverrideWithContextForTesting(params) {
15993
16356
  return resolveCollectionOverrideWithContext(params);
15994
16357
  }
16358
+ function shouldPreserveAssistantJobDomainCollectionForRoute(message, permissionView, routePreferredName, requestedCollection) {
16359
+ var route = normalizeOptionalString(permissionView).toLowerCase();
16360
+ var requested = normalizeAssistantCollectionOverrideName(requestedCollection);
16361
+ if (!route || routePreferredName !== 'jobs') {
16362
+ return false;
16363
+ }
16364
+ if (!/\/(?:customer-info\/jobs|job\/list|jobs?)(?:\/|$)/.test(route)) {
16365
+ return false;
16366
+ }
16367
+ var text = normalizeOptionalString(message).toLowerCase();
16368
+ if (!text || !/\b(job|jobs|work\s*orders?|wos?)\b/.test(text)) {
16369
+ return false;
16370
+ }
16371
+ if (requested === 'work-order-dynamics') {
16372
+ return /\b(chemical|chemicals|sent|shipped|shipment|delivered|work\s*orders?|wos?)\b/.test(text);
16373
+ }
16374
+ if (requested === 'invoices') {
16375
+ return /\b(invoice|invoices|billed|billing|revenue|charged|dollars?)\b/.test(text);
16376
+ }
16377
+ return requested === 'jobs';
16378
+ }
15995
16379
  function isAssistantWorkOrderCollectionOverrideCandidate(value) {
15996
16380
  var normalized = normalizeAssistantCollectionOverrideName(value);
15997
16381
  return normalized === 'work-order-dynamics'