@facthouse/mcp 0.26.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 (413) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +611 -0
  3. package/dist/cli/index.d.ts +30 -0
  4. package/dist/cli/index.d.ts.map +1 -0
  5. package/dist/cli/index.js +785 -0
  6. package/dist/cli/index.js.map +1 -0
  7. package/dist/cli/init-backfill.d.ts +26 -0
  8. package/dist/cli/init-backfill.d.ts.map +1 -0
  9. package/dist/cli/init-backfill.js +56 -0
  10. package/dist/cli/init-backfill.js.map +1 -0
  11. package/dist/cli/init-knobs.d.ts +137 -0
  12. package/dist/cli/init-knobs.d.ts.map +1 -0
  13. package/dist/cli/init-knobs.js +400 -0
  14. package/dist/cli/init-knobs.js.map +1 -0
  15. package/dist/cli/init-wizard.d.ts +67 -0
  16. package/dist/cli/init-wizard.d.ts.map +1 -0
  17. package/dist/cli/init-wizard.js +319 -0
  18. package/dist/cli/init-wizard.js.map +1 -0
  19. package/dist/cli/init.d.ts +124 -0
  20. package/dist/cli/init.d.ts.map +1 -0
  21. package/dist/cli/init.js +286 -0
  22. package/dist/cli/init.js.map +1 -0
  23. package/dist/cli/inspect-html.d.ts +15 -0
  24. package/dist/cli/inspect-html.d.ts.map +1 -0
  25. package/dist/cli/inspect-html.js +491 -0
  26. package/dist/cli/inspect-html.js.map +1 -0
  27. package/dist/cli/inspect-intelligence.d.ts +8 -0
  28. package/dist/cli/inspect-intelligence.d.ts.map +1 -0
  29. package/dist/cli/inspect-intelligence.js +103 -0
  30. package/dist/cli/inspect-intelligence.js.map +1 -0
  31. package/dist/cli/inspect-model.d.ts +42 -0
  32. package/dist/cli/inspect-model.d.ts.map +1 -0
  33. package/dist/cli/inspect-model.js +108 -0
  34. package/dist/cli/inspect-model.js.map +1 -0
  35. package/dist/cli/inspect-payload.d.ts +122 -0
  36. package/dist/cli/inspect-payload.d.ts.map +1 -0
  37. package/dist/cli/inspect-payload.js +338 -0
  38. package/dist/cli/inspect-payload.js.map +1 -0
  39. package/dist/cli/inspect-theme.d.ts +44 -0
  40. package/dist/cli/inspect-theme.d.ts.map +1 -0
  41. package/dist/cli/inspect-theme.js +118 -0
  42. package/dist/cli/inspect-theme.js.map +1 -0
  43. package/dist/cli/inspect.d.ts +24 -0
  44. package/dist/cli/inspect.d.ts.map +1 -0
  45. package/dist/cli/inspect.js +172 -0
  46. package/dist/cli/inspect.js.map +1 -0
  47. package/dist/cli/log-event.d.ts +55 -0
  48. package/dist/cli/log-event.d.ts.map +1 -0
  49. package/dist/cli/log-event.js +149 -0
  50. package/dist/cli/log-event.js.map +1 -0
  51. package/dist/cli/package-version.d.ts +2 -0
  52. package/dist/cli/package-version.d.ts.map +1 -0
  53. package/dist/cli/package-version.js +15 -0
  54. package/dist/cli/package-version.js.map +1 -0
  55. package/dist/cli/query.d.ts +53 -0
  56. package/dist/cli/query.d.ts.map +1 -0
  57. package/dist/cli/query.js +325 -0
  58. package/dist/cli/query.js.map +1 -0
  59. package/dist/cli/record.d.ts +55 -0
  60. package/dist/cli/record.d.ts.map +1 -0
  61. package/dist/cli/record.js +149 -0
  62. package/dist/cli/record.js.map +1 -0
  63. package/dist/cli/settings.d.ts +26 -0
  64. package/dist/cli/settings.d.ts.map +1 -0
  65. package/dist/cli/settings.js +161 -0
  66. package/dist/cli/settings.js.map +1 -0
  67. package/dist/cli/spend-dashboard.d.ts +53 -0
  68. package/dist/cli/spend-dashboard.d.ts.map +1 -0
  69. package/dist/cli/spend-dashboard.js +297 -0
  70. package/dist/cli/spend-dashboard.js.map +1 -0
  71. package/dist/cli/stats-html.d.ts +20 -0
  72. package/dist/cli/stats-html.d.ts.map +1 -0
  73. package/dist/cli/stats-html.js +879 -0
  74. package/dist/cli/stats-html.js.map +1 -0
  75. package/dist/cli/web.d.ts +60 -0
  76. package/dist/cli/web.d.ts.map +1 -0
  77. package/dist/cli/web.js +416 -0
  78. package/dist/cli/web.js.map +1 -0
  79. package/dist/config.d.ts +102 -0
  80. package/dist/config.d.ts.map +1 -0
  81. package/dist/config.js +302 -0
  82. package/dist/config.js.map +1 -0
  83. package/dist/db/connection.d.ts +96 -0
  84. package/dist/db/connection.d.ts.map +1 -0
  85. package/dist/db/connection.js +200 -0
  86. package/dist/db/connection.js.map +1 -0
  87. package/dist/db/consolidation-lock.d.ts +16 -0
  88. package/dist/db/consolidation-lock.d.ts.map +1 -0
  89. package/dist/db/consolidation-lock.js +67 -0
  90. package/dist/db/consolidation-lock.js.map +1 -0
  91. package/dist/db/consolidations.d.ts +41 -0
  92. package/dist/db/consolidations.d.ts.map +1 -0
  93. package/dist/db/consolidations.js +165 -0
  94. package/dist/db/consolidations.js.map +1 -0
  95. package/dist/db/disk-budget.d.ts +44 -0
  96. package/dist/db/disk-budget.d.ts.map +1 -0
  97. package/dist/db/disk-budget.js +177 -0
  98. package/dist/db/disk-budget.js.map +1 -0
  99. package/dist/db/domains.d.ts +14 -0
  100. package/dist/db/domains.d.ts.map +1 -0
  101. package/dist/db/domains.js +37 -0
  102. package/dist/db/domains.js.map +1 -0
  103. package/dist/db/embeddings-hnsw.d.ts +30 -0
  104. package/dist/db/embeddings-hnsw.d.ts.map +1 -0
  105. package/dist/db/embeddings-hnsw.js +129 -0
  106. package/dist/db/embeddings-hnsw.js.map +1 -0
  107. package/dist/db/embeddings.d.ts +68 -0
  108. package/dist/db/embeddings.d.ts.map +1 -0
  109. package/dist/db/embeddings.js +139 -0
  110. package/dist/db/embeddings.js.map +1 -0
  111. package/dist/db/entities.d.ts +157 -0
  112. package/dist/db/entities.d.ts.map +1 -0
  113. package/dist/db/entities.js +489 -0
  114. package/dist/db/entities.js.map +1 -0
  115. package/dist/db/extract-watermarks.d.ts +55 -0
  116. package/dist/db/extract-watermarks.d.ts.map +1 -0
  117. package/dist/db/extract-watermarks.js +171 -0
  118. package/dist/db/extract-watermarks.js.map +1 -0
  119. package/dist/db/facts.d.ts +117 -0
  120. package/dist/db/facts.d.ts.map +1 -0
  121. package/dist/db/facts.js +326 -0
  122. package/dist/db/facts.js.map +1 -0
  123. package/dist/db/index.d.ts +26 -0
  124. package/dist/db/index.d.ts.map +1 -0
  125. package/dist/db/index.js +23 -0
  126. package/dist/db/index.js.map +1 -0
  127. package/dist/db/inferences.d.ts +36 -0
  128. package/dist/db/inferences.d.ts.map +1 -0
  129. package/dist/db/inferences.js +212 -0
  130. package/dist/db/inferences.js.map +1 -0
  131. package/dist/db/intelligence-runs.d.ts +20 -0
  132. package/dist/db/intelligence-runs.d.ts.map +1 -0
  133. package/dist/db/intelligence-runs.js +60 -0
  134. package/dist/db/intelligence-runs.js.map +1 -0
  135. package/dist/db/pg-backend.d.ts +14 -0
  136. package/dist/db/pg-backend.d.ts.map +1 -0
  137. package/dist/db/pg-backend.js +53 -0
  138. package/dist/db/pg-backend.js.map +1 -0
  139. package/dist/db/postgres-schema.d.ts +13 -0
  140. package/dist/db/postgres-schema.d.ts.map +1 -0
  141. package/dist/db/postgres-schema.js +392 -0
  142. package/dist/db/postgres-schema.js.map +1 -0
  143. package/dist/db/postgres.d.ts +30 -0
  144. package/dist/db/postgres.d.ts.map +1 -0
  145. package/dist/db/postgres.js +154 -0
  146. package/dist/db/postgres.js.map +1 -0
  147. package/dist/db/prune.d.ts +67 -0
  148. package/dist/db/prune.d.ts.map +1 -0
  149. package/dist/db/prune.js +113 -0
  150. package/dist/db/prune.js.map +1 -0
  151. package/dist/db/scheduler-signal.d.ts +20 -0
  152. package/dist/db/scheduler-signal.d.ts.map +1 -0
  153. package/dist/db/scheduler-signal.js +32 -0
  154. package/dist/db/scheduler-signal.js.map +1 -0
  155. package/dist/db/schema-version.d.ts +7 -0
  156. package/dist/db/schema-version.d.ts.map +1 -0
  157. package/dist/db/schema-version.js +7 -0
  158. package/dist/db/schema-version.js.map +1 -0
  159. package/dist/db/schema.d.ts +12 -0
  160. package/dist/db/schema.d.ts.map +1 -0
  161. package/dist/db/schema.js +983 -0
  162. package/dist/db/schema.js.map +1 -0
  163. package/dist/db/session-facts.d.ts +108 -0
  164. package/dist/db/session-facts.d.ts.map +1 -0
  165. package/dist/db/session-facts.js +244 -0
  166. package/dist/db/session-facts.js.map +1 -0
  167. package/dist/db/sessions.d.ts +85 -0
  168. package/dist/db/sessions.d.ts.map +1 -0
  169. package/dist/db/sessions.js +169 -0
  170. package/dist/db/sessions.js.map +1 -0
  171. package/dist/db/sources.d.ts +22 -0
  172. package/dist/db/sources.d.ts.map +1 -0
  173. package/dist/db/sources.js +43 -0
  174. package/dist/db/sources.js.map +1 -0
  175. package/dist/db/stats.d.ts +94 -0
  176. package/dist/db/stats.d.ts.map +1 -0
  177. package/dist/db/stats.js +79 -0
  178. package/dist/db/stats.js.map +1 -0
  179. package/dist/db/store.d.ts +19 -0
  180. package/dist/db/store.d.ts.map +1 -0
  181. package/dist/db/store.js +58 -0
  182. package/dist/db/store.js.map +1 -0
  183. package/dist/db/watermarks.d.ts +20 -0
  184. package/dist/db/watermarks.d.ts.map +1 -0
  185. package/dist/db/watermarks.js +30 -0
  186. package/dist/db/watermarks.js.map +1 -0
  187. package/dist/embedding/ollama.d.ts +31 -0
  188. package/dist/embedding/ollama.d.ts.map +1 -0
  189. package/dist/embedding/ollama.js +148 -0
  190. package/dist/embedding/ollama.js.map +1 -0
  191. package/dist/embedding/provider.d.ts +37 -0
  192. package/dist/embedding/provider.d.ts.map +1 -0
  193. package/dist/embedding/provider.js +71 -0
  194. package/dist/embedding/provider.js.map +1 -0
  195. package/dist/embedding/types.d.ts +61 -0
  196. package/dist/embedding/types.d.ts.map +1 -0
  197. package/dist/embedding/types.js +13 -0
  198. package/dist/embedding/types.js.map +1 -0
  199. package/dist/embedding/voyage.d.ts +31 -0
  200. package/dist/embedding/voyage.d.ts.map +1 -0
  201. package/dist/embedding/voyage.js +105 -0
  202. package/dist/embedding/voyage.js.map +1 -0
  203. package/dist/identity.d.ts +32 -0
  204. package/dist/identity.d.ts.map +1 -0
  205. package/dist/identity.js +53 -0
  206. package/dist/identity.js.map +1 -0
  207. package/dist/index.d.ts +9 -0
  208. package/dist/index.d.ts.map +1 -0
  209. package/dist/index.js +275 -0
  210. package/dist/index.js.map +1 -0
  211. package/dist/intelligence/backing.d.ts +19 -0
  212. package/dist/intelligence/backing.d.ts.map +1 -0
  213. package/dist/intelligence/backing.js +88 -0
  214. package/dist/intelligence/backing.js.map +1 -0
  215. package/dist/intelligence/cli.d.ts +90 -0
  216. package/dist/intelligence/cli.d.ts.map +1 -0
  217. package/dist/intelligence/cli.js +823 -0
  218. package/dist/intelligence/cli.js.map +1 -0
  219. package/dist/intelligence/consolidate.d.ts +84 -0
  220. package/dist/intelligence/consolidate.d.ts.map +1 -0
  221. package/dist/intelligence/consolidate.js +1234 -0
  222. package/dist/intelligence/consolidate.js.map +1 -0
  223. package/dist/intelligence/extract-prompt.d.ts +78 -0
  224. package/dist/intelligence/extract-prompt.d.ts.map +1 -0
  225. package/dist/intelligence/extract-prompt.js +146 -0
  226. package/dist/intelligence/extract-prompt.js.map +1 -0
  227. package/dist/intelligence/heuristic.d.ts +15 -0
  228. package/dist/intelligence/heuristic.d.ts.map +1 -0
  229. package/dist/intelligence/heuristic.js +224 -0
  230. package/dist/intelligence/heuristic.js.map +1 -0
  231. package/dist/intelligence/http.d.ts +77 -0
  232. package/dist/intelligence/http.d.ts.map +1 -0
  233. package/dist/intelligence/http.js +496 -0
  234. package/dist/intelligence/http.js.map +1 -0
  235. package/dist/intelligence/provider-type.d.ts +6 -0
  236. package/dist/intelligence/provider-type.d.ts.map +1 -0
  237. package/dist/intelligence/provider-type.js +18 -0
  238. package/dist/intelligence/provider-type.js.map +1 -0
  239. package/dist/intelligence/provider.d.ts +49 -0
  240. package/dist/intelligence/provider.d.ts.map +1 -0
  241. package/dist/intelligence/provider.js +52 -0
  242. package/dist/intelligence/provider.js.map +1 -0
  243. package/dist/intelligence/related-k.d.ts +15 -0
  244. package/dist/intelligence/related-k.d.ts.map +1 -0
  245. package/dist/intelligence/related-k.js +57 -0
  246. package/dist/intelligence/related-k.js.map +1 -0
  247. package/dist/intelligence/routing-view.d.ts +33 -0
  248. package/dist/intelligence/routing-view.d.ts.map +1 -0
  249. package/dist/intelligence/routing-view.js +54 -0
  250. package/dist/intelligence/routing-view.js.map +1 -0
  251. package/dist/intelligence/sampling.d.ts +20 -0
  252. package/dist/intelligence/sampling.d.ts.map +1 -0
  253. package/dist/intelligence/sampling.js +324 -0
  254. package/dist/intelligence/sampling.js.map +1 -0
  255. package/dist/intelligence/stage-router.d.ts +32 -0
  256. package/dist/intelligence/stage-router.d.ts.map +1 -0
  257. package/dist/intelligence/stage-router.js +251 -0
  258. package/dist/intelligence/stage-router.js.map +1 -0
  259. package/dist/intelligence/steps.d.ts +65 -0
  260. package/dist/intelligence/steps.d.ts.map +1 -0
  261. package/dist/intelligence/steps.js +86 -0
  262. package/dist/intelligence/steps.js.map +1 -0
  263. package/dist/intelligence/subject.d.ts +42 -0
  264. package/dist/intelligence/subject.d.ts.map +1 -0
  265. package/dist/intelligence/subject.js +44 -0
  266. package/dist/intelligence/subject.js.map +1 -0
  267. package/dist/intelligence/token-budget.d.ts +86 -0
  268. package/dist/intelligence/token-budget.d.ts.map +1 -0
  269. package/dist/intelligence/token-budget.js +312 -0
  270. package/dist/intelligence/token-budget.js.map +1 -0
  271. package/dist/intelligence/types.d.ts +179 -0
  272. package/dist/intelligence/types.d.ts.map +1 -0
  273. package/dist/intelligence/types.js +7 -0
  274. package/dist/intelligence/types.js.map +1 -0
  275. package/dist/intelligence/usage.d.ts +104 -0
  276. package/dist/intelligence/usage.d.ts.map +1 -0
  277. package/dist/intelligence/usage.js +233 -0
  278. package/dist/intelligence/usage.js.map +1 -0
  279. package/dist/ipc/scheduler-ipc.d.ts +52 -0
  280. package/dist/ipc/scheduler-ipc.d.ts.map +1 -0
  281. package/dist/ipc/scheduler-ipc.js +214 -0
  282. package/dist/ipc/scheduler-ipc.js.map +1 -0
  283. package/dist/paths.d.ts +29 -0
  284. package/dist/paths.d.ts.map +1 -0
  285. package/dist/paths.js +47 -0
  286. package/dist/paths.js.map +1 -0
  287. package/dist/scheduler.d.ts +39 -0
  288. package/dist/scheduler.d.ts.map +1 -0
  289. package/dist/scheduler.js +93 -0
  290. package/dist/scheduler.js.map +1 -0
  291. package/dist/schemas/domains.d.ts +89 -0
  292. package/dist/schemas/domains.d.ts.map +1 -0
  293. package/dist/schemas/domains.js +160 -0
  294. package/dist/schemas/domains.js.map +1 -0
  295. package/dist/schemas/starter-vocabulary.d.ts +34 -0
  296. package/dist/schemas/starter-vocabulary.d.ts.map +1 -0
  297. package/dist/schemas/starter-vocabulary.js +91 -0
  298. package/dist/schemas/starter-vocabulary.js.map +1 -0
  299. package/dist/search/ann.d.ts +36 -0
  300. package/dist/search/ann.d.ts.map +1 -0
  301. package/dist/search/ann.js +59 -0
  302. package/dist/search/ann.js.map +1 -0
  303. package/dist/search/entity.d.ts +43 -0
  304. package/dist/search/entity.d.ts.map +1 -0
  305. package/dist/search/entity.js +147 -0
  306. package/dist/search/entity.js.map +1 -0
  307. package/dist/search/episodes.d.ts +28 -0
  308. package/dist/search/episodes.d.ts.map +1 -0
  309. package/dist/search/episodes.js +102 -0
  310. package/dist/search/episodes.js.map +1 -0
  311. package/dist/search/hnsw-graph.d.ts +35 -0
  312. package/dist/search/hnsw-graph.d.ts.map +1 -0
  313. package/dist/search/hnsw-graph.js +177 -0
  314. package/dist/search/hnsw-graph.js.map +1 -0
  315. package/dist/search/hnsw-ram.d.ts +29 -0
  316. package/dist/search/hnsw-ram.d.ts.map +1 -0
  317. package/dist/search/hnsw-ram.js +95 -0
  318. package/dist/search/hnsw-ram.js.map +1 -0
  319. package/dist/search/index.d.ts +119 -0
  320. package/dist/search/index.d.ts.map +1 -0
  321. package/dist/search/index.js +420 -0
  322. package/dist/search/index.js.map +1 -0
  323. package/dist/search/interlocutor-rank.d.ts +10 -0
  324. package/dist/search/interlocutor-rank.d.ts.map +1 -0
  325. package/dist/search/interlocutor-rank.js +21 -0
  326. package/dist/search/interlocutor-rank.js.map +1 -0
  327. package/dist/search/key-facts.d.ts +34 -0
  328. package/dist/search/key-facts.d.ts.map +1 -0
  329. package/dist/search/key-facts.js +43 -0
  330. package/dist/search/key-facts.js.map +1 -0
  331. package/dist/search/profile.d.ts +31 -0
  332. package/dist/search/profile.d.ts.map +1 -0
  333. package/dist/search/profile.js +43 -0
  334. package/dist/search/profile.js.map +1 -0
  335. package/dist/search/vector.d.ts +90 -0
  336. package/dist/search/vector.d.ts.map +1 -0
  337. package/dist/search/vector.js +187 -0
  338. package/dist/search/vector.js.map +1 -0
  339. package/dist/sources/claude-code.d.ts +35 -0
  340. package/dist/sources/claude-code.d.ts.map +1 -0
  341. package/dist/sources/claude-code.js +401 -0
  342. package/dist/sources/claude-code.js.map +1 -0
  343. package/dist/sources/copy.d.ts +58 -0
  344. package/dist/sources/copy.d.ts.map +1 -0
  345. package/dist/sources/copy.js +104 -0
  346. package/dist/sources/copy.js.map +1 -0
  347. package/dist/sources/cursor.d.ts +44 -0
  348. package/dist/sources/cursor.d.ts.map +1 -0
  349. package/dist/sources/cursor.js +99 -0
  350. package/dist/sources/cursor.js.map +1 -0
  351. package/dist/sources/jsonl-copy.d.ts +58 -0
  352. package/dist/sources/jsonl-copy.d.ts.map +1 -0
  353. package/dist/sources/jsonl-copy.js +195 -0
  354. package/dist/sources/jsonl-copy.js.map +1 -0
  355. package/dist/sources/jsonl-ingest.d.ts +58 -0
  356. package/dist/sources/jsonl-ingest.d.ts.map +1 -0
  357. package/dist/sources/jsonl-ingest.js +195 -0
  358. package/dist/sources/jsonl-ingest.js.map +1 -0
  359. package/dist/sources/pull.d.ts +85 -0
  360. package/dist/sources/pull.d.ts.map +1 -0
  361. package/dist/sources/pull.js +145 -0
  362. package/dist/sources/pull.js.map +1 -0
  363. package/dist/sources/resolve.d.ts +38 -0
  364. package/dist/sources/resolve.d.ts.map +1 -0
  365. package/dist/sources/resolve.js +91 -0
  366. package/dist/sources/resolve.js.map +1 -0
  367. package/dist/tools/capture-fact-description.d.ts +18 -0
  368. package/dist/tools/capture-fact-description.d.ts.map +1 -0
  369. package/dist/tools/capture-fact-description.js +41 -0
  370. package/dist/tools/capture-fact-description.js.map +1 -0
  371. package/dist/tools/fact-manager.d.ts +69 -0
  372. package/dist/tools/fact-manager.d.ts.map +1 -0
  373. package/dist/tools/fact-manager.js +332 -0
  374. package/dist/tools/fact-manager.js.map +1 -0
  375. package/dist/tools/inference-descriptions.d.ts +8 -0
  376. package/dist/tools/inference-descriptions.d.ts.map +1 -0
  377. package/dist/tools/inference-descriptions.js +16 -0
  378. package/dist/tools/inference-descriptions.js.map +1 -0
  379. package/dist/tools/inferences.d.ts +11 -0
  380. package/dist/tools/inferences.d.ts.map +1 -0
  381. package/dist/tools/inferences.js +98 -0
  382. package/dist/tools/inferences.js.map +1 -0
  383. package/dist/tools/read-tools.d.ts +21 -0
  384. package/dist/tools/read-tools.d.ts.map +1 -0
  385. package/dist/tools/read-tools.js +276 -0
  386. package/dist/tools/read-tools.js.map +1 -0
  387. package/dist/tools/resources.d.ts +62 -0
  388. package/dist/tools/resources.d.ts.map +1 -0
  389. package/dist/tools/resources.js +199 -0
  390. package/dist/tools/resources.js.map +1 -0
  391. package/dist/tools/session-manager.d.ts +44 -0
  392. package/dist/tools/session-manager.d.ts.map +1 -0
  393. package/dist/tools/session-manager.js +227 -0
  394. package/dist/tools/session-manager.js.map +1 -0
  395. package/dist/types/config.d.ts +400 -0
  396. package/dist/types/config.d.ts.map +1 -0
  397. package/dist/types/config.js +105 -0
  398. package/dist/types/config.js.map +1 -0
  399. package/dist/types/data.d.ts +357 -0
  400. package/dist/types/data.d.ts.map +1 -0
  401. package/dist/types/data.js +36 -0
  402. package/dist/types/data.js.map +1 -0
  403. package/dist/types/index.d.ts +6 -0
  404. package/dist/types/index.d.ts.map +1 -0
  405. package/dist/types/index.js +6 -0
  406. package/dist/types/index.js.map +1 -0
  407. package/dist/types.d.ts +115 -0
  408. package/dist/types.d.ts.map +1 -0
  409. package/dist/types.js +6 -0
  410. package/dist/types.js.map +1 -0
  411. package/dist/warnings.d.ts.map +1 -0
  412. package/dist/warnings.js.map +1 -0
  413. package/package.json +62 -0
@@ -0,0 +1,879 @@
1
+ /**
2
+ * Spend page for `facthouse inspect`. Catch-up vs cost.
3
+ * Cost defaults to tokens (what providers bill and quota) when reported;
4
+ * uses/time are alternatives. Grain then period on the chart.
5
+ * More detail is Cost only: metric/filter rows plus the per-run table.
6
+ */
7
+ import { STAGE_HELP, rollSpendDays, spendBucketKey, spendDashboardFromStats, stageTotals, stagesIn, } from "./spend-dashboard.js";
8
+ import { TOKEN_BUDGET_HOW_TO, billedCapName, billedProviderName, formatResetAt, formatTokenCount, tokenBudgetUsageLead, } from "../intelligence/token-budget.js";
9
+ import { LEDGER, LEDGER_MARK_SVG, ledgerFaviconHref, ledgerSpendCss } from "./inspect-theme.js";
10
+ export function spendBucketLabel(day, grain = "day") {
11
+ const p = (day || "").split("-");
12
+ if (p.length < 3)
13
+ return day;
14
+ const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
15
+ const mon = months[Number(p[1]) - 1] ?? "";
16
+ if (grain === "month")
17
+ return mon + " " + p[0];
18
+ const d = String(Number(p[2]));
19
+ if (grain === "week")
20
+ return "Week of " + d + " " + mon;
21
+ return d + " " + mon;
22
+ }
23
+ /** Day number plus a month name on the 1st (and the first bar). Sparse on long windows. */
24
+ export function spendAxisTicks(i, view, grain = "day") {
25
+ const n = view.length;
26
+ const d = view[i];
27
+ if (!d)
28
+ return { day: "", month: "" };
29
+ const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
30
+ const mon = months[Number(d.day.slice(5, 7)) - 1] ?? "";
31
+ const year = d.day.slice(0, 4);
32
+ const dayn = String(Number(d.day.slice(8)));
33
+ if (grain === "month") {
34
+ const yearBoundary = i === 0 || view[i - 1].day.slice(0, 4) !== year;
35
+ return { day: mon, month: yearBoundary ? year : "" };
36
+ }
37
+ const monthBoundary = i === 0 || view[i - 1].day.slice(5, 7) !== d.day.slice(5, 7);
38
+ if (n <= 8)
39
+ return { day: `${dayn} ${mon}`, month: "" };
40
+ const step = grain === "week" && n > 16 ? 4 : n <= 16 ? 2 : 5;
41
+ const showDay = i % step === 0 || i === n - 1;
42
+ return { day: showDay ? dayn : "", month: monthBoundary ? mon : "" };
43
+ }
44
+ const STAGE_COLOUR = { ...LEDGER.stage };
45
+ export const SPEND_BOARD_CSS = `
46
+ .spend-board { max-width: 920px; margin: 0 auto; color: var(--ink, var(--text)); }
47
+ .spend-hero { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 16px; }
48
+ @media (max-width: 720px) { .spend-hero { grid-template-columns: 1fr; } }
49
+ .spend-hero-card { background: var(--card, var(--elev)); border: 1px solid var(--line);
50
+ border-radius: 12px; padding: 18px 18px 16px; }
51
+ .spend-hero-card h2 { margin: 0 0 8px; font-size: 13px; font-weight: 600; color: var(--muted);
52
+ letter-spacing: .02em; text-transform: uppercase; }
53
+ .spend-hero-card p { margin: 0 0 8px; font-size: 16px; line-height: 1.4; }
54
+ .spend-hero-card p.lead { font-size: 22px; font-weight: 650; letter-spacing: -.02em; }
55
+ .spend-hero-card.warn p.lead { color: var(--warn); }
56
+ .spend-hero-card .do { color: var(--accent); font-size: 13px; margin: 10px 0 0; }
57
+ .spend-card { background: var(--card, var(--elev)); border: 1px solid var(--line);
58
+ border-radius: 12px; padding: 16px 18px 14px; margin-bottom: 12px; }
59
+ .spend-toolbar { display: flex; flex-wrap: wrap; gap: 8px 0; align-items: center; margin-bottom: 10px; }
60
+ .spend-cluster { display: flex; align-items: center; }
61
+ .spend-split {
62
+ width: 1px; height: 22px; margin: 0 12px; flex: 0 0 1px;
63
+ background: var(--line); align-self: center;
64
+ }
65
+ .spend-seg {
66
+ display: inline-flex; gap: 2px; padding: 3px; width: max-content; max-width: 100%;
67
+ background: var(--input); border: 1px solid var(--line); border-radius: 8px;
68
+ }
69
+ .spend-board .spend-seg button {
70
+ font-size: 12px; padding: 5px 10px; border: 1px solid transparent;
71
+ background: transparent; border-radius: 6px;
72
+ }
73
+ .spend-board .spend-seg button.on {
74
+ background: var(--chip, #243044); border-color: transparent;
75
+ color: var(--accent);
76
+ }
77
+ #om-detail { font-size: 12px; padding: 5px 10px; }
78
+ #om-detail.on { border-color: var(--accent); color: var(--accent); }
79
+ .spend-toolbar .grow { margin-left: auto; }
80
+ .spend-chart-title { margin: 0 0 8px; font-size: 14px; font-weight: 600; }
81
+ #om-chart { width: 100%; height: auto; display: block; }
82
+ .om-day { cursor: pointer; }
83
+ .om-day:hover rect { filter: brightness(1.1); }
84
+ .om-day.is-on rect { stroke: var(--text, #e7edf5); stroke-width: 1.25; }
85
+ .spend-legend { display: flex; flex-wrap: wrap; gap: 8px 14px; margin: 10px 0 0; font-size: 12px; color: var(--muted); }
86
+ .spend-legend button { border: 0; background: transparent; color: inherit; padding: 0; font: inherit; cursor: help; }
87
+ .sw { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }
88
+ .spend-quiet { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
89
+ .spend-board:not(.is-detail) .spend-detail-only { display: none !important; }
90
+ .spend-board:not(.is-cost) #om-detail,
91
+ .spend-board:not(.is-cost) .spend-detail-only { display: none !important; }
92
+ .spend-board:not(.grain-day) .grain-day { display: none !important; }
93
+ .spend-board:not(.grain-week) .grain-week { display: none !important; }
94
+ .spend-board:not(.grain-month) .grain-month { display: none !important; }
95
+ #om-tip {
96
+ position: fixed; z-index: 4000; max-width: min(280px, calc(100vw - 16px));
97
+ padding: 8px 10px; background: var(--elev, #1c2430); color: var(--text, #e7edf5);
98
+ border: 1px solid var(--line); border-radius: 8px; font-size: 12px; line-height: 1.4;
99
+ pointer-events: none; box-shadow: 0 10px 30px rgba(0,0,0,.4);
100
+ }
101
+ #om-tip strong { display: block; margin-bottom: 4px; font-size: 12px; }
102
+ #om-advanced table { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 8px; }
103
+ #om-advanced th, #om-advanced td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
104
+ #om-advanced th { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: .04em; }
105
+ #om-advanced td.num { font-variant-numeric: tabular-nums; text-align: right; }
106
+ #om-advanced .pill { display: inline-block; padding: 1px 6px; border-radius: 999px; background: var(--chip, #243044); font-size: 11px; }
107
+ .spend-board button.spend-stage, .spend-stage {
108
+ display: grid; grid-template-columns: 10.5rem 1fr 4.2rem; gap: 8px; align-items: center;
109
+ font-size: 12px; border: 0; background: transparent; color: inherit; text-align: left;
110
+ padding: 4px 0; cursor: help; font: inherit; width: 100%; border-radius: 0; }
111
+ .spend-stage > span:first-child { white-space: nowrap; }
112
+ .spend-board button.spend-stage:hover { border-color: transparent; color: inherit; }
113
+ .spend-stage .track { display: block; height: 10px; background: var(--chip, #243044);
114
+ border-radius: 5px; overflow: hidden; }
115
+ .spend-stage .fill { display: block; height: 100%; width: 0; max-width: 100%;
116
+ border-radius: 5px; min-width: 0; }
117
+ .spend-stage .nm { font-variant-numeric: tabular-nums; text-align: right; color: var(--muted); }
118
+ .spend-board:not(.is-cost) .cost-only { display: none !important; }
119
+ .spend-board.is-cost .catch-only { display: none !important; }
120
+ #om-chart-axis text { font-size: 10px; fill: var(--muted); }
121
+ .spend-caps { margin: 12px 0 2px; display: flex; flex-direction: column; gap: 10px; }
122
+ .spend-cap-row { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; }
123
+ .spend-cap-row .nm { color: var(--muted); font-variant-numeric: tabular-nums; }
124
+ .spend-cap .track { display: block; height: 10px; margin-top: 6px; background: var(--chip, #2a3126);
125
+ border-radius: 5px; overflow: hidden; }
126
+ .spend-cap .fill { display: block; height: 100%; width: 0; max-width: 100%;
127
+ background: var(--accent); border-radius: 5px; }
128
+ .spend-cap.is-full .fill { background: var(--warn); }
129
+ .spend-cap.is-broken .track { opacity: 0.45; }
130
+ `;
131
+ function isDashboard(input) {
132
+ return Array.isArray(input.days);
133
+ }
134
+ export function renderSpendBoard(input, generatedAt = new Date()) {
135
+ const data = isDashboard(input) ? input : spendDashboardFromStats(input);
136
+ const json = JSON.stringify(payloadOf(data, generatedAt)).replace(/</g, "\\u003c");
137
+ const startCost = Boolean(data.token_budget?.tightest);
138
+ return `<div class="spend-board grain-day${startCost ? " is-cost" : ""}" id="om-spend">
139
+ <script type="application/json" id="om-spend-json">${json}</script>
140
+ ${hero(data)}
141
+ <div class="spend-card">
142
+ <div class="spend-toolbar">
143
+ <div class="spend-cluster">
144
+ <div class="spend-seg" id="om-mode" role="group" aria-label="What to show">
145
+ <button type="button" data-mode="catchup"${startCost ? "" : ' class="on"'}>Catch-up</button>
146
+ <button type="button" data-mode="cost"${startCost ? ' class="on"' : ""}>Cost</button>
147
+ </div>
148
+ </div>
149
+ <span class="spend-split" aria-hidden="true"></span>
150
+ <div class="spend-cluster">
151
+ <div class="spend-seg" id="om-grain" role="group" aria-label="Bar size">
152
+ <button type="button" data-grain="day" class="on">Day</button>
153
+ <button type="button" data-grain="week">Week</button>
154
+ <button type="button" data-grain="month">Month</button>
155
+ </div>
156
+ </div>
157
+ <span class="spend-split" aria-hidden="true"></span>
158
+ <div class="spend-cluster">
159
+ <div class="spend-seg grain-day" id="om-period-day" role="group" aria-label="How far back">
160
+ <button type="button" data-period="7">7 days</button>
161
+ <button type="button" data-period="14" class="on">14 days</button>
162
+ <button type="button" data-period="30">30 days</button>
163
+ </div>
164
+ <div class="spend-seg grain-week" id="om-period-week" role="group" aria-label="How far back">
165
+ <button type="button" data-period="8">8 weeks</button>
166
+ <button type="button" data-period="12" class="on">12 weeks</button>
167
+ <button type="button" data-period="26">26 weeks</button>
168
+ </div>
169
+ <div class="spend-seg grain-month" id="om-period-month" role="group" aria-label="How far back">
170
+ <button type="button" data-period="6">6 months</button>
171
+ <button type="button" data-period="12" class="on">12 months</button>
172
+ </div>
173
+ </div>
174
+ <button type="button" class="grow cost-only" id="om-detail">More detail</button>
175
+ </div>
176
+ <p class="spend-chart-title" id="om-chart-title">${startCost ? "Tokens the model read" : "Chat written, and how much is still waiting"}</p>
177
+ <div class="spend-seg catch-only" id="om-catch-metric" role="group" aria-label="Catch-up units" style="margin:0 0 8px">
178
+ <button type="button" data-cmetric="waiting" class="on">Waiting vs read</button>
179
+ <button type="button" data-cmetric="facts">Facts written</button>
180
+ </div>
181
+ <div class="spend-seg cost-only spend-detail-only" id="om-metrics" role="group" aria-label="Cost units" style="margin-bottom:8px">
182
+ <button type="button" data-metric="tokens">Tokens</button>
183
+ <button type="button" data-metric="calls">Uses</button>
184
+ <button type="button" data-metric="time">Time</button>
185
+ </div>
186
+ <div class="spend-seg cost-only spend-detail-only" id="om-filter" role="group" aria-label="Which work" style="margin-bottom:8px">
187
+ <button type="button" data-filter="all" class="on">All work</button>
188
+ <button type="button" data-filter="read">Reading chat</button>
189
+ <button type="button" data-filter="file">Filing memory</button>
190
+ </div>
191
+ <svg id="om-chart" viewBox="0 0 720 228" role="img" aria-label="Activity by day"></svg>
192
+ <div class="spend-legend" id="om-legend"></div>
193
+ </div>
194
+ <div class="spend-card">
195
+ <p class="spend-chart-title" id="om-day-title">That day</p>
196
+ <div id="om-stage-bars"></div>
197
+ <p class="spend-quiet" id="om-day-note"></p>
198
+ </div>
199
+ <div class="spend-card spend-detail-only" id="om-advanced">
200
+ <p class="spend-quiet">Each model run in this bar</p>
201
+ <div id="om-runs"></div>
202
+ </div>
203
+ <div id="om-tip" hidden></div>
204
+ </div>
205
+ <script>${SPEND_BOARD_JS}</script>`;
206
+ }
207
+ export function formatStatsHtml(stats, generatedAt = new Date()) {
208
+ return `<!DOCTYPE html>
209
+ <html lang="en-GB">
210
+ <head>
211
+ <meta charset="utf-8">
212
+ <meta name="viewport" content="width=device-width, initial-scale=1">
213
+ <title>Facthouse spend</title>
214
+ <link rel="icon" href="${ledgerFaviconHref()}"/>
215
+ <style>
216
+ ${ledgerSpendCss()}
217
+ html, body { margin: 0; background: var(--bg); color: var(--ink);
218
+ font: 15px/1.45 "Segoe UI", ui-sans-serif, system-ui, sans-serif; }
219
+ .spend-brand { display: flex; align-items: center; gap: 10px; padding: 16px 20px 0;
220
+ max-width: 920px; margin: 0 auto; }
221
+ .spend-brand svg { display: block; width: 28px; height: 28px; border-radius: 6px; }
222
+ .spend-brand h1 { font-size: 15px; font-weight: 650; letter-spacing: -0.02em; margin: 0; }
223
+ .spend-brand p { margin: 1px 0 0; font-size: 11px; letter-spacing: 0.08em;
224
+ text-transform: uppercase; color: var(--muted); }
225
+ main { padding: 20px 20px 56px; }
226
+ ${SPEND_BOARD_CSS}
227
+ </style>
228
+ </head>
229
+ <body>
230
+ <div class="spend-brand">
231
+ <span>${LEDGER_MARK_SVG}</span>
232
+ <div>
233
+ <h1>Facthouse</h1>
234
+ <p>Spend</p>
235
+ </div>
236
+ </div>
237
+ <main>${renderSpendBoard(stats, generatedAt)}</main>
238
+ </body>
239
+ </html>
240
+ `;
241
+ }
242
+ function payloadOf(data, generatedAt) {
243
+ const stages = stagesIn(data.runs);
244
+ const totals = stageTotals(data.runs);
245
+ const since = new Date(generatedAt.getTime() - 24 * 60 * 60 * 1000).toISOString();
246
+ const last24Runs = data.runs.filter((r) => r.created_at >= since);
247
+ const last24Stages = stageTotals(last24Runs);
248
+ let topId = "";
249
+ let topCalls = 0;
250
+ for (const [id, s] of Object.entries(last24Stages)) {
251
+ if (s.calls > topCalls) {
252
+ topCalls = s.calls;
253
+ topId = id;
254
+ }
255
+ }
256
+ const hasTokens = data.days.some((d) => d.input_tokens != null) ||
257
+ data.last_24h_input != null;
258
+ return {
259
+ days: data.days,
260
+ runs: data.runs,
261
+ unread: data.unread_events,
262
+ pending: data.pending_facts,
263
+ reclaimable: data.reclaimable_events,
264
+ last24: {
265
+ calls: data.last_24h_calls,
266
+ input: data.last_24h_input ?? null,
267
+ output: data.last_24h_output ?? null,
268
+ elapsed: data.last_24h_elapsed_ms,
269
+ top: topId,
270
+ topCalls,
271
+ },
272
+ hasTokens,
273
+ budget: data.token_budget ?? null,
274
+ stages: stages.map((id) => {
275
+ const help = STAGE_HELP[id];
276
+ return {
277
+ id,
278
+ colour: STAGE_COLOUR[id] ?? "#8b98a8",
279
+ calls: totals[id]?.calls ?? 0,
280
+ group: help?.group ?? "file",
281
+ title: help?.title ?? id,
282
+ does: help?.does ?? "A step of turning chat into memory.",
283
+ when: help?.when ?? "",
284
+ };
285
+ }),
286
+ };
287
+ }
288
+ function hero(data) {
289
+ const unread = data.unread_events;
290
+ const pending = data.pending_facts;
291
+ const calls = data.last_24h_calls;
292
+ const catchLead = unread === 0
293
+ ? "Caught up."
294
+ : `${fmtInt(unread)} chat lines still waiting.`;
295
+ const catchBody = unread === 0
296
+ ? "Everything logged has been read into draft memory."
297
+ : "Those lines are in the store but have not been turned into facts yet.";
298
+ const catchDo = unread === 0
299
+ ? pending > 0
300
+ ? `${fmtInt(pending)} drafts are waiting to become lasting facts — run consolidate.`
301
+ : "Nothing you need to do."
302
+ : "Run consolidate when you want catch-up. That uses the model.";
303
+ const tokens = data.last_24h_input;
304
+ const costLead = tokens != null && tokens > 0
305
+ ? `${compact(tokens)} tokens in the last day.`
306
+ : calls === 0
307
+ ? "Quiet day."
308
+ : `${fmtInt(calls)} model uses in the last day.`;
309
+ const budget = data.token_budget;
310
+ const tight = budget?.tightest;
311
+ const unmetered = Boolean(budget?.providers && Object.values(budget.providers).some((p) => p?.unmetered));
312
+ const capSpent = Boolean(tight && tight.remaining <= 0);
313
+ const costWarn = capSpent || unmetered;
314
+ let costLeadBudget = costLead;
315
+ let costBody = tokens != null && tokens > 0
316
+ ? `${fmtInt(calls)} uses in the last 24 hours. Embeddings are not counted.`
317
+ : calls === 0
318
+ ? "No billed consolidation in the last 24 hours."
319
+ : `The model did not report tokens. ${fmtInt(calls)} uses in the last 24 hours.`;
320
+ if (unmetered) {
321
+ costLeadBudget = "Tokens were not reported, so billed extract is paused.";
322
+ costBody = "";
323
+ }
324
+ else if (tight) {
325
+ const lines = tokenBudgetUsageLead(tight);
326
+ costLeadBudget = lines.lead;
327
+ costBody = capSpent
328
+ ? `Billed extract is paused until it ${tight.resets_at ? `resets ${formatResetAt(tight.resets_at)}` : "ages"}, or you raise the cap.`
329
+ : lines.detail;
330
+ }
331
+ const howTo = budget?.how_to ?? TOKEN_BUDGET_HOW_TO;
332
+ const reclaim = data.reclaimable_events > 0
333
+ ? `<p class="do">${fmtInt(data.reclaimable_events)} old lines can be deleted with prune — dry-run first.</p>`
334
+ : "";
335
+ return `<div class="spend-hero">
336
+ <div class="spend-hero-card${unread > 0 ? " warn" : ""}">
337
+ <h2>Catch-up</h2>
338
+ <p class="lead">${esc(catchLead)}</p>
339
+ <p>${esc(catchBody)}</p>
340
+ <p class="do">${esc(catchDo)}</p>
341
+ </div>
342
+ <div class="spend-hero-card${costWarn ? " warn" : ""}">
343
+ <h2>Cost</h2>
344
+ <p class="lead">${esc(costLeadBudget)}</p>
345
+ ${capMeters(data)}
346
+ ${costBody ? `<p>${esc(costBody)}</p>` : ""}
347
+ <p class="do">${howToHtml(howTo)}</p>
348
+ ${reclaim}
349
+ </div>
350
+ </div>`;
351
+ }
352
+ function capMeters(data) {
353
+ const budget = data.token_budget;
354
+ if (!budget)
355
+ return "";
356
+ const rows = [];
357
+ for (const [provider, slice] of Object.entries(budget.providers)) {
358
+ if (!slice)
359
+ continue;
360
+ if (slice.unmetered) {
361
+ rows.push(`<div class="spend-cap is-broken" role="img" aria-label="${esc(billedProviderName(provider))}: tokens were not reported">` +
362
+ `<div class="spend-cap-row"><span>${esc(billedProviderName(provider))}</span>` +
363
+ `<span class="nm">not reported</span></div>` +
364
+ `<span class="track"></span></div>`);
365
+ continue;
366
+ }
367
+ for (const w of slice.windows) {
368
+ const pct = w.cap > 0 ? Math.min(100, (100 * w.used) / w.cap) : 0;
369
+ const full = w.remaining <= 0;
370
+ const name = billedCapName(provider, w.scale);
371
+ const used = `${compact(w.used)} used · ${compact(w.remaining)} remaining`;
372
+ rows.push(`<div class="spend-cap${full ? " is-full" : ""}" role="img" aria-label="${esc(name)}: ${esc(used)}">` +
373
+ `<div class="spend-cap-row"><span>${esc(name)}</span>` +
374
+ `<span class="nm">${esc(used)}</span></div>` +
375
+ `<span class="track"><span class="fill" style="width:${pct.toFixed(1)}%"></span></span></div>`);
376
+ }
377
+ }
378
+ return rows.length ? `<div class="spend-caps">${rows.join("")}</div>` : "";
379
+ }
380
+ function howToHtml(text) {
381
+ return text.split("\n").map((line) => esc(line)).join("<br>");
382
+ }
383
+ function compact(n) {
384
+ if (n == null)
385
+ return "—";
386
+ return formatTokenCount(n);
387
+ }
388
+ function fmtInt(n) {
389
+ return Math.round(n).toLocaleString("en-GB");
390
+ }
391
+ function esc(value) {
392
+ return value
393
+ .replace(/&/g, "&amp;")
394
+ .replace(/</g, "&lt;")
395
+ .replace(/>/g, "&gt;")
396
+ .replace(/"/g, "&quot;");
397
+ }
398
+ const SPEND_BOARD_JS = `(function(){
399
+ var C = ${JSON.stringify({
400
+ drafts: LEDGER.stage.classify,
401
+ lasting: LEDGER.stage.entities,
402
+ waiting: LEDGER.stage.extract,
403
+ muted: LEDGER.dark.muted,
404
+ })};
405
+ var box = document.getElementById("om-spend-json");
406
+ if (!box) return;
407
+ var D;
408
+ try { D = JSON.parse(box.textContent || "{}"); } catch (err) { return; }
409
+ var days = D.days || [];
410
+ var runs = D.runs || [];
411
+ var stages = D.stages || [];
412
+ var spendBucketKey = ${spendBucketKey.toString()};
413
+ var rollSpendDays = ${rollSpendDays.toString()};
414
+ var spendBucketLabel = ${spendBucketLabel.toString()};
415
+ var grain = "day";
416
+ var periodByGrain = { day: 14, week: 12, month: 12 };
417
+ var mode = D.budget && D.budget.tightest ? "cost" : "catchup";
418
+ var metric = D.hasTokens ? "tokens" : "calls";
419
+ var cmetric = "waiting";
420
+ var filter = "all";
421
+ var detail = false;
422
+ var selected = null;
423
+ var chart = document.getElementById("om-chart");
424
+ var legend = document.getElementById("om-legend");
425
+ var title = document.getElementById("om-chart-title");
426
+ var dayTitle = document.getElementById("om-day-title");
427
+ var dayNote = document.getElementById("om-day-note");
428
+ var bars = document.getElementById("om-stage-bars");
429
+ var runsEl = document.getElementById("om-runs");
430
+ var root = document.getElementById("om-spend");
431
+ var tip = document.getElementById("om-tip");
432
+ if (tip && tip.parentNode !== document.body) document.body.appendChild(tip);
433
+
434
+ function fmt(n){ return Math.round(n).toLocaleString("en-GB"); }
435
+ var formatTokenCount = ${formatTokenCount.toString()};
436
+ function compact(n){
437
+ if (n == null) return "—";
438
+ return formatTokenCount(n);
439
+ }
440
+ function whoRan(t){
441
+ if (t === "cli") return "CLI";
442
+ if (t === "mcp") return "MCP";
443
+ if (t === "scheduler") return "scheduler";
444
+ return "—";
445
+ }
446
+ function niceDay(iso){ return spendBucketLabel(iso, grain); }
447
+ function fmtTime(ms){
448
+ if (ms == null) return "—";
449
+ if (ms >= 60000) {
450
+ var m = Math.floor(ms / 60000);
451
+ var s = Math.round((ms % 60000) / 1000);
452
+ return s ? m + "m " + s + "s" : m + "m";
453
+ }
454
+ if (ms >= 1000) return (ms / 1000).toFixed(ms >= 10000 ? 0 : 1) + "s";
455
+ return fmt(ms) + " ms";
456
+ }
457
+ function fmtMetric(n){
458
+ if (metric === "tokens") return compact(n);
459
+ if (metric === "time") return fmtTime(n);
460
+ return fmt(n);
461
+ }
462
+ function viewDays(){
463
+ var rolled = rollSpendDays(days, grain);
464
+ var n = periodByGrain[grain] || 14;
465
+ if (rolled.length <= n) return rolled;
466
+ return rolled.slice(rolled.length - n);
467
+ }
468
+ function activeStages(){
469
+ if (filter === "read") return stages.filter(function(s){ return s.group === "read"; });
470
+ if (filter === "file") return stages.filter(function(s){ return s.group === "file"; });
471
+ return stages;
472
+ }
473
+ function dayRuns(bucket){
474
+ return runs.filter(function(r){
475
+ return spendBucketKey((r.created_at || "").slice(0, 10), grain) === bucket;
476
+ });
477
+ }
478
+ function stageAmt(st, list){
479
+ var n = 0;
480
+ list.forEach(function(r){
481
+ var x = r.stages && r.stages[st.id];
482
+ if (!x) return;
483
+ if (metric === "tokens") n += x.input_tokens || 0;
484
+ else if (metric === "time") n += x.elapsed_ms || 0;
485
+ else n += x.calls;
486
+ });
487
+ return n;
488
+ }
489
+ function dayValue(d){
490
+ if (mode === "catchup") return cmetric === "facts" ? (d.staged + d.integrated) : d.logged;
491
+ if (metric === "tokens") {
492
+ if (filter === "all") return d.input_tokens || 0;
493
+ var n = 0;
494
+ activeStages().forEach(function(s){ n += stageAmt(s, dayRuns(d.day)); });
495
+ return n;
496
+ }
497
+ if (metric === "time") {
498
+ var ms = 0;
499
+ dayRuns(d.day).forEach(function(r){
500
+ if (filter === "all") { ms += r.elapsed_ms || 0; return; }
501
+ activeStages().forEach(function(s){ ms += stageAmt(s, [r]); });
502
+ });
503
+ return ms;
504
+ }
505
+ if (filter === "all") return d.calls;
506
+ var c = 0;
507
+ activeStages().forEach(function(s){ c += stageAmt(s, dayRuns(d.day)); });
508
+ return c;
509
+ }
510
+ var axisTicks = ${spendAxisTicks.toString()};
511
+ function axisY(lab){
512
+ if (mode === "cost" && metric === "time") {
513
+ return lab >= 60000 ? Math.round(lab / 60000) + "m" : Math.round(lab / 1000) + "s";
514
+ }
515
+ if ((mode === "cost" && metric === "tokens") || lab >= 10000) return compact(lab);
516
+ return String(lab);
517
+ }
518
+ function placeTip(ev){
519
+ if (!tip) return;
520
+ var pad = 14;
521
+ var tw = tip.offsetWidth || 240;
522
+ var th = tip.offsetHeight || 72;
523
+ var x = ev.clientX + pad;
524
+ var y = ev.clientY + pad;
525
+ if (x + tw > innerWidth - 8) x = ev.clientX - tw - pad;
526
+ if (y + th > innerHeight - 8) y = ev.clientY - th - pad;
527
+ tip.style.left = Math.max(8, x) + "px";
528
+ tip.style.top = Math.max(8, y) + "px";
529
+ }
530
+ function showTip(ev, html){
531
+ if (!tip) return;
532
+ tip.hidden = false;
533
+ tip.innerHTML = html;
534
+ placeTip(ev);
535
+ }
536
+ function hideTip(){ if (tip) tip.hidden = true; }
537
+
538
+ function draw(){
539
+ if (!chart) return;
540
+ var view = viewDays();
541
+ var w = 720, h = 228, l = 44, r = 10, t = 10, b = 42;
542
+ var iw = w - l - r, ih = h - t - b;
543
+ var max = 0;
544
+ for (var i = 0; i < view.length; i++) {
545
+ var v = dayValue(view[i]);
546
+ if (v > max) max = v;
547
+ }
548
+ var slot = iw / Math.max(view.length, 1);
549
+ var bw = Math.min(slot * 0.72, grain === "day" ? 26 : 52);
550
+ var svg = "";
551
+ if (max <= 0) {
552
+ var yZero = t + ih;
553
+ svg += '<line x1="'+l+'" x2="'+(w-r)+'" y1="'+yZero+'" y2="'+yZero+'" stroke="var(--line)"/>';
554
+ svg += '<text x="'+(l-6)+'" y="'+(yZero+3)+'" text-anchor="end" fill="var(--muted)" font-size="10">0</text>';
555
+ } else {
556
+ for (var g = 0; g <= 4; g++){
557
+ var y = t + ih * g / 4;
558
+ var lab = Math.round(max * (4 - g) / 4);
559
+ svg += '<line x1="'+l+'" x2="'+(w-r)+'" y1="'+y+'" y2="'+y+'" stroke="var(--line)"/>';
560
+ svg += '<text x="'+(l-6)+'" y="'+(y+3)+'" text-anchor="end" fill="var(--muted)" font-size="10">'+axisY(lab)+'</text>';
561
+ }
562
+ }
563
+ for (var i = 0; i < view.length; i++){
564
+ var d = view[i];
565
+ var x = l + slot * i + (slot - bw) / 2;
566
+ var on = selected === d.day ? " is-on" : "";
567
+ svg += '<g class="om-day'+on+'" data-day="'+d.day+'">';
568
+ if (mode === "catchup" && cmetric === "facts"){
569
+ var stH = max ? d.staged / max * ih : 0;
570
+ var gH = max ? d.integrated / max * ih : 0;
571
+ var yS = t + ih - gH - stH;
572
+ var yG = t + ih - gH;
573
+ if (stH > 0) svg += '<rect x="'+x+'" y="'+yS+'" width="'+bw+'" height="'+stH+'" fill="'+C.drafts+'" rx="2"/>';
574
+ if (gH > 0) svg += '<rect x="'+x+'" y="'+yG+'" width="'+bw+'" height="'+gH+'" fill="'+C.lasting+'" rx="2"/>';
575
+ if (d.staged + d.integrated === 0) svg += '<rect x="'+x+'" y="'+(t+ih-2)+'" width="'+bw+'" height="2" fill="var(--line)"/>';
576
+ } else if (mode === "catchup"){
577
+ var examH = max ? d.examined / max * ih : 0;
578
+ var unreadH = max ? d.unread / max * ih : 0;
579
+ var yU = t + ih - examH - unreadH;
580
+ var yE = t + ih - examH;
581
+ if (unreadH > 0) svg += '<rect x="'+x+'" y="'+yU+'" width="'+bw+'" height="'+unreadH+'" fill="'+C.waiting+'" rx="2"/>';
582
+ if (examH > 0) svg += '<rect x="'+x+'" y="'+yE+'" width="'+bw+'" height="'+examH+'" fill="'+C.lasting+'" rx="2"/>';
583
+ if (d.logged === 0) svg += '<rect x="'+x+'" y="'+(t+ih-2)+'" width="'+bw+'" height="2" fill="var(--line)"/>';
584
+ } else {
585
+ var y = t + ih;
586
+ var st = activeStages();
587
+ var list = dayRuns(d.day);
588
+ st.forEach(function(s){
589
+ var c = stageAmt(s, list);
590
+ var bh = max ? c / max * ih : 0;
591
+ if (bh <= 0) return;
592
+ y -= bh;
593
+ svg += '<rect x="'+x+'" y="'+y+'" width="'+bw+'" height="'+bh+'" fill="'+s.colour+'" rx="1"/>';
594
+ });
595
+ if (dayValue(d) === 0) svg += '<rect x="'+x+'" y="'+(t+ih-2)+'" width="'+bw+'" height="2" fill="var(--line)"/>';
596
+ }
597
+ var ax = axisTicks(i, view, grain);
598
+ var cx = x + bw / 2;
599
+ if (ax.month) {
600
+ if (ax.day) svg += '<text x="'+cx+'" y="'+(h-22)+'" text-anchor="middle" fill="var(--muted)" font-size="10">'+ax.day+'</text>';
601
+ svg += '<text x="'+cx+'" y="'+(h-8)+'" text-anchor="middle" fill="var(--muted)" font-size="10" font-weight="600">'+ax.month+'</text>';
602
+ } else if (ax.day) {
603
+ svg += '<text x="'+cx+'" y="'+(h-12)+'" text-anchor="middle" fill="var(--muted)" font-size="10">'+ax.day+'</text>';
604
+ }
605
+ svg += '</g>';
606
+ }
607
+ chart.innerHTML = svg;
608
+ chart.querySelectorAll(".om-day").forEach(function(g){
609
+ g.addEventListener("mousemove", function(ev){
610
+ var day = g.getAttribute("data-day");
611
+ var d = view.find(function(x){ return x.day === day; });
612
+ if (!d) return;
613
+ var html;
614
+ if (mode === "catchup" && cmetric === "facts"){
615
+ html = "<strong>"+niceDay(d.day)+"</strong>"+fmt(d.staged)+" drafts · "+fmt(d.integrated)+" lasting facts";
616
+ } else if (mode === "catchup"){
617
+ html = "<strong>"+niceDay(d.day)+"</strong>"+fmt(d.logged)+" new lines<br>"+
618
+ fmt(d.examined)+" read · "+fmt(d.unread)+" waiting";
619
+ } else if (metric === "tokens"){
620
+ html = "<strong>"+niceDay(d.day)+"</strong>"+compact(dayValue(d))+" tokens in";
621
+ } else if (metric === "time"){
622
+ html = "<strong>"+niceDay(d.day)+"</strong>"+fmtTime(dayValue(d));
623
+ } else {
624
+ html = "<strong>"+niceDay(d.day)+"</strong>"+fmt(dayValue(d))+" uses";
625
+ }
626
+ showTip(ev, html);
627
+ });
628
+ g.addEventListener("mouseleave", hideTip);
629
+ g.addEventListener("click", function(){
630
+ selected = g.getAttribute("data-day");
631
+ render();
632
+ });
633
+ });
634
+ }
635
+
636
+ function legendHtml(){
637
+ if (!legend) return;
638
+ if (mode === "catchup" && cmetric === "facts"){
639
+ legend.innerHTML = '<span><i class="sw" style="background:'+C.drafts+'"></i>Drafts</span>'+
640
+ '<span><i class="sw" style="background:'+C.lasting+'"></i>Lasting facts</span>';
641
+ return;
642
+ }
643
+ if (mode === "catchup"){
644
+ legend.innerHTML = '<span><i class="sw" style="background:'+C.lasting+'"></i>Already read</span>'+
645
+ '<span><i class="sw" style="background:'+C.waiting+'"></i>Still waiting</span>';
646
+ return;
647
+ }
648
+ legend.innerHTML = activeStages().map(function(s){
649
+ return '<button type="button" data-stage="'+s.id+'"><i class="sw" style="background:'+s.colour+'"></i>'+s.title+'</button>';
650
+ }).join("");
651
+ legend.querySelectorAll("button").forEach(function(b){
652
+ b.addEventListener("mousemove", function(ev){
653
+ var s = stages.find(function(x){ return x.id === b.getAttribute("data-stage"); });
654
+ if (s) showTip(ev, "<strong>"+s.title+"</strong>"+s.does+(s.when ? "<br>"+s.when : ""));
655
+ });
656
+ b.addEventListener("mouseleave", hideTip);
657
+ });
658
+ }
659
+
660
+ function pickDay(){
661
+ var view = viewDays();
662
+ var day = selected;
663
+ var hit = null;
664
+ if (day) {
665
+ hit = view.find(function(x){ return x.day === day; }) || null;
666
+ if (!hit) {
667
+ var mapped = spendBucketKey(day, grain);
668
+ hit = view.find(function(x){ return x.day === mapped; }) || null;
669
+ }
670
+ }
671
+ if (!hit) {
672
+ for (var i = view.length - 1; i >= 0; i--) {
673
+ if (view[i].logged || view[i].calls || view[i].staged || view[i].integrated) {
674
+ hit = view[i];
675
+ break;
676
+ }
677
+ }
678
+ if (!hit && view.length) hit = view[view.length - 1];
679
+ }
680
+ selected = hit ? hit.day : null;
681
+ return hit;
682
+ }
683
+ function paintBars(rows, withTip){
684
+ if (!bars) return;
685
+ var max = 1;
686
+ rows.forEach(function(r){ if (r.n > max) max = r.n; });
687
+ bars.innerHTML = rows.map(function(r){
688
+ var pct = 100 * r.n / max;
689
+ var attr = withTip && r.id ? ' data-stage="'+r.id+'"' : "";
690
+ return '<button type="button" class="spend-stage"'+attr+'>'+
691
+ '<span>'+r.title+'</span>'+
692
+ '<span class="track"><span class="fill" style="width:'+pct.toFixed(1)+'%;background:'+r.colour+'"></span></span>'+
693
+ '<span class="nm">'+r.label+'</span></button>';
694
+ }).join("") || '<p class="spend-quiet">Nothing to show.</p>';
695
+ if (!withTip) return;
696
+ bars.querySelectorAll(".spend-stage").forEach(function(b){
697
+ b.addEventListener("mousemove", function(ev){
698
+ var s = stages.find(function(x){ return x.id === b.getAttribute("data-stage"); });
699
+ if (s) showTip(ev, "<strong>"+s.title+"</strong>"+s.does+(s.when ? "<br>"+s.when : ""));
700
+ });
701
+ b.addEventListener("mouseleave", hideTip);
702
+ });
703
+ }
704
+ function writeDay(){
705
+ var d = pickDay();
706
+ if (dayTitle) dayTitle.textContent = d ? niceDay(d.day) : "That day";
707
+ if (!d){
708
+ if (bars) bars.innerHTML = '<p class="spend-quiet">No activity in this window.</p>';
709
+ if (dayNote) dayNote.textContent = "";
710
+ if (runsEl) runsEl.innerHTML = "";
711
+ return;
712
+ }
713
+ selected = d.day;
714
+ if (mode === "catchup"){
715
+ if (cmetric === "facts"){
716
+ paintBars([
717
+ { title: "Drafts", n: d.staged, colour: C.drafts, label: fmt(d.staged) },
718
+ { title: "Lasting facts", n: d.integrated, colour: C.lasting, label: fmt(d.integrated) }
719
+ ], false);
720
+ if (dayNote) dayNote.textContent = "Click another bar to compare.";
721
+ } else {
722
+ paintBars([
723
+ { title: "New chat", n: d.logged, colour: C.muted, label: fmt(d.logged) },
724
+ { title: "Already read", n: d.examined, colour: C.lasting, label: fmt(d.examined) },
725
+ { title: "Still waiting", n: d.unread, colour: C.waiting, label: fmt(d.unread) }
726
+ ], false);
727
+ if (dayNote) dayNote.textContent = "Click another bar to compare.";
728
+ }
729
+ } else {
730
+ var list = dayRuns(d.day);
731
+ var st = activeStages();
732
+ if (!list.length || !st.length){
733
+ if (bars) bars.innerHTML = '<p class="spend-quiet">No model work in this bar.</p>';
734
+ } else {
735
+ paintBars(st.map(function(s){
736
+ var n = stageAmt(s, list);
737
+ return { title: s.title, n: n, colour: s.colour, id: s.id, label: fmtMetric(n) };
738
+ }), true);
739
+ }
740
+ if (dayNote) {
741
+ dayNote.textContent = metric === "tokens"
742
+ ? "Longer bar = more tokens that step read."
743
+ : metric === "time"
744
+ ? "Longer bar = more time that step took, not the bill."
745
+ : "Longer bar = more times that step ran.";
746
+ }
747
+ }
748
+ writeRuns(d.day);
749
+ }
750
+ function writeRuns(day){
751
+ if (!runsEl) return;
752
+ var list = dayRuns(day);
753
+ if (!list.length){
754
+ runsEl.innerHTML = '<p class="spend-quiet">No model run in this bar.</p>';
755
+ return;
756
+ }
757
+ var html = '<table><thead><tr><th>When</th><th>Who</th><th></th><th>Tokens in</th><th>Out</th><th>Uses</th><th>Time</th><th>What ran</th></tr></thead><tbody>';
758
+ list.forEach(function(r){
759
+ var bits = [];
760
+ Object.keys(r.stages || {}).forEach(function(k){
761
+ var s = r.stages[k];
762
+ var help = stages.find(function(x){ return x.id === k; });
763
+ var who = s.model ? (s.provider+"/"+s.model) : s.provider;
764
+ var amt = metric === "tokens"
765
+ ? compact(s.input_tokens || 0)+" tok"
766
+ : metric === "time"
767
+ ? fmtTime(s.elapsed_ms || 0)
768
+ : "× "+s.calls;
769
+ bits.push((help ? help.title : k)+" "+amt+" ("+who+")");
770
+ });
771
+ html += '<tr><td>'+r.created_at.replace("T"," ").replace(".000Z"," UTC").replace("Z"," UTC")+'</td>'+
772
+ '<td>'+whoRan(r.trigger)+'</td>'+
773
+ '<td><span class="pill">'+r.kind+'</span></td>'+
774
+ '<td class="num">'+(r.input_tokens == null ? "—" : compact(r.input_tokens))+'</td>'+
775
+ '<td class="num">'+(r.output_tokens == null ? "—" : compact(r.output_tokens))+'</td>'+
776
+ '<td class="num">'+fmt(r.calls)+'</td>'+
777
+ '<td class="num">'+fmtTime(r.elapsed_ms)+'</td>'+
778
+ '<td>'+bits.join("<br>")+'</td></tr>';
779
+ });
780
+ html += '</tbody></table>';
781
+ runsEl.innerHTML = html;
782
+ }
783
+
784
+ function chartTitle(){
785
+ if (!title) return;
786
+ if (mode === "catchup") {
787
+ title.textContent = cmetric === "facts"
788
+ ? "Drafts and lasting facts written"
789
+ : "Chat written, and how much is still waiting";
790
+ return;
791
+ }
792
+ if (metric === "tokens") title.textContent = "Tokens the model read";
793
+ else if (metric === "time") title.textContent = "How long the model ran";
794
+ else title.textContent = "How often the model ran";
795
+ }
796
+
797
+ function syncPeriodButtons(id, current){
798
+ document.querySelectorAll("#" + id + " button").forEach(function(x){
799
+ x.classList.toggle("on", Number(x.getAttribute("data-period")) === current);
800
+ });
801
+ }
802
+ function render(){
803
+ if (root) {
804
+ root.classList.toggle("is-detail", detail);
805
+ root.classList.toggle("is-cost", mode === "cost");
806
+ root.classList.toggle("grain-day", grain === "day");
807
+ root.classList.toggle("grain-week", grain === "week");
808
+ root.classList.toggle("grain-month", grain === "month");
809
+ }
810
+ var det = document.getElementById("om-detail");
811
+ if (det) det.classList.toggle("on", detail);
812
+ document.querySelectorAll("#om-mode button").forEach(function(x){
813
+ x.classList.toggle("on", x.getAttribute("data-mode") === mode);
814
+ });
815
+ document.querySelectorAll("#om-grain button").forEach(function(x){
816
+ x.classList.toggle("on", x.getAttribute("data-grain") === grain);
817
+ });
818
+ syncPeriodButtons("om-period-day", periodByGrain.day);
819
+ syncPeriodButtons("om-period-week", periodByGrain.week);
820
+ syncPeriodButtons("om-period-month", periodByGrain.month);
821
+ document.querySelectorAll("#om-metrics button").forEach(function(x){
822
+ var m = x.getAttribute("data-metric");
823
+ if (m === "tokens") x.hidden = !D.hasTokens;
824
+ x.classList.toggle("on", m === metric);
825
+ });
826
+ document.querySelectorAll("#om-catch-metric button").forEach(function(x){
827
+ x.classList.toggle("on", x.getAttribute("data-cmetric") === cmetric);
828
+ });
829
+ pickDay();
830
+ chartTitle();
831
+ draw();
832
+ legendHtml();
833
+ writeDay();
834
+ }
835
+
836
+ document.querySelectorAll("#om-mode button").forEach(function(b){
837
+ b.addEventListener("click", function(){
838
+ mode = b.getAttribute("data-mode") || "catchup";
839
+ document.querySelectorAll("#om-mode button").forEach(function(x){ x.classList.toggle("on", x === b); });
840
+ render();
841
+ });
842
+ });
843
+ document.querySelectorAll("#om-grain button").forEach(function(b){
844
+ b.addEventListener("click", function(){
845
+ grain = b.getAttribute("data-grain") || "day";
846
+ render();
847
+ });
848
+ });
849
+ document.querySelectorAll("#om-spend [data-period]").forEach(function(b){
850
+ b.addEventListener("click", function(){
851
+ var n = Number(b.getAttribute("data-period"));
852
+ if (n) periodByGrain[grain] = n;
853
+ render();
854
+ });
855
+ });
856
+ document.querySelectorAll("#om-catch-metric button").forEach(function(b){
857
+ b.addEventListener("click", function(){
858
+ cmetric = b.getAttribute("data-cmetric") || "waiting";
859
+ render();
860
+ });
861
+ });
862
+ document.querySelectorAll("#om-metrics button").forEach(function(b){
863
+ b.addEventListener("click", function(){
864
+ metric = b.getAttribute("data-metric") || "calls";
865
+ render();
866
+ });
867
+ });
868
+ document.querySelectorAll("#om-filter button").forEach(function(b){
869
+ b.addEventListener("click", function(){
870
+ filter = b.getAttribute("data-filter") || "all";
871
+ document.querySelectorAll("#om-filter button").forEach(function(x){ x.classList.toggle("on", x === b); });
872
+ render();
873
+ });
874
+ });
875
+ var detBtn = document.getElementById("om-detail");
876
+ if (detBtn) detBtn.addEventListener("click", function(){ detail = !detail; render(); });
877
+ render();
878
+ })();`;
879
+ //# sourceMappingURL=stats-html.js.map