@sarjallab09/figma-intelligence 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 (286) hide show
  1. package/LICENSE +26 -0
  2. package/README.md +327 -0
  3. package/bin/cli.js +859 -0
  4. package/design-bridge/.env.example +5 -0
  5. package/design-bridge/bridge.js +196 -0
  6. package/design-bridge/lib/assets.js +367 -0
  7. package/design-bridge/lib/prompt.js +85 -0
  8. package/design-bridge/lib/server.js +66 -0
  9. package/design-bridge/lib/stitch.js +37 -0
  10. package/design-bridge/lib/tokens.js +82 -0
  11. package/design-bridge/package-lock.json +579 -0
  12. package/design-bridge/package.json +19 -0
  13. package/figma-bridge-plugin/README.md +97 -0
  14. package/figma-bridge-plugin/anthropic-chat-runner.js +192 -0
  15. package/figma-bridge-plugin/bridge-relay.js +2363 -0
  16. package/figma-bridge-plugin/chat-runner.js +459 -0
  17. package/figma-bridge-plugin/code.js +1528 -0
  18. package/figma-bridge-plugin/codex-runner.js +505 -0
  19. package/figma-bridge-plugin/component-schemas.js +110 -0
  20. package/figma-bridge-plugin/content-context.js +869 -0
  21. package/figma-bridge-plugin/create-button.js +216 -0
  22. package/figma-bridge-plugin/gemini-cli-runner.js +291 -0
  23. package/figma-bridge-plugin/gemini-runner.js +187 -0
  24. package/figma-bridge-plugin/html-to-figma.js +927 -0
  25. package/figma-bridge-plugin/knowledge-hub/.gitkeep +0 -0
  26. package/figma-bridge-plugin/knowledge-hub/uspec-references/anatomy-spec.md +159 -0
  27. package/figma-bridge-plugin/knowledge-hub/uspec-references/api-spec.md +162 -0
  28. package/figma-bridge-plugin/knowledge-hub/uspec-references/color-spec.md +148 -0
  29. package/figma-bridge-plugin/knowledge-hub/uspec-references/full-spec-template.md +314 -0
  30. package/figma-bridge-plugin/knowledge-hub/uspec-references/property-spec.md +175 -0
  31. package/figma-bridge-plugin/knowledge-hub/uspec-references/screen-reader-spec.md +180 -0
  32. package/figma-bridge-plugin/knowledge-hub/uspec-references/structure-spec.md +165 -0
  33. package/figma-bridge-plugin/manifest.json +21 -0
  34. package/figma-bridge-plugin/package-lock.json +1936 -0
  35. package/figma-bridge-plugin/package.json +20 -0
  36. package/figma-bridge-plugin/perplexity-runner.js +188 -0
  37. package/figma-bridge-plugin/references/SKILL.md +178 -0
  38. package/figma-bridge-plugin/references/anatomy-spec.md +159 -0
  39. package/figma-bridge-plugin/references/api-spec.md +162 -0
  40. package/figma-bridge-plugin/references/color-spec.md +148 -0
  41. package/figma-bridge-plugin/references/full-spec-template.md +314 -0
  42. package/figma-bridge-plugin/references/property-spec.md +175 -0
  43. package/figma-bridge-plugin/references/screen-reader-spec.md +180 -0
  44. package/figma-bridge-plugin/references/structure-spec.md +165 -0
  45. package/figma-bridge-plugin/shared-prompt-config.js +604 -0
  46. package/figma-bridge-plugin/spec-helpers/build-table.js +269 -0
  47. package/figma-bridge-plugin/spec-helpers/classify-elements.js +189 -0
  48. package/figma-bridge-plugin/spec-helpers/index.js +35 -0
  49. package/figma-bridge-plugin/spec-helpers/parse-figma-link.js +49 -0
  50. package/figma-bridge-plugin/spec-helpers/position-markers.js +158 -0
  51. package/figma-bridge-plugin/stitch-auth.js +322 -0
  52. package/figma-bridge-plugin/stitch-runner.js +1427 -0
  53. package/figma-bridge-plugin/token-resolver.js +107 -0
  54. package/figma-bridge-plugin/ui.html +4467 -0
  55. package/figma-intelligence-layer/.env.example +39 -0
  56. package/figma-intelligence-layer/docs/local-image-generation.md +60 -0
  57. package/figma-intelligence-layer/examples/comfyui-workflow-template.example.json +101 -0
  58. package/figma-intelligence-layer/jest.config.js +14 -0
  59. package/figma-intelligence-layer/mcp-config.json +19 -0
  60. package/figma-intelligence-layer/package-lock.json +5892 -0
  61. package/figma-intelligence-layer/package.json +48 -0
  62. package/figma-intelligence-layer/scripts/setup-comfyui-local.sh +67 -0
  63. package/figma-intelligence-layer/scripts/start-comfyui.sh +33 -0
  64. package/figma-intelligence-layer/src/index.ts +2233 -0
  65. package/figma-intelligence-layer/src/shared/auto-layout-validator.ts +404 -0
  66. package/figma-intelligence-layer/src/shared/cache.ts +187 -0
  67. package/figma-intelligence-layer/src/shared/color-operations.ts +533 -0
  68. package/figma-intelligence-layer/src/shared/color-utils.ts +138 -0
  69. package/figma-intelligence-layer/src/shared/component-script-builder.ts +413 -0
  70. package/figma-intelligence-layer/src/shared/component-templates.ts +2767 -0
  71. package/figma-intelligence-layer/src/shared/concept-taxonomy.ts +694 -0
  72. package/figma-intelligence-layer/src/shared/decision-log.ts +128 -0
  73. package/figma-intelligence-layer/src/shared/design-system-context.ts +568 -0
  74. package/figma-intelligence-layer/src/shared/design-system-intelligence.ts +131 -0
  75. package/figma-intelligence-layer/src/shared/design-system-matcher.ts +184 -0
  76. package/figma-intelligence-layer/src/shared/design-system-normalizers.ts +196 -0
  77. package/figma-intelligence-layer/src/shared/design-system-tokens.ts +295 -0
  78. package/figma-intelligence-layer/src/shared/dtcg-validator.ts +530 -0
  79. package/figma-intelligence-layer/src/shared/enrichment-pipeline.ts +671 -0
  80. package/figma-intelligence-layer/src/shared/figma-bridge.ts +1408 -0
  81. package/figma-intelligence-layer/src/shared/font-config.ts +126 -0
  82. package/figma-intelligence-layer/src/shared/icon-catalog.ts +360 -0
  83. package/figma-intelligence-layer/src/shared/icon-fetch.ts +80 -0
  84. package/figma-intelligence-layer/src/shared/prototype-script-builder.ts +162 -0
  85. package/figma-intelligence-layer/src/shared/response-compression.ts +440 -0
  86. package/figma-intelligence-layer/src/shared/semantic-token-catalog.ts +324 -0
  87. package/figma-intelligence-layer/src/shared/token-binder.ts +505 -0
  88. package/figma-intelligence-layer/src/shared/token-math.ts +427 -0
  89. package/figma-intelligence-layer/src/shared/token-naming.ts +468 -0
  90. package/figma-intelligence-layer/src/shared/token-utils.ts +420 -0
  91. package/figma-intelligence-layer/src/shared/types.ts +346 -0
  92. package/figma-intelligence-layer/src/shared/typography-presets.ts +94 -0
  93. package/figma-intelligence-layer/src/shared/unsplash.ts +165 -0
  94. package/figma-intelligence-layer/src/shared/vision-client.ts +607 -0
  95. package/figma-intelligence-layer/src/shared/vision-provider-anthropic.ts +334 -0
  96. package/figma-intelligence-layer/src/shared/vision-provider-openai.ts +446 -0
  97. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotate-handler.ts +782 -0
  98. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotate-renderer.ts +496 -0
  99. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotation-kit.ts +230 -0
  100. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/colorblind-sim.ts +66 -0
  101. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/index.ts +810 -0
  102. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-analyzer.ts +1191 -0
  103. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-figma-page.ts +1346 -0
  104. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-handler.ts +148 -0
  105. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/vpat-figma-page.ts +499 -0
  106. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/vpat-report.ts +910 -0
  107. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/wcag-checker.ts +989 -0
  108. package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/wcag-criteria.ts +1160 -0
  109. package/figma-intelligence-layer/src/tools/phase1-vision/design-from-ref/index.ts +424 -0
  110. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/component-recognizer.ts +38 -0
  111. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/ds-matcher.ts +111 -0
  112. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/font-matcher.ts +114 -0
  113. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/icon-resolver.ts +103 -0
  114. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/index.ts +1060 -0
  115. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/layout-segmenter.ts +18 -0
  116. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/token-inferencer.ts +39 -0
  117. package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/vision-pipeline.ts +58 -0
  118. package/figma-intelligence-layer/src/tools/phase1-vision/sketch-to-design/index.ts +298 -0
  119. package/figma-intelligence-layer/src/tools/phase1-vision/visual-audit/index.ts +197 -0
  120. package/figma-intelligence-layer/src/tools/phase2-accuracy/component-audit/index.ts +494 -0
  121. package/figma-intelligence-layer/src/tools/phase2-accuracy/intent-translator/index.ts +356 -0
  122. package/figma-intelligence-layer/src/tools/phase2-accuracy/layout-intelligence/container-patterns.ts +123 -0
  123. package/figma-intelligence-layer/src/tools/phase2-accuracy/layout-intelligence/index.ts +663 -0
  124. package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/built-in-rules.yaml +56 -0
  125. package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/index.ts +614 -0
  126. package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/rule-engine.ts +113 -0
  127. package/figma-intelligence-layer/src/tools/phase2-accuracy/theme-generator/color-theory.ts +178 -0
  128. package/figma-intelligence-layer/src/tools/phase2-accuracy/theme-generator/index.ts +470 -0
  129. package/figma-intelligence-layer/src/tools/phase2-accuracy/variant-expander/index.ts +429 -0
  130. package/figma-intelligence-layer/src/tools/phase2-accuracy/variant-expander/token-override-maps.ts +226 -0
  131. package/figma-intelligence-layer/src/tools/phase3-generation/ai-image-insert/index.ts +535 -0
  132. package/figma-intelligence-layer/src/tools/phase3-generation/component-archaeologist/index.ts +660 -0
  133. package/figma-intelligence-layer/src/tools/phase3-generation/component-archaeologist/pattern-fingerprints.ts +209 -0
  134. package/figma-intelligence-layer/src/tools/phase3-generation/composition-builder/index.ts +540 -0
  135. package/figma-intelligence-layer/src/tools/phase3-generation/figma-animated-build.ts +391 -0
  136. package/figma-intelligence-layer/src/tools/phase3-generation/page-architect/index.ts +2019 -0
  137. package/figma-intelligence-layer/src/tools/phase3-generation/page-architect/screen-templates.ts +131 -0
  138. package/figma-intelligence-layer/src/tools/phase3-generation/prototype-map/index.ts +381 -0
  139. package/figma-intelligence-layer/src/tools/phase3-generation/prototype-wire/index.ts +565 -0
  140. package/figma-intelligence-layer/src/tools/phase3-generation/swarm-build/index.ts +764 -0
  141. package/figma-intelligence-layer/src/tools/phase3-generation/system-drift/index.ts +535 -0
  142. package/figma-intelligence-layer/src/tools/phase3-generation/unsplash-search/index.ts +84 -0
  143. package/figma-intelligence-layer/src/tools/phase3-generation/url-to-frame/index.ts +401 -0
  144. package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/css-animations.ts +68 -0
  145. package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/framer-motion.ts +78 -0
  146. package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/swift-animations.ts +93 -0
  147. package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/index.ts +596 -0
  148. package/figma-intelligence-layer/src/tools/phase4-sync/ci-check/index.ts +462 -0
  149. package/figma-intelligence-layer/src/tools/phase4-sync/export-tokens/index.ts +1470 -0
  150. package/figma-intelligence-layer/src/tools/phase4-sync/generate-component-code/index.ts +829 -0
  151. package/figma-intelligence-layer/src/tools/phase4-sync/handoff-spec/index.ts +702 -0
  152. package/figma-intelligence-layer/src/tools/phase4-sync/icon-library-sync/index.ts +483 -0
  153. package/figma-intelligence-layer/src/tools/phase4-sync/sync-from-code/index.ts +501 -0
  154. package/figma-intelligence-layer/src/tools/phase4-sync/sync-from-code/storybook-parser.ts +106 -0
  155. package/figma-intelligence-layer/src/tools/phase4-sync/watch-docs/index.ts +676 -0
  156. package/figma-intelligence-layer/src/tools/phase4-sync/webhook-listener/index.ts +560 -0
  157. package/figma-intelligence-layer/src/tools/phase5-governance/apg-doc/index.ts +1043 -0
  158. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/component-detection.ts +620 -0
  159. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/anatomy.ts +331 -0
  160. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/color-tokens.ts +77 -0
  161. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/properties.ts +54 -0
  162. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/snapshot.ts +287 -0
  163. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/spacing.ts +71 -0
  164. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/states.ts +43 -0
  165. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/typography.ts +71 -0
  166. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/index.ts +221 -0
  167. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/_default.ts +166 -0
  168. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/accordion.ts +232 -0
  169. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/alert.ts +234 -0
  170. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/avatar-group.ts +270 -0
  171. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/avatar.ts +249 -0
  172. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/badge.ts +231 -0
  173. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/banner.ts +293 -0
  174. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/breadcrumb.ts +240 -0
  175. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/button.ts +243 -0
  176. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/calendar.ts +307 -0
  177. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/card.ts +143 -0
  178. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/checkbox.ts +227 -0
  179. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/chip.ts +233 -0
  180. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/combobox.ts +282 -0
  181. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/datepicker.ts +276 -0
  182. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/divider.ts +223 -0
  183. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/drawer.ts +255 -0
  184. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/dropdown-menu.ts +289 -0
  185. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/empty-state.ts +261 -0
  186. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/file-uploader.ts +290 -0
  187. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/form.ts +265 -0
  188. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/grid.ts +238 -0
  189. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/icon.ts +255 -0
  190. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/index.ts +128 -0
  191. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/inline-edit.ts +286 -0
  192. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/inline-message.ts +255 -0
  193. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/input.ts +330 -0
  194. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/link.ts +247 -0
  195. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/list.ts +250 -0
  196. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/menu.ts +247 -0
  197. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/modal.ts +144 -0
  198. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/navbar.ts +264 -0
  199. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/navigation.ts +251 -0
  200. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/number-input.ts +261 -0
  201. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/pagination.ts +248 -0
  202. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/popover.ts +270 -0
  203. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/progress.ts +251 -0
  204. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/radio.ts +142 -0
  205. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/range-slider.ts +282 -0
  206. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/rating.ts +250 -0
  207. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/search.ts +258 -0
  208. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/segmented-control.ts +265 -0
  209. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/select.ts +319 -0
  210. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/skeleton.ts +256 -0
  211. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/slider.ts +232 -0
  212. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/spinner.ts +239 -0
  213. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/status-dot.ts +252 -0
  214. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/stepper.ts +270 -0
  215. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/table.ts +244 -0
  216. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tabs.ts +143 -0
  217. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tag.ts +243 -0
  218. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/textarea.ts +259 -0
  219. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/time-picker.ts +293 -0
  220. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toast.ts +144 -0
  221. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toggle.ts +289 -0
  222. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toolbar.ts +267 -0
  223. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tooltip.ts +232 -0
  224. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/treeview.ts +257 -0
  225. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/typography.ts +319 -0
  226. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/legacy-compat.ts +121 -0
  227. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/anatomy-diagram.ts +430 -0
  228. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/figma-page.ts +312 -0
  229. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/json.ts +129 -0
  230. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/markdown.ts +78 -0
  231. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/visual-doc.ts +2333 -0
  232. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/accessibility.ts +100 -0
  233. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/anatomy.ts +32 -0
  234. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/color-tokens.ts +59 -0
  235. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/content-guidance.ts +18 -0
  236. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/design-tokens.ts +53 -0
  237. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/interaction-rules.ts +19 -0
  238. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/overview.ts +91 -0
  239. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/properties-api.ts +71 -0
  240. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/qa-criteria.ts +19 -0
  241. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/related-components.ts +110 -0
  242. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/responsive.ts +19 -0
  243. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/size-specs.ts +67 -0
  244. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/spacing-structure.ts +58 -0
  245. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/state-specs.ts +79 -0
  246. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/states.ts +50 -0
  247. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/type-hierarchy.ts +33 -0
  248. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/typography.ts +55 -0
  249. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/usage-guidelines.ts +73 -0
  250. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/variants.ts +81 -0
  251. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/types.ts +409 -0
  252. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/index.ts +198 -0
  253. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/renderer.ts +701 -0
  254. package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/types.ts +88 -0
  255. package/figma-intelligence-layer/src/tools/phase5-governance/decision-log/index.ts +135 -0
  256. package/figma-intelligence-layer/src/tools/phase5-governance/design-decision-log/index.ts +491 -0
  257. package/figma-intelligence-layer/src/tools/phase5-governance/ds-primitives/index.ts +416 -0
  258. package/figma-intelligence-layer/src/tools/phase5-governance/ds-scaffolder/index.ts +722 -0
  259. package/figma-intelligence-layer/src/tools/phase5-governance/ds-variables/index.ts +449 -0
  260. package/figma-intelligence-layer/src/tools/phase5-governance/health-report/index.ts +393 -0
  261. package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/index.ts +406 -0
  262. package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/figma-page.ts +292 -0
  263. package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/json.ts +24 -0
  264. package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/markdown.ts +172 -0
  265. package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/naming-guide.ts +409 -0
  266. package/figma-intelligence-layer/src/tools/phase5-governance/token-analytics/index.ts +594 -0
  267. package/figma-intelligence-layer/src/tools/phase5-governance/token-docs/index.ts +710 -0
  268. package/figma-intelligence-layer/src/tools/phase5-governance/token-migrate/index.ts +458 -0
  269. package/figma-intelligence-layer/src/tools/phase5-governance/token-naming/index.ts +134 -0
  270. package/figma-intelligence-layer/tests/apg-doc.test.ts +101 -0
  271. package/figma-intelligence-layer/tests/design-system-context.test.ts +152 -0
  272. package/figma-intelligence-layer/tests/design-system-matcher.test.ts +144 -0
  273. package/figma-intelligence-layer/tests/figma-bridge.test.ts +83 -0
  274. package/figma-intelligence-layer/tests/generate-image-and-insert.test.ts +56 -0
  275. package/figma-intelligence-layer/tests/screen-cloner-regression.test.ts +69 -0
  276. package/figma-intelligence-layer/tests/smoke.test.ts +174 -0
  277. package/figma-intelligence-layer/tests/spec-generator.test.ts +127 -0
  278. package/figma-intelligence-layer/tests/token-migrate.test.ts +21 -0
  279. package/figma-intelligence-layer/tests/token-naming.test.ts +30 -0
  280. package/figma-intelligence-layer/tsconfig.json +19 -0
  281. package/package.json +35 -0
  282. package/scripts/clean-existing-chunks.js +179 -0
  283. package/scripts/connect-ai-tool.js +490 -0
  284. package/scripts/convert-hub-pdfs.js +425 -0
  285. package/scripts/figma-mcp-status.js +349 -0
  286. package/scripts/register-codex-mcp.js +96 -0
@@ -0,0 +1,406 @@
1
+ /**
2
+ * figma_taxonomy_docs — Living concept taxonomy documentation
3
+ *
4
+ * Generates documentation for the semantic token naming convention by
5
+ * cross-referencing the concept taxonomy with actual Figma variables.
6
+ * Supports markdown, JSON, and Figma page output formats.
7
+ *
8
+ * The Figma page output supports auto-sync: when variables change in the
9
+ * file, the documentation page updates automatically.
10
+ */
11
+
12
+ import { getBridge } from "../../../shared/figma-bridge.js";
13
+ import {
14
+ CONCEPT_TAXONOMY,
15
+ GRAMMAR,
16
+ getConceptById,
17
+ getRequiredTokenPaths,
18
+ getAllTokenPaths,
19
+ type ConceptDefinition,
20
+ } from "../../../shared/concept-taxonomy.js";
21
+ import { renderMarkdown } from "./renderers/markdown.js";
22
+ import { renderJson } from "./renderers/json.js";
23
+ import { renderFigmaPage } from "./renderers/figma-page.js";
24
+ import { renderNamingGuide } from "./renderers/naming-guide.js";
25
+
26
+ // ─── Types ──────────────────────────────────────────────────────────────────
27
+
28
+ export interface TaxonomyDocsArgs {
29
+ outputFormat: "markdown" | "json" | "figma" | "naming-guide";
30
+ concepts?: string[];
31
+ includeTokenAnatomy?: boolean;
32
+ showCoverage?: boolean;
33
+ autoSync?: boolean;
34
+ pageName?: string;
35
+ }
36
+
37
+ export interface TaxonomyDocsResult {
38
+ ok: boolean;
39
+ format: TaxonomyDocsArgs["outputFormat"];
40
+ conceptCount: number;
41
+ tokenCount: number;
42
+ output: string | TaxonomyDocsJsonOutput;
43
+ coverage: ConceptCoverage[];
44
+ notes: string[];
45
+ autoSyncEnabled?: boolean;
46
+ pageId?: string;
47
+ }
48
+
49
+ export interface ConceptCoverage {
50
+ conceptId: string;
51
+ conceptName: string;
52
+ purpose: string;
53
+ status: "full" | "partial" | "missing";
54
+ expectedTokens: string[];
55
+ foundTokens: string[];
56
+ missingTokens: string[];
57
+ /** Maps expected token path → actual file token name that matched (for fuzzy matches) */
58
+ matchedFileTokens: Record<string, string>;
59
+ typicalForms: string[];
60
+ validStates: string[];
61
+ validVariants: string[];
62
+ }
63
+
64
+ export interface TaxonomyDocsJsonOutput {
65
+ generatedAt: string;
66
+ grammar: typeof GRAMMAR;
67
+ concepts: ConceptCoverage[];
68
+ totalExpected: number;
69
+ totalFound: number;
70
+ totalMissing: number;
71
+ tokenValues: Record<string, TokenValueSnapshot>;
72
+ }
73
+
74
+ export interface TokenValueSnapshot {
75
+ name: string;
76
+ type: string;
77
+ collection: string;
78
+ values: Record<string, unknown>;
79
+ }
80
+
81
+ // ─── Core Logic ─────────────────────────────────────────────────────────────
82
+
83
+ interface RawVariable {
84
+ id: string;
85
+ name: string;
86
+ type: string;
87
+ resolvedType?: string;
88
+ description?: string;
89
+ valuesByMode?: Record<string, unknown>;
90
+ }
91
+
92
+ interface RawCollection {
93
+ id: string;
94
+ name: string;
95
+ modes: Array<{ modeId: string; name: string }>;
96
+ variables: RawVariable[];
97
+ }
98
+
99
+ /**
100
+ * Build a set of all token names present in the Figma file.
101
+ * Also builds a map of name → snapshot for value rendering.
102
+ */
103
+ function indexFileTokens(collections: RawCollection[]): {
104
+ tokenNames: Set<string>;
105
+ snapshots: Record<string, TokenValueSnapshot>;
106
+ } {
107
+ const tokenNames = new Set<string>();
108
+ const snapshots: Record<string, TokenValueSnapshot> = {};
109
+
110
+ for (const coll of collections) {
111
+ for (const v of coll.variables) {
112
+ const name = v.name;
113
+ tokenNames.add(name);
114
+
115
+ // Also index with common prefix variations for fuzzy matching
116
+ const segments = name.split("/");
117
+ if (segments.length > 2) {
118
+ // Allow suffix matching: "actions/primary/bg/default" matches "color/semantic/actions/primary/bg/default"
119
+ tokenNames.add(segments.slice(1).join("/"));
120
+ tokenNames.add(segments.slice(2).join("/"));
121
+ }
122
+
123
+ snapshots[name] = {
124
+ name,
125
+ type: v.resolvedType ?? v.type,
126
+ collection: coll.name,
127
+ values: v.valuesByMode ?? {},
128
+ };
129
+ }
130
+ }
131
+
132
+ return { tokenNames, snapshots };
133
+ }
134
+
135
+ /**
136
+ * Check which expected tokens exist for each concept.
137
+ */
138
+ function computeCoverage(
139
+ concepts: ConceptDefinition[],
140
+ tokenNames: Set<string>,
141
+ ): ConceptCoverage[] {
142
+ return concepts.map((concept) => {
143
+ const allPaths = getAllTokenPaths(concept.id);
144
+ const requiredPaths = getRequiredTokenPaths(concept.id);
145
+
146
+ const foundTokens: string[] = [];
147
+ const missingTokens: string[] = [];
148
+ const matchedFileTokens: Record<string, string> = {};
149
+
150
+ for (const path of allPaths) {
151
+ const match = tokenNameMatch(path, tokenNames);
152
+ if (match) {
153
+ foundTokens.push(path);
154
+ if (match !== path) {
155
+ matchedFileTokens[path] = match;
156
+ }
157
+ } else {
158
+ missingTokens.push(path);
159
+ }
160
+ }
161
+
162
+ // Coverage status based on required tokens
163
+ const requiredFound = requiredPaths.filter((p) => tokenNameMatch(p, tokenNames) !== null);
164
+ let status: ConceptCoverage["status"];
165
+ if (requiredFound.length === requiredPaths.length) {
166
+ status = "full";
167
+ } else if (requiredFound.length > 0 || foundTokens.length > 0) {
168
+ status = "partial";
169
+ } else {
170
+ status = "missing";
171
+ }
172
+
173
+ return {
174
+ conceptId: concept.id,
175
+ conceptName: concept.id.charAt(0).toUpperCase() + concept.id.slice(1),
176
+ purpose: concept.purpose,
177
+ status,
178
+ expectedTokens: allPaths,
179
+ foundTokens,
180
+ missingTokens,
181
+ matchedFileTokens,
182
+ typicalForms: concept.typicalForms,
183
+ validStates: concept.validStates,
184
+ validVariants: concept.validVariants,
185
+ };
186
+ });
187
+ }
188
+
189
+ // ─── Semantic keyword synonyms for fuzzy role matching ────────────────────
190
+ // Maps each semantic keyword to a set of equivalent terms found in real
191
+ // design-system token names (Carbon, MUI, Ant, Fluent, Polaris, etc.).
192
+
193
+ const KEYWORD_SYNONYMS: Record<string, string[]> = {
194
+ // Concept synonyms
195
+ actions: ["action", "actions", "btn", "button", "cta", "link"],
196
+ field: ["field", "input", "text-input", "textfield", "form", "select", "textarea"],
197
+ surface: ["surface", "card", "panel", "container", "paper", "well", "layer", "bg"],
198
+ navigation: ["nav", "navigation", "tab", "tabs", "breadcrumb", "sidebar", "appbar", "navbar"],
199
+ feedback: ["feedback", "alert", "toast", "banner", "snackbar", "notification", "callout", "message"],
200
+ // Property synonyms
201
+ bg: ["bg", "background", "fill", "surface", "base"],
202
+ text: ["text", "label", "foreground", "fg", "content", "on"],
203
+ icon: ["icon", "glyph", "symbol"],
204
+ border: ["border", "stroke", "outline", "divider", "separator", "edge"],
205
+ ring: ["ring", "focus-ring", "outline", "focus"],
206
+ shadow: ["shadow", "elevation", "drop-shadow", "box-shadow"],
207
+ inset: ["inset", "padding", "pad", "spacing", "inner"],
208
+ gap: ["gap", "gutter", "stack", "spacing"],
209
+ // State synonyms
210
+ hover: ["hover", "hovered", "over"],
211
+ active: ["active", "pressed", "down", "tap"],
212
+ focus: ["focus", "focused", "keyboard"],
213
+ disabled: ["disabled", "inactive", "muted", "dimmed"],
214
+ // Variant synonyms
215
+ primary: ["primary", "main", "brand", "accent", "key"],
216
+ secondary: ["secondary", "subtle", "alt", "alternative"],
217
+ ghost: ["ghost", "text", "plain", "minimal", "bare", "link"],
218
+ destructive:["destructive", "danger", "error", "critical", "negative", "red"],
219
+ };
220
+
221
+ /**
222
+ * Extract semantic keywords from a token path.
223
+ * E.g. "color/semantic/actions/primary/bg/default" → ["color","semantic","actions","primary","bg","default"]
224
+ */
225
+ function extractKeywords(path: string): string[] {
226
+ return path
227
+ .toLowerCase()
228
+ .split(/[\/\-_.]+/)
229
+ .filter(Boolean);
230
+ }
231
+
232
+ /**
233
+ * Check if a candidate token matches the semantic role described by `path`
234
+ * by checking whether the candidate contains synonyms for the key segments.
235
+ */
236
+ function semanticKeywordMatch(expectedPath: string, candidateName: string): boolean {
237
+ const expectedKw = extractKeywords(expectedPath);
238
+ const candidateNorm = candidateName.toLowerCase().replace(/[\/\-_.]+/g, " ");
239
+
240
+ // Identify the "meaningful" keywords from the expected path —
241
+ // skip structural words like "semantic", "default", "control"
242
+ const STRUCTURAL = new Set(["semantic", "default", "control", "md", "sm", "lg"]);
243
+ const meaningfulKw = expectedKw.filter((kw) => !STRUCTURAL.has(kw));
244
+
245
+ if (meaningfulKw.length === 0) return false;
246
+
247
+ // For each meaningful keyword, check if the candidate contains
248
+ // that keyword or any of its synonyms
249
+ let matchCount = 0;
250
+ for (const kw of meaningfulKw) {
251
+ const synonyms = KEYWORD_SYNONYMS[kw] ?? [kw];
252
+ const allTerms = [kw, ...synonyms];
253
+ if (allTerms.some((term) => candidateNorm.includes(term))) {
254
+ matchCount++;
255
+ }
256
+ }
257
+
258
+ // Require at least 2 meaningful keyword matches (or all if fewer than 2)
259
+ const threshold = Math.min(2, meaningfulKw.length);
260
+ return matchCount >= threshold;
261
+ }
262
+
263
+ /**
264
+ * Check if a token name exists in the file tokens.
265
+ * Uses three strategies in order:
266
+ * 1. Exact match
267
+ * 2. Suffix / normalized match
268
+ * 3. Semantic keyword match (catches tokens named differently but serving the same role)
269
+ *
270
+ * Returns the matched file token name, or null if no match.
271
+ */
272
+ function tokenNameMatch(path: string, tokenNames: Set<string>): string | null {
273
+ // 1. Exact match
274
+ if (tokenNames.has(path)) return path;
275
+
276
+ // 2. Suffix match — some files prefix with collection name
277
+ for (const existing of tokenNames) {
278
+ if (existing.endsWith(`/${path}`) || existing.endsWith(path)) return existing;
279
+ }
280
+
281
+ // 3. Normalized comparison
282
+ const normalized = path.toLowerCase().replace(/[\/\-_]/g, "");
283
+ for (const existing of tokenNames) {
284
+ const n = existing.toLowerCase().replace(/[\/\-_]/g, "");
285
+ if (n === normalized || n.endsWith(normalized)) return existing;
286
+ }
287
+
288
+ // 4. Semantic keyword matching — find a token that semantically serves the same role
289
+ for (const existing of tokenNames) {
290
+ if (semanticKeywordMatch(path, existing)) return existing;
291
+ }
292
+
293
+ return null;
294
+ }
295
+
296
+ /**
297
+ * Check if a token name exists in the file tokens, with fuzzy matching.
298
+ */
299
+ function tokenNameExists(path: string, tokenNames: Set<string>): boolean {
300
+ return tokenNameMatch(path, tokenNames) !== null;
301
+ }
302
+
303
+ // ─── Handler ────────────────────────────────────────────────────────────────
304
+
305
+ export async function taxonomyDocsHandler(args: TaxonomyDocsArgs): Promise<TaxonomyDocsResult> {
306
+ const bridge = await getBridge();
307
+ const notes: string[] = [];
308
+
309
+ // Fetch actual tokens from the Figma file
310
+ let collections: RawCollection[] = [];
311
+ try {
312
+ collections = (await bridge.getVariables(undefined, "full")) as unknown as RawCollection[];
313
+ } catch {
314
+ notes.push("Could not fetch Figma variables — coverage report will show all tokens as missing.");
315
+ }
316
+
317
+ const { tokenNames, snapshots } = indexFileTokens(collections);
318
+ notes.push(`Indexed ${tokenNames.size} token names from ${collections.length} collections.`);
319
+
320
+ // Filter concepts if requested
321
+ let concepts = CONCEPT_TAXONOMY;
322
+ if (args.concepts?.length) {
323
+ const filter = new Set(args.concepts.map((c) => c.toLowerCase()));
324
+ concepts = concepts.filter((c) => filter.has(c.id));
325
+ if (concepts.length === 0) {
326
+ return {
327
+ ok: false,
328
+ format: args.outputFormat,
329
+ conceptCount: 0,
330
+ tokenCount: 0,
331
+ output: "No matching concepts found. Available: " + CONCEPT_TAXONOMY.map((c) => c.id).join(", "),
332
+ coverage: [],
333
+ notes: ["Filter matched 0 concepts."],
334
+ };
335
+ }
336
+ }
337
+
338
+ // Compute coverage
339
+ const coverage = computeCoverage(concepts, tokenNames);
340
+ const totalExpected = coverage.reduce((sum, c) => sum + c.expectedTokens.length, 0);
341
+ const totalFound = coverage.reduce((sum, c) => sum + c.foundTokens.length, 0);
342
+
343
+ // Render based on format
344
+ let output: string | TaxonomyDocsJsonOutput;
345
+ let pageId: string | undefined;
346
+ let autoSyncEnabled = false;
347
+
348
+ switch (args.outputFormat) {
349
+ case "markdown":
350
+ output = renderMarkdown(coverage, snapshots, {
351
+ includeTokenAnatomy: args.includeTokenAnatomy ?? true,
352
+ showCoverage: args.showCoverage ?? true,
353
+ });
354
+ break;
355
+
356
+ case "json":
357
+ output = renderJson(coverage, snapshots, totalExpected, totalFound);
358
+ break;
359
+
360
+ case "naming-guide":
361
+ output = renderNamingGuide(coverage);
362
+ break;
363
+
364
+ case "figma": {
365
+ const figmaOpts = {
366
+ pageName: args.pageName ?? "Token Taxonomy",
367
+ includeTokenAnatomy: args.includeTokenAnatomy ?? true,
368
+ showCoverage: args.showCoverage ?? true,
369
+ autoSync: args.autoSync ?? false,
370
+ };
371
+ const result = await renderFigmaPage(bridge, coverage, snapshots, figmaOpts);
372
+ pageId = result.pageId;
373
+ autoSyncEnabled = result.autoSyncEnabled;
374
+ output = `Figma page "${figmaOpts.pageName}" created with ${concepts.length} concepts. Page ID: ${pageId}`;
375
+
376
+ // Register auto-sync handler on the bridge
377
+ if (autoSyncEnabled && "setTaxonomyAutoSyncHandler" in bridge) {
378
+ const syncBridge = bridge as unknown as { setTaxonomyAutoSyncHandler: (h: (() => Promise<void>) | null) => void };
379
+ syncBridge.setTaxonomyAutoSyncHandler(async () => {
380
+ // Re-fetch tokens, recompute coverage, re-render page
381
+ let freshCollections: RawCollection[] = [];
382
+ try {
383
+ freshCollections = (await bridge.getVariables(undefined, "full")) as unknown as RawCollection[];
384
+ } catch { /* ignore — page will show all missing */ }
385
+ const fresh = indexFileTokens(freshCollections);
386
+ const freshCoverage = computeCoverage(concepts, fresh.tokenNames);
387
+ await renderFigmaPage(bridge, freshCoverage, fresh.snapshots, figmaOpts);
388
+ });
389
+ notes.push("Auto-sync enabled: documentation will update when variables change.");
390
+ }
391
+ break;
392
+ }
393
+ }
394
+
395
+ return {
396
+ ok: true,
397
+ format: args.outputFormat,
398
+ conceptCount: concepts.length,
399
+ tokenCount: totalFound,
400
+ output,
401
+ coverage,
402
+ notes,
403
+ autoSyncEnabled,
404
+ pageId,
405
+ };
406
+ }
@@ -0,0 +1,292 @@
1
+ /**
2
+ * Figma page renderer for taxonomy documentation.
3
+ * Creates a visual documentation page with concept tables, token anatomy,
4
+ * coverage badges, and color swatches — all from live Figma data.
5
+ *
6
+ * Supports auto-sync: stores the page name so the variable-change event
7
+ * handler can trigger a lightweight re-render.
8
+ */
9
+
10
+ import type { ConceptCoverage, TokenValueSnapshot } from "../index.js";
11
+
12
+ interface FigmaPageOptions {
13
+ pageName: string;
14
+ includeTokenAnatomy: boolean;
15
+ showCoverage: boolean;
16
+ autoSync: boolean;
17
+ }
18
+
19
+ interface FigmaPageResult {
20
+ pageId: string;
21
+ autoSyncEnabled: boolean;
22
+ }
23
+
24
+ // Module-level state for auto-sync
25
+ let autoSyncPageName: string | null = null;
26
+
27
+ export function getAutoSyncPageName(): string | null {
28
+ return autoSyncPageName;
29
+ }
30
+
31
+ export function clearAutoSync(): void {
32
+ autoSyncPageName = null;
33
+ }
34
+
35
+ function escapeJs(s: string): string {
36
+ return JSON.stringify(s);
37
+ }
38
+
39
+ function statusColor(status: ConceptCoverage["status"]): string {
40
+ if (status === "full") return "{ r: 0.13, g: 0.73, b: 0.33 }";
41
+ if (status === "partial") return "{ r: 0.95, g: 0.77, b: 0.06 }";
42
+ return "{ r: 0.85, g: 0.12, b: 0.12 }";
43
+ }
44
+
45
+ function statusLabel(status: ConceptCoverage["status"]): string {
46
+ if (status === "full") return "Full";
47
+ if (status === "partial") return "Partial";
48
+ return "Missing";
49
+ }
50
+
51
+ export async function renderFigmaPage(
52
+ bridge: { execute: (code: string) => Promise<{ success: boolean; result?: unknown }> },
53
+ coverage: ConceptCoverage[],
54
+ snapshots: Record<string, TokenValueSnapshot>,
55
+ options: FigmaPageOptions,
56
+ ): Promise<FigmaPageResult> {
57
+ const pageName = options.pageName;
58
+
59
+ // Track for auto-sync
60
+ if (options.autoSync) {
61
+ autoSyncPageName = pageName;
62
+ }
63
+
64
+ const lines: string[] = [];
65
+ lines.push(`(async () => {`);
66
+
67
+ // ── Create or find page ──
68
+ lines.push(` let page = figma.root.children.find(p => p.name === ${escapeJs(pageName)});`);
69
+ lines.push(` if (!page) {`);
70
+ lines.push(` page = figma.createPage();`);
71
+ lines.push(` page.name = ${escapeJs(pageName)};`);
72
+ lines.push(` }`);
73
+ lines.push(` figma.currentPage = page;`);
74
+ lines.push(``);
75
+
76
+ // Clear existing content
77
+ lines.push(` for (const child of [...page.children]) { child.remove(); }`);
78
+ lines.push(``);
79
+
80
+ // ── Layout constants ──
81
+ lines.push(` const COL_W = 260;`);
82
+ lines.push(` const ROW_H = 36;`);
83
+ lines.push(` const SECTION_GAP = 60;`);
84
+ lines.push(` const HEADER_H = 48;`);
85
+ lines.push(` let cursorY = 0;`);
86
+ lines.push(``);
87
+
88
+ // ── Helper: create text node ──
89
+ lines.push(` async function txt(x, y, text, size, bold, color) {`);
90
+ lines.push(` const t = figma.createText();`);
91
+ lines.push(` await figma.loadFontAsync({ family: "Inter", style: bold ? "Bold" : "Regular" });`);
92
+ lines.push(` t.fontName = { family: "Inter", style: bold ? "Bold" : "Regular" };`);
93
+ lines.push(` t.fontSize = size;`);
94
+ lines.push(` t.characters = String(text);`);
95
+ lines.push(` t.x = x;`);
96
+ lines.push(` t.y = y;`);
97
+ lines.push(` if (color) t.fills = [{ type: "SOLID", color: color }];`);
98
+ lines.push(` page.appendChild(t);`);
99
+ lines.push(` return t;`);
100
+ lines.push(` }`);
101
+ lines.push(``);
102
+
103
+ // ── Helper: create rectangle ──
104
+ lines.push(` function rect(x, y, w, h, color, radius) {`);
105
+ lines.push(` const r = figma.createRectangle();`);
106
+ lines.push(` r.x = x; r.y = y; r.resize(w, h);`);
107
+ lines.push(` r.fills = [{ type: "SOLID", color: color }];`);
108
+ lines.push(` if (radius) r.cornerRadius = radius;`);
109
+ lines.push(` page.appendChild(r);`);
110
+ lines.push(` return r;`);
111
+ lines.push(` }`);
112
+ lines.push(``);
113
+
114
+ // ── Title Section ──
115
+ lines.push(` await txt(0, cursorY, "Token Taxonomy", 32, true);`);
116
+ lines.push(` cursorY += 44;`);
117
+ lines.push(` await txt(0, cursorY, "Generated ${new Date().toISOString().split("T")[0]} — re-run figma_taxonomy_docs to refresh", 14, false, { r: 0.5, g: 0.5, b: 0.5 });`);
118
+ lines.push(` cursorY += 32;`);
119
+ lines.push(``);
120
+
121
+ // ── Naming Grammar Section ──
122
+ lines.push(` await txt(0, cursorY, "Naming Grammar", 24, true);`);
123
+ lines.push(` cursorY += 36;`);
124
+ lines.push(` rect(0, cursorY, 800, 70, { r: 0.96, g: 0.96, b: 0.97 }, 8);`);
125
+ lines.push(` await txt(16, cursorY + 12, "Semantic: <category>/<concept>/[<variant>]/<property>/[<state>]", 14, false, { r: 0.1, g: 0.1, b: 0.1 });`);
126
+ lines.push(` await txt(16, cursorY + 36, "Component: component/<component>/[<part>]/[<variant>]/<category>/<property>/[<state>]", 14, false, { r: 0.1, g: 0.1, b: 0.1 });`);
127
+ lines.push(` cursorY += 70 + SECTION_GAP;`);
128
+ lines.push(``);
129
+
130
+ // ── Coverage Summary ──
131
+ if (options.showCoverage) {
132
+ const full = coverage.filter((c) => c.status === "full").length;
133
+ const partial = coverage.filter((c) => c.status === "partial").length;
134
+ const missing = coverage.filter((c) => c.status === "missing").length;
135
+
136
+ lines.push(` await txt(0, cursorY, "Coverage Summary", 24, true);`);
137
+ lines.push(` cursorY += 36;`);
138
+
139
+ // Status badges
140
+ lines.push(` rect(0, cursorY, 120, 40, { r: 0.13, g: 0.73, b: 0.33 }, 8);`);
141
+ lines.push(` await txt(12, cursorY + 10, "Full: ${full}", 16, true, { r: 1, g: 1, b: 1 });`);
142
+
143
+ lines.push(` rect(136, cursorY, 120, 40, { r: 0.95, g: 0.77, b: 0.06 }, 8);`);
144
+ lines.push(` await txt(148, cursorY + 10, "Partial: ${partial}", 16, true, { r: 0.1, g: 0.1, b: 0.1 });`);
145
+
146
+ lines.push(` rect(272, cursorY, 120, 40, { r: 0.85, g: 0.12, b: 0.12 }, 8);`);
147
+ lines.push(` await txt(284, cursorY + 10, "Missing: ${missing}", 16, true, { r: 1, g: 1, b: 1 });`);
148
+
149
+ lines.push(` cursorY += 40 + SECTION_GAP;`);
150
+ lines.push(``);
151
+ }
152
+
153
+ // ── Concept Overview Table ──
154
+ lines.push(` await txt(0, cursorY, "Concept Taxonomy", 24, true);`);
155
+ lines.push(` cursorY += 36;`);
156
+
157
+ // Table header
158
+ const cols = [
159
+ { label: "Concept", x: 0, w: 120 },
160
+ { label: "Purpose", x: 130, w: 300 },
161
+ { label: "Forms", x: 440, w: 200 },
162
+ { label: "States", x: 650, w: 200 },
163
+ { label: "Coverage", x: 860, w: 100 },
164
+ ];
165
+
166
+ lines.push(` rect(0, cursorY, 960, 36, { r: 0.94, g: 0.94, b: 0.96 }, 4);`);
167
+ for (const col of cols) {
168
+ lines.push(` await txt(${col.x + 8}, cursorY + 9, ${escapeJs(col.label)}, 13, true);`);
169
+ }
170
+ lines.push(` cursorY += 36;`);
171
+
172
+ // Table rows
173
+ for (const c of coverage) {
174
+ const forms = c.typicalForms.slice(0, 3).join(", ") + (c.typicalForms.length > 3 ? "..." : "");
175
+ const states = c.validStates.slice(0, 4).join(", ") + (c.validStates.length > 4 ? "..." : "");
176
+ const covLabel = `${statusLabel(c.status)} ${c.foundTokens.length}/${c.expectedTokens.length}`;
177
+
178
+ // Alternating row background
179
+ lines.push(` rect(0, cursorY, 960, 36, { r: 0.99, g: 0.99, b: 1.0 }, 0);`);
180
+ lines.push(` await txt(8, cursorY + 9, ${escapeJs(c.conceptName)}, 12, true);`);
181
+ lines.push(` await txt(138, cursorY + 9, ${escapeJs(c.purpose.slice(0, 50) + (c.purpose.length > 50 ? "..." : ""))}, 11, false);`);
182
+ lines.push(` await txt(448, cursorY + 9, ${escapeJs(forms)}, 11, false);`);
183
+ lines.push(` await txt(658, cursorY + 9, ${escapeJs(states)}, 11, false);`);
184
+
185
+ // Coverage badge
186
+ lines.push(` rect(868, cursorY + 6, 84, 24, ${statusColor(c.status)}, 12);`);
187
+ lines.push(` await txt(878, cursorY + 10, ${escapeJs(covLabel)}, 10, true, { r: 1, g: 1, b: 1 });`);
188
+
189
+ lines.push(` cursorY += 36;`);
190
+ }
191
+
192
+ lines.push(` cursorY += SECTION_GAP;`);
193
+ lines.push(``);
194
+
195
+ // ── Per-Concept Token Anatomy ──
196
+ if (options.includeTokenAnatomy) {
197
+ for (const c of coverage) {
198
+ if (c.expectedTokens.length === 0) continue;
199
+
200
+ lines.push(` // ── ${c.conceptName} anatomy ──`);
201
+ lines.push(` await txt(0, cursorY, ${escapeJs(c.conceptName + " Tokens")}, 20, true);`);
202
+ lines.push(` cursorY += 28;`);
203
+ lines.push(` await txt(0, cursorY, ${escapeJs(c.purpose)}, 12, false, { r: 0.5, g: 0.5, b: 0.5 });`);
204
+ lines.push(` cursorY += 24;`);
205
+
206
+ for (const token of c.expectedTokens) {
207
+ const found = c.foundTokens.includes(token);
208
+ const snap = snapshots[token];
209
+
210
+ // Token row with status indicator
211
+ const indicatorColor = found
212
+ ? "{ r: 0.13, g: 0.73, b: 0.33 }"
213
+ : "{ r: 0.85, g: 0.12, b: 0.12 }";
214
+
215
+ lines.push(` rect(0, cursorY + 4, 8, 8, ${indicatorColor}, 4);`);
216
+ const fileToken = c.matchedFileTokens[token];
217
+ const displayLabel = fileToken ? `${token} → ${fileToken}` : token;
218
+ lines.push(` await txt(16, cursorY, ${escapeJs(displayLabel)}, 11, false);`);
219
+
220
+ // If it's a color token and we have the value, show a swatch
221
+ if (snap?.type === "COLOR" && snap.values) {
222
+ const firstValue = Object.values(snap.values)[0];
223
+ if (firstValue && typeof firstValue === "object" && "r" in (firstValue as Record<string, unknown>)) {
224
+ const cv = firstValue as { r: number; g: number; b: number };
225
+ lines.push(` rect(500, cursorY, 20, 16, { r: ${cv.r.toFixed(3)}, g: ${cv.g.toFixed(3)}, b: ${cv.b.toFixed(3)} }, 3);`);
226
+ const hex = `#${Math.round(cv.r * 255).toString(16).padStart(2, "0")}${Math.round(cv.g * 255).toString(16).padStart(2, "0")}${Math.round(cv.b * 255).toString(16).padStart(2, "0")}`.toUpperCase();
227
+ lines.push(` await txt(528, cursorY, ${escapeJs(hex)}, 11, false, { r: 0.4, g: 0.4, b: 0.4 });`);
228
+ }
229
+ } else if (snap?.values) {
230
+ const firstValue = Object.values(snap.values)[0];
231
+ if (firstValue !== undefined && firstValue !== null) {
232
+ lines.push(` await txt(500, cursorY, ${escapeJs(String(firstValue))}, 11, false, { r: 0.4, g: 0.4, b: 0.4 });`);
233
+ }
234
+ }
235
+
236
+ lines.push(` cursorY += 20;`);
237
+ }
238
+
239
+ lines.push(` cursorY += 32;`);
240
+ }
241
+ }
242
+
243
+ // ── Timestamp footer ──
244
+ lines.push(` await txt(0, cursorY, "Last synced: ${new Date().toISOString()}", 11, false, { r: 0.6, g: 0.6, b: 0.6 });`);
245
+ lines.push(``);
246
+
247
+ lines.push(` return { pageId: page.id };`);
248
+ lines.push(`})();`);
249
+
250
+ const result = await bridge.execute(lines.join("\n"));
251
+
252
+ let pageId = "unknown";
253
+ if (result.success && result.result) {
254
+ try {
255
+ const parsed = typeof result.result === "string" ? JSON.parse(result.result) : result.result;
256
+ pageId = parsed.pageId ?? "unknown";
257
+ } catch {
258
+ // result may be the page ID string directly
259
+ pageId = String(result.result);
260
+ }
261
+ }
262
+
263
+ return {
264
+ pageId,
265
+ autoSyncEnabled: options.autoSync,
266
+ };
267
+ }
268
+
269
+ /**
270
+ * Build a lightweight re-render script that updates only dynamic content
271
+ * (coverage badges, swatches, timestamp) without recreating the full page.
272
+ * Used by the auto-sync handler.
273
+ */
274
+ export function buildAutoSyncScript(
275
+ pageName: string,
276
+ coverage: ConceptCoverage[],
277
+ ): string {
278
+ // For auto-sync, we regenerate the full page since Figma plugin API
279
+ // doesn't support efficient partial updates of text/fill properties
280
+ // across many unrelated nodes. The full re-render is fast enough
281
+ // (< 500ms for ~25 concepts) given the 2-second debounce.
282
+ //
283
+ // This function is called by the variable-change event handler
284
+ // in figma-bridge.ts — it re-computes coverage and rebuilds the page.
285
+ return `
286
+ (async () => {
287
+ const page = figma.root.children.find(p => p.name === ${escapeJs(pageName)});
288
+ if (!page) return { skipped: true, reason: "page not found" };
289
+ return { needsFullRerender: true, pageId: page.id };
290
+ })();
291
+ `;
292
+ }