@senad-d/drawme 0.1.3 → 0.1.5

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/src/workflow.ts CHANGED
@@ -1,10 +1,64 @@
1
+ import type { DrawioInfo } from "./drawio";
2
+
3
+ /**
4
+ * Maximum number of automatic source-correction retries across the visual
5
+ * review. The initial critique pass and the final holistic confirmation do not
6
+ * consume retries; one targeted edit → validation → replacement preview cycle
7
+ * consumes one.
8
+ */
9
+ export const AUTOMATIC_CORRECTION_LIMIT = 5;
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
+ }
46
+
1
47
  /**
2
48
  * Builds the message the `/drawme` command injects to drive the diagram
3
- * workflow. It steers the model through the author→validate→preview→self-check→
4
- * final loop while pointing it at the DrawMe tools and bundled references.
49
+ * workflow. It steers the model through authoring, deterministic validation,
50
+ * a critique-first visual review, and autonomous final export — once started,
51
+ * the run never pauses for human input. The CLI check result and the topic
52
+ * references are embedded, so no upfront tool calls or file reads are needed;
53
+ * only the rarely needed troubleshooting reference stays on-demand. The
54
+ * preview tool result states whether the review image is attached, so the
55
+ * review instructions branch on that signal rather than assuming vision.
5
56
  */
6
- export function drawmeWorkflow(description: string, refDir: string, agentReferencePath: string): string {
57
+ export function drawmeWorkflow(description: string, refDir: string, cli: DrawioInfo, refs: WorkflowReferences): string {
7
58
  const quoted = description.replaceAll("\n", "\n> ");
59
+ const mermaidPlanning = refs.mermaidAuthoring !== null || cli.supportsMermaid
60
+ ? " If it is a standard type with no custom styling, prefer concise Mermaid converted with `drawio_from_mermaid`; see the Mermaid authoring reference below."
61
+ : "";
8
62
  return [
9
63
  "# DrawMe — create a diagram",
10
64
  "",
@@ -12,26 +66,37 @@ export function drawmeWorkflow(description: string, refDir: string, agentReferen
12
66
  "",
13
67
  `> ${quoted}`,
14
68
  "",
15
- "Before starting, read `" +
16
- agentReferencePath +
17
- "`, the official DrawMe extension reference for agent behavior, tool options, decision rules, and limitations.",
69
+ "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.",
18
70
  "",
19
- "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.",
71
+ cliStatus(cli),
72
+ "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." +
73
+ mermaidPlanning,
74
+ "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.",
75
+ "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.",
76
+ "5. **Visual review — critique before editing** — export one holistic preview to `<name>.review.png` with `drawio_export` mode `\"preview\"`. The result text states whether the preview image is attached; follow the matching path:",
77
+ " - **Image attached** — write a structured critique of the render before touching the source. Walk every category in this order and, for each one, either record findings as `category / severity / affected cell ids / fix` or state explicitly why it passes — never conclude the render is fine without walking all four:",
78
+ " 1. **Canvas and composition**: page bounds, outer margins, clipping, excessive empty space, aspect ratio, major alignment, and overall balance.",
79
+ " 2. **Nodes and typography**: shape overlap, label clipping/wrapping, readable font size, contrast, padding, sizing, and alignment. If the 2000px full-diagram preview makes labels unreadable, re-export a focused preview (`width`, `pageIndex`) rather than assuming the typography passes.",
80
+ " 3. **Connectors**: endpoints, arrowheads, stacked lines, crossings, routes through unrelated elements, labels, and routing corridors.",
81
+ " 4. **Semantics**: requested components and relationships, hierarchy, consistency, legends, and grouping.",
82
+ " - **Image NOT attached** — the current model cannot view images. Do not describe or judge renders; review structurally instead: cross-check `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.",
83
+ "6. **Fix and confirm** — resolve critique findings with targeted source edits: after each edit, validate, then re-export a focused preview named for the affected category (`<name>.review-canvas.png`, `<name>.review-nodes.png`, `<name>.review-connectors.png`, or `<name>.review-semantics.png`) and confirm the fix. 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. When no findings remain, re-export the holistic `<name>.review.png`, re-walk all four categories to confirm nothing regressed, and retain that image for the final report. Each successful preview export automatically removes the previous preview file for the same source, so only the latest review image remains.",
84
+ `7. **Correction limit** — the initial critique pass and the final holistic confirmation do not count against the limit. Across the whole automatic review, allow at most ${AUTOMATIC_CORRECTION_LIMIT} correction retries; one retry is one targeted source-edit → \`drawio_validate\` → replacement-preview cycle. 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 and its category plainly in the final report instead of looping or waiting for input.`,
85
+ "8. **No human gate** — the run is fully autonomous: once started, never pause to ask for approval, confirmation, or feedback. When the holistic confirmation is clean (or the correction limit was reached), proceed directly to final export. If the user interrupts with feedback mid-run, treat it as new critique findings: targeted edit, validate, focused confirmation, fresh holistic preview, then continue to export.",
86
+ "9. **Final export** — immediately after the holistic confirmation, call `drawio_export` with `mode: \"final\"` for each requested format (default PNG; SVG/PDF/JPG on request). 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.",
20
87
  "",
21
- "1. **Check** — call `drawio_check` once (note the version: v30+ unlocks Mermaid conversion and the auto-layout pass). If the CLI is unavailable, still author the `.drawio` XML, tell the user how to install draw.io / open the file, and skip the export + self-check steps.",
22
- "2. **Plan** — choose the diagram type, shapes, relationships, and layout direction (LR or TB). For a specific type (ERD, UML class, sequence, C4, architecture, ML/DL, flowchart, SysML, BPMN, network, swimlane), read `" +
88
+ "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 `" +
23
89
  refDir +
24
- "/diagram-types.md`. If it is a standard type with no custom styling AND the CLI is v30+, prefer authoring concise **Mermaid** and converting it with `drawio_from_mermaid` (automatic layout, no hand-placed coordinates) — read `" +
25
- refDir +
26
- "/mermaid-authoring.md`.",
27
- "3. **Author** — for hand-written `.drawio` XML, first read `" +
28
- refDir +
29
- "/xml-authoring.md` (skeleton, cell/edge forms, containers, palette, spacing). Never reuse ids `0`/`1`; every edge `mxCell` needs a `<mxGeometry relative=\"1\" as=\"geometry\"/>` child; keep the XML uncompressed. When you need a specific vendor / UML / BPMN / cloud shape, call `drawio_shapesearch` for the exact `style=` instead of guessing. For large graphs (>~15 nodes) on v30+, hand-write the nodes then run `drawio_layout` rather than hand-placing coordinates. Write the file to the user's working directory unless they gave a path.",
30
- "4. **Validate** — call `drawio_validate` on the file and fix every reported error before exporting.",
31
- "5. **Preview & self-check** — call `drawio_export` with `mode: \"preview\"` (clean, width-capped PNG). Read the PNG with your own vision and fix obvious problems (overlaps, clipped labels, missing/stacked/crossing edges, off-canvas shapes) by editing the XML and re-previewing. Cap at 2 self-check rounds. If an export fails, read `" +
32
- refDir +
33
- "/troubleshooting.md`.",
34
- "6. **Review** — show the PNG to the user, apply targeted XML edits from their feedback, and re-preview until they approve.",
35
- "7. **Final export** — call `drawio_export` with `mode: \"final\"` for each requested format (default PNG; SVG/PDF/JPG on request). The first successful final export automatically removes preview artifacts created for the same source; do not delete them yourself or report them as retained outputs. Report the `.drawio` source path and every final exported file path, and offer to open the source in the desktop app with `drawio_open` for fine-tuning.",
90
+ "/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 critique-first review, requested-format handling, and final cleanup unchanged.",
91
+ "",
92
+ "---",
93
+ "",
94
+ "# Packaged references (pre-loaded — do not re-read these files)",
95
+ "",
96
+ ...referenceSection("diagram types", `${refDir}/diagram-types.md`, refs.diagramTypes),
97
+ ...referenceSection("XML authoring", `${refDir}/xml-authoring.md`, refs.xmlAuthoring),
98
+ ...(refs.mermaidAuthoring !== null || cli.supportsMermaid
99
+ ? referenceSection("Mermaid authoring", `${refDir}/mermaid-authoring.md`, refs.mermaidAuthoring)
100
+ : []),
36
101
  ].join("\n");
37
102
  }