@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.
- package/LICENSE +661 -661
- package/NOTICE +16 -16
- package/README.md +110 -93
- package/dist/components/ui/command.d.ts +2 -2
- package/dist/components/ui/input.d.ts +1 -1
- package/dist/components/ui/resizable.d.ts +1 -1
- package/dist/components/ui/textarea.d.ts +1 -1
- package/dist/graph/BaseNode.js +10 -10
- package/dist/graph/reactFlowRegistry.d.ts.map +1 -1
- package/dist/lib/utils.d.ts +3 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/utils.js +6 -0
- package/dist/lib/utils.js.map +1 -0
- package/dist/toolbars/CanvasTabsToolbar.d.ts +11 -0
- package/dist/toolbars/CanvasTabsToolbar.d.ts.map +1 -0
- package/dist/toolbars/CanvasTabsToolbar.js +101 -0
- package/dist/toolbars/CanvasTabsToolbar.js.map +1 -0
- package/package.json +2 -2
- package/src/BuilderLayout.tsx +345 -345
- package/src/Canvas.tsx +261 -261
- package/src/CanvasEditor.tsx +142 -142
- package/src/CanvasTabsToolbar.tsx +176 -176
- package/src/RightConfigPanel.tsx +266 -266
- package/src/WorkflowBuilder.tsx +412 -412
- package/src/cn.ts +6 -6
- package/src/components/ui/add-button.tsx +39 -39
- package/src/components/ui/alert-dialog.tsx +141 -141
- package/src/components/ui/alert.tsx +59 -59
- package/src/components/ui/badge.tsx +36 -36
- package/src/components/ui/button.tsx +85 -85
- package/src/components/ui/card.tsx +79 -79
- package/src/components/ui/checkbox.tsx +28 -28
- package/src/components/ui/collapsible.tsx +9 -9
- package/src/components/ui/command.tsx +153 -153
- package/src/components/ui/delete-button.tsx +23 -23
- package/src/components/ui/dialog.tsx +125 -125
- package/src/components/ui/dropdown-menu.tsx +198 -198
- package/src/components/ui/input.tsx +55 -55
- package/src/components/ui/label.tsx +24 -24
- package/src/components/ui/readonly-banner.tsx +15 -15
- package/src/components/ui/resizable.tsx +43 -43
- package/src/components/ui/scroll-area.tsx +102 -102
- package/src/components/ui/select.tsx +160 -160
- package/src/components/ui/separator.tsx +29 -29
- package/src/components/ui/switch.tsx +27 -27
- package/src/components/ui/textarea.tsx +51 -51
- package/src/components/ui/toast.tsx +127 -127
- package/src/components/ui/toaster.tsx +33 -33
- package/src/components/ui/toggle-group.tsx +59 -59
- package/src/components/ui/toggle.tsx +43 -43
- package/src/components/ui/tooltip.tsx +32 -32
- package/src/dialogs/NodePickerDialog.tsx +84 -84
- package/src/dialogs/ValidationDialog.tsx +184 -184
- package/src/graph/BaseNode.tsx +557 -557
- package/src/graph/CustomEdge.tsx +185 -185
- package/src/graph/CustomNode.tsx +16 -16
- package/src/graph/FunctionCallNode.tsx +30 -30
- package/src/graph/PortHandle.tsx +189 -189
- package/src/graph/reactFlowRegistry.ts +26 -26
- package/src/hooks/use-toast.ts +125 -125
- package/src/hooks/useAvailableVariables.ts +20 -20
- package/src/hooks/useCanvasHistory.ts +22 -22
- package/src/hooks/useCanvasTabs.ts +168 -168
- package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
- package/src/hooks/useFunctionRegistry.ts +26 -26
- package/src/hooks/useFunctions.ts +44 -44
- package/src/hooks/useGraph.ts +161 -161
- package/src/hooks/useNodeDefinitions.ts +82 -82
- package/src/hooks/useParamErrors.ts +26 -26
- package/src/hooks/useResolvedTheme.ts +30 -30
- package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
- package/src/hooks/useSuppressThemeTransition.ts +79 -79
- package/src/hooks/useWorkflowSerialization.ts +127 -127
- package/src/i18n/index.ts +53 -53
- package/src/i18n/locales/de.json +501 -501
- package/src/i18n/locales/en.json +557 -557
- package/src/index.ts +27 -27
- package/src/inputs/ExpressionInput.tsx +297 -297
- package/src/inputs/ParameterEditor.tsx +515 -515
- package/src/inputs/PortSection.tsx +144 -144
- package/src/panels/BuilderSidebar.tsx +301 -301
- package/src/panels/ChannelConfigPanel.tsx +49 -49
- package/src/panels/ChannelsPanel.tsx +28 -28
- package/src/panels/DebugConsolePanel.tsx +73 -73
- package/src/panels/DebugContextPanel.tsx +77 -77
- package/src/panels/DebugExternalIOPanel.tsx +180 -180
- package/src/panels/DiagnosticsPanel.tsx +170 -170
- package/src/panels/EdgeConfigPanel.tsx +104 -104
- package/src/panels/FunctionConfigPanel.tsx +179 -179
- package/src/panels/FunctionListPanel.tsx +45 -45
- package/src/panels/MemoryConfigPanel.tsx +55 -55
- package/src/panels/MemoryPanel.tsx +40 -40
- package/src/panels/ModelConfigPanel.tsx +41 -41
- package/src/panels/ModelsPanel.tsx +36 -36
- package/src/panels/NodeConfigPanel.tsx +630 -630
- package/src/panels/NodeLibrary.tsx +288 -288
- package/src/panels/ResourceConfigPanel.tsx +132 -132
- package/src/panels/ResourceListPanel.tsx +113 -113
- package/src/panels/VariableConfigPanel.tsx +161 -161
- package/src/panels/VariablesPanel.tsx +145 -145
- package/src/stores/canvasStore.test.ts +44 -44
- package/src/stores/canvasStore.ts +245 -245
- package/src/stores/debugStore.ts +74 -74
- package/src/stores/diagnosticsStore.ts +130 -130
- package/src/stores/editorStore.ts +202 -202
- package/src/styles/index.css +526 -526
- package/src/utils/categoryConstants.ts +26 -26
- package/src/utils/channelOperations.ts +86 -86
- package/src/utils/connectionRules.ts +137 -137
- package/src/utils/functionOperations.ts +179 -179
- package/src/utils/graphOperations.ts +550 -550
- package/src/utils/history.ts +207 -207
- package/src/utils/memoryOperations.ts +57 -57
- package/src/utils/migrateFunctionNodes.ts +107 -107
- package/src/utils/modelOperations.ts +55 -55
- package/src/utils/paramDisplay.ts +71 -71
- package/src/utils/resourceHelpers.ts +32 -32
- package/src/utils/translation.ts +28 -28
- package/src/utils/variableOperations.ts +75 -75
- package/tailwind-preset.ts +166 -166
package/src/WorkflowBuilder.tsx
CHANGED
|
@@ -1,412 +1,412 @@
|
|
|
1
|
-
import type { ApiWorkflow } from "@foresthubai/workflow-core/workflow";
|
|
2
|
-
import type { ModelInfo } from "@foresthubai/workflow-core/model";
|
|
3
|
-
import {
|
|
4
|
-
validateWorkflowState,
|
|
5
|
-
validateChannel,
|
|
6
|
-
validateMemory,
|
|
7
|
-
validateModel,
|
|
8
|
-
type Diagnostic,
|
|
9
|
-
type ValidationResult,
|
|
10
|
-
} from "@foresthubai/workflow-core/diagnostics";
|
|
11
|
-
import { forwardRef, useCallback, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react";
|
|
12
|
-
import { I18nextProvider } from "react-i18next";
|
|
13
|
-
|
|
14
|
-
import i18n from "./i18n";
|
|
15
|
-
import { toast } from "./hooks/use-toast";
|
|
16
|
-
import ValidationDialog from "./dialogs/ValidationDialog";
|
|
17
|
-
import { BuilderLayout } from "./BuilderLayout";
|
|
18
|
-
import { TooltipProvider } from "./components/ui/tooltip";
|
|
19
|
-
import { Toaster } from "./components/ui/toaster";
|
|
20
|
-
import { useCanvasTabs } from "./hooks/useCanvasTabs";
|
|
21
|
-
import { useResourceDiagnosticsSync } from "./hooks/useResourceDiagnosticsSync";
|
|
22
|
-
import { useFunctionDiagnosticsSync } from "./hooks/useFunctionDiagnosticsSync";
|
|
23
|
-
import { useSuppressThemeTransition } from "./hooks/useSuppressThemeTransition";
|
|
24
|
-
import { useFunctions } from "./hooks/useFunctions";
|
|
25
|
-
import { useWorkflowSerialization, readStateFromStores } from "./hooks/useWorkflowSerialization";
|
|
26
|
-
import {
|
|
27
|
-
clearAllCanvasStores,
|
|
28
|
-
getAllCanvasStores,
|
|
29
|
-
getOrCreateCanvasStore,
|
|
30
|
-
subscribeCanvasRegistryChanges,
|
|
31
|
-
MAIN_CANVAS_ID,
|
|
32
|
-
type CanvasStore,
|
|
33
|
-
} from "./stores/canvasStore";
|
|
34
|
-
import { useDebugStore, type DebugSessionPhase } from "./stores/debugStore";
|
|
35
|
-
import { useEditorStore } from "./stores/editorStore";
|
|
36
|
-
|
|
37
|
-
/** BuilderMode steers the overall behavior of the workflow builder. */
|
|
38
|
-
export type BuilderMode = { type: "edit" } | { type: "preview" } | { type: "debug" };
|
|
39
|
-
|
|
40
|
-
/** True when canvas mutations should be blocked (preview or debug). */
|
|
41
|
-
export function isReadOnly(mode: BuilderMode): boolean {
|
|
42
|
-
return mode.type !== "edit";
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/** Type guard for preview mode. */
|
|
46
|
-
export function isPreview(mode: BuilderMode): boolean {
|
|
47
|
-
return mode.type === "preview";
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// ============================================================================
|
|
51
|
-
// Public contract
|
|
52
|
-
// ============================================================================
|
|
53
|
-
|
|
54
|
-
export interface WorkflowBuilderProps {
|
|
55
|
-
/** Workflow loaded on mount. If none is provided, an empty workflow is created. */
|
|
56
|
-
initialWorkflow?: ApiWorkflow;
|
|
57
|
-
/** Builder mode on mount. Defaults to { type: "edit" }. */
|
|
58
|
-
initialMode?: BuilderMode;
|
|
59
|
-
/**
|
|
60
|
-
* Static model catalog — the models the llmproxy supports. Shown as the
|
|
61
|
-
* built-in options in agent model pickers. Self-hosted/custom models are
|
|
62
|
-
* declared in the Models tab instead. Defaults to [] (empty dropdown).
|
|
63
|
-
*/
|
|
64
|
-
models?: ModelInfo[];
|
|
65
|
-
/**
|
|
66
|
-
* UI language (e.g. "en", "de"). The host owns locale; the builder follows.
|
|
67
|
-
* Defaults to "en". The builder never auto-detects language.
|
|
68
|
-
*/
|
|
69
|
-
language?: string;
|
|
70
|
-
|
|
71
|
-
// ── Embedder-fulfilled actions (builder asks, embedder does) ──
|
|
72
|
-
/** A node requested embedder-side testing (e.g. Agent "Test" button). */
|
|
73
|
-
onTestNode?: (nodeId: string) => void;
|
|
74
|
-
/** Step request from the in-builder debug panel — embedder forwards to the engine. */
|
|
75
|
-
onDebugStep?: (nodeId?: string) => void;
|
|
76
|
-
|
|
77
|
-
// ── Lifecycle events ──
|
|
78
|
-
/** Fires after any domain-state mutation. Pull current state via handle.exportWorkflow(). */
|
|
79
|
-
onChange?: () => void;
|
|
80
|
-
/**
|
|
81
|
-
* Undo/redo availability for the ACTIVE canvas changed — on history mutation,
|
|
82
|
-
* undo/redo, or a tab switch (each canvas has its own history). For wiring host
|
|
83
|
-
* undo/redo buttons.
|
|
84
|
-
*/
|
|
85
|
-
onHistoryChange?: (state: { canUndo: boolean; canRedo: boolean }) => void;
|
|
86
|
-
/** Unexpected error during builder operations (e.g. failed load). */
|
|
87
|
-
onError?: (error: Error) => void;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export interface WorkflowBuilderHandle {
|
|
91
|
-
// State I/O
|
|
92
|
-
loadWorkflow: (workflow: ApiWorkflow) => void;
|
|
93
|
-
exportWorkflow: () => ApiWorkflow;
|
|
94
|
-
clear: () => void;
|
|
95
|
-
|
|
96
|
-
// Mode (replaces preview/debug entry-point props)
|
|
97
|
-
setMode: (mode: BuilderMode) => void;
|
|
98
|
-
getMode: () => BuilderMode;
|
|
99
|
-
|
|
100
|
-
// Initiate the in-builder validation process which will either show the validation dialog or a toast if clean.
|
|
101
|
-
validate: () => void;
|
|
102
|
-
|
|
103
|
-
// History (so embedder chrome can wire undo/redo buttons)
|
|
104
|
-
undo: () => void;
|
|
105
|
-
redo: () => void;
|
|
106
|
-
|
|
107
|
-
// Debug (embedder pushes engine events for visualization)
|
|
108
|
-
setDebugPhase: (phase: DebugSessionPhase) => void;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
// ============================================================================
|
|
112
|
-
// Component — owns tabs/functions/dialogs; exposes the handle; delegates
|
|
113
|
-
// layout/canvas rendering to BuilderLayout.
|
|
114
|
-
// ============================================================================
|
|
115
|
-
|
|
116
|
-
export const WorkflowBuilder = forwardRef<WorkflowBuilderHandle, WorkflowBuilderProps>(
|
|
117
|
-
function WorkflowBuilder(props, ref) {
|
|
118
|
-
const {
|
|
119
|
-
initialWorkflow,
|
|
120
|
-
initialMode,
|
|
121
|
-
models,
|
|
122
|
-
language,
|
|
123
|
-
onTestNode,
|
|
124
|
-
onDebugStep,
|
|
125
|
-
onChange,
|
|
126
|
-
onHistoryChange,
|
|
127
|
-
onError,
|
|
128
|
-
} = props;
|
|
129
|
-
|
|
130
|
-
// Host drives locale. useLayoutEffect (not useEffect) so the language is set
|
|
131
|
-
// before paint — mounting with language="de" shows German on the first frame
|
|
132
|
-
// rather than flashing English. changeLanguage is sync here (bundled resources).
|
|
133
|
-
useLayoutEffect(() => {
|
|
134
|
-
if (language && i18n.language !== language) i18n.changeLanguage(language);
|
|
135
|
-
}, [language]);
|
|
136
|
-
|
|
137
|
-
const { importProject, exportProject } = useWorkflowSerialization();
|
|
138
|
-
|
|
139
|
-
// Color-mode toggles should snap, not fade — see hook docs.
|
|
140
|
-
useSuppressThemeTransition();
|
|
141
|
-
|
|
142
|
-
// Keep project-scoped (channel + memory + model) diagnostics in sync. Mounted
|
|
143
|
-
// once here at the root so badges survive sidebar tab open/close.
|
|
144
|
-
useResourceDiagnosticsSync({
|
|
145
|
-
selectItems: (s) => s.channels,
|
|
146
|
-
validate: validateChannel,
|
|
147
|
-
getStored: (d) => d.byChannelId,
|
|
148
|
-
set: (d, id, diags) => d.setChannelDiagnostics(id, diags),
|
|
149
|
-
clear: (d, id) => d.clearChannelDiagnostics(id),
|
|
150
|
-
});
|
|
151
|
-
useResourceDiagnosticsSync({
|
|
152
|
-
selectItems: (s) => s.memory,
|
|
153
|
-
validate: validateMemory,
|
|
154
|
-
getStored: (d) => d.byMemoryId,
|
|
155
|
-
set: (d, id, diags) => d.setMemoryDiagnostics(id, diags),
|
|
156
|
-
clear: (d, id) => d.clearMemoryDiagnostics(id),
|
|
157
|
-
});
|
|
158
|
-
useResourceDiagnosticsSync({
|
|
159
|
-
selectItems: (s) => s.models,
|
|
160
|
-
validate: validateModel,
|
|
161
|
-
getStored: (d) => d.byModelId,
|
|
162
|
-
set: (d, id, diags) => d.setModelDiagnostics(id, diags),
|
|
163
|
-
clear: (d, id) => d.clearModelDiagnostics(id),
|
|
164
|
-
});
|
|
165
|
-
// Functions are a FunctionDeclaration (not a flat resource bag), so they use a
|
|
166
|
-
// dedicated diagnostics sync.
|
|
167
|
-
useFunctionDiagnosticsSync();
|
|
168
|
-
|
|
169
|
-
// Push the embedder-supplied model catalog into the store so agent model
|
|
170
|
-
// pickers can read it. Catalog is config (not workflow content), so this
|
|
171
|
-
// never fires onChange.
|
|
172
|
-
useEffect(() => {
|
|
173
|
-
useEditorStore.getState().setAvailableModels(models ?? []);
|
|
174
|
-
}, [models]);
|
|
175
|
-
|
|
176
|
-
// Canvas tabs + functions live here because they survive canvas switches.
|
|
177
|
-
const canvasTabs = useCanvasTabs();
|
|
178
|
-
const functionsHook = useFunctions({ onOpenTab: canvasTabs.openTab });
|
|
179
|
-
|
|
180
|
-
// Built-in validation UX. validate() presents the result itself rather than
|
|
181
|
-
// returning it: a success toast when clean, else this dialog. Non-null = open.
|
|
182
|
-
const [validation, setValidation] = useState<ValidationResult | null>(null);
|
|
183
|
-
|
|
184
|
-
const runValidate = useCallback(() => {
|
|
185
|
-
const result = validateWorkflowState(readStateFromStores());
|
|
186
|
-
if (result.totalErrors === 0 && result.totalWarnings === 0) {
|
|
187
|
-
toast({ title: i18n.t("validationPassed") });
|
|
188
|
-
} else {
|
|
189
|
-
setValidation(result);
|
|
190
|
-
}
|
|
191
|
-
}, []);
|
|
192
|
-
|
|
193
|
-
// Jump to a diagnostic's target, then dismiss the dialog so it's visible.
|
|
194
|
-
const navigateToDiagnostic = useCallback(
|
|
195
|
-
(d: Diagnostic) => {
|
|
196
|
-
const editor = useEditorStore.getState();
|
|
197
|
-
// Project-scoped targets: open the matching sidebar tab AND select the item.
|
|
198
|
-
if (d.channelId) {
|
|
199
|
-
editor.setActiveSidebarTab("channels");
|
|
200
|
-
editor.selectChannel(d.channelId);
|
|
201
|
-
} else if (d.memoryId) {
|
|
202
|
-
editor.setActiveSidebarTab("memory");
|
|
203
|
-
editor.selectMemory(d.memoryId);
|
|
204
|
-
} else if (d.modelId) {
|
|
205
|
-
editor.setActiveSidebarTab("models");
|
|
206
|
-
editor.selectModel(d.modelId);
|
|
207
|
-
} else if (d.canvasId) {
|
|
208
|
-
// Switch first so selectGraph targets the right canvas, then select.
|
|
209
|
-
if (d.canvasId === MAIN_CANVAS_ID) editor.setActiveCanvas(MAIN_CANVAS_ID);
|
|
210
|
-
else functionsHook.openFunction(d.canvasId);
|
|
211
|
-
if (d.nodeId) editor.selectGraph([d.nodeId], []);
|
|
212
|
-
else if (d.edgeId) editor.selectGraph([], [d.edgeId]);
|
|
213
|
-
}
|
|
214
|
-
setValidation(null);
|
|
215
|
-
},
|
|
216
|
-
[functionsHook],
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
// Initial load (runs once, even under StrictMode double-mount).
|
|
220
|
-
const initialLoadDone = useRef(false);
|
|
221
|
-
useEffect(() => {
|
|
222
|
-
if (initialLoadDone.current) return;
|
|
223
|
-
initialLoadDone.current = true;
|
|
224
|
-
try {
|
|
225
|
-
if (initialMode) useEditorStore.getState().setBuilderMode(initialMode);
|
|
226
|
-
if (initialWorkflow) importProject(initialWorkflow);
|
|
227
|
-
} catch (e) {
|
|
228
|
-
onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
229
|
-
}
|
|
230
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
231
|
-
}, []);
|
|
232
|
-
|
|
233
|
-
// ── Lifecycle subscriptions ────────────────────────────────────────────
|
|
234
|
-
// Stash latest callbacks in refs so the subscription effect runs once.
|
|
235
|
-
const onChangeRef = useRef(onChange);
|
|
236
|
-
const onHistoryChangeRef = useRef(onHistoryChange);
|
|
237
|
-
onChangeRef.current = onChange;
|
|
238
|
-
onHistoryChangeRef.current = onHistoryChange;
|
|
239
|
-
|
|
240
|
-
// onChange fires on any domain change. For canvas content we watch the
|
|
241
|
-
// history middleware's `mutationCount`, which bumps on checkpoints AND
|
|
242
|
-
// undo/redo but never on selection/drag (those go through setNodes without a
|
|
243
|
-
// checkpoint). That makes onChange honest for undo/redo and silent on
|
|
244
|
-
// view-state — the thing a raw store subscription can't do, since selection
|
|
245
|
-
// lives inside the nodes array. (editorStore exposes its own `mutationCount`
|
|
246
|
-
// for project-scoped channel/memory/model edits; watched separately below.)
|
|
247
|
-
useEffect(() => {
|
|
248
|
-
const subs: Array<() => void> = [];
|
|
249
|
-
const subscribedStores = new WeakSet<CanvasStore>();
|
|
250
|
-
|
|
251
|
-
function subscribeCanvas(store: CanvasStore) {
|
|
252
|
-
if (subscribedStores.has(store)) return;
|
|
253
|
-
subscribedStores.add(store);
|
|
254
|
-
let prev = store.getState().mutationCount;
|
|
255
|
-
const unsub = store.subscribe((state) => {
|
|
256
|
-
if (state.mutationCount !== prev) {
|
|
257
|
-
prev = state.mutationCount;
|
|
258
|
-
onChangeRef.current?.();
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
subs.push(unsub);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function subscribeAllCanvases() {
|
|
265
|
-
for (const store of Object.values(getAllCanvasStores())) {
|
|
266
|
-
subscribeCanvas(store);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
subscribeAllCanvases();
|
|
271
|
-
|
|
272
|
-
// Canvas stores come and go (function add/delete, project load). Re-subscribe
|
|
273
|
-
// to the new set so newly created function bodies are watched. We do NOT fire
|
|
274
|
-
// onChange here: function add/delete/rename and all definition edits flow
|
|
275
|
-
// through editorStore.mutationCount (setFunctions), caught by the editor
|
|
276
|
-
// subscription below — so the change signal is covered without double-firing.
|
|
277
|
-
const unsubRegistry = subscribeCanvasRegistryChanges(() => {
|
|
278
|
-
subscribeAllCanvases();
|
|
279
|
-
});
|
|
280
|
-
subs.push(unsubRegistry);
|
|
281
|
-
|
|
282
|
-
// Project-scoped mutations (channels, memory, models, functions).
|
|
283
|
-
let prevEditorCount = useEditorStore.getState().mutationCount;
|
|
284
|
-
const unsubEditor = useEditorStore.subscribe((state) => {
|
|
285
|
-
if (state.mutationCount !== prevEditorCount) {
|
|
286
|
-
prevEditorCount = state.mutationCount;
|
|
287
|
-
onChangeRef.current?.();
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
subs.push(unsubEditor);
|
|
291
|
-
|
|
292
|
-
return () => {
|
|
293
|
-
for (const u of subs) u();
|
|
294
|
-
};
|
|
295
|
-
}, []);
|
|
296
|
-
|
|
297
|
-
// History-affordance subscription — emits the ACTIVE canvas's canUndo/canRedo
|
|
298
|
-
// so host chrome can drive undo/redo buttons. Distinct from onChange: a tab
|
|
299
|
-
// switch changes which history is active without being a domain mutation, so
|
|
300
|
-
// it must update buttons without marking the document dirty.
|
|
301
|
-
useEffect(() => {
|
|
302
|
-
let prevCanUndo: boolean | null = null;
|
|
303
|
-
let prevCanRedo: boolean | null = null;
|
|
304
|
-
let unsubActive: (() => void) | null = null;
|
|
305
|
-
|
|
306
|
-
const emit = () => {
|
|
307
|
-
const store = getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId);
|
|
308
|
-
const canUndo = store.canUndo();
|
|
309
|
-
const canRedo = store.canRedo();
|
|
310
|
-
if (canUndo === prevCanUndo && canRedo === prevCanRedo) return;
|
|
311
|
-
prevCanUndo = canUndo;
|
|
312
|
-
prevCanRedo = canRedo;
|
|
313
|
-
onHistoryChangeRef.current?.({ canUndo, canRedo });
|
|
314
|
-
};
|
|
315
|
-
|
|
316
|
-
// Bind to the current active canvas (a) and emit. Re-run on tab switch (b)
|
|
317
|
-
// and on store-instance rebuilds from load/clear (c) — both can change which
|
|
318
|
-
// store, or store object, is active under us.
|
|
319
|
-
const bindActive = () => {
|
|
320
|
-
unsubActive?.();
|
|
321
|
-
unsubActive = getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId).subscribe(emit);
|
|
322
|
-
emit();
|
|
323
|
-
};
|
|
324
|
-
|
|
325
|
-
bindActive();
|
|
326
|
-
|
|
327
|
-
let prevActive = useEditorStore.getState().activeCanvasId;
|
|
328
|
-
const unsubEditor = useEditorStore.subscribe((state) => {
|
|
329
|
-
if (state.activeCanvasId !== prevActive) {
|
|
330
|
-
prevActive = state.activeCanvasId;
|
|
331
|
-
bindActive(); // (b) tab switch
|
|
332
|
-
}
|
|
333
|
-
});
|
|
334
|
-
const unsubRegistry = subscribeCanvasRegistryChanges(bindActive); // (c) load/clear rebuild
|
|
335
|
-
|
|
336
|
-
return () => {
|
|
337
|
-
unsubActive?.();
|
|
338
|
-
unsubEditor();
|
|
339
|
-
unsubRegistry();
|
|
340
|
-
};
|
|
341
|
-
}, []);
|
|
342
|
-
|
|
343
|
-
// ── Imperative handle ─────────────────────────────────────────────────
|
|
344
|
-
useImperativeHandle(
|
|
345
|
-
ref,
|
|
346
|
-
(): WorkflowBuilderHandle => ({
|
|
347
|
-
loadWorkflow: (workflow) => {
|
|
348
|
-
try {
|
|
349
|
-
importProject(workflow);
|
|
350
|
-
} catch (e) {
|
|
351
|
-
onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
352
|
-
}
|
|
353
|
-
},
|
|
354
|
-
exportWorkflow: () => exportProject(),
|
|
355
|
-
clear: () => {
|
|
356
|
-
clearAllCanvasStores();
|
|
357
|
-
// Function declarations are project-scoped (not in canvas stores), so reset
|
|
358
|
-
// them explicitly alongside the cleared bodies.
|
|
359
|
-
useEditorStore.getState().setFunctions(() => ({}));
|
|
360
|
-
useEditorStore.getState().clearSelection();
|
|
361
|
-
},
|
|
362
|
-
setMode: (mode) => useEditorStore.getState().setBuilderMode(mode),
|
|
363
|
-
getMode: () => useEditorStore.getState().builderMode,
|
|
364
|
-
validate: runValidate,
|
|
365
|
-
undo: () => getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId).undo(),
|
|
366
|
-
redo: () => getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId).redo(),
|
|
367
|
-
setDebugPhase: (phase) => useDebugStore.getState().setPhase(phase),
|
|
368
|
-
}),
|
|
369
|
-
[importProject, exportProject, onError, runValidate],
|
|
370
|
-
);
|
|
371
|
-
|
|
372
|
-
// I18nextProvider scopes the builder's PRIVATE i18n instance to this subtree,
|
|
373
|
-
// so the useTranslation() consumers read it (never the host's i18next).
|
|
374
|
-
//
|
|
375
|
-
// The `fh-builder` root carries the builder's OWN base look (font,
|
|
376
|
-
// text color, antialiasing) on its own element. The builder no longer styles
|
|
377
|
-
// the host's <body> — the host owns the page. `h-full w-full` makes the
|
|
378
|
-
// builder fill whatever container it's mounted in; it never assumes the
|
|
379
|
-
// viewport. TooltipProvider + Toaster live inside the package so the embedder
|
|
380
|
-
// doesn't need to know we use Radix tooltips or shadcn toasts internally.
|
|
381
|
-
return (
|
|
382
|
-
<I18nextProvider i18n={i18n}>
|
|
383
|
-
<TooltipProvider delayDuration={300}>
|
|
384
|
-
<div className="fh-builder h-full w-full bg-background text-foreground font-sans antialiased">
|
|
385
|
-
<BuilderLayout
|
|
386
|
-
functions={functionsHook.functions}
|
|
387
|
-
onOpenFunction={functionsHook.openFunction}
|
|
388
|
-
onCreateFunction={functionsHook.createFunction}
|
|
389
|
-
canvasTabs={canvasTabs.tabs}
|
|
390
|
-
onCanvasTabChange={canvasTabs.setActiveTabId}
|
|
391
|
-
onCanvasTabClose={canvasTabs.closeTab}
|
|
392
|
-
onCanvasTabReorder={canvasTabs.reorderTabs}
|
|
393
|
-
onTestNode={onTestNode}
|
|
394
|
-
onDebugStep={onDebugStep}
|
|
395
|
-
/>
|
|
396
|
-
<Toaster />
|
|
397
|
-
{validation && (
|
|
398
|
-
<ValidationDialog
|
|
399
|
-
open
|
|
400
|
-
onOpenChange={(o) => {
|
|
401
|
-
if (!o) setValidation(null);
|
|
402
|
-
}}
|
|
403
|
-
validation={validation}
|
|
404
|
-
onSelectDiagnostic={navigateToDiagnostic}
|
|
405
|
-
/>
|
|
406
|
-
)}
|
|
407
|
-
</div>
|
|
408
|
-
</TooltipProvider>
|
|
409
|
-
</I18nextProvider>
|
|
410
|
-
);
|
|
411
|
-
},
|
|
412
|
-
);
|
|
1
|
+
import type { ApiWorkflow } from "@foresthubai/workflow-core/workflow";
|
|
2
|
+
import type { ModelInfo } from "@foresthubai/workflow-core/model";
|
|
3
|
+
import {
|
|
4
|
+
validateWorkflowState,
|
|
5
|
+
validateChannel,
|
|
6
|
+
validateMemory,
|
|
7
|
+
validateModel,
|
|
8
|
+
type Diagnostic,
|
|
9
|
+
type ValidationResult,
|
|
10
|
+
} from "@foresthubai/workflow-core/diagnostics";
|
|
11
|
+
import { forwardRef, useCallback, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from "react";
|
|
12
|
+
import { I18nextProvider } from "react-i18next";
|
|
13
|
+
|
|
14
|
+
import i18n from "./i18n";
|
|
15
|
+
import { toast } from "./hooks/use-toast";
|
|
16
|
+
import ValidationDialog from "./dialogs/ValidationDialog";
|
|
17
|
+
import { BuilderLayout } from "./BuilderLayout";
|
|
18
|
+
import { TooltipProvider } from "./components/ui/tooltip";
|
|
19
|
+
import { Toaster } from "./components/ui/toaster";
|
|
20
|
+
import { useCanvasTabs } from "./hooks/useCanvasTabs";
|
|
21
|
+
import { useResourceDiagnosticsSync } from "./hooks/useResourceDiagnosticsSync";
|
|
22
|
+
import { useFunctionDiagnosticsSync } from "./hooks/useFunctionDiagnosticsSync";
|
|
23
|
+
import { useSuppressThemeTransition } from "./hooks/useSuppressThemeTransition";
|
|
24
|
+
import { useFunctions } from "./hooks/useFunctions";
|
|
25
|
+
import { useWorkflowSerialization, readStateFromStores } from "./hooks/useWorkflowSerialization";
|
|
26
|
+
import {
|
|
27
|
+
clearAllCanvasStores,
|
|
28
|
+
getAllCanvasStores,
|
|
29
|
+
getOrCreateCanvasStore,
|
|
30
|
+
subscribeCanvasRegistryChanges,
|
|
31
|
+
MAIN_CANVAS_ID,
|
|
32
|
+
type CanvasStore,
|
|
33
|
+
} from "./stores/canvasStore";
|
|
34
|
+
import { useDebugStore, type DebugSessionPhase } from "./stores/debugStore";
|
|
35
|
+
import { useEditorStore } from "./stores/editorStore";
|
|
36
|
+
|
|
37
|
+
/** BuilderMode steers the overall behavior of the workflow builder. */
|
|
38
|
+
export type BuilderMode = { type: "edit" } | { type: "preview" } | { type: "debug" };
|
|
39
|
+
|
|
40
|
+
/** True when canvas mutations should be blocked (preview or debug). */
|
|
41
|
+
export function isReadOnly(mode: BuilderMode): boolean {
|
|
42
|
+
return mode.type !== "edit";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Type guard for preview mode. */
|
|
46
|
+
export function isPreview(mode: BuilderMode): boolean {
|
|
47
|
+
return mode.type === "preview";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// Public contract
|
|
52
|
+
// ============================================================================
|
|
53
|
+
|
|
54
|
+
export interface WorkflowBuilderProps {
|
|
55
|
+
/** Workflow loaded on mount. If none is provided, an empty workflow is created. */
|
|
56
|
+
initialWorkflow?: ApiWorkflow;
|
|
57
|
+
/** Builder mode on mount. Defaults to { type: "edit" }. */
|
|
58
|
+
initialMode?: BuilderMode;
|
|
59
|
+
/**
|
|
60
|
+
* Static model catalog — the models the llmproxy supports. Shown as the
|
|
61
|
+
* built-in options in agent model pickers. Self-hosted/custom models are
|
|
62
|
+
* declared in the Models tab instead. Defaults to [] (empty dropdown).
|
|
63
|
+
*/
|
|
64
|
+
models?: ModelInfo[];
|
|
65
|
+
/**
|
|
66
|
+
* UI language (e.g. "en", "de"). The host owns locale; the builder follows.
|
|
67
|
+
* Defaults to "en". The builder never auto-detects language.
|
|
68
|
+
*/
|
|
69
|
+
language?: string;
|
|
70
|
+
|
|
71
|
+
// ── Embedder-fulfilled actions (builder asks, embedder does) ──
|
|
72
|
+
/** A node requested embedder-side testing (e.g. Agent "Test" button). */
|
|
73
|
+
onTestNode?: (nodeId: string) => void;
|
|
74
|
+
/** Step request from the in-builder debug panel — embedder forwards to the engine. */
|
|
75
|
+
onDebugStep?: (nodeId?: string) => void;
|
|
76
|
+
|
|
77
|
+
// ── Lifecycle events ──
|
|
78
|
+
/** Fires after any domain-state mutation. Pull current state via handle.exportWorkflow(). */
|
|
79
|
+
onChange?: () => void;
|
|
80
|
+
/**
|
|
81
|
+
* Undo/redo availability for the ACTIVE canvas changed — on history mutation,
|
|
82
|
+
* undo/redo, or a tab switch (each canvas has its own history). For wiring host
|
|
83
|
+
* undo/redo buttons.
|
|
84
|
+
*/
|
|
85
|
+
onHistoryChange?: (state: { canUndo: boolean; canRedo: boolean }) => void;
|
|
86
|
+
/** Unexpected error during builder operations (e.g. failed load). */
|
|
87
|
+
onError?: (error: Error) => void;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface WorkflowBuilderHandle {
|
|
91
|
+
// State I/O
|
|
92
|
+
loadWorkflow: (workflow: ApiWorkflow) => void;
|
|
93
|
+
exportWorkflow: () => ApiWorkflow;
|
|
94
|
+
clear: () => void;
|
|
95
|
+
|
|
96
|
+
// Mode (replaces preview/debug entry-point props)
|
|
97
|
+
setMode: (mode: BuilderMode) => void;
|
|
98
|
+
getMode: () => BuilderMode;
|
|
99
|
+
|
|
100
|
+
// Initiate the in-builder validation process which will either show the validation dialog or a toast if clean.
|
|
101
|
+
validate: () => void;
|
|
102
|
+
|
|
103
|
+
// History (so embedder chrome can wire undo/redo buttons)
|
|
104
|
+
undo: () => void;
|
|
105
|
+
redo: () => void;
|
|
106
|
+
|
|
107
|
+
// Debug (embedder pushes engine events for visualization)
|
|
108
|
+
setDebugPhase: (phase: DebugSessionPhase) => void;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// ============================================================================
|
|
112
|
+
// Component — owns tabs/functions/dialogs; exposes the handle; delegates
|
|
113
|
+
// layout/canvas rendering to BuilderLayout.
|
|
114
|
+
// ============================================================================
|
|
115
|
+
|
|
116
|
+
export const WorkflowBuilder = forwardRef<WorkflowBuilderHandle, WorkflowBuilderProps>(
|
|
117
|
+
function WorkflowBuilder(props, ref) {
|
|
118
|
+
const {
|
|
119
|
+
initialWorkflow,
|
|
120
|
+
initialMode,
|
|
121
|
+
models,
|
|
122
|
+
language,
|
|
123
|
+
onTestNode,
|
|
124
|
+
onDebugStep,
|
|
125
|
+
onChange,
|
|
126
|
+
onHistoryChange,
|
|
127
|
+
onError,
|
|
128
|
+
} = props;
|
|
129
|
+
|
|
130
|
+
// Host drives locale. useLayoutEffect (not useEffect) so the language is set
|
|
131
|
+
// before paint — mounting with language="de" shows German on the first frame
|
|
132
|
+
// rather than flashing English. changeLanguage is sync here (bundled resources).
|
|
133
|
+
useLayoutEffect(() => {
|
|
134
|
+
if (language && i18n.language !== language) i18n.changeLanguage(language);
|
|
135
|
+
}, [language]);
|
|
136
|
+
|
|
137
|
+
const { importProject, exportProject } = useWorkflowSerialization();
|
|
138
|
+
|
|
139
|
+
// Color-mode toggles should snap, not fade — see hook docs.
|
|
140
|
+
useSuppressThemeTransition();
|
|
141
|
+
|
|
142
|
+
// Keep project-scoped (channel + memory + model) diagnostics in sync. Mounted
|
|
143
|
+
// once here at the root so badges survive sidebar tab open/close.
|
|
144
|
+
useResourceDiagnosticsSync({
|
|
145
|
+
selectItems: (s) => s.channels,
|
|
146
|
+
validate: validateChannel,
|
|
147
|
+
getStored: (d) => d.byChannelId,
|
|
148
|
+
set: (d, id, diags) => d.setChannelDiagnostics(id, diags),
|
|
149
|
+
clear: (d, id) => d.clearChannelDiagnostics(id),
|
|
150
|
+
});
|
|
151
|
+
useResourceDiagnosticsSync({
|
|
152
|
+
selectItems: (s) => s.memory,
|
|
153
|
+
validate: validateMemory,
|
|
154
|
+
getStored: (d) => d.byMemoryId,
|
|
155
|
+
set: (d, id, diags) => d.setMemoryDiagnostics(id, diags),
|
|
156
|
+
clear: (d, id) => d.clearMemoryDiagnostics(id),
|
|
157
|
+
});
|
|
158
|
+
useResourceDiagnosticsSync({
|
|
159
|
+
selectItems: (s) => s.models,
|
|
160
|
+
validate: validateModel,
|
|
161
|
+
getStored: (d) => d.byModelId,
|
|
162
|
+
set: (d, id, diags) => d.setModelDiagnostics(id, diags),
|
|
163
|
+
clear: (d, id) => d.clearModelDiagnostics(id),
|
|
164
|
+
});
|
|
165
|
+
// Functions are a FunctionDeclaration (not a flat resource bag), so they use a
|
|
166
|
+
// dedicated diagnostics sync.
|
|
167
|
+
useFunctionDiagnosticsSync();
|
|
168
|
+
|
|
169
|
+
// Push the embedder-supplied model catalog into the store so agent model
|
|
170
|
+
// pickers can read it. Catalog is config (not workflow content), so this
|
|
171
|
+
// never fires onChange.
|
|
172
|
+
useEffect(() => {
|
|
173
|
+
useEditorStore.getState().setAvailableModels(models ?? []);
|
|
174
|
+
}, [models]);
|
|
175
|
+
|
|
176
|
+
// Canvas tabs + functions live here because they survive canvas switches.
|
|
177
|
+
const canvasTabs = useCanvasTabs();
|
|
178
|
+
const functionsHook = useFunctions({ onOpenTab: canvasTabs.openTab });
|
|
179
|
+
|
|
180
|
+
// Built-in validation UX. validate() presents the result itself rather than
|
|
181
|
+
// returning it: a success toast when clean, else this dialog. Non-null = open.
|
|
182
|
+
const [validation, setValidation] = useState<ValidationResult | null>(null);
|
|
183
|
+
|
|
184
|
+
const runValidate = useCallback(() => {
|
|
185
|
+
const result = validateWorkflowState(readStateFromStores());
|
|
186
|
+
if (result.totalErrors === 0 && result.totalWarnings === 0) {
|
|
187
|
+
toast({ title: i18n.t("validationPassed") });
|
|
188
|
+
} else {
|
|
189
|
+
setValidation(result);
|
|
190
|
+
}
|
|
191
|
+
}, []);
|
|
192
|
+
|
|
193
|
+
// Jump to a diagnostic's target, then dismiss the dialog so it's visible.
|
|
194
|
+
const navigateToDiagnostic = useCallback(
|
|
195
|
+
(d: Diagnostic) => {
|
|
196
|
+
const editor = useEditorStore.getState();
|
|
197
|
+
// Project-scoped targets: open the matching sidebar tab AND select the item.
|
|
198
|
+
if (d.channelId) {
|
|
199
|
+
editor.setActiveSidebarTab("channels");
|
|
200
|
+
editor.selectChannel(d.channelId);
|
|
201
|
+
} else if (d.memoryId) {
|
|
202
|
+
editor.setActiveSidebarTab("memory");
|
|
203
|
+
editor.selectMemory(d.memoryId);
|
|
204
|
+
} else if (d.modelId) {
|
|
205
|
+
editor.setActiveSidebarTab("models");
|
|
206
|
+
editor.selectModel(d.modelId);
|
|
207
|
+
} else if (d.canvasId) {
|
|
208
|
+
// Switch first so selectGraph targets the right canvas, then select.
|
|
209
|
+
if (d.canvasId === MAIN_CANVAS_ID) editor.setActiveCanvas(MAIN_CANVAS_ID);
|
|
210
|
+
else functionsHook.openFunction(d.canvasId);
|
|
211
|
+
if (d.nodeId) editor.selectGraph([d.nodeId], []);
|
|
212
|
+
else if (d.edgeId) editor.selectGraph([], [d.edgeId]);
|
|
213
|
+
}
|
|
214
|
+
setValidation(null);
|
|
215
|
+
},
|
|
216
|
+
[functionsHook],
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
// Initial load (runs once, even under StrictMode double-mount).
|
|
220
|
+
const initialLoadDone = useRef(false);
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
if (initialLoadDone.current) return;
|
|
223
|
+
initialLoadDone.current = true;
|
|
224
|
+
try {
|
|
225
|
+
if (initialMode) useEditorStore.getState().setBuilderMode(initialMode);
|
|
226
|
+
if (initialWorkflow) importProject(initialWorkflow);
|
|
227
|
+
} catch (e) {
|
|
228
|
+
onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
229
|
+
}
|
|
230
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
231
|
+
}, []);
|
|
232
|
+
|
|
233
|
+
// ── Lifecycle subscriptions ────────────────────────────────────────────
|
|
234
|
+
// Stash latest callbacks in refs so the subscription effect runs once.
|
|
235
|
+
const onChangeRef = useRef(onChange);
|
|
236
|
+
const onHistoryChangeRef = useRef(onHistoryChange);
|
|
237
|
+
onChangeRef.current = onChange;
|
|
238
|
+
onHistoryChangeRef.current = onHistoryChange;
|
|
239
|
+
|
|
240
|
+
// onChange fires on any domain change. For canvas content we watch the
|
|
241
|
+
// history middleware's `mutationCount`, which bumps on checkpoints AND
|
|
242
|
+
// undo/redo but never on selection/drag (those go through setNodes without a
|
|
243
|
+
// checkpoint). That makes onChange honest for undo/redo and silent on
|
|
244
|
+
// view-state — the thing a raw store subscription can't do, since selection
|
|
245
|
+
// lives inside the nodes array. (editorStore exposes its own `mutationCount`
|
|
246
|
+
// for project-scoped channel/memory/model edits; watched separately below.)
|
|
247
|
+
useEffect(() => {
|
|
248
|
+
const subs: Array<() => void> = [];
|
|
249
|
+
const subscribedStores = new WeakSet<CanvasStore>();
|
|
250
|
+
|
|
251
|
+
function subscribeCanvas(store: CanvasStore) {
|
|
252
|
+
if (subscribedStores.has(store)) return;
|
|
253
|
+
subscribedStores.add(store);
|
|
254
|
+
let prev = store.getState().mutationCount;
|
|
255
|
+
const unsub = store.subscribe((state) => {
|
|
256
|
+
if (state.mutationCount !== prev) {
|
|
257
|
+
prev = state.mutationCount;
|
|
258
|
+
onChangeRef.current?.();
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
subs.push(unsub);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function subscribeAllCanvases() {
|
|
265
|
+
for (const store of Object.values(getAllCanvasStores())) {
|
|
266
|
+
subscribeCanvas(store);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
subscribeAllCanvases();
|
|
271
|
+
|
|
272
|
+
// Canvas stores come and go (function add/delete, project load). Re-subscribe
|
|
273
|
+
// to the new set so newly created function bodies are watched. We do NOT fire
|
|
274
|
+
// onChange here: function add/delete/rename and all definition edits flow
|
|
275
|
+
// through editorStore.mutationCount (setFunctions), caught by the editor
|
|
276
|
+
// subscription below — so the change signal is covered without double-firing.
|
|
277
|
+
const unsubRegistry = subscribeCanvasRegistryChanges(() => {
|
|
278
|
+
subscribeAllCanvases();
|
|
279
|
+
});
|
|
280
|
+
subs.push(unsubRegistry);
|
|
281
|
+
|
|
282
|
+
// Project-scoped mutations (channels, memory, models, functions).
|
|
283
|
+
let prevEditorCount = useEditorStore.getState().mutationCount;
|
|
284
|
+
const unsubEditor = useEditorStore.subscribe((state) => {
|
|
285
|
+
if (state.mutationCount !== prevEditorCount) {
|
|
286
|
+
prevEditorCount = state.mutationCount;
|
|
287
|
+
onChangeRef.current?.();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
subs.push(unsubEditor);
|
|
291
|
+
|
|
292
|
+
return () => {
|
|
293
|
+
for (const u of subs) u();
|
|
294
|
+
};
|
|
295
|
+
}, []);
|
|
296
|
+
|
|
297
|
+
// History-affordance subscription — emits the ACTIVE canvas's canUndo/canRedo
|
|
298
|
+
// so host chrome can drive undo/redo buttons. Distinct from onChange: a tab
|
|
299
|
+
// switch changes which history is active without being a domain mutation, so
|
|
300
|
+
// it must update buttons without marking the document dirty.
|
|
301
|
+
useEffect(() => {
|
|
302
|
+
let prevCanUndo: boolean | null = null;
|
|
303
|
+
let prevCanRedo: boolean | null = null;
|
|
304
|
+
let unsubActive: (() => void) | null = null;
|
|
305
|
+
|
|
306
|
+
const emit = () => {
|
|
307
|
+
const store = getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId);
|
|
308
|
+
const canUndo = store.canUndo();
|
|
309
|
+
const canRedo = store.canRedo();
|
|
310
|
+
if (canUndo === prevCanUndo && canRedo === prevCanRedo) return;
|
|
311
|
+
prevCanUndo = canUndo;
|
|
312
|
+
prevCanRedo = canRedo;
|
|
313
|
+
onHistoryChangeRef.current?.({ canUndo, canRedo });
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// Bind to the current active canvas (a) and emit. Re-run on tab switch (b)
|
|
317
|
+
// and on store-instance rebuilds from load/clear (c) — both can change which
|
|
318
|
+
// store, or store object, is active under us.
|
|
319
|
+
const bindActive = () => {
|
|
320
|
+
unsubActive?.();
|
|
321
|
+
unsubActive = getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId).subscribe(emit);
|
|
322
|
+
emit();
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
bindActive();
|
|
326
|
+
|
|
327
|
+
let prevActive = useEditorStore.getState().activeCanvasId;
|
|
328
|
+
const unsubEditor = useEditorStore.subscribe((state) => {
|
|
329
|
+
if (state.activeCanvasId !== prevActive) {
|
|
330
|
+
prevActive = state.activeCanvasId;
|
|
331
|
+
bindActive(); // (b) tab switch
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
const unsubRegistry = subscribeCanvasRegistryChanges(bindActive); // (c) load/clear rebuild
|
|
335
|
+
|
|
336
|
+
return () => {
|
|
337
|
+
unsubActive?.();
|
|
338
|
+
unsubEditor();
|
|
339
|
+
unsubRegistry();
|
|
340
|
+
};
|
|
341
|
+
}, []);
|
|
342
|
+
|
|
343
|
+
// ── Imperative handle ─────────────────────────────────────────────────
|
|
344
|
+
useImperativeHandle(
|
|
345
|
+
ref,
|
|
346
|
+
(): WorkflowBuilderHandle => ({
|
|
347
|
+
loadWorkflow: (workflow) => {
|
|
348
|
+
try {
|
|
349
|
+
importProject(workflow);
|
|
350
|
+
} catch (e) {
|
|
351
|
+
onError?.(e instanceof Error ? e : new Error(String(e)));
|
|
352
|
+
}
|
|
353
|
+
},
|
|
354
|
+
exportWorkflow: () => exportProject(),
|
|
355
|
+
clear: () => {
|
|
356
|
+
clearAllCanvasStores();
|
|
357
|
+
// Function declarations are project-scoped (not in canvas stores), so reset
|
|
358
|
+
// them explicitly alongside the cleared bodies.
|
|
359
|
+
useEditorStore.getState().setFunctions(() => ({}));
|
|
360
|
+
useEditorStore.getState().clearSelection();
|
|
361
|
+
},
|
|
362
|
+
setMode: (mode) => useEditorStore.getState().setBuilderMode(mode),
|
|
363
|
+
getMode: () => useEditorStore.getState().builderMode,
|
|
364
|
+
validate: runValidate,
|
|
365
|
+
undo: () => getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId).undo(),
|
|
366
|
+
redo: () => getOrCreateCanvasStore(useEditorStore.getState().activeCanvasId).redo(),
|
|
367
|
+
setDebugPhase: (phase) => useDebugStore.getState().setPhase(phase),
|
|
368
|
+
}),
|
|
369
|
+
[importProject, exportProject, onError, runValidate],
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
// I18nextProvider scopes the builder's PRIVATE i18n instance to this subtree,
|
|
373
|
+
// so the useTranslation() consumers read it (never the host's i18next).
|
|
374
|
+
//
|
|
375
|
+
// The `fh-builder` root carries the builder's OWN base look (font,
|
|
376
|
+
// text color, antialiasing) on its own element. The builder no longer styles
|
|
377
|
+
// the host's <body> — the host owns the page. `h-full w-full` makes the
|
|
378
|
+
// builder fill whatever container it's mounted in; it never assumes the
|
|
379
|
+
// viewport. TooltipProvider + Toaster live inside the package so the embedder
|
|
380
|
+
// doesn't need to know we use Radix tooltips or shadcn toasts internally.
|
|
381
|
+
return (
|
|
382
|
+
<I18nextProvider i18n={i18n}>
|
|
383
|
+
<TooltipProvider delayDuration={300}>
|
|
384
|
+
<div className="fh-builder h-full w-full bg-background text-foreground font-sans antialiased">
|
|
385
|
+
<BuilderLayout
|
|
386
|
+
functions={functionsHook.functions}
|
|
387
|
+
onOpenFunction={functionsHook.openFunction}
|
|
388
|
+
onCreateFunction={functionsHook.createFunction}
|
|
389
|
+
canvasTabs={canvasTabs.tabs}
|
|
390
|
+
onCanvasTabChange={canvasTabs.setActiveTabId}
|
|
391
|
+
onCanvasTabClose={canvasTabs.closeTab}
|
|
392
|
+
onCanvasTabReorder={canvasTabs.reorderTabs}
|
|
393
|
+
onTestNode={onTestNode}
|
|
394
|
+
onDebugStep={onDebugStep}
|
|
395
|
+
/>
|
|
396
|
+
<Toaster />
|
|
397
|
+
{validation && (
|
|
398
|
+
<ValidationDialog
|
|
399
|
+
open
|
|
400
|
+
onOpenChange={(o) => {
|
|
401
|
+
if (!o) setValidation(null);
|
|
402
|
+
}}
|
|
403
|
+
validation={validation}
|
|
404
|
+
onSelectDiagnostic={navigateToDiagnostic}
|
|
405
|
+
/>
|
|
406
|
+
)}
|
|
407
|
+
</div>
|
|
408
|
+
</TooltipProvider>
|
|
409
|
+
</I18nextProvider>
|
|
410
|
+
);
|
|
411
|
+
},
|
|
412
|
+
);
|