@jagilber-org/index-server 1.22.0

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 (372) hide show
  1. package/CHANGELOG.md +1354 -0
  2. package/CODE_OF_CONDUCT.md +49 -0
  3. package/CONTRIBUTING.md +99 -0
  4. package/LICENSE +21 -0
  5. package/README.md +228 -0
  6. package/SECURITY.md +50 -0
  7. package/dist/config/configUtils.d.ts +11 -0
  8. package/dist/config/configUtils.js +87 -0
  9. package/dist/config/dashboardConfig.d.ts +46 -0
  10. package/dist/config/dashboardConfig.js +67 -0
  11. package/dist/config/defaultValues.d.ts +63 -0
  12. package/dist/config/defaultValues.js +72 -0
  13. package/dist/config/dirConstants.d.ts +18 -0
  14. package/dist/config/dirConstants.js +29 -0
  15. package/dist/config/featureConfig.d.ts +61 -0
  16. package/dist/config/featureConfig.js +121 -0
  17. package/dist/config/runtimeConfig.d.ts +151 -0
  18. package/dist/config/runtimeConfig.js +380 -0
  19. package/dist/config/serverConfig.d.ts +90 -0
  20. package/dist/config/serverConfig.js +167 -0
  21. package/dist/dashboard/analytics/AnalyticsEngine.d.ts +142 -0
  22. package/dist/dashboard/analytics/AnalyticsEngine.js +373 -0
  23. package/dist/dashboard/analytics/BusinessIntelligence.d.ts +187 -0
  24. package/dist/dashboard/analytics/BusinessIntelligence.js +594 -0
  25. package/dist/dashboard/client/admin.html +2175 -0
  26. package/dist/dashboard/client/chunks/mermaid-layout-elk.esm.min/chunk-SP2CHFBE.mjs +1 -0
  27. package/dist/dashboard/client/chunks/mermaid-layout-elk.esm.min/render-T6MDALS3.mjs +27 -0
  28. package/dist/dashboard/client/css/admin.css +1587 -0
  29. package/dist/dashboard/client/js/admin.auth.js +179 -0
  30. package/dist/dashboard/client/js/admin.boot.js +359 -0
  31. package/dist/dashboard/client/js/admin.config.js +196 -0
  32. package/dist/dashboard/client/js/admin.embeddings.js +426 -0
  33. package/dist/dashboard/client/js/admin.graph.js +615 -0
  34. package/dist/dashboard/client/js/admin.instances.js +120 -0
  35. package/dist/dashboard/client/js/admin.instructions.js +579 -0
  36. package/dist/dashboard/client/js/admin.logs.js +113 -0
  37. package/dist/dashboard/client/js/admin.maintenance.js +393 -0
  38. package/dist/dashboard/client/js/admin.messaging.js +636 -0
  39. package/dist/dashboard/client/js/admin.monitor.js +184 -0
  40. package/dist/dashboard/client/js/admin.overview.js +221 -0
  41. package/dist/dashboard/client/js/admin.performance.js +61 -0
  42. package/dist/dashboard/client/js/admin.sessions.js +292 -0
  43. package/dist/dashboard/client/js/admin.sqlite.js +373 -0
  44. package/dist/dashboard/client/js/admin.utils.js +49 -0
  45. package/dist/dashboard/client/js/chart.umd.js +14 -0
  46. package/dist/dashboard/client/js/elk.bundled.js +6696 -0
  47. package/dist/dashboard/client/js/marked.umd.js +74 -0
  48. package/dist/dashboard/client/js/mermaid.min.js +3022 -0
  49. package/dist/dashboard/client/mermaid-layout-elk.esm.min.mjs +1 -0
  50. package/dist/dashboard/export/DataExporter.d.ts +169 -0
  51. package/dist/dashboard/export/DataExporter.js +737 -0
  52. package/dist/dashboard/export/exporters/csvExporter.d.ts +11 -0
  53. package/dist/dashboard/export/exporters/csvExporter.js +47 -0
  54. package/dist/dashboard/export/exporters/exportTypes.d.ts +89 -0
  55. package/dist/dashboard/export/exporters/exportTypes.js +5 -0
  56. package/dist/dashboard/export/exporters/jsonExporter.d.ts +7 -0
  57. package/dist/dashboard/export/exporters/jsonExporter.js +23 -0
  58. package/dist/dashboard/export/exporters/xmlExporter.d.ts +17 -0
  59. package/dist/dashboard/export/exporters/xmlExporter.js +176 -0
  60. package/dist/dashboard/integration/APIIntegration.d.ts +41 -0
  61. package/dist/dashboard/integration/APIIntegration.js +95 -0
  62. package/dist/dashboard/security/SecurityMonitor.d.ts +167 -0
  63. package/dist/dashboard/security/SecurityMonitor.js +560 -0
  64. package/dist/dashboard/server/AdminPanel.d.ts +195 -0
  65. package/dist/dashboard/server/AdminPanel.js +861 -0
  66. package/dist/dashboard/server/AdminPanelConfig.d.ts +42 -0
  67. package/dist/dashboard/server/AdminPanelConfig.js +80 -0
  68. package/dist/dashboard/server/AdminPanelState.d.ts +47 -0
  69. package/dist/dashboard/server/AdminPanelState.js +215 -0
  70. package/dist/dashboard/server/ApiRoutes.d.ts +17 -0
  71. package/dist/dashboard/server/ApiRoutes.js +184 -0
  72. package/dist/dashboard/server/DashboardServer.d.ts +49 -0
  73. package/dist/dashboard/server/DashboardServer.js +160 -0
  74. package/dist/dashboard/server/FileMetricsStorage.d.ts +49 -0
  75. package/dist/dashboard/server/FileMetricsStorage.js +196 -0
  76. package/dist/dashboard/server/HttpTransport.d.ts +23 -0
  77. package/dist/dashboard/server/HttpTransport.js +116 -0
  78. package/dist/dashboard/server/InstanceManager.d.ts +53 -0
  79. package/dist/dashboard/server/InstanceManager.js +295 -0
  80. package/dist/dashboard/server/KnowledgeStore.d.ts +35 -0
  81. package/dist/dashboard/server/KnowledgeStore.js +105 -0
  82. package/dist/dashboard/server/LeaderElection.d.ts +81 -0
  83. package/dist/dashboard/server/LeaderElection.js +268 -0
  84. package/dist/dashboard/server/MetricsCollector.d.ts +200 -0
  85. package/dist/dashboard/server/MetricsCollector.js +810 -0
  86. package/dist/dashboard/server/SessionPersistenceManager.d.ts +88 -0
  87. package/dist/dashboard/server/SessionPersistenceManager.js +458 -0
  88. package/dist/dashboard/server/ThinClient.d.ts +64 -0
  89. package/dist/dashboard/server/ThinClient.js +237 -0
  90. package/dist/dashboard/server/WebSocketManager.d.ts +161 -0
  91. package/dist/dashboard/server/WebSocketManager.js +448 -0
  92. package/dist/dashboard/server/httpLifecycle.d.ts +17 -0
  93. package/dist/dashboard/server/httpLifecycle.js +35 -0
  94. package/dist/dashboard/server/legacyDashboardHtml.d.ts +9 -0
  95. package/dist/dashboard/server/legacyDashboardHtml.js +618 -0
  96. package/dist/dashboard/server/legacyDashboardStyles.d.ts +5 -0
  97. package/dist/dashboard/server/legacyDashboardStyles.js +490 -0
  98. package/dist/dashboard/server/metricsAggregation.d.ts +252 -0
  99. package/dist/dashboard/server/metricsAggregation.js +210 -0
  100. package/dist/dashboard/server/metricsSerializer.d.ts +25 -0
  101. package/dist/dashboard/server/metricsSerializer.js +195 -0
  102. package/dist/dashboard/server/middleware/ensureLoadedMiddleware.d.ts +25 -0
  103. package/dist/dashboard/server/middleware/ensureLoadedMiddleware.js +24 -0
  104. package/dist/dashboard/server/routes/admin.routes.d.ts +16 -0
  105. package/dist/dashboard/server/routes/admin.routes.js +574 -0
  106. package/dist/dashboard/server/routes/adminAuth.d.ts +4 -0
  107. package/dist/dashboard/server/routes/adminAuth.js +46 -0
  108. package/dist/dashboard/server/routes/alerts.routes.d.ts +7 -0
  109. package/dist/dashboard/server/routes/alerts.routes.js +91 -0
  110. package/dist/dashboard/server/routes/api.feedback.routes.d.ts +73 -0
  111. package/dist/dashboard/server/routes/api.feedback.routes.js +171 -0
  112. package/dist/dashboard/server/routes/api.instructions.routes.d.ts +101 -0
  113. package/dist/dashboard/server/routes/api.instructions.routes.js +213 -0
  114. package/dist/dashboard/server/routes/api.usage.routes.d.ts +57 -0
  115. package/dist/dashboard/server/routes/api.usage.routes.js +374 -0
  116. package/dist/dashboard/server/routes/embeddings.routes.d.ts +6 -0
  117. package/dist/dashboard/server/routes/embeddings.routes.js +246 -0
  118. package/dist/dashboard/server/routes/graph.routes.d.ts +6 -0
  119. package/dist/dashboard/server/routes/graph.routes.js +279 -0
  120. package/dist/dashboard/server/routes/index.d.ts +39 -0
  121. package/dist/dashboard/server/routes/index.js +229 -0
  122. package/dist/dashboard/server/routes/instances.routes.d.ts +6 -0
  123. package/dist/dashboard/server/routes/instances.routes.js +35 -0
  124. package/dist/dashboard/server/routes/instructions.routes.d.ts +8 -0
  125. package/dist/dashboard/server/routes/instructions.routes.js +268 -0
  126. package/dist/dashboard/server/routes/knowledge.routes.d.ts +6 -0
  127. package/dist/dashboard/server/routes/knowledge.routes.js +80 -0
  128. package/dist/dashboard/server/routes/logs.routes.d.ts +6 -0
  129. package/dist/dashboard/server/routes/logs.routes.js +166 -0
  130. package/dist/dashboard/server/routes/messaging.routes.d.ts +16 -0
  131. package/dist/dashboard/server/routes/messaging.routes.js +307 -0
  132. package/dist/dashboard/server/routes/metrics.routes.d.ts +10 -0
  133. package/dist/dashboard/server/routes/metrics.routes.js +335 -0
  134. package/dist/dashboard/server/routes/scripts.routes.d.ts +9 -0
  135. package/dist/dashboard/server/routes/scripts.routes.js +84 -0
  136. package/dist/dashboard/server/routes/sqlite.routes.d.ts +9 -0
  137. package/dist/dashboard/server/routes/sqlite.routes.js +570 -0
  138. package/dist/dashboard/server/routes/status.routes.d.ts +7 -0
  139. package/dist/dashboard/server/routes/status.routes.js +179 -0
  140. package/dist/dashboard/server/routes/synthetic.routes.d.ts +7 -0
  141. package/dist/dashboard/server/routes/synthetic.routes.js +197 -0
  142. package/dist/dashboard/server/routes/tools.routes.d.ts +6 -0
  143. package/dist/dashboard/server/routes/tools.routes.js +47 -0
  144. package/dist/dashboard/server/routes/usage.routes.d.ts +6 -0
  145. package/dist/dashboard/server/routes/usage.routes.js +26 -0
  146. package/dist/dashboard/server/wsInit.d.ts +16 -0
  147. package/dist/dashboard/server/wsInit.js +35 -0
  148. package/dist/externalClientLib.d.ts +1 -0
  149. package/dist/externalClientLib.js +2 -0
  150. package/dist/minimal/index.d.ts +1 -0
  151. package/dist/minimal/index.js +140 -0
  152. package/dist/models/SessionPersistence.d.ts +115 -0
  153. package/dist/models/SessionPersistence.js +66 -0
  154. package/dist/models/instruction.d.ts +46 -0
  155. package/dist/models/instruction.js +2 -0
  156. package/dist/perf/benchmark.d.ts +1 -0
  157. package/dist/perf/benchmark.js +50 -0
  158. package/dist/portableClientWrapper.d.ts +1 -0
  159. package/dist/portableClientWrapper.js +2 -0
  160. package/dist/schemas/index.d.ts +132 -0
  161. package/dist/schemas/index.js +372 -0
  162. package/dist/scripts/runPerformanceBaseline.d.ts +1 -0
  163. package/dist/scripts/runPerformanceBaseline.js +17 -0
  164. package/dist/server/backgroundServicesStartup.d.ts +3 -0
  165. package/dist/server/backgroundServicesStartup.js +51 -0
  166. package/dist/server/handshakeManager.d.ts +25 -0
  167. package/dist/server/handshakeManager.js +470 -0
  168. package/dist/server/index-server.d.ts +38 -0
  169. package/dist/server/index-server.js +620 -0
  170. package/dist/server/multiInstanceStartup.d.ts +6 -0
  171. package/dist/server/multiInstanceStartup.js +132 -0
  172. package/dist/server/registry.d.ts +44 -0
  173. package/dist/server/registry.js +236 -0
  174. package/dist/server/sdkServer.d.ts +8 -0
  175. package/dist/server/sdkServer.js +299 -0
  176. package/dist/server/shutdownGuard.d.ts +41 -0
  177. package/dist/server/shutdownGuard.js +52 -0
  178. package/dist/server/startupDiagnostics.d.ts +2 -0
  179. package/dist/server/startupDiagnostics.js +33 -0
  180. package/dist/server/thin-client.d.ts +22 -0
  181. package/dist/server/thin-client.js +111 -0
  182. package/dist/server/transport.d.ts +41 -0
  183. package/dist/server/transport.js +312 -0
  184. package/dist/server/transportFactory.d.ts +21 -0
  185. package/dist/server/transportFactory.js +429 -0
  186. package/dist/services/atomicFs.d.ts +22 -0
  187. package/dist/services/atomicFs.js +103 -0
  188. package/dist/services/auditLog.d.ts +38 -0
  189. package/dist/services/auditLog.js +142 -0
  190. package/dist/services/autoBackup.d.ts +14 -0
  191. package/dist/services/autoBackup.js +171 -0
  192. package/dist/services/autoSplit.d.ts +32 -0
  193. package/dist/services/autoSplit.js +113 -0
  194. package/dist/services/backupZip.d.ts +25 -0
  195. package/dist/services/backupZip.js +112 -0
  196. package/dist/services/bootstrapGating.d.ts +123 -0
  197. package/dist/services/bootstrapGating.js +221 -0
  198. package/dist/services/canonical.d.ts +23 -0
  199. package/dist/services/canonical.js +65 -0
  200. package/dist/services/categoryRules.d.ts +7 -0
  201. package/dist/services/categoryRules.js +37 -0
  202. package/dist/services/classificationService.d.ts +42 -0
  203. package/dist/services/classificationService.js +168 -0
  204. package/dist/services/embeddingService.d.ts +62 -0
  205. package/dist/services/embeddingService.js +264 -0
  206. package/dist/services/errors.d.ts +22 -0
  207. package/dist/services/errors.js +31 -0
  208. package/dist/services/featureFlags.d.ts +25 -0
  209. package/dist/services/featureFlags.js +89 -0
  210. package/dist/services/features.d.ts +13 -0
  211. package/dist/services/features.js +35 -0
  212. package/dist/services/handlers/instructions.add.d.ts +1 -0
  213. package/dist/services/handlers/instructions.add.js +510 -0
  214. package/dist/services/handlers/instructions.groom.d.ts +1 -0
  215. package/dist/services/handlers/instructions.groom.js +575 -0
  216. package/dist/services/handlers/instructions.import.d.ts +1 -0
  217. package/dist/services/handlers/instructions.import.js +205 -0
  218. package/dist/services/handlers/instructions.patch.d.ts +1 -0
  219. package/dist/services/handlers/instructions.patch.js +121 -0
  220. package/dist/services/handlers/instructions.query.d.ts +159 -0
  221. package/dist/services/handlers/instructions.query.js +469 -0
  222. package/dist/services/handlers/instructions.reload.d.ts +1 -0
  223. package/dist/services/handlers/instructions.reload.js +13 -0
  224. package/dist/services/handlers/instructions.remove.d.ts +1 -0
  225. package/dist/services/handlers/instructions.remove.js +122 -0
  226. package/dist/services/handlers/instructions.shared.d.ts +32 -0
  227. package/dist/services/handlers/instructions.shared.js +91 -0
  228. package/dist/services/handlers.activation.d.ts +1 -0
  229. package/dist/services/handlers.activation.js +203 -0
  230. package/dist/services/handlers.bootstrap.d.ts +1 -0
  231. package/dist/services/handlers.bootstrap.js +38 -0
  232. package/dist/services/handlers.dashboardConfig.d.ts +34 -0
  233. package/dist/services/handlers.dashboardConfig.js +110 -0
  234. package/dist/services/handlers.diagnostics.d.ts +1 -0
  235. package/dist/services/handlers.diagnostics.js +64 -0
  236. package/dist/services/handlers.feedback.d.ts +15 -0
  237. package/dist/services/handlers.feedback.js +389 -0
  238. package/dist/services/handlers.gates.d.ts +1 -0
  239. package/dist/services/handlers.gates.js +47 -0
  240. package/dist/services/handlers.graph.d.ts +53 -0
  241. package/dist/services/handlers.graph.js +231 -0
  242. package/dist/services/handlers.help.d.ts +1 -0
  243. package/dist/services/handlers.help.js +119 -0
  244. package/dist/services/handlers.instructionSchema.d.ts +1 -0
  245. package/dist/services/handlers.instructionSchema.js +227 -0
  246. package/dist/services/handlers.instructions.d.ts +8 -0
  247. package/dist/services/handlers.instructions.js +14 -0
  248. package/dist/services/handlers.instructionsDiagnostics.d.ts +1 -0
  249. package/dist/services/handlers.instructionsDiagnostics.js +14 -0
  250. package/dist/services/handlers.integrity.d.ts +1 -0
  251. package/dist/services/handlers.integrity.js +35 -0
  252. package/dist/services/handlers.manifest.d.ts +1 -0
  253. package/dist/services/handlers.manifest.js +24 -0
  254. package/dist/services/handlers.messaging.d.ts +12 -0
  255. package/dist/services/handlers.messaging.js +203 -0
  256. package/dist/services/handlers.metrics.d.ts +1 -0
  257. package/dist/services/handlers.metrics.js +43 -0
  258. package/dist/services/handlers.promote.d.ts +1 -0
  259. package/dist/services/handlers.promote.js +326 -0
  260. package/dist/services/handlers.prompt.d.ts +1 -0
  261. package/dist/services/handlers.prompt.js +7 -0
  262. package/dist/services/handlers.search.d.ts +69 -0
  263. package/dist/services/handlers.search.js +669 -0
  264. package/dist/services/handlers.testPrimitive.d.ts +1 -0
  265. package/dist/services/handlers.testPrimitive.js +5 -0
  266. package/dist/services/handlers.trace.d.ts +1 -0
  267. package/dist/services/handlers.trace.js +35 -0
  268. package/dist/services/handlers.usage.d.ts +1 -0
  269. package/dist/services/handlers.usage.js +11 -0
  270. package/dist/services/hotScore.d.ts +137 -0
  271. package/dist/services/hotScore.js +244 -0
  272. package/dist/services/indexContext.d.ts +117 -0
  273. package/dist/services/indexContext.js +989 -0
  274. package/dist/services/indexLoader.d.ts +44 -0
  275. package/dist/services/indexLoader.js +920 -0
  276. package/dist/services/indexRepository.d.ts +32 -0
  277. package/dist/services/indexRepository.js +71 -0
  278. package/dist/services/indexingService.d.ts +1 -0
  279. package/dist/services/indexingService.js +2 -0
  280. package/dist/services/instructions.dispatcher.d.ts +1 -0
  281. package/dist/services/instructions.dispatcher.js +231 -0
  282. package/dist/services/logPrefix.d.ts +1 -0
  283. package/dist/services/logPrefix.js +30 -0
  284. package/dist/services/logger.d.ts +52 -0
  285. package/dist/services/logger.js +268 -0
  286. package/dist/services/manifestManager.d.ts +82 -0
  287. package/dist/services/manifestManager.js +200 -0
  288. package/dist/services/messaging/agentMailbox.d.ts +60 -0
  289. package/dist/services/messaging/agentMailbox.js +353 -0
  290. package/dist/services/messaging/messagingPersistence.d.ts +20 -0
  291. package/dist/services/messaging/messagingPersistence.js +111 -0
  292. package/dist/services/messaging/messagingTypes.d.ts +150 -0
  293. package/dist/services/messaging/messagingTypes.js +66 -0
  294. package/dist/services/ownershipService.d.ts +1 -0
  295. package/dist/services/ownershipService.js +36 -0
  296. package/dist/services/performanceBaseline.d.ts +19 -0
  297. package/dist/services/performanceBaseline.js +210 -0
  298. package/dist/services/preflight.d.ts +12 -0
  299. package/dist/services/preflight.js +79 -0
  300. package/dist/services/promptReviewService.d.ts +44 -0
  301. package/dist/services/promptReviewService.js +101 -0
  302. package/dist/services/responseEnvelope.d.ts +6 -0
  303. package/dist/services/responseEnvelope.js +25 -0
  304. package/dist/services/seedBootstrap.d.ts +34 -0
  305. package/dist/services/seedBootstrap.js +259 -0
  306. package/dist/services/storage/factory.d.ts +17 -0
  307. package/dist/services/storage/factory.js +35 -0
  308. package/dist/services/storage/hashUtils.d.ts +11 -0
  309. package/dist/services/storage/hashUtils.js +35 -0
  310. package/dist/services/storage/index.d.ts +12 -0
  311. package/dist/services/storage/index.js +18 -0
  312. package/dist/services/storage/jsonFileStore.d.ts +32 -0
  313. package/dist/services/storage/jsonFileStore.js +241 -0
  314. package/dist/services/storage/migrationEngine.d.ts +35 -0
  315. package/dist/services/storage/migrationEngine.js +93 -0
  316. package/dist/services/storage/sqliteMessageStore.d.ts +53 -0
  317. package/dist/services/storage/sqliteMessageStore.js +146 -0
  318. package/dist/services/storage/sqliteSchema.d.ts +12 -0
  319. package/dist/services/storage/sqliteSchema.js +123 -0
  320. package/dist/services/storage/sqliteStore.d.ts +42 -0
  321. package/dist/services/storage/sqliteStore.js +361 -0
  322. package/dist/services/storage/sqliteUsageStore.d.ts +35 -0
  323. package/dist/services/storage/sqliteUsageStore.js +94 -0
  324. package/dist/services/storage/types.d.ts +171 -0
  325. package/dist/services/storage/types.js +12 -0
  326. package/dist/services/toolHandlers.d.ts +23 -0
  327. package/dist/services/toolHandlers.js +50 -0
  328. package/dist/services/toolRegistry.d.ts +20 -0
  329. package/dist/services/toolRegistry.js +490 -0
  330. package/dist/services/toolRegistry.zod.d.ts +10 -0
  331. package/dist/services/toolRegistry.zod.js +325 -0
  332. package/dist/services/tracing.d.ts +26 -0
  333. package/dist/services/tracing.js +260 -0
  334. package/dist/services/usageBuckets.d.ts +161 -0
  335. package/dist/services/usageBuckets.js +364 -0
  336. package/dist/services/validationService.d.ts +38 -0
  337. package/dist/services/validationService.js +125 -0
  338. package/dist/utils/BufferRing.d.ts +203 -0
  339. package/dist/utils/BufferRing.js +551 -0
  340. package/dist/utils/BufferRingExamples.d.ts +55 -0
  341. package/dist/utils/BufferRingExamples.js +188 -0
  342. package/dist/utils/envUtils.d.ts +42 -0
  343. package/dist/utils/envUtils.js +80 -0
  344. package/dist/utils/memoryMonitor.d.ts +83 -0
  345. package/dist/utils/memoryMonitor.js +275 -0
  346. package/dist/versioning/schemaVersion.d.ts +6 -0
  347. package/dist/versioning/schemaVersion.js +94 -0
  348. package/package.json +139 -0
  349. package/schemas/README.md +13 -0
  350. package/schemas/feedback-entry.schema.json +27 -0
  351. package/schemas/graph-export-v2.schema.json +60 -0
  352. package/schemas/index-server.code-schema.json +40670 -0
  353. package/schemas/instruction.schema.json +262 -0
  354. package/schemas/json-schema/SessionPersistence-persisted-admin-session.schema.json +54 -0
  355. package/schemas/json-schema/SessionPersistence-persisted-session-history-entry.schema.json +51 -0
  356. package/schemas/json-schema/SessionPersistence-persisted-web-socket-connection.schema.json +54 -0
  357. package/schemas/json-schema/SessionPersistence-session-persistence-config.schema.json +110 -0
  358. package/schemas/json-schema/SessionPersistence-session-persistence-data.schema.json +229 -0
  359. package/schemas/json-schema/SessionPersistence-session-persistence-manifest.schema.json +109 -0
  360. package/schemas/json-schema/SessionPersistence-session-persistence-metadata.schema.json +55 -0
  361. package/schemas/json-schema/instruction-audience-scope.schema.json +14 -0
  362. package/schemas/json-schema/instruction-content-type.schema.json +17 -0
  363. package/schemas/json-schema/instruction-instruction-entry.schema.json +210 -0
  364. package/schemas/json-schema/instruction-requirement-level.schema.json +16 -0
  365. package/schemas/manifest.json +78 -0
  366. package/schemas/manifest.schema.json +33 -0
  367. package/schemas/usage-batch.schema.json +16 -0
  368. package/schemas/usage-buckets.schema.json +30 -0
  369. package/schemas/usage-event.schema.json +17 -0
  370. package/scripts/copy-dashboard-assets.mjs +170 -0
  371. package/scripts/dist/README.md +15 -0
  372. package/scripts/setup-hooks.cjs +28 -0
@@ -0,0 +1,810 @@
1
+ "use strict";
2
+ /**
3
+ * MetricsCollector — coordinator class (Phase 1–4 Dashboard Foundation).
4
+ *
5
+ * Owns all mutable metrics state and delegates:
6
+ * - pure math to metricsAggregation.ts
7
+ * - output serialisation to metricsSerializer.ts
8
+ */
9
+ var __importDefault = (this && this.__importDefault) || function (mod) {
10
+ return (mod && mod.__esModule) ? mod : { "default": mod };
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ exports.MetricsCollector = void 0;
14
+ exports.getMetricsCollector = getMetricsCollector;
15
+ exports.setMetricsCollector = setMetricsCollector;
16
+ const fs_1 = __importDefault(require("fs"));
17
+ const path_1 = __importDefault(require("path"));
18
+ const v8_1 = __importDefault(require("v8"));
19
+ const FileMetricsStorage_js_1 = require("./FileMetricsStorage.js");
20
+ const BufferRing_js_1 = require("../../utils/BufferRing.js");
21
+ const runtimeConfig_1 = require("../../config/runtimeConfig");
22
+ const logger_js_1 = require("../../services/logger.js");
23
+ const metricsAggregation_js_1 = require("./metricsAggregation.js");
24
+ const metricsSerializer_js_1 = require("./metricsSerializer.js");
25
+ class MetricsCollector {
26
+ tools = new Map();
27
+ // Resource usage samples (CPU/Memory) for leak/trend analysis
28
+ resourceSamples;
29
+ lastCpuUsageSample = null;
30
+ lastCpuSampleTime = 0;
31
+ snapshots = []; // Keep small in-memory cache for real-time queries
32
+ connections = new Set();
33
+ disconnectedCount = 0;
34
+ totalSessionTime = 0;
35
+ sessionStartTimes = new Map();
36
+ startTime = Date.now();
37
+ options;
38
+ collectTimer;
39
+ lastCpuUsage;
40
+ // Phase 4: Advanced features
41
+ recentAlerts = [];
42
+ activeConnections = 0;
43
+ // Rolling timestamp buffer for recent tool calls (used for stable RPM calculation)
44
+ recentCallTimestamps = [];
45
+ static MAX_RECENT_CALLS = 10000; // cap to avoid unbounded growth
46
+ static MAX_MEMORY_SNAPSHOTS = 60; // Keep only 1 hour in memory
47
+ static MAX_TOOL_METRICS = 1000; // cap tool metrics to prevent memory leaks
48
+ // File storage for historical data (optional)
49
+ fileStorage = null;
50
+ useFileStorage;
51
+ // BufferRing-Enhanced Storage
52
+ historicalSnapshots;
53
+ toolCallEvents;
54
+ // Persistence throttling state for tool call events (defined explicitly to avoid dynamic props)
55
+ _lastToolPersist = 0;
56
+ _pendingToolPersist = 0;
57
+ performanceMetrics;
58
+ bufferConfig;
59
+ // Append/segment logging state (optional)
60
+ appendMode = false;
61
+ appendLogPath = null;
62
+ pendingAppendEvents = [];
63
+ lastAppendFlush = 0;
64
+ lastAppendCompact = 0;
65
+ appendChunkSize = 250;
66
+ appendFlushMs = 5000;
67
+ appendCompactMs = 5 * 60 * 1000; // 5 min default
68
+ constructor(options = {}) {
69
+ this.options = {
70
+ retentionMinutes: options.retentionMinutes ?? 60,
71
+ maxSnapshots: options.maxSnapshots ?? 720, // 12 hours at 1-minute intervals
72
+ collectInterval: options.collectInterval ?? 60000, // 1 minute
73
+ };
74
+ const metricsConfig = (0, runtimeConfig_1.getRuntimeConfig)().metrics;
75
+ // Initialize resource sampling buffer (default capacity ~1h at 5s interval = 720)
76
+ const resourceCapacity = Math.max(1, metricsConfig.resourceCapacity || 720);
77
+ this.resourceSamples = new BufferRing_js_1.BufferRing({
78
+ capacity: resourceCapacity,
79
+ overflowStrategy: BufferRing_js_1.OverflowStrategy.DROP_OLDEST,
80
+ autoPersist: false,
81
+ enableIntegrityCheck: false
82
+ });
83
+ try {
84
+ const intervalMs = Math.max(100, metricsConfig.sampleIntervalMs || 5000);
85
+ setInterval(() => this.sampleResources(), intervalMs).unref();
86
+ }
87
+ catch { /* ignore */ }
88
+ // Configure BufferRing settings
89
+ const metricsDir = metricsConfig.dir || path_1.default.join(process.cwd(), 'metrics');
90
+ this.bufferConfig = {
91
+ historicalSnapshots: {
92
+ capacity: this.options.maxSnapshots,
93
+ retentionMinutes: this.options.retentionMinutes * 12, // Keep 12x longer than original
94
+ persistenceFile: path_1.default.join(metricsDir, 'historical-snapshots.json')
95
+ },
96
+ toolCallEvents: {
97
+ capacity: 10000, // Store last 10k tool calls
98
+ retentionMinutes: this.options.retentionMinutes * 2, // 2 hours of tool calls
99
+ persistenceFile: path_1.default.join(metricsDir, 'tool-call-events.json')
100
+ },
101
+ performanceMetrics: {
102
+ capacity: 1440, // Store 24 hours worth of minute-by-minute metrics
103
+ persistenceFile: path_1.default.join(metricsDir, 'performance-metrics.json')
104
+ }
105
+ };
106
+ // Check if file storage should be enabled (accept "true", "1", "yes", "on")
107
+ this.useFileStorage = metricsConfig.fileStorage;
108
+ // Initialize BufferRing storage
109
+ this.historicalSnapshots = new BufferRing_js_1.BufferRing({
110
+ capacity: this.bufferConfig.historicalSnapshots.capacity,
111
+ overflowStrategy: BufferRing_js_1.OverflowStrategy.DROP_OLDEST,
112
+ persistPath: this.useFileStorage ? this.bufferConfig.historicalSnapshots.persistenceFile : undefined,
113
+ autoPersist: this.useFileStorage
114
+ });
115
+ this.toolCallEvents = new BufferRing_js_1.BufferRing({
116
+ capacity: this.bufferConfig.toolCallEvents.capacity,
117
+ overflowStrategy: BufferRing_js_1.OverflowStrategy.DROP_OLDEST,
118
+ persistPath: this.useFileStorage ? this.bufferConfig.toolCallEvents.persistenceFile : undefined,
119
+ autoPersist: false, // we'll manage chunked persistence manually for performance
120
+ suppressPersistLog: true
121
+ });
122
+ this.performanceMetrics = new BufferRing_js_1.BufferRing({
123
+ capacity: this.bufferConfig.performanceMetrics.capacity,
124
+ overflowStrategy: BufferRing_js_1.OverflowStrategy.DROP_OLDEST,
125
+ persistPath: this.useFileStorage ? this.bufferConfig.performanceMetrics.persistenceFile : undefined,
126
+ autoPersist: this.useFileStorage
127
+ });
128
+ // Configure optional append-only logging for tool call events (reduces large snapshot writes)
129
+ if (this.useFileStorage) {
130
+ this.appendMode = metricsConfig.toolcall.appendLogEnabled;
131
+ if (this.appendMode) {
132
+ this.appendLogPath = path_1.default.join(path_1.default.dirname(this.bufferConfig.toolCallEvents.persistenceFile), 'tool-call-events.ndjson');
133
+ this.appendChunkSize = Math.max(1, metricsConfig.toolcall.chunkSize || this.appendChunkSize);
134
+ this.appendFlushMs = Math.max(1, metricsConfig.toolcall.flushMs || this.appendFlushMs);
135
+ this.appendCompactMs = Math.max(1, metricsConfig.toolcall.compactMs || this.appendCompactMs);
136
+ try {
137
+ // Load any un-compacted append log tail (best-effort)
138
+ if (this.appendLogPath) {
139
+ try {
140
+ const stat = fs_1.default.statSync(this.appendLogPath);
141
+ if (stat.size < 25 * 1024 * 1024) { // safety cap 25MB
142
+ const raw = fs_1.default.readFileSync(this.appendLogPath, 'utf8');
143
+ const lines = raw.split(/\r?\n/).filter(l => l.trim().length > 0);
144
+ for (const line of lines) {
145
+ try {
146
+ const evt = JSON.parse(line);
147
+ this.toolCallEvents.add(evt);
148
+ }
149
+ catch { /* ignore bad line */ }
150
+ }
151
+ }
152
+ else {
153
+ (0, logger_js_1.logWarn)('[MetricsCollector] tool-call-events.ndjson too large to preload (>25MB); will rely on last snapshot');
154
+ }
155
+ }
156
+ catch (statErr) {
157
+ if (statErr.code !== 'ENOENT')
158
+ throw statErr;
159
+ }
160
+ }
161
+ }
162
+ catch (err) {
163
+ (0, logger_js_1.logWarn)('[MetricsCollector] Failed to preload append log', err);
164
+ }
165
+ }
166
+ }
167
+ if (this.useFileStorage) {
168
+ // Initialize legacy file storage for backward compatibility
169
+ this.fileStorage = (0, FileMetricsStorage_js_1.getFileMetricsStorage)({
170
+ storageDir: metricsDir,
171
+ maxFiles: this.options.maxSnapshots,
172
+ retentionMinutes: this.options.retentionMinutes,
173
+ });
174
+ (0, logger_js_1.logInfo)('[MetricsCollector] BufferRing + File storage enabled');
175
+ }
176
+ else {
177
+ (0, logger_js_1.logInfo)('[MetricsCollector] BufferRing memory-only mode (set INDEX_SERVER_METRICS_FILE_STORAGE=1|true|yes|on for persistence)');
178
+ }
179
+ // Start periodic collection
180
+ this.startCollection();
181
+ }
182
+ /**
183
+ * Record a tool call event
184
+ */
185
+ recordToolCall(toolName, success, responseTimeMs, errorType, clientId) {
186
+ const now = Date.now();
187
+ if (!this.tools.has(toolName)) {
188
+ this.tools.set(toolName, {
189
+ callCount: 0,
190
+ successCount: 0,
191
+ errorCount: 0,
192
+ totalResponseTime: 0,
193
+ errorTypes: {},
194
+ });
195
+ }
196
+ const metrics = this.tools.get(toolName);
197
+ metrics.callCount++;
198
+ metrics.totalResponseTime += responseTimeMs;
199
+ metrics.lastCalled = now;
200
+ if (success) {
201
+ metrics.successCount++;
202
+ }
203
+ else {
204
+ metrics.errorCount++;
205
+ if (errorType) {
206
+ metrics.errorTypes[errorType] = (metrics.errorTypes[errorType] || 0) + 1;
207
+ }
208
+ }
209
+ // Store detailed tool call event in BufferRing
210
+ this.toolCallEvents.add({
211
+ timestamp: now,
212
+ toolName,
213
+ success,
214
+ responseTimeMs,
215
+ errorType,
216
+ clientId
217
+ });
218
+ if (this.useFileStorage) {
219
+ if (this.appendMode) {
220
+ this.pendingAppendEvents.push({ timestamp: now, toolName, success, responseTimeMs, errorType, clientId });
221
+ this.flushToolCallEvents(false); // schedule conditional flush
222
+ }
223
+ else {
224
+ // legacy snapshot batching (retain previous throttling fields)
225
+ this._pendingToolPersist++;
226
+ const dueTime = now - this._lastToolPersist > this.appendFlushMs;
227
+ if (this._pendingToolPersist >= this.appendChunkSize || dueTime) {
228
+ setTimeout(() => {
229
+ this.toolCallEvents.saveToDisk().catch(() => { });
230
+ this._lastToolPersist = Date.now();
231
+ this._pendingToolPersist = 0;
232
+ }, 0).unref?.();
233
+ }
234
+ }
235
+ }
236
+ // Track call timestamp for rolling RPM calculation (last 60s window)
237
+ this.recentCallTimestamps.push(now);
238
+ // Prune anything older than 5 minutes to constrain memory
239
+ const cutoff = now - 5 * 60 * 1000;
240
+ if (this.recentCallTimestamps.length > MetricsCollector.MAX_RECENT_CALLS || (this.recentCallTimestamps.length % 100) === 0) {
241
+ // Periodically prune (on every 100th push or when over cap)
242
+ let firstValidIdx = 0;
243
+ while (firstValidIdx < this.recentCallTimestamps.length && this.recentCallTimestamps[firstValidIdx] < cutoff)
244
+ firstValidIdx++;
245
+ if (firstValidIdx > 0)
246
+ this.recentCallTimestamps.splice(0, firstValidIdx);
247
+ // Hard cap safeguard
248
+ if (this.recentCallTimestamps.length > MetricsCollector.MAX_RECENT_CALLS) {
249
+ this.recentCallTimestamps.splice(0, this.recentCallTimestamps.length - MetricsCollector.MAX_RECENT_CALLS);
250
+ }
251
+ }
252
+ // Prevent unbounded tool metrics growth - cleanup old/unused tools after adding the new tool
253
+ if (this.tools.size > MetricsCollector.MAX_TOOL_METRICS) {
254
+ this.cleanupOldToolMetrics();
255
+ }
256
+ }
257
+ /** Flush tool call events (append or snapshot) */
258
+ flushToolCallEvents(force) {
259
+ if (!this.useFileStorage)
260
+ return;
261
+ const now = Date.now();
262
+ if (this.appendMode) {
263
+ const timeDue = (now - this.lastAppendFlush) >= this.appendFlushMs;
264
+ if (!force && this.pendingAppendEvents.length < this.appendChunkSize && !timeDue)
265
+ return;
266
+ if (!this.appendLogPath || this.pendingAppendEvents.length === 0)
267
+ return;
268
+ const toWrite = this.pendingAppendEvents.splice(0, this.pendingAppendEvents.length);
269
+ const lines = toWrite.map(e => JSON.stringify(e)).join('\n') + '\n';
270
+ fs_1.default.promises.appendFile(this.appendLogPath, lines).catch(() => { });
271
+ this.lastAppendFlush = now;
272
+ // Periodic compaction: write full snapshot & truncate log
273
+ if ((now - this.lastAppendCompact) >= this.appendCompactMs || force) {
274
+ this.toolCallEvents.saveToDisk().catch(() => { });
275
+ this.lastAppendCompact = now;
276
+ if (this.appendLogPath) {
277
+ fs_1.default.promises.writeFile(this.appendLogPath, '').catch(() => { }); // truncate
278
+ }
279
+ }
280
+ }
281
+ else {
282
+ // snapshot mode manual force
283
+ if (force) {
284
+ this.toolCallEvents.saveToDisk().catch(() => { });
285
+ this._lastToolPersist = now;
286
+ this._pendingToolPersist = 0;
287
+ }
288
+ }
289
+ }
290
+ /**
291
+ * Record client connection
292
+ */
293
+ recordConnection(clientId) {
294
+ this.connections.add(clientId);
295
+ this.sessionStartTimes.set(clientId, Date.now());
296
+ }
297
+ /**
298
+ * Record client disconnection
299
+ */
300
+ recordDisconnection(clientId) {
301
+ if (this.connections.has(clientId)) {
302
+ this.connections.delete(clientId);
303
+ this.disconnectedCount++;
304
+ const sessionStart = this.sessionStartTimes.get(clientId);
305
+ if (sessionStart) {
306
+ this.totalSessionTime += Date.now() - sessionStart;
307
+ this.sessionStartTimes.delete(clientId);
308
+ }
309
+ }
310
+ }
311
+ /**
312
+ * Get current metrics snapshot
313
+ */
314
+ getCurrentSnapshot() {
315
+ const now = Date.now();
316
+ const uptime = now - this.startTime;
317
+ // Calculate performance metrics
318
+ const totalCalls = Array.from(this.tools.values()).reduce((sum, tool) => sum + tool.callCount, 0);
319
+ const totalSuccesses = Array.from(this.tools.values()).reduce((sum, tool) => sum + tool.successCount, 0);
320
+ const totalErrors = Array.from(this.tools.values()).reduce((sum, tool) => sum + tool.errorCount, 0);
321
+ const totalResponseTime = Array.from(this.tools.values()).reduce((sum, tool) => sum + tool.totalResponseTime, 0);
322
+ // Stable rolling Requests Per Minute based on last 60s calls (not lifetime average)
323
+ const oneMinuteCutoff = now - 60 * 1000;
324
+ let recentCount = 0;
325
+ // Iterate from end backwards until we exit window (timestamps are append-only & ascending)
326
+ for (let i = this.recentCallTimestamps.length - 1; i >= 0; i--) {
327
+ const ts = this.recentCallTimestamps[i];
328
+ if (ts >= oneMinuteCutoff)
329
+ recentCount++;
330
+ else
331
+ break;
332
+ }
333
+ const requestsPerMinute = recentCount;
334
+ const successRate = totalCalls > 0 ? (totalSuccesses / totalCalls) * 100 : 100;
335
+ const avgResponseTime = totalCalls > 0 ? (totalResponseTime / totalCalls) : 0;
336
+ const errorRate = totalCalls > 0 ? (totalErrors / totalCalls) * 100 : 0;
337
+ // Calculate average session duration
338
+ const activeSessionTime = Array.from(this.sessionStartTimes.values())
339
+ .reduce((sum, startTime) => sum + (now - startTime), 0);
340
+ const totalSessions = this.disconnectedCount + this.connections.size;
341
+ const avgSessionDuration = totalSessions > 0
342
+ ? (this.totalSessionTime + activeSessionTime) / totalSessions
343
+ : 0;
344
+ // Get CPU usage (if available)
345
+ let cpuUsage;
346
+ try {
347
+ cpuUsage = process.cpuUsage(this.lastCpuUsage);
348
+ this.lastCpuUsage = process.cpuUsage();
349
+ }
350
+ catch {
351
+ // CPU usage not available on all platforms
352
+ }
353
+ const memUsage = process.memoryUsage();
354
+ let heapLimit;
355
+ try {
356
+ heapLimit = v8_1.default.getHeapStatistics().heap_size_limit;
357
+ }
358
+ catch { /* v8 not available */ }
359
+ return {
360
+ timestamp: now,
361
+ server: {
362
+ uptime,
363
+ version: this.getVersion(),
364
+ memoryUsage: { ...memUsage, ...(heapLimit ? { heapLimit } : {}) },
365
+ cpuUsage,
366
+ startTime: this.startTime,
367
+ },
368
+ tools: Object.fromEntries(this.tools.entries()),
369
+ connections: {
370
+ activeConnections: this.connections.size,
371
+ totalConnections: this.disconnectedCount + this.connections.size,
372
+ disconnectedConnections: this.disconnectedCount,
373
+ avgSessionDuration,
374
+ },
375
+ performance: {
376
+ requestsPerMinute,
377
+ successRate,
378
+ avgResponseTime,
379
+ errorRate,
380
+ },
381
+ };
382
+ }
383
+ /**
384
+ * Get historical snapshots (from memory for recent, or from files for historical)
385
+ */
386
+ getSnapshots(count) {
387
+ if (count) {
388
+ return this.snapshots.slice(-count);
389
+ }
390
+ return [...this.snapshots];
391
+ }
392
+ /**
393
+ * Get snapshots from file storage (for historical analysis)
394
+ */
395
+ async getHistoricalSnapshots(count = 720) {
396
+ if (!this.fileStorage) {
397
+ (0, logger_js_1.logWarn)('[MetricsCollector] File storage not enabled, returning memory snapshots only');
398
+ return this.getSnapshots(count);
399
+ }
400
+ try {
401
+ return await this.fileStorage.getRecentSnapshots(count);
402
+ }
403
+ catch (error) {
404
+ (0, logger_js_1.logError)('[MetricsCollector] Failed to load historical snapshots', error);
405
+ return [];
406
+ }
407
+ }
408
+ /**
409
+ * Get snapshots within a specific time range from files
410
+ */
411
+ async getSnapshotsInRange(startTime, endTime) {
412
+ if (!this.fileStorage) {
413
+ (0, logger_js_1.logWarn)('[MetricsCollector] File storage not enabled, filtering memory snapshots');
414
+ return this.snapshots.filter(s => s.timestamp >= startTime && s.timestamp <= endTime);
415
+ }
416
+ try {
417
+ return await this.fileStorage.getSnapshotsInRange(startTime, endTime);
418
+ }
419
+ catch (error) {
420
+ (0, logger_js_1.logError)('[MetricsCollector] Failed to load snapshots in range', error);
421
+ return [];
422
+ }
423
+ }
424
+ /**
425
+ * Get file storage statistics
426
+ */
427
+ async getStorageStats() {
428
+ if (!this.fileStorage) {
429
+ return {
430
+ fileCount: 0,
431
+ totalSizeKB: 0,
432
+ memorySnapshots: this.snapshots.length,
433
+ };
434
+ }
435
+ const fileStats = await this.fileStorage.getStorageStats();
436
+ return {
437
+ ...fileStats,
438
+ memorySnapshots: this.snapshots.length,
439
+ };
440
+ }
441
+ /**
442
+ * Get tool-specific metrics
443
+ */
444
+ getToolMetrics(toolName) {
445
+ if (toolName) {
446
+ return this.tools.get(toolName) || null;
447
+ }
448
+ return Object.fromEntries(this.tools.entries());
449
+ }
450
+ /**
451
+ * Clear all metrics data (memory and files)
452
+ */
453
+ async clearMetrics() {
454
+ // Clear in-memory data
455
+ this.tools.clear();
456
+ this.snapshots.length = 0;
457
+ this.connections.clear();
458
+ this.disconnectedCount = 0;
459
+ this.totalSessionTime = 0;
460
+ this.sessionStartTimes.clear();
461
+ this.startTime = Date.now();
462
+ this.recentCallTimestamps.length = 0;
463
+ // Clear file storage if enabled
464
+ if (this.fileStorage) {
465
+ try {
466
+ await this.fileStorage.clearAll();
467
+ }
468
+ catch (error) {
469
+ (0, logger_js_1.logError)('[MetricsCollector] Failed to clear file storage', error);
470
+ }
471
+ }
472
+ }
473
+ /**
474
+ * Clear only memory data (keep files)
475
+ */
476
+ clearMemoryMetrics() {
477
+ this.tools.clear();
478
+ this.snapshots.length = 0;
479
+ this.connections.clear();
480
+ this.disconnectedCount = 0;
481
+ this.totalSessionTime = 0;
482
+ this.sessionStartTimes.clear();
483
+ this.startTime = Date.now();
484
+ this.recentCallTimestamps.length = 0;
485
+ }
486
+ /**
487
+ * Stop metrics collection
488
+ */
489
+ stop() {
490
+ if (this.collectTimer) {
491
+ clearInterval(this.collectTimer);
492
+ this.collectTimer = undefined;
493
+ }
494
+ }
495
+ startCollection() {
496
+ // Take initial snapshot
497
+ this.takeSnapshot();
498
+ // Schedule periodic snapshots
499
+ this.collectTimer = setInterval(() => {
500
+ this.takeSnapshot();
501
+ }, this.options.collectInterval);
502
+ }
503
+ takeSnapshot() {
504
+ const snapshot = this.getCurrentSnapshot();
505
+ // Store enhanced snapshot in BufferRing with performance data
506
+ const timeSeriesEntry = {
507
+ timestamp: snapshot.timestamp,
508
+ snapshot,
509
+ performanceData: {
510
+ responseTimeMs: snapshot.performance.avgResponseTime,
511
+ requestCount: Array.from(this.tools.values()).reduce((sum, tool) => sum + tool.callCount, 0),
512
+ errorCount: Array.from(this.tools.values()).reduce((sum, tool) => sum + tool.errorCount, 0),
513
+ connectionCount: snapshot.connections.activeConnections
514
+ }
515
+ };
516
+ this.historicalSnapshots.add(timeSeriesEntry);
517
+ // Store performance metrics for charting
518
+ this.performanceMetrics.add({
519
+ timestamp: snapshot.timestamp,
520
+ responseTime: snapshot.performance.avgResponseTime,
521
+ throughput: snapshot.performance.requestsPerMinute,
522
+ errorRate: snapshot.performance.errorRate
523
+ });
524
+ // Store to file immediately (async, non-blocking) if file storage enabled
525
+ if (this.fileStorage) {
526
+ this.fileStorage.storeSnapshot(snapshot).catch(error => {
527
+ (0, logger_js_1.logError)('[MetricsCollector] Failed to store metrics snapshot to file', error);
528
+ });
529
+ }
530
+ // Keep limited snapshots in memory for real-time queries
531
+ this.snapshots.push(snapshot);
532
+ // Trim in-memory snapshots to prevent memory accumulation
533
+ const maxMemorySnapshots = this.useFileStorage
534
+ ? MetricsCollector.MAX_MEMORY_SNAPSHOTS
535
+ : this.options.maxSnapshots;
536
+ if (this.snapshots.length > maxMemorySnapshots) {
537
+ this.snapshots.splice(0, this.snapshots.length - maxMemorySnapshots);
538
+ }
539
+ // If not using file storage, apply original retention logic
540
+ if (!this.useFileStorage) {
541
+ const cutoff = Date.now() - (this.options.retentionMinutes * 60 * 1000);
542
+ const firstValidIndex = this.snapshots.findIndex(s => s.timestamp >= cutoff);
543
+ if (firstValidIndex > 0) {
544
+ this.snapshots.splice(0, firstValidIndex);
545
+ }
546
+ }
547
+ // Periodically cleanup tool metrics (every 10 snapshots, ~10 minutes)
548
+ if (this.snapshots.length % 10 === 0) {
549
+ this.cleanupOldToolMetrics();
550
+ }
551
+ }
552
+ getVersion() {
553
+ try {
554
+ const candidates = [
555
+ path_1.default.join(process.cwd(), 'package.json'),
556
+ path_1.default.join(__dirname, '..', '..', '..', 'package.json')
557
+ ];
558
+ for (const candidate of candidates) {
559
+ if (fs_1.default.existsSync(candidate)) {
560
+ const pkg = JSON.parse(fs_1.default.readFileSync(candidate, 'utf8'));
561
+ if (pkg?.version)
562
+ return pkg.version;
563
+ }
564
+ }
565
+ }
566
+ catch {
567
+ // Ignore errors
568
+ }
569
+ return '0.0.0';
570
+ }
571
+ // ====== Phase 3: Real-time Chart Data Methods ======
572
+ getRealtimeMetrics() {
573
+ return (0, metricsSerializer_js_1.buildRealtimeMetrics)(this.getCurrentSnapshot(), this.tools);
574
+ }
575
+ getToolUsageChartData(minutes = 60) {
576
+ return (0, metricsSerializer_js_1.buildToolUsageChartData)(this.snapshots, minutes);
577
+ }
578
+ getPerformanceChartData(minutes = 60) {
579
+ return (0, metricsSerializer_js_1.buildPerformanceChartData)(this.snapshots, minutes);
580
+ }
581
+ getTimeRangeMetrics(range) {
582
+ const rangeMinutes = { '1h': 60, '6h': 360, '24h': 1440, '7d': 10080, '30d': 43200 };
583
+ const cutoff = Date.now() - rangeMinutes[range] * 60_000;
584
+ return this.snapshots.filter(s => s.timestamp >= cutoff);
585
+ }
586
+ // ====== Phase 4: Advanced Real-time & Analytics Methods ======
587
+ getRealtimeStreamingData() {
588
+ const recentSnaps10 = this.snapshots.slice(-10);
589
+ const latency = recentSnaps10.length > 0
590
+ ? recentSnaps10.reduce((s, snap) => s + snap.performance.avgResponseTime, 0) / recentSnaps10.length
591
+ : 0;
592
+ return {
593
+ timestamp: Date.now(),
594
+ systemHealth: this.getSystemHealth(),
595
+ performanceMetrics: this.getDetailedPerformanceMetrics(),
596
+ recentActivity: (0, metricsSerializer_js_1.buildRecentActivity)(this.tools),
597
+ streamingStats: {
598
+ totalStreamingConnections: this.activeConnections,
599
+ dataTransferRate: this.connections.size * 0.1 + Math.random() * 0.5,
600
+ latency,
601
+ compressionRatio: 0.7,
602
+ },
603
+ };
604
+ }
605
+ getSystemHealth() {
606
+ const cpu = (0, metricsAggregation_js_1.estimateCPUUsage)(this.snapshots.slice(-5));
607
+ const memory = (0, metricsAggregation_js_1.estimateMemoryUsage)(this.connections.size, this.snapshots.length);
608
+ return {
609
+ cpuUsage: cpu,
610
+ memoryUsage: memory,
611
+ diskUsage: (0, metricsAggregation_js_1.estimateDiskUsage)(this.snapshots.length),
612
+ networkLatency: (0, metricsAggregation_js_1.getAverageResponseTime)(this.tools),
613
+ uptime: Date.now() - this.startTime,
614
+ lastHealthCheck: new Date(),
615
+ status: (0, metricsAggregation_js_1.getOverallHealthStatus)(cpu, memory, (0, metricsAggregation_js_1.getErrorRate)(this.tools)),
616
+ };
617
+ }
618
+ getDetailedPerformanceMetrics() {
619
+ const totalRequests = (0, metricsAggregation_js_1.getTotalRequests)(this.tools);
620
+ const avg = (0, metricsAggregation_js_1.getAverageResponseTime)(this.tools);
621
+ const rps = (0, metricsAggregation_js_1.calculateRequestsPerSecond)(this.snapshots);
622
+ return {
623
+ requestThroughput: rps * 60,
624
+ averageResponseTime: avg,
625
+ p95ResponseTime: (0, metricsAggregation_js_1.calculatePercentileFromAvg)(avg, 95),
626
+ p99ResponseTime: (0, metricsAggregation_js_1.calculatePercentileFromAvg)(avg, 99),
627
+ errorRate: (0, metricsAggregation_js_1.getErrorRate)(this.tools),
628
+ concurrentConnections: this.activeConnections,
629
+ totalRequests,
630
+ successfulRequests: totalRequests - (0, metricsAggregation_js_1.getTotalErrors)(this.tools),
631
+ requestsPerSecond: rps,
632
+ bytesTransferred: totalRequests * 1024 * 2,
633
+ };
634
+ }
635
+ getAdvancedAnalytics(timeRange = '1h') {
636
+ return (0, metricsSerializer_js_1.buildAdvancedAnalytics)(timeRange, this.snapshots, this.tools, (0, metricsAggregation_js_1.getErrorRate)(this.tools), (0, metricsAggregation_js_1.detectAnomalies)(this.snapshots.slice(-20)));
637
+ }
638
+ /**
639
+ * Generate real-time alert
640
+ */
641
+ generateRealTimeAlert(type, severity, message, value, threshold) {
642
+ const alert = {
643
+ id: `alert_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
644
+ type: type,
645
+ severity: severity,
646
+ message,
647
+ timestamp: new Date(),
648
+ resolved: false,
649
+ value,
650
+ threshold,
651
+ source: 'MetricsCollector',
652
+ category: (0, metricsAggregation_js_1.categorizeAlert)(type)
653
+ };
654
+ this.recentAlerts.unshift(alert);
655
+ if (this.recentAlerts.length > 100) {
656
+ this.recentAlerts = this.recentAlerts.slice(0, 100);
657
+ }
658
+ return alert;
659
+ }
660
+ /**
661
+ * Get active alerts
662
+ */
663
+ getActiveAlerts() {
664
+ return this.recentAlerts.filter(alert => !alert.resolved);
665
+ }
666
+ /**
667
+ * Resolve alert by ID
668
+ */
669
+ resolveAlert(alertId) {
670
+ const alert = this.recentAlerts.find(a => a.id === alertId);
671
+ if (alert) {
672
+ alert.resolved = true;
673
+ alert.resolvedAt = new Date();
674
+ return true;
675
+ }
676
+ return false;
677
+ }
678
+ // ===== Resource Sampling / Leak Detection =====
679
+ sampleResources() {
680
+ try {
681
+ const now = Date.now();
682
+ const mem = process.memoryUsage();
683
+ let cpuPercent = 0;
684
+ const curr = process.cpuUsage();
685
+ if (this.lastCpuUsageSample && this.lastCpuSampleTime) {
686
+ const userDiff = curr.user - this.lastCpuUsageSample.user; // microseconds
687
+ const systemDiff = curr.system - this.lastCpuUsageSample.system;
688
+ const elapsedMs = now - this.lastCpuSampleTime;
689
+ if (elapsedMs > 0) {
690
+ // total diff in microseconds / (elapsedMs * 1000) gives fraction of single core; *100 -> percent
691
+ cpuPercent = ((userDiff + systemDiff) / 1000) / elapsedMs * 100;
692
+ // Clamp 0-100 (single process perspective)
693
+ if (cpuPercent < 0)
694
+ cpuPercent = 0;
695
+ else if (cpuPercent > 100)
696
+ cpuPercent = 100;
697
+ }
698
+ }
699
+ this.lastCpuUsageSample = curr;
700
+ this.lastCpuSampleTime = now;
701
+ this.resourceSamples.add({ timestamp: now, cpuPercent, heapUsed: mem.heapUsed, rss: mem.rss });
702
+ }
703
+ catch { /* ignore sampling errors */ }
704
+ }
705
+ getResourceHistory(limit = 200) {
706
+ const all = this.resourceSamples.getAll();
707
+ const samples = limit > 0 ? all.slice(-limit) : all;
708
+ const trend = (0, metricsAggregation_js_1.calculateLinearSlopes)(samples);
709
+ return { samples, trend };
710
+ }
711
+ /** Remove stale / low-activity tool entries to prevent unbounded map growth. */
712
+ cleanupOldToolMetrics() {
713
+ const staleThreshold = Date.now() - 3_600_000; // 1 hour
714
+ const toRemove = (0, metricsAggregation_js_1.computeToolsToCleanup)(this.tools, MetricsCollector.MAX_TOOL_METRICS, staleThreshold);
715
+ let removedCount = 0;
716
+ for (const name of toRemove) {
717
+ if (this.tools.delete(name))
718
+ removedCount++;
719
+ }
720
+ if (removedCount > 0) {
721
+ (0, logger_js_1.logInfo)('[MetricsCollector] Cleaned up stale tool metrics', { removedCount, remaining: this.tools.size });
722
+ }
723
+ }
724
+ // ====== BufferRing-Enhanced Methods ======
725
+ /**
726
+ * Get historical metrics data for charting
727
+ */
728
+ getHistoricalMetrics(minutes = 60) {
729
+ const cutoff = Date.now() - (minutes * 60 * 1000);
730
+ return this.historicalSnapshots.filter(entry => entry.timestamp >= cutoff);
731
+ }
732
+ /**
733
+ * Get recent tool call events for analysis
734
+ */
735
+ getRecentToolCallEvents(minutes = 30) {
736
+ const cutoff = Date.now() - (minutes * 60 * 1000);
737
+ return this.toolCallEvents.filter(event => event.timestamp >= cutoff);
738
+ }
739
+ /**
740
+ * Get performance metrics time series for dashboard charts (BufferRing-enhanced)
741
+ */
742
+ getPerformanceTimeSeriesData(minutes = 60) {
743
+ return (0, metricsSerializer_js_1.buildPerformanceTimeSeriesData)(this.performanceMetrics.getAll(), minutes);
744
+ }
745
+ /**
746
+ * Get tool usage analytics from historical data
747
+ */
748
+ getToolUsageAnalytics(minutes = 60) {
749
+ return (0, metricsSerializer_js_1.buildToolUsageAnalytics)(this.toolCallEvents.getAll(), minutes);
750
+ }
751
+ /**
752
+ * Get BufferRing statistics for monitoring
753
+ */
754
+ getBufferRingStats() {
755
+ return {
756
+ historicalSnapshots: this.historicalSnapshots.getStats(),
757
+ toolCallEvents: this.toolCallEvents.getStats(),
758
+ performanceMetrics: this.performanceMetrics.getStats()
759
+ };
760
+ }
761
+ /**
762
+ * Export metrics data for backup/analysis
763
+ */
764
+ exportMetricsData(options = {}) {
765
+ const data = {
766
+ timestamp: Date.now(),
767
+ currentSnapshot: this.getCurrentSnapshot(),
768
+ bufferStats: this.getBufferRingStats()
769
+ };
770
+ const result = data;
771
+ if (options.includeHistorical !== false) {
772
+ result.historicalSnapshots = this.historicalSnapshots.getAll();
773
+ }
774
+ if (options.includeEvents !== false) {
775
+ result.toolCallEvents = this.toolCallEvents.getAll();
776
+ }
777
+ if (options.includePerformance !== false) {
778
+ result.performanceMetrics = this.performanceMetrics.getAll();
779
+ }
780
+ return result;
781
+ }
782
+ /**
783
+ * Clear all BufferRing data (for maintenance)
784
+ */
785
+ clearBufferedData() {
786
+ this.historicalSnapshots.clear();
787
+ this.toolCallEvents.clear();
788
+ this.performanceMetrics.clear();
789
+ (0, logger_js_1.logInfo)('[MetricsCollector] Cleared all BufferRing data');
790
+ }
791
+ }
792
+ exports.MetricsCollector = MetricsCollector;
793
+ // Global singleton instance
794
+ let globalCollector = null;
795
+ /**
796
+ * Get or create the global metrics collector instance
797
+ */
798
+ function getMetricsCollector() {
799
+ if (!globalCollector) {
800
+ globalCollector = new MetricsCollector();
801
+ }
802
+ return globalCollector;
803
+ }
804
+ /**
805
+ * Set a custom metrics collector instance (useful for testing)
806
+ */
807
+ function setMetricsCollector(collector) {
808
+ globalCollector = collector;
809
+ }
810
+ // (No changes needed in this file for the current UI fixes)