From d23c8b645661fef7d0e156bcc230ab2ae1e9ebe0 Mon Sep 17 00:00:00 2001 From: B-uchi Date: Wed, 18 Nov 2026 22:09:07 +0100 Subject: [PATCH 01/65] M8: Code Connect for agents MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A link can say where the code component lives (`source`, a path inside the repository), what a property's design values are called in code (`values`: {"State": {"Hover": "hovered"}} renders `hovered`; a value without an entry keeps its design name) and `notes`, through codeConnect.set, the connect file and Inspect ▸ Connect to code (a Source file field and a row per variant value). The fields are skipped when empty, so existing files and exports don't change. codeConnect.list and the MCP tool component_mappings say which code component each design component is: import line, name, ready snippet, properties and variants, and with a repository root whether each source file is there. sync_mappings imports or exports a repository's connect file. codeConnect.export {path} keeps the file's other top-level keys and the links of components this design doesn't have, so several designs can share one file. --- ROADMAP.md | 1 + crates/doc/src/node.rs | 9 ++ crates/engine/src/code_connect_tests.rs | 121 ++++++++++++++++ crates/engine/src/dev/code_connect.rs | 174 ++++++++++++++++++++++-- crates/mcp/src/tools.rs | 53 ++++++++ crates/mcp/tests/it/code_connect.rs | 37 +++++ crates/mcp/tests/it/main.rs | 2 + crates/ui-egui/locales/en.toml | 3 + crates/ui-egui/src/panels/dev_mode.rs | 48 +++++++ docs/mcp.md | 16 ++- 10 files changed, 447 insertions(+), 17 deletions(-) create mode 100644 crates/mcp/tests/it/code_connect.rs diff --git a/ROADMAP.md b/ROADMAP.md index 2286b0c6e..cbd89bc8d 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -367,6 +367,7 @@ Ranked; detail and milestones in [`docs/roadmap.md`](docs/roadmap.md). | Date | Change | Summary | |---|---|---| | 2026-11-18 | minor | M8: design context for AI agents. `inspect.designContext` and the MCP tool `design_context` read the selection (or `ids`, or a `scope`) in one call: a tree where each layer keeps apart `explicit` values, `derived` ones layout worked out and `unsupported` ones with no direct CSS equivalent, joined with the variables (every mode's stored and resolved value), styles, components, fonts and assets it uses, and what the design leaves `missing`. A connected instance gives its code instead of its layers, and a result that wouldn't fit the client asks for fewer layers. Numbers unchanged | +| 2026-11-19 | minor | M8: Code Connect for agents. A link can name its source file, what variant values are called in code (Primary becomes `primary` in the snippet) and notes, in the Inspect tab, `codeConnect.set` and `figcraft.connect.json`. `codeConnect.list` and the MCP tool `component_mappings` say which code component each design component is, and whether its source file exists; `sync_mappings` imports and exports a repository's file. `codeConnect.export {path}` keeps the file's other keys and the links of components the design doesn't have, so several designs can share one file. Numbers unchanged | | 2026-11-18 | minor | M3, gap 10: library analytics count insertions and detachments of styles and variables. Applying one, or copying or duplicating content that uses it, inserts it; the Detach button detaches it; an instance counts as its component alone. Show usage and `library.usage` give them per style and variable over the chosen period. Modes aren't counted. Numbers unchanged. Tests: `library_asset_counts_tests`, `library_assets_tests`. | | 2026-11-18 | minor | M9, gap 6: the browser build uses team libraries and plugin directories on a server, through its WebSocket requests: adding a library by its link, its styles and components, publishing to it, Show usage and the reports a file sends; browsing a directory and installing from it, for the page only. Scripts through `window.figcraft` still can't, as commands there don't wait for the server. Numbers unchanged. Tests: `exchange_tests`. | | 2026-11-17 | minor | M6: Android Vector Drawable XML serialization uses the shared writer; application projection and colour policy remain local. Byte-parity tests cover full documents and generated deep wrappers; no new format capability or incumbent certification. | diff --git a/crates/doc/src/node.rs b/crates/doc/src/node.rs index bea172277..650966626 100644 --- a/crates/doc/src/node.rs +++ b/crates/doc/src/node.rs @@ -250,6 +250,15 @@ pub struct CodeLink { pub name: String, /// The snippet with `{property}` and `{property ? "a" : "b"}` placeholders. pub template: String, + /// Where the code component lives, relative to the code repository. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub source: String, + /// What a property's design values are called in code: property → design value → code value. + #[serde(default, skip_serializing_if = "BTreeMap::is_empty")] + pub values: BTreeMap>, + /// Notes for whoever uses the component. + #[serde(default, skip_serializing_if = "String::is_empty")] + pub notes: String, } /// A dev resource: a link for developers attached to a layer. The URL is data only: it is shown diff --git a/crates/engine/src/code_connect_tests.rs b/crates/engine/src/code_connect_tests.rs index 4474fe03a..9750db813 100644 --- a/crates/engine/src/code_connect_tests.rs +++ b/crates/engine/src/code_connect_tests.rs @@ -298,3 +298,124 @@ fn many_entries_for_one_component_each_link() { let i = idv(&ex(&mut s, "instance.create", json!({"component": hover, "x": 0, "y": 500}))); assert_eq!(code(&mut s, i, "react"), "", 3) { edit = Some(("template", v)); } widgets::note(ui, tr("{property} is its value; {property ? \"a\" : \"b\"} picks a string. Other {…} stays as written."), t.text_secondary); + variant_values(app, ui, doc, owner, lang, &link); if let Some((key, value)) = edit { let mut params = params; params[key] = json!(value); @@ -499,6 +509,44 @@ pub fn connect_section(app: &mut FigApp, ui: &mut Ui, doc: &Document, id: NodeId ui.add_space(4.0); } +/// The variant values of a connected component and what code calls them: a value left empty +/// stays as the design names it. +fn variant_values(app: &mut FigApp, ui: &mut Ui, doc: &Document, owner: NodeId, lang: CodeLang, link: &CodeLink) { + let properties = code_connect::variants(doc, owner); + if properties.is_empty() { + return; + } + let mut change: Option<(String, String, String)> = None; + widgets::sublabels(ui, tr("Variant value"), Some(tr("In code"))); + let col = widgets::col_width(ui.available_width()); + for (property, options) in &properties { + for option in options { + let current = link.values.get(property).and_then(|m| m.get(option)).map_or("", String::as_str); + row(ui, 32.0, |ui| { + ui.spacing_mut().item_spacing.x = 8.0; + ui.allocate_ui_with_layout(vec2(col, 24.0), egui::Layout::left_to_right(egui::Align::Center), |ui| { + ui.set_min_width(col); + ui.add(egui::Label::new(format!("{property}: {option}")).truncate()); + }); + if let Some(v) = widgets::text_field(ui, Id::unique(("code_connect_value", owner, lang.id(), property, option)), current, col, option) + { + change = Some((property.clone(), option.clone(), v)); + } + }); + } + } + let Some((property, option, value)) = change else { return }; + let mut values = link.values.clone(); + let mapped = values.entry(property).or_default(); + if value.trim().is_empty() { + mapped.remove(&option); + } else { + mapped.insert(option, value.trim().to_string()); + } + values.retain(|_, m| !m.is_empty()); + app.run("codeConnect.set", json!({"id": owner, "language": lang.id(), "values": values})); +} + /// Dev Mode plugins: the installed plugins that only read the document, run from the Inspect tab. pub fn plugins_section(app: &mut FigApp, ui: &mut Ui) { let runnable: Vec<(String, String, String)> = app diff --git a/docs/mcp.md b/docs/mcp.md index c1f3992a9..b2aba1f43 100644 --- a/docs/mcp.md +++ b/docs/mcp.md @@ -409,6 +409,8 @@ a later step can use `{"$step": 0, "field": "ids/0"}` to target the first match. | `inspect_document {depth, allPages, fields?, outline?}` | The document as JSON (its `id`, layer tree, styles, selection, history); `fields` selects layer properties, `outline: true` gives a compact tree (below). The tools that act on a document also take `document` (see Naming the document below). | | `get_node {id, depth, fields?, outline?}` | One layer as JSON; takes the same `fields` and `outline` options; `pluginData` (`{key: value}`, values as strings) when the layer or page has layer data written with `set_properties {props: {pluginData}}`. | | `design_context {ids?, scope?, depth?, maxNodes?, maxBytes?}` | Everything needed to build a design in one read: see [Design context](#design-context). | +| `component_mappings {ids?, unmapped?, root?}` | Which code component each design component is: `import`, `name`, `source` (and `sourceFound` under `root`, default the server's folder), a ready `snippet`, `properties` with variants, and `values` that need translating. | +| `sync_mappings {direction, path?}` | Apply a code repository's `figcraft.connect.json` to the open design (`import`) or write the design's mappings to it (`export`); `path` defaults to that file in the server's folder. | | `select {ids}` | Replace the selection (`[]` deselects). | | `export {ids, format, scale \| size, path?, bleed?, cropMarks?, bleedMarks?, cmyk?, contentsOnly?, includeBoundingBox?, svgOutlineText?, svgIdAttribute?, svgSimplifyStroke?, vdCompat?, colorProfile?, quality?}` | png, jpg, svg, webp, pdf, xml (an Android Vector Drawable: `svgIdAttribute` writes `android:name`, `vdCompat` is `bestEffort` (default: approximated with a warning), `exact` (left out with a warning) or `ignore` (no warnings: overlapping layer opacity dropped, even-odd fills kept as `fillType="evenOdd"`, diamond gradients left out), and the result lists `warnings` for what was changed or left out), or `fig`: the whole file as `.fig`, experimental (see Exporting `.fig` files below); `size` is `2x`, `512w` or `512h`; `quality` is the JPG quality, 1 to 100 (default 90). The image covers what the layers paint (shadows towards their offset, strokes, unclipped children, a mask group's mask; hidden layers take no room; a slice is its area with everything under it, or only the frame or group it sits in while `contentsOnly` is on); `contentsOnly: false` also draws every other layer showing in that area, `includeBoundingBox` exports the layers' own boxes; SVG: `svgOutlineText: false` keeps ``, `svgIdAttribute` writes unique ids from layer names, `svgSimplifyStroke: false` writes every stroke as an outline; png, jpg and webp: `colorProfile` is `sameAsFile` (default: the document's profile), `srgb` or `displayP3`, the pixels converted (out-of-gamut colors clip) and the file tagged for it (PNG: `sRGB` chunk, or ICC and `cICP` for Display P3; JPG: ICC profile; WebP: ICC profile for Display P3, none for sRGB) (SVG, XML and RGB PDF output are always sRGB; PDF with `cmyk` is DeviceCMYK). The same options go in export settings (`export.add` / `export.update`). PDF: frames one page each (sized in points), anything else together on one page; print options: `bleed` (points or a length like `"3mm"`, `"0.125in"`), crop and bleed marks, CMYK colour (see Exports below), also taken by `file.export`, `file.exportLayers`, `file.exportFramesPdf` and stored per export setting by `export.add` / `export.update`; `run_command` `file.exportFramesPdf {page?, path?}` writes every top-level frame of a page. Without `path`, images come back as MCP image content, SVG as text, XML as text followed by `{format, bytes, warnings}`. Layers' own export settings: `run_command` with `export.add` / `export.update {index, size?, suffix?, format?}` / `export.remove` and `file.exportLayers {ids?, dir?, assets?}` (every layer once per setting, named `.`, a slash in a layer name making a folder; `assets: true` exports the Inspect tab's assets instead: icons as SVG, images as PNG). | | `screenshot {ids?, scale?, maxSize?, window?}` | PNG of the page (with background) or of layers, scaled to fit `maxSize` (default 1600). `window: true` captures the app window in front when attached, a window id (see Windows below) that window. | @@ -1860,11 +1862,19 @@ permissions: without it nothing is downloaded); `plugin.directory.publish {path `@acme/ui` gives `import { Button } from "@acme/ui";` then ``. `codeConnect.remove {id?, language?}` (no language: all). `codeConnect.export {path?}` → `{text, links}` (or writes the file): `{version: 1, links: [{component, language, - importPath, name, template}]}`, sorted, meant to live in the code repository as - `figcraft.connect.json`; `codeConnect.import {json | path}` → `{linked, warnings}` links each + importPath, name, template, source?, values?, notes?}]}`, sorted, meant to live in the code + repository as `figcraft.connect.json` (writing a file that's there keeps its other top-level keys + and the links of components this design doesn't have, so several designs can share one file); `codeConnect.import {json | path}` → `{linked, warnings}` links each entry to every component or set with that name (one undo step; at most 5,000 links and 4 MB; templates up to 4,000 characters, names 100, import paths 300; invalid entries are skipped with a - warning). `get_node` lists a component's links as `codeLinks`. In the app, the Inspect tab's + warning). A link can also say where the code component lives (`source`, a path inside the + repository), what a property's design values are called in code (`values`: + `{"Variant": {"Primary": "primary"}}`, so `{Variant}` renders `primary`; a value without an entry + keeps its design name) and `notes`, through `codeConnect.set`, the file and the Inspect tab. + `codeConnect.list {ids?, unmapped?, root?}` → `{components: [{id, name, type, mapped, properties, + links: [{language, importPath, import, name, template, snippet, source?, sourceFound?, values?, + notes?}]}], mapped, unmapped}` answers which code component each design component is; with `root` + it says whether each `source` exists there. `get_node` lists a component's links as `codeLinks`. In the app, the Inspect tab's Connect to code section edits them in the Code section's language, with a live preview and a Connected badge. An editor extension isn't needed: agents and editors read the same snippets through this server. Plugins whose manifest asks only for `read` also run from the Inspect tab, From dbccabce4170dfe1479a3f9469a93082b7719739 Mon Sep 17 00:00:00 2001 From: B-uchi Date: Thu, 19 Nov 2026 01:54:50 +0100 Subject: [PATCH 02/65] M8: design context gives Code Connect links as the connect file does `inspect.designContext` wrote a component's links through `camel`, which renames keys at every depth, so a mapped design value such as `extra_large` came out as `extraLarge`. Its components and the code it gives each connected layer now use Code Connect's own link JSON: value names stay as written, and the code carries the link's `source` and `notes`. Export also sorts its links once, after merging the file's other links. --- crates/engine/src/design_context_tests.rs | 26 +++++++++++++++++++++++ crates/engine/src/dev/code_connect.rs | 16 ++++++++------ crates/engine/src/dev/design_context.rs | 11 ++++++++-- docs/mcp.md | 2 +- 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/design_context_tests.rs b/crates/engine/src/design_context_tests.rs index 5d6f51304..722edc8b4 100644 --- a/crates/engine/src/design_context_tests.rs +++ b/crates/engine/src/design_context_tests.rs @@ -407,3 +407,29 @@ fn a_join_stopped_at_its_budget_says_so() { let (some, complete) = crate::design_context::referenced(s.doc().unwrap(), &start, &none, 3); assert_eq!((some.len(), complete), (3, false), "{some:?}"); } + +#[test] +fn code_links_keep_their_value_names_and_carry_source_and_notes() { + let mut s = session(); + let variants: Vec<_> = ["small", "extra_large"] + .iter() + .map(|v| { + let f = node(&mut s, json!({"type": "frame", "name": format!("Size={v}"), "width": 80, "height": 32})); + id(&s.execute("component.create", &json!({"ids": [f]})).unwrap()) + }) + .collect(); + let set = id(&s.execute("component.combineVariants", &json!({"ids": variants})).unwrap()); + s.execute( + "codeConnect.set", + &json!({"id": set, "name": "Button", "template": "