@resolveio/server-lib 22.3.17 → 22.3.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (674) hide show
  1. package/.nodemon.json +5 -0
  2. package/.vscode/settings.json +21 -0
  3. package/AGENTS.md +189 -0
  4. package/README.md +22 -0
  5. package/build_package.sh +5 -0
  6. package/compileDTS.pl +64 -0
  7. package/docs/ai-assistant-nightly-eval.md +65 -0
  8. package/docs/ai-assistant-preflight-checklist.md +23 -0
  9. package/docs/ai-assistant-report-builder-bridge-playbook.md +115 -0
  10. package/eslint-plugin-custom/index.js +7 -0
  11. package/eslint-plugin-custom/rules/no-filter-zero-index.js +44 -0
  12. package/eslint.config.js +103 -0
  13. package/gulpfile.js +216 -0
  14. package/methodAndPublicationListGenerator.py +344 -0
  15. package/mongodbensurers.js +2 -0
  16. package/mongostop.js +3 -0
  17. package/package.json +1 -1
  18. package/scripts/cleanup-bypassed-callmethod-logs.js +616 -0
  19. package/settings.development.json +25 -0
  20. package/settings.development.redacted.json +25 -0
  21. package/src/.env +12 -0
  22. package/src/ai/assistant-core-heuristics.ts +379 -0
  23. package/src/ai/resolveio-platform-intelligence-memory-corpus.ts +185 -0
  24. package/src/ai/resolveio-platform-intelligence-memory.ts +325 -0
  25. package/{ai/resolveio-platform-intelligence-types.d.ts → src/ai/resolveio-platform-intelligence-types.ts} +20 -15
  26. package/src/ai/resolveio-platform-intelligence.ts +462 -0
  27. package/src/client-server-app.ts +12 -0
  28. package/src/collections/ai-terminal-conversation.collection.ts +91 -0
  29. package/src/collections/ai-terminal-issue-report.collection.ts +99 -0
  30. package/src/collections/ai-terminal-message.collection.ts +77 -0
  31. package/src/collections/app-setting.collection.ts +104 -0
  32. package/src/collections/app-status.collection.ts +58 -0
  33. package/src/collections/communication-metric.collection.ts +84 -0
  34. package/src/collections/counter.collection.ts +56 -0
  35. package/src/collections/cron-job-history.collection.ts +94 -0
  36. package/src/collections/cron-job.collection.ts +92 -0
  37. package/src/collections/customer-notification.collection.ts +131 -0
  38. package/src/collections/customer-portal-password.collection.ts +76 -0
  39. package/src/collections/email-history.collection.ts +127 -0
  40. package/src/collections/email-verified.collection.ts +62 -0
  41. package/src/collections/file.collection.ts +74 -0
  42. package/src/collections/flag-update.collection.ts +57 -0
  43. package/src/collections/flag.collection.ts +57 -0
  44. package/src/collections/log-method-latency.collection.ts +77 -0
  45. package/src/collections/log-subscription.collection.ts +80 -0
  46. package/src/collections/log.collection.ts +93 -0
  47. package/src/collections/logged-in-users.collection.ts +67 -0
  48. package/src/collections/monitor-cpu.collection.ts +65 -0
  49. package/src/collections/monitor-function.collection.ts +74 -0
  50. package/src/collections/monitor-memory.collection.ts +77 -0
  51. package/src/collections/monitor-mongo.collection.ts +71 -0
  52. package/src/collections/notification.collection.ts +57 -0
  53. package/src/collections/openai-usage-ledger.collection.ts +77 -0
  54. package/src/collections/report-builder-dashboard-builder.collection.ts +109 -0
  55. package/src/collections/report-builder-library.collection.ts +89 -0
  56. package/src/collections/report-builder-report.collection.ts +184 -0
  57. package/src/collections/user-group.collection.ts +89 -0
  58. package/src/collections/user-guide.collection.ts +57 -0
  59. package/src/collections/user.collection.ts +181 -0
  60. package/src/cron/cron.ts +117 -0
  61. package/src/fixtures/cron-jobs.ts +95 -0
  62. package/src/fixtures/init.ts +35 -0
  63. package/src/http/auth.ts +764 -0
  64. package/src/http/health.ts +7 -0
  65. package/src/http/home.ts +90 -0
  66. package/src/http/slow-query-publication.ts +49 -0
  67. package/src/index.ts +1 -0
  68. package/src/managers/ai-assistant-codex-manager.manager.ts +1131 -0
  69. package/src/managers/communication-metric.manager.ts +82 -0
  70. package/src/managers/cron.manager.ts +333 -0
  71. package/src/managers/customer-notification-content.manager.ts +236 -0
  72. package/src/managers/diagnostic-manager-bootstrap.ts +165 -0
  73. package/src/managers/error-auto-fix.manager.ts +2767 -0
  74. package/src/managers/local-log.manager.ts +113 -0
  75. package/src/managers/method.manager.ts +1827 -0
  76. package/src/managers/mongo.manager.ts +4575 -0
  77. package/src/managers/monitor.manager.ts +507 -0
  78. package/src/managers/openai-usage-ledger.manager.ts +116 -0
  79. package/src/managers/slow-query-verifier.manager.ts +3590 -0
  80. package/src/managers/slow-query.manager.ts +519 -0
  81. package/src/managers/subscription.manager.ts +3128 -0
  82. package/src/managers/websocket.manager.ts +746 -0
  83. package/src/managers/worker-dispatcher.manager.ts +1360 -0
  84. package/src/managers/worker-server.manager.ts +536 -0
  85. package/src/methods/accounts.ts +532 -0
  86. package/src/methods/ai-terminal.ts +22943 -0
  87. package/src/methods/app-settings.ts +114 -0
  88. package/src/methods/aws.ts +649 -0
  89. package/src/methods/collections.ts +641 -0
  90. package/src/methods/counters.ts +69 -0
  91. package/src/methods/cron-jobs.ts +2614 -0
  92. package/src/methods/customer-notifications.ts +458 -0
  93. package/src/methods/diagnostics.ts +616 -0
  94. package/src/methods/flag-updates.ts +7 -0
  95. package/src/methods/flags.ts +7 -0
  96. package/src/methods/logs.ts +657 -0
  97. package/src/methods/mongo-explorer.ts +1880 -0
  98. package/src/methods/monitor.ts +540 -0
  99. package/src/methods/pdf.ts +1236 -0
  100. package/src/methods/publications.ts +129 -0
  101. package/src/methods/report-builder.ts +3300 -0
  102. package/src/methods/support.ts +335 -0
  103. package/src/models/ai-terminal-conversation.model.ts +19 -0
  104. package/src/models/ai-terminal-issue-report.model.ts +21 -0
  105. package/src/models/ai-terminal-message.model.ts +24 -0
  106. package/src/models/app-setting.model.ts +17 -0
  107. package/{models/app-status.model.d.ts → src/models/app-status.model.ts} +3 -2
  108. package/{models/billing-logged-in-users.model.d.ts → src/models/billing-logged-in-users.model.ts} +5 -4
  109. package/src/models/collection-document.model.ts +24 -0
  110. package/src/models/communication-metric.model.ts +23 -0
  111. package/{models/counter.model.d.ts → src/models/counter.model.ts} +4 -3
  112. package/src/models/cron-job-history.model.ts +16 -0
  113. package/src/models/cron-job.model.ts +15 -0
  114. package/src/models/customer-notification.model.ts +28 -0
  115. package/src/models/customer-portal-password.model.ts +12 -0
  116. package/src/models/dialog.model.ts +25 -0
  117. package/{models/email-history.model.js → src/models/email-history.model.ts} +35 -4
  118. package/{models/email-verified.model.d.ts → src/models/email-verified.model.ts} +6 -5
  119. package/{models/file.model.d.ts → src/models/file.model.ts} +8 -7
  120. package/{models/flag-update.model.d.ts → src/models/flag-update.model.ts} +4 -3
  121. package/{models/flag.model.d.ts → src/models/flag.model.ts} +4 -3
  122. package/src/models/log-method-latency.model.ts +11 -0
  123. package/{models/log-subscription.model.d.ts → src/models/log-subscription.model.ts} +11 -9
  124. package/src/models/log.model.ts +19 -0
  125. package/{models/logged-in-users.model.d.ts → src/models/logged-in-users.model.ts} +6 -5
  126. package/{models/method-response.model.d.ts → src/models/method-response.model.ts} +7 -6
  127. package/src/models/method.model.ts +25 -0
  128. package/{models/monitor-cpu.model.d.ts → src/models/monitor-cpu.model.ts} +9 -7
  129. package/src/models/monitor-function.model.ts +16 -0
  130. package/src/models/monitor-memory.model.ts +17 -0
  131. package/src/models/monitor-mongo.model.ts +15 -0
  132. package/{models/notification.model.d.ts → src/models/notification.model.ts} +6 -4
  133. package/src/models/openai-usage-ledger.model.ts +16 -0
  134. package/src/models/pagination.model.ts +35 -0
  135. package/src/models/permission.model.ts +14 -0
  136. package/src/models/report-builder-dashboard-builder.model.ts +29 -0
  137. package/src/models/report-builder-library.model.ts +20 -0
  138. package/src/models/report-builder-report.model.ts +136 -0
  139. package/src/models/report-builder.model.ts +68 -0
  140. package/src/models/select-data-label.model.ts +9 -0
  141. package/src/models/server-message.model.ts +31 -0
  142. package/src/models/slow-query-report.model.ts +23 -0
  143. package/src/models/subscription.model.ts +73 -0
  144. package/src/models/support-ticket.model.ts +104 -0
  145. package/src/models/user-group.model.ts +24 -0
  146. package/{models/user-guide.model.d.ts → src/models/user-guide.model.ts} +5 -4
  147. package/src/models/user.model.ts +96 -0
  148. package/src/private/images/ResolveIO.png +0 -0
  149. package/src/publications/ai-terminal.ts +73 -0
  150. package/src/publications/app-settings.ts +25 -0
  151. package/src/publications/app-status.ts +13 -0
  152. package/src/publications/cron-jobs.ts +40 -0
  153. package/src/publications/customer-notifications.ts +101 -0
  154. package/src/publications/files.ts +33 -0
  155. package/src/publications/flags-update.ts +19 -0
  156. package/src/publications/flags.ts +19 -0
  157. package/src/publications/logs.ts +163 -0
  158. package/src/publications/notifications.ts +13 -0
  159. package/src/publications/report-builder-dashboard-builders.ts +39 -0
  160. package/src/publications/report-builder-libraries.ts +41 -0
  161. package/src/publications/report-builder-reports.ts +47 -0
  162. package/src/publications/super-admin.ts +13 -0
  163. package/src/publications/user-groups.ts +12 -0
  164. package/src/publications/user-guides.ts +12 -0
  165. package/src/resolveio-server-app.ts +617 -0
  166. package/src/server-app.ts +3354 -0
  167. package/src/services/codex-client.ts +1223 -0
  168. package/src/services/openai-client.ts +265 -0
  169. package/src/types/error-report.ts +26 -0
  170. package/src/types/js-tiktoken.d.ts +11 -0
  171. package/src/types/slow-query-report.ts +28 -0
  172. package/src/util/ai-qa-policy.ts +654 -0
  173. package/src/util/common.ts +649 -0
  174. package/src/util/customer-portal-password.ts +183 -0
  175. package/src/util/error-reporter.ts +332 -0
  176. package/src/util/error-tracking.ts +79 -0
  177. package/src/util/report-builder-unwinds.ts +180 -0
  178. package/src/util/schema-report-builder.ts +448 -0
  179. package/src/util/slow-query-reporter.ts +216 -0
  180. package/src/util/subscription-dependency-context.ts +1096 -0
  181. package/src/util/tokenizer.ts +38 -0
  182. package/src/workers/codex-runner.worker.ts +142 -0
  183. package/start_server.sh +5 -0
  184. package/tests/ai-assistant-corpus-build.ts +484 -0
  185. package/tests/ai-assistant-corpus-replay-e2e.ts +774 -0
  186. package/tests/ai-assistant-data-parity-e2e.ts +1989 -0
  187. package/tests/ai-assistant-eval-triage.ts +831 -0
  188. package/tests/ai-assistant-openai-e2e.ts +1061 -0
  189. package/tests/ai-assistant-openai-git-e2e.ts +155 -0
  190. package/tests/ai-assistant-preflight-matrix.ts +215 -0
  191. package/tests/ai-assistant-routing-eval.test.ts +560 -0
  192. package/tests/ai-assistant-snf-live-eval.ts +975 -0
  193. package/tests/ai-assistant-utils.test.ts +2860 -0
  194. package/tests/ai-runner-contract.test.ts +60 -0
  195. package/tests/error-reporter.test.ts +145 -0
  196. package/tests/report-builder-linking.test.ts +79 -0
  197. package/tests/resolveio-platform-intelligence.test.ts +352 -0
  198. package/tests/server-app-cron-owner.test.ts +127 -0
  199. package/tests/subscription-connect-race.test.ts +158 -0
  200. package/tests/subscription-dependency-context.test.ts +324 -0
  201. package/tests/subscription-manager-collection-tracking.test.ts +86 -0
  202. package/tests/subscription-manager-invalidation.test.ts +86 -0
  203. package/tsconfig.json +34 -0
  204. package/ai/assistant-core-heuristics.d.ts +0 -11
  205. package/ai/assistant-core-heuristics.js +0 -356
  206. package/ai/assistant-core-heuristics.js.map +0 -1
  207. package/ai/resolveio-platform-intelligence-memory-corpus.d.ts +0 -3
  208. package/ai/resolveio-platform-intelligence-memory-corpus.js +0 -214
  209. package/ai/resolveio-platform-intelligence-memory-corpus.js.map +0 -1
  210. package/ai/resolveio-platform-intelligence-memory.d.ts +0 -20
  211. package/ai/resolveio-platform-intelligence-memory.js +0 -341
  212. package/ai/resolveio-platform-intelligence-memory.js.map +0 -1
  213. package/ai/resolveio-platform-intelligence-types.js +0 -4
  214. package/ai/resolveio-platform-intelligence-types.js.map +0 -1
  215. package/ai/resolveio-platform-intelligence.d.ts +0 -6
  216. package/ai/resolveio-platform-intelligence.js +0 -463
  217. package/ai/resolveio-platform-intelligence.js.map +0 -1
  218. package/client-server-app.d.ts +0 -1
  219. package/client-server-app.js +0 -68
  220. package/client-server-app.js.map +0 -1
  221. package/collections/ai-terminal-conversation.collection.d.ts +0 -2
  222. package/collections/ai-terminal-conversation.collection.js +0 -140
  223. package/collections/ai-terminal-conversation.collection.js.map +0 -1
  224. package/collections/ai-terminal-issue-report.collection.d.ts +0 -2
  225. package/collections/ai-terminal-issue-report.collection.js +0 -148
  226. package/collections/ai-terminal-issue-report.collection.js.map +0 -1
  227. package/collections/ai-terminal-message.collection.d.ts +0 -2
  228. package/collections/ai-terminal-message.collection.js +0 -121
  229. package/collections/ai-terminal-message.collection.js.map +0 -1
  230. package/collections/app-setting.collection.d.ts +0 -3
  231. package/collections/app-setting.collection.js +0 -103
  232. package/collections/app-setting.collection.js.map +0 -1
  233. package/collections/app-status.collection.d.ts +0 -3
  234. package/collections/app-status.collection.js +0 -57
  235. package/collections/app-status.collection.js.map +0 -1
  236. package/collections/communication-metric.collection.d.ts +0 -2
  237. package/collections/communication-metric.collection.js +0 -133
  238. package/collections/communication-metric.collection.js.map +0 -1
  239. package/collections/counter.collection.d.ts +0 -3
  240. package/collections/counter.collection.js +0 -56
  241. package/collections/counter.collection.js.map +0 -1
  242. package/collections/cron-job-history.collection.d.ts +0 -3
  243. package/collections/cron-job-history.collection.js +0 -137
  244. package/collections/cron-job-history.collection.js.map +0 -1
  245. package/collections/cron-job.collection.d.ts +0 -3
  246. package/collections/cron-job.collection.js +0 -92
  247. package/collections/cron-job.collection.js.map +0 -1
  248. package/collections/customer-notification.collection.d.ts +0 -3
  249. package/collections/customer-notification.collection.js +0 -130
  250. package/collections/customer-notification.collection.js.map +0 -1
  251. package/collections/customer-portal-password.collection.d.ts +0 -3
  252. package/collections/customer-portal-password.collection.js +0 -75
  253. package/collections/customer-portal-password.collection.js.map +0 -1
  254. package/collections/email-history.collection.d.ts +0 -3
  255. package/collections/email-history.collection.js +0 -127
  256. package/collections/email-history.collection.js.map +0 -1
  257. package/collections/email-verified.collection.d.ts +0 -3
  258. package/collections/email-verified.collection.js +0 -62
  259. package/collections/email-verified.collection.js.map +0 -1
  260. package/collections/file.collection.d.ts +0 -3
  261. package/collections/file.collection.js +0 -74
  262. package/collections/file.collection.js.map +0 -1
  263. package/collections/flag-update.collection.d.ts +0 -3
  264. package/collections/flag-update.collection.js +0 -57
  265. package/collections/flag-update.collection.js.map +0 -1
  266. package/collections/flag.collection.d.ts +0 -3
  267. package/collections/flag.collection.js +0 -57
  268. package/collections/flag.collection.js.map +0 -1
  269. package/collections/log-method-latency.collection.d.ts +0 -3
  270. package/collections/log-method-latency.collection.js +0 -77
  271. package/collections/log-method-latency.collection.js.map +0 -1
  272. package/collections/log-subscription.collection.d.ts +0 -3
  273. package/collections/log-subscription.collection.js +0 -80
  274. package/collections/log-subscription.collection.js.map +0 -1
  275. package/collections/log.collection.d.ts +0 -3
  276. package/collections/log.collection.js +0 -93
  277. package/collections/log.collection.js.map +0 -1
  278. package/collections/logged-in-users.collection.d.ts +0 -3
  279. package/collections/logged-in-users.collection.js +0 -67
  280. package/collections/logged-in-users.collection.js.map +0 -1
  281. package/collections/monitor-cpu.collection.d.ts +0 -3
  282. package/collections/monitor-cpu.collection.js +0 -65
  283. package/collections/monitor-cpu.collection.js.map +0 -1
  284. package/collections/monitor-function.collection.d.ts +0 -3
  285. package/collections/monitor-function.collection.js +0 -74
  286. package/collections/monitor-function.collection.js.map +0 -1
  287. package/collections/monitor-memory.collection.d.ts +0 -3
  288. package/collections/monitor-memory.collection.js +0 -77
  289. package/collections/monitor-memory.collection.js.map +0 -1
  290. package/collections/monitor-mongo.collection.d.ts +0 -3
  291. package/collections/monitor-mongo.collection.js +0 -71
  292. package/collections/monitor-mongo.collection.js.map +0 -1
  293. package/collections/notification.collection.d.ts +0 -3
  294. package/collections/notification.collection.js +0 -57
  295. package/collections/notification.collection.js.map +0 -1
  296. package/collections/openai-usage-ledger.collection.d.ts +0 -2
  297. package/collections/openai-usage-ledger.collection.js +0 -124
  298. package/collections/openai-usage-ledger.collection.js.map +0 -1
  299. package/collections/report-builder-dashboard-builder.collection.d.ts +0 -3
  300. package/collections/report-builder-dashboard-builder.collection.js +0 -109
  301. package/collections/report-builder-dashboard-builder.collection.js.map +0 -1
  302. package/collections/report-builder-library.collection.d.ts +0 -3
  303. package/collections/report-builder-library.collection.js +0 -87
  304. package/collections/report-builder-library.collection.js.map +0 -1
  305. package/collections/report-builder-report.collection.d.ts +0 -4
  306. package/collections/report-builder-report.collection.js +0 -184
  307. package/collections/report-builder-report.collection.js.map +0 -1
  308. package/collections/user-group.collection.d.ts +0 -4
  309. package/collections/user-group.collection.js +0 -89
  310. package/collections/user-group.collection.js.map +0 -1
  311. package/collections/user-guide.collection.d.ts +0 -3
  312. package/collections/user-guide.collection.js +0 -57
  313. package/collections/user-guide.collection.js.map +0 -1
  314. package/collections/user.collection.d.ts +0 -4
  315. package/collections/user.collection.js +0 -180
  316. package/collections/user.collection.js.map +0 -1
  317. package/cron/cron.d.ts +0 -14
  318. package/cron/cron.js +0 -216
  319. package/cron/cron.js.map +0 -1
  320. package/fixtures/cron-jobs.d.ts +0 -1
  321. package/fixtures/cron-jobs.js +0 -150
  322. package/fixtures/cron-jobs.js.map +0 -1
  323. package/fixtures/init.d.ts +0 -1
  324. package/fixtures/init.js +0 -91
  325. package/fixtures/init.js.map +0 -1
  326. package/http/auth.d.ts +0 -2
  327. package/http/auth.js +0 -906
  328. package/http/auth.js.map +0 -1
  329. package/http/health.d.ts +0 -1
  330. package/http/health.js +0 -11
  331. package/http/health.js.map +0 -1
  332. package/http/home.d.ts +0 -1
  333. package/http/home.js +0 -134
  334. package/http/home.js.map +0 -1
  335. package/http/slow-query-publication.d.ts +0 -2
  336. package/http/slow-query-publication.js +0 -99
  337. package/http/slow-query-publication.js.map +0 -1
  338. package/index.d.ts +0 -1
  339. package/index.js +0 -19
  340. package/index.js.map +0 -1
  341. package/managers/ai-assistant-codex-manager.manager.d.ts +0 -67
  342. package/managers/ai-assistant-codex-manager.manager.js +0 -1113
  343. package/managers/ai-assistant-codex-manager.manager.js.map +0 -1
  344. package/managers/communication-metric.manager.d.ts +0 -16
  345. package/managers/communication-metric.manager.js +0 -134
  346. package/managers/communication-metric.manager.js.map +0 -1
  347. package/managers/cron.manager.d.ts +0 -20
  348. package/managers/cron.manager.js +0 -534
  349. package/managers/cron.manager.js.map +0 -1
  350. package/managers/customer-notification-content.manager.d.ts +0 -55
  351. package/managers/customer-notification-content.manager.js +0 -158
  352. package/managers/customer-notification-content.manager.js.map +0 -1
  353. package/managers/diagnostic-manager-bootstrap.d.ts +0 -9
  354. package/managers/diagnostic-manager-bootstrap.js +0 -260
  355. package/managers/diagnostic-manager-bootstrap.js.map +0 -1
  356. package/managers/error-auto-fix.manager.d.ts +0 -149
  357. package/managers/error-auto-fix.manager.js +0 -3064
  358. package/managers/error-auto-fix.manager.js.map +0 -1
  359. package/managers/local-log.manager.d.ts +0 -18
  360. package/managers/local-log.manager.js +0 -88
  361. package/managers/local-log.manager.js.map +0 -1
  362. package/managers/method.manager.d.ts +0 -83
  363. package/managers/method.manager.js +0 -1941
  364. package/managers/method.manager.js.map +0 -1
  365. package/managers/mongo.manager.d.ts +0 -224
  366. package/managers/mongo.manager.js +0 -5000
  367. package/managers/mongo.manager.js.map +0 -1
  368. package/managers/monitor.manager.d.ts +0 -70
  369. package/managers/monitor.manager.js +0 -550
  370. package/managers/monitor.manager.js.map +0 -1
  371. package/managers/openai-usage-ledger.manager.d.ts +0 -15
  372. package/managers/openai-usage-ledger.manager.js +0 -144
  373. package/managers/openai-usage-ledger.manager.js.map +0 -1
  374. package/managers/slow-query-verifier.manager.d.ts +0 -144
  375. package/managers/slow-query-verifier.manager.js +0 -3857
  376. package/managers/slow-query-verifier.manager.js.map +0 -1
  377. package/managers/slow-query.manager.d.ts +0 -28
  378. package/managers/slow-query.manager.js +0 -468
  379. package/managers/slow-query.manager.js.map +0 -1
  380. package/managers/subscription.manager.d.ts +0 -169
  381. package/managers/subscription.manager.js +0 -3434
  382. package/managers/subscription.manager.js.map +0 -1
  383. package/managers/websocket.manager.d.ts +0 -73
  384. package/managers/websocket.manager.js +0 -673
  385. package/managers/websocket.manager.js.map +0 -1
  386. package/managers/worker-dispatcher.manager.d.ts +0 -120
  387. package/managers/worker-dispatcher.manager.js +0 -1266
  388. package/managers/worker-dispatcher.manager.js.map +0 -1
  389. package/managers/worker-server.manager.d.ts +0 -35
  390. package/managers/worker-server.manager.js +0 -582
  391. package/managers/worker-server.manager.js.map +0 -1
  392. package/methods/accounts.d.ts +0 -2
  393. package/methods/accounts.js +0 -624
  394. package/methods/accounts.js.map +0 -1
  395. package/methods/ai-terminal.d.ts +0 -322
  396. package/methods/ai-terminal.js +0 -22699
  397. package/methods/ai-terminal.js.map +0 -1
  398. package/methods/app-settings.d.ts +0 -2
  399. package/methods/app-settings.js +0 -169
  400. package/methods/app-settings.js.map +0 -1
  401. package/methods/aws.d.ts +0 -2
  402. package/methods/aws.js +0 -877
  403. package/methods/aws.js.map +0 -1
  404. package/methods/collections.d.ts +0 -2
  405. package/methods/collections.js +0 -719
  406. package/methods/collections.js.map +0 -1
  407. package/methods/counters.d.ts +0 -2
  408. package/methods/counters.js +0 -113
  409. package/methods/counters.js.map +0 -1
  410. package/methods/cron-jobs.d.ts +0 -2
  411. package/methods/cron-jobs.js +0 -2475
  412. package/methods/cron-jobs.js.map +0 -1
  413. package/methods/customer-notifications.d.ts +0 -2
  414. package/methods/customer-notifications.js +0 -528
  415. package/methods/customer-notifications.js.map +0 -1
  416. package/methods/diagnostics.d.ts +0 -2
  417. package/methods/diagnostics.js +0 -703
  418. package/methods/diagnostics.js.map +0 -1
  419. package/methods/flag-updates.d.ts +0 -2
  420. package/methods/flag-updates.js +0 -8
  421. package/methods/flag-updates.js.map +0 -1
  422. package/methods/flags.d.ts +0 -2
  423. package/methods/flags.js +0 -8
  424. package/methods/flags.js.map +0 -1
  425. package/methods/logs.d.ts +0 -2
  426. package/methods/logs.js +0 -751
  427. package/methods/logs.js.map +0 -1
  428. package/methods/mongo-explorer.d.ts +0 -2
  429. package/methods/mongo-explorer.js +0 -1808
  430. package/methods/mongo-explorer.js.map +0 -1
  431. package/methods/monitor.d.ts +0 -2
  432. package/methods/monitor.js +0 -543
  433. package/methods/monitor.js.map +0 -1
  434. package/methods/pdf.d.ts +0 -2
  435. package/methods/pdf.js +0 -1216
  436. package/methods/pdf.js.map +0 -1
  437. package/methods/publications.d.ts +0 -1
  438. package/methods/publications.js +0 -183
  439. package/methods/publications.js.map +0 -1
  440. package/methods/report-builder.d.ts +0 -2
  441. package/methods/report-builder.js +0 -3094
  442. package/methods/report-builder.js.map +0 -1
  443. package/methods/support.d.ts +0 -2
  444. package/methods/support.js +0 -430
  445. package/methods/support.js.map +0 -1
  446. package/models/ai-terminal-conversation.model.d.ts +0 -17
  447. package/models/ai-terminal-conversation.model.js +0 -4
  448. package/models/ai-terminal-conversation.model.js.map +0 -1
  449. package/models/ai-terminal-issue-report.model.d.ts +0 -19
  450. package/models/ai-terminal-issue-report.model.js +0 -4
  451. package/models/ai-terminal-issue-report.model.js.map +0 -1
  452. package/models/ai-terminal-message.model.d.ts +0 -22
  453. package/models/ai-terminal-message.model.js +0 -4
  454. package/models/ai-terminal-message.model.js.map +0 -1
  455. package/models/app-setting.model.d.ts +0 -16
  456. package/models/app-setting.model.js +0 -4
  457. package/models/app-setting.model.js.map +0 -1
  458. package/models/app-status.model.js +0 -4
  459. package/models/app-status.model.js.map +0 -1
  460. package/models/billing-logged-in-users.model.js +0 -4
  461. package/models/billing-logged-in-users.model.js.map +0 -1
  462. package/models/collection-document.model.d.ts +0 -21
  463. package/models/collection-document.model.js +0 -4
  464. package/models/collection-document.model.js.map +0 -1
  465. package/models/communication-metric.model.d.ts +0 -20
  466. package/models/communication-metric.model.js +0 -4
  467. package/models/communication-metric.model.js.map +0 -1
  468. package/models/counter.model.js +0 -4
  469. package/models/counter.model.js.map +0 -1
  470. package/models/cron-job-history.model.d.ts +0 -15
  471. package/models/cron-job-history.model.js +0 -4
  472. package/models/cron-job-history.model.js.map +0 -1
  473. package/models/cron-job.model.d.ts +0 -14
  474. package/models/cron-job.model.js +0 -4
  475. package/models/cron-job.model.js.map +0 -1
  476. package/models/customer-notification.model.d.ts +0 -26
  477. package/models/customer-notification.model.js +0 -4
  478. package/models/customer-notification.model.js.map +0 -1
  479. package/models/customer-portal-password.model.d.ts +0 -11
  480. package/models/customer-portal-password.model.js +0 -4
  481. package/models/customer-portal-password.model.js.map +0 -1
  482. package/models/dialog.model.d.ts +0 -23
  483. package/models/dialog.model.js +0 -4
  484. package/models/dialog.model.js.map +0 -1
  485. package/models/email-history.model.d.ts +0 -31
  486. package/models/email-history.model.js.map +0 -1
  487. package/models/email-verified.model.js +0 -4
  488. package/models/email-verified.model.js.map +0 -1
  489. package/models/file.model.js +0 -4
  490. package/models/file.model.js.map +0 -1
  491. package/models/flag-update.model.js +0 -4
  492. package/models/flag-update.model.js.map +0 -1
  493. package/models/flag.model.js +0 -4
  494. package/models/flag.model.js.map +0 -1
  495. package/models/log-method-latency.model.d.ts +0 -10
  496. package/models/log-method-latency.model.js +0 -4
  497. package/models/log-method-latency.model.js.map +0 -1
  498. package/models/log-subscription.model.js +0 -4
  499. package/models/log-subscription.model.js.map +0 -1
  500. package/models/log.model.d.ts +0 -17
  501. package/models/log.model.js +0 -4
  502. package/models/log.model.js.map +0 -1
  503. package/models/logged-in-users.model.js +0 -4
  504. package/models/logged-in-users.model.js.map +0 -1
  505. package/models/method-response.model.js +0 -4
  506. package/models/method-response.model.js.map +0 -1
  507. package/models/method.model.d.ts +0 -26
  508. package/models/method.model.js +0 -4
  509. package/models/method.model.js.map +0 -1
  510. package/models/monitor-cpu.model.js +0 -4
  511. package/models/monitor-cpu.model.js.map +0 -1
  512. package/models/monitor-function.model.d.ts +0 -14
  513. package/models/monitor-function.model.js +0 -4
  514. package/models/monitor-function.model.js.map +0 -1
  515. package/models/monitor-memory.model.d.ts +0 -15
  516. package/models/monitor-memory.model.js +0 -4
  517. package/models/monitor-memory.model.js.map +0 -1
  518. package/models/monitor-mongo.model.d.ts +0 -13
  519. package/models/monitor-mongo.model.js +0 -4
  520. package/models/monitor-mongo.model.js.map +0 -1
  521. package/models/notification.model.js +0 -4
  522. package/models/notification.model.js.map +0 -1
  523. package/models/openai-usage-ledger.model.d.ts +0 -15
  524. package/models/openai-usage-ledger.model.js +0 -4
  525. package/models/openai-usage-ledger.model.js.map +0 -1
  526. package/models/pagination.model.d.ts +0 -11
  527. package/models/pagination.model.js +0 -28
  528. package/models/pagination.model.js.map +0 -1
  529. package/models/permission.model.d.ts +0 -12
  530. package/models/permission.model.js +0 -4
  531. package/models/permission.model.js.map +0 -1
  532. package/models/report-builder-dashboard-builder.model.d.ts +0 -25
  533. package/models/report-builder-dashboard-builder.model.js +0 -4
  534. package/models/report-builder-dashboard-builder.model.js.map +0 -1
  535. package/models/report-builder-library.model.d.ts +0 -17
  536. package/models/report-builder-library.model.js +0 -4
  537. package/models/report-builder-library.model.js.map +0 -1
  538. package/models/report-builder-report.model.d.ts +0 -121
  539. package/models/report-builder-report.model.js +0 -4
  540. package/models/report-builder-report.model.js.map +0 -1
  541. package/models/report-builder.model.d.ts +0 -61
  542. package/models/report-builder.model.js +0 -4
  543. package/models/report-builder.model.js.map +0 -1
  544. package/models/select-data-label.model.d.ts +0 -9
  545. package/models/select-data-label.model.js +0 -4
  546. package/models/select-data-label.model.js.map +0 -1
  547. package/models/server-message.model.d.ts +0 -32
  548. package/models/server-message.model.js +0 -4
  549. package/models/server-message.model.js.map +0 -1
  550. package/models/slow-query-report.model.d.ts +0 -23
  551. package/models/slow-query-report.model.js +0 -4
  552. package/models/slow-query-report.model.js.map +0 -1
  553. package/models/subscription.model.d.ts +0 -31
  554. package/models/subscription.model.js +0 -4
  555. package/models/subscription.model.js.map +0 -1
  556. package/models/support-ticket.model.d.ts +0 -87
  557. package/models/support-ticket.model.js +0 -4
  558. package/models/support-ticket.model.js.map +0 -1
  559. package/models/user-group.model.d.ts +0 -20
  560. package/models/user-group.model.js +0 -4
  561. package/models/user-group.model.js.map +0 -1
  562. package/models/user-guide.model.js +0 -4
  563. package/models/user-guide.model.js.map +0 -1
  564. package/models/user.model.d.ts +0 -84
  565. package/models/user.model.js +0 -4
  566. package/models/user.model.js.map +0 -1
  567. package/private/images/ResolveIO.png +0 -0
  568. package/public_api.js +0 -112
  569. package/public_api.js.map +0 -1
  570. package/publications/ai-terminal.d.ts +0 -1
  571. package/publications/ai-terminal.js +0 -122
  572. package/publications/ai-terminal.js.map +0 -1
  573. package/publications/app-settings.d.ts +0 -2
  574. package/publications/app-settings.js +0 -28
  575. package/publications/app-settings.js.map +0 -1
  576. package/publications/app-status.d.ts +0 -2
  577. package/publications/app-status.js +0 -16
  578. package/publications/app-status.js.map +0 -1
  579. package/publications/cron-jobs.d.ts +0 -2
  580. package/publications/cron-jobs.js +0 -88
  581. package/publications/cron-jobs.js.map +0 -1
  582. package/publications/customer-notifications.d.ts +0 -2
  583. package/publications/customer-notifications.js +0 -161
  584. package/publications/customer-notifications.js.map +0 -1
  585. package/publications/files.d.ts +0 -2
  586. package/publications/files.js +0 -36
  587. package/publications/files.js.map +0 -1
  588. package/publications/flags-update.d.ts +0 -2
  589. package/publications/flags-update.js +0 -22
  590. package/publications/flags-update.js.map +0 -1
  591. package/publications/flags.d.ts +0 -2
  592. package/publications/flags.js +0 -22
  593. package/publications/flags.js.map +0 -1
  594. package/publications/logs.d.ts +0 -2
  595. package/publications/logs.js +0 -164
  596. package/publications/logs.js.map +0 -1
  597. package/publications/notifications.d.ts +0 -2
  598. package/publications/notifications.js +0 -16
  599. package/publications/notifications.js.map +0 -1
  600. package/publications/report-builder-dashboard-builders.d.ts +0 -2
  601. package/publications/report-builder-dashboard-builders.js +0 -42
  602. package/publications/report-builder-dashboard-builders.js.map +0 -1
  603. package/publications/report-builder-libraries.d.ts +0 -2
  604. package/publications/report-builder-libraries.js +0 -90
  605. package/publications/report-builder-libraries.js.map +0 -1
  606. package/publications/report-builder-reports.d.ts +0 -2
  607. package/publications/report-builder-reports.js +0 -50
  608. package/publications/report-builder-reports.js.map +0 -1
  609. package/publications/super-admin.d.ts +0 -2
  610. package/publications/super-admin.js +0 -16
  611. package/publications/super-admin.js.map +0 -1
  612. package/publications/user-groups.d.ts +0 -1
  613. package/publications/user-groups.js +0 -16
  614. package/publications/user-groups.js.map +0 -1
  615. package/publications/user-guides.d.ts +0 -1
  616. package/publications/user-guides.js +0 -16
  617. package/publications/user-guides.js.map +0 -1
  618. package/resolveio-server-app.d.ts +0 -70
  619. package/resolveio-server-app.js +0 -801
  620. package/resolveio-server-app.js.map +0 -1
  621. package/server-app.d.ts +0 -228
  622. package/server-app.js +0 -3566
  623. package/server-app.js.map +0 -1
  624. package/services/codex-client.d.ts +0 -126
  625. package/services/codex-client.js +0 -1622
  626. package/services/codex-client.js.map +0 -1
  627. package/services/openai-client.d.ts +0 -46
  628. package/services/openai-client.js +0 -318
  629. package/services/openai-client.js.map +0 -1
  630. package/types/error-report.d.ts +0 -25
  631. package/types/error-report.js +0 -4
  632. package/types/error-report.js.map +0 -1
  633. package/types/slow-query-report.d.ts +0 -27
  634. package/types/slow-query-report.js +0 -6
  635. package/types/slow-query-report.js.map +0 -1
  636. package/util/ai-qa-policy.d.ts +0 -66
  637. package/util/ai-qa-policy.js +0 -250
  638. package/util/ai-qa-policy.js.map +0 -1
  639. package/util/common.d.ts +0 -31
  640. package/util/common.js +0 -683
  641. package/util/common.js.map +0 -1
  642. package/util/customer-portal-password.d.ts +0 -13
  643. package/util/customer-portal-password.js +0 -209
  644. package/util/customer-portal-password.js.map +0 -1
  645. package/util/error-reporter.d.ts +0 -52
  646. package/util/error-reporter.js +0 -326
  647. package/util/error-reporter.js.map +0 -1
  648. package/util/error-tracking.d.ts +0 -13
  649. package/util/error-tracking.js +0 -120
  650. package/util/error-tracking.js.map +0 -1
  651. package/util/report-builder-unwinds.d.ts +0 -15
  652. package/util/report-builder-unwinds.js +0 -156
  653. package/util/report-builder-unwinds.js.map +0 -1
  654. package/util/schema-report-builder.d.ts +0 -6
  655. package/util/schema-report-builder.js +0 -481
  656. package/util/schema-report-builder.js.map +0 -1
  657. package/util/slow-query-reporter.d.ts +0 -28
  658. package/util/slow-query-reporter.js +0 -226
  659. package/util/slow-query-reporter.js.map +0 -1
  660. package/util/subscription-dependency-context.d.ts +0 -34
  661. package/util/subscription-dependency-context.js +0 -1283
  662. package/util/subscription-dependency-context.js.map +0 -1
  663. package/util/tokenizer.d.ts +0 -5
  664. package/util/tokenizer.js +0 -41
  665. package/util/tokenizer.js.map +0 -1
  666. package/workers/codex-runner.worker.d.ts +0 -1
  667. package/workers/codex-runner.worker.js +0 -192
  668. package/workers/codex-runner.worker.js.map +0 -1
  669. /package/{private → src/private}/email-templates/enrollment.html +0 -0
  670. /package/{private → src/private}/email-templates/forgot-password.html +0 -0
  671. /package/{private → src/private}/email-templates/support-ticket-deleted.html +0 -0
  672. /package/{private → src/private}/email-templates/support-ticket-modified.html +0 -0
  673. /package/{private → src/private}/email-templates/support-ticket.html +0 -0
  674. /package/{public_api.d.ts → src/public_api.ts} +0 -0
@@ -0,0 +1,1880 @@
1
+ import SimpleSchema from 'simpl-schema';
2
+ import { Db } from 'mongodb';
3
+ import { Users } from '../collections/user.collection';
4
+ import { MethodManager } from '../managers/method.manager';
5
+ import { recordOpenAIUsage } from '../managers/openai-usage-ledger.manager';
6
+ import { ResolveIOServer } from '../resolveio-server-app';
7
+ import { CodexClient } from '../services/codex-client';
8
+ import { objectIdHexString, round } from '../util/common';
9
+ import { countChatTokens, countTokens } from '../util/tokenizer';
10
+
11
+ const DEFAULT_LIMIT = 100;
12
+ const MAX_LIMIT = 2000;
13
+ const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?Z$/;
14
+ const MAX_REVIEW_STRING_LENGTH = 500;
15
+ const MAX_REVIEW_ARRAY_ITEMS = 10;
16
+ const MAX_REVIEW_OBJECT_KEYS = 40;
17
+ const MAX_REVIEW_DEPTH = 4;
18
+ const MAX_REVIEW_LIST_ITEMS = 6;
19
+ const MAX_REVIEW_CHANGED_FIELDS = 60;
20
+ const DEFAULT_RISK_REVIEW_TIMEOUT_MS = 30000;
21
+ const DEFAULT_RISK_REVIEW_MAX_TOKENS = 700;
22
+ const SENSITIVE_REVIEW_FIELD_REGEX = /(password|secret|token|api[_-]?key|salt|hash|email|phone|address|ssn|services|roles)/i;
23
+ const DEFAULT_AI_SUGGEST_TIMEOUT_MS = 60000;
24
+ const DEFAULT_AI_SUGGEST_MAX_TOKENS = 1000;
25
+ const MAX_AI_SUGGEST_FIELDS = 700;
26
+ const MAX_AI_SUGGEST_COLLECTIONS = 800;
27
+ const MAX_AI_SUGGEST_RESULTS = 500;
28
+ const RESTRICTED_AGGREGATE_STAGES = new Set(['$out', '$merge']);
29
+
30
+ type ExplorerFindOptions = {
31
+ projection?: Record<string, number>;
32
+ sort?: Record<string, any>;
33
+ limit?: number;
34
+ skip?: number;
35
+ includeTotal?: boolean;
36
+ };
37
+
38
+ type ExplorerMode = 'resolveio' | 'aicoder';
39
+
40
+ type MongoExplorerDeleteImpactCollection = {
41
+ collection: string;
42
+ references: number;
43
+ sample_ids: string[];
44
+ };
45
+
46
+ type MongoExplorerDeleteImpact = {
47
+ has_references: boolean;
48
+ unresolved: boolean;
49
+ collections: MongoExplorerDeleteImpactCollection[];
50
+ };
51
+
52
+ type MongoExplorerRiskOperation = 'insert' | 'replace' | 'delete' | 'command';
53
+ type MongoExplorerRiskLevel = 'low' | 'medium' | 'high' | 'critical';
54
+ type MongoExplorerRiskReviewStatus = 'ok' | 'fallback' | 'disabled';
55
+
56
+ type MongoExplorerRiskReview = {
57
+ operation: MongoExplorerRiskOperation;
58
+ risk_level: MongoExplorerRiskLevel;
59
+ should_block: boolean;
60
+ summary: string;
61
+ reasons: string[];
62
+ suggested_checks: string[];
63
+ confidence: number;
64
+ model: string;
65
+ request_id: string;
66
+ review_status: MongoExplorerRiskReviewStatus;
67
+ reviewed_at: string;
68
+ };
69
+
70
+ type MongoExplorerRiskReviewInput = {
71
+ database: string;
72
+ collection: string;
73
+ operation: MongoExplorerRiskOperation;
74
+ mode: ExplorerMode;
75
+ before_document?: any;
76
+ after_document?: any;
77
+ delete_impact?: MongoExplorerDeleteImpact;
78
+ };
79
+
80
+ type MongoExplorerAiAction = 'find' | 'aggregate';
81
+
82
+ type MongoExplorerAiPayload = {
83
+ prompt?: string;
84
+ database?: string;
85
+ selected_collection?: string;
86
+ available_collections?: Array<{ name?: string; collection?: string; type?: string }>;
87
+ available_fields?: Array<{ path?: string; label?: string; type?: string; collection?: string }>;
88
+ permission_view?: string;
89
+ mode?: string;
90
+ id_client?: string;
91
+ max_results?: number;
92
+ };
93
+
94
+ type MongoExplorerAiField = {
95
+ path: string;
96
+ label: string;
97
+ type: string;
98
+ collection: string;
99
+ };
100
+
101
+ type MongoExplorerAiPlan = {
102
+ action: MongoExplorerAiAction;
103
+ collection: string;
104
+ query?: Record<string, any>;
105
+ pipeline?: any[];
106
+ options?: Record<string, any>;
107
+ documents?: any[];
108
+ total?: number | null;
109
+ notes?: string;
110
+ };
111
+
112
+ function parseMongoExplorerWriteUsers(): string[] {
113
+ const raw = typeof process.env.MONGO_EXPLORER_WRITE_USERS === 'string' ? process.env.MONGO_EXPLORER_WRITE_USERS.trim() : '';
114
+ if (!raw) {
115
+ return [];
116
+ }
117
+
118
+ try {
119
+ const parsed = JSON.parse(raw);
120
+ if (Array.isArray(parsed)) {
121
+ return parsed.map(value => String(value)).filter(value => value.trim().length);
122
+ }
123
+ if (typeof parsed === 'string') {
124
+ return [parsed.trim()].filter(Boolean);
125
+ }
126
+ }
127
+ catch {
128
+ // Fall back to comma-separated list.
129
+ }
130
+
131
+ return raw
132
+ .split(',')
133
+ .map(value => value.trim())
134
+ .filter(Boolean);
135
+ }
136
+
137
+ function normalizeExplorerMode(mode?: string): ExplorerMode {
138
+ const normalizedMode = String(mode || '').trim().toLowerCase();
139
+ if (normalizedMode === 'resolveio') {
140
+ return 'resolveio';
141
+ }
142
+ return 'aicoder';
143
+ }
144
+
145
+ function allowUnschemaizedWrites(): boolean {
146
+ return process.env.MONGO_EXPLORER_ALLOW_UNSCHEMATIZED_WRITE === 'true';
147
+ }
148
+
149
+ function resolveDatabaseName(database?: string): string {
150
+ const defaultDb = ResolveIOServer.getServerConfig()?.DATABASE || '';
151
+ const dbName = typeof database === 'string' && database.trim().length ? database.trim() : defaultDb;
152
+
153
+ if (!dbName) {
154
+ throw new Error('Mongo Explorer: Database is required');
155
+ }
156
+
157
+ const allowedDatabases = ResolveIOServer.getMongoManager()?.getWatchedDatabases() || [];
158
+ if (allowedDatabases.length && !allowedDatabases.includes(dbName)) {
159
+ throw new Error('Mongo Explorer: Database access denied');
160
+ }
161
+
162
+ return dbName;
163
+ }
164
+
165
+ function resolveDatabase(database?: string): Db {
166
+ const dbName = resolveDatabaseName(database);
167
+ return ResolveIOServer.getMongoConnection().db(dbName);
168
+ }
169
+
170
+ function resolveCollectionHandle(database: string, collection: string) {
171
+ const mainDb = ResolveIOServer.getServerConfig()?.DATABASE || '';
172
+ const isMainDb = database === mainDb;
173
+ const managerCollection = isMainDb ? ResolveIOServer.getMongoManager()?.collection(collection) : null;
174
+ const db = ResolveIOServer.getMongoConnection().db(database);
175
+
176
+ return {
177
+ managerCollection,
178
+ dbCollection: db.collection(collection)
179
+ };
180
+ }
181
+
182
+ function getSchemaDefinition(collectionRef: any): Record<string, any> {
183
+ if (!collectionRef || !collectionRef.simplschema || typeof collectionRef.simplschema.schema !== 'function') {
184
+ return {};
185
+ }
186
+
187
+ const schema = collectionRef.simplschema.schema();
188
+ return schema && typeof schema === 'object' ? schema : {};
189
+ }
190
+
191
+ function getSchemaTypeName(definition: any): string {
192
+ const typeDefs = definition?.type?.definitions;
193
+ if (!Array.isArray(typeDefs) || !typeDefs.length) {
194
+ return 'Any';
195
+ }
196
+
197
+ const firstType = typeDefs[0]?.type;
198
+ if (!firstType) {
199
+ return 'Any';
200
+ }
201
+
202
+ if (firstType === String) {
203
+ return 'String';
204
+ }
205
+ if (firstType === Number) {
206
+ return 'Number';
207
+ }
208
+ if (firstType === Boolean) {
209
+ return 'Boolean';
210
+ }
211
+ if (firstType === Date) {
212
+ return 'Date';
213
+ }
214
+ if (firstType === Object) {
215
+ return 'Object';
216
+ }
217
+ if (firstType === Array) {
218
+ return 'Array';
219
+ }
220
+
221
+ const typeName = firstType.name || String(firstType);
222
+ if (typeName === 'Integer') {
223
+ return 'Number';
224
+ }
225
+ return typeName;
226
+ }
227
+
228
+ function toLabel(path: string): string {
229
+ const last = path.split('.').pop() || path;
230
+ return last
231
+ .replace(/\$/g, '')
232
+ .replace(/_/g, ' ')
233
+ .replace(/\s+/g, ' ')
234
+ .trim();
235
+ }
236
+
237
+ function isProtectedFieldSegment(segment: string): boolean {
238
+ if (!segment) {
239
+ return false;
240
+ }
241
+
242
+ if (segment === '_id') {
243
+ return true;
244
+ }
245
+
246
+ const normalizedSegment = segment.replace(/\[\d+\]/g, '');
247
+ return normalizedSegment.toLowerCase().startsWith('id_');
248
+ }
249
+
250
+ function isProtectedFieldPath(path: string): boolean {
251
+ const segments = String(path || '').split('.').filter(Boolean);
252
+ return segments.some(segment => isProtectedFieldSegment(segment));
253
+ }
254
+
255
+ function safeStringify(value: unknown): string {
256
+ try {
257
+ return JSON.stringify(value);
258
+ }
259
+ catch {
260
+ return String(value);
261
+ }
262
+ }
263
+
264
+ function collectProtectedFieldPaths(value: any, prefix = '', out: Record<string, any> = {}): Record<string, any> {
265
+ if (Array.isArray(value)) {
266
+ value.forEach((item, index) => {
267
+ const nextPrefix = prefix ? `${prefix}[${index}]` : `[${index}]`;
268
+ collectProtectedFieldPaths(item, nextPrefix, out);
269
+ });
270
+ return out;
271
+ }
272
+
273
+ if (!value || typeof value !== 'object') {
274
+ return out;
275
+ }
276
+
277
+ Object.keys(value).forEach((key) => {
278
+ const nextPrefix = prefix ? `${prefix}.${key}` : key;
279
+ if (isProtectedFieldSegment(key)) {
280
+ out[nextPrefix] = value[key];
281
+ }
282
+ collectProtectedFieldPaths(value[key], nextPrefix, out);
283
+ });
284
+
285
+ return out;
286
+ }
287
+
288
+ function ensureProtectedFieldsUnchanged(originalDoc: any, updatedDoc: any) {
289
+ const originalProtected = collectProtectedFieldPaths(originalDoc || {});
290
+ const updatedProtected = collectProtectedFieldPaths(updatedDoc || {});
291
+ const violations: string[] = [];
292
+
293
+ Object.keys(originalProtected).forEach((path) => {
294
+ if (!Object.prototype.hasOwnProperty.call(updatedProtected, path)) {
295
+ violations.push(path);
296
+ return;
297
+ }
298
+
299
+ const originalValue = safeStringify(originalProtected[path]);
300
+ const updatedValue = safeStringify(updatedProtected[path]);
301
+ if (originalValue !== updatedValue) {
302
+ violations.push(path);
303
+ }
304
+ });
305
+
306
+ Object.keys(updatedProtected).forEach((path) => {
307
+ if (!Object.prototype.hasOwnProperty.call(originalProtected, path)) {
308
+ violations.push(path);
309
+ }
310
+ });
311
+
312
+ if (violations.length) {
313
+ const preview = violations.slice(0, 4).join(', ');
314
+ throw new Error(`Mongo Explorer: Protected fields cannot be edited (${preview}${violations.length > 4 ? ', ...' : ''})`);
315
+ }
316
+ }
317
+
318
+ function coerceDateValue(value: unknown): unknown {
319
+ if (value instanceof Date) {
320
+ return value;
321
+ }
322
+
323
+ if (typeof value !== 'string' || !ISO_DATE_REGEX.test(value)) {
324
+ return value;
325
+ }
326
+
327
+ const parsed = new Date(value);
328
+ return Number.isNaN(parsed.getTime()) ? value : parsed;
329
+ }
330
+
331
+ function coerceDatePath(target: any, segments: string[]) {
332
+ if (!target || !segments.length) {
333
+ return;
334
+ }
335
+
336
+ const [segment, ...rest] = segments;
337
+
338
+ if (segment === '$') {
339
+ if (Array.isArray(target)) {
340
+ target.forEach(item => coerceDatePath(item, rest));
341
+ }
342
+ return;
343
+ }
344
+
345
+ if (!Object.prototype.hasOwnProperty.call(target, segment)) {
346
+ return;
347
+ }
348
+
349
+ if (rest.length === 0) {
350
+ const updatedValue = coerceDateValue(target[segment]);
351
+ if (updatedValue !== target[segment]) {
352
+ target[segment] = updatedValue;
353
+ }
354
+ return;
355
+ }
356
+
357
+ coerceDatePath(target[segment], rest);
358
+ }
359
+
360
+ function coerceDateFields(collectionRef: any, doc: any) {
361
+ if (!collectionRef || !collectionRef.simplschema || !doc) {
362
+ return;
363
+ }
364
+
365
+ const schema = getSchemaDefinition(collectionRef);
366
+ Object.keys(schema).forEach((schemaKey) => {
367
+ const definition = schema[schemaKey];
368
+ const typeDefs = definition?.type?.definitions;
369
+ if (!Array.isArray(typeDefs) || !typeDefs.some(typeDef => typeDef.type === Date)) {
370
+ return;
371
+ }
372
+
373
+ coerceDatePath(doc, schemaKey.split('.'));
374
+ });
375
+ }
376
+
377
+ function normalizeFindOptions(options?: ExplorerFindOptions) {
378
+ const normalized = options || {};
379
+ const projection = normalized.projection && Object.keys(normalized.projection).length ? normalized.projection : undefined;
380
+ const sort = normalized.sort && Object.keys(normalized.sort).length ? normalized.sort : undefined;
381
+ const limit = typeof normalized.limit === 'number' ? Math.min(Math.max(normalized.limit, 0), MAX_LIMIT) : DEFAULT_LIMIT;
382
+ const skip = typeof normalized.skip === 'number' ? Math.max(normalized.skip, 0) : 0;
383
+
384
+ return {
385
+ findOptions: {
386
+ projection,
387
+ sort,
388
+ limit,
389
+ skip
390
+ },
391
+ includeTotal: normalized.includeTotal === true
392
+ };
393
+ }
394
+
395
+ function userHasView(user, view: string): boolean {
396
+ if (!user || !view) {
397
+ return false;
398
+ }
399
+
400
+ if (user.roles?.super_admin) {
401
+ return true;
402
+ }
403
+
404
+ const groups = Array.isArray(user.roles?.groups) ? user.roles.groups : [];
405
+ const miscs = Array.isArray(user.roles?.miscs) ? user.roles.miscs : [];
406
+
407
+ if (groups.some(group => Array.isArray(group.views) && group.views.some(v => v.startsWith(view)))) {
408
+ return true;
409
+ }
410
+
411
+ if (miscs.some(v => v.startsWith(view))) {
412
+ return true;
413
+ }
414
+
415
+ if (groups.some(group => group.name === view)) {
416
+ return true;
417
+ }
418
+
419
+ return false;
420
+ }
421
+
422
+ async function ensureWriteAccess(context: any, permissionView?: string, mode?: string) {
423
+ const idUser = context?.id_user;
424
+ if (!idUser) {
425
+ throw new Error('Mongo Explorer: Unauthorized');
426
+ }
427
+
428
+ const user = await Users.findOne({_id: idUser});
429
+ if (!user) {
430
+ throw new Error('Mongo Explorer: Unauthorized');
431
+ }
432
+
433
+ if (user.readonly) {
434
+ throw new Error('Mongo Explorer: Readonly user');
435
+ }
436
+
437
+ const normalizedMode = normalizeExplorerMode(mode);
438
+ if (normalizedMode === 'resolveio') {
439
+ throw new Error('Mongo Explorer: ResolveIO mode is read only');
440
+ }
441
+
442
+ const allowedUsers = parseMongoExplorerWriteUsers();
443
+ if (allowedUsers.length) {
444
+ const username = String(user.username || '').trim().toLowerCase();
445
+ const email = String(user.email || '').trim().toLowerCase();
446
+ const id = String(user._id || '').trim();
447
+ const isAllowed = allowedUsers.some(entry => {
448
+ const normalized = String(entry || '').trim().toLowerCase();
449
+ if (!normalized) {
450
+ return false;
451
+ }
452
+ return normalized === username || (email && normalized === email) || (id && entry === id);
453
+ });
454
+
455
+ if (!isAllowed) {
456
+ throw new Error('Mongo Explorer: Write access denied');
457
+ }
458
+ }
459
+
460
+ if (user.roles?.super_admin) {
461
+ return;
462
+ }
463
+
464
+ const normalizedPermission = typeof permissionView === 'string' ? permissionView.trim() : '';
465
+
466
+ if (!normalizedPermission) {
467
+ return;
468
+ }
469
+
470
+ if (userHasView(user, normalizedPermission)) {
471
+ return;
472
+ }
473
+
474
+ throw new Error('Mongo Explorer: Write access denied');
475
+ }
476
+
477
+ function buildDeleteSafetyCollections(collectionRef: any): string[] {
478
+ const schema = getSchemaDefinition(collectionRef);
479
+ return Object.keys(schema).filter(path => isProtectedFieldPath(path) && path !== '_id');
480
+ }
481
+
482
+ async function buildDeleteImpact(database: string, sourceCollection: string, docId: string): Promise<MongoExplorerDeleteImpact> {
483
+ const mainDb = ResolveIOServer.getServerConfig()?.DATABASE || '';
484
+ if (database !== mainDb) {
485
+ return {
486
+ has_references: false,
487
+ unresolved: true,
488
+ collections: []
489
+ };
490
+ }
491
+
492
+ const mongoManager = ResolveIOServer.getMongoManager();
493
+ const collections = mongoManager?.collections ? mongoManager.collections() : [];
494
+ const impacts: MongoExplorerDeleteImpactCollection[] = [];
495
+ let unresolved = false;
496
+
497
+ for (const collectionRef of collections) {
498
+ const collectionName = collectionRef?.collectionName;
499
+ if (!collectionName) {
500
+ continue;
501
+ }
502
+ if (collectionName === `${sourceCollection}.versions`) {
503
+ continue;
504
+ }
505
+
506
+ const referencePaths = buildDeleteSafetyCollections(collectionRef);
507
+ if (!referencePaths.length) {
508
+ continue;
509
+ }
510
+
511
+ const query = {
512
+ $or: referencePaths.map(path => ({ [path]: docId }))
513
+ };
514
+
515
+ try {
516
+ const docs = await collectionRef.find(query, { projection: { _id: 1 }, limit: 3 });
517
+ if (Array.isArray(docs) && docs.length) {
518
+ impacts.push({
519
+ collection: collectionName,
520
+ references: docs.length,
521
+ sample_ids: docs.map(doc => String(doc?._id || '')).filter(Boolean)
522
+ });
523
+ }
524
+ }
525
+ catch {
526
+ unresolved = true;
527
+ }
528
+ }
529
+
530
+ return {
531
+ has_references: impacts.length > 0,
532
+ unresolved,
533
+ collections: impacts.sort((a, b) => a.collection.localeCompare(b.collection))
534
+ };
535
+ }
536
+
537
+ function normalizeOptionalString(value: any): string | undefined {
538
+ if (typeof value === 'string') {
539
+ const trimmed = value.trim();
540
+ return trimmed.length ? trimmed : undefined;
541
+ }
542
+ if (value === null || value === undefined) {
543
+ return undefined;
544
+ }
545
+ const normalized = String(value).trim();
546
+ return normalized.length ? normalized : undefined;
547
+ }
548
+
549
+ function normalizePositiveNumber(value: any, fallback: number): number {
550
+ const parsed = Number(value);
551
+ if (!Number.isFinite(parsed) || parsed <= 0) {
552
+ return fallback;
553
+ }
554
+ return parsed;
555
+ }
556
+
557
+ function parseBooleanValue(value: any, fallback: boolean): boolean {
558
+ if (typeof value === 'boolean') {
559
+ return value;
560
+ }
561
+ const normalized = String(value || '').trim().toLowerCase();
562
+ if (!normalized.length) {
563
+ return fallback;
564
+ }
565
+ if (['1', 'true', 'yes', 'on'].includes(normalized)) {
566
+ return true;
567
+ }
568
+ if (['0', 'false', 'no', 'off'].includes(normalized)) {
569
+ return false;
570
+ }
571
+ return fallback;
572
+ }
573
+
574
+ function truncateForRiskReview(value: string, maxLength = MAX_REVIEW_STRING_LENGTH): string {
575
+ const normalized = String(value || '');
576
+ if (normalized.length <= maxLength) {
577
+ return normalized;
578
+ }
579
+ const diff = normalized.length - maxLength;
580
+ return `${normalized.slice(0, maxLength)}...[+${diff} chars]`;
581
+ }
582
+
583
+ function sanitizeForRiskReview(value: any, depth = 0): any {
584
+ if (value === null || value === undefined) {
585
+ return value;
586
+ }
587
+
588
+ if (depth > MAX_REVIEW_DEPTH) {
589
+ return '[Truncated depth]';
590
+ }
591
+
592
+ if (value instanceof Date) {
593
+ return value.toISOString();
594
+ }
595
+
596
+ if (typeof value === 'string') {
597
+ return truncateForRiskReview(value);
598
+ }
599
+
600
+ if (typeof value === 'number' || typeof value === 'boolean') {
601
+ return value;
602
+ }
603
+
604
+ if (Array.isArray(value)) {
605
+ const sanitized = value
606
+ .slice(0, MAX_REVIEW_ARRAY_ITEMS)
607
+ .map(item => sanitizeForRiskReview(item, depth + 1));
608
+ if (value.length > MAX_REVIEW_ARRAY_ITEMS) {
609
+ sanitized.push(`[${value.length - MAX_REVIEW_ARRAY_ITEMS} more item(s)]`);
610
+ }
611
+ return sanitized;
612
+ }
613
+
614
+ if (typeof value === 'object') {
615
+ const output: Record<string, any> = {};
616
+ const keys = Object.keys(value);
617
+ const limitedKeys = keys.slice(0, MAX_REVIEW_OBJECT_KEYS);
618
+ limitedKeys.forEach((key) => {
619
+ if (SENSITIVE_REVIEW_FIELD_REGEX.test(key)) {
620
+ output[key] = '[REDACTED]';
621
+ return;
622
+ }
623
+ output[key] = sanitizeForRiskReview(value[key], depth + 1);
624
+ });
625
+ if (keys.length > MAX_REVIEW_OBJECT_KEYS) {
626
+ output.__truncated_keys = keys.length - MAX_REVIEW_OBJECT_KEYS;
627
+ }
628
+ return output;
629
+ }
630
+
631
+ return truncateForRiskReview(String(value));
632
+ }
633
+
634
+ function pickChangedFields(beforeDocument: any, afterDocument: any): string[] {
635
+ if (!beforeDocument || typeof beforeDocument !== 'object' || !afterDocument || typeof afterDocument !== 'object') {
636
+ return [];
637
+ }
638
+
639
+ const changed: string[] = [];
640
+ const fieldNames = new Set<string>([
641
+ ...Object.keys(beforeDocument),
642
+ ...Object.keys(afterDocument)
643
+ ]);
644
+
645
+ for (const field of fieldNames) {
646
+ if (changed.length >= MAX_REVIEW_CHANGED_FIELDS) {
647
+ break;
648
+ }
649
+ const beforeValue = safeStringify(beforeDocument[field]);
650
+ const afterValue = safeStringify(afterDocument[field]);
651
+ if (beforeValue !== afterValue) {
652
+ changed.push(field);
653
+ }
654
+ }
655
+
656
+ return changed;
657
+ }
658
+
659
+ function normalizeRiskLevel(value: any): MongoExplorerRiskLevel {
660
+ const normalized = String(value || '').trim().toLowerCase();
661
+ if (normalized === 'low') {
662
+ return 'low';
663
+ }
664
+ if (normalized === 'medium') {
665
+ return 'medium';
666
+ }
667
+ if (normalized === 'high') {
668
+ return 'high';
669
+ }
670
+ if (normalized === 'critical') {
671
+ return 'critical';
672
+ }
673
+ return 'high';
674
+ }
675
+
676
+ function normalizeRiskList(value: any): string[] {
677
+ if (!Array.isArray(value)) {
678
+ return [];
679
+ }
680
+ return value
681
+ .map(item => normalizeOptionalString(item))
682
+ .filter((item): item is string => !!item)
683
+ .slice(0, MAX_REVIEW_LIST_ITEMS)
684
+ .map(item => truncateForRiskReview(item, 220));
685
+ }
686
+
687
+ function buildFallbackRiskReview(operation: MongoExplorerRiskOperation, summary: string, model = ''): MongoExplorerRiskReview {
688
+ return {
689
+ operation,
690
+ risk_level: 'high',
691
+ should_block: true,
692
+ summary: truncateForRiskReview(summary, 220),
693
+ reasons: [
694
+ 'AI review could not produce a reliable result.',
695
+ 'Proceed only with explicit approval and manual dependency checks.'
696
+ ],
697
+ suggested_checks: [
698
+ 'Confirm related records and id_* references before writing.',
699
+ 'Validate that schema-required fields and date values remain valid.'
700
+ ],
701
+ confidence: 0,
702
+ model: model || '',
703
+ request_id: '',
704
+ review_status: 'fallback',
705
+ reviewed_at: new Date().toISOString()
706
+ };
707
+ }
708
+
709
+ function buildDisabledRiskReview(operation: MongoExplorerRiskOperation): MongoExplorerRiskReview {
710
+ return {
711
+ operation,
712
+ risk_level: 'low',
713
+ should_block: false,
714
+ summary: 'AI risk review is disabled by configuration.',
715
+ reasons: [],
716
+ suggested_checks: [],
717
+ confidence: 1,
718
+ model: '',
719
+ request_id: '',
720
+ review_status: 'disabled',
721
+ reviewed_at: new Date().toISOString()
722
+ };
723
+ }
724
+
725
+ function parseRiskReviewPayload(content: string): any {
726
+ const normalized = String(content || '').trim();
727
+ if (!normalized.length) {
728
+ return {};
729
+ }
730
+
731
+ try {
732
+ return JSON.parse(normalized);
733
+ }
734
+ catch {
735
+ // Fall through and try to parse a JSON object embedded in plain text.
736
+ }
737
+
738
+ const start = normalized.indexOf('{');
739
+ const end = normalized.lastIndexOf('}');
740
+ if (start !== -1 && end > start) {
741
+ const candidate = normalized.slice(start, end + 1);
742
+ try {
743
+ return JSON.parse(candidate);
744
+ }
745
+ catch {
746
+ return {};
747
+ }
748
+ }
749
+
750
+ return {};
751
+ }
752
+
753
+ function normalizeRiskReview(
754
+ operation: MongoExplorerRiskOperation,
755
+ payload: any,
756
+ model: string,
757
+ requestId: string
758
+ ): MongoExplorerRiskReview {
759
+ const riskLevel = normalizeRiskLevel(payload?.risk_level);
760
+ const normalizedConfidence = Number(payload?.confidence);
761
+ const confidence = Number.isFinite(normalizedConfidence)
762
+ ? Math.max(0, Math.min(1, normalizedConfidence))
763
+ : 0.6;
764
+ const summary = normalizeOptionalString(payload?.summary)
765
+ || `AI review marked this operation as ${riskLevel} risk.`;
766
+ const reasons = normalizeRiskList(payload?.reasons);
767
+ const suggestedChecks = normalizeRiskList(payload?.suggested_checks);
768
+ const shouldBlock = payload?.should_block === true || riskLevel === 'critical';
769
+
770
+ return {
771
+ operation,
772
+ risk_level: riskLevel,
773
+ should_block: !!shouldBlock,
774
+ summary: truncateForRiskReview(summary, 220),
775
+ reasons,
776
+ suggested_checks: suggestedChecks,
777
+ confidence,
778
+ model: model || '',
779
+ request_id: requestId || '',
780
+ review_status: 'ok',
781
+ reviewed_at: new Date().toISOString()
782
+ };
783
+ }
784
+
785
+ function resolveRiskReviewSettings() {
786
+ const serverConfig = ResolveIOServer.getServerConfig() || {};
787
+
788
+ const enabled = parseBooleanValue(
789
+ serverConfig['MONGO_EXPLORER_ENABLE_AI_RISK_REVIEW'] ?? process.env.MONGO_EXPLORER_ENABLE_AI_RISK_REVIEW,
790
+ true
791
+ );
792
+
793
+ const model = normalizeOptionalString(
794
+ serverConfig['MONGO_EXPLORER_RISK_REVIEW_MODEL']
795
+ || process.env.MONGO_EXPLORER_RISK_REVIEW_MODEL
796
+ || serverConfig['AI_ASSISTANT_CODEX_MODEL']
797
+ || process.env.AI_ASSISTANT_CODEX_MODEL
798
+ || serverConfig['AI_DASHBOARD_CODEX_MODEL']
799
+ || process.env.AI_DASHBOARD_CODEX_MODEL
800
+ );
801
+
802
+ const fallbackModel = normalizeOptionalString(
803
+ serverConfig['MONGO_EXPLORER_RISK_REVIEW_FALLBACK_MODEL']
804
+ || process.env.MONGO_EXPLORER_RISK_REVIEW_FALLBACK_MODEL
805
+ || serverConfig['AI_ASSISTANT_CODEX_FALLBACK_MODEL']
806
+ || process.env.AI_ASSISTANT_CODEX_FALLBACK_MODEL
807
+ || serverConfig['AI_DASHBOARD_CODEX_FALLBACK_MODEL']
808
+ || process.env.AI_DASHBOARD_CODEX_FALLBACK_MODEL
809
+ );
810
+
811
+ const timeoutMs = normalizePositiveNumber(
812
+ serverConfig['MONGO_EXPLORER_RISK_REVIEW_TIMEOUT_MS'] ?? process.env.MONGO_EXPLORER_RISK_REVIEW_TIMEOUT_MS,
813
+ DEFAULT_RISK_REVIEW_TIMEOUT_MS
814
+ );
815
+
816
+ const maxTokens = normalizePositiveNumber(
817
+ serverConfig['MONGO_EXPLORER_RISK_REVIEW_MAX_TOKENS'] ?? process.env.MONGO_EXPLORER_RISK_REVIEW_MAX_TOKENS,
818
+ DEFAULT_RISK_REVIEW_MAX_TOKENS
819
+ );
820
+
821
+ const maxRetries = normalizePositiveNumber(
822
+ serverConfig['OPENAI_MAX_RETRIES'] ?? process.env.OPENAI_MAX_RETRIES,
823
+ 1
824
+ );
825
+
826
+ const retryDelayMs = normalizePositiveNumber(
827
+ serverConfig['OPENAI_RETRY_DELAY_MS'] ?? process.env.OPENAI_RETRY_DELAY_MS,
828
+ 750
829
+ );
830
+
831
+ const apiKey = normalizeOptionalString(serverConfig['OPENAI_API_KEY'] || process.env.OPENAI_API_KEY) || '';
832
+ const baseUrl = normalizeOptionalString(serverConfig['OPENAI_BASE_URL'] || process.env.OPENAI_BASE_URL);
833
+
834
+ return {
835
+ enabled,
836
+ apiKey,
837
+ baseUrl,
838
+ model,
839
+ fallbackModel,
840
+ timeoutMs,
841
+ maxTokens,
842
+ maxRetries,
843
+ retryDelayMs
844
+ };
845
+ }
846
+
847
+ function buildCodexPrompt(systemPrompt: string, userPrompt: string): string {
848
+ return `System:\n${systemPrompt}\n\nUser:\n${userPrompt}`.trim();
849
+ }
850
+
851
+ function buildCodexClient(settings: {
852
+ apiKey: string;
853
+ baseUrl?: string;
854
+ model?: string;
855
+ fallbackModel?: string;
856
+ maxRetries?: number;
857
+ retryDelayMs?: number;
858
+ }): CodexClient {
859
+ const fallbackModels: string[] = [];
860
+ const fallback = normalizeOptionalString(settings.fallbackModel);
861
+ if (fallback && fallback !== settings.model) {
862
+ fallbackModels.push(fallback);
863
+ }
864
+ return new CodexClient({
865
+ apiKey: settings.apiKey,
866
+ baseUrl: settings.baseUrl,
867
+ ...(settings.model ? { model: settings.model } : {}),
868
+ ...(fallbackModels.length ? { fallbackModel: fallbackModels[0], fallbackModels } : {}),
869
+ maxRetries: round(settings.maxRetries || 0),
870
+ retryDelayMs: round(settings.retryDelayMs || 0)
871
+ });
872
+ }
873
+
874
+ function estimateCodexUsage(messages: Array<{ role: string; content: string }>, responseText: string, model: string) {
875
+ const inputTokens = countChatTokens(messages, model);
876
+ const outputTokens = countTokens(responseText || '', model);
877
+ return {
878
+ inputTokens,
879
+ outputTokens,
880
+ totalTokens: inputTokens + outputTokens
881
+ };
882
+ }
883
+
884
+ function buildRiskReviewPrompt(input: MongoExplorerRiskReviewInput): string {
885
+ const payload = {
886
+ database: input.database,
887
+ collection: input.collection,
888
+ operation: input.operation,
889
+ mode: input.mode,
890
+ changed_fields: pickChangedFields(input.before_document, input.after_document),
891
+ delete_impact: sanitizeForRiskReview(input.delete_impact || null),
892
+ before_document: sanitizeForRiskReview(input.before_document || null),
893
+ after_document: sanitizeForRiskReview(input.after_document || null)
894
+ };
895
+
896
+ return [
897
+ 'Review this MongoDB write operation for runtime and data integrity risk.',
898
+ 'Focus on foreign-key style id_* dependencies, schema compatibility, destructive impact, and financial/operational record safety.',
899
+ 'Return JSON only.',
900
+ JSON.stringify(payload, null, 2)
901
+ ].join('\n');
902
+ }
903
+
904
+ async function reviewOperationRisk(input: MongoExplorerRiskReviewInput): Promise<MongoExplorerRiskReview> {
905
+ const settings = resolveRiskReviewSettings();
906
+ if (!settings.enabled) {
907
+ return buildDisabledRiskReview(input.operation);
908
+ }
909
+
910
+ if (!settings.apiKey) {
911
+ return buildFallbackRiskReview(input.operation, 'AI risk review unavailable: AI API key is missing.');
912
+ }
913
+
914
+ const client = buildCodexClient(settings);
915
+
916
+ const systemPrompt = [
917
+ 'You are a MongoDB operation safety reviewer for a production SaaS application.',
918
+ 'Respond with a single JSON object only.',
919
+ 'Required keys:',
920
+ 'risk_level ("low" | "medium" | "high" | "critical"),',
921
+ 'should_block (boolean),',
922
+ 'summary (string),',
923
+ 'reasons (string[]),',
924
+ 'suggested_checks (string[]),',
925
+ 'confidence (number between 0 and 1).',
926
+ 'Keep summary concise (<= 220 chars).'
927
+ ].join(' ');
928
+ const userPrompt = buildRiskReviewPrompt(input);
929
+ const prompt = buildCodexPrompt(systemPrompt, userPrompt);
930
+
931
+ try {
932
+ const responseText = await client.run(prompt, {
933
+ timeoutMs: round(settings.timeoutMs),
934
+ threadOptions: {
935
+ model: settings.model,
936
+ sandboxMode: 'read-only',
937
+ skipGitRepoCheck: true,
938
+ networkAccessEnabled: false,
939
+ webSearchMode: 'disabled',
940
+ webSearchEnabled: false,
941
+ approvalPolicy: 'never'
942
+ }
943
+ });
944
+
945
+ const payload = parseRiskReviewPayload(responseText);
946
+ return normalizeRiskReview(input.operation, payload, settings.model, '');
947
+ }
948
+ catch (err) {
949
+ const detail = err?.message ? String(err.message) : 'Unknown AI review error';
950
+ return buildFallbackRiskReview(input.operation, `AI risk review failed: ${truncateForRiskReview(detail, 160)}`, settings.model);
951
+ }
952
+ }
953
+
954
+ function normalizeAiAction(value: any): MongoExplorerAiAction {
955
+ const normalized = normalizeOptionalString(value) || '';
956
+ return normalized.toLowerCase() === 'aggregate' ? 'aggregate' : 'find';
957
+ }
958
+
959
+ function sanitizeAiCollections(raw: any[]): string[] {
960
+ const values: string[] = [];
961
+ const seen = new Set<string>();
962
+ (raw || []).forEach((entry) => {
963
+ const normalized = normalizeOptionalString((entry as any)?.collection || (entry as any)?.name || entry);
964
+ if (!normalized) {
965
+ return;
966
+ }
967
+ const key = normalized.toLowerCase();
968
+ if (seen.has(key)) {
969
+ return;
970
+ }
971
+ seen.add(key);
972
+ values.push(normalized);
973
+ });
974
+ return values.slice(0, MAX_AI_SUGGEST_COLLECTIONS);
975
+ }
976
+
977
+ function sanitizeAiFields(raw: any[], fallbackCollection: string): MongoExplorerAiField[] {
978
+ const fields: MongoExplorerAiField[] = [];
979
+ const seen = new Set<string>();
980
+ (raw || []).forEach((entry) => {
981
+ const path = normalizeOptionalString(entry?.path || entry?.field_path || entry?.fieldPath);
982
+ if (!path) {
983
+ return;
984
+ }
985
+ const collection = normalizeOptionalString(
986
+ entry?.collection || entry?.collection_name || entry?.collectionName
987
+ ) || fallbackCollection || '';
988
+ const key = `${collection.toLowerCase()}::${path.toLowerCase()}`;
989
+ if (seen.has(key)) {
990
+ return;
991
+ }
992
+ seen.add(key);
993
+ fields.push({
994
+ path,
995
+ label: normalizeOptionalString(entry?.label || entry?.field_path_name || entry?.fieldPathName) || path,
996
+ type: normalizeOptionalString(entry?.type || entry?.field_type || entry?.fieldType) || 'Any',
997
+ collection
998
+ });
999
+ });
1000
+ return fields.slice(0, MAX_AI_SUGGEST_FIELDS);
1001
+ }
1002
+
1003
+ function deriveAiFieldsFromCollection(database: string, collection: string): MongoExplorerAiField[] {
1004
+ if (!collection) {
1005
+ return [];
1006
+ }
1007
+ const { managerCollection } = resolveCollectionHandle(database, collection);
1008
+ if (!managerCollection) {
1009
+ return [];
1010
+ }
1011
+ const schema = getSchemaDefinition(managerCollection);
1012
+ return Object.keys(schema)
1013
+ .sort((a, b) => a.localeCompare(b))
1014
+ .slice(0, MAX_AI_SUGGEST_FIELDS)
1015
+ .map((path) => {
1016
+ const definition = schema[path];
1017
+ return {
1018
+ path,
1019
+ label: toLabel(path),
1020
+ type: getSchemaTypeName(definition),
1021
+ collection
1022
+ };
1023
+ });
1024
+ }
1025
+
1026
+ function resolveCollectionFromList(value: any, collections: string[], fallback = ''): string {
1027
+ const normalized = normalizeOptionalString(value);
1028
+ if (normalized) {
1029
+ const matched = (collections || []).find(entry => entry.toLowerCase() === normalized.toLowerCase());
1030
+ if (matched) {
1031
+ return matched;
1032
+ }
1033
+ }
1034
+ if (fallback) {
1035
+ return fallback;
1036
+ }
1037
+ return collections?.[0] || '';
1038
+ }
1039
+
1040
+ function normalizeAiResultLimit(value: any, fallback = 100): number {
1041
+ const fallbackValue = Math.min(Math.max(fallback, 1), MAX_AI_SUGGEST_RESULTS);
1042
+ const parsed = Number(value);
1043
+ if (!Number.isFinite(parsed) || parsed <= 0) {
1044
+ return fallbackValue;
1045
+ }
1046
+ return Math.min(Math.max(round(parsed), 1), MAX_AI_SUGGEST_RESULTS);
1047
+ }
1048
+
1049
+ function normalizeAiSkip(value: any): number {
1050
+ const parsed = Number(value);
1051
+ if (!Number.isFinite(parsed) || parsed < 0) {
1052
+ return 0;
1053
+ }
1054
+ return Math.max(0, round(parsed));
1055
+ }
1056
+
1057
+ function normalizeAiObject(value: any): Record<string, any> {
1058
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
1059
+ return {};
1060
+ }
1061
+ return value;
1062
+ }
1063
+
1064
+ function hasObjectKeys(value: any): boolean {
1065
+ return !!value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length > 0;
1066
+ }
1067
+
1068
+ function parseAiPlanPayload(content: string): any {
1069
+ const normalized = String(content || '').trim();
1070
+ if (!normalized.length) {
1071
+ return {};
1072
+ }
1073
+
1074
+ try {
1075
+ return JSON.parse(normalized);
1076
+ }
1077
+ catch {
1078
+ // Fall through and parse embedded JSON object.
1079
+ }
1080
+
1081
+ const start = normalized.indexOf('{');
1082
+ const end = normalized.lastIndexOf('}');
1083
+ if (start !== -1 && end > start) {
1084
+ try {
1085
+ return JSON.parse(normalized.slice(start, end + 1));
1086
+ }
1087
+ catch {
1088
+ return {};
1089
+ }
1090
+ }
1091
+
1092
+ return {};
1093
+ }
1094
+
1095
+ function resolveMongoExplorerAiSettings() {
1096
+ const serverConfig = ResolveIOServer.getServerConfig() || {};
1097
+ const apiKey = normalizeOptionalString(serverConfig['OPENAI_API_KEY'] || process.env.OPENAI_API_KEY) || '';
1098
+ const baseUrl = normalizeOptionalString(serverConfig['OPENAI_BASE_URL'] || process.env.OPENAI_BASE_URL);
1099
+
1100
+ const model = normalizeOptionalString(
1101
+ serverConfig['MONGO_EXPLORER_AI_MODEL']
1102
+ || process.env.MONGO_EXPLORER_AI_MODEL
1103
+ || serverConfig['AI_ASSISTANT_CODEX_MODEL']
1104
+ || process.env.AI_ASSISTANT_CODEX_MODEL
1105
+ );
1106
+
1107
+ const fallbackModel = normalizeOptionalString(
1108
+ serverConfig['MONGO_EXPLORER_AI_FALLBACK_MODEL']
1109
+ || process.env.MONGO_EXPLORER_AI_FALLBACK_MODEL
1110
+ || serverConfig['AI_ASSISTANT_CODEX_FALLBACK_MODEL']
1111
+ || process.env.AI_ASSISTANT_CODEX_FALLBACK_MODEL
1112
+ );
1113
+
1114
+ const timeoutMs = normalizePositiveNumber(
1115
+ serverConfig['MONGO_EXPLORER_AI_TIMEOUT_MS'] ?? process.env.MONGO_EXPLORER_AI_TIMEOUT_MS,
1116
+ DEFAULT_AI_SUGGEST_TIMEOUT_MS
1117
+ );
1118
+
1119
+ const maxTokens = normalizePositiveNumber(
1120
+ serverConfig['MONGO_EXPLORER_AI_MAX_TOKENS'] ?? process.env.MONGO_EXPLORER_AI_MAX_TOKENS,
1121
+ DEFAULT_AI_SUGGEST_MAX_TOKENS
1122
+ );
1123
+
1124
+ const maxRetries = normalizePositiveNumber(
1125
+ serverConfig['OPENAI_MAX_RETRIES'] ?? process.env.OPENAI_MAX_RETRIES,
1126
+ 1
1127
+ );
1128
+
1129
+ const retryDelayMs = normalizePositiveNumber(
1130
+ serverConfig['OPENAI_RETRY_DELAY_MS'] ?? process.env.OPENAI_RETRY_DELAY_MS,
1131
+ 750
1132
+ );
1133
+
1134
+ return {
1135
+ apiKey,
1136
+ baseUrl,
1137
+ model,
1138
+ fallbackModel,
1139
+ timeoutMs,
1140
+ maxTokens,
1141
+ maxRetries,
1142
+ retryDelayMs
1143
+ };
1144
+ }
1145
+
1146
+ function buildMongoExplorerAiSystemPrompt(): string {
1147
+ return [
1148
+ 'You are a MongoDB query planner for a read-only Mongo Explorer UI.',
1149
+ 'Return a single JSON object and nothing else.',
1150
+ 'Supported schema:',
1151
+ '{',
1152
+ ' "action": "find|aggregate",',
1153
+ ' "collection": "string",',
1154
+ ' "query": { ... },',
1155
+ ' "pipeline": [{ ... }],',
1156
+ ' "options": {',
1157
+ ' "projection": { "field": 1 },',
1158
+ ' "sort": { "field": -1 },',
1159
+ ' "limit": 100,',
1160
+ ' "skip": 0,',
1161
+ ' "includeTotal": true,',
1162
+ ' "allowDiskUse": true',
1163
+ ' },',
1164
+ ' "notes": "short summary"',
1165
+ '}',
1166
+ 'Rules:',
1167
+ '- Use only provided collection names and field paths.',
1168
+ '- Read-only only. Never output write operations, update/delete commands, or aggregation stages $out/$merge.',
1169
+ '- Prefer action=find for direct lookups and simple filters.',
1170
+ '- Use action=aggregate for grouped totals, rankings, or trend buckets.',
1171
+ '- Keep options.limit between 1 and 500.',
1172
+ '- If request is ambiguous, use selected_collection and a conservative limit.'
1173
+ ].join('\n');
1174
+ }
1175
+
1176
+ function buildMongoExplorerAiUserPrompt(input: {
1177
+ prompt: string;
1178
+ selectedCollection: string;
1179
+ availableCollections: string[];
1180
+ availableFields: MongoExplorerAiField[];
1181
+ maxResults: number;
1182
+ }): string {
1183
+ const payload = {
1184
+ request: input.prompt,
1185
+ selected_collection: input.selectedCollection || '',
1186
+ max_results: input.maxResults,
1187
+ available_collections: input.availableCollections || [],
1188
+ available_fields: input.availableFields || []
1189
+ };
1190
+ return JSON.stringify(payload);
1191
+ }
1192
+
1193
+ function sanitizeAggregatePipeline(rawPipeline: any, limit: number): { pipeline: any[]; removedRestricted: boolean } {
1194
+ const source = Array.isArray(rawPipeline) ? rawPipeline : [];
1195
+ const sanitized: any[] = [];
1196
+ let removedRestricted = false;
1197
+
1198
+ source.forEach((stage) => {
1199
+ if (!stage || typeof stage !== 'object' || Array.isArray(stage)) {
1200
+ return;
1201
+ }
1202
+
1203
+ const keys = Object.keys(stage);
1204
+ if (!keys.length) {
1205
+ return;
1206
+ }
1207
+
1208
+ const stageName = keys[0];
1209
+ if (RESTRICTED_AGGREGATE_STAGES.has(String(stageName).toLowerCase())) {
1210
+ removedRestricted = true;
1211
+ return;
1212
+ }
1213
+
1214
+ if (stageName === '$limit') {
1215
+ sanitized.push({ $limit: normalizeAiResultLimit((stage as any).$limit, limit) });
1216
+ return;
1217
+ }
1218
+
1219
+ sanitized.push(stage);
1220
+ });
1221
+
1222
+ if (!sanitized.some(stage => Object.keys(stage || {})[0] === '$limit')) {
1223
+ sanitized.push({ $limit: limit });
1224
+ }
1225
+
1226
+ return {
1227
+ pipeline: sanitized,
1228
+ removedRestricted
1229
+ };
1230
+ }
1231
+
1232
+ function sanitizeFindOptions(raw: any, fallbackLimit: number): {
1233
+ projection?: Record<string, any>;
1234
+ sort?: Record<string, any>;
1235
+ limit: number;
1236
+ skip: number;
1237
+ includeTotal: boolean;
1238
+ } {
1239
+ const options = normalizeAiObject(raw);
1240
+ return {
1241
+ projection: hasObjectKeys(options.projection) ? options.projection : undefined,
1242
+ sort: hasObjectKeys(options.sort) ? options.sort : undefined,
1243
+ limit: normalizeAiResultLimit(options.limit, fallbackLimit),
1244
+ skip: normalizeAiSkip(options.skip),
1245
+ includeTotal: options.includeTotal === true
1246
+ };
1247
+ }
1248
+
1249
+ function buildMongoExplorerAiNotes(
1250
+ rawNotes: any,
1251
+ action: MongoExplorerAiAction,
1252
+ collection: string,
1253
+ rowCount: number,
1254
+ total: number | null,
1255
+ removedRestricted: boolean
1256
+ ): string {
1257
+ const notes: string[] = [];
1258
+ const base = normalizeOptionalString(rawNotes);
1259
+ if (base) {
1260
+ notes.push(base);
1261
+ }
1262
+
1263
+ const actionLabel = action === 'aggregate' ? 'aggregate pipeline' : 'query';
1264
+ if (typeof total === 'number' && total >= 0) {
1265
+ notes.push(`Applied ${actionLabel} on ${collection}. Loaded ${rowCount} of ${total} rows.`);
1266
+ }
1267
+ else {
1268
+ notes.push(`Applied ${actionLabel} on ${collection}. Loaded ${rowCount} rows.`);
1269
+ }
1270
+
1271
+ if (removedRestricted) {
1272
+ notes.push('Removed restricted write stages from the generated pipeline.');
1273
+ }
1274
+
1275
+ return notes.join(' ').trim();
1276
+ }
1277
+
1278
+ async function resolveUsageClientId(idClientInput?: string, idUser?: string): Promise<string> {
1279
+ const normalized = normalizeOptionalString(idClientInput);
1280
+ if (normalized) {
1281
+ return normalized;
1282
+ }
1283
+ if (!idUser) {
1284
+ return '';
1285
+ }
1286
+ try {
1287
+ const user = await Users.findById(idUser);
1288
+ return normalizeOptionalString(user?.other?.id_client || user?.other?.idClient) || '';
1289
+ }
1290
+ catch {
1291
+ return '';
1292
+ }
1293
+ }
1294
+
1295
+ async function executeMongoExplorerAi(payload: MongoExplorerAiPayload, context: any) {
1296
+ const input = payload || {};
1297
+ const prompt = normalizeOptionalString(input.prompt);
1298
+ if (!prompt) {
1299
+ throw new Error('Prompt is required.');
1300
+ }
1301
+
1302
+ const database = resolveDatabaseName(input.database);
1303
+ const db = resolveDatabase(database);
1304
+
1305
+ let availableCollections = sanitizeAiCollections(input.available_collections || []);
1306
+ if (!availableCollections.length) {
1307
+ const listed = await db.listCollections().toArray();
1308
+ availableCollections = listed
1309
+ .map(collection => normalizeOptionalString(collection?.name))
1310
+ .filter((collection): collection is string => !!collection)
1311
+ .slice(0, MAX_AI_SUGGEST_COLLECTIONS);
1312
+ }
1313
+
1314
+ if (!availableCollections.length) {
1315
+ throw new Error('Mongo Explorer AI assistant: no collections available.');
1316
+ }
1317
+
1318
+ const selectedCollection = resolveCollectionFromList(
1319
+ input.selected_collection,
1320
+ availableCollections,
1321
+ availableCollections[0]
1322
+ );
1323
+
1324
+ let availableFields = sanitizeAiFields(input.available_fields || [], selectedCollection);
1325
+ if (!availableFields.length && selectedCollection) {
1326
+ availableFields = deriveAiFieldsFromCollection(database, selectedCollection);
1327
+ }
1328
+
1329
+ const settings = resolveMongoExplorerAiSettings();
1330
+ if (!settings.apiKey) {
1331
+ throw new Error('AI API key missing. Add an AI API key to server config.');
1332
+ }
1333
+
1334
+ const maxResults = normalizeAiResultLimit(input.max_results, 100);
1335
+ const client = buildCodexClient(settings);
1336
+ const messages: Array<{ role: 'system' | 'user'; content: string }> = [
1337
+ { role: 'system', content: buildMongoExplorerAiSystemPrompt() },
1338
+ {
1339
+ role: 'user',
1340
+ content: buildMongoExplorerAiUserPrompt({
1341
+ prompt,
1342
+ selectedCollection,
1343
+ availableCollections,
1344
+ availableFields,
1345
+ maxResults
1346
+ })
1347
+ }
1348
+ ];
1349
+ const responseText = await client.run(
1350
+ buildCodexPrompt(messages[0].content, messages[1].content),
1351
+ {
1352
+ timeoutMs: round(settings.timeoutMs),
1353
+ threadOptions: {
1354
+ model: settings.model,
1355
+ sandboxMode: 'read-only',
1356
+ skipGitRepoCheck: true,
1357
+ networkAccessEnabled: false,
1358
+ webSearchMode: 'disabled',
1359
+ webSearchEnabled: false,
1360
+ approvalPolicy: 'never'
1361
+ }
1362
+ }
1363
+ );
1364
+
1365
+ const parsed = parseAiPlanPayload(responseText);
1366
+ const action = normalizeAiAction(parsed?.action || (Array.isArray(parsed?.pipeline) ? 'aggregate' : 'find'));
1367
+ const collection = resolveCollectionFromList(
1368
+ parsed?.collection || parsed?.collection_name,
1369
+ availableCollections,
1370
+ selectedCollection || availableCollections[0]
1371
+ );
1372
+
1373
+ if (!collection) {
1374
+ throw new Error('Mongo Explorer AI assistant could not resolve a collection.');
1375
+ }
1376
+
1377
+ let plan: MongoExplorerAiPlan = {
1378
+ action,
1379
+ collection,
1380
+ options: {}
1381
+ };
1382
+ let removedRestrictedStages = false;
1383
+
1384
+ if (action === 'aggregate') {
1385
+ const optionsRaw = normalizeAiObject(parsed?.options);
1386
+ const aggregateLimit = normalizeAiResultLimit(optionsRaw.limit ?? parsed?.limit, maxResults);
1387
+ const pipelineResult = sanitizeAggregatePipeline(parsed?.pipeline, aggregateLimit);
1388
+ removedRestrictedStages = pipelineResult.removedRestricted;
1389
+
1390
+ const aggregateOptions = {
1391
+ allowDiskUse: optionsRaw.allowDiskUse === true
1392
+ };
1393
+
1394
+ const aggregateRows = await db.collection(collection).aggregate(pipelineResult.pipeline, aggregateOptions).toArray();
1395
+ const rows = (aggregateRows || []).slice(0, aggregateLimit);
1396
+
1397
+ plan = {
1398
+ action,
1399
+ collection,
1400
+ pipeline: pipelineResult.pipeline,
1401
+ options: {
1402
+ allowDiskUse: aggregateOptions.allowDiskUse,
1403
+ limit: aggregateLimit
1404
+ },
1405
+ documents: rows,
1406
+ total: null
1407
+ };
1408
+ }
1409
+ else {
1410
+ const query = normalizeAiObject(parsed?.query);
1411
+ const options = sanitizeFindOptions(parsed?.options, maxResults);
1412
+ const findOptions: any = {
1413
+ limit: options.limit,
1414
+ skip: options.skip
1415
+ };
1416
+ if (options.projection) {
1417
+ findOptions.projection = options.projection;
1418
+ }
1419
+ if (options.sort) {
1420
+ findOptions.sort = options.sort;
1421
+ }
1422
+
1423
+ const rows = await db.collection(collection).find(query, findOptions).toArray();
1424
+ const total = options.includeTotal ? await db.collection(collection).countDocuments(query) : null;
1425
+
1426
+ plan = {
1427
+ action,
1428
+ collection,
1429
+ query,
1430
+ options: {
1431
+ ...(options.projection ? { projection: options.projection } : {}),
1432
+ ...(options.sort ? { sort: options.sort } : {}),
1433
+ limit: options.limit,
1434
+ skip: options.skip,
1435
+ includeTotal: options.includeTotal
1436
+ },
1437
+ documents: rows,
1438
+ total
1439
+ };
1440
+ }
1441
+
1442
+ plan.notes = buildMongoExplorerAiNotes(
1443
+ parsed?.notes,
1444
+ plan.action,
1445
+ plan.collection,
1446
+ Array.isArray(plan.documents) ? plan.documents.length : 0,
1447
+ typeof plan.total === 'number' ? plan.total : null,
1448
+ removedRestrictedStages
1449
+ );
1450
+
1451
+ const usage = estimateCodexUsage(messages, responseText, settings.model);
1452
+ const idClient = await resolveUsageClientId(input.id_client, context?.id_user);
1453
+ if (usage.totalTokens) {
1454
+ await recordOpenAIUsage({
1455
+ id_client: idClient || '',
1456
+ model: settings.model || 'unknown',
1457
+ input_tokens: usage.inputTokens || 0,
1458
+ output_tokens: usage.outputTokens || 0,
1459
+ total_tokens: usage.totalTokens || 0,
1460
+ category: 'mongo-explorer-ai',
1461
+ id_request: ''
1462
+ });
1463
+ }
1464
+
1465
+ return {
1466
+ notes: plan.notes,
1467
+ plan,
1468
+ model: settings.model,
1469
+ usage: {
1470
+ input_tokens: usage.inputTokens || 0,
1471
+ output_tokens: usage.outputTokens || 0,
1472
+ total_tokens: usage.totalTokens || 0
1473
+ }
1474
+ };
1475
+ }
1476
+
1477
+ export function loadMongoExplorerMethods(methodManager: MethodManager) {
1478
+ methodManager.methods({
1479
+ mongoExplorerAiSuggest: {
1480
+ check: new SimpleSchema({
1481
+ payload: {
1482
+ type: Object,
1483
+ blackbox: true
1484
+ }
1485
+ }),
1486
+ function: async function(payload: any) {
1487
+ return executeMongoExplorerAi(payload || {}, this);
1488
+ }
1489
+ },
1490
+ mongoExplorerListCollections: {
1491
+ check: new SimpleSchema({
1492
+ database: {
1493
+ type: String,
1494
+ optional: true
1495
+ }
1496
+ }),
1497
+ function: async function(database?: string) {
1498
+ const db = resolveDatabase(database);
1499
+ const collections = await db.listCollections().toArray();
1500
+ return collections
1501
+ .map(col => ({
1502
+ name: col.name,
1503
+ type: col.type || 'collection'
1504
+ }))
1505
+ .sort((a, b) => a.name.localeCompare(b.name));
1506
+ }
1507
+ },
1508
+ mongoExplorerFind: {
1509
+ bypassSession: true,
1510
+ check: new SimpleSchema({
1511
+ database: {
1512
+ type: String,
1513
+ optional: true
1514
+ },
1515
+ collection: {
1516
+ type: String
1517
+ },
1518
+ query: {
1519
+ type: Object,
1520
+ blackbox: true,
1521
+ optional: true
1522
+ },
1523
+ options: {
1524
+ type: Object,
1525
+ blackbox: true,
1526
+ optional: true
1527
+ }
1528
+ }),
1529
+ function: async function(database: string, collection: string, query: any = {}, options?) {
1530
+ const db = resolveDatabase(database);
1531
+ const normalized = normalizeFindOptions(options);
1532
+ const documents = await db.collection(collection).find(query || {}, normalized.findOptions).toArray();
1533
+ let total = null;
1534
+
1535
+ if (normalized.includeTotal) {
1536
+ total = await db.collection(collection).countDocuments(query || {});
1537
+ }
1538
+
1539
+ return {
1540
+ documents,
1541
+ total
1542
+ };
1543
+ }
1544
+ },
1545
+ mongoExplorerGetCollectionSchema: {
1546
+ check: new SimpleSchema({
1547
+ database: {
1548
+ type: String,
1549
+ optional: true
1550
+ },
1551
+ collection: {
1552
+ type: String
1553
+ }
1554
+ }),
1555
+ function: function(database: string, collection: string) {
1556
+ const dbName = resolveDatabaseName(database);
1557
+ const { managerCollection } = resolveCollectionHandle(dbName, collection);
1558
+ if (!managerCollection) {
1559
+ return Promise.resolve({
1560
+ collection,
1561
+ has_schema: false,
1562
+ fields: []
1563
+ });
1564
+ }
1565
+
1566
+ const schema = getSchemaDefinition(managerCollection);
1567
+ const fields = Object.keys(schema)
1568
+ .sort((a, b) => a.localeCompare(b))
1569
+ .map((path) => {
1570
+ const definition = schema[path];
1571
+ return {
1572
+ path,
1573
+ label: toLabel(path),
1574
+ type: getSchemaTypeName(definition),
1575
+ optional: definition?.optional === true,
1576
+ protected: isProtectedFieldPath(path),
1577
+ depth: path.split('.').filter((segment: string) => segment !== '$').length - 1
1578
+ };
1579
+ });
1580
+
1581
+ return Promise.resolve({
1582
+ collection,
1583
+ has_schema: true,
1584
+ fields
1585
+ });
1586
+ }
1587
+ },
1588
+ mongoExplorerAggregate: {
1589
+ bypassSession: true,
1590
+ check: new SimpleSchema({
1591
+ database: {
1592
+ type: String,
1593
+ optional: true
1594
+ },
1595
+ collection: {
1596
+ type: String
1597
+ },
1598
+ pipeline: {
1599
+ type: Array
1600
+ },
1601
+ 'pipeline.$': {
1602
+ type: Object,
1603
+ blackbox: true
1604
+ },
1605
+ options: {
1606
+ type: Object,
1607
+ optional: true,
1608
+ blackbox: true
1609
+ }
1610
+ }),
1611
+ function: async function(database: string, collection: string, pipeline: any[], options: any = {}) {
1612
+ const db = resolveDatabase(database);
1613
+ return db.collection(collection).aggregate(pipeline || [], options || undefined).toArray();
1614
+ }
1615
+ },
1616
+ mongoExplorerCommand: {
1617
+ check: new SimpleSchema({
1618
+ database: {
1619
+ type: String,
1620
+ optional: true
1621
+ },
1622
+ command: {
1623
+ type: Object,
1624
+ blackbox: true
1625
+ },
1626
+ permissionView: {
1627
+ type: String,
1628
+ optional: true
1629
+ },
1630
+ mode: {
1631
+ type: String,
1632
+ optional: true
1633
+ }
1634
+ }),
1635
+ function: async function(database: string, command: any, permissionView?: string, mode?: string) {
1636
+ await ensureWriteAccess(this, permissionView, mode);
1637
+ const db = resolveDatabase(database);
1638
+ return db.command(command);
1639
+ }
1640
+ },
1641
+ mongoExplorerInsertDocument: {
1642
+ check: new SimpleSchema({
1643
+ database: {
1644
+ type: String,
1645
+ optional: true
1646
+ },
1647
+ collection: {
1648
+ type: String
1649
+ },
1650
+ document: {
1651
+ type: Object,
1652
+ blackbox: true
1653
+ },
1654
+ permissionView: {
1655
+ type: String,
1656
+ optional: true
1657
+ },
1658
+ mode: {
1659
+ type: String,
1660
+ optional: true
1661
+ }
1662
+ }),
1663
+ function: async function(database: string, collection: string, document: any, permissionView?: string, mode?: string) {
1664
+ await ensureWriteAccess(this, permissionView, mode);
1665
+ const dbName = resolveDatabaseName(database);
1666
+ const { managerCollection, dbCollection } = resolveCollectionHandle(dbName, collection);
1667
+
1668
+ if (!document || typeof document !== 'object') {
1669
+ throw new Error('Mongo Explorer: Document is required');
1670
+ }
1671
+
1672
+ if (!managerCollection && !allowUnschemaizedWrites()) {
1673
+ throw new Error('Mongo Explorer: Writes require schema-backed collections. Set MONGO_EXPLORER_ALLOW_UNSCHEMATIZED_WRITE=true to bypass.');
1674
+ }
1675
+
1676
+ if (!document._id) {
1677
+ document._id = objectIdHexString();
1678
+ }
1679
+
1680
+ if (document.__v === undefined) {
1681
+ document.__v = 0;
1682
+ }
1683
+
1684
+ if (managerCollection) {
1685
+ coerceDateFields(managerCollection, document);
1686
+ await managerCollection.insertOne(document);
1687
+ }
1688
+ else {
1689
+ await dbCollection.insertOne(document);
1690
+ }
1691
+
1692
+ return document._id;
1693
+ }
1694
+ },
1695
+ mongoExplorerReplaceDocument: {
1696
+ check: new SimpleSchema({
1697
+ database: {
1698
+ type: String,
1699
+ optional: true
1700
+ },
1701
+ collection: {
1702
+ type: String
1703
+ },
1704
+ document: {
1705
+ type: Object,
1706
+ blackbox: true
1707
+ },
1708
+ permissionView: {
1709
+ type: String,
1710
+ optional: true
1711
+ },
1712
+ mode: {
1713
+ type: String,
1714
+ optional: true
1715
+ }
1716
+ }),
1717
+ function: async function(database: string, collection: string, document: any, permissionView?: string, mode?: string) {
1718
+ await ensureWriteAccess(this, permissionView, mode);
1719
+ const dbName = resolveDatabaseName(database);
1720
+ const { managerCollection, dbCollection } = resolveCollectionHandle(dbName, collection);
1721
+
1722
+ if (!document || typeof document !== 'object' || !document._id) {
1723
+ throw new Error('Mongo Explorer: Document with _id is required');
1724
+ }
1725
+
1726
+ const currentDoc = managerCollection
1727
+ ? await managerCollection.findOne({_id: document._id})
1728
+ : await dbCollection.findOne({_id: document._id});
1729
+ if (!currentDoc) {
1730
+ throw new Error('Mongo Explorer: Document not found');
1731
+ }
1732
+
1733
+ ensureProtectedFieldsUnchanged(currentDoc, document);
1734
+
1735
+ if (!managerCollection && !allowUnschemaizedWrites()) {
1736
+ throw new Error('Mongo Explorer: Writes require schema-backed collections. Set MONGO_EXPLORER_ALLOW_UNSCHEMATIZED_WRITE=true to bypass.');
1737
+ }
1738
+
1739
+ if (managerCollection) {
1740
+ coerceDateFields(managerCollection, document);
1741
+ await managerCollection.replaceOne({_id: document._id}, document);
1742
+ }
1743
+ else {
1744
+ await dbCollection.replaceOne({_id: document._id}, document);
1745
+ }
1746
+
1747
+ return true;
1748
+ }
1749
+ },
1750
+ mongoExplorerReviewOperationRisk: {
1751
+ check: new SimpleSchema({
1752
+ database: {
1753
+ type: String,
1754
+ optional: true
1755
+ },
1756
+ collection: {
1757
+ type: String
1758
+ },
1759
+ operation: {
1760
+ type: String,
1761
+ allowedValues: ['insert', 'replace', 'delete', 'command']
1762
+ },
1763
+ before_document: {
1764
+ type: Object,
1765
+ blackbox: true,
1766
+ optional: true
1767
+ },
1768
+ after_document: {
1769
+ type: Object,
1770
+ blackbox: true,
1771
+ optional: true
1772
+ },
1773
+ delete_impact: {
1774
+ type: Object,
1775
+ blackbox: true,
1776
+ optional: true
1777
+ },
1778
+ permissionView: {
1779
+ type: String,
1780
+ optional: true
1781
+ },
1782
+ mode: {
1783
+ type: String,
1784
+ optional: true
1785
+ }
1786
+ }),
1787
+ function: async function(
1788
+ database: string,
1789
+ collection: string,
1790
+ operation: MongoExplorerRiskOperation,
1791
+ before_document?: any,
1792
+ after_document?: any,
1793
+ delete_impact?: MongoExplorerDeleteImpact,
1794
+ permissionView?: string,
1795
+ mode?: string
1796
+ ) {
1797
+ await ensureWriteAccess(this, permissionView, mode);
1798
+ const dbName = resolveDatabaseName(database);
1799
+
1800
+ return reviewOperationRisk({
1801
+ database: dbName,
1802
+ collection,
1803
+ operation,
1804
+ mode: normalizeExplorerMode(mode),
1805
+ before_document,
1806
+ after_document,
1807
+ delete_impact
1808
+ });
1809
+ }
1810
+ },
1811
+ mongoExplorerDeleteDocument: {
1812
+ check: new SimpleSchema({
1813
+ database: {
1814
+ type: String,
1815
+ optional: true
1816
+ },
1817
+ collection: {
1818
+ type: String
1819
+ },
1820
+ doc_id: {
1821
+ type: String
1822
+ },
1823
+ permissionView: {
1824
+ type: String,
1825
+ optional: true
1826
+ },
1827
+ force: {
1828
+ type: Boolean,
1829
+ optional: true
1830
+ },
1831
+ mode: {
1832
+ type: String,
1833
+ optional: true
1834
+ }
1835
+ }),
1836
+ function: async function(database: string, collection: string, doc_id: string, permissionView?: string, force = false, mode?: string) {
1837
+ await ensureWriteAccess(this, permissionView, mode);
1838
+ const dbName = resolveDatabaseName(database);
1839
+ const impact = await buildDeleteImpact(dbName, collection, doc_id);
1840
+ if (!force && (impact.unresolved || impact.has_references)) {
1841
+ const collectionPreview = impact.collections.slice(0, 3).map(item => item.collection).join(', ');
1842
+ if (impact.unresolved) {
1843
+ throw new Error('Mongo Explorer: Delete blocked because dependency checks could not complete. Use force delete after review.');
1844
+ }
1845
+ throw new Error(`Mongo Explorer: Delete blocked. Document is referenced by ${collectionPreview}${impact.collections.length > 3 ? ', ...' : ''}`);
1846
+ }
1847
+
1848
+ const { managerCollection, dbCollection } = resolveCollectionHandle(dbName, collection);
1849
+ const deleteFilter = { _id: doc_id } as any;
1850
+
1851
+ if (managerCollection) {
1852
+ await managerCollection.deleteOne(deleteFilter);
1853
+ }
1854
+ else {
1855
+ await dbCollection.deleteOne(deleteFilter);
1856
+ }
1857
+
1858
+ return true;
1859
+ }
1860
+ },
1861
+ mongoExplorerDeleteImpact: {
1862
+ check: new SimpleSchema({
1863
+ database: {
1864
+ type: String,
1865
+ optional: true
1866
+ },
1867
+ collection: {
1868
+ type: String
1869
+ },
1870
+ doc_id: {
1871
+ type: String
1872
+ }
1873
+ }),
1874
+ function: async function(database: string, collection: string, doc_id: string) {
1875
+ const dbName = resolveDatabaseName(database);
1876
+ return buildDeleteImpact(dbName, collection, doc_id);
1877
+ }
1878
+ }
1879
+ });
1880
+ }