@gmickel/gno 2.15.0 → 2.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (218) hide show
  1. package/README.md +1 -1
  2. package/assets/skill/SKILL.md +27 -0
  3. package/assets/skill/cli-reference.md +24 -15
  4. package/assets/spa-production.json.gz +0 -0
  5. package/browser-extension/artifacts/{gno-browser-clipper-v2.15.0.zip → gno-browser-clipper-v2.17.0.zip} +0 -0
  6. package/browser-extension/artifacts/gno-browser-clipper-v2.17.0.zip.sha256 +1 -0
  7. package/browser-extension/dist/manifest.json +1 -1
  8. package/package.json +1 -1
  9. package/spec/cli.md +43 -3
  10. package/spec/db/schema.sql +53 -1
  11. package/spec/mcp.md +9 -3
  12. package/spec/output-schemas/doctor.schema.json +27 -0
  13. package/spec/output-schemas/retrieval-trace-payloads.schema.json +14 -0
  14. package/spec/output-schemas/search-results.schema.json +14 -0
  15. package/spec/output-schemas/status.schema.json +22 -0
  16. package/src/cli/commands/agents/commands.ts +1 -1
  17. package/src/cli/commands/ask.ts +1 -1
  18. package/src/cli/commands/context-build.ts +1 -1
  19. package/src/cli/commands/doctor.ts +101 -29
  20. package/src/cli/commands/embed.ts +2 -2
  21. package/src/cli/commands/links.ts +30 -6
  22. package/src/cli/commands/mcp/install.ts +2 -2
  23. package/src/cli/commands/mcp/status.ts +1 -1
  24. package/src/cli/commands/mcp/uninstall.ts +1 -1
  25. package/src/cli/commands/query.ts +1 -1
  26. package/src/cli/commands/shared.ts +4 -4
  27. package/src/cli/commands/skill/install.ts +1 -1
  28. package/src/cli/commands/skill/paths-cmd.ts +1 -1
  29. package/src/cli/commands/skill/uninstall.ts +1 -1
  30. package/src/cli/commands/status.ts +75 -58
  31. package/src/cli/commands/vec.ts +180 -8
  32. package/src/cli/config-cache.ts +86 -0
  33. package/src/cli/globals.ts +64 -0
  34. package/src/cli/program.ts +63 -36
  35. package/src/cli/run.ts +121 -67
  36. package/src/cli/runtime-health.ts +132 -122
  37. package/src/cli/session-binding.ts +11 -2
  38. package/src/config/saver.ts +8 -0
  39. package/src/config/types.ts +6 -0
  40. package/src/converters/adapters/browser-export/formats.ts +25 -15
  41. package/src/converters/adapters/email/adapter.ts +37 -53
  42. package/src/converters/adapters/email/html.ts +37 -20
  43. package/src/converters/adapters/email/mime.ts +71 -39
  44. package/src/converters/adapters/ical/adapter.ts +20 -12
  45. package/src/converters/adapters/jsonl/adapter.ts +8 -6
  46. package/src/converters/adapters/shared/html-text.ts +55 -37
  47. package/src/converters/adapters/shared/ooxml-protection.ts +5 -13
  48. package/src/converters/adapters/shared/utf8-lines.ts +69 -31
  49. package/src/converters/adapters/transcript/adapter.ts +12 -7
  50. package/src/converters/adapters/xlsx/adapter.ts +10 -7
  51. package/src/converters/adapters/xlsx/sheet-markdown.ts +93 -50
  52. package/src/converters/canonicalize.ts +17 -3
  53. package/src/converters/native/markdown.ts +24 -17
  54. package/src/converters/native/plaintext.ts +5 -6
  55. package/src/converters/registry.ts +130 -8
  56. package/src/core/activation-status.ts +4 -8
  57. package/src/core/change-diff.ts +38 -11
  58. package/src/core/context-compiler.ts +17 -18
  59. package/src/core/context-evidence.ts +116 -25
  60. package/src/core/context-verifier.ts +70 -31
  61. package/src/core/file-lock.ts +10 -2
  62. package/src/core/graph-analysis.ts +3 -3
  63. package/src/core/link-inventory-markdown.ts +2 -2
  64. package/src/core/links.ts +20 -87
  65. package/src/core/memory-fence.ts +16 -9
  66. package/src/core/memory-recall.ts +168 -34
  67. package/src/core/path-rules.ts +143 -0
  68. package/src/core/retrieval-trace-session.ts +3 -0
  69. package/src/core/retrieval-trace.ts +9 -0
  70. package/src/core/section-parse.ts +16 -6
  71. package/src/core/typed-metadata.ts +68 -0
  72. package/src/core/vector-partition-status.ts +13 -0
  73. package/src/core/wiki-link-syntax.ts +88 -0
  74. package/src/embed/backlog.ts +142 -60
  75. package/src/embed/retry.ts +39 -33
  76. package/src/embed/variant-backlog.ts +582 -116
  77. package/src/embed/variant-retry.ts +157 -48
  78. package/src/index.ts +3 -3
  79. package/src/ingestion/budget-error-codes.ts +13 -0
  80. package/src/ingestion/chunker.ts +60 -49
  81. package/src/ingestion/compiled-context.ts +4 -5
  82. package/src/ingestion/convert-file.ts +1 -1
  83. package/src/ingestion/document-metadata.ts +16 -9
  84. package/src/ingestion/excluded-ranges.ts +97 -0
  85. package/src/ingestion/file-processor.ts +122 -37
  86. package/src/ingestion/frontmatter.ts +35 -7
  87. package/src/ingestion/graph-reconciliation.ts +187 -51
  88. package/src/ingestion/language.ts +66 -61
  89. package/src/ingestion/position.ts +5 -5
  90. package/src/ingestion/prepare-file.ts +50 -29
  91. package/src/ingestion/record-adapter-canonical.ts +37 -26
  92. package/src/ingestion/record-adapter.ts +46 -26
  93. package/src/ingestion/record-container.ts +121 -53
  94. package/src/ingestion/record-path.ts +5 -2
  95. package/src/ingestion/source-availability/darwin-io.ts +10 -7
  96. package/src/ingestion/source-availability/darwin-path.ts +21 -3
  97. package/src/ingestion/source-availability/readers.ts +51 -0
  98. package/src/ingestion/strip.ts +39 -105
  99. package/src/ingestion/sync.ts +692 -386
  100. package/src/ingestion/typed-metadata.ts +10 -4
  101. package/src/ingestion/walker.ts +485 -158
  102. package/src/llm/cache.ts +128 -84
  103. package/src/llm/http-inference.ts +112 -35
  104. package/src/llm/http-policy.ts +101 -0
  105. package/src/llm/httpEmbedding.ts +8 -1
  106. package/src/llm/httpGeneration.ts +5 -1
  107. package/src/llm/httpRerank.ts +5 -1
  108. package/src/llm/inference-scope.ts +15 -2
  109. package/src/llm/lazy-ports.ts +33 -3
  110. package/src/llm/lockfile.ts +34 -7
  111. package/src/llm/model-cleanup.ts +24 -0
  112. package/src/llm/native-worker/client.ts +23 -12
  113. package/src/llm/native-worker/dispatcher.ts +19 -9
  114. package/src/llm/native-worker/embedding-identity.ts +42 -16
  115. package/src/llm/native-worker/entry.ts +2 -1
  116. package/src/llm/native-worker/owner.ts +1 -0
  117. package/src/llm/native-worker/ports.ts +86 -23
  118. package/src/llm/native-worker/protocol.ts +386 -55
  119. package/src/llm/nodeLlamaCpp/adapter.ts +46 -29
  120. package/src/llm/nodeLlamaCpp/embedding.ts +93 -40
  121. package/src/llm/nodeLlamaCpp/generation.ts +194 -30
  122. package/src/llm/nodeLlamaCpp/lifecycle.ts +2 -0
  123. package/src/llm/nodeLlamaCpp/rerank-capacity.ts +101 -23
  124. package/src/llm/nodeLlamaCpp/rerank.ts +253 -44
  125. package/src/llm/types.ts +14 -0
  126. package/src/mcp/context.ts +342 -23
  127. package/src/mcp/http-transport.ts +21 -3
  128. package/src/mcp/tool-profile-types.ts +62 -0
  129. package/src/mcp/tool-profile.ts +32 -56
  130. package/src/mcp/tools/context.ts +5 -1
  131. package/src/mcp/tools/index.ts +269 -191
  132. package/src/mcp/tools/links.ts +14 -6
  133. package/src/mcp/tools/query.ts +60 -37
  134. package/src/mcp/tools/vsearch.ts +25 -14
  135. package/src/pipeline/answer.ts +133 -40
  136. package/src/pipeline/claim-verification.ts +293 -189
  137. package/src/pipeline/claim-verifier.ts +6 -7
  138. package/src/pipeline/egress-lineage.ts +37 -26
  139. package/src/pipeline/exclude.ts +34 -11
  140. package/src/pipeline/expansion.ts +233 -19
  141. package/src/pipeline/explain.ts +17 -2
  142. package/src/pipeline/filters.ts +169 -33
  143. package/src/pipeline/graph-retrieval.ts +117 -61
  144. package/src/pipeline/hybrid.ts +626 -330
  145. package/src/pipeline/hydration.ts +86 -24
  146. package/src/pipeline/intent.ts +15 -7
  147. package/src/pipeline/owner-fusion.ts +61 -19
  148. package/src/pipeline/query-language.ts +15 -2
  149. package/src/pipeline/rerank.ts +15 -22
  150. package/src/pipeline/search.ts +193 -72
  151. package/src/pipeline/snippet.ts +15 -2
  152. package/src/pipeline/temporal.ts +51 -10
  153. package/src/pipeline/types.ts +7 -0
  154. package/src/pipeline/vsearch.ts +269 -154
  155. package/src/publish/attachment-raster.ts +2 -16
  156. package/src/publish/encrypted-export.ts +15 -5
  157. package/src/serve/browse-tree.ts +50 -42
  158. package/src/serve/connectors.ts +2 -1
  159. package/src/serve/context.ts +13 -4
  160. package/src/serve/doc-events.ts +3 -0
  161. package/src/serve/public/app.tsx +49 -21
  162. package/src/serve/public/components/BrowseTreeSidebar.tsx +163 -23
  163. package/src/serve/public/components/CaptureModal.tsx +14 -10
  164. package/src/serve/public/components/ai-elements/code-block.tsx +87 -50
  165. package/src/serve/public/components/editor/MarkdownPreview.tsx +99 -67
  166. package/src/serve/public/globals.built.css +1 -1
  167. package/src/serve/public/globals.css +19 -0
  168. package/src/serve/public/hooks/useCaptureModal.tsx +23 -10
  169. package/src/serve/public/lib/metadata-filter.ts +234 -9
  170. package/src/serve/public/pages/Browse.tsx +61 -27
  171. package/src/serve/public/pages/DocView.tsx +44 -25
  172. package/src/serve/public/pages/GraphView.tsx +8 -3
  173. package/src/serve/resident-runtime.ts +199 -48
  174. package/src/serve/routes/api.ts +98 -67
  175. package/src/serve/routes/links.ts +25 -5
  176. package/src/serve/server.ts +1370 -1180
  177. package/src/serve/spa-bundle-source.ts +52 -27
  178. package/src/serve/status.ts +101 -52
  179. package/src/serve/watch-reconciliation-fallback.ts +11 -7
  180. package/src/serve/watch-reconciliation-shared.ts +42 -4
  181. package/src/serve/watch-reconciliation.ts +21 -8
  182. package/src/serve/watch-service-events.ts +7 -1
  183. package/src/serve/watch-service-hosts.ts +3 -1
  184. package/src/serve/watch-service.ts +46 -21
  185. package/src/serve/watch-snapshot-scan.ts +6 -15
  186. package/src/sessions/archive.ts +20 -8
  187. package/src/sessions/sanitize.ts +38 -18
  188. package/src/sessions/service.ts +180 -102
  189. package/src/store/migrations/037-performance-indexes.ts +56 -0
  190. package/src/store/migrations/038-vector-mirror-null-cosine.ts +47 -0
  191. package/src/store/migrations/039-vector-mirror-v2.ts +94 -0
  192. package/src/store/migrations/index.ts +6 -0
  193. package/src/store/sqlite/adapter.ts +1260 -651
  194. package/src/store/sqlite/chunking-policy.ts +15 -1
  195. package/src/store/sqlite/eligibility.ts +110 -70
  196. package/src/store/sqlite/graph-neighbors.ts +71 -46
  197. package/src/store/sqlite/graph-reference-state.ts +27 -3
  198. package/src/store/sqlite/graph-similarity.ts +59 -12
  199. package/src/store/sqlite/metadata-predicate.ts +123 -28
  200. package/src/store/sqlite/retrieval-trace-management-store.ts +126 -33
  201. package/src/store/sqlite/retrieval-trace-retention.ts +88 -76
  202. package/src/store/sqlite/retrieval-trace-rows.ts +7 -3
  203. package/src/store/types.ts +32 -0
  204. package/src/store/vector/coarse-router.ts +312 -0
  205. package/src/store/vector/eligibility.ts +39 -0
  206. package/src/store/vector/lazy.ts +17 -0
  207. package/src/store/vector/partition-index.ts +333 -59
  208. package/src/store/vector/proof-cache.ts +209 -0
  209. package/src/store/vector/runtime-compat.ts +6 -0
  210. package/src/store/vector/sqlite-vec.ts +270 -162
  211. package/src/store/vector/stats.ts +37 -73
  212. package/src/store/vector/status.ts +169 -53
  213. package/src/store/vector/stored-vectors.ts +131 -45
  214. package/src/store/vector/types.ts +19 -0
  215. package/src/store/vector/v2-mirror.ts +669 -0
  216. package/src/store/vector/variant-search.ts +1187 -111
  217. package/src/store/vector/variants.ts +964 -160
  218. package/browser-extension/artifacts/gno-browser-clipper-v2.15.0.zip.sha256 +0 -1
@@ -293,6 +293,111 @@ export function acceptsGzip(acceptEncoding: string | null): boolean {
293
293
  return (wildcardQ ?? 0) > 0;
294
294
  }
295
295
 
296
+ const API_GZIP_MIN_BYTES = 2048;
297
+ const API_GZIP_EXEMPT_ROUTES = new Set(["/api/doc-asset", "/api/events"]);
298
+
299
+ function appendVaryAcceptEncoding(headers: Headers): void {
300
+ const existing = headers.get("vary");
301
+ if (!existing) {
302
+ headers.set("Vary", "Accept-Encoding");
303
+ return;
304
+ }
305
+ const tokens = existing
306
+ .split(",")
307
+ .map((part) => part.trim().toLowerCase())
308
+ .filter(Boolean);
309
+ if (!tokens.includes("accept-encoding")) {
310
+ headers.set("Vary", `${existing}, Accept-Encoding`);
311
+ }
312
+ }
313
+
314
+ /**
315
+ * Compress REST `/api/*` JSON responses larger than 2 KiB when the client
316
+ * advertises gzip support, preserving all existing headers and adding
317
+ * `Vary: Accept-Encoding`.
318
+ */
319
+ export async function maybeCompressApiJsonResponse(
320
+ req: Request,
321
+ response: Response
322
+ ): Promise<Response> {
323
+ if (
324
+ req.method === "HEAD" ||
325
+ response.body === null ||
326
+ response.headers.get("content-encoding") !== null
327
+ ) {
328
+ return response;
329
+ }
330
+ const contentType = response.headers.get("content-type") ?? "";
331
+ if (!contentType.includes("application/json")) {
332
+ return response;
333
+ }
334
+ if (!acceptsGzip(req.headers.get("accept-encoding"))) {
335
+ return response;
336
+ }
337
+ const raw = new Uint8Array(await response.arrayBuffer());
338
+ const headers = new Headers(response.headers);
339
+ if (raw.byteLength <= API_GZIP_MIN_BYTES) {
340
+ headers.set("Content-Length", String(raw.byteLength));
341
+ return new Response(raw, {
342
+ status: response.status,
343
+ statusText: response.statusText,
344
+ headers,
345
+ });
346
+ }
347
+ const compressed = new Uint8Array(Bun.gzipSync(raw));
348
+ headers.set("Content-Encoding", "gzip");
349
+ headers.set("Content-Length", String(compressed.byteLength));
350
+ appendVaryAcceptEncoding(headers);
351
+ return new Response(compressed, {
352
+ status: response.status,
353
+ statusText: response.statusText,
354
+ headers,
355
+ });
356
+ }
357
+
358
+ function withApiJsonCompression<T extends Record<string, unknown>>(
359
+ routes: T
360
+ ): T {
361
+ const isMethodTable = (route: unknown): route is Record<string, unknown> =>
362
+ route !== null &&
363
+ typeof route === "object" &&
364
+ Object.getPrototypeOf(route) === Object.prototype;
365
+
366
+ const wrap =
367
+ (
368
+ handler: (req: Request, server: unknown) => Response | Promise<Response>
369
+ ) =>
370
+ async (req: Request, server: unknown): Promise<Response> =>
371
+ maybeCompressApiJsonResponse(req, await handler(req, server));
372
+
373
+ const wrapped: Record<string, unknown> = { ...routes };
374
+ for (const [path, route] of Object.entries(routes)) {
375
+ if (!path.startsWith("/api/") || API_GZIP_EXEMPT_ROUTES.has(path)) {
376
+ continue;
377
+ }
378
+ if (typeof route === "function") {
379
+ wrapped[path] = wrap(
380
+ route as (req: Request, server: unknown) => Response | Promise<Response>
381
+ );
382
+ } else if (isMethodTable(route)) {
383
+ wrapped[path] = Object.fromEntries(
384
+ Object.entries(route).map(([method, handler]) => [
385
+ method,
386
+ typeof handler === "function"
387
+ ? wrap(
388
+ handler as (
389
+ req: Request,
390
+ server: unknown
391
+ ) => Response | Promise<Response>
392
+ )
393
+ : handler,
394
+ ])
395
+ );
396
+ }
397
+ }
398
+ return wrapped as T;
399
+ }
400
+
296
401
  export type PublicFetchFallbackOptions = {
297
402
  isDev: boolean;
298
403
  spaBundleSource: SpaBundleSource | null;
@@ -311,7 +416,28 @@ export function createPublicFetchFallback(
311
416
  options: PublicFetchFallbackOptions
312
417
  ): (req: Request) => Promise<Response> {
313
418
  const { isDev, spaBundleSource } = options;
314
- const gzipBodies = new Map<string, Uint8Array<ArrayBuffer>>();
419
+ const gzipBodies = new Map<
420
+ string,
421
+ { body: Uint8Array<ArrayBuffer>; contentType: string | null }
422
+ >();
423
+
424
+ const buildCachedGzipResponse = (
425
+ req: Request,
426
+ cached: { body: Uint8Array<ArrayBuffer>; contentType: string | null }
427
+ ): Response => {
428
+ const headers = new Headers();
429
+ if (cached.contentType) {
430
+ headers.set("Content-Type", cached.contentType);
431
+ }
432
+ headers.set("Cache-Control", SPA_CHUNK_CACHE_CONTROL);
433
+ headers.set("Vary", "Accept-Encoding");
434
+ headers.set("Content-Encoding", "gzip");
435
+ headers.set("Content-Length", String(cached.body.byteLength));
436
+ return new Response(req.method === "HEAD" ? null : cached.body, {
437
+ status: 200,
438
+ headers,
439
+ });
440
+ };
315
441
 
316
442
  const gzipBodyFor = async (
317
443
  req: Request,
@@ -321,7 +447,7 @@ export function createPublicFetchFallback(
321
447
  const cached = gzipBodies.get(pathname);
322
448
  if (cached) {
323
449
  await asset.body?.cancel();
324
- return cached;
450
+ return cached.body;
325
451
  }
326
452
  // HEAD reaches the source as HEAD, so its body is empty; fetch the bytes.
327
453
  const identity =
@@ -332,7 +458,12 @@ export function createPublicFetchFallback(
332
458
  const encoded = new Uint8Array(
333
459
  Bun.gzipSync(new Uint8Array(await identity.arrayBuffer()))
334
460
  );
335
- gzipBodies.set(pathname, encoded);
461
+ gzipBodies.set(pathname, {
462
+ body: encoded,
463
+ contentType:
464
+ asset.headers.get("content-type") ??
465
+ identity.headers.get("content-type"),
466
+ });
336
467
  return encoded;
337
468
  };
338
469
 
@@ -375,6 +506,19 @@ export function createPublicFetchFallback(
375
506
  });
376
507
  }
377
508
  if (spaBundleSource && (req.method === "GET" || req.method === "HEAD")) {
509
+ if (
510
+ !isDev &&
511
+ isHashedSpaChunkPath(pathname) &&
512
+ acceptsGzip(req.headers.get("accept-encoding"))
513
+ ) {
514
+ const cached = gzipBodies.get(pathname);
515
+ if (cached) {
516
+ return withSecurityHeaders(
517
+ buildCachedGzipResponse(req, cached),
518
+ isDev
519
+ );
520
+ }
521
+ }
378
522
  const asset = await spaBundleSource.fetch(req);
379
523
  if (asset.status === 200 && !isDev && isHashedSpaChunkPath(pathname)) {
380
524
  return withSecurityHeaders(
@@ -526,24 +670,56 @@ export async function startServer(
526
670
  spaBundleSource?.entryPath ??
527
671
  `/__gno_spa_${crypto.randomUUID().replaceAll("-", "")}`;
528
672
  let spaHtmlCache: {
529
- body: ArrayBuffer;
673
+ body: Uint8Array<ArrayBuffer>;
530
674
  contentType: string;
531
- etag: string | null;
675
+ etag: string;
532
676
  } | null = null;
533
677
 
534
- const serveSpaHtml = async (): Promise<Response> => {
535
- if (!isDev && spaHtmlCache) {
536
- const headers = new Headers({
537
- "Content-Type": spaHtmlCache.contentType,
538
- });
539
- if (spaHtmlCache.etag) {
540
- headers.set("ETag", spaHtmlCache.etag);
678
+ const matchesIfNoneMatch = (
679
+ headerValue: string | null,
680
+ etag: string
681
+ ): boolean => {
682
+ if (!headerValue) return false;
683
+ const normalizeTag = (tag: string): string =>
684
+ tag.trim().replace(/^W\//, "");
685
+ const target = normalizeTag(etag);
686
+ for (const part of headerValue.split(",")) {
687
+ const trimmed = part.trim();
688
+ if (trimmed === "*" || normalizeTag(trimmed) === target) {
689
+ return true;
541
690
  }
691
+ }
692
+ return false;
693
+ };
694
+
695
+ const buildSpaHtmlResponse = (
696
+ req: Request | undefined,
697
+ cached: {
698
+ body: Uint8Array<ArrayBuffer>;
699
+ contentType: string;
700
+ etag: string;
701
+ }
702
+ ): Response => {
703
+ const headers = new Headers({
704
+ "Content-Type": cached.contentType,
705
+ ETag: cached.etag,
706
+ });
707
+ if (
708
+ matchesIfNoneMatch(req?.headers.get("if-none-match") ?? null, cached.etag)
709
+ ) {
542
710
  return withSecurityHeaders(
543
- new Response(spaHtmlCache.body.slice(0), { headers }),
711
+ new Response(null, { status: 304, headers }),
544
712
  isDev
545
713
  );
546
714
  }
715
+ headers.set("Content-Length", String(cached.body.byteLength));
716
+ return withSecurityHeaders(new Response(cached.body, { headers }), isDev);
717
+ };
718
+
719
+ const serveSpaHtml = async (req?: Request): Promise<Response> => {
720
+ if (!isDev && spaHtmlCache) {
721
+ return buildSpaHtmlResponse(req, spaHtmlCache);
722
+ }
547
723
  const boundPort = server.port ?? port;
548
724
  const internalRequest = new Request(
549
725
  `${loopbackHttpOrigin(gatewayConfig.host, boundPort)}${spaInternalPath}`
@@ -558,22 +734,16 @@ export async function startServer(
558
734
  );
559
735
  }
560
736
  if (!isDev) {
737
+ const body = new Uint8Array(await raw.arrayBuffer());
738
+ const etag =
739
+ raw.headers.get("etag") ?? `W/"${Bun.hash(body).toString(16)}"`;
561
740
  spaHtmlCache = {
562
- body: await raw.arrayBuffer(),
741
+ body,
563
742
  contentType:
564
743
  raw.headers.get("content-type") ?? "text/html;charset=utf-8",
565
- etag: raw.headers.get("etag"),
744
+ etag,
566
745
  };
567
- const headers = new Headers({
568
- "Content-Type": spaHtmlCache.contentType,
569
- });
570
- if (spaHtmlCache.etag) {
571
- headers.set("ETag", spaHtmlCache.etag);
572
- }
573
- return withSecurityHeaders(
574
- new Response(spaHtmlCache.body.slice(0), { headers }),
575
- isDev
576
- );
746
+ return buildSpaHtmlResponse(req, spaHtmlCache);
577
747
  }
578
748
  return withSecurityHeaders(raw, isDev);
579
749
  };
@@ -591,1192 +761,1212 @@ export async function startServer(
591
761
  development: isDev,
592
762
 
593
763
  // Static routes - Bun handles HTML bundling and /_bun/* assets automatically
594
- routes: withRemoteHostPathRedaction({
595
- "/mcp": gateway.route,
596
- ...clipperRoutesForBind(
597
- isHttpGatewayLoopbackBind(gatewayConfig.host),
598
- clipperGateway
599
- ),
600
- // Injected-server tests only. Real runs keep the raw HTMLBundle off the
601
- // public listener in createSpaBundleSource above.
602
- ...(spaBundleSource ? {} : { [spaInternalPath]: homepage }),
603
- // SPA routes - same React app, security envelope on every document
604
- "/": spaPageRoute,
605
- "/search": spaPageRoute,
606
- "/browse": spaPageRoute,
607
- "/doc": spaPageRoute,
608
- "/edit": spaPageRoute,
609
- "/collections": spaPageRoute,
610
- "/connectors": spaPageRoute,
611
- "/traces": spaPageRoute,
612
- "/sessions": spaPageRoute,
613
- "/context/compiled": spaPageRoute,
614
- "/ask": spaPageRoute,
615
- "/graph": spaPageRoute,
616
- "/clipper/pair": spaPageRoute,
764
+ routes: withApiJsonCompression(
765
+ withRemoteHostPathRedaction({
766
+ "/mcp": gateway.route,
767
+ ...clipperRoutesForBind(
768
+ isHttpGatewayLoopbackBind(gatewayConfig.host),
769
+ clipperGateway
770
+ ),
771
+ // Injected-server tests only. Real runs keep the raw HTMLBundle off the
772
+ // public listener in createSpaBundleSource above.
773
+ ...(spaBundleSource ? {} : { [spaInternalPath]: homepage }),
774
+ // SPA routes - same React app, security envelope on every document
775
+ "/": spaPageRoute,
776
+ "/search": spaPageRoute,
777
+ "/browse": spaPageRoute,
778
+ "/doc": spaPageRoute,
779
+ "/edit": spaPageRoute,
780
+ "/collections": spaPageRoute,
781
+ "/connectors": spaPageRoute,
782
+ "/traces": spaPageRoute,
783
+ "/sessions": spaPageRoute,
784
+ "/context/compiled": spaPageRoute,
785
+ "/ask": spaPageRoute,
786
+ "/graph": spaPageRoute,
787
+ "/clipper/pair": spaPageRoute,
617
788
 
618
- // API routes with CSRF protection wrapper
619
- "/api/health": {
620
- GET: () => withSecurityHeaders(handleHealth(), isDev),
621
- },
622
- "/api/status": {
623
- GET: async (req: Request) =>
624
- withSecurityHeaders(
625
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
626
- handleStatus(ctxHolder.current, {
627
- getResidentStatus: () =>
628
- (runtime as ResidentRuntime).getStatus(),
629
- })
630
- ),
631
- isDev
632
- ),
633
- },
634
- "/api/traces/export": {
635
- POST: async (req: Request) => {
636
- if (!isRequestAllowed(req, port)) {
637
- return withSecurityHeaders(forbiddenResponse(), isDev);
638
- }
639
- return withSecurityHeaders(
640
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
641
- handleTraceExport(store, req, ctxHolder.config.collections)
789
+ // API routes with CSRF protection wrapper
790
+ "/api/health": {
791
+ GET: () => withSecurityHeaders(handleHealth(), isDev),
792
+ },
793
+ "/api/status": {
794
+ GET: async (req: Request) =>
795
+ withSecurityHeaders(
796
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
797
+ handleStatus(ctxHolder.current, {
798
+ getResidentStatus: () =>
799
+ (runtime as ResidentRuntime).getStatus(),
800
+ })
801
+ ),
802
+ isDev
642
803
  ),
643
- isDev
644
- );
645
- },
646
- },
647
- "/api/traces/:traceId/judgments": {
648
- POST: async (req: Request) => {
649
- if (!isRequestAllowed(req, port)) {
650
- return withSecurityHeaders(forbiddenResponse(), isDev);
651
- }
652
- const traceId = decodeURIComponent(
653
- new URL(req.url).pathname.split("/")[3] ?? ""
654
- );
655
- return withSecurityHeaders(
656
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
657
- handleTraceLabel(store, traceId, req)
804
+ },
805
+ "/api/traces/export": {
806
+ POST: async (req: Request) => {
807
+ if (!isRequestAllowed(req, port)) {
808
+ return withSecurityHeaders(forbiddenResponse(), isDev);
809
+ }
810
+ return withSecurityHeaders(
811
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
812
+ handleTraceExport(store, req, ctxHolder.config.collections)
813
+ ),
814
+ isDev
815
+ );
816
+ },
817
+ },
818
+ "/api/traces/:traceId/judgments": {
819
+ POST: async (req: Request) => {
820
+ if (!isRequestAllowed(req, port)) {
821
+ return withSecurityHeaders(forbiddenResponse(), isDev);
822
+ }
823
+ const traceId = decodeURIComponent(
824
+ new URL(req.url).pathname.split("/")[3] ?? ""
825
+ );
826
+ return withSecurityHeaders(
827
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
828
+ handleTraceLabel(store, traceId, req)
829
+ ),
830
+ isDev
831
+ );
832
+ },
833
+ },
834
+ "/api/traces/:traceId": {
835
+ GET: async (req: Request) => {
836
+ const traceId = decodeURIComponent(
837
+ new URL(req.url).pathname.split("/")[3] ?? ""
838
+ );
839
+ return withSecurityHeaders(
840
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
841
+ handleTraceShow(store, traceId, req)
842
+ ),
843
+ isDev
844
+ );
845
+ },
846
+ DELETE: async (req: Request) => {
847
+ if (!isRequestAllowed(req, port)) {
848
+ return withSecurityHeaders(forbiddenResponse(), isDev);
849
+ }
850
+ const traceId = decodeURIComponent(
851
+ new URL(req.url).pathname.split("/")[3] ?? ""
852
+ );
853
+ return withSecurityHeaders(
854
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
855
+ handleTraceDelete(store, traceId)
856
+ ),
857
+ isDev
858
+ );
859
+ },
860
+ },
861
+ "/api/traces": {
862
+ GET: async (req: Request) =>
863
+ withSecurityHeaders(
864
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
865
+ handleTraceList(store, req)
866
+ ),
867
+ isDev
658
868
  ),
659
- isDev
660
- );
661
- },
662
- },
663
- "/api/traces/:traceId": {
664
- GET: async (req: Request) => {
665
- const traceId = decodeURIComponent(
666
- new URL(req.url).pathname.split("/")[3] ?? ""
667
- );
668
- return withSecurityHeaders(
669
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
670
- handleTraceShow(store, traceId, req)
869
+ DELETE: async (req: Request) => {
870
+ if (!isRequestAllowed(req, port)) {
871
+ return withSecurityHeaders(forbiddenResponse(), isDev);
872
+ }
873
+ return withSecurityHeaders(
874
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
875
+ handleTracePurge(store)
876
+ ),
877
+ isDev
878
+ );
879
+ },
880
+ },
881
+ "/api/resident/status": {
882
+ GET: () =>
883
+ withSecurityHeaders(
884
+ handleResidentStatus(() =>
885
+ (runtime as ResidentRuntime).getStatus()
886
+ ),
887
+ isDev
671
888
  ),
672
- isDev
673
- );
674
- },
675
- DELETE: async (req: Request) => {
676
- if (!isRequestAllowed(req, port)) {
677
- return withSecurityHeaders(forbiddenResponse(), isDev);
678
- }
679
- const traceId = decodeURIComponent(
680
- new URL(req.url).pathname.split("/")[3] ?? ""
681
- );
682
- return withSecurityHeaders(
683
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
684
- handleTraceDelete(store, traceId)
889
+ },
890
+ "/api/collections": {
891
+ GET: async () =>
892
+ withSecurityHeaders(
893
+ (await refreshResidentConfig(runtime as ResidentRuntime)) ??
894
+ (await handleCollections(ctxHolder.config)),
895
+ isDev
685
896
  ),
686
- isDev
687
- );
688
- },
689
- },
690
- "/api/traces": {
691
- GET: async (req: Request) =>
692
- withSecurityHeaders(
693
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
694
- handleTraceList(store, req)
897
+ POST: async (req: Request) => {
898
+ if (!isRequestAllowed(req, port)) {
899
+ return withSecurityHeaders(forbiddenResponse(), isDev);
900
+ }
901
+ return withSecurityHeaders(
902
+ await handleCreateCollection(ctxHolder, store, req),
903
+ isDev
904
+ );
905
+ },
906
+ },
907
+ "/api/egress/check": {
908
+ POST: async (req: Request) =>
909
+ withSecurityHeaders(
910
+ await handleCollectionEgressCheck(ctxHolder, req),
911
+ isDev
695
912
  ),
696
- isDev
697
- ),
698
- DELETE: async (req: Request) => {
699
- if (!isRequestAllowed(req, port)) {
700
- return withSecurityHeaders(forbiddenResponse(), isDev);
701
- }
702
- return withSecurityHeaders(
703
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
704
- handleTracePurge(store)
913
+ },
914
+ "/api/egress/audits/status": {
915
+ GET: async () =>
916
+ withSecurityHeaders(await handleEgressAuditStatus(store), isDev),
917
+ },
918
+ "/api/egress/audits": {
919
+ GET: async (req: Request) =>
920
+ withSecurityHeaders(
921
+ await handleEgressAuditList(store, req),
922
+ isDev
705
923
  ),
706
- isDev
707
- );
708
- },
709
- },
710
- "/api/resident/status": {
711
- GET: () =>
712
- withSecurityHeaders(
713
- handleResidentStatus(() =>
714
- (runtime as ResidentRuntime).getStatus()
924
+ DELETE: async (req: Request) => {
925
+ if (!isRequestAllowed(req, port)) {
926
+ return withSecurityHeaders(forbiddenResponse(), isDev);
927
+ }
928
+ return withSecurityHeaders(
929
+ await handleEgressAuditPurge(store),
930
+ isDev
931
+ );
932
+ },
933
+ },
934
+ "/api/egress/audits/:auditId": {
935
+ GET: async (req: Request) => {
936
+ const auditId = decodeURIComponent(
937
+ new URL(req.url).pathname.split("/").at(-1) ?? ""
938
+ );
939
+ return withSecurityHeaders(
940
+ await handleEgressAuditShow(store, auditId),
941
+ isDev
942
+ );
943
+ },
944
+ DELETE: async (req: Request) => {
945
+ if (!isRequestAllowed(req, port)) {
946
+ return withSecurityHeaders(forbiddenResponse(), isDev);
947
+ }
948
+ const auditId = decodeURIComponent(
949
+ new URL(req.url).pathname.split("/").at(-1) ?? ""
950
+ );
951
+ return withSecurityHeaders(
952
+ await handleEgressAuditDelete(store, auditId),
953
+ isDev
954
+ );
955
+ },
956
+ },
957
+ "/api/connectors": {
958
+ GET: async (req: Request) =>
959
+ withSecurityHeaders(
960
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
961
+ handleConnectors(ctxHolder.config)
962
+ ),
963
+ isDev
715
964
  ),
716
- isDev
717
- ),
718
- },
719
- "/api/collections": {
720
- GET: async () =>
721
- withSecurityHeaders(
722
- (await refreshResidentConfig(runtime as ResidentRuntime)) ??
723
- (await handleCollections(ctxHolder.config)),
724
- isDev
725
- ),
726
- POST: async (req: Request) => {
727
- if (!isRequestAllowed(req, port)) {
728
- return withSecurityHeaders(forbiddenResponse(), isDev);
729
- }
730
- return withSecurityHeaders(
731
- await handleCreateCollection(ctxHolder, store, req),
732
- isDev
733
- );
734
- },
735
- },
736
- "/api/egress/check": {
737
- POST: async (req: Request) =>
738
- withSecurityHeaders(
739
- await handleCollectionEgressCheck(ctxHolder, req),
740
- isDev
741
- ),
742
- },
743
- "/api/egress/audits/status": {
744
- GET: async () =>
745
- withSecurityHeaders(await handleEgressAuditStatus(store), isDev),
746
- },
747
- "/api/egress/audits": {
748
- GET: async (req: Request) =>
749
- withSecurityHeaders(await handleEgressAuditList(store, req), isDev),
750
- DELETE: async (req: Request) => {
751
- if (!isRequestAllowed(req, port)) {
752
- return withSecurityHeaders(forbiddenResponse(), isDev);
753
- }
754
- return withSecurityHeaders(
755
- await handleEgressAuditPurge(store),
756
- isDev
757
- );
758
- },
759
- },
760
- "/api/egress/audits/:auditId": {
761
- GET: async (req: Request) => {
762
- const auditId = decodeURIComponent(
763
- new URL(req.url).pathname.split("/").at(-1) ?? ""
764
- );
765
- return withSecurityHeaders(
766
- await handleEgressAuditShow(store, auditId),
767
- isDev
768
- );
769
- },
770
- DELETE: async (req: Request) => {
771
- if (!isRequestAllowed(req, port)) {
772
- return withSecurityHeaders(forbiddenResponse(), isDev);
773
- }
774
- const auditId = decodeURIComponent(
775
- new URL(req.url).pathname.split("/").at(-1) ?? ""
776
- );
777
- return withSecurityHeaders(
778
- await handleEgressAuditDelete(store, auditId),
779
- isDev
780
- );
781
- },
782
- },
783
- "/api/connectors": {
784
- GET: async (req: Request) =>
785
- withSecurityHeaders(
786
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
787
- handleConnectors(ctxHolder.config)
965
+ },
966
+ "/api/connectors/install": {
967
+ POST: async (req: Request) => {
968
+ if (!isRequestAllowed(req, port)) {
969
+ return withSecurityHeaders(forbiddenResponse(), isDev);
970
+ }
971
+ return withSecurityHeaders(
972
+ await (
973
+ dependencies.handleInstallConnector ?? handleInstallConnector
974
+ )(req, {
975
+ indexName: options.index,
976
+ configPath: runtime.actualConfigPath,
977
+ }),
978
+ isDev
979
+ );
980
+ },
981
+ },
982
+ "/api/connectors/verify": {
983
+ POST: async (req: Request) => {
984
+ if (!isRequestAllowed(req, port)) {
985
+ return withSecurityHeaders(forbiddenResponse(), isDev);
986
+ }
987
+ return withSecurityHeaders(
988
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
989
+ (dependencies.handleVerifyConnector ?? handleVerifyConnector)(
990
+ ctxHolder.config,
991
+ store,
992
+ req
993
+ )
994
+ ),
995
+ isDev
996
+ );
997
+ },
998
+ },
999
+ "/api/import/preview": {
1000
+ POST: async (req: Request) => {
1001
+ if (!isRequestAllowed(req, port)) {
1002
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1003
+ }
1004
+ return withSecurityHeaders(
1005
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1006
+ (dependencies.handleImportPreview ?? handleImportPreview)(
1007
+ ctxHolder,
1008
+ req
1009
+ )
1010
+ ),
1011
+ isDev
1012
+ );
1013
+ },
1014
+ },
1015
+ "/api/publish/export": {
1016
+ POST: async (req: Request) => {
1017
+ if (!isRequestAllowed(req, port)) {
1018
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1019
+ }
1020
+ return withSecurityHeaders(
1021
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1022
+ (dependencies.handlePublishExport ?? handlePublishExport)(
1023
+ ctxHolder.config,
1024
+ store,
1025
+ req,
1026
+ runtime.actualConfigPath
1027
+ )
1028
+ ),
1029
+ isDev
1030
+ );
1031
+ },
1032
+ },
1033
+ "/api/sync": {
1034
+ POST: async (req: Request) => {
1035
+ if (!isRequestAllowed(req, port)) {
1036
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1037
+ }
1038
+ return withSecurityHeaders(
1039
+ await handleSync(ctxHolder, store, req),
1040
+ isDev
1041
+ );
1042
+ },
1043
+ },
1044
+ "/api/capture": {
1045
+ POST: async (req: Request) => {
1046
+ if (!isRequestAllowed(req, port)) {
1047
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1048
+ }
1049
+ return withSecurityHeaders(
1050
+ await handleCreateCapture(ctxHolder, store, req),
1051
+ isDev
1052
+ );
1053
+ },
1054
+ },
1055
+ "/api/sessions/status": {
1056
+ GET: async (req: Request) =>
1057
+ withSecurityHeaders(
1058
+ (await refreshSessionsConfig(ctxHolder, store)) ??
1059
+ (await handleResidentRead(
1060
+ runtime as ResidentRuntime,
1061
+ req,
1062
+ () => handleSessionsStatus(ctxHolder)
1063
+ )),
1064
+ isDev
788
1065
  ),
789
- isDev
790
- ),
791
- },
792
- "/api/connectors/install": {
793
- POST: async (req: Request) => {
794
- if (!isRequestAllowed(req, port)) {
795
- return withSecurityHeaders(forbiddenResponse(), isDev);
796
- }
797
- return withSecurityHeaders(
798
- await (
799
- dependencies.handleInstallConnector ?? handleInstallConnector
800
- )(req, {
801
- indexName: options.index,
802
- configPath: runtime.actualConfigPath,
803
- }),
804
- isDev
805
- );
806
- },
807
- },
808
- "/api/connectors/verify": {
809
- POST: async (req: Request) => {
810
- if (!isRequestAllowed(req, port)) {
811
- return withSecurityHeaders(forbiddenResponse(), isDev);
812
- }
813
- return withSecurityHeaders(
814
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
815
- (dependencies.handleVerifyConnector ?? handleVerifyConnector)(
816
- ctxHolder.config,
1066
+ },
1067
+ "/api/sessions/discover": {
1068
+ GET: async (req: Request, server: RequestPeerServer) => {
1069
+ // Returns host paths: a cross-origin page is refused like a write.
1070
+ if (!isOriginOrTokenAllowed(req, port)) {
1071
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1072
+ }
1073
+ return withSecurityHeaders(
1074
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1075
+ handleSessionsDiscover(ctxHolder, req, { server })
1076
+ ),
1077
+ isDev
1078
+ );
1079
+ },
1080
+ },
1081
+ "/api/sessions/import": {
1082
+ POST: async (req: Request) => {
1083
+ if (!isRequestAllowed(req, port)) {
1084
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1085
+ }
1086
+ return withSecurityHeaders(
1087
+ await handleSessionsImport(ctxHolder, req),
1088
+ isDev
1089
+ );
1090
+ },
1091
+ },
1092
+ "/api/sessions/sources": {
1093
+ POST: async (req: Request, server: RequestPeerServer) => {
1094
+ if (!isRequestAllowed(req, port)) {
1095
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1096
+ }
1097
+ return withSecurityHeaders(
1098
+ await handleSessionsAddSource(ctxHolder, store, req, {
1099
+ server,
1100
+ }),
1101
+ isDev
1102
+ );
1103
+ },
1104
+ },
1105
+ "/api/sessions/sources/:id": {
1106
+ DELETE: async (req: Request, server: RequestPeerServer) => {
1107
+ if (!isRequestAllowed(req, port)) {
1108
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1109
+ }
1110
+ const id = decodeURIComponent(
1111
+ new URL(req.url).pathname.split("/")[4] ?? ""
1112
+ );
1113
+ return withSecurityHeaders(
1114
+ await handleSessionsRemoveSource(ctxHolder, store, id, req, {
1115
+ server,
1116
+ }),
1117
+ isDev
1118
+ );
1119
+ },
1120
+ },
1121
+ "/api/sessions/automation/run": {
1122
+ POST: async (req: Request) => {
1123
+ if (!isRequestAllowed(req, port)) {
1124
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1125
+ }
1126
+ return withSecurityHeaders(
1127
+ await handleSessionsAutomationRun(ctxHolder, store, req),
1128
+ isDev
1129
+ );
1130
+ },
1131
+ },
1132
+ "/api/sessions/automation/:id": {
1133
+ PUT: async (req: Request, server: RequestPeerServer) => {
1134
+ if (!isRequestAllowed(req, port)) {
1135
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1136
+ }
1137
+ return withSecurityHeaders(
1138
+ await handleSessionsAutomationSet(
1139
+ ctxHolder,
817
1140
  store,
818
- req
819
- )
820
- ),
821
- isDev
822
- );
823
- },
824
- },
825
- "/api/import/preview": {
826
- POST: async (req: Request) => {
827
- if (!isRequestAllowed(req, port)) {
828
- return withSecurityHeaders(forbiddenResponse(), isDev);
829
- }
830
- return withSecurityHeaders(
831
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
832
- (dependencies.handleImportPreview ?? handleImportPreview)(
1141
+ automationProfileId(req),
1142
+ req,
1143
+ { server }
1144
+ ),
1145
+ isDev
1146
+ );
1147
+ },
1148
+ DELETE: async (req: Request, server: RequestPeerServer) => {
1149
+ if (!isRequestAllowed(req, port)) {
1150
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1151
+ }
1152
+ return withSecurityHeaders(
1153
+ await handleSessionsAutomationRemove(
833
1154
  ctxHolder,
834
- req
835
- )
836
- ),
837
- isDev
838
- );
839
- },
840
- },
841
- "/api/publish/export": {
842
- POST: async (req: Request) => {
843
- if (!isRequestAllowed(req, port)) {
844
- return withSecurityHeaders(forbiddenResponse(), isDev);
845
- }
846
- return withSecurityHeaders(
847
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
848
- (dependencies.handlePublishExport ?? handlePublishExport)(
849
- ctxHolder.config,
850
1155
  store,
1156
+ automationProfileId(req),
851
1157
  req,
852
- runtime.actualConfigPath
853
- )
854
- ),
855
- isDev
856
- );
857
- },
858
- },
859
- "/api/sync": {
860
- POST: async (req: Request) => {
861
- if (!isRequestAllowed(req, port)) {
862
- return withSecurityHeaders(forbiddenResponse(), isDev);
863
- }
864
- return withSecurityHeaders(
865
- await handleSync(ctxHolder, store, req),
866
- isDev
867
- );
868
- },
869
- },
870
- "/api/capture": {
871
- POST: async (req: Request) => {
872
- if (!isRequestAllowed(req, port)) {
873
- return withSecurityHeaders(forbiddenResponse(), isDev);
874
- }
875
- return withSecurityHeaders(
876
- await handleCreateCapture(ctxHolder, store, req),
877
- isDev
878
- );
879
- },
880
- },
881
- "/api/sessions/status": {
882
- GET: async (req: Request) =>
883
- withSecurityHeaders(
884
- (await refreshSessionsConfig(ctxHolder, store)) ??
885
- (await handleResidentRead(runtime as ResidentRuntime, req, () =>
886
- handleSessionsStatus(ctxHolder)
887
- )),
888
- isDev
889
- ),
890
- },
891
- "/api/sessions/discover": {
892
- GET: async (req: Request, server: RequestPeerServer) => {
893
- // Returns host paths: a cross-origin page is refused like a write.
894
- if (!isOriginOrTokenAllowed(req, port)) {
895
- return withSecurityHeaders(forbiddenResponse(), isDev);
896
- }
897
- return withSecurityHeaders(
898
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
899
- handleSessionsDiscover(ctxHolder, req, { server })
900
- ),
901
- isDev
902
- );
903
- },
904
- },
905
- "/api/sessions/import": {
906
- POST: async (req: Request) => {
907
- if (!isRequestAllowed(req, port)) {
908
- return withSecurityHeaders(forbiddenResponse(), isDev);
909
- }
910
- return withSecurityHeaders(
911
- await handleSessionsImport(ctxHolder, req),
912
- isDev
913
- );
914
- },
915
- },
916
- "/api/sessions/sources": {
917
- POST: async (req: Request, server: RequestPeerServer) => {
918
- if (!isRequestAllowed(req, port)) {
919
- return withSecurityHeaders(forbiddenResponse(), isDev);
920
- }
921
- return withSecurityHeaders(
922
- await handleSessionsAddSource(ctxHolder, store, req, { server }),
923
- isDev
924
- );
925
- },
926
- },
927
- "/api/sessions/sources/:id": {
928
- DELETE: async (req: Request, server: RequestPeerServer) => {
929
- if (!isRequestAllowed(req, port)) {
930
- return withSecurityHeaders(forbiddenResponse(), isDev);
931
- }
932
- const id = decodeURIComponent(
933
- new URL(req.url).pathname.split("/")[4] ?? ""
934
- );
935
- return withSecurityHeaders(
936
- await handleSessionsRemoveSource(ctxHolder, store, id, req, {
937
- server,
938
- }),
939
- isDev
940
- );
941
- },
942
- },
943
- "/api/sessions/automation/run": {
944
- POST: async (req: Request) => {
945
- if (!isRequestAllowed(req, port)) {
946
- return withSecurityHeaders(forbiddenResponse(), isDev);
947
- }
948
- return withSecurityHeaders(
949
- await handleSessionsAutomationRun(ctxHolder, store, req),
950
- isDev
951
- );
952
- },
953
- },
954
- "/api/sessions/automation/:id": {
955
- PUT: async (req: Request, server: RequestPeerServer) => {
956
- if (!isRequestAllowed(req, port)) {
957
- return withSecurityHeaders(forbiddenResponse(), isDev);
958
- }
959
- return withSecurityHeaders(
960
- await handleSessionsAutomationSet(
961
- ctxHolder,
962
- store,
963
- automationProfileId(req),
964
- req,
965
- { server }
966
- ),
967
- isDev
968
- );
969
- },
970
- DELETE: async (req: Request, server: RequestPeerServer) => {
971
- if (!isRequestAllowed(req, port)) {
972
- return withSecurityHeaders(forbiddenResponse(), isDev);
973
- }
974
- return withSecurityHeaders(
975
- await handleSessionsAutomationRemove(
976
- ctxHolder,
977
- store,
978
- automationProfileId(req),
979
- req,
980
- { server }
1158
+ { server }
1159
+ ),
1160
+ isDev
1161
+ );
1162
+ },
1163
+ },
1164
+ "/api/sessions/automation/:id/preview": {
1165
+ GET: async (req: Request, server: RequestPeerServer) =>
1166
+ !isOriginOrTokenAllowed(req, port)
1167
+ ? withSecurityHeaders(forbiddenResponse(), isDev)
1168
+ : withSecurityHeaders(
1169
+ await handleResidentRead(
1170
+ runtime as ResidentRuntime,
1171
+ req,
1172
+ () =>
1173
+ handleSessionsAutomationPreview(
1174
+ ctxHolder,
1175
+ automationProfileId(req),
1176
+ req,
1177
+ { server }
1178
+ )
1179
+ ),
1180
+ isDev
1181
+ ),
1182
+ },
1183
+ "/api/sessions/automation/:id/enable": {
1184
+ POST: async (req: Request, server: RequestPeerServer) => {
1185
+ if (!isRequestAllowed(req, port)) {
1186
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1187
+ }
1188
+ return withSecurityHeaders(
1189
+ await handleSessionsAutomationEnable(
1190
+ ctxHolder,
1191
+ store,
1192
+ automationProfileId(req),
1193
+ req,
1194
+ { server }
1195
+ ),
1196
+ isDev
1197
+ );
1198
+ },
1199
+ },
1200
+ "/api/sessions/automation/:id/disable": {
1201
+ POST: async (req: Request, server: RequestPeerServer) => {
1202
+ if (!isRequestAllowed(req, port)) {
1203
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1204
+ }
1205
+ return withSecurityHeaders(
1206
+ await handleSessionsAutomationDisable(
1207
+ ctxHolder,
1208
+ store,
1209
+ automationProfileId(req),
1210
+ req,
1211
+ { server }
1212
+ ),
1213
+ isDev
1214
+ );
1215
+ },
1216
+ },
1217
+ "/api/sessions/init": {
1218
+ POST: async (req: Request, server: RequestPeerServer) => {
1219
+ if (!isRequestAllowed(req, port)) {
1220
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1221
+ }
1222
+ return withSecurityHeaders(
1223
+ await handleSessionsInit(ctxHolder, store, req, { server }),
1224
+ isDev
1225
+ );
1226
+ },
1227
+ },
1228
+ "/api/memory/remember": {
1229
+ POST: async (req: Request) => {
1230
+ if (!isRequestAllowed(req, port)) {
1231
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1232
+ }
1233
+ return withSecurityHeaders(
1234
+ await handleMemoryRemember(ctxHolder, store, req),
1235
+ isDev
1236
+ );
1237
+ },
1238
+ },
1239
+ "/api/memory/recall": {
1240
+ POST: async (req: Request) => {
1241
+ if (!isRequestAllowed(req, port)) {
1242
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1243
+ }
1244
+ return withSecurityHeaders(
1245
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1246
+ handleMemoryRecall(ctxHolder, store, req)
1247
+ ),
1248
+ isDev
1249
+ );
1250
+ },
1251
+ },
1252
+ "/api/docs": {
1253
+ GET: async (req: Request) => {
1254
+ const url = new URL(req.url);
1255
+ return withSecurityHeaders(
1256
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1257
+ (dependencies.handleDocs ?? handleDocs)(store, url)
1258
+ ),
1259
+ isDev
1260
+ );
1261
+ },
1262
+ POST: async (req: Request) => {
1263
+ if (!isRequestAllowed(req, port)) {
1264
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1265
+ }
1266
+ return withSecurityHeaders(
1267
+ await handleCreateDoc(ctxHolder, store, req),
1268
+ isDev
1269
+ );
1270
+ },
1271
+ },
1272
+ "/api/docs/autocomplete": {
1273
+ GET: async (req: Request) => {
1274
+ const url = new URL(req.url);
1275
+ return withSecurityHeaders(
1276
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1277
+ handleDocsAutocomplete(store, url)
1278
+ ),
1279
+ isDev
1280
+ );
1281
+ },
1282
+ },
1283
+ "/api/note-presets": {
1284
+ GET: async () =>
1285
+ withSecurityHeaders(await handleNotePresets(), isDev),
1286
+ },
1287
+ "/api/folders": {
1288
+ POST: async (req: Request) => {
1289
+ if (!isRequestAllowed(req, port)) {
1290
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1291
+ }
1292
+ return withSecurityHeaders(
1293
+ await handleCreateFolder(ctxHolder, req),
1294
+ isDev
1295
+ );
1296
+ },
1297
+ },
1298
+ "/api/browse/tree": {
1299
+ GET: async (req: Request) =>
1300
+ withSecurityHeaders(
1301
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1302
+ handleBrowseTree(store)
1303
+ ),
1304
+ isDev
981
1305
  ),
982
- isDev
983
- );
984
- },
985
- },
986
- "/api/sessions/automation/:id/preview": {
987
- GET: async (req: Request, server: RequestPeerServer) =>
988
- !isOriginOrTokenAllowed(req, port)
989
- ? withSecurityHeaders(forbiddenResponse(), isDev)
990
- : withSecurityHeaders(
991
- await handleResidentRead(
992
- runtime as ResidentRuntime,
993
- req,
994
- () =>
995
- handleSessionsAutomationPreview(
996
- ctxHolder,
997
- automationProfileId(req),
998
- req,
999
- { server }
1000
- )
1306
+ },
1307
+ "/api/docs/:id/deactivate": {
1308
+ POST: async (req: Request) => {
1309
+ if (!isRequestAllowed(req, port)) {
1310
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1311
+ }
1312
+ const url = new URL(req.url);
1313
+ // Extract id from /api/docs/:id/deactivate
1314
+ const parts = url.pathname.split("/");
1315
+ const id = decodeURIComponent(parts[3] || "");
1316
+ return withSecurityHeaders(
1317
+ await handleDeactivateDoc(ctxHolder, store, id, req),
1318
+ isDev
1319
+ );
1320
+ },
1321
+ },
1322
+ "/api/docs/:id/rename": {
1323
+ POST: async (req: Request) => {
1324
+ if (!isRequestAllowed(req, port)) {
1325
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1326
+ }
1327
+ const url = new URL(req.url);
1328
+ const parts = url.pathname.split("/");
1329
+ const id = decodeURIComponent(parts[3] || "");
1330
+ return withSecurityHeaders(
1331
+ await handleRenameDoc(ctxHolder, store, id, req),
1332
+ isDev
1333
+ );
1334
+ },
1335
+ },
1336
+ "/api/docs/:id/move": {
1337
+ POST: async (req: Request) => {
1338
+ if (!isRequestAllowed(req, port)) {
1339
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1340
+ }
1341
+ const url = new URL(req.url);
1342
+ const parts = url.pathname.split("/");
1343
+ const id = decodeURIComponent(parts[3] || "");
1344
+ return withSecurityHeaders(
1345
+ await handleMoveDoc(ctxHolder, store, id, req),
1346
+ isDev
1347
+ );
1348
+ },
1349
+ },
1350
+ "/api/docs/:id/duplicate": {
1351
+ POST: async (req: Request) => {
1352
+ if (!isRequestAllowed(req, port)) {
1353
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1354
+ }
1355
+ const url = new URL(req.url);
1356
+ const parts = url.pathname.split("/");
1357
+ const id = decodeURIComponent(parts[3] || "");
1358
+ return withSecurityHeaders(
1359
+ await handleDuplicateDoc(ctxHolder, store, id, req),
1360
+ isDev
1361
+ );
1362
+ },
1363
+ },
1364
+ "/api/docs/:id/refactor-plan": {
1365
+ POST: async (req: Request) => {
1366
+ if (!isRequestAllowed(req, port)) {
1367
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1368
+ }
1369
+ const url = new URL(req.url);
1370
+ const parts = url.pathname.split("/");
1371
+ const id = decodeURIComponent(parts[3] || "");
1372
+ return withSecurityHeaders(
1373
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1374
+ (dependencies.handleRefactorPlan ?? handleRefactorPlan)(
1375
+ ctxHolder,
1376
+ store,
1377
+ id,
1378
+ req
1379
+ )
1380
+ ),
1381
+ isDev
1382
+ );
1383
+ },
1384
+ },
1385
+ "/api/docs/:id/trash": {
1386
+ POST: async (req: Request) => {
1387
+ if (!isRequestAllowed(req, port)) {
1388
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1389
+ }
1390
+ const url = new URL(req.url);
1391
+ const parts = url.pathname.split("/");
1392
+ const id = decodeURIComponent(parts[3] || "");
1393
+ return withSecurityHeaders(
1394
+ await handleTrashDoc(ctxHolder, store, id, req),
1395
+ isDev
1396
+ );
1397
+ },
1398
+ },
1399
+ "/api/docs/:id/reveal": {
1400
+ POST: async (req: Request, server: RequestPeerServer) => {
1401
+ if (!isRequestAllowed(req, port)) {
1402
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1403
+ }
1404
+ const url = new URL(req.url);
1405
+ const parts = url.pathname.split("/");
1406
+ const id = decodeURIComponent(parts[3] || "");
1407
+ return withSecurityHeaders(
1408
+ await handleRevealDoc(ctxHolder, store, id, req, { server }),
1409
+ isDev
1410
+ );
1411
+ },
1412
+ },
1413
+ "/api/docs/:id/editable-copy": {
1414
+ POST: async (req: Request) => {
1415
+ if (!isRequestAllowed(req, port)) {
1416
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1417
+ }
1418
+ const url = new URL(req.url);
1419
+ const parts = url.pathname.split("/");
1420
+ const id = decodeURIComponent(parts[3] || "");
1421
+ return withSecurityHeaders(
1422
+ await handleCreateEditableCopy(ctxHolder, store, id, req),
1423
+ isDev
1424
+ );
1425
+ },
1426
+ },
1427
+ "/api/docs/:id": {
1428
+ PUT: async (req: Request) => {
1429
+ if (!isRequestAllowed(req, port)) {
1430
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1431
+ }
1432
+ const url = new URL(req.url);
1433
+ // Extract id from /api/docs/:id
1434
+ const id = decodeURIComponent(
1435
+ url.pathname.split("/").pop() || ""
1436
+ );
1437
+ return withSecurityHeaders(
1438
+ await handleUpdateDoc(ctxHolder, store, id, req),
1439
+ isDev
1440
+ );
1441
+ },
1442
+ },
1443
+ "/api/requests/:requestId": {
1444
+ GET: async (req: Request) => {
1445
+ const url = new URL(req.url);
1446
+ const requestId = decodeURIComponent(
1447
+ url.pathname.split("/").pop() || ""
1448
+ );
1449
+ return withSecurityHeaders(
1450
+ await handleRequestStatus(store, requestId),
1451
+ isDev
1452
+ );
1453
+ },
1454
+ },
1455
+ "/api/doc": {
1456
+ GET: async (req: Request) => {
1457
+ const url = new URL(req.url);
1458
+ return withSecurityHeaders(
1459
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1460
+ handleDoc(store, ctxHolder.config, url, req)
1461
+ ),
1462
+ isDev
1463
+ );
1464
+ },
1465
+ },
1466
+ // fn-112: production factories shared with route-level tests (I1-04)
1467
+ "/api/doc-asset": createDocAssetRouteHandlers({
1468
+ store,
1469
+ getConfig: () => ctxHolder.config,
1470
+ runtime: runtime as ResidentRuntime,
1471
+ isDev,
1472
+ withSecurityHeaders,
1473
+ }),
1474
+ // Vendor assets are NOT mounted as valid-only patterns here.
1475
+ // ALL /vendor/pdfjs/* traffic is handled by handlePdfjsVendorRequest
1476
+ // in the fetch fallback below (same production dispatcher tests use).
1477
+ "/api/events": {
1478
+ GET: (req: Request) => {
1479
+ const residentRuntime = runtime as ResidentRuntime;
1480
+ const admitted = residentRuntime.admitRequest(req.signal);
1481
+ if (!admitted || !residentRuntime.eventBus) {
1482
+ admitted?.finish();
1483
+ return withSecurityHeaders(
1484
+ Response.json(
1485
+ {
1486
+ error: {
1487
+ code: "UNAVAILABLE",
1488
+ message: "Document event stream unavailable",
1489
+ },
1490
+ },
1491
+ { status: 503 }
1001
1492
  ),
1002
1493
  isDev
1494
+ );
1495
+ }
1496
+ try {
1497
+ return withSecurityHeaders(
1498
+ residentRuntime.eventBus.createResponse({
1499
+ authorizationEpoch:
1500
+ admitted.authorizationEpoch ??
1501
+ residentRuntime.authorizationEpoch,
1502
+ isAuthorizationEpochCurrent: () =>
1503
+ admitted.isAuthorizationEpochCurrent?.() ?? true,
1504
+ onClose: () => admitted.finish(),
1505
+ signal: admitted.signal,
1506
+ }),
1507
+ isDev
1508
+ );
1509
+ } catch (error) {
1510
+ admitted.finish();
1511
+ throw error;
1512
+ }
1513
+ },
1514
+ },
1515
+ "/api/metadata": {
1516
+ GET: async (req: Request) => {
1517
+ const url = new URL(req.url);
1518
+ return withSecurityHeaders(
1519
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1520
+ handleMetadata(store, url)
1003
1521
  ),
1004
- },
1005
- "/api/sessions/automation/:id/enable": {
1006
- POST: async (req: Request, server: RequestPeerServer) => {
1007
- if (!isRequestAllowed(req, port)) {
1008
- return withSecurityHeaders(forbiddenResponse(), isDev);
1009
- }
1010
- return withSecurityHeaders(
1011
- await handleSessionsAutomationEnable(
1012
- ctxHolder,
1013
- store,
1014
- automationProfileId(req),
1015
- req,
1016
- { server }
1017
- ),
1018
- isDev
1019
- );
1020
- },
1021
- },
1022
- "/api/sessions/automation/:id/disable": {
1023
- POST: async (req: Request, server: RequestPeerServer) => {
1024
- if (!isRequestAllowed(req, port)) {
1025
- return withSecurityHeaders(forbiddenResponse(), isDev);
1026
- }
1027
- return withSecurityHeaders(
1028
- await handleSessionsAutomationDisable(
1029
- ctxHolder,
1030
- store,
1031
- automationProfileId(req),
1032
- req,
1033
- { server }
1034
- ),
1035
- isDev
1036
- );
1037
- },
1038
- },
1039
- "/api/sessions/init": {
1040
- POST: async (req: Request, server: RequestPeerServer) => {
1041
- if (!isRequestAllowed(req, port)) {
1042
- return withSecurityHeaders(forbiddenResponse(), isDev);
1043
- }
1044
- return withSecurityHeaders(
1045
- await handleSessionsInit(ctxHolder, store, req, { server }),
1046
- isDev
1047
- );
1048
- },
1049
- },
1050
- "/api/memory/remember": {
1051
- POST: async (req: Request) => {
1052
- if (!isRequestAllowed(req, port)) {
1053
- return withSecurityHeaders(forbiddenResponse(), isDev);
1054
- }
1055
- return withSecurityHeaders(
1056
- await handleMemoryRemember(ctxHolder, store, req),
1057
- isDev
1058
- );
1059
- },
1060
- },
1061
- "/api/memory/recall": {
1062
- POST: async (req: Request) => {
1063
- if (!isRequestAllowed(req, port)) {
1064
- return withSecurityHeaders(forbiddenResponse(), isDev);
1065
- }
1066
- return withSecurityHeaders(
1067
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1068
- handleMemoryRecall(ctxHolder, store, req)
1069
- ),
1070
- isDev
1071
- );
1072
- },
1073
- },
1074
- "/api/docs": {
1075
- GET: async (req: Request) => {
1076
- const url = new URL(req.url);
1077
- return withSecurityHeaders(
1078
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1079
- (dependencies.handleDocs ?? handleDocs)(store, url)
1080
- ),
1081
- isDev
1082
- );
1083
- },
1084
- POST: async (req: Request) => {
1085
- if (!isRequestAllowed(req, port)) {
1086
- return withSecurityHeaders(forbiddenResponse(), isDev);
1087
- }
1088
- return withSecurityHeaders(
1089
- await handleCreateDoc(ctxHolder, store, req),
1090
- isDev
1091
- );
1092
- },
1093
- },
1094
- "/api/docs/autocomplete": {
1095
- GET: async (req: Request) => {
1096
- const url = new URL(req.url);
1097
- return withSecurityHeaders(
1098
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1099
- handleDocsAutocomplete(store, url)
1522
+ isDev
1523
+ );
1524
+ },
1525
+ },
1526
+ "/api/tags": {
1527
+ GET: async (req: Request) => {
1528
+ const url = new URL(req.url);
1529
+ return withSecurityHeaders(
1530
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1531
+ handleTags(store, url)
1532
+ ),
1533
+ isDev
1534
+ );
1535
+ },
1536
+ },
1537
+ "/api/search": {
1538
+ POST: async (req: Request) => {
1539
+ if (!isRequestAllowed(req, port)) {
1540
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1541
+ }
1542
+ return withSecurityHeaders(
1543
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1544
+ handleSearch(ctxHolder.current, req)
1545
+ ),
1546
+ isDev
1547
+ );
1548
+ },
1549
+ },
1550
+ "/api/query": {
1551
+ POST: async (req: Request) => {
1552
+ if (!isRequestAllowed(req, port)) {
1553
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1554
+ }
1555
+ return withSecurityHeaders(
1556
+ await handleResidentRead(
1557
+ runtime as ResidentRuntime,
1558
+ req,
1559
+ (signal) =>
1560
+ handleQuery(ctxHolder.current, new Request(req, { signal }))
1561
+ ),
1562
+ isDev
1563
+ );
1564
+ },
1565
+ },
1566
+ "/api/context": {
1567
+ POST: async (req: Request) => {
1568
+ if (!isRequestAllowed(req, port)) {
1569
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1570
+ }
1571
+ return withSecurityHeaders(
1572
+ await handleResidentRead(
1573
+ runtime as ResidentRuntime,
1574
+ req,
1575
+ (signal) =>
1576
+ handleContextBuild(
1577
+ ctxHolder.current,
1578
+ new Request(req, { signal })
1579
+ )
1580
+ ),
1581
+ isDev
1582
+ );
1583
+ },
1584
+ },
1585
+ "/api/context/compiled/preview": {
1586
+ POST: async (req: Request, server: RequestPeerServer) => {
1587
+ if (!isRequestAllowed(req, port))
1588
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1589
+ return withSecurityHeaders(
1590
+ await handleResidentRead(
1591
+ runtime as ResidentRuntime,
1592
+ req,
1593
+ (signal) =>
1594
+ handleCompiledContext(
1595
+ ctxHolder.current,
1596
+ new Request(req, { signal }),
1597
+ { requestIP: () => server.requestIP(req) },
1598
+ "preview"
1599
+ )
1600
+ ),
1601
+ isDev
1602
+ );
1603
+ },
1604
+ },
1605
+ "/api/context/compiled/check": {
1606
+ POST: async (req: Request, server: RequestPeerServer) => {
1607
+ if (!isRequestAllowed(req, port))
1608
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1609
+ return withSecurityHeaders(
1610
+ await handleResidentRead(
1611
+ runtime as ResidentRuntime,
1612
+ req,
1613
+ (signal) =>
1614
+ handleCompiledContext(
1615
+ ctxHolder.current,
1616
+ new Request(req, { signal }),
1617
+ { requestIP: () => server.requestIP(req) },
1618
+ "check"
1619
+ )
1620
+ ),
1621
+ isDev
1622
+ );
1623
+ },
1624
+ },
1625
+ "/api/context/verify": {
1626
+ POST: async (req: Request) => {
1627
+ if (!isRequestAllowed(req, port)) {
1628
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1629
+ }
1630
+ return withSecurityHeaders(
1631
+ await handleResidentRead(
1632
+ runtime as ResidentRuntime,
1633
+ req,
1634
+ (signal) =>
1635
+ handleContextVerify(
1636
+ ctxHolder.current,
1637
+ new Request(req, { signal })
1638
+ )
1639
+ ),
1640
+ isDev
1641
+ );
1642
+ },
1643
+ },
1644
+ "/api/query/diagnose": {
1645
+ POST: async (req: Request) => {
1646
+ if (!isRequestAllowed(req, port)) {
1647
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1648
+ }
1649
+ return withSecurityHeaders(
1650
+ await handleResidentRead(
1651
+ runtime as ResidentRuntime,
1652
+ req,
1653
+ (signal) =>
1654
+ handleQueryDiagnose(
1655
+ ctxHolder.current,
1656
+ new Request(req, { signal })
1657
+ )
1658
+ ),
1659
+ isDev
1660
+ );
1661
+ },
1662
+ },
1663
+ "/api/ask": {
1664
+ POST: async (req: Request) => {
1665
+ if (!isRequestAllowed(req, port)) {
1666
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1667
+ }
1668
+ return withSecurityHeaders(
1669
+ await handleResidentRead(
1670
+ runtime as ResidentRuntime,
1671
+ req,
1672
+ (signal) =>
1673
+ handleAsk(ctxHolder.current, new Request(req, { signal }))
1674
+ ),
1675
+ isDev
1676
+ );
1677
+ },
1678
+ },
1679
+ "/api/capabilities": {
1680
+ GET: (req: Request, server: RequestPeerServer) =>
1681
+ withSecurityHeaders(
1682
+ handleCapabilities(ctxHolder.current, req, server),
1683
+ isDev
1100
1684
  ),
1101
- isDev
1102
- );
1103
- },
1104
- },
1105
- "/api/note-presets": {
1106
- GET: async () =>
1107
- withSecurityHeaders(await handleNotePresets(), isDev),
1108
- },
1109
- "/api/folders": {
1110
- POST: async (req: Request) => {
1111
- if (!isRequestAllowed(req, port)) {
1112
- return withSecurityHeaders(forbiddenResponse(), isDev);
1113
- }
1114
- return withSecurityHeaders(
1115
- await handleCreateFolder(ctxHolder, req),
1116
- isDev
1117
- );
1118
- },
1119
- },
1120
- "/api/browse/tree": {
1121
- GET: async (req: Request) =>
1122
- withSecurityHeaders(
1123
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1124
- handleBrowseTree(store)
1685
+ },
1686
+ "/api/presets": {
1687
+ GET: () =>
1688
+ withSecurityHeaders(handlePresets(ctxHolder.current), isDev),
1689
+ POST: async (req: Request) => {
1690
+ if (!isRequestAllowed(req, port)) {
1691
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1692
+ }
1693
+ return withSecurityHeaders(
1694
+ await handleSetPreset(ctxHolder, req),
1695
+ isDev
1696
+ );
1697
+ },
1698
+ },
1699
+ "/api/models/status": {
1700
+ GET: () => withSecurityHeaders(handleModelStatus(), isDev),
1701
+ },
1702
+ "/api/models/pull": {
1703
+ POST: async (req: Request) => {
1704
+ if (!isRequestAllowed(req, port)) {
1705
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1706
+ }
1707
+ return withSecurityHeaders(
1708
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1709
+ handleModelPull(ctxHolder)
1710
+ ),
1711
+ isDev
1712
+ );
1713
+ },
1714
+ },
1715
+ "/api/jobs/active": {
1716
+ GET: () =>
1717
+ withSecurityHeaders(handleActiveJob(ctxHolder.jobManager), isDev),
1718
+ },
1719
+ "/api/jobs/:id": {
1720
+ GET: (req: Request) => {
1721
+ const url = new URL(req.url);
1722
+ const id = decodeURIComponent(
1723
+ url.pathname.split("/").pop() || ""
1724
+ );
1725
+ return withSecurityHeaders(
1726
+ handleJob(id, ctxHolder.jobManager),
1727
+ isDev
1728
+ );
1729
+ },
1730
+ },
1731
+ "/api/embed": {
1732
+ POST: async (req: Request) => {
1733
+ if (!isRequestAllowed(req, port)) {
1734
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1735
+ }
1736
+ return withSecurityHeaders(
1737
+ await handleEmbed(ctxHolder.scheduler),
1738
+ isDev
1739
+ );
1740
+ },
1741
+ },
1742
+ "/api/embed/status": {
1743
+ GET: () =>
1744
+ withSecurityHeaders(
1745
+ handleEmbedStatus(ctxHolder.scheduler),
1746
+ isDev
1125
1747
  ),
1126
- isDev
1127
- ),
1128
- },
1129
- "/api/docs/:id/deactivate": {
1130
- POST: async (req: Request) => {
1131
- if (!isRequestAllowed(req, port)) {
1132
- return withSecurityHeaders(forbiddenResponse(), isDev);
1133
- }
1134
- const url = new URL(req.url);
1135
- // Extract id from /api/docs/:id/deactivate
1136
- const parts = url.pathname.split("/");
1137
- const id = decodeURIComponent(parts[3] || "");
1138
- return withSecurityHeaders(
1139
- await handleDeactivateDoc(ctxHolder, store, id, req),
1140
- isDev
1141
- );
1142
- },
1143
- },
1144
- "/api/docs/:id/rename": {
1145
- POST: async (req: Request) => {
1146
- if (!isRequestAllowed(req, port)) {
1147
- return withSecurityHeaders(forbiddenResponse(), isDev);
1148
- }
1149
- const url = new URL(req.url);
1150
- const parts = url.pathname.split("/");
1151
- const id = decodeURIComponent(parts[3] || "");
1152
- return withSecurityHeaders(
1153
- await handleRenameDoc(ctxHolder, store, id, req),
1154
- isDev
1155
- );
1156
- },
1157
- },
1158
- "/api/docs/:id/move": {
1159
- POST: async (req: Request) => {
1160
- if (!isRequestAllowed(req, port)) {
1161
- return withSecurityHeaders(forbiddenResponse(), isDev);
1162
- }
1163
- const url = new URL(req.url);
1164
- const parts = url.pathname.split("/");
1165
- const id = decodeURIComponent(parts[3] || "");
1166
- return withSecurityHeaders(
1167
- await handleMoveDoc(ctxHolder, store, id, req),
1168
- isDev
1169
- );
1170
- },
1171
- },
1172
- "/api/docs/:id/duplicate": {
1173
- POST: async (req: Request) => {
1174
- if (!isRequestAllowed(req, port)) {
1175
- return withSecurityHeaders(forbiddenResponse(), isDev);
1176
- }
1177
- const url = new URL(req.url);
1178
- const parts = url.pathname.split("/");
1179
- const id = decodeURIComponent(parts[3] || "");
1180
- return withSecurityHeaders(
1181
- await handleDuplicateDoc(ctxHolder, store, id, req),
1182
- isDev
1183
- );
1184
- },
1185
- },
1186
- "/api/docs/:id/refactor-plan": {
1187
- POST: async (req: Request) => {
1188
- if (!isRequestAllowed(req, port)) {
1189
- return withSecurityHeaders(forbiddenResponse(), isDev);
1190
- }
1191
- const url = new URL(req.url);
1192
- const parts = url.pathname.split("/");
1193
- const id = decodeURIComponent(parts[3] || "");
1194
- return withSecurityHeaders(
1195
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1196
- (dependencies.handleRefactorPlan ?? handleRefactorPlan)(
1748
+ },
1749
+ "/api/collections/:name": {
1750
+ PATCH: async (req: Request) => {
1751
+ if (!isRequestAllowed(req, port)) {
1752
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1753
+ }
1754
+ const url = new URL(req.url);
1755
+ const name = decodeURIComponent(
1756
+ url.pathname.split("/").pop() || ""
1757
+ );
1758
+ return withSecurityHeaders(
1759
+ await handleUpdateCollection(ctxHolder, store, name, req),
1760
+ isDev
1761
+ );
1762
+ },
1763
+ DELETE: async (req: Request) => {
1764
+ if (!isRequestAllowed(req, port)) {
1765
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1766
+ }
1767
+ const url = new URL(req.url);
1768
+ const name = decodeURIComponent(
1769
+ url.pathname.split("/").pop() || ""
1770
+ );
1771
+ return withSecurityHeaders(
1772
+ await handleDeleteCollection(ctxHolder, store, name),
1773
+ isDev
1774
+ );
1775
+ },
1776
+ },
1777
+ "/api/collections/:name/egress-policy": {
1778
+ GET: (req: Request) => {
1779
+ const parts = new URL(req.url).pathname.split("/");
1780
+ const name = decodeURIComponent(parts.at(-2) ?? "");
1781
+ return withSecurityHeaders(
1782
+ handleCollectionEgressPolicy(ctxHolder, name),
1783
+ isDev
1784
+ );
1785
+ },
1786
+ PUT: async (req: Request) => {
1787
+ if (!isRequestAllowed(req, port)) {
1788
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1789
+ }
1790
+ const parts = new URL(req.url).pathname.split("/");
1791
+ const name = decodeURIComponent(parts.at(-2) ?? "");
1792
+ return withSecurityHeaders(
1793
+ await handleUpdateCollectionEgressPolicy(
1197
1794
  ctxHolder,
1198
1795
  store,
1199
- id,
1796
+ name,
1200
1797
  req
1201
- )
1202
- ),
1203
- isDev
1204
- );
1205
- },
1206
- },
1207
- "/api/docs/:id/trash": {
1208
- POST: async (req: Request) => {
1209
- if (!isRequestAllowed(req, port)) {
1210
- return withSecurityHeaders(forbiddenResponse(), isDev);
1211
- }
1212
- const url = new URL(req.url);
1213
- const parts = url.pathname.split("/");
1214
- const id = decodeURIComponent(parts[3] || "");
1215
- return withSecurityHeaders(
1216
- await handleTrashDoc(ctxHolder, store, id, req),
1217
- isDev
1218
- );
1219
- },
1220
- },
1221
- "/api/docs/:id/reveal": {
1222
- POST: async (req: Request, server: RequestPeerServer) => {
1223
- if (!isRequestAllowed(req, port)) {
1224
- return withSecurityHeaders(forbiddenResponse(), isDev);
1225
- }
1226
- const url = new URL(req.url);
1227
- const parts = url.pathname.split("/");
1228
- const id = decodeURIComponent(parts[3] || "");
1229
- return withSecurityHeaders(
1230
- await handleRevealDoc(ctxHolder, store, id, req, { server }),
1231
- isDev
1232
- );
1233
- },
1234
- },
1235
- "/api/docs/:id/editable-copy": {
1236
- POST: async (req: Request) => {
1237
- if (!isRequestAllowed(req, port)) {
1238
- return withSecurityHeaders(forbiddenResponse(), isDev);
1239
- }
1240
- const url = new URL(req.url);
1241
- const parts = url.pathname.split("/");
1242
- const id = decodeURIComponent(parts[3] || "");
1243
- return withSecurityHeaders(
1244
- await handleCreateEditableCopy(ctxHolder, store, id, req),
1245
- isDev
1246
- );
1247
- },
1248
- },
1249
- "/api/docs/:id": {
1250
- PUT: async (req: Request) => {
1251
- if (!isRequestAllowed(req, port)) {
1252
- return withSecurityHeaders(forbiddenResponse(), isDev);
1253
- }
1254
- const url = new URL(req.url);
1255
- // Extract id from /api/docs/:id
1256
- const id = decodeURIComponent(url.pathname.split("/").pop() || "");
1257
- return withSecurityHeaders(
1258
- await handleUpdateDoc(ctxHolder, store, id, req),
1259
- isDev
1260
- );
1261
- },
1262
- },
1263
- "/api/requests/:requestId": {
1264
- GET: async (req: Request) => {
1265
- const url = new URL(req.url);
1266
- const requestId = decodeURIComponent(
1267
- url.pathname.split("/").pop() || ""
1268
- );
1269
- return withSecurityHeaders(
1270
- await handleRequestStatus(store, requestId),
1271
- isDev
1272
- );
1273
- },
1274
- },
1275
- "/api/doc": {
1276
- GET: async (req: Request) => {
1277
- const url = new URL(req.url);
1278
- return withSecurityHeaders(
1279
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1280
- handleDoc(store, ctxHolder.config, url, req)
1281
- ),
1282
- isDev
1283
- );
1284
- },
1285
- },
1286
- // fn-112: production factories shared with route-level tests (I1-04)
1287
- "/api/doc-asset": createDocAssetRouteHandlers({
1288
- store,
1289
- getConfig: () => ctxHolder.config,
1290
- runtime: runtime as ResidentRuntime,
1291
- isDev,
1292
- withSecurityHeaders,
1293
- }),
1294
- // Vendor assets are NOT mounted as valid-only patterns here.
1295
- // ALL /vendor/pdfjs/* traffic is handled by handlePdfjsVendorRequest
1296
- // in the fetch fallback below (same production dispatcher tests use).
1297
- "/api/events": {
1298
- GET: (req: Request) => {
1299
- const residentRuntime = runtime as ResidentRuntime;
1300
- const admitted = residentRuntime.admitRequest(req.signal);
1301
- if (!admitted || !residentRuntime.eventBus) {
1302
- admitted?.finish();
1303
- return withSecurityHeaders(
1304
- Response.json(
1305
- {
1306
- error: {
1307
- code: "UNAVAILABLE",
1308
- message: "Document event stream unavailable",
1309
- },
1310
- },
1311
- { status: 503 }
1312
1798
  ),
1313
1799
  isDev
1314
1800
  );
1315
- }
1316
- try {
1801
+ },
1802
+ },
1803
+ "/api/collections/:name/embeddings/clear": {
1804
+ POST: async (req: Request) => {
1805
+ if (!isRequestAllowed(req, port)) {
1806
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1807
+ }
1808
+ const url = new URL(req.url);
1809
+ const parts = url.pathname.split("/");
1810
+ const name = decodeURIComponent(parts.at(-3) || "");
1317
1811
  return withSecurityHeaders(
1318
- residentRuntime.eventBus.createResponse({
1319
- authorizationEpoch:
1320
- admitted.authorizationEpoch ??
1321
- residentRuntime.authorizationEpoch,
1322
- isAuthorizationEpochCurrent: () =>
1323
- admitted.isAuthorizationEpochCurrent?.() ?? true,
1324
- onClose: () => admitted.finish(),
1325
- signal: admitted.signal,
1326
- }),
1812
+ await handleClearCollectionEmbeddings(
1813
+ ctxHolder,
1814
+ store,
1815
+ name,
1816
+ req
1817
+ ),
1818
+ isDev
1819
+ );
1820
+ },
1821
+ },
1822
+ "/api/doc/:id/links": {
1823
+ GET: async (req: Request) => {
1824
+ const url = new URL(req.url);
1825
+ // Extract id from /api/doc/:id/links
1826
+ const parts = url.pathname.split("/");
1827
+ const id = decodeURIComponent(parts[3] || "");
1828
+ return withSecurityHeaders(
1829
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1830
+ handleDocLinks(store, id, url)
1831
+ ),
1327
1832
  isDev
1328
1833
  );
1329
- } catch (error) {
1330
- admitted.finish();
1331
- throw error;
1332
- }
1834
+ },
1333
1835
  },
1334
- },
1335
- "/api/metadata": {
1336
- GET: async (req: Request) => {
1337
- const url = new URL(req.url);
1338
- return withSecurityHeaders(
1339
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1340
- handleMetadata(store, url)
1341
- ),
1342
- isDev
1343
- );
1344
- },
1345
- },
1346
- "/api/tags": {
1347
- GET: async (req: Request) => {
1348
- const url = new URL(req.url);
1349
- return withSecurityHeaders(
1350
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1351
- handleTags(store, url)
1352
- ),
1353
- isDev
1354
- );
1355
- },
1356
- },
1357
- "/api/search": {
1358
- POST: async (req: Request) => {
1359
- if (!isRequestAllowed(req, port)) {
1360
- return withSecurityHeaders(forbiddenResponse(), isDev);
1361
- }
1362
- return withSecurityHeaders(
1363
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1364
- handleSearch(ctxHolder.current, req)
1365
- ),
1366
- isDev
1367
- );
1368
- },
1369
- },
1370
- "/api/query": {
1371
- POST: async (req: Request) => {
1372
- if (!isRequestAllowed(req, port)) {
1373
- return withSecurityHeaders(forbiddenResponse(), isDev);
1374
- }
1375
- return withSecurityHeaders(
1376
- await handleResidentRead(
1377
- runtime as ResidentRuntime,
1378
- req,
1379
- (signal) =>
1380
- handleQuery(ctxHolder.current, new Request(req, { signal }))
1381
- ),
1382
- isDev
1383
- );
1384
- },
1385
- },
1386
- "/api/context": {
1387
- POST: async (req: Request) => {
1388
- if (!isRequestAllowed(req, port)) {
1389
- return withSecurityHeaders(forbiddenResponse(), isDev);
1390
- }
1391
- return withSecurityHeaders(
1392
- await handleResidentRead(
1393
- runtime as ResidentRuntime,
1394
- req,
1395
- (signal) =>
1396
- handleContextBuild(
1397
- ctxHolder.current,
1398
- new Request(req, { signal })
1399
- )
1400
- ),
1401
- isDev
1402
- );
1403
- },
1404
- },
1405
- "/api/context/compiled/preview": {
1406
- POST: async (req: Request, server: RequestPeerServer) => {
1407
- if (!isRequestAllowed(req, port))
1408
- return withSecurityHeaders(forbiddenResponse(), isDev);
1409
- return withSecurityHeaders(
1410
- await handleResidentRead(
1411
- runtime as ResidentRuntime,
1412
- req,
1413
- (signal) =>
1414
- handleCompiledContext(
1415
- ctxHolder.current,
1416
- new Request(req, { signal }),
1417
- { requestIP: () => server.requestIP(req) },
1418
- "preview"
1419
- )
1420
- ),
1421
- isDev
1422
- );
1423
- },
1424
- },
1425
- "/api/context/compiled/check": {
1426
- POST: async (req: Request, server: RequestPeerServer) => {
1427
- if (!isRequestAllowed(req, port))
1428
- return withSecurityHeaders(forbiddenResponse(), isDev);
1429
- return withSecurityHeaders(
1430
- await handleResidentRead(
1431
- runtime as ResidentRuntime,
1432
- req,
1433
- (signal) =>
1434
- handleCompiledContext(
1435
- ctxHolder.current,
1436
- new Request(req, { signal }),
1437
- { requestIP: () => server.requestIP(req) },
1438
- "check"
1439
- )
1440
- ),
1441
- isDev
1442
- );
1443
- },
1444
- },
1445
- "/api/context/verify": {
1446
- POST: async (req: Request) => {
1447
- if (!isRequestAllowed(req, port)) {
1448
- return withSecurityHeaders(forbiddenResponse(), isDev);
1449
- }
1450
- return withSecurityHeaders(
1451
- await handleResidentRead(
1452
- runtime as ResidentRuntime,
1453
- req,
1454
- (signal) =>
1455
- handleContextVerify(
1456
- ctxHolder.current,
1457
- new Request(req, { signal })
1458
- )
1459
- ),
1460
- isDev
1461
- );
1462
- },
1463
- },
1464
- "/api/query/diagnose": {
1465
- POST: async (req: Request) => {
1466
- if (!isRequestAllowed(req, port)) {
1467
- return withSecurityHeaders(forbiddenResponse(), isDev);
1468
- }
1469
- return withSecurityHeaders(
1470
- await handleResidentRead(
1471
- runtime as ResidentRuntime,
1472
- req,
1473
- (signal) =>
1474
- handleQueryDiagnose(
1475
- ctxHolder.current,
1476
- new Request(req, { signal })
1836
+ "/api/doc/:id/sections": {
1837
+ GET: async (req: Request) => {
1838
+ const url = new URL(req.url);
1839
+ const parts = url.pathname.split("/");
1840
+ const id = decodeURIComponent(parts[3] || "");
1841
+ return withSecurityHeaders(
1842
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1843
+ handleDocSections(store, id, req)
1844
+ ),
1845
+ isDev
1846
+ );
1847
+ },
1848
+ },
1849
+ "/api/doc/:id/section-targets": {
1850
+ POST: async (req: Request) => {
1851
+ if (!isRequestAllowed(req, port)) {
1852
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1853
+ }
1854
+ const parts = new URL(req.url).pathname.split("/");
1855
+ const id = decodeURIComponent(parts[3] || "");
1856
+ return withSecurityHeaders(
1857
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1858
+ handleCreateSectionTarget(store, id, req)
1859
+ ),
1860
+ isDev
1861
+ );
1862
+ },
1863
+ },
1864
+ "/api/doc/:id/section-targets/resolve": {
1865
+ POST: async (req: Request) => {
1866
+ if (!isRequestAllowed(req, port)) {
1867
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1868
+ }
1869
+ const parts = new URL(req.url).pathname.split("/");
1870
+ const id = decodeURIComponent(parts[3] || "");
1871
+ return withSecurityHeaders(
1872
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1873
+ handleResolveSectionTarget(store, id, req)
1874
+ ),
1875
+ isDev
1876
+ );
1877
+ },
1878
+ },
1879
+ "/api/doc/:id/backlinks": {
1880
+ GET: async (req: Request) => {
1881
+ const url = new URL(req.url);
1882
+ // Extract id from /api/doc/:id/backlinks
1883
+ const parts = url.pathname.split("/");
1884
+ const id = decodeURIComponent(parts[3] || "");
1885
+ return withSecurityHeaders(
1886
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1887
+ handleDocBacklinks(store, id)
1888
+ ),
1889
+ isDev
1890
+ );
1891
+ },
1892
+ },
1893
+ "/api/doc/:id/similar": {
1894
+ GET: async (req: Request) => {
1895
+ const url = new URL(req.url);
1896
+ // Extract id from /api/doc/:id/similar
1897
+ const parts = url.pathname.split("/");
1898
+ const id = decodeURIComponent(parts[3] || "");
1899
+ return withSecurityHeaders(
1900
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1901
+ handleDocSimilar(ctxHolder.current, id, url)
1902
+ ),
1903
+ isDev
1904
+ );
1905
+ },
1906
+ },
1907
+ "/api/graph": {
1908
+ GET: async (req: Request) => {
1909
+ const url = new URL(req.url);
1910
+ return withSecurityHeaders(
1911
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1912
+ handleGraph(
1913
+ store,
1914
+ url,
1915
+ getActivePreset(ctxHolder.config).embed
1477
1916
  )
1478
- ),
1479
- isDev
1480
- );
1481
- },
1482
- },
1483
- "/api/ask": {
1484
- POST: async (req: Request) => {
1485
- if (!isRequestAllowed(req, port)) {
1486
- return withSecurityHeaders(forbiddenResponse(), isDev);
1487
- }
1488
- return withSecurityHeaders(
1489
- await handleResidentRead(
1490
- runtime as ResidentRuntime,
1491
- req,
1492
- (signal) =>
1493
- handleAsk(ctxHolder.current, new Request(req, { signal }))
1494
- ),
1495
- isDev
1496
- );
1497
- },
1498
- },
1499
- "/api/capabilities": {
1500
- GET: (req: Request, server: RequestPeerServer) =>
1501
- withSecurityHeaders(
1502
- handleCapabilities(ctxHolder.current, req, server),
1503
- isDev
1504
- ),
1505
- },
1506
- "/api/presets": {
1507
- GET: () =>
1508
- withSecurityHeaders(handlePresets(ctxHolder.current), isDev),
1509
- POST: async (req: Request) => {
1510
- if (!isRequestAllowed(req, port)) {
1511
- return withSecurityHeaders(forbiddenResponse(), isDev);
1512
- }
1513
- return withSecurityHeaders(
1514
- await handleSetPreset(ctxHolder, req),
1515
- isDev
1516
- );
1517
- },
1518
- },
1519
- "/api/models/status": {
1520
- GET: () => withSecurityHeaders(handleModelStatus(), isDev),
1521
- },
1522
- "/api/models/pull": {
1523
- POST: async (req: Request) => {
1524
- if (!isRequestAllowed(req, port)) {
1525
- return withSecurityHeaders(forbiddenResponse(), isDev);
1526
- }
1527
- return withSecurityHeaders(
1528
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1529
- handleModelPull(ctxHolder)
1530
- ),
1531
- isDev
1532
- );
1533
- },
1534
- },
1535
- "/api/jobs/active": {
1536
- GET: () =>
1537
- withSecurityHeaders(handleActiveJob(ctxHolder.jobManager), isDev),
1538
- },
1539
- "/api/jobs/:id": {
1540
- GET: (req: Request) => {
1541
- const url = new URL(req.url);
1542
- const id = decodeURIComponent(url.pathname.split("/").pop() || "");
1543
- return withSecurityHeaders(
1544
- handleJob(id, ctxHolder.jobManager),
1545
- isDev
1546
- );
1547
- },
1548
- },
1549
- "/api/embed": {
1550
- POST: async (req: Request) => {
1551
- if (!isRequestAllowed(req, port)) {
1552
- return withSecurityHeaders(forbiddenResponse(), isDev);
1553
- }
1554
- return withSecurityHeaders(
1555
- await handleEmbed(ctxHolder.scheduler),
1556
- isDev
1557
- );
1558
- },
1559
- },
1560
- "/api/embed/status": {
1561
- GET: () =>
1562
- withSecurityHeaders(handleEmbedStatus(ctxHolder.scheduler), isDev),
1563
- },
1564
- "/api/collections/:name": {
1565
- PATCH: async (req: Request) => {
1566
- if (!isRequestAllowed(req, port)) {
1567
- return withSecurityHeaders(forbiddenResponse(), isDev);
1568
- }
1569
- const url = new URL(req.url);
1570
- const name = decodeURIComponent(
1571
- url.pathname.split("/").pop() || ""
1572
- );
1573
- return withSecurityHeaders(
1574
- await handleUpdateCollection(ctxHolder, store, name, req),
1575
- isDev
1576
- );
1577
- },
1578
- DELETE: async (req: Request) => {
1579
- if (!isRequestAllowed(req, port)) {
1580
- return withSecurityHeaders(forbiddenResponse(), isDev);
1581
- }
1582
- const url = new URL(req.url);
1583
- const name = decodeURIComponent(
1584
- url.pathname.split("/").pop() || ""
1585
- );
1586
- return withSecurityHeaders(
1587
- await handleDeleteCollection(ctxHolder, store, name),
1588
- isDev
1589
- );
1590
- },
1591
- },
1592
- "/api/collections/:name/egress-policy": {
1593
- GET: (req: Request) => {
1594
- const parts = new URL(req.url).pathname.split("/");
1595
- const name = decodeURIComponent(parts.at(-2) ?? "");
1596
- return withSecurityHeaders(
1597
- handleCollectionEgressPolicy(ctxHolder, name),
1598
- isDev
1599
- );
1600
- },
1601
- PUT: async (req: Request) => {
1602
- if (!isRequestAllowed(req, port)) {
1603
- return withSecurityHeaders(forbiddenResponse(), isDev);
1604
- }
1605
- const parts = new URL(req.url).pathname.split("/");
1606
- const name = decodeURIComponent(parts.at(-2) ?? "");
1607
- return withSecurityHeaders(
1608
- await handleUpdateCollectionEgressPolicy(
1609
- ctxHolder,
1610
- store,
1611
- name,
1612
- req
1613
- ),
1614
- isDev
1615
- );
1616
- },
1617
- },
1618
- "/api/collections/:name/embeddings/clear": {
1619
- POST: async (req: Request) => {
1620
- if (!isRequestAllowed(req, port)) {
1621
- return withSecurityHeaders(forbiddenResponse(), isDev);
1622
- }
1623
- const url = new URL(req.url);
1624
- const parts = url.pathname.split("/");
1625
- const name = decodeURIComponent(parts.at(-3) || "");
1626
- return withSecurityHeaders(
1627
- await handleClearCollectionEmbeddings(
1628
- ctxHolder,
1629
- store,
1630
- name,
1631
- req
1632
- ),
1633
- isDev
1634
- );
1635
- },
1636
- },
1637
- "/api/doc/:id/links": {
1638
- GET: async (req: Request) => {
1639
- const url = new URL(req.url);
1640
- // Extract id from /api/doc/:id/links
1641
- const parts = url.pathname.split("/");
1642
- const id = decodeURIComponent(parts[3] || "");
1643
- return withSecurityHeaders(
1644
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1645
- handleDocLinks(store, id, url)
1646
- ),
1647
- isDev
1648
- );
1649
- },
1650
- },
1651
- "/api/doc/:id/sections": {
1652
- GET: async (req: Request) => {
1653
- const url = new URL(req.url);
1654
- const parts = url.pathname.split("/");
1655
- const id = decodeURIComponent(parts[3] || "");
1656
- return withSecurityHeaders(
1657
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1658
- handleDocSections(store, id, req)
1659
- ),
1660
- isDev
1661
- );
1662
- },
1663
- },
1664
- "/api/doc/:id/section-targets": {
1665
- POST: async (req: Request) => {
1666
- if (!isRequestAllowed(req, port)) {
1667
- return withSecurityHeaders(forbiddenResponse(), isDev);
1668
- }
1669
- const parts = new URL(req.url).pathname.split("/");
1670
- const id = decodeURIComponent(parts[3] || "");
1671
- return withSecurityHeaders(
1672
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1673
- handleCreateSectionTarget(store, id, req)
1674
- ),
1675
- isDev
1676
- );
1677
- },
1678
- },
1679
- "/api/doc/:id/section-targets/resolve": {
1680
- POST: async (req: Request) => {
1681
- if (!isRequestAllowed(req, port)) {
1682
- return withSecurityHeaders(forbiddenResponse(), isDev);
1683
- }
1684
- const parts = new URL(req.url).pathname.split("/");
1685
- const id = decodeURIComponent(parts[3] || "");
1686
- return withSecurityHeaders(
1687
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1688
- handleResolveSectionTarget(store, id, req)
1689
- ),
1690
- isDev
1691
- );
1692
- },
1693
- },
1694
- "/api/doc/:id/backlinks": {
1695
- GET: async (req: Request) => {
1696
- const url = new URL(req.url);
1697
- // Extract id from /api/doc/:id/backlinks
1698
- const parts = url.pathname.split("/");
1699
- const id = decodeURIComponent(parts[3] || "");
1700
- return withSecurityHeaders(
1701
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1702
- handleDocBacklinks(store, id)
1703
- ),
1704
- isDev
1705
- );
1706
- },
1707
- },
1708
- "/api/doc/:id/similar": {
1709
- GET: async (req: Request) => {
1710
- const url = new URL(req.url);
1711
- // Extract id from /api/doc/:id/similar
1712
- const parts = url.pathname.split("/");
1713
- const id = decodeURIComponent(parts[3] || "");
1714
- return withSecurityHeaders(
1715
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1716
- handleDocSimilar(ctxHolder.current, id, url)
1717
- ),
1718
- isDev
1719
- );
1720
- },
1721
- },
1722
- "/api/graph": {
1723
- GET: async (req: Request) => {
1724
- const url = new URL(req.url);
1725
- return withSecurityHeaders(
1726
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1727
- handleGraph(store, url, getActivePreset(ctxHolder.config).embed)
1728
- ),
1729
- isDev
1730
- );
1731
- },
1732
- },
1733
- "/api/graph/query": {
1734
- POST: async (req: Request) => {
1735
- if (!isRequestAllowed(req, port)) {
1736
- return withSecurityHeaders(forbiddenResponse(), isDev);
1737
- }
1738
- return withSecurityHeaders(
1739
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1740
- handleGraphQuery(store, ctxHolder.config, req)
1741
- ),
1742
- isDev
1743
- );
1744
- },
1745
- },
1746
- "/api/changes": {
1747
- GET: async (req: Request) => {
1748
- const url = new URL(req.url);
1749
- return withSecurityHeaders(
1750
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1751
- handleChanges(store, url)
1752
- ),
1753
- isDev
1754
- );
1755
- },
1756
- },
1757
- "/api/diff": {
1758
- GET: async (req: Request) => {
1759
- const url = new URL(req.url);
1760
- return withSecurityHeaders(
1761
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1762
- handleDiff(store, url)
1763
- ),
1764
- isDev
1765
- );
1766
- },
1767
- },
1768
- "/api/impact": {
1769
- GET: async (req: Request) => {
1770
- const url = new URL(req.url);
1771
- return withSecurityHeaders(
1772
- await handleResidentRead(runtime as ResidentRuntime, req, () =>
1773
- handleImpact(store, url)
1774
- ),
1775
- isDev
1776
- );
1917
+ ),
1918
+ isDev
1919
+ );
1920
+ },
1921
+ },
1922
+ "/api/graph/query": {
1923
+ POST: async (req: Request) => {
1924
+ if (!isRequestAllowed(req, port)) {
1925
+ return withSecurityHeaders(forbiddenResponse(), isDev);
1926
+ }
1927
+ return withSecurityHeaders(
1928
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1929
+ handleGraphQuery(store, ctxHolder.config, req)
1930
+ ),
1931
+ isDev
1932
+ );
1933
+ },
1934
+ },
1935
+ "/api/changes": {
1936
+ GET: async (req: Request) => {
1937
+ const url = new URL(req.url);
1938
+ return withSecurityHeaders(
1939
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1940
+ handleChanges(store, url)
1941
+ ),
1942
+ isDev
1943
+ );
1944
+ },
1945
+ },
1946
+ "/api/diff": {
1947
+ GET: async (req: Request) => {
1948
+ const url = new URL(req.url);
1949
+ return withSecurityHeaders(
1950
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1951
+ handleDiff(store, url)
1952
+ ),
1953
+ isDev
1954
+ );
1955
+ },
1956
+ },
1957
+ "/api/impact": {
1958
+ GET: async (req: Request) => {
1959
+ const url = new URL(req.url);
1960
+ return withSecurityHeaders(
1961
+ await handleResidentRead(runtime as ResidentRuntime, req, () =>
1962
+ handleImpact(store, url)
1963
+ ),
1964
+ isDev
1965
+ );
1966
+ },
1777
1967
  },
1778
- },
1779
- }),
1968
+ })
1969
+ ),
1780
1970
  // Production catch-all: /vendor/pdfjs prefix, then hashed SPA chunks
1781
1971
  // (gzip + immutable) and the private SPA source — the same factory the
1782
1972
  // tests mount (no test-only fallback path).