@resolveio/server-lib 22.1.22 → 22.1.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,11 +1711,11 @@ 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
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, 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;
@@ -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,
@@ -16634,27 +16616,34 @@ function waitForCodexWorkerMessage(worker, streamStatusHandler) {
16634
16616
  }
16635
16617
  function runCodexInWorkerThread(prompt, runOptions, config, streamStatusHandler) {
16636
16618
  return __awaiter(this, void 0, void 0, function () {
16637
- var streamedOptions, codexClient, workerPath, codexClient, error_10, codexClient;
16619
+ var streamedOptions, normalizedThreadKey, shouldForceInProcessThreadReuse, codexClient, codexClient, workerPath, codexClient, error_10, codexClient;
16638
16620
  return __generator(this, function (_a) {
16639
16621
  switch (_a.label) {
16640
16622
  case 0:
16641
16623
  streamedOptions = applyCodexStreamStatusHandler(runOptions, streamStatusHandler);
16642
- if (!!resolveCodexWorkerThreadEnabled()) return [3 /*break*/, 2];
16624
+ normalizedThreadKey = normalizeOptionalString(runOptions === null || runOptions === void 0 ? void 0 : runOptions.threadKey);
16625
+ shouldForceInProcessThreadReuse = !!(normalizedThreadKey && (runOptions === null || runOptions === void 0 ? void 0 : runOptions.reuseThread) !== false);
16626
+ if (!shouldForceInProcessThreadReuse) return [3 /*break*/, 2];
16643
16627
  codexClient = getAssistantCodexClient(config);
16644
16628
  return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16645
16629
  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];
16630
+ case 2:
16631
+ if (!!resolveCodexWorkerThreadEnabled()) return [3 /*break*/, 4];
16650
16632
  codexClient = getAssistantCodexClient(config);
16651
16633
  return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16652
- case 4: return [2 /*return*/, _a.sent()];
16634
+ case 3: return [2 /*return*/, _a.sent()];
16635
+ case 4: return [4 /*yield*/, resolveCodexWorkerPath()];
16653
16636
  case 5:
16654
- _a.trys.push([5, 7, , 9]);
16655
- return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config, streamStatusHandler)];
16637
+ workerPath = _a.sent();
16638
+ if (!!workerPath) return [3 /*break*/, 7];
16639
+ codexClient = getAssistantCodexClient(config);
16640
+ return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16656
16641
  case 6: return [2 /*return*/, _a.sent()];
16657
16642
  case 7:
16643
+ _a.trys.push([7, 9, , 11]);
16644
+ return [4 /*yield*/, runCodexInWorkerThreadInternal(workerPath, prompt, runOptions, config, streamStatusHandler)];
16645
+ case 8: return [2 /*return*/, _a.sent()];
16646
+ case 9:
16658
16647
  error_10 = _a.sent();
16659
16648
  if (!(error_10 instanceof CodexWorkerBootstrapError)) {
16660
16649
  throw error_10;
@@ -16662,8 +16651,8 @@ function runCodexInWorkerThread(prompt, runOptions, config, streamStatusHandler)
16662
16651
  console.error('Codex worker bootstrap failed, falling back to in-process run.', error_10);
16663
16652
  codexClient = getAssistantCodexClient(config);
16664
16653
  return [4 /*yield*/, codexClient.run(prompt, streamedOptions)];
16665
- case 8: return [2 /*return*/, _a.sent()];
16666
- case 9: return [2 /*return*/];
16654
+ case 10: return [2 /*return*/, _a.sent()];
16655
+ case 11: return [2 /*return*/];
16667
16656
  }
16668
16657
  });
16669
16658
  });
@@ -17899,34 +17888,6 @@ function buildAssistantFieldHints(message, collectionNames, options) {
17899
17888
  });
17900
17889
  return hints;
17901
17890
  }
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
17891
  var cachedClientRouteIndex = null;
17931
17892
  function normalizeRouteKey(value) {
17932
17893
  var trimmed = normalizeOptionalString(value);