@v11g/agents-rock 1.0.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 (223) hide show
  1. package/README.md +65 -0
  2. package/bin/agents-rock.mjs +4 -0
  3. package/package.json +13 -0
  4. package/plugins/solution-architect/.claude-plugin/plugin.json +9 -0
  5. package/plugins/solution-architect/skills/analyze-requirements/README.md +75 -0
  6. package/plugins/solution-architect/skills/analyze-requirements/SKILL.md +31 -0
  7. package/plugins/solution-architect/skills/analyze-requirements/assets/fonts/OFL.txt +98 -0
  8. package/plugins/solution-architect/skills/analyze-requirements/assets/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
  9. package/plugins/solution-architect/skills/analyze-requirements/assets/fonts/ibm-plex-mono-latin-600-normal.woff2 +0 -0
  10. package/plugins/solution-architect/skills/analyze-requirements/assets/fonts/ibm-plex-sans-latin-400-normal.woff2 +0 -0
  11. package/plugins/solution-architect/skills/analyze-requirements/assets/fonts/ibm-plex-sans-latin-500-normal.woff2 +0 -0
  12. package/plugins/solution-architect/skills/analyze-requirements/assets/fonts/ibm-plex-sans-latin-600-normal.woff2 +0 -0
  13. package/plugins/solution-architect/skills/analyze-requirements/assets/mermaid-theme.json +52 -0
  14. package/plugins/solution-architect/skills/analyze-requirements/assets/section-help.json +101 -0
  15. package/plugins/solution-architect/skills/analyze-requirements/assets/viewer-template.html +2389 -0
  16. package/plugins/solution-architect/skills/analyze-requirements/references/interview.md +53 -0
  17. package/plugins/solution-architect/skills/analyze-requirements/references/likec4.md +157 -0
  18. package/plugins/solution-architect/skills/analyze-requirements/references/project-types.md +60 -0
  19. package/plugins/solution-architect/skills/analyze-requirements/references/research.md +78 -0
  20. package/plugins/solution-architect/skills/analyze-requirements/references/viewer.md +772 -0
  21. package/plugins/solution-architect/skills/analyze-requirements/references/writing.md +206 -0
  22. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/cdp.mjs +74 -0
  23. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/chrome.mjs +66 -0
  24. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/companion-links.mjs +20 -0
  25. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/doc-kinds.mjs +37 -0
  26. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/doc-links.mjs +26 -0
  27. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/doc-routes.mjs +27 -0
  28. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/doc-sections.mjs +78 -0
  29. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/embed.mjs +8 -0
  30. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/er-entities.mjs +16 -0
  31. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/fonts.mjs +39 -0
  32. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/frontmatter.mjs +17 -0
  33. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/inputs.mjs +41 -0
  34. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/likec4-config.mjs +41 -0
  35. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/likec4-extract.mjs +29 -0
  36. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/likec4-steps.mjs +22 -0
  37. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-defs.mjs +20 -0
  38. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-flows.mjs +46 -0
  39. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-inline.mjs +31 -0
  40. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-links.mjs +7 -0
  41. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-list.mjs +64 -0
  42. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-render.mjs +127 -0
  43. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-shape.mjs +19 -0
  44. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-tables.mjs +30 -0
  45. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-views.mjs +48 -0
  46. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/md-wide.mjs +44 -0
  47. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/nav.mjs +72 -0
  48. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/offline.mjs +19 -0
  49. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/port.mjs +13 -0
  50. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/quality-gate.mjs +113 -0
  51. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/section-help.mjs +49 -0
  52. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/slug-registry.mjs +11 -0
  53. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-clusters.mjs +15 -0
  54. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-deployment.mjs +8 -0
  55. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-elections.mjs +20 -0
  56. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-links.mjs +23 -0
  57. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-model-tables.mjs +24 -0
  58. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-palette.mjs +34 -0
  59. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-provenance.mjs +20 -0
  60. package/plugins/solution-architect/skills/analyze-requirements/scripts/lib/validate-tree.mjs +16 -0
  61. package/plugins/solution-architect/skills/analyze-requirements/scripts/likec4-config.mjs +19 -0
  62. package/plugins/solution-architect/skills/analyze-requirements/scripts/likec4-export.mjs +35 -0
  63. package/plugins/solution-architect/skills/analyze-requirements/scripts/likec4-gen.mjs +39 -0
  64. package/plugins/solution-architect/skills/analyze-requirements/scripts/render.mjs +130 -0
  65. package/plugins/solution-architect/skills/analyze-requirements/scripts/serve.mjs +71 -0
  66. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/browser.test.mjs +553 -0
  67. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/companion-links.test.mjs +60 -0
  68. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/doc-kinds.test.mjs +41 -0
  69. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/doc-links.test.mjs +104 -0
  70. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/doc-routes.test.mjs +54 -0
  71. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/doc-sections.test.mjs +197 -0
  72. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/embed.test.mjs +28 -0
  73. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/er-entities.test.mjs +14 -0
  74. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/ARCHITECTURE.md +47 -0
  75. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/c4-src/broken/model.c4 +27 -0
  76. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/c4-src/pass/model.c4 +29 -0
  77. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/docs/adr/0001-sample.md +18 -0
  78. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/docs/estimate.html +2 -0
  79. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/docs/estimation.md +8 -0
  80. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/model-broken.json +1 -0
  81. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/model.json +1 -0
  82. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/docs-pass/threat-model.md +8 -0
  83. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/sample-model.json +1 -0
  84. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fixtures/sample.c4 +28 -0
  85. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/fonts.test.mjs +38 -0
  86. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/frontmatter.test.mjs +27 -0
  87. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/inputs.test.mjs +72 -0
  88. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/likec4-config-cli.test.mjs +26 -0
  89. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/likec4-config.test.mjs +74 -0
  90. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/likec4-export-cli.test.mjs +61 -0
  91. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/likec4-extract.test.mjs +37 -0
  92. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/likec4-gen-cli.test.mjs +73 -0
  93. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/likec4-steps.test.mjs +32 -0
  94. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-flows.test.mjs +82 -0
  95. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-links.test.mjs +16 -0
  96. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-provenance.test.mjs +42 -0
  97. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-render.test.mjs +226 -0
  98. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-tables.test.mjs +30 -0
  99. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-views.test.mjs +76 -0
  100. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/md-wide.test.mjs +76 -0
  101. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/nav.test.mjs +236 -0
  102. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/offline.test.mjs +37 -0
  103. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/quality-gate.test.mjs +86 -0
  104. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/quality-gates.test.mjs +20 -0
  105. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/render.test.mjs +171 -0
  106. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/research-workflow.test.mjs +38 -0
  107. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/section-help.test.mjs +101 -0
  108. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/serve.test.mjs +48 -0
  109. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-clusters.test.mjs +46 -0
  110. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-deployment.test.mjs +23 -0
  111. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-elections.test.mjs +30 -0
  112. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-links.test.mjs +38 -0
  113. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-model-tables.test.mjs +51 -0
  114. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-provenance.test.mjs +28 -0
  115. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/validate-tree.test.mjs +43 -0
  116. package/plugins/solution-architect/skills/analyze-requirements/scripts/test/viewer-template.test.mjs +1206 -0
  117. package/plugins/solution-architect/skills/analyze-requirements/scripts/validate.mjs +88 -0
  118. package/plugins/solution-architect/skills/analyze-requirements/workflows/research.js +57 -0
  119. package/plugins/solution-architect/skills/estimate/README.md +67 -0
  120. package/plugins/solution-architect/skills/estimate/SKILL.md +64 -0
  121. package/plugins/solution-architect/skills/estimate/assets/estimate-template.html +1128 -0
  122. package/plugins/solution-architect/skills/estimate/references/ai-multipliers.md +95 -0
  123. package/plugins/solution-architect/skills/estimate/references/interview.md +80 -0
  124. package/plugins/solution-architect/skills/estimate/references/slicing.md +55 -0
  125. package/plugins/solution-architect/skills/estimate/references/techniques.md +111 -0
  126. package/plugins/solution-architect/skills/estimate/references/writing.md +211 -0
  127. package/plugins/solution-architect/skills/estimate/scripts/compute.mjs +23 -0
  128. package/plugins/solution-architect/skills/estimate/scripts/lib/checks.mjs +128 -0
  129. package/plugins/solution-architect/skills/estimate/scripts/lib/components.mjs +19 -0
  130. package/plugins/solution-architect/skills/estimate/scripts/lib/estimate-math.mjs +85 -0
  131. package/plugins/solution-architect/skills/estimate/scripts/lib/inline.mjs +10 -0
  132. package/plugins/solution-architect/skills/estimate/scripts/lib/redact.mjs +30 -0
  133. package/plugins/solution-architect/skills/estimate/scripts/lib/roadmap.mjs +32 -0
  134. package/plugins/solution-architect/skills/estimate/scripts/lib/rollup.mjs +122 -0
  135. package/plugins/solution-architect/skills/estimate/scripts/lib/schema.mjs +128 -0
  136. package/plugins/solution-architect/skills/estimate/scripts/render.mjs +65 -0
  137. package/plugins/solution-architect/skills/estimate/scripts/test/browser.test.mjs +623 -0
  138. package/plugins/solution-architect/skills/estimate/scripts/test/compute.test.mjs +88 -0
  139. package/plugins/solution-architect/skills/estimate/scripts/test/e2e.test.mjs +34 -0
  140. package/plugins/solution-architect/skills/estimate/scripts/test/estimate-math.test.mjs +112 -0
  141. package/plugins/solution-architect/skills/estimate/scripts/test/fixtures/booking-inputs.json +58 -0
  142. package/plugins/solution-architect/skills/estimate/scripts/test/fixtures/estimation-fail.md +30 -0
  143. package/plugins/solution-architect/skills/estimate/scripts/test/fixtures/estimation-pass.md +60 -0
  144. package/plugins/solution-architect/skills/estimate/scripts/test/quality-gates.test.mjs +20 -0
  145. package/plugins/solution-architect/skills/estimate/scripts/test/references.test.mjs +71 -0
  146. package/plugins/solution-architect/skills/estimate/scripts/test/render.test.mjs +121 -0
  147. package/plugins/solution-architect/skills/estimate/scripts/test/schema.test.mjs +129 -0
  148. package/plugins/solution-architect/skills/estimate/scripts/test/validate.test.mjs +72 -0
  149. package/plugins/solution-architect/skills/estimate/scripts/validate.mjs +20 -0
  150. package/plugins/solution-architect/skills/new-lead/README.md +48 -0
  151. package/plugins/solution-architect/skills/new-lead/SKILL.md +113 -0
  152. package/plugins/solution-architect/skills/new-lead/assets/dashboard/detail.html +842 -0
  153. package/plugins/solution-architect/skills/new-lead/assets/dashboard/index.html +655 -0
  154. package/plugins/solution-architect/skills/new-lead/assets/dashboard/start.sh +3 -0
  155. package/plugins/solution-architect/skills/new-lead/assets/dashboard/stats.mjs +98 -0
  156. package/plugins/solution-architect/skills/new-lead/assets/dashboard/vendor/reactflow-bundle.js +473 -0
  157. package/plugins/solution-architect/skills/new-lead/assets/vendor-build/entry.jsx +33 -0
  158. package/plugins/solution-architect/skills/new-lead/assets/vendor-build/package-lock.json +1055 -0
  159. package/plugins/solution-architect/skills/new-lead/assets/vendor-build/package.json +19 -0
  160. package/plugins/solution-architect/skills/new-lead/scripts/init-root.mjs +73 -0
  161. package/plugins/solution-architect/skills/new-lead/scripts/lead-upsert.mjs +31 -0
  162. package/plugins/solution-architect/skills/new-lead/scripts/lib/enrich.mjs +20 -0
  163. package/plugins/solution-architect/skills/new-lead/scripts/lib/map-nodes.mjs +124 -0
  164. package/plugins/solution-architect/skills/new-lead/scripts/lib/map.mjs +117 -0
  165. package/plugins/solution-architect/skills/new-lead/scripts/lib/port.mjs +15 -0
  166. package/plugins/solution-architect/skills/new-lead/scripts/lib/registry.mjs +116 -0
  167. package/plugins/solution-architect/skills/new-lead/scripts/serve.mjs +163 -0
  168. package/plugins/solution-architect/skills/new-lead/scripts/test/e2e-workspace.test.mjs +59 -0
  169. package/plugins/solution-architect/skills/new-lead/scripts/test/enrich.test.mjs +16 -0
  170. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/ARCHITECTURE.md +16 -0
  171. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/brief.md +4 -0
  172. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/dist/index.html +11 -0
  173. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/dist/proposal-non-tech.html +2 -0
  174. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/dist/threat-model.html +2 -0
  175. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/estimation-inputs.json +6 -0
  176. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/estimation.json +10 -0
  177. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/notes.md +3 -0
  178. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/proposal-non-tech.md +3 -0
  179. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads/acme-crm/rfp.md +3 -0
  180. package/plugins/solution-architect/skills/new-lead/scripts/test/fixtures/root/leads.json +25 -0
  181. package/plugins/solution-architect/skills/new-lead/scripts/test/init-root.test.mjs +81 -0
  182. package/plugins/solution-architect/skills/new-lead/scripts/test/lead-upsert.test.mjs +67 -0
  183. package/plugins/solution-architect/skills/new-lead/scripts/test/map.test.mjs +72 -0
  184. package/plugins/solution-architect/skills/new-lead/scripts/test/port.test.mjs +31 -0
  185. package/plugins/solution-architect/skills/new-lead/scripts/test/registry-io.test.mjs +57 -0
  186. package/plugins/solution-architect/skills/new-lead/scripts/test/registry.test.mjs +49 -0
  187. package/plugins/solution-architect/skills/new-lead/scripts/test/serve.test.mjs +205 -0
  188. package/plugins/solution-architect/skills/new-lead/scripts/test/stats.test.mjs +92 -0
  189. package/plugins/solution-architect/skills/new-lead/scripts/test/validate-cli.test.mjs +24 -0
  190. package/plugins/solution-architect/skills/new-lead/scripts/test/vendor-bundle.test.mjs +9 -0
  191. package/plugins/solution-architect/skills/new-lead/scripts/validate.mjs +9 -0
  192. package/plugins/solution-architect/skills/proposal/README.md +36 -0
  193. package/plugins/solution-architect/skills/proposal/SKILL.md +58 -0
  194. package/plugins/solution-architect/skills/proposal/assets/proposal-template.html +82 -0
  195. package/plugins/solution-architect/skills/proposal/references/interview.md +63 -0
  196. package/plugins/solution-architect/skills/proposal/references/review.md +28 -0
  197. package/plugins/solution-architect/skills/proposal/references/writing.md +102 -0
  198. package/plugins/solution-architect/skills/proposal/scripts/derive.mjs +22 -0
  199. package/plugins/solution-architect/skills/proposal/scripts/lib/checks-client.mjs +75 -0
  200. package/plugins/solution-architect/skills/proposal/scripts/lib/checks-doc.mjs +54 -0
  201. package/plugins/solution-architect/skills/proposal/scripts/lib/checks.mjs +17 -0
  202. package/plugins/solution-architect/skills/proposal/scripts/lib/figures.mjs +43 -0
  203. package/plugins/solution-architect/skills/proposal/scripts/lib/jargon.mjs +9 -0
  204. package/plugins/solution-architect/skills/proposal/scripts/lib/sections.mjs +32 -0
  205. package/plugins/solution-architect/skills/proposal/scripts/render.mjs +60 -0
  206. package/plugins/solution-architect/skills/proposal/scripts/test/checks-client.test.mjs +144 -0
  207. package/plugins/solution-architect/skills/proposal/scripts/test/checks-doc.test.mjs +93 -0
  208. package/plugins/solution-architect/skills/proposal/scripts/test/docs.test.mjs +51 -0
  209. package/plugins/solution-architect/skills/proposal/scripts/test/e2e.test.mjs +30 -0
  210. package/plugins/solution-architect/skills/proposal/scripts/test/figures.test.mjs +81 -0
  211. package/plugins/solution-architect/skills/proposal/scripts/test/fixtures/proposal-fail.md +22 -0
  212. package/plugins/solution-architect/skills/proposal/scripts/test/fixtures/proposal-pass.md +55 -0
  213. package/plugins/solution-architect/skills/proposal/scripts/test/quality-gates.test.mjs +20 -0
  214. package/plugins/solution-architect/skills/proposal/scripts/test/render.test.mjs +70 -0
  215. package/plugins/solution-architect/skills/proposal/scripts/test/validate.test.mjs +40 -0
  216. package/plugins/solution-architect/skills/proposal/scripts/validate.mjs +20 -0
  217. package/src/cli/agents.mjs +12 -0
  218. package/src/cli/args.mjs +46 -0
  219. package/src/cli/install.mjs +68 -0
  220. package/src/cli/main.mjs +124 -0
  221. package/src/cli/picker.mjs +78 -0
  222. package/src/cli/registry.mjs +26 -0
  223. package/src/cli/uninstall.mjs +38 -0
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "vendor-build",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [],
10
+ "author": "",
11
+ "license": "ISC",
12
+ "type": "commonjs",
13
+ "dependencies": {
14
+ "esbuild": "^0.28.2",
15
+ "react": "^18.3.1",
16
+ "react-dom": "^18.3.1",
17
+ "reactflow": "^11.11.4"
18
+ }
19
+ }
@@ -0,0 +1,73 @@
1
+ // scripts/init-root.mjs
2
+ // new-lead-dashboard v2
3
+ import { existsSync } from 'node:fs';
4
+ import { mkdir, readFile, writeFile, copyFile, chmod } from 'node:fs/promises';
5
+ import { dirname, join } from 'node:path';
6
+ import { fileURLToPath } from 'node:url';
7
+ import { parseArgs } from 'node:util';
8
+
9
+ const SCRIPTS = dirname(fileURLToPath(import.meta.url));
10
+ export const ASSET_FILES = [
11
+ { from: 'index.html', to: 'scripts/index.html' },
12
+ { from: 'detail.html', to: 'scripts/detail.html' },
13
+ { from: 'stats.mjs', to: 'scripts/stats.mjs' },
14
+ { from: 'vendor/reactflow-bundle.js', to: 'scripts/vendor/reactflow-bundle.js' },
15
+ { from: 'start.sh', to: 'start.sh' },
16
+ ];
17
+ export const SCRIPT_FILES = [
18
+ { from: 'serve.mjs', to: 'scripts/serve.mjs' },
19
+ { from: 'lead-upsert.mjs', to: 'scripts/lead-upsert.mjs' },
20
+ { from: 'validate.mjs', to: 'scripts/validate.mjs' },
21
+ { from: 'lib/registry.mjs', to: 'scripts/lib/registry.mjs' },
22
+ { from: 'lib/port.mjs', to: 'scripts/lib/port.mjs' },
23
+ { from: 'lib/enrich.mjs', to: 'scripts/lib/enrich.mjs' },
24
+ { from: 'lib/map.mjs', to: 'scripts/lib/map.mjs' },
25
+ { from: 'lib/map-nodes.mjs', to: 'scripts/lib/map-nodes.mjs' },
26
+ ];
27
+
28
+ const DIRS = ['leads', 'scripts/lib', 'scripts/vendor'];
29
+
30
+ export async function initRoot(root, assetsDir) {
31
+ for (const dir of DIRS) await mkdir(join(root, dir), { recursive: true });
32
+ const created = await ensureRegistry(root);
33
+ const copied = [];
34
+ for (const f of ASSET_FILES) copied.push(...await copyIfNewer(join(assetsDir, f.from), root, f.to));
35
+ for (const f of SCRIPT_FILES) copied.push(...await copyIfNewer(join(SCRIPTS, f.from), root, f.to));
36
+ return { created, copied };
37
+ }
38
+
39
+ async function ensureRegistry(root) {
40
+ if (existsSync(join(root, 'leads.json'))) return false;
41
+ await writeFile(join(root, 'leads.json'), JSON.stringify({ version: 1, leads: [] }, null, 2) + '\n');
42
+ return true;
43
+ }
44
+
45
+ async function copyIfNewer(src, root, rel) {
46
+ const dest = join(root, rel);
47
+ const [destStamp, srcStamp] = await Promise.all([stampOf(dest), stampOf(src)]);
48
+ if (destStamp >= srcStamp) return [];
49
+ await copyFile(src, dest);
50
+ if (rel.endsWith('.sh')) await chmod(dest, 0o755);
51
+ return [rel];
52
+ }
53
+
54
+ // A header window rather than the whole file, so a stamp quoted in prose or in a
55
+ // string literal further down cannot be mistaken for the file's own. Sized to clear a
56
+ // path comment plus an import block, because a stamp legitimately sits below one:
57
+ // lib/registry.mjs stamps at line 6 and read as 0 under the previous 3-line window, so
58
+ // it silently never refreshed. init-root.test.mjs pins this for every copied file.
59
+ const STAMP_LINES = 10;
60
+
61
+ export async function stampOf(file) {
62
+ if (!existsSync(file)) return -1;
63
+ const head = (await readFile(file, 'utf8')).split('\n', STAMP_LINES).join('\n');
64
+ return Number(head.match(/new-lead-dashboard v(\d+)/)?.[1] ?? 0);
65
+ }
66
+
67
+ if (process.argv[1] === fileURLToPath(import.meta.url)) {
68
+ const { values } = parseArgs({ options: { root: { type: 'string' }, assets: { type: 'string' } } });
69
+ if (!values.root) { console.error('usage: init-root.mjs --root <dir> [--assets <dir>]'); process.exit(2); }
70
+ const assets = values.assets ?? join(SCRIPTS, '..', 'assets', 'dashboard');
71
+ const res = await initRoot(values.root, assets);
72
+ console.log(JSON.stringify(res));
73
+ }
@@ -0,0 +1,31 @@
1
+ // scripts/lead-upsert.mjs
2
+ // new-lead-dashboard v1
3
+ import { parseArgs } from 'node:util';
4
+ import { readRegistry, writeRegistry } from './lib/registry.mjs';
5
+
6
+ const DEFAULTS = { client: null, status: 'active', closed: null, value: null, scenario: null };
7
+
8
+ const { values } = parseArgs({
9
+ options: { root: { type: 'string' }, id: { type: 'string' }, patch: { type: 'string' } },
10
+ });
11
+ if (!values.root || !values.id || !values.patch) {
12
+ console.error('usage: lead-upsert.mjs --root <dir> --id <lead-id> --patch <json>');
13
+ process.exit(2);
14
+ }
15
+
16
+ const patch = JSON.parse(values.patch);
17
+ const registry = await readRegistry(values.root);
18
+ const index = registry.leads.findIndex((l) => l.id === values.id);
19
+ const existing = index === -1 ? {} : registry.leads[index];
20
+ const merged = { ...DEFAULTS, ...existing, ...patch, id: values.id };
21
+
22
+ if (index === -1) registry.leads.push(merged);
23
+ else registry.leads[index] = merged;
24
+
25
+ try {
26
+ await writeRegistry(values.root, registry);
27
+ } catch (err) {
28
+ console.error(err.message);
29
+ process.exit(1);
30
+ }
31
+ console.log(JSON.stringify(merged));
@@ -0,0 +1,20 @@
1
+ // scripts/lib/enrich.mjs
2
+ // new-lead-dashboard v2
3
+ import { existsSync } from 'node:fs';
4
+ import { join } from 'node:path';
5
+ import { leadDir } from './registry.mjs';
6
+
7
+ export async function enrichLead(root, lead) {
8
+ const dir = leadDir(root, lead.id);
9
+ const dist = join(dir, 'dist');
10
+ return {
11
+ ...lead,
12
+ artifacts: {
13
+ docs: existsSync(join(dist, 'index.html')),
14
+ estimate: existsSync(join(dist, 'estimate.html')),
15
+ proposal: existsSync(join(dist, 'proposal.html')),
16
+ },
17
+ hasBrief: existsSync(join(dir, 'brief.md')),
18
+ hasNotes: existsSync(join(dir, 'notes.md')),
19
+ };
20
+ }
@@ -0,0 +1,124 @@
1
+ // scripts/lib/map-nodes.mjs
2
+ // new-lead-dashboard v2
3
+ import { readdir } from 'node:fs/promises';
4
+ import { join } from 'node:path';
5
+
6
+ // Everything the three skills write. What is left in the lead directory is what
7
+ // the human put there, which is exactly the evidence.
8
+ const GENERATED = new Set([
9
+ 'ARCHITECTURE.md', 'estimation.md', 'estimation.json', 'estimation-inputs.json',
10
+ 'proposal.md', 'proposal-figures.json', 'notes.md', 'brief.md', 'dist',
11
+ 'CONTEXT.md', 'CONTEXT-MAP.md', 'DOMAIN-OVERVIEW.md', 'threat-model.md', 'docs',
12
+ ]);
13
+
14
+ // proposal.md plus every proposal-* variant (client, non-tech, ...) — all
15
+ // written by the proposal skill, so never evidence.
16
+ const isProposalMd = (name) => name.startsWith('proposal') && name.endsWith('.md');
17
+
18
+ // The dist/ pages the pipeline owns by name; every other html in dist/ becomes
19
+ // a generic page node unless a proposal-family md already claims it.
20
+ const NAMED_PAGES = { arch: 'index.html', estimate: 'estimate.html', proposal: 'proposal.html' };
21
+
22
+ const htmlFor = (md) => md.replace(/\.md$/, '.html');
23
+
24
+ // One readdir per directory; every builder below is a pure function over the
25
+ // two listings, carried in ctx: { id, src, names (lead dir), pages (dist/) }.
26
+ export async function leadNodes(id, dir, src) {
27
+ const [entries, distEntries] = await Promise.all([
28
+ readdir(dir, { withFileTypes: true }).catch(() => []),
29
+ readdir(join(dir, 'dist'), { withFileTypes: true }).catch(() => []),
30
+ ]);
31
+ const ctx = {
32
+ id,
33
+ src,
34
+ names: new Set(entries.map((e) => e.name)),
35
+ pages: new Set(distEntries.filter((e) => e.isFile()).map((e) => e.name)),
36
+ };
37
+ const proposalMds = [...ctx.names].filter((n) => isProposalMd(n) && n !== 'proposal.md');
38
+ const claimed = new Set([...Object.values(NAMED_PAGES), ...proposalMds.map(htmlFor)]);
39
+ return [
40
+ ...evidenceNodes(entries),
41
+ ...docNodes(ctx),
42
+ ...proposalMds.map((n) => docNode(ctx, { key: n, label: n, page: htmlFor(n), exists: true })),
43
+ ...pageNodes(ctx, claimed),
44
+ ];
45
+ }
46
+
47
+ function isEvidence(entry) {
48
+ return !entry.name.startsWith('.')
49
+ && !GENERATED.has(entry.name)
50
+ && !entry.name.endsWith('.c4')
51
+ && !isProposalMd(entry.name);
52
+ }
53
+
54
+ function evidenceNodes(entries) {
55
+ return entries.filter(isEvidence).map((entry) => ({
56
+ id: `evidence-${entry.name}`,
57
+ type: 'evidence',
58
+ position: { x: 0, y: 0 },
59
+ data: { label: entry.name, status: 'ready', href: null, detail: null },
60
+ }));
61
+ }
62
+
63
+ function docNode(ctx, spec) {
64
+ const href = spec.exists && ctx.pages.has(spec.page)
65
+ ? `/leads/${ctx.id}/dist/${spec.page}` : null;
66
+ return {
67
+ id: spec.key,
68
+ type: 'doc',
69
+ position: { x: 0, y: 0 },
70
+ data: { label: spec.label, status: spec.exists ? 'ready' : 'pending', href, detail: null },
71
+ };
72
+ }
73
+
74
+ function docNodes(ctx) {
75
+ return [
76
+ docNode(ctx, { key: 'arch', label: 'Architecture', page: NAMED_PAGES.arch, exists: !!ctx.src.arch }),
77
+ ...componentNodes(ctx.src.arch),
78
+ docNode(ctx, { key: 'estimate', label: 'Estimate', page: NAMED_PAGES.estimate, exists: !!ctx.src.estimation }),
79
+ ...scenarioNodes(ctx.src.estimation),
80
+ docNode(ctx, { key: 'proposal', label: 'Proposal', page: NAMED_PAGES.proposal, exists: ctx.names.has('proposal.md') }),
81
+ ];
82
+ }
83
+
84
+ // Any unclaimed page in dist/ is still a rendered deliverable: give it a
85
+ // clickable node beside the ones the pipeline knows by name.
86
+ function pageNodes(ctx, claimed) {
87
+ return [...ctx.pages]
88
+ .filter((name) => name.endsWith('.html') && !claimed.has(name))
89
+ .map((name) => docNode(ctx, { key: `page-${name}`, label: name, page: name, exists: true }));
90
+ }
91
+
92
+ function sliceSection(archMd) {
93
+ const start = archMd.match(/^##\s*6\..*$/m);
94
+ if (!start) return '';
95
+ const rest = archMd.slice(start.index + start[0].length);
96
+ const end = rest.match(/^##\s/m);
97
+ return end ? rest.slice(0, end.index) : rest;
98
+ }
99
+
100
+ function parseComponents(archMd) {
101
+ if (!archMd) return [];
102
+ const rows = sliceSection(archMd).split('\n').filter((l) => l.trim().startsWith('|'));
103
+ return rows.slice(1)
104
+ .filter((l) => !/^\|\s*-+\s*\|/.test(l.trim()))
105
+ .map((l) => l.split('|')[1].trim().replace(/`/g, ''));
106
+ }
107
+
108
+ function componentNodes(archMd) {
109
+ return parseComponents(archMd).map((name) => ({
110
+ id: `component-${name}`,
111
+ type: 'component',
112
+ position: { x: 0, y: 0 },
113
+ data: { label: name, status: 'ready', href: null, detail: null },
114
+ }));
115
+ }
116
+
117
+ function scenarioNodes(est) {
118
+ return (est?.scenarios ?? []).map((s) => ({
119
+ id: `scenario-${s.id ?? s.name}`,
120
+ type: 'scenario',
121
+ position: { x: 0, y: 0 },
122
+ data: { label: s.label ?? s.plan ?? String(s.id ?? s.name), status: 'ready', href: null, detail: null },
123
+ }));
124
+ }
@@ -0,0 +1,117 @@
1
+ // scripts/lib/map.mjs
2
+ // new-lead-dashboard v6
3
+ import { readFile } from 'node:fs/promises';
4
+ import { execFile } from 'node:child_process';
5
+ import { promisify } from 'node:util';
6
+ import { join } from 'node:path';
7
+ import { leadDir, readRegistry } from './registry.mjs';
8
+ import { leadNodes } from './map-nodes.mjs';
9
+
10
+ const run = promisify(execFile);
11
+ const X = { evidence: 0, arch: 480, estimate: 720, proposal: 960, page: 1200 };
12
+ const STEP_Y = 90;
13
+
14
+ export async function buildLeadMap(root, id) {
15
+ const dir = leadDir(root, id);
16
+ const src = await readSources(dir);
17
+ const nodes = await leadNodes(id, dir, src);
18
+ layout(nodes);
19
+ return { nodes, edges: edgesFor(nodes), panels: await panelsFor(root, id, src) };
20
+ }
21
+
22
+ async function readJson(path) {
23
+ try { return JSON.parse(await readFile(path, 'utf8')); } catch { return null; }
24
+ }
25
+
26
+ async function readText(path) {
27
+ try { return await readFile(path, 'utf8'); } catch { return null; }
28
+ }
29
+
30
+ async function readSources(dir) {
31
+ const [arch, estimation, inputs, brief] = await Promise.all([
32
+ readText(join(dir, 'ARCHITECTURE.md')),
33
+ readJson(join(dir, 'estimation.json')),
34
+ readJson(join(dir, 'estimation-inputs.json')),
35
+ readText(join(dir, 'brief.md')),
36
+ ]);
37
+ return { arch, estimation, inputs, brief };
38
+ }
39
+
40
+ function edge(source, target) {
41
+ return { id: `e-${source}-${target}`, source, target };
42
+ }
43
+
44
+ function edgesFor(nodes) {
45
+ const ids = new Set(nodes.map((n) => n.id));
46
+ const link = (a, b) => (ids.has(a) && ids.has(b) ? [edge(a, b)] : []);
47
+ const byPrefix = (prefix) => nodes.filter((n) => n.id.startsWith(prefix));
48
+ return [
49
+ ...byPrefix('evidence-').flatMap((n) => link(n.id, 'arch')),
50
+ ...link('arch', 'estimate'),
51
+ ...link('estimate', 'proposal'),
52
+ ...byPrefix('component-').flatMap((n) => link('arch', n.id)),
53
+ ...byPrefix('scenario-').flatMap((n) => link('estimate', n.id)),
54
+ // proposal-family mds (ids are their filenames, e.g. proposal-non-tech.md)
55
+ ...byPrefix('proposal').filter((n) => n.id !== 'proposal')
56
+ .flatMap((n) => link('estimate', n.id)),
57
+ ];
58
+ }
59
+
60
+ function colX(n) {
61
+ if (n.id.startsWith('evidence-')) return X.evidence;
62
+ if (n.id === 'arch') return X.arch;
63
+ if (n.id.startsWith('component-')) return X.arch + 40;
64
+ if (n.id === 'estimate') return X.estimate;
65
+ if (n.id.startsWith('scenario-')) return X.estimate + 40;
66
+ if (n.id.startsWith('page-')) return X.page;
67
+ return X.proposal; // the named proposal node plus proposal-family mds
68
+ }
69
+
70
+ function layout(nodes) {
71
+ const y = new Map();
72
+ for (const n of nodes) {
73
+ const x = colX(n);
74
+ const cur = y.get(x) ?? 0;
75
+ n.position = { x, y: cur };
76
+ y.set(x, cur + STEP_Y);
77
+ }
78
+ }
79
+
80
+ function risksFor(est) {
81
+ return (est?.risks ?? []).slice(0, 3).map((r) => r.name ?? r.title ?? String(r));
82
+ }
83
+
84
+ function openQuestionsFor(inputs) {
85
+ return (inputs?.scope ?? [])
86
+ .filter((s) => (s.provenance ?? s.label) === 'proposed')
87
+ .map((s) => s.name ?? s.item);
88
+ }
89
+
90
+ async function activityFor(root, id) {
91
+ try {
92
+ const { stdout } = await run('git', ['-C', root, 'log', '--format=%as %s', '--', join('leads', id)]);
93
+ return stdout.split('\n').filter(Boolean);
94
+ } catch {
95
+ return [];
96
+ }
97
+ }
98
+
99
+ // facts is the registry entry itself — business metadata the dashboard displays.
100
+ // It is the only source for these fields now that the answers file is gone.
101
+ async function factsFor(root, id) {
102
+ const registry = await readRegistry(root).catch(() => ({ leads: [] }));
103
+ const lead = registry.leads.find((l) => l.id === id);
104
+ if (!lead) return null;
105
+ const { client, title, status, created, value, scenario } = lead;
106
+ return { client, title, status, created, value, scenario };
107
+ }
108
+
109
+ async function panelsFor(root, id, src) {
110
+ return {
111
+ brief: src.brief ?? null,
112
+ facts: await factsFor(root, id),
113
+ risks: risksFor(src.estimation),
114
+ openQuestions: openQuestionsFor(src.inputs),
115
+ activity: await activityFor(root, id),
116
+ };
117
+ }
@@ -0,0 +1,15 @@
1
+ // scripts/lib/port.mjs
2
+ // new-lead-dashboard v1
3
+ import net from 'node:net';
4
+
5
+ export async function findFreePort(start = 4600) {
6
+ return (await isFree(start)) ? start : findFreePort(start + 1);
7
+ }
8
+
9
+ function isFree(port) {
10
+ return new Promise((resolve) => {
11
+ const srv = net.createServer();
12
+ srv.once('error', () => resolve(false));
13
+ srv.listen(port, '127.0.0.1', () => srv.close(() => resolve(true)));
14
+ });
15
+ }
@@ -0,0 +1,116 @@
1
+ import { constants, existsSync } from 'node:fs';
2
+ import { readFile, rename, open, unlink } from 'node:fs/promises';
3
+ import { dirname, join, resolve } from 'node:path';
4
+
5
+ // scripts/lib/registry.mjs
6
+ // new-lead-dashboard v2
7
+ export const STATUSES = new Set(['active', 'won', 'lost']);
8
+ export const ID_RE = /^[a-z0-9]+(-[a-z0-9]+)*$/;
9
+ const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
10
+
11
+ export function validateRegistry(registry) {
12
+ const findings = [];
13
+ if (registry?.version !== 1) findings.push('version must be 1');
14
+ if (!Array.isArray(registry?.leads)) {
15
+ findings.push('leads must be an array');
16
+ return findings;
17
+ }
18
+ const seen = new Set();
19
+ registry.leads.forEach((l, i) => findings.push(...validateLead(l, `leads[${i}]`, seen)));
20
+ return findings;
21
+ }
22
+
23
+ function validateLead(lead, at, seen) {
24
+ const f = [];
25
+ if (!ID_RE.test(lead.id ?? '')) {
26
+ f.push(`${at}: id must be kebab-case`);
27
+ } else if (seen.has(lead.id)) {
28
+ f.push(`${at}: duplicate id ${lead.id}`);
29
+ } else {
30
+ seen.add(lead.id);
31
+ }
32
+ if (lead.client !== null && !nonEmpty(lead.client)) {
33
+ f.push(`${at}: client must be null or a non-empty string`);
34
+ }
35
+ if (!nonEmpty(lead.title)) f.push(`${at}: title must be a non-empty string`);
36
+ if (!STATUSES.has(lead.status)) f.push(`${at}: status must be active|won|lost`);
37
+ if (!DATE_RE.test(lead.created ?? '')) f.push(`${at}: created must be YYYY-MM-DD`);
38
+ f.push(...validateClosed(lead, at));
39
+ if (lead.value !== null && !isValue(lead.value)) f.push(`${at}: value must be null or {low<=high, currency}`);
40
+ return f;
41
+ }
42
+
43
+ function validateClosed(lead, at) {
44
+ const ok = lead.status === 'active' ? lead.closed === null : DATE_RE.test(lead.closed ?? '');
45
+ return ok ? [] : [`${at}: closed must be null while active, a date once won/lost`];
46
+ }
47
+
48
+ function nonEmpty(v) {
49
+ return typeof v === 'string' && v.length > 0;
50
+ }
51
+
52
+ function isValue(v) {
53
+ return typeof v?.low === 'number' && typeof v?.high === 'number'
54
+ && v.low <= v.high && typeof v.currency === 'string';
55
+ }
56
+
57
+ const REGISTRY_FILE = 'leads.json';
58
+
59
+ const LEADS_DIR = 'leads';
60
+
61
+ // The single place the on-disk layout is encoded: every lead lives under
62
+ // <root>/leads/<id>, and <root>/scripts holds the dashboard. Callers that join
63
+ // root and id themselves will silently read the wrong path.
64
+ export function leadDir(root, id) {
65
+ return join(root, LEADS_DIR, id);
66
+ }
67
+
68
+ export function findLeadsRoot(startDir) {
69
+ let dir = resolve(startDir);
70
+ while (true) {
71
+ if (existsSync(join(dir, REGISTRY_FILE))) return dir;
72
+ const parent = dirname(dir);
73
+ if (parent === dir) return null;
74
+ dir = parent;
75
+ }
76
+ }
77
+
78
+ export async function readRegistry(root) {
79
+ return JSON.parse(await readFile(join(root, REGISTRY_FILE), 'utf8'));
80
+ }
81
+
82
+ export async function writeRegistry(root, registry) {
83
+ const findings = validateRegistry(registry);
84
+ if (findings.length) throw new Error(`invalid registry: ${findings.join('; ')}`);
85
+ const lock = await acquireLock(root);
86
+ try {
87
+ await writeTemp(join(root, `${REGISTRY_FILE}.tmp`), JSON.stringify(registry, null, 2) + '\n');
88
+ await rename(join(root, `${REGISTRY_FILE}.tmp`), join(root, REGISTRY_FILE));
89
+ } finally {
90
+ await lock.close();
91
+ await unlink(join(root, `${REGISTRY_FILE}.lock`));
92
+ }
93
+ }
94
+
95
+ // O_NOFOLLOW, because an ordinary write follows a symlinked temp file: the registry
96
+ // would land on its target, and the rename would then move the symlink itself into
97
+ // place, leaving leads.json a permanent redirect out of the root. The lock needs no
98
+ // such guard - O_CREAT|O_EXCL refuses a symlink by definition.
99
+ const TEMP_FLAGS = constants.O_WRONLY | constants.O_CREAT | constants.O_TRUNC | constants.O_NOFOLLOW;
100
+
101
+ async function writeTemp(path, body) {
102
+ const handle = await open(path, TEMP_FLAGS).catch((err) => {
103
+ if (err.code === 'ELOOP') throw new Error(`${REGISTRY_FILE}.tmp is a symlink; refusing to write through it`);
104
+ throw err;
105
+ });
106
+ try { await handle.writeFile(body); } finally { await handle.close(); }
107
+ }
108
+
109
+ async function acquireLock(root) {
110
+ try {
111
+ return await open(join(root, `${REGISTRY_FILE}.lock`), 'wx');
112
+ } catch (err) {
113
+ if (err.code === 'EEXIST') throw new Error('leads.json is locked by another session');
114
+ throw err;
115
+ }
116
+ }
@@ -0,0 +1,163 @@
1
+ // scripts/serve.mjs
2
+ // new-lead-dashboard v3
3
+ import { createServer } from 'node:http';
4
+ import { readFile, writeFile, realpath, stat, lstat } from 'node:fs/promises';
5
+ import { join, resolve, sep, extname } from 'node:path';
6
+ import { parseArgs } from 'node:util';
7
+ import { fileURLToPath } from 'node:url';
8
+ import { findLeadsRoot, readRegistry, writeRegistry, leadDir, STATUSES, ID_RE } from './lib/registry.mjs';
9
+ import { enrichLead } from './lib/enrich.mjs';
10
+ import { buildLeadMap } from './lib/map.mjs';
11
+ import { findFreePort } from './lib/port.mjs';
12
+
13
+ const MIME = {
14
+ '.html': 'text/html', '.js': 'text/javascript', '.mjs': 'text/javascript', '.css': 'text/css',
15
+ '.json': 'application/json', '.svg': 'image/svg+xml', '.md': 'text/plain',
16
+ };
17
+ const ID = ID_RE.source.slice(1, -1);
18
+ const DIST_RE = new RegExp(`^/leads/(${ID})/dist/`);
19
+ const SCRIPTS_DIR = 'scripts';
20
+ const DEFAULT_PORT = 4600;
21
+
22
+ const ROUTES = [
23
+ ['GET', /^\/$/, ({ root }, req, res) => serveFile(root, res, join(SCRIPTS_DIR, 'index.html'))],
24
+ ['GET', new RegExp(`^/detail/(${ID})$`), ({ root }, req, res) => serveFile(root, res, join(SCRIPTS_DIR, 'detail.html'))],
25
+ ['GET', /^\/api\/leads$/, apiLeads],
26
+ ['GET', new RegExp(`^/api/leads/(${ID})/map$`), async ({ root, id }, req, res) => send(res, 200, await buildLeadMap(root, id))],
27
+ ['POST', new RegExp(`^/api/leads/(${ID})/notes$`), apiNotes],
28
+ ['POST', new RegExp(`^/api/leads/(${ID})$`), apiUpdate],
29
+ ];
30
+
31
+ export async function startServer(root, port) {
32
+ root = await realpath(resolve(root));
33
+ const server = createServer((req, res) => route(root, req, res).catch((err) => {
34
+ console.error(err);
35
+ if (res.headersSent) return res.destroy();
36
+ send(res, 500, { error: 'internal error' });
37
+ }));
38
+ return new Promise((ok, fail) => {
39
+ server.once('error', (err) => fail(err.code === 'EADDRINUSE' ? new Error(`port ${port} in use`) : err));
40
+ server.listen(port, '127.0.0.1', () => ok(server));
41
+ });
42
+ }
43
+
44
+ async function route(root, req, res) {
45
+ const url = new URL(req.url, 'http://internal');
46
+ for (const [method, re, handler] of ROUTES) {
47
+ if (method !== req.method) continue;
48
+ const found = re.exec(url.pathname);
49
+ if (found) return handler({ root, id: found[1] }, req, res);
50
+ }
51
+ if (req.method !== 'GET') return send(res, 404, { error: 'not found' });
52
+ return serveStatic(root, url.pathname, res);
53
+ }
54
+
55
+ async function serveFile(root, res, name) {
56
+ const target = join(root, name);
57
+ let real;
58
+ try { real = await realpath(target); } catch { return send(res, 404, { error: 'not found' }); }
59
+ // name is a fixed literal (index.html/detail.html), so unlike serveStatic there's no
60
+ // pattern to match: the only file this route may ever serve is target itself. If a
61
+ // symlink swapped it for something else, realpath resolves to a different identity.
62
+ if (real !== target) return send(res, 403);
63
+ let data;
64
+ try {
65
+ data = await readFile(real);
66
+ } catch {
67
+ return send(res, 404, { error: 'not found' });
68
+ }
69
+ res.writeHead(200, { 'content-type': 'text/html' });
70
+ res.end(data);
71
+ }
72
+
73
+ async function apiLeads({ root }, req, res) {
74
+ const registry = await readRegistry(root);
75
+ const leads = await Promise.all(registry.leads.map((l) => enrichLead(root, l)));
76
+ send(res, 200, { version: registry.version, leads });
77
+ }
78
+
79
+ async function apiUpdate({ root, id }, req, res) {
80
+ const body = await readJsonBody(req);
81
+ if (!body || !STATUSES.has(body.status)) return send(res, 400, { error: 'invalid status' });
82
+ const registry = await readRegistry(root);
83
+ const lead = registry.leads.find((l) => l.id === id);
84
+ if (!lead) return send(res, 404, { error: 'not found' });
85
+ lead.status = body.status;
86
+ lead.closed = body.status === 'active' ? null : (body.closed ?? new Date().toISOString().slice(0, 10));
87
+ try {
88
+ await writeRegistry(root, registry);
89
+ } catch (err) {
90
+ return send(res, 409, { error: err.message });
91
+ }
92
+ send(res, 200, lead);
93
+ }
94
+
95
+ async function apiNotes({ root, id }, req, res) {
96
+ // The only write path into a lead dir, held to the same bar as the read paths:
97
+ // realpath resolves every component and every symlink hop of the directory, and
98
+ // lstat refuses a final component that is a symlink (or any non-regular file).
99
+ // notes.md usually does not exist yet, and an absent lstat is that normal case.
100
+ let dir;
101
+ try { dir = await realpath(leadDir(root, id)); } catch { return send(res, 404, { error: 'not found' }); }
102
+ if (dir !== root && !dir.startsWith(root + sep)) return send(res, 403);
103
+ const target = join(dir, 'notes.md');
104
+ const st = await lstat(target).catch(() => null);
105
+ if (st && !st.isFile()) return send(res, 403);
106
+ const body = await readJsonBody(req);
107
+ if (typeof body?.content !== 'string') return send(res, 400, { error: 'invalid content' });
108
+ await writeFile(target, body.content);
109
+ send(res, 200, { ok: true });
110
+ }
111
+
112
+ async function readJsonBody(req) {
113
+ const chunks = [];
114
+ for await (const chunk of req) chunks.push(chunk);
115
+ try {
116
+ return JSON.parse(Buffer.concat(chunks).toString('utf8') || '{}');
117
+ } catch {
118
+ return null;
119
+ }
120
+ }
121
+
122
+ function send(res, status, body) {
123
+ if (body === undefined) { res.writeHead(status); return res.end(); }
124
+ res.writeHead(status, { 'content-type': 'application/json' });
125
+ res.end(JSON.stringify(body));
126
+ }
127
+
128
+ function isAllowlisted(decoded) {
129
+ return decoded === '/scripts/stats.mjs'
130
+ || decoded.startsWith('/scripts/vendor/')
131
+ || DIST_RE.test(decoded);
132
+ }
133
+
134
+ async function serveStatic(root, pathname, res) {
135
+ let decoded;
136
+ try { decoded = decodeURIComponent(pathname); } catch { return send(res, 403); }
137
+ if (decoded.split('/').some((s) => s.startsWith('.'))) return send(res, 403);
138
+ const p = resolve(root, '.' + decoded);
139
+ if (p !== root && !p.startsWith(root + sep)) return send(res, 403);
140
+ if (!isAllowlisted(decoded)) return send(res, 404);
141
+ let real;
142
+ try { real = await realpath(p); } catch { return send(res, 404); }
143
+ if (real !== root && !real.startsWith(root + sep)) return send(res, 403);
144
+ // Re-check the allowlist against the resolved path: a symlink can stay inside root
145
+ // (containment above) yet land on a file the URL-path check refused, e.g.
146
+ // dist/link.json -> ../../leads.json. Side effect: a directory GET on an allowlisted
147
+ // prefix now 403s here (no trailing slash post-realpath) instead of 404 below.
148
+ const realDecoded = real === root ? '' : real.slice(root.length).split(sep).join('/');
149
+ if (!isAllowlisted(realDecoded)) return send(res, 403);
150
+ const st = await stat(real);
151
+ if (!st.isFile()) return send(res, 404);
152
+ res.writeHead(200, { 'content-type': MIME[extname(real)] ?? 'application/octet-stream' });
153
+ res.end(await readFile(real));
154
+ }
155
+
156
+ if (process.argv[1] === fileURLToPath(import.meta.url)) {
157
+ const { values } = parseArgs({ options: { root: { type: 'string' }, port: { type: 'string' } } });
158
+ const root = values.root ?? findLeadsRoot(process.cwd());
159
+ if (!root) { console.error('usage: serve.mjs [--root <dir>] [--port <n>]'); process.exit(2); }
160
+ const port = values.port ? Number(values.port) : await findFreePort(DEFAULT_PORT);
161
+ const server = await startServer(root, port);
162
+ console.log(`dashboard: http://127.0.0.1:${server.address().port}`);
163
+ }