@foresthubai/workflow-builder 0.4.2 → 0.4.4

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 (123) hide show
  1. package/LICENSE +661 -661
  2. package/NOTICE +16 -16
  3. package/README.md +110 -110
  4. package/dist/components/ui/command.d.ts +2 -2
  5. package/dist/components/ui/input.d.ts +1 -1
  6. package/dist/components/ui/resizable.d.ts +1 -1
  7. package/dist/components/ui/textarea.d.ts +1 -1
  8. package/dist/graph/BaseNode.js +10 -10
  9. package/dist/graph/reactFlowRegistry.d.ts.map +1 -1
  10. package/dist/hooks/useWorkflowSerialization.d.ts.map +1 -1
  11. package/dist/hooks/useWorkflowSerialization.js +8 -11
  12. package/dist/hooks/useWorkflowSerialization.js.map +1 -1
  13. package/package.json +2 -2
  14. package/src/BuilderLayout.tsx +345 -345
  15. package/src/Canvas.tsx +261 -261
  16. package/src/CanvasEditor.tsx +142 -142
  17. package/src/CanvasTabsToolbar.tsx +176 -176
  18. package/src/RightConfigPanel.tsx +266 -266
  19. package/src/WorkflowBuilder.tsx +408 -408
  20. package/src/cn.ts +6 -6
  21. package/src/components/ui/add-button.tsx +39 -39
  22. package/src/components/ui/alert-dialog.tsx +141 -141
  23. package/src/components/ui/alert.tsx +59 -59
  24. package/src/components/ui/badge.tsx +36 -36
  25. package/src/components/ui/button.tsx +85 -85
  26. package/src/components/ui/card.tsx +79 -79
  27. package/src/components/ui/checkbox.tsx +28 -28
  28. package/src/components/ui/collapsible.tsx +9 -9
  29. package/src/components/ui/command.tsx +153 -153
  30. package/src/components/ui/delete-button.tsx +23 -23
  31. package/src/components/ui/dialog.tsx +125 -125
  32. package/src/components/ui/dropdown-menu.tsx +198 -198
  33. package/src/components/ui/input.tsx +55 -55
  34. package/src/components/ui/label.tsx +24 -24
  35. package/src/components/ui/readonly-banner.tsx +15 -15
  36. package/src/components/ui/resizable.tsx +43 -43
  37. package/src/components/ui/scroll-area.tsx +102 -102
  38. package/src/components/ui/select.tsx +160 -160
  39. package/src/components/ui/separator.tsx +29 -29
  40. package/src/components/ui/switch.tsx +27 -27
  41. package/src/components/ui/textarea.tsx +51 -51
  42. package/src/components/ui/toast.tsx +127 -127
  43. package/src/components/ui/toaster.tsx +33 -33
  44. package/src/components/ui/toggle-group.tsx +59 -59
  45. package/src/components/ui/toggle.tsx +43 -43
  46. package/src/components/ui/tooltip.tsx +32 -32
  47. package/src/dialogs/NodePickerDialog.tsx +84 -84
  48. package/src/dialogs/ValidationDialog.tsx +184 -184
  49. package/src/graph/BaseNode.tsx +557 -557
  50. package/src/graph/CustomEdge.tsx +185 -185
  51. package/src/graph/CustomNode.tsx +16 -16
  52. package/src/graph/FunctionCallNode.tsx +30 -30
  53. package/src/graph/PortHandle.tsx +189 -189
  54. package/src/graph/reactFlowRegistry.ts +26 -26
  55. package/src/hooks/use-toast.ts +125 -125
  56. package/src/hooks/useAvailableVariables.ts +20 -20
  57. package/src/hooks/useCanvasHistory.ts +22 -22
  58. package/src/hooks/useCanvasTabs.ts +168 -168
  59. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  60. package/src/hooks/useFunctionRegistry.ts +26 -26
  61. package/src/hooks/useFunctions.ts +44 -44
  62. package/src/hooks/useGraph.ts +161 -161
  63. package/src/hooks/useNodeDefinitions.ts +82 -82
  64. package/src/hooks/useParamErrors.ts +26 -26
  65. package/src/hooks/useResolvedTheme.ts +30 -30
  66. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  67. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  68. package/src/hooks/useWorkflowSerialization.ts +141 -144
  69. package/src/i18n/index.ts +53 -53
  70. package/src/i18n/locales/de.json +501 -501
  71. package/src/i18n/locales/en.json +557 -557
  72. package/src/index.ts +27 -27
  73. package/src/inputs/ExpressionInput.tsx +297 -297
  74. package/src/inputs/ParameterEditor.tsx +515 -515
  75. package/src/inputs/PortSection.tsx +144 -144
  76. package/src/panels/BuilderSidebar.tsx +301 -301
  77. package/src/panels/ChannelConfigPanel.tsx +49 -49
  78. package/src/panels/ChannelsPanel.tsx +28 -28
  79. package/src/panels/DebugConsolePanel.tsx +73 -73
  80. package/src/panels/DebugContextPanel.tsx +77 -77
  81. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  82. package/src/panels/DiagnosticsPanel.tsx +170 -170
  83. package/src/panels/EdgeConfigPanel.tsx +104 -104
  84. package/src/panels/FunctionConfigPanel.tsx +179 -179
  85. package/src/panels/FunctionListPanel.tsx +45 -45
  86. package/src/panels/MemoryConfigPanel.tsx +55 -55
  87. package/src/panels/MemoryPanel.tsx +40 -40
  88. package/src/panels/ModelConfigPanel.tsx +41 -41
  89. package/src/panels/ModelsPanel.tsx +36 -36
  90. package/src/panels/NodeConfigPanel.tsx +630 -630
  91. package/src/panels/NodeLibrary.tsx +288 -288
  92. package/src/panels/ResourceConfigPanel.tsx +132 -132
  93. package/src/panels/ResourceListPanel.tsx +113 -113
  94. package/src/panels/VariableConfigPanel.tsx +161 -161
  95. package/src/panels/VariablesPanel.tsx +145 -145
  96. package/src/stores/canvasStore.test.ts +44 -44
  97. package/src/stores/canvasStore.ts +245 -245
  98. package/src/stores/debugStore.ts +74 -74
  99. package/src/stores/diagnosticsStore.ts +130 -130
  100. package/src/stores/editorStore.ts +190 -190
  101. package/src/styles/index.css +526 -526
  102. package/src/utils/categoryConstants.ts +26 -26
  103. package/src/utils/channelOperations.ts +86 -86
  104. package/src/utils/connectionRules.ts +137 -137
  105. package/src/utils/functionOperations.ts +179 -179
  106. package/src/utils/graphOperations.ts +550 -550
  107. package/src/utils/history.ts +207 -207
  108. package/src/utils/memoryOperations.ts +57 -57
  109. package/src/utils/migrateFunctionNodes.ts +107 -107
  110. package/src/utils/modelOperations.ts +55 -55
  111. package/src/utils/paramDisplay.ts +71 -71
  112. package/src/utils/resourceHelpers.ts +32 -32
  113. package/src/utils/translation.ts +28 -28
  114. package/src/utils/variableOperations.ts +75 -75
  115. package/tailwind-preset.ts +166 -166
  116. package/dist/lib/utils.d.ts +0 -3
  117. package/dist/lib/utils.d.ts.map +0 -1
  118. package/dist/lib/utils.js +0 -6
  119. package/dist/lib/utils.js.map +0 -1
  120. package/dist/toolbars/CanvasTabsToolbar.d.ts +0 -11
  121. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +0 -1
  122. package/dist/toolbars/CanvasTabsToolbar.js +0 -101
  123. package/dist/toolbars/CanvasTabsToolbar.js.map +0 -1
package/src/Canvas.tsx CHANGED
@@ -1,261 +1,261 @@
1
- import {
2
- applyEdgeChanges,
3
- applyNodeChanges,
4
- Background,
5
- BackgroundVariant,
6
- Connection,
7
- Controls,
8
- EdgeChange,
9
- MiniMap,
10
- Node,
11
- NodeChange,
12
- OnSelectionChangeFunc,
13
- ReactFlow,
14
- ReactFlowProvider,
15
- SelectionMode,
16
- useNodesInitialized,
17
- useReactFlow,
18
- } from "@xyflow/react";
19
- import React, { useCallback, useEffect, useRef, useState } from "react";
20
- import { useResolvedTheme } from "./hooks/useResolvedTheme";
21
-
22
- import { NodeCategory, NodeDefinition, NodeData } from "@foresthubai/workflow-core/node";
23
- import { isValidConnection as validateConnection } from "./utils/connectionRules";
24
- import { getOrCreateCanvasStore } from "./stores/canvasStore";
25
- import { useEditorStore } from "./stores/editorStore";
26
- import { nodeTypes, edgeTypes } from "./graph/reactFlowRegistry";
27
- import { isReadOnly } from "./WorkflowBuilder";
28
-
29
- interface CanvasProps {
30
- canvasId: string;
31
- onConnect: (connection: Connection) => void;
32
- onSelectionChange: OnSelectionChangeFunc;
33
- onSelectionStart: () => void;
34
- onSelectionStop: () => void;
35
- onPaneClick: (event: React.MouseEvent) => void;
36
- onAddNode: (nodeType: NodeDefinition, position?: { x: number; y: number }) => void;
37
- onNodeDragStart: (event: React.MouseEvent, node: Node<NodeData>) => void;
38
- viewportCenterRef: React.MutableRefObject<(() => { x: number; y: number }) | null>;
39
- }
40
-
41
- // Inner component that uses useReactFlow - must be inside ReactFlowProvider
42
- const CanvasArea = ({
43
- canvasId,
44
- onConnect,
45
- onSelectionChange,
46
- onSelectionStart,
47
- onSelectionStop,
48
- onPaneClick,
49
- onAddNode,
50
- onNodeDragStart,
51
- viewportCenterRef,
52
- }: CanvasProps) => {
53
- const readOnly = useEditorStore((s) => isReadOnly(s.builderMode));
54
- const resolvedTheme = useResolvedTheme();
55
- const { screenToFlowPosition, getViewport, fitView } = useReactFlow();
56
- const nodesInitialized = useNodesInitialized();
57
-
58
- // Expose viewport center calculation to parent via ref so new nodes can be dropped there.
59
- useEffect(() => {
60
- viewportCenterRef.current = () => {
61
- const container = document.querySelector(".react-flow");
62
- if (!container) return { x: 250, y: 100 };
63
- const { width, height } = container.getBoundingClientRect();
64
- const { x, y, zoom } = getViewport();
65
- // Offset by approximate half-node size so the node appears centered, not top-left aligned
66
- return {
67
- x: (-x + width / 2) / zoom - 90,
68
- y: (-y + height / 2) / zoom - 50,
69
- };
70
- };
71
- }, [getViewport, viewportCenterRef]);
72
-
73
- // Get the independent store for this canvas - direct top-level access
74
- const useStore = getOrCreateCanvasStore(canvasId);
75
- const nodes = useStore((s) => s.nodes);
76
- const edges = useStore((s) => s.edges);
77
- const setNodes = useStore((s) => s.setNodes);
78
- const setEdges = useStore((s) => s.setEdges);
79
- const setViewport = useStore((s) => s.setViewport);
80
-
81
- // This component remounts per canvas (key={canvasId} in BuilderLayout), so ReactFlow
82
- // starts cold on every tab switch. Read the stored viewport ONCE at mount, non-reactively
83
- // (a reactive read would re-render on every pan). Present → restore via defaultViewport so
84
- // the FIRST painted frame is already correct. Absent (first visit) → fit once nodes are
85
- // measured (effect below).
86
- const initialViewport = useRef(useStore.getState().viewport).current;
87
-
88
- // A first visit has nothing to restore, so the fit must run post-mount and would otherwise
89
- // paint the default {0,0,1} frame first and jump to the fit on the next frame. Keep the
90
- // canvas hidden until that fit lands, then reveal the already-correct frame. A restored
91
- // viewport is right on frame 1, so it starts visible — only the first-visit fit is gated.
92
- const [ready, setReady] = useState(initialViewport != null);
93
-
94
- useEffect(() => {
95
- if (ready || initialViewport != null) return;
96
- // An empty canvas has nothing to fit — and useNodesInitialized stays false forever with
97
- // zero nodes — so reveal at the default viewport instead of staying hidden (which would
98
- // blank the canvas: no background, can't drop nodes in).
99
- if (nodes.length === 0) {
100
- setReady(true);
101
- return;
102
- }
103
- if (!nodesInitialized) return;
104
- // fitView updates ReactFlow's viewport, but the transform paints ONE FRAME LATER than this
105
- // call (measured). Revealing now would show one frame at the identity viewport before the
106
- // fit lands — the twitch. So fit now, then reveal on the next frame, once the transform has
107
- // painted. Seed the store so re-entering this canvas restores the view instead of refitting.
108
- fitView({ padding: 0.15, maxZoom: 1 });
109
- const raf = requestAnimationFrame(() => {
110
- setViewport(getViewport());
111
- setReady(true);
112
- });
113
- return () => cancelAnimationFrame(raf);
114
- }, [ready, initialViewport, nodes.length, nodesInitialized, fitView, getViewport, setViewport]);
115
-
116
- // Function to determine node color for MiniMap (uses ReactFlow node types, not domain NodeType)
117
- const nodeColor = (node: Node) => {
118
- switch (node.type) {
119
- case NodeCategory.Trigger:
120
- return "hsl(var(--node-trigger))";
121
- case NodeCategory.Tool:
122
- return "hsl(var(--node-tool))";
123
- case NodeCategory.AI:
124
- return "hsl(var(--node-agent))";
125
- default:
126
- return "hsl(var(--muted))";
127
- }
128
- };
129
-
130
- // ReactFlow change handlers - apply changes directly to store
131
- const onNodesChange = useCallback(
132
- (changes: NodeChange<Node<NodeData>>[]) => {
133
- setNodes((nds) => applyNodeChanges(changes, nds));
134
- },
135
- [setNodes],
136
- );
137
-
138
- const onEdgesChange = useCallback(
139
- (changes: EdgeChange[]) => {
140
- setEdges((eds) => applyEdgeChanges(changes, eds) as typeof eds);
141
- },
142
- [setEdges],
143
- );
144
-
145
- const handleDrop = useCallback(
146
- (event: React.DragEvent) => {
147
- event.preventDefault();
148
-
149
- if (!onAddNode || readOnly) return;
150
-
151
- try {
152
- const dragData = JSON.parse(event.dataTransfer.getData("application/json"));
153
- // Convert screen coordinates to flow coordinates (accounting for pan/zoom)
154
- const position = screenToFlowPosition({
155
- x: event.clientX,
156
- y: event.clientY,
157
- });
158
-
159
- onAddNode(dragData.nodeDef, position);
160
- } catch (error) {
161
- console.error("Failed to parse node data:", error);
162
- }
163
- },
164
- [onAddNode, screenToFlowPosition, readOnly],
165
- );
166
-
167
- const handleDragOver = (event: React.DragEvent) => {
168
- if (readOnly) return;
169
- event.preventDefault();
170
- event.dataTransfer.dropEffect = "copy";
171
- };
172
-
173
- // Prevent browser middle-click auto-scroll which causes viewport jumps
174
- const handleMouseDownCapture = (e: React.MouseEvent) => {
175
- if (e.button === 1) {
176
- e.preventDefault();
177
- }
178
- };
179
-
180
- const handleAuxClick = (e: React.MouseEvent) => {
181
- if (e.button === 1) {
182
- e.preventDefault();
183
- }
184
- };
185
-
186
- return (
187
- // Gate the first-visit fit with opacity, NOT visibility/display:
188
- // - visibility:hidden is overridden by ReactFlow, which sets visibility:visible on each
189
- // measured node, so the nodes stay on screen and you still see the fit jump.
190
- // - display:none removes layout boxes, so nodes never measure and the fit can't compute.
191
- // opacity applies to the whole subtree as a group (children can't override it) yet keeps
192
- // layout intact, so measurement/fitView work while it's invisible.
193
- <div className="w-full h-full" style={{ opacity: ready ? 1 : 0 }}>
194
- <ReactFlow
195
- nodes={nodes}
196
- edges={edges}
197
- onNodesChange={onNodesChange}
198
- onEdgesChange={onEdgesChange}
199
- onConnect={onConnect}
200
- isValidConnection={(c) =>
201
- !!validateConnection(c.source, c.target, c.sourceHandle, c.targetHandle, nodes, edges)
202
- }
203
- onPaneClick={onPaneClick}
204
- onNodeDragStart={onNodeDragStart}
205
- onSelectionChange={onSelectionChange}
206
- onSelectionStart={onSelectionStart}
207
- onSelectionEnd={onSelectionStop}
208
- onMoveEnd={(_, vp) => setViewport(vp)}
209
- nodeTypes={nodeTypes}
210
- edgeTypes={edgeTypes}
211
- defaultViewport={initialViewport ?? undefined}
212
- selectionOnDrag={!readOnly}
213
- panOnDrag={[1, 2]}
214
- selectionMode={SelectionMode.Partial}
215
- selectNodesOnDrag={false}
216
- nodesConnectable={!readOnly}
217
- nodesDraggable={!readOnly}
218
- zoomOnScroll={true}
219
- zoomOnPinch={true}
220
- onDrop={handleDrop}
221
- onDragOver={handleDragOver}
222
- deleteKeyCode={null} // Disable delete controlled by react flow itself
223
- onContextMenu={(e) => e.preventDefault()}
224
- onMouseDownCapture={handleMouseDownCapture}
225
- onAuxClick={handleAuxClick}
226
- colorMode={resolvedTheme}
227
- style={{ "--xy-background-color": "hsl(var(--canvas-background))" } as React.CSSProperties}
228
- >
229
- <Background
230
- variant={BackgroundVariant.Dots}
231
- color="hsl(var(--muted-foreground))"
232
- gap={24}
233
- size={1.5}
234
- className="opacity-40"
235
- />
236
- <Controls className="glass-forest-panel !border !shadow-lg [&_button]:glass-forest-button [&_button]:!text-foreground hover:[&_button]:!scale-105" />
237
- <MiniMap
238
- nodeColor={nodeColor}
239
- className="glass-forest-panel !border !shadow-lg"
240
- maskColor="hsl(var(--primary) / 0.15)"
241
- nodeBorderRadius={12}
242
- nodeStrokeWidth={2}
243
- style={{ width: 120, height: 80 }}
244
- />
245
- </ReactFlow>
246
- </div>
247
- );
248
- };
249
-
250
- // Wrapper component that provides ReactFlowProvider context
251
- const Canvas = (props: CanvasProps) => {
252
- return (
253
- <div className="w-full h-full overflow-hidden overscroll-contain">
254
- <ReactFlowProvider>
255
- <CanvasArea {...props} />
256
- </ReactFlowProvider>
257
- </div>
258
- );
259
- };
260
-
261
- export default Canvas;
1
+ import {
2
+ applyEdgeChanges,
3
+ applyNodeChanges,
4
+ Background,
5
+ BackgroundVariant,
6
+ Connection,
7
+ Controls,
8
+ EdgeChange,
9
+ MiniMap,
10
+ Node,
11
+ NodeChange,
12
+ OnSelectionChangeFunc,
13
+ ReactFlow,
14
+ ReactFlowProvider,
15
+ SelectionMode,
16
+ useNodesInitialized,
17
+ useReactFlow,
18
+ } from "@xyflow/react";
19
+ import React, { useCallback, useEffect, useRef, useState } from "react";
20
+ import { useResolvedTheme } from "./hooks/useResolvedTheme";
21
+
22
+ import { NodeCategory, NodeDefinition, NodeData } from "@foresthubai/workflow-core/node";
23
+ import { isValidConnection as validateConnection } from "./utils/connectionRules";
24
+ import { getOrCreateCanvasStore } from "./stores/canvasStore";
25
+ import { useEditorStore } from "./stores/editorStore";
26
+ import { nodeTypes, edgeTypes } from "./graph/reactFlowRegistry";
27
+ import { isReadOnly } from "./WorkflowBuilder";
28
+
29
+ interface CanvasProps {
30
+ canvasId: string;
31
+ onConnect: (connection: Connection) => void;
32
+ onSelectionChange: OnSelectionChangeFunc;
33
+ onSelectionStart: () => void;
34
+ onSelectionStop: () => void;
35
+ onPaneClick: (event: React.MouseEvent) => void;
36
+ onAddNode: (nodeType: NodeDefinition, position?: { x: number; y: number }) => void;
37
+ onNodeDragStart: (event: React.MouseEvent, node: Node<NodeData>) => void;
38
+ viewportCenterRef: React.MutableRefObject<(() => { x: number; y: number }) | null>;
39
+ }
40
+
41
+ // Inner component that uses useReactFlow - must be inside ReactFlowProvider
42
+ const CanvasArea = ({
43
+ canvasId,
44
+ onConnect,
45
+ onSelectionChange,
46
+ onSelectionStart,
47
+ onSelectionStop,
48
+ onPaneClick,
49
+ onAddNode,
50
+ onNodeDragStart,
51
+ viewportCenterRef,
52
+ }: CanvasProps) => {
53
+ const readOnly = useEditorStore((s) => isReadOnly(s.builderMode));
54
+ const resolvedTheme = useResolvedTheme();
55
+ const { screenToFlowPosition, getViewport, fitView } = useReactFlow();
56
+ const nodesInitialized = useNodesInitialized();
57
+
58
+ // Expose viewport center calculation to parent via ref so new nodes can be dropped there.
59
+ useEffect(() => {
60
+ viewportCenterRef.current = () => {
61
+ const container = document.querySelector(".react-flow");
62
+ if (!container) return { x: 250, y: 100 };
63
+ const { width, height } = container.getBoundingClientRect();
64
+ const { x, y, zoom } = getViewport();
65
+ // Offset by approximate half-node size so the node appears centered, not top-left aligned
66
+ return {
67
+ x: (-x + width / 2) / zoom - 90,
68
+ y: (-y + height / 2) / zoom - 50,
69
+ };
70
+ };
71
+ }, [getViewport, viewportCenterRef]);
72
+
73
+ // Get the independent store for this canvas - direct top-level access
74
+ const useStore = getOrCreateCanvasStore(canvasId);
75
+ const nodes = useStore((s) => s.nodes);
76
+ const edges = useStore((s) => s.edges);
77
+ const setNodes = useStore((s) => s.setNodes);
78
+ const setEdges = useStore((s) => s.setEdges);
79
+ const setViewport = useStore((s) => s.setViewport);
80
+
81
+ // This component remounts per canvas (key={canvasId} in BuilderLayout), so ReactFlow
82
+ // starts cold on every tab switch. Read the stored viewport ONCE at mount, non-reactively
83
+ // (a reactive read would re-render on every pan). Present → restore via defaultViewport so
84
+ // the FIRST painted frame is already correct. Absent (first visit) → fit once nodes are
85
+ // measured (effect below).
86
+ const initialViewport = useRef(useStore.getState().viewport).current;
87
+
88
+ // A first visit has nothing to restore, so the fit must run post-mount and would otherwise
89
+ // paint the default {0,0,1} frame first and jump to the fit on the next frame. Keep the
90
+ // canvas hidden until that fit lands, then reveal the already-correct frame. A restored
91
+ // viewport is right on frame 1, so it starts visible — only the first-visit fit is gated.
92
+ const [ready, setReady] = useState(initialViewport != null);
93
+
94
+ useEffect(() => {
95
+ if (ready || initialViewport != null) return;
96
+ // An empty canvas has nothing to fit — and useNodesInitialized stays false forever with
97
+ // zero nodes — so reveal at the default viewport instead of staying hidden (which would
98
+ // blank the canvas: no background, can't drop nodes in).
99
+ if (nodes.length === 0) {
100
+ setReady(true);
101
+ return;
102
+ }
103
+ if (!nodesInitialized) return;
104
+ // fitView updates ReactFlow's viewport, but the transform paints ONE FRAME LATER than this
105
+ // call (measured). Revealing now would show one frame at the identity viewport before the
106
+ // fit lands — the twitch. So fit now, then reveal on the next frame, once the transform has
107
+ // painted. Seed the store so re-entering this canvas restores the view instead of refitting.
108
+ fitView({ padding: 0.15, maxZoom: 1 });
109
+ const raf = requestAnimationFrame(() => {
110
+ setViewport(getViewport());
111
+ setReady(true);
112
+ });
113
+ return () => cancelAnimationFrame(raf);
114
+ }, [ready, initialViewport, nodes.length, nodesInitialized, fitView, getViewport, setViewport]);
115
+
116
+ // Function to determine node color for MiniMap (uses ReactFlow node types, not domain NodeType)
117
+ const nodeColor = (node: Node) => {
118
+ switch (node.type) {
119
+ case NodeCategory.Trigger:
120
+ return "hsl(var(--node-trigger))";
121
+ case NodeCategory.Tool:
122
+ return "hsl(var(--node-tool))";
123
+ case NodeCategory.AI:
124
+ return "hsl(var(--node-agent))";
125
+ default:
126
+ return "hsl(var(--muted))";
127
+ }
128
+ };
129
+
130
+ // ReactFlow change handlers - apply changes directly to store
131
+ const onNodesChange = useCallback(
132
+ (changes: NodeChange<Node<NodeData>>[]) => {
133
+ setNodes((nds) => applyNodeChanges(changes, nds));
134
+ },
135
+ [setNodes],
136
+ );
137
+
138
+ const onEdgesChange = useCallback(
139
+ (changes: EdgeChange[]) => {
140
+ setEdges((eds) => applyEdgeChanges(changes, eds) as typeof eds);
141
+ },
142
+ [setEdges],
143
+ );
144
+
145
+ const handleDrop = useCallback(
146
+ (event: React.DragEvent) => {
147
+ event.preventDefault();
148
+
149
+ if (!onAddNode || readOnly) return;
150
+
151
+ try {
152
+ const dragData = JSON.parse(event.dataTransfer.getData("application/json"));
153
+ // Convert screen coordinates to flow coordinates (accounting for pan/zoom)
154
+ const position = screenToFlowPosition({
155
+ x: event.clientX,
156
+ y: event.clientY,
157
+ });
158
+
159
+ onAddNode(dragData.nodeDef, position);
160
+ } catch (error) {
161
+ console.error("Failed to parse node data:", error);
162
+ }
163
+ },
164
+ [onAddNode, screenToFlowPosition, readOnly],
165
+ );
166
+
167
+ const handleDragOver = (event: React.DragEvent) => {
168
+ if (readOnly) return;
169
+ event.preventDefault();
170
+ event.dataTransfer.dropEffect = "copy";
171
+ };
172
+
173
+ // Prevent browser middle-click auto-scroll which causes viewport jumps
174
+ const handleMouseDownCapture = (e: React.MouseEvent) => {
175
+ if (e.button === 1) {
176
+ e.preventDefault();
177
+ }
178
+ };
179
+
180
+ const handleAuxClick = (e: React.MouseEvent) => {
181
+ if (e.button === 1) {
182
+ e.preventDefault();
183
+ }
184
+ };
185
+
186
+ return (
187
+ // Gate the first-visit fit with opacity, NOT visibility/display:
188
+ // - visibility:hidden is overridden by ReactFlow, which sets visibility:visible on each
189
+ // measured node, so the nodes stay on screen and you still see the fit jump.
190
+ // - display:none removes layout boxes, so nodes never measure and the fit can't compute.
191
+ // opacity applies to the whole subtree as a group (children can't override it) yet keeps
192
+ // layout intact, so measurement/fitView work while it's invisible.
193
+ <div className="w-full h-full" style={{ opacity: ready ? 1 : 0 }}>
194
+ <ReactFlow
195
+ nodes={nodes}
196
+ edges={edges}
197
+ onNodesChange={onNodesChange}
198
+ onEdgesChange={onEdgesChange}
199
+ onConnect={onConnect}
200
+ isValidConnection={(c) =>
201
+ !!validateConnection(c.source, c.target, c.sourceHandle, c.targetHandle, nodes, edges)
202
+ }
203
+ onPaneClick={onPaneClick}
204
+ onNodeDragStart={onNodeDragStart}
205
+ onSelectionChange={onSelectionChange}
206
+ onSelectionStart={onSelectionStart}
207
+ onSelectionEnd={onSelectionStop}
208
+ onMoveEnd={(_, vp) => setViewport(vp)}
209
+ nodeTypes={nodeTypes}
210
+ edgeTypes={edgeTypes}
211
+ defaultViewport={initialViewport ?? undefined}
212
+ selectionOnDrag={!readOnly}
213
+ panOnDrag={[1, 2]}
214
+ selectionMode={SelectionMode.Partial}
215
+ selectNodesOnDrag={false}
216
+ nodesConnectable={!readOnly}
217
+ nodesDraggable={!readOnly}
218
+ zoomOnScroll={true}
219
+ zoomOnPinch={true}
220
+ onDrop={handleDrop}
221
+ onDragOver={handleDragOver}
222
+ deleteKeyCode={null} // Disable delete controlled by react flow itself
223
+ onContextMenu={(e) => e.preventDefault()}
224
+ onMouseDownCapture={handleMouseDownCapture}
225
+ onAuxClick={handleAuxClick}
226
+ colorMode={resolvedTheme}
227
+ style={{ "--xy-background-color": "hsl(var(--canvas-background))" } as React.CSSProperties}
228
+ >
229
+ <Background
230
+ variant={BackgroundVariant.Dots}
231
+ color="hsl(var(--muted-foreground))"
232
+ gap={24}
233
+ size={1.5}
234
+ className="opacity-40"
235
+ />
236
+ <Controls className="glass-forest-panel !border !shadow-lg [&_button]:glass-forest-button [&_button]:!text-foreground hover:[&_button]:!scale-105" />
237
+ <MiniMap
238
+ nodeColor={nodeColor}
239
+ className="glass-forest-panel !border !shadow-lg"
240
+ maskColor="hsl(var(--primary) / 0.15)"
241
+ nodeBorderRadius={12}
242
+ nodeStrokeWidth={2}
243
+ style={{ width: 120, height: 80 }}
244
+ />
245
+ </ReactFlow>
246
+ </div>
247
+ );
248
+ };
249
+
250
+ // Wrapper component that provides ReactFlowProvider context
251
+ const Canvas = (props: CanvasProps) => {
252
+ return (
253
+ <div className="w-full h-full overflow-hidden overscroll-contain">
254
+ <ReactFlowProvider>
255
+ <CanvasArea {...props} />
256
+ </ReactFlowProvider>
257
+ </div>
258
+ );
259
+ };
260
+
261
+ export default Canvas;