@resolveio/server-lib 22.1.28 → 22.1.29

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.
@@ -1614,6 +1614,15 @@ function executeAiAssistantCodexRun(payload, context) {
1614
1614
  user = _d.sent();
1615
1615
  isSuperAdmin = !!((_a = user === null || user === void 0 ? void 0 : user.roles) === null || _a === void 0 ? void 0 : _a.super_admin);
1616
1616
  canViewDebug = isAssistantDebugUser(user);
1617
+ if (shouldLogAssistantFinalizeDebug()) {
1618
+ console.log(new Date(), '[AI-Dashboard][AI-Assistant] debug-visibility', {
1619
+ requestId: requestId || null,
1620
+ id_user: (context === null || context === void 0 ? void 0 : context.id_user) || null,
1621
+ username: normalizeOptionalString(user === null || user === void 0 ? void 0 : user.username) || null,
1622
+ isSuperAdmin: isSuperAdmin,
1623
+ canViewDebug: canViewDebug
1624
+ });
1625
+ }
1617
1626
  hasInvoiceAccess = userHasInvoiceAccess(user);
1618
1627
  customerId = normalizeOptionalString((_b = user === null || user === void 0 ? void 0 : user.other) === null || _b === void 0 ? void 0 : _b.id_customer);
1619
1628
  return [4 /*yield*/, ensureConversation(input, 'codex', context === null || context === void 0 ? void 0 : context.id_user)];
@@ -20485,54 +20494,110 @@ function resolveAssistantDatabaseNameForSystemCollections() {
20485
20494
  }
20486
20495
  function updateAssistantMessageWithFallback(messageId, setPayload) {
20487
20496
  return __awaiter(this, void 0, void 0, function () {
20488
- var error_12, dbName, _a;
20489
- return __generator(this, function (_b) {
20490
- switch (_b.label) {
20497
+ var safeSetPayload, fallbackId, debugEnabled, result, error_12, dbName, messagesCollection, result, _a, retryResult;
20498
+ var _b, _c, _d, _e, _f, _g;
20499
+ return __generator(this, function (_h) {
20500
+ switch (_h.label) {
20491
20501
  case 0:
20492
20502
  if (!messageId || !setPayload || typeof setPayload !== 'object') {
20493
20503
  return [2 /*return*/];
20494
20504
  }
20495
- _b.label = 1;
20505
+ safeSetPayload = sanitizeAssistantMessageSetPayload(setPayload);
20506
+ fallbackId = normalizeOptionalString(typeof messageId === 'string' ? messageId : String(messageId));
20507
+ debugEnabled = shouldLogAssistantFinalizeDebug();
20508
+ _h.label = 1;
20496
20509
  case 1:
20497
- _b.trys.push([1, 3, , 10]);
20498
- return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.updateOne({ _id: messageId }, { $set: setPayload }, undefined, false, false, true)];
20510
+ _h.trys.push([1, 5, , 17]);
20511
+ return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.updateOne({ _id: messageId }, { $set: safeSetPayload }, undefined, false, false, true)];
20499
20512
  case 2:
20500
- _b.sent();
20501
- return [2 /*return*/];
20513
+ result = _h.sent();
20514
+ if (!((result === null || result === void 0 ? void 0 : result.matchedCount) === 0 && fallbackId && fallbackId !== messageId)) return [3 /*break*/, 4];
20515
+ return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.updateOne({ _id: fallbackId }, { $set: safeSetPayload }, undefined, false, false, true)];
20502
20516
  case 3:
20503
- error_12 = _b.sent();
20517
+ result = _h.sent();
20518
+ _h.label = 4;
20519
+ case 4:
20520
+ if (debugEnabled) {
20521
+ console.log(new Date(), '[AI-Dashboard][AI-Assistant] message finalize update', {
20522
+ messageId: fallbackId || messageId,
20523
+ matchedCount: (_b = result === null || result === void 0 ? void 0 : result.matchedCount) !== null && _b !== void 0 ? _b : 0,
20524
+ modifiedCount: (_c = result === null || result === void 0 ? void 0 : result.modifiedCount) !== null && _c !== void 0 ? _c : 0,
20525
+ via: 'collection_manager'
20526
+ });
20527
+ }
20528
+ return [2 /*return*/];
20529
+ case 5:
20530
+ error_12 = _h.sent();
20504
20531
  if (!isAssistantTransactionCommittedError(error_12)) {
20505
20532
  throw error_12;
20506
20533
  }
20534
+ if (debugEnabled) {
20535
+ console.log(new Date(), '[AI-Dashboard][AI-Assistant] message finalize update fallback triggered', {
20536
+ messageId: fallbackId || messageId,
20537
+ reason: normalizeOptionalString(error_12 === null || error_12 === void 0 ? void 0 : error_12.message) || 'transaction_committed'
20538
+ });
20539
+ }
20507
20540
  dbName = resolveAssistantDatabaseNameForSystemCollections();
20508
- _b.label = 4;
20509
- case 4:
20510
- _b.trys.push([4, 6, , 7]);
20511
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMongoConnection()
20512
- .db(dbName)
20513
- .collection('ai-terminal-messages')
20514
- .updateOne({ _id: messageId }, { $set: setPayload })];
20515
- case 5:
20516
- _b.sent();
20517
- return [2 /*return*/];
20541
+ _h.label = 6;
20518
20542
  case 6:
20519
- _a = _b.sent();
20520
- return [3 /*break*/, 7];
20521
- case 7: return [4 /*yield*/, (0, promises_1.setTimeout)(15)];
20543
+ _h.trys.push([6, 11, , 12]);
20544
+ messagesCollection = resolveio_server_app_1.ResolveIOServer.getMongoConnection()
20545
+ .db(dbName)
20546
+ .collection('ai-terminal-messages');
20547
+ return [4 /*yield*/, messagesCollection
20548
+ .updateOne({ _id: String(messageId) }, { $set: safeSetPayload })];
20549
+ case 7:
20550
+ result = _h.sent();
20551
+ if (!((result === null || result === void 0 ? void 0 : result.matchedCount) === 0 && fallbackId && fallbackId !== messageId)) return [3 /*break*/, 9];
20552
+ return [4 /*yield*/, messagesCollection
20553
+ .updateOne({ _id: fallbackId }, { $set: safeSetPayload })];
20522
20554
  case 8:
20523
- _b.sent();
20524
- return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.updateOne({ _id: messageId }, { $set: setPayload }, undefined, false, false, true)];
20525
- case 9:
20526
- _b.sent();
20527
- return [3 /*break*/, 10];
20528
- case 10: return [2 /*return*/];
20555
+ result = _h.sent();
20556
+ _h.label = 9;
20557
+ case 9: return [4 /*yield*/, notifyAiTerminalCollectionUpdate('ai-terminal-messages', fallbackId || messageId)];
20558
+ case 10:
20559
+ _h.sent();
20560
+ if (debugEnabled) {
20561
+ console.log(new Date(), '[AI-Dashboard][AI-Assistant] message finalize update', {
20562
+ messageId: fallbackId || messageId,
20563
+ matchedCount: (_d = result === null || result === void 0 ? void 0 : result.matchedCount) !== null && _d !== void 0 ? _d : 0,
20564
+ modifiedCount: (_e = result === null || result === void 0 ? void 0 : result.modifiedCount) !== null && _e !== void 0 ? _e : 0,
20565
+ via: 'direct_mongo_fallback'
20566
+ });
20567
+ }
20568
+ return [2 /*return*/];
20569
+ case 11:
20570
+ _a = _h.sent();
20571
+ return [3 /*break*/, 12];
20572
+ case 12: return [4 /*yield*/, (0, promises_1.setTimeout)(15)];
20573
+ case 13:
20574
+ _h.sent();
20575
+ return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.updateOne({ _id: messageId }, { $set: safeSetPayload }, undefined, false, false, true)];
20576
+ case 14:
20577
+ retryResult = _h.sent();
20578
+ if (!((retryResult === null || retryResult === void 0 ? void 0 : retryResult.matchedCount) === 0 && fallbackId && fallbackId !== messageId)) return [3 /*break*/, 16];
20579
+ return [4 /*yield*/, ai_terminal_message_collection_1.AiTerminalMessages.updateOne({ _id: fallbackId }, { $set: safeSetPayload }, undefined, false, false, true)];
20580
+ case 15:
20581
+ retryResult = _h.sent();
20582
+ _h.label = 16;
20583
+ case 16:
20584
+ if (debugEnabled) {
20585
+ console.log(new Date(), '[AI-Dashboard][AI-Assistant] message finalize update', {
20586
+ messageId: fallbackId || messageId,
20587
+ matchedCount: (_f = retryResult === null || retryResult === void 0 ? void 0 : retryResult.matchedCount) !== null && _f !== void 0 ? _f : 0,
20588
+ modifiedCount: (_g = retryResult === null || retryResult === void 0 ? void 0 : retryResult.modifiedCount) !== null && _g !== void 0 ? _g : 0,
20589
+ via: 'collection_manager_retry'
20590
+ });
20591
+ }
20592
+ return [3 /*break*/, 17];
20593
+ case 17: return [2 /*return*/];
20529
20594
  }
20530
20595
  });
20531
20596
  });
20532
20597
  }
20533
20598
  function touchConversation(idConversation, timestamp, lastMessageId) {
20534
20599
  return __awaiter(this, void 0, void 0, function () {
20535
- var update, error_13, dbName;
20600
+ var update, error_13, dbName, conversationsCollection;
20536
20601
  return __generator(this, function (_a) {
20537
20602
  switch (_a.label) {
20538
20603
  case 0:
@@ -20545,7 +20610,7 @@ function touchConversation(idConversation, timestamp, lastMessageId) {
20545
20610
  }
20546
20611
  _a.label = 1;
20547
20612
  case 1:
20548
- _a.trys.push([1, 3, , 5]);
20613
+ _a.trys.push([1, 3, , 6]);
20549
20614
  return [4 /*yield*/, ai_terminal_conversation_collection_1.AiTerminalConversations.updateOne({ _id: idConversation }, { $set: update }, undefined, false, false, true)];
20550
20615
  case 2:
20551
20616
  _a.sent();
@@ -20556,18 +20621,106 @@ function touchConversation(idConversation, timestamp, lastMessageId) {
20556
20621
  throw error_13;
20557
20622
  }
20558
20623
  dbName = resolveAssistantDatabaseNameForSystemCollections();
20559
- return [4 /*yield*/, resolveio_server_app_1.ResolveIOServer.getMongoConnection()
20560
- .db(dbName)
20561
- .collection('ai-terminal-conversations')
20562
- .updateOne({ _id: idConversation }, { $set: update })];
20624
+ conversationsCollection = resolveio_server_app_1.ResolveIOServer.getMongoConnection()
20625
+ .db(dbName)
20626
+ .collection('ai-terminal-conversations');
20627
+ return [4 /*yield*/, conversationsCollection.updateOne({ _id: idConversation }, { $set: update })];
20563
20628
  case 4:
20564
20629
  _a.sent();
20630
+ return [4 /*yield*/, notifyAiTerminalCollectionUpdate('ai-terminal-conversations', idConversation)];
20631
+ case 5:
20632
+ _a.sent();
20633
+ return [3 /*break*/, 6];
20634
+ case 6: return [2 /*return*/];
20635
+ }
20636
+ });
20637
+ });
20638
+ }
20639
+ function shouldLogAssistantFinalizeDebug() {
20640
+ return parseDebugFlag(process.env.AI_DASHBOARD_DEBUG)
20641
+ || parseDebugFlag(process.env.AI_ASSISTANT_WORKER_DEBUG);
20642
+ }
20643
+ function notifyAiTerminalCollectionUpdate(collection, documentId) {
20644
+ return __awaiter(this, void 0, void 0, function () {
20645
+ var subManager, _a;
20646
+ var _b, _c;
20647
+ return __generator(this, function (_d) {
20648
+ switch (_d.label) {
20649
+ case 0:
20650
+ try {
20651
+ resolveio_server_app_1.ResolveIOServer.getMongoManager().invalidateQueryCache(collection, true);
20652
+ }
20653
+ catch (_e) { }
20654
+ _d.label = 1;
20655
+ case 1:
20656
+ _d.trys.push([1, 4, , 5]);
20657
+ subManager = (_c = (_b = resolveio_server_app_1.ResolveIOServer.getMainServer()) === null || _b === void 0 ? void 0 : _b.getSubscriptionManager) === null || _c === void 0 ? void 0 : _c.call(_b);
20658
+ if (!(subManager && typeof subManager.invalidatePubsCache === 'function')) return [3 /*break*/, 3];
20659
+ return [4 /*yield*/, subManager.invalidatePubsCache(collection, 'update', documentId)];
20660
+ case 2:
20661
+ _d.sent();
20662
+ _d.label = 3;
20663
+ case 3: return [3 /*break*/, 5];
20664
+ case 4:
20665
+ _a = _d.sent();
20565
20666
  return [3 /*break*/, 5];
20566
20667
  case 5: return [2 /*return*/];
20567
20668
  }
20568
20669
  });
20569
20670
  });
20570
20671
  }
20672
+ function sanitizeAssistantMessageSetPayload(setPayload) {
20673
+ var next = __assign({}, setPayload);
20674
+ if (next.metadata && typeof next.metadata === 'object') {
20675
+ next.metadata = sanitizeMongoSafeObject(next.metadata);
20676
+ }
20677
+ return next;
20678
+ }
20679
+ function sanitizeMongoSafeObject(value) {
20680
+ var e_57, _a;
20681
+ if (value === null || value === undefined) {
20682
+ return value;
20683
+ }
20684
+ if (Array.isArray(value)) {
20685
+ return value.map(function (entry) { return sanitizeMongoSafeObject(entry); });
20686
+ }
20687
+ if (value instanceof Date) {
20688
+ return value;
20689
+ }
20690
+ if (isMongoObjectId(value)) {
20691
+ return value;
20692
+ }
20693
+ if (!isPlainObject(value)) {
20694
+ return value;
20695
+ }
20696
+ var out = {};
20697
+ try {
20698
+ for (var _b = __values(Object.entries(value)), _c = _b.next(); !_c.done; _c = _b.next()) {
20699
+ var _d = __read(_c.value, 2), rawKey = _d[0], rawValue = _d[1];
20700
+ var key = sanitizeMongoKey(rawKey);
20701
+ out[key] = sanitizeMongoSafeObject(rawValue);
20702
+ }
20703
+ }
20704
+ catch (e_57_1) { e_57 = { error: e_57_1 }; }
20705
+ finally {
20706
+ try {
20707
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
20708
+ }
20709
+ finally { if (e_57) throw e_57.error; }
20710
+ }
20711
+ return out;
20712
+ }
20713
+ function sanitizeMongoKey(rawKey) {
20714
+ var key = String(rawKey || '');
20715
+ if (!key) {
20716
+ return '_';
20717
+ }
20718
+ var sanitized = key.replace(/\./g, '__dot__');
20719
+ if (sanitized.startsWith('$')) {
20720
+ sanitized = "_dollar_".concat(sanitized.slice(1));
20721
+ }
20722
+ return sanitized || '_';
20723
+ }
20571
20724
  function normalizeOptionalString(value) {
20572
20725
  var raw = typeof value === 'string' ? value.trim() : '';
20573
20726
  return raw || '';
@@ -20654,7 +20807,7 @@ function estimateUsage(messages, responseText, model) {
20654
20807
  };
20655
20808
  }
20656
20809
  function evaluateGuardrails(message) {
20657
- var e_57, _a;
20810
+ var e_58, _a;
20658
20811
  var normalized = String(message || '').toLowerCase();
20659
20812
  var identityGuardrail = evaluateAssistantIdentityDisclosureGuardrail(normalized);
20660
20813
  if (identityGuardrail === null || identityGuardrail === void 0 ? void 0 : identityGuardrail.blocked) {
@@ -20680,12 +20833,12 @@ function evaluateGuardrails(message) {
20680
20833
  }
20681
20834
  }
20682
20835
  }
20683
- catch (e_57_1) { e_57 = { error: e_57_1 }; }
20836
+ catch (e_58_1) { e_58 = { error: e_58_1 }; }
20684
20837
  finally {
20685
20838
  try {
20686
20839
  if (patterns_3_1 && !patterns_3_1.done && (_a = patterns_3.return)) _a.call(patterns_3);
20687
20840
  }
20688
- finally { if (e_57) throw e_57.error; }
20841
+ finally { if (e_58) throw e_58.error; }
20689
20842
  }
20690
20843
  return null;
20691
20844
  }