@senad-d/drawme 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/README.md +27 -20
- package/assets/references/diagram-types.md +24 -0
- package/assets/references/troubleshooting.md +2 -2
- package/assets/references/xml-authoring.md +33 -9
- package/docs/{agent-guide.md → reference.md} +47 -126
- package/docs/visual-review-implementation-tasks.md +1 -1
- package/example/drawme-extension-waterfall-overview.drawio +72 -0
- package/example/drawme-how-it-works.drawio +124 -80
- package/example/drawme-installation-guide.drawio +95 -67
- package/example/drawme-validation-decision-tree.drawio +210 -0
- package/package.json +1 -1
- package/src/drawio.ts +12 -3
- package/src/fitcanvas.ts +172 -0
- package/src/geometry.ts +259 -0
- package/src/index.ts +94 -14
- package/src/validate.ts +18 -225
- package/src/workflow.ts +89 -32
package/src/validate.ts
CHANGED
|
@@ -6,18 +6,30 @@
|
|
|
6
6
|
*/
|
|
7
7
|
import { readFile } from "node:fs/promises";
|
|
8
8
|
import { DOMParser } from "@xmldom/xmldom";
|
|
9
|
-
import { type DomEl, attr, childrenByTag, directText,
|
|
9
|
+
import { type DomEl, attr, childrenByTag, directText, firstByTag } from "./dom";
|
|
10
|
+
import {
|
|
11
|
+
type ById,
|
|
12
|
+
type ContentBounds,
|
|
13
|
+
type Point,
|
|
14
|
+
type Rect,
|
|
15
|
+
absRect,
|
|
16
|
+
collectCells,
|
|
17
|
+
contentBounds,
|
|
18
|
+
edgeRoute,
|
|
19
|
+
geometryIsRelative,
|
|
20
|
+
hasInvalidNumber,
|
|
21
|
+
isEdgeLabel,
|
|
22
|
+
isVisible,
|
|
23
|
+
pyFloat,
|
|
24
|
+
rect,
|
|
25
|
+
styleNum,
|
|
26
|
+
} from "./geometry";
|
|
10
27
|
|
|
11
28
|
const RESERVED = new Set(["0", "1"]);
|
|
12
29
|
export const MIN_OUTER_MARGIN = 20;
|
|
13
30
|
const EXCESSIVE_CANVAS_AREA_RATIO = 4;
|
|
14
31
|
const EXCESSIVE_CANVAS_AXIS_RATIO = 0.6;
|
|
15
32
|
|
|
16
|
-
/** [x, y, width, height]. width/height may be NaN when the source omits them. */
|
|
17
|
-
type Rect = [number, number, number, number];
|
|
18
|
-
type Point = [number, number];
|
|
19
|
-
type ById = Map<string, DomEl>;
|
|
20
|
-
|
|
21
33
|
export interface ValidateResult {
|
|
22
34
|
errors: string[];
|
|
23
35
|
warnings: string[];
|
|
@@ -27,180 +39,21 @@ export interface ValidateResult {
|
|
|
27
39
|
score: { total: number; through: number; crossings: number; overlaps: number };
|
|
28
40
|
}
|
|
29
41
|
|
|
30
|
-
/** Python `float()` semantics: accepts nan/inf, strict-numeric otherwise; else undefined (≈ ValueError). */
|
|
31
|
-
function pyFloat(s: string): number | undefined {
|
|
32
|
-
const t = s.trim().toLowerCase();
|
|
33
|
-
if (t === "nan" || t === "+nan" || t === "-nan") return Number.NaN;
|
|
34
|
-
if (t === "inf" || t === "+inf" || t === "infinity" || t === "+infinity") return Infinity;
|
|
35
|
-
if (t === "-inf" || t === "-infinity") return -Infinity;
|
|
36
|
-
if (!/^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$/.test(t)) return undefined;
|
|
37
|
-
const n = Number(t);
|
|
38
|
-
return Number.isNaN(n) ? undefined : n;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
42
|
/** Quote an optional attribute consistently in diagnostic messages. */
|
|
42
43
|
function repr(s: string | null): string {
|
|
43
44
|
return s === null ? "None" : `'${s}'`;
|
|
44
45
|
}
|
|
45
46
|
|
|
46
|
-
function rect(cell: DomEl): Rect | null {
|
|
47
|
-
const gy = firstByTag(cell, "mxGeometry");
|
|
48
|
-
if (!gy) return null;
|
|
49
|
-
const x = pyFloat(attr(gy, "x") ?? "0");
|
|
50
|
-
const y = pyFloat(attr(gy, "y") ?? "0");
|
|
51
|
-
const w = pyFloat(attr(gy, "width") ?? "nan");
|
|
52
|
-
const h = pyFloat(attr(gy, "height") ?? "nan");
|
|
53
|
-
if (x === undefined || y === undefined || w === undefined || h === undefined) return null;
|
|
54
|
-
return [x, y, w, h];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function hasInvalidNumber(values: number[]): boolean {
|
|
58
|
-
return values.some((value) => !Number.isFinite(value));
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function geometryIsRelative(cell: DomEl): boolean {
|
|
62
|
-
return firstByTag(cell, "mxGeometry")?.getAttribute("relative") === "1";
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/** True for separate edge-label vertices. */
|
|
66
|
-
function isEdgeLabel(cell: DomEl): boolean {
|
|
67
|
-
return (attr(cell, "style") ?? "").includes("edgeLabel");
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function isVisible(cell: DomEl, byId: ById): boolean {
|
|
71
|
-
let current: DomEl | undefined = cell;
|
|
72
|
-
const seen = new Set<string>();
|
|
73
|
-
while (current) {
|
|
74
|
-
if (attr(current, "visible") === "0") return false;
|
|
75
|
-
const parent = attr(current, "parent");
|
|
76
|
-
if (!parent || seen.has(parent)) break;
|
|
77
|
-
seen.add(parent);
|
|
78
|
-
current = byId.get(parent);
|
|
79
|
-
}
|
|
80
|
-
return true;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
47
|
function overlap(a: Rect, b: Rect): boolean {
|
|
84
48
|
const [ax, ay, aw, ah] = a;
|
|
85
49
|
const [bx, by, bw, bh] = b;
|
|
86
50
|
return ax < bx + bw && bx < ax + aw && ay < by + bh && by < ay + ah;
|
|
87
51
|
}
|
|
88
52
|
|
|
89
|
-
function styleNum(style: string | null, key: string): number | undefined {
|
|
90
|
-
for (const part of (style ?? "").split(";")) {
|
|
91
|
-
if (part.startsWith(key + "=")) return pyFloat(part.split("=").slice(1).join("="));
|
|
92
|
-
}
|
|
93
|
-
return undefined;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
53
|
function styleHas(style: string | null, key: string, expected: string): boolean {
|
|
97
54
|
return (style ?? "").split(";").includes(`${key}=${expected}`);
|
|
98
55
|
}
|
|
99
56
|
|
|
100
|
-
/** Absolute (x, y, w, h) of a non-relative vertex, summing parent-container offsets. */
|
|
101
|
-
function absRect(cell: DomEl, byId: ById): Rect | null {
|
|
102
|
-
const r = rect(cell);
|
|
103
|
-
if (r === null || hasInvalidNumber(r)) return null;
|
|
104
|
-
let [x, y] = r;
|
|
105
|
-
const [, , w, h] = r;
|
|
106
|
-
let parent = attr(cell, "parent");
|
|
107
|
-
const seen = new Set<string>();
|
|
108
|
-
while (parent && byId.has(parent) && !seen.has(parent)) {
|
|
109
|
-
seen.add(parent);
|
|
110
|
-
const p = byId.get(parent)!;
|
|
111
|
-
if (p.getAttribute("vertex") === "1" && !geometryIsRelative(p)) {
|
|
112
|
-
const pr = rect(p);
|
|
113
|
-
if (pr && !hasInvalidNumber(pr)) {
|
|
114
|
-
x += pr[0];
|
|
115
|
-
y += pr[1];
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
parent = attr(p, "parent");
|
|
119
|
-
}
|
|
120
|
-
return [x, y, w, h];
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/** Absolute point where `edge` meets its source/target vertex (honours exit/entry). */
|
|
124
|
-
function endpoint(edge: DomEl, end: "source" | "target", byId: ById): Point | null {
|
|
125
|
-
const vid = attr(edge, end);
|
|
126
|
-
if (!vid || !byId.has(vid)) return null;
|
|
127
|
-
const box = absRect(byId.get(vid)!, byId);
|
|
128
|
-
if (box === null) return null;
|
|
129
|
-
const [x, y, w, h] = box;
|
|
130
|
-
const style = attr(edge, "style") ?? "";
|
|
131
|
-
const fx = styleNum(style, end === "source" ? "exitX" : "entryX");
|
|
132
|
-
const fy = styleNum(style, end === "source" ? "exitY" : "entryY");
|
|
133
|
-
return [x + (fx ?? 0.5) * w, y + (fy ?? 0.5) * h];
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
function edgeWaypoints(edge: DomEl): Point[] {
|
|
137
|
-
const gy = firstByTag(edge, "mxGeometry");
|
|
138
|
-
if (!gy) return [];
|
|
139
|
-
const arr = firstByTag(gy, "Array");
|
|
140
|
-
if (!arr) return [];
|
|
141
|
-
const points: Point[] = [];
|
|
142
|
-
for (const point of childrenByTag(arr, "mxPoint")) {
|
|
143
|
-
const x = pyFloat(attr(point, "x") ?? "");
|
|
144
|
-
const y = pyFloat(attr(point, "y") ?? "");
|
|
145
|
-
if (x !== undefined && y !== undefined && Number.isFinite(x) && Number.isFinite(y)) points.push([x, y]);
|
|
146
|
-
}
|
|
147
|
-
return points;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/** Absolute polyline for a waypointed edge, or null when auto-routed / unresolved. */
|
|
151
|
-
function edgeRoute(edge: DomEl, byId: ById): Point[] | null {
|
|
152
|
-
const waypoints = edgeWaypoints(edge);
|
|
153
|
-
if (waypoints.length === 0) return null;
|
|
154
|
-
const source = endpoint(edge, "source", byId);
|
|
155
|
-
const target = endpoint(edge, "target", byId);
|
|
156
|
-
if (source === null || target === null) return null;
|
|
157
|
-
return [source, ...waypoints, target];
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function routeMidpoint(points: Point[]): Point {
|
|
161
|
-
const lengths: number[] = [];
|
|
162
|
-
let total = 0;
|
|
163
|
-
for (let i = 0; i < points.length - 1; i++) {
|
|
164
|
-
const length = Math.hypot(points[i + 1][0] - points[i][0], points[i + 1][1] - points[i][1]);
|
|
165
|
-
lengths.push(length);
|
|
166
|
-
total += length;
|
|
167
|
-
}
|
|
168
|
-
let remaining = total / 2;
|
|
169
|
-
for (let i = 0; i < lengths.length; i++) {
|
|
170
|
-
if (remaining <= lengths[i]) {
|
|
171
|
-
const fraction = lengths[i] === 0 ? 0 : remaining / lengths[i];
|
|
172
|
-
return [
|
|
173
|
-
points[i][0] + fraction * (points[i + 1][0] - points[i][0]),
|
|
174
|
-
points[i][1] + fraction * (points[i + 1][1] - points[i][1]),
|
|
175
|
-
];
|
|
176
|
-
}
|
|
177
|
-
remaining -= lengths[i];
|
|
178
|
-
}
|
|
179
|
-
return points.at(-1) ?? [0, 0];
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
/** Conservative box for a separately sized label on an explicitly routed edge. */
|
|
183
|
-
function explicitEdgeLabelRect(cell: DomEl, byId: ById): Rect | null {
|
|
184
|
-
if (!isEdgeLabel(cell)) return null;
|
|
185
|
-
const local = rect(cell);
|
|
186
|
-
const parent = attr(cell, "parent");
|
|
187
|
-
const edge = parent ? byId.get(parent) : undefined;
|
|
188
|
-
if (!local || hasInvalidNumber(local) || edge?.getAttribute("edge") !== "1") return null;
|
|
189
|
-
const route = edgeRoute(edge, byId);
|
|
190
|
-
if (!route) return null;
|
|
191
|
-
const [midX, midY] = routeMidpoint(route);
|
|
192
|
-
const geometry = firstByTag(cell, "mxGeometry");
|
|
193
|
-
const offset = geometry
|
|
194
|
-
? childrenByTag(geometry, "mxPoint").find((point) => attr(point, "as") === "offset")
|
|
195
|
-
: undefined;
|
|
196
|
-
const offsetX = offset ? pyFloat(attr(offset, "x") ?? "0") : 0;
|
|
197
|
-
const offsetY = offset ? pyFloat(attr(offset, "y") ?? "0") : 0;
|
|
198
|
-
if (offsetX === undefined || offsetY === undefined || !Number.isFinite(offsetX) || !Number.isFinite(offsetY)) {
|
|
199
|
-
return null;
|
|
200
|
-
}
|
|
201
|
-
return [midX + offsetX - local[2] / 2, midY + offsetY - local[3] / 2, local[2], local[3]];
|
|
202
|
-
}
|
|
203
|
-
|
|
204
57
|
function orient(a: Point, b: Point, c: Point): number {
|
|
205
58
|
const value = (b[0] - a[0]) * (c[1] - a[1]) - (b[1] - a[1]) * (c[0] - a[0]);
|
|
206
59
|
if (Math.abs(value) < 1e-9) return 0;
|
|
@@ -308,22 +161,6 @@ function geometryWarnings(cells: DomEl[], ids: ById, parents: Set<string | null>
|
|
|
308
161
|
return [...routeThroughWarnings(routed, leaves), ...routeCrossWarnings(routed)];
|
|
309
162
|
}
|
|
310
163
|
|
|
311
|
-
/** Fold UserObject/object wrappers into their inner mxCell so wrapper ids resolve. */
|
|
312
|
-
function collectCells(root: DomEl | null): DomEl[] {
|
|
313
|
-
const cells: DomEl[] = [];
|
|
314
|
-
for (const child of root ? elemChildren(root) : []) {
|
|
315
|
-
if (child.tagName === "mxCell") cells.push(child);
|
|
316
|
-
else if (child.tagName === "UserObject" || child.tagName === "object") {
|
|
317
|
-
const inner = firstByTag(child, "mxCell");
|
|
318
|
-
if (inner) {
|
|
319
|
-
inner.setAttribute("id", attr(child, "id") ?? "");
|
|
320
|
-
cells.push(inner);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return cells;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
164
|
/** Map every cell by id (blank key for id-less cells); report duplicate ids as errors. */
|
|
328
165
|
function buildIds(cells: DomEl[], errors: string[]): ById {
|
|
329
166
|
const ids: ById = new Map();
|
|
@@ -443,50 +280,6 @@ function containmentWarnings(cells: DomEl[], ids: ById): string[] {
|
|
|
443
280
|
.filter((warning): warning is string => warning !== null);
|
|
444
281
|
}
|
|
445
282
|
|
|
446
|
-
interface ContentBounds {
|
|
447
|
-
minX: number;
|
|
448
|
-
minY: number;
|
|
449
|
-
maxX: number;
|
|
450
|
-
maxY: number;
|
|
451
|
-
}
|
|
452
|
-
|
|
453
|
-
function addRectToBounds(bounds: ContentBounds | null, box: Rect): ContentBounds {
|
|
454
|
-
const [x, y, width, height] = box;
|
|
455
|
-
const next = bounds ?? { minX: x, minY: y, maxX: x + width, maxY: y + height };
|
|
456
|
-
next.minX = Math.min(next.minX, x);
|
|
457
|
-
next.minY = Math.min(next.minY, y);
|
|
458
|
-
next.maxX = Math.max(next.maxX, x + width);
|
|
459
|
-
next.maxY = Math.max(next.maxY, y + height);
|
|
460
|
-
return next;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
function addPointToBounds(bounds: ContentBounds | null, point: Point): ContentBounds {
|
|
464
|
-
return addRectToBounds(bounds, [point[0], point[1], 0, 0]);
|
|
465
|
-
}
|
|
466
|
-
|
|
467
|
-
function contentRect(cell: DomEl, ids: ById): Rect | null {
|
|
468
|
-
if (cell.getAttribute("vertex") !== "1") return null;
|
|
469
|
-
if (isEdgeLabel(cell)) return explicitEdgeLabelRect(cell, ids);
|
|
470
|
-
if (geometryIsRelative(cell)) return null;
|
|
471
|
-
return absRect(cell, ids);
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
function contentWaypoints(cell: DomEl): Point[] {
|
|
475
|
-
return cell.getAttribute("edge") === "1" ? edgeWaypoints(cell) : [];
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
/** Visible vertices/containers, practical edge-label boxes, and explicit waypoints. */
|
|
479
|
-
function contentBounds(cells: DomEl[], ids: ById): ContentBounds | null {
|
|
480
|
-
let bounds: ContentBounds | null = null;
|
|
481
|
-
for (const cell of cells) {
|
|
482
|
-
if (!isVisible(cell, ids)) continue;
|
|
483
|
-
const box = contentRect(cell, ids);
|
|
484
|
-
if (box && !hasInvalidNumber(box)) bounds = addRectToBounds(bounds, box);
|
|
485
|
-
for (const point of contentWaypoints(cell)) bounds = addPointToBounds(bounds, point);
|
|
486
|
-
}
|
|
487
|
-
return bounds;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
283
|
interface PageSettings {
|
|
491
284
|
enabled: boolean;
|
|
492
285
|
width?: number;
|
package/src/workflow.ts
CHANGED
|
@@ -1,17 +1,71 @@
|
|
|
1
|
+
import type { DrawioInfo } from "./drawio";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
|
-
* Maximum number of automatic source-correction retries across
|
|
3
|
-
*
|
|
4
|
-
* one targeted edit → validation → replacement
|
|
4
|
+
* Maximum number of automatic source-correction retries across the visual
|
|
5
|
+
* review. Pass inspection previews (the first preview of each review pass) do
|
|
6
|
+
* not consume retries; one targeted edit → validation → replacement-preview
|
|
7
|
+
* cycle consumes one.
|
|
5
8
|
*/
|
|
6
|
-
export const AUTOMATIC_CORRECTION_LIMIT =
|
|
9
|
+
export const AUTOMATIC_CORRECTION_LIMIT = 8;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Reference file contents pre-loaded by the `/drawme` command handler so the
|
|
13
|
+
* model starts with everything in context instead of reading files itself.
|
|
14
|
+
* A null entry means the packaged file could not be read; the workflow then
|
|
15
|
+
* falls back to a read-the-file instruction for that topic.
|
|
16
|
+
*/
|
|
17
|
+
export interface WorkflowReferences {
|
|
18
|
+
diagramTypes: string | null;
|
|
19
|
+
xmlAuthoring: string | null;
|
|
20
|
+
/** Only provided when the CLI supports Mermaid conversion (v30+). */
|
|
21
|
+
mermaidAuthoring: string | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function cliStatus(cli: DrawioInfo): string {
|
|
25
|
+
if (!cli.available) {
|
|
26
|
+
return (
|
|
27
|
+
"1. **CLI status (already checked — do not call `drawio_check`)** — the draw.io CLI is NOT available. " +
|
|
28
|
+
"Still author and validate the `.drawio` XML, tell the user how to install draw.io (macOS: `brew install --cask drawio`; other platforms: https://github.com/jgraph/drawio-desktop/releases) or open the source, and skip conversion, layout, previews, visual review, and exports."
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
const features = cli.supportsMermaid
|
|
32
|
+
? "Mermaid conversion and the ELK auto-layout pass are available (v30+)."
|
|
33
|
+
: "This is below v30: hand-author XML; Mermaid conversion and ELK auto-layout are NOT available.";
|
|
34
|
+
return (
|
|
35
|
+
`1. **CLI status (already checked — do not call \`drawio_check\`)** — draw.io ${cli.version ?? "?"} at \`${cli.binary ?? "?"}\`. ` +
|
|
36
|
+
features
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function referenceSection(title: string, path: string, content: string | null): string[] {
|
|
41
|
+
if (content === null) {
|
|
42
|
+
return [`## Reference: ${title}`, "", `Could not be pre-loaded — read \`${path}\` before relying on it.`, ""];
|
|
43
|
+
}
|
|
44
|
+
return [`## Reference: ${title} (pre-loaded from \`${path}\`)`, "", content.trim(), ""];
|
|
45
|
+
}
|
|
7
46
|
|
|
8
47
|
/**
|
|
9
48
|
* Builds the message the `/drawme` command injects to drive the diagram
|
|
10
49
|
* workflow. It steers the model through authoring, deterministic validation,
|
|
11
|
-
*
|
|
50
|
+
* a gated multi-pass visual review, and autonomous final export — once
|
|
51
|
+
* started, the run never pauses for human input. The review runs as fixed
|
|
52
|
+
* passes (fit/sizing → element placement → connections → holistic typography,
|
|
53
|
+
* semantics, and regression), each answering explicit questions on its own
|
|
54
|
+
* preview and each required to be clean before the next starts. Findings go
|
|
55
|
+
* into a numbered ledger naming the affected cells, and every fix must be
|
|
56
|
+
* verified by comparing the replacement preview against the previous image
|
|
57
|
+
* (verdict: fixed / not fixed / regressed) rather than by trusting the XML
|
|
58
|
+
* edit. The CLI check result and the topic references are embedded, so no
|
|
59
|
+
* upfront tool calls or file reads are needed; only the rarely needed
|
|
60
|
+
* troubleshooting reference stays on-demand. The preview tool result states
|
|
61
|
+
* whether the review image is attached, so the review instructions branch on
|
|
62
|
+
* that signal rather than assuming vision.
|
|
12
63
|
*/
|
|
13
|
-
export function drawmeWorkflow(description: string, refDir: string,
|
|
64
|
+
export function drawmeWorkflow(description: string, refDir: string, cli: DrawioInfo, refs: WorkflowReferences): string {
|
|
14
65
|
const quoted = description.replaceAll("\n", "\n> ");
|
|
66
|
+
const mermaidPlanning = refs.mermaidAuthoring !== null || cli.supportsMermaid
|
|
67
|
+
? " If it is a standard type with no custom styling, prefer concise Mermaid converted with `drawio_from_mermaid`; see the Mermaid authoring reference below."
|
|
68
|
+
: "";
|
|
15
69
|
return [
|
|
16
70
|
"# DrawMe — create a diagram",
|
|
17
71
|
"",
|
|
@@ -19,34 +73,37 @@ export function drawmeWorkflow(description: string, refDir: string, agentReferen
|
|
|
19
73
|
"",
|
|
20
74
|
`> ${quoted}`,
|
|
21
75
|
"",
|
|
22
|
-
"
|
|
23
|
-
agentReferencePath +
|
|
24
|
-
"`, the official DrawMe extension reference for agent behavior, tool options, decision rules, and limitations.",
|
|
76
|
+
"Produce a `.drawio` file and export it, following the workflow below. For anything that touches the draw.io CLI, use the DrawMe tools — do NOT shell out to `drawio` yourself. The references at the end of this message are already loaded: do not re-read those files.",
|
|
25
77
|
"",
|
|
26
|
-
|
|
27
|
-
""
|
|
28
|
-
|
|
29
|
-
"
|
|
30
|
-
refDir +
|
|
31
|
-
"/diagram-types.md`. If it is a standard type with no custom styling and the CLI is v30+, prefer concise Mermaid converted with `drawio_from_mermaid`; read `" +
|
|
32
|
-
refDir +
|
|
33
|
-
"/mermaid-authoring.md`.",
|
|
34
|
-
"3. **Author** — for hand-written XML, first read `" +
|
|
35
|
-
refDir +
|
|
36
|
-
"/xml-authoring.md`. Keep XML uncompressed; never reuse ids `0`/`1`; give every edge `<mxGeometry relative=\"1\" as=\"geometry\"/>`; use `drawio_shapesearch` rather than guessing exact vendor/UML/BPMN/cloud styles. For large graphs (>~15 nodes) on v30+, hand-write nodes and use `drawio_layout`. Treat every write, edit, Mermaid conversion, and layout result as a source edit.",
|
|
78
|
+
cliStatus(cli),
|
|
79
|
+
"2. **Plan** — choose the diagram type, shapes, relationships, and layout direction (LR or TB). Plan generous spacing (200–350px between shapes) and do NOT fix page dimensions up front: the canvas is fitted to the finished content later with `drawio_fit_canvas`. For a specific type (ERD, UML class, sequence, C4, architecture, ML/DL, flowchart, SysML, BPMN, network, swimlane), follow the diagram-types reference below." +
|
|
80
|
+
mermaidPlanning,
|
|
81
|
+
"3. **Author** — for hand-written XML, follow the XML authoring reference below. Keep XML uncompressed; never reuse ids `0`/`1`; give every edge `<mxGeometry relative=\"1\" as=\"geometry\"/>`; use `drawio_shapesearch` rather than guessing exact vendor/UML/BPMN/cloud styles. For large graphs (>~15 nodes) on v30+, hand-write nodes and use `drawio_layout`. Space elements generously rather than packing them, then run `drawio_fit_canvas` after authoring — and again after any Mermaid conversion or layout — so the page wraps the content with a comfortable margin. Treat every write, edit, Mermaid conversion, layout, and canvas-fit result as a source edit.",
|
|
37
82
|
"4. **Validate after every source edit** — call `drawio_validate` immediately after authoring and after every later XML edit, Mermaid conversion, or layout operation. Fix all errors before previewing. Every warning must be either fixed or recorded explicitly as reviewed and acceptable with a reason; unresolved or unreviewed warnings block final export.",
|
|
38
|
-
"5. **
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
|
|
45
|
-
"
|
|
46
|
-
|
|
47
|
-
"",
|
|
48
|
-
"
|
|
83
|
+
"5. **Visual review — three focused passes, then a holistic confirmation** — run this once authoring and validation are clean. Export previews with `drawio_export` mode `\"preview\"`; every preview renders a single page, so for a multi-page file, export one preview per page with `pageIndex` and review every page (page-suffixed names, e.g. `<name>.review-p2-fit.png`). The result text states whether the preview image is attached; follow the matching path:",
|
|
84
|
+
" - **Image attached** — run the four passes below in order. Do not start the next pass until every finding of the current one is verified **fixed** in a fresh render or explicitly accepted with a reason. For each pass: export its preview, write a structured critique that hunts for defects — do not summarize or admire the diagram — answer the pass's questions explicitly (a bare \"yes\" is not an answer; point at the evidence in the image), and record findings in one numbered ledger kept for the rest of the run: `F1, F2, …: pass / severity / affected cell ids / what is visibly wrong and where in the image / planned fix` (pick fixes from the defect→fix recipes in the XML authoring reference). The critique's job is to narrow every problem down to specific cells so the fix step can be surgical.",
|
|
85
|
+
" 1. **Pass 1 — fit and sizing** (`<name>.review-fit.png`) answers: does the diagram fit its page, and does the canvas or any element need resizing? Hunt: content touching or crossing a page edge; one outer margin visibly thinner than the others; large empty regions or a page far larger than its content; elements too small for their labels — text touching or crossing its own shape border, clipped or mid-word truncated; a smallest label you cannot comfortably read at full-diagram scale (tighten the canvas with `drawio_fit_canvas`, split content across pages, or simplify the layout — never judge what you cannot read, and never treat \"too small to tell\" as a pass).",
|
|
86
|
+
" 2. **Pass 2 — element placement** (`<name>.review-layout.png`) answers: is every element positioned correctly, and is there a visibly better arrangement? Hunt: shapes whose borders touch or overlap; two labels touching or overlapping each other; label text lying on top of any line or unrelated shape; rows/columns that drift out of alignment; uneven gaps between peers; children poking out of their container or crowding its title; related elements far apart while unrelated ones sit adjacent. Improve, not only repair: if moving an element would shorten edges, remove crossings, or make the arrangement symmetric, record that as a finding too.",
|
|
87
|
+
" 3. **Pass 3 — connections** (`<name>.review-connectors.png`) answers: is every connection correct, cleanly routed, symmetric where the layout is symmetric, and anchored to the middle of the element side it faces wherever possible? Trace every edge from source to target and check it against the plan — right source, right target, right direction, right label. Hunt: an endpoint floating off its shape or plunging deep into it; an edge entering at an odd corner when a side-center anchor is available (pin `exitX`/`exitY`/`entryX`/`entryY` to the facing side's center, spreading evenly only when several edges share one side); mirrored branches routed asymmetrically; an arrowhead hidden by a shape or crushed into a bend; an edge passing through a shape it does not connect; parallel edges stacked on top of each other; an edge label sitting on its line so the text is struck through, or closer than about half a text-height to any line; crossings a reroute through a free corridor would remove.",
|
|
88
|
+
" 4. **Pass 4 — typography, semantics, and regression** (`<name>.review.png`) is the holistic confirmation. Read every label word for word — a label you cannot fully read is itself a finding; check text contrast against fills; confirm every requested component and relationship is present, pointing the right way, with the right label; check hierarchy, grouping, and that legend entries match the colors actually used; apply the type's `Review:` checklist from the diagram-types reference; and re-check passes 1–3 for regressions. Retain this image (every page, for a multi-page file) for the final report.",
|
|
89
|
+
" - **Image NOT attached** — the current model cannot view images. Skip the visual passes: do not describe or judge renders; review structurally instead by cross-checking `drawio_validate` findings and a `drawio_explain` read-back against the user's request. Keep the latest `<name>.review.png` on disk so the user can view it, and state plainly in the summary that rendered inspection was skipped.",
|
|
90
|
+
"6. **Fix and verify before the next pass** — resolve a pass's findings with targeted source edits: after each edit, validate, then re-export the same pass preview (for a multi-page file, the page the finding is on). The superseded preview file is deleted from disk, but its image is still visible earlier in this conversation: compare the new image against it at each addressed finding's location and record a verdict — **fixed** (the defect is gone in the new render), **not fixed** (still visible), or **regressed** (the edit introduced a new defect; log it as a new numbered finding). Never mark a finding fixed because the XML changed — only because the new image shows it fixed. When elements overlap, are cramped, clipped, or short on margin, never shrink or squeeze elements to fit the page — move them apart, then run `drawio_fit_canvas` to enlarge the canvas around them. A pass is complete only when all of its findings are fixed or explicitly accepted, and every pass must be complete before final export. If the correction limit is reached mid-pass, finish the remaining passes as inspection-only and report their findings as unresolved. Each successful preview export automatically removes the previous preview file for the same source, so only the latest review image remains.",
|
|
91
|
+
`7. **Correction limit** — pass inspection previews (the first preview of each pass) do not count against the limit. Across the whole review, allow at most ${AUTOMATIC_CORRECTION_LIMIT} correction retries; one retry is one targeted source-edit → \`drawio_validate\` → replacement-preview cycle, whatever its verdict. Count retries explicitly. If the limit is reached with a finding unresolved, stop automatic editing, keep the best validated state, and continue to final export — name the unresolved finding, its pass, and its last verdict plainly in the final report instead of looping or waiting for input.`,
|
|
92
|
+
"8. **No human gate** — the run is fully autonomous: once started, never pause to ask for approval, confirmation, or feedback. When pass 4 is clean (or the correction limit was reached), proceed directly to final export. If the user interrupts with feedback mid-run, append it to the ledger as new numbered findings: targeted edit, validate, before/after confirmation on the relevant pass preview, a fresh pass-4 holistic preview, then continue to export.",
|
|
93
|
+
"9. **Final export** — immediately after pass 4 is clean (or the correction limit was reached), call `drawio_export` with `mode: \"final\"` for each requested format (default PNG; SVG/PDF/JPG on request). A final export also renders a single page: for a multi-page file, export each page with `pageIndex` and a distinct `output` per page and format. The first successful final export removes the one remaining latest preview for the same source; do not delete previews yourself or report it as a retained output. Report the `.drawio` source and every final path, retained reviewed warnings, any unresolved finding from the correction limit, and cleanup warnings. Offer `drawio_open` for fine-tuning afterwards.",
|
|
94
|
+
"",
|
|
95
|
+
"Failure handling: several DrawMe tools report failures as a text result beginning with `Export failed`, `Layout failed`, `Mermaid conversion failed`, `Fit canvas failed`, `Explain failed`, or `Open failed` rather than a tool error — always read the result text and treat those as failures. If export fails, read `" +
|
|
49
96
|
refDir +
|
|
50
|
-
"/troubleshooting.md
|
|
97
|
+
"/troubleshooting.md` (the one reference kept on-demand). On draw.io below v30, use hand-authored XML instead of Mermaid conversion or ELK layout, but keep validation, the pass-based review, requested-format handling, and final cleanup unchanged.",
|
|
98
|
+
"",
|
|
99
|
+
"---",
|
|
100
|
+
"",
|
|
101
|
+
"# Packaged references (pre-loaded — do not re-read these files)",
|
|
102
|
+
"",
|
|
103
|
+
...referenceSection("diagram types", `${refDir}/diagram-types.md`, refs.diagramTypes),
|
|
104
|
+
...referenceSection("XML authoring", `${refDir}/xml-authoring.md`, refs.xmlAuthoring),
|
|
105
|
+
...(refs.mermaidAuthoring !== null || cli.supportsMermaid
|
|
106
|
+
? referenceSection("Mermaid authoring", `${refDir}/mermaid-authoring.md`, refs.mermaidAuthoring)
|
|
107
|
+
: []),
|
|
51
108
|
].join("\n");
|
|
52
109
|
}
|