@resolveio/server-lib 22.3.220 → 22.3.221

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 (745) hide show
  1. package/.nodemon.json +5 -0
  2. package/.vscode/settings.json +21 -0
  3. package/AGENTS.md +195 -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 +375 -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-run.collection.ts +117 -0
  29. package/src/collections/ai-terminal-conversation.collection.ts +91 -0
  30. package/src/collections/ai-terminal-issue-report.collection.ts +99 -0
  31. package/src/collections/ai-terminal-message.collection.ts +77 -0
  32. package/src/collections/app-setting.collection.ts +104 -0
  33. package/src/collections/app-status.collection.ts +58 -0
  34. package/src/collections/communication-metric.collection.ts +84 -0
  35. package/src/collections/counter.collection.ts +56 -0
  36. package/src/collections/cron-job-history.collection.ts +94 -0
  37. package/src/collections/cron-job.collection.ts +92 -0
  38. package/src/collections/customer-notification.collection.ts +131 -0
  39. package/src/collections/customer-portal-password.collection.ts +76 -0
  40. package/src/collections/email-history.collection.ts +134 -0
  41. package/src/collections/email-verified.collection.ts +62 -0
  42. package/src/collections/file.collection.ts +74 -0
  43. package/src/collections/flag-update.collection.ts +57 -0
  44. package/src/collections/flag.collection.ts +57 -0
  45. package/src/collections/log-method-latency.collection.ts +77 -0
  46. package/src/collections/log-subscription.collection.ts +80 -0
  47. package/src/collections/log.collection.ts +93 -0
  48. package/src/collections/logged-in-users.collection.ts +67 -0
  49. package/src/collections/monitor-cpu.collection.ts +65 -0
  50. package/src/collections/monitor-function.collection.ts +74 -0
  51. package/src/collections/monitor-memory.collection.ts +77 -0
  52. package/src/collections/monitor-mongo.collection.ts +71 -0
  53. package/src/collections/notification.collection.ts +57 -0
  54. package/src/collections/openai-usage-ledger.collection.ts +131 -0
  55. package/src/collections/report-builder-dashboard-builder.collection.ts +109 -0
  56. package/src/collections/report-builder-library.collection.ts +89 -0
  57. package/src/collections/report-builder-report.collection.ts +184 -0
  58. package/src/collections/user-group.collection.ts +89 -0
  59. package/src/collections/user-guide.collection.ts +57 -0
  60. package/src/collections/user.collection.ts +181 -0
  61. package/src/cron/cron.ts +117 -0
  62. package/src/fixtures/cron-jobs.ts +95 -0
  63. package/src/fixtures/init.ts +35 -0
  64. package/src/http/auth.ts +818 -0
  65. package/src/http/health.ts +7 -0
  66. package/src/http/home.ts +90 -0
  67. package/src/http/slow-query-publication.ts +49 -0
  68. package/src/index.ts +1 -0
  69. package/src/managers/ai-assistant-codex-manager.manager.ts +1131 -0
  70. package/src/managers/ai-run-evidence.manager.ts +264 -0
  71. package/src/managers/communication-metric.manager.ts +82 -0
  72. package/src/managers/cron.manager.ts +333 -0
  73. package/src/managers/customer-notification-content.manager.ts +236 -0
  74. package/src/managers/diagnostic-manager-bootstrap.ts +165 -0
  75. package/src/managers/error-auto-fix.manager.ts +2767 -0
  76. package/src/managers/local-log.manager.ts +113 -0
  77. package/src/managers/method.manager.ts +1857 -0
  78. package/src/managers/mongo.manager.ts +4575 -0
  79. package/src/managers/monitor.manager.ts +507 -0
  80. package/src/managers/openai-usage-ledger.manager.ts +112 -0
  81. package/src/managers/slow-query-verifier.manager.ts +3590 -0
  82. package/src/managers/slow-query.manager.ts +519 -0
  83. package/src/managers/subscription.manager.ts +3128 -0
  84. package/src/managers/websocket.manager.ts +746 -0
  85. package/src/managers/worker-dispatcher.manager.ts +1360 -0
  86. package/src/managers/worker-server.manager.ts +536 -0
  87. package/src/methods/accounts.ts +532 -0
  88. package/src/methods/ai-terminal.ts +29070 -0
  89. package/src/methods/app-settings.ts +114 -0
  90. package/src/methods/aws.ts +649 -0
  91. package/src/methods/collections.ts +641 -0
  92. package/src/methods/counters.ts +69 -0
  93. package/src/methods/cron-jobs.ts +2614 -0
  94. package/src/methods/customer-notifications.ts +458 -0
  95. package/src/methods/diagnostics.ts +616 -0
  96. package/src/methods/flag-updates.ts +7 -0
  97. package/src/methods/flags.ts +7 -0
  98. package/src/methods/logs.ts +657 -0
  99. package/src/methods/mongo-explorer.ts +1880 -0
  100. package/src/methods/monitor.ts +540 -0
  101. package/src/methods/pdf.ts +1236 -0
  102. package/src/methods/publications.ts +129 -0
  103. package/src/methods/report-builder.ts +3300 -0
  104. package/src/methods/support.ts +335 -0
  105. package/src/models/ai-run.model.ts +27 -0
  106. package/src/models/ai-terminal-conversation.model.ts +19 -0
  107. package/src/models/ai-terminal-issue-report.model.ts +21 -0
  108. package/src/models/ai-terminal-message.model.ts +24 -0
  109. package/src/models/app-setting.model.ts +17 -0
  110. package/{models/app-status.model.d.ts → src/models/app-status.model.ts} +3 -2
  111. package/{models/billing-logged-in-users.model.d.ts → src/models/billing-logged-in-users.model.ts} +5 -4
  112. package/src/models/collection-document.model.ts +24 -0
  113. package/src/models/communication-metric.model.ts +23 -0
  114. package/{models/counter.model.d.ts → src/models/counter.model.ts} +4 -3
  115. package/src/models/cron-job-history.model.ts +16 -0
  116. package/src/models/cron-job.model.ts +15 -0
  117. package/src/models/customer-notification.model.ts +28 -0
  118. package/src/models/customer-portal-password.model.ts +12 -0
  119. package/src/models/dialog.model.ts +25 -0
  120. package/{models/email-history.model.js → src/models/email-history.model.ts} +36 -4
  121. package/{models/email-verified.model.d.ts → src/models/email-verified.model.ts} +6 -5
  122. package/{models/file.model.d.ts → src/models/file.model.ts} +8 -7
  123. package/{models/flag-update.model.d.ts → src/models/flag-update.model.ts} +4 -3
  124. package/{models/flag.model.d.ts → src/models/flag.model.ts} +4 -3
  125. package/src/models/log-method-latency.model.ts +11 -0
  126. package/{models/log-subscription.model.d.ts → src/models/log-subscription.model.ts} +11 -9
  127. package/src/models/log.model.ts +19 -0
  128. package/{models/logged-in-users.model.d.ts → src/models/logged-in-users.model.ts} +6 -5
  129. package/{models/method-response.model.d.ts → src/models/method-response.model.ts} +7 -6
  130. package/src/models/method.model.ts +25 -0
  131. package/{models/monitor-cpu.model.d.ts → src/models/monitor-cpu.model.ts} +9 -7
  132. package/src/models/monitor-function.model.ts +16 -0
  133. package/src/models/monitor-memory.model.ts +17 -0
  134. package/src/models/monitor-mongo.model.ts +15 -0
  135. package/{models/notification.model.d.ts → src/models/notification.model.ts} +6 -4
  136. package/src/models/openai-usage-ledger.model.ts +56 -0
  137. package/src/models/pagination.model.ts +35 -0
  138. package/src/models/permission.model.ts +14 -0
  139. package/src/models/report-builder-dashboard-builder.model.ts +29 -0
  140. package/src/models/report-builder-library.model.ts +20 -0
  141. package/src/models/report-builder-report.model.ts +136 -0
  142. package/src/models/report-builder.model.ts +68 -0
  143. package/src/models/select-data-label.model.ts +9 -0
  144. package/src/models/server-message.model.ts +31 -0
  145. package/src/models/slow-query-report.model.ts +23 -0
  146. package/src/models/subscription.model.ts +73 -0
  147. package/src/models/support-ticket.model.ts +104 -0
  148. package/src/models/user-group.model.ts +24 -0
  149. package/{models/user-guide.model.d.ts → src/models/user-guide.model.ts} +5 -4
  150. package/src/models/user.model.ts +96 -0
  151. package/src/private/images/ResolveIO.png +0 -0
  152. package/src/publications/ai-terminal.ts +73 -0
  153. package/src/publications/app-settings.ts +25 -0
  154. package/src/publications/app-status.ts +13 -0
  155. package/src/publications/cron-jobs.ts +40 -0
  156. package/src/publications/customer-notifications.ts +101 -0
  157. package/src/publications/files.ts +33 -0
  158. package/src/publications/flags-update.ts +19 -0
  159. package/src/publications/flags.ts +19 -0
  160. package/src/publications/logs.ts +163 -0
  161. package/src/publications/notifications.ts +13 -0
  162. package/src/publications/report-builder-dashboard-builders.ts +39 -0
  163. package/src/publications/report-builder-libraries.ts +41 -0
  164. package/src/publications/report-builder-reports.ts +47 -0
  165. package/src/publications/super-admin.ts +13 -0
  166. package/src/publications/user-groups.ts +12 -0
  167. package/src/publications/user-guides.ts +12 -0
  168. package/src/resolveio-server-app.ts +617 -0
  169. package/src/server-app.ts +3354 -0
  170. package/src/services/codex-client.ts +1231 -0
  171. package/src/services/openai-client.ts +265 -0
  172. package/src/types/error-report.ts +26 -0
  173. package/src/types/js-tiktoken.d.ts +11 -0
  174. package/src/types/slow-query-report.ts +28 -0
  175. package/src/util/ai-qa-policy.ts +925 -0
  176. package/src/util/ai-run-evidence-adapters.ts +8347 -0
  177. package/src/util/ai-run-evidence-dashboard.ts +323 -0
  178. package/src/util/ai-run-evidence-eval.ts +1057 -0
  179. package/src/util/ai-run-evidence.ts +1430 -0
  180. package/src/util/ai-runner-artifacts.ts +586 -0
  181. package/src/util/ai-runner-manager-autopilot.ts +961 -0
  182. package/src/util/ai-runner-manager-policy.ts +5011 -0
  183. package/src/util/ai-runner-qa-auth.ts +838 -0
  184. package/src/util/ai-runner-qa-tools.ts +3536 -0
  185. package/src/util/aicoder-runner-v6.ts +3121 -0
  186. package/src/util/common.ts +649 -0
  187. package/src/util/customer-portal-password.ts +183 -0
  188. package/src/util/error-reporter.ts +332 -0
  189. package/src/util/error-tracking.ts +79 -0
  190. package/src/util/openai-usage-cost.ts +114 -0
  191. package/src/util/report-builder-unwinds.ts +180 -0
  192. package/src/util/runner-process-janitor.ts +219 -0
  193. package/src/util/schema-report-builder.ts +448 -0
  194. package/src/util/slow-query-reporter.ts +216 -0
  195. package/src/util/subscription-dependency-context.ts +1096 -0
  196. package/src/util/support-runner-v5.ts +10040 -0
  197. package/src/util/tokenizer.ts +38 -0
  198. package/src/workers/codex-runner.worker.ts +142 -0
  199. package/start_server.sh +5 -0
  200. package/tests/ai-assistant-corpus-build.ts +484 -0
  201. package/tests/ai-assistant-corpus-replay-e2e.ts +774 -0
  202. package/tests/ai-assistant-data-parity-e2e.ts +1989 -0
  203. package/tests/ai-assistant-eval-triage.ts +831 -0
  204. package/tests/ai-assistant-openai-e2e.ts +1061 -0
  205. package/tests/ai-assistant-openai-git-e2e.ts +155 -0
  206. package/tests/ai-assistant-preflight-matrix.ts +215 -0
  207. package/tests/ai-assistant-routing-eval.test.ts +585 -0
  208. package/tests/ai-assistant-snf-live-eval.ts +975 -0
  209. package/tests/ai-assistant-utils.test.ts +4834 -0
  210. package/tests/ai-manager-autopilot-snapshot.test.ts +193 -0
  211. package/tests/ai-manager-recovery-checkpoint.test.ts +1383 -0
  212. package/tests/ai-run-eval.test.ts +132 -0
  213. package/tests/ai-run-evidence.test.ts +3773 -0
  214. package/tests/ai-runner-contract.test.ts +515 -0
  215. package/tests/aicoder-runner-v6.test.ts +822 -0
  216. package/tests/error-reporter.test.ts +145 -0
  217. package/tests/method-publication-generator.test.ts +46 -0
  218. package/tests/report-builder-linking.test.ts +79 -0
  219. package/tests/resolveio-platform-intelligence.test.ts +352 -0
  220. package/tests/server-app-cron-owner.test.ts +127 -0
  221. package/tests/subscription-connect-race.test.ts +158 -0
  222. package/tests/subscription-dependency-context.test.ts +324 -0
  223. package/tests/subscription-manager-collection-tracking.test.ts +86 -0
  224. package/tests/subscription-manager-invalidation.test.ts +86 -0
  225. package/tests/support-runner-v5.test.ts +3201 -0
  226. package/tsconfig.json +34 -0
  227. package/ai/assistant-core-heuristics.d.ts +0 -11
  228. package/ai/assistant-core-heuristics.js +0 -356
  229. package/ai/assistant-core-heuristics.js.map +0 -1
  230. package/ai/resolveio-platform-intelligence-memory-corpus.d.ts +0 -3
  231. package/ai/resolveio-platform-intelligence-memory-corpus.js +0 -214
  232. package/ai/resolveio-platform-intelligence-memory-corpus.js.map +0 -1
  233. package/ai/resolveio-platform-intelligence-memory.d.ts +0 -20
  234. package/ai/resolveio-platform-intelligence-memory.js +0 -341
  235. package/ai/resolveio-platform-intelligence-memory.js.map +0 -1
  236. package/ai/resolveio-platform-intelligence-types.js +0 -4
  237. package/ai/resolveio-platform-intelligence-types.js.map +0 -1
  238. package/ai/resolveio-platform-intelligence.d.ts +0 -6
  239. package/ai/resolveio-platform-intelligence.js +0 -463
  240. package/ai/resolveio-platform-intelligence.js.map +0 -1
  241. package/client-server-app.d.ts +0 -1
  242. package/client-server-app.js +0 -68
  243. package/client-server-app.js.map +0 -1
  244. package/collections/ai-run.collection.d.ts +0 -3
  245. package/collections/ai-run.collection.js +0 -170
  246. package/collections/ai-run.collection.js.map +0 -1
  247. package/collections/ai-terminal-conversation.collection.d.ts +0 -2
  248. package/collections/ai-terminal-conversation.collection.js +0 -140
  249. package/collections/ai-terminal-conversation.collection.js.map +0 -1
  250. package/collections/ai-terminal-issue-report.collection.d.ts +0 -2
  251. package/collections/ai-terminal-issue-report.collection.js +0 -148
  252. package/collections/ai-terminal-issue-report.collection.js.map +0 -1
  253. package/collections/ai-terminal-message.collection.d.ts +0 -2
  254. package/collections/ai-terminal-message.collection.js +0 -121
  255. package/collections/ai-terminal-message.collection.js.map +0 -1
  256. package/collections/app-setting.collection.d.ts +0 -3
  257. package/collections/app-setting.collection.js +0 -103
  258. package/collections/app-setting.collection.js.map +0 -1
  259. package/collections/app-status.collection.d.ts +0 -3
  260. package/collections/app-status.collection.js +0 -57
  261. package/collections/app-status.collection.js.map +0 -1
  262. package/collections/communication-metric.collection.d.ts +0 -2
  263. package/collections/communication-metric.collection.js +0 -133
  264. package/collections/communication-metric.collection.js.map +0 -1
  265. package/collections/counter.collection.d.ts +0 -3
  266. package/collections/counter.collection.js +0 -56
  267. package/collections/counter.collection.js.map +0 -1
  268. package/collections/cron-job-history.collection.d.ts +0 -3
  269. package/collections/cron-job-history.collection.js +0 -137
  270. package/collections/cron-job-history.collection.js.map +0 -1
  271. package/collections/cron-job.collection.d.ts +0 -3
  272. package/collections/cron-job.collection.js +0 -92
  273. package/collections/cron-job.collection.js.map +0 -1
  274. package/collections/customer-notification.collection.d.ts +0 -3
  275. package/collections/customer-notification.collection.js +0 -130
  276. package/collections/customer-notification.collection.js.map +0 -1
  277. package/collections/customer-portal-password.collection.d.ts +0 -3
  278. package/collections/customer-portal-password.collection.js +0 -75
  279. package/collections/customer-portal-password.collection.js.map +0 -1
  280. package/collections/email-history.collection.d.ts +0 -3
  281. package/collections/email-history.collection.js +0 -134
  282. package/collections/email-history.collection.js.map +0 -1
  283. package/collections/email-verified.collection.d.ts +0 -3
  284. package/collections/email-verified.collection.js +0 -62
  285. package/collections/email-verified.collection.js.map +0 -1
  286. package/collections/file.collection.d.ts +0 -3
  287. package/collections/file.collection.js +0 -74
  288. package/collections/file.collection.js.map +0 -1
  289. package/collections/flag-update.collection.d.ts +0 -3
  290. package/collections/flag-update.collection.js +0 -57
  291. package/collections/flag-update.collection.js.map +0 -1
  292. package/collections/flag.collection.d.ts +0 -3
  293. package/collections/flag.collection.js +0 -57
  294. package/collections/flag.collection.js.map +0 -1
  295. package/collections/log-method-latency.collection.d.ts +0 -3
  296. package/collections/log-method-latency.collection.js +0 -77
  297. package/collections/log-method-latency.collection.js.map +0 -1
  298. package/collections/log-subscription.collection.d.ts +0 -3
  299. package/collections/log-subscription.collection.js +0 -80
  300. package/collections/log-subscription.collection.js.map +0 -1
  301. package/collections/log.collection.d.ts +0 -3
  302. package/collections/log.collection.js +0 -93
  303. package/collections/log.collection.js.map +0 -1
  304. package/collections/logged-in-users.collection.d.ts +0 -3
  305. package/collections/logged-in-users.collection.js +0 -67
  306. package/collections/logged-in-users.collection.js.map +0 -1
  307. package/collections/monitor-cpu.collection.d.ts +0 -3
  308. package/collections/monitor-cpu.collection.js +0 -65
  309. package/collections/monitor-cpu.collection.js.map +0 -1
  310. package/collections/monitor-function.collection.d.ts +0 -3
  311. package/collections/monitor-function.collection.js +0 -74
  312. package/collections/monitor-function.collection.js.map +0 -1
  313. package/collections/monitor-memory.collection.d.ts +0 -3
  314. package/collections/monitor-memory.collection.js +0 -77
  315. package/collections/monitor-memory.collection.js.map +0 -1
  316. package/collections/monitor-mongo.collection.d.ts +0 -3
  317. package/collections/monitor-mongo.collection.js +0 -71
  318. package/collections/monitor-mongo.collection.js.map +0 -1
  319. package/collections/notification.collection.d.ts +0 -3
  320. package/collections/notification.collection.js +0 -57
  321. package/collections/notification.collection.js.map +0 -1
  322. package/collections/openai-usage-ledger.collection.d.ts +0 -2
  323. package/collections/openai-usage-ledger.collection.js +0 -188
  324. package/collections/openai-usage-ledger.collection.js.map +0 -1
  325. package/collections/report-builder-dashboard-builder.collection.d.ts +0 -3
  326. package/collections/report-builder-dashboard-builder.collection.js +0 -109
  327. package/collections/report-builder-dashboard-builder.collection.js.map +0 -1
  328. package/collections/report-builder-library.collection.d.ts +0 -3
  329. package/collections/report-builder-library.collection.js +0 -87
  330. package/collections/report-builder-library.collection.js.map +0 -1
  331. package/collections/report-builder-report.collection.d.ts +0 -4
  332. package/collections/report-builder-report.collection.js +0 -184
  333. package/collections/report-builder-report.collection.js.map +0 -1
  334. package/collections/user-group.collection.d.ts +0 -4
  335. package/collections/user-group.collection.js +0 -89
  336. package/collections/user-group.collection.js.map +0 -1
  337. package/collections/user-guide.collection.d.ts +0 -3
  338. package/collections/user-guide.collection.js +0 -57
  339. package/collections/user-guide.collection.js.map +0 -1
  340. package/collections/user.collection.d.ts +0 -4
  341. package/collections/user.collection.js +0 -180
  342. package/collections/user.collection.js.map +0 -1
  343. package/cron/cron.d.ts +0 -14
  344. package/cron/cron.js +0 -216
  345. package/cron/cron.js.map +0 -1
  346. package/fixtures/cron-jobs.d.ts +0 -1
  347. package/fixtures/cron-jobs.js +0 -150
  348. package/fixtures/cron-jobs.js.map +0 -1
  349. package/fixtures/init.d.ts +0 -1
  350. package/fixtures/init.js +0 -91
  351. package/fixtures/init.js.map +0 -1
  352. package/http/auth.d.ts +0 -2
  353. package/http/auth.js +0 -951
  354. package/http/auth.js.map +0 -1
  355. package/http/health.d.ts +0 -1
  356. package/http/health.js +0 -11
  357. package/http/health.js.map +0 -1
  358. package/http/home.d.ts +0 -1
  359. package/http/home.js +0 -134
  360. package/http/home.js.map +0 -1
  361. package/http/slow-query-publication.d.ts +0 -2
  362. package/http/slow-query-publication.js +0 -99
  363. package/http/slow-query-publication.js.map +0 -1
  364. package/index.d.ts +0 -1
  365. package/index.js +0 -19
  366. package/index.js.map +0 -1
  367. package/managers/ai-assistant-codex-manager.manager.d.ts +0 -67
  368. package/managers/ai-assistant-codex-manager.manager.js +0 -1113
  369. package/managers/ai-assistant-codex-manager.manager.js.map +0 -1
  370. package/managers/ai-run-evidence.manager.d.ts +0 -36
  371. package/managers/ai-run-evidence.manager.js +0 -377
  372. package/managers/ai-run-evidence.manager.js.map +0 -1
  373. package/managers/communication-metric.manager.d.ts +0 -16
  374. package/managers/communication-metric.manager.js +0 -134
  375. package/managers/communication-metric.manager.js.map +0 -1
  376. package/managers/cron.manager.d.ts +0 -20
  377. package/managers/cron.manager.js +0 -534
  378. package/managers/cron.manager.js.map +0 -1
  379. package/managers/customer-notification-content.manager.d.ts +0 -55
  380. package/managers/customer-notification-content.manager.js +0 -158
  381. package/managers/customer-notification-content.manager.js.map +0 -1
  382. package/managers/diagnostic-manager-bootstrap.d.ts +0 -9
  383. package/managers/diagnostic-manager-bootstrap.js +0 -260
  384. package/managers/diagnostic-manager-bootstrap.js.map +0 -1
  385. package/managers/error-auto-fix.manager.d.ts +0 -149
  386. package/managers/error-auto-fix.manager.js +0 -3064
  387. package/managers/error-auto-fix.manager.js.map +0 -1
  388. package/managers/local-log.manager.d.ts +0 -18
  389. package/managers/local-log.manager.js +0 -88
  390. package/managers/local-log.manager.js.map +0 -1
  391. package/managers/method.manager.d.ts +0 -84
  392. package/managers/method.manager.js +0 -1964
  393. package/managers/method.manager.js.map +0 -1
  394. package/managers/mongo.manager.d.ts +0 -224
  395. package/managers/mongo.manager.js +0 -5000
  396. package/managers/mongo.manager.js.map +0 -1
  397. package/managers/monitor.manager.d.ts +0 -70
  398. package/managers/monitor.manager.js +0 -550
  399. package/managers/monitor.manager.js.map +0 -1
  400. package/managers/openai-usage-ledger.manager.d.ts +0 -30
  401. package/managers/openai-usage-ledger.manager.js +0 -142
  402. package/managers/openai-usage-ledger.manager.js.map +0 -1
  403. package/managers/slow-query-verifier.manager.d.ts +0 -144
  404. package/managers/slow-query-verifier.manager.js +0 -3857
  405. package/managers/slow-query-verifier.manager.js.map +0 -1
  406. package/managers/slow-query.manager.d.ts +0 -28
  407. package/managers/slow-query.manager.js +0 -468
  408. package/managers/slow-query.manager.js.map +0 -1
  409. package/managers/subscription.manager.d.ts +0 -169
  410. package/managers/subscription.manager.js +0 -3434
  411. package/managers/subscription.manager.js.map +0 -1
  412. package/managers/websocket.manager.d.ts +0 -73
  413. package/managers/websocket.manager.js +0 -673
  414. package/managers/websocket.manager.js.map +0 -1
  415. package/managers/worker-dispatcher.manager.d.ts +0 -120
  416. package/managers/worker-dispatcher.manager.js +0 -1266
  417. package/managers/worker-dispatcher.manager.js.map +0 -1
  418. package/managers/worker-server.manager.d.ts +0 -35
  419. package/managers/worker-server.manager.js +0 -582
  420. package/managers/worker-server.manager.js.map +0 -1
  421. package/methods/accounts.d.ts +0 -2
  422. package/methods/accounts.js +0 -624
  423. package/methods/accounts.js.map +0 -1
  424. package/methods/ai-terminal.d.ts +0 -458
  425. package/methods/ai-terminal.js +0 -27991
  426. package/methods/ai-terminal.js.map +0 -1
  427. package/methods/app-settings.d.ts +0 -2
  428. package/methods/app-settings.js +0 -169
  429. package/methods/app-settings.js.map +0 -1
  430. package/methods/aws.d.ts +0 -2
  431. package/methods/aws.js +0 -877
  432. package/methods/aws.js.map +0 -1
  433. package/methods/collections.d.ts +0 -2
  434. package/methods/collections.js +0 -719
  435. package/methods/collections.js.map +0 -1
  436. package/methods/counters.d.ts +0 -2
  437. package/methods/counters.js +0 -113
  438. package/methods/counters.js.map +0 -1
  439. package/methods/cron-jobs.d.ts +0 -2
  440. package/methods/cron-jobs.js +0 -2475
  441. package/methods/cron-jobs.js.map +0 -1
  442. package/methods/customer-notifications.d.ts +0 -2
  443. package/methods/customer-notifications.js +0 -528
  444. package/methods/customer-notifications.js.map +0 -1
  445. package/methods/diagnostics.d.ts +0 -2
  446. package/methods/diagnostics.js +0 -703
  447. package/methods/diagnostics.js.map +0 -1
  448. package/methods/flag-updates.d.ts +0 -2
  449. package/methods/flag-updates.js +0 -8
  450. package/methods/flag-updates.js.map +0 -1
  451. package/methods/flags.d.ts +0 -2
  452. package/methods/flags.js +0 -8
  453. package/methods/flags.js.map +0 -1
  454. package/methods/logs.d.ts +0 -2
  455. package/methods/logs.js +0 -751
  456. package/methods/logs.js.map +0 -1
  457. package/methods/mongo-explorer.d.ts +0 -2
  458. package/methods/mongo-explorer.js +0 -1808
  459. package/methods/mongo-explorer.js.map +0 -1
  460. package/methods/monitor.d.ts +0 -2
  461. package/methods/monitor.js +0 -543
  462. package/methods/monitor.js.map +0 -1
  463. package/methods/pdf.d.ts +0 -2
  464. package/methods/pdf.js +0 -1216
  465. package/methods/pdf.js.map +0 -1
  466. package/methods/publications.d.ts +0 -1
  467. package/methods/publications.js +0 -183
  468. package/methods/publications.js.map +0 -1
  469. package/methods/report-builder.d.ts +0 -2
  470. package/methods/report-builder.js +0 -3094
  471. package/methods/report-builder.js.map +0 -1
  472. package/methods/support.d.ts +0 -2
  473. package/methods/support.js +0 -430
  474. package/methods/support.js.map +0 -1
  475. package/models/ai-run.model.d.ts +0 -19
  476. package/models/ai-run.model.js +0 -4
  477. package/models/ai-run.model.js.map +0 -1
  478. package/models/ai-terminal-conversation.model.d.ts +0 -17
  479. package/models/ai-terminal-conversation.model.js +0 -4
  480. package/models/ai-terminal-conversation.model.js.map +0 -1
  481. package/models/ai-terminal-issue-report.model.d.ts +0 -19
  482. package/models/ai-terminal-issue-report.model.js +0 -4
  483. package/models/ai-terminal-issue-report.model.js.map +0 -1
  484. package/models/ai-terminal-message.model.d.ts +0 -22
  485. package/models/ai-terminal-message.model.js +0 -4
  486. package/models/ai-terminal-message.model.js.map +0 -1
  487. package/models/app-setting.model.d.ts +0 -16
  488. package/models/app-setting.model.js +0 -4
  489. package/models/app-setting.model.js.map +0 -1
  490. package/models/app-status.model.js +0 -4
  491. package/models/app-status.model.js.map +0 -1
  492. package/models/billing-logged-in-users.model.js +0 -4
  493. package/models/billing-logged-in-users.model.js.map +0 -1
  494. package/models/collection-document.model.d.ts +0 -21
  495. package/models/collection-document.model.js +0 -4
  496. package/models/collection-document.model.js.map +0 -1
  497. package/models/communication-metric.model.d.ts +0 -20
  498. package/models/communication-metric.model.js +0 -4
  499. package/models/communication-metric.model.js.map +0 -1
  500. package/models/counter.model.js +0 -4
  501. package/models/counter.model.js.map +0 -1
  502. package/models/cron-job-history.model.d.ts +0 -15
  503. package/models/cron-job-history.model.js +0 -4
  504. package/models/cron-job-history.model.js.map +0 -1
  505. package/models/cron-job.model.d.ts +0 -14
  506. package/models/cron-job.model.js +0 -4
  507. package/models/cron-job.model.js.map +0 -1
  508. package/models/customer-notification.model.d.ts +0 -26
  509. package/models/customer-notification.model.js +0 -4
  510. package/models/customer-notification.model.js.map +0 -1
  511. package/models/customer-portal-password.model.d.ts +0 -11
  512. package/models/customer-portal-password.model.js +0 -4
  513. package/models/customer-portal-password.model.js.map +0 -1
  514. package/models/dialog.model.d.ts +0 -23
  515. package/models/dialog.model.js +0 -4
  516. package/models/dialog.model.js.map +0 -1
  517. package/models/email-history.model.d.ts +0 -32
  518. package/models/email-history.model.js.map +0 -1
  519. package/models/email-verified.model.js +0 -4
  520. package/models/email-verified.model.js.map +0 -1
  521. package/models/file.model.js +0 -4
  522. package/models/file.model.js.map +0 -1
  523. package/models/flag-update.model.js +0 -4
  524. package/models/flag-update.model.js.map +0 -1
  525. package/models/flag.model.js +0 -4
  526. package/models/flag.model.js.map +0 -1
  527. package/models/log-method-latency.model.d.ts +0 -10
  528. package/models/log-method-latency.model.js +0 -4
  529. package/models/log-method-latency.model.js.map +0 -1
  530. package/models/log-subscription.model.js +0 -4
  531. package/models/log-subscription.model.js.map +0 -1
  532. package/models/log.model.d.ts +0 -17
  533. package/models/log.model.js +0 -4
  534. package/models/log.model.js.map +0 -1
  535. package/models/logged-in-users.model.js +0 -4
  536. package/models/logged-in-users.model.js.map +0 -1
  537. package/models/method-response.model.js +0 -4
  538. package/models/method-response.model.js.map +0 -1
  539. package/models/method.model.d.ts +0 -26
  540. package/models/method.model.js +0 -4
  541. package/models/method.model.js.map +0 -1
  542. package/models/monitor-cpu.model.js +0 -4
  543. package/models/monitor-cpu.model.js.map +0 -1
  544. package/models/monitor-function.model.d.ts +0 -14
  545. package/models/monitor-function.model.js +0 -4
  546. package/models/monitor-function.model.js.map +0 -1
  547. package/models/monitor-memory.model.d.ts +0 -15
  548. package/models/monitor-memory.model.js +0 -4
  549. package/models/monitor-memory.model.js.map +0 -1
  550. package/models/monitor-mongo.model.d.ts +0 -13
  551. package/models/monitor-mongo.model.js +0 -4
  552. package/models/monitor-mongo.model.js.map +0 -1
  553. package/models/notification.model.js +0 -4
  554. package/models/notification.model.js.map +0 -1
  555. package/models/openai-usage-ledger.model.d.ts +0 -30
  556. package/models/openai-usage-ledger.model.js +0 -4
  557. package/models/openai-usage-ledger.model.js.map +0 -1
  558. package/models/pagination.model.d.ts +0 -11
  559. package/models/pagination.model.js +0 -28
  560. package/models/pagination.model.js.map +0 -1
  561. package/models/permission.model.d.ts +0 -12
  562. package/models/permission.model.js +0 -4
  563. package/models/permission.model.js.map +0 -1
  564. package/models/report-builder-dashboard-builder.model.d.ts +0 -25
  565. package/models/report-builder-dashboard-builder.model.js +0 -4
  566. package/models/report-builder-dashboard-builder.model.js.map +0 -1
  567. package/models/report-builder-library.model.d.ts +0 -17
  568. package/models/report-builder-library.model.js +0 -4
  569. package/models/report-builder-library.model.js.map +0 -1
  570. package/models/report-builder-report.model.d.ts +0 -121
  571. package/models/report-builder-report.model.js +0 -4
  572. package/models/report-builder-report.model.js.map +0 -1
  573. package/models/report-builder.model.d.ts +0 -61
  574. package/models/report-builder.model.js +0 -4
  575. package/models/report-builder.model.js.map +0 -1
  576. package/models/select-data-label.model.d.ts +0 -9
  577. package/models/select-data-label.model.js +0 -4
  578. package/models/select-data-label.model.js.map +0 -1
  579. package/models/server-message.model.d.ts +0 -32
  580. package/models/server-message.model.js +0 -4
  581. package/models/server-message.model.js.map +0 -1
  582. package/models/slow-query-report.model.d.ts +0 -23
  583. package/models/slow-query-report.model.js +0 -4
  584. package/models/slow-query-report.model.js.map +0 -1
  585. package/models/subscription.model.d.ts +0 -31
  586. package/models/subscription.model.js +0 -4
  587. package/models/subscription.model.js.map +0 -1
  588. package/models/support-ticket.model.d.ts +0 -87
  589. package/models/support-ticket.model.js +0 -4
  590. package/models/support-ticket.model.js.map +0 -1
  591. package/models/user-group.model.d.ts +0 -20
  592. package/models/user-group.model.js +0 -4
  593. package/models/user-group.model.js.map +0 -1
  594. package/models/user-guide.model.js +0 -4
  595. package/models/user-guide.model.js.map +0 -1
  596. package/models/user.model.d.ts +0 -84
  597. package/models/user.model.js +0 -4
  598. package/models/user.model.js.map +0 -1
  599. package/private/images/ResolveIO.png +0 -0
  600. package/public_api.js +0 -127
  601. package/public_api.js.map +0 -1
  602. package/publications/ai-terminal.d.ts +0 -1
  603. package/publications/ai-terminal.js +0 -122
  604. package/publications/ai-terminal.js.map +0 -1
  605. package/publications/app-settings.d.ts +0 -2
  606. package/publications/app-settings.js +0 -28
  607. package/publications/app-settings.js.map +0 -1
  608. package/publications/app-status.d.ts +0 -2
  609. package/publications/app-status.js +0 -16
  610. package/publications/app-status.js.map +0 -1
  611. package/publications/cron-jobs.d.ts +0 -2
  612. package/publications/cron-jobs.js +0 -88
  613. package/publications/cron-jobs.js.map +0 -1
  614. package/publications/customer-notifications.d.ts +0 -2
  615. package/publications/customer-notifications.js +0 -161
  616. package/publications/customer-notifications.js.map +0 -1
  617. package/publications/files.d.ts +0 -2
  618. package/publications/files.js +0 -36
  619. package/publications/files.js.map +0 -1
  620. package/publications/flags-update.d.ts +0 -2
  621. package/publications/flags-update.js +0 -22
  622. package/publications/flags-update.js.map +0 -1
  623. package/publications/flags.d.ts +0 -2
  624. package/publications/flags.js +0 -22
  625. package/publications/flags.js.map +0 -1
  626. package/publications/logs.d.ts +0 -2
  627. package/publications/logs.js +0 -164
  628. package/publications/logs.js.map +0 -1
  629. package/publications/notifications.d.ts +0 -2
  630. package/publications/notifications.js +0 -16
  631. package/publications/notifications.js.map +0 -1
  632. package/publications/report-builder-dashboard-builders.d.ts +0 -2
  633. package/publications/report-builder-dashboard-builders.js +0 -42
  634. package/publications/report-builder-dashboard-builders.js.map +0 -1
  635. package/publications/report-builder-libraries.d.ts +0 -2
  636. package/publications/report-builder-libraries.js +0 -90
  637. package/publications/report-builder-libraries.js.map +0 -1
  638. package/publications/report-builder-reports.d.ts +0 -2
  639. package/publications/report-builder-reports.js +0 -50
  640. package/publications/report-builder-reports.js.map +0 -1
  641. package/publications/super-admin.d.ts +0 -2
  642. package/publications/super-admin.js +0 -16
  643. package/publications/super-admin.js.map +0 -1
  644. package/publications/user-groups.d.ts +0 -1
  645. package/publications/user-groups.js +0 -16
  646. package/publications/user-groups.js.map +0 -1
  647. package/publications/user-guides.d.ts +0 -1
  648. package/publications/user-guides.js +0 -16
  649. package/publications/user-guides.js.map +0 -1
  650. package/resolveio-server-app.d.ts +0 -70
  651. package/resolveio-server-app.js +0 -801
  652. package/resolveio-server-app.js.map +0 -1
  653. package/server-app.d.ts +0 -228
  654. package/server-app.js +0 -3566
  655. package/server-app.js.map +0 -1
  656. package/services/codex-client.d.ts +0 -128
  657. package/services/codex-client.js +0 -1629
  658. package/services/codex-client.js.map +0 -1
  659. package/services/openai-client.d.ts +0 -46
  660. package/services/openai-client.js +0 -318
  661. package/services/openai-client.js.map +0 -1
  662. package/types/error-report.d.ts +0 -25
  663. package/types/error-report.js +0 -4
  664. package/types/error-report.js.map +0 -1
  665. package/types/slow-query-report.d.ts +0 -27
  666. package/types/slow-query-report.js +0 -6
  667. package/types/slow-query-report.js.map +0 -1
  668. package/util/ai-qa-policy.d.ts +0 -124
  669. package/util/ai-qa-policy.js +0 -736
  670. package/util/ai-qa-policy.js.map +0 -1
  671. package/util/ai-run-evidence-adapters.d.ts +0 -109
  672. package/util/ai-run-evidence-adapters.js +0 -7234
  673. package/util/ai-run-evidence-adapters.js.map +0 -1
  674. package/util/ai-run-evidence-dashboard.d.ts +0 -88
  675. package/util/ai-run-evidence-dashboard.js +0 -343
  676. package/util/ai-run-evidence-dashboard.js.map +0 -1
  677. package/util/ai-run-evidence-eval.d.ts +0 -86
  678. package/util/ai-run-evidence-eval.js +0 -1018
  679. package/util/ai-run-evidence-eval.js.map +0 -1
  680. package/util/ai-run-evidence.d.ts +0 -244
  681. package/util/ai-run-evidence.js +0 -1096
  682. package/util/ai-run-evidence.js.map +0 -1
  683. package/util/ai-runner-artifacts.d.ts +0 -82
  684. package/util/ai-runner-artifacts.js +0 -713
  685. package/util/ai-runner-artifacts.js.map +0 -1
  686. package/util/ai-runner-manager-autopilot.d.ts +0 -210
  687. package/util/ai-runner-manager-autopilot.js +0 -642
  688. package/util/ai-runner-manager-autopilot.js.map +0 -1
  689. package/util/ai-runner-manager-policy.d.ts +0 -807
  690. package/util/ai-runner-manager-policy.js +0 -3501
  691. package/util/ai-runner-manager-policy.js.map +0 -1
  692. package/util/ai-runner-qa-auth.d.ts +0 -5
  693. package/util/ai-runner-qa-auth.js +0 -839
  694. package/util/ai-runner-qa-auth.js.map +0 -1
  695. package/util/ai-runner-qa-tools.d.ts +0 -26
  696. package/util/ai-runner-qa-tools.js +0 -3520
  697. package/util/ai-runner-qa-tools.js.map +0 -1
  698. package/util/aicoder-runner-v6.d.ts +0 -426
  699. package/util/aicoder-runner-v6.js +0 -2464
  700. package/util/aicoder-runner-v6.js.map +0 -1
  701. package/util/common.d.ts +0 -31
  702. package/util/common.js +0 -683
  703. package/util/common.js.map +0 -1
  704. package/util/customer-portal-password.d.ts +0 -13
  705. package/util/customer-portal-password.js +0 -209
  706. package/util/customer-portal-password.js.map +0 -1
  707. package/util/error-reporter.d.ts +0 -52
  708. package/util/error-reporter.js +0 -326
  709. package/util/error-reporter.js.map +0 -1
  710. package/util/error-tracking.d.ts +0 -13
  711. package/util/error-tracking.js +0 -120
  712. package/util/error-tracking.js.map +0 -1
  713. package/util/openai-usage-cost.d.ts +0 -6
  714. package/util/openai-usage-cost.js +0 -103
  715. package/util/openai-usage-cost.js.map +0 -1
  716. package/util/report-builder-unwinds.d.ts +0 -15
  717. package/util/report-builder-unwinds.js +0 -156
  718. package/util/report-builder-unwinds.js.map +0 -1
  719. package/util/runner-process-janitor.d.ts +0 -27
  720. package/util/runner-process-janitor.js +0 -208
  721. package/util/runner-process-janitor.js.map +0 -1
  722. package/util/schema-report-builder.d.ts +0 -6
  723. package/util/schema-report-builder.js +0 -481
  724. package/util/schema-report-builder.js.map +0 -1
  725. package/util/slow-query-reporter.d.ts +0 -28
  726. package/util/slow-query-reporter.js +0 -226
  727. package/util/slow-query-reporter.js.map +0 -1
  728. package/util/subscription-dependency-context.d.ts +0 -34
  729. package/util/subscription-dependency-context.js +0 -1283
  730. package/util/subscription-dependency-context.js.map +0 -1
  731. package/util/support-runner-v5.d.ts +0 -1426
  732. package/util/support-runner-v5.js +0 -7631
  733. package/util/support-runner-v5.js.map +0 -1
  734. package/util/tokenizer.d.ts +0 -5
  735. package/util/tokenizer.js +0 -41
  736. package/util/tokenizer.js.map +0 -1
  737. package/workers/codex-runner.worker.d.ts +0 -1
  738. package/workers/codex-runner.worker.js +0 -192
  739. package/workers/codex-runner.worker.js.map +0 -1
  740. /package/{private → src/private}/email-templates/enrollment.html +0 -0
  741. /package/{private → src/private}/email-templates/forgot-password.html +0 -0
  742. /package/{private → src/private}/email-templates/support-ticket-deleted.html +0 -0
  743. /package/{private → src/private}/email-templates/support-ticket-modified.html +0 -0
  744. /package/{private → src/private}/email-templates/support-ticket.html +0 -0
  745. /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
+ }