@sarjallab09/figma-intelligence 1.1.0 → 1.2.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.
- package/README.md +67 -36
- package/dist/bin/cli.js +2 -0
- package/dist/design-bridge/bridge.js +2 -0
- package/dist/figma-bridge-plugin/bridge-relay.js +2 -0
- package/dist/figma-bridge-plugin/code.js +1 -0
- package/{figma-bridge-plugin → dist/figma-bridge-plugin}/package-lock.json +0 -3
- package/dist/figma-bridge-plugin/ui.html +4970 -0
- package/dist/figma-intelligence-layer/dist/index.js +2 -0
- package/dist/scripts/clean-existing-chunks.js +2 -0
- package/dist/scripts/connect-ai-tool.js +2 -0
- package/dist/scripts/convert-hub-pdfs.js +2 -0
- package/dist/scripts/figma-mcp-status.js +2 -0
- package/dist/scripts/register-codex-mcp.js +2 -0
- package/dist/scripts/test-copilot-chat.js +2 -0
- package/package.json +11 -8
- package/bin/cli.js +0 -859
- package/design-bridge/bridge.js +0 -196
- package/design-bridge/lib/assets.js +0 -367
- package/design-bridge/lib/prompt.js +0 -85
- package/design-bridge/lib/server.js +0 -66
- package/design-bridge/lib/stitch.js +0 -37
- package/design-bridge/lib/tokens.js +0 -82
- package/design-bridge/package-lock.json +0 -579
- package/figma-bridge-plugin/README.md +0 -97
- package/figma-bridge-plugin/anthropic-chat-runner.js +0 -192
- package/figma-bridge-plugin/bridge-relay.js +0 -2505
- package/figma-bridge-plugin/chat-runner.js +0 -485
- package/figma-bridge-plugin/code.js +0 -1534
- package/figma-bridge-plugin/codex-runner.js +0 -505
- package/figma-bridge-plugin/component-schemas.js +0 -110
- package/figma-bridge-plugin/content-context.js +0 -869
- package/figma-bridge-plugin/create-button.js +0 -216
- package/figma-bridge-plugin/gemini-cli-runner.js +0 -291
- package/figma-bridge-plugin/gemini-runner.js +0 -187
- package/figma-bridge-plugin/html-to-figma.js +0 -927
- package/figma-bridge-plugin/knowledge-hub/.gitkeep +0 -0
- package/figma-bridge-plugin/knowledge-hub/uspec-references/anatomy-spec.md +0 -159
- package/figma-bridge-plugin/knowledge-hub/uspec-references/api-spec.md +0 -162
- package/figma-bridge-plugin/knowledge-hub/uspec-references/color-spec.md +0 -148
- package/figma-bridge-plugin/knowledge-hub/uspec-references/full-spec-template.md +0 -314
- package/figma-bridge-plugin/knowledge-hub/uspec-references/property-spec.md +0 -175
- package/figma-bridge-plugin/knowledge-hub/uspec-references/screen-reader-spec.md +0 -180
- package/figma-bridge-plugin/knowledge-hub/uspec-references/structure-spec.md +0 -165
- package/figma-bridge-plugin/perplexity-runner.js +0 -188
- package/figma-bridge-plugin/references/SKILL.md +0 -178
- package/figma-bridge-plugin/references/anatomy-spec.md +0 -159
- package/figma-bridge-plugin/references/api-spec.md +0 -162
- package/figma-bridge-plugin/references/color-spec.md +0 -148
- package/figma-bridge-plugin/references/full-spec-template.md +0 -314
- package/figma-bridge-plugin/references/property-spec.md +0 -175
- package/figma-bridge-plugin/references/screen-reader-spec.md +0 -180
- package/figma-bridge-plugin/references/structure-spec.md +0 -165
- package/figma-bridge-plugin/shared-prompt-config.js +0 -645
- package/figma-bridge-plugin/spec-helpers/build-table.js +0 -269
- package/figma-bridge-plugin/spec-helpers/classify-elements.js +0 -189
- package/figma-bridge-plugin/spec-helpers/index.js +0 -35
- package/figma-bridge-plugin/spec-helpers/parse-figma-link.js +0 -49
- package/figma-bridge-plugin/spec-helpers/position-markers.js +0 -158
- package/figma-bridge-plugin/stitch-auth.js +0 -322
- package/figma-bridge-plugin/stitch-runner.js +0 -1427
- package/figma-bridge-plugin/token-resolver.js +0 -107
- package/figma-bridge-plugin/ui.html +0 -4542
- package/figma-intelligence-layer/.env.example +0 -39
- package/figma-intelligence-layer/docs/local-image-generation.md +0 -60
- package/figma-intelligence-layer/examples/comfyui-workflow-template.example.json +0 -101
- package/figma-intelligence-layer/jest.config.js +0 -14
- package/figma-intelligence-layer/mcp-config.json +0 -19
- package/figma-intelligence-layer/package-lock.json +0 -5892
- package/figma-intelligence-layer/scripts/setup-comfyui-local.sh +0 -67
- package/figma-intelligence-layer/scripts/start-comfyui.sh +0 -33
- package/figma-intelligence-layer/src/index.ts +0 -2233
- package/figma-intelligence-layer/src/shared/auto-layout-validator.ts +0 -404
- package/figma-intelligence-layer/src/shared/cache.ts +0 -187
- package/figma-intelligence-layer/src/shared/color-operations.ts +0 -533
- package/figma-intelligence-layer/src/shared/color-utils.ts +0 -138
- package/figma-intelligence-layer/src/shared/component-script-builder.ts +0 -413
- package/figma-intelligence-layer/src/shared/component-templates.ts +0 -2767
- package/figma-intelligence-layer/src/shared/concept-taxonomy.ts +0 -694
- package/figma-intelligence-layer/src/shared/decision-log.ts +0 -128
- package/figma-intelligence-layer/src/shared/design-system-context.ts +0 -568
- package/figma-intelligence-layer/src/shared/design-system-intelligence.ts +0 -131
- package/figma-intelligence-layer/src/shared/design-system-matcher.ts +0 -184
- package/figma-intelligence-layer/src/shared/design-system-normalizers.ts +0 -196
- package/figma-intelligence-layer/src/shared/design-system-tokens.ts +0 -295
- package/figma-intelligence-layer/src/shared/dtcg-validator.ts +0 -530
- package/figma-intelligence-layer/src/shared/enrichment-pipeline.ts +0 -671
- package/figma-intelligence-layer/src/shared/figma-bridge.ts +0 -1418
- package/figma-intelligence-layer/src/shared/font-config.ts +0 -126
- package/figma-intelligence-layer/src/shared/icon-catalog.ts +0 -360
- package/figma-intelligence-layer/src/shared/icon-fetch.ts +0 -80
- package/figma-intelligence-layer/src/shared/prototype-script-builder.ts +0 -162
- package/figma-intelligence-layer/src/shared/response-compression.ts +0 -440
- package/figma-intelligence-layer/src/shared/semantic-token-catalog.ts +0 -324
- package/figma-intelligence-layer/src/shared/token-binder.ts +0 -505
- package/figma-intelligence-layer/src/shared/token-math.ts +0 -427
- package/figma-intelligence-layer/src/shared/token-naming.ts +0 -468
- package/figma-intelligence-layer/src/shared/token-utils.ts +0 -420
- package/figma-intelligence-layer/src/shared/types.ts +0 -346
- package/figma-intelligence-layer/src/shared/typography-presets.ts +0 -94
- package/figma-intelligence-layer/src/shared/unsplash.ts +0 -165
- package/figma-intelligence-layer/src/shared/vision-client.ts +0 -607
- package/figma-intelligence-layer/src/shared/vision-provider-anthropic.ts +0 -334
- package/figma-intelligence-layer/src/shared/vision-provider-openai.ts +0 -446
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotate-handler.ts +0 -782
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotate-renderer.ts +0 -496
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/a11y-annotation-kit.ts +0 -230
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/colorblind-sim.ts +0 -66
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/index.ts +0 -810
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-analyzer.ts +0 -1191
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-figma-page.ts +0 -1346
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/keyboard-sr-order-handler.ts +0 -148
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/vpat-figma-page.ts +0 -499
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/vpat-report.ts +0 -910
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/wcag-checker.ts +0 -989
- package/figma-intelligence-layer/src/tools/phase1-vision/a11y-audit/wcag-criteria.ts +0 -1160
- package/figma-intelligence-layer/src/tools/phase1-vision/design-from-ref/index.ts +0 -424
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/component-recognizer.ts +0 -38
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/ds-matcher.ts +0 -111
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/font-matcher.ts +0 -114
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/icon-resolver.ts +0 -103
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/index.ts +0 -1060
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/layout-segmenter.ts +0 -18
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/token-inferencer.ts +0 -39
- package/figma-intelligence-layer/src/tools/phase1-vision/screen-cloner/vision-pipeline.ts +0 -58
- package/figma-intelligence-layer/src/tools/phase1-vision/sketch-to-design/index.ts +0 -298
- package/figma-intelligence-layer/src/tools/phase1-vision/visual-audit/index.ts +0 -197
- package/figma-intelligence-layer/src/tools/phase2-accuracy/component-audit/index.ts +0 -494
- package/figma-intelligence-layer/src/tools/phase2-accuracy/intent-translator/index.ts +0 -356
- package/figma-intelligence-layer/src/tools/phase2-accuracy/layout-intelligence/container-patterns.ts +0 -123
- package/figma-intelligence-layer/src/tools/phase2-accuracy/layout-intelligence/index.ts +0 -663
- package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/built-in-rules.yaml +0 -56
- package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/index.ts +0 -614
- package/figma-intelligence-layer/src/tools/phase2-accuracy/lint-rules/rule-engine.ts +0 -113
- package/figma-intelligence-layer/src/tools/phase2-accuracy/theme-generator/color-theory.ts +0 -178
- package/figma-intelligence-layer/src/tools/phase2-accuracy/theme-generator/index.ts +0 -470
- package/figma-intelligence-layer/src/tools/phase2-accuracy/variant-expander/index.ts +0 -429
- package/figma-intelligence-layer/src/tools/phase2-accuracy/variant-expander/token-override-maps.ts +0 -226
- package/figma-intelligence-layer/src/tools/phase3-generation/ai-image-insert/index.ts +0 -535
- package/figma-intelligence-layer/src/tools/phase3-generation/component-archaeologist/index.ts +0 -660
- package/figma-intelligence-layer/src/tools/phase3-generation/component-archaeologist/pattern-fingerprints.ts +0 -209
- package/figma-intelligence-layer/src/tools/phase3-generation/composition-builder/index.ts +0 -540
- package/figma-intelligence-layer/src/tools/phase3-generation/figma-animated-build.ts +0 -391
- package/figma-intelligence-layer/src/tools/phase3-generation/page-architect/index.ts +0 -2019
- package/figma-intelligence-layer/src/tools/phase3-generation/page-architect/screen-templates.ts +0 -131
- package/figma-intelligence-layer/src/tools/phase3-generation/prototype-map/index.ts +0 -381
- package/figma-intelligence-layer/src/tools/phase3-generation/prototype-wire/index.ts +0 -565
- package/figma-intelligence-layer/src/tools/phase3-generation/swarm-build/index.ts +0 -764
- package/figma-intelligence-layer/src/tools/phase3-generation/system-drift/index.ts +0 -535
- package/figma-intelligence-layer/src/tools/phase3-generation/unsplash-search/index.ts +0 -84
- package/figma-intelligence-layer/src/tools/phase3-generation/url-to-frame/index.ts +0 -401
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/css-animations.ts +0 -68
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/framer-motion.ts +0 -78
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/code-generators/swift-animations.ts +0 -93
- package/figma-intelligence-layer/src/tools/phase4-sync/animation-specifier/index.ts +0 -596
- package/figma-intelligence-layer/src/tools/phase4-sync/ci-check/index.ts +0 -462
- package/figma-intelligence-layer/src/tools/phase4-sync/export-tokens/index.ts +0 -1470
- package/figma-intelligence-layer/src/tools/phase4-sync/generate-component-code/index.ts +0 -829
- package/figma-intelligence-layer/src/tools/phase4-sync/handoff-spec/index.ts +0 -702
- package/figma-intelligence-layer/src/tools/phase4-sync/icon-library-sync/index.ts +0 -483
- package/figma-intelligence-layer/src/tools/phase4-sync/sync-from-code/index.ts +0 -501
- package/figma-intelligence-layer/src/tools/phase4-sync/sync-from-code/storybook-parser.ts +0 -106
- package/figma-intelligence-layer/src/tools/phase4-sync/watch-docs/index.ts +0 -676
- package/figma-intelligence-layer/src/tools/phase4-sync/webhook-listener/index.ts +0 -560
- package/figma-intelligence-layer/src/tools/phase5-governance/apg-doc/index.ts +0 -1043
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/component-detection.ts +0 -620
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/anatomy.ts +0 -331
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/color-tokens.ts +0 -77
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/properties.ts +0 -54
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/snapshot.ts +0 -287
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/spacing.ts +0 -71
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/states.ts +0 -43
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/extractors/typography.ts +0 -71
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/index.ts +0 -221
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/_default.ts +0 -166
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/accordion.ts +0 -232
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/alert.ts +0 -234
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/avatar-group.ts +0 -270
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/avatar.ts +0 -249
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/badge.ts +0 -231
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/banner.ts +0 -293
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/breadcrumb.ts +0 -240
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/button.ts +0 -243
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/calendar.ts +0 -307
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/card.ts +0 -143
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/checkbox.ts +0 -227
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/chip.ts +0 -233
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/combobox.ts +0 -282
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/datepicker.ts +0 -276
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/divider.ts +0 -223
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/drawer.ts +0 -255
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/dropdown-menu.ts +0 -289
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/empty-state.ts +0 -261
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/file-uploader.ts +0 -290
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/form.ts +0 -265
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/grid.ts +0 -238
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/icon.ts +0 -255
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/index.ts +0 -128
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/inline-edit.ts +0 -286
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/inline-message.ts +0 -255
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/input.ts +0 -330
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/link.ts +0 -247
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/list.ts +0 -250
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/menu.ts +0 -247
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/modal.ts +0 -144
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/navbar.ts +0 -264
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/navigation.ts +0 -251
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/number-input.ts +0 -261
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/pagination.ts +0 -248
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/popover.ts +0 -270
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/progress.ts +0 -251
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/radio.ts +0 -142
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/range-slider.ts +0 -282
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/rating.ts +0 -250
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/search.ts +0 -258
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/segmented-control.ts +0 -265
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/select.ts +0 -319
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/skeleton.ts +0 -256
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/slider.ts +0 -232
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/spinner.ts +0 -239
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/status-dot.ts +0 -252
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/stepper.ts +0 -270
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/table.ts +0 -244
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tabs.ts +0 -143
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tag.ts +0 -243
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/textarea.ts +0 -259
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/time-picker.ts +0 -293
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toast.ts +0 -144
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toggle.ts +0 -289
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/toolbar.ts +0 -267
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/tooltip.ts +0 -232
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/treeview.ts +0 -257
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/knowledge/typography.ts +0 -319
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/legacy-compat.ts +0 -121
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/anatomy-diagram.ts +0 -430
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/figma-page.ts +0 -312
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/json.ts +0 -129
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/markdown.ts +0 -78
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/renderers/visual-doc.ts +0 -2333
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/accessibility.ts +0 -100
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/anatomy.ts +0 -32
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/color-tokens.ts +0 -59
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/content-guidance.ts +0 -18
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/design-tokens.ts +0 -53
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/interaction-rules.ts +0 -19
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/overview.ts +0 -91
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/properties-api.ts +0 -71
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/qa-criteria.ts +0 -19
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/related-components.ts +0 -110
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/responsive.ts +0 -19
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/size-specs.ts +0 -67
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/spacing-structure.ts +0 -58
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/state-specs.ts +0 -79
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/states.ts +0 -50
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/type-hierarchy.ts +0 -33
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/typography.ts +0 -55
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/usage-guidelines.ts +0 -73
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/sections/variants.ts +0 -81
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec/types.ts +0 -409
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/index.ts +0 -198
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/renderer.ts +0 -701
- package/figma-intelligence-layer/src/tools/phase5-governance/component-spec-sheet/types.ts +0 -88
- package/figma-intelligence-layer/src/tools/phase5-governance/decision-log/index.ts +0 -135
- package/figma-intelligence-layer/src/tools/phase5-governance/design-decision-log/index.ts +0 -491
- package/figma-intelligence-layer/src/tools/phase5-governance/ds-primitives/index.ts +0 -416
- package/figma-intelligence-layer/src/tools/phase5-governance/ds-scaffolder/index.ts +0 -722
- package/figma-intelligence-layer/src/tools/phase5-governance/ds-variables/index.ts +0 -449
- package/figma-intelligence-layer/src/tools/phase5-governance/health-report/index.ts +0 -393
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/index.ts +0 -406
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/figma-page.ts +0 -292
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/json.ts +0 -24
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/markdown.ts +0 -172
- package/figma-intelligence-layer/src/tools/phase5-governance/taxonomy-docs/renderers/naming-guide.ts +0 -409
- package/figma-intelligence-layer/src/tools/phase5-governance/token-analytics/index.ts +0 -594
- package/figma-intelligence-layer/src/tools/phase5-governance/token-docs/index.ts +0 -710
- package/figma-intelligence-layer/src/tools/phase5-governance/token-migrate/index.ts +0 -458
- package/figma-intelligence-layer/src/tools/phase5-governance/token-naming/index.ts +0 -134
- package/figma-intelligence-layer/tests/apg-doc.test.ts +0 -101
- package/figma-intelligence-layer/tests/design-system-context.test.ts +0 -152
- package/figma-intelligence-layer/tests/design-system-matcher.test.ts +0 -144
- package/figma-intelligence-layer/tests/figma-bridge.test.ts +0 -83
- package/figma-intelligence-layer/tests/generate-image-and-insert.test.ts +0 -56
- package/figma-intelligence-layer/tests/screen-cloner-regression.test.ts +0 -69
- package/figma-intelligence-layer/tests/smoke.test.ts +0 -174
- package/figma-intelligence-layer/tests/spec-generator.test.ts +0 -127
- package/figma-intelligence-layer/tests/token-migrate.test.ts +0 -21
- package/figma-intelligence-layer/tests/token-naming.test.ts +0 -30
- package/figma-intelligence-layer/tsconfig.json +0 -19
- package/scripts/clean-existing-chunks.js +0 -179
- package/scripts/connect-ai-tool.js +0 -490
- package/scripts/convert-hub-pdfs.js +0 -425
- package/scripts/figma-mcp-status.js +0 -349
- package/scripts/register-codex-mcp.js +0 -96
- /package/{design-bridge → dist/design-bridge}/.env.example +0 -0
- /package/{design-bridge → dist/design-bridge}/package.json +0 -0
- /package/{figma-bridge-plugin → dist/figma-bridge-plugin}/manifest.json +0 -0
- /package/{figma-bridge-plugin → dist/figma-bridge-plugin}/package.json +0 -0
- /package/{figma-intelligence-layer → dist/figma-intelligence-layer}/package.json +0 -0
|
@@ -1,1418 +0,0 @@
|
|
|
1
|
-
import WebSocket, { WebSocketServer } from "ws";
|
|
2
|
-
import {
|
|
3
|
-
FigmaNode,
|
|
4
|
-
ExecuteResult,
|
|
5
|
-
ComponentSet,
|
|
6
|
-
Token,
|
|
7
|
-
FigmaBridgeEvent,
|
|
8
|
-
FigmaBridgeEventType,
|
|
9
|
-
FigmaContextSnapshot,
|
|
10
|
-
FigmaDocumentChangeSummary,
|
|
11
|
-
FigmaPageSummary,
|
|
12
|
-
FigmaSelectionItem,
|
|
13
|
-
} from "./types.js";
|
|
14
|
-
import { BridgeCache } from "./cache.js";
|
|
15
|
-
import { compressResponse, CompressedResponse, CompressionTier } from "./response-compression.js";
|
|
16
|
-
import { enrichDesignSystem, EnrichedDesignSystem, resolveStyles, ResolvedStyle } from "./enrichment-pipeline.js";
|
|
17
|
-
|
|
18
|
-
const WS_PORT = parseInt(process.env.FIGMA_BRIDGE_PORT || "9001", 10);
|
|
19
|
-
const REQUEST_TIMEOUT = parseInt(process.env.FIGMA_REQUEST_TIMEOUT || "30000", 10);
|
|
20
|
-
|
|
21
|
-
let relayServer: WebSocketServer | null = null;
|
|
22
|
-
let relayPluginSocket: WebSocket | null = null;
|
|
23
|
-
const relayMcpSockets = new Set<WebSocket>();
|
|
24
|
-
const relayPendingRequests = new Map<string, WebSocket>();
|
|
25
|
-
let relayStartupPromise: Promise<void> | null = null;
|
|
26
|
-
|
|
27
|
-
type RelayMessage = {
|
|
28
|
-
id?: string;
|
|
29
|
-
method?: string;
|
|
30
|
-
params?: Record<string, unknown>;
|
|
31
|
-
result?: unknown;
|
|
32
|
-
error?: string;
|
|
33
|
-
type?: string;
|
|
34
|
-
fileName?: string;
|
|
35
|
-
eventType?: FigmaBridgeEventType;
|
|
36
|
-
payload?: unknown;
|
|
37
|
-
timestamp?: number;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
function sendRelayStatus(ws: WebSocket | null, mcpConnected: boolean) {
|
|
41
|
-
if (!ws || ws.readyState !== WebSocket.OPEN) return;
|
|
42
|
-
|
|
43
|
-
ws.send(JSON.stringify({
|
|
44
|
-
type: "bridge-status",
|
|
45
|
-
mcpConnected,
|
|
46
|
-
}));
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function hasConnectedMcpSocket() {
|
|
50
|
-
for (const socket of relayMcpSockets) {
|
|
51
|
-
if (socket.readyState === WebSocket.OPEN) return true;
|
|
52
|
-
}
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function broadcastToMcpSockets(raw: string) {
|
|
57
|
-
for (const socket of relayMcpSockets) {
|
|
58
|
-
if (socket.readyState === WebSocket.OPEN) {
|
|
59
|
-
socket.send(raw);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
function setupRelayRouting(wss: WebSocketServer) {
|
|
65
|
-
wss.on("connection", (ws, req) => {
|
|
66
|
-
const path = req.url || "/";
|
|
67
|
-
const isPlugin = path.includes("/plugin");
|
|
68
|
-
|
|
69
|
-
if (isPlugin) {
|
|
70
|
-
relayPluginSocket = ws;
|
|
71
|
-
process.stderr.write("Figma bridge plugin connected\n");
|
|
72
|
-
sendRelayStatus(ws, hasConnectedMcpSocket());
|
|
73
|
-
} else {
|
|
74
|
-
relayMcpSockets.add(ws);
|
|
75
|
-
process.stderr.write("Figma bridge MCP socket connected\n");
|
|
76
|
-
sendRelayStatus(relayPluginSocket, true);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
ws.on("message", (data) => {
|
|
80
|
-
const raw = data.toString();
|
|
81
|
-
let msg: RelayMessage;
|
|
82
|
-
|
|
83
|
-
try {
|
|
84
|
-
msg = JSON.parse(raw);
|
|
85
|
-
} catch {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (isPlugin) {
|
|
90
|
-
if (msg.type === "plugin-hello") {
|
|
91
|
-
process.stderr.write(`Figma bridge plugin ready (${msg.fileName || "unknown file"})\n`);
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (msg.type === "bridge-event") {
|
|
96
|
-
broadcastToMcpSockets(raw);
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
if (msg.id && !msg.method) {
|
|
101
|
-
const targetSocket = relayPendingRequests.get(msg.id);
|
|
102
|
-
if (targetSocket && targetSocket.readyState === WebSocket.OPEN) {
|
|
103
|
-
targetSocket.send(raw);
|
|
104
|
-
} else {
|
|
105
|
-
broadcastToMcpSockets(raw);
|
|
106
|
-
}
|
|
107
|
-
relayPendingRequests.delete(msg.id);
|
|
108
|
-
}
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (msg.id && msg.method) {
|
|
113
|
-
if (relayPluginSocket && relayPluginSocket.readyState === WebSocket.OPEN) {
|
|
114
|
-
relayPendingRequests.set(msg.id, ws);
|
|
115
|
-
relayPluginSocket.send(JSON.stringify({
|
|
116
|
-
type: "bridge-request",
|
|
117
|
-
id: msg.id,
|
|
118
|
-
method: msg.method,
|
|
119
|
-
params: msg.params || {},
|
|
120
|
-
}));
|
|
121
|
-
} else {
|
|
122
|
-
ws.send(JSON.stringify({
|
|
123
|
-
id: msg.id,
|
|
124
|
-
error: "Figma plugin is not connected. Open Figma and run the Intelligence Bridge plugin.",
|
|
125
|
-
}));
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
ws.on("close", () => {
|
|
131
|
-
if (isPlugin && relayPluginSocket === ws) {
|
|
132
|
-
relayPluginSocket = null;
|
|
133
|
-
process.stderr.write("Figma bridge plugin disconnected\n");
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (!isPlugin) {
|
|
137
|
-
relayMcpSockets.delete(ws);
|
|
138
|
-
for (const [requestId, requestSocket] of relayPendingRequests.entries()) {
|
|
139
|
-
if (requestSocket === ws) {
|
|
140
|
-
relayPendingRequests.delete(requestId);
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
process.stderr.write("Figma bridge MCP socket disconnected\n");
|
|
144
|
-
sendRelayStatus(relayPluginSocket, hasConnectedMcpSocket());
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// P3: Port fallback range
|
|
151
|
-
const PORT_FALLBACK_RANGE = 10;
|
|
152
|
-
|
|
153
|
-
export async function ensureRelayServer(): Promise<void> {
|
|
154
|
-
if (relayServer) return;
|
|
155
|
-
if (relayStartupPromise) return relayStartupPromise;
|
|
156
|
-
|
|
157
|
-
relayStartupPromise = new Promise<void>((resolve, reject) => {
|
|
158
|
-
// Try to create our own relay server on the configured port
|
|
159
|
-
const wss = new WebSocketServer({ port: WS_PORT });
|
|
160
|
-
let settled = false;
|
|
161
|
-
|
|
162
|
-
const finish = (callback: () => unknown) => {
|
|
163
|
-
if (settled) return;
|
|
164
|
-
settled = true;
|
|
165
|
-
callback();
|
|
166
|
-
};
|
|
167
|
-
|
|
168
|
-
setupRelayRouting(wss);
|
|
169
|
-
|
|
170
|
-
wss.on("listening", () => {
|
|
171
|
-
relayServer = wss;
|
|
172
|
-
process.stderr.write(`Figma bridge relay listening on ws://localhost:${WS_PORT}\n`);
|
|
173
|
-
finish(() => resolve());
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
wss.on("error", (error: NodeJS.ErrnoException) => {
|
|
177
|
-
if (error.code === "EADDRINUSE") {
|
|
178
|
-
// Port in use — bridge-relay is already running externally.
|
|
179
|
-
// Don't try other ports; just connect as a client to the existing relay.
|
|
180
|
-
process.stderr.write(`Figma bridge relay already running on ws://localhost:${WS_PORT} — connecting as client\n`);
|
|
181
|
-
finish(() => resolve());
|
|
182
|
-
return;
|
|
183
|
-
}
|
|
184
|
-
finish(() => reject(error));
|
|
185
|
-
});
|
|
186
|
-
}).finally(() => {
|
|
187
|
-
relayStartupPromise = null;
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
return relayStartupPromise ?? Promise.resolve();
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
194
|
-
// FigmaBridge
|
|
195
|
-
// Wraps the Desktop Bridge WebSocket connection with typed helpers
|
|
196
|
-
// ─────────────────────────────────────────────────────────────────────────────
|
|
197
|
-
|
|
198
|
-
export class FigmaBridge {
|
|
199
|
-
private ws: WebSocket | null = null;
|
|
200
|
-
private pendingRequests = new Map<
|
|
201
|
-
string,
|
|
202
|
-
{ resolve: (v: unknown) => void; reject: (e: Error) => void }
|
|
203
|
-
>();
|
|
204
|
-
private eventListeners = new Map<FigmaBridgeEventType | "*", Set<(event: FigmaBridgeEvent) => void>>();
|
|
205
|
-
private context: FigmaContextSnapshot = {
|
|
206
|
-
status: "disconnected",
|
|
207
|
-
selection: [],
|
|
208
|
-
};
|
|
209
|
-
private hasHydratedStatus = false;
|
|
210
|
-
private hasHydratedSelection = false;
|
|
211
|
-
private connected = false;
|
|
212
|
-
private connectPromise: Promise<void> | null = null;
|
|
213
|
-
private capabilitiesCache: Record<string, unknown> | null = null;
|
|
214
|
-
private _activeDesignSystemId: string | null = null;
|
|
215
|
-
|
|
216
|
-
// ─── Taxonomy auto-sync ─────────────────────────────────────────────────
|
|
217
|
-
private _taxonomyAutoSyncTimer: ReturnType<typeof setTimeout> | null = null;
|
|
218
|
-
private _taxonomyAutoSyncCallback: (() => Promise<void>) | null = null;
|
|
219
|
-
|
|
220
|
-
/**
|
|
221
|
-
* Register a callback to run when variables change (for taxonomy docs auto-sync).
|
|
222
|
-
* The callback is debounced with a 2-second delay.
|
|
223
|
-
*/
|
|
224
|
-
setTaxonomyAutoSyncHandler(handler: (() => Promise<void>) | null): void {
|
|
225
|
-
this._taxonomyAutoSyncCallback = handler;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
private scheduleTaxonomyAutoSync(): void {
|
|
229
|
-
if (!this._taxonomyAutoSyncCallback) return;
|
|
230
|
-
if (this._taxonomyAutoSyncTimer) clearTimeout(this._taxonomyAutoSyncTimer);
|
|
231
|
-
this._taxonomyAutoSyncTimer = setTimeout(async () => {
|
|
232
|
-
this._taxonomyAutoSyncTimer = null;
|
|
233
|
-
try {
|
|
234
|
-
await this._taxonomyAutoSyncCallback?.();
|
|
235
|
-
} catch (e) {
|
|
236
|
-
process.stderr.write(`Taxonomy auto-sync error: ${e}\n`);
|
|
237
|
-
}
|
|
238
|
-
}, 2000);
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
// ─── P1: Caching Layer ──────────────────────────────────────────────────
|
|
242
|
-
private _cache: BridgeCache | null = null;
|
|
243
|
-
get cache(): BridgeCache {
|
|
244
|
-
if (!this._cache) this._cache = new BridgeCache(this);
|
|
245
|
-
return this._cache;
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
isConnected(): boolean {
|
|
249
|
-
return this.connected && this.ws?.readyState === WebSocket.OPEN;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* Get the currently selected design system ID (e.g. "antd", "carbon", "mui").
|
|
254
|
-
* Returns null if no design system is selected in the UI.
|
|
255
|
-
*/
|
|
256
|
-
getActiveDesignSystemId(): string | null {
|
|
257
|
-
return this._activeDesignSystemId;
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Request the current design system ID from the relay and cache it locally.
|
|
262
|
-
*/
|
|
263
|
-
private async hydrateDesignSystemId(): Promise<void> {
|
|
264
|
-
try {
|
|
265
|
-
const result = await this.send<string | null>("getActiveDesignSystemId");
|
|
266
|
-
this._activeDesignSystemId = result ?? null;
|
|
267
|
-
} catch {
|
|
268
|
-
// Non-critical — may not be supported by older relays
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
private rejectAllPending(reason: Error) {
|
|
273
|
-
const pending = Array.from(this.pendingRequests.values());
|
|
274
|
-
this.pendingRequests.clear();
|
|
275
|
-
for (const request of pending) {
|
|
276
|
-
request.reject(reason);
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
private invalidateConnection(reason: Error) {
|
|
281
|
-
const socket = this.ws;
|
|
282
|
-
this.connected = false;
|
|
283
|
-
this.ws = null;
|
|
284
|
-
// Clear ALL cached state so reconnect always fetches fresh data
|
|
285
|
-
this.context = {
|
|
286
|
-
status: "disconnected",
|
|
287
|
-
fileName: undefined,
|
|
288
|
-
currentPage: undefined,
|
|
289
|
-
pageCount: 0,
|
|
290
|
-
selection: [],
|
|
291
|
-
lastUpdatedAt: Date.now(),
|
|
292
|
-
lastDocumentChange: undefined,
|
|
293
|
-
};
|
|
294
|
-
this.hasHydratedStatus = false;
|
|
295
|
-
this.hasHydratedSelection = false;
|
|
296
|
-
this.capabilitiesCache = null;
|
|
297
|
-
this.rejectAllPending(reason);
|
|
298
|
-
if (socket && socket.readyState === WebSocket.OPEN) {
|
|
299
|
-
socket.close();
|
|
300
|
-
}
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
async connect(): Promise<void> {
|
|
304
|
-
if (this.isConnected()) return;
|
|
305
|
-
if (this.connectPromise) return this.connectPromise;
|
|
306
|
-
await ensureRelayServer();
|
|
307
|
-
this.connectPromise = new Promise<void>((resolve, reject) => {
|
|
308
|
-
const socket = new WebSocket(`ws://localhost:${WS_PORT}`);
|
|
309
|
-
const handleFailure = (err: Error) => {
|
|
310
|
-
this.connected = false;
|
|
311
|
-
this.ws = null;
|
|
312
|
-
reject(err);
|
|
313
|
-
};
|
|
314
|
-
|
|
315
|
-
socket.on("open", () => {
|
|
316
|
-
this.ws = socket;
|
|
317
|
-
this.connected = true;
|
|
318
|
-
this.context = {
|
|
319
|
-
...this.context,
|
|
320
|
-
status: "connected",
|
|
321
|
-
lastUpdatedAt: Date.now(),
|
|
322
|
-
};
|
|
323
|
-
// Proactively hydrate status + selection in background so next getStatus() is instant
|
|
324
|
-
this.hydrateAfterConnect();
|
|
325
|
-
resolve();
|
|
326
|
-
});
|
|
327
|
-
socket.on("error", (err) => {
|
|
328
|
-
handleFailure(err instanceof Error ? err : new Error(String(err)));
|
|
329
|
-
});
|
|
330
|
-
socket.on("message", (data) => this.handleMessage(String(data)));
|
|
331
|
-
socket.on("close", () => {
|
|
332
|
-
this.invalidateConnection(new Error("FigmaBridge: socket closed"));
|
|
333
|
-
});
|
|
334
|
-
}).finally((): void => {
|
|
335
|
-
this.connectPromise = null;
|
|
336
|
-
});
|
|
337
|
-
|
|
338
|
-
return this.connectPromise ?? Promise.resolve();
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/** Fire-and-forget hydration after (re)connect — populates context cache quickly */
|
|
342
|
-
private hydrateAfterConnect(): void {
|
|
343
|
-
// Hydrate active design system ID from relay
|
|
344
|
-
this.hydrateDesignSystemId();
|
|
345
|
-
if (this.hasHydratedStatus) return;
|
|
346
|
-
// Use a short timeout for the hydration RPC (5s instead of default 30s)
|
|
347
|
-
const FAST_TIMEOUT = 5000;
|
|
348
|
-
const id = `req_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
349
|
-
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) return;
|
|
350
|
-
|
|
351
|
-
const timeout = setTimeout(() => {
|
|
352
|
-
this.pendingRequests.delete(id);
|
|
353
|
-
// Don't invalidate connection on hydration timeout — it's best-effort
|
|
354
|
-
}, FAST_TIMEOUT);
|
|
355
|
-
|
|
356
|
-
this.pendingRequests.set(id, {
|
|
357
|
-
resolve: (v) => {
|
|
358
|
-
clearTimeout(timeout);
|
|
359
|
-
const status = v as { fileName?: string; currentPage?: FigmaPageSummary; pageCount?: number; timestamp?: number };
|
|
360
|
-
if (status?.fileName) {
|
|
361
|
-
this.context = {
|
|
362
|
-
...this.context,
|
|
363
|
-
status: "connected",
|
|
364
|
-
fileName: status.fileName,
|
|
365
|
-
currentPage: status.currentPage,
|
|
366
|
-
pageCount: status.pageCount || 0,
|
|
367
|
-
lastUpdatedAt: status.timestamp || Date.now(),
|
|
368
|
-
};
|
|
369
|
-
this.hasHydratedStatus = true;
|
|
370
|
-
}
|
|
371
|
-
},
|
|
372
|
-
reject: (e) => {
|
|
373
|
-
clearTimeout(timeout);
|
|
374
|
-
// Swallow — hydration is best-effort
|
|
375
|
-
},
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
this.ws.send(JSON.stringify({ id, method: "getStatus", params: {} }));
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
private emitEvent(event: FigmaBridgeEvent) {
|
|
382
|
-
const specificListeners = this.eventListeners.get(event.eventType);
|
|
383
|
-
const catchAllListeners = this.eventListeners.get("*");
|
|
384
|
-
|
|
385
|
-
for (const listener of specificListeners || []) {
|
|
386
|
-
listener(event);
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
for (const listener of catchAllListeners || []) {
|
|
390
|
-
listener(event);
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
|
|
394
|
-
private updateContextFromEvent(event: FigmaBridgeEvent) {
|
|
395
|
-
const nextContext: FigmaContextSnapshot = {
|
|
396
|
-
...this.context,
|
|
397
|
-
status: "connected",
|
|
398
|
-
lastUpdatedAt: event.timestamp,
|
|
399
|
-
};
|
|
400
|
-
|
|
401
|
-
if (event.eventType === "bridge.ready") {
|
|
402
|
-
const payload = event.payload as Partial<FigmaContextSnapshot>;
|
|
403
|
-
nextContext.fileName = payload.fileName || nextContext.fileName;
|
|
404
|
-
nextContext.currentPage = payload.currentPage || nextContext.currentPage;
|
|
405
|
-
nextContext.pageCount = payload.pageCount || nextContext.pageCount;
|
|
406
|
-
nextContext.selection = payload.selection || nextContext.selection;
|
|
407
|
-
this.hasHydratedStatus = true;
|
|
408
|
-
this.hasHydratedSelection = true;
|
|
409
|
-
}
|
|
410
|
-
|
|
411
|
-
if (event.eventType === "selectionchange") {
|
|
412
|
-
nextContext.selection = (event.payload as { selection?: FigmaSelectionItem[] }).selection || [];
|
|
413
|
-
this.hasHydratedSelection = true;
|
|
414
|
-
}
|
|
415
|
-
|
|
416
|
-
if (event.eventType === "currentpagechange") {
|
|
417
|
-
const payload = event.payload as { currentPage?: FigmaPageSummary; selection?: FigmaSelectionItem[] };
|
|
418
|
-
nextContext.currentPage = payload.currentPage || nextContext.currentPage;
|
|
419
|
-
if (payload.selection) {
|
|
420
|
-
nextContext.selection = payload.selection;
|
|
421
|
-
this.hasHydratedSelection = true;
|
|
422
|
-
}
|
|
423
|
-
this.hasHydratedStatus = true;
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
if (event.eventType === "documentchange") {
|
|
427
|
-
const payload = event.payload as FigmaDocumentChangeSummary;
|
|
428
|
-
nextContext.lastDocumentChange = payload;
|
|
429
|
-
// P1: Invalidate cache on document changes
|
|
430
|
-
if (this._cache) this._cache.onDocumentChange();
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Auto-sync: trigger taxonomy docs re-render on variable changes
|
|
434
|
-
if ((event.eventType as string) === "variable-change") {
|
|
435
|
-
this.scheduleTaxonomyAutoSync();
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
this.context = nextContext;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
private handleMessage(raw: string) {
|
|
442
|
-
let msg: RelayMessage;
|
|
443
|
-
try {
|
|
444
|
-
msg = JSON.parse(raw);
|
|
445
|
-
} catch {
|
|
446
|
-
return;
|
|
447
|
-
}
|
|
448
|
-
// Handle design system change broadcasts from relay
|
|
449
|
-
if (msg.type === "design-system-changed") {
|
|
450
|
-
this._activeDesignSystemId = (msg as Record<string, unknown>).designSystemId as string | null ?? null;
|
|
451
|
-
return;
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
if (msg.type === "bridge-event" && msg.eventType) {
|
|
455
|
-
const event: FigmaBridgeEvent = {
|
|
456
|
-
type: "bridge-event",
|
|
457
|
-
eventType: msg.eventType,
|
|
458
|
-
payload: msg.payload,
|
|
459
|
-
timestamp: msg.timestamp || Date.now(),
|
|
460
|
-
};
|
|
461
|
-
this.updateContextFromEvent(event);
|
|
462
|
-
this.emitEvent(event);
|
|
463
|
-
return;
|
|
464
|
-
}
|
|
465
|
-
if (!msg.id) return;
|
|
466
|
-
const pending = this.pendingRequests.get(msg.id);
|
|
467
|
-
if (!pending) return;
|
|
468
|
-
this.pendingRequests.delete(msg.id);
|
|
469
|
-
if (msg.error) {
|
|
470
|
-
pending.reject(new Error(msg.error));
|
|
471
|
-
} else {
|
|
472
|
-
pending.resolve(msg.result);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
|
|
476
|
-
private send<T>(method: string, params: Record<string, unknown> = {}): Promise<T> {
|
|
477
|
-
return this.sendWithTimeout<T>(method, params, REQUEST_TIMEOUT);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
private sendWithTimeout<T>(method: string, params: Record<string, unknown> = {}, timeoutMs: number = REQUEST_TIMEOUT): Promise<T> {
|
|
481
|
-
return new Promise((resolve, reject) => {
|
|
482
|
-
if (!this.isConnected() || !this.ws) {
|
|
483
|
-
reject(new Error("FigmaBridge: not connected"));
|
|
484
|
-
return;
|
|
485
|
-
}
|
|
486
|
-
const id = `req_${Date.now()}_${Math.random().toString(36).slice(2)}`;
|
|
487
|
-
const timeout = setTimeout(() => {
|
|
488
|
-
this.pendingRequests.delete(id);
|
|
489
|
-
const error = new Error(`FigmaBridge: timeout on method ${method}`);
|
|
490
|
-
this.invalidateConnection(error);
|
|
491
|
-
reject(error);
|
|
492
|
-
}, timeoutMs);
|
|
493
|
-
|
|
494
|
-
this.pendingRequests.set(id, {
|
|
495
|
-
resolve: (v) => { clearTimeout(timeout); resolve(v as T); },
|
|
496
|
-
reject: (e) => { clearTimeout(timeout); reject(e); },
|
|
497
|
-
});
|
|
498
|
-
|
|
499
|
-
this.ws.send(JSON.stringify({ id, method, params }));
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
|
|
503
|
-
async execute(script: string, timeoutMs?: number): Promise<ExecuteResult> {
|
|
504
|
-
try {
|
|
505
|
-
const result = await this.sendWithTimeout<unknown>("execute", { code: script }, timeoutMs || REQUEST_TIMEOUT);
|
|
506
|
-
return { success: true, result };
|
|
507
|
-
} catch (err) {
|
|
508
|
-
return {
|
|
509
|
-
success: false,
|
|
510
|
-
error: err instanceof Error ? err.message : String(err),
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
async getNode(nodeId: string): Promise<FigmaNode> {
|
|
516
|
-
const result = await this.execute(`
|
|
517
|
-
const node = await figma.getNodeByIdAsync(${JSON.stringify(nodeId)});
|
|
518
|
-
if (!node) throw new Error("Node not found: " + ${JSON.stringify(nodeId)});
|
|
519
|
-
return JSON.parse(JSON.stringify(node));
|
|
520
|
-
`);
|
|
521
|
-
if (!result.success) throw new Error(result.error);
|
|
522
|
-
return result.result as FigmaNode;
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
async takeScreenshot(nodeId: string): Promise<string> {
|
|
526
|
-
// Use the plugin's dedicated "screenshot" handler — it exports PNG bytes
|
|
527
|
-
// in the plugin sandbox, then the UI layer (browser) converts to base64.
|
|
528
|
-
// This avoids calling btoa() inside the Figma plugin sandbox where it doesn't exist.
|
|
529
|
-
const dataUri = await this.sendWithTimeout<string>("screenshot", { nodeId, scale: 2 }, 60000);
|
|
530
|
-
return dataUri;
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
async importImage(imageDataUri: string): Promise<{ imageHash: string; byteLength: number }> {
|
|
534
|
-
const result = await this.send<{ imageHash: string; byteLength: number }>("importImage", {
|
|
535
|
-
imageDataUri,
|
|
536
|
-
});
|
|
537
|
-
return result;
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
async getComponentSets(): Promise<ComponentSet[]> {
|
|
541
|
-
const result = await this.execute(`
|
|
542
|
-
var page = figma.currentPage;
|
|
543
|
-
var sets = page.findAll(function(n) { return n.type === 'COMPONENT_SET'; });
|
|
544
|
-
var allSets = [];
|
|
545
|
-
for (var j = 0; j < sets.length; j++) {
|
|
546
|
-
var s = sets[j];
|
|
547
|
-
try {
|
|
548
|
-
allSets.push({
|
|
549
|
-
id: s.id,
|
|
550
|
-
name: s.name,
|
|
551
|
-
description: s.description || '',
|
|
552
|
-
variantGroupProperties: s.variantGroupProperties || {},
|
|
553
|
-
children: s.children.map(function(c) { return { id: c.id, name: c.name, type: c.type, description: c.description || '' }; })
|
|
554
|
-
});
|
|
555
|
-
} catch (err) {
|
|
556
|
-
// Skip component sets with corrupted variant metadata so clone generation can continue.
|
|
557
|
-
}
|
|
558
|
-
if (allSets.length > 200) break;
|
|
559
|
-
}
|
|
560
|
-
return allSets;
|
|
561
|
-
`);
|
|
562
|
-
if (!result.success) throw new Error(result.error);
|
|
563
|
-
return result.result as ComponentSet[];
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
async getTokens(collectionId?: string): Promise<Token[]> {
|
|
567
|
-
const filter = collectionId
|
|
568
|
-
? `collections.filter(c => c.id === ${JSON.stringify(collectionId)})`
|
|
569
|
-
: "collections";
|
|
570
|
-
const result = await this.execute(`
|
|
571
|
-
const collections = await figma.variables.getLocalVariableCollectionsAsync();
|
|
572
|
-
const filtered = ${filter};
|
|
573
|
-
const tokens = [];
|
|
574
|
-
for (const col of filtered) {
|
|
575
|
-
const vars = [];
|
|
576
|
-
for (const id of col.variableIds) {
|
|
577
|
-
const v = await figma.variables.getVariableByIdAsync(id);
|
|
578
|
-
if (v) vars.push(v);
|
|
579
|
-
}
|
|
580
|
-
for (const v of vars) {
|
|
581
|
-
const modeValues = {};
|
|
582
|
-
for (const [modeId, val] of Object.entries(v.valuesByMode)) {
|
|
583
|
-
const mode = col.modes.find(m => m.modeId === modeId);
|
|
584
|
-
modeValues[mode ? mode.name : modeId] = val;
|
|
585
|
-
}
|
|
586
|
-
var firstVal = Object.values(modeValues)[0];
|
|
587
|
-
tokens.push({
|
|
588
|
-
id: v.id,
|
|
589
|
-
name: v.name,
|
|
590
|
-
type: v.resolvedType,
|
|
591
|
-
value: firstVal !== undefined ? firstVal : null,
|
|
592
|
-
collectionId: col.id,
|
|
593
|
-
modeValues,
|
|
594
|
-
description: v.description || '',
|
|
595
|
-
});
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
return tokens;
|
|
599
|
-
`);
|
|
600
|
-
if (!result.success) throw new Error(result.error);
|
|
601
|
-
return result.result as Token[];
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
async getAllPages(): Promise<Array<{ id: string; name: string }>> {
|
|
605
|
-
const result = await this.execute(`
|
|
606
|
-
return figma.root.children.map(p => ({ id: p.id, name: p.name }));
|
|
607
|
-
`);
|
|
608
|
-
if (!result.success) throw new Error(result.error);
|
|
609
|
-
return result.result as Array<{ id: string; name: string }>;
|
|
610
|
-
}
|
|
611
|
-
|
|
612
|
-
async createPage(name: string): Promise<string> {
|
|
613
|
-
const result = await this.execute(`
|
|
614
|
-
const page = figma.createPage();
|
|
615
|
-
page.name = ${JSON.stringify(name)};
|
|
616
|
-
return page.id;
|
|
617
|
-
`);
|
|
618
|
-
if (!result.success) throw new Error(result.error);
|
|
619
|
-
return result.result as string;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
async navigateToNode(nodeId: string): Promise<void> {
|
|
623
|
-
const result = await this.execute(`
|
|
624
|
-
const node = await figma.getNodeByIdAsync(${JSON.stringify(nodeId)});
|
|
625
|
-
if (node) figma.viewport.scrollAndZoomIntoView([node]);
|
|
626
|
-
`);
|
|
627
|
-
if (!result.success) throw new Error(result.error);
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
async getAllInstances(): Promise<Array<{ id: string; mainComponentId: string; name: string; pageId: string }>> {
|
|
631
|
-
const result = await this.execute(`
|
|
632
|
-
const instances = [];
|
|
633
|
-
const page = figma.currentPage;
|
|
634
|
-
const found = page.findAll(n => n.type === 'INSTANCE');
|
|
635
|
-
for (const inst of found) {
|
|
636
|
-
let mainComponentId = '';
|
|
637
|
-
try {
|
|
638
|
-
const main = await inst.getMainComponentAsync();
|
|
639
|
-
if (main) mainComponentId = main.id;
|
|
640
|
-
} catch (e) {}
|
|
641
|
-
instances.push({
|
|
642
|
-
id: inst.id,
|
|
643
|
-
mainComponentId,
|
|
644
|
-
name: inst.name,
|
|
645
|
-
pageId: page.id,
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
return instances;
|
|
649
|
-
`);
|
|
650
|
-
if (!result.success) throw new Error(result.error);
|
|
651
|
-
return result.result as Array<{ id: string; mainComponentId: string; name: string; pageId: string }>;
|
|
652
|
-
}
|
|
653
|
-
|
|
654
|
-
async getPrototypeConnections(): Promise<Array<{
|
|
655
|
-
fromId: string;
|
|
656
|
-
fromName: string;
|
|
657
|
-
toId: string;
|
|
658
|
-
toName: string;
|
|
659
|
-
trigger: Record<string, unknown>;
|
|
660
|
-
action: Record<string, unknown>;
|
|
661
|
-
}>> {
|
|
662
|
-
const result = await this.execute(`
|
|
663
|
-
const connections = [];
|
|
664
|
-
const page = figma.currentPage;
|
|
665
|
-
const frames = page.findAll(n => n.type === 'FRAME' || n.type === 'COMPONENT');
|
|
666
|
-
for (const frame of frames) {
|
|
667
|
-
const reactions = frame.reactions || [];
|
|
668
|
-
for (const r of reactions) {
|
|
669
|
-
if (r.action && r.action.type === 'NODE') {
|
|
670
|
-
const destNode = await figma.getNodeByIdAsync(r.action.destinationId);
|
|
671
|
-
connections.push({
|
|
672
|
-
fromId: frame.id,
|
|
673
|
-
fromName: frame.name,
|
|
674
|
-
toId: r.action.destinationId,
|
|
675
|
-
toName: destNode ? destNode.name : 'Unknown',
|
|
676
|
-
trigger: r.trigger || {},
|
|
677
|
-
action: r.action || {},
|
|
678
|
-
});
|
|
679
|
-
}
|
|
680
|
-
}
|
|
681
|
-
}
|
|
682
|
-
return connections;
|
|
683
|
-
`);
|
|
684
|
-
if (!result.success) throw new Error(result.error);
|
|
685
|
-
return result.result as Array<{
|
|
686
|
-
fromId: string;
|
|
687
|
-
fromName: string;
|
|
688
|
-
toId: string;
|
|
689
|
-
toName: string;
|
|
690
|
-
trigger: Record<string, unknown>;
|
|
691
|
-
action: Record<string, unknown>;
|
|
692
|
-
}>;
|
|
693
|
-
}
|
|
694
|
-
|
|
695
|
-
async disconnect(): Promise<void> {
|
|
696
|
-
if (this.ws) {
|
|
697
|
-
this.ws.close();
|
|
698
|
-
this.ws = null;
|
|
699
|
-
this.connected = false;
|
|
700
|
-
this.context = {
|
|
701
|
-
...this.context,
|
|
702
|
-
status: "disconnected",
|
|
703
|
-
lastUpdatedAt: Date.now(),
|
|
704
|
-
};
|
|
705
|
-
}
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
async hydrateContext(): Promise<FigmaContextSnapshot> {
|
|
709
|
-
if (!this.context.fileName || !this.context.currentPage) {
|
|
710
|
-
const status = await this.send<{
|
|
711
|
-
status: "connected";
|
|
712
|
-
fileName: string;
|
|
713
|
-
currentPage: FigmaPageSummary;
|
|
714
|
-
pageCount: number;
|
|
715
|
-
timestamp: number;
|
|
716
|
-
}>("getStatus");
|
|
717
|
-
this.context = {
|
|
718
|
-
...this.context,
|
|
719
|
-
status: "connected",
|
|
720
|
-
fileName: status.fileName,
|
|
721
|
-
currentPage: status.currentPage,
|
|
722
|
-
pageCount: status.pageCount,
|
|
723
|
-
lastUpdatedAt: status.timestamp,
|
|
724
|
-
};
|
|
725
|
-
this.hasHydratedStatus = true;
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
if (!this.hasHydratedSelection) {
|
|
729
|
-
const selection = await this.send<FigmaSelectionItem[]>("getSelection");
|
|
730
|
-
this.context = {
|
|
731
|
-
...this.context,
|
|
732
|
-
status: "connected",
|
|
733
|
-
selection,
|
|
734
|
-
lastUpdatedAt: Date.now(),
|
|
735
|
-
};
|
|
736
|
-
this.hasHydratedSelection = true;
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
return this.getContextSnapshot();
|
|
740
|
-
}
|
|
741
|
-
|
|
742
|
-
getContextSnapshot(): FigmaContextSnapshot {
|
|
743
|
-
return {
|
|
744
|
-
...this.context,
|
|
745
|
-
selection: [...this.context.selection],
|
|
746
|
-
currentPage: this.context.currentPage ? { ...this.context.currentPage } : undefined,
|
|
747
|
-
lastDocumentChange: this.context.lastDocumentChange
|
|
748
|
-
? {
|
|
749
|
-
documentChanges: this.context.lastDocumentChange.documentChanges.map((change) => ({ ...change })),
|
|
750
|
-
}
|
|
751
|
-
: undefined,
|
|
752
|
-
};
|
|
753
|
-
}
|
|
754
|
-
|
|
755
|
-
onEvent(
|
|
756
|
-
eventType: FigmaBridgeEventType | "*",
|
|
757
|
-
listener: (event: FigmaBridgeEvent) => void
|
|
758
|
-
): () => void {
|
|
759
|
-
const listeners = this.eventListeners.get(eventType) || new Set<(event: FigmaBridgeEvent) => void>();
|
|
760
|
-
listeners.add(listener);
|
|
761
|
-
this.eventListeners.set(eventType, listeners);
|
|
762
|
-
|
|
763
|
-
return () => {
|
|
764
|
-
const existing = this.eventListeners.get(eventType);
|
|
765
|
-
if (!existing) return;
|
|
766
|
-
existing.delete(listener);
|
|
767
|
-
if (existing.size === 0) {
|
|
768
|
-
this.eventListeners.delete(eventType);
|
|
769
|
-
}
|
|
770
|
-
};
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
handleIncomingMessage(raw: string) {
|
|
774
|
-
this.handleMessage(raw);
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
// ─── Navigation & Status ─────────────────────────────────────────────────
|
|
778
|
-
|
|
779
|
-
async getStatus(): Promise<Record<string, unknown>> {
|
|
780
|
-
// Return cached status only if context has been hydrated and looks fresh
|
|
781
|
-
if (this.hasHydratedStatus && this.context.fileName && this.context.currentPage) {
|
|
782
|
-
return {
|
|
783
|
-
status: this.context.status,
|
|
784
|
-
fileName: this.context.fileName,
|
|
785
|
-
currentPage: this.context.currentPage,
|
|
786
|
-
pageCount: this.context.pageCount,
|
|
787
|
-
timestamp: this.context.lastUpdatedAt || Date.now(),
|
|
788
|
-
};
|
|
789
|
-
}
|
|
790
|
-
|
|
791
|
-
// Use shorter timeout (5s) for status — don't wait 30s for a simple ping
|
|
792
|
-
const STATUS_TIMEOUT = 5000;
|
|
793
|
-
const status = await this.sendWithTimeout<{
|
|
794
|
-
status: "connected";
|
|
795
|
-
fileName: string;
|
|
796
|
-
currentPage: FigmaPageSummary;
|
|
797
|
-
pageCount: number;
|
|
798
|
-
timestamp: number;
|
|
799
|
-
}>("getStatus", {}, STATUS_TIMEOUT);
|
|
800
|
-
|
|
801
|
-
this.context = {
|
|
802
|
-
...this.context,
|
|
803
|
-
status: "connected",
|
|
804
|
-
fileName: status.fileName,
|
|
805
|
-
currentPage: status.currentPage,
|
|
806
|
-
pageCount: status.pageCount,
|
|
807
|
-
lastUpdatedAt: status.timestamp,
|
|
808
|
-
};
|
|
809
|
-
this.hasHydratedStatus = true;
|
|
810
|
-
|
|
811
|
-
return status;
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
async navigate(nodeId: string): Promise<Record<string, unknown>> {
|
|
815
|
-
return this.send("navigate", { nodeId });
|
|
816
|
-
}
|
|
817
|
-
|
|
818
|
-
async getSelection(): Promise<Array<{ id: string; name: string; type: string }>> {
|
|
819
|
-
if (this.hasHydratedSelection) {
|
|
820
|
-
return this.context.selection.map((item) => ({ ...item }));
|
|
821
|
-
}
|
|
822
|
-
|
|
823
|
-
// Use shorter timeout (5s) for selection queries
|
|
824
|
-
const selection = await this.sendWithTimeout<FigmaSelectionItem[]>("getSelection", {}, 5000);
|
|
825
|
-
this.context = {
|
|
826
|
-
...this.context,
|
|
827
|
-
status: "connected",
|
|
828
|
-
selection,
|
|
829
|
-
lastUpdatedAt: Date.now(),
|
|
830
|
-
};
|
|
831
|
-
this.hasHydratedSelection = true;
|
|
832
|
-
|
|
833
|
-
return selection;
|
|
834
|
-
}
|
|
835
|
-
|
|
836
|
-
async getCapabilities(force = false): Promise<Record<string, unknown>> {
|
|
837
|
-
if (!force && this.capabilitiesCache) {
|
|
838
|
-
return { ...this.capabilitiesCache };
|
|
839
|
-
}
|
|
840
|
-
|
|
841
|
-
const capabilities = await this.send<Record<string, unknown>>("getCapabilities");
|
|
842
|
-
this.capabilitiesCache = capabilities;
|
|
843
|
-
return { ...capabilities };
|
|
844
|
-
}
|
|
845
|
-
|
|
846
|
-
private async ensureVariablesApi(): Promise<void> {
|
|
847
|
-
const capabilities = await this.getCapabilities();
|
|
848
|
-
if (!capabilities.variablesApi || !capabilities.localVariablesApi) {
|
|
849
|
-
throw new Error(
|
|
850
|
-
"Figma Variables API is unavailable in the current plugin runtime. Reopen the bridge plugin in a Variables-capable Figma editor context."
|
|
851
|
-
);
|
|
852
|
-
}
|
|
853
|
-
}
|
|
854
|
-
|
|
855
|
-
// ─── Variable CRUD ───────────────────────────────────────────────────────
|
|
856
|
-
|
|
857
|
-
async createVariableCollection(name: string, initialModeName?: string): Promise<Record<string, unknown>> {
|
|
858
|
-
await this.ensureVariablesApi();
|
|
859
|
-
return this.send("createVariableCollection", { name, initialModeName });
|
|
860
|
-
}
|
|
861
|
-
|
|
862
|
-
async createVariable(
|
|
863
|
-
name: string,
|
|
864
|
-
collectionId: string,
|
|
865
|
-
resolvedType: string,
|
|
866
|
-
valuesByMode?: Record<string, unknown>,
|
|
867
|
-
description?: string,
|
|
868
|
-
scopes?: string[],
|
|
869
|
-
codeSyntax?: Record<string, string>
|
|
870
|
-
): Promise<Record<string, unknown>> {
|
|
871
|
-
await this.ensureVariablesApi();
|
|
872
|
-
return this.send("createVariable", { name, collectionId, resolvedType, valuesByMode, description, scopes, codeSyntax });
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
/**
|
|
876
|
-
* Set variable scoping (which properties this variable can be applied to).
|
|
877
|
-
* Scopes: ALL_SCOPES, ALL_FILLS, FRAME_FILL, SHAPE_FILL, TEXT_FILL, STROKE_COLOR,
|
|
878
|
-
* EFFECT_COLOR, WIDTH_HEIGHT, GAP, CORNER_RADIUS, OPACITY, STROKE_FLOAT,
|
|
879
|
-
* EFFECT_FLOAT, FONT_SIZE, LINE_HEIGHT, LETTER_SPACING, PARAGRAPH_SPACING,
|
|
880
|
-
* PARAGRAPH_INDENT, FONT_WEIGHT, FONT_FAMILY, FONT_STYLE, TEXT_CONTENT
|
|
881
|
-
*/
|
|
882
|
-
async setVariableScopes(variableId: string, scopes: string[]): Promise<Record<string, unknown>> {
|
|
883
|
-
await this.ensureVariablesApi();
|
|
884
|
-
return this.send("setVariableScopes", { variableId, scopes });
|
|
885
|
-
}
|
|
886
|
-
|
|
887
|
-
/**
|
|
888
|
-
* Set code syntax for a variable (platform-specific code identifiers).
|
|
889
|
-
* e.g. { WEB: "--color-brand-500", ANDROID: "colorBrand500", iOS: "Color.brand500" }
|
|
890
|
-
*/
|
|
891
|
-
async setVariableCodeSyntax(variableId: string, codeSyntax: Record<string, string>): Promise<Record<string, unknown>> {
|
|
892
|
-
await this.ensureVariablesApi();
|
|
893
|
-
return this.send("setVariableCodeSyntax", { variableId, codeSyntax });
|
|
894
|
-
}
|
|
895
|
-
|
|
896
|
-
/**
|
|
897
|
-
* Set variable description.
|
|
898
|
-
*/
|
|
899
|
-
async setVariableDescription(variableId: string, description: string): Promise<Record<string, unknown>> {
|
|
900
|
-
await this.ensureVariablesApi();
|
|
901
|
-
return this.send("setVariableDescription", { variableId, description });
|
|
902
|
-
}
|
|
903
|
-
|
|
904
|
-
async updateVariable(variableId: string, modeId: string, value: unknown): Promise<Record<string, unknown>> {
|
|
905
|
-
await this.ensureVariablesApi();
|
|
906
|
-
return this.send("updateVariable", { variableId, modeId, value });
|
|
907
|
-
}
|
|
908
|
-
|
|
909
|
-
async deleteVariable(variableId: string): Promise<Record<string, unknown>> {
|
|
910
|
-
await this.ensureVariablesApi();
|
|
911
|
-
return this.send("deleteVariable", { variableId });
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
async renameVariable(variableId: string, newName: string): Promise<Record<string, unknown>> {
|
|
915
|
-
await this.ensureVariablesApi();
|
|
916
|
-
return this.send("renameVariable", { variableId, newName });
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
async deleteVariableCollection(collectionId: string): Promise<Record<string, unknown>> {
|
|
920
|
-
await this.ensureVariablesApi();
|
|
921
|
-
return this.send("deleteVariableCollection", { collectionId });
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
async addMode(collectionId: string, modeName: string): Promise<Record<string, unknown>> {
|
|
925
|
-
await this.ensureVariablesApi();
|
|
926
|
-
return this.send("addMode", { collectionId, modeName });
|
|
927
|
-
}
|
|
928
|
-
|
|
929
|
-
async renameMode(collectionId: string, modeId: string, newName: string): Promise<Record<string, unknown>> {
|
|
930
|
-
await this.ensureVariablesApi();
|
|
931
|
-
return this.send("renameMode", { collectionId, modeId, newName });
|
|
932
|
-
}
|
|
933
|
-
|
|
934
|
-
async batchCreateVariables(
|
|
935
|
-
variables: Array<{
|
|
936
|
-
name: string;
|
|
937
|
-
collectionId: string;
|
|
938
|
-
resolvedType: string;
|
|
939
|
-
valuesByMode?: Record<string, unknown>;
|
|
940
|
-
description?: string;
|
|
941
|
-
scopes?: string[];
|
|
942
|
-
codeSyntax?: Record<string, string>;
|
|
943
|
-
}>
|
|
944
|
-
): Promise<Record<string, unknown>> {
|
|
945
|
-
await this.ensureVariablesApi();
|
|
946
|
-
return this.send("batchCreateVariables", { variables });
|
|
947
|
-
}
|
|
948
|
-
|
|
949
|
-
async batchUpdateVariables(
|
|
950
|
-
updates: Array<{ variableId: string; modeId: string; value: unknown }>
|
|
951
|
-
): Promise<Record<string, unknown>> {
|
|
952
|
-
await this.ensureVariablesApi();
|
|
953
|
-
return this.send("batchUpdateVariables", { updates });
|
|
954
|
-
}
|
|
955
|
-
|
|
956
|
-
// ─── Node Operations ─────────────────────────────────────────────────────
|
|
957
|
-
|
|
958
|
-
async cloneNode(nodeId: string, x?: number, y?: number): Promise<Record<string, unknown>> {
|
|
959
|
-
return this.send("cloneNode", { nodeId, x, y });
|
|
960
|
-
}
|
|
961
|
-
|
|
962
|
-
async deleteNode(nodeId: string): Promise<Record<string, unknown>> {
|
|
963
|
-
return this.send("deleteNode", { nodeId });
|
|
964
|
-
}
|
|
965
|
-
|
|
966
|
-
async moveNode(nodeId: string, x?: number, y?: number, parentId?: string): Promise<Record<string, unknown>> {
|
|
967
|
-
return this.send("moveNode", { nodeId, x, y, parentId });
|
|
968
|
-
}
|
|
969
|
-
|
|
970
|
-
async resizeNode(nodeId: string, width: number, height: number): Promise<Record<string, unknown>> {
|
|
971
|
-
return this.send("resizeNode", { nodeId, width, height });
|
|
972
|
-
}
|
|
973
|
-
|
|
974
|
-
async renameNode(nodeId: string, newName: string): Promise<Record<string, unknown>> {
|
|
975
|
-
return this.send("renameNode", { nodeId, newName });
|
|
976
|
-
}
|
|
977
|
-
|
|
978
|
-
async setFills(nodeId: string, fills: unknown[]): Promise<Record<string, unknown>> {
|
|
979
|
-
return this.send("setFills", { nodeId, fills });
|
|
980
|
-
}
|
|
981
|
-
|
|
982
|
-
async setStrokes(nodeId: string, strokes: unknown[], strokeWeight?: number): Promise<Record<string, unknown>> {
|
|
983
|
-
return this.send("setStrokes", { nodeId, strokes, strokeWeight });
|
|
984
|
-
}
|
|
985
|
-
|
|
986
|
-
async setText(nodeId: string, characters: string, fontSize?: number): Promise<Record<string, unknown>> {
|
|
987
|
-
return this.send("setText", { nodeId, characters, fontSize });
|
|
988
|
-
}
|
|
989
|
-
|
|
990
|
-
// ─── Component Operations ────────────────────────────────────────────────
|
|
991
|
-
|
|
992
|
-
async searchComponents(query: string, limit?: number): Promise<Array<Record<string, unknown>>> {
|
|
993
|
-
return this.send("searchComponents", { query, limit });
|
|
994
|
-
}
|
|
995
|
-
|
|
996
|
-
async instantiateComponent(
|
|
997
|
-
nodeId: string,
|
|
998
|
-
variant?: Record<string, string>,
|
|
999
|
-
x?: number,
|
|
1000
|
-
y?: number,
|
|
1001
|
-
parentId?: string
|
|
1002
|
-
): Promise<Record<string, unknown>> {
|
|
1003
|
-
return this.send("instantiateComponent", { nodeId, variant, x, y, parentId });
|
|
1004
|
-
}
|
|
1005
|
-
|
|
1006
|
-
async setDescription(nodeId: string, description: string): Promise<Record<string, unknown>> {
|
|
1007
|
-
return this.send("setDescription", { nodeId, description });
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
// ─── Variable Binding ───────────────────────────────────────────────────
|
|
1011
|
-
|
|
1012
|
-
async bindVariables(
|
|
1013
|
-
bindings: Array<{ nodeId: string; field: string; variableId: string; fillIndex?: number }>
|
|
1014
|
-
): Promise<{ bound: number; total: number }> {
|
|
1015
|
-
if (bindings.length === 0) return { bound: 0, total: 0 };
|
|
1016
|
-
|
|
1017
|
-
await this.ensureVariablesApi();
|
|
1018
|
-
|
|
1019
|
-
const script = `
|
|
1020
|
-
(async () => {
|
|
1021
|
-
const bindings = ${JSON.stringify(bindings)};
|
|
1022
|
-
let bound = 0;
|
|
1023
|
-
|
|
1024
|
-
for (const b of bindings) {
|
|
1025
|
-
const node = await figma.getNodeByIdAsync(b.nodeId);
|
|
1026
|
-
if (!node) continue;
|
|
1027
|
-
|
|
1028
|
-
const variable = await figma.variables.getVariableByIdAsync(b.variableId);
|
|
1029
|
-
if (!variable) continue;
|
|
1030
|
-
|
|
1031
|
-
if (b.field === 'fills' || b.field === 'strokes') {
|
|
1032
|
-
const idx = b.fillIndex ?? 0;
|
|
1033
|
-
if (node[b.field] && node[b.field].length > idx) {
|
|
1034
|
-
const paints = [...node[b.field]];
|
|
1035
|
-
paints[idx] = figma.variables.setBoundVariableForPaint(paints[idx], 'color', variable);
|
|
1036
|
-
node[b.field] = paints;
|
|
1037
|
-
bound++;
|
|
1038
|
-
}
|
|
1039
|
-
} else {
|
|
1040
|
-
try {
|
|
1041
|
-
node.setBoundVariable(b.field, variable.id);
|
|
1042
|
-
bound++;
|
|
1043
|
-
} catch (e) { /* skip unsupported fields */ }
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
|
|
1047
|
-
return { bound, total: bindings.length };
|
|
1048
|
-
})();
|
|
1049
|
-
`.trim();
|
|
1050
|
-
|
|
1051
|
-
const result = await this.execute(script);
|
|
1052
|
-
if (!result.success) {
|
|
1053
|
-
return { bound: 0, total: bindings.length };
|
|
1054
|
-
}
|
|
1055
|
-
return result.result as { bound: number; total: number };
|
|
1056
|
-
}
|
|
1057
|
-
|
|
1058
|
-
// ─── Design System Extraction ────────────────────────────────────────────
|
|
1059
|
-
|
|
1060
|
-
async getVariables(collectionId?: string, verbosity?: string): Promise<unknown[]> {
|
|
1061
|
-
await this.ensureVariablesApi();
|
|
1062
|
-
return this.send("getVariables", { collectionId, verbosity });
|
|
1063
|
-
}
|
|
1064
|
-
|
|
1065
|
-
async getStyles(): Promise<Record<string, unknown>> {
|
|
1066
|
-
return this.send("getStyles");
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
// ─── Deep Node Serialization & Batch Read ──────────────────────────────
|
|
1070
|
-
|
|
1071
|
-
/**
|
|
1072
|
-
* Get a node with full recursive child data up to maxDepth.
|
|
1073
|
-
* Unlike getNode() which returns 1-level children as {id, name, type},
|
|
1074
|
-
* this returns the full property set for every descendant.
|
|
1075
|
-
*/
|
|
1076
|
-
async getNodeDeep(nodeId: string, maxDepth: number = 10): Promise<unknown> {
|
|
1077
|
-
const result = await this.execute(`
|
|
1078
|
-
(async () => {
|
|
1079
|
-
const maxDepth = ${Math.min(maxDepth, 20)};
|
|
1080
|
-
|
|
1081
|
-
async function serializeNode(node, depth) {
|
|
1082
|
-
const data = {
|
|
1083
|
-
id: node.id,
|
|
1084
|
-
name: node.name,
|
|
1085
|
-
type: node.type,
|
|
1086
|
-
visible: node.visible !== false,
|
|
1087
|
-
};
|
|
1088
|
-
|
|
1089
|
-
// Geometry
|
|
1090
|
-
if ('x' in node) { data.x = node.x; data.y = node.y; }
|
|
1091
|
-
if ('width' in node) { data.width = node.width; data.height = node.height; }
|
|
1092
|
-
if ('absoluteBoundingBox' in node) data.absoluteBoundingBox = node.absoluteBoundingBox;
|
|
1093
|
-
if ('absoluteRenderBounds' in node) data.absoluteRenderBounds = node.absoluteRenderBounds;
|
|
1094
|
-
|
|
1095
|
-
// Visual (fills/strokes/effects can be figma.mixed Symbol — guard with try-catch)
|
|
1096
|
-
try { if ('fills' in node && node.fills && typeof node.fills !== 'symbol') data.fills = JSON.parse(JSON.stringify(node.fills)); } catch (e) {}
|
|
1097
|
-
try { if ('strokes' in node && node.strokes && typeof node.strokes !== 'symbol') data.strokes = JSON.parse(JSON.stringify(node.strokes)); } catch (e) {}
|
|
1098
|
-
try { if ('effects' in node && node.effects && typeof node.effects !== 'symbol') data.effects = JSON.parse(JSON.stringify(node.effects)); } catch (e) {}
|
|
1099
|
-
if ('opacity' in node) data.opacity = node.opacity;
|
|
1100
|
-
try { if ('cornerRadius' in node && typeof node.cornerRadius !== 'symbol') data.cornerRadius = node.cornerRadius; } catch (e) {}
|
|
1101
|
-
try { if ('strokeWeight' in node && typeof node.strokeWeight !== 'symbol') data.strokeWeight = node.strokeWeight; } catch (e) {}
|
|
1102
|
-
|
|
1103
|
-
// Layout
|
|
1104
|
-
if ('layoutMode' in node) {
|
|
1105
|
-
data.layoutMode = node.layoutMode;
|
|
1106
|
-
data.primaryAxisSizingMode = node.primaryAxisSizingMode;
|
|
1107
|
-
data.counterAxisSizingMode = node.counterAxisSizingMode;
|
|
1108
|
-
data.paddingTop = node.paddingTop;
|
|
1109
|
-
data.paddingRight = node.paddingRight;
|
|
1110
|
-
data.paddingBottom = node.paddingBottom;
|
|
1111
|
-
data.paddingLeft = node.paddingLeft;
|
|
1112
|
-
data.itemSpacing = node.itemSpacing;
|
|
1113
|
-
if (node.primaryAxisAlignItems) data.primaryAxisAlignItems = node.primaryAxisAlignItems;
|
|
1114
|
-
if (node.counterAxisAlignItems) data.counterAxisAlignItems = node.counterAxisAlignItems;
|
|
1115
|
-
}
|
|
1116
|
-
|
|
1117
|
-
// Text (all text properties can be figma.mixed Symbol for mixed-style text)
|
|
1118
|
-
if (node.type === 'TEXT') {
|
|
1119
|
-
data.characters = node.characters;
|
|
1120
|
-
try {
|
|
1121
|
-
if (typeof node.fontSize !== 'symbol') data.fontSize = node.fontSize;
|
|
1122
|
-
if (typeof node.fontName !== 'symbol') data.fontName = JSON.parse(JSON.stringify(node.fontName));
|
|
1123
|
-
if (typeof node.lineHeight !== 'symbol') data.lineHeight = node.lineHeight;
|
|
1124
|
-
if (typeof node.letterSpacing !== 'symbol') data.letterSpacing = node.letterSpacing;
|
|
1125
|
-
if (typeof node.textAlignHorizontal !== 'symbol') data.textAlignHorizontal = node.textAlignHorizontal;
|
|
1126
|
-
if (typeof node.textAlignVertical !== 'symbol') data.textAlignVertical = node.textAlignVertical;
|
|
1127
|
-
} catch (e) { /* mixed styles */ }
|
|
1128
|
-
}
|
|
1129
|
-
|
|
1130
|
-
// Component metadata
|
|
1131
|
-
if ('componentPropertyReferences' in node) data.componentPropertyReferences = node.componentPropertyReferences;
|
|
1132
|
-
if ('variantProperties' in node && node.variantProperties) data.variantProperties = node.variantProperties;
|
|
1133
|
-
if (node.type === 'INSTANCE') {
|
|
1134
|
-
try {
|
|
1135
|
-
const main = await node.getMainComponentAsync();
|
|
1136
|
-
if (main) {
|
|
1137
|
-
data.mainComponentId = main.id;
|
|
1138
|
-
data.mainComponentName = main.name;
|
|
1139
|
-
}
|
|
1140
|
-
} catch (e) { /* mainComponent unavailable */ }
|
|
1141
|
-
}
|
|
1142
|
-
if (node.description) data.description = node.description;
|
|
1143
|
-
|
|
1144
|
-
// Variable bindings
|
|
1145
|
-
if ('boundVariables' in node && node.boundVariables) {
|
|
1146
|
-
try {
|
|
1147
|
-
const bv = {};
|
|
1148
|
-
for (const [prop, binding] of Object.entries(node.boundVariables)) {
|
|
1149
|
-
if (binding && typeof binding === 'object' && 'id' in binding) {
|
|
1150
|
-
bv[prop] = { id: binding.id, type: binding.type };
|
|
1151
|
-
} else if (Array.isArray(binding)) {
|
|
1152
|
-
bv[prop] = binding.map(b => b && typeof b === 'object' && 'id' in b ? { id: b.id } : null).filter(Boolean);
|
|
1153
|
-
}
|
|
1154
|
-
}
|
|
1155
|
-
if (Object.keys(bv).length > 0) data.boundVariables = bv;
|
|
1156
|
-
} catch (e) { /* skip */ }
|
|
1157
|
-
}
|
|
1158
|
-
|
|
1159
|
-
// Recursive children
|
|
1160
|
-
if ('children' in node && node.children && depth < maxDepth) {
|
|
1161
|
-
data.children = [];
|
|
1162
|
-
for (const child of node.children) {
|
|
1163
|
-
data.children.push(await serializeNode(child, depth + 1));
|
|
1164
|
-
}
|
|
1165
|
-
data.childCount = node.children.length;
|
|
1166
|
-
} else if ('children' in node && node.children) {
|
|
1167
|
-
data.childCount = node.children.length;
|
|
1168
|
-
data.children = node.children.map(c => ({ id: c.id, name: c.name, type: c.type }));
|
|
1169
|
-
data.truncatedAtDepth = depth;
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
return data;
|
|
1173
|
-
}
|
|
1174
|
-
|
|
1175
|
-
const root = await figma.getNodeByIdAsync(${JSON.stringify(nodeId)});
|
|
1176
|
-
if (!root) throw new Error('Node not found: ' + ${JSON.stringify(nodeId)});
|
|
1177
|
-
return await serializeNode(root, 0);
|
|
1178
|
-
})();
|
|
1179
|
-
`, 60000); // 60s timeout for deep trees
|
|
1180
|
-
|
|
1181
|
-
if (!result.success) throw new Error(result.error);
|
|
1182
|
-
return result.result;
|
|
1183
|
-
}
|
|
1184
|
-
|
|
1185
|
-
/**
|
|
1186
|
-
* Batch read multiple nodes in a single round-trip.
|
|
1187
|
-
* Returns a map of nodeId → serialized node data (1-level deep children).
|
|
1188
|
-
*/
|
|
1189
|
-
async batchGetNodes(nodeIds: string[], includeChildren: boolean = true): Promise<Record<string, unknown>> {
|
|
1190
|
-
if (nodeIds.length === 0) return {};
|
|
1191
|
-
|
|
1192
|
-
const result = await this.execute(`
|
|
1193
|
-
(async () => {
|
|
1194
|
-
const ids = ${JSON.stringify(nodeIds.slice(0, 200))};
|
|
1195
|
-
const includeChildren = ${includeChildren};
|
|
1196
|
-
const results = {};
|
|
1197
|
-
|
|
1198
|
-
for (const id of ids) {
|
|
1199
|
-
const node = await figma.getNodeByIdAsync(id);
|
|
1200
|
-
if (!node) { results[id] = null; continue; }
|
|
1201
|
-
|
|
1202
|
-
const data = {
|
|
1203
|
-
id: node.id,
|
|
1204
|
-
name: node.name,
|
|
1205
|
-
type: node.type,
|
|
1206
|
-
visible: node.visible !== false,
|
|
1207
|
-
};
|
|
1208
|
-
|
|
1209
|
-
if ('x' in node) { data.x = node.x; data.y = node.y; }
|
|
1210
|
-
if ('width' in node) { data.width = node.width; data.height = node.height; }
|
|
1211
|
-
if ('fills' in node && node.fills) data.fills = JSON.parse(JSON.stringify(node.fills));
|
|
1212
|
-
if ('strokes' in node && node.strokes) data.strokes = JSON.parse(JSON.stringify(node.strokes));
|
|
1213
|
-
if ('effects' in node && node.effects) data.effects = JSON.parse(JSON.stringify(node.effects));
|
|
1214
|
-
if ('opacity' in node) data.opacity = node.opacity;
|
|
1215
|
-
if ('cornerRadius' in node) data.cornerRadius = node.cornerRadius;
|
|
1216
|
-
|
|
1217
|
-
if ('layoutMode' in node) {
|
|
1218
|
-
data.layoutMode = node.layoutMode;
|
|
1219
|
-
data.paddingTop = node.paddingTop;
|
|
1220
|
-
data.paddingRight = node.paddingRight;
|
|
1221
|
-
data.paddingBottom = node.paddingBottom;
|
|
1222
|
-
data.paddingLeft = node.paddingLeft;
|
|
1223
|
-
data.itemSpacing = node.itemSpacing;
|
|
1224
|
-
}
|
|
1225
|
-
|
|
1226
|
-
if (node.type === 'TEXT') {
|
|
1227
|
-
data.characters = node.characters;
|
|
1228
|
-
try {
|
|
1229
|
-
data.fontSize = node.fontSize;
|
|
1230
|
-
data.fontName = JSON.parse(JSON.stringify(node.fontName));
|
|
1231
|
-
} catch (e) {}
|
|
1232
|
-
}
|
|
1233
|
-
|
|
1234
|
-
if (includeChildren && 'children' in node && node.children) {
|
|
1235
|
-
data.childCount = node.children.length;
|
|
1236
|
-
data.children = node.children.map(c => ({
|
|
1237
|
-
id: c.id,
|
|
1238
|
-
name: c.name,
|
|
1239
|
-
type: c.type,
|
|
1240
|
-
visible: c.visible !== false,
|
|
1241
|
-
x: 'x' in c ? c.x : undefined,
|
|
1242
|
-
y: 'y' in c ? c.y : undefined,
|
|
1243
|
-
width: 'width' in c ? c.width : undefined,
|
|
1244
|
-
height: 'height' in c ? c.height : undefined,
|
|
1245
|
-
}));
|
|
1246
|
-
}
|
|
1247
|
-
|
|
1248
|
-
results[id] = data;
|
|
1249
|
-
}
|
|
1250
|
-
|
|
1251
|
-
return results;
|
|
1252
|
-
})();
|
|
1253
|
-
`, 60000);
|
|
1254
|
-
|
|
1255
|
-
if (!result.success) throw new Error(result.error);
|
|
1256
|
-
return result.result as Record<string, unknown>;
|
|
1257
|
-
}
|
|
1258
|
-
|
|
1259
|
-
// ─── Multi-Mode Variable Binding ──────────────────────────────────────────
|
|
1260
|
-
|
|
1261
|
-
/**
|
|
1262
|
-
* Bind semantic variables to nodes AND set the explicit variable mode on a
|
|
1263
|
-
* container frame. This is the key method for theme-switching support.
|
|
1264
|
-
*
|
|
1265
|
-
* Unlike bindVariables() which just binds variables without mode awareness,
|
|
1266
|
-
* this method:
|
|
1267
|
-
* 1. Binds semantic variables (which have Light/Dark mode values)
|
|
1268
|
-
* 2. Sets the explicit mode on the target frame so children resolve correctly
|
|
1269
|
-
*/
|
|
1270
|
-
async bindVariablesMultiMode(
|
|
1271
|
-
bindings: Array<{ nodeId: string; field: string; variableId: string; fillIndex?: number }>,
|
|
1272
|
-
targetFrameId: string,
|
|
1273
|
-
collectionId: string,
|
|
1274
|
-
activeModeId: string
|
|
1275
|
-
): Promise<{ bound: number; total: number; modeSet: boolean; errors?: string[] }> {
|
|
1276
|
-
if (bindings.length === 0 && !targetFrameId) {
|
|
1277
|
-
return { bound: 0, total: 0, modeSet: false };
|
|
1278
|
-
}
|
|
1279
|
-
|
|
1280
|
-
await this.ensureVariablesApi();
|
|
1281
|
-
|
|
1282
|
-
// Import from token-binder for script generation
|
|
1283
|
-
const { buildMultiModeBindingScript } = await import("./token-binder.js");
|
|
1284
|
-
const script = buildMultiModeBindingScript(
|
|
1285
|
-
bindings.map((b) => ({
|
|
1286
|
-
nodeId: b.nodeId,
|
|
1287
|
-
field: b.field,
|
|
1288
|
-
fillIndex: b.fillIndex,
|
|
1289
|
-
semanticVariableId: b.variableId,
|
|
1290
|
-
})),
|
|
1291
|
-
targetFrameId,
|
|
1292
|
-
collectionId,
|
|
1293
|
-
activeModeId
|
|
1294
|
-
);
|
|
1295
|
-
|
|
1296
|
-
const result = await this.execute(script);
|
|
1297
|
-
if (!result.success) {
|
|
1298
|
-
return { bound: 0, total: bindings.length, modeSet: false, errors: [result.error ?? "Unknown error"] };
|
|
1299
|
-
}
|
|
1300
|
-
return result.result as { bound: number; total: number; modeSet: boolean; errors?: string[] };
|
|
1301
|
-
}
|
|
1302
|
-
|
|
1303
|
-
/**
|
|
1304
|
-
* Switch a frame's variable mode (theme switching).
|
|
1305
|
-
* All children with bound variables will resolve to the new mode's values.
|
|
1306
|
-
*/
|
|
1307
|
-
async switchMode(
|
|
1308
|
-
frameId: string,
|
|
1309
|
-
collectionId: string,
|
|
1310
|
-
modeId: string
|
|
1311
|
-
): Promise<{ success: boolean; error?: string }> {
|
|
1312
|
-
await this.ensureVariablesApi();
|
|
1313
|
-
const { buildModeSwitchScript } = await import("./token-binder.js");
|
|
1314
|
-
const script = buildModeSwitchScript(frameId, collectionId, modeId);
|
|
1315
|
-
const result = await this.execute(script);
|
|
1316
|
-
if (!result.success) {
|
|
1317
|
-
return { success: false, error: result.error };
|
|
1318
|
-
}
|
|
1319
|
-
return result.result as { success: boolean; error?: string };
|
|
1320
|
-
}
|
|
1321
|
-
|
|
1322
|
-
/**
|
|
1323
|
-
* List all modes for a variable collection.
|
|
1324
|
-
* Returns mode IDs and names so callers can pick one for switchMode().
|
|
1325
|
-
*/
|
|
1326
|
-
async listModes(collectionId: string): Promise<unknown> {
|
|
1327
|
-
await this.ensureVariablesApi();
|
|
1328
|
-
const { buildListModesScript } = await import("./token-binder.js");
|
|
1329
|
-
const script = buildListModesScript(collectionId);
|
|
1330
|
-
const result = await this.execute(script);
|
|
1331
|
-
if (!result.success) throw new Error(result.error);
|
|
1332
|
-
return result.result;
|
|
1333
|
-
}
|
|
1334
|
-
|
|
1335
|
-
// ─── P0: Enrichment & Compression ─────────────────────────────────────
|
|
1336
|
-
|
|
1337
|
-
/**
|
|
1338
|
-
* Get enriched design system data — tokens organized semantically,
|
|
1339
|
-
* components categorized, relationships mapped. Cached for 5 minutes.
|
|
1340
|
-
*/
|
|
1341
|
-
async getEnrichedDesignSystem(): Promise<EnrichedDesignSystem> {
|
|
1342
|
-
return this.cache.getEnrichedDesignSystem();
|
|
1343
|
-
}
|
|
1344
|
-
|
|
1345
|
-
/**
|
|
1346
|
-
* Get a node with resolved styles — fills as hex, typography categorized,
|
|
1347
|
-
* spacing grid-snapped, radius categorized.
|
|
1348
|
-
*/
|
|
1349
|
-
async getNodeEnriched(nodeId: string): Promise<{ node: FigmaNode; styles: ResolvedStyle }> {
|
|
1350
|
-
const [node, tokens] = await Promise.all([
|
|
1351
|
-
this.cache.getNode(nodeId),
|
|
1352
|
-
this.cache.getTokens(),
|
|
1353
|
-
]);
|
|
1354
|
-
const styles = resolveStyles(node, tokens);
|
|
1355
|
-
return { node, styles };
|
|
1356
|
-
}
|
|
1357
|
-
|
|
1358
|
-
/**
|
|
1359
|
-
* Compress any response payload to fit within AI context window limits.
|
|
1360
|
-
* Automatically selects compression tier based on byte size.
|
|
1361
|
-
*/
|
|
1362
|
-
compressForAI(data: unknown, forceTier?: CompressionTier): CompressedResponse {
|
|
1363
|
-
return compressResponse(data, { forceTier });
|
|
1364
|
-
}
|
|
1365
|
-
|
|
1366
|
-
// ─── Create Child Node ───────────────────────────────────────────────────
|
|
1367
|
-
|
|
1368
|
-
async createChild(
|
|
1369
|
-
childType: string,
|
|
1370
|
-
parentId?: string,
|
|
1371
|
-
name?: string,
|
|
1372
|
-
width?: number,
|
|
1373
|
-
height?: number,
|
|
1374
|
-
x?: number,
|
|
1375
|
-
y?: number,
|
|
1376
|
-
characters?: string
|
|
1377
|
-
): Promise<Record<string, unknown>> {
|
|
1378
|
-
return this.send("createChild", { childType, parentId, name, width, height, x, y, characters });
|
|
1379
|
-
}
|
|
1380
|
-
|
|
1381
|
-
// ─── Swarm Agent Cursor Methods ──────────────────────────────────────────
|
|
1382
|
-
|
|
1383
|
-
async spawnAgentCursor(agentId: string, x: number, y: number): Promise<void> {
|
|
1384
|
-
await this.send("spawnAgentCursor", { agentId, x, y });
|
|
1385
|
-
}
|
|
1386
|
-
|
|
1387
|
-
async moveAgentCursor(agentId: string, x: number, y: number, animate = true, durationMs = 250): Promise<void> {
|
|
1388
|
-
await this.send("moveAgentCursor", { agentId, x, y, animate, durationMs });
|
|
1389
|
-
}
|
|
1390
|
-
|
|
1391
|
-
async updateAgentLabel(agentId: string, label: string): Promise<void> {
|
|
1392
|
-
await this.send("updateAgentLabel", { agentId, label });
|
|
1393
|
-
}
|
|
1394
|
-
|
|
1395
|
-
async removeAgentCursor(agentId: string): Promise<void> {
|
|
1396
|
-
await this.send("removeAgentCursor", { agentId });
|
|
1397
|
-
}
|
|
1398
|
-
|
|
1399
|
-
async postAgentChat(agentId: string, message: string, x: number, y: number): Promise<string> {
|
|
1400
|
-
const result = await this.send<{ noteId: string }>("agentChat", { agentId, message, x, y });
|
|
1401
|
-
return result.noteId;
|
|
1402
|
-
}
|
|
1403
|
-
|
|
1404
|
-
async cleanupSwarm(): Promise<void> {
|
|
1405
|
-
await this.send("cleanupAgentCursors", {});
|
|
1406
|
-
}
|
|
1407
|
-
}
|
|
1408
|
-
|
|
1409
|
-
// Singleton bridge instance
|
|
1410
|
-
let bridgeInstance: FigmaBridge | null = null;
|
|
1411
|
-
|
|
1412
|
-
export async function getBridge(): Promise<FigmaBridge> {
|
|
1413
|
-
if (!bridgeInstance) {
|
|
1414
|
-
bridgeInstance = new FigmaBridge();
|
|
1415
|
-
}
|
|
1416
|
-
await bridgeInstance.connect();
|
|
1417
|
-
return bridgeInstance;
|
|
1418
|
-
}
|