@foresthubai/workflow-builder 0.3.0 → 0.4.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.
Files changed (120) hide show
  1. package/LICENSE +661 -661
  2. package/NOTICE +16 -16
  3. package/README.md +110 -93
  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/lib/utils.d.ts +3 -0
  11. package/dist/lib/utils.d.ts.map +1 -0
  12. package/dist/lib/utils.js +6 -0
  13. package/dist/lib/utils.js.map +1 -0
  14. package/dist/toolbars/CanvasTabsToolbar.d.ts +11 -0
  15. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +1 -0
  16. package/dist/toolbars/CanvasTabsToolbar.js +101 -0
  17. package/dist/toolbars/CanvasTabsToolbar.js.map +1 -0
  18. package/package.json +2 -2
  19. package/src/BuilderLayout.tsx +345 -345
  20. package/src/Canvas.tsx +261 -261
  21. package/src/CanvasEditor.tsx +142 -142
  22. package/src/CanvasTabsToolbar.tsx +176 -176
  23. package/src/RightConfigPanel.tsx +266 -266
  24. package/src/WorkflowBuilder.tsx +412 -412
  25. package/src/cn.ts +6 -6
  26. package/src/components/ui/add-button.tsx +39 -39
  27. package/src/components/ui/alert-dialog.tsx +141 -141
  28. package/src/components/ui/alert.tsx +59 -59
  29. package/src/components/ui/badge.tsx +36 -36
  30. package/src/components/ui/button.tsx +85 -85
  31. package/src/components/ui/card.tsx +79 -79
  32. package/src/components/ui/checkbox.tsx +28 -28
  33. package/src/components/ui/collapsible.tsx +9 -9
  34. package/src/components/ui/command.tsx +153 -153
  35. package/src/components/ui/delete-button.tsx +23 -23
  36. package/src/components/ui/dialog.tsx +125 -125
  37. package/src/components/ui/dropdown-menu.tsx +198 -198
  38. package/src/components/ui/input.tsx +55 -55
  39. package/src/components/ui/label.tsx +24 -24
  40. package/src/components/ui/readonly-banner.tsx +15 -15
  41. package/src/components/ui/resizable.tsx +43 -43
  42. package/src/components/ui/scroll-area.tsx +102 -102
  43. package/src/components/ui/select.tsx +160 -160
  44. package/src/components/ui/separator.tsx +29 -29
  45. package/src/components/ui/switch.tsx +27 -27
  46. package/src/components/ui/textarea.tsx +51 -51
  47. package/src/components/ui/toast.tsx +127 -127
  48. package/src/components/ui/toaster.tsx +33 -33
  49. package/src/components/ui/toggle-group.tsx +59 -59
  50. package/src/components/ui/toggle.tsx +43 -43
  51. package/src/components/ui/tooltip.tsx +32 -32
  52. package/src/dialogs/NodePickerDialog.tsx +84 -84
  53. package/src/dialogs/ValidationDialog.tsx +184 -184
  54. package/src/graph/BaseNode.tsx +557 -557
  55. package/src/graph/CustomEdge.tsx +185 -185
  56. package/src/graph/CustomNode.tsx +16 -16
  57. package/src/graph/FunctionCallNode.tsx +30 -30
  58. package/src/graph/PortHandle.tsx +189 -189
  59. package/src/graph/reactFlowRegistry.ts +26 -26
  60. package/src/hooks/use-toast.ts +125 -125
  61. package/src/hooks/useAvailableVariables.ts +20 -20
  62. package/src/hooks/useCanvasHistory.ts +22 -22
  63. package/src/hooks/useCanvasTabs.ts +168 -168
  64. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  65. package/src/hooks/useFunctionRegistry.ts +26 -26
  66. package/src/hooks/useFunctions.ts +44 -44
  67. package/src/hooks/useGraph.ts +161 -161
  68. package/src/hooks/useNodeDefinitions.ts +82 -82
  69. package/src/hooks/useParamErrors.ts +26 -26
  70. package/src/hooks/useResolvedTheme.ts +30 -30
  71. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  72. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  73. package/src/hooks/useWorkflowSerialization.ts +127 -127
  74. package/src/i18n/index.ts +53 -53
  75. package/src/i18n/locales/de.json +501 -501
  76. package/src/i18n/locales/en.json +557 -557
  77. package/src/index.ts +27 -27
  78. package/src/inputs/ExpressionInput.tsx +297 -297
  79. package/src/inputs/ParameterEditor.tsx +515 -515
  80. package/src/inputs/PortSection.tsx +144 -144
  81. package/src/panels/BuilderSidebar.tsx +301 -301
  82. package/src/panels/ChannelConfigPanel.tsx +49 -49
  83. package/src/panels/ChannelsPanel.tsx +28 -28
  84. package/src/panels/DebugConsolePanel.tsx +73 -73
  85. package/src/panels/DebugContextPanel.tsx +77 -77
  86. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  87. package/src/panels/DiagnosticsPanel.tsx +170 -170
  88. package/src/panels/EdgeConfigPanel.tsx +104 -104
  89. package/src/panels/FunctionConfigPanel.tsx +179 -179
  90. package/src/panels/FunctionListPanel.tsx +45 -45
  91. package/src/panels/MemoryConfigPanel.tsx +55 -55
  92. package/src/panels/MemoryPanel.tsx +40 -40
  93. package/src/panels/ModelConfigPanel.tsx +41 -41
  94. package/src/panels/ModelsPanel.tsx +36 -36
  95. package/src/panels/NodeConfigPanel.tsx +630 -630
  96. package/src/panels/NodeLibrary.tsx +288 -288
  97. package/src/panels/ResourceConfigPanel.tsx +132 -132
  98. package/src/panels/ResourceListPanel.tsx +113 -113
  99. package/src/panels/VariableConfigPanel.tsx +161 -161
  100. package/src/panels/VariablesPanel.tsx +145 -145
  101. package/src/stores/canvasStore.test.ts +44 -44
  102. package/src/stores/canvasStore.ts +245 -245
  103. package/src/stores/debugStore.ts +74 -74
  104. package/src/stores/diagnosticsStore.ts +130 -130
  105. package/src/stores/editorStore.ts +202 -202
  106. package/src/styles/index.css +526 -526
  107. package/src/utils/categoryConstants.ts +26 -26
  108. package/src/utils/channelOperations.ts +86 -86
  109. package/src/utils/connectionRules.ts +137 -137
  110. package/src/utils/functionOperations.ts +179 -179
  111. package/src/utils/graphOperations.ts +550 -550
  112. package/src/utils/history.ts +207 -207
  113. package/src/utils/memoryOperations.ts +57 -57
  114. package/src/utils/migrateFunctionNodes.ts +107 -107
  115. package/src/utils/modelOperations.ts +55 -55
  116. package/src/utils/paramDisplay.ts +71 -71
  117. package/src/utils/resourceHelpers.ts +32 -32
  118. package/src/utils/translation.ts +28 -28
  119. package/src/utils/variableOperations.ts +75 -75
  120. package/tailwind-preset.ts +166 -166
@@ -1,202 +1,202 @@
1
- import { create } from "zustand";
2
- import { getCanvasStore, getOrCreateCanvasStore, MAIN_CANVAS_ID } from "./canvasStore";
3
- import type { Channel } from "@foresthubai/workflow-core/channel";
4
- import type { Memory } from "@foresthubai/workflow-core/memory";
5
- import type { Model, ModelInfo } from "@foresthubai/workflow-core/model";
6
- import type { FunctionDeclaration } from "@foresthubai/workflow-core/function";
7
-
8
- // ---------------------------------------------------------------------------
9
- // Default Channels — every workflow starts pre-initialized with a UART
10
- // port so nodes that need a serial port (SerialRead/Write, OnSerialReceive)
11
- // have something to bind to out of the box. Domain shape only — the driver
12
- // binding is supplied at deploy time via the DeploymentMapping, not here.
13
- // ---------------------------------------------------------------------------
14
-
15
- export function createDefaultChannels(): Record<string, Channel> {
16
- const uart: Channel = { id: "uart0", label: "Serial", type: "UART", arguments: {} };
17
- return { [uart.id]: uart };
18
- }
19
-
20
- import type { BuilderMode } from "../WorkflowBuilder";
21
- // Type-only (erased) — the active left-sidebar tab lives here so non-sidebar code
22
- // (e.g. validation navigation) can open a specific panel. No runtime cycle.
23
- import type { SidebarTab } from "../panels/BuilderSidebar";
24
-
25
- // ---------------------------------------------------------------------------
26
- // Selection
27
- // ---------------------------------------------------------------------------
28
-
29
- /**
30
- * What the editor is focused on, driving right-side config-panel visibility.
31
- * A discriminated union so exclusivity is structural: at most one primitive is
32
- * ever selected, except nodes+edges which coexist under `graph` (box-select can
33
- * grab both). The only way to mutate it is the select* / clearSelection actions,
34
- * each of which replaces the whole value — no field can drift out of sync.
35
- */
36
- export type Selection =
37
- | { kind: "none" }
38
- | { kind: "graph"; nodeIds: string[]; edgeIds: string[] }
39
- | { kind: "channel"; id: string }
40
- | { kind: "memory"; id: string }
41
- | { kind: "model"; id: string }
42
- | { kind: "function"; id: string }
43
- | { kind: "variable"; uid: string };
44
-
45
- const NO_SELECTION: Selection = { kind: "none" };
46
-
47
- // Drop ReactFlow's visual selection on a canvas so previously-glowing nodes/edges
48
- // stop glowing. Peek (never create) — clearing selection must not resurrect a
49
- // canvas store that was just dropped (e.g. after clearAllCanvasStores).
50
- function clearRFselect(canvasId: string): void {
51
- getCanvasStore(canvasId)?.getState().setRFselect([], []);
52
- }
53
-
54
- // ---------------------------------------------------------------------------
55
- // Store
56
- // ---------------------------------------------------------------------------
57
-
58
- interface EditorState {
59
- activeCanvasId: string;
60
- activeSidebarTab: SidebarTab;
61
- builderMode: BuilderMode;
62
- selection: Selection;
63
- // Project-scoped channels (pins, buses) — shared across all canvases
64
- channels: Record<string, Channel>;
65
- // Project-scoped memory primitives (memory files + vector databases) — shared
66
- // across all canvases, referenced from nodes by id.
67
- memory: Record<string, Memory>;
68
- // Project-scoped declared custom/self-hosted models (channel-like) — referenced
69
- // from nodes by id, mapped to llmproxy providers at deploy.
70
- models: Record<string, Model>;
71
- // Project-scoped function declarations (signature + bundled output assignments).
72
- // The body of each lives in the matching canvas store (id === fn.id). Like the
73
- // other resources above, edits here are NOT undo-tracked.
74
- functions: Record<string, FunctionDeclaration>;
75
- // The static model catalog (what the llmproxy supports), supplied by the
76
- // embedder via WorkflowBuilderProps.models. Not workflow state — config only.
77
- availableModels: ModelInfo[];
78
- /**
79
- * Monotonic counter bumped on project-scoped domain mutations
80
- * (channels/memory/models). Mirrors canvasStores' history mutationCount so the
81
- * builder can fire a single onChange event from either source.
82
- */
83
- mutationCount: number;
84
- setActiveCanvas: (canvasId: string) => void;
85
- setBuilderMode: (mode: BuilderMode) => void;
86
- /** Programmatic graph selection (change selection and pushes into ReactFlow). */
87
- selectGraph: (nodeIds: string[], edgeIds: string[]) => void;
88
- /** ReactFlow-origin graph selection fires onSelectionChange which needs to update the editor state without pushing back to ReactFlow. */
89
- syncSelectionFromRF: (nodeIds: string[], edgeIds: string[]) => void;
90
- selectChannel: (id: string) => void;
91
- selectMemory: (id: string) => void;
92
- selectModel: (id: string) => void;
93
- /** Select a function AND switch the active canvas to its body (id === canvasId), so
94
- * the config panel's return-expression editors resolve against the body's scope. */
95
- selectFunction: (id: string) => void;
96
- selectVariable: (uid: string) => void;
97
- clearSelection: () => void;
98
- setActiveSidebarTab: (tab: SidebarTab) => void;
99
- setChannels: (updater: (vars: Record<string, Channel>) => Record<string, Channel>) => void;
100
- setMemory: (updater: (mem: Record<string, Memory>) => Record<string, Memory>) => void;
101
- setModels: (updater: (models: Record<string, Model>) => Record<string, Model>) => void;
102
- setFunctions: (updater: (funcs: Record<string, FunctionDeclaration>) => Record<string, FunctionDeclaration>) => void;
103
- setAvailableModels: (models: ModelInfo[]) => void;
104
- }
105
-
106
- export const useEditorStore = create<EditorState>((set, get) => ({
107
- activeCanvasId: MAIN_CANVAS_ID,
108
- builderMode: { type: "edit" },
109
- selection: NO_SELECTION,
110
- activeSidebarTab: "nodes",
111
- channels: createDefaultChannels(),
112
- memory: {},
113
- models: {},
114
- functions: {},
115
- availableModels: [],
116
- mutationCount: 0,
117
- // A `variable` selection is canvas-local; its uid would resolve to nothing (or,
118
- // worse, a collision) on the new canvas, so drop it. A `function` selection is
119
- // tied to being on that function's body canvas (selectFunction switches to it),
120
- // so leaving that canvas drops it too — switching INTO a function tab instead
121
- // routes through selectFunction (see useCanvasTabs.setActiveTabId), not here.
122
- // Project-scoped channel/memory/model selections survive the switch.
123
- setActiveCanvas: (canvasId: string) =>
124
- set((state) => ({
125
- activeCanvasId: canvasId,
126
- selection:
127
- state.selection.kind === "variable" || state.selection.kind === "function" ? NO_SELECTION : state.selection,
128
- })),
129
- setBuilderMode: (mode: BuilderMode) => set({ builderMode: mode }),
130
- selectGraph: (nodeIds, edgeIds) => {
131
- set({ selection: nodeIds.length || edgeIds.length ? { kind: "graph", nodeIds, edgeIds } : NO_SELECTION });
132
- // Programmatic pick — mirror it into ReactFlow so the canvas reflects it.
133
- getOrCreateCanvasStore(get().activeCanvasId).getState().setRFselect(nodeIds, edgeIds);
134
- },
135
- syncSelectionFromRF: (nodeIds, edgeIds) => {
136
- if (nodeIds.length || edgeIds.length) {
137
- // A selection made on the canvas (click, box-drag) is hoisted into the editor state.
138
- // A programmatic selectGraph also round-trips here via onSelectionChange; that just re-sets an
139
- // equal value (one benign re-render), so it needs no special-casing.
140
- set({ selection: { kind: "graph", nodeIds, edgeIds } });
141
- } else if (get().selection.kind === "graph") {
142
- // Empty while a graph selection was active = user deselected on the canvas.
143
- set({ selection: NO_SELECTION });
144
- }
145
- // Empty + non-graph kind = echo of the canvas-clear we triggered when picking
146
- // a channel/memory/etc; ignore it, or it would wipe that pick.
147
- },
148
- selectChannel: (id) => {
149
- set({ selection: { kind: "channel", id } });
150
- clearRFselect(get().activeCanvasId);
151
- },
152
- selectMemory: (id) => {
153
- set({ selection: { kind: "memory", id } });
154
- clearRFselect(get().activeCanvasId);
155
- },
156
- selectModel: (id) => {
157
- set({ selection: { kind: "model", id } });
158
- clearRFselect(get().activeCanvasId);
159
- },
160
- selectFunction: (id) => {
161
- // Drop the outgoing canvas's RF selection, then focus the function: select it
162
- // AND make its body the active canvas so the config panel's expression editors
163
- // resolve against the function's local variable scope.
164
- clearRFselect(get().activeCanvasId);
165
- set({ selection: { kind: "function", id }, activeCanvasId: id });
166
- },
167
- selectVariable: (uid) => {
168
- set({ selection: { kind: "variable", uid } });
169
- clearRFselect(get().activeCanvasId);
170
- },
171
- clearSelection: () => {
172
- set({ selection: NO_SELECTION });
173
- clearRFselect(get().activeCanvasId);
174
- },
175
- setActiveSidebarTab: (tab) => set({ activeSidebarTab: tab }),
176
- setChannels: (updater) =>
177
- set((state) => {
178
- const next = updater(state.channels);
179
- if (next === state.channels) return state;
180
- return { channels: next, mutationCount: state.mutationCount + 1 };
181
- }),
182
- setMemory: (updater) =>
183
- set((state) => {
184
- const next = updater(state.memory);
185
- if (next === state.memory) return state;
186
- return { memory: next, mutationCount: state.mutationCount + 1 };
187
- }),
188
- setModels: (updater) =>
189
- set((state) => {
190
- const next = updater(state.models);
191
- if (next === state.models) return state;
192
- return { models: next, mutationCount: state.mutationCount + 1 };
193
- }),
194
- setFunctions: (updater) =>
195
- set((state) => {
196
- const next = updater(state.functions);
197
- if (next === state.functions) return state;
198
- return { functions: next, mutationCount: state.mutationCount + 1 };
199
- }),
200
- // Catalog is config (from props), not workflow content — never bumps mutationCount.
201
- setAvailableModels: (models) => set({ availableModels: models }),
202
- }));
1
+ import { create } from "zustand";
2
+ import { getCanvasStore, getOrCreateCanvasStore, MAIN_CANVAS_ID } from "./canvasStore";
3
+ import type { Channel } from "@foresthubai/workflow-core/channel";
4
+ import type { Memory } from "@foresthubai/workflow-core/memory";
5
+ import type { Model, ModelInfo } from "@foresthubai/workflow-core/model";
6
+ import type { FunctionDeclaration } from "@foresthubai/workflow-core/function";
7
+
8
+ // ---------------------------------------------------------------------------
9
+ // Default Channels — every workflow starts pre-initialized with a UART
10
+ // port so nodes that need a serial port (SerialRead/Write, OnSerialReceive)
11
+ // have something to bind to out of the box. Domain shape only — the driver
12
+ // binding is supplied at deploy time via the DeploymentMapping, not here.
13
+ // ---------------------------------------------------------------------------
14
+
15
+ export function createDefaultChannels(): Record<string, Channel> {
16
+ const uart: Channel = { id: "uart0", label: "Serial", type: "UART", arguments: {} };
17
+ return { [uart.id]: uart };
18
+ }
19
+
20
+ import type { BuilderMode } from "../WorkflowBuilder";
21
+ // Type-only (erased) — the active left-sidebar tab lives here so non-sidebar code
22
+ // (e.g. validation navigation) can open a specific panel. No runtime cycle.
23
+ import type { SidebarTab } from "../panels/BuilderSidebar";
24
+
25
+ // ---------------------------------------------------------------------------
26
+ // Selection
27
+ // ---------------------------------------------------------------------------
28
+
29
+ /**
30
+ * What the editor is focused on, driving right-side config-panel visibility.
31
+ * A discriminated union so exclusivity is structural: at most one primitive is
32
+ * ever selected, except nodes+edges which coexist under `graph` (box-select can
33
+ * grab both). The only way to mutate it is the select* / clearSelection actions,
34
+ * each of which replaces the whole value — no field can drift out of sync.
35
+ */
36
+ export type Selection =
37
+ | { kind: "none" }
38
+ | { kind: "graph"; nodeIds: string[]; edgeIds: string[] }
39
+ | { kind: "channel"; id: string }
40
+ | { kind: "memory"; id: string }
41
+ | { kind: "model"; id: string }
42
+ | { kind: "function"; id: string }
43
+ | { kind: "variable"; uid: string };
44
+
45
+ const NO_SELECTION: Selection = { kind: "none" };
46
+
47
+ // Drop ReactFlow's visual selection on a canvas so previously-glowing nodes/edges
48
+ // stop glowing. Peek (never create) — clearing selection must not resurrect a
49
+ // canvas store that was just dropped (e.g. after clearAllCanvasStores).
50
+ function clearRFselect(canvasId: string): void {
51
+ getCanvasStore(canvasId)?.getState().setRFselect([], []);
52
+ }
53
+
54
+ // ---------------------------------------------------------------------------
55
+ // Store
56
+ // ---------------------------------------------------------------------------
57
+
58
+ interface EditorState {
59
+ activeCanvasId: string;
60
+ activeSidebarTab: SidebarTab;
61
+ builderMode: BuilderMode;
62
+ selection: Selection;
63
+ // Project-scoped channels (pins, buses) — shared across all canvases
64
+ channels: Record<string, Channel>;
65
+ // Project-scoped memory primitives (memory files + vector databases) — shared
66
+ // across all canvases, referenced from nodes by id.
67
+ memory: Record<string, Memory>;
68
+ // Project-scoped declared custom/self-hosted models (channel-like) — referenced
69
+ // from nodes by id, mapped to llmproxy providers at deploy.
70
+ models: Record<string, Model>;
71
+ // Project-scoped function declarations (signature + bundled output assignments).
72
+ // The body of each lives in the matching canvas store (id === fn.id). Like the
73
+ // other resources above, edits here are NOT undo-tracked.
74
+ functions: Record<string, FunctionDeclaration>;
75
+ // The static model catalog (what the llmproxy supports), supplied by the
76
+ // embedder via WorkflowBuilderProps.models. Not workflow state — config only.
77
+ availableModels: ModelInfo[];
78
+ /**
79
+ * Monotonic counter bumped on project-scoped domain mutations
80
+ * (channels/memory/models). Mirrors canvasStores' history mutationCount so the
81
+ * builder can fire a single onChange event from either source.
82
+ */
83
+ mutationCount: number;
84
+ setActiveCanvas: (canvasId: string) => void;
85
+ setBuilderMode: (mode: BuilderMode) => void;
86
+ /** Programmatic graph selection (change selection and pushes into ReactFlow). */
87
+ selectGraph: (nodeIds: string[], edgeIds: string[]) => void;
88
+ /** ReactFlow-origin graph selection fires onSelectionChange which needs to update the editor state without pushing back to ReactFlow. */
89
+ syncSelectionFromRF: (nodeIds: string[], edgeIds: string[]) => void;
90
+ selectChannel: (id: string) => void;
91
+ selectMemory: (id: string) => void;
92
+ selectModel: (id: string) => void;
93
+ /** Select a function AND switch the active canvas to its body (id === canvasId), so
94
+ * the config panel's return-expression editors resolve against the body's scope. */
95
+ selectFunction: (id: string) => void;
96
+ selectVariable: (uid: string) => void;
97
+ clearSelection: () => void;
98
+ setActiveSidebarTab: (tab: SidebarTab) => void;
99
+ setChannels: (updater: (vars: Record<string, Channel>) => Record<string, Channel>) => void;
100
+ setMemory: (updater: (mem: Record<string, Memory>) => Record<string, Memory>) => void;
101
+ setModels: (updater: (models: Record<string, Model>) => Record<string, Model>) => void;
102
+ setFunctions: (updater: (funcs: Record<string, FunctionDeclaration>) => Record<string, FunctionDeclaration>) => void;
103
+ setAvailableModels: (models: ModelInfo[]) => void;
104
+ }
105
+
106
+ export const useEditorStore = create<EditorState>((set, get) => ({
107
+ activeCanvasId: MAIN_CANVAS_ID,
108
+ builderMode: { type: "edit" },
109
+ selection: NO_SELECTION,
110
+ activeSidebarTab: "nodes",
111
+ channels: createDefaultChannels(),
112
+ memory: {},
113
+ models: {},
114
+ functions: {},
115
+ availableModels: [],
116
+ mutationCount: 0,
117
+ // A `variable` selection is canvas-local; its uid would resolve to nothing (or,
118
+ // worse, a collision) on the new canvas, so drop it. A `function` selection is
119
+ // tied to being on that function's body canvas (selectFunction switches to it),
120
+ // so leaving that canvas drops it too — switching INTO a function tab instead
121
+ // routes through selectFunction (see useCanvasTabs.setActiveTabId), not here.
122
+ // Project-scoped channel/memory/model selections survive the switch.
123
+ setActiveCanvas: (canvasId: string) =>
124
+ set((state) => ({
125
+ activeCanvasId: canvasId,
126
+ selection:
127
+ state.selection.kind === "variable" || state.selection.kind === "function" ? NO_SELECTION : state.selection,
128
+ })),
129
+ setBuilderMode: (mode: BuilderMode) => set({ builderMode: mode }),
130
+ selectGraph: (nodeIds, edgeIds) => {
131
+ set({ selection: nodeIds.length || edgeIds.length ? { kind: "graph", nodeIds, edgeIds } : NO_SELECTION });
132
+ // Programmatic pick — mirror it into ReactFlow so the canvas reflects it.
133
+ getOrCreateCanvasStore(get().activeCanvasId).getState().setRFselect(nodeIds, edgeIds);
134
+ },
135
+ syncSelectionFromRF: (nodeIds, edgeIds) => {
136
+ if (nodeIds.length || edgeIds.length) {
137
+ // A selection made on the canvas (click, box-drag) is hoisted into the editor state.
138
+ // A programmatic selectGraph also round-trips here via onSelectionChange; that just re-sets an
139
+ // equal value (one benign re-render), so it needs no special-casing.
140
+ set({ selection: { kind: "graph", nodeIds, edgeIds } });
141
+ } else if (get().selection.kind === "graph") {
142
+ // Empty while a graph selection was active = user deselected on the canvas.
143
+ set({ selection: NO_SELECTION });
144
+ }
145
+ // Empty + non-graph kind = echo of the canvas-clear we triggered when picking
146
+ // a channel/memory/etc; ignore it, or it would wipe that pick.
147
+ },
148
+ selectChannel: (id) => {
149
+ set({ selection: { kind: "channel", id } });
150
+ clearRFselect(get().activeCanvasId);
151
+ },
152
+ selectMemory: (id) => {
153
+ set({ selection: { kind: "memory", id } });
154
+ clearRFselect(get().activeCanvasId);
155
+ },
156
+ selectModel: (id) => {
157
+ set({ selection: { kind: "model", id } });
158
+ clearRFselect(get().activeCanvasId);
159
+ },
160
+ selectFunction: (id) => {
161
+ // Drop the outgoing canvas's RF selection, then focus the function: select it
162
+ // AND make its body the active canvas so the config panel's expression editors
163
+ // resolve against the function's local variable scope.
164
+ clearRFselect(get().activeCanvasId);
165
+ set({ selection: { kind: "function", id }, activeCanvasId: id });
166
+ },
167
+ selectVariable: (uid) => {
168
+ set({ selection: { kind: "variable", uid } });
169
+ clearRFselect(get().activeCanvasId);
170
+ },
171
+ clearSelection: () => {
172
+ set({ selection: NO_SELECTION });
173
+ clearRFselect(get().activeCanvasId);
174
+ },
175
+ setActiveSidebarTab: (tab) => set({ activeSidebarTab: tab }),
176
+ setChannels: (updater) =>
177
+ set((state) => {
178
+ const next = updater(state.channels);
179
+ if (next === state.channels) return state;
180
+ return { channels: next, mutationCount: state.mutationCount + 1 };
181
+ }),
182
+ setMemory: (updater) =>
183
+ set((state) => {
184
+ const next = updater(state.memory);
185
+ if (next === state.memory) return state;
186
+ return { memory: next, mutationCount: state.mutationCount + 1 };
187
+ }),
188
+ setModels: (updater) =>
189
+ set((state) => {
190
+ const next = updater(state.models);
191
+ if (next === state.models) return state;
192
+ return { models: next, mutationCount: state.mutationCount + 1 };
193
+ }),
194
+ setFunctions: (updater) =>
195
+ set((state) => {
196
+ const next = updater(state.functions);
197
+ if (next === state.functions) return state;
198
+ return { functions: next, mutationCount: state.mutationCount + 1 };
199
+ }),
200
+ // Catalog is config (from props), not workflow content — never bumps mutationCount.
201
+ setAvailableModels: (models) => set({ availableModels: models }),
202
+ }));