@resolveio/server-lib 22.1.22 → 22.1.24

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.
@@ -1519,11 +1519,11 @@ function executeAiFormPatch(payload, context) {
1519
1519
  }
1520
1520
  function executeAiAssistantCodexRun(payload, context) {
1521
1521
  return __awaiter(this, void 0, void 0, function () {
1522
- var input, message, aiWorkerDebug, requestId, codexModel, codexFallbackModels, guardrail, conversation_3, now_3, userMsg, assistantMsg, user, isSuperAdmin, canViewDebug, hasInvoiceAccess, customerId, conversation, now, attachments, navigationFastPath, userMsg, assistantMsg, assistantInsert, assistantMessageId_1, changeHistoryFastPath, changeHistoryFastPathBypassedReason, userMsg, assistantMsg, assistantInsert, assistantMessageId_2, attachmentData, historyLimit, history, _a, historyLines, recentToolError, userDoc, initialProgress, assistantDoc, insertResult, assistantMessageId;
1522
+ var input, message, aiWorkerDebug, requestId, codexModel, codexFallbackModels, guardrail, conversation_3, now_3, userMsg, assistantMsg, user, isSuperAdmin, canViewDebug, hasInvoiceAccess, customerId, conversation, now, attachments, navigationFastPath, userMsg, assistantMsg, assistantInsert, assistantMessageId_1, changeHistoryFastPath, changeHistoryFastPathBypassedReason, userMsg, assistantMsg, assistantInsert, assistantMessageId_2, attachmentData, historyLines, recentToolError, userDoc, initialProgress, assistantDoc, insertResult, assistantMessageId;
1523
1523
  var _this = this;
1524
- var _b, _c, _d;
1525
- return __generator(this, function (_e) {
1526
- switch (_e.label) {
1524
+ var _a, _b, _c;
1525
+ return __generator(this, function (_d) {
1526
+ switch (_d.label) {
1527
1527
  case 0:
1528
1528
  input = payload || {};
1529
1529
  message = normalizeOptionalString(input.message);
@@ -1541,7 +1541,7 @@ function executeAiAssistantCodexRun(payload, context) {
1541
1541
  if (!(guardrail === null || guardrail === void 0 ? void 0 : guardrail.blocked)) return [3 /*break*/, 5];
1542
1542
  return [4 /*yield*/, ensureConversation(input, 'codex', context === null || context === void 0 ? void 0 : context.id_user)];
1543
1543
  case 1:
1544
- conversation_3 = _e.sent();
1544
+ conversation_3 = _d.sent();
1545
1545
  now_3 = new Date();
1546
1546
  userMsg = {
1547
1547
  id_conversation: conversation_3._id,
@@ -1561,13 +1561,13 @@ function executeAiAssistantCodexRun(payload, context) {
1561
1561
  };
1562
1562
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(userMsg)];
1563
1563
  case 2:
1564
- _e.sent();
1564
+ _d.sent();
1565
1565
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(assistantMsg)];
1566
1566
  case 3:
1567
- _e.sent();
1567
+ _d.sent();
1568
1568
  return [4 /*yield*/, touchConversation(conversation_3._id, now_3)];
1569
1569
  case 4:
1570
- _e.sent();
1570
+ _d.sent();
1571
1571
  return [2 /*return*/, {
1572
1572
  conversation: conversation_3,
1573
1573
  message: assistantMsg,
@@ -1575,14 +1575,14 @@ function executeAiAssistantCodexRun(payload, context) {
1575
1575
  }];
1576
1576
  case 5: return [4 /*yield*/, user_collection_1.Users.findById(context === null || context === void 0 ? void 0 : context.id_user)];
1577
1577
  case 6:
1578
- user = _e.sent();
1579
- isSuperAdmin = !!((_b = user === null || user === void 0 ? void 0 : user.roles) === null || _b === void 0 ? void 0 : _b.super_admin);
1578
+ user = _d.sent();
1579
+ isSuperAdmin = !!((_a = user === null || user === void 0 ? void 0 : user.roles) === null || _a === void 0 ? void 0 : _a.super_admin);
1580
1580
  canViewDebug = isAssistantDebugUser(user);
1581
1581
  hasInvoiceAccess = userHasInvoiceAccess(user);
1582
- customerId = normalizeOptionalString((_c = user === null || user === void 0 ? void 0 : user.other) === null || _c === void 0 ? void 0 : _c.id_customer);
1582
+ customerId = normalizeOptionalString((_b = user === null || user === void 0 ? void 0 : user.other) === null || _b === void 0 ? void 0 : _b.id_customer);
1583
1583
  return [4 /*yield*/, ensureConversation(input, 'codex', context === null || context === void 0 ? void 0 : context.id_user)];
1584
1584
  case 7:
1585
- conversation = _e.sent();
1585
+ conversation = _d.sent();
1586
1586
  if (aiWorkerDebug) {
1587
1587
  console.log(new Date(), '[AI Worker Debug] codex run queued', {
1588
1588
  requestId: requestId || null,
@@ -1600,7 +1600,7 @@ function executeAiAssistantCodexRun(payload, context) {
1600
1600
  hasAttachments: attachments.length > 0,
1601
1601
  user: user,
1602
1602
  isSuperAdmin: isSuperAdmin,
1603
- contextRoute: normalizeOptionalString((_d = input === null || input === void 0 ? void 0 : input.context) === null || _d === void 0 ? void 0 : _d.route)
1603
+ contextRoute: normalizeOptionalString((_c = input === null || input === void 0 ? void 0 : input.context) === null || _c === void 0 ? void 0 : _c.route)
1604
1604
  });
1605
1605
  if (!navigationFastPath) return [3 /*break*/, 11];
1606
1606
  userMsg = {
@@ -1622,14 +1622,14 @@ function executeAiAssistantCodexRun(payload, context) {
1622
1622
  };
1623
1623
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(userMsg)];
1624
1624
  case 8:
1625
- _e.sent();
1625
+ _d.sent();
1626
1626
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(assistantMsg)];
1627
1627
  case 9:
1628
- assistantInsert = _e.sent();
1628
+ assistantInsert = _d.sent();
1629
1629
  assistantMessageId_1 = (assistantInsert === null || assistantInsert === void 0 ? void 0 : assistantInsert._id) || (assistantInsert === null || assistantInsert === void 0 ? void 0 : assistantInsert.insertedId);
1630
1630
  return [4 /*yield*/, touchConversation(conversation._id, now, assistantMessageId_1 ? String(assistantMessageId_1) : undefined)];
1631
1631
  case 10:
1632
- _e.sent();
1632
+ _d.sent();
1633
1633
  return [2 /*return*/, {
1634
1634
  conversation: conversation,
1635
1635
  message: assistantMsg,
@@ -1645,7 +1645,7 @@ function executeAiAssistantCodexRun(payload, context) {
1645
1645
  isSuperAdmin: isSuperAdmin
1646
1646
  })];
1647
1647
  case 12:
1648
- changeHistoryFastPath = _e.sent();
1648
+ changeHistoryFastPath = _d.sent();
1649
1649
  changeHistoryFastPathBypassedReason = (changeHistoryFastPath
1650
1650
  && (changeHistoryFastPath.reason === 'no_repo' || changeHistoryFastPath.reason === 'error')
1651
1651
  && !shouldReturnAssistantChangeHistoryUnavailableFastPath())
@@ -1671,14 +1671,14 @@ function executeAiAssistantCodexRun(payload, context) {
1671
1671
  };
1672
1672
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(userMsg)];
1673
1673
  case 13:
1674
- _e.sent();
1674
+ _d.sent();
1675
1675
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(assistantMsg)];
1676
1676
  case 14:
1677
- assistantInsert = _e.sent();
1677
+ assistantInsert = _d.sent();
1678
1678
  assistantMessageId_2 = (assistantInsert === null || assistantInsert === void 0 ? void 0 : assistantInsert._id) || (assistantInsert === null || assistantInsert === void 0 ? void 0 : assistantInsert.insertedId);
1679
1679
  return [4 /*yield*/, touchConversation(conversation._id, now, assistantMessageId_2 ? String(assistantMessageId_2) : undefined)];
1680
1680
  case 15:
1681
- _e.sent();
1681
+ _d.sent();
1682
1682
  return [2 /*return*/, {
1683
1683
  conversation: conversation,
1684
1684
  message: assistantMsg,
@@ -1689,29 +1689,9 @@ function executeAiAssistantCodexRun(payload, context) {
1689
1689
  }];
1690
1690
  case 16: return [4 /*yield*/, readAttachmentContents(attachments)];
1691
1691
  case 17:
1692
- attachmentData = _e.sent();
1693
- historyLimit = normalizeHistoryLimit(input.max_history);
1694
- if (!(historyLimit > 0)) return [3 /*break*/, 19];
1695
- return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.find({ id_conversation: conversation._id, role: { $in: ['user', 'assistant'] } }, { sort: { createdAt: 1 }, limit: historyLimit * 2 })];
1696
- case 18:
1697
- _a = _e.sent();
1698
- return [3 /*break*/, 20];
1699
- case 19:
1700
- _a = [];
1701
- _e.label = 20;
1702
- case 20:
1703
- history = _a;
1692
+ attachmentData = _d.sent();
1704
1693
  historyLines = [];
1705
- history.forEach(function (entry) {
1706
- var role = entry.role === 'assistant' ? 'Assistant' : 'User';
1707
- var content = normalizeOptionalString(entry.content);
1708
- if (content) {
1709
- historyLines.push("".concat(role, ": ").concat(content));
1710
- }
1711
- });
1712
- recentToolError = isAssistantWhyFollowupMessage(message)
1713
- ? resolveRecentAssistantToolError(history)
1714
- : '';
1694
+ recentToolError = '';
1715
1695
  userDoc = {
1716
1696
  id_conversation: conversation._id,
1717
1697
  role: 'user',
@@ -1731,14 +1711,14 @@ function executeAiAssistantCodexRun(payload, context) {
1731
1711
  updatedAt: now
1732
1712
  };
1733
1713
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(userDoc)];
1734
- case 21:
1735
- _e.sent();
1714
+ case 18:
1715
+ _d.sent();
1736
1716
  return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.insertOne(assistantDoc)];
1737
- case 22:
1738
- insertResult = _e.sent();
1717
+ case 19:
1718
+ insertResult = _d.sent();
1739
1719
  assistantMessageId = (insertResult === null || insertResult === void 0 ? void 0 : insertResult._id) || (insertResult === null || insertResult === void 0 ? void 0 : insertResult.insertedId);
1740
1720
  enqueueAssistantCodexRun(function () { return __awaiter(_this, void 0, void 0, function () {
1741
- var runStart, steps, recordStep, progressTracker, streamProgress, assistantContent, toolResult, assistantDebug, directiveSource, requestClassification, dataQuestion, lastDirective, heuristicDirectivePrecomputed, usedDeterministicHeuristicFastPath, requestedTimeGrain, requestedBreakdownDimensions, enforceDatedDirective, enforceGroupedDirective, datedDirectiveRetryUsed, datedDirectiveResolved, toolResponseDebug, toolError, termHints, collectionHints, fieldHints, methodHints, publicationHints, collectionTokenization, collectionRanking, collectionSelection, collectionOverride, collectionNames, plannerEnabled, plannerUsed, plannerSkipReason, plannerOutput, plannerRaw, timingBreakdown, codexUsage, accumulateCodexUsage, contextRoute, contextMode, hintSeed, termExpansion, hintText, baseTokens, expandedTokens, baseWeights, expandedWeights, dbName, db, surfaceHints, _a, allowedRoutes, rankedAllowedRoutes, routeHints, rankedCollectionHints, rankedCollections, hintCollections, assistantContext, hasDeterministicHeuristicFastPath, prompt_1, workspaceRoot, codexConfig, runOptions, plannerRunOptions, shouldRunPlanner, 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, effectiveDirective, toolRequest, toolStart, toolResponse, _h, toolPayload, skipFollowupCodex, followupPrompt, followupStart, followupText, _j, error_3, error_4, finishedAt, finalNow, finishedAt, codexMs, draftingMs, finalMetadata, finalUsage, usageClientId, usageError_1, finalAssistantDoc, setPayload;
1721
+ var runStart, steps, recordStep, progressTracker, streamProgress, assistantContent, toolResult, assistantDebug, directiveSource, requestClassification, dataQuestion, lastDirective, heuristicDirectivePrecomputed, usedDeterministicHeuristicFastPath, requestedTimeGrain, requestedBreakdownDimensions, enforceDatedDirective, enforceGroupedDirective, datedDirectiveRetryUsed, datedDirectiveResolved, toolResponseDebug, toolError, termHints, collectionHints, fieldHints, methodHints, publicationHints, collectionTokenization, collectionRanking, collectionSelection, collectionOverride, collectionNames, plannerEnabled, plannerUsed, plannerSkipReason, plannerOutput, plannerRaw, timingBreakdown, codexUsage, accumulateCodexUsage, contextRoute, contextMode, hintSeed, termExpansion, hintText, baseTokens, expandedTokens, baseWeights, expandedWeights, dbName, db, surfaceHints, _a, allowedRoutes, rankedAllowedRoutes, routeHints, rankedCollectionHints, rankedCollections, hintCollections, assistantContext, hasDeterministicHeuristicFastPath, prompt_1, workspaceRoot, codexConfig, runOptions, plannerRunOptions, shouldRunPlanner, 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, denyInvoiceDataByIntent, effectiveDirective, toolRequest, toolStart, toolResponse, _h, toolPayload, skipFollowupCodex, followupPrompt, followupStart, followupText, _j, error_3, error_4, finishedAt, finalNow, finishedAt, codexMs, draftingMs, finalMetadata, finalUsage, usageClientId, usageError_1, finalAssistantDoc, setPayload;
1742
1722
  var _k, _l, _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;
1743
1723
  return __generator(this, function (_13) {
1744
1724
  switch (_13.label) {
@@ -1836,7 +1816,7 @@ function executeAiAssistantCodexRun(payload, context) {
1836
1816
  });
1837
1817
  _13.label = 1;
1838
1818
  case 1:
1839
- _13.trys.push([1, 52, 53, 54]);
1819
+ _13.trys.push([1, 53, 54, 55]);
1840
1820
  hintSeed = [message, contextRoute].filter(Boolean).join(' ');
1841
1821
  termExpansion = expandAssistantTermSynonyms(hintSeed);
1842
1822
  hintText = termExpansion.expanded || hintSeed;
@@ -1955,6 +1935,8 @@ function executeAiAssistantCodexRun(payload, context) {
1955
1935
  runOptions = {
1956
1936
  timeoutMs: resolveCodexTimeoutMs(),
1957
1937
  fallbackModels: codexFallbackModels,
1938
+ threadKey: conversation._id,
1939
+ reuseThread: true,
1958
1940
  threadOptions: {
1959
1941
  model: codexModel,
1960
1942
  workingDirectory: workspaceRoot,
@@ -1971,7 +1953,7 @@ function executeAiAssistantCodexRun(payload, context) {
1971
1953
  approvalPolicy: 'never'
1972
1954
  }
1973
1955
  };
1974
- plannerRunOptions = __assign(__assign({}, runOptions), { threadOptions: __assign(__assign({}, (runOptions.threadOptions || {})), { modelReasoningEffort: 'minimal' }) });
1956
+ plannerRunOptions = __assign(__assign({}, runOptions), { threadKey: "".concat(conversation._id, ":planner"), threadOptions: __assign(__assign({}, (runOptions.threadOptions || {})), { modelReasoningEffort: 'minimal' }) });
1975
1957
  shouldRunPlanner = shouldRunAssistantPlanner({
1976
1958
  plannerEnabled: plannerEnabled,
1977
1959
  requestClassification: requestClassification,
@@ -2347,7 +2329,20 @@ function executeAiAssistantCodexRun(payload, context) {
2347
2329
  if (cleanedResponseText) {
2348
2330
  assistantContent = sanitizeAssistantResponse(cleanedResponseText);
2349
2331
  }
2350
- if (!((directive === null || directive === void 0 ? void 0 : directive.payload) && AI_ASSISTANT_TOOL_MAX_STEPS > 0)) return [3 /*break*/, 50];
2332
+ denyInvoiceDataByIntent = dataQuestion
2333
+ && !isSuperAdmin
2334
+ && !hasInvoiceAccess
2335
+ && assistantMessageRequestsInvoiceData(message);
2336
+ if (!denyInvoiceDataByIntent) return [3 /*break*/, 38];
2337
+ progressTracker.push('Grabbing Data');
2338
+ recordStep('Grabbing Data: denied', {
2339
+ reason: 'invoice_permission_required'
2340
+ });
2341
+ assistantContent = 'I couldn\'t run the data query because this account does not have permission for that dataset.';
2342
+ toolError = new Error('AI assistant report builder bridge: Access denied.');
2343
+ return [3 /*break*/, 52];
2344
+ case 38:
2345
+ if (!((directive === null || directive === void 0 ? void 0 : directive.payload) && AI_ASSISTANT_TOOL_MAX_STEPS > 0)) return [3 /*break*/, 51];
2351
2346
  effectiveDirective = collectionOverride
2352
2347
  ? __assign(__assign({}, directive), { payload: __assign(__assign({}, (directive.payload || {})), { collection: collectionOverride.to }) }) : directive;
2353
2348
  toolRequest = buildAssistantToolRequest(effectiveDirective, input);
@@ -2357,20 +2352,20 @@ function executeAiAssistantCodexRun(payload, context) {
2357
2352
  collection: normalizeOptionalString(toolRequest === null || toolRequest === void 0 ? void 0 : toolRequest.collection) || undefined,
2358
2353
  permissionView: normalizeOptionalString(toolRequest === null || toolRequest === void 0 ? void 0 : toolRequest.permissionView) || undefined
2359
2354
  });
2360
- _13.label = 38;
2361
- case 38:
2362
- _13.trys.push([38, 48, , 49]);
2355
+ _13.label = 39;
2356
+ case 39:
2357
+ _13.trys.push([39, 49, , 50]);
2363
2358
  toolStart = Date.now();
2364
- if (!(effectiveDirective.type === 'aggregate')) return [3 /*break*/, 40];
2359
+ if (!(effectiveDirective.type === 'aggregate')) return [3 /*break*/, 41];
2365
2360
  return [4 /*yield*/, executeAiAssistantReportBuilderAggregate(toolRequest, context)];
2366
- case 39:
2361
+ case 40:
2367
2362
  _h = _13.sent();
2368
- return [3 /*break*/, 42];
2369
- case 40: return [4 /*yield*/, executeAiAssistantReportBuilderRead(toolRequest, context)];
2370
- case 41:
2371
- _h = _13.sent();
2372
- _13.label = 42;
2363
+ return [3 /*break*/, 43];
2364
+ case 41: return [4 /*yield*/, executeAiAssistantReportBuilderRead(toolRequest, context)];
2373
2365
  case 42:
2366
+ _h = _13.sent();
2367
+ _13.label = 43;
2368
+ case 43:
2374
2369
  toolResponse = _h;
2375
2370
  timingBreakdown.toolMs = Date.now() - toolStart;
2376
2371
  toolResponseDebug = (toolResponse === null || toolResponse === void 0 ? void 0 : toolResponse.debug) && typeof toolResponse.debug === 'object'
@@ -2386,54 +2381,54 @@ function executeAiAssistantCodexRun(payload, context) {
2386
2381
  progressTracker.push('Drafting response');
2387
2382
  skipFollowupCodex = usedDeterministicHeuristicFastPath
2388
2383
  || isAssistantDeterministicHeuristicDirective(effectiveDirective);
2389
- if (!skipFollowupCodex) return [3 /*break*/, 43];
2384
+ if (!skipFollowupCodex) return [3 /*break*/, 44];
2390
2385
  recordStep('Drafting response: deterministic summary', {
2391
2386
  reason: normalizeOptionalString(effectiveDirective.rawLine) || 'deterministic_heuristic'
2392
2387
  });
2393
2388
  assistantContent = buildAssistantToolFallbackResponse(toolPayload.result);
2394
2389
  assistantContent = applyAssistantDisplayTableToResponse(assistantContent, toolPayload.result.output.display);
2395
- return [3 /*break*/, 47];
2396
- case 43:
2390
+ return [3 /*break*/, 48];
2391
+ case 44:
2397
2392
  recordStep('Drafting response');
2398
2393
  followupPrompt = buildAssistantCodexToolFollowupPrompt(message, attachmentData.promptText, historyLines.join('\n'), assistantContext, toolPayload.prompt);
2399
- _13.label = 44;
2400
- case 44:
2401
- _13.trys.push([44, 46, , 47]);
2394
+ _13.label = 45;
2395
+ case 45:
2396
+ _13.trys.push([45, 47, , 48]);
2402
2397
  followupStart = Date.now();
2403
2398
  return [4 /*yield*/, runCodexInWorkerThread(followupPrompt, runOptions, codexConfig, streamProgress)];
2404
- case 45:
2399
+ case 46:
2405
2400
  followupText = _13.sent();
2406
2401
  accumulateCodexUsage(followupPrompt, followupText);
2407
2402
  timingBreakdown.followupMs = Date.now() - followupStart;
2408
2403
  assistantContent = sanitizeAssistantResponse(followupText);
2409
2404
  assistantContent = applyAssistantDisplayTableToResponse(assistantContent, toolPayload.result.output.display);
2410
- return [3 /*break*/, 47];
2411
- case 46:
2405
+ return [3 /*break*/, 48];
2406
+ case 47:
2412
2407
  _j = _13.sent();
2413
2408
  assistantContent = buildAssistantToolFallbackResponse(toolPayload.result);
2414
2409
  assistantContent = applyAssistantDisplayTableToResponse(assistantContent, toolPayload.result.output.display);
2415
- return [3 /*break*/, 47];
2416
- case 47: return [3 /*break*/, 49];
2417
- case 48:
2410
+ return [3 /*break*/, 48];
2411
+ case 48: return [3 /*break*/, 50];
2412
+ case 49:
2418
2413
  error_3 = _13.sent();
2419
2414
  assistantContent = buildAssistantToolErrorMessage(error_3);
2420
2415
  toolError = error_3;
2421
- return [3 /*break*/, 49];
2422
- case 49: return [3 /*break*/, 51];
2423
- case 50:
2416
+ return [3 /*break*/, 50];
2417
+ case 50: return [3 /*break*/, 52];
2418
+ case 51:
2424
2419
  progressTracker.push('Drafting response');
2425
2420
  recordStep('Drafting response');
2426
- _13.label = 51;
2427
- case 51: return [3 /*break*/, 54];
2428
- case 52:
2421
+ _13.label = 52;
2422
+ case 52: return [3 /*break*/, 55];
2423
+ case 53:
2429
2424
  error_4 = _13.sent();
2430
2425
  assistantContent = buildAssistantCodexErrorMessage(error_4);
2431
2426
  recordStep('Error', { message: normalizeOptionalString(error_4 === null || error_4 === void 0 ? void 0 : error_4.message) || 'Unknown error' });
2432
- return [3 /*break*/, 54];
2433
- case 53:
2427
+ return [3 /*break*/, 55];
2428
+ case 54:
2434
2429
  progressTracker.stop();
2435
2430
  return [7 /*endfinally*/];
2436
- case 54:
2431
+ case 55:
2437
2432
  if (!assistantContent) {
2438
2433
  assistantContent = buildAssistantCodexErrorMessage(null);
2439
2434
  }
@@ -2533,14 +2528,14 @@ function executeAiAssistantCodexRun(payload, context) {
2533
2528
  output_tokens: codexUsage.output_tokens,
2534
2529
  total_tokens: codexUsage.total_tokens
2535
2530
  } : null;
2536
- if (!finalUsage) return [3 /*break*/, 59];
2531
+ if (!finalUsage) return [3 /*break*/, 60];
2537
2532
  return [4 /*yield*/, resolveClientId(conversation, input.id_client, context === null || context === void 0 ? void 0 : context.id_user)];
2538
- case 55:
2539
- usageClientId = _13.sent();
2540
- if (!usageClientId) return [3 /*break*/, 59];
2541
- _13.label = 56;
2542
2533
  case 56:
2543
- _13.trys.push([56, 58, , 59]);
2534
+ usageClientId = _13.sent();
2535
+ if (!usageClientId) return [3 /*break*/, 60];
2536
+ _13.label = 57;
2537
+ case 57:
2538
+ _13.trys.push([57, 59, , 60]);
2544
2539
  return [4 /*yield*/, (0, openai_usage_ledger_manager_1.recordOpenAIUsage)({
2545
2540
  id_client: usageClientId,
2546
2541
  model: finalUsage.model,
@@ -2551,16 +2546,16 @@ function executeAiAssistantCodexRun(payload, context) {
2551
2546
  id_request: requestId || undefined,
2552
2547
  id_conversation: conversation._id
2553
2548
  })];
2554
- case 57:
2555
- _13.sent();
2556
- return [3 /*break*/, 59];
2557
2549
  case 58:
2550
+ _13.sent();
2551
+ return [3 /*break*/, 60];
2552
+ case 59:
2558
2553
  usageError_1 = _13.sent();
2559
2554
  console.error(new Date(), 'Failed to record codex usage', usageError_1);
2560
- return [3 /*break*/, 59];
2561
- case 59:
2555
+ return [3 /*break*/, 60];
2556
+ case 60:
2562
2557
  finalAssistantDoc = __assign(__assign(__assign(__assign({}, assistantDoc), { _id: assistantMessageId, content: assistantContent, metadata: finalMetadata }), (finalUsage ? { usage: finalUsage } : {})), { updatedAt: finalNow });
2563
- if (!assistantMessageId) return [3 /*break*/, 61];
2558
+ if (!assistantMessageId) return [3 /*break*/, 62];
2564
2559
  setPayload = {
2565
2560
  content: assistantContent,
2566
2561
  metadata: finalMetadata,
@@ -2570,18 +2565,18 @@ function executeAiAssistantCodexRun(payload, context) {
2570
2565
  setPayload.usage = finalUsage;
2571
2566
  }
2572
2567
  return [4 /*yield*/, updateAssistantMessageWithFallback(assistantMessageId, setPayload)];
2573
- case 60:
2568
+ case 61:
2574
2569
  _13.sent();
2575
- _13.label = 61;
2576
- case 61: return [4 /*yield*/, touchConversation(conversation._id, finalNow, assistantMessageId ? String(assistantMessageId) : undefined)];
2577
- case 62:
2570
+ _13.label = 62;
2571
+ case 62: return [4 /*yield*/, touchConversation(conversation._id, finalNow, assistantMessageId ? String(assistantMessageId) : undefined)];
2572
+ case 63:
2578
2573
  _13.sent();
2579
- if (!(input.delete_files_after_run !== false)) return [3 /*break*/, 64];
2574
+ if (!(input.delete_files_after_run !== false)) return [3 /*break*/, 65];
2580
2575
  return [4 /*yield*/, cleanupAttachments(attachmentData.attachments)];
2581
- case 63:
2576
+ case 64:
2582
2577
  _13.sent();
2583
- _13.label = 64;
2584
- case 64: return [2 /*return*/, finalAssistantDoc];
2578
+ _13.label = 65;
2579
+ case 65: return [2 /*return*/, finalAssistantDoc];
2585
2580
  }
2586
2581
  });
2587
2582
  }); });
@@ -9712,7 +9707,8 @@ function ensureAssistantReadAccess(context, permissionView, collection) {
9712
9707
  if (!normalizedPermission) {
9713
9708
  throw new Error('AI assistant report builder bridge: Permission scope required.');
9714
9709
  }
9715
- requiresInvoiceAccess = normalizedCollection ? requiresInvoicePermission(normalizedCollection) : false;
9710
+ requiresInvoiceAccess = (normalizedCollection ? requiresInvoicePermission(normalizedCollection) : false)
9711
+ || /invoice/i.test(normalizedPermission);
9716
9712
  hasInvoiceAccess = requiresInvoiceAccess && userHasInvoiceAccess(user);
9717
9713
  hasViewAccess = userHasViewPermission(user, normalizedPermission);
9718
9714
  if (!hasViewAccess && !hasInvoiceAccess) {
@@ -16299,27 +16295,166 @@ function fetchAssistantProbeDocs(params) {
16299
16295
  })
16300
16296
  .toArray();
16301
16297
  }
16302
- function userHasViewPermission(user, view) {
16303
- var _a, _b, _c;
16304
- if (!user || !view) {
16298
+ var AI_ASSISTANT_PERMISSION_ACTION_TOKENS = new Set([
16299
+ 'list',
16300
+ 'detail',
16301
+ 'new',
16302
+ 'edit',
16303
+ 'delete',
16304
+ 'create',
16305
+ 'dashboard',
16306
+ 'report',
16307
+ 'reports',
16308
+ 'calendar',
16309
+ 'board',
16310
+ 'timeline',
16311
+ 'settings',
16312
+ 'setting',
16313
+ 'view'
16314
+ ]);
16315
+ function tokenizeAssistantPermissionScope(value) {
16316
+ var normalizedRoute = normalizeRouteMatchKey(value);
16317
+ if (!normalizedRoute) {
16318
+ return [];
16319
+ }
16320
+ var rawTokens = normalizedRoute
16321
+ .split(/[^a-z0-9]+/g)
16322
+ .map(function (token) { return token.trim(); })
16323
+ .filter(Boolean);
16324
+ var tokens = [];
16325
+ rawTokens.forEach(function (token) {
16326
+ normalizeCollectionToken(token).forEach(function (normalized) {
16327
+ if (!normalized) {
16328
+ return;
16329
+ }
16330
+ tokens.push(normalized);
16331
+ });
16332
+ });
16333
+ return Array.from(new Set(tokens));
16334
+ }
16335
+ function splitAssistantPermissionTokenSets(value) {
16336
+ var tokens = tokenizeAssistantPermissionScope(value);
16337
+ var actionTokens = new Set();
16338
+ var domainTokens = new Set();
16339
+ tokens.forEach(function (token) {
16340
+ if (AI_ASSISTANT_PERMISSION_ACTION_TOKENS.has(token)) {
16341
+ actionTokens.add(token);
16342
+ return;
16343
+ }
16344
+ domainTokens.add(token);
16345
+ });
16346
+ return {
16347
+ tokens: new Set(tokens),
16348
+ domainTokens: domainTokens,
16349
+ actionTokens: actionTokens
16350
+ };
16351
+ }
16352
+ function isAssistantTokenSetSubset(subset, superset) {
16353
+ var e_43, _a;
16354
+ if (!subset.size) {
16305
16355
  return false;
16306
16356
  }
16307
- if ((_a = user.roles) === null || _a === void 0 ? void 0 : _a.super_admin) {
16357
+ try {
16358
+ for (var subset_1 = __values(subset), subset_1_1 = subset_1.next(); !subset_1_1.done; subset_1_1 = subset_1.next()) {
16359
+ var token = subset_1_1.value;
16360
+ if (!superset.has(token)) {
16361
+ return false;
16362
+ }
16363
+ }
16364
+ }
16365
+ catch (e_43_1) { e_43 = { error: e_43_1 }; }
16366
+ finally {
16367
+ try {
16368
+ if (subset_1_1 && !subset_1_1.done && (_a = subset_1.return)) _a.call(subset_1);
16369
+ }
16370
+ finally { if (e_43) throw e_43.error; }
16371
+ }
16372
+ return true;
16373
+ }
16374
+ function buildAssistantPermissionRegex(value) {
16375
+ var tokens = tokenizeAssistantPermissionScope(value);
16376
+ if (!tokens.length) {
16377
+ return null;
16378
+ }
16379
+ var _a = __read(tokens), first = _a[0], rest = _a.slice(1);
16380
+ var pattern = "(?:^|[^a-z0-9])".concat(escapeRegexValue(first), "s?");
16381
+ rest.forEach(function (token) {
16382
+ pattern += "(?:[^a-z0-9]+".concat(escapeRegexValue(token), "s?)");
16383
+ });
16384
+ pattern += '(?:$|[^a-z0-9])';
16385
+ return new RegExp(pattern, 'i');
16386
+ }
16387
+ function isAssistantViewPermissionMatch(grantedPermission, requestedView) {
16388
+ var grantedRoute = normalizeRouteMatchKey(grantedPermission);
16389
+ var requestedRoute = normalizeRouteMatchKey(requestedView);
16390
+ if (!grantedRoute || !requestedRoute) {
16391
+ return false;
16392
+ }
16393
+ if (grantedRoute === requestedRoute
16394
+ || grantedRoute.startsWith("".concat(requestedRoute, "/"))
16395
+ || requestedRoute.startsWith("".concat(grantedRoute, "/"))) {
16308
16396
  return true;
16309
16397
  }
16310
- var groups = Array.isArray((_b = user.roles) === null || _b === void 0 ? void 0 : _b.groups) ? user.roles.groups : [];
16311
- var miscs = Array.isArray((_c = user.roles) === null || _c === void 0 ? void 0 : _c.miscs) ? user.roles.miscs : [];
16312
- if (groups.some(function (group) { return Array.isArray(group.views) && group.views.some(function (v) { return v.startsWith(view); }); })) {
16398
+ var grantedTokens = splitAssistantPermissionTokenSets(grantedRoute);
16399
+ var requestedTokens = splitAssistantPermissionTokenSets(requestedRoute);
16400
+ if (!grantedTokens.tokens.size || !requestedTokens.tokens.size) {
16401
+ return false;
16402
+ }
16403
+ if (grantedTokens.actionTokens.size && requestedTokens.actionTokens.size) {
16404
+ var hasSharedAction = Array.from(requestedTokens.actionTokens)
16405
+ .some(function (token) { return grantedTokens.actionTokens.has(token); });
16406
+ if (!hasSharedAction) {
16407
+ return false;
16408
+ }
16409
+ }
16410
+ if (!grantedTokens.domainTokens.size || !requestedTokens.domainTokens.size) {
16411
+ return false;
16412
+ }
16413
+ if (isAssistantTokenSetSubset(requestedTokens.domainTokens, grantedTokens.domainTokens)) {
16313
16414
  return true;
16314
16415
  }
16315
- if (miscs.some(function (v) { return v.startsWith(view); })) {
16416
+ if (requestedTokens.domainTokens.size > 1
16417
+ && grantedTokens.domainTokens.size > 1
16418
+ && isAssistantTokenSetSubset(grantedTokens.domainTokens, requestedTokens.domainTokens)) {
16316
16419
  return true;
16317
16420
  }
16318
- if (groups.some(function (group) { return group.name === view; })) {
16421
+ var sharedTokenCount = 0;
16422
+ requestedTokens.tokens.forEach(function (token) {
16423
+ if (grantedTokens.tokens.has(token)) {
16424
+ sharedTokenCount += 1;
16425
+ }
16426
+ });
16427
+ var requestedCoverage = requestedTokens.tokens.size
16428
+ ? sharedTokenCount / requestedTokens.tokens.size
16429
+ : 0;
16430
+ if (sharedTokenCount >= 2 && requestedCoverage >= 0.8) {
16431
+ return true;
16432
+ }
16433
+ var requestedRegex = buildAssistantPermissionRegex(requestedRoute);
16434
+ if (requestedRegex && requestedRegex.test(grantedRoute)) {
16435
+ return true;
16436
+ }
16437
+ var grantedRegex = buildAssistantPermissionRegex(grantedRoute);
16438
+ if (grantedRegex && grantedRegex.test(requestedRoute)) {
16319
16439
  return true;
16320
16440
  }
16321
16441
  return false;
16322
16442
  }
16443
+ function userHasViewPermission(user, view) {
16444
+ var _a, _b;
16445
+ if (!user || !view) {
16446
+ return false;
16447
+ }
16448
+ if ((_a = user.roles) === null || _a === void 0 ? void 0 : _a.super_admin) {
16449
+ return true;
16450
+ }
16451
+ var permissions = collectUserViewPermissions(user);
16452
+ if (permissions.some(function (permission) { return isAssistantViewPermissionMatch(permission, view); })) {
16453
+ return true;
16454
+ }
16455
+ var groups = Array.isArray((_b = user.roles) === null || _b === void 0 ? void 0 : _b.groups) ? user.roles.groups : [];
16456
+ return groups.some(function (group) { return isAssistantViewPermissionMatch(normalizeOptionalString(group === null || group === void 0 ? void 0 : group.name), view); });
16457
+ }
16323
16458
  function collectUserViewPermissions(user) {
16324
16459
  var _a, _b;
16325
16460
  if (!user) {
@@ -16345,7 +16480,7 @@ function collectUserViewPermissions(user) {
16345
16480
  return collected;
16346
16481
  }
16347
16482
  function userHasViewTokenPermission(user, tokenRegex) {
16348
- var _a;
16483
+ var _a, _b;
16349
16484
  if (!user || !tokenRegex) {
16350
16485
  return false;
16351
16486
  }
@@ -16353,7 +16488,11 @@ function userHasViewTokenPermission(user, tokenRegex) {
16353
16488
  return true;
16354
16489
  }
16355
16490
  var permissions = collectUserViewPermissions(user);
16356
- return permissions.some(function (view) { return tokenRegex.test(view); });
16491
+ if (permissions.some(function (view) { return tokenRegex.test(view); })) {
16492
+ return true;
16493
+ }
16494
+ var groups = Array.isArray((_b = user.roles) === null || _b === void 0 ? void 0 : _b.groups) ? user.roles.groups : [];
16495
+ return groups.some(function (group) { return tokenRegex.test(normalizeOptionalString(group === null || group === void 0 ? void 0 : group.name)); });
16357
16496
  }
16358
16497
  function userHasInvoiceAccess(user) {
16359
16498
  return userHasViewTokenPermission(user, /invoice/i);
@@ -16373,6 +16512,13 @@ function requiresInvoicePermission(collection) {
16373
16512
  }
16374
16513
  return normalized.includes('invoice');
16375
16514
  }
16515
+ function assistantMessageRequestsInvoiceData(message) {
16516
+ var normalized = normalizeOptionalString(message).toLowerCase();
16517
+ if (!normalized) {
16518
+ return false;
16519
+ }
16520
+ return /\binvoices?\b/.test(normalized);
16521
+ }
16376
16522
  function redactSensitiveFields(value) {
16377
16523
  if (Array.isArray(value)) {
16378
16524
  return value.map(function (entry) { return redactSensitiveFields(entry); });
@@ -16584,8 +16730,8 @@ function applyCodexStreamStatusHandler(runOptions, streamStatusHandler) {
16584
16730
  }
16585
16731
  function waitForCodexWorkerMessage(worker, streamStatusHandler) {
16586
16732
  return __awaiter(this, void 0, void 0, function () {
16587
- var _a, _b, _c, _d, message, payload, status_1, e_43_1;
16588
- var _e, e_43, _f, _g;
16733
+ var _a, _b, _c, _d, message, payload, status_1, e_44_1;
16734
+ var _e, e_44, _f, _g;
16589
16735
  return __generator(this, function (_h) {
16590
16736
  switch (_h.label) {
16591
16737
  case 0:
@@ -16612,8 +16758,8 @@ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
16612
16758
  return [3 /*break*/, 1];
16613
16759
  case 4: return [3 /*break*/, 11];
16614
16760
  case 5:
16615
- e_43_1 = _h.sent();
16616
- e_43 = { error: e_43_1 };
16761
+ e_44_1 = _h.sent();
16762
+ e_44 = { error: e_44_1 };
16617
16763
  return [3 /*break*/, 11];
16618
16764
  case 6:
16619
16765
  _h.trys.push([6, , 9, 10]);
@@ -16624,7 +16770,7 @@ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
16624
16770
  _h.label = 8;
16625
16771
  case 8: return [3 /*break*/, 10];
16626
16772
  case 9:
16627
- if (e_43) throw e_43.error;
16773
+ if (e_44) throw e_44.error;
16628
16774
  return [7 /*endfinally*/];
16629
16775
  case 10: return [7 /*endfinally*/];
16630
16776
  case 11: throw new CodexWorkerBootstrapError('Codex worker exited before completing.');
@@ -16634,27 +16780,34 @@ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
16634
16780
  }
16635
16781
  function runCodexInWorkerThread(prompt, runOptions, config, streamStatusHandler) {
16636
16782
  return __awaiter(this, void 0, void 0, function () {
16637
- var streamedOptions, codexClient, workerPath, codexClient, error_10, codexClient;
16783
+ var streamedOptions, normalizedThreadKey, shouldForceInProcessThreadReuse, codexClient, codexClient, workerPath, codexClient, error_10, codexClient;
16638
16784
  return __generator(this, function (_a) {
16639
16785
  switch (_a.label) {
16640
16786
  case 0:
16641
16787
  streamedOptions = applyCodexStreamStatusHandler(runOptions, streamStatusHandler);
16642
- if (!!resolveCodexWorkerThreadEnabled()) return [3 /*break*/, 2];
16788
+ normalizedThreadKey = normalizeOptionalString(runOptions === null || runOptions === void 0 ? void 0 : runOptions.threadKey);
16789
+ shouldForceInProcessThreadReuse = !!(normalizedThreadKey && (runOptions === null || runOptions === void 0 ? void 0 : runOptions.reuseThread) !== false);
16790
+ if (!shouldForceInProcessThreadReuse) return [3 /*break*/, 2];
16643
16791
  codexClient = getAssistantCodexClient(config);
16644
16792
  return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16645
16793
  case 1: return [2 /*return*/, _a.sent()];
16646
- case 2: return [4 /*yield*/, resolveCodexWorkerPath()];
16647
- case 3:
16648
- workerPath = _a.sent();
16649
- if (!!workerPath) return [3 /*break*/, 5];
16794
+ case 2:
16795
+ if (!!resolveCodexWorkerThreadEnabled()) return [3 /*break*/, 4];
16650
16796
  codexClient = getAssistantCodexClient(config);
16651
16797
  return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16652
- case 4: return [2 /*return*/, _a.sent()];
16798
+ case 3: return [2 /*return*/, _a.sent()];
16799
+ case 4: return [4 /*yield*/, resolveCodexWorkerPath()];
16653
16800
  case 5:
16654
- _a.trys.push([5, 7, , 9]);
16655
- return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config, streamStatusHandler)];
16801
+ workerPath = _a.sent();
16802
+ if (!!workerPath) return [3 /*break*/, 7];
16803
+ codexClient = getAssistantCodexClient(config);
16804
+ return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16656
16805
  case 6: return [2 /*return*/, _a.sent()];
16657
16806
  case 7:
16807
+ _a.trys.push([7, 9, , 11]);
16808
+ return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config, streamStatusHandler)];
16809
+ case 8: return [2 /*return*/, _a.sent()];
16810
+ case 9:
16658
16811
  error_10 = _a.sent();
16659
16812
  if (!(error_10 instanceof CodexWorkerBootstrapError)) {
16660
16813
  throw error_10;
@@ -16662,8 +16815,8 @@ function runCodexInWorkerThread(prompt, runOptions, config, streamStatusHandler)
16662
16815
  console.error('Codex worker bootstrap failed, falling back to in-process run.', error_10);
16663
16816
  codexClient = getAssistantCodexClient(config);
16664
16817
  return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16665
- case 8: return [2 /*return*/, _a.sent()];
16666
- case 9: return [2 /*return*/];
16818
+ case 10: return [2 /*return*/, _a.sent()];
16819
+ case 11: return [2 /*return*/];
16667
16820
  }
16668
16821
  });
16669
16822
  });
@@ -16961,8 +17114,8 @@ function buildAssistantWorkspaceRootCandidates(params) {
16961
17114
  }
16962
17115
  function resolveAssistantWorkspaceRoot() {
16963
17116
  return __awaiter(this, void 0, void 0, function () {
16964
- var candidates, firstExisting, firstNestedGitRoot, candidates_8, candidates_8_1, candidate, _a, gitRoot, nestedGitRoots, e_44_1;
16965
- var e_44, _b;
17117
+ var candidates, firstExisting, firstNestedGitRoot, candidates_8, candidates_8_1, candidate, _a, gitRoot, nestedGitRoots, e_45_1;
17118
+ var e_45, _b;
16966
17119
  return __generator(this, function (_c) {
16967
17120
  switch (_c.label) {
16968
17121
  case 0:
@@ -17009,14 +17162,14 @@ function resolveAssistantWorkspaceRoot() {
17009
17162
  return [3 /*break*/, 2];
17010
17163
  case 8: return [3 /*break*/, 11];
17011
17164
  case 9:
17012
- e_44_1 = _c.sent();
17013
- e_44 = { error: e_44_1 };
17165
+ e_45_1 = _c.sent();
17166
+ e_45 = { error: e_45_1 };
17014
17167
  return [3 /*break*/, 11];
17015
17168
  case 10:
17016
17169
  try {
17017
17170
  if (candidates_8_1 && !candidates_8_1.done && (_b = candidates_8.return)) _b.call(candidates_8);
17018
17171
  }
17019
- finally { if (e_44) throw e_44.error; }
17172
+ finally { if (e_45) throw e_45.error; }
17020
17173
  return [7 /*endfinally*/];
17021
17174
  case 11:
17022
17175
  if (firstNestedGitRoot) {
@@ -17419,7 +17572,7 @@ var AI_ASSISTANT_BREAKDOWN_DIMENSION_STOPWORDS = new Set([
17419
17572
  'by'
17420
17573
  ]);
17421
17574
  function normalizeAssistantBreakdownDimension(value) {
17422
- var e_45, _a;
17575
+ var e_46, _a;
17423
17576
  var normalized = normalizeOptionalString(value)
17424
17577
  .toLowerCase()
17425
17578
  .replace(/[^a-z0-9_\s-]+/g, ' ')
@@ -17451,12 +17604,12 @@ function normalizeAssistantBreakdownDimension(value) {
17451
17604
  }
17452
17605
  }
17453
17606
  }
17454
- catch (e_45_1) { e_45 = { error: e_45_1 }; }
17607
+ catch (e_46_1) { e_46 = { error: e_46_1 }; }
17455
17608
  finally {
17456
17609
  try {
17457
17610
  if (tokens_1_1 && !tokens_1_1.done && (_a = tokens_1.return)) _a.call(tokens_1);
17458
17611
  }
17459
- finally { if (e_45) throw e_45.error; }
17612
+ finally { if (e_46) throw e_46.error; }
17460
17613
  }
17461
17614
  if (!kept.length) {
17462
17615
  return '';
@@ -17654,7 +17807,7 @@ function resolveAssistantPlannerEnabled() {
17654
17807
  return raw === undefined ? true : raw === true;
17655
17808
  }
17656
17809
  function resolveAssistantPlannerKnownRoutes(user, isSuperAdmin) {
17657
- var e_46, _a;
17810
+ var e_47, _a;
17658
17811
  var _b;
17659
17812
  if (isSuperAdmin === void 0) { isSuperAdmin = false; }
17660
17813
  var routes = ((_b = resolveio_server_app_1.ResolveIOServer.getClientRoutes) === null || _b === void 0 ? void 0 : _b.call(resolveio_server_app_1.ResolveIOServer)) || [];
@@ -17668,12 +17821,12 @@ function resolveAssistantPlannerKnownRoutes(user, isSuperAdmin) {
17668
17821
  }
17669
17822
  }
17670
17823
  }
17671
- catch (e_46_1) { e_46 = { error: e_46_1 }; }
17824
+ catch (e_47_1) { e_47 = { error: e_47_1 }; }
17672
17825
  finally {
17673
17826
  try {
17674
17827
  if (routes_1_1 && !routes_1_1.done && (_a = routes_1.return)) _a.call(routes_1);
17675
17828
  }
17676
- finally { if (e_46) throw e_46.error; }
17829
+ finally { if (e_47) throw e_47.error; }
17677
17830
  }
17678
17831
  var normalizedRoutes = Array.from(unique);
17679
17832
  var allowedRoutes = collectAssistantAllowedRoutesForUser(user, normalizedRoutes, isSuperAdmin);
@@ -17899,34 +18052,6 @@ function buildAssistantFieldHints(message, collectionNames, options) {
17899
18052
  });
17900
18053
  return hints;
17901
18054
  }
17902
- function isAssistantWhyFollowupMessage(message) {
17903
- var normalized = normalizeOptionalString(message).toLowerCase();
17904
- if (!normalized) {
17905
- return false;
17906
- }
17907
- return /^(so\s+)?why(?:\s+not)?[.!?]*$/.test(normalized)
17908
- || /^(so\s+)?why\b/.test(normalized);
17909
- }
17910
- function resolveRecentAssistantToolError(history) {
17911
- var _a, _b;
17912
- var entries = Array.isArray(history) ? history : [];
17913
- for (var i = entries.length - 1; i >= 0; i -= 1) {
17914
- var entry = entries[i];
17915
- if ((entry === null || entry === void 0 ? void 0 : entry.role) !== 'assistant') {
17916
- continue;
17917
- }
17918
- var notes = Array.isArray((_b = (_a = entry === null || entry === void 0 ? void 0 : entry.metadata) === null || _a === void 0 ? void 0 : _a.debug) === null || _b === void 0 ? void 0 : _b.notes)
17919
- ? entry.metadata.debug.notes
17920
- : [];
17921
- var matched = notes
17922
- .map(function (note) { return normalizeOptionalString(note); })
17923
- .find(function (note) { return note.toLowerCase().startsWith('tool error:'); });
17924
- if (matched) {
17925
- return matched.replace(/^tool error:\s*/i, '').trim();
17926
- }
17927
- }
17928
- return '';
17929
- }
17930
18055
  var cachedClientRouteIndex = null;
17931
18056
  function normalizeRouteKey(value) {
17932
18057
  var trimmed = normalizeOptionalString(value);
@@ -17940,7 +18065,7 @@ function normalizeRouteMatchKey(value) {
17940
18065
  return normalizeRouteKey(value).toLowerCase();
17941
18066
  }
17942
18067
  function buildClientRouteIndex() {
17943
- var e_47, _a;
18068
+ var e_48, _a;
17944
18069
  var _b;
17945
18070
  var routes = ((_b = resolveio_server_app_1.ResolveIOServer.getClientRoutes) === null || _b === void 0 ? void 0 : _b.call(resolveio_server_app_1.ResolveIOServer)) || [];
17946
18071
  var set = new Set();
@@ -17959,12 +18084,12 @@ function buildClientRouteIndex() {
17959
18084
  }
17960
18085
  }
17961
18086
  }
17962
- catch (e_47_1) { e_47 = { error: e_47_1 }; }
18087
+ catch (e_48_1) { e_48 = { error: e_48_1 }; }
17963
18088
  finally {
17964
18089
  try {
17965
18090
  if (routes_2_1 && !routes_2_1.done && (_a = routes_2.return)) _a.call(routes_2);
17966
18091
  }
17967
- finally { if (e_47) throw e_47.error; }
18092
+ finally { if (e_48) throw e_48.error; }
17968
18093
  }
17969
18094
  return { set: set, map: map, size: routes.length };
17970
18095
  }
@@ -18130,16 +18255,15 @@ function collectAssistantAllowedRoutesForUser(user, allRoutes, isSuperAdmin) {
18130
18255
  seen.add(normalized);
18131
18256
  allowed.push(normalized);
18132
18257
  };
18133
- var allRouteEntries = normalizedRoutes.map(function (route) { return ({ route: route, routeKey: route.toLowerCase() }); });
18258
+ var allRouteEntries = normalizedRoutes.map(function (route) { return ({ route: route }); });
18134
18259
  views.forEach(function (view) {
18135
- var viewKey = view.toLowerCase();
18136
- var canonical = normalizedRoutes.find(function (route) { return route.toLowerCase() === viewKey; });
18260
+ var canonical = normalizedRoutes.find(function (route) { return normalizeRouteMatchKey(route) === normalizeRouteMatchKey(view); });
18137
18261
  if (canonical) {
18138
18262
  push(canonical);
18139
18263
  }
18140
18264
  var matched = false;
18141
18265
  allRouteEntries.forEach(function (entry) {
18142
- if (entry.routeKey === viewKey || entry.routeKey.startsWith("".concat(viewKey, "/"))) {
18266
+ if (isAssistantViewPermissionMatch(view, entry.route)) {
18143
18267
  push(entry.route);
18144
18268
  matched = true;
18145
18269
  }
@@ -18744,8 +18868,8 @@ function shouldSkipAssistantGitDiscoveryDirectory(name) {
18744
18868
  }
18745
18869
  function resolveAssistantWorkspaceGitRoots(workspaceRoot) {
18746
18870
  return __awaiter(this, void 0, void 0, function () {
18747
- var roots, seen, push, _a, configuredRoots, configuredRoots_1, configuredRoots_1_1, configuredRoot, _b, e_48_1, queue, queued, enqueue, next, entries, _c, entries_1, entries_1_1, entry, childName, candidate, gitPath, _d, e_49_1;
18748
- var e_48, _e, e_49, _f;
18871
+ var roots, seen, push, _a, configuredRoots, configuredRoots_1, configuredRoots_1_1, configuredRoot, _b, e_49_1, queue, queued, enqueue, next, entries, _c, entries_1, entries_1_1, entry, childName, candidate, gitPath, _d, e_50_1;
18872
+ var e_49, _e, e_50, _f;
18749
18873
  var _g;
18750
18874
  return __generator(this, function (_h) {
18751
18875
  switch (_h.label) {
@@ -18790,14 +18914,14 @@ function resolveAssistantWorkspaceGitRoots(workspaceRoot) {
18790
18914
  return [3 /*break*/, 3];
18791
18915
  case 6: return [3 /*break*/, 9];
18792
18916
  case 7:
18793
- e_48_1 = _h.sent();
18794
- e_48 = { error: e_48_1 };
18917
+ e_49_1 = _h.sent();
18918
+ e_49 = { error: e_49_1 };
18795
18919
  return [3 /*break*/, 9];
18796
18920
  case 8:
18797
18921
  try {
18798
18922
  if (configuredRoots_1_1 && !configuredRoots_1_1.done && (_e = configuredRoots_1.return)) _e.call(configuredRoots_1);
18799
18923
  }
18800
- finally { if (e_48) throw e_48.error; }
18924
+ finally { if (e_49) throw e_49.error; }
18801
18925
  return [7 /*endfinally*/];
18802
18926
  case 9:
18803
18927
  queue = [];
@@ -18837,7 +18961,7 @@ function resolveAssistantWorkspaceGitRoots(workspaceRoot) {
18837
18961
  return [3 /*break*/, 14];
18838
18962
  case 14:
18839
18963
  _h.trys.push([14, 21, 22, 23]);
18840
- entries_1 = (e_49 = void 0, __values(entries)), entries_1_1 = entries_1.next();
18964
+ entries_1 = (e_50 = void 0, __values(entries)), entries_1_1 = entries_1.next();
18841
18965
  _h.label = 15;
18842
18966
  case 15:
18843
18967
  if (!!entries_1_1.done) return [3 /*break*/, 20];
@@ -18876,14 +19000,14 @@ function resolveAssistantWorkspaceGitRoots(workspaceRoot) {
18876
19000
  return [3 /*break*/, 15];
18877
19001
  case 20: return [3 /*break*/, 23];
18878
19002
  case 21:
18879
- e_49_1 = _h.sent();
18880
- e_49 = { error: e_49_1 };
19003
+ e_50_1 = _h.sent();
19004
+ e_50 = { error: e_50_1 };
18881
19005
  return [3 /*break*/, 23];
18882
19006
  case 22:
18883
19007
  try {
18884
19008
  if (entries_1_1 && !entries_1_1.done && (_f = entries_1.return)) _f.call(entries_1);
18885
19009
  }
18886
- finally { if (e_49) throw e_49.error; }
19010
+ finally { if (e_50) throw e_50.error; }
18887
19011
  return [7 /*endfinally*/];
18888
19012
  case 23: return [3 /*break*/, 10];
18889
19013
  case 24: return [2 /*return*/, roots];
@@ -19186,8 +19310,8 @@ function syncAssistantGitMirror(repoUrl) {
19186
19310
  }
19187
19311
  function resolveAssistantChangeHistoryGitRoots(workspaceRoot) {
19188
19312
  return __awaiter(this, void 0, void 0, function () {
19189
- var roots, repoUrls, mirroredRoots, repoUrls_1, repoUrls_1_1, repoUrl, mirrorRoot, e_50_1;
19190
- var e_50, _a;
19313
+ var roots, repoUrls, mirroredRoots, repoUrls_1, repoUrls_1_1, repoUrl, mirrorRoot, e_51_1;
19314
+ var e_51, _a;
19191
19315
  return __generator(this, function (_b) {
19192
19316
  switch (_b.label) {
19193
19317
  case 0: return [4 /*yield*/, resolveAssistantWorkspaceGitRoots(workspaceRoot)];
@@ -19221,14 +19345,14 @@ function resolveAssistantChangeHistoryGitRoots(workspaceRoot) {
19221
19345
  return [3 /*break*/, 3];
19222
19346
  case 6: return [3 /*break*/, 9];
19223
19347
  case 7:
19224
- e_50_1 = _b.sent();
19225
- e_50 = { error: e_50_1 };
19348
+ e_51_1 = _b.sent();
19349
+ e_51 = { error: e_51_1 };
19226
19350
  return [3 /*break*/, 9];
19227
19351
  case 8:
19228
19352
  try {
19229
19353
  if (repoUrls_1_1 && !repoUrls_1_1.done && (_a = repoUrls_1.return)) _a.call(repoUrls_1);
19230
19354
  }
19231
- finally { if (e_50) throw e_50.error; }
19355
+ finally { if (e_51) throw e_51.error; }
19232
19356
  return [7 /*endfinally*/];
19233
19357
  case 9: return [2 /*return*/, mirroredRoots];
19234
19358
  }
@@ -19237,8 +19361,8 @@ function resolveAssistantChangeHistoryGitRoots(workspaceRoot) {
19237
19361
  }
19238
19362
  function resolveAssistantChangeHistoryFastPathResponse(params) {
19239
19363
  return __awaiter(this, void 0, void 0, function () {
19240
- var workspaceRoot, _a, gitRoots, featureKeywords, sawExecutionError, bestFallback, gitRoots_1, gitRoots_1_1, gitRoot, _b, branch, _c, _d, limit, historyDepth, rawHistory, commits, summary, hasKeywordMatches, _e, e_51_1;
19241
- var e_51, _f;
19364
+ var workspaceRoot, _a, gitRoots, featureKeywords, sawExecutionError, bestFallback, gitRoots_1, gitRoots_1_1, gitRoot, _b, branch, _c, _d, limit, historyDepth, rawHistory, commits, summary, hasKeywordMatches, _e, e_52_1;
19365
+ var e_52, _f;
19242
19366
  return __generator(this, function (_g) {
19243
19367
  switch (_g.label) {
19244
19368
  case 0:
@@ -19360,14 +19484,14 @@ function resolveAssistantChangeHistoryFastPathResponse(params) {
19360
19484
  return [3 /*break*/, 7];
19361
19485
  case 20: return [3 /*break*/, 23];
19362
19486
  case 21:
19363
- e_51_1 = _g.sent();
19364
- e_51 = { error: e_51_1 };
19487
+ e_52_1 = _g.sent();
19488
+ e_52 = { error: e_52_1 };
19365
19489
  return [3 /*break*/, 23];
19366
19490
  case 22:
19367
19491
  try {
19368
19492
  if (gitRoots_1_1 && !gitRoots_1_1.done && (_f = gitRoots_1.return)) _f.call(gitRoots_1);
19369
19493
  }
19370
- finally { if (e_51) throw e_51.error; }
19494
+ finally { if (e_52) throw e_52.error; }
19371
19495
  return [7 /*endfinally*/];
19372
19496
  case 23:
19373
19497
  if (bestFallback) {
@@ -19548,7 +19672,7 @@ function sanitizeAssistantResponse(value) {
19548
19672
  return normalizeAssistantRoutes(normalizedCurrency);
19549
19673
  }
19550
19674
  function evaluateAssistantGuardrails(message) {
19551
- var e_52, _a;
19675
+ var e_53, _a;
19552
19676
  var normalized = String(message || '').toLowerCase();
19553
19677
  var identityGuardrail = evaluateAssistantIdentityDisclosureGuardrail(normalized);
19554
19678
  if (identityGuardrail === null || identityGuardrail === void 0 ? void 0 : identityGuardrail.blocked) {
@@ -19598,12 +19722,12 @@ function evaluateAssistantGuardrails(message) {
19598
19722
  }
19599
19723
  }
19600
19724
  }
19601
- catch (e_52_1) { e_52 = { error: e_52_1 }; }
19725
+ catch (e_53_1) { e_53 = { error: e_53_1 }; }
19602
19726
  finally {
19603
19727
  try {
19604
19728
  if (patterns_2_1 && !patterns_2_1.done && (_a = patterns_2.return)) _a.call(patterns_2);
19605
19729
  }
19606
- finally { if (e_52) throw e_52.error; }
19730
+ finally { if (e_53) throw e_53.error; }
19607
19731
  }
19608
19732
  return null;
19609
19733
  }
@@ -19718,7 +19842,7 @@ function tokenizeArithmeticExpression(expression) {
19718
19842
  return tokens;
19719
19843
  }
19720
19844
  function evaluateArithmeticExpression(expression) {
19721
- var e_53, _a, e_54, _b;
19845
+ var e_54, _a, e_55, _b;
19722
19846
  var tokens = tokenizeArithmeticExpression(expression);
19723
19847
  if (!tokens || !tokens.length) {
19724
19848
  return null;
@@ -19775,12 +19899,12 @@ function evaluateArithmeticExpression(expression) {
19775
19899
  prevToken = token;
19776
19900
  }
19777
19901
  }
19778
- catch (e_53_1) { e_53 = { error: e_53_1 }; }
19902
+ catch (e_54_1) { e_54 = { error: e_54_1 }; }
19779
19903
  finally {
19780
19904
  try {
19781
19905
  if (tokens_2_1 && !tokens_2_1.done && (_a = tokens_2.return)) _a.call(tokens_2);
19782
19906
  }
19783
- finally { if (e_53) throw e_53.error; }
19907
+ finally { if (e_54) throw e_54.error; }
19784
19908
  }
19785
19909
  while (ops.length) {
19786
19910
  var op = ops.pop();
@@ -19820,12 +19944,12 @@ function evaluateArithmeticExpression(expression) {
19820
19944
  stack.push(Number(token));
19821
19945
  }
19822
19946
  }
19823
- catch (e_54_1) { e_54 = { error: e_54_1 }; }
19947
+ catch (e_55_1) { e_55 = { error: e_55_1 }; }
19824
19948
  finally {
19825
19949
  try {
19826
19950
  if (output_1_1 && !output_1_1.done && (_b = output_1.return)) _b.call(output_1);
19827
19951
  }
19828
- finally { if (e_54) throw e_54.error; }
19952
+ finally { if (e_55) throw e_55.error; }
19829
19953
  }
19830
19954
  if (stack.length !== 1 || Number.isNaN(stack[0])) {
19831
19955
  return null;
@@ -20009,8 +20133,8 @@ function handleCodexUpload(id_conversation, file_name, content_base64, size, con
20009
20133
  }
20010
20134
  function readAttachmentContents(attachments) {
20011
20135
  return __awaiter(this, void 0, void 0, function () {
20012
- var limits, totalBytes, totalChars, chunks, cleaned, attachments_1, attachments_1_1, attachment, localPath, safe, stat, ext, name_1, type, readable, content, _a, e_55_1;
20013
- var e_55, _b;
20136
+ var limits, totalBytes, totalChars, chunks, cleaned, attachments_1, attachments_1_1, attachment, localPath, safe, stat, ext, name_1, type, readable, content, _a, e_56_1;
20137
+ var e_56, _b;
20014
20138
  return __generator(this, function (_c) {
20015
20139
  switch (_c.label) {
20016
20140
  case 0:
@@ -20089,14 +20213,14 @@ function readAttachmentContents(attachments) {
20089
20213
  return [3 /*break*/, 2];
20090
20214
  case 10: return [3 /*break*/, 13];
20091
20215
  case 11:
20092
- e_55_1 = _c.sent();
20093
- e_55 = { error: e_55_1 };
20216
+ e_56_1 = _c.sent();
20217
+ e_56 = { error: e_56_1 };
20094
20218
  return [3 /*break*/, 13];
20095
20219
  case 12:
20096
20220
  try {
20097
20221
  if (attachments_1_1 && !attachments_1_1.done && (_b = attachments_1.return)) _b.call(attachments_1);
20098
20222
  }
20099
- finally { if (e_55) throw e_55.error; }
20223
+ finally { if (e_56) throw e_56.error; }
20100
20224
  return [7 /*endfinally*/];
20101
20225
  case 13: return [2 /*return*/, {
20102
20226
  promptText: chunks.length ? "\n\nAttachments:\n".concat(chunks.join('\n\n')) : '',
@@ -20420,7 +20544,7 @@ function estimateUsage(messages, responseText, model) {
20420
20544
  };
20421
20545
  }
20422
20546
  function evaluateGuardrails(message) {
20423
- var e_56, _a;
20547
+ var e_57, _a;
20424
20548
  var normalized = String(message || '').toLowerCase();
20425
20549
  var identityGuardrail = evaluateAssistantIdentityDisclosureGuardrail(normalized);
20426
20550
  if (identityGuardrail === null || identityGuardrail === void 0 ? void 0 : identityGuardrail.blocked) {
@@ -20446,12 +20570,12 @@ function evaluateGuardrails(message) {
20446
20570
  }
20447
20571
  }
20448
20572
  }
20449
- catch (e_56_1) { e_56 = { error: e_56_1 }; }
20573
+ catch (e_57_1) { e_57 = { error: e_57_1 }; }
20450
20574
  finally {
20451
20575
  try {
20452
20576
  if (patterns_3_1 && !patterns_3_1.done && (_a = patterns_3.return)) _a.call(patterns_3);
20453
20577
  }
20454
- finally { if (e_56) throw e_56.error; }
20578
+ finally { if (e_57) throw e_57.error; }
20455
20579
  }
20456
20580
  return null;
20457
20581
  }