nlm-memory 0.4.2 → 0.5.1

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 (285) hide show
  1. package/README.md +72 -34
  2. package/dist/cli/nlm.js +223 -33
  3. package/dist/cli/nlm.js.map +1 -1
  4. package/dist/core/adapters/cursor.d.ts +45 -0
  5. package/dist/core/adapters/cursor.js +397 -0
  6. package/dist/core/adapters/cursor.js.map +1 -0
  7. package/dist/core/adapters/from-source.js +10 -0
  8. package/dist/core/adapters/from-source.js.map +1 -1
  9. package/dist/core/adapters/windsurf.d.ts +44 -0
  10. package/dist/core/adapters/windsurf.js +299 -0
  11. package/dist/core/adapters/windsurf.js.map +1 -0
  12. package/dist/core/hook/claude-settings.d.ts +12 -5
  13. package/dist/core/hook/claude-settings.js +21 -6
  14. package/dist/core/hook/claude-settings.js.map +1 -1
  15. package/dist/core/sources/source-registry.d.ts +1 -1
  16. package/dist/core/sources/source-registry.js +18 -0
  17. package/dist/core/sources/source-registry.js.map +1 -1
  18. package/dist/core/storage/sqlite-session-store.d.ts +2 -0
  19. package/dist/core/storage/sqlite-session-store.js +38 -2
  20. package/dist/core/storage/sqlite-session-store.js.map +1 -1
  21. package/dist/hook/hook-auth.d.ts +13 -0
  22. package/dist/hook/hook-auth.js +19 -0
  23. package/dist/hook/hook-auth.js.map +1 -0
  24. package/dist/hook/prompt-recall-hook.js +7 -1
  25. package/dist/hook/prompt-recall-hook.js.map +1 -1
  26. package/dist/hook/session-start-hook.js +4 -1
  27. package/dist/hook/session-start-hook.js.map +1 -1
  28. package/dist/hook/stop-hook.js +4 -1
  29. package/dist/hook/stop-hook.js.map +1 -1
  30. package/dist/http/app.d.ts +2 -0
  31. package/dist/http/app.js +76 -1
  32. package/dist/http/app.js.map +1 -1
  33. package/dist/install/claude-code.js +1 -1
  34. package/dist/install/claude-code.js.map +1 -1
  35. package/dist/install/cursor.d.ts +25 -0
  36. package/dist/install/cursor.js +43 -0
  37. package/dist/install/cursor.js.map +1 -0
  38. package/dist/install/nlm-dir-perms.d.ts +19 -0
  39. package/dist/install/nlm-dir-perms.js +43 -0
  40. package/dist/install/nlm-dir-perms.js.map +1 -0
  41. package/dist/install/ollama.d.ts +18 -1
  42. package/dist/install/ollama.js +62 -7
  43. package/dist/install/ollama.js.map +1 -1
  44. package/dist/install/setup.d.ts +4 -0
  45. package/dist/install/setup.js +141 -18
  46. package/dist/install/setup.js.map +1 -1
  47. package/dist/install/windsurf.d.ts +25 -0
  48. package/dist/install/windsurf.js +43 -0
  49. package/dist/install/windsurf.js.map +1 -0
  50. package/dist/mcp/server.js +20 -1
  51. package/dist/mcp/server.js.map +1 -1
  52. package/dist/shared/types.d.ts +4 -0
  53. package/dist/ui/assets/{index-BA6IpU8g.css → index-Beo8psd-.css} +1 -1
  54. package/dist/ui/assets/index-CSPTTeeM.js +69 -0
  55. package/dist/ui/index.html +2 -2
  56. package/package.json +26 -1
  57. package/plugin/scripts/prompt-recall-hook.mjs +55 -4
  58. package/plugin/scripts/stop-hook.mjs +57 -6
  59. package/.agents/plugins/marketplace.json +0 -20
  60. package/.github/workflows/ci.yml +0 -30
  61. package/dist/ui/assets/index-B_qIVV0k.js +0 -69
  62. package/docs/methodology/re-derivation-rate.md +0 -112
  63. package/docs/methodology/useful-hit-rate.md +0 -79
  64. package/docs/plans/2026-05-20-fts5-lexical-recall.md +0 -1088
  65. package/docs/plans/2026-05-20-recall-daemon-wedge-fix.md +0 -662
  66. package/docs/plans/2026-05-20-recall-hook-design.md +0 -131
  67. package/docs/plans/2026-05-20-recall-hook-implementation.md +0 -1222
  68. package/docs/plans/desktop-product.md +0 -69
  69. package/docs/plans/factstore-design.md +0 -236
  70. package/logs/CHANGELOG/CHANGELOG-2026.md +0 -1389
  71. package/logs/CHANGELOG/CHANGELOG.md +0 -337
  72. package/migrations/000_initial_schema.sql +0 -174
  73. package/migrations/001_entity_type_rename.sql +0 -17
  74. package/migrations/002_adapter_state_extend.sql +0 -12
  75. package/migrations/003_session_embeddings.sql +0 -11
  76. package/migrations/004_facts.sql +0 -46
  77. package/migrations/005_sources.sql +0 -31
  78. package/migrations/006_providers.sql +0 -33
  79. package/migrations/007_source_tokens.sql +0 -17
  80. package/migrations/008_fts_rebuild.sql +0 -9
  81. package/migrations/009_session_embedding_chunks.sql +0 -46
  82. package/migrations/010_sources_opencode.sql +0 -30
  83. package/migrations/011_sources_hermes_agent.sql +0 -30
  84. package/migrations/012_sources_aider.sql +0 -30
  85. package/migrations/013_adapter_state_failure_count.sql +0 -12
  86. package/plugin-hermes-agent/README.md +0 -49
  87. package/plugin-hermes-agent/__init__.py +0 -75
  88. package/plugin-hermes-agent/plugin.yaml +0 -15
  89. package/scripts/backfill-citations.mjs +0 -0
  90. package/scripts/build-codex-plugin.mjs +0 -61
  91. package/scripts/deepseek-probe.mjs +0 -67
  92. package/scripts/extract-triples.mjs +0 -207
  93. package/scripts/longmemeval/embedding-cache.ts +0 -77
  94. package/scripts/longmemeval/fetch-dataset.sh +0 -25
  95. package/scripts/longmemeval/run-harness.ts +0 -315
  96. package/scripts/longmemeval/scorer.ts +0 -99
  97. package/scripts/longmemeval/tsconfig.json +0 -9
  98. package/scripts/longmemeval/types.ts +0 -35
  99. package/scripts/nlm-daily-digest.py +0 -239
  100. package/scripts/nlm-daily-digest.sh +0 -28
  101. package/src/cli/classify-parity.ts +0 -257
  102. package/src/cli/launchctl-helpers.ts +0 -49
  103. package/src/cli/nlm.ts +0 -885
  104. package/src/core/actions/actions-log.ts +0 -118
  105. package/src/core/actions/overlay.ts +0 -117
  106. package/src/core/adapters/aider.ts +0 -205
  107. package/src/core/adapters/claude-code.ts +0 -293
  108. package/src/core/adapters/common.ts +0 -54
  109. package/src/core/adapters/from-source.ts +0 -57
  110. package/src/core/adapters/hermes-agent.ts +0 -240
  111. package/src/core/adapters/hermes.ts +0 -277
  112. package/src/core/adapters/jsonl-generic.ts +0 -208
  113. package/src/core/adapters/opencode.ts +0 -281
  114. package/src/core/adapters/pi.ts +0 -264
  115. package/src/core/classifier/prompt.ts +0 -200
  116. package/src/core/dataset/build-dataset.ts +0 -463
  117. package/src/core/embedding/chunk-body.ts +0 -76
  118. package/src/core/embedding/embed-backfill.ts +0 -210
  119. package/src/core/embedding/embed-normalize.ts +0 -135
  120. package/src/core/facts/backfill-facts.ts +0 -254
  121. package/src/core/facts/extract-facts.ts +0 -50
  122. package/src/core/hook/citation-detect.ts +0 -124
  123. package/src/core/hook/cite-memo.ts +0 -68
  124. package/src/core/hook/claude-settings.ts +0 -166
  125. package/src/core/hook/gate.ts +0 -25
  126. package/src/core/hook/hook-log.ts +0 -41
  127. package/src/core/hook/memo-sweep.ts +0 -164
  128. package/src/core/hook/memo.ts +0 -67
  129. package/src/core/hook/pointer-block.ts +0 -26
  130. package/src/core/hook/select.ts +0 -32
  131. package/src/core/hook/transcript.ts +0 -121
  132. package/src/core/ingest/ingest-session.ts +0 -111
  133. package/src/core/providers/provider-models.ts +0 -100
  134. package/src/core/providers/provider-registry.ts +0 -196
  135. package/src/core/recall/citation-log.ts +0 -108
  136. package/src/core/recall/filter.ts +0 -27
  137. package/src/core/recall/index.ts +0 -6
  138. package/src/core/recall/match-fields.ts +0 -40
  139. package/src/core/recall/query-log.ts +0 -149
  140. package/src/core/recall/query-shape.ts +0 -66
  141. package/src/core/recall/recall-service.ts +0 -320
  142. package/src/core/recall/recent-log.ts +0 -59
  143. package/src/core/recall/tokenize.ts +0 -18
  144. package/src/core/recall/useful-scan.ts +0 -336
  145. package/src/core/recall-facts/fact-query-log.ts +0 -150
  146. package/src/core/recall-facts/fact-recall-service.ts +0 -327
  147. package/src/core/scheduler/scan-once.ts +0 -142
  148. package/src/core/scheduler/scheduler.ts +0 -225
  149. package/src/core/sources/source-registry.ts +0 -260
  150. package/src/core/storage/db-restore.ts +0 -133
  151. package/src/core/storage/live-status.ts +0 -45
  152. package/src/core/storage/migrate.ts +0 -72
  153. package/src/core/storage/sqlite-fact-store.ts +0 -304
  154. package/src/core/storage/sqlite-session-store.ts +0 -765
  155. package/src/hook/prompt-recall-hook.ts +0 -174
  156. package/src/hook/session-end-hook.ts +0 -81
  157. package/src/hook/session-start-hook.ts +0 -165
  158. package/src/hook/stop-hook.ts +0 -236
  159. package/src/http/app.ts +0 -1137
  160. package/src/install/claude-code.ts +0 -128
  161. package/src/install/codex.ts +0 -367
  162. package/src/install/hermes-agent.ts +0 -76
  163. package/src/install/hermes.ts +0 -78
  164. package/src/install/ollama.ts +0 -211
  165. package/src/install/setup.ts +0 -368
  166. package/src/llm/classifier-box.ts +0 -64
  167. package/src/llm/deepseek-client.ts +0 -150
  168. package/src/llm/env-autoload.ts +0 -55
  169. package/src/llm/ollama-client.ts +0 -189
  170. package/src/mcp/server.ts +0 -534
  171. package/src/ports/fact-store.ts +0 -102
  172. package/src/ports/llm-client.ts +0 -52
  173. package/src/ports/logger.ts +0 -16
  174. package/src/ports/session-store.ts +0 -45
  175. package/src/ports/transcript-adapter.ts +0 -55
  176. package/src/shared/types.ts +0 -145
  177. package/src/ui/App.tsx +0 -58
  178. package/src/ui/components/PromoteOpenButton.tsx +0 -65
  179. package/src/ui/components/SessionDrawer.tsx +0 -136
  180. package/src/ui/components/SideNav.tsx +0 -162
  181. package/src/ui/components/Skeleton.tsx +0 -107
  182. package/src/ui/index.html +0 -13
  183. package/src/ui/lib/actions.ts +0 -30
  184. package/src/ui/lib/api.ts +0 -92
  185. package/src/ui/lib/dataset.ts +0 -141
  186. package/src/ui/lib/registries.ts +0 -155
  187. package/src/ui/lib/view-settings.ts +0 -41
  188. package/src/ui/main.tsx +0 -15
  189. package/src/ui/pages/Live.tsx +0 -229
  190. package/src/ui/pages/Pulse.tsx +0 -415
  191. package/src/ui/pages/Recall.tsx +0 -190
  192. package/src/ui/pages/River.tsx +0 -308
  193. package/src/ui/pages/Search.tsx +0 -93
  194. package/src/ui/pages/Stub.tsx +0 -9
  195. package/src/ui/pages/Thread.tsx +0 -262
  196. package/src/ui/pages/settings/Classifier.tsx +0 -227
  197. package/src/ui/pages/settings/Data.tsx +0 -190
  198. package/src/ui/pages/settings/Index.tsx +0 -65
  199. package/src/ui/pages/settings/Labels.tsx +0 -224
  200. package/src/ui/pages/settings/Providers.tsx +0 -305
  201. package/src/ui/pages/settings/SettingsSubnav.tsx +0 -28
  202. package/src/ui/pages/settings/Sources.tsx +0 -326
  203. package/src/ui/pages/settings/Views.tsx +0 -96
  204. package/src/ui/styles.css +0 -1766
  205. package/src/ui/tsconfig.json +0 -21
  206. package/src/ui/vite.config.ts +0 -19
  207. package/tests/fixtures/claude_code/short_session.jsonl +0 -2
  208. package/tests/fixtures/claude_code/standard_iso.jsonl +0 -4
  209. package/tests/fixtures/claude_code/tool_heavy.jsonl +0 -8
  210. package/tests/fixtures/claude_code/with_subagent.jsonl +0 -7
  211. package/tests/fixtures/facts.ts +0 -17
  212. package/tests/fixtures/golden-corpus.ts +0 -85
  213. package/tests/fixtures/hermes/paired_request_dump.json +0 -24
  214. package/tests/fixtures/hermes/paired_session.json +0 -23
  215. package/tests/fixtures/hermes/request_dump.json +0 -28
  216. package/tests/fixtures/hermes/session_iso.json +0 -38
  217. package/tests/fixtures/hermes/session_unix.json +0 -38
  218. package/tests/fixtures/hermes/system_only.json +0 -18
  219. package/tests/fixtures/pi/error-connection-abort.jsonl +0 -8
  220. package/tests/fixtures/pi/short-successful.jsonl +0 -5
  221. package/tests/fixtures/pi/with-custom-message.jsonl +0 -6
  222. package/tests/fixtures/sessions.ts +0 -22
  223. package/tests/integration/backfill-facts.test.ts +0 -362
  224. package/tests/integration/citation-explicit.test.ts +0 -111
  225. package/tests/integration/cite-event.test.ts +0 -169
  226. package/tests/integration/cite-memo.test.ts +0 -87
  227. package/tests/integration/db-restore.test.ts +0 -153
  228. package/tests/integration/embed-backfill.test.ts +0 -176
  229. package/tests/integration/fact-supersedence.test.ts +0 -313
  230. package/tests/integration/fts-index.test.ts +0 -60
  231. package/tests/integration/getbyids-sqlite.test.ts +0 -60
  232. package/tests/integration/hermes-agent-hooks.test.ts +0 -248
  233. package/tests/integration/hook-claude-settings.test.ts +0 -205
  234. package/tests/integration/hook-log.test.ts +0 -54
  235. package/tests/integration/hook-memo.test.ts +0 -68
  236. package/tests/integration/hook-pre-compact.test.ts +0 -105
  237. package/tests/integration/hook-subagent-start.test.ts +0 -102
  238. package/tests/integration/http.test.ts +0 -401
  239. package/tests/integration/keyword-search-fts.test.ts +0 -66
  240. package/tests/integration/mcp-recall-logging.test.ts +0 -88
  241. package/tests/integration/mcp.test.ts +0 -248
  242. package/tests/integration/memo-sweep.test.ts +0 -91
  243. package/tests/integration/prompt-recall-hook.test.ts +0 -88
  244. package/tests/integration/provider-registry.test.ts +0 -107
  245. package/tests/integration/recall-golden.test.ts +0 -59
  246. package/tests/integration/recall-sqlite.test.ts +0 -169
  247. package/tests/integration/scheduler.test.ts +0 -391
  248. package/tests/integration/session-end-hook.test.ts +0 -48
  249. package/tests/integration/session-start-hook.test.ts +0 -126
  250. package/tests/integration/source-registry.test.ts +0 -120
  251. package/tests/integration/sqlite-fact-store.test.ts +0 -346
  252. package/tests/integration/stop-hook.test.ts +0 -560
  253. package/tests/integration/wal-checkpoint.test.ts +0 -49
  254. package/tests/unit/cli/launchctl-helpers.test.ts +0 -60
  255. package/tests/unit/core/adapters/aider.test.ts +0 -230
  256. package/tests/unit/core/adapters/claude-code.test.ts +0 -118
  257. package/tests/unit/core/adapters/hermes-agent.test.ts +0 -329
  258. package/tests/unit/core/adapters/hermes.test.ts +0 -81
  259. package/tests/unit/core/adapters/jsonl-generic.test.ts +0 -142
  260. package/tests/unit/core/adapters/opencode.test.ts +0 -354
  261. package/tests/unit/core/adapters/pi.test.ts +0 -110
  262. package/tests/unit/core/classifier/prompt.test.ts +0 -126
  263. package/tests/unit/core/embedding/chunk-body.test.ts +0 -100
  264. package/tests/unit/core/facts/extract-facts.test.ts +0 -117
  265. package/tests/unit/core/filter.test.ts +0 -40
  266. package/tests/unit/core/hook/citation-detect-cite-session.test.ts +0 -96
  267. package/tests/unit/core/hook/citation-detect.test.ts +0 -124
  268. package/tests/unit/core/hook/gate.test.ts +0 -29
  269. package/tests/unit/core/hook/pointer-block.test.ts +0 -22
  270. package/tests/unit/core/hook/select.test.ts +0 -66
  271. package/tests/unit/core/match-fields.test.ts +0 -39
  272. package/tests/unit/core/mcp-cite-session.test.ts +0 -51
  273. package/tests/unit/core/providers/provider-models.test.ts +0 -101
  274. package/tests/unit/core/query-shape.test.ts +0 -92
  275. package/tests/unit/core/recall-facts/fact-recall-service.test.ts +0 -258
  276. package/tests/unit/core/recall-service.test.ts +0 -200
  277. package/tests/unit/core/storage/live-status.test.ts +0 -54
  278. package/tests/unit/core/tokenize.test.ts +0 -32
  279. package/tests/unit/core/useful-scan.test.ts +0 -537
  280. package/tests/unit/llm/embed.test.ts +0 -93
  281. package/tests/unit/llm/ollama-client.test.ts +0 -124
  282. package/tests/unit/scripts/longmemeval-scorer.test.ts +0 -114
  283. package/tsconfig.json +0 -31
  284. package/tsconfig.test.json +0 -11
  285. package/vitest.config.ts +0 -22
package/src/http/app.ts DELETED
@@ -1,1137 +0,0 @@
1
- /**
2
- * Hono app factory. Routes mirror the Python daemon's API surface (GET
3
- * /api/recall, GET /api/recall/stats, GET /api/session/:id, GET /api/health)
4
- * so existing UI clients and the agent-recall observability panel can switch
5
- * to this server without contract changes.
6
- *
7
- * Layering: this module knows about RecallService and SessionStore (the
8
- * inner ring), but core/ knows nothing about Hono. Adapter direction stays
9
- * one-way.
10
- *
11
- * POST /mcp — Streamable-HTTP MCP endpoint for container agents (e.g. Hermes
12
- * WebUI). Requires Authorization: Bearer <NLM_MCP_TOKEN>. Stateless: each
13
- * request gets its own transport + server instance so there is no in-memory
14
- * session state to manage. The existing stdio MCP path is untouched.
15
- */
16
-
17
- import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
18
- import { timingSafeEqual } from "node:crypto";
19
- import { homedir } from "node:os";
20
- import { dirname, extname, join, normalize, sep } from "node:path";
21
- import { Hono } from "hono";
22
- import { WebStandardStreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/webStandardStreamableHttp.js";
23
- import { createMcpServer } from "../mcp/server.js";
24
- import {
25
- snapshotScratchPath,
26
- stageRestore,
27
- vacuumSnapshot,
28
- } from "@core/storage/db-restore.js";
29
- import type { RecallService } from "@core/recall/recall-service.js";
30
- import { logQuery, recallStats } from "@core/recall/query-log.js";
31
- import { recentQueryLog } from "@core/recall/recent-log.js";
32
- import { appendCitation, citationStats } from "@core/recall/citation-log.js";
33
- import { clearSurfaced, loadSurfaced, recordSurfaced } from "@core/hook/memo.js";
34
- import { clearCited } from "@core/hook/cite-memo.js";
35
- import { classifyPrompt } from "@core/hook/gate.js";
36
- import { selectHits, type RecallHitInput } from "@core/hook/select.js";
37
- import { formatPointerBlock } from "@core/hook/pointer-block.js";
38
- import type { FactRecallService } from "@core/recall-facts/fact-recall-service.js";
39
- import { factRecallStats, logFactQuery } from "@core/recall-facts/fact-query-log.js";
40
- import type { FactStore } from "@ports/fact-store.js";
41
- import { buildDataset } from "@core/dataset/build-dataset.js";
42
- import { ClassifierBox, type ClassifierProvider } from "../llm/classifier-box.js";
43
- import {
44
- SourceRegistry,
45
- type SourceInsert,
46
- type SourceKind,
47
- type SourceUpdate,
48
- } from "@core/sources/source-registry.js";
49
- import {
50
- ProviderRegistry,
51
- type ProviderInsert,
52
- type ProviderKind,
53
- type ProviderUpdate,
54
- } from "@core/providers/provider-registry.js";
55
- import { listModels } from "@core/providers/provider-models.js";
56
- import { ingestSession, deriveSessionId, type IngestDeps } from "@core/ingest/ingest-session.js";
57
- import {
58
- listActions,
59
- undoAction,
60
- writeAction,
61
- writeActionsBatch,
62
- } from "@core/actions/actions-log.js";
63
- import type { SessionStore } from "@ports/session-store.js";
64
- import type { SqliteSessionStore } from "@core/storage/sqlite-session-store.js";
65
- import type { McpDeps } from "../mcp/server.js";
66
- import type {
67
- FactKind,
68
- FactRecallQuery,
69
- RecallKindFilter,
70
- RecallMode,
71
- RecallQuery,
72
- } from "@shared/types.js";
73
-
74
- export interface HttpDeps {
75
- readonly recall: RecallService;
76
- readonly store: SessionStore;
77
- /** Pass the concrete store when /live endpoints (recent-writes / recent-markers) should be served. */
78
- readonly liveStore?: SqliteSessionStore;
79
- /** Optional override for the query log path. Defaults to ~/.nlm/query_log.jsonl or $NLM_QUERY_LOG. */
80
- readonly queryLogPath?: string;
81
- /** Optional override for the citation log path. Defaults to ~/.nlm/citation-log.jsonl or $NLM_CITATION_LOG. */
82
- readonly citationLogPath?: string;
83
- /** Fact recall — wire to enable /api/recall/facts + /api/facts/history. */
84
- readonly factRecall?: FactRecallService;
85
- readonly factStore?: FactStore;
86
- /** Optional override for the fact query log path. Defaults to ~/.nlm/fact_query_log.jsonl. */
87
- readonly factQueryLogPath?: string;
88
- /** Path to canonical.sqlite for the /api/dataset endpoint. */
89
- readonly dbPath?: string;
90
- /** Mutable classifier — read by /api/classifier/info, swapped by POST /api/classifier. */
91
- readonly classifier?: ClassifierBox;
92
- /** Sources registry — exposes /api/sources CRUD for the desktop UI. */
93
- readonly sources?: SourceRegistry;
94
- /** Providers registry — exposes /api/providers CRUD for the desktop UI. */
95
- readonly providers?: ProviderRegistry;
96
- /** Wire to enable POST /api/ingest. When omitted, push ingest is disabled. */
97
- readonly ingest?: IngestDeps;
98
- /** Static embedder info — embeddings are always Ollama in this build (DeepSeek has no /embed). */
99
- readonly embedderInfo?: { provider: string; model: string; dims: number };
100
- /** Directory containing the built UI (dist/ui). When set, /ui/* serves the SPA. */
101
- readonly uiDist?: string;
102
- /**
103
- * When provided, POST /mcp is mounted and token-gated with NLM_MCP_TOKEN.
104
- * Omitting this keeps the route absent — no auth surface, no risk.
105
- */
106
- readonly mcpDeps?: McpDeps;
107
- }
108
-
109
- const MIME_TYPES: Record<string, string> = {
110
- ".html": "text/html; charset=utf-8",
111
- ".js": "application/javascript; charset=utf-8",
112
- ".mjs": "application/javascript; charset=utf-8",
113
- ".css": "text/css; charset=utf-8",
114
- ".svg": "image/svg+xml",
115
- ".png": "image/png",
116
- ".ico": "image/x-icon",
117
- ".json": "application/json; charset=utf-8",
118
- ".map": "application/json; charset=utf-8",
119
- };
120
-
121
- /** Tables surfaced on the Settings → Data page, in display order. */
122
- const DATA_STAT_TABLES = [
123
- "sessions",
124
- "entities",
125
- "markers",
126
- "facts",
127
- "session_embedding_chunks",
128
- "fact_embeddings",
129
- "actions",
130
- "session_edges",
131
- "sources",
132
- "providers",
133
- ] as const;
134
-
135
- function parseLimit(raw: string | undefined, fallback: number, max: number): number {
136
- if (raw === undefined) return fallback;
137
- const n = Number.parseInt(raw, 10);
138
- if (!Number.isFinite(n) || n < 1) return fallback;
139
- return Math.min(max, n);
140
- }
141
-
142
- const VALID_MODES: ReadonlyArray<RecallMode> = ["keyword", "semantic", "hybrid"];
143
- const VALID_KINDS: ReadonlyArray<RecallKindFilter> = ["decision", "open"];
144
- const VALID_FACT_KINDS: ReadonlyArray<FactKind> = ["decision", "open", "attribute"];
145
-
146
- export function createApp(deps: HttpDeps): Hono {
147
- const app = new Hono();
148
-
149
- app.get("/api/health", (c) =>
150
- c.json({ status: "ok", service: "nlm-memory", version: "0.2.0-dev" }),
151
- );
152
-
153
- // ── MCP over HTTP (for container agents — e.g. Hermes WebUI) ─────────
154
- // Stateless: one transport + McpServer instance per request, no in-memory
155
- // session state. Bearer token from NLM_MCP_TOKEN is mandatory.
156
- // The existing stdio MCP path (nlm mcp / .mcp.json) is untouched.
157
- if (deps.mcpDeps) {
158
- const mcpToken = process.env["NLM_MCP_TOKEN"];
159
- if (!mcpToken) {
160
- throw new Error(
161
- "NLM_MCP_TOKEN must be set when mcpDeps is provided — " +
162
- "refusing to mount an unauthenticated /mcp endpoint",
163
- );
164
- }
165
- const capturedMcpDeps = deps.mcpDeps;
166
- app.all("/mcp", async (c) => {
167
- const auth = c.req.header("authorization") ?? "";
168
- const match = /^Bearer\s+(\S+)$/i.exec(auth);
169
- const given = Buffer.from(match?.[1] ?? "", "utf8");
170
- const want = Buffer.from(mcpToken, "utf8");
171
- if (!match || given.length !== want.length || !timingSafeEqual(given, want)) {
172
- return c.json({ error: "unauthorized" }, 401);
173
- }
174
- // No sessionIdGenerator = stateless mode: no session ID in responses,
175
- // no session validation. Correct for per-request agent calls.
176
- const transport = new WebStandardStreamableHTTPServerTransport({});
177
- const server = createMcpServer(capturedMcpDeps);
178
- await server.connect(transport);
179
- return transport.handleRequest(c.req.raw);
180
- });
181
- }
182
-
183
- app.get("/api/recall", async (c) => {
184
- const q = c.req.query("q") ?? "";
185
- const entity = c.req.query("entity");
186
- const kind = c.req.query("kind");
187
- const mode = (c.req.query("mode") ?? "keyword") as string;
188
- const limitStr = c.req.query("limit");
189
-
190
- if (kind !== undefined && !VALID_KINDS.includes(kind as RecallKindFilter)) {
191
- return c.json({ error: "kind must be 'decision', 'open', or omitted" }, 400);
192
- }
193
- if (!VALID_MODES.includes(mode as RecallMode)) {
194
- return c.json({ error: "mode must be 'keyword', 'semantic', or 'hybrid'" }, 400);
195
- }
196
- const limit = limitStr === undefined ? 20 : Number.parseInt(limitStr, 10);
197
- if (!Number.isFinite(limit) || limit < 1 || limit > 100) {
198
- return c.json({ error: "limit must be 1..100" }, 400);
199
- }
200
-
201
- const query: RecallQuery = {
202
- query: q,
203
- mode: mode as RecallMode,
204
- limit,
205
- ...(entity !== undefined ? { entity } : {}),
206
- ...(kind !== undefined ? { kind: kind as RecallKindFilter } : {}),
207
- };
208
- const result = await deps.recall.search(query);
209
-
210
- // Fire-and-forget telemetry — never blocks the response.
211
- const source = c.req.header("x-recall-source") ?? "http";
212
- void logQuery(
213
- {
214
- source,
215
- query: q || null,
216
- entity: entity ?? null,
217
- kind: (kind as RecallKindFilter | undefined) ?? null,
218
- mode: mode as RecallMode,
219
- limit,
220
- nResults: result.total,
221
- returnedIds: result.results.map((r) => r.id),
222
- },
223
- ...(deps.queryLogPath !== undefined ? [deps.queryLogPath] : []),
224
- );
225
-
226
- return c.json(result);
227
- });
228
-
229
- app.get("/api/recall/stats", async (c) => {
230
- const daysStr = c.req.query("days") ?? "7";
231
- const days = Number.parseInt(daysStr, 10);
232
- if (!Number.isFinite(days) || days < 1 || days > 365) {
233
- return c.json({ error: "days must be 1..365" }, 400);
234
- }
235
- const stats = await recallStats(
236
- days,
237
- ...(deps.queryLogPath !== undefined ? [deps.queryLogPath] : []),
238
- );
239
- return c.json(stats);
240
- });
241
-
242
- app.get("/api/recall/recent", (c) => {
243
- const limit = parseLimit(c.req.query("limit"), 50, 200);
244
- const entries = recentQueryLog(
245
- limit,
246
- ...(deps.queryLogPath !== undefined ? [deps.queryLogPath] : []),
247
- );
248
- return c.json({ entries });
249
- });
250
-
251
- // Citation events from the Stop hook. One POST per surfaced ID the
252
- // assistant cited in its response. Drives useful_hit_rate and is the
253
- // training-data substrate for the future learned reranker.
254
- app.post("/api/recall/cite-event", async (c) => {
255
- let body: Record<string, unknown>;
256
- try {
257
- body = (await c.req.json()) as Record<string, unknown>;
258
- } catch {
259
- return c.json({ error: "body must be JSON" }, 400);
260
- }
261
- const conversationId = body["conversation_id"];
262
- const citedId = body["cited_id"];
263
- if (typeof conversationId !== "string" || !conversationId) {
264
- return c.json({ error: "conversation_id required" }, 400);
265
- }
266
- if (typeof citedId !== "string" || !citedId) {
267
- return c.json({ error: "cited_id required" }, 400);
268
- }
269
- const responsePreview = body["response_preview"];
270
- const kind = body["kind"];
271
- await appendCitation(
272
- {
273
- conversationId,
274
- citedId,
275
- ...(kind === "tool_use" || kind === "prose" ? { kind } : {}),
276
- ...(typeof responsePreview === "string"
277
- ? { responsePreview }
278
- : {}),
279
- },
280
- ...(deps.citationLogPath !== undefined ? [deps.citationLogPath] : []),
281
- );
282
- return c.json({ ok: true });
283
- });
284
-
285
- app.get("/api/recall/cite-stats", async (c) => {
286
- const daysStr = c.req.query("days") ?? "7";
287
- const days = Number.parseInt(daysStr, 10);
288
- if (!Number.isFinite(days) || days < 1 || days > 365) {
289
- return c.json({ error: "days must be 1..365" }, 400);
290
- }
291
- const stats = await citationStats(
292
- days,
293
- ...(deps.citationLogPath !== undefined ? [deps.citationLogPath] : []),
294
- );
295
- return c.json(stats);
296
- });
297
-
298
- // Explicit citation from the cite_session MCP tool. One POST per session
299
- // the agent explicitly declares it referenced. Source is always "mcp_tool"
300
- // so the training extractor can distinguish deterministic tool citations
301
- // from stop-hook detected prose citations.
302
- app.post("/api/citation/explicit", async (c) => {
303
- let body: Record<string, unknown>;
304
- try {
305
- body = (await c.req.json()) as Record<string, unknown>;
306
- } catch {
307
- return c.json({ error: "body must be JSON" }, 400);
308
- }
309
- const id = body["id"];
310
- if (typeof id !== "string" || !id) {
311
- return c.json({ error: "id required" }, 400);
312
- }
313
- await appendCitation(
314
- {
315
- conversationId: typeof body["conversation_id"] === "string" ? body["conversation_id"] : "mcp_tool",
316
- citedId: id,
317
- kind: "tool_use",
318
- ...(typeof body["reason"] === "string" ? { responsePreview: body["reason"] } : {}),
319
- },
320
- ...(deps.citationLogPath !== undefined ? [deps.citationLogPath] : []),
321
- );
322
- return c.json({ logged: true, id, source: "mcp_tool" });
323
- });
324
-
325
- // ── Hook endpoints (Phase 1d) ─────────────────────────────────────────────
326
-
327
- // PreCompact hook: flush surfaced-ID memo for the compacting conversation
328
- // and stamp a compaction record so post-compaction recalls don't get
329
- // suppressed by stale "already surfaced" gates.
330
- // Payload: { conversation_id, transcript_path?, surfaced_set?, ts? }
331
- app.post("/api/hook/pre-compact", async (c) => {
332
- let body: Record<string, unknown>;
333
- try {
334
- body = (await c.req.json()) as Record<string, unknown>;
335
- } catch {
336
- return c.json({ error: "body must be JSON" }, 400);
337
- }
338
- const conversationId = body["conversation_id"];
339
- if (typeof conversationId !== "string" || !conversationId) {
340
- return c.json({ error: "conversation_id required" }, 400);
341
- }
342
- const flushed = loadSurfaced(conversationId).size;
343
- clearSurfaced(conversationId);
344
- const compactedAt = new Date().toISOString();
345
- const logPath = process.env["NLM_HOOK_LOG"] ?? join(homedir(), ".nlm", "hook-log.jsonl");
346
- try {
347
- mkdirSync(dirname(logPath), { recursive: true });
348
- appendFileSync(
349
- logPath,
350
- `${JSON.stringify({ ts: compactedAt, kind: "pre-compact", conversationId, flushed })}\n`,
351
- "utf8",
352
- );
353
- } catch {
354
- // Log failure must not fail the endpoint.
355
- }
356
- return c.json({ ok: true, flushed, compacted_at: compactedAt });
357
- });
358
-
359
- // SubagentStart hook: logging-only stub. Records the parent→subagent link
360
- // so future corpus-linking logic can correlate subagent sessions back to
361
- // their dispatching conversation.
362
- // Payload: { parent_conversation_id, subagent_session_id, subagent_description?, ts? }
363
- app.post("/api/hook/subagent-start", async (c) => {
364
- let body: Record<string, unknown>;
365
- try {
366
- body = (await c.req.json()) as Record<string, unknown>;
367
- } catch {
368
- return c.json({ error: "body must be JSON" }, 400);
369
- }
370
- const parentConversationId = body["parent_conversation_id"];
371
- const subagentSessionId = body["subagent_session_id"];
372
- if (typeof parentConversationId !== "string" || !parentConversationId) {
373
- return c.json({ error: "parent_conversation_id required" }, 400);
374
- }
375
- if (typeof subagentSessionId !== "string" || !subagentSessionId) {
376
- return c.json({ error: "subagent_session_id required" }, 400);
377
- }
378
- const subagentDescription = typeof body["subagent_description"] === "string" ? body["subagent_description"] : "";
379
- const ts = typeof body["ts"] === "string" ? body["ts"] : new Date().toISOString();
380
- const logPath = process.env["NLM_SUBAGENT_LOG"] ?? join(homedir(), ".nlm", "subagent-log.jsonl");
381
- try {
382
- mkdirSync(dirname(logPath), { recursive: true });
383
- appendFileSync(
384
- logPath,
385
- `${JSON.stringify({ ts, parent_conversation_id: parentConversationId, subagent_session_id: subagentSessionId, subagent_description: subagentDescription })}\n`,
386
- "utf8",
387
- );
388
- } catch {
389
- // Log failure must not fail the endpoint.
390
- }
391
- return c.json({ ok: true, recorded: true });
392
- });
393
-
394
- // ── NousResearch Hermes Agent lifecycle hooks ─────────────────────────────
395
- //
396
- // Python plugin (~/.hermes/plugins/nlm-memory/__init__.py) calls these
397
- // endpoints for the 6 events it registers with ctx.register_hook().
398
- //
399
- // pre_llm_call → POST /api/hook/hermes-agent/pre-turn (recall + inject)
400
- // post_llm_call → POST /api/hook/hermes-agent/post-turn (citation detect)
401
- // on_session_{start,end,finalize,reset} → POST /api/hook/hermes-agent/session-lifecycle
402
-
403
- // pre-turn: run keyword recall against user_message, update the per-session
404
- // memo to avoid re-surfacing the same sessions within one conversation, and
405
- // return the formatted pointer block as {"context": "..."}.
406
- // Returns {"context": null} when there is nothing worth surfacing.
407
- app.post("/api/hook/hermes-agent/pre-turn", async (c) => {
408
- let body: Record<string, unknown>;
409
- try {
410
- body = (await c.req.json()) as Record<string, unknown>;
411
- } catch {
412
- return c.json({ error: "body must be JSON" }, 400);
413
- }
414
- const sessionId = body["session_id"];
415
- const userMessage = body["user_message"];
416
- if (typeof sessionId !== "string" || !sessionId) {
417
- return c.json({ error: "session_id required" }, 400);
418
- }
419
- if (typeof userMessage !== "string" || !userMessage.trim()) {
420
- return c.json({ context: null });
421
- }
422
- if (classifyPrompt(userMessage) === "generative") {
423
- return c.json({ context: null });
424
- }
425
- try {
426
- const result = await deps.recall.search({ query: userMessage, mode: "keyword", limit: 5 });
427
- const hits: ReadonlyArray<RecallHitInput> = result.results.map((r) => ({
428
- id: r.id,
429
- label: r.label,
430
- startedAt: r.startedAt,
431
- matchScore: r.matchScore,
432
- }));
433
- const surfaced = loadSurfaced(sessionId);
434
- const selected = selectHits({ hits, surfaced, scoreThreshold: 0, perFireCap: 3, perConversationCap: 10 });
435
- if (selected.length === 0) return c.json({ context: null });
436
- recordSurfaced(sessionId, selected.map((h) => h.id));
437
- return c.json({ context: formatPointerBlock(selected) });
438
- } catch {
439
- return c.json({ context: null });
440
- }
441
- });
442
-
443
- // post-turn: scan assistant_response for session IDs that were surfaced in
444
- // this conversation and log prose citation events.
445
- app.post("/api/hook/hermes-agent/post-turn", async (c) => {
446
- let body: Record<string, unknown>;
447
- try {
448
- body = (await c.req.json()) as Record<string, unknown>;
449
- } catch {
450
- return c.json({ error: "body must be JSON" }, 400);
451
- }
452
- const sessionId = body["session_id"];
453
- const assistantResponse = body["assistant_response"];
454
- if (typeof sessionId !== "string" || !sessionId) {
455
- return c.json({ error: "session_id required" }, 400);
456
- }
457
- if (typeof assistantResponse !== "string" || !assistantResponse) {
458
- return c.json({ ok: true, cited: 0 });
459
- }
460
- const surfacedIds = [...loadSurfaced(sessionId)];
461
- const cited: string[] = [];
462
- for (const id of surfacedIds) {
463
- if (assistantResponse.includes(id)) cited.push(id);
464
- }
465
- const preview = assistantResponse.slice(0, 200);
466
- for (const citedId of cited) {
467
- await appendCitation(
468
- { conversationId: sessionId, citedId, kind: "prose", responsePreview: preview },
469
- ...(deps.citationLogPath !== undefined ? [deps.citationLogPath] : []),
470
- );
471
- }
472
- return c.json({ ok: true, cited: cited.length });
473
- });
474
-
475
- // session-lifecycle: memo housekeeping for on_session_{start,end,finalize,reset}.
476
- // start is a no-op (memo is created lazily). end/finalize/reset clear the memo.
477
- app.post("/api/hook/hermes-agent/session-lifecycle", async (c) => {
478
- let body: Record<string, unknown>;
479
- try {
480
- body = (await c.req.json()) as Record<string, unknown>;
481
- } catch {
482
- return c.json({ error: "body must be JSON" }, 400);
483
- }
484
- const event = body["event"];
485
- if (typeof event !== "string" || !["start", "end", "finalize", "reset"].includes(event)) {
486
- return c.json({ error: "event must be one of: start, end, finalize, reset" }, 400);
487
- }
488
- if (event !== "start") {
489
- const sessionId = body["session_id"];
490
- if (typeof sessionId === "string" && sessionId) {
491
- clearSurfaced(sessionId);
492
- clearCited(sessionId);
493
- }
494
- }
495
- return c.json({ ok: true, event });
496
- });
497
-
498
- // ── Fact recall (Phase B.3 surface, exposed over HTTP for the MCP proxy) ──
499
-
500
- app.get("/api/recall/facts", async (c) => {
501
- if (!deps.factRecall) {
502
- return c.json({ error: "fact recall not wired in this deployment" }, 503);
503
- }
504
- const q = c.req.query("q") ?? "";
505
- const subject = c.req.query("subject");
506
- const predicate = c.req.query("predicate");
507
- const kind = c.req.query("kind");
508
- const mode = (c.req.query("mode") ?? "keyword") as string;
509
- const includeSuperseded = c.req.query("includeSuperseded") === "true";
510
- const minConfidenceStr = c.req.query("minConfidence");
511
- const limitStr = c.req.query("limit");
512
-
513
- if (kind !== undefined && !VALID_FACT_KINDS.includes(kind as FactKind)) {
514
- return c.json({ error: "kind must be 'decision', 'open', 'attribute', or omitted" }, 400);
515
- }
516
- if (!VALID_MODES.includes(mode as RecallMode)) {
517
- return c.json({ error: "mode must be 'keyword', 'semantic', or 'hybrid'" }, 400);
518
- }
519
- const limit = limitStr === undefined ? 10 : Number.parseInt(limitStr, 10);
520
- if (!Number.isFinite(limit) || limit < 1 || limit > 100) {
521
- return c.json({ error: "limit must be 1..100" }, 400);
522
- }
523
- let minConfidence: number | undefined;
524
- if (minConfidenceStr !== undefined) {
525
- minConfidence = Number.parseFloat(minConfidenceStr);
526
- if (!Number.isFinite(minConfidence) || minConfidence < 0 || minConfidence > 1) {
527
- return c.json({ error: "minConfidence must be 0..1" }, 400);
528
- }
529
- }
530
-
531
- const query: FactRecallQuery = {
532
- query: q,
533
- mode: mode as RecallMode,
534
- limit,
535
- includeSuperseded,
536
- ...(subject !== undefined ? { subject } : {}),
537
- ...(predicate !== undefined ? { predicate } : {}),
538
- ...(kind !== undefined ? { kind: kind as FactKind } : {}),
539
- ...(minConfidence !== undefined ? { minConfidence } : {}),
540
- };
541
- const result = await deps.factRecall.search(query);
542
-
543
- const source = c.req.header("x-recall-source") ?? "http";
544
- void logFactQuery(
545
- {
546
- source,
547
- query: q || null,
548
- subject: subject ?? null,
549
- predicate: predicate ?? null,
550
- kind: (kind as FactKind | undefined) ?? null,
551
- mode: mode as RecallMode,
552
- limit,
553
- nResults: result.total,
554
- returnedIds: result.results.map((r) => r.id),
555
- },
556
- ...(deps.factQueryLogPath !== undefined ? [deps.factQueryLogPath] : []),
557
- );
558
-
559
- return c.json(result);
560
- });
561
-
562
- app.get("/api/facts/history", async (c) => {
563
- if (!deps.factStore) {
564
- return c.json({ error: "fact store not wired in this deployment" }, 503);
565
- }
566
- const subject = c.req.query("subject");
567
- if (!subject) {
568
- return c.json({ error: "subject is required" }, 400);
569
- }
570
- const predicate = c.req.query("predicate");
571
- const chains = await deps.factStore.getHistory(subject, predicate);
572
- return c.json({ subject, predicate: predicate ?? null, chains });
573
- });
574
-
575
- app.get("/api/recall/facts/stats", async (c) => {
576
- const daysStr = c.req.query("days") ?? "7";
577
- const days = Number.parseInt(daysStr, 10);
578
- if (!Number.isFinite(days) || days < 1 || days > 365) {
579
- return c.json({ error: "days must be 1..365" }, 400);
580
- }
581
- const stats = await factRecallStats(
582
- days,
583
- ...(deps.factQueryLogPath !== undefined ? [deps.factQueryLogPath] : []),
584
- );
585
- return c.json(stats);
586
- });
587
-
588
- app.get("/api/live/recent-writes", (c) => {
589
- if (!deps.liveStore) return c.json({ writes: [] });
590
- const limit = parseLimit(c.req.query("limit"), 50, 200);
591
- return c.json({ writes: deps.liveStore.recentWrites(limit) });
592
- });
593
-
594
- app.get("/api/live/recent-markers", (c) => {
595
- if (!deps.liveStore) return c.json({ markers: [] });
596
- const limit = parseLimit(c.req.query("limit"), 50, 200);
597
- return c.json({ markers: deps.liveStore.recentMarkers(limit) });
598
- });
599
-
600
- app.get("/api/dataset", (c) => {
601
- if (!deps.dbPath) return c.json({ error: "dataset endpoint requires dbPath" }, 503);
602
- const includePaths = c.req.query("include_paths") === "true";
603
- return c.json(buildDataset(deps.dbPath, { includePaths }));
604
- });
605
-
606
- // ── Data management ─────────────────────────────────────────────
607
- // Storage stats, live-safe backup snapshot, and staged restore.
608
-
609
- app.get("/api/data/stats", (c) => {
610
- if (!deps.liveStore || !deps.dbPath) {
611
- return c.json({ error: "data stats require liveStore + dbPath" }, 503);
612
- }
613
- const db = deps.liveStore.rawDb();
614
- const countOf = (table: string): number => {
615
- try {
616
- const row = db.prepare<[], { n: number }>(`SELECT COUNT(*) AS n FROM ${table}`).get();
617
- return row?.n ?? 0;
618
- } catch {
619
- return 0;
620
- }
621
- };
622
- const tables = DATA_STAT_TABLES.map((name) => ({ name, rows: countOf(name) }));
623
-
624
- const migrations = db
625
- .prepare<[], { version: number; name: string; applied_at: string }>(
626
- "SELECT version, name, applied_at FROM schema_migrations ORDER BY version",
627
- )
628
- .all();
629
-
630
- const runtimes = db
631
- .prepare<[], { runtime: string; n: number }>(
632
- "SELECT runtime, COUNT(*) AS n FROM sessions GROUP BY runtime ORDER BY n DESC",
633
- )
634
- .all();
635
-
636
- let dbBytes = 0;
637
- let dbPresent = false;
638
- try {
639
- dbBytes = statSync(deps.dbPath).size;
640
- dbPresent = true;
641
- } catch { /* file absent */ }
642
- for (const sidecar of [`${deps.dbPath}-wal`, `${deps.dbPath}-shm`]) {
643
- try { dbBytes += statSync(sidecar).size; } catch { /* no sidecar */ }
644
- }
645
-
646
- return c.json({
647
- dbPath: deps.dbPath,
648
- dbBytes,
649
- dbPresent,
650
- schemaVersion: migrations.length > 0 ? migrations[migrations.length - 1]!.version : null,
651
- migrations,
652
- tables,
653
- runtimes,
654
- });
655
- });
656
-
657
- app.get("/api/data/backup", (c) => {
658
- const adminToken = process.env["NLM_MCP_TOKEN"];
659
- if (adminToken) {
660
- const auth = c.req.header("authorization") ?? "";
661
- const m = /^Bearer\s+(\S+)$/i.exec(auth);
662
- const given = Buffer.from(m?.[1] ?? "", "utf8");
663
- const want = Buffer.from(adminToken, "utf8");
664
- if (!m || given.length !== want.length || !timingSafeEqual(given, want)) {
665
- return c.json({ error: "unauthorized" }, 401);
666
- }
667
- }
668
- if (!deps.liveStore || !deps.dbPath) {
669
- return c.json({ error: "backup requires liveStore + dbPath" }, 503);
670
- }
671
- const scratch = snapshotScratchPath(deps.dbPath);
672
- try {
673
- vacuumSnapshot(deps.liveStore.rawDb(), scratch);
674
- const bytes = readFileSync(scratch);
675
- const stamp = new Date().toISOString().slice(0, 10);
676
- c.header("Content-Type", "application/x-sqlite3");
677
- c.header("Content-Disposition", `attachment; filename="nlm-memory-backup-${stamp}.sqlite"`);
678
- return c.body(bytes);
679
- } catch (e) {
680
- return c.json({ error: e instanceof Error ? e.message : String(e) }, 500);
681
- } finally {
682
- rmSync(scratch, { force: true });
683
- }
684
- });
685
-
686
- app.post("/api/data/restore", async (c) => {
687
- const adminToken = process.env["NLM_MCP_TOKEN"];
688
- if (adminToken) {
689
- const auth = c.req.header("authorization") ?? "";
690
- const m = /^Bearer\s+(\S+)$/i.exec(auth);
691
- const given = Buffer.from(m?.[1] ?? "", "utf8");
692
- const want = Buffer.from(adminToken, "utf8");
693
- if (!m || given.length !== want.length || !timingSafeEqual(given, want)) {
694
- return c.json({ error: "unauthorized" }, 401);
695
- }
696
- }
697
- if (!deps.dbPath) return c.json({ error: "restore requires dbPath" }, 503);
698
- const form = await c.req.parseBody().catch(() => null);
699
- const file = form?.["file"];
700
- if (!(file instanceof File)) {
701
- return c.json({ error: "multipart body must include a `file` field" }, 400);
702
- }
703
- const scratch = snapshotScratchPath(deps.dbPath);
704
- try {
705
- writeFileSync(scratch, Buffer.from(await file.arrayBuffer()));
706
- const result = stageRestore(deps.dbPath, scratch);
707
- if (!result.ok) {
708
- return c.json({ error: `rejected: ${result.error}` }, 400);
709
- }
710
- return c.json({
711
- staged: true,
712
- restartRequired: true,
713
- sessions: result.sessions,
714
- schemaVersion: result.schemaVersion,
715
- });
716
- } catch (e) {
717
- rmSync(scratch, { force: true });
718
- return c.json({ error: e instanceof Error ? e.message : String(e) }, 500);
719
- }
720
- });
721
-
722
- // ── Actions API ────────────────────────────────────────────────
723
- // Append-only event log: dismiss/snooze/retire/label/merge all land here.
724
- // Mutations are projected into the dataset at read time, never applied to
725
- // the underlying sessions/entities/markers tables.
726
-
727
- app.post("/api/action", async (c) => {
728
- if (!deps.liveStore) return c.json({ error: "actions require liveStore" }, 503);
729
- const body = await c.req.json().catch(() => null);
730
- const parsed = parseActionInput(body);
731
- if (!parsed) return c.json({ error: "invalid action payload" }, 400);
732
- const id = writeAction(deps.liveStore.rawDb(), parsed);
733
- return c.json({ id, timestamp: new Date().toISOString() });
734
- });
735
-
736
- app.post("/api/action/batch", async (c) => {
737
- if (!deps.liveStore) return c.json({ error: "actions require liveStore" }, 503);
738
- const body = (await c.req.json().catch(() => null)) as { actions?: unknown[] } | null;
739
- if (!body || !Array.isArray(body.actions)) return c.json({ error: "missing actions array" }, 400);
740
- const inputs = body.actions
741
- .map(parseActionInput)
742
- .filter((x): x is NonNullable<ReturnType<typeof parseActionInput>> => x !== null);
743
- if (inputs.length === 0) return c.json({ accepted: 0, ids: [] });
744
- const ids = writeActionsBatch(deps.liveStore.rawDb(), inputs);
745
- return c.json({ accepted: ids.length, ids });
746
- });
747
-
748
- app.post("/api/action/:id/undo", (c) => {
749
- if (!deps.liveStore) return c.json({ error: "actions require liveStore" }, 503);
750
- const result = undoAction(deps.liveStore.rawDb(), c.req.param("id"));
751
- if (!result) return c.json({ error: "action not found or already undone" }, 404);
752
- return c.json({ id: result.undoId, timestamp: new Date().toISOString() });
753
- });
754
-
755
- app.get("/api/actions", (c) => {
756
- if (!deps.liveStore) return c.json({ actions: [] });
757
- const limitRaw = c.req.query("limit");
758
- const subjectId = c.req.query("subject_id");
759
- const kind = c.req.query("kind");
760
- const limit = limitRaw ? Math.max(1, Math.min(500, Number.parseInt(limitRaw, 10))) : 100;
761
- const rows = listActions(deps.liveStore.rawDb(), {
762
- limit,
763
- ...(subjectId ? { subjectId } : {}),
764
- ...(kind ? { kind } : {}),
765
- });
766
- return c.json({ actions: rows });
767
- });
768
-
769
- app.get("/api/classifier/info", (c) => {
770
- const provider = deps.classifier?.provider ?? "deepseek";
771
- const model = deps.classifier?.model ?? "deepseek-v4-flash";
772
- return c.json({
773
- provider,
774
- model,
775
- available_providers: ["deepseek", "ollama"] as const,
776
- env_present: {
777
- deepseek: Boolean(process.env["DEEPSEEK_API_KEY"]),
778
- ollama: true,
779
- },
780
- default_models: {
781
- deepseek: ["deepseek-v4-flash", "deepseek-v4-pro", "deepseek-chat"],
782
- ollama: ["phi4-mini:latest", "qwen2.5:3b-instruct", "llama3.2:3b", "mistral:7b"],
783
- },
784
- embedder: deps.embedderInfo ?? { provider: "ollama", model: "nomic-embed-text", dims: 768 },
785
- });
786
- });
787
-
788
- app.post("/api/classifier", async (c) => {
789
- if (!deps.classifier) return c.json({ error: "classifier swap requires classifier box" }, 503);
790
- const body = (await c.req.json().catch(() => null)) as { provider?: string; model?: string } | null;
791
- const provider = body?.provider;
792
- const model = body?.model;
793
- if (provider !== "deepseek" && provider !== "ollama") {
794
- return c.json({ error: "provider must be 'deepseek' or 'ollama'" }, 400);
795
- }
796
- if (!model || typeof model !== "string" || model.length === 0) {
797
- return c.json({ error: "model is required" }, 400);
798
- }
799
- if (provider === "deepseek" && !process.env["DEEPSEEK_API_KEY"]) {
800
- return c.json({ error: "DEEPSEEK_API_KEY not set — cannot swap to deepseek" }, 400);
801
- }
802
- deps.classifier.swap(provider as ClassifierProvider, model);
803
- return c.json({ provider: deps.classifier.provider, model: deps.classifier.model });
804
- });
805
-
806
- // ── Sources registry ────────────────────────────────────────────
807
- // Each row = one transcript origin the daemon scans. UI uses these
808
- // endpoints to surface existing sources + let users add custom ones.
809
-
810
- app.get("/api/sources", (c) => {
811
- if (!deps.sources) return c.json({ sources: [] });
812
- return c.json({ sources: deps.sources.list() });
813
- });
814
-
815
- app.post("/api/sources", async (c) => {
816
- if (!deps.sources) return c.json({ error: "sources registry unavailable" }, 503);
817
- const body = (await c.req.json().catch(() => null)) as Partial<SourceInsert> | null;
818
- const parsed = parseSourceInsert(body);
819
- if (!parsed) return c.json({ error: "invalid source payload" }, 400);
820
- if (deps.sources.getByName(parsed.name)) {
821
- return c.json({ error: `source named '${parsed.name}' already exists` }, 409);
822
- }
823
- return c.json(deps.sources.insert(parsed), 201);
824
- });
825
-
826
- app.patch("/api/sources/:id", async (c) => {
827
- if (!deps.sources) return c.json({ error: "sources registry unavailable" }, 503);
828
- const id = Number.parseInt(c.req.param("id"), 10);
829
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
830
- const body = (await c.req.json().catch(() => null)) as Partial<SourceUpdate> | null;
831
- const patch = parseSourceUpdate(body);
832
- if (!patch) return c.json({ error: "invalid patch payload" }, 400);
833
- const updated = deps.sources.update(id, patch);
834
- if (!updated) return c.json({ error: `source ${id} not found` }, 404);
835
- return c.json(updated);
836
- });
837
-
838
- app.delete("/api/sources/:id", (c) => {
839
- if (!deps.sources) return c.json({ error: "sources registry unavailable" }, 503);
840
- const id = Number.parseInt(c.req.param("id"), 10);
841
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
842
- const ok = deps.sources.delete(id);
843
- if (!ok) return c.json({ error: `source ${id} not found` }, 404);
844
- return c.json({ deleted: id });
845
- });
846
-
847
- app.post("/api/sources/:id/regenerate-token", (c) => {
848
- if (!deps.sources) return c.json({ error: "sources registry unavailable" }, 503);
849
- const id = Number.parseInt(c.req.param("id"), 10);
850
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
851
- const token = deps.sources.regenerateToken(id);
852
- if (!token) return c.json({ error: "regenerate-token only applies to webhook sources" }, 400);
853
- return c.json({ token });
854
- });
855
-
856
- // Ingest (webhook push). Auth: Bearer token tied to a webhook source.
857
- // Classification runs async so callers get a fast 202.
858
- app.post("/api/ingest", async (c) => {
859
- if (!deps.ingest || !deps.sources) {
860
- return c.json({ error: "ingest pipeline not wired" }, 503);
861
- }
862
- const auth = c.req.header("authorization") ?? "";
863
- const match = /^Bearer\s+(\S+)$/i.exec(auth);
864
- if (!match || !match[1]) return c.json({ error: "missing or malformed bearer token" }, 401);
865
- const source = deps.sources.findByToken(match[1]);
866
- if (!source || source.kind !== "webhook") return c.json({ error: "invalid token" }, 401);
867
- if (!source.enabled) return c.json({ error: "source is disabled" }, 403);
868
-
869
- const body = (await c.req.json().catch(() => null)) as Record<string, unknown> | null;
870
- if (!body || typeof body["text"] !== "string" || (body["text"] as string).length === 0) {
871
- return c.json({ error: "body must include `text` string" }, 400);
872
- }
873
- const text = body["text"] as string;
874
- const startedAt = typeof body["startedAt"] === "string" ? (body["startedAt"] as string) : new Date().toISOString();
875
- const suppliedId = typeof body["id"] === "string" ? (body["id"] as string) : null;
876
- const id = suppliedId ?? deriveSessionId(source.runtimeLabel, startedAt, text);
877
-
878
- const input = {
879
- id,
880
- runtime: source.runtimeLabel,
881
- runtimeSessionId: typeof body["runtimeSessionId"] === "string" ? (body["runtimeSessionId"] as string) : null,
882
- text,
883
- startedAt,
884
- endedAt: typeof body["endedAt"] === "string" ? (body["endedAt"] as string) : null,
885
- transcriptPath: typeof body["transcriptPath"] === "string" ? (body["transcriptPath"] as string) : null,
886
- sourceId: source.id,
887
- };
888
-
889
- const ingest = deps.ingest;
890
- void ingestSession(input, ingest).catch((e) => {
891
- const msg = e instanceof Error ? e.message : String(e);
892
- console.error(`[ingest] background failure for ${id}: ${msg}`);
893
- });
894
-
895
- return c.json({ id, status: "accepted", source: source.name }, 202);
896
- });
897
-
898
- // ── Providers registry ──────────────────────────────────────────
899
- // Each row = one LLM endpoint. Keys are redacted on every response
900
- // (rows carry hasApiKey:boolean instead).
901
-
902
- app.get("/api/providers", (c) => {
903
- if (!deps.providers) return c.json({ providers: [] });
904
- return c.json({ providers: deps.providers.list() });
905
- });
906
-
907
- app.post("/api/providers", async (c) => {
908
- if (!deps.providers) return c.json({ error: "providers registry unavailable" }, 503);
909
- const body = (await c.req.json().catch(() => null)) as Partial<ProviderInsert> | null;
910
- const parsed = parseProviderInsert(body);
911
- if (!parsed) return c.json({ error: "invalid provider payload" }, 400);
912
- if (deps.providers.getByName(parsed.name)) {
913
- return c.json({ error: `provider named '${parsed.name}' already exists` }, 409);
914
- }
915
- return c.json(deps.providers.insert(parsed), 201);
916
- });
917
-
918
- app.patch("/api/providers/:id", async (c) => {
919
- if (!deps.providers) return c.json({ error: "providers registry unavailable" }, 503);
920
- const id = Number.parseInt(c.req.param("id"), 10);
921
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
922
- const body = (await c.req.json().catch(() => null)) as Partial<ProviderUpdate> | null;
923
- const patch = parseProviderUpdate(body);
924
- if (!patch) return c.json({ error: "invalid patch payload" }, 400);
925
- const updated = deps.providers.update(id, patch);
926
- if (!updated) return c.json({ error: `provider ${id} not found` }, 404);
927
- return c.json(updated);
928
- });
929
-
930
- app.delete("/api/providers/:id", (c) => {
931
- if (!deps.providers) return c.json({ error: "providers registry unavailable" }, 503);
932
- const id = Number.parseInt(c.req.param("id"), 10);
933
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
934
- const ok = deps.providers.delete(id);
935
- if (!ok) return c.json({ error: `provider ${id} not found` }, 404);
936
- return c.json({ deleted: id });
937
- });
938
-
939
- app.get("/api/providers/:id/models", async (c) => {
940
- if (!deps.providers) return c.json({ error: "providers registry unavailable" }, 503);
941
- const id = Number.parseInt(c.req.param("id"), 10);
942
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
943
- const provider = deps.providers.get(id);
944
- if (!provider) return c.json({ error: `provider ${id} not found` }, 404);
945
- const key = deps.providers.getSecret(id);
946
- try {
947
- const models = await listModels(provider, { apiKey: key });
948
- return c.json({ models });
949
- } catch (e) {
950
- const message = e instanceof Error ? e.message : String(e);
951
- return c.json({ error: message }, 502);
952
- }
953
- });
954
-
955
- app.post("/api/providers/:id/test", async (c) => {
956
- if (!deps.providers) return c.json({ error: "providers registry unavailable" }, 503);
957
- const id = Number.parseInt(c.req.param("id"), 10);
958
- if (!Number.isFinite(id)) return c.json({ error: "invalid id" }, 400);
959
- const provider = deps.providers.get(id);
960
- if (!provider) return c.json({ error: `provider ${id} not found` }, 404);
961
- const key = deps.providers.getSecret(id);
962
- const startedAt = Date.now();
963
- try {
964
- const models = await listModels(provider, { apiKey: key });
965
- return c.json({
966
- ok: true,
967
- modelCount: models.length,
968
- latencyMs: Date.now() - startedAt,
969
- });
970
- } catch (e) {
971
- const message = e instanceof Error ? e.message : String(e);
972
- return c.json({ ok: false, error: message, latencyMs: Date.now() - startedAt }, 200);
973
- }
974
- });
975
-
976
- app.get("/api/session/:id", async (c) => {
977
- const id = c.req.param("id");
978
- const session = await deps.store.getById(id);
979
- if (!session) {
980
- return c.json({ error: `session ${id} not found` }, 404);
981
- }
982
- return c.json(session);
983
- });
984
-
985
- if (deps.uiDist) {
986
- mountSpa(app, deps.uiDist);
987
- }
988
-
989
- return app;
990
- }
991
-
992
- function parseActionInput(raw: unknown): {
993
- kind: string;
994
- subjectType: string;
995
- subjectId: string;
996
- payload?: Record<string, unknown>;
997
- actor?: string;
998
- runtime?: string;
999
- } | null {
1000
- if (!raw || typeof raw !== "object") return null;
1001
- const r = raw as Record<string, unknown>;
1002
- const kind = typeof r["kind"] === "string" ? r["kind"] : null;
1003
- const subjectType = typeof r["subject_type"] === "string" ? r["subject_type"] : null;
1004
- const subjectId = typeof r["subject_id"] === "string" ? r["subject_id"] : null;
1005
- if (!kind || !subjectType || !subjectId) return null;
1006
- return {
1007
- kind,
1008
- subjectType,
1009
- subjectId,
1010
- ...(r["payload"] && typeof r["payload"] === "object" && !Array.isArray(r["payload"])
1011
- ? { payload: r["payload"] as Record<string, unknown> }
1012
- : {}),
1013
- ...(typeof r["actor"] === "string" ? { actor: r["actor"] } : {}),
1014
- ...(typeof r["runtime"] === "string" ? { runtime: r["runtime"] } : {}),
1015
- };
1016
- }
1017
-
1018
- const VALID_SOURCE_KINDS: ReadonlyArray<SourceKind> = [
1019
- "claude-code", "hermes", "pi", "jsonl-generic", "webhook",
1020
- ];
1021
-
1022
- function parseSourceInsert(raw: unknown): SourceInsert | null {
1023
- if (!raw || typeof raw !== "object") return null;
1024
- const r = raw as Record<string, unknown>;
1025
- const kind = r["kind"];
1026
- const name = r["name"];
1027
- const runtimeLabel = r["runtimeLabel"] ?? r["runtime_label"];
1028
- if (typeof kind !== "string" || !VALID_SOURCE_KINDS.includes(kind as SourceKind)) return null;
1029
- if (typeof name !== "string" || name.length === 0) return null;
1030
- if (typeof runtimeLabel !== "string" || runtimeLabel.length === 0) return null;
1031
- const pathOrUrl = r["pathOrUrl"] ?? r["path_or_url"];
1032
- const parseConfig = r["parseConfig"] ?? r["parse_config"];
1033
- const enabled = r["enabled"];
1034
- const out: SourceInsert = { kind: kind as SourceKind, name, runtimeLabel };
1035
- if (typeof pathOrUrl === "string" || pathOrUrl === null) {
1036
- (out as { pathOrUrl?: string | null }).pathOrUrl = pathOrUrl;
1037
- }
1038
- if (parseConfig && typeof parseConfig === "object") {
1039
- (out as { parseConfig?: Record<string, unknown> }).parseConfig = parseConfig as Record<string, unknown>;
1040
- }
1041
- if (typeof enabled === "boolean") {
1042
- (out as { enabled?: boolean }).enabled = enabled;
1043
- }
1044
- return out;
1045
- }
1046
-
1047
- function parseSourceUpdate(raw: unknown): SourceUpdate | null {
1048
- if (!raw || typeof raw !== "object") return null;
1049
- const r = raw as Record<string, unknown>;
1050
- const patch: SourceUpdate = {};
1051
- if (typeof r["name"] === "string") (patch as { name?: string }).name = r["name"];
1052
- if ("pathOrUrl" in r || "path_or_url" in r) {
1053
- const v = r["pathOrUrl"] ?? r["path_or_url"];
1054
- if (typeof v === "string" || v === null) (patch as { pathOrUrl?: string | null }).pathOrUrl = v;
1055
- }
1056
- const rt = r["runtimeLabel"] ?? r["runtime_label"];
1057
- if (typeof rt === "string") (patch as { runtimeLabel?: string }).runtimeLabel = rt;
1058
- const cfg = r["parseConfig"] ?? r["parse_config"];
1059
- if (cfg && typeof cfg === "object") (patch as { parseConfig?: Record<string, unknown> }).parseConfig = cfg as Record<string, unknown>;
1060
- if (typeof r["enabled"] === "boolean") (patch as { enabled?: boolean }).enabled = r["enabled"] as boolean;
1061
- if (Object.keys(patch).length === 0) return null;
1062
- return patch;
1063
- }
1064
-
1065
- const VALID_PROVIDER_KINDS: ReadonlyArray<ProviderKind> = [
1066
- "deepseek", "ollama", "openai", "anthropic", "openrouter", "openai-compatible",
1067
- ];
1068
-
1069
- function parseProviderInsert(raw: unknown): ProviderInsert | null {
1070
- if (!raw || typeof raw !== "object") return null;
1071
- const r = raw as Record<string, unknown>;
1072
- const kind = r["kind"];
1073
- const name = r["name"];
1074
- if (typeof kind !== "string" || !VALID_PROVIDER_KINDS.includes(kind as ProviderKind)) return null;
1075
- if (typeof name !== "string" || name.length === 0) return null;
1076
- const out: ProviderInsert = { kind: kind as ProviderKind, name };
1077
- const baseUrl = r["baseUrl"] ?? r["base_url"];
1078
- if (typeof baseUrl === "string" || baseUrl === null) {
1079
- (out as { baseUrl?: string | null }).baseUrl = baseUrl;
1080
- }
1081
- const apiKey = r["apiKey"] ?? r["api_key"];
1082
- if (typeof apiKey === "string" || apiKey === null) {
1083
- (out as { apiKey?: string | null }).apiKey = apiKey;
1084
- }
1085
- const defaultModel = r["defaultModel"] ?? r["default_model"];
1086
- if (typeof defaultModel === "string" || defaultModel === null) {
1087
- (out as { defaultModel?: string | null }).defaultModel = defaultModel;
1088
- }
1089
- if (typeof r["enabled"] === "boolean") {
1090
- (out as { enabled?: boolean }).enabled = r["enabled"] as boolean;
1091
- }
1092
- return out;
1093
- }
1094
-
1095
- function parseProviderUpdate(raw: unknown): ProviderUpdate | null {
1096
- if (!raw || typeof raw !== "object") return null;
1097
- const r = raw as Record<string, unknown>;
1098
- const patch: ProviderUpdate = {};
1099
- if (typeof r["name"] === "string") (patch as { name?: string }).name = r["name"];
1100
- if ("baseUrl" in r || "base_url" in r) {
1101
- const v = r["baseUrl"] ?? r["base_url"];
1102
- if (typeof v === "string" || v === null) (patch as { baseUrl?: string | null }).baseUrl = v;
1103
- }
1104
- if ("apiKey" in r || "api_key" in r) {
1105
- const v = r["apiKey"] ?? r["api_key"];
1106
- if (typeof v === "string" || v === null) (patch as { apiKey?: string | null }).apiKey = v;
1107
- }
1108
- if ("defaultModel" in r || "default_model" in r) {
1109
- const v = r["defaultModel"] ?? r["default_model"];
1110
- if (typeof v === "string" || v === null) (patch as { defaultModel?: string | null }).defaultModel = v;
1111
- }
1112
- if (typeof r["enabled"] === "boolean") (patch as { enabled?: boolean }).enabled = r["enabled"] as boolean;
1113
- if (Object.keys(patch).length === 0) return null;
1114
- return patch;
1115
- }
1116
-
1117
- function mountSpa(app: Hono, dist: string): void {
1118
- const indexHtml = join(dist, "index.html");
1119
- if (!existsSync(indexHtml)) return;
1120
-
1121
- app.get("/ui/*", (c) => {
1122
- const rel = c.req.path.replace(/^\/ui\/?/, "");
1123
- if (rel) {
1124
- const safe = normalize(rel);
1125
- if (!safe.startsWith("..") && !safe.startsWith(sep)) {
1126
- const candidate = join(dist, safe);
1127
- if (existsSync(candidate) && statSync(candidate).isFile()) {
1128
- const mime = MIME_TYPES[extname(candidate)] ?? "application/octet-stream";
1129
- return c.body(readFileSync(candidate), 200, { "content-type": mime });
1130
- }
1131
- }
1132
- }
1133
- return c.html(readFileSync(indexHtml, "utf8"));
1134
- });
1135
-
1136
- app.get("/ui", (c) => c.redirect("/ui/"));
1137
- }