@resolveio/server-lib 22.3.126 → 22.3.128

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