@omnidim-ai/mcp-server 0.15.0 → 0.16.0

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.
@@ -1,29 +1,85 @@
1
- // MCP Apps view (ui:// resource) that draws a workflow draft inside the chat.
1
+ // MCP Apps view (ui:// resource) that shows and edits a workflow draft inside the chat.
2
2
  // The page holds no API key: every read and edit goes back through this
3
3
  // server's own tools via the host, so it runs on the user's existing login.
4
4
  export const WORKFLOW_CANVAS_URI = "ui://omnidim/workflow-canvas";
5
- const CANVAS_TOOLS = new Set(["getWorkflow", "createWorkflow", "editWorkflow"]);
5
+ // Only showWorkflow draws the canvas. Linking get/create/edit posted one canvas per build step.
6
6
  export function toolMeta(name) {
7
- return CANVAS_TOOLS.has(name) ? { ui: { resourceUri: WORKFLOW_CANVAS_URI } } : undefined;
7
+ return name === "showWorkflow" ? { ui: { resourceUri: WORKFLOW_CANVAS_URI } } : undefined;
8
8
  }
9
- // Embedded into the page with toString(), so it must not reference anything outside itself.
9
+ const SHOW_WORKFLOW_DESCRIPTION = `Show a workflow to the user as an interactive canvas in the chat, where they can move, rename, edit and delete nodes. Call it when the user asks to see a workflow, and once after you finish building or changing one. Do not call it before every edit: use \`getWorkflow\` to read the draft.
10
+
11
+ (Tags: Workflows)`;
12
+ // showWorkflow reads the same endpoint as getWorkflow; it exists so the canvas has one trigger.
13
+ export function addShowWorkflow(map) {
14
+ const get = map.get("getWorkflow");
15
+ if (!get)
16
+ return;
17
+ map.set("showWorkflow", {
18
+ ...get,
19
+ name: "showWorkflow",
20
+ description: SHOW_WORKFLOW_DESCRIPTION,
21
+ inputSchema: {
22
+ type: "object",
23
+ properties: { workflow_id: get.inputSchema.properties.workflow_id },
24
+ required: ["workflow_id"],
25
+ },
26
+ executionParameters: get.executionParameters.filter((p) => p.name === "workflow_id"),
27
+ });
28
+ }
29
+ // The three helpers below are embedded into the page with toString(), so they must not
30
+ // reference anything outside themselves.
10
31
  export function readWorkflow(result) {
11
32
  const text = (result.content || []).filter((c) => c.type === "text").map((c) => c.text || "").join("\n");
12
33
  if (result.isError)
13
- return { workflow: null, workflowId: null, error: text };
34
+ return { workflow: null, workflowId: null, issues: [], error: text };
14
35
  const body = text.startsWith("API Response") ? text.slice(text.indexOf("\n") + 1) : text;
15
36
  let data;
16
37
  try {
17
38
  data = JSON.parse(body);
18
39
  }
19
40
  catch {
20
- return { workflow: null, workflowId: null, error: text };
41
+ return { workflow: null, workflowId: null, issues: [], error: text };
21
42
  }
22
43
  const wf = data && (data.workflow || data);
23
44
  const id = wf && (wf.id ?? wf.workflow_id);
24
45
  const workflowId = typeof id === "number" ? id : null;
25
- return { workflow: wf && wf.draft ? wf : null, workflowId, error: null };
46
+ const src = data && data.issues ? data.issues : data;
47
+ const issues = src && Array.isArray(src.errors) ? src.errors : [];
48
+ return { workflow: wf && wf.draft ? wf : null, workflowId, issues, error: null };
26
49
  }
50
+ // The hosted server takes edit fields flat; the npm server takes them under requestBody.
51
+ // Each drops the shape it does not use, and the API ignores the extra key.
52
+ export function editArgs(workflowId, revision, ops) {
53
+ return { workflow_id: workflowId, revision, ops, requestBody: { revision, ops } };
54
+ }
55
+ export function errorText(text) {
56
+ const start = text.indexOf("{");
57
+ if (start >= 0) {
58
+ try {
59
+ const data = JSON.parse(text.slice(start));
60
+ const msg = data.error_description || data.message || data.detail || data.error;
61
+ if (typeof msg === "string" && msg)
62
+ return msg;
63
+ }
64
+ catch { /* not JSON: fall through to the plain text */ }
65
+ }
66
+ return text.length > 160 ? text.slice(0, 157) + "..." : text;
67
+ }
68
+ // Icon shapes copied from lucide (the set the dashboard's workflow editor uses).
69
+ const ICONS = {
70
+ zap: '<path d="M4 14a1 1 0 0 1-.78-1.63l9.9-10.2a.5.5 0 0 1 .86.46l-1.92 6.02A1 1 0 0 0 13 10h7a1 1 0 0 1 .78 1.63l-9.9 10.2a.5.5 0 0 1-.86-.46l1.92-6.02A1 1 0 0 0 11 14z"/>',
71
+ phone: '<path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/>',
72
+ clock: '<circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/>',
73
+ branch: '<line x1="6" x2="6" y1="3" y2="15"/><circle cx="18" cy="6" r="3"/><circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/>',
74
+ mail: '<rect width="20" height="16" x="2" y="4" rx="2"/><path d="m22 7-8.97 5.7a1.94 1.94 0 0 1-2.06 0L2 7"/>',
75
+ globe: '<circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/>',
76
+ braces: '<path d="M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1"/><path d="M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 1-2-2V5a2 2 0 0 0-2-2h-1"/>',
77
+ workflow: '<rect width="8" height="8" x="3" y="3" rx="2"/><path d="M7 11v4a2 2 0 0 0 2 2h4"/><rect width="8" height="8" x="13" y="13" rx="2"/>',
78
+ message: '<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>',
79
+ sparkles: '<path d="M9.937 15.5A2 2 0 0 0 8.5 14.063l-6.135-1.582a.5.5 0 0 1 0-.962L8.5 9.936A2 2 0 0 0 9.937 8.5l1.582-6.135a.5.5 0 0 1 .963 0L14.063 8.5A2 2 0 0 0 15.5 9.937l6.135 1.581a.5.5 0 0 1 0 .964L15.5 14.063a2 2 0 0 0-1.437 1.437l-1.582 6.135a.5.5 0 0 1-.963 0z"/>',
80
+ user: '<path d="M16 21v-2a4 4 0 0 0-4-4H6a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><line x1="19" x2="19" y1="8" y2="14"/><line x1="22" x2="16" y1="11" y2="11"/>',
81
+ note: '<path d="M16 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V8Z"/><path d="M15 3v4a2 2 0 0 0 2 2h4"/>',
82
+ };
27
83
  export const WORKFLOW_CANVAS_HTML = `<!doctype html>
28
84
  <html lang="en">
29
85
  <head>
@@ -31,68 +87,99 @@ export const WORKFLOW_CANVAS_HTML = `<!doctype html>
31
87
  <meta name="viewport" content="width=device-width, initial-scale=1">
32
88
  <title>Workflow canvas</title>
33
89
  <style>
90
+ /* Colors follow the dashboard's workflow editor tokens. */
34
91
  :root {
35
- --bg: #f7f6f2; --panel: #ffffff; --ink: #1d1c19; --muted: #6b675e; --line: #d9d5ca;
36
- --edge: #9a9486; --grid: #e8e4da; --sel: #2f5bd3; --err: #b3261e;
37
- --trigger: #2e7d4f; --logic: #b26a00; --voice: #6a3fb5; --msg: #0f7b74; --mail: #2f5bd3; --other: #5f5b52;
92
+ --bg: hsl(210 30% 96%); --card: hsl(210 30% 99%); --fg: hsl(222 47% 9%); --muted: hsl(215 19% 36%);
93
+ --line: hsl(214 25% 87%); --edge: #94a3b8; --dot: hsl(214 25% 84%); --primary: hsl(172.5 66% 40.4%);
94
+ --primary-soft: hsl(172.5 66% 40.4% / .12); --sel: #fbbf24; --err: #dc2626; --err-soft: hsl(0 84% 60% / .08);
38
95
  color-scheme: light;
39
96
  }
40
97
  :root[data-theme="dark"] {
41
- --bg: #17161a; --panel: #211f25; --ink: #ecebe6; --muted: #a29e94; --line: #3a3741;
42
- --edge: #7d7888; --grid: #24222a; --sel: #8aa8ff; --err: #ff8a80;
43
- --trigger: #6fcf97; --logic: #f2b84b; --voice: #b49cf0; --msg: #5fd3c7; --mail: #8aa8ff; --other: #b0aba1;
98
+ --bg: hsl(0 0% 3.9%); --card: hsl(0 0% 7%); --fg: hsl(0 0% 98%); --muted: hsl(0 0% 64%);
99
+ --line: hsl(0 0% 16%); --edge: #64748b; --dot: hsl(0 0% 14%); --primary: hsl(180 100% 35.1%);
100
+ --primary-soft: hsl(180 100% 35.1% / .16); --sel: #fbbf24; --err: #f87171; --err-soft: hsl(0 91% 71% / .1);
44
101
  color-scheme: dark;
45
102
  }
46
103
  * { box-sizing: border-box; }
47
- body { margin: 0; font: 13px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--ink); background: var(--bg); }
48
- header { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
104
+ body { margin: 0; font: 13px/1.4 ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--fg); background: var(--bg); }
105
+ header { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); flex-wrap: wrap; background: var(--card); }
49
106
  header h1 { font-size: 14px; margin: 0; font-weight: 600; flex: 1 1 200px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
50
107
  .pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); white-space: nowrap; }
51
- button { font: inherit; font-size: 12px; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; cursor: pointer; }
108
+ .pill.bad { color: var(--err); border-color: var(--err); }
109
+ button, input, textarea { font: inherit; font-size: 12px; color: var(--fg); }
110
+ button { background: var(--card); border: 1px solid var(--line); border-radius: 6px; padding: 4px 10px; cursor: pointer; }
52
111
  button:hover { border-color: var(--edge); }
53
- button:focus-visible { outline: 2px solid var(--sel); outline-offset: 1px; }
54
- #stage { overflow: auto; background-color: var(--bg); background-image: radial-gradient(var(--grid) 1px, transparent 1px); background-size: 16px 16px; }
55
- :root[data-mode="fullscreen"] #stage { max-height: none; height: calc(100vh - 160px); }
112
+ button:disabled { opacity: .6; cursor: progress; }
113
+ button.danger { color: var(--err); }
114
+ button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
115
+ #stage { overflow: auto; background-color: var(--bg); background-image: radial-gradient(var(--dot) 1px, transparent 1px); background-size: 16px 16px; }
116
+ :root[data-mode="fullscreen"] #stage { height: calc(100vh - 52px - var(--panel-h, 0px)); }
56
117
  svg { display: block; touch-action: none; }
57
- .node rect.card { fill: var(--panel); stroke: var(--line); stroke-width: 1; }
58
- .node.selected rect.card { stroke: var(--sel); stroke-width: 2; }
118
+ .card { fill: var(--card); stroke: var(--line); stroke-width: 1; }
119
+ .node:hover .card { stroke: var(--primary); }
120
+ .node.selected .card { stroke: var(--sel); stroke-width: 2; }
121
+ .node.invalid .card { stroke: var(--err); stroke-width: 1.5; }
59
122
  .node { cursor: grab; }
60
- .node.dragging { cursor: grabbing; opacity: .85; }
61
- .node .label { font-size: 13px; font-weight: 600; fill: var(--ink); }
62
- .node .type { font-size: 11px; fill: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
63
- .edge { fill: none; stroke: var(--edge); stroke-width: 1.5; }
64
- .handle-label { font-size: 11px; fill: var(--muted); paint-order: stroke; stroke: var(--bg); stroke-width: 4px; }
65
- #detail { padding: 10px 12px; border-top: 1px solid var(--line); min-height: 44px; }
66
- #detail .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
67
- #detail pre { margin: 8px 0 0; padding: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 6px; max-height: 180px; overflow: auto; font-size: 11px; }
123
+ .node.dragging { cursor: grabbing; }
124
+ .icon-box { fill: var(--primary-soft); }
125
+ .icon { fill: none; stroke: var(--primary); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
126
+ .label { font-size: 13px; font-weight: 600; fill: var(--fg); }
127
+ .sub { font-size: 11.5px; fill: var(--muted); }
128
+ .handle { fill: var(--edge); stroke: var(--card); stroke-width: 2; }
129
+ .edge { fill: none; stroke: var(--edge); stroke-width: 2; }
130
+ .chip rect { fill: var(--card); stroke: var(--line); }
131
+ .chip text { font-size: 10px; font-weight: 700; letter-spacing: .04em; fill: var(--muted); text-transform: uppercase; }
132
+ .skel { fill: var(--card); stroke: var(--line); }
133
+ @media (prefers-reduced-motion: no-preference) { .skel { animation: pulse 1.2s ease-in-out infinite; } }
134
+ @keyframes pulse { 50% { opacity: .5; } }
135
+ #panel { padding: 10px 12px; border-top: 1px solid var(--line); background: var(--card); display: grid; gap: 8px; }
136
+ #panel[hidden] { display: none; }
137
+ .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
68
138
  .muted { color: var(--muted); }
69
- #status { font-size: 12px; color: var(--muted); }
70
- #status.error { color: var(--err); }
139
+ #n-label { flex: 1 1 180px; min-width: 0; padding: 4px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
140
+ #n-config { width: 100%; min-height: 120px; max-height: 240px; padding: 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); font: 11px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical; }
141
+ #n-errors { color: var(--err); background: var(--err-soft); border-radius: 6px; padding: 6px 8px; font-size: 12px; }
142
+ #n-errors[hidden] { display: none; }
143
+ #notice { font-size: 12px; color: var(--err); padding: 6px 12px; border-top: 1px solid var(--line); background: var(--err-soft); }
144
+ #notice.info { color: var(--muted); background: var(--card); }
145
+ #notice[hidden] { display: none; }
71
146
  </style>
72
147
  </head>
73
148
  <body>
74
149
  <header>
75
150
  <h1 id="title">Loading workflow</h1>
76
151
  <span class="pill" id="state" hidden></span>
77
- <button id="refresh" type="button">Refresh</button>
152
+ <span class="pill bad" id="issues" hidden></span>
153
+ <button id="refresh" type="button">Reload</button>
78
154
  <button id="full" type="button">Full screen</button>
79
155
  <button id="open" type="button">Open in dashboard</button>
80
156
  </header>
81
157
  <div id="stage"><svg id="canvas" role="img" aria-label="Workflow graph"></svg></div>
82
- <div id="detail">
83
- <div id="status">Waiting for the workflow.</div>
84
- <div id="node" hidden>
85
- <div class="row"><strong id="n-label"></strong><span class="pill" id="n-type"></span><span class="muted" id="n-id"></span>
86
- <button id="ask" type="button">Ask Claude about this node</button></div>
87
- <pre id="n-config"></pre>
158
+ <div id="notice" role="status" hidden></div>
159
+ <div id="panel" hidden>
160
+ <div class="row">
161
+ <input id="n-label" aria-label="Node name">
162
+ <span class="pill" id="n-type"></span>
163
+ <span class="muted" id="n-id"></span>
164
+ </div>
165
+ <div id="n-errors" hidden></div>
166
+ <textarea id="n-config" aria-label="Node settings (JSON)" spellcheck="false"></textarea>
167
+ <div class="row">
168
+ <button id="save" type="button">Save changes</button>
169
+ <button id="delete" type="button" class="danger">Delete node</button>
170
+ <button id="ask" type="button">Ask Claude about this node</button>
88
171
  </div>
89
172
  </div>
90
173
  <script>
91
174
  const readWorkflow = ${readWorkflow.toString()};
92
- const NODE_W = 210, NODE_H = 58, PAD = 40, MIN_SCALE = 0.45;
175
+ const editArgs = ${editArgs.toString()};
176
+ const errorText = ${errorText.toString()};
177
+ const ICONS = ${JSON.stringify(ICONS)};
178
+ // Card size and handles match the dashboard editor: inputs on top, outputs at the bottom.
179
+ const NODE_W = 220, NODE_H = 62, PAD = 40, MIN_SCALE = 0.7, FALLBACK_MS = 800;
93
180
  const SVG_NS = "http://www.w3.org/2000/svg";
94
181
  const $ = (id) => document.getElementById(id);
95
- const state = { wf: null, id: null, selected: null, busy: false };
182
+ const state = { wf: null, id: null, issues: [], selected: null, busy: false, dirty: false, dragging: false, redraw: false, form: null };
96
183
 
97
184
  // JSON-RPC over postMessage to the host (MCP Apps protocol 2026-01-26).
98
185
  let nextId = 1;
@@ -117,6 +204,8 @@ window.addEventListener("message", (event) => {
117
204
  if (msg.method === "ui/notifications/tool-input") {
118
205
  const id = msg.params && msg.params.arguments && msg.params.arguments.workflow_id;
119
206
  if (typeof id === "number") state.id = id;
207
+ // Some hosts hand over the result late; load it ourselves instead of sitting empty.
208
+ setTimeout(() => { if (!state.wf) refresh(); }, FALLBACK_MS);
120
209
  } else if (msg.method === "ui/notifications/tool-result") {
121
210
  applyResult(msg.params);
122
211
  } else if (msg.method === "ui/notifications/host-context-changed") {
@@ -134,42 +223,89 @@ function applyContext(ctx) {
134
223
  if (ctx.displayMode) document.documentElement.dataset.mode = ctx.displayMode;
135
224
  }
136
225
 
137
- function setStatus(text, isError) {
138
- $("status").textContent = text;
139
- $("status").className = isError ? "error" : "";
226
+ // The bar under the canvas stays hidden unless something needs the user's attention.
227
+ function notice(text, kind) {
228
+ const el = $("notice");
229
+ el.hidden = !text;
230
+ el.textContent = text || "";
231
+ el.className = kind === "info" ? "info" : "";
140
232
  }
141
233
 
142
- async function callTool(name, args) {
143
- return request("tools/call", { name, arguments: args });
144
- }
234
+ function callTool(name, args) { return request("tools/call", { name, arguments: args }); }
145
235
 
146
236
  async function refresh() {
147
237
  if (state.id == null || state.busy) return;
148
- state.busy = true; setStatus("Loading the latest draft.");
149
- try { applyResult(await callTool("getWorkflow", { workflow_id: state.id })); }
150
- catch (e) { setStatus("Could not load the workflow: " + e.message, true); }
238
+ state.busy = true;
239
+ try {
240
+ const r = readWorkflow(await callTool("getWorkflow", { workflow_id: state.id }));
241
+ if (r.error) notice("Could not load the workflow: " + errorText(r.error));
242
+ else if (r.workflow) show(r.workflow, null);
243
+ } catch (e) { notice("Could not load the workflow: " + e.message); }
151
244
  finally { state.busy = false; }
152
245
  }
153
246
 
154
247
  function applyResult(result) {
155
248
  const r = readWorkflow(result || {});
156
- if (r.error) { setStatus(r.error, true); return; }
249
+ if (r.error) { notice(errorText(r.error)); return; }
157
250
  if (r.workflowId != null) state.id = r.workflowId;
158
251
  if (!r.workflow) { refresh(); return; }
159
- state.wf = r.workflow;
252
+ show(r.workflow, r.issues);
253
+ }
254
+
255
+ function show(wf, issues) {
256
+ state.wf = wf;
257
+ if (issues) state.issues = issues;
258
+ notice("");
160
259
  render();
161
- setStatus("Drag a node to move it. Select a node to see its settings.");
260
+ validate();
261
+ }
262
+
263
+ // getWorkflow carries no problems, so ask validateWorkflow which nodes to mark, like the dashboard does.
264
+ async function validate() {
265
+ try {
266
+ const r = readWorkflow(await callTool("validateWorkflow", { workflow_id: state.id }));
267
+ if (!r.error) { state.issues = r.issues; render(); }
268
+ } catch { /* the marks are a hint; the canvas works without them */ }
162
269
  }
163
270
 
164
- function accent(type) {
165
- if (type === "trigger" || type.endsWith("_trigger")) return "var(--trigger)";
166
- if (type.endsWith("_condition") || type === "switch" || type === "delay" || type === "code") return "var(--logic)";
167
- if (type.startsWith("voice")) return "var(--voice)";
168
- if (type.includes("whatsapp") || type.includes("sms")) return "var(--msg)";
169
- if (type.includes("email")) return "var(--mail)";
170
- return "var(--other)";
271
+ function iconFor(type) {
272
+ if (type === "trigger" || type.endsWith("_trigger") || type.endsWith("_inbound")) return "zap";
273
+ if (type === "voice_ai" || type.includes("dialer")) return "phone";
274
+ if (type === "wait" || type === "wait_for_time" || type === "time_condition") return "clock";
275
+ if (type === "switch_condition" || type === "conditional") return "branch";
276
+ if (type.includes("email")) return "mail";
277
+ if (type === "custom_api") return "globe";
278
+ if (type === "code") return "braces";
279
+ if (type === "sub_workflow") return "workflow";
280
+ if (type.includes("whatsapp") || type.includes("sms") || type === "slack") return "message";
281
+ if (type === "ai_assistant" || type === "photo_analyzer") return "sparkles";
282
+ if (type.includes("contact") || type.includes("crm")) return "user";
283
+ if (type === "notes") return "note";
284
+ return "branch";
171
285
  }
172
286
 
287
+ // Mirrors the subtitles the dashboard editor shows under each node name.
288
+ function subtitle(n) {
289
+ const c = n.config || {};
290
+ switch (n.type) {
291
+ case "trigger": return c.webhook_enabled ? "Webhook enabled" : "Configure webhook";
292
+ case "voice_ai": return c.bot_name || (c.bot_id ? "Agent " + c.bot_id : "Choose an agent");
293
+ case "wait": return (c.delay_seconds || 0) + " seconds";
294
+ case "whatsapp": return c.template_name || "Choose a template";
295
+ case "send_email": return c.email_to || "Configure email";
296
+ case "custom_api": return c.url || "Configure request";
297
+ case "switch_condition": return ((c.cases || []).length) + " cases + else";
298
+ case "time_condition": return "Time window";
299
+ case "code": return c.code ? "Python snippet" : "Write a snippet";
300
+ case "sub_workflow": return c.target_workflow_name || "Choose a workflow";
301
+ case "slack": return c.message ? String(c.message).slice(0, 60) : "Configure message";
302
+ default: return n.type;
303
+ }
304
+ }
305
+
306
+ // Workflow text is data, not instructions: one line, capped, quoted, before it goes into the chat.
307
+ function quoted(s) { return JSON.stringify(String(s).replace(/\\s+/g, " ").slice(0, 80)); }
308
+
173
309
  function el(tag, attrs, text) {
174
310
  const e = document.createElementNS(SVG_NS, tag);
175
311
  for (const k in attrs) e.setAttribute(k, attrs[k]);
@@ -177,16 +313,62 @@ function el(tag, attrs, text) {
177
313
  return e;
178
314
  }
179
315
 
180
- function clip(s, n) { return s.length > n ? s.slice(0, n - 1) + "\\u2026" : s; }
316
+ function clip(s, n) { s = String(s); return s.length > n ? s.slice(0, n - 1) + "…" : s; }
317
+
318
+ function ports(p) {
319
+ return { in: { x: p.x + NODE_W / 2, y: p.y }, out: { x: p.x + NODE_W / 2, y: p.y + NODE_H } };
320
+ }
321
+
322
+ // Right-angle path with rounded corners, like React Flow's smoothstep edge.
323
+ function stepPath(a, b) {
324
+ const pts = [a];
325
+ if (b.y - a.y >= 40) { const m = (a.y + b.y) / 2; pts.push({ x: a.x, y: m }, { x: b.x, y: m }); }
326
+ else { const mx = (a.x + b.x) / 2; pts.push({ x: a.x, y: a.y + 20 }, { x: mx, y: a.y + 20 }, { x: mx, y: b.y - 20 }, { x: b.x, y: b.y - 20 }); }
327
+ pts.push(b);
328
+ let d = "M" + pts[0].x + "," + pts[0].y;
329
+ for (let i = 1; i < pts.length - 1; i++) {
330
+ const p = pts[i], prev = pts[i - 1], next = pts[i + 1];
331
+ const r = Math.min(6, Math.hypot(p.x - prev.x, p.y - prev.y) / 2, Math.hypot(next.x - p.x, next.y - p.y) / 2);
332
+ const ux = Math.sign(p.x - prev.x), uy = Math.sign(p.y - prev.y), vx = Math.sign(next.x - p.x), vy = Math.sign(next.y - p.y);
333
+ d += " L" + (p.x - ux * r) + "," + (p.y - uy * r) + " Q" + p.x + "," + p.y + " " + (p.x + vx * r) + "," + (p.y + vy * r);
334
+ }
335
+ d += " L" + b.x + "," + b.y;
336
+ const mid = pts[Math.floor(pts.length / 2)], mid2 = pts[Math.floor(pts.length / 2) - 1];
337
+ return { d, label: { x: (mid.x + mid2.x) / 2, y: (mid.y + mid2.y) / 2 } };
338
+ }
339
+
340
+ function drawEdges(layer) {
341
+ layer.replaceChildren();
342
+ const pos = {};
343
+ for (const n of state.wf.draft.nodes) pos[n.id] = n.position;
344
+ for (const e of state.wf.draft.edges) {
345
+ const a = pos[e.from], b = pos[e.to];
346
+ if (!a || !b) continue;
347
+ const s = stepPath(ports(a).out, ports(b).in);
348
+ layer.append(el("path", { class: "edge", d: s.d, "marker-end": "url(#arrow)" }));
349
+ if (e.handle && e.handle !== "default") {
350
+ const text = clip(e.handle, 18), w = text.length * 6.4 + 14;
351
+ const chip = el("g", { class: "chip", transform: "translate(" + (s.label.x - w / 2) + "," + (s.label.y - 9) + ")" });
352
+ chip.append(el("rect", { width: w, height: 18, rx: 5 }), el("text", { x: 7, y: 12.5 }, text));
353
+ layer.append(chip);
354
+ }
355
+ }
356
+ }
357
+
358
+ function errorsFor(id) { return state.issues.filter((i) => i.node === id); }
181
359
 
182
360
  function render() {
361
+ if (!state.wf) return;
362
+ // Redrawing replaces the node under the pointer and kills the drag, so wait for the drop.
363
+ if (state.dragging) { state.redraw = true; return; }
183
364
  const wf = state.wf, draft = wf.draft, svg = $("canvas");
184
365
  $("title").textContent = wf.name || "Workflow " + wf.id;
185
366
  $("state").hidden = false;
186
- $("state").textContent = (wf.status || "draft") + " \\u00b7 " + draft.nodes.length + " nodes";
187
- const pos = {};
188
- draft.nodes.forEach((n, i) => { pos[n.id] = n.position || { x: i * 260, y: 0 }; });
189
- const xs = Object.values(pos).map((p) => p.x), ys = Object.values(pos).map((p) => p.y);
367
+ $("state").textContent = (wf.status || "draft") + " · " + draft.nodes.length + " nodes";
368
+ $("issues").hidden = !state.issues.length;
369
+ $("issues").textContent = state.issues.length + (state.issues.length === 1 ? " problem" : " problems");
370
+ draft.nodes.forEach((n, i) => { if (!n.position) n.position = { x: i * 260, y: 0 }; });
371
+ const xs = draft.nodes.map((n) => n.position.x), ys = draft.nodes.map((n) => n.position.y);
190
372
  const minX = Math.min(...xs, 0) - PAD, minY = Math.min(...ys, 0) - PAD;
191
373
  const w = Math.max(...xs, 0) + NODE_W + PAD - minX, h = Math.max(...ys, 0) + NODE_H + PAD - minY;
192
374
  const avail = $("stage").clientWidth || 600;
@@ -195,35 +377,40 @@ function render() {
195
377
  svg.setAttribute("width", Math.round(w * scale));
196
378
  svg.setAttribute("height", Math.round(h * scale));
197
379
  svg.replaceChildren();
198
-
199
- const edges = el("g", {});
200
- for (const e of draft.edges) {
201
- const a = pos[e.from], b = pos[e.to];
202
- if (!a || !b) continue;
203
- const x1 = a.x + NODE_W, y1 = a.y + NODE_H / 2, x2 = b.x, y2 = b.y + NODE_H / 2, dx = Math.max(40, (x2 - x1) / 2);
204
- edges.append(el("path", { class: "edge", d: "M" + x1 + "," + y1 + " C" + (x1 + dx) + "," + y1 + " " + (x2 - dx) + "," + y2 + " " + x2 + "," + y2, "marker-end": "url(#arrow)" }));
205
- if (e.handle && e.handle !== "default") {
206
- edges.append(el("text", { class: "handle-label", x: x2 - 8, y: y2 - 8, "text-anchor": "end" }, e.handle));
207
- }
208
- }
209
380
  const defs = el("defs", {});
210
- const marker = el("marker", { id: "arrow", viewBox: "0 0 10 10", refX: "9", refY: "5", markerWidth: "7", markerHeight: "7", orient: "auto-start-reverse" });
381
+ const marker = el("marker", { id: "arrow", viewBox: "0 0 10 10", refX: "8", refY: "5", markerWidth: "6", markerHeight: "6", orient: "auto-start-reverse" });
211
382
  marker.append(el("path", { d: "M0,0 L10,5 L0,10 z", fill: "var(--edge)" }));
212
383
  defs.append(marker);
384
+ const edges = el("g", {});
213
385
  svg.append(defs, edges);
386
+ drawEdges(edges);
214
387
 
215
388
  for (const n of draft.nodes) {
216
- const p = pos[n.id];
217
- const g = el("g", { class: "node" + (state.selected === n.id ? " selected" : ""), transform: "translate(" + p.x + "," + p.y + ")", tabindex: "0", role: "button", "aria-label": (n.label || n.id) + ", " + n.type });
389
+ const bad = errorsFor(n.id).length > 0;
390
+ const cls = "node" + (state.selected === n.id ? " selected" : "") + (bad ? " invalid" : "");
391
+ const g = el("g", { class: cls, transform: "translate(" + n.position.x + "," + n.position.y + ")", tabindex: "0", role: "button", "aria-label": (n.label || n.id) + ", " + n.type + (bad ? ", has problems" : "") });
218
392
  g.append(el("rect", { class: "card", width: NODE_W, height: NODE_H, rx: 8 }));
219
- g.append(el("rect", { width: 4, height: NODE_H - 16, x: 0, y: 8, rx: 2, fill: accent(n.type) }));
220
- g.append(el("text", { class: "label", x: 14, y: 24 }, clip(n.label || n.id, 26)));
221
- g.append(el("text", { class: "type", x: 14, y: 43 }, clip(n.type, 30)));
222
- g.addEventListener("pointerdown", (ev) => startDrag(ev, n, g, p));
393
+ g.append(el("rect", { class: "icon-box", x: 10, y: 10, width: 24, height: 24, rx: 6 }));
394
+ const icon = el("g", { class: "icon", transform: "translate(15,15) scale(0.5833)" });
395
+ icon.innerHTML = ICONS[iconFor(n.type)];
396
+ g.append(icon);
397
+ g.append(el("text", { class: "label", x: 42, y: 26.5 }, clip(n.label || n.id, 23)));
398
+ g.append(el("text", { class: "sub", x: 12, y: NODE_H - 12 }, clip(subtitle(n), 31)));
399
+ const pt = ports({ x: 0, y: 0 });
400
+ if (!n.type.endsWith("trigger") && !n.type.endsWith("_inbound")) g.append(el("circle", { class: "handle", cx: pt.in.x, cy: pt.in.y, r: 4 }));
401
+ g.append(el("circle", { class: "handle", cx: pt.out.x, cy: pt.out.y, r: 4 }));
402
+ g.addEventListener("pointerdown", (ev) => startDrag(ev, n, g, edges));
223
403
  g.addEventListener("keydown", (ev) => { if (ev.key === "Enter" || ev.key === " ") { ev.preventDefault(); select(n.id); } });
224
404
  svg.append(g);
225
405
  }
226
- if (state.selected) showNode(draft.nodes.find((n) => n.id === state.selected));
406
+ showPanel();
407
+ }
408
+
409
+ function placeholder() {
410
+ const svg = $("canvas");
411
+ svg.setAttribute("viewBox", "0 0 600 240"); svg.setAttribute("width", 600); svg.setAttribute("height", 240);
412
+ svg.replaceChildren();
413
+ [[40, 90], [320, 30], [320, 150]].forEach(([x, y]) => svg.append(el("rect", { class: "skel", x, y, width: 220, height: 62, rx: 8 })));
227
414
  }
228
415
 
229
416
  function toSvg(ev) {
@@ -232,67 +419,137 @@ function toSvg(ev) {
232
419
  return pt.matrixTransform(svg.getScreenCTM().inverse());
233
420
  }
234
421
 
235
- function startDrag(ev, node, g, p) {
236
- if (state.busy) return;
237
- const start = toSvg(ev), origin = { x: p.x, y: p.y };
422
+ // The drag is tracked on the whole document, not the node: a release outside the chat's
423
+ // frame never reaches the frame, so leaving the frame (or a move with no button held) drops the node there.
424
+ function startDrag(ev, node, g, edges) {
425
+ // One drag at a time, primary pointer only: a second finger must not start a drag nobody saves.
426
+ if (state.busy || state.dragging || ev.button !== 0 || !ev.isPrimary) return;
427
+ const start = toSvg(ev), origin = { x: node.position.x, y: node.position.y };
238
428
  let moved = false;
239
- g.setPointerCapture(ev.pointerId);
429
+ state.dragging = true;
240
430
  const move = (e) => {
431
+ if (e.buttons === 0) return up();
241
432
  const cur = toSvg(e), dx = cur.x - start.x, dy = cur.y - start.y;
242
433
  if (!moved && Math.hypot(dx, dy) < 4) return;
243
434
  moved = true; g.classList.add("dragging");
244
- p.x = Math.round(origin.x + dx); p.y = Math.round(origin.y + dy);
245
- g.setAttribute("transform", "translate(" + p.x + "," + p.y + ")");
435
+ node.position = { x: Math.round(origin.x + dx), y: Math.round(origin.y + dy) };
436
+ g.setAttribute("transform", "translate(" + node.position.x + "," + node.position.y + ")");
437
+ drawEdges(edges);
246
438
  };
247
439
  const up = () => {
248
- g.removeEventListener("pointermove", move);
249
- g.removeEventListener("pointerup", up);
440
+ document.removeEventListener("pointermove", move);
441
+ document.removeEventListener("pointerup", up);
442
+ document.documentElement.removeEventListener("pointerleave", up);
443
+ window.removeEventListener("blur", up);
250
444
  g.classList.remove("dragging");
445
+ if (!state.dragging) return;
446
+ state.dragging = false;
447
+ if (state.redraw) { state.redraw = false; render(); }
251
448
  if (!moved) { select(node.id); return; }
252
- saveMove(node, { x: p.x, y: p.y }, origin);
449
+ if (node.position.x === origin.x && node.position.y === origin.y) return;
450
+ save([{ op: "move_node", id: node.id, position: node.position }], "moved node " + quoted(node.id), () => { node.position = origin; });
253
451
  };
254
- g.addEventListener("pointermove", move);
255
- g.addEventListener("pointerup", up);
452
+ document.addEventListener("pointermove", move);
453
+ document.addEventListener("pointerup", up);
454
+ document.documentElement.addEventListener("pointerleave", up);
455
+ window.addEventListener("blur", up);
256
456
  }
257
457
 
258
- async function saveMove(node, position, origin) {
259
- state.busy = true; setStatus("Saving the new position.");
458
+ // Every edit sends the draft revision, so a stale edit is refused instead of overwriting Claude's.
459
+ async function save(ops, what, rollback, button) {
460
+ if (!state.wf) return false;
461
+ if (state.busy) { if (rollback) rollback(); render(); notice("Still loading the workflow. Try again in a moment."); return false; }
462
+ state.busy = true;
463
+ if (button) { button.disabled = true; button.dataset.text = button.textContent; button.textContent = "Saving…"; }
464
+ let ok = false;
260
465
  try {
261
- const res = await callTool("editWorkflow", { workflow_id: state.id, requestBody: { revision: state.wf.draft.revision, ops: [{ op: "move_node", id: node.id, position }] } });
262
- const r = readWorkflow(res || {});
263
- if (r.error) throw new Error(r.error);
264
- node.position = position;
265
- notify("ui/update-model-context", { content: [{ type: "text", text: "The user moved node \\"" + node.id + "\\" on workflow " + state.id + " in the canvas. Call getWorkflow for the new revision before your next edit." }] });
466
+ const r = readWorkflow(await callTool("editWorkflow", editArgs(state.id, state.wf.draft.revision, ops)) || {});
467
+ if (r.error) throw new Error(errorText(r.error));
468
+ ok = true;
469
+ notify("ui/update-model-context", { content: [{ type: "text", text: "In the workflow canvas the user " + what + " on workflow " + state.id + ". Call getWorkflow for the new revision before your next edit." }] });
266
470
  } catch (e) {
267
- node.position = origin;
268
- setStatus("Not saved: " + e.message, true);
269
- state.busy = false; render(); return;
471
+ if (rollback) rollback();
472
+ notice("Not saved: " + e.message);
473
+ } finally {
474
+ state.busy = false;
475
+ if (button) { button.disabled = false; button.textContent = button.dataset.text; }
270
476
  }
271
- state.busy = false;
477
+ const failure = ok ? "" : $("notice").textContent;
272
478
  await refresh();
479
+ if (!ok) notice(failure);
480
+ return ok;
273
481
  }
274
482
 
275
483
  function select(id) {
276
484
  state.selected = id;
485
+ state.dirty = false;
277
486
  render();
278
487
  const n = state.wf.draft.nodes.find((x) => x.id === id);
279
- notify("ui/update-model-context", { content: [{ type: "text", text: "In the workflow canvas the user selected node \\"" + id + "\\" (" + n.type + ") on workflow " + state.id + "." }] });
488
+ if (!n) return;
489
+ notify("ui/update-model-context", { content: [{ type: "text", text: "In the workflow canvas the user selected node " + quoted(id) + " (type " + quoted(n.type) + ") on workflow " + state.id + "." }] });
280
490
  }
281
491
 
282
- function showNode(n) {
283
- if (!n) { $("node").hidden = true; return; }
284
- $("node").hidden = false;
285
- $("n-label").textContent = n.label || n.id;
492
+ function showPanel() {
493
+ const n = state.selected && state.wf.draft.nodes.find((x) => x.id === state.selected);
494
+ $("panel").hidden = !n;
495
+ if (!n) return;
496
+ if (state.dirty && $("panel").dataset.node === n.id) return;
497
+ $("panel").dataset.node = n.id;
498
+ state.dirty = false;
499
+ // What the form was loaded with: the base for working out what the user changed.
500
+ state.form = { id: n.id, label: n.label || "", config: JSON.parse(JSON.stringify(n.config || {})) };
501
+ $("n-label").value = n.label || "";
286
502
  $("n-type").textContent = n.type;
287
503
  $("n-id").textContent = "id " + n.id;
288
- $("n-config").textContent = n.config ? JSON.stringify(n.config, null, 2) : "Settings are not loaded in this view.";
504
+ $("n-config").value = n.config ? JSON.stringify(n.config, null, 2) : "{}";
505
+ const errs = errorsFor(n.id);
506
+ $("n-errors").hidden = !errs.length;
507
+ $("n-errors").textContent = errs.map((e) => e.message).join(" ");
508
+ $("delete").textContent = "Delete node";
509
+ document.documentElement.style.setProperty("--panel-h", $("panel").offsetHeight + "px");
289
510
  }
290
511
 
512
+ // Typed but unsaved settings survive the refresh that follows a move or another save.
513
+ for (const id of ["n-label", "n-config"]) $(id).addEventListener("input", () => { state.dirty = true; });
514
+
515
+ // Sends only what the user changed since the form loaded. If Claude changed the same field in the
516
+ // meantime the save is refused, so neither edit silently overwrites the other.
517
+ $("save").addEventListener("click", () => {
518
+ const n = state.wf && state.wf.draft.nodes.find((x) => x.id === state.selected);
519
+ const base = state.form;
520
+ if (!n || !base || base.id !== n.id) return;
521
+ let config;
522
+ try { config = JSON.parse($("n-config").value || "{}"); }
523
+ catch { notice("The settings are not valid JSON. Fix them and save again."); return; }
524
+ if (!config || typeof config !== "object" || Array.isArray(config)) { notice('The settings must be a JSON object, like {"key": "value"}.'); return; }
525
+ const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
526
+ const now = n.config || {}, op = { op: "update_node", id: n.id }, changed = Object.create(null), clash = [];
527
+ for (const k of Object.keys(config)) if (k !== "__proto__" && !same(config[k], base.config[k])) changed[k] = config[k];
528
+ for (const k of Object.keys(base.config)) if (!(k in config)) changed[k] = null;
529
+ for (const k of Object.keys(changed)) if (!same(now[k], base.config[k])) clash.push(k);
530
+ const label = $("n-label").value.trim();
531
+ if (label && label !== base.label) {
532
+ if ((n.label || "") !== base.label) clash.push("name");
533
+ op.label = label;
534
+ }
535
+ if (clash.length) { notice("Claude changed " + clash.join(", ") + " on this node while you were editing. Select the node again to load its version, then redo your change."); return; }
536
+ if (Object.keys(changed).length) op.config = changed;
537
+ if (!op.label && !op.config) { notice("Nothing changed.", "info"); return; }
538
+ save([op], "edited node " + quoted(n.id), null, $("save")).then((ok) => { if (ok) { state.dirty = false; showPanel(); } });
539
+ });
540
+
541
+ $("delete").addEventListener("click", () => {
542
+ const btn = $("delete"), id = state.selected;
543
+ if (!id) return;
544
+ if (btn.textContent !== "Click again to delete") { btn.textContent = "Click again to delete"; return; }
545
+ save([{ op: "delete_node", id }], "deleted node " + quoted(id), null, btn).then((ok) => { if (ok) { state.selected = null; render(); } });
546
+ });
547
+
291
548
  $("refresh").addEventListener("click", refresh);
292
549
  $("full").addEventListener("click", async () => {
293
550
  const mode = document.documentElement.dataset.mode === "fullscreen" ? "inline" : "fullscreen";
294
551
  try { const r = await request("ui/request-display-mode", { mode }); applyContext({ displayMode: r && r.mode }); render(); }
295
- catch (e) { setStatus("This app cannot change size: " + e.message, true); }
552
+ catch (e) { notice("Full screen is not available here."); }
296
553
  });
297
554
  $("open").addEventListener("click", () => {
298
555
  if (state.id != null) request("ui/open-link", { url: "https://omnidim.io/workflow/" + state.id }).catch(() => {});
@@ -300,19 +557,20 @@ $("open").addEventListener("click", () => {
300
557
  $("ask").addEventListener("click", () => {
301
558
  const n = state.wf && state.wf.draft.nodes.find((x) => x.id === state.selected);
302
559
  if (!n) return;
303
- request("ui/message", { role: "user", content: [{ type: "text", text: "In workflow " + state.id + ", explain the \\"" + (n.label || n.id) + "\\" node (id " + n.id + ", " + n.type + ") and check its settings for problems." }] }).catch((e) => setStatus(e.message, true));
560
+ request("ui/message", { role: "user", content: [{ type: "text", text: "In workflow " + state.id + ", explain the node with id " + quoted(n.id) + " (type " + quoted(n.type) + ", named " + quoted(n.label || n.id) + ") and check its settings for problems." }] }).catch(() => notice("Could not send the message to the chat."));
304
561
  });
305
562
 
306
563
  new ResizeObserver(() => notify("ui/notifications/size-changed", { width: document.documentElement.scrollWidth, height: document.documentElement.scrollHeight })).observe(document.body);
307
564
 
565
+ placeholder();
308
566
  request("ui/initialize", {
309
567
  protocolVersion: "2026-01-26",
310
- appInfo: { name: "omnidim-workflow-canvas", version: "0.1.0" },
568
+ appInfo: { name: "omnidim-workflow-canvas", version: "0.2.0" },
311
569
  appCapabilities: { availableDisplayModes: ["inline", "fullscreen"] },
312
570
  }).then((r) => {
313
571
  applyContext(r && r.hostContext);
314
572
  notify("ui/notifications/initialized", {});
315
- }).catch((e) => setStatus("The host did not accept the view: " + e.message, true));
573
+ }).catch(() => notice("This chat app could not open the canvas."));
316
574
  </script>
317
575
  </body>
318
576
  </html>