Approach A: moi scratch diagram β declarative diagrams with ELK auto-layout#27
Open
molefrog wants to merge 1 commit into
Conversation
The agent declares nodes/groups/edges/title in a JSON spec and never touches a coordinate: labels are measured with the real canvas font (scratchpad-metrics), nodes sized via fitRectToLabel, positions computed by elkjs's layered algorithm (real-worker API β Bun defines `self`, which breaks the bundled fake-worker entry). Groups become padded hierarchical ELK nodes, edges land as bound arrows that re-route in the browser, and everything goes in as one batch with deterministic prefixed ids (<name>-<nodeId>, <name>-title, <name>-edge-<i>) so the pieces stay addressable. - lib/types.ts: ScratchDiagramSpec (+ node/group/edge), `diagram` op, ScratchOpResult gains `created` - server/scratchpad-diagram.ts: spec validation (descriptive, actionable errors) + compiler; server/scratchpad-records.ts: record builders shared with the executor; server/scratchpad-style.ts: color/fill/size parsing shared with the CLI - control assigns diagram ops a d_<uuid8> default name; CLI reads the spec from --spec or stdin and prints the created ids - docs + workspace skill teach: diagram for any boxes-and-arrows structure, primitives for annotations/one-offs (skill version 0.5.0) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01A4d4g5CV89L1AanPrEYp7p
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bet
For "draw how X works" β the bulk of agent drawing β the agent should never touch a coordinate. It declares structure; geometry is non-overlapping, aligned, and evenly spaced by construction.
Stacked on #26 (text-metrics foundation). Sibling approaches: B (relative tools), C (see + lint).
What it does
One command produced the motivating screenshot's diagram with zero overlaps, fitted labels, top-aligned group label, and even layer gaps.
How
server/scratchpad-diagram.tsβ spec validation (unknown-key/reserved-id/cycle checks, errors name the exact entry) β node sizing viafitRectToLabelβ ELK layered layout (elkjs, the engine behind D2/Mermaid's ELK mode;INCLUDE_CHILDRENfor edges across group boundaries, model-order strategy for stable spec-order output, nested groups with label-aware padding and min-width) β tldraw records in one atomicstore.put. Arrows are bound to nodes so they follow later moves; edge labels ride the arrow's richText.self, so elkjs's bundled fake-worker path misdetects its environment. We useelkjs/lib/elk-api.jswith a realWorker(unref'd lazy singleton).server/scratchpad-records.ts/scratchpad-style.tsβ record builders and color/fill parsing extracted from the executor/CLI for reuse (no behavior change).--atis omitted;--idprefixes every created shape id (printed on success) so the agent can adjust pieces afterwards.SCRATCHPAD.md, bumped 0.4.0 β 0.5.0) teach: usediagramfor any boxes-and-arrows; primitives are for annotations.Tests
23 new tests: browser-loadable snapshots, no pairwise overlaps, rects β₯ measured labels, group containment, correct bindings, auto-place/
--at, deterministic ids, 10 validation-error cases. Full suite 342 green; lint/format clean.Trade-offs
Freeform annotation of the user's sketch isn't a graph; regenerating a diagram the user hand-tweaked can fight them. That's what approaches B/C cover β the long-term ship is likely A+B+C merged.
π€ Generated with Claude Code
https://claude.ai/code/session_01A4d4g5CV89L1AanPrEYp7p
Generated by Claude Code