@foresthubai/workflow-builder 0.4.3 → 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 (120) 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/package.json +2 -2
  11. package/src/BuilderLayout.tsx +345 -345
  12. package/src/Canvas.tsx +261 -261
  13. package/src/CanvasEditor.tsx +142 -142
  14. package/src/CanvasTabsToolbar.tsx +176 -176
  15. package/src/RightConfigPanel.tsx +266 -266
  16. package/src/WorkflowBuilder.tsx +408 -408
  17. package/src/cn.ts +6 -6
  18. package/src/components/ui/add-button.tsx +39 -39
  19. package/src/components/ui/alert-dialog.tsx +141 -141
  20. package/src/components/ui/alert.tsx +59 -59
  21. package/src/components/ui/badge.tsx +36 -36
  22. package/src/components/ui/button.tsx +85 -85
  23. package/src/components/ui/card.tsx +79 -79
  24. package/src/components/ui/checkbox.tsx +28 -28
  25. package/src/components/ui/collapsible.tsx +9 -9
  26. package/src/components/ui/command.tsx +153 -153
  27. package/src/components/ui/delete-button.tsx +23 -23
  28. package/src/components/ui/dialog.tsx +125 -125
  29. package/src/components/ui/dropdown-menu.tsx +198 -198
  30. package/src/components/ui/input.tsx +55 -55
  31. package/src/components/ui/label.tsx +24 -24
  32. package/src/components/ui/readonly-banner.tsx +15 -15
  33. package/src/components/ui/resizable.tsx +43 -43
  34. package/src/components/ui/scroll-area.tsx +102 -102
  35. package/src/components/ui/select.tsx +160 -160
  36. package/src/components/ui/separator.tsx +29 -29
  37. package/src/components/ui/switch.tsx +27 -27
  38. package/src/components/ui/textarea.tsx +51 -51
  39. package/src/components/ui/toast.tsx +127 -127
  40. package/src/components/ui/toaster.tsx +33 -33
  41. package/src/components/ui/toggle-group.tsx +59 -59
  42. package/src/components/ui/toggle.tsx +43 -43
  43. package/src/components/ui/tooltip.tsx +32 -32
  44. package/src/dialogs/NodePickerDialog.tsx +84 -84
  45. package/src/dialogs/ValidationDialog.tsx +184 -184
  46. package/src/graph/BaseNode.tsx +557 -557
  47. package/src/graph/CustomEdge.tsx +185 -185
  48. package/src/graph/CustomNode.tsx +16 -16
  49. package/src/graph/FunctionCallNode.tsx +30 -30
  50. package/src/graph/PortHandle.tsx +189 -189
  51. package/src/graph/reactFlowRegistry.ts +26 -26
  52. package/src/hooks/use-toast.ts +125 -125
  53. package/src/hooks/useAvailableVariables.ts +20 -20
  54. package/src/hooks/useCanvasHistory.ts +22 -22
  55. package/src/hooks/useCanvasTabs.ts +168 -168
  56. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  57. package/src/hooks/useFunctionRegistry.ts +26 -26
  58. package/src/hooks/useFunctions.ts +44 -44
  59. package/src/hooks/useGraph.ts +161 -161
  60. package/src/hooks/useNodeDefinitions.ts +82 -82
  61. package/src/hooks/useParamErrors.ts +26 -26
  62. package/src/hooks/useResolvedTheme.ts +30 -30
  63. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  64. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  65. package/src/hooks/useWorkflowSerialization.ts +141 -141
  66. package/src/i18n/index.ts +53 -53
  67. package/src/i18n/locales/de.json +501 -501
  68. package/src/i18n/locales/en.json +557 -557
  69. package/src/index.ts +27 -27
  70. package/src/inputs/ExpressionInput.tsx +297 -297
  71. package/src/inputs/ParameterEditor.tsx +515 -515
  72. package/src/inputs/PortSection.tsx +144 -144
  73. package/src/panels/BuilderSidebar.tsx +301 -301
  74. package/src/panels/ChannelConfigPanel.tsx +49 -49
  75. package/src/panels/ChannelsPanel.tsx +28 -28
  76. package/src/panels/DebugConsolePanel.tsx +73 -73
  77. package/src/panels/DebugContextPanel.tsx +77 -77
  78. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  79. package/src/panels/DiagnosticsPanel.tsx +170 -170
  80. package/src/panels/EdgeConfigPanel.tsx +104 -104
  81. package/src/panels/FunctionConfigPanel.tsx +179 -179
  82. package/src/panels/FunctionListPanel.tsx +45 -45
  83. package/src/panels/MemoryConfigPanel.tsx +55 -55
  84. package/src/panels/MemoryPanel.tsx +40 -40
  85. package/src/panels/ModelConfigPanel.tsx +41 -41
  86. package/src/panels/ModelsPanel.tsx +36 -36
  87. package/src/panels/NodeConfigPanel.tsx +630 -630
  88. package/src/panels/NodeLibrary.tsx +288 -288
  89. package/src/panels/ResourceConfigPanel.tsx +132 -132
  90. package/src/panels/ResourceListPanel.tsx +113 -113
  91. package/src/panels/VariableConfigPanel.tsx +161 -161
  92. package/src/panels/VariablesPanel.tsx +145 -145
  93. package/src/stores/canvasStore.test.ts +44 -44
  94. package/src/stores/canvasStore.ts +245 -245
  95. package/src/stores/debugStore.ts +74 -74
  96. package/src/stores/diagnosticsStore.ts +130 -130
  97. package/src/stores/editorStore.ts +190 -190
  98. package/src/styles/index.css +526 -526
  99. package/src/utils/categoryConstants.ts +26 -26
  100. package/src/utils/channelOperations.ts +86 -86
  101. package/src/utils/connectionRules.ts +137 -137
  102. package/src/utils/functionOperations.ts +179 -179
  103. package/src/utils/graphOperations.ts +550 -550
  104. package/src/utils/history.ts +207 -207
  105. package/src/utils/memoryOperations.ts +57 -57
  106. package/src/utils/migrateFunctionNodes.ts +107 -107
  107. package/src/utils/modelOperations.ts +55 -55
  108. package/src/utils/paramDisplay.ts +71 -71
  109. package/src/utils/resourceHelpers.ts +32 -32
  110. package/src/utils/translation.ts +28 -28
  111. package/src/utils/variableOperations.ts +75 -75
  112. package/tailwind-preset.ts +166 -166
  113. package/dist/lib/utils.d.ts +0 -3
  114. package/dist/lib/utils.d.ts.map +0 -1
  115. package/dist/lib/utils.js +0 -6
  116. package/dist/lib/utils.js.map +0 -1
  117. package/dist/toolbars/CanvasTabsToolbar.d.ts +0 -11
  118. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +0 -1
  119. package/dist/toolbars/CanvasTabsToolbar.js +0 -101
  120. package/dist/toolbars/CanvasTabsToolbar.js.map +0 -1
@@ -1,345 +1,345 @@
1
- import type { FunctionDeclaration } from "@foresthubai/workflow-core/function";
2
- import type { NodeDefinition } from "@foresthubai/workflow-core/node";
3
- import { useCallback, useEffect, useRef, useState } from "react";
4
- import type { Connection, OnSelectionChangeFunc } from "@xyflow/react";
5
-
6
- import { toast } from "./hooks/use-toast";
7
- import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "./components/ui/resizable";
8
- import { BuilderSidebar } from "./panels/BuilderSidebar";
9
- import { CanvasTabsToolbar } from "./CanvasTabsToolbar";
10
- import { CanvasEditor } from "./CanvasEditor";
11
- import { RightConfigPanel } from "./RightConfigPanel";
12
- import { useCanvasHistory } from "./hooks/useCanvasHistory";
13
- import { useGraph } from "./hooks/useGraph";
14
- import { useNodeDefinitions } from "./hooks/useNodeDefinitions";
15
- import { DebugConsolePanel } from "./panels/DebugConsolePanel";
16
- import type { CanvasTab } from "./hooks/useCanvasTabs";
17
- import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "./stores/canvasStore";
18
- import { useEditorStore } from "./stores/editorStore";
19
- import { isReadOnly } from "./WorkflowBuilder";
20
-
21
- /**
22
- * Chrome composer. Stable across canvas switches — only the {@link CanvasEditor}
23
- * child remounts via `key={activeCanvasId}`.
24
- *
25
- * Owns:
26
- * - Sidebar tab state and the mode → sidebar-tab auto-switch effect.
27
- * - viewportCenterRef (populated by ReactFlow, consumed by sidebar's
28
- * click-to-add path).
29
- * - Selection-drag flag (lifted here so RightConfigPanel can read it).
30
- * - All graph mutation handlers — bound to active canvas via
31
- * `useGraph(activeCanvasId)`. Handlers stay stable enough to pass to the
32
- * sidebar; closures refresh automatically when the active canvas changes.
33
- * - Document-level keyboard handlers (undo/redo/copy/paste/delete/escape).
34
- *
35
- * Receives the open-tab list and function-CRUD callbacks from
36
- * {@link WorkflowBuilder} above (which owns long-lived editor state).
37
- */
38
- export interface BuilderLayoutProps {
39
- functions: FunctionDeclaration[];
40
- /** Open (and select) an existing function — used by the sidebar list and tab dropdown. */
41
- onOpenFunction: (functionId: string) => void;
42
- /** Create a new function and open it — the sidebar list's "Add" action. */
43
- onCreateFunction: () => string;
44
-
45
- canvasTabs: CanvasTab[];
46
- onCanvasTabChange: (tabId: string) => void;
47
- onCanvasTabClose: (tabId: string) => void;
48
- onCanvasTabReorder: (fromIndex: number, toIndex: number) => void;
49
-
50
- onTestNode?: (nodeId: string) => void;
51
- onDebugStep?: (nodeId?: string) => void;
52
- }
53
-
54
- export const BuilderLayout = ({
55
- functions,
56
- onOpenFunction,
57
- onCreateFunction,
58
- canvasTabs,
59
- onCanvasTabChange,
60
- onCanvasTabClose,
61
- onCanvasTabReorder,
62
- onTestNode,
63
- onDebugStep,
64
- }: BuilderLayoutProps) => {
65
- const activeCanvasId = useEditorStore((s) => s.activeCanvasId);
66
- const builderMode = useEditorStore((s) => s.builderMode);
67
- const readOnly = isReadOnly(builderMode);
68
- const isDebugMode = builderMode.type === "debug";
69
-
70
- // NodeRegistry (static) + dynamic function nodes — derived, not embedder-provided.
71
- const { nodeDefinitions, getNodeDefinition, getAllCategories } = useNodeDefinitions();
72
-
73
- const graph = useGraph(activeCanvasId, readOnly);
74
- const { undo, redo, takeCheckpoint, canUndo, canRedo } = useCanvasHistory(activeCanvasId);
75
-
76
- // Selection (project-wide in editorStore, mirrored to canvas store for RF visual)
77
- const selection = useEditorStore((s) => s.selection);
78
- const selectGraph = useEditorStore((s) => s.selectGraph);
79
- const syncSelectionFromRF = useEditorStore((s) => s.syncSelectionFromRF);
80
- const clearSelection = useEditorStore((s) => s.clearSelection);
81
-
82
- // Sidebar tab state + mode auto-switch.
83
- const activeSidebarTab = useEditorStore((s) => s.activeSidebarTab);
84
- const setActiveSidebarTab = useEditorStore((s) => s.setActiveSidebarTab);
85
- useEffect(() => {
86
- const isDebugTab = activeSidebarTab === "debug-context";
87
- if (isDebugMode && !isDebugTab) {
88
- setActiveSidebarTab("debug-context");
89
- } else if (!isDebugMode && isDebugTab) {
90
- setActiveSidebarTab("nodes");
91
- }
92
- }, [isDebugMode, activeSidebarTab, setActiveSidebarTab]);
93
-
94
- // Selection-drag flag (used by RightConfigPanel to suppress during drag).
95
- const [selectionDrag, setSelectionDrag] = useState(false);
96
-
97
- // ViewportCenter ref (populated by ReactFlow inside CanvasEditor, consumed
98
- // here for sidebar's click-to-add path).
99
- const viewportCenterRef = useRef<(() => { x: number; y: number }) | null>(null);
100
-
101
- // ── Handlers ──────────────────────────────────────────────────────────────
102
-
103
- const selectNodeById: (id: string) => void = useCallback(
104
- (nodeId: string) => selectGraph([nodeId], []),
105
- [selectGraph],
106
- );
107
-
108
- const selectEdgeById = useCallback((edgeId: string) => selectGraph([], [edgeId]), [selectGraph]);
109
-
110
- const handleAddNode = useCallback(
111
- (nodeDef: NodeDefinition, position?: { x: number; y: number }) => {
112
- const pos = position ?? viewportCenterRef.current?.();
113
- const id = graph.addNode(nodeDef, pos);
114
- if (id == null) {
115
- toast({ title: `Only one ${nodeDef.label} node allowed per canvas`, variant: "destructive" });
116
- }
117
- return id;
118
- },
119
- [graph],
120
- );
121
-
122
- const handleConnect = useCallback(
123
- (conn: Connection) => {
124
- const edgeType = graph.onConnect(conn);
125
- // Auto-select agent edges so the config panel opens for parameter entry.
126
- if (edgeType && edgeType !== "control" && edgeType !== "tool") {
127
- const { edges: currentEdges } = getOrCreateCanvasStore(activeCanvasId).getState();
128
- const newEdge = currentEdges.find(
129
- (e) =>
130
- e.source === conn.source &&
131
- e.target === conn.target &&
132
- e.sourceHandle === conn.sourceHandle &&
133
- e.targetHandle === conn.targetHandle,
134
- );
135
- if (newEdge) selectEdgeById(newEdge.id);
136
- }
137
- },
138
- [graph, activeCanvasId, selectEdgeById],
139
- );
140
-
141
- const handleAddNodeAndConnect = useCallback(
142
- (
143
- nodeDef: NodeDefinition,
144
- position: { x: number; y: number },
145
- connection: { source: string; sourceHandle: string; target: string; targetHandle: string },
146
- ) => {
147
- const newNodeId = graph.addNodeAndConnect(nodeDef, position, connection);
148
- if (newNodeId == null) {
149
- toast({ title: `Only one ${nodeDef.label} node allowed per canvas`, variant: "destructive" });
150
- }
151
- return newNodeId;
152
- },
153
- [graph],
154
- );
155
-
156
- const handleSelectionChange: OnSelectionChangeFunc = useCallback(
157
- ({ nodes: selNodes, edges: selEdges }) => {
158
- syncSelectionFromRF(
159
- selNodes.map((n) => n.id),
160
- selEdges.map((e) => e.id),
161
- );
162
- },
163
- [syncSelectionFromRF],
164
- );
165
-
166
- const handleDeleteEdge = useCallback(
167
- (edgeId: string) => {
168
- graph.deleteEdges([edgeId]);
169
- clearSelection();
170
- },
171
- [graph, clearSelection],
172
- );
173
-
174
- const handleNodeDragStart = useCallback(() => {
175
- takeCheckpoint();
176
- }, [takeCheckpoint]);
177
-
178
- const deleteSelected = useCallback(() => {
179
- const sel = useEditorStore.getState().selection;
180
- const nodeIds = sel.kind === "graph" ? sel.nodeIds : [];
181
- const edgeIds = sel.kind === "graph" ? sel.edgeIds : [];
182
- graph.deleteSelected(nodeIds, edgeIds);
183
- clearSelection();
184
- }, [graph, clearSelection]);
185
-
186
- const handlePaste = useCallback(
187
- (offset?: { x: number; y: number }) => {
188
- const result = graph.pasteSelection(offset);
189
- if (result?.skippedLabels.length) {
190
- for (const label of result.skippedLabels) {
191
- toast({ title: `Only one ${label} node allowed per canvas`, variant: "destructive" });
192
- }
193
- }
194
- return result;
195
- },
196
- [graph],
197
- );
198
-
199
- // Debug mode: clicking a node sets the debug cursor.
200
- // useEffect(() => {
201
- // if (!isDebugMode || selectedNodeIds.length !== 1) return;
202
- // const nodeId = selectedNodeIds[0];
203
- // const phase = useDebugStore.getState().phase;
204
- // if (phase.status === "idle") {
205
- // useDebugStore
206
- // .getState()
207
- // .setPhase({ status: "paused", sessionId: phase.sessionId, cursorNodeId: nodeId });
208
- // } else if (phase.status === "paused") {
209
- // useDebugStore.getState().setPhase({ ...phase, cursorNodeId: nodeId });
210
- // }
211
- // }, [isDebugMode, selectedNodeIds]);
212
-
213
- // Keyboard handlers — undo/redo/copy/paste/delete/escape.
214
- useEffect(() => {
215
- const handleKeyDown = (event: KeyboardEvent) => {
216
- const target = event.target as HTMLElement;
217
- if (target?.tagName === "INPUT" || target?.tagName === "TEXTAREA" || target?.isContentEditable) {
218
- return;
219
- }
220
- if (readOnly) {
221
- if (event.key === "Escape") clearSelection();
222
- return;
223
- }
224
- if ((event.ctrlKey || event.metaKey) && event.key === "z" && !event.shiftKey) {
225
- event.preventDefault();
226
- if (canUndo()) {
227
- clearSelection();
228
- undo();
229
- }
230
- return;
231
- }
232
- if ((event.ctrlKey || event.metaKey) && (event.key === "y" || (event.key === "z" && event.shiftKey))) {
233
- event.preventDefault();
234
- if (canRedo()) {
235
- clearSelection();
236
- redo();
237
- }
238
- return;
239
- }
240
- if ((event.ctrlKey || event.metaKey) && event.key === "c") {
241
- if (selection.kind === "graph" && selection.nodeIds.length > 0) {
242
- event.preventDefault();
243
- graph.copySelection(selection.nodeIds);
244
- }
245
- return;
246
- }
247
- if ((event.ctrlKey || event.metaKey) && event.key === "v") {
248
- event.preventDefault();
249
- handlePaste();
250
- return;
251
- }
252
- if (event.key === "Delete" || event.key === "Backspace") {
253
- deleteSelected();
254
- }
255
- if (event.key === "Escape") {
256
- clearSelection();
257
- }
258
- };
259
- document.addEventListener("keydown", handleKeyDown);
260
- return () => document.removeEventListener("keydown", handleKeyDown);
261
- }, [canUndo, canRedo, undo, redo, clearSelection, deleteSelected, selection, graph, handlePaste, readOnly]);
262
-
263
- const isFunctionCanvas = activeCanvasId !== MAIN_CANVAS_ID;
264
-
265
- // ── Render ───────────────────────────────────────────────────────────────
266
-
267
- const canvasArea = (
268
- <div className="flex flex-col h-full min-w-0">
269
- <CanvasTabsToolbar
270
- tabs={canvasTabs}
271
- activeTabId={activeCanvasId}
272
- onTabChange={onCanvasTabChange}
273
- onTabClose={onCanvasTabClose}
274
- onTabReorder={onCanvasTabReorder}
275
- />
276
- <div className="flex-1 relative">
277
- <CanvasEditor
278
- key={activeCanvasId}
279
- canvasId={activeCanvasId}
280
- viewportCenterRef={viewportCenterRef}
281
- nodeDefinitions={nodeDefinitions}
282
- onConnect={handleConnect}
283
- onAddNode={handleAddNode}
284
- onAddNodeAndConnect={handleAddNodeAndConnect}
285
- onSelectionChange={handleSelectionChange}
286
- onPaneClick={clearSelection}
287
- onNodeDragStart={handleNodeDragStart}
288
- setSelectionDrag={setSelectionDrag}
289
- />
290
- </div>
291
- </div>
292
- );
293
-
294
- return (
295
- <div className="h-full bg-canvas-bg flex flex-col">
296
- <div className="flex-1 flex overflow-hidden">
297
- <BuilderSidebar
298
- canvasId={activeCanvasId}
299
- activeTab={activeSidebarTab}
300
- onTabChange={setActiveSidebarTab}
301
- onAddNode={handleAddNode}
302
- nodeDefinitions={nodeDefinitions}
303
- getAllCategories={getAllCategories}
304
- onSelectNode={selectNodeById}
305
- onSelectEdge={selectEdgeById}
306
- isFunctionCanvas={isFunctionCanvas}
307
- functions={functions}
308
- onOpenFunction={onOpenFunction}
309
- onCreateFunction={onCreateFunction}
310
- isDebugMode={isDebugMode}
311
- />
312
-
313
- <div className="flex-1 flex flex-col h-full min-w-0">
314
- {isDebugMode ? (
315
- <ResizablePanelGroup direction="vertical">
316
- <ResizablePanel defaultSize={75} minSize={30}>
317
- {canvasArea}
318
- </ResizablePanel>
319
- <ResizableHandle withHandle />
320
- <ResizablePanel defaultSize={25} minSize={10}>
321
- <DebugConsolePanel />
322
- </ResizablePanel>
323
- </ResizablePanelGroup>
324
- ) : (
325
- canvasArea
326
- )}
327
- </div>
328
-
329
- <RightConfigPanel
330
- canvasId={activeCanvasId}
331
- isDebugMode={isDebugMode}
332
- selectionDrag={selectionDrag}
333
- getNodeDef={getNodeDefinition}
334
- onNodeUpdate={graph.updateNode}
335
- onNodeDelete={graph.deleteNode}
336
- onEdgeUpdate={graph.updateEdge}
337
- onEdgeDelete={handleDeleteEdge}
338
- onClearSelection={clearSelection}
339
- onTestNode={onTestNode}
340
- onDebugStep={onDebugStep}
341
- />
342
- </div>
343
- </div>
344
- );
345
- };
1
+ import type { FunctionDeclaration } from "@foresthubai/workflow-core/function";
2
+ import type { NodeDefinition } from "@foresthubai/workflow-core/node";
3
+ import { useCallback, useEffect, useRef, useState } from "react";
4
+ import type { Connection, OnSelectionChangeFunc } from "@xyflow/react";
5
+
6
+ import { toast } from "./hooks/use-toast";
7
+ import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "./components/ui/resizable";
8
+ import { BuilderSidebar } from "./panels/BuilderSidebar";
9
+ import { CanvasTabsToolbar } from "./CanvasTabsToolbar";
10
+ import { CanvasEditor } from "./CanvasEditor";
11
+ import { RightConfigPanel } from "./RightConfigPanel";
12
+ import { useCanvasHistory } from "./hooks/useCanvasHistory";
13
+ import { useGraph } from "./hooks/useGraph";
14
+ import { useNodeDefinitions } from "./hooks/useNodeDefinitions";
15
+ import { DebugConsolePanel } from "./panels/DebugConsolePanel";
16
+ import type { CanvasTab } from "./hooks/useCanvasTabs";
17
+ import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "./stores/canvasStore";
18
+ import { useEditorStore } from "./stores/editorStore";
19
+ import { isReadOnly } from "./WorkflowBuilder";
20
+
21
+ /**
22
+ * Chrome composer. Stable across canvas switches — only the {@link CanvasEditor}
23
+ * child remounts via `key={activeCanvasId}`.
24
+ *
25
+ * Owns:
26
+ * - Sidebar tab state and the mode → sidebar-tab auto-switch effect.
27
+ * - viewportCenterRef (populated by ReactFlow, consumed by sidebar's
28
+ * click-to-add path).
29
+ * - Selection-drag flag (lifted here so RightConfigPanel can read it).
30
+ * - All graph mutation handlers — bound to active canvas via
31
+ * `useGraph(activeCanvasId)`. Handlers stay stable enough to pass to the
32
+ * sidebar; closures refresh automatically when the active canvas changes.
33
+ * - Document-level keyboard handlers (undo/redo/copy/paste/delete/escape).
34
+ *
35
+ * Receives the open-tab list and function-CRUD callbacks from
36
+ * {@link WorkflowBuilder} above (which owns long-lived editor state).
37
+ */
38
+ export interface BuilderLayoutProps {
39
+ functions: FunctionDeclaration[];
40
+ /** Open (and select) an existing function — used by the sidebar list and tab dropdown. */
41
+ onOpenFunction: (functionId: string) => void;
42
+ /** Create a new function and open it — the sidebar list's "Add" action. */
43
+ onCreateFunction: () => string;
44
+
45
+ canvasTabs: CanvasTab[];
46
+ onCanvasTabChange: (tabId: string) => void;
47
+ onCanvasTabClose: (tabId: string) => void;
48
+ onCanvasTabReorder: (fromIndex: number, toIndex: number) => void;
49
+
50
+ onTestNode?: (nodeId: string) => void;
51
+ onDebugStep?: (nodeId?: string) => void;
52
+ }
53
+
54
+ export const BuilderLayout = ({
55
+ functions,
56
+ onOpenFunction,
57
+ onCreateFunction,
58
+ canvasTabs,
59
+ onCanvasTabChange,
60
+ onCanvasTabClose,
61
+ onCanvasTabReorder,
62
+ onTestNode,
63
+ onDebugStep,
64
+ }: BuilderLayoutProps) => {
65
+ const activeCanvasId = useEditorStore((s) => s.activeCanvasId);
66
+ const builderMode = useEditorStore((s) => s.builderMode);
67
+ const readOnly = isReadOnly(builderMode);
68
+ const isDebugMode = builderMode.type === "debug";
69
+
70
+ // NodeRegistry (static) + dynamic function nodes — derived, not embedder-provided.
71
+ const { nodeDefinitions, getNodeDefinition, getAllCategories } = useNodeDefinitions();
72
+
73
+ const graph = useGraph(activeCanvasId, readOnly);
74
+ const { undo, redo, takeCheckpoint, canUndo, canRedo } = useCanvasHistory(activeCanvasId);
75
+
76
+ // Selection (project-wide in editorStore, mirrored to canvas store for RF visual)
77
+ const selection = useEditorStore((s) => s.selection);
78
+ const selectGraph = useEditorStore((s) => s.selectGraph);
79
+ const syncSelectionFromRF = useEditorStore((s) => s.syncSelectionFromRF);
80
+ const clearSelection = useEditorStore((s) => s.clearSelection);
81
+
82
+ // Sidebar tab state + mode auto-switch.
83
+ const activeSidebarTab = useEditorStore((s) => s.activeSidebarTab);
84
+ const setActiveSidebarTab = useEditorStore((s) => s.setActiveSidebarTab);
85
+ useEffect(() => {
86
+ const isDebugTab = activeSidebarTab === "debug-context";
87
+ if (isDebugMode && !isDebugTab) {
88
+ setActiveSidebarTab("debug-context");
89
+ } else if (!isDebugMode && isDebugTab) {
90
+ setActiveSidebarTab("nodes");
91
+ }
92
+ }, [isDebugMode, activeSidebarTab, setActiveSidebarTab]);
93
+
94
+ // Selection-drag flag (used by RightConfigPanel to suppress during drag).
95
+ const [selectionDrag, setSelectionDrag] = useState(false);
96
+
97
+ // ViewportCenter ref (populated by ReactFlow inside CanvasEditor, consumed
98
+ // here for sidebar's click-to-add path).
99
+ const viewportCenterRef = useRef<(() => { x: number; y: number }) | null>(null);
100
+
101
+ // ── Handlers ──────────────────────────────────────────────────────────────
102
+
103
+ const selectNodeById: (id: string) => void = useCallback(
104
+ (nodeId: string) => selectGraph([nodeId], []),
105
+ [selectGraph],
106
+ );
107
+
108
+ const selectEdgeById = useCallback((edgeId: string) => selectGraph([], [edgeId]), [selectGraph]);
109
+
110
+ const handleAddNode = useCallback(
111
+ (nodeDef: NodeDefinition, position?: { x: number; y: number }) => {
112
+ const pos = position ?? viewportCenterRef.current?.();
113
+ const id = graph.addNode(nodeDef, pos);
114
+ if (id == null) {
115
+ toast({ title: `Only one ${nodeDef.label} node allowed per canvas`, variant: "destructive" });
116
+ }
117
+ return id;
118
+ },
119
+ [graph],
120
+ );
121
+
122
+ const handleConnect = useCallback(
123
+ (conn: Connection) => {
124
+ const edgeType = graph.onConnect(conn);
125
+ // Auto-select agent edges so the config panel opens for parameter entry.
126
+ if (edgeType && edgeType !== "control" && edgeType !== "tool") {
127
+ const { edges: currentEdges } = getOrCreateCanvasStore(activeCanvasId).getState();
128
+ const newEdge = currentEdges.find(
129
+ (e) =>
130
+ e.source === conn.source &&
131
+ e.target === conn.target &&
132
+ e.sourceHandle === conn.sourceHandle &&
133
+ e.targetHandle === conn.targetHandle,
134
+ );
135
+ if (newEdge) selectEdgeById(newEdge.id);
136
+ }
137
+ },
138
+ [graph, activeCanvasId, selectEdgeById],
139
+ );
140
+
141
+ const handleAddNodeAndConnect = useCallback(
142
+ (
143
+ nodeDef: NodeDefinition,
144
+ position: { x: number; y: number },
145
+ connection: { source: string; sourceHandle: string; target: string; targetHandle: string },
146
+ ) => {
147
+ const newNodeId = graph.addNodeAndConnect(nodeDef, position, connection);
148
+ if (newNodeId == null) {
149
+ toast({ title: `Only one ${nodeDef.label} node allowed per canvas`, variant: "destructive" });
150
+ }
151
+ return newNodeId;
152
+ },
153
+ [graph],
154
+ );
155
+
156
+ const handleSelectionChange: OnSelectionChangeFunc = useCallback(
157
+ ({ nodes: selNodes, edges: selEdges }) => {
158
+ syncSelectionFromRF(
159
+ selNodes.map((n) => n.id),
160
+ selEdges.map((e) => e.id),
161
+ );
162
+ },
163
+ [syncSelectionFromRF],
164
+ );
165
+
166
+ const handleDeleteEdge = useCallback(
167
+ (edgeId: string) => {
168
+ graph.deleteEdges([edgeId]);
169
+ clearSelection();
170
+ },
171
+ [graph, clearSelection],
172
+ );
173
+
174
+ const handleNodeDragStart = useCallback(() => {
175
+ takeCheckpoint();
176
+ }, [takeCheckpoint]);
177
+
178
+ const deleteSelected = useCallback(() => {
179
+ const sel = useEditorStore.getState().selection;
180
+ const nodeIds = sel.kind === "graph" ? sel.nodeIds : [];
181
+ const edgeIds = sel.kind === "graph" ? sel.edgeIds : [];
182
+ graph.deleteSelected(nodeIds, edgeIds);
183
+ clearSelection();
184
+ }, [graph, clearSelection]);
185
+
186
+ const handlePaste = useCallback(
187
+ (offset?: { x: number; y: number }) => {
188
+ const result = graph.pasteSelection(offset);
189
+ if (result?.skippedLabels.length) {
190
+ for (const label of result.skippedLabels) {
191
+ toast({ title: `Only one ${label} node allowed per canvas`, variant: "destructive" });
192
+ }
193
+ }
194
+ return result;
195
+ },
196
+ [graph],
197
+ );
198
+
199
+ // Debug mode: clicking a node sets the debug cursor.
200
+ // useEffect(() => {
201
+ // if (!isDebugMode || selectedNodeIds.length !== 1) return;
202
+ // const nodeId = selectedNodeIds[0];
203
+ // const phase = useDebugStore.getState().phase;
204
+ // if (phase.status === "idle") {
205
+ // useDebugStore
206
+ // .getState()
207
+ // .setPhase({ status: "paused", sessionId: phase.sessionId, cursorNodeId: nodeId });
208
+ // } else if (phase.status === "paused") {
209
+ // useDebugStore.getState().setPhase({ ...phase, cursorNodeId: nodeId });
210
+ // }
211
+ // }, [isDebugMode, selectedNodeIds]);
212
+
213
+ // Keyboard handlers — undo/redo/copy/paste/delete/escape.
214
+ useEffect(() => {
215
+ const handleKeyDown = (event: KeyboardEvent) => {
216
+ const target = event.target as HTMLElement;
217
+ if (target?.tagName === "INPUT" || target?.tagName === "TEXTAREA" || target?.isContentEditable) {
218
+ return;
219
+ }
220
+ if (readOnly) {
221
+ if (event.key === "Escape") clearSelection();
222
+ return;
223
+ }
224
+ if ((event.ctrlKey || event.metaKey) && event.key === "z" && !event.shiftKey) {
225
+ event.preventDefault();
226
+ if (canUndo()) {
227
+ clearSelection();
228
+ undo();
229
+ }
230
+ return;
231
+ }
232
+ if ((event.ctrlKey || event.metaKey) && (event.key === "y" || (event.key === "z" && event.shiftKey))) {
233
+ event.preventDefault();
234
+ if (canRedo()) {
235
+ clearSelection();
236
+ redo();
237
+ }
238
+ return;
239
+ }
240
+ if ((event.ctrlKey || event.metaKey) && event.key === "c") {
241
+ if (selection.kind === "graph" && selection.nodeIds.length > 0) {
242
+ event.preventDefault();
243
+ graph.copySelection(selection.nodeIds);
244
+ }
245
+ return;
246
+ }
247
+ if ((event.ctrlKey || event.metaKey) && event.key === "v") {
248
+ event.preventDefault();
249
+ handlePaste();
250
+ return;
251
+ }
252
+ if (event.key === "Delete" || event.key === "Backspace") {
253
+ deleteSelected();
254
+ }
255
+ if (event.key === "Escape") {
256
+ clearSelection();
257
+ }
258
+ };
259
+ document.addEventListener("keydown", handleKeyDown);
260
+ return () => document.removeEventListener("keydown", handleKeyDown);
261
+ }, [canUndo, canRedo, undo, redo, clearSelection, deleteSelected, selection, graph, handlePaste, readOnly]);
262
+
263
+ const isFunctionCanvas = activeCanvasId !== MAIN_CANVAS_ID;
264
+
265
+ // ── Render ───────────────────────────────────────────────────────────────
266
+
267
+ const canvasArea = (
268
+ <div className="flex flex-col h-full min-w-0">
269
+ <CanvasTabsToolbar
270
+ tabs={canvasTabs}
271
+ activeTabId={activeCanvasId}
272
+ onTabChange={onCanvasTabChange}
273
+ onTabClose={onCanvasTabClose}
274
+ onTabReorder={onCanvasTabReorder}
275
+ />
276
+ <div className="flex-1 relative">
277
+ <CanvasEditor
278
+ key={activeCanvasId}
279
+ canvasId={activeCanvasId}
280
+ viewportCenterRef={viewportCenterRef}
281
+ nodeDefinitions={nodeDefinitions}
282
+ onConnect={handleConnect}
283
+ onAddNode={handleAddNode}
284
+ onAddNodeAndConnect={handleAddNodeAndConnect}
285
+ onSelectionChange={handleSelectionChange}
286
+ onPaneClick={clearSelection}
287
+ onNodeDragStart={handleNodeDragStart}
288
+ setSelectionDrag={setSelectionDrag}
289
+ />
290
+ </div>
291
+ </div>
292
+ );
293
+
294
+ return (
295
+ <div className="h-full bg-canvas-bg flex flex-col">
296
+ <div className="flex-1 flex overflow-hidden">
297
+ <BuilderSidebar
298
+ canvasId={activeCanvasId}
299
+ activeTab={activeSidebarTab}
300
+ onTabChange={setActiveSidebarTab}
301
+ onAddNode={handleAddNode}
302
+ nodeDefinitions={nodeDefinitions}
303
+ getAllCategories={getAllCategories}
304
+ onSelectNode={selectNodeById}
305
+ onSelectEdge={selectEdgeById}
306
+ isFunctionCanvas={isFunctionCanvas}
307
+ functions={functions}
308
+ onOpenFunction={onOpenFunction}
309
+ onCreateFunction={onCreateFunction}
310
+ isDebugMode={isDebugMode}
311
+ />
312
+
313
+ <div className="flex-1 flex flex-col h-full min-w-0">
314
+ {isDebugMode ? (
315
+ <ResizablePanelGroup direction="vertical">
316
+ <ResizablePanel defaultSize={75} minSize={30}>
317
+ {canvasArea}
318
+ </ResizablePanel>
319
+ <ResizableHandle withHandle />
320
+ <ResizablePanel defaultSize={25} minSize={10}>
321
+ <DebugConsolePanel />
322
+ </ResizablePanel>
323
+ </ResizablePanelGroup>
324
+ ) : (
325
+ canvasArea
326
+ )}
327
+ </div>
328
+
329
+ <RightConfigPanel
330
+ canvasId={activeCanvasId}
331
+ isDebugMode={isDebugMode}
332
+ selectionDrag={selectionDrag}
333
+ getNodeDef={getNodeDefinition}
334
+ onNodeUpdate={graph.updateNode}
335
+ onNodeDelete={graph.deleteNode}
336
+ onEdgeUpdate={graph.updateEdge}
337
+ onEdgeDelete={handleDeleteEdge}
338
+ onClearSelection={clearSelection}
339
+ onTestNode={onTestNode}
340
+ onDebugStep={onDebugStep}
341
+ />
342
+ </div>
343
+ </div>
344
+ );
345
+ };