nlm-memory 0.5.0 → 0.5.2

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