@itsthecsguy/linework 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/LICENSE +21 -0
  2. package/NOTICE +11 -0
  3. package/README.md +206 -2
  4. package/assets/drawio/LICENSE +201 -0
  5. package/assets/drawio/VERSION +1 -0
  6. package/assets/drawio/favicon.ico +0 -0
  7. package/assets/drawio/images/1x1.png +0 -0
  8. package/assets/drawio/images/2x2.png +0 -0
  9. package/assets/drawio/images/3x3.png +0 -0
  10. package/assets/drawio/images/ajax-loader.gif +0 -0
  11. package/assets/drawio/images/android-chrome-512x512.png +0 -0
  12. package/assets/drawio/images/apple-touch-icon.png +0 -0
  13. package/assets/drawio/images/aui-wait.gif +0 -0
  14. package/assets/drawio/images/broken.png +0 -0
  15. package/assets/drawio/images/browserconfig.xml +9 -0
  16. package/assets/drawio/images/close.png +0 -0
  17. package/assets/drawio/images/cloud-connector-atlas.png +0 -0
  18. package/assets/drawio/images/collapsed.gif +0 -0
  19. package/assets/drawio/images/delete.png +0 -0
  20. package/assets/drawio/images/document-google-drive-icon-80.png +0 -0
  21. package/assets/drawio/images/draw-search.png +0 -0
  22. package/assets/drawio/images/drawio.svg +19 -0
  23. package/assets/drawio/images/drawlogo-color.svg +50 -0
  24. package/assets/drawio/images/drawlogo-gray.svg +18 -0
  25. package/assets/drawio/images/drawlogo-text-bottom.svg +75 -0
  26. package/assets/drawio/images/drawlogo.svg +18 -0
  27. package/assets/drawio/images/drawlogo128.png +0 -0
  28. package/assets/drawio/images/drawlogo144.png +0 -0
  29. package/assets/drawio/images/drawlogo256.png +0 -0
  30. package/assets/drawio/images/drawlogo48-gray.png +0 -0
  31. package/assets/drawio/images/drawlogo48.png +0 -0
  32. package/assets/drawio/images/drawlogo80.png +0 -0
  33. package/assets/drawio/images/droptarget.png +0 -0
  34. package/assets/drawio/images/edit.gif +0 -0
  35. package/assets/drawio/images/edit16.png +0 -0
  36. package/assets/drawio/images/edit32.png +0 -0
  37. package/assets/drawio/images/edit80.png +0 -0
  38. package/assets/drawio/images/embed-icon.png +0 -0
  39. package/assets/drawio/images/expanded.gif +0 -0
  40. package/assets/drawio/images/facebook.png +0 -0
  41. package/assets/drawio/images/favicon-16x16.png +0 -0
  42. package/assets/drawio/images/favicon-32x32.png +0 -0
  43. package/assets/drawio/images/file.png +0 -0
  44. package/assets/drawio/images/folder.png +0 -0
  45. package/assets/drawio/images/gadget-screenshot.png +0 -0
  46. package/assets/drawio/images/gadget-thumb.png +0 -0
  47. package/assets/drawio/images/github-logo-white.svg +6 -0
  48. package/assets/drawio/images/github-logo.svg +17 -0
  49. package/assets/drawio/images/gitlab-logo-white.svg +1 -0
  50. package/assets/drawio/images/gitlab-logo.svg +1 -0
  51. package/assets/drawio/images/glyphicons_blogger.png +0 -0
  52. package/assets/drawio/images/glyphicons_facebook.png +0 -0
  53. package/assets/drawio/images/glyphicons_github.png +0 -0
  54. package/assets/drawio/images/glyphicons_google.png +0 -0
  55. package/assets/drawio/images/glyphicons_star.png +0 -0
  56. package/assets/drawio/images/glyphicons_twitter.png +0 -0
  57. package/assets/drawio/images/glyphicons_youtube.png +0 -0
  58. package/assets/drawio/images/google-drive-connector-atlas.png +0 -0
  59. package/assets/drawio/images/google-drive-connector-atlas.svg +121 -0
  60. package/assets/drawio/images/google-drive-logo-white.svg +9 -0
  61. package/assets/drawio/images/google-drive-logo.svg +17 -0
  62. package/assets/drawio/images/google-share.png +0 -0
  63. package/assets/drawio/images/icon-192-maskable.png +0 -0
  64. package/assets/drawio/images/icon-192.png +0 -0
  65. package/assets/drawio/images/icon-512-maskable.png +0 -0
  66. package/assets/drawio/images/icon-512.png +0 -0
  67. package/assets/drawio/images/icon-search.svg +21 -0
  68. package/assets/drawio/images/img-hi-res.png +0 -0
  69. package/assets/drawio/images/img-lo-res.png +0 -0
  70. package/assets/drawio/images/list.svg +16 -0
  71. package/assets/drawio/images/locked.png +0 -0
  72. package/assets/drawio/images/log.png +0 -0
  73. package/assets/drawio/images/logo-confluence.png +0 -0
  74. package/assets/drawio/images/logo-flat-small.png +0 -0
  75. package/assets/drawio/images/logo-flat.png +0 -0
  76. package/assets/drawio/images/logo-jira.png +0 -0
  77. package/assets/drawio/images/logo-small.gif +0 -0
  78. package/assets/drawio/images/logo-white.png +0 -0
  79. package/assets/drawio/images/logo.png +0 -0
  80. package/assets/drawio/images/manifest.json +25 -0
  81. package/assets/drawio/images/move.png +0 -0
  82. package/assets/drawio/images/notion-logo-white.svg +1 -0
  83. package/assets/drawio/images/notion-logo.svg +1 -0
  84. package/assets/drawio/images/onedrive-connector-atlas.png +0 -0
  85. package/assets/drawio/images/onedrive-connector-atlas.svg +248 -0
  86. package/assets/drawio/images/onedrive-logo-white.svg +29 -0
  87. package/assets/drawio/images/onedrive-logo.svg +29 -0
  88. package/assets/drawio/images/osa_database.png +0 -0
  89. package/assets/drawio/images/osa_drive-harddisk.png +0 -0
  90. package/assets/drawio/images/plant-uml-atlas.png +0 -0
  91. package/assets/drawio/images/samepage-icon-color.svg +37 -0
  92. package/assets/drawio/images/save.png +0 -0
  93. package/assets/drawio/images/sidebar-android.png +0 -0
  94. package/assets/drawio/images/sidebar-archimate.png +0 -0
  95. package/assets/drawio/images/sidebar-arrows.png +0 -0
  96. package/assets/drawio/images/sidebar-arrows2.png +0 -0
  97. package/assets/drawio/images/sidebar-basic.png +0 -0
  98. package/assets/drawio/images/sidebar-c4.png +0 -0
  99. package/assets/drawio/images/sidebar-cabinets.png +0 -0
  100. package/assets/drawio/images/sidebar-cumulus.png +0 -0
  101. package/assets/drawio/images/sidebar-dfd.png +0 -0
  102. package/assets/drawio/images/sidebar-dynamics365.png +0 -0
  103. package/assets/drawio/images/sidebar-eip.png +0 -0
  104. package/assets/drawio/images/sidebar-er.png +0 -0
  105. package/assets/drawio/images/sidebar-floorplans.png +0 -0
  106. package/assets/drawio/images/sidebar-flowchart.png +0 -0
  107. package/assets/drawio/images/sidebar-gcp3.png +0 -0
  108. package/assets/drawio/images/sidebar-general.png +0 -0
  109. package/assets/drawio/images/sidebar-kubernetes.png +0 -0
  110. package/assets/drawio/images/sidebar-leanmapping.png +0 -0
  111. package/assets/drawio/images/sidebar-mockups.png +0 -0
  112. package/assets/drawio/images/sidebar-network.png +0 -0
  113. package/assets/drawio/images/sidebar-network2.png +0 -0
  114. package/assets/drawio/images/sidebar-openstack.png +0 -0
  115. package/assets/drawio/images/sidebar-sitemap.png +0 -0
  116. package/assets/drawio/images/sidebar-threatmodeling.png +0 -0
  117. package/assets/drawio/images/sidebar-uml.png +0 -0
  118. package/assets/drawio/images/sketch-logo128.png +0 -0
  119. package/assets/drawio/images/spin.gif +0 -0
  120. package/assets/drawio/images/sprites.png +0 -0
  121. package/assets/drawio/images/stop-flat-icon-80.png +0 -0
  122. package/assets/drawio/images/submenu.gif +0 -0
  123. package/assets/drawio/images/touch-submenu.png +0 -0
  124. package/assets/drawio/images/transparent.gif +0 -0
  125. package/assets/drawio/images/trello-logo-white-orange.svg +6 -0
  126. package/assets/drawio/images/trello-logo-white.svg +6 -0
  127. package/assets/drawio/images/trello-logo.svg +1 -0
  128. package/assets/drawio/images/tweet.png +0 -0
  129. package/assets/drawio/images/update16.png +0 -0
  130. package/assets/drawio/images/update32.png +0 -0
  131. package/assets/drawio/images/update80.png +0 -0
  132. package/assets/drawio/images/youtube.png +0 -0
  133. package/assets/drawio/img/clipart/Battery_0_128x128.png +0 -0
  134. package/assets/drawio/img/clipart/Battery_100_128x128.png +0 -0
  135. package/assets/drawio/img/clipart/Battery_50_128x128.png +0 -0
  136. package/assets/drawio/img/clipart/Battery_75_128x128.png +0 -0
  137. package/assets/drawio/img/clipart/Battery_allstates_128x128.png +0 -0
  138. package/assets/drawio/img/clipart/Bluetooth_128x128.png +0 -0
  139. package/assets/drawio/img/clipart/Earth_globe_128x128.png +0 -0
  140. package/assets/drawio/img/clipart/Empty_Folder_128x128.png +0 -0
  141. package/assets/drawio/img/clipart/Full_Folder_128x128.png +0 -0
  142. package/assets/drawio/img/clipart/Gear_128x128.png +0 -0
  143. package/assets/drawio/img/clipart/Keys_128x128.png +0 -0
  144. package/assets/drawio/img/clipart/Lock_128x128.png +0 -0
  145. package/assets/drawio/img/clipart/Mouse_Pointer_128x128.png +0 -0
  146. package/assets/drawio/img/clipart/Plug_128x128.png +0 -0
  147. package/assets/drawio/img/clipart/Ships_Wheel_128x128.png +0 -0
  148. package/assets/drawio/img/clipart/Star_128x128.png +0 -0
  149. package/assets/drawio/img/clipart/Tire_128x128.png +0 -0
  150. package/assets/drawio/index.html +92 -0
  151. package/assets/drawio/js/PostConfig.js +7 -0
  152. package/assets/drawio/js/PreConfig.js +13 -0
  153. package/assets/drawio/js/app.min.js +18466 -0
  154. package/assets/drawio/js/bootstrap.js +428 -0
  155. package/assets/drawio/js/diagramly/LibavoidRouting.js +4459 -0
  156. package/assets/drawio/js/export-init.js +21 -0
  157. package/assets/drawio/js/extensions.min.js +10478 -0
  158. package/assets/drawio/js/libavoid-js/LICENSE +505 -0
  159. package/assets/drawio/js/libavoid-js/libavoid-routing.js +1633 -0
  160. package/assets/drawio/js/libavoid-js/libavoid.min.js +59 -0
  161. package/assets/drawio/js/main.js +22 -0
  162. package/assets/drawio/js/rough/rough.min.js +1 -0
  163. package/assets/drawio/js/shapes-14-6-5.min.js +3457 -0
  164. package/assets/drawio/js/stencils.min.js +447 -0
  165. package/assets/drawio/mxgraph/css/common.css +147 -0
  166. package/assets/drawio/resources/dia.txt +2901 -0
  167. package/assets/drawio/styles/dark-default.xml +109 -0
  168. package/assets/drawio/styles/default-old.xml +109 -0
  169. package/assets/drawio/styles/default.xml +203 -0
  170. package/assets/drawio/styles/fonts/ArchitectsDaughter-Regular.ttf +0 -0
  171. package/assets/drawio/styles/grapheditor.css +4501 -0
  172. package/assets/drawio/styles/high-contrast.css +128 -0
  173. package/assets/drawio/styles/thumb_horz.png +0 -0
  174. package/assets/drawio/styles/thumb_vertical.png +0 -0
  175. package/assets/fonts/ibm-plex-mono-LICENSE.txt +93 -0
  176. package/assets/fonts/ibm-plex-sans-LICENSE.txt +93 -0
  177. package/assets/fonts/kalam-400.woff2 +0 -0
  178. package/assets/fonts/kalam-700.woff2 +0 -0
  179. package/assets/fonts/kalam-LICENSE.txt +93 -0
  180. package/assets/fonts/plex-mono-400.woff2 +0 -0
  181. package/assets/fonts/plex-mono-600.woff2 +0 -0
  182. package/assets/fonts/plex-sans-400.woff2 +0 -0
  183. package/assets/fonts/plex-sans-600.woff2 +0 -0
  184. package/dist/api.js +218 -0
  185. package/dist/cli.js +275 -0
  186. package/dist/drawio/icons.js +121 -0
  187. package/dist/drawio/merge.js +309 -0
  188. package/dist/drawio/read.js +180 -0
  189. package/dist/drawio/show.js +501 -0
  190. package/dist/drawio/theme.js +425 -0
  191. package/dist/drawio/write.js +300 -0
  192. package/dist/edit/server.js +212 -0
  193. package/dist/git.js +46 -0
  194. package/dist/import/mermaid.js +569 -0
  195. package/dist/layout/layout.js +212 -0
  196. package/dist/layout/sequence.js +333 -0
  197. package/dist/layout/text.js +73 -0
  198. package/dist/lint/check.js +188 -0
  199. package/dist/lint/fixlabels.js +128 -0
  200. package/dist/mcp/server.js +239 -0
  201. package/dist/publish.js +121 -0
  202. package/dist/render/browser.js +236 -0
  203. package/dist/render/fonts.js +66 -0
  204. package/dist/report.js +36 -0
  205. package/dist/spec/model.js +53 -0
  206. package/dist/spec/parse.js +497 -0
  207. package/package.json +71 -4
  208. package/skills/linework/SKILL.md +233 -0
  209. package/skills/linework/examples/billing-er.yaml +31 -0
  210. package/skills/linework/examples/checkout.yaml +28 -0
  211. package/skills/linework/examples/pay-sequence.yaml +28 -0
  212. package/skills/linework/examples/payment-state.yaml +22 -0
  213. package/skills/linework/examples/refund-flow.yaml +29 -0
  214. package/skills/linework/examples/serverless-aws.yaml +23 -0
  215. package/skills/linework/references/google-docs.md +46 -0
  216. package/skills/linework/references/spec.md +312 -0
  217. package/skills/linework/references/style-guide.md +53 -0
@@ -0,0 +1,501 @@
1
+ // draw.io model -> Diagram -> YAML spec. Works on files linework wrote, files a
2
+ // person edited, and plain draw.io files with no linework metadata at all.
3
+ import { DEFAULT_KIND, DIAGRAM_TYPES, DIRECTIONS, FRAME_OPS, KINDS, THEMES, kindDef, } from '../spec/model.js';
4
+ import { humanize } from '../spec/parse.js';
5
+ import { absBox, labelLines } from './read.js';
6
+ import { cardFromArrow } from './theme.js';
7
+ import { lookOf } from './write.js';
8
+ import { COLORS } from '../spec/model.js';
9
+ const ID_RE = /^[A-Za-z_][\w.-]*$/;
10
+ const FIELD_FLAGS = new Set(['pk', 'fk', 'uk', 'unique', 'null', 'not_null']);
11
+ const isLifeline = (c) => c.style.shape === 'umlLifeline';
12
+ const isEntityStyle = (c) => (c.style.shape === 'swimlane' || 'swimlane' in c.style) && c.style.childLayout === 'stackLayout';
13
+ export function toDiagram(m) {
14
+ const notes = [];
15
+ const vertices = m.cells.filter((c) => c.vertex && c.attrs.aiviz_type !== 'icon');
16
+ let type = m.meta.aiviz_type;
17
+ if (!type || !DIAGRAM_TYPES.includes(type)) {
18
+ type = vertices.some(isLifeline)
19
+ ? 'sequence'
20
+ : vertices.some((v) => isEntityStyle(v) && !v.attrs.aiviz_type)
21
+ ? 'er'
22
+ : vertices.some((v) => 'rhombus' in v.style)
23
+ ? 'flow'
24
+ : 'architecture';
25
+ if (!m.meta.aiviz_type)
26
+ notes.push(`no linework metadata; guessed type: ${type}`);
27
+ }
28
+ const dirRaw = m.meta.aiviz_direction;
29
+ const direction = dirRaw && DIRECTIONS.includes(dirRaw) ? dirRaw : 'auto';
30
+ const themeRaw = m.meta.aiviz_theme;
31
+ const theme = themeRaw && THEMES.includes(themeRaw) ? themeRaw : 'clean';
32
+ if (type === 'sequence')
33
+ return sequenceDiagram(m, theme, notes);
34
+ const isEntity = (c) => type === 'er' && ((!!c.attrs.aiviz_kind && kindDef('er', c.attrs.aiviz_kind).shape === 'table') || isEntityStyle(c));
35
+ const entityIds = new Set(vertices.filter(isEntity).map((c) => c.id));
36
+ const isRow = (c) => !!c.parent && entityIds.has(c.parent);
37
+ const hasChildren = new Set(vertices.filter((v) => !isRow(v)).map((v) => v.parent).filter(Boolean));
38
+ const isGroup = (c) => !isEntity(c) && !c.attrs.aiviz_kind && (c.attrs.aiviz_type === 'group' || c.style.container === '1' || 'swimlane' in c.style || hasChildren.has(c.id));
39
+ // Text-only cells (free notes) aren't diagram nodes.
40
+ const isText = (c) => 'text' in c.style && !c.attrs.aiviz_kind && !isRow(c);
41
+ // Assign spec ids: keep linework/readable ids, slug the rest from their labels.
42
+ const idMap = new Map();
43
+ const used = new Set();
44
+ const claim = (want) => {
45
+ let id = want, n = 2;
46
+ while (used.has(id))
47
+ id = `${want}_${n++}`;
48
+ used.add(id);
49
+ return id;
50
+ };
51
+ const known = (c) => !!(c.attrs.aiviz_kind || c.attrs.aiviz_type);
52
+ const nodeCells = vertices.filter((c) => !isRow(c));
53
+ for (const c of nodeCells)
54
+ if (known(c) && ID_RE.test(c.id))
55
+ idMap.set(c.id, claim(c.id));
56
+ for (const c of nodeCells) {
57
+ if (idMap.has(c.id) || isText(c))
58
+ continue;
59
+ const text = labelLines(c.label)[0] ?? '';
60
+ idMap.set(c.id, claim(slug(text) || slug(c.id) || 'node'));
61
+ }
62
+ const groups = [];
63
+ const nodes = [];
64
+ for (const c of nodeCells) {
65
+ if (isText(c))
66
+ continue;
67
+ const id = idMap.get(c.id);
68
+ const parentCell = c.parent && c.parent !== '1' ? m.byId.get(c.parent) : undefined;
69
+ const parent = parentCell && isGroup(parentCell) ? idMap.get(parentCell.id) : undefined;
70
+ const lines = labelLines(c.label);
71
+ if (isGroup(c)) {
72
+ groups.push({ id, label: lines.join(' ') || c.attrs.aiviz_label || humanize(id), parent, ...look(c) });
73
+ continue;
74
+ }
75
+ const kind = c.attrs.aiviz_kind && KINDS[type][c.attrs.aiviz_kind] ? c.attrs.aiviz_kind : isEntity(c) ? 'entity' : inferKind(type, c);
76
+ if (c.attrs.aiviz_kind && !KINDS[type][c.attrs.aiviz_kind])
77
+ notes.push(`${id}: kind "${c.attrs.aiviz_kind}" isn't valid for ${type}; using ${kind}`);
78
+ const node = { id, kind, label: lines[0] ?? c.attrs.aiviz_label ?? '', parent, ...look(c) };
79
+ if (isEntity(c)) {
80
+ node.label = lines.join(' ') || c.attrs.aiviz_label || humanize(id);
81
+ const rows = vertices.filter((r) => r.parent === c.id).sort((a, b) => (a.geom?.y ?? 0) - (b.geom?.y ?? 0));
82
+ const fields = rows.map(parseFieldRow).filter((f) => !!f);
83
+ if (fields.length)
84
+ node.fields = fields;
85
+ }
86
+ else if (lines.length > 1)
87
+ node.tech = lines.slice(1).join(' ');
88
+ if (c.attrs.aiviz_icon)
89
+ node.icon = c.attrs.aiviz_icon;
90
+ nodes.push(node);
91
+ }
92
+ const edges = [];
93
+ const edgeCells = new Map();
94
+ const skipped = vertices.filter(isText).map((c) => c.id);
95
+ const counts = new Map();
96
+ const endOf = (cellId) => {
97
+ if (!cellId)
98
+ return undefined;
99
+ const c = m.byId.get(cellId);
100
+ // An edge drawn to an ER field row belongs to its entity.
101
+ if (c && isRow(c))
102
+ return idMap.get(c.parent);
103
+ return idMap.get(cellId);
104
+ };
105
+ for (const c of m.cells.filter((c) => c.edge)) {
106
+ const from = endOf(c.source);
107
+ const to = endOf(c.target);
108
+ if (!from || !to) {
109
+ notes.push(`skipped an edge that isn't connected at both ends${c.label ? ` ("${labelLines(c.label).join(' ')}")` : ''}`);
110
+ skipped.push(c.id);
111
+ continue;
112
+ }
113
+ if (groups.some((g) => g.id === from || g.id === to)) {
114
+ notes.push(`skipped edge ${from} -> ${to}: edges to groups aren't supported yet`);
115
+ skipped.push(c.id);
116
+ continue;
117
+ }
118
+ const base = `${from}->${to}`;
119
+ const n = (counts.get(base) ?? 0) + 1;
120
+ counts.set(base, n);
121
+ const label = labelLines(c.label).join(' ') || undefined;
122
+ const eid = n === 1 ? base : `${base}#${n}`;
123
+ edgeCells.set(eid, c.id);
124
+ const edge = { id: eid, from, to, label, style: edgeStyleOf(c), ...look(c) };
125
+ if (type === 'er') {
126
+ edge.style = 'line';
127
+ edge.card = {
128
+ from: cardFromArrow(c.style.startArrow) ?? 'one',
129
+ to: cardFromArrow(c.style.endArrow) ?? 'many',
130
+ identifying: c.style.dashed !== '1',
131
+ };
132
+ }
133
+ edges.push(edge);
134
+ }
135
+ return { diagram: { type, title: m.meta.aiviz_title, direction, theme, groups, nodes, edges }, notes, idMap, edgeCells, skipped };
136
+ }
137
+ function look(c) {
138
+ const l = lookOf(c.attrs);
139
+ if (l.color && !COLORS.includes(l.color))
140
+ delete l.color;
141
+ return l;
142
+ }
143
+ function parseFieldRow(c) {
144
+ if (c.attrs.aiviz_field && !c.label)
145
+ return { name: c.attrs.aiviz_field, type: c.attrs.aiviz_ftype, flags: (c.attrs.aiviz_flags ?? '').split(' ').filter(Boolean) };
146
+ const text = labelLines(c.label).join(' ').replace(/\s+/g, ' ').trim();
147
+ if (!text)
148
+ return null;
149
+ // "name: type flags" (draw.io's own ER rows) or "name type flags" (ours)
150
+ const m = /^([^\s:]+):?\s*(.*)$/.exec(text);
151
+ const flags = [];
152
+ const type = [];
153
+ for (const tok of m[2].split(' ').filter(Boolean))
154
+ (FIELD_FLAGS.has(tok.toLowerCase()) ? flags : type).push(FIELD_FLAGS.has(tok.toLowerCase()) ? tok.toLowerCase() : tok);
155
+ return { name: m[1], type: type.join(' ') || undefined, flags };
156
+ }
157
+ // ---------- sequence ----------
158
+ function sequenceDiagram(m, theme, notes) {
159
+ const idMap = new Map();
160
+ const used = new Set();
161
+ const lifelines = m.cells.filter((c) => c.vertex && isLifeline(c)).sort((a, b) => (absBox(m, a.id)?.x ?? 0) - (absBox(m, b.id)?.x ?? 0));
162
+ const nodes = [];
163
+ for (const c of lifelines) {
164
+ const lines = labelLines(c.label);
165
+ let id = c.attrs.aiviz_kind && ID_RE.test(c.id) ? c.id : slug(lines[0] ?? '') || 'participant';
166
+ let n = 2;
167
+ const base = id;
168
+ while (used.has(id))
169
+ id = `${base}_${n++}`;
170
+ used.add(id);
171
+ idMap.set(c.id, id);
172
+ const kind = c.attrs.aiviz_kind && KINDS.sequence[c.attrs.aiviz_kind] ? c.attrs.aiviz_kind : c.style.dashed === '1' ? 'external' : 'service';
173
+ nodes.push({ id, kind, label: lines[0] ?? humanize(id), tech: lines.length > 1 ? lines.slice(1).join(' ') : undefined, ...look(c) });
174
+ }
175
+ // Activation bars and anything else inside a lifeline belong to that participant.
176
+ const partOf = (cellId) => {
177
+ for (let c = cellId ? m.byId.get(cellId) : undefined; c; c = c.parent ? m.byId.get(c.parent) : undefined)
178
+ if (idMap.has(c.id))
179
+ return idMap.get(c.id);
180
+ return undefined;
181
+ };
182
+ const center = (id) => {
183
+ const cell = lifelines.find((c) => idMap.get(c.id) === id);
184
+ const b = absBox(m, cell.id);
185
+ return b.x + b.w / 2;
186
+ };
187
+ const events = [];
188
+ const skipped = [];
189
+ const yAt = (c) => {
190
+ const end = (cellId, fx) => {
191
+ const b = cellId ? absBox(m, cellId) : undefined;
192
+ return b && fx != null ? b.y + Number(fx) * b.h : undefined;
193
+ };
194
+ return end(c.source, c.style.exitY) ?? end(c.target, c.style.entryY) ?? c.geom?.points[0]?.y;
195
+ };
196
+ const isFrame = (c) => c.attrs.aiviz_type === 'frame' || c.style.shape === 'umlFrame';
197
+ const frames = m.cells.filter((c) => c.vertex && isFrame(c));
198
+ const frameBox = new Map(frames.map((f) => [f.id, absBox(m, f.id)]));
199
+ const innermost = (y, x) => frames
200
+ .filter((f) => {
201
+ const b = frameBox.get(f.id);
202
+ return y > b.y && y < b.y + b.h && (x == null || (x >= b.x - 2 && x <= b.x + b.w + 2));
203
+ })
204
+ .sort((a, b) => frameBox.get(a.id).h - frameBox.get(b.id).h)[0];
205
+ for (const c of m.cells) {
206
+ if (c.edge) {
207
+ const from = partOf(c.source), to = partOf(c.target);
208
+ if (from && to) {
209
+ const y = yAt(c);
210
+ if (y == null)
211
+ continue;
212
+ const style = c.attrs.aiviz_msg ?? (c.style.dashed === '1' ? 'reply' : c.style.endArrow === 'open' ? 'async' : 'sync');
213
+ const label = labelLines(c.label).join(' ') || undefined;
214
+ events.push({ y, o: 1, t: 'item', item: { kind: 'msg', from, to, label, style, ...look(c) } });
215
+ }
216
+ else if (!c.source && !c.target && c.geom) {
217
+ // A dashed line across a frame divides its branches.
218
+ const raw = /<mxPoint[^>]*y="([-\d.]+)"[^>]*as="sourcePoint"/.exec(c.geomRaw ?? '');
219
+ const y = raw ? Number(raw[1]) : undefined;
220
+ const f = y != null ? innermost(y) : undefined;
221
+ if (f)
222
+ events.push({ y: y, o: 0.5, t: 'sep', frame: f });
223
+ else
224
+ skipped.push(c.id);
225
+ }
226
+ else
227
+ skipped.push(c.id);
228
+ }
229
+ else if (c.vertex && (c.attrs.aiviz_type === 'note' || c.style.shape === 'note')) {
230
+ const b = absBox(m, c.id);
231
+ let over = c.attrs.aiviz_over?.split(',').filter((p) => nodes.some((n) => n.id === p)) ?? [];
232
+ let side = c.attrs.aiviz_note ?? 'over';
233
+ if (!over.length) {
234
+ over = nodes.filter((n) => center(n.id) >= b.x && center(n.id) <= b.x + b.w).map((n) => n.id);
235
+ if (!over.length && nodes.length) {
236
+ const nearest = [...nodes].sort((a, z) => Math.abs(center(a.id) - (b.x + b.w / 2)) - Math.abs(center(z.id) - (b.x + b.w / 2)))[0];
237
+ over = [nearest.id];
238
+ side = center(nearest.id) < b.x ? 'right' : 'left';
239
+ }
240
+ }
241
+ events.push({ y: b.y + 4, o: 1, t: 'item', item: { kind: 'note', over, side, text: labelLines(c.label).join(' ') } });
242
+ }
243
+ else if (c.vertex && isFrame(c)) {
244
+ const b = frameBox.get(c.id);
245
+ events.push({ y: b.y, o: 0, t: 'start', frame: c }, { y: b.y + b.h, o: 2, t: 'end', frame: c });
246
+ }
247
+ else if (c.vertex && !isLifeline(c) && !partOf(c.id) && !('text' in c.style))
248
+ skipped.push(c.id);
249
+ }
250
+ // Guards: bracketed text near the top of a frame or just under a separator.
251
+ const guards = m.cells.filter((c) => c.vertex && 'text' in c.style).map((c) => ({ c, b: absBox(m, c.id), text: labelLines(c.label).join(' ') }));
252
+ const guardNear = (frame, y) => {
253
+ const fb = frameBox.get(frame.id);
254
+ const g = guards.find((g) => g.b.y >= y - 4 && g.b.y <= y + 26 && g.b.x >= fb.x && g.b.x <= fb.x + fb.w);
255
+ if (!g)
256
+ return undefined;
257
+ g.c = { ...g.c, id: '' }; // consumed
258
+ return g.text.replace(/^\[|\]$/g, '').trim() || undefined;
259
+ };
260
+ for (const g of guards)
261
+ if (!frames.some((f) => { const fb = frameBox.get(f.id); return g.b.y >= fb.y - 4 && g.b.y <= fb.y + fb.h; }))
262
+ skipped.push(g.c.id);
263
+ events.sort((a, b) => a.y - b.y || a.o - b.o);
264
+ const root = [];
265
+ const stack = [];
266
+ const top = () => (stack.length ? stack[stack.length - 1].branches[stack[stack.length - 1].branches.length - 1].items : root);
267
+ for (const ev of events) {
268
+ if (ev.t === 'item')
269
+ top().push(ev.item);
270
+ else if (ev.t === 'start') {
271
+ const label = (labelLines(ev.frame.label)[0] ?? '').toLowerCase();
272
+ const op = (ev.frame.attrs.aiviz_frame ?? label);
273
+ stack.push({ frame: ev.frame, op: FRAME_OPS.includes(op) ? op : 'opt', branches: [{ when: guardNear(ev.frame, ev.y), items: [] }] });
274
+ }
275
+ else if (ev.t === 'sep') {
276
+ const s = stack.find((o) => o.frame.id === ev.frame.id);
277
+ if (s)
278
+ s.branches.push({ when: guardNear(ev.frame, ev.y), items: [] });
279
+ }
280
+ else {
281
+ const i = stack.findIndex((o) => o.frame.id === ev.frame.id);
282
+ if (i < 0)
283
+ continue;
284
+ const [o] = stack.splice(i, 1);
285
+ top().push({ kind: 'frame', op: o.op, branches: o.branches });
286
+ }
287
+ }
288
+ return {
289
+ diagram: { type: 'sequence', title: m.meta.aiviz_title, direction: 'auto', theme, groups: [], nodes, edges: [], seq: root },
290
+ notes,
291
+ idMap,
292
+ edgeCells: new Map(),
293
+ skipped,
294
+ };
295
+ }
296
+ function inferKind(type, c) {
297
+ const s = c.style;
298
+ const has = (k) => k in s;
299
+ const pick = (k) => (KINDS[type][k] ? k : DEFAULT_KIND[type]);
300
+ const small = (c.geom?.w ?? 100) <= 40 && (c.geom?.h ?? 100) <= 40;
301
+ if (s.shape === 'doubleEllipse' || s.shape === 'endState')
302
+ return pick('end');
303
+ if ((has('ellipse') || s.shape === 'ellipse') && small)
304
+ return pick('start');
305
+ if (s.shape?.startsWith('cylinder') || s.shape === 'datastore')
306
+ return pick('database');
307
+ if (has('rhombus') || s.shape === 'rhombus')
308
+ return pick('decision');
309
+ if (s.shape === 'parallelogram')
310
+ return pick('data');
311
+ if (s.shape === 'umlActor' || s.shape === 'actor')
312
+ return pick('person');
313
+ if (s.rounded === '1' && s.arcSize === '50')
314
+ return type === 'flow' ? pick('terminal') : pick('queue');
315
+ if (s.dashed === '1')
316
+ return pick('external');
317
+ return DEFAULT_KIND[type];
318
+ }
319
+ function edgeStyleOf(c) {
320
+ const s = c.style;
321
+ const end = s.endArrow ?? 'classic';
322
+ const start = s.startArrow ?? 'none';
323
+ if (end === 'none' && start === 'none')
324
+ return 'line';
325
+ if (end !== 'none' && start !== 'none')
326
+ return 'bidir';
327
+ if (s.dashed === '1')
328
+ return 'async';
329
+ return 'sync';
330
+ }
331
+ function slug(s) {
332
+ const out = s.toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '').slice(0, 32);
333
+ return out && /^[a-z_]/.test(out) ? out : out ? `n_${out}` : '';
334
+ }
335
+ // ---------- YAML output ----------
336
+ const RESERVED = /^(true|false|yes|no|on|off|null|~|y|n)$/i;
337
+ function q(s, inFlow = false) {
338
+ const plain = /^[A-Za-z_$/(][^:#{}\[\],&*!|>'"%@`\n]*$/.test(s) && !/\s$/.test(s) && !RESERVED.test(s);
339
+ if (plain && !(inFlow && s.includes(',')))
340
+ return s;
341
+ return JSON.stringify(s);
342
+ }
343
+ /** A list item like `- head: text`, quoted as one string when the text needs it. */
344
+ function lineItem(indent, head, text) {
345
+ const startsOdd = /^[\[{"'&*!|>%@`#-]/.test(head);
346
+ if (!text)
347
+ return `${indent}- ${startsOdd ? JSON.stringify(head) : head}`;
348
+ const plain = q(text) === text && !startsOdd;
349
+ return plain ? `${indent}- ${head}: ${text}` : `${indent}- ${JSON.stringify(`${head}: ${text}`)}`;
350
+ }
351
+ const CARD_L = { zero_or_one: '|o', one: '||', many: '}o', one_or_many: '}|' };
352
+ const CARD_R = { zero_or_one: 'o|', one: '||', many: 'o{', one_or_many: '|{' };
353
+ function nodeParts(d, n) {
354
+ const parts = [];
355
+ if (n.label !== humanize(n.id))
356
+ parts.push(`label: ${q(n.label, true)}`);
357
+ if (n.kind !== DEFAULT_KIND[d.type])
358
+ parts.push(`kind: ${n.kind}`);
359
+ if (n.tech)
360
+ parts.push(`tech: ${q(n.tech, true)}`);
361
+ if (n.icon)
362
+ parts.push(`icon: ${n.icon}`);
363
+ if (n.parent)
364
+ parts.push(`in: ${n.parent}`);
365
+ if (n.color)
366
+ parts.push(`color: ${n.color}`);
367
+ if (n.highlight)
368
+ parts.push('highlight: true');
369
+ return parts;
370
+ }
371
+ export function toYaml(d, header) {
372
+ const out = [];
373
+ if (header)
374
+ out.push(...header.split('\n').map((l) => `# ${l}`));
375
+ out.push(`type: ${d.type}`);
376
+ if (d.title)
377
+ out.push(`title: ${q(d.title)}`);
378
+ if (d.type !== 'sequence')
379
+ out.push(`direction: ${d.direction}`);
380
+ if (d.theme !== 'clean')
381
+ out.push(`theme: ${d.theme}`);
382
+ if (d.type === 'sequence') {
383
+ out.push('participants:');
384
+ for (const n of d.nodes)
385
+ out.push(` ${n.id}: { ${nodeParts(d, n).join(', ')} }`.replace('{ }', '{}'));
386
+ out.push('messages:');
387
+ writeSeq(d.seq ?? [], ' ', out);
388
+ return out.join('\n') + '\n';
389
+ }
390
+ if (d.groups.length) {
391
+ out.push('groups:');
392
+ for (const g of d.groups) {
393
+ const parts = [`label: ${q(g.label, true)}`];
394
+ if (g.parent)
395
+ parts.push(`in: ${g.parent}`);
396
+ if (g.color)
397
+ parts.push(`color: ${g.color}`);
398
+ if (g.highlight)
399
+ parts.push('highlight: true');
400
+ out.push(` ${g.id}: { ${parts.join(', ')} }`);
401
+ }
402
+ }
403
+ const isPseudo = (n) => d.type === 'state' && (n.kind === 'start' || n.kind === 'end');
404
+ const pseudo = new Set(d.nodes.filter(isPseudo).map((n) => n.id));
405
+ out.push('nodes:');
406
+ for (const n of d.nodes.filter((n) => !isPseudo(n))) {
407
+ const parts = nodeParts(d, n);
408
+ if (n.fields?.length) {
409
+ out.push(` ${n.id}:`);
410
+ for (const p of parts)
411
+ out.push(` ${p}`);
412
+ out.push(' fields:');
413
+ for (const f of n.fields) {
414
+ const rest = [f.type, ...f.flags].filter(Boolean).join(' ');
415
+ out.push(rest ? ` - ${q(f.name)}: ${q(rest)}` : ` - ${q(f.name)}`);
416
+ }
417
+ }
418
+ else
419
+ out.push(` ${n.id}: { ${parts.join(', ')} }`.replace('{ }', '{}'));
420
+ }
421
+ if (d.edges.length) {
422
+ out.push('edges:');
423
+ const arrow = { sync: '->', async: '-.->', bidir: '<->', line: '--' };
424
+ for (const e of d.edges) {
425
+ const from = pseudo.has(e.from) ? '[*]' : e.from;
426
+ const to = pseudo.has(e.to) ? '[*]' : e.to;
427
+ const op = e.card ? `${CARD_L[e.card.from]}${e.card.identifying ? '--' : '..'}${CARD_R[e.card.to]}` : e.highlight && e.style === 'sync' ? '==>' : arrow[e.style];
428
+ if (!e.card && (e.color || (e.highlight && op !== '==>'))) {
429
+ // The one-line form has no room for a color: use the object form.
430
+ const parts = [`from: ${from === '[*]' ? '"[*]"' : from}`, `to: ${to === '[*]' ? '"[*]"' : to}`];
431
+ if (e.label)
432
+ parts.push(`label: ${q(e.label, true)}`);
433
+ if (e.style !== 'sync')
434
+ parts.push(`style: ${e.style}`);
435
+ if (e.color)
436
+ parts.push(`color: ${e.color}`);
437
+ if (e.highlight)
438
+ parts.push('highlight: true');
439
+ out.push(` - { ${parts.join(', ')} }`);
440
+ }
441
+ else
442
+ out.push(lineItem(' ', `${from} ${op} ${to}`, e.label));
443
+ }
444
+ }
445
+ return out.join('\n') + '\n';
446
+ }
447
+ function writeSeq(items, ind, out) {
448
+ const arrow = { sync: '->', reply: '-->', async: '-.->' };
449
+ for (const it of items) {
450
+ if (it.kind === 'msg')
451
+ out.push(lineItem(ind, `${it.from} ${it.highlight && it.style === 'sync' ? '==>' : arrow[it.style]} ${it.to}`, it.label));
452
+ else if (it.kind === 'note') {
453
+ const side = it.side === 'over' ? 'over' : it.side === 'left' ? 'left of' : 'right of';
454
+ out.push(lineItem(ind, `note ${side} ${it.over.join(', ')}`, it.text || ' '));
455
+ }
456
+ else if (it.op === 'alt' || it.op === 'par') {
457
+ out.push(`${ind}- ${it.op}:`);
458
+ for (const b of it.branches) {
459
+ out.push(`${ind} - when: ${b.when == null ? '""' : q(b.when)}`);
460
+ out.push(`${ind} do:`);
461
+ if (b.items.length)
462
+ writeSeq(b.items, `${ind} `, out);
463
+ else
464
+ out[out.length - 1] += ' []';
465
+ }
466
+ }
467
+ else {
468
+ const b = it.branches[0] ?? { items: [] };
469
+ out.push(`${ind}- ${it.op}: ${b.when == null ? '""' : q(b.when)}`);
470
+ out.push(`${ind} do:`);
471
+ if (b.items.length)
472
+ writeSeq(b.items, `${ind} `, out);
473
+ else
474
+ out[out.length - 1] += ' []';
475
+ }
476
+ }
477
+ }
478
+ const STRUCTURAL = new Set(['bar', 'frame', 'note', 'guard', 'sep', 'icon']);
479
+ /** Changes a person made in draw.io that a plain rebuild would throw away. */
480
+ export function manualEdits(m) {
481
+ const out = [];
482
+ for (const c of m.cells) {
483
+ if (STRUCTURAL.has(c.attrs.aiviz_type ?? '') || c.attrs.aiviz_field || c.attrs.aiviz_msg)
484
+ continue;
485
+ if (c.vertex && !('text' in c.style)) {
486
+ const auto = c.attrs.aiviz_auto?.split(',').map(Number);
487
+ if (!c.attrs.aiviz_kind && !c.attrs.aiviz_type)
488
+ out.push({ id: c.id, what: 'added' });
489
+ else if (auto && c.geom) {
490
+ const [x, y, w, h] = auto;
491
+ if (Math.abs(c.geom.x - x) > 1 || Math.abs(c.geom.y - y) > 1)
492
+ out.push({ id: c.id, what: 'moved' });
493
+ else if (Math.abs(c.geom.w - w) > 1 || Math.abs(c.geom.h - h) > 1)
494
+ out.push({ id: c.id, what: 'resized' });
495
+ }
496
+ }
497
+ else if (c.edge && !c.attrs.aiviz_edge)
498
+ out.push({ id: c.id, what: 'added' });
499
+ }
500
+ return out;
501
+ }