theokit 0.14.0 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/dist/{actions-virtual-module-3CDQTWOC.js → actions-virtual-module-G4BANOLW.js} +5 -3
  2. package/dist/{actions-virtual-module-3CDQTWOC.js.map → actions-virtual-module-G4BANOLW.js.map} +1 -1
  3. package/dist/agent-MN7XGJR3.js +209 -0
  4. package/dist/agent-MN7XGJR3.js.map +1 -0
  5. package/dist/{app-typed-client-CSOK7NPC.js → app-typed-client-Z6BHD4MF.js} +5 -3
  6. package/dist/{app-typed-client-CSOK7NPC.js.map → app-typed-client-Z6BHD4MF.js.map} +1 -1
  7. package/dist/{build-5K7LK77K.js → build-QDAFSKKW.js} +11 -7
  8. package/dist/{build-5K7LK77K.js.map → build-QDAFSKKW.js.map} +1 -1
  9. package/dist/chunk-34YQOXGM.js +37 -0
  10. package/dist/chunk-34YQOXGM.js.map +1 -0
  11. package/dist/{chunk-7YZHAQU7.js → chunk-567NA7Y6.js} +8 -99
  12. package/dist/chunk-567NA7Y6.js.map +1 -0
  13. package/dist/{chunk-BQDGES7C.js → chunk-GBXLKYIA.js} +19 -43
  14. package/dist/chunk-GBXLKYIA.js.map +1 -0
  15. package/dist/chunk-GDN3PXFH.js +101 -0
  16. package/dist/chunk-GDN3PXFH.js.map +1 -0
  17. package/dist/chunk-NBWB4S46.js +81 -0
  18. package/dist/chunk-NBWB4S46.js.map +1 -0
  19. package/dist/{chunk-F4YUPDJ2.js → chunk-NXTF5PPW.js} +15 -41
  20. package/dist/chunk-NXTF5PPW.js.map +1 -0
  21. package/dist/{chunk-S7Y5WLZY.js → chunk-OTFIRP6S.js} +76 -46
  22. package/dist/chunk-OTFIRP6S.js.map +1 -0
  23. package/dist/chunk-P37RZRFV.js +31 -0
  24. package/dist/chunk-P37RZRFV.js.map +1 -0
  25. package/dist/{chunk-RBHCJHRR.js → chunk-UOR6JTCI.js} +142 -7
  26. package/dist/chunk-UOR6JTCI.js.map +1 -0
  27. package/dist/{chunk-637WJB7Z.js → chunk-ZJDKAD3L.js} +55 -14
  28. package/dist/chunk-ZJDKAD3L.js.map +1 -0
  29. package/dist/cli/index.js +22 -6
  30. package/dist/cli/index.js.map +1 -1
  31. package/dist/{dev-QOEVYNBG.js → dev-TEE4T6ZB.js} +13 -8
  32. package/dist/{dev-QOEVYNBG.js.map → dev-TEE4T6ZB.js.map} +1 -1
  33. package/dist/{dev-emit-5MDSBP5D.js → dev-emit-VJ5CFMPY.js} +5 -3
  34. package/dist/{dev-emit-5MDSBP5D.js.map → dev-emit-VJ5CFMPY.js.map} +1 -1
  35. package/dist/index.js +1 -1
  36. package/dist/{info-OUEUZOT7.js → info-7PE2PZJI.js} +6 -5
  37. package/dist/{info-OUEUZOT7.js.map → info-7PE2PZJI.js.map} +1 -1
  38. package/dist/{internal-api-EFKZWIYZ.js → internal-api-J27TYE2I.js} +7 -4
  39. package/dist/load-config-JKYO5RFK.js +14 -0
  40. package/dist/{openapi-FHY6HC6I.js → openapi-MXMLZCXC.js} +7 -4
  41. package/dist/{openapi-FHY6HC6I.js.map → openapi-MXMLZCXC.js.map} +1 -1
  42. package/dist/{registry-34LL7NF4.js → registry-XJUYD2OU.js} +2 -2
  43. package/dist/{routes-EW7TP7NJ.js → routes-NNBEZSGN.js} +5 -3
  44. package/dist/{routes-EW7TP7NJ.js.map → routes-NNBEZSGN.js.map} +1 -1
  45. package/dist/{start-2KG4JSXM.js → start-7MQEEQH4.js} +68 -8
  46. package/dist/start-7MQEEQH4.js.map +1 -0
  47. package/dist/{static-55G3LX2I.js → static-7ARBVDJF.js} +4 -4
  48. package/dist/vite-plugin/index.js +1 -1
  49. package/dist/{vite-plugin-TDIDZ5U7.js → vite-plugin-GC6WCU4P.js} +11 -7
  50. package/dist/vite-plugin-GC6WCU4P.js.map +1 -0
  51. package/package.json +2 -2
  52. package/dist/chunk-637WJB7Z.js.map +0 -1
  53. package/dist/chunk-7YZHAQU7.js.map +0 -1
  54. package/dist/chunk-BQDGES7C.js.map +0 -1
  55. package/dist/chunk-F4YUPDJ2.js.map +0 -1
  56. package/dist/chunk-RBHCJHRR.js.map +0 -1
  57. package/dist/chunk-S7Y5WLZY.js.map +0 -1
  58. package/dist/start-2KG4JSXM.js.map +0 -1
  59. /package/dist/{internal-api-EFKZWIYZ.js.map → internal-api-J27TYE2I.js.map} +0 -0
  60. /package/dist/{vite-plugin-TDIDZ5U7.js.map → load-config-JKYO5RFK.js.map} +0 -0
  61. /package/dist/{registry-34LL7NF4.js.map → registry-XJUYD2OU.js.map} +0 -0
  62. /package/dist/{static-55G3LX2I.js.map → static-7ARBVDJF.js.map} +0 -0
@@ -1,35 +1,12 @@
1
1
  #!/usr/bin/env node
2
2
  import "tsx/esm";
3
+ import {
4
+ walkSourceFiles
5
+ } from "./chunk-P37RZRFV.js";
3
6
 
4
7
  // src/server/scan/action-scan.ts
5
8
  import { existsSync, readFileSync, statSync } from "fs";
6
- import { extname as extname2, join as join2, relative } from "path";
7
-
8
- // src/server/_internal/scan-walker.ts
9
- import { readdirSync } from "fs";
10
- import { extname, join, resolve } from "path";
11
- function walkSourceFiles(root, opts, onFile) {
12
- const skipPrefixes = opts.skipPrefixes ?? ["_", "."];
13
- const visit = (dir) => {
14
- let entries;
15
- try {
16
- entries = readdirSync(dir, { withFileTypes: true });
17
- } catch {
18
- return;
19
- }
20
- for (const entry of entries) {
21
- const fullPath = join(dir, entry.name);
22
- if (entry.isDirectory() && !skipPrefixes.some((p) => entry.name.startsWith(p))) {
23
- visit(fullPath);
24
- } else if (entry.isFile() && opts.extensions.has(extname(entry.name))) {
25
- onFile(resolve(fullPath));
26
- }
27
- }
28
- };
29
- visit(root);
30
- }
31
-
32
- // src/server/scan/action-scan.ts
9
+ import { extname, join, relative } from "path";
33
10
  var ActionScanError = class extends Error {
34
11
  code;
35
12
  conflictingPaths;
@@ -44,7 +21,7 @@ var ACTION_EXTENSIONS = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx"]);
44
21
  var TEST_FILE_RE = /\.(test|spec)\.(ts|tsx|js|jsx)$/;
45
22
  var RESERVED_NAMES = /* @__PURE__ */ new Set(["index", "constructor", "__proto__", "prototype", "hasOwnProperty"]);
46
23
  function scanServerActions(serverDir) {
47
- const actionsDir = join2(serverDir, "actions");
24
+ const actionsDir = join(serverDir, "actions");
48
25
  if (!existsSync(actionsDir) || !statSync(actionsDir).isDirectory()) {
49
26
  return [];
50
27
  }
@@ -53,7 +30,7 @@ function scanServerActions(serverDir) {
53
30
  if (TEST_FILE_RE.test(absPath)) return;
54
31
  let rel = relative(actionsDir, absPath);
55
32
  rel = rel.replace(/\\/g, "/");
56
- rel = rel.slice(0, -extname2(rel).length);
33
+ rel = rel.slice(0, -extname(rel).length);
57
34
  results.push({
58
35
  filePath: absPath,
59
36
  actionPath: rel
@@ -62,7 +39,7 @@ function scanServerActions(serverDir) {
62
39
  return results;
63
40
  }
64
41
  function scanServerActionsEnriched(serverDir) {
65
- const actionsDir = join2(serverDir, "actions");
42
+ const actionsDir = join(serverDir, "actions");
66
43
  if (!existsSync(actionsDir) || !statSync(actionsDir).isDirectory()) {
67
44
  return [];
68
45
  }
@@ -72,7 +49,7 @@ function scanServerActionsEnriched(serverDir) {
72
49
  if (TEST_FILE_RE.test(absPath)) return;
73
50
  const rel = relative(actionsDir, absPath).replace(/\\/g, "/");
74
51
  if (rel.startsWith("schemas/")) return;
75
- const name = rel.slice(0, -extname2(rel).length);
52
+ const name = rel.slice(0, -extname(rel).length);
76
53
  const basename2 = name.includes("/") ? name.split("/").pop() ?? name : name;
77
54
  if (RESERVED_NAMES.has(basename2)) {
78
55
  throw new ActionScanError(
@@ -143,10 +120,10 @@ function extractActionExportNames(stripped) {
143
120
  return [...names];
144
121
  }
145
122
  function detectSchemaFile(actionsDir, basename2) {
146
- const schemasDir = join2(actionsDir, "schemas");
123
+ const schemasDir = join(actionsDir, "schemas");
147
124
  if (!existsSync(schemasDir)) return void 0;
148
125
  for (const ext of [".ts", ".tsx", ".js", ".jsx"]) {
149
- const candidate = join2(schemasDir, `${basename2}${ext}`);
126
+ const candidate = join(schemasDir, `${basename2}${ext}`);
150
127
  if (existsSync(candidate)) return candidate;
151
128
  }
152
129
  return void 0;
@@ -214,7 +191,7 @@ var HTTP_METHOD_LOWERCASE = HTTP_METHODS.map((m) => m.toLowerCase());
214
191
 
215
192
  // src/server/scan/scan.ts
216
193
  import { existsSync as existsSync2, statSync as statSync2 } from "fs";
217
- import { basename, extname as extname3, join as join3, relative as relative2 } from "path";
194
+ import { basename, extname as extname2, join as join2, relative as relative2 } from "path";
218
195
 
219
196
  // src/server/scan/detect-http-methods.ts
220
197
  import { readFileSync as readFileSync2 } from "fs";
@@ -332,14 +309,14 @@ function splitDottedSegmentOutsideBrackets(segment) {
332
309
  }
333
310
  function buildDirectoryNestedSuggestion(filePath, routesDir) {
334
311
  const rel = relative2(routesDir, filePath).replace(/\\/g, "/");
335
- const ext = extname3(rel);
312
+ const ext = extname2(rel);
336
313
  const withoutExt = rel.slice(0, -ext.length);
337
314
  const segments = withoutExt.split("/").flatMap(splitDottedSegmentOutsideBrackets);
338
315
  return `routes/${segments.join("/")}${ext}`;
339
316
  }
340
317
  function assertNoDottedSegment(filePath, routesDir) {
341
318
  const rel = relative2(routesDir, filePath).replace(/\\/g, "/");
342
- const ext = extname3(rel);
319
+ const ext = extname2(rel);
343
320
  const withoutExt = rel.slice(0, -ext.length);
344
321
  const segments = withoutExt.split("/");
345
322
  for (const seg of segments) {
@@ -353,7 +330,7 @@ function assertNoDottedSegment(filePath, routesDir) {
353
330
  }
354
331
  function fileToRoutePath(filePath, routesDir) {
355
332
  let rel = relative2(routesDir, filePath);
356
- const ext = extname3(rel);
333
+ const ext = extname2(rel);
357
334
  rel = rel.slice(0, -ext.length);
358
335
  rel = rel.replace(/\\/g, "/");
359
336
  if (rel.endsWith("/index")) {
@@ -366,7 +343,7 @@ function fileToRoutePath(filePath, routesDir) {
366
343
  return `/api/${rel}`;
367
344
  }
368
345
  function scanServerRoutes(serverDir) {
369
- const routesDir = join3(serverDir, "routes");
346
+ const routesDir = join2(serverDir, "routes");
370
347
  if (!existsSync2(routesDir) || !statSync2(routesDir).isDirectory()) {
371
348
  return [];
372
349
  }
@@ -408,10 +385,10 @@ function scanServerRoutes(serverDir) {
408
385
 
409
386
  // src/server/scan/ws-scan.ts
410
387
  import { existsSync as existsSync3, statSync as statSync3 } from "fs";
411
- import { extname as extname4, join as join4, relative as relative3 } from "path";
388
+ import { extname as extname3, join as join3, relative as relative3 } from "path";
412
389
  var WS_EXTENSIONS = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx"]);
413
390
  function scanWebSocketRoutes(serverDir) {
414
- const wsDir = join4(serverDir, "ws");
391
+ const wsDir = join3(serverDir, "ws");
415
392
  if (!existsSync3(wsDir) || !statSync3(wsDir).isDirectory()) {
416
393
  return [];
417
394
  }
@@ -419,7 +396,7 @@ function scanWebSocketRoutes(serverDir) {
419
396
  walkSourceFiles(wsDir, { extensions: WS_EXTENSIONS }, (absPath) => {
420
397
  let rel = relative3(wsDir, absPath);
421
398
  rel = rel.replace(/\\/g, "/");
422
- rel = rel.slice(0, -extname4(rel).length);
399
+ rel = rel.slice(0, -extname3(rel).length);
423
400
  if (rel.endsWith("/index")) rel = rel.slice(0, -6);
424
401
  else if (rel === "index") rel = "";
425
402
  results.push({
@@ -431,7 +408,6 @@ function scanWebSocketRoutes(serverDir) {
431
408
  }
432
409
 
433
410
  export {
434
- walkSourceFiles,
435
411
  scanServerActions,
436
412
  scanServerActionsEnriched,
437
413
  compilePattern,
@@ -440,4 +416,4 @@ export {
440
416
  scanServerRoutes,
441
417
  scanWebSocketRoutes
442
418
  };
443
- //# sourceMappingURL=chunk-BQDGES7C.js.map
419
+ //# sourceMappingURL=chunk-GBXLKYIA.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/server/scan/action-scan.ts","../src/server/scan/match.ts","../src/core/contracts/http-methods.ts","../src/server/scan/scan.ts","../src/server/scan/detect-http-methods.ts","../src/server/scan/errors.ts","../src/server/scan/ws-scan.ts"],"sourcesContent":["/* eslint-disable security/detect-non-literal-fs-filename --\n * Build-time scanner: walks `serverDir/actions/` derived from cwd.\n * No HTTP input ever reaches these fs calls.\n */\nimport { existsSync, readFileSync, statSync } from 'node:fs'\nimport { extname, join, relative } from 'node:path'\n\nimport { walkSourceFiles } from '../_internal/scan-walker.js'\n\nexport interface ActionNode {\n filePath: string\n actionPath: string\n}\n\n/**\n * Enriched manifest entry per plan g3-server-actions-and-useaction v1.2\n * § Phase 1 / T1.4 + ADR D4. Consumed by virtual module `@theo/actions`\n * (T3.1) and G4 devtools \"Actions\" tab (T5.1).\n */\nexport interface ActionManifestEntry {\n name: string\n filePath: string\n urlPath: string\n accept: 'form' | 'json'\n hasInput: boolean\n /**\n * P#4 plugin-forms shared-schema convention (per plan p4-plugin-forms v1.1 T1.1).\n * When present, points to an isomorphic schema file at\n * `<serverDir>/actions/schemas/<basename>.ts` exporting `export const schema = z.object(...)`.\n * Virtual module emits `import {schema} from '<schemaFilePath>'` + attaches as\n * `actions.X.__zodSchema` so client-side <TheoForm> can drive zodResolver.\n * Undefined when convention not followed (graceful degrade — TheoForm still\n * works via explicit `schema={...}` prop escape hatch).\n */\n schemaFilePath?: string\n}\n\n/**\n * EC-2: structured error for scan-time defects (name collision, reserved\n * identifier, etc.). Throw at scan time to fail loud — silent shadowing is\n * a security/correctness footgun.\n */\nexport class ActionScanError extends Error {\n readonly code: 'NAME_COLLISION' | 'RESERVED_NAME'\n readonly conflictingPaths: readonly string[]\n\n constructor(\n code: 'NAME_COLLISION' | 'RESERVED_NAME',\n message: string,\n conflictingPaths: readonly string[],\n ) {\n super(message)\n // T4.1 fix: identify class by name so serverErrorToEnvelope boundary\n // translator can route via class-name lookup (without this, runtime\n // `err.name` defaults to 'Error' and the meta.name diagnostic is wrong).\n this.name = 'ActionScanError'\n this.code = code\n this.conflictingPaths = conflictingPaths\n }\n}\n\nconst ACTION_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx'])\nconst TEST_FILE_RE = /\\.(test|spec)\\.(ts|tsx|js|jsx)$/\nconst RESERVED_NAMES = new Set(['index', 'constructor', '__proto__', 'prototype', 'hasOwnProperty'])\n\n/**\n * Backward-compatible: original simple-shape scanner used by existing\n * consumers. Preserved verbatim; new consumers should call\n * `scanServerActionsEnriched`.\n */\nexport function scanServerActions(serverDir: string): ActionNode[] {\n const actionsDir = join(serverDir, 'actions')\n if (!existsSync(actionsDir) || !statSync(actionsDir).isDirectory()) {\n return []\n }\n\n const results: ActionNode[] = []\n walkSourceFiles(actionsDir, { extensions: ACTION_EXTENSIONS }, (absPath) => {\n if (TEST_FILE_RE.test(absPath)) return\n let rel = relative(actionsDir, absPath)\n rel = rel.replace(/\\\\/g, '/')\n rel = rel.slice(0, -extname(rel).length)\n results.push({\n filePath: absPath,\n actionPath: rel,\n })\n })\n return results\n}\n\n/**\n * Enriched scan: light AST detection of `accept: 'form'|'json'` + `input:`\n * presence via regex-after-comment-stripping (EC-9). Throws ActionScanError\n * on file/dir name collision (EC-2) or reserved JS identifier names.\n *\n * Output `ActionManifestEntry[]` is sorted by `name` for deterministic\n * `.theokit/actions-manifest.json` emission.\n */\nexport function scanServerActionsEnriched(serverDir: string): ActionManifestEntry[] {\n const actionsDir = join(serverDir, 'actions')\n if (!existsSync(actionsDir) || !statSync(actionsDir).isDirectory()) {\n return []\n }\n\n const seenNames = new Set<string>()\n const entries: ActionManifestEntry[] = []\n\n // Collect first; collision-check after the full walk.\n walkSourceFiles(actionsDir, { extensions: ACTION_EXTENSIONS }, (absPath) => {\n if (TEST_FILE_RE.test(absPath)) return\n const rel = relative(actionsDir, absPath).replace(/\\\\/g, '/')\n // P#4 plugin-forms — `schemas/` subdir holds isomorphic zod schemas\n // for the shared-schema convention (T1.1). NOT executable actions; skip.\n if (rel.startsWith('schemas/')) return\n const name = rel.slice(0, -extname(rel).length)\n\n const basename = name.includes('/') ? (name.split('/').pop() ?? name) : name\n if (RESERVED_NAMES.has(basename)) {\n throw new ActionScanError(\n 'RESERVED_NAME',\n `Reserved JS identifier \"${basename}\" cannot be an action name (${absPath})`,\n [absPath],\n )\n }\n // File-level collision (one file appearing twice) is impossible via the\n // walker; per-export collision check happens inside the export loop below.\n const source = readFileSync(absPath, 'utf8')\n const stripped = stripComments(source)\n const accept = /\\baccept\\s*:\\s*['\"]form['\"]/.test(stripped) ? 'form' : 'json'\n const hasInput = /\\binput\\s*:\\s*z\\./.test(stripped) || /\\binput\\s*:\\s*\\w+\\(/.test(stripped)\n\n // T7.1 wire fix — extract exports so the urlPath includes the second\n // segment required by action-middleware (`/api/__actions/<file>/<export>`).\n // Each named action export becomes its own manifest entry.\n // Proxy key on the EXPORT name (so consumers write `actions.saveMemory(input)`).\n // URL keeps the runtime 2-segment shape `/api/__actions/<file>/<export>`\n // expected by action-middleware. Cross-file export collisions become a\n // scan error to surface the ambiguity early.\n const exportNames = extractActionExportNames(stripped)\n // P#4 plugin-forms shared-schema convention: check for\n // `<actionsDir>/schemas/<basename>.ts` (or .tsx/.js/.jsx).\n // Skip when actions live in subdirs (`schemas/` is flat by convention).\n const schemaFilePath = name.includes('/') ? undefined : detectSchemaFile(actionsDir, basename)\n for (const exportName of exportNames) {\n const proxyKey = exportName === 'default' ? name : exportName\n if (seenNames.has(proxyKey)) {\n throw new ActionScanError(\n 'NAME_COLLISION',\n `Duplicate action proxy key \"${proxyKey}\" (two files export the same name)`,\n [absPath],\n )\n }\n seenNames.add(proxyKey)\n const entry: ActionManifestEntry = {\n name: proxyKey,\n filePath: absPath,\n urlPath: `/api/__actions/${name}/${exportName}`,\n accept,\n hasInput,\n }\n if (schemaFilePath !== undefined) {\n entry.schemaFilePath = schemaFilePath\n }\n entries.push(entry)\n }\n })\n\n // EC-2: detect file vs dir collisions (e.g., foo.ts AND foo/bar.ts).\n // After walk completes, any name that has children prefixed `name/` triggers collision.\n for (const entry of entries) {\n const childPrefix = `${entry.name}/`\n const conflictingChild = entries.find((other) => other.name.startsWith(childPrefix))\n if (conflictingChild) {\n throw new ActionScanError(\n 'NAME_COLLISION',\n `Action \"${entry.name}\" conflicts with directory of same name containing \"${conflictingChild.name}\"`,\n [entry.filePath, conflictingChild.filePath],\n )\n }\n }\n\n entries.sort((a, b) => {\n if (a.name < b.name) return -1\n if (a.name > b.name) return 1\n return 0\n })\n return entries\n}\n\n/**\n * Strip JavaScript line + block comments from source. Simple state machine\n * (does not parse strings — false positives if a comment marker appears\n * inside a string literal, but that's an acceptable trade-off for v1 vs\n * full AST parse).\n */\n/**\n * Extract action export names via regex over comment-stripped source.\n * Matches `export const <name> = defineAction(...)`, `export default\n * defineAction(...)`, and `export function <name>(...)` forms. Returns\n * `['default']` when no named action exports are found (best-effort fallback\n * for default-export shapes the regex misses).\n */\nfunction extractActionExportNames(stripped: string): string[] {\n const names = new Set<string>()\n const namedRe = /\\bexport\\s+(?:const|let|var|function\\*?)\\s+([a-zA-Z_$][\\w$]*)\\s*[=(]/g\n let m: RegExpExecArray | null\n while ((m = namedRe.exec(stripped)) !== null) {\n const name = m[1]\n if (typeof name === 'string' && name.length > 0) names.add(name)\n }\n if (/\\bexport\\s+default\\s+defineAction\\b/.test(stripped)) {\n names.add('default')\n }\n if (names.size === 0) names.add('default')\n return [...names]\n}\n\n/**\n * P#4 plugin-forms shared-schema convention helper (per plan p4-plugin-forms v1.1 T1.1).\n * Returns the resolved path to `<actionsDir>/schemas/<basename>.<ext>` if it exists.\n * Tries `.ts`, `.tsx`, `.js`, `.jsx` in that order. Returns undefined when no match.\n */\nfunction detectSchemaFile(actionsDir: string, basename: string): string | undefined {\n const schemasDir = join(actionsDir, 'schemas')\n if (!existsSync(schemasDir)) return undefined\n for (const ext of ['.ts', '.tsx', '.js', '.jsx']) {\n const candidate = join(schemasDir, `${basename}${ext}`)\n if (existsSync(candidate)) return candidate\n }\n return undefined\n}\n\nfunction stripComments(source: string): string {\n let out = ''\n let i = 0\n while (i < source.length) {\n const ch = source[i]\n const next = source[i + 1]\n if (ch === '/' && next === '/') {\n // Line comment: skip until newline\n while (i < source.length && source[i] !== '\\n') i++\n continue\n }\n if (ch === '/' && next === '*') {\n // Block comment: skip until */\n i += 2\n while (i < source.length - 1 && !(source[i] === '*' && source[i + 1] === '/')) i++\n i += 2\n continue\n }\n out += ch\n i++\n }\n return out\n}\n","export interface ServerRouteNode {\n filePath: string\n routePath: string\n paramNames: string[]\n pattern: RegExp\n /** HTTP methods (uppercase) the route file exports. Optional for backward\n * compatibility with manifests generated before G1. Empty array means the\n * file has no HTTP exports (util-only); undefined means \"not detected\". */\n methods?: string[]\n}\n\nexport function compilePattern(routePath: string): {\n pattern: RegExp\n paramNames: string[]\n} {\n const paramNames: string[] = []\n // Single pass: handle both catch-all (:...name) and regular (:name) params\n const regexStr = routePath.replace(/:(?:\\.\\.\\.)?([^/]+)/g, (match: string, name: string) => {\n paramNames.push(name)\n // Catch-all matches across slashes, regular matches single segment\n return match.startsWith(':...') ? '(.+)' : '([^/]+)'\n })\n // `regexStr` is derived from a developer-authored route path (build-time\n // input, not HTTP-controlled). The `security/detect-non-literal-regexp`\n // rule cannot see this constraint — disable narrowly.\n // eslint-disable-next-line security/detect-non-literal-regexp -- route pattern from build-time scan, never HTTP input\n return { pattern: new RegExp(`^${regexStr}$`), paramNames }\n}\n\nexport function matchRoute(\n url: string,\n routes: ServerRouteNode[],\n): { route: ServerRouteNode; params: Record<string, string> } | null {\n // Strip query string and trailing slash\n let path = url.split('?')[0]\n if (path.length > 1 && path.endsWith('/')) {\n path = path.slice(0, -1)\n }\n\n for (const route of routes) {\n const match = route.pattern.exec(path)\n if (match) {\n const params: Record<string, string> = {}\n route.paramNames.forEach((name, i) => {\n params[name] = match[i + 1]\n })\n return { route, params }\n }\n }\n return null\n}\n","/**\n * Canonical HTTP method set used by the typed client codegen + Proxy runtime.\n * Kept here (under `server/scan/`) because the SCAN is the first consumer —\n * `vite-plugin/app-typed-client.ts` (Phase 2) and `client/app-client.ts` (Phase 3)\n * import from here to ensure a single source of truth.\n *\n * Lowercase variants are derived via `.toLowerCase()` at call sites; we do not\n * export them separately to avoid drift between the two lists.\n */\n\nexport const HTTP_METHODS = [\n 'GET',\n 'POST',\n 'PUT',\n 'PATCH',\n 'DELETE',\n 'HEAD',\n 'OPTIONS',\n] as const\n\nexport type HttpMethod = (typeof HTTP_METHODS)[number]\n\nexport const HTTP_METHOD_LOWERCASE = HTTP_METHODS.map((m) => m.toLowerCase()) as readonly string[]\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Build-time scanner: walks `serverDir/routes/` derived from cwd.\n * No HTTP input ever reaches these fs calls.\n */\nimport { existsSync, statSync } from 'node:fs'\nimport { basename, extname, join, relative } from 'node:path'\n\nimport { walkSourceFiles } from '../_internal/scan-walker.js'\n\nimport { detectExportedHttpMethods } from './detect-http-methods.js'\nimport { RouterConventionError } from './errors.js'\nimport { compilePattern, type ServerRouteNode } from './match.js'\n\nconst ROUTE_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx'])\n\n// EC-4: Co-located unit/spec tests must be silently skipped by the scanner,\n// BEFORE the dotted-basename check fires. Matches `*.test.ts|tsx|js|jsx`\n// and `*.spec.ts|tsx|js|jsx`.\nconst TEST_OR_SPEC_RE = /\\.(test|spec)\\.[jt]sx?$/\n\nfunction isTestOrSpecFile(filePath: string): boolean {\n return TEST_OR_SPEC_RE.test(basename(filePath))\n}\n\nfunction hasDotOutsideBrackets(segment: string): boolean {\n let depth = 0\n for (const ch of segment) {\n if (ch === '[') depth++\n else if (ch === ']') depth--\n else if (ch === '.' && depth === 0) return true\n }\n return false\n}\n\nfunction splitDottedSegmentOutsideBrackets(segment: string): string[] {\n const parts: string[] = []\n let current = ''\n let depth = 0\n for (const ch of segment) {\n if (ch === '[') {\n depth++\n current += ch\n } else if (ch === ']') {\n depth--\n current += ch\n } else if (ch === '.' && depth === 0) {\n if (current) parts.push(current)\n current = ''\n } else {\n current += ch\n }\n }\n if (current) parts.push(current)\n return parts\n}\n\nfunction buildDirectoryNestedSuggestion(filePath: string, routesDir: string): string {\n const rel = relative(routesDir, filePath).replace(/\\\\/g, '/')\n const ext = extname(rel)\n const withoutExt = rel.slice(0, -ext.length)\n const segments = withoutExt.split('/').flatMap(splitDottedSegmentOutsideBrackets)\n return `routes/${segments.join('/')}${ext}`\n}\n\nfunction assertNoDottedSegment(filePath: string, routesDir: string): void {\n const rel = relative(routesDir, filePath).replace(/\\\\/g, '/')\n const ext = extname(rel)\n const withoutExt = rel.slice(0, -ext.length)\n const segments = withoutExt.split('/')\n for (const seg of segments) {\n if (hasDotOutsideBrackets(seg)) {\n throw new RouterConventionError({\n file: filePath,\n suggestion: buildDirectoryNestedSuggestion(filePath, routesDir),\n })\n }\n }\n}\n\nfunction fileToRoutePath(filePath: string, routesDir: string): string {\n let rel = relative(routesDir, filePath)\n // Strip extension\n const ext = extname(rel)\n rel = rel.slice(0, -ext.length)\n // Normalize separators\n rel = rel.replace(/\\\\/g, '/')\n // Strip index suffix\n if (rel.endsWith('/index')) {\n rel = rel.slice(0, -6)\n } else if (rel === 'index') {\n rel = ''\n }\n // Replace [...param] with :...param (catch-all, before regular params).\n // Replace [param] with :param. Inputs are file paths bounded by the\n // OS filename limit; the bracket capture is bounded by `]`.\n rel = rel.replace(/\\[\\.\\.\\.([^\\]]+)\\]/g, ':...$1')\n // eslint-disable-next-line sonarjs/slow-regex -- bounded by `]`; input is a single filename\n rel = rel.replace(/\\[([^\\]]+)\\]/g, ':$1')\n return `/api/${rel}`\n}\n\nexport function scanServerRoutes(serverDir: string): ServerRouteNode[] {\n const routesDir = join(serverDir, 'routes')\n if (!existsSync(routesDir) || !statSync(routesDir).isDirectory()) {\n return []\n }\n\n const results: ServerRouteNode[] = []\n walkSourceFiles(routesDir, { extensions: ROUTE_EXTENSIONS }, (absPath) => {\n // EC-4: skip co-located test/spec files BEFORE the dotted-basename check\n if (isTestOrSpecFile(absPath)) return\n\n // G6 T1.1: reject dotted basenames (legacy convention that produced wrong\n // paramNames due to greedy `:(?:\\.\\.\\.)?([^/]+)` regex in compilePattern).\n assertNoDottedSegment(absPath, routesDir)\n\n const routePath = fileToRoutePath(absPath, routesDir)\n const { pattern, paramNames } = compilePattern(routePath)\n const methods = detectExportedHttpMethods(absPath)\n results.push({\n filePath: absPath,\n routePath,\n paramNames,\n pattern,\n methods,\n })\n })\n\n // T1.4 / EC-2 — refuse to scan if a user route collides with the reserved\n // batch endpoint path. User must rename or disable batching.\n const conflicting = results.find((r) => r.routePath === '/api/__theo_batch__')\n if (conflicting) {\n throw new Error(\n `Server route ${conflicting.filePath} resolves to '/api/__theo_batch__' which is reserved for the batch endpoint. Rename the route or disable batching in theo.config.ts.`,\n )\n }\n\n // Sort: static first, then dynamic, then catch-all last\n const isCatchAll = (route: ServerRouteNode) => route.routePath.includes(':...')\n results.sort((a, b) => {\n const aStatic = a.paramNames.length === 0\n const bStatic = b.paramNames.length === 0\n const aCatchAll = isCatchAll(a)\n const bCatchAll = isCatchAll(b)\n\n // Static routes first\n if (aStatic && !bStatic) return -1\n if (!aStatic && bStatic) return 1\n // Catch-all routes last\n if (aCatchAll && !bCatchAll) return 1\n if (!aCatchAll && bCatchAll) return -1\n return a.routePath.localeCompare(b.routePath)\n })\n\n return results\n}\n","/**\n * Detect which HTTP-method named exports a route file declares.\n *\n * Uses the TypeScript compiler API (not regex) per G1 edge-case review EC-4:\n * regex over file content emits false positives for `// export const GET = ...`\n * in comments and `` `export const GET = ...` `` in template literals. AST\n * walking avoids both classes of bug.\n *\n * `typescript` ships as CommonJS with internal dynamic `require('fs')`.\n * When loaded via ESM `import`, the dynamic requires fail at module-bootstrap.\n * We use `createRequire(import.meta.url)` to keep the package on its native\n * CJS path. The type-only namespace import gives us the AST helpers shape.\n *\n * Returns the set of HTTP methods (uppercase) the file exports. Empty array\n * means the file has no HTTP exports (the route file is util-only).\n */\n\nimport { readFileSync } from 'node:fs'\nimport { createRequire } from 'node:module'\n\nimport type * as TS from 'typescript'\n\nimport { HTTP_METHODS, type HttpMethod } from '../../core/contracts/http-methods.js'\n\nconst require_ = createRequire(import.meta.url)\n// eslint-disable-next-line @typescript-eslint/no-require-imports\nconst ts = require_('typescript') as typeof TS\n\nconst HTTP_METHOD_NAMES = new Set<string>(HTTP_METHODS)\n\nfunction hasExportModifier(modifiers: readonly TS.Modifier[] | undefined): boolean {\n if (!modifiers) return false\n for (const m of modifiers) {\n if (m.kind === ts.SyntaxKind.ExportKeyword) return true\n }\n return false\n}\n\nfunction collectFromStatement(stmt: TS.Statement, found: Set<HttpMethod>): void {\n // `export const GET = ...` / `export function GET ...` / `export async function GET ...`\n if (ts.isVariableStatement(stmt) && hasExportModifier(ts.getModifiers(stmt))) {\n for (const decl of stmt.declarationList.declarations) {\n if (ts.isIdentifier(decl.name) && HTTP_METHOD_NAMES.has(decl.name.text)) {\n found.add(decl.name.text as HttpMethod)\n }\n }\n return\n }\n\n if (\n (ts.isFunctionDeclaration(stmt) || ts.isClassDeclaration(stmt)) &&\n hasExportModifier(ts.getModifiers(stmt))\n ) {\n if (stmt.name && HTTP_METHOD_NAMES.has(stmt.name.text)) {\n found.add(stmt.name.text as HttpMethod)\n }\n return\n }\n\n // `export { GET }` / `export { handler as GET } from './shared'` (EC-5)\n if (ts.isExportDeclaration(stmt) && stmt.exportClause && ts.isNamedExports(stmt.exportClause)) {\n for (const spec of stmt.exportClause.elements) {\n // spec.name is the exported (re-)name; spec.propertyName is the original (when renamed)\n if (HTTP_METHOD_NAMES.has(spec.name.text)) {\n found.add(spec.name.text as HttpMethod)\n }\n }\n }\n}\n\nexport function detectExportedHttpMethods(filePath: string, content?: string): HttpMethod[] {\n const src = content ?? readFileSync(filePath, 'utf-8')\n const sourceFile = ts.createSourceFile(\n filePath,\n src,\n ts.ScriptTarget.Latest,\n /* setParentNodes */ false,\n ts.ScriptKind.TS,\n )\n const found = new Set<HttpMethod>()\n for (const stmt of sourceFile.statements) {\n collectFromStatement(stmt, found)\n }\n return [...found].sort()\n}\n","/**\n * Router-convention errors thrown by the server-route scanner.\n *\n * Plan: .claude/knowledge-base/plans/g6-router-convention-plan.md v1.1\n *\n * theokit 0.4.0+ enforces directory-nested file-system routing\n * (`auth/[provider]/login.ts`) and REJECTS dotted-basename routes\n * (`auth.[provider].login.ts`) because the legacy regex extracted the\n * dotted basename incorrectly — `params.provider` was undefined at request\n * time. See ADR-XXX (router-convention-decision) in CHANGELOG 0.4.0.\n */\n\n/**\n * Canonical migration guide URL. T4.2 establishes this as the authoritative\n * landing page. EC-3: error message uses this constant so the URL never\n * drifts from the doc location.\n */\nexport const ROUTER_MIGRATION_GUIDE_URL = 'https://theokit.dev/migration/0.3-to-0.4-router'\n\nexport interface RouterConventionErrorOptions {\n /** Absolute path of the offending route file. */\n file: string\n /** Suggested directory-nested replacement path (relative, e.g. `routes/auth/[provider]/login.ts`). */\n suggestion: string\n /** Migration guide URL (defaults to `ROUTER_MIGRATION_GUIDE_URL`). */\n migrationUrl?: string\n}\n\n/**\n * Thrown by `scanServerRoutes` when a route file uses the legacy\n * dotted-basename convention (`auth.[provider].login.ts`).\n *\n * The error is FAIL-FAST by design — running with a route that has wrong\n * `paramNames` produces silent 404s at request time, which is strictly\n * worse than a build-time error.\n */\nexport class RouterConventionError extends Error {\n override readonly name = 'RouterConventionError'\n readonly file: string\n readonly suggestion: string\n readonly migrationUrl: string\n\n constructor(opts: RouterConventionErrorOptions) {\n const migrationUrl = opts.migrationUrl ?? ROUTER_MIGRATION_GUIDE_URL\n const message = [\n `Router convention violation: dotted route basename is not supported in theokit 0.4+.`,\n ``,\n ` File: ${opts.file}`,\n ` Use directory-nested form: ${opts.suggestion}`,\n ``,\n `Migration guide: ${migrationUrl}`,\n `Run \\`theokit migrate router\\` to convert all dotted basenames automatically.`,\n ].join('\\n')\n super(message)\n this.file = opts.file\n this.suggestion = opts.suggestion\n this.migrationUrl = migrationUrl\n }\n}\n","/* eslint-disable security/detect-non-literal-fs-filename --\n * Build-time scanner: walks `serverDir/ws/` derived from cwd.\n * No HTTP input ever reaches these fs calls.\n */\nimport { existsSync, statSync } from 'node:fs'\nimport { extname, join, relative } from 'node:path'\n\nimport { walkSourceFiles } from '../_internal/scan-walker.js'\n\nconst WS_EXTENSIONS = new Set(['.ts', '.tsx', '.js', '.jsx'])\n\nexport interface WebSocketRouteNode {\n filePath: string\n wsPath: string\n}\n\nexport function scanWebSocketRoutes(serverDir: string): WebSocketRouteNode[] {\n const wsDir = join(serverDir, 'ws')\n if (!existsSync(wsDir) || !statSync(wsDir).isDirectory()) {\n return []\n }\n\n const results: WebSocketRouteNode[] = []\n walkSourceFiles(wsDir, { extensions: WS_EXTENSIONS }, (absPath) => {\n let rel = relative(wsDir, absPath)\n rel = rel.replace(/\\\\/g, '/')\n rel = rel.slice(0, -extname(rel).length)\n if (rel.endsWith('/index')) rel = rel.slice(0, -6)\n else if (rel === 'index') rel = ''\n results.push({\n filePath: absPath,\n wsPath: `/ws/${rel}`,\n })\n })\n return results\n}\n"],"mappings":";;;;;;;AAIA,SAAS,YAAY,cAAc,gBAAgB;AACnD,SAAS,SAAS,MAAM,gBAAgB;AAqCjC,IAAM,kBAAN,cAA8B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YACE,MACA,SACA,kBACA;AACA,UAAM,OAAO;AAIb,SAAK,OAAO;AACZ,SAAK,OAAO;AACZ,SAAK,mBAAmB;AAAA,EAC1B;AACF;AAEA,IAAM,oBAAoB,oBAAI,IAAI,CAAC,OAAO,QAAQ,OAAO,MAAM,CAAC;AAChE,IAAM,eAAe;AACrB,IAAM,iBAAiB,oBAAI,IAAI,CAAC,SAAS,eAAe,aAAa,aAAa,gBAAgB,CAAC;AAO5F,SAAS,kBAAkB,WAAiC;AACjE,QAAM,aAAa,KAAK,WAAW,SAAS;AAC5C,MAAI,CAAC,WAAW,UAAU,KAAK,CAAC,SAAS,UAAU,EAAE,YAAY,GAAG;AAClE,WAAO,CAAC;AAAA,EACV;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,EACV;AAEA,QAAM,UAAgC,CAAC;AACvC,kBAAgB,OAAO,EAAE,YAAY,cAAc,GAAG,CAAC,YAAY;AACjE,QAAI,MAAMC,UAAS,OAAO,OAAO;AACjC,UAAM,IAAI,QAAQ,OAAO,GAAG;AAC5B,UAAM,IAAI,MAAM,GAAG,CAACC,SAAQ,GAAG,EAAE,MAAM;AACvC,QAAI,IAAI,SAAS,QAAQ,EAAG,OAAM,IAAI,MAAM,GAAG,EAAE;AAAA,aACxC,QAAQ,QAAS,OAAM;AAChC,YAAQ,KAAK;AAAA,MACX,UAAU;AAAA,MACV,QAAQ,OAAO,GAAG;AAAA,IACpB,CAAC;AAAA,EACH,CAAC;AACD,SAAO;AACT;","names":["basename","existsSync","statSync","extname","join","relative","readFileSync","readFileSync","relative","extname","join","existsSync","statSync","existsSync","statSync","extname","join","relative","join","existsSync","statSync","relative","extname"]}
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env node
2
+ import "tsx/esm";
3
+
4
+ // src/config/load-env.ts
5
+ import { lstatSync, readFileSync, realpathSync, statSync } from "fs";
6
+ import { resolve } from "path";
7
+ import dotenv from "dotenv";
8
+ import { expand } from "dotenv-expand";
9
+ var MAX_ENV_FILE_BYTES = 1048576;
10
+ var cache = /* @__PURE__ */ new Map();
11
+ function envFilesInPriorityOrder(mode) {
12
+ const files = [`.env.${mode}.local`, `.env.local`, `.env.${mode}`, `.env`];
13
+ if (mode === "test") {
14
+ return files.filter((f) => f !== ".env.local");
15
+ }
16
+ return files;
17
+ }
18
+ function readEnvFile(path) {
19
+ let stat;
20
+ try {
21
+ stat = statSync(path);
22
+ } catch {
23
+ return null;
24
+ }
25
+ if (!stat.isFile() && !stat.isFIFO()) return null;
26
+ if (stat.size > MAX_ENV_FILE_BYTES) {
27
+ console.warn(
28
+ `[theokit] .env file at ${path} exceeds ${MAX_ENV_FILE_BYTES} bytes \u2014 skipping (likely a generated artifact, not a real env file)`
29
+ );
30
+ return null;
31
+ }
32
+ try {
33
+ const lstat = lstatSync(path);
34
+ if (lstat.isSymbolicLink()) {
35
+ const real = realpathSync(path);
36
+ console.info(`[theokit] .env at ${path} is a symlink \u2192 ${real}`);
37
+ }
38
+ } catch {
39
+ }
40
+ try {
41
+ const content = readFileSync(path, "utf-8");
42
+ return dotenv.parse(content);
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+ function loadEnv(options = {}) {
48
+ const cwd = options.cwd ?? process.cwd();
49
+ const mode = options.mode ?? process.env.NODE_ENV ?? "development";
50
+ const cacheKey = `${cwd}:${mode}`;
51
+ if (!options.forceReload) {
52
+ const cached = cache.get(cacheKey);
53
+ if (cached) return cached;
54
+ }
55
+ const fileNames = envFilesInPriorityOrder(mode);
56
+ const filePaths = fileNames.map((f) => resolve(cwd, f));
57
+ const merged = {};
58
+ const loadedFromFiles = [];
59
+ for (const path of [...filePaths].reverse()) {
60
+ const parsed = readEnvFile(path);
61
+ if (parsed === null) continue;
62
+ for (const [k, v] of Object.entries(parsed)) {
63
+ merged[k] = v;
64
+ }
65
+ loadedFromFiles.unshift(path);
66
+ }
67
+ if (merged.NODE_ENV && process.env.__THEOKIT_USER_NODE_ENV === void 0) {
68
+ process.env.__THEOKIT_USER_NODE_ENV = merged.NODE_ENV;
69
+ }
70
+ delete merged.NODE_ENV;
71
+ const processEnvClone = {};
72
+ for (const [k, v] of Object.entries(process.env)) {
73
+ if (typeof v === "string") processEnvClone[k] = v;
74
+ }
75
+ try {
76
+ expand({ parsed: merged, processEnv: processEnvClone });
77
+ } catch (err) {
78
+ if (err instanceof RangeError) {
79
+ console.warn(
80
+ `[theokit] .env expansion overflow (likely circular reference like A=\${B}, B=\${A}). Leaving values as literals.`
81
+ );
82
+ } else {
83
+ throw err;
84
+ }
85
+ }
86
+ const loaded = {};
87
+ for (const [k, v] of Object.entries(merged)) {
88
+ if (process.env[k] !== void 0) continue;
89
+ process.env[k] = v;
90
+ loaded[k] = v;
91
+ }
92
+ process.env.__THEOKIT_PROCESSED_ENV = "true";
93
+ const result = { loaded, loadedFromFiles };
94
+ cache.set(cacheKey, result);
95
+ return result;
96
+ }
97
+
98
+ export {
99
+ loadEnv
100
+ };
101
+ //# sourceMappingURL=chunk-GDN3PXFH.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/config/load-env.ts"],"sourcesContent":["/* eslint-disable security/detect-non-literal-fs-filename --\n * Reads `.env` files from a CLI-controlled `cwd`. File names are a fixed\n * literal set. Build-time + CLI tool; no HTTP input.\n */\n/**\n * `loadEnv()` — auto-loads `.env` files into `process.env` for server code.\n *\n * Implements the Next.js `loadEnvConfig` algorithm\n * (`referencias/next.js/packages/next-env/index.ts:114-180`) with TheoKit\n * adaptations:\n *\n * - EC-1: 1MB file-size cap (anti-OOM, anti-supply-chain).\n * - EC-2: `_resetEnvCache()` test-only side-door.\n * - EC-8: `dotenv-expand` circular-ref safe (lib-provided).\n * - EC-13: log when `.env` is a symlink (transparency).\n * - D6: real `process.env` wins over `.env`-file values.\n * - NODE_ENV stash: `.env`-set NODE_ENV NEVER overrides the real\n * `process.env.NODE_ENV`. Stashed in `__THEOKIT_USER_NODE_ENV` instead.\n */\n\nimport { lstatSync, readFileSync, realpathSync, statSync } from 'node:fs'\nimport { resolve } from 'node:path'\n\nimport dotenv from 'dotenv'\nimport { expand, type DotenvPopulateInput } from 'dotenv-expand'\n\nimport type { LoadEnvOptions, LoadEnvResult } from './load-env-types.js'\n\n/** EC-1: 1MB cap. Real `.env`s are < 10KB. */\nconst MAX_ENV_FILE_BYTES = 1_048_576\n\nconst cache = new Map<string, LoadEnvResult>()\n\n/** Test-only side-door for EC-2 — clear the module-level cache between vitest test files. */\nexport function _resetEnvCache(): void {\n cache.clear()\n}\n\nfunction envFilesInPriorityOrder(mode: string): string[] {\n // Top of list = highest priority. Will be read in REVERSE so first-in\n // wins via overwrite during merge.\n const files = [`.env.${mode}.local`, `.env.local`, `.env.${mode}`, `.env`]\n // Test mode skips `.env.local` per dotenv convention (avoid leaking dev\n // secrets into the test suite).\n if (mode === 'test') {\n return files.filter((f) => f !== '.env.local')\n }\n return files\n}\n\nfunction readEnvFile(path: string): Record<string, string> | null {\n let stat\n try {\n stat = statSync(path)\n } catch {\n return null // ENOENT / EACCES → skip\n }\n\n // Allow files + FIFOs (1Password/SOPS pipe support).\n if (!stat.isFile() && !stat.isFIFO()) return null\n\n // EC-1 — anti-OOM cap.\n if (stat.size > MAX_ENV_FILE_BYTES) {\n console.warn(\n `[theokit] .env file at ${path} exceeds ${MAX_ENV_FILE_BYTES} bytes — skipping (likely a generated artifact, not a real env file)`,\n )\n return null\n }\n\n // EC-13 — symlink transparency. Don't refuse; just log.\n try {\n const lstat = lstatSync(path)\n if (lstat.isSymbolicLink()) {\n const real = realpathSync(path)\n // eslint-disable-next-line no-console -- intentional transparency log on a build-time tool\n console.info(`[theokit] .env at ${path} is a symlink → ${real}`)\n }\n } catch {\n // lstat failure is non-fatal — fall through to read.\n }\n\n try {\n const content = readFileSync(path, 'utf-8')\n return dotenv.parse(content)\n } catch {\n return null\n }\n}\n\n// eslint-disable-next-line complexity -- canonical env-load algorithm (priority → expand → guards → apply); inlining branches is clearer than extracting micro-helpers\nexport function loadEnv(options: LoadEnvOptions = {}): LoadEnvResult {\n const cwd = options.cwd ?? process.cwd()\n const mode = options.mode ?? process.env.NODE_ENV ?? 'development'\n const cacheKey = `${cwd}:${mode}`\n\n if (!options.forceReload) {\n const cached = cache.get(cacheKey)\n if (cached) return cached\n }\n\n const fileNames = envFilesInPriorityOrder(mode)\n const filePaths = fileNames.map((f) => resolve(cwd, f))\n\n // Read in REVERSE — lower-priority files first, so higher-priority\n // (lower-index) overwrites during merge.\n const merged: Record<string, string> = {}\n const loadedFromFiles: string[] = []\n for (const path of [...filePaths].reverse()) {\n const parsed = readEnvFile(path)\n if (parsed === null) continue\n for (const [k, v] of Object.entries(parsed)) {\n merged[k] = v\n }\n loadedFromFiles.unshift(path) // keep priority order in result\n }\n\n // NODE_ENV stash — `.env`-set NODE_ENV never overrides real process.env.NODE_ENV.\n if (merged.NODE_ENV && process.env.__THEOKIT_USER_NODE_ENV === undefined) {\n process.env.__THEOKIT_USER_NODE_ENV = merged.NODE_ENV\n }\n delete merged.NODE_ENV // never propagate\n\n // EC-8 — dotenv-expand@11 stack-overflows on circular refs (A=${B}, B=${A}).\n // Wrap in try/catch — on overflow, leave the parsed map untouched (literal\n // values survive). Tested via test_loadEnv_circular_expand_no_loop_EC8.\n // Pass a CLONE of process.env (string-only, filtered) so expand can\n // reference real env without mutating it.\n const processEnvClone: DotenvPopulateInput = {}\n for (const [k, v] of Object.entries(process.env)) {\n if (typeof v === 'string') processEnvClone[k] = v\n }\n try {\n expand({ parsed: merged, processEnv: processEnvClone })\n } catch (err) {\n if (err instanceof RangeError) {\n console.warn(\n `[theokit] .env expansion overflow (likely circular reference like A=\\${B}, B=\\${A}). Leaving values as literals.`,\n )\n } else {\n throw err\n }\n }\n\n // Apply: D6 — real process.env wins over file values.\n const loaded: Record<string, string> = {}\n for (const [k, v] of Object.entries(merged)) {\n if (process.env[k] !== undefined) continue // real env wins\n process.env[k] = v\n loaded[k] = v\n }\n\n // Sentinel\n process.env.__THEOKIT_PROCESSED_ENV = 'true'\n\n const result: LoadEnvResult = { loaded, loadedFromFiles }\n cache.set(cacheKey, result)\n return result\n}\n"],"mappings":";;;;AAoBA,SAAS,WAAW,cAAc,cAAc,gBAAgB;AAChE,SAAS,eAAe;AAExB,OAAO,YAAY;AACnB,SAAS,cAAwC;AAKjD,IAAM,qBAAqB;AAE3B,IAAM,QAAQ,oBAAI,IAA2B;AAO7C,SAAS,wBAAwB,MAAwB;AAGvD,QAAM,QAAQ,CAAC,QAAQ,IAAI,UAAU,cAAc,QAAQ,IAAI,IAAI,MAAM;AAGzE,MAAI,SAAS,QAAQ;AACnB,WAAO,MAAM,OAAO,CAAC,MAAM,MAAM,YAAY;AAAA,EAC/C;AACA,SAAO;AACT;AAEA,SAAS,YAAY,MAA6C;AAChE,MAAI;AACJ,MAAI;AACF,W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,QAAQ,IAAI;AAAA,EAC9B;AAGA,MAAI,OAAO,YAAY,QAAQ,IAAI,4BAA4B,QAAW;AACxE,YAAQ,IAAI,0BAA0B,OAAO;AAAA,EAC/C;AACA,SAAO,OAAO;AAOd,QAAM,kBAAuC,CAAC;AAC9C,aAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,QAAQ,GAAG,GAAG;AAChD,QAAI,OAAO,MAAM,SAAU,iBAAgB,CAAC,IAAI;AAAA,EAClD;AACA,MAAI;AACF,WAAO,EAAE,QAAQ,QAAQ,YAAY,gBAAgB,CAAC;AAAA,EACxD,SAAS,KAAK;AACZ,QAAI,eAAe,YAAY;AAC7B,cAAQ;AAAA,QACN;AAAA,MACF;AAAA,IACF,OAAO;AACL,YAAM;AAAA,IACR;AAAA,EACF;AAGA,QAAM,SAAiC,CAAC;AACxC,aAAW,CAAC,GAAG,CAAC,KAAK,OAAO,QAAQ,MAAM,GAAG;AAC3C,QAAI,QAAQ,IAAI,CAAC,MAAM,OAAW;AAClC,YAAQ,IAAI,CAAC,IAAI;AACjB,WAAO,CAAC,IAAI;AAAA,EACd;AAGA,UAAQ,IAAI,0BAA0B;AAEtC,QAAM,SAAwB,EAAE,QAAQ,gBAAgB;AACxD,QAAM,IAAI,UAAU,MAAM;AAC1B,SAAO;AACT;","names":[]}
@@ -0,0 +1,81 @@
1
+ #!/usr/bin/env node
2
+ import "tsx/esm";
3
+
4
+ // src/server/agent/provider-resolver.ts
5
+ var DEFAULT_REGISTRY = [
6
+ {
7
+ name: "openrouter",
8
+ envKey: "OPENROUTER_API_KEY",
9
+ baseUrl: "https://openrouter.ai/api/v1",
10
+ priority: 1
11
+ },
12
+ {
13
+ name: "openai",
14
+ envKey: "OPENAI_API_KEY",
15
+ baseUrl: "https://api.openai.com/v1",
16
+ priority: 2
17
+ },
18
+ {
19
+ name: "anthropic",
20
+ envKey: "ANTHROPIC_API_KEY",
21
+ baseUrl: "https://api.anthropic.com",
22
+ priority: 3
23
+ }
24
+ ];
25
+ var registry = [...DEFAULT_REGISTRY];
26
+ function resolveProvider() {
27
+ const sorted = [...registry].sort((a, b) => a.priority - b.priority);
28
+ for (const desc of sorted) {
29
+ const apiKey = process.env[desc.envKey];
30
+ if (apiKey && apiKey.length > 0) {
31
+ return {
32
+ name: desc.name,
33
+ apiKey,
34
+ baseUrl: desc.baseUrl
35
+ };
36
+ }
37
+ }
38
+ const envKeys = sorted.map((p) => p.envKey).join(" OR ");
39
+ throw new Error(
40
+ `No LLM provider API key found in environment. Set one of: ${envKeys}. Get a free OpenRouter key at https://openrouter.ai/keys (recommended \u2014 one key, many models).`
41
+ );
42
+ }
43
+
44
+ // src/server/agent/approval-registry.ts
45
+ var serverRegistry;
46
+ function getApprovalRegistry() {
47
+ serverRegistry ??= createInProcessApprovalRegistry();
48
+ return serverRegistry;
49
+ }
50
+ function createInProcessApprovalRegistry() {
51
+ const pending = /* @__PURE__ */ new Map();
52
+ return {
53
+ register(approvalId, opts) {
54
+ return new Promise((resolve) => {
55
+ const settle = (approved) => {
56
+ const entry = pending.get(approvalId);
57
+ if (!entry) return;
58
+ clearTimeout(entry.timer);
59
+ pending.delete(approvalId);
60
+ resolve(approved);
61
+ };
62
+ const timer = setTimeout(() => {
63
+ settle(opts.onTimeout === "proceed");
64
+ }, opts.timeoutMs);
65
+ pending.set(approvalId, { settle, timer });
66
+ });
67
+ },
68
+ resolve(approvalId, approved) {
69
+ const entry = pending.get(approvalId);
70
+ if (!entry) return false;
71
+ entry.settle(approved);
72
+ return true;
73
+ }
74
+ };
75
+ }
76
+
77
+ export {
78
+ getApprovalRegistry,
79
+ resolveProvider
80
+ };
81
+ //# sourceMappingURL=chunk-NBWB4S46.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/server/agent/provider-resolver.ts","../src/server/agent/approval-registry.ts"],"sourcesContent":["/**\n * Provider Resolver — Strategy + Registry pattern (FAANG-grade).\n *\n * Inspiração: Dapr Conversation Registry (`dapr/pkg/components/conversation/registry.go`)\n * + Encore Manager provider array (`encore/runtimes/go/pubsub/manager_internal.go`).\n *\n * Princípio: provider routing é responsabilidade do FRAMEWORK, não do consumer.\n * Consumer template usa `model: { id: 'gpt-4o-mini' }` puro — sem conditionals.\n *\n * Wire protocol: OpenAI Chat Completions (universal — implementado por todos\n * os providers: OpenRouter, Groq, Mistral, Together, Anthropic via proxy, etc).\n *\n * Resolução por prioridade (FIRST match wins):\n * 1. OPENROUTER_API_KEY → baseUrl=openrouter.ai (gateway multi-modelo)\n * 2. OPENAI_API_KEY → baseUrl=api.openai.com\n * 3. ANTHROPIC_API_KEY → direct Anthropic (Messages API, não OpenAI-compat)\n *\n * Escape hatch: `options.apiKey` explícito SOBREPÕE auto-resolution\n * (consumer pode forçar provider específico se quiser).\n */\n\n/**\n * Provider configuration descriptor — Registry entry shape.\n *\n * @public\n */\nexport interface ProviderDescriptor {\n /** Stable name used internally — não exposto no wire. */\n name: string\n /** Environment variable that holds the API key for this provider. */\n envKey: string\n /** Base URL for the provider's OpenAI-compatible (or native) API. */\n baseUrl: string\n /** Resolution priority (lower = higher priority). FIRST match wins. */\n priority: number\n}\n\n/**\n * Resolved provider configuration — output of `resolveProvider()`.\n *\n * @public\n */\nexport interface ResolvedProvider {\n name: string\n apiKey: string\n baseUrl: string\n}\n\n/**\n * Default provider registry. Order = priority (first = highest).\n *\n * Adding a new provider:\n * 1. Append entry below (or register via `registerProvider()`).\n * 2. Set `envKey` matching the user's env var convention.\n * 3. Set `baseUrl` to the OpenAI-compat endpoint (or native if not compat).\n * 4. Provider name used in telemetry/logs only — never wire-exposed.\n */\nconst DEFAULT_REGISTRY: ProviderDescriptor[] = [\n {\n name: 'openrouter',\n envKey: 'OPENROUTER_API_KEY',\n baseUrl: 'https://openrouter.ai/api/v1',\n priority: 1,\n },\n {\n name: 'openai',\n envKey: 'OPENAI_API_KEY',\n baseUrl: 'https://api.openai.com/v1',\n priority: 2,\n },\n {\n name: 'anthropic',\n envKey: 'ANTHROPIC_API_KEY',\n baseUrl: 'https://api.anthropic.com',\n priority: 3,\n },\n]\n\n/**\n * Runtime registry — copy of DEFAULT_REGISTRY mutable via registerProvider().\n * Sorted by priority on every resolve (stable, O(n log n) — n <= ~10 providers).\n */\nconst registry: ProviderDescriptor[] = [...DEFAULT_REGISTRY]\n\n/**\n * Register a new provider (Registry pattern — runtime extension point).\n * Useful for self-hosted endpoints or custom providers without touching theokit src.\n *\n * @example\n * registerProvider({\n * name: 'self-hosted',\n * envKey: 'SELF_HOSTED_API_KEY',\n * baseUrl: 'https://llm.internal.acme.com/v1',\n * priority: 0, // highest priority\n * })\n *\n * @public\n */\nexport function registerProvider(descriptor: ProviderDescriptor): void {\n // Idempotent — replace existing by name.\n const idx = registry.findIndex((p) => p.name === descriptor.name)\n if (idx >= 0) registry[idx] = descriptor\n else registry.push(descriptor)\n}\n\n/**\n * Reset registry to DEFAULT_REGISTRY (test-only / dev escape hatch).\n *\n * @public\n */\nexport function resetProviderRegistry(): void {\n registry.length = 0\n registry.push(...DEFAULT_REGISTRY)\n}\n\n/**\n * Get current registry snapshot (read-only — inspection).\n *\n * @public\n */\nexport function listProviders(): readonly ProviderDescriptor[] {\n return [...registry].sort((a, b) => a.priority - b.priority)\n}\n\n/**\n * Resolve provider from env vars by priority. FIRST env var found wins.\n *\n * @returns ResolvedProvider with apiKey + baseUrl + name\n * @throws Error if NO provider env var is set (actionable message)\n *\n * @public\n */\nexport function resolveProvider(): ResolvedProvider {\n const sorted = [...registry].sort((a, b) => a.priority - b.priority)\n for (const desc of sorted) {\n const apiKey = process.env[desc.envKey]\n if (apiKey && apiKey.length > 0) {\n return {\n name: desc.name,\n apiKey,\n baseUrl: desc.baseUrl,\n }\n }\n }\n // No env var found — emit actionable error.\n const envKeys = sorted.map((p) => p.envKey).join(' OR ')\n throw new Error(\n `No LLM provider API key found in environment. Set one of: ${envKeys}. ` +\n `Get a free OpenRouter key at https://openrouter.ai/keys (recommended — one key, many models).`,\n )\n}\n\n/**\n * Try to resolve — does NOT throw. Returns null if no provider available.\n * Useful for graceful degradation (e.g., mock mode).\n *\n * @public\n */\nexport function tryResolveProvider(): ResolvedProvider | null {\n try {\n return resolveProvider()\n } catch {\n return null\n }\n}\n","/**\n * M4 (theokit-ai-first) — the in-process HITL approval registry.\n *\n * The HITL plugin's `awaitApproval` calls `register(approvalId)` and awaits the returned Promise\n * (this is what genuinely PAUSES the SDK run — the SDK `pre_tool_call` hook is awaited). The\n * approve route calls `resolve(approvalId, approved)` to settle it. A per-approval timeout settles\n * the Promise deterministically per the `@HumanInTheLoop` `onTimeout` policy so a hung approval\n * never leaks the paused stream.\n *\n * Single-process contract (ADR 0038 / plan Drawback 2): a multi-instance deploy needs a shared\n * registry — the interface is injectable so a durable impl (Redis, etc.) slots in without touching\n * the harness. We do NOT build a durable store now (YAGNI).\n */\n// The timeout policy vocabulary is owned by the `@HumanInTheLoop` decorator (DRY / G12) — reuse it\n// rather than re-declaring the union, so the two can never drift.\nimport type { TimeoutAction } from '@theokit/agents'\n\nexport type { TimeoutAction }\n\nexport interface RegisterOptions {\n /** Milliseconds before the approval auto-settles per `onTimeout`. */\n timeoutMs: number\n /**\n * What a timeout means. Only `'proceed'` auto-approves; `'abort'` and `'retry'` both deny — the\n * registry does NOT implement retry semantics (a timed-out `'retry'` is a deny, not a re-prompt).\n */\n onTimeout: TimeoutAction\n}\n\nexport interface ApprovalRegistry {\n /** Register a pending approval; the returned Promise settles on `resolve` or timeout. */\n register(approvalId: string, opts: RegisterOptions): Promise<boolean>\n /** Settle a pending approval. Returns false if the id is unknown or already settled. */\n resolve(approvalId: string, approved: boolean): boolean\n}\n\ninterface Pending {\n settle: (approved: boolean) => void\n timer: ReturnType<typeof setTimeout>\n}\n\n/**\n * The one process-wide registry the stream mount (`mountAgent`) and the approve route share.\n *\n * The in-process impl holds LIVE Promise resolvers in memory — the approval a request awaits and\n * the approval the route resolves MUST be the same object, so a single instance per process is not\n * a convenience but a correctness requirement. Lazily created; a durable/multi-instance deploy\n * swaps this accessor for a shared-store impl (ADR 0038 / plan Drawback 2) without touching callers.\n * Tests use {@link createInProcessApprovalRegistry} directly — never this singleton.\n */\nlet serverRegistry: ApprovalRegistry | undefined\nexport function getApprovalRegistry(): ApprovalRegistry {\n serverRegistry ??= createInProcessApprovalRegistry()\n return serverRegistry\n}\n\nexport function createInProcessApprovalRegistry(): ApprovalRegistry {\n const pending = new Map<string, Pending>()\n\n return {\n register(approvalId, opts) {\n return new Promise<boolean>((resolve) => {\n const settle = (approved: boolean): void => {\n const entry = pending.get(approvalId)\n if (!entry) return\n clearTimeout(entry.timer)\n pending.delete(approvalId)\n resolve(approved)\n }\n // 'proceed' → allow on timeout; 'abort'/'retry' → deny on timeout.\n const timer = setTimeout(() => {\n settle(opts.onTimeout === 'proceed')\n }, opts.timeoutMs)\n pending.set(approvalId, { settle, timer })\n })\n },\n resolve(approvalId, approved) {\n const entry = pending.get(approvalId)\n if (!entry) return false\n entry.settle(approved)\n return true\n },\n }\n}\n"],"mappings":";;;;AAyDA,IAAM,mBAAyC;AAAA,EAC7C;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,UAAU;AAAA,EACZ;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,UAAU;AAAA,EACZ;AAAA,EACA;AAAA,IACE,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,SAAS;AAAA,IACT,UAAU;AAAA,EACZ;AACF;AAMA,IAAM,WAAiC,CAAC,GAAG,gBAAgB;AAkDpD,SAAS,kBAAoC;AAClD,QAAM,SAAS,CAAC,GAAG,QAAQ,EAAE,KAAK,CAAC,GAAG,MAAM,EAAE,WAAW,EAAE,QAAQ;AACnE,aAAW,QAAQ,QAAQ;AACzB,UAAM,SAAS,QAAQ,IAAI,KAAK,MAAM;AACtC,QAAI,UAAU,OAAO,SAAS,GAAG;AAC/B,aAAO;AAAA,QACL,MAAM,KAAK;AAAA,QACX;AAAA,QACA,SAAS,KAAK;AAAA,MAChB;AAAA,IACF;AAAA,EACF;AAEA,QAAM,UAAU,OAAO,IAAI,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,MAAM;AACvD,QAAM,IAAI;AAAA,IACR,6DAA6D,OAAO;AAAA,EAEtE;AACF;;;ACpGA,IAAI;AACG,SAAS,sBAAwC;AACtD,qBAAmB,gCAAgC;AACnD,SAAO;AACT;AAEO,SAAS,kCAAoD;AAClE,QAAM,UAAU,oBAAI,IAAqB;AAEzC,SAAO;AAAA,IACL,SAAS,YAAY,MAAM;AACzB,aAAO,IAAI,QAAiB,CAAC,YAAY;AACvC,cAAM,SAAS,CAAC,aAA4B;AAC1C,gBAAM,QAAQ,QAAQ,IAAI,UAAU;AACpC,cAAI,CAAC,MAAO;AACZ,uBAAa,MAAM,KAAK;AACxB,kBAAQ,OAAO,UAAU;AACzB,kBAAQ,QAAQ;AAAA,QAClB;AAEA,cAAM,QAAQ,WAAW,MAAM;AAC7B,iBAAO,KAAK,cAAc,SAAS;AAAA,QACrC,GAAG,KAAK,SAAS;AACjB,gBAAQ,IAAI,YAAY,EAAE,QAAQ,MAAM,CAAC;AAAA,MAC3C,CAAC;AAAA,IACH;AAAA,IACA,QAAQ,YAAY,UAAU;AAC5B,YAAM,QAAQ,QAAQ,IAAI,UAAU;AACpC,UAAI,CAAC,MAAO,QAAO;AACnB,YAAM,OAAO,QAAQ;AACrB,aAAO;AAAA,IACT;AAAA,EACF;AACF;","names":[]}
@@ -1,43 +1,18 @@
1
1
  #!/usr/bin/env node
2
2
  import "tsx/esm";
3
+ import {
4
+ scanAgents
5
+ } from "./chunk-34YQOXGM.js";
3
6
  import {
4
7
  compilePattern,
5
8
  scanServerActions,
6
9
  scanServerRoutes,
7
- scanWebSocketRoutes,
8
- walkSourceFiles
9
- } from "./chunk-BQDGES7C.js";
10
-
11
- // src/server/scan/agent-scan.ts
12
- import { existsSync, statSync } from "fs";
13
- import { extname, join, relative } from "path";
14
- var AGENT_EXTENSIONS = /* @__PURE__ */ new Set([".ts", ".tsx", ".js", ".jsx"]);
15
- var TEST_FILE = /\.(test|spec)$/;
16
- function scanAgents(projectRoot) {
17
- const agentsDir = join(projectRoot, "agents");
18
- if (!existsSync(agentsDir) || !statSync(agentsDir).isDirectory()) {
19
- return [];
20
- }
21
- const results = [];
22
- walkSourceFiles(agentsDir, { extensions: AGENT_EXTENSIONS }, (absPath) => {
23
- let rel = relative(agentsDir, absPath);
24
- rel = rel.replace(/\\/g, "/");
25
- rel = rel.slice(0, -extname(rel).length);
26
- if (TEST_FILE.test(rel)) return;
27
- if (rel.endsWith("/index")) rel = rel.slice(0, -6);
28
- if (rel === "index" || rel === "") return;
29
- results.push({
30
- filePath: absPath,
31
- agentPath: `/api/agents/${rel}`,
32
- name: rel
33
- });
34
- });
35
- return results;
36
- }
10
+ scanWebSocketRoutes
11
+ } from "./chunk-GBXLKYIA.js";
37
12
 
38
13
  // src/server/scan/manifest.ts
39
- import { existsSync as existsSync2, readFileSync, writeFileSync, mkdirSync } from "fs";
40
- import { join as join2, resolve, relative as relative2, dirname } from "path";
14
+ import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
15
+ import { join, resolve, relative, dirname } from "path";
41
16
  function generateManifest(serverDir, projectRoot = dirname(serverDir)) {
42
17
  const routes = scanServerRoutes(serverDir);
43
18
  const actions = scanServerActions(serverDir);
@@ -47,22 +22,22 @@ function generateManifest(serverDir, projectRoot = dirname(serverDir)) {
47
22
  version: 1,
48
23
  generatedAt: (/* @__PURE__ */ new Date()).toISOString(),
49
24
  routes: routes.map((r) => ({
50
- filePath: relative2(serverDir, r.filePath),
25
+ filePath: relative(serverDir, r.filePath),
51
26
  routePath: r.routePath,
52
27
  paramNames: r.paramNames,
53
28
  ...r.methods !== void 0 ? { methods: r.methods } : {}
54
29
  })),
55
30
  actions: actions.map((a) => ({
56
- filePath: relative2(serverDir, a.filePath),
31
+ filePath: relative(serverDir, a.filePath),
57
32
  actionPath: a.actionPath
58
33
  })),
59
34
  websockets: websockets.map((w) => ({
60
- filePath: relative2(serverDir, w.filePath),
35
+ filePath: relative(serverDir, w.filePath),
61
36
  wsPath: w.wsPath
62
37
  })),
63
38
  agents: agents.map((a) => ({
64
39
  // Relative to projectRoot (agents/ is outside serverDir).
65
- filePath: relative2(projectRoot, a.filePath),
40
+ filePath: relative(projectRoot, a.filePath),
66
41
  agentPath: a.agentPath,
67
42
  name: a.name
68
43
  }))
@@ -70,12 +45,12 @@ function generateManifest(serverDir, projectRoot = dirname(serverDir)) {
70
45
  }
71
46
  function writeManifest(manifest, outputDir) {
72
47
  mkdirSync(outputDir, { recursive: true });
73
- const manifestPath = join2(outputDir, "manifest.json");
48
+ const manifestPath = join(outputDir, "manifest.json");
74
49
  writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
75
50
  }
76
51
  function loadManifest(distDir, serverDir) {
77
- const manifestPath = join2(distDir, "manifest.json");
78
- if (!existsSync2(manifestPath)) {
52
+ const manifestPath = join(distDir, "manifest.json");
53
+ if (!existsSync(manifestPath)) {
79
54
  throw new Error(`No manifest found at ${manifestPath}. Run "theo build" first.`);
80
55
  }
81
56
  const raw = JSON.parse(readFileSync(manifestPath, "utf-8"));
@@ -107,9 +82,8 @@ function loadManifest(distDir, serverDir) {
107
82
  }
108
83
 
109
84
  export {
110
- scanAgents,
111
85
  generateManifest,
112
86
  writeManifest,
113
87
  loadManifest
114
88
  };
115
- //# sourceMappingURL=chunk-F4YUPDJ2.js.map
89
+ //# sourceMappingURL=chunk-NXTF5PPW.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/server/scan/manifest.ts"],"sourcesContent":["/* eslint-disable security/detect-non-literal-fs-filename --\n * Build-time manifest emitter / loader. All paths derived from `distDir`\n * + `serverDir`, themselves resolved from `process.cwd()`. No HTTP input.\n */\nimport { existsSync, readFileSync, writeFileSync, mkdirSync } from 'node:fs'\nimport { join, resolve, relative, dirname } from 'node:path'\n\nimport { scanServerActions } from './action-scan.js'\nimport type { ActionNode } from './action-scan.js'\nimport { scanAgents } from './agent-scan.js'\nimport type { AgentNode } from './agent-scan.js'\nimport { compilePattern } from './match.js'\nimport type { ServerRouteNode } from './match.js'\nimport { scanServerRoutes } from './scan.js'\nimport { scanWebSocketRoutes } from './ws-scan.js'\nimport type { WebSocketRouteNode } from './ws-scan.js'\n\n// --- Manifest Types ---\n\nexport interface ManifestRoute {\n filePath: string\n routePath: string\n paramNames: string[]\n /** HTTP methods (uppercase) the route file exports. Optional — manifests\n * generated before G1 omit this; loaders treat absence as \"unknown\". */\n methods?: string[]\n}\n\nexport interface ManifestAction {\n filePath: string\n actionPath: string\n}\n\nexport interface ManifestWebSocket {\n filePath: string\n wsPath: string\n}\n\n/** M2 — a top-level `agents/*.ts` convention entry. `filePath` is relative to the\n * project root (agents live OUTSIDE `serverDir`), unlike routes/actions/ws. */\nexport interface ManifestAgent {\n filePath: string\n agentPath: string\n name: string\n}\n\nexport interface TheoManifest {\n version: 1\n generatedAt: string\n routes: ManifestRoute[]\n actions: ManifestAction[]\n websockets: ManifestWebSocket[]\n /** M2 — optional for backward compat: manifests generated before M2 omit it. */\n agents?: ManifestAgent[]\n}\n\nexport interface LoadedManifest {\n routes: ServerRouteNode[]\n actions: ActionNode[]\n websockets: WebSocketRouteNode[]\n agents: AgentNode[]\n}\n\n// --- Generate ---\n\nexport function generateManifest(\n serverDir: string,\n // Agents live at `<projectRoot>/agents`, a sibling of `serverDir` (LOCKED naming).\n // Defaults to the server dir's parent; overridable for tests / non-standard layouts.\n projectRoot: string = dirname(serverDir),\n): TheoManifest {\n const routes = scanServerRoutes(serverDir)\n const actions = scanServerActions(serverDir)\n const websockets = scanWebSocketRoutes(serverDir)\n const agents = scanAgents(projectRoot)\n\n return {\n version: 1,\n generatedAt: new Date().toISOString(),\n routes: routes.map((r) => ({\n filePath: relative(serverDir, r.filePath),\n routePath: r.routePath,\n paramNames: r.paramNames,\n ...(r.methods !== undefined ? { methods: r.methods } : {}),\n })),\n actions: actions.map((a) => ({\n filePath: relative(serverDir, a.filePath),\n actionPath: a.actionPath,\n })),\n websockets: websockets.map((w) => ({\n filePath: relative(serverDir, w.filePath),\n wsPath: w.wsPath,\n })),\n agents: agents.map((a) => ({\n // Relative to projectRoot (agents/ is outside serverDir).\n filePath: relative(projectRoot, a.filePath),\n agentPath: a.agentPath,\n name: a.name,\n })),\n }\n}\n\n// --- Write ---\n\nexport function writeManifest(manifest: TheoManifest, outputDir: string): void {\n mkdirSync(outputDir, { recursive: true })\n const manifestPath = join(outputDir, 'manifest.json')\n writeFileSync(manifestPath, JSON.stringify(manifest, null, 2))\n}\n\n// --- Load ---\n\nexport function loadManifest(distDir: string, serverDir: string): LoadedManifest {\n const manifestPath = join(distDir, 'manifest.json')\n\n if (!existsSync(manifestPath)) {\n throw new Error(`No manifest found at ${manifestPath}. Run \"theo build\" first.`)\n }\n\n const raw = JSON.parse(readFileSync(manifestPath, 'utf-8')) as TheoManifest\n\n const routes: ServerRouteNode[] = raw.routes.map((r) => {\n const { pattern, paramNames } = compilePattern(r.routePath)\n return {\n filePath: resolve(serverDir, r.filePath),\n routePath: r.routePath,\n paramNames,\n pattern,\n ...(r.methods !== undefined ? { methods: r.methods } : {}),\n }\n })\n\n const actions: ActionNode[] = raw.actions.map((a) => ({\n filePath: resolve(serverDir, a.filePath),\n actionPath: a.actionPath,\n }))\n\n const websockets: WebSocketRouteNode[] = raw.websockets.map((w) => ({\n filePath: resolve(serverDir, w.filePath),\n wsPath: w.wsPath,\n }))\n\n // M2 — agents resolve relative to the project root (sibling of serverDir).\n // `?? []` keeps pre-M2 manifests (no `agents` field) loadable (fail-safe).\n const projectRoot = dirname(serverDir)\n const agents: AgentNode[] = (raw.agents ?? []).map((a) => ({\n filePath: resolve(projectRoot, a.filePath),\n agentPath: a.agentPath,\n name: a.name,\n }))\n\n return { routes, actions, websockets, agents }\n}\n"],"mappings":";;;;;;;;;;;;;AAIA,SAAS,YAAY,cAAc,eAAe,iBAAiB;A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