theokit 0.39.1 → 0.43.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 (67) hide show
  1. package/dist/{actions-virtual-module-RNPMHJOM.js → actions-virtual-module-LD3YKWIX.js} +2 -2
  2. package/dist/{actions-virtual-module-YGKSEIOJ.js → actions-virtual-module-N7GKUS3D.js} +3 -3
  3. package/dist/{agent-ST6GB3VX.js → agent-U35G3WNE.js} +2 -2
  4. package/dist/{app-typed-client-YSLQU5HT.js → app-typed-client-6YZRDAPY.js} +3 -3
  5. package/dist/{app-typed-client-FQPUOL6A.js → app-typed-client-V77SVU2O.js} +2 -2
  6. package/dist/{build-VAR5EFOU.js → build-BYLQZJT2.js} +2 -2
  7. package/dist/{chunk-L4CYLM2V.js → chunk-2ZX4J4SX.js} +11 -7
  8. package/dist/chunk-2ZX4J4SX.js.map +1 -0
  9. package/dist/{chunk-KLFCJQXU.js → chunk-AXVFVMLM.js} +17 -3
  10. package/dist/chunk-AXVFVMLM.js.map +1 -0
  11. package/dist/{chunk-63BUBV5L.js → chunk-E25FBYJP.js} +1 -1
  12. package/dist/chunk-E25FBYJP.js.map +1 -0
  13. package/dist/{chunk-YMS6KMKA.js → chunk-FEDSPOY6.js} +2 -2
  14. package/dist/{chunk-MYDS52BG.js → chunk-IPC5UREF.js} +13 -9
  15. package/dist/chunk-IPC5UREF.js.map +1 -0
  16. package/dist/{chunk-EV33DOG6.js → chunk-JQJN6LFK.js} +2 -2
  17. package/dist/{chunk-HQQADLLD.js → chunk-SPJNVWOK.js} +2 -2
  18. package/dist/{chunk-SPFMJAFW.js → chunk-UI6FWWOF.js} +2 -2
  19. package/dist/{chunk-HEDLJJDF.js → chunk-UNLG36Z5.js} +17 -3
  20. package/dist/{chunk-HEDLJJDF.js.map → chunk-UNLG36Z5.js.map} +1 -1
  21. package/dist/{chunk-BEQQ6WCK.js → chunk-UO7WS32S.js} +2 -2
  22. package/dist/{chunk-32GOXYA7.js → chunk-XZ4QD5IN.js} +7 -3
  23. package/dist/{chunk-32GOXYA7.js.map → chunk-XZ4QD5IN.js.map} +1 -1
  24. package/dist/cli/index.js +6 -6
  25. package/dist/client/core.js +1 -1
  26. package/dist/client/index.js +1 -1
  27. package/dist/{define-agent-tool-ChaGymol.d.ts → define-agent-tool-cRuSpidb.d.ts} +29 -9
  28. package/dist/{dev-VAIYPLV6.js → dev-PS2NGT6I.js} +5 -5
  29. package/dist/{generate-ATHMC4ET.js → generate-5X4LG77M.js} +12 -17
  30. package/dist/generate-5X4LG77M.js.map +1 -0
  31. package/dist/index.js +2 -2
  32. package/dist/{internal-api-ORXPQTEI.js → internal-api-FBFV265J.js} +3 -3
  33. package/dist/{internal-api-MRTUTAM7.js → internal-api-LFW7R65K.js} +2 -2
  34. package/dist/{mcp-SMDPAW7T.js → mcp-ML4QK5ON.js} +2 -2
  35. package/dist/server/agent/index.d.ts +1 -1
  36. package/dist/server/agent/index.js +2 -2
  37. package/dist/server/define/index.d.ts +2 -2
  38. package/dist/server/define/index.js +2 -2
  39. package/dist/server/index.d.ts +1 -1
  40. package/dist/server/index.js +3 -3
  41. package/dist/{start-MK3MNSFB.js → start-YLR2SFHU.js} +91 -10
  42. package/dist/start-YLR2SFHU.js.map +1 -0
  43. package/dist/vite-plugin/index.js +2 -2
  44. package/dist/{vite-plugin-WB57H4AT.js → vite-plugin-6VVU6PPC.js} +5 -5
  45. package/package.json +5 -4
  46. package/dist/chunk-63BUBV5L.js.map +0 -1
  47. package/dist/chunk-KLFCJQXU.js.map +0 -1
  48. package/dist/chunk-L4CYLM2V.js.map +0 -1
  49. package/dist/chunk-MYDS52BG.js.map +0 -1
  50. package/dist/generate-ATHMC4ET.js.map +0 -1
  51. package/dist/start-MK3MNSFB.js.map +0 -1
  52. /package/dist/{actions-virtual-module-RNPMHJOM.js.map → actions-virtual-module-LD3YKWIX.js.map} +0 -0
  53. /package/dist/{actions-virtual-module-YGKSEIOJ.js.map → actions-virtual-module-N7GKUS3D.js.map} +0 -0
  54. /package/dist/{agent-ST6GB3VX.js.map → agent-U35G3WNE.js.map} +0 -0
  55. /package/dist/{app-typed-client-YSLQU5HT.js.map → app-typed-client-6YZRDAPY.js.map} +0 -0
  56. /package/dist/{app-typed-client-FQPUOL6A.js.map → app-typed-client-V77SVU2O.js.map} +0 -0
  57. /package/dist/{build-VAR5EFOU.js.map → build-BYLQZJT2.js.map} +0 -0
  58. /package/dist/{chunk-YMS6KMKA.js.map → chunk-FEDSPOY6.js.map} +0 -0
  59. /package/dist/{chunk-EV33DOG6.js.map → chunk-JQJN6LFK.js.map} +0 -0
  60. /package/dist/{chunk-HQQADLLD.js.map → chunk-SPJNVWOK.js.map} +0 -0
  61. /package/dist/{chunk-SPFMJAFW.js.map → chunk-UI6FWWOF.js.map} +0 -0
  62. /package/dist/{chunk-BEQQ6WCK.js.map → chunk-UO7WS32S.js.map} +0 -0
  63. /package/dist/{dev-VAIYPLV6.js.map → dev-PS2NGT6I.js.map} +0 -0
  64. /package/dist/{internal-api-MRTUTAM7.js.map → internal-api-FBFV265J.js.map} +0 -0
  65. /package/dist/{internal-api-ORXPQTEI.js.map → internal-api-LFW7R65K.js.map} +0 -0
  66. /package/dist/{mcp-SMDPAW7T.js.map → mcp-ML4QK5ON.js.map} +0 -0
  67. /package/dist/{vite-plugin-WB57H4AT.js.map → vite-plugin-6VVU6PPC.js.map} +0 -0
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/vite-plugin/index.ts","../src/vite-plugin/config-hook.ts","../src/config/load-config.ts","../src/config/errors.ts","../src/config/schema.ts","../src/config/schemas/header-safe.ts","../src/config/schemas/rate-limit.ts","../src/config/schemas/upload.ts","../src/config/schemas/logging.ts","../src/config/schemas/cache.ts","../src/config/schemas/security.ts","../src/config/schemas/storage.ts","../src/vite-plugin/theoui-detect.ts","../src/vite-plugin/config-resolve.ts","../src/vite-plugin/configure-server-hook.ts","../src/devtools/server-side/route-manifest.ts","../src/router/scan.ts","../src/router/types.ts","../src/vite-plugin/action-middleware.ts","../src/vite-plugin/agent-middleware.ts","../src/server/agent/agent-card-handler.ts","../src/server/agent/approve-agent.ts","../src/server/agent/list-approvals-handler.ts","../src/server/agent/handle-agent-run-reconnect.ts","../src/server/agent/thread-dispatcher.ts","../src/server/agent/thread-run-registry.ts","../src/server/agent/handle-thread-routes.ts","../src/server/agent/serve-aux-routes.ts","../src/server/http/node-web-adapter.ts","../src/vite-plugin/api-middleware.ts","../src/vite-plugin/ssr-dev-middleware.ts","../src/vite-plugin/ws-upgrade.ts","../src/vite-plugin/integrate-ui.ts","../src/vite-plugin/auto-detect.ts","../src/vite-plugin/resolve-theo-root.ts","../src/vite-plugin/transform-html-hook.ts","../src/vite-plugin/inject-devtools.ts","../src/vite-plugin/inject-entry-client.ts","../src/vite-plugin/inject-stylesheets.ts","../src/router/entry-server.ts","../src/router/entry.ts","../src/router/generate.ts","../src/vite-plugin/virtual-modules-hook.ts","../src/vite-plugin/integrations.ts"],"sourcesContent":["/* eslint-disable security/detect-non-literal-fs-filename --\n * Vite plugin entry. Reads `package.json` + checks for ts vs js source\n * layout under `theoSrcDir` (build-time literal). No HTTP input.\n */\nimport { existsSync } from 'node:fs'\nimport { resolve, dirname } from 'node:path'\nimport { fileURLToPath } from 'node:url'\n\nimport type { Plugin } from 'vite'\n\nimport {\n type AuditLogger,\n type CorsConfig,\n type CsrfReadinessStore,\n type DisallowedConfig,\n type PluginRunner,\n type RateLimitConfig,\n type SecurityHeadersConfig,\n} from '../server/internal-api.js'\nimport type { TheoTransformer } from '../server/transformer.js'\nimport { type ServicesConfig } from '../services/index.js'\n\n// T2.1-T2.3 (architecture-medium-deferrals) + T2.6 (M6) — sibling extractions.\nimport { runConfigHook } from './config-hook.js'\nimport { resolvePluginConfig, type ResolvedOpenApi } from './config-resolve.js'\nimport { runConfigureServer } from './configure-server-hook.js'\nimport { integrateUseTheoUI } from './integrate-ui.js'\nimport { resolveTheoRootDir } from './resolve-theo-root.js'\nimport type { TheoUiDetectResult } from './theoui-detect.js'\nimport { runTransformIndexHtml } from './transform-html-hook.js'\nimport {\n loadVirtualModule,\n resolveVirtualModuleId,\n type VirtualModuleIds,\n} from './virtual-modules-hook.js'\n\nexport {\n defineTheoIntegration,\n createIntegrationRegistry,\n IntegrationRouteCollisionError,\n IntegrationVirtualModulePrefixError,\n} from './integrations.js'\nexport type {\n TheoIntegration,\n HookName as IntegrationHookName,\n Hook as IntegrationHook,\n HookContext as IntegrationHookContext,\n IntegrationRegistry,\n IntegrationRegistryOptions,\n IntegrationRoute,\n RouteHandler as IntegrationRouteHandler,\n} from './integrations.js'\n\nconst VIRTUAL_ENTRY_ID = '/@theo/entry-client'\nconst RESOLVED_ENTRY_ID = '\\0@theo/entry-client'\nconst VIRTUAL_MANIFEST_ID = '/@theo/route-manifest'\nconst RESOLVED_MANIFEST_ID = '\\0@theo/route-manifest'\nconst VIRTUAL_ENTRY_SERVER_ID = '/@theo/entry-server'\nconst RESOLVED_ENTRY_SERVER_ID = '\\0@theo/entry-server'\nconst VIRTUAL_RUNTIME_CONFIG_ID = '/@theo/runtime-config'\nconst RESOLVED_RUNTIME_CONFIG_ID = '\\0@theo/runtime-config'\n\nexport interface TheoPluginOptions {\n root?: string\n rateLimit?: RateLimitConfig\n ssr?: boolean\n ssrStreaming?: boolean\n /**\n * Wave 2 (T3.1) — polyglot services config. When passed, the\n * services-typed-client plugin is wired and generates `clients/<name>.ts`\n * per service with an OpenAPI URL. Empty `{}` → no-op.\n */\n services?: ServicesConfig\n /**\n * T4.1 (canvas-ecosystem-refactor / ADR D6) — extra package names appended\n * to Vite `optimizeDeps.include`. Required when an installed plugin\n * peer-dep is consumed via a literal `import('<pkg>')` (dynamic specifier)\n * — Vite cannot trace those without a hint, and the browser receives a\n * `Failed to resolve module specifier '<pkg>'` error.\n *\n * Default targets pre-bundled regardless: `@theokit/ui`, `lucide-react`.\n *\n * Example:\n * viteOptimizeDeps: ['mermaid']\n */\n viteOptimizeDeps?: string[]\n /**\n * Optional override for the in-memory CSRF readiness store consumed by\n * the `/__theo/csrf-readiness` endpoint and the devtools CSRF Readiness\n * tab. When omitted, the plugin auto-instantiates a default in-process\n * `CsrfReadinessStore` so the tab works out-of-the-box in dev.\n *\n * Override with a custom impl when you need cross-worker telemetry\n * (e.g., Redis-backed store) or want to silence the tab entirely\n * (pass an inert store that discards writes).\n */\n csrfReadinessStore?: CsrfReadinessStore\n /**\n * Backend root directory name (config `serverDir`), relative to the project root. Route/action\n * discovery, the typed-client codegen, and the routes HMR watcher all scan `<serverDir>/routes`,\n * `<serverDir>/actions`, etc. Defaults to `\"server\"` — apps that keep the canonical layout are\n * unaffected. Set to e.g. `\"core\"` to organize the backend by domain (issue #95).\n */\n serverDir?: string\n /**\n * Directory (relative to project root) holding `agents/<name>.ts` definitions (config `agentsDir`).\n * Scanned by the dev agent-middleware + the typed-agent codegen + the routes manifest. Defaults to\n * `\"agents\"`. Set to e.g. `\"core/agents\"` to co-locate agents with the domain root (#95 follow-up).\n */\n agentsDir?: string\n /**\n * Frontend dir (config `appDir`, default \"app\") holding the file-based router (`page.tsx`,\n * `layout.tsx`, …), relative to the project root. Set to e.g. `\"apps/web\"` to group the web UI\n * under an `apps/` root alongside other frontends (#95 follow-up).\n */\n appDir?: string\n}\n\n/**\n * T3.3 — Find a consumer-side config file (`tailwind.config.*`, `postcss.config.*`)\n * by walking from projectRoot up to 3 levels. Returns the absolute path on the\n * first hit, or `undefined`. Used to defer to consumer's manual config (D3).\n */\nfunction findConsumerConfig(projectRoot: string, basename: string): string | undefined {\n const extensions = ['.ts', '.js', '.mjs', '.cjs']\n let dir = projectRoot\n for (let level = 0; level < 3; level++) {\n for (const ext of extensions) {\n const candidate = resolve(dir, `${basename}${ext}`)\n if (existsSync(candidate)) return candidate\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n return undefined\n}\n\n/**\n * Async factory companion. Returns `[theoPlugin, ...autoChainedPlugins]`.\n *\n * Why this exists alongside the sync `theoPlugin()` factory: Vite does NOT\n * reliably merge plugins returned from a child plugin's `config()` hook\n * (verified empirically 2026-05-23 — plugins were silently dropped from\n * the resolved chain despite being returned correctly). The canonical\n * Vite pattern for plugin-from-plugin is to return `Plugin[]` from the\n * plugin factory itself, which Vite flattens into the consumer's\n * `plugins` array.\n *\n * Consumers should prefer `await theoPluginAsync(...)` over `theoPlugin(...)`\n * when they want @theokit/ui + @tailwindcss/vite auto-chaining. The sync\n * factory remains for backward compatibility and tests.\n */\nexport async function theoPluginAsync(\n rootOrOptions?: string | TheoPluginOptions,\n): Promise<Plugin[]> {\n const options =\n typeof rootOrOptions === 'string' ? { root: rootOrOptions } : (rootOrOptions ?? {})\n const projectRoot = options.root ?? process.cwd()\n // Backend root (config `serverDir`, default \"server\"). Route/action discovery, typed-client\n // codegen, and the routes HMR watcher all scan under this dir (issue #95). Absolute.\n const serverDirAbs = resolve(projectRoot, options.serverDir ?? 'server')\n // Agents dir NAME (config `agentsDir`, default \"agents\"), relative to projectRoot — scanAgents\n // joins it. May be nested (e.g. \"core/agents\"). (#95 follow-up)\n const agentsDirName = options.agentsDir ?? 'agents'\n\n const consumerTailwindConfig = findConsumerConfig(projectRoot, 'tailwind.config')\n const consumerPostcssConfig = findConsumerConfig(projectRoot, 'postcss.config')\n const uiPlugins = await integrateUseTheoUI(projectRoot, {\n consumerTailwindConfig,\n consumerPostcssConfig,\n })\n\n // Wave 2 (T3.1) — typed-client plugin wired only when services declared.\n // Empty `services: {}` (default) → plugin is a no-op (no fetch fired).\n const servicesPlugins: Plugin[] = []\n if (options.services && Object.keys(options.services).length > 0) {\n const { servicesTypedClientPlugin } = await import('./services-typed-client.js')\n servicesPlugins.push(\n servicesTypedClientPlugin({\n cwd: projectRoot,\n services: options.services,\n }),\n )\n }\n\n // G1 — app typed client (`@theo/client`). Always wired (route detection is\n // cheap and the plugin is a no-op when `server/routes/` is absent).\n const { appTypedClientPlugin } = await import('./app-typed-client.js')\n const appClientPlugin = appTypedClientPlugin({\n cwd: projectRoot,\n serverDir: serverDirAbs,\n distDir: resolve(projectRoot, '.theokit'),\n })\n\n // G3 — actions virtual module (`@theo/actions`). Always wired; the plugin\n // emits an empty `{}` facade when `server/actions/` is absent so consumers\n // can still `import { actions } from '@theo/actions'` without breakage.\n // The `distDir` opt enables `.theokit/actions.d.ts` emit so TS resolves the\n // virtual module ambient declaration in consumer apps.\n const { actionsVirtualModule } = await import('./actions-virtual-module.js')\n const actionsPlugin = actionsVirtualModule({\n serverDir: serverDirAbs,\n distDir: resolve(projectRoot, '.theokit'),\n })\n\n // M2 — agents typed client (`@theo/agents`). Emits `.theokit/agents.d.ts` mapping each\n // top-level `agents/<name>.ts` to its `defineAgent({ input })` type, so `useAgent('name')`\n // is typed end-to-end with zero manual wiring. Watches `agents/*`.\n const { agentsTypedClientPlugin } = await import('./agents-typed-client.js')\n const { generateManifest } = await import('../server/internal-api.js')\n const agentsClientPlugin = agentsTypedClientPlugin({\n projectRoot,\n agentsDir: agentsDirName,\n distDir: resolve(projectRoot, '.theokit'),\n // Agents live at <projectRoot>/<agentsDir>; generateManifest scans them via the server dir's\n // parent (projectRoot for the canonical layout) + the agents dir name (#95 follow-up).\n scanManifest: () => generateManifest(serverDirAbs, projectRoot, agentsDirName),\n })\n\n // G6 T1.2 — server-routes HMR. Watches `<serverDir>/routes/**` and\n // invalidates SSR module cache + sends full-reload on any add/change/unlink\n // with a 50 ms debounce per EC-6.\n const { serverRoutesHmrPlugin } = await import('./server-routes-hmr.js')\n const routesHmrPlugin = serverRoutesHmrPlugin({\n serverDir: serverDirAbs,\n })\n\n // #122 (T1.1) — compile `<serverDir>/controllers/**` through @theokit/http's\n // swc transform so parameter decorators (@Body/@Param/@Query) emit metadata\n // esbuild drops. No-op for every non-controller file (ADR-4 — file-based\n // routes are byte-for-byte unchanged).\n const { controllerSwcTransformPlugin } = await import('./controller-swc-transform.js')\n const controllerTransformPlugin = controllerSwcTransformPlugin({\n serverDir: serverDirAbs,\n })\n\n return [\n theoPlugin(rootOrOptions),\n ...uiPlugins,\n ...servicesPlugins,\n // #122 — swc-compile controllers/** (enforce:'pre'; order-independent).\n controllerTransformPlugin,\n appClientPlugin,\n actionsPlugin,\n agentsClientPlugin,\n routesHmrPlugin,\n ]\n}\n\n/**\n * Build the `optimizeDeps.include` array Vite uses to pre-bundle heavy\n * deps at server startup. Detects `@theokit/ui` and `lucide-react` in\n * the consumer's `node_modules` (auto-include) and appends user-declared\n * peer deps via `viteOptimizeDeps` (e.g., `mermaid` for plugin-canvas\n * — without this hint Vite fails to resolve dynamic `import('mermaid')`\n * inside an installed plugin at dev time). T4.1 of canvas-ecosystem-refactor.\n */\nfunction buildOptimizeDepsInclude(\n projectRoot: string,\n viteOptimizeDeps: readonly string[] | undefined,\n): string[] {\n const include: string[] = []\n if (existsSync(resolve(projectRoot, 'node_modules', '@theokit', 'ui'))) {\n include.push('@theokit/ui')\n }\n if (existsSync(resolve(projectRoot, 'node_modules', 'lucide-react'))) {\n include.push('lucide-react')\n }\n // T7.1 — devalue is a peer of the @theo/actions virtual module facade.\n // Without this include hint, Vite import-analysis fails to resolve the\n // facade's `await import('devalue')` from the consumer context (devalue\n // lives in theokit's node_modules subtree, not the consumer root).\n include.push('devalue')\n if (Array.isArray(viteOptimizeDeps)) {\n for (const pkg of viteOptimizeDeps) {\n if (typeof pkg === 'string' && pkg.length > 0 && !include.includes(pkg)) {\n include.push(pkg)\n }\n }\n }\n return include\n}\n\nconst VIRTUAL_MODULE_IDS: VirtualModuleIds = {\n VIRTUAL_ENTRY_ID,\n RESOLVED_ENTRY_ID,\n VIRTUAL_MANIFEST_ID,\n RESOLVED_MANIFEST_ID,\n VIRTUAL_ENTRY_SERVER_ID,\n RESOLVED_ENTRY_SERVER_ID,\n VIRTUAL_RUNTIME_CONFIG_ID,\n RESOLVED_RUNTIME_CONFIG_ID,\n}\n\nexport function theoPlugin(rootOrOptions?: string | TheoPluginOptions): Plugin {\n const options =\n typeof rootOrOptions === 'string' ? { root: rootOrOptions } : (rootOrOptions ?? {})\n const projectRoot = options.root ?? process.cwd()\n // Frontend dir (config `appDir`, default \"app\") — the file-based router scan + SSR/client entry (#95).\n const appDir = resolve(projectRoot, options.appDir ?? 'app')\n // Backend root (config `serverDir`, default \"server\") — route/action middleware scan under it (#95).\n const serverDir = resolve(projectRoot, options.serverDir ?? 'server')\n // Agents dir NAME (config `agentsDir`, default \"agents\") — the agent-middleware scanAgents uses it.\n const agentsDir = options.agentsDir ?? 'agents'\n const ssrEnabled = options.ssr ?? false\n\n // Resolve paths for SSR module loading. Pure helper extracted for T1.3\n // regression test — see resolve-theo-root.ts for branch documentation.\n const currentDir = dirname(fileURLToPath(import.meta.url))\n const theoSrcDir = resolveTheoRootDir(currentDir)\n\n // EC-1: plugin runner cached in module closure. Instantiated in configResolved\n // (non-HMR-able). theo.config.ts edits during dev emit a warn but do NOT\n // re-instantiate — user must restart dev server for plugin changes.\n let pluginRunner: PluginRunner | undefined\n let transformer: TheoTransformer | undefined\n let resolvedBatching: { max?: number } | undefined\n let theoUi: TheoUiDetectResult | undefined\n let csrfMode: 'off' | 'warn' | 'strict' = 'strict'\n let securityHeaders: SecurityHeadersConfig | undefined\n let disallowed: DisallowedConfig | undefined\n // T1.2 — CORS config resolved from theo.config.ts; passed to api-middleware.\n let cors: CorsConfig | undefined\n // T4.1 — Audit logger from theo.config.ts.audit.logger\n let auditLogger: AuditLogger | undefined\n // T1.2 — devtools opt-out. `false` skips injection; anything else enables (dev only).\n let devtoolsEnabled = true\n // P#3 T1.1 — OpenAPI dev-emit config. undefined = opt-out (no behavior).\n let resolvedOpenApi: ResolvedOpenApi | undefined\n let resolvedDistDir = '.theokit'\n // Dev mode flag set in configureServer. transformIndexHtml runs in both\n // dev and build; we only want to inject during dev. Wrapped in a ref\n // struct so sub-hooks (configure-server-hook, transform-html-hook) can\n // observe mutations across hook boundaries without losing identity.\n const isDevModeRef = { value: false }\n let configLoadedOnce = false\n\n return {\n name: 'theo',\n\n async configResolved() {\n // T2.1 (architecture-medium-deferrals, ADR D2) — config-load logic\n // extracted to `config-resolve.ts`. This hook owns only the one-shot\n // semantic and the closure-variable assignment.\n if (configLoadedOnce) return\n configLoadedOnce = true\n const resolved = await resolvePluginConfig(projectRoot)\n pluginRunner = resolved.pluginRunner\n transformer = resolved.transformer\n resolvedBatching = resolved.resolvedBatching\n theoUi = resolved.theoUi\n csrfMode = resolved.csrfMode\n securityHeaders = resolved.securityHeaders\n disallowed = resolved.disallowed\n cors = resolved.cors\n auditLogger = resolved.auditLogger\n devtoolsEnabled = resolved.devtoolsEnabled\n resolvedOpenApi = resolved.openapi\n resolvedDistDir = resolved.distDir\n },\n\n // T2.6 (M6) — config() body extracted to config-hook.ts. The hook\n // owns optimizeDeps + warmup + services proxy + alias cascade.\n // The @theokit/ui + @tailwindcss/vite auto-chain lives in\n // `theoPluginAsync` because Vite drops plugins returned from a\n // child plugin's config() hook.\n config() {\n return runConfigHook({\n projectRoot,\n theoSrcDir,\n services: options.services,\n optimizeDepsInclude: buildOptimizeDepsInclude(projectRoot, options.viteOptimizeDeps),\n })\n },\n\n // T2.6 (M6) — transformIndexHtml hook extracted to transform-html-hook.ts.\n // Preserves the canonical 3-step order (entry-client → devtools → styles)\n // so EC-10 (Vite hook ordering side effects) is honored.\n transformIndexHtml: {\n order: 'pre' as const,\n handler(html: string): string {\n return runTransformIndexHtml(html, {\n isDevMode: isDevModeRef,\n devtoolsEnabled,\n projectRoot,\n })\n },\n },\n\n // T2.6 (M6) — virtual-module resolveId + load dispatch extracted to\n // virtual-modules-hook.ts. Pure structural split; identity branches\n // preserved.\n resolveId(id: string) {\n return resolveVirtualModuleId(id, VIRTUAL_MODULE_IDS)\n },\n\n load(id: string) {\n return loadVirtualModule(id, {\n ids: VIRTUAL_MODULE_IDS,\n appDir,\n ssrEnabled,\n streamingEnabled: options.ssrStreaming === true,\n theoUi,\n transformer,\n })\n },\n\n // T2.6 (M6) — configureServer body extracted to configure-server-hook.ts.\n // The hook owns middleware order, ws subscriptions, watcher handlers,\n // OpenAPI re-emit, and WS upgrade. EC-10 honored (every effect preserved\n // in its original ordering).\n async configureServer(server) {\n await runConfigureServer(server, {\n projectRoot,\n appDir,\n serverDir,\n agentsDir,\n resolvedDistDir,\n ssrEnabled,\n isDevMode: isDevModeRef,\n pluginRunner,\n transformer,\n resolvedBatching,\n csrfMode,\n securityHeaders,\n disallowed,\n cors,\n auditLogger,\n resolvedOpenApi,\n services: options.services,\n rateLimit: options.rateLimit,\n csrfReadinessStoreOverride: options.csrfReadinessStore,\n virtualEntryServerId: VIRTUAL_ENTRY_SERVER_ID,\n resolvedManifestId: RESOLVED_MANIFEST_ID,\n })\n },\n }\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * config() hook extraction (T2.6 / M6). Reads `app/<file>` candidates +\n * theoSrcDir composition (build-time literals).\n */\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\nimport { buildServicesProxyConfig, type ServicesConfig } from '../services/index.js'\n\nexport interface ConfigHookCtx {\n projectRoot: string\n theoSrcDir: string\n services: ServicesConfig | undefined\n optimizeDepsInclude: string[]\n}\n\n/**\n * Builds the partial Vite config returned from `theoPlugin.config()`.\n * Pure extraction — `optimizeDeps.force: true` (workspace iteration\n * coherence), `server.watch.ignored` (ENOSPC prevention), and the\n * `resolve.alias` cascade with order-matters comment all preserved.\n */\nexport function runConfigHook(ctx: ConfigHookCtx): Record<string, unknown> {\n // Detect whether we're running from source (.ts) or compiled dist (.js)\n const ext = existsSync(resolve(ctx.theoSrcDir, 'index.ts')) ? '.ts' : '.js'\n\n // Perf: warm up the app's critical-path files so Vite transforms them\n // before the browser asks. Cuts the visible LCP when these files\n // import heavy barrels (TheoUI).\n const warmupClientFiles: string[] = []\n for (const candidate of ['app/layout.tsx', 'app/page.tsx', 'app/page.jsx']) {\n const full = resolve(ctx.projectRoot, candidate)\n if (existsSync(full)) warmupClientFiles.push(`./${candidate}`)\n }\n\n // Wave 2 completion T1.1 — translate `services: {}` into Vite proxy.\n // Empty services → empty record → Vite proxy unaffected.\n const servicesProxy =\n ctx.services && Object.keys(ctx.services).length > 0\n ? buildServicesProxyConfig(ctx.services)\n : undefined\n\n // T7.1 dogfood fix — workspace-linked packages change frequently as\n // we iterate. Vite's pre-bundler caches by lockfile hash, NOT source-\n // content hash; when the workspace dist updates but the lockfile\n // doesn't, the cache serves stale bundles. Setting `force: true`\n // always re-optimizes on boot — slower cold-start but cache-coherent\n // across workspace iter.\n return {\n envPrefix: 'THEO_PUBLIC_',\n optimizeDeps: {\n ...(ctx.optimizeDepsInclude.length > 0 ? { include: ctx.optimizeDepsInclude } : {}),\n force: true,\n },\n server: {\n ...(warmupClientFiles.length > 0 ? { warmup: { clientFiles: warmupClientFiles } } : {}),\n ...(servicesProxy !== undefined ? { proxy: servicesProxy } : {}),\n // Skip watching gitignored heavy dirs to avoid ENOSPC (inotify watcher\n // exhaustion), theokit's own generated output, and local SQLite data.\n // #121: a DB write (`.data/app.db` + `-wal`/`-shm`) on every agent turn /\n // request must NOT trigger a full page reload — that \"blinks\" the screen\n // and tears down the in-flight agent stream.\n watch: {\n ignored: [\n '**/referencias/**',\n '**/.theokit/**',\n '**/dist/**',\n '**/node_modules/**',\n '**/.data/**',\n '**/*.db',\n '**/*.db-wal',\n '**/*.db-shm',\n '**/*.sqlite',\n '**/*.sqlite-journal',\n ],\n },\n },\n resolve: {\n alias: [\n // Order matters: most-specific first so `theokit/X` doesn't\n // get matched by the bare `theokit` alias.\n { find: 'theokit/server', replacement: resolve(ctx.theoSrcDir, `server/index${ext}`) },\n { find: 'theokit/client', replacement: resolve(ctx.theoSrcDir, `client/index${ext}`) },\n {\n // T2.1 (M5 lonely folders) moved the source from\n // `react-query/index.ts` into `client/react-query.ts` (sibling of\n // `client/index.ts`). The package.json export `./react-query`\n // still points at the build artifact `./dist/react-query/index.{js,d.ts}`,\n // but the dev-time source alias must follow the new location.\n find: 'theokit/react-query',\n replacement: resolve(ctx.theoSrcDir, `client/react-query${ext}`),\n },\n {\n find: 'theokit/vite-plugin',\n replacement: resolve(ctx.theoSrcDir, `vite-plugin/index${ext}`),\n },\n {\n find: 'theokit/adapters/web-shim',\n replacement: resolve(ctx.theoSrcDir, `adapters/web-shim${ext}`),\n },\n {\n find: 'theokit/adapters/ws-shim',\n replacement: resolve(ctx.theoSrcDir, `adapters/ws-shim${ext}`),\n },\n // T1.2 — devtools entry (DEV only; tree-shaken in build).\n // Source layout: src/devtools/dom/entry.tsx (has /dom/ segment)\n // Dist layout: dist/devtools/entry.js (tsup flattens /dom/)\n {\n find: 'theokit/devtools/entry',\n replacement: resolve(\n ctx.theoSrcDir,\n ext === '.ts' ? 'devtools/dom/entry.tsx' : 'devtools/entry.js',\n ),\n },\n { find: 'theokit', replacement: resolve(ctx.theoSrcDir, `index${ext}`) },\n ],\n },\n }\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Config loader. Reads `theo.config.{ts,js,mjs}` under the user's `cwd`.\n * Names are a fixed set of literals; `cwd` is a CLI arg resolved to\n * absolute. Build-time tool — no HTTP input.\n */\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\nimport { TheoConfigError } from './errors.js'\nimport { importUserModule } from './import-user-module.js'\nimport { loadEnv } from './load-env.js'\nimport { theoConfigSchema } from './schema.js'\nimport type { TheoConfig } from './schema.js'\n\nconst CONFIG_FILE = 'theo.config.ts'\n\n/**\n * Deep merges two plain objects. Override values take precedence.\n * Arrays are replaced (not concatenated).\n * Protects against prototype pollution (EC-4).\n */\nexport function deepMerge(\n base: Record<string, unknown>,\n override: Record<string, unknown>,\n): Record<string, unknown> {\n const result = { ...base }\n for (const key of Object.keys(override)) {\n // EC-4: Prevent prototype pollution\n if (key === '__proto__' || key === 'constructor' || key === 'prototype') continue\n\n const baseVal = base[key]\n const overVal = override[key]\n\n if (\n overVal !== null &&\n typeof overVal === 'object' &&\n !Array.isArray(overVal) &&\n baseVal !== null &&\n typeof baseVal === 'object' &&\n !Array.isArray(baseVal)\n ) {\n result[key] = deepMerge(\n baseVal as Record<string, unknown>,\n overVal as Record<string, unknown>,\n )\n } else {\n result[key] = overVal\n }\n }\n return result\n}\n\nexport async function loadConfig(dir: string): Promise<TheoConfig> {\n // T1.3 — Load .env BEFORE reading theo.config.ts. Defensive: CLI commands\n // (dev/build/start) already call loadEnv() first, but programmatic users\n // who call loadConfig directly (tests, custom scripts) need this too.\n // Module-level cache makes the second call a Map-lookup (no FS).\n loadEnv({ cwd: dir })\n\n const configPath = resolve(dir, CONFIG_FILE)\n\n if (!existsSync(configPath)) {\n return theoConfigSchema.parse({})\n }\n\n let mod: Record<string, unknown>\n try {\n mod = await importUserModule(configPath)\n } catch (err) {\n throw new TheoConfigError([{ field: '_file', message: (err as Error).message }], configPath)\n }\n\n const userConfig = mod.default\n\n if (userConfig == null || typeof userConfig !== 'object') {\n throw new TheoConfigError(\n [\n {\n field: '_export',\n message: 'theo.config.ts must use export default defineConfig({...})',\n },\n ],\n configPath,\n )\n }\n\n // Merge with per-environment config if NODE_ENV is set\n let rawConfig = userConfig as Record<string, unknown>\n const nodeEnv = process.env.NODE_ENV\n\n if (nodeEnv) {\n const envFile = `theo.config.${nodeEnv}.ts`\n const envPath = resolve(dir, envFile)\n\n if (existsSync(envPath)) {\n try {\n const envMod = await importUserModule(envPath)\n const envConfig = envMod.default\n\n if (envConfig != null && typeof envConfig === 'object') {\n rawConfig = deepMerge(rawConfig, envConfig as Record<string, unknown>)\n }\n } catch (err) {\n throw new TheoConfigError([{ field: '_file', message: (err as Error).message }], envPath)\n }\n }\n }\n\n const result = theoConfigSchema.safeParse(rawConfig)\n\n if (!result.success) {\n const issues = result.error.issues.map((i) => ({\n field: i.path.join('.'),\n message: i.message,\n }))\n throw new TheoConfigError(issues, configPath)\n }\n\n return result.data\n}\n","export interface ConfigIssue {\n field: string\n message: string\n}\n\nexport class TheoConfigError extends Error {\n public readonly issues: ConfigIssue[]\n public readonly configPath: string\n\n constructor(issues: ConfigIssue[], configPath: string) {\n const issueLines = issues.map((i) => ` - ${i.field}: ${i.message}`).join('\\n')\n\n super(\n `Invalid theo.config.ts\\n\\n` +\n ` File: ${configPath}\\n\\n` +\n (issueLines ? ` Issues:\\n${issueLines}\\n` : ''),\n )\n\n this.name = 'TheoConfigError'\n this.issues = issues\n this.configPath = configPath\n }\n}\n","import { z } from 'zod'\n\nimport { servicesConfigSchema } from '../services/index.js'\n\nimport {\n cacheSchema,\n loggingSchema,\n rateLimitSchema,\n securitySchema,\n storageSchema,\n uploadSchema,\n type FormatErrorContext,\n type FormatErrorHook,\n} from './schemas/index.js'\n\n// Re-export per-concern schemas + types so downstream consumers\n// (`adapters/*`, vite-plugin, generators, tests) keep their existing\n// imports valid. Per plan T2.3 — pure structural split; zero behavior\n// change at the call site.\nexport {\n cacheSchema,\n corsSchema,\n disallowedConfigSchema,\n headerSafeString,\n loggingSchema,\n rateLimitSchema,\n securityHeadersSchema,\n securitySchema,\n storageSchema,\n uploadSchema,\n} from './schemas/index.js'\n\nexport type { FormatErrorContext, FormatErrorHook, StorageConfig } from './schemas/index.js'\n\n/**\n * Root `theo.config.ts` schema — composer assembled from the per-concern\n * primitives re-exported above.\n *\n * Embedded blocks that exist ONLY as part of the root config (`agents`,\n * `ui`, `devtools`, `jobs`, `openapi`) stay inline below. They have no\n * external consumers; splitting them would create lonely files (M5\n * smell) without comprehension benefit.\n */\nexport const theoConfigSchema = z\n .object({\n /**\n * Plan v1.2 T2.1 — project identifier propagated into\n * `.theokit/services.json` v2 `project` field. DNS-1123 compatible (drives\n * Gitea repo + ArgoCD App naming on TheoCloud). When omitted, the build\n * emits services.json v1 with the legacy `services-bundle` fallback\n * (ADR D10) and logs a deprecation warning.\n */\n name: z\n .string()\n .max(63)\n .refine(\n // DNS-1123 equivalent expressed as explicit single-char anchors so\n // security/detect-unsafe-regex stays clean (no backtracking).\n (value) =>\n value.length > 0 &&\n /^[a-z0-9-]+$/u.test(value) &&\n !value.startsWith('-') &&\n !value.endsWith('-'),\n 'name must match DNS-1123 (lowercase alphanumeric+hyphens, 1-63 chars, no leading/trailing hyphen)',\n )\n .optional(),\n appDir: z.string().default('app'),\n serverDir: z.string().default('server'),\n /**\n * Directory (relative to the project root) holding `agents/<name>.ts` definitions, scanned by\n * dev/build/terminal/mcp. Defaults to `\"agents\"` — apps that keep the canonical layout are\n * unaffected. Set to e.g. `\"core/agents\"` to organize agents under a domain root (#95 follow-up).\n */\n agentsDir: z.string().default('agents'),\n /**\n * T2.2 / EC-4 — Build output directory. Must be a relative path inside\n * the project root. Refused absolute or parent-relative paths to prevent\n * `cleanOutDir` from wiping arbitrary locations (defense-in-depth on\n * top of cleanOutDir's runtime EC-3 guard).\n */\n distDir: z\n .string()\n .default('.theokit')\n .refine(\n (d) => !/^([A-Za-z]:)?[/\\\\]/.test(d) && !d.startsWith('..'),\n 'distDir must be a relative path inside the project root (e.g., \".theokit\")',\n ),\n /**\n * Agent runtime configuration.\n *\n * - `maxRegistries` (T2.3, legacy): dev-mode cleanup of stale\n * `.theokit/agents/<id>/` dirs by mtime. Now superseded by SDK's\n * native registry GC but kept for backward-compat.\n * - `registry` (Phase 6, Production-Readiness #2): forwarded to\n * `Agent.registry.configure()` lazily on first request.\n * * `maxAgents` — LRU cap. MUST be ≥ max-concurrent-active-conversations\n * (EC-17 DOCUMENT): with maxAgents:1 and 2 concurrent chats, LRU evicts\n * mid-stream → SDK aborts with code:'aborted'. Default 100 covers\n * indie/small-team; tune up for high-traffic.\n * * `idleTimeoutMs` — auto-evict idle agents (default 30 min). 0 disables\n * idle eviction (LRU only).\n */\n agents: z\n .object({\n maxRegistries: z.number().int().positive().default(100),\n registry: z\n .object({\n /** LRU cap — MUST be ≥ max-concurrent-active-conversations. */\n maxAgents: z.number().int().positive().max(10_000).default(100),\n /** Idle eviction window in ms. 0 disables idle eviction (LRU only). */\n idleTimeoutMs: z\n .number()\n .int()\n .nonnegative()\n .default(30 * 60_000),\n })\n .optional(),\n })\n .optional(),\n port: z.number().int().min(1).max(65535).default(3000),\n /** Listen on all addresses (0.0.0.0) for LAN/mobile testing. */\n host: z.union([z.string(), z.boolean()]).default('localhost'),\n /** Automatically open browser on `theokit dev`. */\n open: z.union([z.boolean(), z.string()]).default(false),\n /** Exit if port is already in use instead of trying next available. */\n strictPort: z.boolean().default(false),\n /** Forward browser console/errors to terminal — useful for AI agent dev. */\n forwardConsole: z\n .union([\n z.boolean(),\n z.object({\n unhandledErrors: z.boolean().optional(),\n logLevels: z.array(z.enum(['error', 'warn', 'info', 'log', 'debug'])).optional(),\n }),\n ])\n .default(false),\n /** Pre-transform files on startup for faster first page load. */\n warmup: z\n .object({\n clientFiles: z.array(z.string()).optional(),\n ssrFiles: z.array(z.string()).optional(),\n })\n .optional(),\n ssr: z.boolean().default(false),\n /** When true (and ssr === true), use renderToPipeableStream with progressive\n * shell flush instead of single-shot renderToString. Opt-in for streaming\n * SSR; default false preserves the current behavior. */\n ssrStreaming: z.boolean().default(false),\n rateLimit: rateLimitSchema.optional(),\n upload: uploadSchema.optional(),\n logging: loggingSchema.optional(),\n security: securitySchema.optional(),\n serialization: z.enum(['json', 'superjson']).default('json'),\n // Plugins are validated structurally at runtime by createPluginRunnerFromConfig.\n // Zod only checks the shape minimally (must be array). Type-level safety is\n // provided through defineConfig at the user surface.\n plugins: z.array(z.unknown()).optional(),\n /** Enable client-side batching of theoFetch calls and the\n * /api/__theo_batch__ server endpoint. */\n batching: z\n .union([z.boolean(), z.object({ max: z.number().int().positive().optional() })])\n .optional(),\n /** T4.1 — Audit log. When `logger` is provided, framework events\n * (csrf.warn, rate-limit.exceeded, session.rotated, csp.violation) are\n * emitted to it. Default: noop. */\n audit: z\n .object({\n logger: z.unknown().optional(),\n })\n .optional(),\n /** TheoUI auto-wire (T2.1). `false` = opt-out; object = explicit theme/fonts;\n * undefined = enabled when @theokit/ui is detected in node_modules. */\n ui: z\n .union([\n z.literal(false),\n z.object({\n theme: z.enum(['violet-forge', 'noir', 'paper']).optional(),\n fonts: z.enum(['bundled', 'cdn']).optional(),\n }),\n ])\n .optional(),\n /**\n * Cache subsystem (caching-and-revalidation-plan).\n * Default `undefined` keeps caching disabled (backward compatible).\n * Pass `cache: {}` to opt in with defaults.\n */\n cache: cacheSchema.optional(),\n /**\n * Devtools overlay (Phase 0.4.0+ — see docs/plans/devtools-plan.md).\n *\n * - `undefined` (default): devtools auto-injects in `pnpm dev`, NEVER in `vite build`.\n * - `false`: devtools disabled entirely (Vite plugin skips injection even in dev).\n * - `{ ... }`: devtools enabled with explicit defaults (position, theme).\n *\n * Tree-shaken to noop in prod via the dual-export pattern in\n * `packages/theo/src/devtools/index.ts` (EC-17 positive prod check).\n */\n devtools: z\n .union([\n z.literal(false),\n z.object({\n position: z.enum(['top-left', 'top-right', 'bottom-left', 'bottom-right']).optional(),\n theme: z.enum(['light', 'dark', 'system']).optional(),\n }),\n ])\n .optional(),\n /**\n * Informative list of deploy adapters the app supports. Does NOT\n * trigger per-build translations — only the `--target` CLI flag does\n * (EC-201 / ADR D2). If `config.adapters` includes a target NOT\n * matching `--target`, `theokit build` emits a cross-reference note.\n */\n adapters: z.array(z.string()).optional(),\n /**\n * Extra package names to pre-bundle via Vite's `optimizeDeps.include`.\n *\n * Framework auto-includes `@theokit/ui` and `lucide-react` when present.\n * Apps adding plugin peer-deps that rely on a runtime `import('<pkg>')`\n * (dynamic specifier) — e.g. `@theokit/plugin-canvas` consumers\n * installing `mermaid` for Mermaid SVG rendering — must declare those\n * here so Vite can resolve the literal in dev mode without\n * \"Failed to resolve module specifier\" errors. Production builds use\n * tsup/esbuild bundling and are not affected.\n *\n * Example:\n * viteOptimizeDeps: ['mermaid']\n */\n viteOptimizeDeps: z.array(z.string()).optional(),\n /**\n * Jobs backend (ADR D3 + T2.1). When configured, every request gets\n * `ctx.queue.enqueue` auto-wired via the outbox lifecycle. Pass a\n * `JobBackend` instance (InMemoryJobBackend, PostgresJobBackend, etc.).\n */\n jobs: z\n .object({\n backend: z.custom<unknown>((v) => typeof v === 'object' && v !== null),\n })\n .optional(),\n /**\n * StorageManager configuration (T1.1 / ADR-0007).\n *\n * Declares Postgres servers + databases + Redis servers. Consumed by\n * `getStorageManager().configure()` at boot via `start.ts`.\n *\n * Default `undefined` means manager exists but is not configured — adapters\n * relying on the manager (PostgresJobBackend.fromStorageManager, etc.) will\n * throw with an actionable error on first use.\n */\n storage: storageSchema.optional(),\n /**\n * Wave 2 — Polyglot services orchestration (T1.1 / ADR-0012/0013/0014/0015).\n *\n * Declarative external sidecar processes (Python FastAPI / Node Hono)\n * that boot alongside the TheoKit TS app. Empty `services: {}` is the\n * default and preserves Wave 1 behavior.\n */\n services: servicesConfigSchema,\n /**\n * G2 — OpenAPI emit (build-time only).\n *\n * When present, `theokit build` writes a generated `openapi.json` from\n * every `defineRoute()` body/query/params Zod schema. `undefined` keeps\n * the framework backward-compatible (no emit). Pass `openapi: {}` to\n * opt in with defaults.\n *\n * Defaults: spec 3.1.0 · servers `http://localhost:3000` · title\n * \"TheoKit App\" · version \"0.0.0\" · outDir \".theokit\" (next to dist).\n *\n * The env var `THEOKIT_OPENAPI_SERVERS` (CSV of URLs) overrides\n * `servers[].url` at emit time without rebuilding the config.\n */\n openapi: z\n .object({\n servers: z\n .array(\n z.object({\n url: z.url(),\n description: z.string().optional(),\n }),\n )\n .default([{ url: 'http://localhost:3000', description: 'Local development' }]),\n specVersion: z.enum(['3.1.0', '3.0.3']).default('3.1.0'),\n title: z.string().default('TheoKit App'),\n version: z.string().default('0.0.0'),\n outDir: z.string().default('.theokit'),\n })\n .optional(),\n /**\n * G5 T1.3 — error envelope transformer hook (blueprint ADR D3).\n *\n * When present, runs at framework error-boundary time to enrich the\n * envelope with consumer-defined extensions (`hint`, custom telemetry\n * tags, etc.) BEFORE the envelope is serialized to the client. The\n * function signature is preserved via the explicit `FormatErrorHook`\n * type — TS inference flows from `theo.config.ts` into `@theo/client`\n * codegen and `@theokit/ui` AgentErrorCard consumers.\n *\n * Use `z.custom<FormatErrorHook>(...)` because Zod's built-in\n * `z.function()` discards its TS signature after parse; `z.custom`\n * with a function-typed runtime guard preserves the call-site type\n * without trading off Zod runtime validation.\n */\n formatError: z\n .custom<FormatErrorHook>((val) => typeof val === 'function', {\n message: 'formatError must be a function',\n })\n .optional(),\n })\n // EC-2 fix: cross-config refine — no service may share TheoKit's web port.\n .refine((cfg) => !Object.values(cfg.services).some((s) => s.port === cfg.port), {\n message: 'service.port collides with TheoKit web port — change one of them',\n path: ['services'],\n })\n\nexport type TheoConfig = z.infer<typeof theoConfigSchema>\n\nexport type OpenApiConfig = NonNullable<TheoConfig['openapi']>\n\n// Silence unused-import warnings for type-only re-exports — TS strips at compile,\n// but exports above re-introduce them for downstream consumers.\nexport type _FormatErrorContext = FormatErrorContext\n","import { z } from 'zod'\n\n/**\n * EC-3 — CWE-113 HTTP Response Splitting mitigation.\n *\n * Every string that becomes a header value must reject CR/LF. Apps that\n * derive header values from untrusted input (feature flags, tenant config,\n * URL params) would otherwise let attackers inject Set-Cookie / Location\n * headers via `\\r\\n`. Apply this refinement to every header-bound string\n * field: permissionsPolicy, csp, hsts, referrerPolicy, CORS allow/expose.\n */\nexport const headerSafeString = z\n .string()\n .refine((s) => !/[\\r\\n]/.test(s), { message: 'Header value must not contain CR/LF' })\n","import { z } from 'zod'\n\n/**\n * Base bucket config — legacy shape preserved for backwards compatibility.\n */\nconst baseRateLimitSchema = z.object({\n windowMs: z.number().min(1),\n max: z.number().int().min(1),\n})\n\n/**\n * T2.2 — Per-route + per-user rate limit. The new shape adds `routes`\n * (path map), `keyBy`, and `cookieName`. The legacy flat shape is still\n * accepted via the union — see `createRouteRateLimiter` for normalization.\n */\nexport const rateLimitSchema = z.union([\n baseRateLimitSchema,\n z.object({\n default: baseRateLimitSchema.optional(),\n routes: z.record(z.string(), baseRateLimitSchema).optional(),\n keyBy: z\n .union([\n z.enum(['ip', 'session', 'user']),\n z.function({ input: z.tuple([z.unknown()]), output: z.string() }),\n ])\n .optional(),\n cookieName: z.string().min(1).optional(),\n }),\n])\n","import { z } from 'zod'\n\nexport const uploadSchema = z.object({\n maxFileSize: z\n .number()\n .min(1)\n .default(10 * 1024 * 1024), // 10MB\n maxFiles: z.number().int().min(1).default(10),\n maxFieldSize: z\n .number()\n .min(1)\n .default(1 * 1024 * 1024), // 1MB\n})\n","import { z } from 'zod'\n\nexport const loggingSchema = z.object({\n level: z.enum(['debug', 'info', 'warn', 'error', 'silent']).default('info'),\n})\n","import { z } from 'zod'\n\n/**\n * Cache subsystem config (caching-and-revalidation-plan).\n * Default `cache: undefined` keeps the framework backward-compatible\n * (no engine initialized → no cache primitives available).\n *\n * Setting `cache: {}` opts in with all defaults.\n */\nconst routeRuleSchema = z.object({\n maxAge: z.number().nonnegative().finite().optional(),\n swr: z.number().nonnegative().finite().optional(),\n tags: z.array(z.string()).optional(),\n})\n\nexport const cacheSchema = z.object({\n enabled: z.boolean().default(true),\n /** 'memory' uses InMemoryCacheAdapter; otherwise pass a custom adapter instance. */\n storage: z.union([z.literal('memory'), z.custom<unknown>()]).default('memory'),\n maxEntries: z.number().int().positive().default(1000),\n defaults: z\n .object({\n maxAge: z.number().nonnegative().finite().default(1),\n swr: z.number().nonnegative().finite().optional(),\n cacheErrors: z.boolean().default(false),\n })\n .default({ maxAge: 1, cacheErrors: false }),\n keyDerivation: z\n .object({\n excludeQuery: z.array(z.string()).optional(),\n sortQuery: z.boolean().default(true),\n lowercaseHost: z.boolean().default(true),\n })\n .default({ sortQuery: true, lowercaseHost: true }),\n routeRules: z.record(z.string(), routeRuleSchema).optional(),\n})\n","import { z } from 'zod'\n\nimport { headerSafeString } from './header-safe.js'\n\n/**\n * Phase 5 — CSRF warn-first (EC-1).\n *\n * 0.2.0 default: `warn`. Existing apps keep working but get structured\n * warnings about every state-mutating request that does not carry the\n * `X-Theo-Action: 1` header. 0.3.0 will flip the default to `strict`.\n *\n * Set explicitly to `strict` to opt into the future default early,\n * or `off` to disable CSRF entirely (only valid when you have another\n * defense — bearer auth, no session cookies, etc).\n */\n\n/**\n * Phase 6 — Default security headers (D4 / EC-2).\n *\n * 0.2.0 defaults:\n * - CSP in `report-only` mode (EC-2: don't break existing apps)\n * - X-Frame-Options: DENY · X-Content-Type-Options: nosniff\n * - Referrer-Policy: strict-origin-when-cross-origin\n * - HSTS in production only (no TLS on localhost)\n *\n * Users override individual headers, swap CSP to `enforce`, or disable\n * CSP entirely (`csp: false` / `cspMode: 'off'`).\n */\nexport const securityHeadersSchema = z.object({\n csp: z.union([headerSafeString, z.literal(false)]).optional(),\n // T6.1 — default flipped from 'report-only' to 'enforce' for 0.3.0.\n // Users who want the old behaviour set `cspMode: 'report-only'`\n // explicitly. See docs/migrating/0.2-to-0.3.md.\n cspMode: z.enum(['enforce', 'report-only', 'off']).default('enforce'),\n hsts: z.union([headerSafeString, z.literal(false)]).optional(),\n frameOptions: z.enum(['DENY', 'SAMEORIGIN']).default('DENY'),\n contentTypeOptions: z.literal('nosniff').default('nosniff'),\n referrerPolicy: headerSafeString.default('strict-origin-when-cross-origin'),\n /**\n * T1.1 — Permissions-Policy directive string. EC-3-refined: rejects CR/LF.\n * Pass `false` to suppress the header.\n */\n permissionsPolicy: z.union([headerSafeString, z.literal(false)]).optional(),\n})\n\n/**\n * T5.1 — Disallowed-routes escalation pattern (Rails-inspired).\n *\n * `routes` accepts string (exact match — trailing slash matters) or\n * RegExp entries. Matched routes that would otherwise emit `csrf.warn`\n * dispatch through `disallowedBehavior` instead:\n * - `'warn'` : no-op vs the default warn-mode behavior\n * - `'raise'` : escalate to 403, even when global `csrf` mode is 'warn'\n *\n * Use to roll out strict mode per-route (e.g., flip /api/auth/* first)\n * without committing the entire surface to strict at once.\n */\nexport const disallowedConfigSchema = z.object({\n routes: z.array(z.union([z.string(), z.instanceof(RegExp)])),\n behavior: z.enum(['warn', 'raise']).default('raise'),\n})\n\n/**\n * T1.2 — CORS configuration.\n *\n * `origins` accepts a single value (`'*'`, string, RegExp, callback) OR an\n * array of (string | RegExp). The spec-violating `origins: '*'` +\n * `credentials: true` combination is rejected at parse time (browsers\n * ignore wildcards when credentials are sent).\n *\n * EC-3 — `allowedHeaders` and `exposedHeaders` entries go through the\n * header-safe refinement (CR/LF rejected — CWE-113 mitigation).\n */\nexport const corsSchema = z\n .object({\n origins: z.union([\n z.literal('*'),\n headerSafeString,\n z.instanceof(RegExp),\n z.array(z.union([headerSafeString, z.instanceof(RegExp)])),\n z.function({ input: z.tuple([z.string()]), output: z.boolean() }),\n ]),\n methods: z\n .array(z.enum(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD']))\n .optional(),\n allowedHeaders: z.array(headerSafeString).optional(),\n exposedHeaders: z.array(headerSafeString).optional(),\n credentials: z.boolean().default(false),\n maxAge: z.number().int().min(0).max(86400).default(600),\n })\n .refine((c) => !(c.origins === '*' && c.credentials), {\n message: 'CORS spec forbids origins:\"*\" with credentials:true — browsers ignore the wildcard',\n })\n\nexport const securitySchema = z.object({\n // T6.1 — default flipped from 'warn' to 'strict' for 0.3.0. Apps that\n // grep their warn-mode logs from 0.2.x already know which endpoints\n // break; opt back into 'warn' globally OR use disallowedRoutes for\n // surgical migration. See docs/migrating/0.2-to-0.3.md.\n csrf: z.enum(['off', 'warn', 'strict']).default('strict'),\n headers: securityHeadersSchema.optional(),\n /** T5.1 — per-route escalation (Rails disallowed_warnings pattern). */\n disallowed: disallowedConfigSchema.optional(),\n /** T1.2 — Cross-Origin Resource Sharing. Single global config; runs first in pipeline. */\n cors: corsSchema.optional(),\n})\n","import { z } from 'zod'\n\n/**\n * StorageManager schemas (T1.1 / ADR-0007 D4).\n *\n * `theo.config.ts > storage` declares Postgres servers (credentials), the\n * databases on each server (TheoCloud / managed PG / self-host), and Redis\n * servers. The runtime `StorageManager` consumes this block; adapters\n * (PostgresJobBackend, PostgresConversationStorage, etc.) request pools by\n * database name and never see raw credentials at the call site.\n *\n * EC-1 (DOCUMENT in concept doc T4.1): Zod default strip-unknown mode\n * silently drops typo keys (`databasees` instead of `databases`). The\n * concept doc warns users to use exact names; runtime errors are still\n * actionable (`Database \"X\" not configured`).\n *\n * EC-2 (DEFERRED to use-time): `databases.X.server` references a key in\n * `servers`. Cross-field validation is intentionally NOT enforced at parse\n * time — `StorageManager.usePostgres()` throws an actionable error on the\n * first call with the dangling reference, matching Rails / Nitro behavior.\n */\nconst tlsConfigSchema = z.object({\n rejectUnauthorized: z.boolean().optional(),\n caCert: z.string().optional(),\n clientCert: z.string().optional(),\n clientKey: z.string().optional(),\n})\n\nconst serverConfigSchema = z.object({\n host: z.string().min(1),\n port: z.number().int().positive().max(65535).optional(),\n user: z.string().min(1),\n /** Password may be the empty string (some providers permit it). */\n password: z.string(),\n tls: tlsConfigSchema.optional(),\n})\n\nconst postgresPoolConfigSchema = z.object({\n min: z.number().int().nonnegative().optional(),\n max: z.number().int().positive().optional(),\n connectionTimeoutMillis: z.number().int().positive().optional(),\n idleTimeoutMillis: z.number().int().nonnegative().optional(),\n})\n\nconst postgresDatabaseConfigSchema = z.object({\n /** References a key in `storage.servers`. Resolved at `usePostgres()` call time. */\n server: z.string().min(1),\n database: z.string().min(1),\n pool: postgresPoolConfigSchema.optional(),\n})\n\nconst redisServerConfigSchema = serverConfigSchema.extend({\n db: z.number().int().nonnegative().optional(),\n maxRetriesPerRequest: z.number().int().nonnegative().optional(),\n})\n\nexport const storageSchema = z.object({\n servers: z.record(z.string(), serverConfigSchema).optional(),\n databases: z.record(z.string(), postgresDatabaseConfigSchema).optional(),\n redis: z.record(z.string(), redisServerConfigSchema).optional(),\n})\n\nexport type StorageConfig = z.infer<typeof storageSchema>\n","/**\n * T2.1 — Detect `@theokit/ui` presence in the user's project.\n *\n * Uses `require.resolve` with explicit `paths` instead of `existsSync` so\n * pnpm-hoist layouts (TheoUI at workspace root, app in `apps/<x>/`) still\n * detect correctly. Returns `{ enabled: false }` on any failure — never throws.\n */\n\n/* eslint-disable security/detect-non-literal-fs-filename --\n * Reads `package.json` of `@theokit/ui` via filesystem walk under `node_modules`.\n * Paths come from a controlled CLI/config inputs; this is a build-time tool.\n *\n * D13 invariant (ADR 0021): @theokit/ui is ESM-only by design (`type: \"module\"`,\n * exports['.'] sem `require` condition). Não usar `createRequire(...).resolve()` —\n * `ERR_PACKAGE_PATH_NOT_EXPORTED` em runtime. Usar filesystem walk direto.\n */\nimport { existsSync, readFileSync } from 'node:fs'\nimport { dirname, join } from 'node:path'\n\nexport type TheoUiTheme = 'violet-forge' | 'noir' | 'paper'\nexport type TheoUiFonts = 'bundled' | 'cdn'\n\nexport interface TheoUiConfig {\n theme: TheoUiTheme\n fonts: TheoUiFonts\n}\n\nexport interface TheoUiDetectResult {\n enabled: boolean\n config: TheoUiConfig\n}\n\n/**\n * Raw user config shape (from theo.config.ts > ui field).\n * `false` = opt-out (force disabled); object = explicit config; undefined = default.\n */\nexport type RawTheoUiConfig = false | undefined | { theme?: TheoUiTheme; fonts?: TheoUiFonts }\n\n/**\n * Resolve the TheoUI config with defaults applied.\n * `false` → returns disabled defaults (caller checks `enabled`).\n */\nexport function resolveTheoUiConfig(\n raw: { theme?: TheoUiTheme; fonts?: TheoUiFonts } | undefined,\n): TheoUiConfig {\n return {\n theme: raw?.theme ?? 'violet-forge',\n fonts: raw?.fonts ?? 'bundled',\n }\n}\n\n/**\n * Detect whether `@theokit/ui` is installed under `projectRoot` (or any\n * parent dir via Node module resolution — handles pnpm hoist).\n *\n * EC-1: uses `require.resolve` with `paths: [projectRoot]` not `existsSync`.\n * EC-5: corrupted installs (dir without an importable entry) fall through to disabled.\n * EC-9: theme validation happens in the Zod schema, not here.\n *\n * Note: we resolve a known subpath (`./styles.css`) instead of `./package.json`\n * or the root specifier because (a) `@theokit/ui` declares `exports` with no\n * CJS entry, so `require.resolve('@theokit/ui')` fails inside CJS contexts,\n * and (b) `./package.json` is not listed in `exports`. The `styles.css`\n * subpath IS exported and is exactly what entry-client imports — if it\n * resolves, the package is installed and usable.\n */\n/** Resolver fn — abstracted so tests can stub Node's walk-up behavior. */\nexport type SubpathResolver = (specifier: string, projectRoot: string) => boolean\n\n/**\n * D13 invariant: substituir `require.resolve` por filesystem walk que LÊ exports.\n *\n * Specifier shape: `<pkgScope>/<pkgName>/<subpath>` (e.g. `@theokit/ui/styles.css`)\n * OR `<pkgName>/<subpath>` (e.g. `react/jsx-runtime`).\n *\n * Algoritmo:\n * 1. Walk up node_modules a partir de projectRoot (handle pnpm hoist + workspaces).\n * 2. Em cada candidato, ler package.json + resolver subpath via exports field.\n * 3. Se exports mapeia o subpath, checar existsSync no path mapeado.\n *\n * Mimica resolução ESM Node sem usar createRequire (D13).\n */\nfunction resolveExportSubpath(pkgRoot: string, subpath: string): string | null {\n const pkgJsonPath = join(pkgRoot, 'package.json')\n if (!existsSync(pkgJsonPath)) return null\n try {\n const pkg = JSON.parse(readFileSync(pkgJsonPath, 'utf-8')) as {\n exports?: Record<string, unknown>\n }\n const exportKey = `./${subpath}`\n const exp = pkg.exports?.[exportKey]\n if (!exp) {\n // Fallback: tentar path direto (dist/<subpath> é convenção comum)\n const fallback = join(pkgRoot, 'dist', subpath)\n return existsSync(fallback) ? fallback : null\n }\n // exports value: string OR { import, require, default, types, ... }\n let target: string | undefined\n if (typeof exp === 'string') target = exp\n else if (typeof exp === 'object') {\n const e = exp as { import?: string; default?: string }\n target = e.import ?? e.default\n }\n if (!target) return null\n const cleaned = target.replace(/^\\.\\//, '')\n const candidate = join(pkgRoot, cleaned)\n return existsSync(candidate) ? candidate : null\n } catch {\n return null\n }\n}\n\nconst defaultResolver: SubpathResolver = (specifier, projectRoot) => {\n const parts = specifier.split('/')\n const pkgName =\n parts[0]?.startsWith('@') && parts.length >= 2 ? `${parts[0]}/${parts[1]}` : parts[0]\n if (!pkgName) return false\n const subpath = specifier.slice(pkgName.length + 1)\n if (!subpath) return false\n // Walk up node_modules (handle pnpm hoist)\n let dir = projectRoot\n for (let depth = 0; depth < 10; depth++) {\n const pkgRoot = join(dir, 'node_modules', ...pkgName.split('/'))\n if (existsSync(pkgRoot)) {\n const resolved = resolveExportSubpath(pkgRoot, subpath)\n if (resolved) return true\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n return false\n}\n\n/** Reads `<projectRoot>/package.json` and returns whether `@theokit/ui`\n * is declared as a (dev)dependency. Conservative gate: pnpm hoist still\n * works because pnpm rewrites the consumer's package.json to include the\n * declared dep, even when the install lives at the workspace root. */\nfunction isDeclaredInPackageJson(projectRoot: string): boolean {\n try {\n const raw = readFileSync(join(projectRoot, 'package.json'), 'utf-8')\n const pkg = JSON.parse(raw) as {\n dependencies?: Record<string, string>\n devDependencies?: Record<string, string>\n peerDependencies?: Record<string, string>\n }\n // `||` is intentional — any truthy version string (including '0.0.0')\n // counts as a declaration. `??` would let '' fall through, but version\n // strings are never empty.\n return Boolean(\n pkg.dependencies?.['@theokit/ui'] ??\n pkg.devDependencies?.['@theokit/ui'] ??\n pkg.peerDependencies?.['@theokit/ui'],\n )\n } catch {\n return false\n }\n}\n\nexport function detectTheoUi(\n projectRoot: string,\n rawConfig: RawTheoUiConfig,\n resolver: SubpathResolver = defaultResolver,\n): TheoUiDetectResult {\n const config = resolveTheoUiConfig(typeof rawConfig === 'object' ? rawConfig : undefined)\n\n // Explicit opt-out wins over detection\n if (rawConfig === false) {\n return { enabled: false, config }\n }\n\n // **Conservative gate** — require the dep to be DECLARED in the user's\n // package.json. Stops the monorepo workspace from being detected as\n // having TheoUI inside fixtures that never asked for it (and would\n // 500 on the import).\n if (!isDeclaredInPackageJson(projectRoot)) {\n return { enabled: false, config }\n }\n\n // EC-1 + EC-5: prefer a subpath that's guaranteed to be in `exports`.\n // Try several known subpaths; success on any one means the package is usable.\n const probes = ['@theokit/ui/styles.css', '@theokit/ui/fonts.css']\n for (const probe of probes) {\n if (resolver(probe, projectRoot)) {\n return { enabled: true, config }\n }\n }\n return { enabled: false, config }\n}\n","/**\n * T2.1 (architecture-medium-deferrals plan, ADR D2) — `configResolved` hook\n * body extracted from `vite-plugin/index.ts` for SRP.\n *\n * `resolvePluginConfig(projectRoot)` reads `theo.config.ts` and returns a\n * `ResolvedPluginConfig` bag with every field the Vite plugin needs at\n * `configResolved` time. The `configLoadedOnce` flag stays in `index.ts`\n * (caller owns the one-shot semantic).\n *\n * Errors during config load are swallowed silently (matches pre-refactor\n * behavior — config errors are surfaced elsewhere via validate-structure).\n */\n\nimport { loadConfig } from '../config/load-config.js'\nimport {\n createPluginRunnerFromConfig,\n type AuditLogger,\n type CorsConfig,\n type DisallowedConfig,\n type PluginRunner,\n type SecurityHeadersConfig,\n} from '../server/internal-api.js'\nimport { resolveTransformer, type TheoTransformer } from '../server/transformer.js'\n\nimport { detectTheoUi, type TheoUiDetectResult } from './theoui-detect.js'\n\nexport interface ResolvedOpenApi {\n servers: { url: string; description?: string }[]\n specVersion: '3.1.0' | '3.0.3'\n title: string\n version: string\n}\n\nexport interface ResolvedPluginConfig {\n pluginRunner: PluginRunner | undefined\n transformer: TheoTransformer | undefined\n resolvedBatching: { max?: number } | undefined\n theoUi: TheoUiDetectResult | undefined\n csrfMode: 'off' | 'warn' | 'strict'\n securityHeaders: SecurityHeadersConfig | undefined\n disallowed: DisallowedConfig | undefined\n cors: CorsConfig | undefined\n auditLogger: AuditLogger | undefined\n devtoolsEnabled: boolean\n // P#3 T1.1 — undefined when config.openapi opt-out; otherwise validated config block\n openapi: ResolvedOpenApi | undefined\n // P#3 T1.1 — distDir resolved relative to projectRoot for openapi-emit output\n distDir: string\n}\n\n/**\n * Default config when load fails — keeps the plugin running with safe defaults.\n */\nconst DEFAULT_RESOLVED: ResolvedPluginConfig = {\n pluginRunner: undefined,\n transformer: undefined,\n resolvedBatching: undefined,\n theoUi: undefined,\n csrfMode: 'strict',\n securityHeaders: undefined,\n disallowed: undefined,\n cors: undefined,\n auditLogger: undefined,\n devtoolsEnabled: true,\n openapi: undefined,\n distDir: '.theokit',\n}\n\nexport async function resolvePluginConfig(projectRoot: string): Promise<ResolvedPluginConfig> {\n try {\n const userConfig = await loadConfig(projectRoot)\n const pluginRunner = await createPluginRunnerFromConfig(userConfig.plugins)\n const transformer = resolveTransformer(userConfig.serialization)\n\n // Batching: Zod admits `boolean | { max?: number }`; normalize to object.\n let resolvedBatching: { max?: number } | undefined\n if (userConfig.batching === true) {\n resolvedBatching = {}\n } else if (typeof userConfig.batching === 'object') {\n resolvedBatching = userConfig.batching\n }\n\n const theoUi = detectTheoUi(projectRoot, userConfig.ui)\n\n // Audit logger — duck-type validation (consumer may pass any shape with `log()`).\n let auditLogger: AuditLogger | undefined\n const maybeLogger = (userConfig as { audit?: { logger?: unknown } }).audit?.logger\n if (maybeLogger && typeof (maybeLogger as { log?: unknown }).log === 'function') {\n auditLogger = maybeLogger as AuditLogger\n }\n\n return {\n pluginRunner,\n transformer,\n resolvedBatching,\n theoUi,\n csrfMode: userConfig.security?.csrf ?? 'strict',\n securityHeaders: userConfig.security?.headers,\n disallowed: userConfig.security?.disallowed,\n cors: userConfig.security?.cors,\n auditLogger,\n devtoolsEnabled: userConfig.devtools !== false,\n openapi: userConfig.openapi,\n distDir: userConfig.distDir,\n }\n } catch {\n // Config load errors are surfaced elsewhere (validate-structure).\n // Return safe defaults so middlewares can run without hooks.\n return DEFAULT_RESOLVED\n }\n}\n","import { resolve, basename } from 'node:path'\n\nimport type { ViteDevServer } from 'vite'\n\nimport { broadcastRouteManifest } from '../devtools/server-side/route-manifest.js'\nimport { scanRoutes } from '../router/scan.js'\nimport { isRouteFile } from '../router/types.js'\nimport {\n CsrfReadinessStore,\n type AuditLogger,\n type CorsConfig,\n type DisallowedConfig,\n type PluginRunner,\n type RateLimitConfig,\n type SecurityHeadersConfig,\n} from '../server/internal-api.js'\nimport type { TheoTransformer } from '../server/transformer.js'\nimport type { ServicesConfig } from '../services/index.js'\n\nimport { createActionMiddleware } from './action-middleware.js'\nimport { createAgentMiddleware } from './agent-middleware.js'\nimport { createApiMiddleware } from './api-middleware.js'\nimport type { ResolvedOpenApi } from './config-resolve.js'\nimport { setupSsrDevMiddleware } from './ssr-dev-middleware.js'\nimport { setupWsUpgrade } from './ws-upgrade.js'\n\n/**\n * Inputs assembled by `theoPlugin` from its closure variables + the\n * resolvedOpenApi from `config-resolve.ts`. Kept as a struct so the\n * extraction is a pure pass-through with no behavior change.\n */\nexport interface ConfigureServerCtx {\n projectRoot: string\n appDir: string\n /** Absolute backend root (config `serverDir`, default `<projectRoot>/server`) — route/action scan (#95). */\n serverDir: string\n /** Agents dir NAME (config `agentsDir`, default \"agents\") relative to projectRoot — agent-middleware scan. */\n agentsDir: string\n resolvedDistDir: string\n ssrEnabled: boolean\n isDevMode: { value: boolean }\n pluginRunner: PluginRunner | undefined\n transformer: TheoTransformer | undefined\n resolvedBatching: { max?: number } | undefined\n csrfMode: 'off' | 'warn' | 'strict'\n securityHeaders: SecurityHeadersConfig | undefined\n disallowed: DisallowedConfig | undefined\n cors: CorsConfig | undefined\n auditLogger: AuditLogger | undefined\n resolvedOpenApi: ResolvedOpenApi | undefined\n services: ServicesConfig | undefined\n rateLimit: RateLimitConfig | undefined\n csrfReadinessStoreOverride: CsrfReadinessStore | undefined\n virtualEntryServerId: string\n resolvedManifestId: string\n}\n\n/**\n * Runs the body of Vite's `configureServer` hook. Pure extraction —\n * preserves invocation order of every server.watcher event, middleware\n * registration, and ws hook so EC-10 (Vite hook ordering side effects)\n * is honored.\n */\n\nexport async function runConfigureServer(\n server: ViteDevServer,\n ctx: ConfigureServerCtx,\n): Promise<void> {\n // T1.2 — mark dev mode so transformIndexHtml injects devtools script\n // and resolveId/load serve the virtual module. Build mode never calls\n // configureServer, so isDevMode stays false and injection is skipped.\n ctx.isDevMode.value = true\n\n // T2.4 — expose dev server WS to server-side broadcast helper\n ;(globalThis as { __theoViteHotServer?: typeof server }).__theoViteHotServer = server\n\n // T3.1 — re-broadcast the route manifest when the devtools bridge asks.\n server.ws.on('theo:devtools:request-manifest', () => {\n try {\n const tree = scanRoutes(ctx.appDir)\n broadcastRouteManifest(tree)\n } catch {\n /* fail silently — dev-only convenience */\n }\n })\n\n // Server middleware (action before API — more specific prefix first). `serverDir` honors the\n // config `serverDir` (default `<projectRoot>/server`) — no longer hardcoded (#95).\n const serverDir = ctx.serverDir\n server.middlewares.use(\n createActionMiddleware(server, serverDir, {\n pluginRunner: ctx.pluginRunner,\n csrfMode: ctx.csrfMode,\n disallowed: ctx.disallowed,\n }),\n )\n // M2 — agent convention (`/api/agents/<name>`) before the generic api-middleware\n // (mirrors the action prefix). Agents live at <projectRoot>/agents (LOCKED naming).\n // CSRF is enforced in `mountAgent` (shared dev+prod point) at the same mode as routes.\n server.middlewares.use(\n createAgentMiddleware(server, ctx.projectRoot, ctx.csrfMode, ctx.agentsDir),\n )\n // Wave 2 completion — services-proxy prefixes flow through to the\n // api-middleware so it can call `next()` for paths that should be\n // forwarded to a sidecar by Vite's proxyMiddleware.\n const servicesProxyPrefixes =\n ctx.services && Object.keys(ctx.services).length > 0\n ? Object.values(ctx.services).map((s) => s.proxy)\n : []\n // Auto-instantiate a dev-only CsrfReadinessStore so the devtools\n // CSRF Readiness tab works out-of-the-box.\n const csrfReadinessStore = ctx.csrfReadinessStoreOverride ?? new CsrfReadinessStore()\n server.middlewares.use(\n createApiMiddleware(server, serverDir, {\n rateLimitConfig: ctx.rateLimit,\n pluginRunner: ctx.pluginRunner,\n batching: ctx.resolvedBatching,\n transformer: ctx.transformer,\n csrfMode: ctx.csrfMode,\n securityHeaders: ctx.securityHeaders,\n disallowed: ctx.disallowed,\n cors: ctx.cors,\n auditLogger: ctx.auditLogger,\n servicesProxyPrefixes,\n csrfReadinessStore,\n }),\n )\n\n // EC-1: warn if theo.config.ts is edited during dev session.\n const configPath = resolve(ctx.projectRoot, 'theo.config.ts')\n server.watcher.on('change', (file) => {\n if (file === configPath) {\n console.warn(\n '\\n[theokit] theo.config.ts changed; restart dev server for plugin changes to take effect\\n',\n )\n }\n })\n\n // T2.2 (architecture-medium-deferrals) — SSR dev middleware extracted.\n if (ctx.ssrEnabled) {\n setupSsrDevMiddleware(server, {\n projectRoot: ctx.projectRoot,\n virtualEntryServerId: ctx.virtualEntryServerId,\n securityHeaders: ctx.securityHeaders,\n })\n }\n\n // Frontend HMR watcher\n function handleRouteChange(filePath: string): void {\n if (!isRouteFile(basename(filePath))) return\n if (!filePath.startsWith(ctx.appDir)) return\n\n const mod = server.moduleGraph.getModuleById(ctx.resolvedManifestId)\n if (mod) {\n server.moduleGraph.invalidateModule(mod)\n server.ws.send({ type: 'full-reload' })\n }\n }\n\n server.watcher.on('add', handleRouteChange)\n server.watcher.on('unlink', handleRouteChange)\n\n // P#3 T1.1 — Dev-mode OpenAPI emit. Gated on config.openapi !== undefined.\n if (ctx.resolvedOpenApi !== undefined) {\n const openApiCfg = ctx.resolvedOpenApi\n const { reEmitOpenApi } = await import('./openapi-emit/dev-emit.js')\n // #95 — the OpenAPI dev-emit scans the SAME backend root as the route middleware (honor serverDir).\n const openApiServerDir = ctx.serverDir\n const openApiDistDir = resolve(ctx.projectRoot, ctx.resolvedDistDir)\n const isRouteFileForOpenApi = (file: string): boolean =>\n file.startsWith(openApiServerDir) && /\\.(ts|tsx|js|mjs)$/.test(file)\n // Boot emit (fire-and-forget — boot doesn't await)\n void reEmitOpenApi(openApiServerDir, openApiDistDir, openApiCfg)\n // Watcher emit\n server.watcher.on('change', (file: string) => {\n if (isRouteFileForOpenApi(file)) {\n void reEmitOpenApi(openApiServerDir, openApiDistDir, openApiCfg)\n }\n })\n server.watcher.on('add', (file: string) => {\n if (isRouteFileForOpenApi(file)) {\n void reEmitOpenApi(openApiServerDir, openApiDistDir, openApiCfg)\n }\n })\n server.watcher.on('unlink', (file: string) => {\n if (isRouteFileForOpenApi(file)) {\n void reEmitOpenApi(openApiServerDir, openApiDistDir, openApiCfg)\n }\n })\n }\n\n // T2.3 (architecture-medium-deferrals) — WS upgrade extracted to sibling.\n setupWsUpgrade(server, ctx.projectRoot)\n\n // T2.4 — clear devtools WS reference on shutdown\n server.httpServer?.once('close', () => {\n ;(globalThis as { __theoViteHotServer?: unknown }).__theoViteHotServer = undefined\n })\n}\n","/**\n * T3.1 — Translate the server-side RouteNode tree into the devtools-facing\n * RouteManifest shape, then broadcast over the HMR bridge.\n *\n * No-op in production / when no dev server is attached.\n *\n * NEVER use dangerouslySetInnerHTML in any devtools component — see plan EC-20.\n */\nimport type { RouteNode } from '../../core/contracts/route-node.js'\nimport type { RouteInfo, RouteManifest } from '../shared.js'\n\nimport { broadcastToDevtools } from './broadcast.js'\n\ninterface FlattenAccumulator {\n routes: RouteInfo[]\n}\n\nfunction buildPath(parents: string[], segment: string): string {\n const joined = [...parents, segment].filter(Boolean).join('/')\n return '/' + joined\n}\n\nfunction walk(\n node: RouteNode,\n parents: string[],\n layoutChain: string[],\n acc: FlattenAccumulator,\n): void {\n const path = buildPath(parents, node.segment)\n // Layout file (if any) joins the chain for any descendant page.\n const nextChain = node.layout ? [...layoutChain, node.layout] : layoutChain\n\n if (node.page) {\n acc.routes.push({\n path,\n absoluteFilePath: node.page,\n layoutChain: [...layoutChain], // freeze chain at this leaf\n hasLoading: Boolean(node.loading),\n hasError: Boolean(node.error),\n hasNotFound: Boolean(node.notFound),\n })\n }\n\n for (const child of node.children) {\n const nextParents = node.segment ? [...parents, node.segment] : parents\n walk(child, nextParents, nextChain, acc)\n }\n}\n\n/**\n * Pure helper: convert RouteNode tree → RouteManifest for the devtools UI.\n * Exported for testing.\n */\nexport function buildRouteManifest(tree: RouteNode): RouteManifest {\n const acc: FlattenAccumulator = { routes: [] }\n walk(tree, [], [], acc)\n return { routes: acc.routes }\n}\n\n/**\n * Broadcast the manifest to the devtools UI over the HMR bridge.\n * Safe to call from any dev-server context — no-op in production.\n */\nexport function broadcastRouteManifest(tree: RouteNode): void {\n const manifest = buildRouteManifest(tree)\n broadcastToDevtools('theo:devtools:manifest', manifest)\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Build-time file-system scanner. All paths originate from `appDir`,\n * which is itself derived from `process.cwd()`. No HTTP input flows here.\n */\nimport { readdirSync, statSync, existsSync } from 'node:fs'\nimport { join, resolve } from 'node:path'\n\nimport type { RouteNode, RouteFileName } from './types.js'\nimport { ROUTE_FILE_NAMES, ROUTE_FILE_EXTENSIONS } from './types.js'\n\nfunction toNodeKey(name: RouteFileName): 'page' | 'layout' | 'error' | 'loading' | 'notFound' {\n if (name === 'not-found') return 'notFound'\n return name\n}\n\nfunction setRouteFile(\n node: RouteNode,\n key: 'page' | 'layout' | 'error' | 'loading' | 'notFound',\n value: string,\n): void {\n switch (key) {\n case 'page':\n node.page = value\n break\n case 'layout':\n node.layout = value\n break\n case 'error':\n node.error = value\n break\n case 'loading':\n node.loading = value\n break\n case 'notFound':\n node.notFound = value\n break\n }\n}\n\nfunction attachRouteFiles(node: RouteNode, dir: string): void {\n // Check route files with extension priority (.tsx > .ts > .jsx > .js)\n for (const name of ROUTE_FILE_NAMES) {\n const key = toNodeKey(name)\n if (node[key] !== undefined) continue\n for (const ext of ROUTE_FILE_EXTENSIONS) {\n const filename = `${name}${ext}`\n if (existsSync(join(dir, filename))) {\n setRouteFile(node, key, resolve(dir, filename))\n break\n }\n }\n }\n}\n\ninterface ParsedSegment {\n kind: 'static' | 'dynamic' | 'catchall'\n paramName?: string\n}\n\n/**\n * Classify a directory name as a static, dynamic (`[param]`), or catch-all\n * (`[...slug]`) route segment. Throws at build time on:\n * - optional catch-all `[[...slug]]` (EC-4 — not supported yet; see plan Q3)\n * - a param name outside `[A-Za-z0-9_]` (EC-5 — react-router would silently mis-match)\n * Order matters: catch-all is checked before single dynamic so `[...slug]` is\n * not misread as a dynamic param literally named `...slug`.\n */\nfunction parseSegment(name: string): ParsedSegment {\n if (name.startsWith('[[')) {\n throw new Error(\n `Optional catch-all route segment is not supported yet: '${name}'. ` +\n `Use '[...param]' for catch-all, or split into explicit segments.`,\n )\n }\n const catchAll = /^\\[\\.\\.\\.(.+)\\]$/.exec(name)\n if (catchAll) return checkedSegment('catchall', catchAll[1], name)\n const dynamic = /^\\[(.+)\\]$/.exec(name)\n if (dynamic) return checkedSegment('dynamic', dynamic[1], name)\n return { kind: 'static' }\n}\n\nfunction checkedSegment(\n kind: 'dynamic' | 'catchall',\n paramName: string,\n name: string,\n): ParsedSegment {\n if (!/^\\w+$/.test(paramName)) {\n throw new Error(\n `Invalid route param '${paramName}' in segment '${name}' — ` +\n `param names must match [A-Za-z0-9_] (react-router constraint).`,\n )\n }\n return { kind, paramName }\n}\n\nfunction nodeHasContent(node: RouteNode): boolean {\n return (\n node.page !== undefined ||\n node.layout !== undefined ||\n node.error !== undefined ||\n node.loading !== undefined ||\n node.notFound !== undefined ||\n node.children.length > 0\n )\n}\n\nfunction buildChildPath(routePath: string, urlSegment: string): string {\n if (!urlSegment) return routePath\n return routePath === '/' ? `/${urlSegment}` : `${routePath}/${urlSegment}`\n}\n\n/** Scan one subdirectory into a child RouteNode, attaching dynamic-segment metadata. */\nfunction scanChild(parentDir: string, dirName: string, routePath: string): RouteNode {\n const isRouteGroup = dirName.startsWith('(') && dirName.endsWith(')')\n const urlSegment = isRouteGroup ? '' : dirName\n const childPath = buildChildPath(routePath, urlSegment)\n const child = scanDir(join(parentDir, dirName), isRouteGroup ? '' : dirName, childPath)\n\n // Dynamic / catch-all segment metadata (throws on [[...]] or invalid charset).\n if (!isRouteGroup) {\n const parsed = parseSegment(dirName)\n if (parsed.kind !== 'static' && parsed.paramName !== undefined) {\n child.dynamic = { paramName: parsed.paramName, catchAll: parsed.kind === 'catchall' }\n }\n }\n return child\n}\n\nfunction scanDir(dir: string, segment: string, routePath: string): RouteNode {\n const node: RouteNode = { segment, path: routePath, children: [] }\n attachRouteFiles(node, dir)\n\n for (const entry of readdirSync(dir, { withFileTypes: true })) {\n if (!entry.isDirectory()) continue\n if (entry.name.startsWith('_') || entry.name.startsWith('.')) continue\n\n const child = scanChild(dir, entry.name, routePath)\n // Prune empty nodes — a directory with no route files AND no children is\n // irrelevant to the manifest.\n if (nodeHasContent(child)) {\n node.children.push(child)\n }\n }\n\n return node\n}\n\nexport function scanRoutes(appDir: string): RouteNode {\n if (!existsSync(appDir)) {\n throw new Error(`App directory does not exist: ${appDir}`)\n }\n if (!statSync(appDir).isDirectory()) {\n throw new Error(`App path is not a directory: ${appDir}`)\n }\n return scanDir(appDir, '', '/')\n}\n","// T2.2 (architecture-cleanup) — RouteNode moved to core/contracts/route-node.ts\n// (canonical home per ADR-0001 v3). Re-export keeps the local path stable.\nexport type { RouteNode } from '../core/contracts/route-node.js'\n\nexport const ROUTE_FILE_NAMES = ['page', 'layout', 'error', 'loading', 'not-found'] as const\n\nexport type RouteFileName = (typeof ROUTE_FILE_NAMES)[number]\n\nexport const ROUTE_FILE_EXTENSIONS = ['.tsx', '.ts', '.jsx', '.js'] as const\n\nconst ROUTE_FILE_REGEX = /^(page|layout|error|loading|not-found)\\.(tsx|ts|jsx|js)$/\n\nexport function isRouteFile(filename: string): boolean {\n return ROUTE_FILE_REGEX.test(filename)\n}\n","import { randomUUID } from 'node:crypto'\n\nimport type { ViteDevServer, Connect } from 'vite'\n\nimport {\n createViteLoader,\n executeAction,\n findSuggestion,\n logRequest,\n scanServerActions,\n sendError,\n type CsrfMode,\n type DisallowedConfig,\n type PluginRunner,\n} from '../server/internal-api.js'\n\nconst PREFIX = '/api/__actions/'\n\nexport interface ActionMiddlewareOptions {\n pluginRunner?: PluginRunner\n /**\n * CSRF mode passed through to `executeAction`. Parity with the route\n * middleware — actions inherit the same warn/strict policy as routes.\n * Defaults to 'strict' (the 0.3.0 default).\n */\n csrfMode?: CsrfMode\n /**\n * T5.1 — Rails-inspired per-route escalation. Forwarded to `executeAction`.\n */\n disallowed?: DisallowedConfig\n}\n\nexport function createActionMiddleware(\n vite: ViteDevServer,\n serverDir: string,\n options?: ActionMiddlewareOptions,\n): Connect.NextHandleFunction {\n const loadModule = createViteLoader(vite)\n const pluginRunner = options?.pluginRunner\n const csrfMode: CsrfMode = options?.csrfMode ?? 'strict'\n const disallowed = options?.disallowed\n return (req, res, next) => {\n void (async () => {\n const url = req.url ?? ''\n if (!url.startsWith(PREFIX)) {\n next()\n return\n }\n\n const requestId = randomUUID()\n const start = Date.now()\n res.setHeader('x-request-id', requestId)\n\n const pathAfterPrefix = url.slice(PREFIX.length).split('?')[0]\n const segments = pathAfterPrefix.split('/').filter(Boolean)\n\n if (segments.length < 2) {\n sendError(\n res,\n 'BAD_REQUEST',\n 'Action URL must be /api/__actions/{file}/{exportName}',\n 400,\n undefined,\n requestId,\n )\n logRequest({\n method: req.method ?? 'POST',\n url,\n status: 400,\n duration: Date.now() - start,\n requestId,\n })\n return\n }\n\n const exportName = segments[segments.length - 1]\n const actionPath = segments.slice(0, -1).join('/')\n\n const actions = scanServerActions(serverDir)\n const action = actions.find((a) => a.actionPath === actionPath)\n\n if (!action) {\n const actionPaths = actions.map((a) => a.actionPath)\n const suggestion = findSuggestion(actionPath, actionPaths)\n const msg = suggestion\n ? `Action file \"${actionPath}\" not found. Did you mean: ${suggestion}?`\n : `Action file \"${actionPath}\" not found`\n sendError(res, 'NOT_FOUND', msg, 404, undefined, requestId)\n logRequest({\n method: req.method ?? 'POST',\n url,\n status: 404,\n duration: Date.now() - start,\n requestId,\n })\n return\n }\n\n await executeAction(\n action.filePath,\n exportName,\n req,\n res,\n loadModule,\n serverDir,\n requestId,\n pluginRunner,\n csrfMode,\n disallowed,\n )\n logRequest(\n {\n method: req.method ?? 'POST',\n url,\n status: res.statusCode,\n duration: Date.now() - start,\n requestId,\n },\n undefined,\n req,\n )\n })()\n }\n}\n","/**\n * M2 (theokit-ai-first) — dev middleware for the `agents/<name>.ts` convention.\n *\n * Serves `POST /api/agents/<name>` in `theokit dev` by scanning the top-level `agents/`\n * directory on each request, loading the matched module via Vite's SSR loader, and\n * streaming the M0/M1 UIMessageStream through `mountAgent` — the SAME wiring point the\n * prod server uses (`start/handlers.ts` `tryServeAgent`), so dev and build never drift.\n *\n * Registered under the reserved `/api/agents/` prefix BEFORE the generic api-middleware\n * (mirrors how the action middleware owns `/api/__actions/`); an unmatched agent path\n * falls through to `next()` so the api-middleware can 404 it consistently.\n */\nimport { randomUUID } from 'node:crypto'\nimport type { ServerResponse } from 'node:http'\n\nimport type { ViteDevServer, Connect } from 'vite'\n\nimport { isAgentCardPath } from '../server/agent/agent-card-handler.js'\nimport { getApprovalRegistry } from '../server/agent/approval-registry.js'\nimport { handleAgentApproval, isApprovalPath } from '../server/agent/approve-agent.js'\nimport { isListApprovalsPath } from '../server/agent/list-approvals-handler.js'\nimport { isMcpPath } from '../server/agent/mcp-handler.js'\nimport { mountAgent } from '../server/agent/mount-agent.js'\nimport { resolveProvider } from '../server/agent/provider-resolver.js'\nimport { serveAgentAuxRoute } from '../server/agent/serve-aux-routes.js'\nimport {\n incomingMessageToWebRequest,\n writeWebResponseToServerResponse,\n} from '../server/http/node-web-adapter.js'\nimport {\n createViteLoader,\n logRequest,\n scanAgents,\n sendError,\n type CsrfMode,\n} from '../server/internal-api.js'\n\nconst PREFIX = '/api/agents/'\n\n/**\n * M15 — serve `/.well-known/<name>/agent-card.json` (GET) if the request matches. Returns `true`\n * when it owned the request (responded or fell through to `next()`), `false` to let the caller\n * continue. Extracted from the middleware arrow to keep that function within the complexity budget.\n */\ninterface CardDeps {\n projectRoot: string\n loadModule: (filePath: string) => Promise<unknown>\n agentsDir: string\n /** M34 (#97) — CSRF mode threaded to the MCP aux route (which drives the agent → spends tokens). */\n csrfMode: CsrfMode\n}\n\n/** M4 — serve the HITL approve route `/api/agents/<name>/approve/<id>` (already matched). */\nasync function serveApprove(\n req: Connect.IncomingMessage,\n res: ServerResponse,\n urlPath: string,\n csrfMode: CsrfMode,\n log: { requestId: string; start: number },\n): Promise<void> {\n const method = (req.method ?? 'POST').toUpperCase()\n if (method !== 'POST') {\n sendError(\n res,\n 'METHOD_NOT_ALLOWED',\n 'Approve endpoints accept POST',\n 405,\n undefined,\n log.requestId,\n )\n logRequest({\n method,\n url: req.url ?? '',\n status: 405,\n duration: Date.now() - log.start,\n requestId: log.requestId,\n })\n return\n }\n try {\n const response = await handleAgentApproval(\n incomingMessageToWebRequest(req),\n urlPath,\n getApprovalRegistry(),\n csrfMode,\n )\n await writeWebResponseToServerResponse(response, res)\n } catch (err) {\n sendError(\n res,\n 'INTERNAL',\n err instanceof Error ? err.message : 'Approve handler failed',\n 500,\n undefined,\n log.requestId,\n )\n }\n logRequest({\n method,\n url: req.url ?? '',\n status: res.statusCode,\n duration: Date.now() - log.start,\n requestId: log.requestId,\n })\n}\n\n/**\n * M15/M16/M14 — serve an agent AUXILIARY route (agent card / MCP / pending-approvals listing) via\n * the shared `serveAgentAuxRoute` dispatcher (the SAME one `theokit start`'s `tryServeAgentAux`\n * uses, so dev and prod never drift). Falls through to `next()` when the path is not an aux route,\n * the method is wrong, or the agent is unknown.\n */\nasync function serveAux(\n req: Connect.IncomingMessage,\n res: ServerResponse,\n next: Connect.NextFunction,\n urlPath: string,\n deps: CardDeps,\n): Promise<void> {\n const requestId = randomUUID()\n const start = Date.now()\n try {\n const request = incomingMessageToWebRequest(req)\n const response = await serveAgentAuxRoute(request, urlPath, {\n agents: scanAgents(deps.projectRoot, deps.agentsDir),\n loadModule: deps.loadModule,\n baseUrl: `http://${req.headers.host ?? 'localhost'}`,\n csrfMode: deps.csrfMode,\n // M39 — the thread follow-up route drives the agent; resolve the key on demand.\n resolveApiKey: () => resolveProvider().apiKey,\n })\n if (response === null) {\n next()\n return\n }\n res.setHeader('x-request-id', requestId)\n await writeWebResponseToServerResponse(response, res)\n } catch (err) {\n res.setHeader('x-request-id', requestId)\n sendError(\n res,\n 'INTERNAL',\n err instanceof Error ? err.message : 'Agent aux handler failed',\n 500,\n undefined,\n requestId,\n )\n }\n logRequest({\n method: (req.method ?? 'GET').toUpperCase(),\n url: req.url ?? '',\n status: res.statusCode,\n duration: Date.now() - start,\n requestId,\n })\n}\n\nexport function createAgentMiddleware(\n vite: ViteDevServer,\n projectRoot: string,\n csrfMode: CsrfMode = 'strict',\n agentsDir = 'agents',\n): Connect.NextHandleFunction {\n const loadModule = createViteLoader(vite)\n return (req, res, next) => {\n void (async () => {\n const url = req.url ?? ''\n\n // M15 — A2A agent card at `/.well-known/<name>/agent-card.json` (GET). The match check is\n // SYNC so a non-card request still calls `next()` synchronously (no `await` before it — a\n // dev-middleware contract some tests rely on); only a real card path enters the async path.\n if (isAgentCardPath(url.split('?')[0]) !== null) {\n await serveAux(req, res, next, url.split('?')[0], {\n projectRoot,\n loadModule,\n agentsDir,\n csrfMode,\n })\n return\n }\n\n if (!url.startsWith(PREFIX)) {\n next()\n return\n }\n\n const requestId = randomUUID()\n const start = Date.now()\n res.setHeader('x-request-id', requestId)\n\n const urlPath = url.split('?')[0]\n\n // HITL approve route (`/api/agents/<name>/approve/<id>`, M4) — resolve the pending approval.\n // Branches BEFORE the agent-path exact match (the approve path never equals an `agentPath`).\n // Extracted to keep this arrow within the complexity budget (G6).\n if (isApprovalPath(urlPath)) {\n await serveApprove(req, res, urlPath, csrfMode, { requestId, start })\n return\n }\n\n // M14 (list approvals) + M16 (MCP) — both are agent aux routes served by the shared\n // dispatcher. Branch BEFORE the agent exact-match (neither path equals an `agentPath`).\n if (isListApprovalsPath(urlPath) !== null || isMcpPath(urlPath) !== null) {\n await serveAux(req, res, next, urlPath, { projectRoot, loadModule, agentsDir, csrfMode })\n return\n }\n\n const agent = scanAgents(projectRoot, agentsDir).find((a) => a.agentPath === urlPath)\n if (!agent) {\n // Not a known agent — let the api-middleware own the 404 (single 404 shape).\n next()\n return\n }\n\n const method = (req.method ?? 'POST').toUpperCase()\n if (method !== 'POST') {\n sendError(\n res,\n 'METHOD_NOT_ALLOWED',\n 'Agent endpoints accept POST',\n 405,\n undefined,\n requestId,\n )\n logRequest({ method, url, status: 405, duration: Date.now() - start, requestId })\n return\n }\n\n try {\n const mod = await loadModule(agent.filePath)\n const apiKey = resolveProvider().apiKey\n const request = incomingMessageToWebRequest(req)\n const response = await mountAgent(mod, request, apiKey, {\n source: agent.filePath,\n csrfMode,\n projectRoot,\n })\n await writeWebResponseToServerResponse(response, res)\n } catch (err) {\n sendError(\n res,\n 'INTERNAL',\n err instanceof Error ? err.message : 'Agent handler failed',\n 500,\n undefined,\n requestId,\n )\n }\n logRequest({ method, url, status: res.statusCode, duration: Date.now() - start, requestId })\n })()\n }\n}\n","/**\n * M15 (theokit-ai-first) — serve the A2A agent card at `/.well-known/<name>/agent-card.json`.\n *\n * `buildAgentCard` (@theokit/agents) is the pure generator; this handler compiles a loaded agent\n * module to its tools + streaming capability, builds the card, and returns it as a Web-Standard\n * JSON `Response` (G8). The dev middleware + prod handler branch to this before the agent POST route.\n */\nimport {\n type AgentManifestEntry,\n buildAgentCard,\n compileAgentModule,\n} from '@theokit/agents'\n\nconst WELL_KNOWN = /^\\/\\.well-known\\/([^/]+)\\/agent-card\\.json$/\n\n/** Return the agent name when `urlPath` is a well-known card path, else `null`. */\nexport function isAgentCardPath(urlPath: string): string | null {\n const match = WELL_KNOWN.exec(urlPath)\n return match ? decodeURIComponent(match[1]) : null\n}\n\n/** Build a minimal manifest entry (the subset `buildAgentCard` reads) from a compiled agent. */\nfunction toManifestEntry(name: string, route: string, mod: unknown): AgentManifestEntry {\n const compiled = compileAgentModule(mod, `agent card for \"${name}\"`)\n return {\n name,\n route,\n stream: compiled.stream,\n mainLoop: { method: '', strategy: '' },\n guards: [],\n interceptors: [],\n tools: compiled.tools.map((t) => ({\n name: t.name,\n description: t.description,\n approval: false,\n trace: false,\n audit: false,\n })),\n subAgents: [],\n }\n}\n\n/**\n * Serve the A2A card for a loaded agent module. Returns 200 with the card JSON, or 500 with an\n * error body if the module is not a valid agent (fail-clear, not a silent empty card).\n */\nexport function handleAgentCard(mod: unknown, name: string, route: string, baseUrl: string): Response {\n try {\n const card = buildAgentCard(toManifestEntry(name, route, mod), { baseUrl })\n return new Response(JSON.stringify(card), {\n status: 200,\n headers: { 'content-type': 'application/json; charset=utf-8' },\n })\n } catch (err) {\n return new Response(\n JSON.stringify({ error: { code: 'AGENT_CARD_FAILED', message: err instanceof Error ? err.message : 'card build failed' } }),\n { status: 500, headers: { 'content-type': 'application/json; charset=utf-8' } },\n )\n }\n}\n","/**\n * M4 (theokit-ai-first) — the HITL approve endpoint: `POST /api/agents/<name>/approve/<approvalId>`.\n *\n * The counterpart to `mountAgent`'s HITL pause. While a gated tool holds the SDK run paused (the\n * awaited `pre_tool_call` hook), the client POSTs here with `{ approved }`; this resolves the\n * pending approval in the shared registry, which un-pauses the run (allow) or vetoes the tool (deny).\n *\n * Web-Standard `Request` → `Response`, one wiring point shared by dev (vite middleware) and prod\n * (built server) so the two never drift (EC-4 parity with `mountAgent`). The registry is INJECTED —\n * dev/prod pass the process singleton (`getApprovalRegistry`), tests pass a fresh instance.\n */\nimport { validateCsrfRequest, type CsrfMode } from '../security/csrf.js'\n\nimport type { ApprovalDecision, ApprovalRegistry } from './approval-registry.js'\n\n/** The path segment separating the agent name from the approval id. */\nconst APPROVE_SEGMENT = '/approve/'\n\n/**\n * Extract the `<approvalId>` from a `/api/agents/<name>/approve/<approvalId>` path.\n * Returns `null` when the path has no `/approve/` segment or an empty / nested id.\n */\nexport function parseApprovalId(urlPath: string): string | null {\n const at = urlPath.indexOf(APPROVE_SEGMENT)\n if (at === -1) return null\n const id = urlPath.slice(at + APPROVE_SEGMENT.length)\n return id.length > 0 && !id.includes('/') ? id : null\n}\n\n/** True when `urlPath` targets a HITL approve endpoint (used by dev/prod routing to branch early). */\nexport function isApprovalPath(urlPath: string): boolean {\n return urlPath.includes(APPROVE_SEGMENT)\n}\n\nfunction jsonError(status: number, code: string, message: string): Response {\n return new Response(JSON.stringify({ error: { code, message } }), {\n status,\n headers: { 'content-type': 'application/json' },\n })\n}\n\n/**\n * M20 — cap on the serialized custom payload (16 KiB). A payload is a small structured note\n * (edited args, a reviewer comment), not a data channel — an oversized one is rejected fail-fast\n * rather than silently truncated (Rule 8).\n */\nconst MAX_PAYLOAD_BYTES = 16 * 1024\n\n/**\n * Extract an {@link ApprovalDecision} from an untrusted body; `null` when the shape is wrong.\n *\n * M20 — accepts an optional `reason` (string) and `payload` (object, capped at\n * {@link MAX_PAYLOAD_BYTES}). Backward-compatible: `{ approved }` and `{ approved, reason }` parse\n * unchanged. A non-object or oversized `payload` is rejected (returns `null` → the route 400s).\n *\n * @public\n */\nexport function parseApprovalBody(body: unknown): ApprovalDecision | null {\n if (typeof body !== 'object' || body === null) return null\n const b = body as Record<string, unknown>\n if (typeof b.approved !== 'boolean') return null\n const decision: ApprovalDecision = { approved: b.approved }\n if (b.reason !== undefined) {\n if (typeof b.reason !== 'string') return null\n decision.reason = b.reason\n }\n if (b.payload !== undefined) {\n if (typeof b.payload !== 'object' || b.payload === null || Array.isArray(b.payload)) return null\n if (JSON.stringify(b.payload).length > MAX_PAYLOAD_BYTES) return null\n decision.payload = b.payload\n }\n return decision\n}\n\n/**\n * Resolve a pending HITL approval. CSRF-guarded like `mountAgent` (the custom `X-Theo-Action`\n * header + Origin match) — a cross-origin POST must not approve a paused tool. Returns:\n * 403 CSRF_FAILED — strict CSRF check failed\n * 400 BAD_REQUEST — no `/approve/<id>` in the path, or body lacks a boolean `approved`\n * 404 NOT_PENDING — the id is unknown or already settled (idempotent double-submit)\n * 200 { resolved:true } — the approval was settled by this call\n */\nexport async function handleAgentApproval(\n request: Request,\n urlPath: string,\n registry: ApprovalRegistry,\n csrfMode: CsrfMode = 'strict',\n): Promise<Response> {\n if (csrfMode !== 'off') {\n const csrf = validateCsrfRequest(request)\n if (!csrf.valid && csrfMode === 'strict') {\n return jsonError(403, 'CSRF_FAILED', `CSRF check failed: ${csrf.reason}`)\n }\n }\n\n const approvalId = parseApprovalId(urlPath)\n if (approvalId === null) {\n return jsonError(400, 'BAD_REQUEST', 'Approval path must be /api/agents/<name>/approve/<id>.')\n }\n\n let body: unknown = null\n try {\n body = await request.json()\n } catch {\n /* invalid/empty JSON → handled below as a 400 */\n }\n const parsed = parseApprovalBody(body)\n if (parsed === null) {\n return jsonError(400, 'BAD_REQUEST', 'Request body must contain a boolean `approved`.')\n }\n\n const resolved = registry.resolve(approvalId, parsed)\n if (!resolved) {\n return jsonError(404, 'NOT_PENDING', `No pending approval for id '${approvalId}'.`)\n }\n return new Response(JSON.stringify({ resolved: true }), {\n status: 200,\n headers: { 'content-type': 'application/json' },\n })\n}\n","/**\n * M14 (theokit-ai-first) — GET /api/agents/<name>/approvals: list pending HITL approvals.\n *\n * Serves `ApprovalRegistry.list()` as JSON. Single-process contract (ADR 0038) — the list is\n * process-wide; the `<name>` segment is accepted for a future per-agent/durable store but the\n * in-process registry lists all pending approvals. Web Standards Response (G8).\n */\nimport type { ApprovalRegistry } from './approval-registry.js'\n\nconst LIST_PATH = /^\\/api\\/agents\\/([^/]+)\\/approvals$/\n\n/** Return the agent name when `urlPath` is the approvals-listing path, else `null`. */\nexport function isListApprovalsPath(urlPath: string): string | null {\n const match = LIST_PATH.exec(urlPath)\n return match ? decodeURIComponent(match[1]) : null\n}\n\n/** Serve the currently-pending approvals as `{ approvals: [...] }` JSON. */\nexport function handleListApprovals(registry: ApprovalRegistry): Response {\n return new Response(JSON.stringify({ approvals: registry.list() }), {\n status: 200,\n headers: { 'content-type': 'application/json; charset=utf-8' },\n })\n}\n","import {\n encodeSse,\n formatSseFrame,\n RUN_ID_HEADER,\n SSE_BASE_HEADERS,\n SSE_DONE_FRAME,\n} from './durable-ui-message-stream-response.js'\nimport type { RunEventCache } from './run-event-cache.js'\n\n/**\n * M37 (ADR-0046 D5) — the reconnect / observe endpoint handler for\n * `GET /api/agents/<name>/runs/<runId>/stream`.\n *\n * Replays the frames the client missed (`seq > Last-Event-ID`, SSE-native), then\n * — if the run is still live — follows the live tail; a SECOND client can\n * observe a run a first started. Run already ended ⇒ replay + `[DONE]`. Unknown\n * `runId` ⇒ 404. The request `AbortSignal` unsubscribes on disconnect.\n */\n\nconst RUN_STREAM_PATH = /^\\/api\\/agents\\/([^/]+)\\/runs\\/([^/]+)\\/stream$/\n\n/**\n * Match `GET /api/agents/<name>/runs/<runId>/stream`; returns the decoded\n * `{ name, runId }` or `null` to fall through (mirrors `isMcpPath` etc.).\n */\nexport function isAgentRunStreamPath(urlPath: string): { name: string; runId: string } | null {\n const m = RUN_STREAM_PATH.exec(urlPath)\n return m ? { name: decodeURIComponent(m[1]), runId: decodeURIComponent(m[2]) } : null\n}\n\n/** Parse `Last-Event-ID` (a frame `seq`) to a number; absent/invalid ⇒ -1 (replay from the start). */\nfunction parseLastEventId(raw: string | null): number {\n if (raw === null) return -1\n const n = Number.parseInt(raw, 10)\n return Number.isInteger(n) && n >= 0 ? n : -1\n}\n\nexport function handleAgentRunReconnect(\n runId: string,\n request: Request,\n cache: RunEventCache,\n): Response {\n // Unknown run ⇒ 404 (a run never started here, or already evicted).\n if (!cache.has(runId)) {\n return new Response(\n JSON.stringify({ error: { code: 'RUN_NOT_FOUND', message: `Unknown run '${runId}'.` } }),\n {\n status: 404,\n headers: { 'content-type': 'application/json' },\n },\n )\n }\n\n const afterSeq = parseLastEventId(request.headers.get('last-event-id'))\n\n const stream = new ReadableStream<Uint8Array>({\n start(controller) {\n let closed = false\n const safeEnqueue = (text: string): void => {\n if (closed) return\n try {\n controller.enqueue(encodeSse(text))\n } catch {\n /* controller already closed by an abort race — ignore */\n }\n }\n const close = (): void => {\n if (closed) return\n safeEnqueue(SSE_DONE_FRAME)\n closed = true\n try {\n controller.close()\n } catch {\n /* already closed */\n }\n }\n\n // Atomic: snapshot replay frames AND subscribe to the live tail in one tick.\n const res = cache.attach(\n runId,\n afterSeq,\n (frame) => {\n safeEnqueue(formatSseFrame(frame.seq, frame.data))\n },\n () => {\n close()\n },\n )\n // Evicted between has() and attach() (rare TOCTOU) ⇒ just end the stream.\n if (!res.known) {\n close()\n return\n }\n for (const frame of res.replay) {\n safeEnqueue(formatSseFrame(frame.seq, frame.data))\n }\n if (res.ended) {\n close()\n return\n }\n // Client disconnects ⇒ detach the live listener + close.\n request.signal.addEventListener('abort', () => {\n res.unsubscribe()\n close()\n })\n },\n })\n\n return new Response(stream, { headers: { ...SSE_BASE_HEADERS, [RUN_ID_HEADER]: runId } })\n}\n","/**\n * M39 (ADR-0048) — the thread follow-up dispatcher.\n *\n * Drives a run over the M37 durable cache HEADLESS — it iterates the SDK chunk\n * generator and `cache.append`s directly, NOT via a `Response` `ReadableStream`\n * (whose backpressure would stall a run with no HTTP reader). Subscribers read\n * from the cache via the thread / reconnect stream.\n *\n * - Post to an IDLE thread ⇒ start a run + pump.\n * - Post to an ACTIVE thread ⇒ FIFO-queue the follow-up.\n * - On terminal ⇒ `registry.endRun` hands back the next queued follow-up, which\n * is dispatched as a continuation on the SAME sessionId (⇒ the SDK continues\n * the conversation via its `ConversationStorageAdapter`).\n *\n * This adds NO agent loop — it reuses the SDK run (`startRun`) + the M37 cache.\n * Single-process (ADR-0048 D2).\n */\n\nimport type { UIMessageChunk } from 'ai'\n\nimport { type RunEventCache, mintRunId } from './run-event-cache.js'\nimport type { ThreadRunRegistry } from './thread-run-registry.js'\n\nexport interface ThreadDispatchDeps {\n readonly registry: ThreadRunRegistry\n readonly cache: RunEventCache\n /** Start a run for a follow-up on `sessionId`; returns the SDK chunk stream. */\n readonly startRun: (sessionId: string, message: string) => AsyncIterable<UIMessageChunk>\n}\n\n/** Result of posting a follow-up: the started `runId` (idle thread) or `queued` (active thread). */\nexport type PostFollowUpResult = { runId: string } | { queued: true }\n\n/**\n * Post a follow-up on a thread. IDLE ⇒ start a run (returns its `runId`); ACTIVE\n * ⇒ FIFO-queue it (returns `{ queued: true }`) — dispatched when the active run ends.\n */\nexport function postThreadFollowUp(\n deps: ThreadDispatchDeps,\n sessionId: string,\n message: string,\n): PostFollowUpResult {\n if (deps.registry.getActive(sessionId) !== null) {\n deps.registry.queue(sessionId, { message })\n return { queued: true }\n }\n return { runId: startAndPump(deps, sessionId, message) }\n}\n\n/** Mint a runId, mark the thread active, and pump the run into the cache headless. */\nfunction startAndPump(deps: ThreadDispatchDeps, sessionId: string, message: string): string {\n const runId = mintRunId()\n // Register the run in the cache SYNCHRONOUSLY (before the async pump appends its\n // first frame) so a subscriber resolving the active runId can attach immediately.\n deps.cache.begin(runId)\n deps.registry.startRun(sessionId, runId)\n pumpIntoCache(deps, sessionId, runId, deps.startRun(sessionId, message))\n return runId\n}\n\n/**\n * Drive `chunks` into the cache under `runId`, then end the run and dispatch the\n * next queued follow-up (if any) as a continuation. Fire-and-forget — the caller\n * does not await the run; subscribers observe it via the cache.\n */\nfunction pumpIntoCache(\n deps: ThreadDispatchDeps,\n sessionId: string,\n runId: string,\n chunks: AsyncIterable<UIMessageChunk>,\n): void {\n void (async () => {\n try {\n for await (const chunk of chunks) {\n deps.cache.append(runId, JSON.stringify(chunk))\n }\n } catch {\n // The SDK translator owns error semantics (surfaces failures as chunks);\n // the transport guarantees only a terminated, cache-ended run.\n } finally {\n deps.cache.end(runId)\n const next = deps.registry.endRun(sessionId, runId)\n if (next !== undefined) startAndPump(deps, sessionId, next.message)\n }\n })()\n}\n","/**\n * M39 (ADR-0048) — the in-process thread→run registry.\n *\n * A thread is the existing `sessionId` (the SDK conversation key). This registry\n * tracks, per thread, the single ACTIVE run, a FIFO queue of follow-ups to\n * dispatch as continuations, and one-shot \"next run\" waiters (so a client can\n * subscribe to a thread before posting a message and attach when the run starts).\n *\n * Pure state only — it never starts a run or touches the transport. The route\n * layer owns dispatch: on `endRun` it receives the next queued follow-up (if any)\n * and starts the continuation over the M37 durable transport.\n *\n * Single-process contract (ADR-0048 D2): a multi-instance deploy needs a shared\n * registry + leasing — that is infra (TheoCloud), explicitly out of M39. The\n * interface is injectable so a durable impl slots in later without touching the\n * routes. We do NOT build a distributed store now (YAGNI).\n */\n\n/** A follow-up message queued on an active thread, dispatched as a continuation. */\nexport interface FollowUp {\n readonly message: string\n}\n\nexport interface ThreadRunRegistry {\n /** The active runId for a thread, or `null` when the thread is idle. */\n getActive(sessionId: string): string | null\n /** Mark a run started for a thread (sets it active; fires + clears one-shot waiters). */\n startRun(sessionId: string, runId: string): void\n /**\n * Mark `runId` ended for a thread. A no-op unless `runId` is the current active\n * run (a stale terminal never clears a newer run). Clears the active run and\n * returns the next FIFO follow-up to dispatch as a continuation, or `undefined`.\n */\n endRun(sessionId: string, runId: string): FollowUp | undefined\n /** FIFO-enqueue a follow-up on a thread (dispatched when the active run ends). */\n queue(sessionId: string, followUp: FollowUp): void\n /**\n * Register a ONE-SHOT waiter fired with the runId of the NEXT run started on\n * this thread. Returns an unsubscribe fn. Used by subscribe-by-thread on an\n * idle thread (attach when the next run starts).\n */\n onNextRun(sessionId: string, cb: (runId: string) => void): () => void\n}\n\ninterface ThreadState {\n activeRunId: string | null\n readonly queue: FollowUp[]\n readonly waiters: Set<(runId: string) => void>\n}\n\nexport function createInProcessThreadRunRegistry(): ThreadRunRegistry {\n const threads = new Map<string, ThreadState>()\n const ensure = (sessionId: string): ThreadState => {\n let s = threads.get(sessionId)\n if (s === undefined) {\n s = { activeRunId: null, queue: [], waiters: new Set() }\n threads.set(sessionId, s)\n }\n return s\n }\n\n return {\n getActive(sessionId) {\n return threads.get(sessionId)?.activeRunId ?? null\n },\n startRun(sessionId, runId) {\n const s = ensure(sessionId)\n s.activeRunId = runId\n // Fire one-shot waiters, then clear them (single-threaded → no re-entrancy race).\n const waiters = [...s.waiters]\n s.waiters.clear()\n for (const cb of waiters) cb(runId)\n },\n endRun(sessionId, runId) {\n const s = threads.get(sessionId)\n if (s?.activeRunId !== runId) return undefined\n s.activeRunId = null\n return s.queue.shift()\n },\n queue(sessionId, followUp) {\n ensure(sessionId).queue.push(followUp)\n },\n onNextRun(sessionId, cb) {\n const s = ensure(sessionId)\n s.waiters.add(cb)\n return () => {\n s.waiters.delete(cb)\n }\n },\n }\n}\n\nlet serverRegistry: ThreadRunRegistry | undefined\n\n/** Process-wide singleton (mirrors `getRunEventCache` / `getApprovalRegistry`). */\nexport function getThreadRunRegistry(): ThreadRunRegistry {\n serverRegistry ??= createInProcessThreadRunRegistry()\n return serverRegistry\n}\n","/**\n * M39 (ADR-0048) — the thread signal routes over the M37 durable transport:\n *\n * - `POST /api/agents/<name>/threads/<sessionId>/message` — a follow-up. ACTIVE\n * run ⇒ FIFO-queue (dispatched as a continuation when it ends); IDLE ⇒ start a\n * run. Returns `202` (the run streams headless into the cache; observe it via\n * the thread stream). Spends LLM tokens ⇒ CSRF-gated (parity with mount-agent).\n * - `GET /api/agents/<name>/threads/<sessionId>/stream` — subscribe. ACTIVE ⇒\n * attach to the durable stream (reuse the M37 reconnect handler); IDLE ⇒ wait\n * (bounded) for the next run on the thread, then attach (subscribe-then-post).\n *\n * These DRIVE the SDK run (via `makeThreadStartRun`) + the M37 cache — no new loop.\n */\n\nimport { validateCsrfRequest, type CsrfMode } from '../security/csrf.js'\n\nimport { makeThreadStartRun } from './build-agent-streamer.js'\nimport {\n encodeSse,\n formatSseFrame,\n RUN_ID_HEADER,\n SSE_BASE_HEADERS,\n SSE_DONE_FRAME,\n} from './durable-ui-message-stream-response.js'\nimport { handleAgentRunReconnect } from './handle-agent-run-reconnect.js'\nimport { parseAgentRequestBody } from './mount-agent.js'\nimport { getRunEventCache, type RunEventCache } from './run-event-cache.js'\nimport { postThreadFollowUp } from './thread-dispatcher.js'\nimport { getThreadRunRegistry, type ThreadRunRegistry } from './thread-run-registry.js'\n\nconst THREAD_MESSAGE_PATH = /^\\/api\\/agents\\/([^/]+)\\/threads\\/([^/]+)\\/message$/\nconst THREAD_STREAM_PATH = /^\\/api\\/agents\\/([^/]+)\\/threads\\/([^/]+)\\/stream$/\n\n/** How long a subscribe-to-idle-thread waits for the next run before closing. */\nconst DEFAULT_IDLE_WAIT_MS = 30_000\n\nfunction matchThread(re: RegExp, urlPath: string): { name: string; sessionId: string } | null {\n const m = re.exec(urlPath)\n return m ? { name: decodeURIComponent(m[1]), sessionId: decodeURIComponent(m[2]) } : null\n}\n\nexport const isThreadMessagePath = (urlPath: string) => matchThread(THREAD_MESSAGE_PATH, urlPath)\nexport const isThreadStreamPath = (urlPath: string) => matchThread(THREAD_STREAM_PATH, urlPath)\n\nfunction jsonError(status: number, code: string, message: string): Response {\n return new Response(JSON.stringify({ error: { code, message } }), {\n status,\n headers: { 'content-type': 'application/json; charset=utf-8' },\n })\n}\n\n/** Inputs for {@link handleThreadMessage} (bundled to stay within the arity budget). */\nexport interface ThreadMessageArgs {\n readonly mod: unknown\n readonly apiKey: string\n readonly sessionId: string\n readonly request: Request\n readonly source: string\n readonly csrfMode?: CsrfMode\n}\n\n/** POST a follow-up on a thread. Returns `202` with `{ runId }` (started) or `{ queued: true }`. */\nexport async function handleThreadMessage(args: ThreadMessageArgs): Promise<Response> {\n const { mod, apiKey, sessionId, request, source, csrfMode = 'strict' } = args\n // A follow-up drives the agent (spends LLM tokens) — reject a cross-origin POST.\n if (csrfMode === 'strict') {\n const csrf = validateCsrfRequest(request)\n if (!csrf.valid) return jsonError(403, 'CSRF_FAILED', `CSRF check failed: ${csrf.reason}`)\n }\n let body: unknown = null\n try {\n body = await request.json()\n } catch {\n /* invalid/empty JSON → 400 below */\n }\n const input = parseAgentRequestBody(body)\n if (input === null) {\n return jsonError(400, 'BAD_REQUEST', 'Request must contain a non-empty message.')\n }\n const result = postThreadFollowUp(\n {\n registry: getThreadRunRegistry(),\n cache: getRunEventCache(),\n startRun: makeThreadStartRun(mod, apiKey, source),\n },\n sessionId,\n input.message,\n )\n const headers: Record<string, string> = { 'content-type': 'application/json; charset=utf-8' }\n if ('runId' in result) headers[RUN_ID_HEADER] = result.runId\n return new Response(JSON.stringify(result), { status: 202, headers })\n}\n\n/** GET the thread's durable stream. Active ⇒ attach now; idle ⇒ wait (bounded) for the next run. */\nexport function handleThreadStream(\n sessionId: string,\n request: Request,\n registry: ThreadRunRegistry = getThreadRunRegistry(),\n cache: RunEventCache = getRunEventCache(),\n idleWaitMs = DEFAULT_IDLE_WAIT_MS,\n): Response {\n const active = registry.getActive(sessionId)\n if (active !== null && cache.has(active)) {\n // Reuse the M37 reconnect handler — replay + live tail on the active run.\n return handleAgentRunReconnect(active, request, cache)\n }\n return waitThenAttachStream(sessionId, request, registry, cache, idleWaitMs)\n}\n\n/** Subscribe to an idle thread: wait (bounded) for the NEXT run to start, then attach + tail. */\nfunction waitThenAttachStream(\n sessionId: string,\n request: Request,\n registry: ThreadRunRegistry,\n cache: RunEventCache,\n idleWaitMs: number,\n): Response {\n const stream = new ReadableStream<Uint8Array>({\n start(controller) {\n let closed = false\n const send = (text: string): void => {\n if (closed) return\n try {\n controller.enqueue(encodeSse(text))\n } catch {\n /* closed by an abort race */\n }\n }\n let detachAttach: (() => void) | undefined\n const close = (): void => {\n if (closed) return\n send(SSE_DONE_FRAME)\n closed = true\n try {\n controller.close()\n } catch {\n /* already closed */\n }\n }\n const offNext = registry.onNextRun(sessionId, (runId) => {\n const res = cache.attach(\n runId,\n -1,\n (frame) => {\n send(formatSseFrame(frame.seq, frame.data))\n },\n () => {\n close()\n },\n )\n if (!res.known) {\n close()\n return\n }\n for (const frame of res.replay) send(formatSseFrame(frame.seq, frame.data))\n if (res.ended) {\n close()\n return\n }\n detachAttach = res.unsubscribe\n })\n // HIGH-1 — the waiter teardown MUST run on BOTH the idle-wait timeout AND the\n // client abort, else the (one-shot) `onNextRun` waiter leaks in the registry\n // when a timed-out subscriber's session never receives another run.\n const teardownWaiter = (): void => {\n offNext()\n detachAttach?.()\n }\n const timer = setTimeout(() => {\n teardownWaiter()\n close()\n }, idleWaitMs)\n timer.unref()\n request.signal.addEventListener('abort', () => {\n teardownWaiter()\n clearTimeout(timer)\n close()\n })\n },\n })\n return new Response(stream, { headers: { ...SSE_BASE_HEADERS } })\n}\n","/**\n * M15/M16 follow-up — shared dispatcher for the agent AUXILIARY routes that BOTH dev (vite\n * middleware) and prod (`theokit start` handler) must serve identically. Before this, these routes\n * were wired only into the dev middleware, so a built/deployed app served none of them (agent cards,\n * MCP, pending-approvals listing all 404'd in production).\n *\n * Single source of truth (DRY): one Web-Request→Response dispatcher, two callers. It handles the\n * routes that derive purely from the agent module + shared registry:\n * - **M15** `GET /.well-known/<name>/agent-card.json` → {@link handleAgentCard}\n * - **M14** `GET /api/agents/<name>/approvals` → {@link handleListApprovals}\n * - **M16** `POST /api/agents/<name>/mcp` → {@link handleMcpJsonRpc}\n *\n * Channels (M27) are NOT here: a channel webhook needs app-supplied `validators` + `onMessage`, so\n * the app wires `handleChannelWebhook` in its own route (it cannot be auto-derived from the module).\n * The HITL approve route stays in each caller (it carries caller-specific rate-limiting/CSRF plumbing).\n */\nimport type { AgentNode } from '../scan/agent-scan.js'\nimport { validateCsrfRequest, type CsrfMode } from '../security/csrf.js'\n\nimport { isAgentCardPath, handleAgentCard } from './agent-card-handler.js'\nimport { getApprovalRegistry } from './approval-registry.js'\nimport { handleAgentRunReconnect, isAgentRunStreamPath } from './handle-agent-run-reconnect.js'\nimport {\n handleThreadMessage,\n handleThreadStream,\n isThreadMessagePath,\n isThreadStreamPath,\n} from './handle-thread-routes.js'\nimport { isListApprovalsPath, handleListApprovals } from './list-approvals-handler.js'\nimport { extractAppResources } from './mcp-app-resources.js'\nimport { isMcpPath, handleMcpJsonRpc } from './mcp-handler.js'\nimport { getRunEventCache } from './run-event-cache.js'\n\n/** JSON error envelope (mirrors mount-agent.ts:37 — the parity source for the MCP CSRF gate). */\nfunction jsonError(status: number, code: string, message: string): Response {\n return new Response(JSON.stringify({ error: { code, message } }), {\n status,\n headers: { 'content-type': 'application/json; charset=utf-8' },\n })\n}\n\n/** Dependencies the aux dispatcher needs from its caller (dev or prod). */\nexport interface AuxRouteDeps {\n /** Discovered agents (from `scanAgents`). */\n agents: readonly AgentNode[]\n /** Load an agent module from its file path (dev: vite loader; prod: dynamic import). */\n loadModule: (filePath: string) => Promise<unknown>\n /** Absolute base URL (`http(s)://host`) for the agent-card endpoint URLs. */\n baseUrl: string\n /**\n * M34 (#97) — CSRF enforcement mode for the MCP route. `POST /api/agents/<name>/mcp` drives the\n * agent (spends LLM tokens), so a cross-origin POST MUST be rejected in `'strict'` — parity with\n * the agent-run route (`mount-agent.ts:83-91`). Defaults to `'strict'` (safe-by-default); a caller\n * that already gated upstream may pass `'off'`.\n */\n csrfMode?: CsrfMode\n /**\n * M39 — lazily resolve the provider apiKey. Required only for the thread\n * follow-up route (which drives the agent); resolved on demand so non-agent\n * aux routes (card, approvals, stream) never need a provider key.\n */\n resolveApiKey?: () => string\n}\n\n/**\n * Serve an agent auxiliary route. Returns a `Response` when `urlPath` matches an aux route (card /\n * list-approvals / mcp) and the method + agent resolve; returns `null` to fall through (not an aux\n * route, wrong method, or unknown agent — the caller then owns the 404/next).\n */\nexport async function serveAgentAuxRoute(\n request: Request,\n urlPath: string,\n deps: AuxRouteDeps,\n): Promise<Response | null> {\n const method = request.method.toUpperCase()\n\n // M15 — A2A agent card at `/.well-known/<name>/agent-card.json` (GET).\n const cardName = isAgentCardPath(urlPath)\n if (cardName !== null) {\n if (method !== 'GET') return null\n const agent = deps.agents.find((a) => a.name === cardName)\n if (!agent) return null\n const mod = await deps.loadModule(agent.filePath)\n return handleAgentCard(mod, agent.name, agent.agentPath, deps.baseUrl)\n }\n\n // M14 — GET /api/agents/<name>/approvals (pending HITL approvals).\n if (isListApprovalsPath(urlPath)) {\n if (method !== 'GET') return null\n return handleListApprovals(getApprovalRegistry())\n }\n\n // M37 — GET /api/agents/<name>/runs/<runId>/stream (durable reconnect / observe).\n // INTENTIONALLY open (no CSRF, no auth gate): a GET is not CSRF-vulnerable, the\n // run-start POST is already gated, and the `runId` is a 122-bit UUID (unguessable).\n // Observe-by-runId is a FEATURE (ADR-0046 D5) — a second client resumes a run a\n // first started. Do NOT add a custom-header CSRF check here: browsers send NO\n // custom headers with `EventSource`, so it would break native SSE reconnect.\n const runStream = isAgentRunStreamPath(urlPath)\n if (runStream !== null) {\n if (method !== 'GET') return null\n if (!deps.agents.some((a) => a.name === runStream.name)) return null\n return handleAgentRunReconnect(runStream.runId, request, getRunEventCache())\n }\n\n // M39 — thread signal routes (follow-up message + subscribe-by-thread). Extracted\n // to keep this dispatcher within the cognitive-complexity budget (G6).\n const threadResponse = await serveThreadRoute(request, method, urlPath, deps)\n if (threadResponse !== null) return threadResponse\n\n // M16 — POST /api/agents/<name>/mcp (JSON-RPC MCP server). Extracted to keep this dispatcher\n // within the cognitive-complexity budget (G6).\n const mcpName = isMcpPath(urlPath)\n if (mcpName !== null) {\n return serveMcpRoute(request, method, mcpName, deps)\n }\n\n return null\n}\n\n/**\n * M39 — serve the thread routes:\n * - `POST .../threads/<sessionId>/message` (follow-up) — loads the module, drives\n * the run headless via the thread dispatcher. Needs `resolveApiKey` (drives the\n * agent) → 501 when absent (rather than a silent 404).\n * - `GET .../threads/<sessionId>/stream` (subscribe) — attach to the active/next\n * run's durable stream. INTENTIONALLY open (GET, no custom headers — like the\n * M37 reconnect route).\n *\n * SECURITY (thread stream): unlike the M37 reconnect route — keyed on an\n * `mintRunId()` UUID (122-bit unguessable) — the thread stream is keyed on the\n * caller-supplied `sessionId`. The open GET is safe ONLY if the `sessionId` is\n * unguessable (e.g. a client-minted UUID). An app that uses a PREDICTABLE\n * sessionId (user id, email, sequential id) MUST add its own auth gate before\n * this endpoint, or any party who can guess the sessionId can read the thread's\n * live conversation stream. Documented in `docs/architecture/multi-surface-architecture.md`.\n *\n * Returns `null` to fall through (not a thread route, wrong method, unknown agent).\n */\nasync function serveThreadRoute(\n request: Request,\n method: string,\n urlPath: string,\n deps: AuxRouteDeps,\n): Promise<Response | null> {\n const stream = isThreadStreamPath(urlPath)\n if (stream !== null) {\n if (method !== 'GET') return null\n if (!deps.agents.some((a) => a.name === stream.name)) return null\n return handleThreadStream(stream.sessionId, request)\n }\n\n const msg = isThreadMessagePath(urlPath)\n if (msg !== null) {\n if (method !== 'POST') return null\n const agent = deps.agents.find((a) => a.name === msg.name)\n if (!agent) return null\n if (deps.resolveApiKey === undefined) {\n // MEDIUM-1 — the path matched but the caller wired no provider-key resolver.\n // Fail loudly (501) instead of a silent 404 that reads as \"route not found\".\n return jsonError(\n 501,\n 'NOT_CONFIGURED',\n 'Thread follow-up requires a provider API key (resolveApiKey was not provided to serveAgentAuxRoute).',\n )\n }\n const mod = await deps.loadModule(agent.filePath)\n return handleThreadMessage({\n mod,\n apiKey: deps.resolveApiKey(),\n sessionId: msg.sessionId,\n request,\n source: `agent \"${msg.name}\"`,\n csrfMode: deps.csrfMode ?? 'strict',\n })\n }\n return null\n}\n\n/**\n * Serve the MCP route with the M34 gates: default-DENY opt-in → CSRF → dispatch. Returns `null` to\n * fall through (wrong method / unknown or non-opted-in agent), a 403 on CSRF failure, else the\n * JSON-RPC response.\n */\nasync function serveMcpRoute(\n request: Request,\n method: string,\n mcpName: string,\n deps: AuxRouteDeps,\n): Promise<Response | null> {\n if (method !== 'POST') return null\n const agent = deps.agents.find((a) => a.name === mcpName)\n if (!agent) return null\n\n const mod = await deps.loadModule(agent.filePath)\n\n // M34 — DEFAULT-DENY: an agent is NOT exposed on MCP unless it explicitly opts in with a named\n // `export const mcp = true` (blueprint D5 — default-EXPOSE is the footgun magnified by the\n // multi-surface thesis). Absent the opt-in, fall through to 404 (the agent is web-only). This is a\n // breaking change from the M16 auto-mount (documented in the CHANGELOG § Security).\n if (!isMcpExposed(mod)) return null\n\n // M34 (#97) — enforce CSRF BEFORE any work. The MCP route drives the agent (real LLM tokens), so a\n // cross-origin POST must be rejected — parity with the agent-run route (`mount-agent.ts:83-91`).\n const csrfMode = deps.csrfMode ?? 'strict'\n if (csrfMode === 'strict') {\n const csrf = validateCsrfRequest(request)\n if (!csrf.valid) return jsonError(403, 'CSRF_FAILED', `CSRF check failed: ${csrf.reason}`)\n }\n\n let body: unknown = null\n try {\n body = await request.json()\n } catch {\n /* malformed/empty JSON → handleMcpJsonRpc returns a -32600 envelope */\n }\n // M30 — pass the agent's declared `ui://` App resources (named `appResources` export) so the MCP\n // server advertises + serves them via resources/list + resources/read.\n return handleMcpJsonRpc(mod, agent.name, body, extractAppResources(mod))\n}\n\n/**\n * M34 — DEFAULT-DENY opt-in check: is this agent module exposed on the MCP surface? An agent opts in\n * with a named `export const mcp = true` (mirroring the `appResources` named-export convention).\n * Anything else (absent / falsy) → NOT exposed. Read at the emit layer (blueprint D5).\n */\nfunction isMcpExposed(mod: unknown): boolean {\n return (mod as { mcp?: unknown } | null | undefined)?.mcp === true\n}\n","/**\n * T5a.2 Phase G slice 5/N — Node adapter shim for the Web request handler.\n *\n * Bridges Node's `IncomingMessage` + `ServerResponse` shape to the\n * Web-Standards `executeWebRequest` (Phase A → G slices 1-4). Per\n * ADR-0028 R3a, the Node adapter is the ONLY place IncomingMessage ↔\n * Request conversion happens — every other layer of `server/` flows\n * through Web `Request`/`Response`.\n *\n * **Two conversions:**\n *\n * 1. `incomingMessageToWebRequest(req)` — Node → Web. Reads\n * `req.method`, `req.url`, `req.headers`, and (for POST/PUT/etc.)\n * drains the Node Readable body into a Web `ReadableStream`. The\n * request URL is resolved to absolute form using `req.headers.host`\n * (Web Request guarantees absolute URL).\n *\n * 2. `writeWebResponseToServerResponse(response, res)` — Web → Node.\n * Sets status code + status text, copies headers (including all\n * `Set-Cookie` values via `getSetCookie()`), then drains the Web\n * `ReadableStream` body into the Node ServerResponse.\n *\n * Plus the convenience composer `executeWebRequestFromNode(req, res,\n * routeModule, opts?)` that wires both ends — exactly what api-middleware\n * (and the prod CLI start path) need to migrate from the legacy\n * `executeRoute` to `executeWebRequest` without touching call sites.\n *\n * Per `docs/plans/t5a2-incoming-message-to-request-shape-refactor-plan.md`\n * v1.0 § Phase G slice 5/N (closes the executor bridge surface).\n */\nimport type { IncomingMessage, ServerResponse } from 'node:http'\n\nimport { executeWebRequest, type ExecuteWebRequestOptions } from '../web-handler.js'\n\nimport { incomingMessageToWebRequest } from './node-request.js'\n\n// The primitive `IncomingMessage` → Web `Request` converters live in\n// `node-request.js` (dependency-free, shared with the executor). Re-exported\n// here so existing importers of the adapter keep resolving them.\nexport { incomingMessageToWebRequest, incomingMessageToHandlerRequest } from './node-request.js'\n\n/**\n * Write a Web `Response` into a Node `ServerResponse`. Mirrors the Node\n * `res.writeHead` + `res.end` pattern.\n *\n * Set-Cookie is the only multi-value header the Web spec exposes via\n * `getSetCookie()`. We append each entry individually so Node emits\n * separate `Set-Cookie:` lines per the HTTP spec.\n *\n * If the Response body is a `ReadableStream`, it's piped chunk-by-chunk\n * to `res`. Empty body (null) → just close.\n */\nexport async function writeWebResponseToServerResponse(\n response: Response,\n res: ServerResponse,\n): Promise<void> {\n // Status + headers FIRST (writeHead locks them).\n // EC-3: Set-Cookie needs special handling (writeHead's plain object\n // shape conflicts with multi-value headers; we set them via setHeader\n // BEFORE writeHead so the array form is preserved).\n const setCookies = response.headers.getSetCookie()\n if (setCookies.length > 0) {\n res.setHeader('Set-Cookie', setCookies)\n }\n const otherHeaders: Record<string, string> = {}\n for (const [key, value] of response.headers.entries()) {\n if (key.toLowerCase() === 'set-cookie') continue\n otherHeaders[key] = value\n }\n res.writeHead(response.status, response.statusText, otherHeaders)\n\n // Body — drain ReadableStream OR just end() for null body.\n if (response.body === null) {\n res.end()\n return\n }\n const reader = response.body.getReader()\n try {\n for (;;) {\n const { done, value } = await reader.read()\n if (done) break\n // Node's res.write accepts Uint8Array natively (no conversion needed).\n res.write(value)\n }\n res.end()\n } finally {\n reader.releaseLock()\n }\n}\n\n/**\n * Convenience composer — full request lifecycle Node → Web → Node.\n *\n * Use case: existing api-middleware (and the prod CLI start path)\n * receive Node `(req, res)` from `http.createServer`. To migrate to the\n * Web-Standards executor without rewriting every call site, wrap with\n * this composer:\n *\n * import * as users from './app/users/route.js'\n * await executeWebRequestFromNode(req, res, users, { csrfMode: 'strict' })\n *\n * The Web request is built, dispatched through executeWebRequest, and\n * the Response is drained back into `res`. Caller does NOT need to call\n * `res.end()` afterwards — this composer handles it.\n */\nexport async function executeWebRequestFromNode(\n req: IncomingMessage,\n res: ServerResponse,\n routeModule: Parameters<typeof executeWebRequest>[1],\n opts?: ExecuteWebRequestOptions,\n): Promise<void> {\n const webRequest = incomingMessageToWebRequest(req)\n const webResponse = await executeWebRequest(webRequest, routeModule, opts)\n await writeWebResponseToServerResponse(webResponse, res)\n}\n","import type { IncomingMessage, ServerResponse } from 'node:http'\nimport { resolve } from 'node:path'\n\nimport type { ViteDevServer, Connect } from 'vite'\n\nimport {\n BATCH_PATH,\n CSP_REPORT_PATH,\n TRACE_HEADER,\n applySecurityHeaders,\n createCorsHandler,\n createRateLimiter,\n createViteLoader,\n executeRoute,\n extractTraceId,\n findSuggestion,\n handleBatchRequest,\n handleCspReport,\n handleCsrfReadiness,\n incomingMessageToHandlerRequest,\n logRequest,\n dispatchControllerRequest,\n mountAgent,\n resolveProvider,\n matchRoute,\n scanServerRoutes,\n sendError,\n type AuditLogger,\n type CorsConfig,\n type CspReportHandlerOptions,\n type CsrfReadinessStore,\n type DisallowedConfig,\n type LoadModule,\n type PluginRunner,\n type RateLimitConfig,\n type SecurityHeadersConfig,\n} from '../server/internal-api.js'\nimport type { TheoTransformer } from '../server/transformer.js'\n\nexport interface ApiMiddlewareOptions {\n rateLimitConfig?: RateLimitConfig\n pluginRunner?: PluginRunner\n transformer?: TheoTransformer\n /** When defined, enables /api/__theo_batch__ endpoint with given options. */\n batching?: { max?: number }\n /** Phase 5 — CSRF enforcement mode. Default 'warn' (0.2.0). */\n csrfMode?: 'off' | 'warn' | 'strict'\n /** Phase 6 — Default security headers config. */\n securityHeaders?: SecurityHeadersConfig\n /** T5.1 — per-route disallowed escalation pattern. */\n disallowed?: DisallowedConfig\n /** T1.2 — CORS configuration. When undefined, no CORS handling (same-origin only). */\n cors?: CorsConfig\n /** T4.1 — Audit logger. Receives csrf.warn, rate-limit.exceeded, csp.violation events. */\n auditLogger?: AuditLogger\n /** T5.1 — Optional CSP violation hook (forwarded to Sentry / custom sink). */\n onCspViolation?: CspReportHandlerOptions['onViolation']\n /**\n * T2.2 — Optional CSRF readiness store. When provided, the\n * `/__theo/csrf-readiness` endpoint is mounted and warn events are\n * recorded into the store. Dev-mode middleware wires this automatically;\n * production hosts opt in via config.security.csrfTelemetry.exposeReadinessEndpoint.\n */\n csrfReadinessStore?: CsrfReadinessStore\n /**\n * Wave 2 completion (T1.1 vite-proxy) — list of services-proxy path prefixes\n * (e.g. `['/api/agent', '/api/worker']`). Requests whose path starts with\n * any of these prefixes bypass the api-middleware (call `next()`) so the\n * later proxyMiddleware (installed by Vite from `server.proxy`) can forward\n * them to the polyglot sidecar. Empty / undefined preserves Wave 1 BC.\n */\n servicesProxyPrefixes?: readonly string[]\n}\n\n/**\n * Wave 2 completion — true when `url` is OUTSIDE TheoKit's API namespace,\n * either because it doesn't start with `/api/` OR because it matches a\n * services-proxy prefix. In both cases the middleware should call `next()`\n * so the request reaches Vite's proxyMiddleware / static fallback. Combining\n * both checks into one helper keeps the main arrow function's cyclomatic\n * complexity within the eslint ceiling.\n */\nfunction shouldBypassApiMiddleware(url: string, prefixes: readonly string[]): boolean {\n if (!url.startsWith('/api/')) return true\n if (prefixes.length === 0) return false\n const path = url.split('?')[0]\n for (const prefix of prefixes) {\n if (path === prefix || path.startsWith(`${prefix}/`)) return true\n }\n return false\n}\n\nasync function handleCspReportIfMatch(\n req: IncomingMessage,\n res: ServerResponse,\n url: string,\n ctx: {\n auditLogger: AuditLogger | undefined\n onCspViolation: CspReportHandlerOptions['onViolation'] | undefined\n },\n): Promise<boolean> {\n const pathOnly = url.split('?')[0]\n if (pathOnly !== CSP_REPORT_PATH || req.method !== 'POST') return false\n await handleCspReport(req, res, { auditLogger: ctx.auditLogger, onViolation: ctx.onCspViolation })\n return true\n}\n\ninterface RateLimitCtx {\n rateLimiter:\n | ((req: IncomingMessage) => { limited: boolean; headers: Record<string, string> })\n | null\n requestId: string\n url: string\n start: number\n}\n\n/**\n * Apply rate-limit headers and send 429 if exceeded. Returns true when\n * the caller should stop handling the request (limit hit), false otherwise.\n * Extracted from `createApiMiddleware` to keep complexity within ceiling.\n */\nfunction applyRateLimitOr429(\n req: IncomingMessage,\n res: ServerResponse,\n ctx: RateLimitCtx,\n): boolean {\n if (!ctx.rateLimiter) return false\n const check = ctx.rateLimiter(req)\n for (const [k, v] of Object.entries(check.headers)) res.setHeader(k, v)\n if (!check.limited) return false\n sendError(res, 'RATE_LIMITED', 'Too many requests', 429, undefined, ctx.requestId)\n logRequest({\n method: req.method ?? 'GET',\n url: ctx.url,\n status: 429,\n duration: Date.now() - ctx.start,\n requestId: ctx.requestId,\n })\n return true\n}\n\ninterface BatchMatchCtx {\n url: string\n batching: { max?: number } | undefined\n requestId: string\n start: number\n}\n\nasync function handleBatchIfMatch(\n req: IncomingMessage,\n res: ServerResponse,\n ctx: BatchMatchCtx,\n): Promise<boolean> {\n if (!ctx.batching) return false\n const urlPathOnly = ctx.url.split('?')[0]\n if (urlPathOnly !== BATCH_PATH || req.method !== 'POST') return false\n await handleBatchInline(req, res, ctx.batching, ctx.requestId)\n logRequest({\n method: req.method ?? 'POST',\n url: ctx.url,\n status: res.statusCode,\n duration: Date.now() - ctx.start,\n requestId: ctx.requestId,\n })\n return true\n}\n\nasync function handleBatchInline(\n req: IncomingMessage,\n res: ServerResponse,\n batching: { max?: number },\n requestId: string,\n): Promise<void> {\n try {\n const chunks: Buffer[] = []\n await new Promise<void>((resolve, reject) => {\n req.on('data', (c) => chunks.push(c as Buffer))\n req.on('end', () => {\n resolve()\n })\n req.on('error', reject)\n })\n // CR-028: pass the parsed JSON as `unknown`; Zod inside\n // handleBatchRequest is the validation boundary.\n const payload: unknown = JSON.parse(Buffer.concat(chunks).toString())\n const outerHeaders: Record<string, string> = {}\n for (const [k, v] of Object.entries(req.headers)) {\n if (typeof v === 'string') outerHeaders[k.toLowerCase()] = v\n }\n const result = await handleBatchRequest(payload, {\n max: batching.max,\n outerHeaders,\n // T1.4 placeholder — full per-item route execution lands in a\n // follow-up; for now we return an empty ack so the integration\n // tests of the batch endpoint shape pass.\n execute: () => Promise.resolve({ data: { ok: true } }),\n })\n res.writeHead(200, { 'Content-Type': 'application/json' })\n res.end(JSON.stringify(result))\n } catch (err) {\n const message = err instanceof Error ? err.message : String(err)\n sendError(res, 'BATCH_ERROR', message, 400, undefined, requestId)\n }\n}\n\n/**\n * P#3 T4.1 — fire pluginRunner.onRequest for unmatched routes so plugins\n * can intercept paths outside server/routes/ (e.g., @theokit/plugin-openapi\n * /api/docs). Returns true when a plugin short-circuited the response.\n */\ninterface RunPluginsCtx {\n req: IncomingMessage\n res: ServerResponse\n requestId: string\n start: number\n url: string\n}\n\nasync function runPluginsBeforeRouteMatch(\n pluginRunner: PluginRunner,\n reqCtx: RunPluginsCtx,\n): Promise<boolean> {\n const { req, res, requestId, start, url } = reqCtx\n try {\n const hookResult = await pluginRunner.runOnRequest({\n // #119 — plugin hooks see a Web `Request` in every runtime (ADR-0028 R3a).\n request: incomingMessageToHandlerRequest(req),\n response: res,\n ctx: {},\n requestId,\n })\n if (hookResult.shortCircuited) {\n logRequest(\n {\n method: req.method ?? 'GET',\n url,\n status: res.statusCode,\n duration: Date.now() - start,\n requestId,\n },\n undefined,\n req,\n )\n return true\n }\n } catch (err) {\n sendError(res, 'PLUGIN_ERROR', (err as Error).message, 500, undefined, requestId)\n return true\n }\n return false\n}\n\n/**\n * #122 (T1.2) — controller fall-through for the `!match` arm. Serves a decorator\n * controller under `<serverDir>/controllers/` (dev parity); inherits the security-\n * headers/CORS/rate-limit/plugin gates already run upstream, and CSRF is enforced\n * inside `dispatchControllerRequest`. Zero cost for routes-only apps. Returns `true`\n * when a controller handled the request (response already written + logged).\n */\nasync function tryControllerFallthrough(ctx: {\n req: IncomingMessage\n res: ServerResponse\n serverDir: string\n loadModule: LoadModule\n csrfMode: 'off' | 'warn' | 'strict'\n disallowed: DisallowedConfig | undefined\n requestId: string\n url: string\n start: number\n}): Promise<boolean> {\n const handled = await dispatchControllerRequest({\n controllersDir: resolve(ctx.serverDir, 'controllers'),\n loadModule: ctx.loadModule,\n req: ctx.req,\n res: ctx.res,\n csrfMode: ctx.csrfMode,\n disallowed: ctx.disallowed,\n requestId: ctx.requestId,\n // M47 — serve @Expose-bound agent routes via the ONE runtime (mountAgent). CSRF is already enforced\n // once at the controller-dispatch boundary (G5), so mountAgent runs with csrfMode 'off' (no double gate).\n serveAgent: (agent: unknown, request: Request) =>\n mountAgent(agent, request, resolveProvider().apiKey, { source: 'expose', csrfMode: 'off' }),\n })\n if (!handled) return false\n logRequest(\n {\n method: (ctx.req.method ?? 'GET').toUpperCase(),\n url: ctx.url,\n status: ctx.res.statusCode,\n duration: Date.now() - ctx.start,\n requestId: ctx.requestId,\n },\n undefined,\n ctx.req,\n )\n return true\n}\n\nexport function createApiMiddleware(\n vite: ViteDevServer,\n serverDir: string,\n rateLimitConfigOrOptions?: RateLimitConfig | ApiMiddlewareOptions,\n): Connect.NextHandleFunction {\n // Backward-compat: accept either RateLimitConfig directly (old signature)\n // or an ApiMiddlewareOptions object (new signature with pluginRunner).\n const opts: ApiMiddlewareOptions =\n rateLimitConfigOrOptions && 'windowMs' in rateLimitConfigOrOptions\n ? { rateLimitConfig: rateLimitConfigOrOptions }\n : (rateLimitConfigOrOptions ?? {})\n\n const loadModule = createViteLoader(vite)\n const rateLimiter = opts.rateLimitConfig ? createRateLimiter(opts.rateLimitConfig) : null\n const pluginRunner = opts.pluginRunner\n const transformer = opts.transformer\n const batching = opts.batching\n // T6.1 — default flipped from 'warn' to 'strict' for 0.3.0.\n const csrfMode = opts.csrfMode ?? 'strict'\n const disallowed = opts.disallowed\n const securityHeadersConfig = opts.securityHeaders ?? {}\n const securityEnv = { production: process.env.NODE_ENV === 'production' }\n // T1.2 — CORS handler runs FIRST in the pipeline (D10).\n const corsHandler = opts.cors ? createCorsHandler(opts.cors) : null\n const auditLogger = opts.auditLogger\n const onCspViolation = opts.onCspViolation\n const csrfReadinessStore = opts.csrfReadinessStore\n const servicesProxyPrefixes = opts.servicesProxyPrefixes ?? []\n\n return (req, res, next) => {\n // Middleware dispatch arm: each branch guards a protocol concern\n // (CSP report, CSRF readiness, services proxy, CORS preflight, rate\n // limit, plugin onRequest, batch, route match); linear dispatch order\n // is more readable than per-concern function extraction.\n // eslint-disable-next-line complexity\n const handle = async (): Promise<void> => {\n const url = req.url ?? ''\n\n // T5.1 — built-in CSP report endpoint. Matched BEFORE the /api/* gate\n // so the path lives outside the user's route namespace. Endpoint is\n // CSRF-exempt (browsers don't send X-Theo-Action on report POSTs).\n if (await handleCspReportIfMatch(req, res, url, { auditLogger, onCspViolation })) {\n return\n }\n\n // T2.2 — CSRF readiness endpoint. Only mounted when a store is wired\n // (dev-mode wires it automatically; prod opt-in via config).\n if (csrfReadinessStore && (await handleCsrfReadiness(req, res, csrfReadinessStore))) {\n return\n }\n\n if (shouldBypassApiMiddleware(url, servicesProxyPrefixes)) {\n next()\n return\n }\n\n // T1.2 + D10 — CORS preflight runs BEFORE rate limit, BEFORE CSRF.\n // Preflight short-circuits the response (204 + Access-Control-* headers).\n if (corsHandler?.handlePreflight(req, res)) {\n return\n }\n\n // Phase 7 — traceId resolution. Reads traceparent / x-request-id /\n // generates UUID. The `requestId` name is kept for backward compat\n // with downstream helpers (sendError, logRequest) — same value flows\n // under both `x-request-id` (legacy) and `x-trace-id` (canonical).\n const requestId = extractTraceId(req)\n const start = Date.now()\n res.setHeader('x-request-id', requestId)\n res.setHeader(TRACE_HEADER, requestId)\n // Phase 6 — Apply security headers BEFORE the handler runs so route\n // handlers can still override via res.setHeader (last write wins).\n applySecurityHeaders(res, securityHeadersConfig, securityEnv)\n // T1.2 — Add CORS headers to non-preflight responses.\n corsHandler?.applyHeaders(req, res)\n\n if (applyRateLimitOr429(req, res, { rateLimiter, requestId, url, start })) {\n return\n }\n\n // P#3 T4.1 — fire pluginRunner.onRequest BEFORE matchRoute so plugins\n // can intercept unmatched paths (e.g., @theokit/plugin-openapi /api/docs).\n if (\n pluginRunner &&\n (await runPluginsBeforeRouteMatch(pluginRunner, { req, res, requestId, start, url }))\n ) {\n return\n }\n\n // T1.4 — Batch endpoint (only when batching is enabled in config)\n if (await handleBatchIfMatch(req, res, { url, batching, requestId, start })) {\n return\n }\n\n const routes = scanServerRoutes(serverDir)\n const match = matchRoute(url, routes)\n\n if (!match) {\n // #122 (T1.2) — no file route matched; try a decorator controller (dev parity).\n if (\n await tryControllerFallthrough({\n req,\n res,\n serverDir,\n loadModule,\n csrfMode,\n disallowed,\n requestId,\n url,\n start,\n })\n ) {\n return\n }\n\n const urlPath = url.split('?')[0]\n const routePaths = routes.map((r) => r.routePath)\n const suggestion = findSuggestion(urlPath, routePaths)\n const msg = suggestion\n ? `API route not found: ${urlPath}. Did you mean: ${suggestion}?`\n : 'API route not found'\n sendError(res, 'NOT_FOUND', msg, 404, undefined, requestId)\n logRequest(\n {\n method: req.method ?? 'GET',\n url,\n status: 404,\n duration: Date.now() - start,\n requestId,\n },\n undefined,\n req,\n )\n return\n }\n\n const method = (req.method ?? 'GET').toUpperCase()\n // T3.1 (ADR-0016) — context object replaces 12 positional args\n await executeRoute({\n route: match.route,\n method,\n params: match.params,\n req,\n res,\n loadModule,\n serverDir,\n requestId,\n pluginRunner,\n transformer,\n csrfMode,\n disallowed,\n })\n logRequest(\n { method, url, status: res.statusCode, duration: Date.now() - start, requestId },\n undefined,\n req,\n )\n }\n void handle()\n }\n}\n","/**\n * T2.2 (architecture-medium-deferrals plan, ADR D2) — SSR dev middleware\n * extracted from `vite-plugin/index.ts` for SRP.\n *\n * `setupSsrDevMiddleware(server, opts)` registers a Connect-style middleware\n * on the Vite dev server that:\n * 1. Skips API, static, and HMR requests (let other middlewares handle).\n * 2. Reads `index.html`, runs `transformIndexHtml`.\n * 3. Generates per-request nonce, applies security headers (CSP + Cache-Control).\n * 4. Calls `ssrLoadModule(VIRTUAL_ENTRY_SERVER_ID).render(url, { nonce })`.\n * 5. Injects rendered HTML (with hydration script) into root div.\n * 6. On error: ssrFixStacktrace + fallback to CSR via `next()`.\n *\n * No-op when `ssrEnabled === false`. Caller's responsibility to gate.\n */\n\nimport { readFileSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\nimport type { ViteDevServer } from 'vite'\n\nimport {\n applySecurityHeaders,\n generateNonce,\n type SecurityHeadersConfig,\n} from '../server/internal-api.js'\n\ninterface SsrRenderResult {\n html: string\n hydrationData: {\n loaderData?: unknown\n actionData?: unknown\n errors?: unknown\n }\n}\n\ninterface SsrEntryServer {\n render: (\n url: string,\n opts: { nonce: string },\n ) => Promise<SsrRenderResult | { redirect: Response } | string>\n}\n\nfunction isSsrRenderResult(value: unknown): value is SsrRenderResult {\n if (typeof value !== 'object' || value === null) return false\n if (!('html' in value)) return false\n return typeof (value as Record<string, unknown>).html === 'string'\n}\n\nexport interface SsrDevMiddlewareOptions {\n projectRoot: string\n virtualEntryServerId: string\n securityHeaders: SecurityHeadersConfig | undefined\n}\n\n/**\n * Attach the SSR dev middleware to a Vite dev server. Caller decides whether\n * to invoke this based on `ssrEnabled` — this function does not gate.\n */\nexport function setupSsrDevMiddleware(server: ViteDevServer, opts: SsrDevMiddlewareOptions): void {\n server.middlewares.use((req, res, next) => {\n void (async () => {\n const url = req.url ?? '/'\n // Skip API, static, and HMR requests\n if (\n url.startsWith('/api/') ||\n url.startsWith('/@') ||\n url.startsWith('/node_modules/') ||\n url.includes('.')\n ) {\n next()\n return\n }\n\n try {\n const indexPath = resolve(opts.projectRoot, 'index.html')\n // eslint-disable-next-line security/detect-non-literal-fs-filename -- projectRoot is from `theokit dev`'s caller-controlled cwd\n let template = readFileSync(indexPath, 'utf-8')\n template = await server.transformIndexHtml(url, template)\n\n // T4.1 — Generate a per-request nonce and apply security headers BEFORE render.\n // The same nonce flows into React's renderToPipeableStream({ nonce }) so every\n // emitted <script> carries it AND into the CSP script-src directive.\n // EC-3: applySecurityHeaders also forces Cache-Control: private, no-store.\n const nonce = generateNonce()\n applySecurityHeaders(\n res,\n opts.securityHeaders ?? {},\n { production: process.env.NODE_ENV === 'production' },\n { nonce },\n )\n\n const mod = (await server.ssrLoadModule(opts.virtualEntryServerId)) as SsrEntryServer\n const result = await mod.render(url, { nonce })\n\n if (result && typeof result === 'object' && 'redirect' in result) {\n res.writeHead(302, {\n Location: result.redirect.headers.get('location') ?? '/',\n })\n res.end()\n return\n }\n\n // Backward-compat: old render returned string. New shape returns\n // { html, hydrationData } so the framework can emit the hydration\n // data script OUTSIDE the React root (fixes hydration mismatch).\n let ssrHtml: string\n let hydrationScript = ''\n if (typeof result === 'string') {\n ssrHtml = result\n } else if (isSsrRenderResult(result)) {\n ssrHtml = result.html\n const dataJson = JSON.stringify(result.hydrationData).replace(/</g, '\\\\u003c')\n hydrationScript = `<script nonce=\"${nonce}\">window.__staticRouterHydrationData=${dataJson}</script>`\n } else {\n ssrHtml = ''\n }\n const rootDivMatch = /<div id=[\"']root[\"'][^>]*>/.exec(template)\n if (!rootDivMatch) {\n res.writeHead(200, { 'Content-Type': 'text/html' })\n res.end(template)\n return\n }\n\n const splitIdx = template.indexOf(rootDivMatch[0]) + rootDivMatch[0].length\n const html =\n template.slice(0, splitIdx) + ssrHtml + hydrationScript + template.slice(splitIdx)\n\n res.writeHead(200, { 'Content-Type': 'text/html' })\n res.end(html)\n } catch (err) {\n server.ssrFixStacktrace(err as Error)\n console.error('[SSR Dev Error]', err)\n // Fallback to CSR\n next()\n return\n }\n })()\n })\n}\n","/**\n * T2.3 (architecture-medium-deferrals plan, ADR D2) — WebSocket upgrade\n * handler extracted from `vite-plugin/index.ts` for SRP.\n *\n * `setupWsUpgrade(server, projectRoot)` scans `server/ws/*` and attaches an\n * upgrade handler to `server.httpServer`. Lazy-imports the `ws` package so\n * non-WS apps don't pay the cost.\n *\n * **EC-1 (architecture-medium-deferrals plan v1.1 MUST FIX):** must tolerate\n * `server.httpServer === null` (Vite middleware mode — embed in Express/etc).\n * Without the guard, dev embeds crash on plugin init. The function exits\n * silently when no httpServer is available; consumer can wire their own.\n *\n * Mirrors the prod surface in `cli/commands/start-websocket-handler.ts` —\n * same `onOpen`/`onMessage`/`onClose`/`onError` handler shape (EC-6 parity).\n */\n\nimport type { IncomingMessage } from 'node:http'\nimport { resolve } from 'node:path'\nimport type { Duplex } from 'node:stream'\n\nimport type { ViteDevServer } from 'vite'\n\nimport { scanWebSocketRoutes } from '../server/internal-api.js'\n\ninterface WsHandler {\n onOpen?: (ws: unknown, request: unknown) => void | Promise<void>\n onMessage?: (ws: unknown, data: string) => void | Promise<void>\n onClose?: (ws: unknown, code: number, reason: Buffer) => void | Promise<void>\n onError?: (ws: unknown, err: Error) => void | Promise<void>\n}\n\nexport function setupWsUpgrade(server: ViteDevServer, projectRoot: string): void {\n const wsRoutes = scanWebSocketRoutes(resolve(projectRoot, 'server'))\n if (wsRoutes.length === 0) return\n\n // EC-1 — middleware-mode Vite has no httpServer. Silently skip; the host\n // platform owns its own upgrade routing in that case.\n const wsHttpServer = server.httpServer\n if (!wsHttpServer) return\n\n // Lazy-import `ws` so non-WS apps don't pay the cost.\n void import('ws')\n .then(({ WebSocketServer }) => {\n const wss = new WebSocketServer({ noServer: true })\n\n wsHttpServer.on('upgrade', (request: IncomingMessage, socket: Duplex, head: Buffer) => {\n void (async () => {\n const url = request.url ?? '/'\n if (!url.startsWith('/ws/')) return // Let Vite handle HMR etc.\n\n const wsPath = url.split('?')[0]\n const match = wsRoutes.find((r) => r.wsPath === wsPath)\n if (!match) {\n socket.destroy()\n return\n }\n\n try {\n const mod = await server.ssrLoadModule(match.filePath)\n const handler = ((mod as { default?: unknown }).default ?? mod) as WsHandler\n\n wss.handleUpgrade(request, socket, head, (ws) => {\n void handler.onOpen?.(ws, request)\n ws.on('message', (data: Buffer) => {\n void handler.onMessage?.(ws, data.toString())\n })\n ws.on('close', (code: number, reason: Buffer) => {\n void handler.onClose?.(ws, code, reason)\n })\n ws.on('error', (err: Error) => {\n void handler.onError?.(ws, err)\n })\n })\n } catch {\n socket.destroy()\n }\n })()\n })\n })\n .catch(() => {\n console.warn(\n '[Theo] WebSocket routes found but \"ws\" package not installed. Run: npm install ws',\n )\n })\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Build-time tool. Read-only filesystem walks under cwd + node_modules\n * to resolve consumer-side optional deps. Paths are derived from the\n * CLI-controlled cwd and a fixed package name list.\n */\n/**\n * `integrateUseTheoUI(cwd, opts)` — auto-chains the Vite plugins needed\n * for `@theokit/ui` styling when the consumer has the dep declared.\n *\n * Algorithm (D3 + D5 of the plan):\n * 1. opts.enabled === false → return [].\n * 2. detectPackage('@theokit/ui') — if absent, return [].\n * 3. opts.consumerTailwindConfig or opts.consumerPostcssConfig set →\n * log info + return []. Consumer-in-control wins (D3).\n * 4. detectPackage('@tailwindcss/vite') — if absent, log warn + return [].\n * 5. Dynamic-import both. Type-check default exports (EC-5). Type-check\n * return shapes (EC-6). If any guard fails, log + return [].\n * 6. Return [tailwindcssPlugin(), useTheoUIPlugin()].\n */\n\nimport { existsSync, readFileSync } from 'node:fs'\nimport { dirname, join } from 'node:path'\nimport { pathToFileURL } from 'node:url'\n\nimport type { Plugin } from 'vite'\n\nimport { detectPackage } from './auto-detect.js'\n\nexport interface IntegrateUiOptions {\n /** When `false`, disable auto-config regardless of detection. Default: true. */\n enabled?: boolean\n /** Path to the consumer's tailwind.config.* (any extension), if present. D3 deferral. */\n consumerTailwindConfig?: string\n /** Path to the consumer's postcss.config.* (any extension), if present. D3 deferral. */\n consumerPostcssConfig?: string\n}\n\n/**\n * Resolve a bare specifier to an absolute file URL by walking up from the\n * consumer's `cwd`, scanning `node_modules` directly. Works for ESM-only\n * packages that don't expose a `require` condition (createRequire fails\n * on those). Reads the resolved package.json's `exports.\".\"` field to\n * find the entry. Returns the file:// URL or null if not resolvable.\n */\nfunction resolveConsumerImport(name: string, cwd: string): string | null {\n let dir = cwd\n for (let i = 0; i < 10; i++) {\n const pkgDir = join(dir, 'node_modules', ...name.split('/'))\n const pkgJsonPath = join(pkgDir, 'package.json')\n if (existsSync(pkgJsonPath)) {\n try {\n const raw = readFileSync(pkgJsonPath, 'utf-8')\n const pkg = JSON.parse(raw) as {\n exports?: { '.'?: { import?: string; default?: string } | string }\n main?: string\n module?: string\n }\n let entry: string | undefined\n const dotExport = pkg.exports?.['.']\n if (typeof dotExport === 'string') entry = dotExport\n else if (dotExport && typeof dotExport === 'object') {\n entry = dotExport.import ?? dotExport.default\n }\n entry ??= pkg.module ?? pkg.main\n if (!entry) return null\n // Strip leading ./ then join.\n const cleaned = entry.replace(/^\\.\\//, '')\n return pathToFileURL(join(pkgDir, cleaned)).href\n } catch {\n return null\n }\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n return null\n}\n\nfunction isValidPlugin(value: unknown): value is Plugin {\n if (value === null || typeof value !== 'object') return false\n if (Array.isArray(value)) return false\n // Vite Plugin shape: object with a `name: string` field at minimum.\n if (!('name' in value)) return false\n return typeof (value as Record<string, unknown>).name === 'string'\n}\n\n/**\n * Normalize a Vite plugin factory return value into a flat Plugin[].\n *\n * Background: Tailwind v3 returned a single Plugin from its factory.\n * Tailwind v4 (`@tailwindcss/vite@4.x`) returns an ARRAY of 3 plugins\n * (`@tailwindcss/vite:scan` + `:generate:serve` + `:generate:build`).\n * The EC-6 single-Plugin guard rejected the v4 array shape and silently\n * skipped chain integration, causing 0 utilities generated in the\n * consumer's CSS. This normalizer accepts both shapes.\n */\nfunction normalizePluginReturn(value: unknown): Plugin[] | null {\n if (Array.isArray(value)) {\n const result: Plugin[] = []\n for (const entry of value) {\n if (!isValidPlugin(entry)) return null\n result.push(entry)\n }\n return result.length > 0 ? result : null\n }\n if (isValidPlugin(value)) return [value]\n return null\n}\n\n/** Test-only side-door so EC-6 shape guard can be unit-tested directly. */\nexport const _isValidPluginForTest = isValidPlugin\n/** Test-only side-door for the v3/v4 normalizer. */\nexport const _normalizePluginReturnForTest = normalizePluginReturn\n\n// eslint-disable-next-line complexity -- canonical auto-config gate (5 detection branches + 2 import guards + 2 shape guards); flattening to micro-helpers would hide the linear contract\nexport async function integrateUseTheoUI(\n cwd: string,\n opts?: IntegrateUiOptions,\n): Promise<Plugin[]> {\n if (opts?.enabled === false) return []\n\n const uiDetect = detectPackage('@theokit/ui', cwd)\n if (!uiDetect.installed) return []\n\n // D3 — consumer's manual config wins\n if (opts?.consumerTailwindConfig || opts?.consumerPostcssConfig) {\n // eslint-disable-next-line no-console -- one-line transparency hint; goal is to surface the override path\n console.info(\n `[theokit] Detected your tailwind.config / postcss.config — skipping auto-config. Extend with \\`import preset from '@theokit/ui/preset'\\` to apply UI theme.`,\n )\n return []\n }\n\n const tailwindDetect = detectPackage('@tailwindcss/vite', cwd)\n if (!tailwindDetect.installed) {\n console.warn(\n `[theokit] @theokit/ui detected but @tailwindcss/vite is not installed. Run \\`pnpm add -D @tailwindcss/vite\\` to enable styling.`,\n )\n return []\n }\n\n // Explicit chain: @tailwindcss/vite FIRST + @theokit/ui/vite-plugin\n // with `{ tailwind: false }` (avoids double-add of @tailwindcss/vite).\n //\n // Why resolve via filesystem then import by URL: this module runs from\n // theokit's own dist/, so `await import('@tailwindcss/vite')` would\n // try to resolve from theokit's node_modules — where the consumer's\n // optional deps don't exist. Resolving from the consumer's cwd\n // gives the correct path.\n const tailwindUrl = resolveConsumerImport('@tailwindcss/vite', cwd)\n if (!tailwindUrl) {\n console.warn(\n `[theokit] @tailwindcss/vite installed but its package entry was not resolvable from ${cwd}. Skipping auto-config.`,\n )\n return []\n }\n let tailwindMod: { default?: unknown }\n let uiMod: { default?: unknown }\n try {\n tailwindMod = (await import(tailwindUrl)) as { default?: unknown }\n } catch (err) {\n console.warn(\n `[theokit] @tailwindcss/vite dynamic import failed (${(err as Error).message}). Skipping auto-config.`,\n )\n return []\n }\n // For @theokit/ui/vite-plugin, resolve via package + subpath:\n // node_modules/@theokit/ui/dist/vite-plugin.js\n const uiPkgInstalled = detectPackage('@theokit/ui', cwd)\n const uiPkgDir = uiPkgInstalled.resolvedPath\n ? uiPkgInstalled.resolvedPath.replace(/[\\\\/]package\\.json$/, '')\n : null\n const uiPluginPath = uiPkgDir ? join(uiPkgDir, 'dist', 'vite-plugin.js') : null\n if (!uiPluginPath || !existsSync(uiPluginPath)) {\n console.warn(\n `[theokit] @theokit/ui/vite-plugin entry not found at ${String(uiPluginPath)}. Skipping auto-config.`,\n )\n return []\n }\n try {\n uiMod = (await import(pathToFileURL(uiPluginPath).href)) as { default?: unknown }\n } catch (err) {\n console.warn(\n `[theokit] @theokit/ui/vite-plugin dynamic import failed (${(err as Error).message}). Skipping auto-config.`,\n )\n return []\n }\n\n if (typeof tailwindMod.default !== 'function') {\n console.warn(\n `[theokit] @tailwindcss/vite does not expose a default-export function. Skipping auto-config.`,\n )\n return []\n }\n if (typeof uiMod.default !== 'function') {\n console.warn(\n `[theokit] @theokit/ui/vite-plugin does not expose a default-export function. Skipping auto-config.`,\n )\n return []\n }\n\n let tailwindPlugin: unknown\n let uiPlugin: unknown\n try {\n tailwindPlugin = (tailwindMod.default as () => unknown)()\n } catch (err) {\n console.warn(`[theokit] @tailwindcss/vite() threw: ${(err as Error).message}`)\n return []\n }\n try {\n // Pass { tailwind: false } so the UI plugin skips its internal\n // @tailwindcss/vite chain (we already added it above).\n uiPlugin = (uiMod.default as (opts?: { tailwind?: boolean }) => unknown)({\n tailwind: false,\n })\n } catch (err) {\n console.warn(`[theokit] @theokit/ui/vite-plugin() threw: ${(err as Error).message}`)\n return []\n }\n\n const tailwindPlugins = normalizePluginReturn(tailwindPlugin)\n if (tailwindPlugins === null) {\n console.warn(`[theokit] @tailwindcss/vite returned unexpected shape. Skipping auto-config.`)\n return []\n }\n const uiPlugins = normalizePluginReturn(uiPlugin)\n if (uiPlugins === null) {\n console.warn(\n `[theokit] @theokit/ui/vite-plugin returned unexpected shape. Skipping auto-config.`,\n )\n return []\n }\n\n return [...tailwindPlugins, ...uiPlugins]\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Reads <projectRoot>/package.json + resolves the target package's own\n * package.json. Paths come from controlled CLI/config inputs; this is a\n * build-time tool, no HTTP.\n */\n/**\n * `detectPackage(name, cwd)` — generalized form of `theoui-detect.ts`.\n *\n * Used by T3.2 `integrateUseTheoUI` to gate auto-config on whether the\n * consumer has `@theokit/ui` AND `@tailwindcss/vite` installed.\n *\n * Algorithm (same as `theoui-detect.ts` but parameterized):\n * 1. Read `<cwd>/package.json`. Check `dependencies` / `devDependencies` /\n * `peerDependencies` for the package name.\n * 2. If declared, probe `require.resolve('<name>/package.json', { paths: [cwd] })`.\n * Read the resolved package.json to capture `version`.\n * 3. If resolution fails, fall back to probing common entrypoints (so\n * packages whose `exports` doesn't include `./package.json` still\n * detect).\n *\n * NEVER throws. Returns `{installed: false}` on any failure.\n */\n\nimport { existsSync, readFileSync } from 'node:fs'\nimport { dirname, join } from 'node:path'\n\nimport type { DetectResult } from './auto-detect-types.js'\n\n/**\n * D13 invariant (ADR 0021): @theokit/ui is ESM-only by design.\n * NÃO usar `createRequire(...).resolve()` — `ERR_PACKAGE_PATH_NOT_EXPORTED`\n * em runtime para packages que declaram `type:\"module\"` sem `require` condition.\n * Tudo filesystem walk direto.\n */\n\nfunction isDeclared(name: string, projectRoot: string): boolean {\n try {\n const raw = readFileSync(join(projectRoot, 'package.json'), 'utf-8')\n const pkg = JSON.parse(raw) as {\n dependencies?: Record<string, string>\n devDependencies?: Record<string, string>\n peerDependencies?: Record<string, string>\n }\n return Boolean(\n pkg.dependencies?.[name] ?? pkg.devDependencies?.[name] ?? pkg.peerDependencies?.[name],\n )\n } catch {\n return false\n }\n}\n\n/**\n * D13: filesystem walk pra encontrar package.json (substitui require.resolve).\n * Walks node_modules up to 10 levels (handles pnpm hoist + workspace symlinks).\n */\nfunction resolvePackageJson(name: string, cwd: string): { path: string; version?: string } | null {\n let dir = cwd\n for (let depth = 0; depth < 10; depth++) {\n const pkgJsonPath = join(dir, 'node_modules', ...name.split('/'), 'package.json')\n if (existsSync(pkgJsonPath)) {\n try {\n const raw = readFileSync(pkgJsonPath, 'utf-8')\n const pkg = JSON.parse(raw) as { name?: string; version?: string }\n // Validate package name matches (defesa contra colisão path)\n if (pkg.name === name) {\n return { path: pkgJsonPath, version: pkg.version }\n }\n } catch {\n // continue walking\n }\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n return null\n}\n\n/**\n * D13: fallback probe via filesystem (substitui require.resolve).\n * Tenta common entry paths em node_modules. Retorna path do entry achado.\n */\nfunction fallbackProbe(name: string, cwd: string): { resolvedEntry: string } | null {\n const candidates = ['index.mjs', 'dist/index.mjs', 'dist/index.js', 'index.js']\n let dir = cwd\n for (let depth = 0; depth < 10; depth++) {\n const pkgDir = join(dir, 'node_modules', ...name.split('/'))\n for (const candidate of candidates) {\n const entry = join(pkgDir, candidate)\n if (existsSync(entry)) return { resolvedEntry: entry }\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n return null\n}\n\n/** Walk up from a resolved entry path until we hit the package's own package.json\n * whose `name` field matches `expectedName`. Returns null on any failure. */\nfunction findOwningPackageJson(\n entry: string,\n expectedName: string,\n): { path: string; version?: string } | null {\n let dir = dirname(entry)\n // Bound the walk to ~10 levels to avoid pathological cases.\n for (let i = 0; i < 10; i++) {\n const candidate = join(dir, 'package.json')\n if (existsSync(candidate)) {\n try {\n const raw = readFileSync(candidate, 'utf-8')\n const pkg = JSON.parse(raw) as { name?: string; version?: string }\n if (pkg.name === expectedName) {\n return { path: candidate, version: pkg.version }\n }\n } catch {\n // ignore + continue walking\n }\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n return null\n}\n\nexport function detectPackage(name: string, cwd: string): DetectResult {\n if (!isDeclared(name, cwd)) {\n return { installed: false }\n }\n\n const pkgJson = resolvePackageJson(name, cwd)\n if (pkgJson) {\n return {\n installed: true,\n version: pkgJson.version,\n resolvedPath: pkgJson.path,\n }\n }\n\n // Filesystem fallback: ESM-only packages (no `require` condition) can't\n // be resolved by createRequire. Check node_modules directly. The walk\n // upward handles pnpm hoist + workspace symlinks.\n let dir = cwd\n for (let i = 0; i < 10; i++) {\n const nmPath = join(dir, 'node_modules', ...name.split('/'))\n const nmPkgJson = join(nmPath, 'package.json')\n if (existsSync(nmPkgJson)) {\n try {\n const raw = readFileSync(nmPkgJson, 'utf-8')\n const pkg = JSON.parse(raw) as { name?: string; version?: string }\n if (pkg.name === name) {\n return { installed: true, version: pkg.version, resolvedPath: nmPkgJson }\n }\n } catch {\n /* fall through to entry probe */\n }\n return { installed: true }\n }\n const parent = dirname(dir)\n if (parent === dir) break\n dir = parent\n }\n\n // Last-resort fallback — exports doesn't include ./package.json. Walk up from\n // the resolved entry to find the package's own package.json.\n const probe = fallbackProbe(name, cwd)\n if (probe) {\n const owning = findOwningPackageJson(probe.resolvedEntry, name)\n if (owning) {\n return { installed: true, version: owning.version, resolvedPath: owning.path }\n }\n return { installed: true }\n }\n\n return { installed: false }\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Resolves theokit's own install directory (the `node_modules/theokit/`).\n * Build-time tool. Read-only check. No HTTP input.\n */\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\n/**\n * Resolve the directory that holds the framework's compiled (or source)\n * subpaths — `client/`, `server/`, `react-query/`, etc.\n *\n * Cases (regression T1.3):\n * - Source mode (`src/vite-plugin/index.ts`) — `currentDir` is\n * `…/src/vite-plugin`. Parent (`…/src`) contains `client/`. Return parent.\n * - Dist mode (`dist/chunk-XYZ.js`) — `currentDir` is `…/dist`. That dir\n * itself contains `client/`. Return currentDir.\n * - Unknown shape — fall back to parent (legacy behavior). The dogfood\n * validator will catch the misconfiguration before runtime.\n *\n * Pure function — no side effects, only filesystem reads via `existsSync`.\n * Exported so the regression test can exercise the branch logic directly.\n */\nexport function resolveTheoRootDir(currentDir: string): string {\n if (existsSync(resolve(currentDir, 'client'))) {\n return currentDir\n }\n return resolve(currentDir, '..')\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * transformIndexHtml extraction (T2.6 / M6). hasPackage probe reads\n * `node_modules/<name>` paths built from build-time projectRoot. No\n * HTTP input.\n */\nimport { existsSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\nimport { injectDevtoolsScript } from './inject-devtools.js'\nimport { injectEntryClient } from './inject-entry-client.js'\nimport { injectStylesheets } from './inject-stylesheets.js'\n\nexport interface TransformHtmlCtx {\n isDevMode: { value: boolean }\n devtoolsEnabled: boolean\n projectRoot: string\n}\n\n/**\n * Runs the 3-step HTML injection sequence in the canonical order:\n * 1. entry-client (always)\n * 2. devtools (dev only)\n * 3. stylesheets (dev only)\n * Order is part of the contract — devtools depends on entry-client\n * being present; stylesheets injection sits last so the <link>\n * follows the bundled <script>. EC-10 honored.\n */\nexport function runTransformIndexHtml(html: string, ctx: TransformHtmlCtx): string {\n // 1. entry-client (always)\n const entry = injectEntryClient(html)\n\n if (entry.warning) console.warn(entry.warning)\n let next = entry.html\n\n // 2. devtools (dev only, respecting config.devtools = false)\n const devtools = injectDevtoolsScript(next, {\n isDev: ctx.isDevMode.value,\n enabled: ctx.devtoolsEnabled,\n })\n\n if (devtools.warning) console.warn(devtools.warning)\n next = devtools.html\n\n // 3. Stylesheet link (dev only) — fixes LCP. Without the\n // <link rel=\"stylesheet\">, CSS only loads after the JS bundle\n // executes `import 'styles.css'`, causing FOUC + LCP > 9s.\n // Production builds rely on Vite's SSR bundle for the\n // correctly-hashed <link>. Font preload was tried + reverted\n // (see inject-stylesheets.ts comment); CLS was hydration\n // mismatch, not font swap.\n const styles = injectStylesheets(next, {\n isDev: ctx.isDevMode.value,\n hasPackage: (name) => existsSync(resolve(ctx.projectRoot, 'node_modules', ...name.split('/'))),\n })\n next = styles.html\n\n return next\n}\n","/**\n * T1.2 — Auto-inject the devtools entry `<script>` into served HTML (dev only).\n *\n * Origin: matches the inject-entry-client.ts pattern but for the devtools\n * module. In dev, the Vite plugin's transformIndexHtml hook calls this for\n * every served HTML. In build, this is a no-op.\n *\n * - EC-30: naive `.replace('</head>', ...)` IS used; alternative HTML parser\n * is too heavy for the bug class (user putting `</head>` literal in inline\n * JS is near-zero). Documented in known limitations.\n * - User opt-out via `config.devtools = false` is respected at the plugin\n * level (this function is simply not called).\n *\n * NEVER use dangerouslySetInnerHTML in any devtools component — see plan EC-20.\n */\n\nexport const DEVTOOLS_VIRTUAL_ID = '/@theo/devtools/entry.js'\nexport const DEVTOOLS_RESOLVED_ID = '\\0@theo/devtools/entry.js'\n\nconst SCRIPT_TAG = `<script type=\"module\" src=\"${DEVTOOLS_VIRTUAL_ID}\"></script>`\n\nexport interface InjectDevtoolsResult {\n html: string\n injected: boolean\n warning?: string\n}\n\nexport function injectDevtoolsScript(\n html: string,\n opts: { isDev: boolean; enabled?: boolean },\n): InjectDevtoolsResult {\n // Build mode → no inject\n if (!opts.isDev) return { html, injected: false }\n // User opt-out via config.devtools = false\n if (opts.enabled === false) return { html, injected: false }\n\n // Already present (HMR re-run, user added manually) → idempotent\n if (html.includes(DEVTOOLS_VIRTUAL_ID)) {\n return { html, injected: false }\n }\n\n // Inject before </head> (case-insensitive, first occurrence)\n const headClosePattern = /<\\/head\\s*>/i\n if (headClosePattern.test(html)) {\n return {\n html: html.replace(headClosePattern, (match) => ` ${SCRIPT_TAG}\\n ${match}`),\n injected: true,\n }\n }\n\n // No </head> tag — skip inject + warn (rather than corrupt the HTML)\n return {\n html,\n injected: false,\n warning:\n 'theokit devtools: index.html has no </head> tag; devtools script was NOT injected. Add <head>…</head> to your template to enable devtools.',\n }\n}\n","/**\n * Auto-inject the entry-client `<script>` into served HTML.\n *\n * Origin: a real user (live session) wrote `index.html` without the\n * `<script type=\"module\" src=\"/@theo/entry-client\"></script>` tag. The\n * page rendered SSR perfectly but NO JS ran — every onClick was dead.\n * No error, no warning. An hour of debugging.\n *\n * Fix: the Vite plugin's `transformIndexHtml` hook runs this for every\n * served HTML. If the script is already present (by URL substring match\n * — handles every quoting/ordering variant) the HTML is returned\n * unchanged. Otherwise the script is inserted before `</body>`. If\n * there's no `</body>` at all (malformed HTML), the script is appended\n * at the end and we emit a console.warn in dev.\n */\n\nconst ENTRY_CLIENT_URL = '/@theo/entry-client'\nconst SCRIPT_TAG = `<script type=\"module\" src=\"${ENTRY_CLIENT_URL}\"></script>`\n\nexport interface InjectResult {\n html: string\n injected: boolean\n warning?: string\n}\n\nexport function injectEntryClient(html: string): InjectResult {\n // Detect via URL substring — robust against:\n // <script src='/@theo/entry-client'>\n // <script type=\"module\" src=\"/@theo/entry-client\" />\n // <link rel=\"modulepreload\" href=\"/@theo/entry-client\">\n if (html.includes(ENTRY_CLIENT_URL)) {\n return { html, injected: false }\n }\n\n // Inject before </body> (case-insensitive, first occurrence)\n const bodyClosePattern = /<\\/body\\s*>/i\n if (bodyClosePattern.test(html)) {\n return {\n html: html.replace(bodyClosePattern, (match) => ` ${SCRIPT_TAG}\\n ${match}`),\n injected: true,\n }\n }\n\n // Malformed HTML — append at end and warn\n return {\n html: `${html}\\n${SCRIPT_TAG}`,\n injected: true,\n warning:\n 'theokit: index.html has no </body> tag; entry-client script was appended at end. Add <body>…</body> to your template.',\n }\n}\n","/**\n * Inject `<link rel=\"stylesheet\">` tags into served HTML during DEV.\n *\n * Why this exists (real bug, 2026-05-22): the example chat app had an\n * LCP of 9-16s because the HTML response landed WITHOUT any `<link>` tag\n * for `@theokit/ui/styles.css`. CSS only loaded after entry-client.js\n * ran the layout import. Browser paint waited for the JS → CSS chain:\n *\n * HTML arrives → browser paints nothing\n * JS bundle arrives → React hydrates → encounters `import 'styles.css'`\n * CSS arrives → browser RE-paints with styles\n *\n * Fix: emit the `<link>` in the HTML head so the browser fetches CSS in\n * PARALLEL with JS. LCP becomes time-to-CSS instead of time-to-JS-exec.\n *\n * DEV-only: in production builds, Vite's SSR pipeline emits the\n * appropriate `<link>` via its own asset manifest (with content hashes).\n * We must not double-inject there.\n */\n\ninterface StylesheetTarget {\n /** Vite dev URL — what the browser hits */\n href: string\n /** node_modules path checked before injection */\n packageRoot: string\n}\n\nconst TARGETS: StylesheetTarget[] = [\n {\n href: '/@id/@theokit/ui/styles.css',\n packageRoot: '@theokit/ui',\n },\n]\n\n/**\n * Note (2026-05-23): font preloads were briefly added here to address a\n * \"FOUT-looking\" CLS of 0.39 measured in DevTools. They turned out to\n * be a misdiagnosis — the real cause was a React hydration mismatch\n * from `<StaticRouterProvider hydrate>` emitting a `<script>` inside\n * the React tree (fixed in commit 31506d1).\n *\n * Why we don't preload fonts:\n * - In dev, pnpm hoists @theokit/ui via `.pnpm/<hash>/...`. The CSS\n * `@font-face` resolves to the real path; our preload would use the\n * symlink path. Browser doesn't dedupe — preload is \"wasted\" and\n * Chrome logs \"preloaded but not used within a few seconds\".\n * - In prod, Vite's SSR bundle emits proper hashed preload metadata\n * for fonts referenced from compiled CSS. Manual preload there\n * would conflict.\n *\n * If a future regression brings back font-related CLS, the right path\n * is `font-display: optional` (no swap) or size-adjust metric matching\n * in @theokit/ui itself — not framework-side preload.\n */\n\nexport interface InjectStylesheetsResult {\n html: string\n injected: string[]\n}\n\nexport interface InjectStylesheetsOptions {\n /** Only inject in dev (default). Skip when building for production. */\n isDev: boolean\n /** Function that checks whether a package is installed at projectRoot. */\n hasPackage(name: string): boolean\n}\n\nexport function injectStylesheets(\n html: string,\n opts: InjectStylesheetsOptions,\n): InjectStylesheetsResult {\n if (!opts.isDev) return { html, injected: [] }\n\n const tagsToInject: string[] = []\n for (const target of TARGETS) {\n if (!opts.hasPackage(target.packageRoot)) continue\n // Idempotency: skip if href already present\n if (html.includes(target.href)) continue\n tagsToInject.push(`<link rel=\"stylesheet\" href=\"${target.href}\" />`)\n }\n\n if (tagsToInject.length === 0) return { html, injected: [] }\n\n const joined = tagsToInject.join('\\n ')\n // Inject before </head>; fall back to start of <body> if no head close.\n const headClosePattern = /<\\/head\\s*>/i\n if (headClosePattern.test(html)) {\n return {\n html: html.replace(headClosePattern, (match) => ` ${joined}\\n ${match}`),\n injected: tagsToInject,\n }\n }\n\n const bodyOpenPattern = /<body[^>]*>/i\n if (bodyOpenPattern.test(html)) {\n return {\n html: html.replace(bodyOpenPattern, (match) => `${match}\\n ${joined}`),\n injected: tagsToInject,\n }\n }\n\n // Malformed HTML — prepend and trust the browser\n return { html: `${joined}\\n${html}`, injected: tagsToInject }\n}\n","export interface EntryServerOptions {\n /** When true, emit a streaming entry using onShellReady + signal cleanup.\n * When false (default), emit the legacy single-shot onAllReady entry. */\n streaming?: boolean\n /**\n * TheoUI config — when present, the SSR React tree wraps StaticRouterProvider\n * in <TheoUIProvider> + <Suspense> to MATCH the client entry exactly. Without\n * this, hydration falls back silently because the trees differ — button\n * onClick handlers never get attached and the page looks dead.\n *\n * EC-2 (CSS): the SSR entry never imports CSS. Only the React tree is\n * mirrored — the CSS stays client-only.\n */\n theoUi?: { theme?: 'violet-forge' | 'noir' | 'paper' }\n}\n\nexport function generateEntryServer(options: EntryServerOptions = {}): string {\n if (options.streaming) {\n return generateStreamingEntry(options)\n }\n return generateSingleShotEntry(options)\n}\n\n/**\n * Build the React element tree the server renders. Must mirror the client\n * tree shape from `generateEntryClient` — same wrapper components, same\n * order — or hydration silently falls back to client-only render.\n */\nfunction buildAppTreeJs(options: EntryServerOptions): string {\n const theme = options.theoUi?.theme ?? 'violet-forge'\n // T4.1 — pass options.nonce to StaticRouterProvider so its internal\n // hydration data script (`<script>window.__staticRouterHydrationData\n // = ...</script>`) carries the nonce attribute. Without this, CSP\n // enforce mode (without 'unsafe-inline') blocks the hydration script\n // → React falls back to client-only render → button onClick handlers\n // never attach → page looks dead. The nonce option to\n // renderToPipeableStream covers React-emitted scripts but NOT the\n // hydration script which is emitted by react-router itself.\n // hydrate: false — CRITICAL fix for hydration mismatch.\n //\n // StaticRouterProvider with `hydrate: true` (default) emits\n // `<script>window.__staticRouterHydrationData = ...</script>` INSIDE\n // the React tree. The client's `<RouterProvider>` does NOT emit any\n // script. React's reconciler sees server={...stuff, <script>} vs\n // client={...stuff} and DISCARDS the entire server tree, regenerating\n // from scratch on the client. That regeneration causes a massive\n // layout shift (CLS 0.39 measured in the example).\n //\n // Fix: tell StaticRouterProvider to NOT emit the script. The framework\n // emits the hydration data as a separate `<script>` in the HTML\n // template (outside #root), via the `hydrationData` returned from\n // `render()`. The script still runs BEFORE entry-client.js, so\n // window.__staticRouterHydrationData is populated when\n // createBrowserRouter reads it.\n if (options.theoUi) {\n return [\n `React.createElement(TheoUIProvider, { theme: { defaultTheme: '${theme}' } },`,\n ` React.createElement(Suspense, { fallback: null },`,\n ` React.createElement(StaticRouterProvider, { router, context, hydrate: false })`,\n ` )`,\n ` )`,\n ].join('\\n')\n }\n return `React.createElement(Suspense, { fallback: null },\\n React.createElement(StaticRouterProvider, { router, context, hydrate: false })\\n )`\n}\n\n/**\n * Generate the hydration data extraction snippet. Reads\n * loaderData/actionData/errors from the StaticHandlerContext and returns\n * them as an object the framework can serialize into a `<script>` tag.\n */\nfunction hydrationDataExtractSnippet(): string {\n return [\n ` const hydrationData = {`,\n ` loaderData: context.loaderData,`,\n ` actionData: context.actionData,`,\n ` errors: context.errors,`,\n ` }`,\n ].join('\\n')\n}\n\nfunction generateSingleShotEntry(options: EntryServerOptions): string {\n const theoUiImport = options.theoUi ? `import { TheoUIProvider } from '@theokit/ui'\\n` : ''\n const appTree = buildAppTreeJs(options)\n return [\n `import React, { Suspense } from 'react'`,\n `import { renderToPipeableStream } from 'react-dom/server'`,\n `import { createStaticHandler, createStaticRouter, StaticRouterProvider } from 'react-router'`,\n `import { PassThrough } from 'node:stream'`,\n `import { routes } from '/@theo/route-manifest'`,\n theoUiImport,\n `export async function render(url, options = {}) {`,\n ` const handler = createStaticHandler(routes)`,\n ` const request = new Request('http://localhost' + url)`,\n ` const context = await handler.query(request)`,\n ``,\n ` if (context instanceof Response) {`,\n ` return { redirect: context }`,\n ` }`,\n ``,\n ` const router = createStaticRouter(handler.dataRoutes, context)`,\n ` const app = ${appTree}`,\n ``,\n hydrationDataExtractSnippet(),\n ``,\n ` return new Promise((resolve, reject) => {`,\n ` let html = ''`,\n ` let piped = false`,\n ` const passthrough = new PassThrough()`,\n ` passthrough.on('data', (chunk) => { html += chunk.toString() })`,\n ` passthrough.on('end', () => { resolve({ html, hydrationData }) })`,\n ` passthrough.on('error', reject)`,\n ``,\n ` // Pipe on onShellReady (Next.js pattern). Calling pipe() twice`,\n ` // throws \"React currently only supports piping to one writable`,\n ` // stream\". The \\`piped\\` flag is a belt-and-suspenders guard if`,\n ` // onShellReady fires unexpectedly more than once.`,\n ` // Forward options.nonce to React so every <script> tag React`,\n ` // emits (Suspense boundary scripts) carries the nonce attribute.`,\n ` const { pipe } = renderToPipeableStream(app, {`,\n ` nonce: options.nonce,`,\n ` onShellReady() { if (!piped) { piped = true; pipe(passthrough) } },`,\n ` onShellError(err) { reject(err) },`,\n ` onError(err) { console.error('[SSR Error]', err) },`,\n ` })`,\n ` })`,\n `}`,\n ].join('\\n')\n}\n\n// Generated-code fragments — extracted so the parent emitter stays under\n// the max-lines-per-function ceiling.\nfunction streamingWebRenderer(appTree: string): string[] {\n return [\n `// T2.3 — Web Standards streaming entry for edge runtimes (Cloudflare,`,\n `// Bun, Deno, Vercel Edge). Uses renderToReadableStream and returns a`,\n `// Response with the stream as body. Honors request.signal for client`,\n `// disconnect cleanup.`,\n `export async function renderStreamingWeb(request, options = {}) {`,\n ` const handler = createStaticHandler(routes)`,\n ` const url = new URL(request.url)`,\n ` const context = await handler.query(request)`,\n ``,\n ` if (context instanceof Response) {`,\n ` return context`,\n ` }`,\n ``,\n ` const router = createStaticRouter(handler.dataRoutes, context)`,\n ` const app = ${appTree}`,\n ``,\n ` const stream = await renderToReadableStream(app, {`,\n ` signal: request.signal,`,\n ` nonce: options.nonce,`,\n ` onError(err) { console.error('[SSR Web Stream Error]', err) },`,\n ` })`,\n ` return new Response(stream, {`,\n ` status: 200,`,\n ` headers: {`,\n ` 'Content-Type': 'text/html; charset=utf-8',`,\n ` 'Transfer-Encoding': 'chunked',`,\n ` },`,\n ` })`,\n `}`,\n ]\n}\n\nfunction streamingNodeRenderer(appTree: string): string[] {\n return [\n `// T6.1 — Node streaming SSR entry (opt-in via theo.config.ts > ssrStreaming: true)`,\n `// Flushes the shell as soon as it's ready, then streams Suspense boundaries.`,\n `// EC-11: respects request.signal for client-disconnect cleanup.`,\n `export async function renderStreaming(url, response, options = {}) {`,\n ` const handler = createStaticHandler(routes)`,\n ` const request = new Request('http://localhost' + url, { signal: options.signal })`,\n ` const context = await handler.query(request)`,\n ``,\n ` if (context instanceof Response) {`,\n ` return { redirect: context }`,\n ` }`,\n ``,\n ` const router = createStaticRouter(handler.dataRoutes, context)`,\n ` const app = ${appTree}`,\n ``,\n ` return new Promise((resolve, reject) => {`,\n ` let didError = false`,\n ` const stream = renderToPipeableStream(app, {`,\n ` nonce: options.nonce,`,\n ` onShellReady() {`,\n ` response.statusCode = didError ? 500 : 200`,\n ` response.setHeader('Content-Type', 'text/html; charset=utf-8')`,\n ` response.setHeader('Transfer-Encoding', 'chunked')`,\n ` stream.pipe(response)`,\n ` resolve({ streaming: true })`,\n ` },`,\n ` onShellError(err) { reject(err) },`,\n ` onError(err) {`,\n ` didError = true`,\n ` console.error('[SSR Stream Error]', err)`,\n ` },`,\n ` })`,\n ``,\n ` // EC-11: client disconnect cleanup`,\n ` if (options.signal) {`,\n ` options.signal.addEventListener('abort', () => { stream.abort() })`,\n ` }`,\n ` })`,\n `}`,\n ]\n}\n\nfunction backCompatRenderer(appTree: string): string[] {\n return [\n `// Backward compatibility: keep the single-shot render export available so`,\n `// callers that always used 'render()' don't break when streaming is on.`,\n `export async function render(url, options = {}) {`,\n ` const handler = createStaticHandler(routes)`,\n ` const request = new Request('http://localhost' + url)`,\n ` const context = await handler.query(request)`,\n ``,\n ` if (context instanceof Response) {`,\n ` return { redirect: context }`,\n ` }`,\n ``,\n ` const router = createStaticRouter(handler.dataRoutes, context)`,\n ` const app = ${appTree}`,\n ``,\n hydrationDataExtractSnippet(),\n ``,\n ` const { PassThrough } = await import('node:stream')`,\n ` return new Promise((resolve, reject) => {`,\n ` let html = ''`,\n ` let piped = false`,\n ` const passthrough = new PassThrough()`,\n ` passthrough.on('data', (chunk) => { html += chunk.toString() })`,\n ` passthrough.on('end', () => { resolve({ html, hydrationData }) })`,\n ` passthrough.on('error', reject)`,\n ``,\n ` // Pipe on onShellReady (Next.js pattern). nonce forwarded.`,\n ` const { pipe } = renderToPipeableStream(app, {`,\n ` nonce: options.nonce,`,\n ` onShellReady() { if (!piped) { piped = true; pipe(passthrough) } },`,\n ` onShellError(err) { reject(err) },`,\n ` onError(err) { console.error('[SSR Error]', err) },`,\n ` })`,\n ` })`,\n `}`,\n ]\n}\n\nfunction generateStreamingEntry(options: EntryServerOptions): string {\n const theoUiImport = options.theoUi ? `import { TheoUIProvider } from '@theokit/ui'\\n` : ''\n const appTree = buildAppTreeJs(options)\n return [\n `import React, { Suspense } from 'react'`,\n `import { renderToPipeableStream, renderToReadableStream } from 'react-dom/server'`,\n `import { createStaticHandler, createStaticRouter, StaticRouterProvider } from 'react-router'`,\n `import { routes } from '/@theo/route-manifest'`,\n theoUiImport,\n ``,\n ...streamingWebRenderer(appTree),\n ``,\n ...streamingNodeRenderer(appTree),\n ``,\n ...backCompatRenderer(appTree),\n ].join('\\n')\n}\n","export interface EntryClientOptions {\n theoUi?: {\n fonts?: 'bundled' | 'cdn'\n theme?: 'violet-forge' | 'noir' | 'paper'\n }\n}\n\nexport function generateEntryClient(ssr?: boolean, opts: EntryClientOptions = {}): string {\n const rootMethod = ssr ? 'hydrateRoot' : 'createRoot'\n\n // T2.2 — TheoUI CSS imports (client-only; EC-2: NEVER in entry-server)\n // T2.3 — Wrap RouterProvider in <TheoUIProvider>\n const theoUiImports: string[] = []\n if (opts.theoUi) {\n theoUiImports.push(\n `// T2.2 — TheoUI CSS auto-injected (config.ui)`,\n `import '@theokit/ui/styles.css'`,\n )\n const fontsModule = opts.theoUi.fonts === 'cdn' ? 'fonts-cdn.css' : 'fonts.css'\n theoUiImports.push(\n `import '@theokit/ui/${fontsModule}'`,\n `// T2.3 — TheoUIProvider auto-wraps RouterProvider`,\n `import { TheoUIProvider } from '@theokit/ui'`,\n )\n }\n\n const theme = opts.theoUi?.theme ?? 'violet-forge'\n // Build the React tree: when theoUi enabled, RouterProvider sits inside\n // TheoUIProvider; otherwise RouterProvider is the top-level child.\n const routerTree = opts.theoUi\n ? `React.createElement(TheoUIProvider, { theme: { defaultTheme: '${theme}' } },\\n React.createElement(Suspense, { fallback: null },\\n React.createElement(RouterProvider, { router })\\n )\\n )`\n : `React.createElement(Suspense, { fallback: null },\\n React.createElement(RouterProvider, { router })\\n )`\n\n const renderCall = ssr\n ? ` ${rootMethod}(el,\\n ${routerTree}\\n )`\n : ` ${rootMethod}(el).render(\\n ${routerTree}\\n )`\n\n // SSR hydration: <StaticRouterProvider hydrate> emits\n // `<script>window.__staticRouterHydrationData = …</script>` into the\n // server HTML. The browser router MUST receive this so it continues\n // from the server's state instead of re-fetching everything.\n //\n // Without `hydrationData`, `createBrowserRouter` boots from scratch,\n // React detects a DOM mismatch with the SSR-rendered HTML, and\n // hydration silently falls back to client-only render — every\n // `onClick` handler attached during hydration is lost. The page\n // looks fine but is \"dead HTML\".\n const hydrationLine = ssr\n ? `const router = createBrowserRouter(routes, { hydrationData: window.__staticRouterHydrationData })`\n : `const router = createBrowserRouter(routes)`\n\n // Phase 4 — Code-splitting + matchRoutes safeguard (EC-3).\n //\n // SSR mode: pages are React.lazy()-wrapped in the manifest. If we render\n // before the matched route's module is loaded, React.lazy throws a\n // promise, the outer Suspense fires its fallback (null), and the SSR\n // DOM is wiped before hydration → onClick handlers die.\n //\n // The fix:\n // 1. matchRoutes(routes, location.pathname) on the client to discover\n // which routes actually render at this URL. Using the client matcher\n // (not a server hint) avoids URL-drift races: SSR may have prepared\n // /foo while a browser auto-redirect now points at /bar.\n // 2. Look up each matched route's path in __theoPreloadMap and call\n // the import() factory. Browsers cache modules by URL — when\n // React.lazy fires its own import() during render, it gets the\n // cached promise instantly.\n // 3. Promise.all with a 1500ms timeout. On slow networks we'd rather\n // lose hydration on ONE request than hang the page forever. The\n // fallback path proceeds to hydrate anyway — React.lazy will then\n // Suspense its fallback as usual.\n const preloadBlock = ssr\n ? [\n `// Phase 4 — preload matched-route modules before hydrate (EC-3 safeguard)`,\n `const __theoMatches = matchRoutes(routes, window.location.pathname) ?? []`,\n `const __theoPreloadPaths = __theoMatches`,\n ` .map((m) => m.route && (m.route).path)`,\n ` .filter((p) => typeof p === 'string' && p in __theoPreloadMap)`,\n `const __theoPreloadPromise = Promise.all(`,\n ` __theoPreloadPaths.map((p) => __theoPreloadMap[p]().catch((err) => { console.error('[theo] preload failed', p, err); return null }))`,\n `)`,\n `const __theoTimeout = new Promise((resolve) => setTimeout(() => resolve('timeout'), 1500))`,\n `await Promise.race([__theoPreloadPromise, __theoTimeout])`,\n '',\n ]\n : []\n\n // The hydrate/render call needs to live inside an async IIFE when we\n // await preloads. In CSR-only mode we keep the existing synchronous\n // block for backward compat.\n const renderBlock = ssr\n ? [\n `if (el) {`,\n ` ;(async () => {`,\n ...preloadBlock.map((l) => ' ' + l),\n renderCall,\n ` })()`,\n `}`,\n ]\n : [`if (el) {`, renderCall, `}`]\n\n const manifestImports = ssr\n ? `import { routes, __theoPreloadMap } from '/@theo/route-manifest'`\n : `import { routes } from '/@theo/route-manifest'`\n\n const reactRouterImports = ssr\n ? `import { createBrowserRouter, RouterProvider, matchRoutes } from 'react-router'`\n : `import { createBrowserRouter, RouterProvider } from 'react-router'`\n\n return [\n `import React, { Suspense } from 'react'`,\n `import { ${rootMethod} } from 'react-dom/client'`,\n reactRouterImports,\n manifestImports,\n `// T1.3 — side-effect import sets globalThis.__THEO_TRANSFORMER__ for theoFetch`,\n `import '/@theo/runtime-config'`,\n ...theoUiImports,\n ``,\n hydrationLine,\n `const el = document.getElementById('root')`,\n ...renderBlock,\n ].join('\\n')\n}\n","import type { RouteNode } from './types.js'\n\nfunction normalizePath(p: string): string {\n return p.replace(/\\\\/g, '/')\n}\n\nfunction safeVarName(segment: string, prefix: string): string {\n const safe = segment.replace(/[^a-zA-Z0-9]/g, '_') || 'root'\n return `${prefix}_${safe}`\n}\n\n/**\n * Map a route node to its react-router path segment. Dynamic `[slug]` →\n * `:slug`; catch-all `[...slug]` → `*` (react-router splat — param read via\n * `params['*']`); static segments pass through unchanged. (T2.2 — the page\n * router uses react-router's own matcher, NOT the server regex; see plan D3.)\n */\nfunction segmentPath(node: RouteNode): string {\n if (node.dynamic?.catchAll) return '*'\n if (node.dynamic) return `:${node.dynamic.paramName}`\n return node.segment\n}\n\ninterface ImportEntry {\n varName: string\n importPath: string\n}\n\n/**\n * Build the absolute route path for a node by accumulating segments from\n * the root. Used to key the preload map exactly as react-router's\n * `matchRoutes` reports `match.route.path`.\n */\nfunction buildRoutePath(parents: string[], segment: string): string {\n const joined = [...parents, segment].filter(Boolean).join('/')\n return '/' + joined\n}\n\ninterface WalkAccumulator {\n staticImports: ImportEntry[]\n lazyPages: { varName: string; importPath: string; routePath: string }[]\n layoutState: { found: boolean }\n}\n\nfunction pushIf(staticImports: ImportEntry[], filePath: string | undefined, varName: string): void {\n if (filePath !== undefined) {\n staticImports.push({ varName, importPath: normalizePath(filePath) })\n }\n}\n\nfunction walkRouteTree(node: RouteNode, parents: string[], acc: WalkAccumulator): void {\n const seg = node.segment || 'root'\n const routePath = buildRoutePath(parents, segmentPath(node))\n\n if (node.page !== undefined) {\n acc.lazyPages.push({\n varName: safeVarName(seg, 'Page'),\n importPath: normalizePath(node.page),\n routePath,\n })\n }\n if (node.layout !== undefined) acc.layoutState.found = true\n pushIf(acc.staticImports, node.layout, safeVarName(seg, 'Layout'))\n pushIf(acc.staticImports, node.error, safeVarName(seg, 'Error'))\n pushIf(acc.staticImports, node.loading, safeVarName(seg, 'Loading'))\n pushIf(acc.staticImports, node.notFound, safeVarName(seg, 'NotFound'))\n for (const child of node.children) {\n const childSegPath = segmentPath(node)\n const nextParents = childSegPath ? [...parents, childSegPath] : parents\n walkRouteTree(child, nextParents, acc)\n }\n}\n\nexport function generateRouteManifest(tree: RouteNode): string {\n // Static imports (always-needed at boot): layouts, errors, loading, not-found.\n // Lazy-loaded pages — tracked separately so we emit React.lazy() and\n // build the preload map.\n const acc: WalkAccumulator = {\n staticImports: [],\n lazyPages: [],\n layoutState: { found: false },\n }\n walkRouteTree(tree, [], acc)\n const staticImports = acc.staticImports\n const lazyPages = acc.lazyPages\n const layoutState = acc.layoutState\n\n // Phase 4 — Code-splitting + matchRoutes safeguard (EC-3).\n // PAGES are lazy. LAYOUTS / ERROR / LOADING / NOT-FOUND stay static\n // because they're always needed at boot regardless of route.\n // The preload map exposes the same `import()` calls keyed by absolute\n // route path. The entry-client re-matches `routes` against\n // `location.pathname` and awaits the matched entries BEFORE\n // `hydrateRoot`, so React.lazy modules resolve from cache and no\n // Suspense fallback fires during hydration.\n const lines: string[] = [`import React, { Suspense } from 'react'`]\n\n if (layoutState.found) {\n lines.push(`import { Outlet } from 'react-router'`)\n }\n\n lines.push('')\n\n // Static imports first\n for (const imp of staticImports) {\n lines.push(`import ${imp.varName} from '${imp.importPath}'`)\n }\n\n // Lazy-loaded pages\n for (const lp of lazyPages) {\n lines.push(`const ${lp.varName} = React.lazy(() => import('${lp.importPath}'))`)\n }\n\n lines.push('')\n\n // Preload map — keys are absolute route paths, values are factories that\n // return the same import() the lazy() above resolves. Browsers cache the\n // module by URL so the preload + lazy() share a single promise.\n const preloadEntries = lazyPages.map(\n (lp) => ` '${lp.routePath}': () => import('${lp.importPath}'),`,\n )\n // No TS type annotation — this manifest is emitted as a virtual JS module\n // and Rollup rejects type annotations in production builds.\n lines.push('export const __theoPreloadMap = {')\n for (const e of preloadEntries) lines.push(e)\n lines.push('}', '')\n\n // Build the children-array string for one node, separately from the\n // wrapping logic — keeps `genRouteConfig` under the complexity ceiling.\n function buildChildrenArray(node: RouteNode, seg: string): string {\n const childConfigs: string[] = []\n\n // Index route for this node's page — wrap in Suspense (the lazy module\n // is preloaded on initial hydrate, so this fallback never fires there;\n // it covers client-side navigation to other routes too).\n if (node.page) {\n const pageVar = safeVarName(seg, 'Page')\n const fallbackEl = node.loading\n ? `React.createElement(${safeVarName(seg, 'Loading')})`\n : 'null'\n const pageElement = `React.createElement(Suspense, { fallback: ${fallbackEl} }, React.createElement(${pageVar}))`\n childConfigs.push(`{ index: true, element: ${pageElement} }`)\n }\n\n // Child routes\n for (const child of node.children) {\n childConfigs.push(genRouteConfig(child, false))\n }\n\n // Not-found wildcard (only at this level)\n if (node.notFound) {\n const nfVar = safeVarName(seg, 'NotFound')\n childConfigs.push(`{ path: '*', element: React.createElement(${nfVar}) }`)\n }\n\n let arr = `[${childConfigs.join(', ')}]`\n if (node.error) {\n const errVar = safeVarName(seg, 'Error')\n arr = `[{ errorElement: React.createElement(${errVar}), children: ${arr} }]`\n }\n return arr\n }\n\n // Generate route config\n function genRouteConfig(node: RouteNode, isRoot: boolean): string {\n const seg = node.segment || 'root'\n const childrenArray = buildChildrenArray(node, seg)\n\n // Build route object\n if (node.layout) {\n const layoutVar = safeVarName(seg, 'Layout')\n const pathPart = isRoot ? `path: '/'` : `path: '${segmentPath(node)}'`\n // Layout receives `<Outlet />` as `children` prop. This supports BOTH\n // conventions: Next.js-style layouts that render `{children}` AND\n // layouts that call `<Outlet />` directly (the prop is the same element,\n // ignored by the latter). Without this, Next.js-style templates render\n // empty because react-router does not pass a `children` prop by default.\n return `{ ${pathPart}, element: React.createElement(${layoutVar}, { children: React.createElement(Outlet) }), children: ${childrenArray} }`\n }\n\n // No layout — if root, wrap in path '/'\n if (isRoot) {\n if (node.children.length === 0 && !node.page && !node.notFound && !node.error) {\n return `{ path: '/', children: [] }`\n }\n // Root without layout: children are direct routes\n return `{ path: '/', children: ${childrenArray} }`\n }\n\n // Child segment without layout — just a route\n if (node.page && node.children.length === 0 && !node.error && !node.notFound) {\n const pageVar = safeVarName(seg, 'Page')\n const fallbackEl = node.loading\n ? `React.createElement(${safeVarName(seg, 'Loading')})`\n : 'null'\n const pageElement = `React.createElement(Suspense, { fallback: ${fallbackEl} }, React.createElement(${pageVar}))`\n return `{ path: '${segmentPath(node)}', element: ${pageElement} }`\n }\n\n // Child with children but no layout\n return `{ path: '${segmentPath(node)}', children: ${childrenArray} }`\n }\n\n const routeConfig = genRouteConfig(tree, true)\n lines.push(`export const routes = [${routeConfig}]`)\n\n return lines.join('\\n')\n}\n","/**\n * Virtual modules resolveId + load extraction (T2.6 / M6).\n *\n * Pure structural split — every branch identity preserved from the\n * original inline body in `theoPlugin()`. The dispatcher is purely\n * synchronous; effects (broadcast, transformer wiring) keep their\n * original ordering so EC-10 (Vite hook ordering side effects) is\n * honored.\n */\n\nimport { broadcastRouteManifest } from '../devtools/server-side/route-manifest.js'\nimport { generateEntryServer } from '../router/entry-server.js'\nimport { generateEntryClient } from '../router/entry.js'\nimport { generateRouteManifest } from '../router/generate.js'\nimport { scanRoutes } from '../router/scan.js'\nimport type { TheoTransformer } from '../server/transformer.js'\n\nimport { DEVTOOLS_RESOLVED_ID, DEVTOOLS_VIRTUAL_ID } from './inject-devtools.js'\nimport type { TheoUiDetectResult } from './theoui-detect.js'\n\nexport interface VirtualModuleIds {\n VIRTUAL_ENTRY_ID: string\n RESOLVED_ENTRY_ID: string\n VIRTUAL_MANIFEST_ID: string\n RESOLVED_MANIFEST_ID: string\n VIRTUAL_ENTRY_SERVER_ID: string\n RESOLVED_ENTRY_SERVER_ID: string\n VIRTUAL_RUNTIME_CONFIG_ID: string\n RESOLVED_RUNTIME_CONFIG_ID: string\n}\n\nexport interface VirtualModulesCtx {\n ids: VirtualModuleIds\n appDir: string\n ssrEnabled: boolean\n streamingEnabled: boolean\n theoUi: TheoUiDetectResult | undefined\n transformer: TheoTransformer | undefined\n}\n\nexport function resolveVirtualModuleId(id: string, ids: VirtualModuleIds): string | undefined {\n if (id === ids.VIRTUAL_ENTRY_ID) return ids.RESOLVED_ENTRY_ID\n if (id === ids.VIRTUAL_MANIFEST_ID) return ids.RESOLVED_MANIFEST_ID\n if (id === ids.VIRTUAL_ENTRY_SERVER_ID) return ids.RESOLVED_ENTRY_SERVER_ID\n if (id === ids.VIRTUAL_RUNTIME_CONFIG_ID) return ids.RESOLVED_RUNTIME_CONFIG_ID\n // T1.2 — devtools virtual module. Only resolves in dev; build mode\n // never serves it because transformIndexHtml never injects the tag.\n if (id === DEVTOOLS_VIRTUAL_ID) return DEVTOOLS_RESOLVED_ID\n return undefined\n}\n\nexport function loadVirtualModule(id: string, ctx: VirtualModulesCtx): string | undefined {\n if (id === ctx.ids.RESOLVED_ENTRY_ID) {\n return generateEntryClient(ctx.ssrEnabled, {\n theoUi: ctx.theoUi?.enabled\n ? { fonts: ctx.theoUi.config.fonts, theme: ctx.theoUi.config.theme }\n : undefined,\n })\n }\n if (id === ctx.ids.RESOLVED_MANIFEST_ID) {\n const tree = scanRoutes(ctx.appDir)\n // T3.1 — also broadcast a devtools-shaped manifest to the overlay\n // (no-op in prod / when no dev server is attached).\n broadcastRouteManifest(tree)\n return generateRouteManifest(tree)\n }\n if (id === ctx.ids.RESOLVED_ENTRY_SERVER_ID) {\n // SSR tree MUST mirror client tree shape — pass theoUi through so\n // <TheoUIProvider> wraps in both. Without this, React detects a\n // hydration mismatch and silently falls back to client-only\n // render — onClick handlers never get attached.\n return generateEntryServer({\n streaming: ctx.streamingEnabled,\n theoUi: ctx.theoUi?.enabled ? { theme: ctx.theoUi.config.theme } : undefined,\n })\n }\n if (id === ctx.ids.RESOLVED_RUNTIME_CONFIG_ID) {\n // T1.3 — set globalThis.__THEO_TRANSFORMER__ so theoFetch picks it up\n const tName = ctx.transformer?.name ?? 'json'\n return [\n `// Generated by Theo Vite plugin`,\n `;(globalThis).__THEO_TRANSFORMER__ = ${JSON.stringify(tName)}`,\n `export const TRANSFORMER_NAME = ${JSON.stringify(tName)}`,\n ].join('\\n')\n }\n if (id === DEVTOOLS_RESOLVED_ID) {\n // T1.2 — virtual module re-exports the real entry from theokit's devtools subpath.\n return [\n `// Theo devtools — virtual entry (DEV only)`,\n `import('theokit/devtools/entry').catch((e) => console.error('[theo devtools] mount failed', e))`,\n ].join('\\n')\n }\n return undefined\n}\n","/**\n * T3.1 — Vite integration extension API (`defineTheoIntegration`).\n *\n * Build-time extension system (mirrors Astro Integrations). Lets third\n * parties hook into Theo's Vite lifecycle without forking the plugin.\n *\n * Strictly separated from runtime `defineTheoPlugin` (Phase 4) — different\n * concerns, different surfaces. See ADR D7 in the plan.\n */\n\nexport type HookName =\n | 'theo:config:setup'\n | 'theo:build:start'\n | 'theo:build:done'\n | 'theo:dev:start'\n\nexport interface HookContext {\n addVirtualModule?: (id: string, code: string) => void\n addRoute?: (path: string, handler: RouteHandler) => void\n [key: string]: unknown\n}\n\nexport type Hook = (ctx: HookContext) => void | Promise<void>\n\nexport type RouteHandler = (request: Request) => Response | Promise<Response>\n\nexport interface TheoIntegration {\n name: string\n hooks: Partial<Record<HookName, Hook>>\n}\n\n/**\n * Identity factory for type-checking against TheoIntegration shape.\n */\nexport function defineTheoIntegration(integration: TheoIntegration): TheoIntegration {\n return integration\n}\n\nexport class IntegrationVirtualModulePrefixError extends Error {\n constructor(integrationName: string, id: string) {\n super(\n `Integration \"${integrationName}\" tried to addVirtualModule(\"${id}\"). ` +\n `Virtual module IDs must start with \"virtual:integration:${integrationName}/\" ` +\n `to avoid collisions with Theo internals or other integrations.`,\n )\n this.name = 'IntegrationVirtualModulePrefixError'\n }\n}\n\nexport class IntegrationRouteCollisionError extends Error {\n constructor(integrationName: string, path: string) {\n super(\n `Integration \"${integrationName}\" tried to register route \"${path}\", ` +\n `but it conflicts with an existing route. Choose a different path.`,\n )\n this.name = 'IntegrationRouteCollisionError'\n }\n}\n\nexport interface IntegrationRegistryOptions {\n /** Routes already scanned from `server/routes/`. Used to detect collisions. */\n existingRoutes: string[]\n}\n\nexport interface IntegrationRoute {\n path: string\n owner: string\n}\n\nexport interface IntegrationRegistry {\n registerIntegration(integration: TheoIntegration): void\n fire(hookName: HookName, baseCtx: Record<string, unknown>): Promise<void>\n addVirtualModule(integrationName: string, id: string, code: string): void\n addRoute(integrationName: string, path: string, handler: RouteHandler): void\n getVirtualModule(id: string): string | undefined\n listVirtualModules(): string[]\n listRoutes(): IntegrationRoute[]\n /** Test helper: invoke hook surface directly without firing the lifecycle. */\n callHook(\n integrationName: string,\n hookName: HookName,\n extraCtx: Partial<HookContext>,\n ): Promise<void>\n}\n\nexport function createIntegrationRegistry(\n options: IntegrationRegistryOptions,\n): IntegrationRegistry {\n const integrations: TheoIntegration[] = []\n const virtualModules = new Map<string, { code: string; owner: string }>()\n const routes: { path: string; owner: string; handler: RouteHandler }[] = []\n const userRoutes = new Set(options.existingRoutes)\n\n function buildHookCtxFor(integrationName: string, extra: Partial<HookContext>): HookContext {\n return {\n ...extra,\n addVirtualModule: (id: string, code: string) => {\n addVirtualModule(integrationName, id, code)\n },\n addRoute: (path: string, handler: RouteHandler) => {\n addRoute(integrationName, path, handler)\n },\n }\n }\n\n function addVirtualModule(integrationName: string, id: string, code: string): void {\n const expectedPrefix = `virtual:integration:${integrationName}/`\n if (!id.startsWith(expectedPrefix)) {\n throw new IntegrationVirtualModulePrefixError(integrationName, id)\n }\n virtualModules.set(id, { code, owner: integrationName })\n }\n\n function addRoute(integrationName: string, path: string, handler: RouteHandler): void {\n if (userRoutes.has(path)) {\n throw new IntegrationRouteCollisionError(integrationName, path)\n }\n if (routes.some((r) => r.path === path)) {\n throw new IntegrationRouteCollisionError(integrationName, path)\n }\n routes.push({ path, owner: integrationName, handler })\n }\n\n return {\n registerIntegration(integration) {\n integrations.push(integration)\n },\n async fire(hookName, baseCtx) {\n for (const intg of integrations) {\n const hook = intg.hooks[hookName]\n if (!hook) continue\n try {\n await hook(buildHookCtxFor(intg.name, baseCtx))\n } catch (err) {\n const e = err instanceof Error ? err : new Error(String(err))\n const wrapped = new Error(\n `Integration \"${intg.name}\" hook \"${hookName}\" threw: ${e.message}`,\n )\n ;(wrapped as { cause?: unknown }).cause = e\n throw wrapped\n }\n }\n },\n addVirtualModule,\n addRoute,\n getVirtualModule(id) {\n return virtualModules.get(id)?.code\n },\n listVirtualModules() {\n return Array.from(virtualModules.keys())\n },\n listRoutes() {\n return routes.map((r) => ({ path: r.path, owner: r.owner }))\n },\n // eslint-disable-next-line @typescript-eslint/require-await -- async surface for the public callHook contract\n async callHook(integrationName, _hookName, extraCtx) {\n // Build the ctx for parity with `fire()` so test invocations\n // observe the same shape; we don't actually need the value here.\n buildHookCtxFor(integrationName, extraCtx)\n // Surface for direct test invocation; production code uses fire()\n if (typeof extraCtx.addVirtualModule === 'function') {\n extraCtx.addVirtualModule(\n `virtual:integration:${integrationName}/test`,\n 'export const ok = true',\n )\n }\n },\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAIA,SAAS,cAAAA,mBAAkB;AAC3B,SAAS,WAAAC,WAAS,WAAAC,gBAAe;AACjC,SAAS,qBAAqB;;;ACF9B,SAAS,kBAAkB;AAC3B,SAAS,eAAe;AAiBjB,SAAS,cAAc,KAA6C;AAEzE,QAAM,MAAM,WAAW,QAAQ,IAAI,YAAY,UAAU,CAAC,IAAI,QAAQ;AAKtE,QAAM,oBAA8B,CAAC;AACrC,aAAW,aAAa,CAAC,kBAAkB,gBAAgB,cAAc,GAAG;AAC1E,UAAM,OAAO,QAAQ,IAAI,aAAa,SAAS;AAC/C,QAAI,WAAW,IAAI,EAAG,mBAAkB,KAAK,KAAK,SAAS,EAAE;AAAA,EAC/D;AAIA,QAAM,gBACJ,IAAI,YAAY,OAAO,KAAK,IAAI,QAAQ,EAAE,SAAS,IAC/C,yBAAyB,IAAI,QAAQ,IACrC;AAQN,SAAO;AAAA,IACL,WAAW;AAAA,IACX,cAAc;AAAA,MACZ,GAAI,IAAI,oBAAoB,SAAS,IAAI,EAAE,SAAS,IAAI,oBAAoB,IAAI,CAAC;AAAA,MACjF,OAAO;AAAA,IACT;AAAA,IACA,QAAQ;AAAA,MACN,GAAI,kBAAkB,SAAS,IAAI,EAAE,QAAQ,EAAE,aAAa,kBAAkB,EAAE,IAAI,CAAC;AAAA,MACrF,GAAI,kBAAkB,SAAY,EAAE,OAAO,cAAc,IAAI,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,MAM9D,OAAO;AAAA,QACL,SAAS;AAAA,UACP;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AAAA,UACA;AA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AA;AAAA;AAAA;AAAA;AAAA,IAOA,SAAS;AACP,aAAO,cAAc;AAAA,QACnB;AAAA,QACA;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,qBAAqB,yBAAyB,aAAa,QAAQ,gBAAgB;AAAA,MACrF,CAAC;AAAA,IACH;AAAA;AAAA;AAAA;AAAA,IAKA,oBAAoB;AAAA,MAClB,OAAO;AAAA,MACP,QAAQ,MAAsB;AAC5B,eAAO,sBAAsB,MAAM;AAAA,UACjC,WAAW;AAAA,UACX;AAAA,UACA;AAAA,QACF,CAAC;AAAA,MACH;AAAA,IACF;AAAA;AAAA;AAAA;AAAA,IAKA,UAAU,IAAY;AACpB,aAAO,uBAAuB,IAAI,kBAAkB;AAAA,IACtD;AAAA,IAEA,KAAK,IAAY;AACf,aAAO,kBAAkB,IAAI;AAAA,QAC3B,KAAK;AAAA,QACL;AAAA,QACA;AAAA,QACA,kBAAkB,QAAQ,iBAAiB;AAAA,QAC3C;AAAA,QACA;AAAA,MACF,CAAC;AAAA,IACH;AAAA;AAAA;AAAA;AAAA;AAAA,IAMA,MAAM,gBAAgB,QAAQ;AAC5B,YAAM,mBAAmB,QAAQ;AAAA,QAC/B;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,WAAW;AAAA,QACX;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,UAAU,QAAQ;AAAA,QAClB,WAAW,QAAQ;AAAA,QACnB,4BAA4B,QAAQ;AAAA,QACpC,sBAAsB;AAAA,QACtB,oBAAoB;AAAA,MACtB,CAAC;AAAA,IACH;AAAA,EACF;AACF;","names":["existsSync","resolve","dirname","existsSync","resolve","z","z","z","z","z","z","z","z","z","resolve","existsSync","existsSync","resolve","existsSync","join","resolve","existsSync","join","resolve","randomUUID","jsonError","jsonError","PREFIX","randomUUID","resolve","resolve","readFileSync","resolve","resolve","readFileSync","resolve","resolve","resolve","existsSync","readFileSync","dirname","join","existsSync","readFileSync","dirname","join","join","existsSync","readFileSync","dirname","join","existsSync","existsSync","resolve","existsSync","resolve","SCRIPT_TAG","existsSync","resolve","basename","resolve","existsSync","dirname"]}