@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,44 +1,44 @@
1
- import { afterEach, describe, expect, it } from "vitest";
2
- import {
3
- clearAllCanvasStores,
4
- getAllCanvasStores,
5
- getCanvasStore,
6
- getOrCreateCanvasStore,
7
- subscribeCanvasRegistryChanges,
8
- MAIN_CANVAS_ID,
9
- } from "./canvasStore";
10
-
11
- // Reset the module-level registry between tests (it's a singleton).
12
- afterEach(() => clearAllCanvasStores());
13
-
14
- describe("clearAllCanvasStores", () => {
15
- it("re-seeds an empty main canvas and drops the rest", () => {
16
- const mainBefore = getOrCreateCanvasStore(MAIN_CANVAS_ID);
17
- getOrCreateCanvasStore("fn-1"); // a function canvas
18
- expect(Object.keys(getAllCanvasStores())).toEqual(expect.arrayContaining([MAIN_CANVAS_ID, "fn-1"]));
19
-
20
- clearAllCanvasStores();
21
-
22
- const mainAfter = getCanvasStore(MAIN_CANVAS_ID);
23
- expect(mainAfter).toBeDefined(); // "main always exists" invariant holds
24
- expect(mainAfter).not.toBe(mainBefore); // it's a fresh, empty instance
25
- expect(mainAfter?.getState().nodes).toEqual([]);
26
- expect(getCanvasStore("fn-1")).toBeUndefined(); // function canvas removed
27
- });
28
-
29
- it("notifies registry listeners with main already present", () => {
30
- // Regression guard: the notification must fire AFTER main is re-seeded, not
31
- // while the registry is empty — otherwise subscribers (onChange / history)
32
- // snapshot an empty set and never re-attach to the recreated main canvas, so
33
- // edits after New/clear wouldn't mark the workflow dirty.
34
- let mainPresentAtNotify = false;
35
- const unsubscribe = subscribeCanvasRegistryChanges(() => {
36
- mainPresentAtNotify = MAIN_CANVAS_ID in getAllCanvasStores();
37
- });
38
-
39
- clearAllCanvasStores();
40
- unsubscribe();
41
-
42
- expect(mainPresentAtNotify).toBe(true);
43
- });
44
- });
1
+ import { afterEach, describe, expect, it } from "vitest";
2
+ import {
3
+ clearAllCanvasStores,
4
+ getAllCanvasStores,
5
+ getCanvasStore,
6
+ getOrCreateCanvasStore,
7
+ subscribeCanvasRegistryChanges,
8
+ MAIN_CANVAS_ID,
9
+ } from "./canvasStore";
10
+
11
+ // Reset the module-level registry between tests (it's a singleton).
12
+ afterEach(() => clearAllCanvasStores());
13
+
14
+ describe("clearAllCanvasStores", () => {
15
+ it("re-seeds an empty main canvas and drops the rest", () => {
16
+ const mainBefore = getOrCreateCanvasStore(MAIN_CANVAS_ID);
17
+ getOrCreateCanvasStore("fn-1"); // a function canvas
18
+ expect(Object.keys(getAllCanvasStores())).toEqual(expect.arrayContaining([MAIN_CANVAS_ID, "fn-1"]));
19
+
20
+ clearAllCanvasStores();
21
+
22
+ const mainAfter = getCanvasStore(MAIN_CANVAS_ID);
23
+ expect(mainAfter).toBeDefined(); // "main always exists" invariant holds
24
+ expect(mainAfter).not.toBe(mainBefore); // it's a fresh, empty instance
25
+ expect(mainAfter?.getState().nodes).toEqual([]);
26
+ expect(getCanvasStore("fn-1")).toBeUndefined(); // function canvas removed
27
+ });
28
+
29
+ it("notifies registry listeners with main already present", () => {
30
+ // Regression guard: the notification must fire AFTER main is re-seeded, not
31
+ // while the registry is empty — otherwise subscribers (onChange / history)
32
+ // snapshot an empty set and never re-attach to the recreated main canvas, so
33
+ // edits after New/clear wouldn't mark the workflow dirty.
34
+ let mainPresentAtNotify = false;
35
+ const unsubscribe = subscribeCanvasRegistryChanges(() => {
36
+ mainPresentAtNotify = MAIN_CANVAS_ID in getAllCanvasStores();
37
+ });
38
+
39
+ clearAllCanvasStores();
40
+ unsubscribe();
41
+
42
+ expect(mainPresentAtNotify).toBe(true);
43
+ });
44
+ });
@@ -1,245 +1,245 @@
1
- import { create, UseBoundStore, StoreApi } from "zustand";
2
- import { Node, Edge, Viewport } from "@xyflow/react";
3
- import { NodeCategory, type NodeData } from "@foresthubai/workflow-core/node";
4
- import type { EdgeData } from "@foresthubai/workflow-core/edge";
5
- import { history, History, type HistoryData, type MutationCount } from "../utils/history";
6
- import { generateId } from "@foresthubai/workflow-core/id";
7
- import { fnargKey, type Variable, type ApiVariable } from "@foresthubai/workflow-core/variable";
8
- import { computeVariablesFromNodes } from "@foresthubai/workflow-core/workflow";
9
-
10
- /**
11
- * Sync fnarg:* entries in a canvas's variables to match a function's arguments.
12
- * fnarg variables are *derived* from the (project-scoped) function declaration —
13
- * they are not authored canvas state, so the source of truth is `editorStore`;
14
- * callers pass the declaration's argument list here. Removes stale fnarg entries
15
- * and adds the current ones.
16
- */
17
- export function syncFunctionArgVariables(store: CanvasStore, args: readonly ApiVariable[]): void {
18
- store.getState().setVariables((vars) => {
19
- const updated = { ...vars };
20
- // Remove all existing fnarg entries
21
- for (const key of Object.keys(updated)) {
22
- if (key.startsWith("fnarg:")) delete updated[key];
23
- }
24
- // Add current args
25
- for (const arg of args) {
26
- updated[fnargKey(arg.uid)] = { kind: "fnarg", uid: arg.uid, name: arg.name, dataType: arg.dataType };
27
- }
28
- return updated;
29
- });
30
- }
31
-
32
- export { MAIN_CANVAS_ID } from "@foresthubai/workflow-core/workflow";
33
- import { MAIN_CANVAS_ID } from "@foresthubai/workflow-core/workflow";
34
- const HISTORY_LIMIT = 50 as const;
35
-
36
- // ============================================================================
37
- // Canvas Registry Change Notification System
38
- // ============================================================================
39
-
40
- // Listeners notified when the *set* of canvas stores changes (a store is created,
41
- // deleted, or the whole registry is cleared/retained). This is decoupled from
42
- // function definitions — those now live in editorStore. Its sole consumer is
43
- // WorkflowBuilder, which re-subscribes to every live store's mutationCount/history
44
- // when the set changes so newly created (or dropped) canvases are watched.
45
- const canvasRegistryListeners = new Set<() => void>();
46
-
47
- // Notify subscribers that the canvas store set changed.
48
- export function notifyCanvasRegistryChange(): void {
49
- canvasRegistryListeners.forEach((listener) => listener());
50
- }
51
-
52
- // Subscribe to canvas registry (store set) changes.
53
- export function subscribeCanvasRegistryChanges(listener: () => void): () => void {
54
- canvasRegistryListeners.add(listener);
55
- return () => canvasRegistryListeners.delete(listener);
56
- }
57
-
58
- // ============================================================================
59
- // Canvas Store Registry
60
- // ============================================================================
61
-
62
- // Registry - Module-level map of independent store instances
63
- const canvasStores = new Map<string, CanvasStore>();
64
-
65
- canvasStores.set(MAIN_CANVAS_ID, createCanvasStore()); // Always exists
66
-
67
- export interface CanvasState {
68
- nodes: Node<NodeData>[];
69
- edges: Edge<EdgeData>[];
70
- // Unified variable record: node outputs (nodeId:outputId), declared (declared:uid), fn args (fnarg:uid).
71
- // fnarg:* entries are derived from the project-scoped function declaration (editorStore) via
72
- // syncFunctionArgVariables — they are not authored here.
73
- variables: Record<string, Variable>;
74
- // Session-only pan/zoom of the canvas viewport. View state, NOT workflow content:
75
- // kept out of partialize (no history/serialization) so it never enters the contract,
76
- // and lost on reload like selection. Persisted across tab switches so re-entering a
77
- // canvas restores its view via defaultViewport instead of a post-paint fitView jump.
78
- // null until first visited (then fitView seeds it).
79
- viewport: Viewport | null;
80
-
81
- setNodes: (updater: (nodes: Node<NodeData>[]) => Node<NodeData>[]) => void;
82
- setEdges: (updater: (edges: Edge<EdgeData>[]) => Edge<EdgeData>[]) => void;
83
- setVariables: (updater: (variables: Record<string, Variable>) => Record<string, Variable>) => void;
84
- setViewport: (viewport: Viewport) => void;
85
- /**
86
- * Visual-only: set ReactFlow selected flags on nodes AND edges in one atomic update.
87
- * This will call a single re-render and a single onSelectionChange callback.
88
- * Not an update of domain state, so it can be used in read-only mode.
89
- */
90
- setRFselect: (nodeIds: string[], edgeIds: string[]) => void;
91
- initialize: (nodes: Node<NodeData>[], edges: Edge<EdgeData>[]) => void;
92
- }
93
-
94
- // Canvas store is a Zustand store + history (undo/redo capabilities) + mutation count.
95
- export type CanvasStore = UseBoundStore<StoreApi<CanvasState & MutationCount>> & History;
96
-
97
- function createCanvasStore(): CanvasStore {
98
- // Create base store with history middleware
99
- const baseStore = create(
100
- history<CanvasState>({
101
- limit: HISTORY_LIMIT,
102
- partialize: (state) => ({
103
- nodes: state.nodes,
104
- edges: state.edges,
105
- variables: state.variables,
106
- }),
107
- equality: (before, after) => before.nodes === after.nodes && before.edges === after.edges && before.variables === after.variables,
108
- })((set) => ({
109
- nodes: [],
110
- edges: [],
111
- variables: {},
112
- viewport: null,
113
-
114
- setNodes: (updater) =>
115
- set((state) => {
116
- const next = updater(state.nodes);
117
- if (next === state.nodes) return state;
118
- return { nodes: next };
119
- }),
120
-
121
- setEdges: (updater) =>
122
- set((state) => {
123
- const next = updater(state.edges);
124
- if (next === state.edges) return state;
125
- return { edges: next };
126
- }),
127
-
128
- setVariables: (updater) =>
129
- set((state) => {
130
- const next = updater(state.variables);
131
- if (next === state.variables) return state;
132
- return { variables: next };
133
- }),
134
-
135
- // View-only — outside partialize, so it neither takes a checkpoint nor bumps
136
- // mutationCount (no spurious dirty dot / onChange from panning).
137
- setViewport: (viewport) => set({ viewport }),
138
-
139
- setRFselect: (nodeIds, edgeIds) => {
140
- const nodeIdSet = new Set(nodeIds);
141
- const edgeIdSet = new Set(edgeIds);
142
- set((state) => ({
143
- nodes: state.nodes.map((n) => {
144
- const shouldSelect = nodeIdSet.has(n.id);
145
- return n.selected === shouldSelect ? n : { ...n, selected: shouldSelect };
146
- }),
147
- edges: state.edges.map((e) => {
148
- const shouldSelect = edgeIdSet.has(e.id);
149
- return e.selected === shouldSelect ? e : { ...e, selected: shouldSelect };
150
- }),
151
- }));
152
- },
153
-
154
- initialize: (nodes, edges) => {
155
- // Build node-output variables. fnarg entries (for function canvases) are
156
- // seeded separately via syncFunctionArgVariables from the editorStore
157
- // declaration, since the canvas store no longer owns the signature.
158
- // computeVariablesFromNodes is the core (NodeData[]) variant; peel the
159
- // React Flow wrapper at the call site.
160
- const vars: Record<string, Variable> = computeVariablesFromNodes(nodes.map((n) => n.data));
161
- set({ nodes, edges, variables: vars });
162
- },
163
- })),
164
- );
165
-
166
- // Bind history methods from state to CanvasStore object
167
- // This makes them accessible as store.undo() instead of store.getState().undo()
168
- const store = baseStore as unknown as CanvasStore;
169
-
170
- store.takeCheckpoint = () => baseStore.getState().takeCheckpoint();
171
- store.withCheckpoint = <R>(operation: () => R): R => baseStore.getState().withCheckpoint(operation);
172
- store.undo = () => baseStore.getState().undo();
173
- store.redo = () => baseStore.getState().redo();
174
- store.clearHistory = () => baseStore.getState().clearHistory();
175
- store.canUndo = () => baseStore.getState().canUndo();
176
- store.canRedo = () => baseStore.getState().canRedo();
177
- store.exportHistory = () => baseStore.getState().exportHistory();
178
- store.importHistory = (data: HistoryData) => baseStore.getState().importHistory(data);
179
-
180
- return store;
181
- }
182
-
183
- // ============================================================================
184
- // Store Access API
185
- // ============================================================================
186
-
187
- // Get a canvas store by ID, or undefined if not exists
188
- export function getCanvasStore(canvasId: string): CanvasStore | undefined {
189
- return canvasStores.get(canvasId);
190
- }
191
-
192
- // Get or create a canvas store by ID
193
- // Non-main canvases (function canvases) are initialized with an OnFunctionCall node
194
- export function getOrCreateCanvasStore(canvasId: string): CanvasStore {
195
- if (!canvasStores.has(canvasId)) {
196
- const store = createCanvasStore();
197
-
198
- // Initialize function canvases with OnFunctionCall trigger node
199
- if (canvasId !== MAIN_CANVAS_ID) {
200
- const nodeId = generateId();
201
- const initialNode: Node<NodeData> = {
202
- id: nodeId,
203
- type: NodeCategory.Trigger,
204
- position: { x: 100, y: 100 },
205
- data: {
206
- id: nodeId,
207
- type: "OnFunctionCall",
208
- arguments: {},
209
- } as NodeData,
210
- };
211
- store.getState().initialize([initialNode], []);
212
- }
213
-
214
- canvasStores.set(canvasId, store);
215
- }
216
- return canvasStores.get(canvasId)!;
217
- }
218
-
219
- // Get all canvas stores
220
- export function getAllCanvasStores(): Record<string, CanvasStore> {
221
- const result: Record<string, CanvasStore> = {};
222
- canvasStores.forEach((store, id) => {
223
- result[id] = store;
224
- });
225
- return result;
226
- }
227
-
228
- // Delete a canvas store by ID. Cannot delete the main canvas.
229
- export function deleteCanvasStore(canvasId: string): void {
230
- if (canvasId === MAIN_CANVAS_ID) return;
231
- canvasStores.delete(canvasId);
232
- notifyCanvasRegistryChange();
233
- }
234
-
235
- // Clear all canvas stores, including the main canvas.
236
- export function clearAllCanvasStores(): void {
237
- canvasStores.clear();
238
- // Re-seed an empty main canvas BEFORE notifying. Two reasons: it preserves the
239
- // "main always exists" invariant, and — critically — subscribers re-subscribe to
240
- // the live store set on this notification. If main were absent here, they'd
241
- // snapshot an empty registry and never attach to the lazily-recreated main, so
242
- // edits after New/clear wouldn't fire onChange (no dirty dot, stale undo state).
243
- canvasStores.set(MAIN_CANVAS_ID, createCanvasStore());
244
- notifyCanvasRegistryChange();
245
- }
1
+ import { create, UseBoundStore, StoreApi } from "zustand";
2
+ import { Node, Edge, Viewport } from "@xyflow/react";
3
+ import { NodeCategory, type NodeData } from "@foresthubai/workflow-core/node";
4
+ import type { EdgeData } from "@foresthubai/workflow-core/edge";
5
+ import { history, History, type HistoryData, type MutationCount } from "../utils/history";
6
+ import { generateId } from "@foresthubai/workflow-core/id";
7
+ import { fnargKey, type Variable, type ApiVariable } from "@foresthubai/workflow-core/variable";
8
+ import { computeVariablesFromNodes } from "@foresthubai/workflow-core/workflow";
9
+
10
+ /**
11
+ * Sync fnarg:* entries in a canvas's variables to match a function's arguments.
12
+ * fnarg variables are *derived* from the (project-scoped) function declaration —
13
+ * they are not authored canvas state, so the source of truth is `editorStore`;
14
+ * callers pass the declaration's argument list here. Removes stale fnarg entries
15
+ * and adds the current ones.
16
+ */
17
+ export function syncFunctionArgVariables(store: CanvasStore, args: readonly ApiVariable[]): void {
18
+ store.getState().setVariables((vars) => {
19
+ const updated = { ...vars };
20
+ // Remove all existing fnarg entries
21
+ for (const key of Object.keys(updated)) {
22
+ if (key.startsWith("fnarg:")) delete updated[key];
23
+ }
24
+ // Add current args
25
+ for (const arg of args) {
26
+ updated[fnargKey(arg.uid)] = { kind: "fnarg", uid: arg.uid, name: arg.name, dataType: arg.dataType };
27
+ }
28
+ return updated;
29
+ });
30
+ }
31
+
32
+ export { MAIN_CANVAS_ID } from "@foresthubai/workflow-core/workflow";
33
+ import { MAIN_CANVAS_ID } from "@foresthubai/workflow-core/workflow";
34
+ const HISTORY_LIMIT = 50 as const;
35
+
36
+ // ============================================================================
37
+ // Canvas Registry Change Notification System
38
+ // ============================================================================
39
+
40
+ // Listeners notified when the *set* of canvas stores changes (a store is created,
41
+ // deleted, or the whole registry is cleared/retained). This is decoupled from
42
+ // function definitions — those now live in editorStore. Its sole consumer is
43
+ // WorkflowBuilder, which re-subscribes to every live store's mutationCount/history
44
+ // when the set changes so newly created (or dropped) canvases are watched.
45
+ const canvasRegistryListeners = new Set<() => void>();
46
+
47
+ // Notify subscribers that the canvas store set changed.
48
+ export function notifyCanvasRegistryChange(): void {
49
+ canvasRegistryListeners.forEach((listener) => listener());
50
+ }
51
+
52
+ // Subscribe to canvas registry (store set) changes.
53
+ export function subscribeCanvasRegistryChanges(listener: () => void): () => void {
54
+ canvasRegistryListeners.add(listener);
55
+ return () => canvasRegistryListeners.delete(listener);
56
+ }
57
+
58
+ // ============================================================================
59
+ // Canvas Store Registry
60
+ // ============================================================================
61
+
62
+ // Registry - Module-level map of independent store instances
63
+ const canvasStores = new Map<string, CanvasStore>();
64
+
65
+ canvasStores.set(MAIN_CANVAS_ID, createCanvasStore()); // Always exists
66
+
67
+ export interface CanvasState {
68
+ nodes: Node<NodeData>[];
69
+ edges: Edge<EdgeData>[];
70
+ // Unified variable record: node outputs (nodeId:outputId), declared (declared:uid), fn args (fnarg:uid).
71
+ // fnarg:* entries are derived from the project-scoped function declaration (editorStore) via
72
+ // syncFunctionArgVariables — they are not authored here.
73
+ variables: Record<string, Variable>;
74
+ // Session-only pan/zoom of the canvas viewport. View state, NOT workflow content:
75
+ // kept out of partialize (no history/serialization) so it never enters the contract,
76
+ // and lost on reload like selection. Persisted across tab switches so re-entering a
77
+ // canvas restores its view via defaultViewport instead of a post-paint fitView jump.
78
+ // null until first visited (then fitView seeds it).
79
+ viewport: Viewport | null;
80
+
81
+ setNodes: (updater: (nodes: Node<NodeData>[]) => Node<NodeData>[]) => void;
82
+ setEdges: (updater: (edges: Edge<EdgeData>[]) => Edge<EdgeData>[]) => void;
83
+ setVariables: (updater: (variables: Record<string, Variable>) => Record<string, Variable>) => void;
84
+ setViewport: (viewport: Viewport) => void;
85
+ /**
86
+ * Visual-only: set ReactFlow selected flags on nodes AND edges in one atomic update.
87
+ * This will call a single re-render and a single onSelectionChange callback.
88
+ * Not an update of domain state, so it can be used in read-only mode.
89
+ */
90
+ setRFselect: (nodeIds: string[], edgeIds: string[]) => void;
91
+ initialize: (nodes: Node<NodeData>[], edges: Edge<EdgeData>[]) => void;
92
+ }
93
+
94
+ // Canvas store is a Zustand store + history (undo/redo capabilities) + mutation count.
95
+ export type CanvasStore = UseBoundStore<StoreApi<CanvasState & MutationCount>> & History;
96
+
97
+ function createCanvasStore(): CanvasStore {
98
+ // Create base store with history middleware
99
+ const baseStore = create(
100
+ history<CanvasState>({
101
+ limit: HISTORY_LIMIT,
102
+ partialize: (state) => ({
103
+ nodes: state.nodes,
104
+ edges: state.edges,
105
+ variables: state.variables,
106
+ }),
107
+ equality: (before, after) => before.nodes === after.nodes && before.edges === after.edges && before.variables === after.variables,
108
+ })((set) => ({
109
+ nodes: [],
110
+ edges: [],
111
+ variables: {},
112
+ viewport: null,
113
+
114
+ setNodes: (updater) =>
115
+ set((state) => {
116
+ const next = updater(state.nodes);
117
+ if (next === state.nodes) return state;
118
+ return { nodes: next };
119
+ }),
120
+
121
+ setEdges: (updater) =>
122
+ set((state) => {
123
+ const next = updater(state.edges);
124
+ if (next === state.edges) return state;
125
+ return { edges: next };
126
+ }),
127
+
128
+ setVariables: (updater) =>
129
+ set((state) => {
130
+ const next = updater(state.variables);
131
+ if (next === state.variables) return state;
132
+ return { variables: next };
133
+ }),
134
+
135
+ // View-only — outside partialize, so it neither takes a checkpoint nor bumps
136
+ // mutationCount (no spurious dirty dot / onChange from panning).
137
+ setViewport: (viewport) => set({ viewport }),
138
+
139
+ setRFselect: (nodeIds, edgeIds) => {
140
+ const nodeIdSet = new Set(nodeIds);
141
+ const edgeIdSet = new Set(edgeIds);
142
+ set((state) => ({
143
+ nodes: state.nodes.map((n) => {
144
+ const shouldSelect = nodeIdSet.has(n.id);
145
+ return n.selected === shouldSelect ? n : { ...n, selected: shouldSelect };
146
+ }),
147
+ edges: state.edges.map((e) => {
148
+ const shouldSelect = edgeIdSet.has(e.id);
149
+ return e.selected === shouldSelect ? e : { ...e, selected: shouldSelect };
150
+ }),
151
+ }));
152
+ },
153
+
154
+ initialize: (nodes, edges) => {
155
+ // Build node-output variables. fnarg entries (for function canvases) are
156
+ // seeded separately via syncFunctionArgVariables from the editorStore
157
+ // declaration, since the canvas store no longer owns the signature.
158
+ // computeVariablesFromNodes is the core (NodeData[]) variant; peel the
159
+ // React Flow wrapper at the call site.
160
+ const vars: Record<string, Variable> = computeVariablesFromNodes(nodes.map((n) => n.data));
161
+ set({ nodes, edges, variables: vars });
162
+ },
163
+ })),
164
+ );
165
+
166
+ // Bind history methods from state to CanvasStore object
167
+ // This makes them accessible as store.undo() instead of store.getState().undo()
168
+ const store = baseStore as unknown as CanvasStore;
169
+
170
+ store.takeCheckpoint = () => baseStore.getState().takeCheckpoint();
171
+ store.withCheckpoint = <R>(operation: () => R): R => baseStore.getState().withCheckpoint(operation);
172
+ store.undo = () => baseStore.getState().undo();
173
+ store.redo = () => baseStore.getState().redo();
174
+ store.clearHistory = () => baseStore.getState().clearHistory();
175
+ store.canUndo = () => baseStore.getState().canUndo();
176
+ store.canRedo = () => baseStore.getState().canRedo();
177
+ store.exportHistory = () => baseStore.getState().exportHistory();
178
+ store.importHistory = (data: HistoryData) => baseStore.getState().importHistory(data);
179
+
180
+ return store;
181
+ }
182
+
183
+ // ============================================================================
184
+ // Store Access API
185
+ // ============================================================================
186
+
187
+ // Get a canvas store by ID, or undefined if not exists
188
+ export function getCanvasStore(canvasId: string): CanvasStore | undefined {
189
+ return canvasStores.get(canvasId);
190
+ }
191
+
192
+ // Get or create a canvas store by ID
193
+ // Non-main canvases (function canvases) are initialized with an OnFunctionCall node
194
+ export function getOrCreateCanvasStore(canvasId: string): CanvasStore {
195
+ if (!canvasStores.has(canvasId)) {
196
+ const store = createCanvasStore();
197
+
198
+ // Initialize function canvases with OnFunctionCall trigger node
199
+ if (canvasId !== MAIN_CANVAS_ID) {
200
+ const nodeId = generateId();
201
+ const initialNode: Node<NodeData> = {
202
+ id: nodeId,
203
+ type: NodeCategory.Trigger,
204
+ position: { x: 100, y: 100 },
205
+ data: {
206
+ id: nodeId,
207
+ type: "OnFunctionCall",
208
+ arguments: {},
209
+ } as NodeData,
210
+ };
211
+ store.getState().initialize([initialNode], []);
212
+ }
213
+
214
+ canvasStores.set(canvasId, store);
215
+ }
216
+ return canvasStores.get(canvasId)!;
217
+ }
218
+
219
+ // Get all canvas stores
220
+ export function getAllCanvasStores(): Record<string, CanvasStore> {
221
+ const result: Record<string, CanvasStore> = {};
222
+ canvasStores.forEach((store, id) => {
223
+ result[id] = store;
224
+ });
225
+ return result;
226
+ }
227
+
228
+ // Delete a canvas store by ID. Cannot delete the main canvas.
229
+ export function deleteCanvasStore(canvasId: string): void {
230
+ if (canvasId === MAIN_CANVAS_ID) return;
231
+ canvasStores.delete(canvasId);
232
+ notifyCanvasRegistryChange();
233
+ }
234
+
235
+ // Clear all canvas stores, including the main canvas.
236
+ export function clearAllCanvasStores(): void {
237
+ canvasStores.clear();
238
+ // Re-seed an empty main canvas BEFORE notifying. Two reasons: it preserves the
239
+ // "main always exists" invariant, and — critically — subscribers re-subscribe to
240
+ // the live store set on this notification. If main were absent here, they'd
241
+ // snapshot an empty registry and never attach to the lazily-recreated main, so
242
+ // edits after New/clear wouldn't fire onChange (no dirty dot, stale undo state).
243
+ canvasStores.set(MAIN_CANVAS_ID, createCanvasStore());
244
+ notifyCanvasRegistryChange();
245
+ }