SLOPSHOPPER

diagram-pane

A side pane that pins every diagram Claude draws, keeps a live map of the work, and lets you go into boxes and change them

paneguardcommandtoolmodel
★ 73v0.2.6MITupdated 2026-10-17ray-amjad/claude-code-mods/plugins/diagram-pane
A shopper browsing a rack in a slop shop
Preview · a replayed session in a sandbox
claude · ~/work/app · diagram-pane
│ ┃ Diagrams ✕ › fix the failing auth test and add an audit log call │ ┃ ◆ LIVE WORK MAP │ ┃ fork reply was not a graph: OK ⏺ Read(src/auth.ts) │ ┃ ───────────────────────────────────────────… ⎿ Read 6 lines │ ┃ ◆ PINNED DIAGRAMS ⏺ Update(src/auth.ts) │ ┃ Mermaid or box diagrams Claude writes appea… ⎿ Added 2 lines, removed 1 line │ ⏺ Bash(bun test) │ ⎿ 3 pass, 1 fail │ │ ● Done. refresh now rejects expired claims and logs an audit event. │ │ ✻ Worked for 42s · done 4:20 PM │ │ › /diagram │ │ ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── › ? for shortcuts

Draws

Pane · Diagrams
◆ LIVE WORK MAP fork reply was not a graph: OK ──────────────────────────────────────────────────────── ◆ PINNED DIAGRAMS Mermaid or box diagrams Claude writes appear here.
README

diagram-pane

A Claude Code mod that opens a side pane with a live picture of the work: every diagram Claude draws, pinned and readable, and a small map of what Claude is doing right now.

diagram-pane: Claude adds a discount module on the left; on the right the pane has gone into the checkout.js box, its numbered steps drawn as boxes, with a change being typed into the field below

Claude explains things with mermaid charts, but in a terminal you only see the raw code, and it scrolls away. Meanwhile the shape of the work (which files, which steps, what connects to what) lives only in Claude's context. This pane shows you both.

Does nothing in -p / SDK sessions, unless DIAGRAM_PANE_SYNC or DIAGRAM_PANE_DUMP is set (see below).

  • Pinned diagrams. Whenever a reply has a mermaid code block or an ASCII box diagram, it is pinned in the pane, newest first. Mermaid flowcharts are redrawn as boxes and arrows that fit a 60 column pane.
  • Live work map. Every few turns (and on /diagram) a cheap Haiku call asks whether the work changed shape. Only on yes does a fork of the session return a small graph of the files, components and steps it is working on, and the pane draws it. The node Claude is on right now is marked ▶.
  • Go into a box. Boxes are numbered. Press a number to open that box's detail: a mermaid subgraph opens at once, and any other box gets a detail drawn by a fork of the session. b goes back, t to the top. A box marked ▸ already has a detail.
  • Change a diagram. Press c (or Tab to the field) and type a change into the field under a diagram ("split auth into login and token refresh") and a fork redraws it. Or ask Claude: it reads and edits the pane with two tools, diagram_get and diagram_set.
  • Square boxes are files, round ones components, double ones steps or decisions.

Claude sees the pane only through those two tools.

A mermaid chart in Claude's reply on the left, the same chart drawn as boxes and arrows in the pane on the right

A plain question: the Haiku gate says the work kept its shape, so the map stays and no fork runs

Claude is asked to add a receipt step: it calls diagram_get and diagram_set, and the map in the pane gains the step and is marked edited

Install

claude plugin marketplace add ray-amjad/claude-code-mods
claude plugin install diagram-pane@claude-code-mods

Or inside Claude Code: /plugin marketplace add ray-amjad/claude-code-mods, then /plugin install diagram-pane@claude-code-mods.

The pane opens on its own in a terminal 144 columns or wider. Narrower than that, type /diagram once and it opens at any width. With fullscreen rendering it docks beside the transcript; in the classic renderer it sits above the prompt.

Usage

  • Ask for a diagram (explain the checkout flow with a mermaid flowchart) and it is pinned when the turn ends.
  • /diagram runs the gate now and redraws the map if the work changed. /diagram force skips the gate and draws the map from scratch, dropping edits.
  • /diagram open gives the pane the keyboard, so number keys work at once. Clicking the pane, or ctrl+x tab, does the same; Esc hands the keys back to the prompt.
  • Ask Claude to change what you are looking at: "add the NAT gateway to the network box", "move the cache under the API layer". It calls diagram_get, then diagram_set, and the pane shows the changed level.

Settings

| Option | Default | What it does | | :- | :- | :- | | everyTurns | 3 | Turns between work-map checks. Set it with /plugin configure diagram-pane@claude-code-mods. | | maxPins | 5 | How many pinned diagrams the pane keeps | | DIAGRAM_PANE_EVERY (env) | unset | Overrides everyTurns for one session, handy for a demo: DIAGRAM_PANE_EVERY=1 claude |

How it works

  • On turn.complete it reads the transcript ($.session.messages()) and pins any new mermaid or ASCII diagram it finds in Claude's replies.
  • Every N turns the gate runs: $.model.complete on Haiku with the current map, the files touched since that are not on it, and a digest of tool calls. It answers YES or NO.
  • On YES, $.model.fork asks the session itself for { title, nodes, edges } as JSON. A fork sees the whole conversation but cannot call tools, so it is cheap and served mostly from the prompt cache.
  • hooks/draw.js lays out both mermaid charts and the map the same way: layers top to bottom, boxes wrapped to the pane's width, connectors drawn with box characters.
  • A box's detail is stored inside it (sub), so the whole map is a tree. hooks/graph.js walks it, patches it, and writes it back out as mermaid with each detail as a subgraph, which is what diagram_get returns.
  • Opening a box with no detail forks the session with the level it sits on and asks for the inside as the same JSON. The detail's ids are renamed where they clash with the rest of the tree, since mermaid ids are global.
  • diagram_set takes a target (view, map or pin:N), a path of node ids to go down, and either a whole mermaid flowchart for that level or ops (add_node, remove_node, relabel, set_kind, add_edge, remove_edge). Boxes that keep their id keep their details.
  • An edited map is marked edited. The next refresh hands the fork the current map and asks it to keep those edits, and boxes that keep their id and label keep their details.
  • The map, the edited pins and the open view are saved per session in $.store, so they survive a hot reload or --resume. Pins are rebuilt from the transcript, with edits laid back over them.

Limits

  • Only mermaid graph and flowchart charts are redrawn. Sequence, class, state and other types are pinned as raw code. diagram_set with mermaid, or a typed change, can turn one into a flowchart.
  • Number keys reach the first nine boxes of a level; Tab or a click reaches the rest. Pinned diagrams on the home view are drawn without numbers: open a pin first to go into its boxes.
  • A fork cannot call tools, so a detail it draws holds only what the session has already seen.
  • Edges between boxes in different subgraphs are drawn to the subgraph's box on the level where both ends show; inside a subgraph, edges that leave it are not shown.
  • Every chart is drawn top to bottom, even graph LR, because a narrow pane has no room sideways.
  • Edges that skip a row or point back up are not routed. They are listed under the drawing with ⇢ and ↺. A row too wide for the pane wraps, and edges into the wrapped part are listed the same way.
  • ASCII diagrams are only picked up from plain code blocks (no language, or text), and shown as written.
  • The map check runs after a turn ends, not while Claude is working. Before the first reply there is nothing to fork, and /diagram says so.
  • The map, edits and view are saved per install: moving the plugin to another marketplace or reinstalling it starts with no saved maps. A session that already has everyTurns turns of work gets a fresh map at its next turn or diagram_get, without waiting.
  • /clear starts the pane fresh: the old session's map, pins and view are dropped (its saved map stays under its own id for --resume), and a map or detail fork still running from before /clear is discarded.
  • The map is the model's own summary of the work, so it can include a step such as "run tests" beside the files.

Develop

claude --plugin-dir ./diagram-pane
claude plugin validate .
claude plugin test .

For headless runs, DIAGRAM_PANE_SYNC=1 waits for the gate and fork before the turn ends, and DIAGRAM_PANE_DUMP=<file> writes the pane's text to a file.

Source 3 files
hooks/register.js 834 lines
1// diagram-pane: a side pane that keeps a live picture of the work.
2//
3//   1. Pinned diagrams: every mermaid block or ASCII box diagram Claude writes
4//      is pinned in the pane, newest first, drawn as boxes and arrows.
5//   2. Live work map: every few turns (and on /diagram) a cheap Haiku gate asks
6//      "did the work change shape?". Only on YES does a fork of the session
7//      draw a small JSON graph of what Claude is doing right now.
8//   3. Going into a box: a number key opens that box's detail, a mermaid
9//      subgraph or one a fork of the session draws. b goes back, t to the top.
10//   4. Changing a diagram: Claude reads and changes the pane with the
11//      diagram_get and diagram_set tools, and the person can type a change
12//      into the pane itself.
13//
14// Claude learns what the pane shows only through those two tools.
15
16import { findDiagrams, parseMermaid, drawGraph, short } from './draw.js'
17import { adoptIds, allIds, applyOps, boxOf, carrySubs, cleanId, levelAt, setLevel, toMermaid, uniqueIds } from './graph.js'
18
19const PANE = 'diagrams'
20const TOOL_GET = 'mcp__diagram-pane__diagram_get'
21const TOOL_SET = 'mcp__diagram-pane__diagram_set'
22const HOME = { root: 'map', path: [] }
23const NOTE = 'The map is drawn after a few turns, or now with /diagram'
24// Set at each session.start; true for -p / SDK sessions
25let headless = false
26
27// Everything the pane shows. Pins are rebuilt from the transcript; the map, the
28// edited pins and the view are also saved in $.store.
29const S = {
30  pins: [],          // [{ kind, code, title, graph, turn, edited }], newest first
31  seen: new Set(),   // the code of every diagram pinned so far, to skip repeats
32  scanned: 0,        // how many transcript messages we already looked at
33  turn: 0,           // prompts the user has sent in this session
34  sinceMap: 0,       // turns since the gate last ran
35  map: null,         // { title, graph, turn, edited } from the last fork or edit
36  mapAt: 0,          // transcript length when the map was drawn
37  edits: {},         // pin code -> { graph, title }: the pins someone changed
38  view: HOME,        // what the pane shows: { root: 'map' | 'pin', code?, path: [node ids gone into] }
39  loading: new Set(),// views whose detail a fork is drawing
40  loaded: false,     // the saved map has been read
41  caughtUp: false,   // the one early map draw for a session with work but no map was tried
42  maxPins: 5,
43  every: 3,          // turns between map checks (everyTurns, or DIAGRAM_PANE_EVERY)
44  note: NOTE,
45  busy: false,       // a gate or fork is running
46  sessionId: null,   // the session the state above belongs to
47  gen: 0,            // bumped when a new session starts, so late forks drop their reply
48  mapEdits: 0,       // bumped on every hand edit of the map, so a map fork that started before one drops its reply
49}
50
51// /clear keeps the process (session.start does not fire) but starts a new
52// session id: drop the old session's map, pins and view
53function startSession(id) {
54  if (S.sessionId !== null && S.sessionId !== id) {
55    Object.assign(S, {
56      pins: [], seen: new Set(), scanned: 0, sinceMap: 0, map: null, mapAt: 0,
57      edits: {}, view: HOME, loading: new Set(), loaded: false, caughtUp: false, note: NOTE, gen: S.gen + 1,
58    })
59  }
60  const changed = S.sessionId !== id
61  S.sessionId = id
62  return changed
63}
64
65export function register(on, options) {
66  const every = Number(options?.everyTurns) || 3
67  S.every = every
68  S.maxPins = Number(options?.maxPins) || 5
69
70  on('session.start', async ($, e, next) => {
71    // Set at each session.start. In -p / SDK sessions there is no pane to draw into
72    headless = e.isInteractive === false
73    if (headless) return next(e)
74    // Open the pane right away. Unasked, it is placed on a wide terminal (144+ columns);
75    // /diagram opens it at any width.
76    await $.ui.open({ id: PANE, title: 'Diagrams' })
77    try {
78      await $.command.register({ name: 'diagram', description: 'Redraw the live work map in the diagram pane', argumentHint: '[force|open]' })
79    } catch (err) {
80      $.ui.log('could not add /diagram: ' + err.message)
81    }
82    for (const tool of TOOLS) {
83      try {
84        await $.tool.register(tool)
85      } catch (err) {
86        $.ui.log(`could not add ${tool.name}: ` + err.message)
87      }
88    }
89    return next(e)
90  })
91
92  // After each main turn: pin new diagrams, and every few turns refresh the map
93  on('turn.complete', async ($, e, next) => {
94    const result = await next(e)
95    // Headless: no pane, so skip, unless DIAGRAM_PANE_SYNC or DIAGRAM_PANE_DUMP asks for the work
96    const asked = (await $.env.get('DIAGRAM_PANE_SYNC')) || (await $.env.get('DIAGRAM_PANE_DUMP'))
97    if ((headless && !asked) || e.agentId || e.isAborted) return result
98    const messages = await sync($)
99    S.sinceMap += 1
100    if (!headless && S.pins.length && !(await $.ui.panes()).some(p => p.id === PANE)) await $.ui.open({ id: PANE, title: 'Diagrams' })
101    S.every = Number(await $.env.get('DIAGRAM_PANE_EVERY')) || every
102    if (S.sinceMap >= S.every || overdue()) {
103      const work = overdue() ? catchUp($, messages) : refreshMap($, messages, false)
104      // Headless runs exit when the hook returns, so tests can ask us to wait
105      if (await $.env.get('DIAGRAM_PANE_SYNC')) await work
106    }
107    await redraw($)
108    return result
109  })
110
111  // /diagram runs the gate now; /diagram force skips the gate and draws the map
112  // from scratch; /diagram open gives the pane the keyboard, so number keys work
113  on('command.run', { command: 'diagram' }, async ($, e) => {
114    const arg = e.args.trim()
115    await $.ui.open({ id: PANE, title: 'Diagrams', ...(arg === 'open' ? { focus: true } : {}) })
116    const messages = await sync($)
117    if (arg === 'open') {
118      await redraw($)
119      return {}
120    }
121    const work = refreshMap($, messages, arg === 'force')
122    if (await $.env.get('DIAGRAM_PANE_SYNC')) await work
123    return {}
124  })
125
126  // Claude's way into the pane
127  on('tool.call', { tool: TOOL_GET }, async $ => {
128    const messages = await sync($)
129    // A session with work but no saved map (a reinstall, a new marketplace,
130    // --resume into a session never mapped): draw it now, so Claude gets a map
131    if (overdue()) await catchUp($, messages)
132    return { result: describe() }
133  }).catch(() => ({ deny: 'diagram_get failed; the pane is unchanged.' }))
134
135  on('tool.call', { tool: TOOL_SET }, async ($, e) => {
136    try {
137      await sync($)
138      const text = setDiagram(resolveTarget(String(e.target || 'view')), e)
139      await saveSaved($)
140      if (!(await $.ui.panes()).some(p => p.id === PANE)) await $.ui.open({ id: PANE, title: 'Diagrams' })
141      await redraw($)
142      return { result: text }
143    } catch (err) {
144      return { deny: 'diagram_set: ' + err.message }
145    }
146  }).catch(() => ({ deny: 'diagram_set failed; the pane is unchanged.' }))
147
148  // The pane itself: lines of Text, rows of Buttons, and one Input
149  on('ui.render', { component: 'Pane', requestId: PANE }, async ($, e) => {
150    const { Box, Text, Button, Input } = $.ui.resolve(e)
151    const items = paneItems(Math.max(30, e.props.bodyColumns || 60), actions($))
152    return h(Box, { flexDirection: 'column' }, ...items.map(it => {
153      if (it.input) return h(Input, it.input)
154      if (it.buttons) {
155        return h(Box, { flexDirection: 'row', flexWrap: 'wrap', columnGap: 2 },
156          ...(it.lead ? [h(Text, { bold: true, wrap: 'truncate-end' }, it.lead)] : []),
157          ...it.buttons.map(b => h(Button, {
158            key: b.key, label: b.label, onPress: b.onPress,
159            ...(b.hotkey ? { hotkey: b.hotkey, plain: true } : {}),
160          })))
161      }
162      return h(Text, { color: it.color, bold: it.bold, dimColor: it.dim, wrap: 'truncate-end' }, it.text || ' ')
163    }))
164  })
165}
166
167// No map, yet the transcript already holds a map's worth of turns. The map is
168// saved per install, so this is a session mapped by another install (or never):
169// waiting `every` more turns would leave the pane empty for no reason.
170// One catch-up try per load; after that the usual cadence takes over.
171const overdue = () => !S.map && !S.busy && !S.caughtUp && S.turn >= S.every
172
173function catchUp($, messages) {
174  S.caughtUp = true
175  return refreshMap($, messages, false)
176}
177
178// Read the saved map once, then pin what the transcript has that is new
179async function sync($) {
180  const fresh = startSession(await $.session.id())
181  const messages = await $.session.messages()
182  S.turn = turnsIn(messages)
183  await loadSaved($)
184  pinNewDiagrams(messages, S.maxPins)
185  if (fresh) await redraw($)
186  return messages
187}
188
189// ---------------------------------------------------------------------------
190// Pinned diagrams
191// ---------------------------------------------------------------------------
192
193// A turn is a prompt the user typed (tool results also arrive as user messages)
194function turnsIn(messages) {
195  return messages.filter(m => m.role === 'user' && m.text && !m.toolResults?.length).length
196}
197
198function pinNewDiagrams(messages, maxPins) {
199  if (messages.length < S.scanned) S.scanned = 0 // compacted: rescan, `seen` stops repeats
200  messages.forEach((m, i) => {
201    if (i < S.scanned || m.role !== 'assistant') return
202    for (const d of findDiagrams(m.text)) {
203      if (S.seen.has(d.code)) continue
204      S.seen.add(d.code)
205      // An edited pin comes back as edited; else parse mermaid flowcharts into a
206      // graph, and show anything else as written
207      const edit = S.edits[d.code]
208      const graph = edit?.graph || (d.kind === 'mermaid' ? parseMermaid(d.code) : null)
209      S.pins.unshift({ ...d, title: edit?.title || d.title, graph, edited: Boolean(edit), turn: turnsIn(messages.slice(0, i)) })
210    }
211  })
212  S.scanned = messages.length
213  S.pins.length = Math.min(S.pins.length, maxPins)
214}
215
216const pinOf = code => S.pins.find(p => p.code === code)
217
218// ---------------------------------------------------------------------------
219// Live work map: the gate, then the fork
220// ---------------------------------------------------------------------------
221
222const GATE_SYSTEM = 'You watch a coding agent so a small diagram of its work stays current. Answer with exactly one word: YES or NO.'
223
224const SIDE = '[diagram-pane side request: not from the user. Do not continue the task and do not call tools.]'
225
226const JSON_SHAPE = `{"title":"what it is, max 40 chars","nodes":[{"id":"a","label":"max 20 chars","kind":"file|component|step","now":false}],"edges":[{"from":"a","to":"b","label":"optional, max 10 chars"}]}`
227
228const FORK_PROMPT = `${SIDE}
229Draw a compact map of the work in this session right now: the files, components and steps involved, and how they connect.
230Reply with ONLY one JSON object, no prose and no code fence:
231${JSON_SHAPE}
232Map the task, not the chat: steps are steps of the work ("add discount", "run tests"), never "read files" or "explain".
233Rules: 3 to 8 nodes. Edges point from earlier to later, or from caller to callee, so the map reads top to bottom. At most 10 edges. Use file basenames as file labels. Set "now": true on the one node being worked on at this moment.`
234
235// The map the person sees goes to the fork, so a redraw builds on it, and on
236// their edits, instead of starting over
237function basePrompt(map) {
238  return `\n\nThe map the user sees now${map.edited ? ' (the user or Claude edited it by hand: keep those edits)' : ''}:\n` +
239    `${toMermaid(shallow(map.graph))}\n` +
240    'Keep the ids and labels of nodes that still fit. Add, drop or reconnect only what the work changed. Reply with the JSON as above.'
241}
242
243async function refreshMap($, messages, force) {
244  if (S.busy) return
245  S.busy = true
246  S.sinceMap = 0
247  const gen = S.gen
248  try {
249    // 1. The cheap gate: skipped only when there is no map yet, or on /diagram force
250    if (S.map && !force) {
251      S.note = 'gate: checking if the work changed shape...'
252      await redraw($)
253      const activity = messages.slice(S.mapAt)
254      const gate = await $.model.complete({
255        model: 'haiku',
256        system: GATE_SYSTEM,
257        prompt: `CURRENT MAP: ${mapSummary()}\n\nFILES TOUCHED SINCE, NOT ON THE MAP: ${newFiles(activity).join(', ') || 'none'}\n\n` +
258          `ACTIVITY SINCE THE MAP WAS DRAWN:\n${digest(activity)}\n\n` +
259          'Answer YES if the map is now out of date: the work touched files or components the map lacks, added a step ' +
260          '(such as tests, a new feature, a fix), or moved to a different task. Answer NO only if the map still shows the work. YES or NO?',
261        maxTokens: 5,
262        timeoutMs: 20000,
263      })
264      if (gen !== S.gen) return
265      const yes = gate.isAnswered && /^\s*yes/i.test(gate.text)
266      if (!yes) {
267        S.note = `gate said same shape at turn ${S.turn}, map kept` + (gate.isAnswered ? '' : ` (${gate.reason})`)
268        return
269      }
270    }
271    // 2. The fork: the session's own context, asked for JSON. It cannot call tools.
272    // /diagram force draws from scratch; otherwise the fork builds on the current map.
273    const base = force ? null : S.map
274    const edits = S.mapEdits
275    S.note = 'fork: drawing the work map...'
276    await redraw($)
277    const r = await $.model.fork({ prompt: FORK_PROMPT + (base ? basePrompt(base) : '') })
278    if (gen !== S.gen) return // /clear ran meanwhile: this map is the old session's
279    if (!r.isAnswered) {
280      S.note = r.reason === 'nothing-to-fork' ? 'nothing to map yet: send a prompt first' : 'fork failed: ' + r.reason
281      return
282    }
283    const map = parseMap(r.text)
284    if (!map) {
285      S.note = 'fork reply was not a graph: ' + short(r.text.replace(/\s+/g, ' '), 60)
286      return
287    }
288    // Someone changed the map while the fork ran: their change wins over a map
289    // drawn from what was there before it
290    if (S.mapEdits !== edits) {
291      S.note = `kept the map as edited at turn ${S.turn}; it refreshes at the next check`
292      return
293    }
294    // Boxes that kept their id and label keep the detail drawn inside them, read
295    // from the map as it is now, so a box opened while the fork ran keeps its detail
296    const now = force ? null : S.map
297    const graph = uniqueIds(now ? carrySubs(now.graph, map.graph, { sameLabel: true }) : map.graph)
298    S.map = { title: map.title, graph, turn: S.turn, edited: Boolean(now?.edited) }
299    S.mapAt = messages.length
300    S.note = `map drawn at turn ${S.turn}`
301    await saveSaved($)
302  } catch (err) {
303    if (gen === S.gen) S.note = 'map error: ' + err.message
304  } finally {
305    S.busy = false
306    await redraw($)
307  }
308}
309
310// A fork's JSON becomes the same { nodes, edges } shape a mermaid chart parses to
311export function parseMap(text) {
312  try {
313    const json = JSON.parse(text.slice(text.indexOf('{'), text.lastIndexOf('}') + 1))
314    const nodes = (json.nodes || []).slice(0, 10).map(n => ({
315      id: cleanId(n.id),
316      label: short(n.label || n.id, 22),
317      box: boxOf(n.kind),
318      ...(n.now ? { now: true } : {}),
319    }))
320    const edges = (json.edges || []).slice(0, 14).map(e => ({ from: cleanId(e.from), to: cleanId(e.to), label: short(e.label || '', 10) }))
321    return nodes.length ? { title: short(json.title || 'the work', 50), graph: { nodes, edges } } : null
322  } catch {
323    return null
324  }
325}
326
327// The map, the edited pins and the view are kept per session in the plugin's
328// store, so they survive a reload or --resume
329async function loadSaved($) {
330  if (S.loaded) return
331  S.loaded = true
332  const saved = ((await $.store.get('maps')) || {})[S.sessionId]
333  if (!saved) return
334  if (saved.map) Object.assign(S, { map: saved.map, mapAt: saved.mapAt || 0, note: `map from turn ${saved.map.turn}` })
335  if (saved.edits) S.edits = saved.edits
336  if (saved.view) S.view = saved.view
337}
338
339async function saveSaved($) {
340  if (S.sessionId === null) return
341  const maps = (await $.store.get('maps')) || {}
342  // Edits of pins that fell off the pane are no longer needed
343  const edits = Object.fromEntries(Object.entries(S.edits).filter(([code]) => pinOf(code)))
344  maps[S.sessionId] = { map: S.map, mapAt: S.mapAt, edits, view: S.view, at: await $.clock.now() }
345  // Keep the 20 newest sessions only
346  const keep = Object.entries(maps).sort((a, b) => b[1].at - a[1].at).slice(0, 20)
347  await $.store.set('maps', Object.fromEntries(keep))
348}
349
350function mapSummary() {
351  const { nodes, edges } = S.map.graph
352  const name = id => nodes.find(n => n.id === id)?.label
353  return `"${S.map.title}": ` + nodes.map(n => n.label).join(', ') + '; ' + edges.map(e => `${name(e.from)} -> ${name(e.to)}`).join(', ')
354}
355
356// Files Claude read, wrote or edited whose names the map does not mention
357function newFiles(messages) {
358  const onMap = S.map.graph.nodes.map(n => n.label).join(' ')
359  const names = messages.flatMap(m => (m.toolUses || []).map(t => fileName(t.input?.file_path)))
360  return [...new Set(names)].filter(n => n && !onMap.includes(n))
361}
362
363// The last part of a path, after a / or (on Windows) a \
364export function fileName(path) {
365  return String(path || '').split(/[\\/]/).pop()
366}
367
368// What happened since the map was drawn, as short lines: prompts and tool calls
369function digest(messages) {
370  const lines = []
371  for (const m of messages) {
372    if (m.role === 'user' && m.text && !m.toolResults?.length) lines.push('user: ' + short(m.text.replace(/\s+/g, ' '), 160))
373    for (const t of m.toolUses || []) {
374      const i = t.input || {}
375      lines.push(`${t.tool}: ` + short(String(i.file_path || i.path || i.command || i.pattern || i.description || ''), 100))
376    }
377  }
378  return lines.slice(-40).join('\n') || '(nothing)'
379}
380
381// One level, without the details inside its boxes: what a prompt needs
382const shallow = g => ({ nodes: g.nodes.map(({ sub, ...n }) => n), edges: g.edges })
383
384// ---------------------------------------------------------------------------
385// Going into a box
386// ---------------------------------------------------------------------------
387
388const rootGraph = (view = S.view) => (view.root === 'pin' ? pinOf(view.code)?.graph : S.map?.graph) || null
389
390const isHome = (view = S.view) => view.root === 'map' && !view.path.length
391
392const viewKey = view => `${view.root}\u0000${view.code || ''}\u0000${view.path.join('/')}`
393
394// The names of the levels down to the view, for the pane's header
395function crumbs(view = S.view) {
396  const pin = view.root === 'pin' ? pinOf(view.code) : null
397  const names = [pin ? short(pin.title || `pin ${S.pins.indexOf(pin) + 1}`, 24) : 'Work map']
398  let g = rootGraph(view)
399  for (const id of view.path) {
400    const n = g?.nodes.find(n => n.id === id)
401    names.push(n ? short(n.label, 20) : id)
402    g = n?.sub
403  }
404  return names
405}
406
407// A redrawn map or a dropped pin can take the view's boxes away: keep the part
408// of the path that is still there
409function normalizeView() {
410  const v = S.view
411  if (v.root === 'pin' && !pinOf(v.code)) return (S.view = HOME)
412  let g = rootGraph(v)
413  const keep = []
414  for (const id of v.path) {
415    const n = g?.nodes.find(n => n.id === id)
416    if (!n) break
417    keep.push(id)
418    g = n.sub
419  }
420  if (keep.length !== v.path.length) S.view = { ...v, path: keep }
421}
422
423function actions($) {
424  const go = async view => {
425    S.view = view
426    await redraw($)
427    await saveSaved($)
428  }
429  return {
430    drill: id => drill($, id),
431    back: () => go(S.view.path.length ? { ...S.view, path: S.view.path.slice(0, -1) } : HOME),
432    top: () => go(HOME),
433    openPin: code => go({ root: 'pin', code, path: [] }),
434    edit: text => editByFork($, text),
435    // A surface that cannot move the focus leaves the field to Tab or a click
436    typeChange: () => $.ui.focus({ requestId: PANE, key: inputKey() }).catch(() => {}),
437  }
438}
439
440async function drill($, id) {
441  normalizeView()
442  const { graph } = levelAt(rootGraph(), S.view.path)
443  const node = graph?.nodes.find(n => n.id === id)
444  if (!node) return
445  S.view = { ...S.view, path: [...S.view.path, id] }
446  await redraw($)
447  await saveSaved($)
448  if (!node.sub) await expand($, S.view)
449}
450
451const DETAIL_PROMPT = (where, parent, node) => `${SIDE}
452The user is looking at this diagram in a side pane (${where}):
453${toMermaid(shallow(parent))}
454They opened the box "${node.label}" (id ${node.id}). Draw what is inside it, one level more detailed: its parts (functions, sub-steps, components, resources) and how they connect.
455Use what this session has seen. Where you have not seen the inside, draw only what you can infer, and do not invent file names.
456Reply with ONLY one JSON object, no prose and no code fence:
457${JSON_SHAPE}
458Rules: 3 to 8 nodes, at most 10 edges, edges read top to bottom. Leave "now" false.`
459
460// A box with no detail yet: ask a fork of the session to draw one
461async function expand($, view) {
462  const key = viewKey(view)
463  if (S.loading.has(key)) return
464  const id = view.path.at(-1)
465  const parentView = { ...view, path: view.path.slice(0, -1) }
466  const parent = levelAt(rootGraph(view), parentView.path).graph
467  const node = parent?.nodes.find(n => n.id === id)
468  if (!node) return
469  S.loading.add(key)
470  S.note = `fork: drawing the inside of ${short(node.label, 24)}...`
471  await redraw($)
472  const gen = S.gen
473  try {
474    const r = await $.model.fork({ prompt: DETAIL_PROMPT(crumbs(parentView).join(' › '), parent, node) })
475    if (gen !== S.gen) return
476    if (!r.isAnswered) {
477      S.note = r.reason === 'nothing-to-fork' ? 'nothing to fork yet: send a prompt first' : 'fork failed: ' + r.reason
478      return
479    }
480    const detail = parseMap(r.text)
481    if (!detail) {
482      S.note = 'fork reply was not a graph: ' + short(r.text.replace(/\s+/g, ' '), 60)
483      return
484    }
485    // The diagram may have changed while the fork ran: attach only to a box that
486    // is still there and still has no detail
487    const root = rootGraph(view)
488    const at = levelAt(root, view.path)
489    if (at.graph || !at.node) return
490    commitRoot(view, setLevel(root, view.path, adoptIds(detail.graph, allIds(root), id)), {})
491    S.note = `drew the inside of ${short(node.label, 24)}`
492    await saveSaved($)
493  } catch (err) {
494    if (gen === S.gen) S.note = 'detail error: ' + err.message
495  } finally {
496    S.loading.delete(key)
497    await redraw($)
498  }
499}
500
501// ---------------------------------------------------------------------------
502// Changing a diagram: Claude's tools and the pane's own input
503// ---------------------------------------------------------------------------
504
505const TOOLS = [
506  {
507    name: 'diagram_get',
508    description:
509      "Read what the user's diagram side pane shows: the live work map, the diagrams pinned from your replies, and which box " +
510      'the user has opened. Each comes back as mermaid with stable node ids; a subgraph is a box with a detail diagram inside ' +
511      'that the user can open. Call this when the user talks about "the diagram", "the map", a box or a pin, and before ' +
512      'diagram_set. The user can change diagrams from the pane too, so read again instead of trusting an earlier copy.',
513    inputSchema: { type: 'object', properties: {} },
514    isDeferred: false,
515  },
516  {
517    name: 'diagram_set',
518    description:
519      "Change a diagram in the user's side pane. target: \"view\" (what the user is looking at; the default), \"map\" " +
520      '(the live work map) or "pin:N" (a pinned diagram, N as diagram_get lists it). path: node ids to go down through ' +
521      "from that diagram's top level (from the open box for \"view\"); the change applies to the last node's detail, " +
522      'created if it has none. Give mermaid (a whole flowchart that replaces that level: ids you keep keep their details, ' +
523      'subgraph blocks become details the user can open) and/or ops (small patches). The pane then shows the changed ' +
524      'level. Edits stay: the automatic map refresh builds on them instead of redrawing over them.',
525    inputSchema: {
526      type: 'object',
527      properties: {
528        target: { type: 'string', description: '"view", "map" or "pin:N"' },
529        path: { type: 'array', items: { type: 'string' }, description: 'Node ids to go into, top level first' },
530        mermaid: { type: 'string', description: 'A flowchart (flowchart TD) that replaces the level' },
531        ops: {
532          type: 'array',
533          description: 'Patches, applied in order after mermaid',
534          items: {
535            type: 'object',
536            properties: {
537              op: { type: 'string', enum: ['add_node', 'remove_node', 'relabel', 'set_kind', 'add_edge', 'remove_edge'] },
538              id: { type: 'string' },
539              label: { type: 'string' },
540              kind: { type: 'string', enum: ['file', 'component', 'step'] },
541              from: { type: 'string' },
542              to: { type: 'string' },
543            },
544            required: ['op'],
545          },
546        },
547        title: { type: 'string', description: 'A new title for the diagram' },
548      },
549    },
550    isDeferred: false,
551  },
552]
553
554// What diagram_get returns: every diagram as mermaid, and where the user is
555export function describe() {
556  normalizeView()
557  const out = [`The pane shows: ${crumbs().join(' › ')}` +
558    (S.view.path.length ? `  (path ${JSON.stringify(S.view.path)} in ${S.view.root === 'map' ? '"map"' : `"pin:${S.pins.indexOf(pinOf(S.view.code)) + 1}"`})` : '')]
559  if (S.map) out.push(`\n## map: "${S.map.title}" (turn ${S.map.turn}${S.map.edited ? ', edited' : ''})\n\`\`\`mermaid\n${toMermaid(S.map.graph)}\n\`\`\``)
560  else {
561    const left = Math.max(1, S.every - S.sinceMap)
562    out.push(`\n## map: not drawn yet. Last status: ${S.note}.\n` +
563      'This install has no saved map for this session (maps are kept per install, so a reinstall or a new marketplace starts empty). ' +
564      `The next check runs after ${left} more turn${left > 1 ? 's' : ''}. The user can run /diagram to draw it now, ` +
565      'or diagram_set with target "map" and mermaid draws one by hand.')
566  }
567  S.pins.forEach((p, i) => {
568    out.push(`\n## pin:${i + 1}: "${p.title || 'untitled'}" (turn ${p.turn}${p.edited ? ', edited' : ''})`)
569    if (p.graph) out.push(`\`\`\`mermaid\n${toMermaid(p.graph)}\n\`\`\``)
570    else out.push(`Not a flowchart, shown as written (mermaid in diagram_set replaces it with one):\n\`\`\`${p.kind === 'mermaid' ? 'mermaid' : 'text'}\n${p.code}\n\`\`\``)
571  })
572  if (!S.pins.length) out.push('\nNo pinned diagrams.')
573  return out.join('\n')
574}
575
576function resolveTarget(target) {
577  if (target === 'view') {
578    normalizeView()
579    return { ...S.view }
580  }
581  if (target === 'map') return { ...HOME }
582  const m = /^pin:?\s*(\d+)$/i.exec(target)
583  if (m) {
584    const pin = S.pins[Number(m[1]) - 1]
585    if (!pin) throw new Error(`no ${target}: there ${S.pins.length === 1 ? 'is 1 pin' : `are ${S.pins.length} pins`}`)
586    return { root: 'pin', code: pin.code, path: [] }
587  }
588  throw new Error(`unknown target "${target}": use "view", "map" or "pin:N"`)
589}
590
591// Apply { path, mermaid, ops, title } at `view`, show the changed level, and say what changed
592export function setDiagram(view, input) {
593  const { mermaid, ops, title } = input
594  const path = [...view.path, ...(Array.isArray(input.path) ? input.path.map(String) : [])]
595  if (!mermaid && !ops?.length && !title) throw new Error('give mermaid, ops or title')
596  const root = rootGraph(view)
597  let level = { nodes: [], edges: [] }
598  if (root) {
599    const at = levelAt(root, path)
600    if (!at.graph && !at.node) throw new Error(`path ${JSON.stringify(path)} does not lead to a box`)
601    level = at.graph || level
602  } else if (path.length) throw new Error('that diagram has no boxes to go into yet')
603  else if (!mermaid && !ops?.length) throw new Error('that diagram has no boxes yet: give mermaid')
604
605  // Only a new title: the diagram's boxes stay as they are
606  if (!mermaid && !ops?.length) {
607    commitRoot(view, root, { edited: true, title })
608    S.view = { ...view, path }
609    return `Retitled ${crumbs().join(' › ')}.`
610  }
611
612  let next = level
613  const changes = []
614  if (mermaid) {
615    const parsed = parseMermaid(String(mermaid).replace(/^\s*```(?:mermaid)?\s*\n?|```\s*$/g, ''))
616    if (!parsed) throw new Error('mermaid must be a flowchart ("flowchart TD" or "graph TD") with at least one node')
617    next = carrySubs(level, parsed)
618    changes.push(`redrew it with ${parsed.nodes.length} boxes`)
619  }
620  if (ops?.length) {
621    const r = applyOps(next, ops)
622    next = r.graph
623    changes.push(...r.changes)
624  }
625  if (title) changes.push('retitled it')
626  // A detail's ids must not clash with the rest of the tree
627  if (path.length) next = adoptIds(next, allIds(setLevel(root, path, { nodes: [], edges: [] })), path.at(-1))
628  // A detail with no boxes left is no detail: the box goes back to having none
629  const tree = path.length ? setLevel(root, path, next.nodes.length ? next : null) : next
630  // A box added on top may take an id already used inside a detail: the deeper one is renamed
631  commitRoot(view, uniqueIds(tree), { edited: true, title })
632  S.view = { ...view, path }
633  normalizeView()
634  next = levelAt(rootGraph(), S.view.path).graph || { nodes: [], edges: [] }
635  return `Changed ${crumbs().join(' › ')}: ${changes.join(', ')}. The pane shows it now:\n` +
636    `\`\`\`mermaid\n${toMermaid(next)}\n\`\`\``
637}
638
639// Store a changed tree as the map or as the pin it came from
640function commitRoot(view, graph, { edited = false, title }) {
641  if (view.root === 'map') {
642    S.map = {
643      title: title || S.map?.title || 'diagram',
644      graph,
645      turn: S.map?.turn ?? S.turn,
646      edited: Boolean(S.map?.edited || edited),
647    }
648    if (edited) S.mapEdits += 1
649    return
650  }
651  const pin = pinOf(view.code)
652  if (!pin) throw new Error('that pin is no longer in the pane')
653  pin.graph = graph
654  if (title) pin.title = title
655  if (edited || pin.edited) {
656    pin.edited = true
657    S.edits[pin.code] = { graph, title: pin.title }
658  }
659}
660
661const EDIT_PROMPT = (where, source, text) => `${SIDE}
662The user is looking at this diagram in a side pane (${where}):
663${source}
664They typed this change: "${text}"
665Reply with ONLY the changed diagram as a mermaid flowchart ("flowchart TD"), no prose. Keep the ids of boxes that stay.`
666
667// The pane's input: a fork of the session redraws the level the person is on
668async function editByFork($, text) {
669  text = String(text).trim()
670  if (!text) return
671  normalizeView()
672  const view = { ...S.view }
673  const root = rootGraph(view)
674  const at = root ? levelAt(root, view.path) : { graph: null }
675  const pin = view.root === 'pin' ? pinOf(view.code) : null
676  const source = at.graph ? toMermaid(shallow(at.graph)) : !view.path.length && pin ? pin.code : null
677  if (!source) {
678    S.note = 'nothing to change here yet'
679    return redraw($)
680  }
681  S.note = `fork: changing it: "${short(text, 30)}"...`
682  await redraw($)
683  const gen = S.gen
684  try {
685    const r = await $.model.fork({ prompt: EDIT_PROMPT(crumbs(view).join(' › '), source, text) })
686    if (gen !== S.gen) return
687    if (!r.isAnswered) {
688      S.note = r.reason === 'nothing-to-fork' ? 'nothing to fork yet: send a prompt first' : 'fork failed: ' + r.reason
689      return
690    }
691    // The chart is in a code fence, or starts at a line of its own that opens with
692    // flowchart or graph (never at those words inside the prose before it)
693    const fenced = r.text.match(/```(?:mermaid)?\s*\n([\s\S]*?)```/)
694    const start = r.text.search(/^[ \t]*(flowchart|graph)\b/m)
695    if (!fenced && start < 0) throw new Error('the reply had no flowchart')
696    const code = fenced ? fenced[1] : r.text.slice(start)
697    setDiagram({ ...view, path: [] }, { path: view.path, mermaid: code })
698    S.note = `changed: ${short(text, 40)}`
699    await saveSaved($)
700  } catch (err) {
701    if (gen === S.gen) S.note = 'change failed: ' + err.message
702  } finally {
703    await redraw($)
704  }
705}
706
707// ---------------------------------------------------------------------------
708// What the pane draws
709// ---------------------------------------------------------------------------
710
711// Mark the box Claude is on and the boxes that already have a detail inside.
712// With `keys`, also number the first nine boxes: the keys that open them. Pins on
713// the home view have no keys of their own, so they are drawn without numbers.
714function marked(graph, keys = true) {
715  return {
716    nodes: graph.nodes.map((n, i) => ({
717      ...n,
718      label: (keys && i < 9 ? `${i + 1}·` : '') + (n.now ? '▶ ' : '') + n.label + (n.sub ? ' ▸' : ''),
719    })),
720    edges: graph.edges,
721  }
722}
723
724// A Button per box: the first nine also have their number as a hotkey, the rest
725// are reached with Tab or a click
726function nodeButtons(graph, act) {
727  return graph.nodes.map((n, i) => ({
728    key: 'node:' + n.id,
729    ...(i < 9 ? { hotkey: String(i + 1) } : {}),
730    label: short(n.label, 16) + (n.sub ? ' ▸' : ''),
731    onPress: () => act?.drill(n.id),
732  }))
733}
734
735// The change field's key: one per view, so text typed for one diagram does not
736// follow you into another. A pin is named by its code, not its place in the
737// list, which shifts when a new diagram is pinned.
738function inputKey(view = S.view) {
739  return ['edit', view.root === 'pin' ? 'pin' + hash(view.code || '') : 'map', ...view.path].join('-')
740}
741
742function hash(text) {
743  let h = 5381
744  for (let i = 0; i < text.length; i++) h = ((h * 33) ^ text.charCodeAt(i)) >>> 0
745  return h.toString(36)
746}
747
748// Items of { text, color, bold, dim }, { buttons, lead? } or { input }, cut to `w` columns
749function paneItems(w, act) {
750  normalizeView()
751  const out = []
752  const add = (text, style = {}) => out.push({ text, ...style })
753  const rule = () => add('─'.repeat(w), { dim: true })
754  // The change field, and a c key that moves the focus into it: number keys and
755  // b and t stay hotkeys, and the person need not Tab past every box to type
756  const input = placeholder => {
757    out.push({ buttons: [{ key: 'change', hotkey: 'c', label: 'type a change', onPress: () => act?.typeChange() }] })
758    out.push({ input: { key: inputKey(), placeholder, submitLabel: 'change', onSubmit: value => act?.edit(value) } })
759    add('keys: click the pane, ctrl+x tab or /diagram open', { dim: true })
760  }
761
762  // Inside a box, or a pin opened on its own: that one diagram
763  if (!isHome()) {
764    const v = S.view
765    add(short('◆ ' + crumbs().join(' › '), w), { bold: true, color: v.root === 'map' ? 'cyan' : 'magenta' })
766    out.push({ buttons: [
767      { key: 'back', hotkey: 'b', label: 'back', onPress: () => act?.back() },
768      { key: 'top', hotkey: 't', label: 'top', onPress: () => act?.top() },
769    ] })
770    const { graph, node } = levelAt(rootGraph(), v.path)
771    const pin = v.root === 'pin' ? pinOf(v.code) : null
772    if (graph) {
773      for (const l of drawGraph(marked(graph), w)) add(l, { color: v.root === 'map' ? 'cyan' : undefined })
774      out.push({ buttons: nodeButtons(graph, act) })
775    } else if (node) {
776      add(S.loading.has(viewKey(v)) ? `… drawing the inside of ${node.label}` : `${node.label} has no detail yet.`, { dim: true })
777    } else if (pin) {
778      for (const l of pin.code.split('\n')) add(l, { dim: pin.kind === 'mermaid' })
779    }
780    add((S.busy ? '… ' : '') + S.note, { dim: true })
781    if (graph || pin) input('describe a change to this diagram')
782    return out
783  }
784
785  add(spread('◆ LIVE WORK MAP', S.map ? `turn ${S.map.turn}${S.map.edited ? ', edited' : ''}` : '', w), { bold: true, color: 'cyan' })
786  if (S.map) {
787    add(S.map.title, { bold: true })
788    for (const l of drawGraph(marked(S.map.graph), w)) add(l, { color: 'cyan' })
789    add('┌ file  ╭ component  ╔ step  ▶ now  ▸ has detail', { dim: true })
790  }
791  add((S.busy ? '… ' : '') + S.note, { dim: true })
792  if (S.map) {
793    out.push({ buttons: nodeButtons(S.map.graph, act) })
794    input('describe a change to the map')
795  }
796  rule()
797
798  add(spread('◆ PINNED DIAGRAMS', S.pins.length ? `${S.pins.length} pinned` : '', w), { bold: true, color: 'magenta' })
799  if (!S.pins.length) add('Mermaid or box diagrams Claude writes appear here.', { dim: true })
800  S.pins.forEach((p, i) => {
801    if (i) add('')
802    const what = p.graph ? `flowchart, ${p.graph.nodes.length} boxes` : p.kind === 'mermaid' ? 'mermaid' : 'box diagram'
803    const head = short(`${i + 1}. ${p.title || what} · turn ${p.turn}${p.edited ? ' · edited' : ''}`, w - 12)
804    out.push({ lead: head, buttons: [{ key: 'pin:' + i, label: 'open ▸', onPress: () => act?.openPin(p.code) }] })
805    if (p.graph) for (const l of drawGraph(marked(p.graph, false), w)) add(l)
806    else {
807      // Not a flowchart we can draw: show the code as written, dimmed for mermaid
808      if (p.kind === 'mermaid') add('(raw mermaid: only flowcharts are drawn)', { dim: true })
809      for (const l of p.code.split('\n')) add(l, { dim: p.kind === 'mermaid' })
810    }
811  })
812  return out
813}
814
815// The pane as plain text: what tests and DIAGRAM_PANE_DUMP read
816export function paneText(w = 60) {
817  return paneItems(w, null).map(it => {
818    if (it.input) return `> ${it.input.placeholder}`
819    if (it.buttons) return [it.lead, ...it.buttons.map(b => (b.hotkey ? `${b.hotkey}: ${b.label}` : `[ ${b.label} ]`))].filter(Boolean).join('  ')
820    return it.text
821  }).join('\n')
822}
823
824function spread(left, right, w) {
825  return right ? left + ' '.repeat(Math.max(1, w - left.length - right.length)) + right : left
826}
827
828// Ask for a redraw, and in tests also write the pane's text to a file
829async function redraw($) {
830  $.ui.invalidate('ui.render')
831  const dump = await $.env.get('DIAGRAM_PANE_DUMP')
832  if (dump) await $.fs.write(dump, paneText(60) + '\n')
833}
834
hooks/draw.js 357 lines
1// draw.js: turns diagrams into lines of terminal text. No $ here, just strings.
2//
3//   findDiagrams(markdown)   -> the mermaid and ASCII diagrams in a reply
4//   parseMermaid(code)       -> { nodes, edges } for a flowchart, or null
5//   drawGraph(graph, width)  -> lines of box-and-arrow text, top to bottom
6
7// ---------------------------------------------------------------------------
8// 1. Find diagrams in a reply
9// ---------------------------------------------------------------------------
10
11const CORNERS = /[┌┐└┘╭╮╰╯╔╗╚╝]|\+[-=]{3,}/
12const ARROWS = /(--+>|──+[>▶]|→|▶|▼|↓|\^)/
13const PLAIN = ['', 'text', 'txt', 'ascii', 'plain', 'diagram', 'art']
14
15// Returns [{ kind: 'mermaid' | 'ascii', code, title }] in reply order
16export function findDiagrams(markdown) {
17  const found = []
18  const fence = /```([\w-]*)[^\n]*\n([\s\S]*?)```/g
19  let m
20  while ((m = fence.exec(markdown))) {
21    const lang = m[1].toLowerCase()
22    const code = m[2].replace(/\s+$/, '')
23    const title = titleBefore(markdown.slice(0, m.index))
24    if (lang === 'mermaid') found.push({ kind: 'mermaid', code, title })
25    else if (PLAIN.includes(lang) && looksLikeBoxes(code)) found.push({ kind: 'ascii', code, title })
26  }
27  return found
28}
29
30// An ASCII diagram has box corners, or several lines of arrows
31function looksLikeBoxes(code) {
32  const lines = code.split('\n')
33  const corners = lines.filter(l => CORNERS.test(l)).length
34  const arrows = lines.filter(l => ARROWS.test(l)).length
35  return corners >= 2 || (arrows >= 2 && lines.length <= 40)
36}
37
38// A heading, or a line ending in ':', just before the diagram makes a good title
39function titleBefore(text) {
40  const lines = text.split('\n').map(l => l.trim()).filter(Boolean).slice(-3).reverse()
41  const pick = lines.find(l => /^#+\s/.test(l) || /:\**$/.test(l))
42  if (!pick || pick.startsWith('```')) return ''
43  return pick.replace(/^#+\s*/, '').replace(/^[-*\d.]+\s+/, '').replace(/[*_`]/g, '').replace(/:$/, '').slice(0, 60)
44}
45
46// ---------------------------------------------------------------------------
47// 2. Parse a mermaid flowchart into nodes and edges
48// ---------------------------------------------------------------------------
49
50// Shape openers, longest first, with their closer and the box we draw for them
51const SHAPES = [
52  ['([', '])', 'round'], ['[[', ']]', 'single'], ['[(', ')]', 'round'], ['((', '))', 'round'],
53  ['{{', '}}', 'double'], ['[/', '/]', 'single'], ['[\\', '\\]', 'single'], ['>', ']', 'single'],
54  ['[', ']', 'single'], ['(', ')', 'round'], ['{', '}', 'double'],
55]
56// Edge operators: an optional label in the middle (-- yes -->) or in pipes (-->|yes|)
57const EDGE_MID = /^\s*(?:<)?(?:--|-\.|==)\s+([^|>]+?)\s+(?:-->|--[ox]|---|\.->|-\.-|==>|===)/
58const EDGE = /^\s*(?:<)?(?:-\.+->|-\.+-|--+>|--+[ox]|---+|==+>|===+|~~~)(?:\|([^|]*)\|)?/
59const SKIP = /^(classDef|class|style|linkStyle|click|direction|%%|accTitle|accDescr)\b/
60
61// A subgraph becomes a node whose `sub` is its contents, so the pane shows it
62// as one box and can go into it. Edges that cross a subgraph's border are drawn
63// to the subgraph's box on the level where both ends are visible.
64export function parseMermaid(code) {
65  const stmts = code.split(/[\n;]/).map(l => l.trim()).filter(Boolean)
66  if (!/^(graph|flowchart)\b/i.test(stmts[0] || '')) return null
67  const nodes = new Map()
68  const edges = []
69  const stack = []
70  for (const stmt of stmts.slice(1)) {
71    if (/^subgraph\b/.test(stmt)) {
72      const g = readSubgraph(stmt.slice(8).trim())
73      const known = nodes.get(g.id)
74      if (known) Object.assign(known, { label: g.label, box: 'round', group: true, parent: stack.at(-1) ?? null })
75      else nodes.set(g.id, { id: g.id, label: g.label, box: 'round', group: true, parent: stack.at(-1) ?? null })
76      stack.push(g.id)
77    } else if (/^end$/.test(stmt)) stack.pop()
78    else if (!SKIP.test(stmt)) parseStatement(stmt, nodes, edges, stack.at(-1) ?? null)
79  }
80  if (!nodes.size) return null
81  return nest(nodes, edges, null)
82}
83
84// `subgraph id`, `subgraph id [Label]`, `subgraph "Label"` or `subgraph Some label`
85function readSubgraph(rest) {
86  // A quoted label may hold brackets: `subgraph S["arr[0] store"]`
87  const m = rest.match(/^(\w+)\s*\[\s*"([^"]*)"\s*\]$/) || rest.match(/^(\w+)\s*\[\s*([^\]"]*)\s*\]$/)
88  if (m) return { id: m[1], label: clean(m[2]) || m[1] }
89  const label = clean(rest) || 'group'
90  return { id: /^\w+$/.test(rest) ? rest : label.replace(/\W+/g, '_'), label }
91}
92
93// The level of the tree under `parent`: its own nodes, and the edges between them
94// after lifting each end to the ancestor that sits on this level
95function nest(flat, edges, parent) {
96  const parentOf = id => flat.get(id)?.parent ?? null
97  const onLevel = id => {
98    let cur = id
99    for (let hops = 0; cur != null && parentOf(cur) !== parent && hops < 50; hops++) cur = parentOf(cur)
100    return cur != null && parentOf(cur) === parent ? cur : null
101  }
102  const nodes = []
103  for (const n of flat.values()) {
104    if ((n.parent ?? null) !== parent) continue
105    const node = { id: n.id, label: n.label, box: n.box }
106    if (n.group) {
107      const sub = nest(flat, edges, n.id)
108      if (sub.nodes.length) node.sub = sub
109    }
110    nodes.push(node)
111  }
112  const out = []
113  const seen = new Set()
114  for (const e of edges) {
115    const from = onLevel(e.from), to = onLevel(e.to)
116    if (!from || !to || from === to) continue
117    const key = `${from}>${to}>${e.label}`
118    if (seen.has(key)) continue
119    seen.add(key)
120    out.push({ from, to, label: e.label })
121  }
122  return { nodes, edges: out }
123}
124
125// A statement is: group (edge group)*, where a group is node (& node)*.
126// A node mentioned inside a subgraph belongs to it, as in mermaid.
127function parseStatement(stmt, nodes, edges, parent) {
128  let rest = stmt
129  let prev = null
130  while (rest.trim()) {
131    const group = []
132    for (;;) {
133      const n = readNode(rest)
134      if (!n) return
135      rest = n.rest
136      const known = nodes.get(n.id)
137      if (!known) nodes.set(n.id, { id: n.id, label: n.label || n.id, box: n.box || 'single', parent })
138      else {
139        if (n.label && !known.group) Object.assign(known, { label: n.label, box: n.box })
140        // Declared first, linked later: a node moves into a deeper subgraph, never out of one.
141        // A subgraph stays where it was declared: moving it could nest it inside itself.
142        if (parent && !known.group && known.id !== parent && within(nodes, parent, known.parent)) known.parent = parent
143      }
144      group.push(n.id)
145      const amp = rest.match(/^\s*&\s*/)
146      if (!amp) break
147      rest = rest.slice(amp[0].length)
148    }
149    if (prev) for (const from of prev.ids) for (const to of group) edges.push({ from, to, label: prev.label })
150    const e = rest.match(EDGE_MID) || rest.match(EDGE)
151    if (!e) return
152    prev = { ids: group, label: clean(e[1] || '') }
153    rest = rest.slice(e[0].length)
154  }
155}
156
157// Is subgraph `inner` inside `outer` (or is `outer` the top level)?
158function within(nodes, inner, outer) {
159  if (outer == null) return true
160  for (let cur = nodes.get(inner)?.parent, hops = 0; cur != null && hops < 50; cur = nodes.get(cur)?.parent, hops++) {
161    if (cur === outer) return true
162  }
163  return false
164}
165
166function readNode(text) {
167  const m = text.match(/^\s*([\w]+)/)
168  if (!m) return null
169  let rest = text.slice(m[0].length)
170  for (const [open, close, box] of SHAPES) {
171    if (!rest.startsWith(open)) continue
172    let body = rest.slice(open.length)
173    let label
174    const quoted = body.match(/^\s*"([^"]*)"\s*/)
175    if (quoted) { label = quoted[1]; body = body.slice(quoted[0].length) }
176    const end = body.indexOf(close)
177    if (end < 0) return null
178    if (label === undefined) label = body.slice(0, end)
179    return { id: m[1], label: clean(label), box, rest: body.slice(end + close.length) }
180  }
181  return { id: m[1], label: '', box: '', rest }
182}
183
184function clean(s) {
185  return s.trim().replace(/^"(.*)"$/, '$1').replace(/<br\s*\/?>/gi, '\n').replace(/<[^>]+>/g, '')
186    .replace(/&quot;/g, '"').replace(/[ \t]+/g, ' ').replace(/ *\n */g, '\n').trim()
187}
188
189// ---------------------------------------------------------------------------
190// 3. Draw a graph as boxes and arrows, top to bottom, in `width` columns
191// ---------------------------------------------------------------------------
192
193const BOX = {
194  single: { tl: '┌', tr: '┐', bl: '└', br: '┘', h: '─', v: '│', out: '┬' },
195  round: { tl: '╭', tr: '╮', bl: '╰', br: '╯', h: '─', v: '│', out: '┬' },
196  double: { tl: '╔', tr: '╗', bl: '╚', br: '╝', h: '═', v: '║', out: '╤' },
197}
198// A connector cell remembers which way its lines go: up, down, left, right
199const U = 1, D = 2, L = 4, R = 8
200const JOIN = { [U | D]: '│', [L | R]: '─', [D | R]: '┌', [D | L]: '┐', [U | R]: '└', [U | L]: '┘',
201  [U | D | R]: '├', [U | D | L]: '┤', [D | L | R]: '┬', [U | L | R]: '┴', [U | D | L | R]: '┼',
202  [U]: '│', [D]: '│', [L]: '─', [R]: '─' }
203
204// graph: { nodes: [{ id, label, box }], edges: [{ from, to, label }] }
205export function drawGraph(graph, width) {
206  const nodes = graph.nodes
207  const ids = new Set(nodes.map(n => n.id))
208  const edges = graph.edges.filter(e => ids.has(e.from) && ids.has(e.to) && e.from !== e.to)
209  const layer = layers(nodes, edges)
210  const rows = displayRows(nodes, edges, layer, width)
211
212  // Measure every box, then place the rows one under another
213  const place = new Map()
214  let y = 0
215  const routed = new Set()
216  const gaps = []
217  rows.forEach((row, r) => {
218    // Labels get 22 columns when the row has room, else 16 (what displayRows packed for)
219    const roomy = row.reduce((s, n) => s + Math.min(longest(n.label), 22) + 6, -2) <= width
220    const boxes = row.map(n => ({ n, lines: wrap(n.label, roomy ? 22 : 16) }))
221    boxes.forEach(b => { b.w = Math.max(...b.lines.map(l => l.length)) + 4; b.h = b.lines.length + 2 })
222    const total = boxes.reduce((s, b) => s + b.w, 0) + (boxes.length - 1) * 2
223    let x = Math.max(0, Math.floor((width - total) / 2))
224    for (const b of boxes) { place.set(b.n.id, { ...b, x, y, row: r, cx: x + Math.floor(b.w / 2) }); x += b.w + 2 }
225    y += Math.max(...boxes.map(b => b.h))
226    // Edges from this row straight to the next one get a 3-line gap to run in
227    const next = new Set((rows[r + 1] || []).map(n => n.id))
228    const down = edges.filter(e => row.some(n => n.id === e.from) && next.has(e.to))
229    down.forEach(e => routed.add(e))
230    if (r < rows.length - 1) { gaps.push({ y, edges: down }); y += down.length ? 3 : 1 }
231  })
232
233  // Paint onto a grid: boxes as characters, connectors as direction bits
234  const grid = Array.from({ length: y }, () => Array.from({ length: width }, () => ({ ch: ' ', bits: 0 })))
235  const put = (gy, gx, ch) => { if (grid[gy]?.[gx]) grid[gy][gx].ch = ch }
236  const bit = (gy, gx, b) => { if (grid[gy]?.[gx]) grid[gy][gx].bits |= b }
237  for (const p of place.values()) {
238    const s = BOX[p.n.box] || BOX.single
239    put(p.y, p.x, s.tl); put(p.y, p.x + p.w - 1, s.tr)
240    put(p.y + p.h - 1, p.x, s.bl); put(p.y + p.h - 1, p.x + p.w - 1, s.br)
241    for (let i = 1; i < p.w - 1; i++) { put(p.y, p.x + i, s.h); put(p.y + p.h - 1, p.x + i, s.h) }
242    p.lines.forEach((line, i) => {
243      put(p.y + 1 + i, p.x, s.v); put(p.y + 1 + i, p.x + p.w - 1, s.v)
244      ;[...line.padEnd(p.w - 4)].forEach((c, j) => put(p.y + 1 + i, p.x + 2 + j, c))
245    })
246  }
247  const labels = []
248  for (const g of gaps) {
249    for (const e of g.edges) {
250      const a = place.get(e.from), b = place.get(e.to)
251      const s = BOX[a.n.box] || BOX.single
252      // Enter straight down when the source sits above the target, else at its centre
253      const tx = a.cx > b.x + 1 && a.cx < b.x + b.w - 2 ? a.cx : b.cx
254      put(a.y + a.h - 1, a.cx, s.out)          // the line leaves the box's bottom edge
255      for (let y = a.y + a.h; y <= g.y; y++) bit(y, a.cx, U | D) // down past shorter boxes
256      bit(g.y + 1, a.cx, U)                   // turn onto the bus row
257      for (let x = Math.min(a.cx, tx); x < Math.max(a.cx, tx); x++) { bit(g.y + 1, x, R); bit(g.y + 1, x + 1, L) }
258      bit(g.y + 1, tx, D)
259      put(g.y + 2, tx, '▼')                  // and points into the target
260      if (e.label) labels.push({ e, y: g.y + 2, x: tx + 1 })
261    }
262  }
263  // Labels go beside their arrow once every arrow is down, so none covers another
264  const notes = []
265  for (const { e, y, x } of labels) {
266    const text = ' ' + short(e.label, 14) + ' '
267    const free = [...text].every((_, j) => grid[y][x + j] && grid[y][x + j].ch === ' ')
268    if (free) [...text.trimEnd()].forEach((c, j) => put(y, x + j, c))
269    else notes.push(e)
270  }
271  const out = grid.map(row => row.map(c => (c.ch === ' ' && c.bits ? JOIN[c.bits] : c.ch)).join('').trimEnd())
272
273  // Edges we could not route straight down are listed under the drawing
274  const label = id => short(nodes.find(n => n.id === id).label, 18)
275  for (const e of edges) if (!routed.has(e) || notes.includes(e)) {
276    const back = layer.get(e.to) <= layer.get(e.from)
277    out.push(short(`  ${back ? '↺' : '⇢'} ${label(e.from)} → ${label(e.to)}${e.label ? '  (' + e.label + ')' : ''}`, width))
278  }
279  return out
280}
281
282// Longest-path layers, after dropping the edges that close a cycle
283function layers(nodes, edges) {
284  const out = new Map(nodes.map(n => [n.id, []]))
285  const state = new Map()
286  const forward = []
287  const visit = id => {
288    state.set(id, 1)
289    for (const e of edges.filter(e => e.from === id)) {
290      if (state.get(e.to) === 1) continue // a back edge: skip it for layering
291      forward.push(e)
292      if (!state.get(e.to)) visit(e.to)
293    }
294    state.set(id, 2)
295  }
296  nodes.forEach(n => { if (!state.get(n.id)) visit(n.id) })
297  forward.forEach(e => out.get(e.from).push(e.to))
298  const layer = new Map(nodes.map(n => [n.id, 0]))
299  for (let pass = 0; pass < nodes.length; pass++) {
300    let moved = false
301    for (const e of forward) if (layer.get(e.to) < layer.get(e.from) + 1) { layer.set(e.to, layer.get(e.from) + 1); moved = true }
302    if (!moved) break
303  }
304  return layer
305}
306
307// Group nodes by layer, order each layer under its parents, and split a layer
308// that is too wide for the pane into several rows
309function displayRows(nodes, edges, layer, width) {
310  const depth = Math.max(...layer.values())
311  const pos = new Map()
312  const rows = []
313  for (let d = 0; d <= depth; d++) {
314    const these = nodes.filter(n => layer.get(n.id) === d)
315    const centre = n => {
316      const ps = edges.filter(e => e.to === n.id && pos.has(e.from)).map(e => pos.get(e.from))
317      return ps.length ? ps.reduce((a, b) => a + b, 0) / ps.length : nodes.indexOf(n)
318    }
319    these.sort((a, b) => centre(a) - centre(b))
320    these.forEach((n, i) => pos.set(n.id, i))
321    // Fill a row while the boxes fit side by side (labels up to 16 wide), then start another
322    let row = [], used = 0
323    for (const n of these) {
324      const w = Math.min(longest(n.label), 16) + 4 + (row.length ? 2 : 0)
325      if (row.length && used + w > width) { rows.push(row); row = [], used = 0 }
326      row.push(n); used += w
327    }
328    if (row.length) rows.push(row)
329  }
330  return rows
331}
332
333// Word-wrap a label into at most 3 lines of `w` columns
334function wrap(text, w) {
335  const lines = []
336  // A <br> in a mermaid label is a line break we keep
337  for (const part of String(text).split('\n')) {
338    let cur = ''
339    for (const word of part.split(/\s+/).filter(Boolean)) {
340      const piece = word.length > w ? word.slice(0, w - 1) + '…' : word
341      if (!cur) cur = piece
342      else if ((cur + ' ' + piece).length <= w) cur += ' ' + piece
343      else { lines.push(cur); cur = piece }
344    }
345    if (cur) lines.push(cur)
346  }
347  if (lines.length > 3) { lines.length = 3; lines[2] = short(lines[2] + ' …', w) }
348  return lines.length ? lines : ['?']
349}
350
351export function short(s, n) {
352  s = String(s).replace(/\n/g, ' ')
353  return s.length <= n ? s : s.slice(0, Math.max(1, n - 1)) + '…'
354}
355
356const longest = label => Math.max(...String(label).split('\n').map(l => l.length))
357
hooks/graph.js 208 lines
1// graph.js: the graph tree the pane navigates and edits. No $ here, just data.
2//
3// A graph is { nodes: [{ id, label, box, now?, sub? }], edges: [{ from, to, label }] }.
4// A node's `sub` is its detail: another graph, one level down.
5//
6//   levelAt(root, path)            -> the graph a path of node ids leads to
7//   setLevel(root, path, level)    -> a copy of root with that level replaced
8//   applyOps(graph, ops)           -> a patched copy, and what changed
9//   uniqueIds(root)                -> a copy where no id appears twice in the tree
10//   toMermaid(graph)               -> flowchart source, details as subgraphs
11
12const KIND_BOX = { file: 'single', component: 'round', step: 'double' }
13
14export const clone = g => JSON.parse(JSON.stringify(g))
15
16export const boxOf = kind => KIND_BOX[kind] || 'single'
17
18// Mermaid ids are words; anything else becomes `_`
19export const cleanId = id => String(id).replace(/\W/g, '_') || '_'
20
21// Follow `path` down through each node's `sub`. Returns { graph } when the path
22// ends on a level, { graph: null, node } when the last node has no detail yet,
23// and { graph: null, node: null } when a node on the path is gone.
24export function levelAt(root, path) {
25  let g = root
26  for (const id of path) {
27    const n = g?.nodes.find(n => n.id === id)
28    if (!n) return { graph: null, node: null }
29    if (!n.sub) return { graph: null, node: n }
30    g = n.sub
31  }
32  return { graph: g, node: null }
33}
34
35// A copy of `root` with the level at `path` replaced; the last node on the path
36// gets `level` as its detail, created when it had none.
37export function setLevel(root, path, level) {
38  if (!path.length) return clone(level)
39  const out = clone(root)
40  let g = out
41  path.forEach((id, i) => {
42    const n = g.nodes.find(n => n.id === id)
43    if (!n) throw new Error(`no node "${id}" on the path`)
44    if (i === path.length - 1) n.sub = clone(level)
45    else if (!n.sub) throw new Error(`node "${id}" has no detail to go into`)
46    else g = n.sub
47  })
48  return out
49}
50
51// Every node id in the tree, at every level
52export function allIds(g, out = new Set()) {
53  for (const n of g?.nodes || []) {
54    out.add(n.id)
55    if (n.sub) allIds(n.sub, out)
56  }
57  return out
58}
59
60// Mermaid ids are global, so a detail's ids must not clash with the rest of the
61// tree: clashing ones get `prefix_` in front. `taken` is the rest of the tree's ids.
62export function adoptIds(level, taken, prefix) {
63  const out = clone(level)
64  const rename = new Map()
65  const walk = g => {
66    for (const n of g.nodes) {
67      if (taken.has(n.id)) {
68        let id = `${cleanId(prefix)}_${n.id}`
69        for (let i = 2; taken.has(id); i++) id = `${cleanId(prefix)}_${n.id}${i}`
70        rename.set(n.id, id)
71        n.id = id
72      }
73      taken.add(n.id)
74      if (n.sub) walk(n.sub)
75    }
76    for (const e of g.edges) {
77      e.from = rename.get(e.from) || e.from
78      e.to = rename.get(e.to) || e.to
79    }
80  }
81  walk(out)
82  return out
83}
84
85// Mermaid ids are global, so one id may appear only once in the whole tree.
86// Levels are walked top first, so a box on a higher level keeps its id and a
87// clashing one further down gets its parent's id in front.
88export function uniqueIds(root) {
89  const out = clone(root)
90  const taken = new Set()
91  let level = [{ g: out, prefix: null }]
92  while (level.length) {
93    const below = []
94    for (const { g, prefix } of level) {
95      const rename = new Map()
96      for (const n of g.nodes) {
97        if (taken.has(n.id)) {
98          const base = prefix ? `${cleanId(prefix)}_${n.id}` : n.id
99          let id = base
100          for (let i = 2; taken.has(id); i++) id = `${base}${i}`
101          rename.set(n.id, id)
102          n.id = id
103        }
104        taken.add(n.id)
105      }
106      for (const e of g.edges) {
107        e.from = rename.get(e.from) || e.from
108        e.to = rename.get(e.to) || e.to
109      }
110      for (const n of g.nodes) if (n.sub) below.push({ g: n.sub, prefix: n.id })
111    }
112    level = below
113  }
114  return out
115}
116
117// Keep the details of nodes that survive a redraw. A node survives on the same
118// id; with `sameLabel` it must keep its label too (a fork may reuse ids).
119export function carrySubs(before, after, { sameLabel = false } = {}) {
120  const out = clone(after)
121  for (const n of out.nodes) {
122    const old = before?.nodes.find(o => o.id === n.id && (!sameLabel || o.label === n.label))
123    if (old?.sub && !n.sub) n.sub = clone(old.sub)
124  }
125  return out
126}
127
128// Patch a graph. ops: [{ op, id, label, kind, from, to }], op one of
129// add_node, remove_node, relabel, set_kind, add_edge, remove_edge.
130// Returns { graph, changes }; throws on an op it cannot apply.
131export function applyOps(graph, ops) {
132  const g = clone(graph || { nodes: [], edges: [] })
133  const changes = []
134  const find = id => {
135    const n = g.nodes.find(n => n.id === id)
136    if (!n) throw new Error(`no node "${id}" (ids: ${g.nodes.map(n => n.id).join(', ') || 'none'})`)
137    return n
138  }
139  for (const o of ops || []) {
140    switch (o.op) {
141      case 'add_node': {
142        const id = cleanId(o.id || o.label)
143        if (g.nodes.some(n => n.id === id)) throw new Error(`node "${id}" already exists`)
144        g.nodes.push({ id, label: String(o.label || id), box: boxOf(o.kind) })
145        changes.push(`added ${id}`)
146        break
147      }
148      case 'remove_node': {
149        const n = find(o.id)
150        g.nodes = g.nodes.filter(x => x !== n)
151        const gone = g.edges.filter(e => e.from === n.id || e.to === n.id).length
152        g.edges = g.edges.filter(e => e.from !== n.id && e.to !== n.id)
153        changes.push(`removed ${n.id}` + (gone ? ` and ${gone} edge${gone > 1 ? 's' : ''}` : ''))
154        break
155      }
156      case 'relabel': {
157        const n = find(o.id)
158        n.label = String(o.label ?? n.label)
159        changes.push(`relabelled ${n.id}`)
160        break
161      }
162      case 'set_kind': {
163        find(o.id).box = boxOf(o.kind)
164        changes.push(`${o.id} is now a ${o.kind}`)
165        break
166      }
167      case 'add_edge': {
168        find(o.from), find(o.to)
169        g.edges.push({ from: o.from, to: o.to, label: String(o.label || '') })
170        changes.push(`linked ${o.from} → ${o.to}`)
171        break
172      }
173      case 'remove_edge': {
174        const before = g.edges.length
175        g.edges = g.edges.filter(e => !(e.from === o.from && e.to === o.to))
176        if (g.edges.length === before) throw new Error(`no edge ${o.from} → ${o.to}`)
177        changes.push(`unlinked ${o.from} → ${o.to}`)
178        break
179      }
180      default:
181        throw new Error(`unknown op "${o.op}"`)
182    }
183  }
184  return { graph: g, changes }
185}
186
187// Flowchart source for a graph; a node with a detail becomes a subgraph, so
188// parseMermaid reads the whole tree back
189export function toMermaid(graph) {
190  return ['flowchart TD', ...body(graph, '  ')].join('\n')
191}
192
193function body(g, pad) {
194  const out = []
195  for (const n of g.nodes) {
196    if (n.sub?.nodes.length) {
197      out.push(`${pad}subgraph ${n.id}["${quote(n.label)}"]`, ...body(n.sub, pad + '  '), `${pad}end`)
198    } else {
199      const [open, close] = { round: ['("', '")'], double: ['{"', '"}'] }[n.box] || ['["', '"]']
200      out.push(`${pad}${n.id}${open}${quote(n.label)}${close}`)
201    }
202  }
203  for (const e of g.edges) out.push(`${pad}${e.from} -->${e.label ? `|${quote(e.label)}|` : ''} ${e.to}`)
204  return out
205}
206
207const quote = s => String(s).replace(/"/g, "'").replace(/\|/g, '/').replace(/\n/g, '<br>')
208