@foresthubai/workflow-builder 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/LICENSE +661 -661
  2. package/NOTICE +16 -16
  3. package/README.md +110 -110
  4. package/dist/components/ui/command.d.ts +2 -2
  5. package/dist/components/ui/input.d.ts +1 -1
  6. package/dist/components/ui/resizable.d.ts +1 -1
  7. package/dist/components/ui/textarea.d.ts +1 -1
  8. package/dist/graph/BaseNode.js +10 -10
  9. package/dist/graph/reactFlowRegistry.d.ts.map +1 -1
  10. package/dist/hooks/useWorkflowSerialization.d.ts.map +1 -1
  11. package/dist/hooks/useWorkflowSerialization.js +8 -11
  12. package/dist/hooks/useWorkflowSerialization.js.map +1 -1
  13. package/package.json +2 -2
  14. package/src/BuilderLayout.tsx +345 -345
  15. package/src/Canvas.tsx +261 -261
  16. package/src/CanvasEditor.tsx +142 -142
  17. package/src/CanvasTabsToolbar.tsx +176 -176
  18. package/src/RightConfigPanel.tsx +266 -266
  19. package/src/WorkflowBuilder.tsx +408 -408
  20. package/src/cn.ts +6 -6
  21. package/src/components/ui/add-button.tsx +39 -39
  22. package/src/components/ui/alert-dialog.tsx +141 -141
  23. package/src/components/ui/alert.tsx +59 -59
  24. package/src/components/ui/badge.tsx +36 -36
  25. package/src/components/ui/button.tsx +85 -85
  26. package/src/components/ui/card.tsx +79 -79
  27. package/src/components/ui/checkbox.tsx +28 -28
  28. package/src/components/ui/collapsible.tsx +9 -9
  29. package/src/components/ui/command.tsx +153 -153
  30. package/src/components/ui/delete-button.tsx +23 -23
  31. package/src/components/ui/dialog.tsx +125 -125
  32. package/src/components/ui/dropdown-menu.tsx +198 -198
  33. package/src/components/ui/input.tsx +55 -55
  34. package/src/components/ui/label.tsx +24 -24
  35. package/src/components/ui/readonly-banner.tsx +15 -15
  36. package/src/components/ui/resizable.tsx +43 -43
  37. package/src/components/ui/scroll-area.tsx +102 -102
  38. package/src/components/ui/select.tsx +160 -160
  39. package/src/components/ui/separator.tsx +29 -29
  40. package/src/components/ui/switch.tsx +27 -27
  41. package/src/components/ui/textarea.tsx +51 -51
  42. package/src/components/ui/toast.tsx +127 -127
  43. package/src/components/ui/toaster.tsx +33 -33
  44. package/src/components/ui/toggle-group.tsx +59 -59
  45. package/src/components/ui/toggle.tsx +43 -43
  46. package/src/components/ui/tooltip.tsx +32 -32
  47. package/src/dialogs/NodePickerDialog.tsx +84 -84
  48. package/src/dialogs/ValidationDialog.tsx +184 -184
  49. package/src/graph/BaseNode.tsx +557 -557
  50. package/src/graph/CustomEdge.tsx +185 -185
  51. package/src/graph/CustomNode.tsx +16 -16
  52. package/src/graph/FunctionCallNode.tsx +30 -30
  53. package/src/graph/PortHandle.tsx +189 -189
  54. package/src/graph/reactFlowRegistry.ts +26 -26
  55. package/src/hooks/use-toast.ts +125 -125
  56. package/src/hooks/useAvailableVariables.ts +20 -20
  57. package/src/hooks/useCanvasHistory.ts +22 -22
  58. package/src/hooks/useCanvasTabs.ts +168 -168
  59. package/src/hooks/useFunctionDiagnosticsSync.ts +40 -40
  60. package/src/hooks/useFunctionRegistry.ts +26 -26
  61. package/src/hooks/useFunctions.ts +44 -44
  62. package/src/hooks/useGraph.ts +161 -161
  63. package/src/hooks/useNodeDefinitions.ts +82 -82
  64. package/src/hooks/useParamErrors.ts +26 -26
  65. package/src/hooks/useResolvedTheme.ts +30 -30
  66. package/src/hooks/useResourceDiagnosticsSync.ts +58 -58
  67. package/src/hooks/useSuppressThemeTransition.ts +79 -79
  68. package/src/hooks/useWorkflowSerialization.ts +141 -144
  69. package/src/i18n/index.ts +53 -53
  70. package/src/i18n/locales/de.json +501 -501
  71. package/src/i18n/locales/en.json +557 -557
  72. package/src/index.ts +27 -27
  73. package/src/inputs/ExpressionInput.tsx +297 -297
  74. package/src/inputs/ParameterEditor.tsx +515 -515
  75. package/src/inputs/PortSection.tsx +144 -144
  76. package/src/panels/BuilderSidebar.tsx +301 -301
  77. package/src/panels/ChannelConfigPanel.tsx +49 -49
  78. package/src/panels/ChannelsPanel.tsx +28 -28
  79. package/src/panels/DebugConsolePanel.tsx +73 -73
  80. package/src/panels/DebugContextPanel.tsx +77 -77
  81. package/src/panels/DebugExternalIOPanel.tsx +180 -180
  82. package/src/panels/DiagnosticsPanel.tsx +170 -170
  83. package/src/panels/EdgeConfigPanel.tsx +104 -104
  84. package/src/panels/FunctionConfigPanel.tsx +179 -179
  85. package/src/panels/FunctionListPanel.tsx +45 -45
  86. package/src/panels/MemoryConfigPanel.tsx +55 -55
  87. package/src/panels/MemoryPanel.tsx +40 -40
  88. package/src/panels/ModelConfigPanel.tsx +41 -41
  89. package/src/panels/ModelsPanel.tsx +36 -36
  90. package/src/panels/NodeConfigPanel.tsx +630 -630
  91. package/src/panels/NodeLibrary.tsx +288 -288
  92. package/src/panels/ResourceConfigPanel.tsx +132 -132
  93. package/src/panels/ResourceListPanel.tsx +113 -113
  94. package/src/panels/VariableConfigPanel.tsx +161 -161
  95. package/src/panels/VariablesPanel.tsx +145 -145
  96. package/src/stores/canvasStore.test.ts +44 -44
  97. package/src/stores/canvasStore.ts +245 -245
  98. package/src/stores/debugStore.ts +74 -74
  99. package/src/stores/diagnosticsStore.ts +130 -130
  100. package/src/stores/editorStore.ts +190 -190
  101. package/src/styles/index.css +526 -526
  102. package/src/utils/categoryConstants.ts +26 -26
  103. package/src/utils/channelOperations.ts +86 -86
  104. package/src/utils/connectionRules.ts +137 -137
  105. package/src/utils/functionOperations.ts +179 -179
  106. package/src/utils/graphOperations.ts +550 -550
  107. package/src/utils/history.ts +207 -207
  108. package/src/utils/memoryOperations.ts +57 -57
  109. package/src/utils/migrateFunctionNodes.ts +107 -107
  110. package/src/utils/modelOperations.ts +55 -55
  111. package/src/utils/paramDisplay.ts +71 -71
  112. package/src/utils/resourceHelpers.ts +32 -32
  113. package/src/utils/translation.ts +28 -28
  114. package/src/utils/variableOperations.ts +75 -75
  115. package/tailwind-preset.ts +166 -166
  116. package/dist/lib/utils.d.ts +0 -3
  117. package/dist/lib/utils.d.ts.map +0 -1
  118. package/dist/lib/utils.js +0 -6
  119. package/dist/lib/utils.js.map +0 -1
  120. package/dist/toolbars/CanvasTabsToolbar.d.ts +0 -11
  121. package/dist/toolbars/CanvasTabsToolbar.d.ts.map +0 -1
  122. package/dist/toolbars/CanvasTabsToolbar.js +0 -101
  123. package/dist/toolbars/CanvasTabsToolbar.js.map +0 -1
@@ -1,161 +1,161 @@
1
- import { useEffect, useState } from "react";
2
- import { useTranslation } from "react-i18next";
3
- import { Button } from "../components/ui/button";
4
- import { Input } from "../components/ui/input";
5
- import { Separator } from "../components/ui/separator";
6
- import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../components/ui/select";
7
- import { ChevronRight } from "lucide-react";
8
- import type { DeclaredVariable } from "@foresthubai/workflow-core/variable";
9
- import type { DataType } from "@foresthubai/workflow-core";
10
- import { useEditorStore } from "../stores/editorStore";
11
- import { isReadOnly } from "../WorkflowBuilder";
12
- import { ReadOnlyBanner } from "../components/ui/readonly-banner";
13
- import { DeleteButton } from "../components/ui/delete-button";
14
- import { deleteDeclaredVariable, setDeclaredVariableType, updateDeclaredVariable } from "../utils/variableOperations";
15
-
16
- interface VariableConfigPanelProps {
17
- canvasId: string;
18
- variable: DeclaredVariable;
19
- onClose: () => void;
20
- }
21
-
22
- const DATA_TYPES: DataType[] = ["int", "float", "bool", "string"];
23
-
24
- export const VariableConfigPanel = ({ canvasId, variable, onClose }: VariableConfigPanelProps) => {
25
- const { t } = useTranslation();
26
- const readOnly = useEditorStore((s) => isReadOnly(s.builderMode));
27
-
28
- // Local name state mirrors the other config panels — preserves cursor position
29
- // while typing and resets when a different variable is opened.
30
- const [localName, setLocalName] = useState(variable.name);
31
- useEffect(() => {
32
- setLocalName(variable.name);
33
- }, [variable.uid, variable.name]);
34
-
35
- const isEmptyName = variable.name.trim() === "";
36
-
37
- // The initial-value widget is chosen by dataType: declared variables store
38
- // `initialValue?: unknown` and the type is enforced here at the input layer,
39
- // not in the data model (untyped DOM input + JSON round-trip would defeat a
40
- // discriminated union). Switching dataType clears the value.
41
- const renderInitialValueInput = () => {
42
- switch (variable.dataType) {
43
- case "bool":
44
- return (
45
- <Select
46
- value={variable.initialValue != null ? String(variable.initialValue) : "false"}
47
- onValueChange={(v) => updateDeclaredVariable(canvasId, variable.uid, { initialValue: v === "true" })}
48
- >
49
- <SelectTrigger className="h-8 text-sm">
50
- <SelectValue />
51
- </SelectTrigger>
52
- <SelectContent>
53
- <SelectItem value="false">false</SelectItem>
54
- <SelectItem value="true">true</SelectItem>
55
- </SelectContent>
56
- </Select>
57
- );
58
- case "string":
59
- return (
60
- <Input
61
- className="h-8 text-sm"
62
- value={(variable.initialValue as string) ?? ""}
63
- onChange={(e) => updateDeclaredVariable(canvasId, variable.uid, { initialValue: e.target.value })}
64
- placeholder='""'
65
- />
66
- );
67
- case "int":
68
- case "float":
69
- return (
70
- <Input
71
- type="number"
72
- step={variable.dataType === "float" ? "any" : 1}
73
- className="h-8 text-sm"
74
- value={variable.initialValue != null ? Number(variable.initialValue) : ""}
75
- onChange={(e) => {
76
- const num = variable.dataType === "float" ? parseFloat(e.target.value) : parseInt(e.target.value, 10);
77
- updateDeclaredVariable(canvasId, variable.uid, { initialValue: isNaN(num) ? undefined : num });
78
- }}
79
- placeholder="0"
80
- />
81
- );
82
- default:
83
- return null;
84
- }
85
- };
86
-
87
- return (
88
- <div className="p-4">
89
- <div className="space-y-4">
90
- <div className="flex items-center justify-between gap-2">
91
- <div className="flex-1 min-w-0">
92
- <div className="group flex items-center gap-1.5 rounded-md border border-transparent px-1.5 -mx-1.5 hover:border-input focus-within:border-input transition-colors">
93
- <input
94
- type="text"
95
- title={t("variableName", "Variable name")}
96
- className="font-semibold text-lg font-mono bg-transparent w-full outline-none cursor-text py-0.5"
97
- value={localName}
98
- readOnly={readOnly}
99
- onChange={(e) => {
100
- setLocalName(e.target.value);
101
- updateDeclaredVariable(canvasId, variable.uid, { name: e.target.value });
102
- }}
103
- />
104
- </div>
105
- <p className="text-sm text-muted-foreground">
106
- {t("variableDescription", "A declared variable you can read and write across this canvas")}
107
- </p>
108
- {isEmptyName && (
109
- <p className="text-xs text-destructive mt-1">{t("variableNameRequired", "Name is required")}</p>
110
- )}
111
- </div>
112
- <Button variant="ghost" size="icon" className="shrink-0" onClick={onClose}>
113
- <ChevronRight className="h-4 w-4" />
114
- </Button>
115
- </div>
116
-
117
- {readOnly && <ReadOnlyBanner />}
118
-
119
- <Separator />
120
-
121
- <div className={`space-y-4 ${readOnly ? "pointer-events-none opacity-60" : ""}`}>
122
- <div className="space-y-1.5">
123
- <label className="text-xs font-medium text-foreground/80">{t("dataType", "Data type")}</label>
124
- <Select
125
- value={variable.dataType}
126
- onValueChange={(v) => setDeclaredVariableType(canvasId, variable.uid, v as DataType)}
127
- >
128
- <SelectTrigger className="h-8 text-sm">
129
- <SelectValue />
130
- </SelectTrigger>
131
- <SelectContent>
132
- {DATA_TYPES.map((dt) => (
133
- <SelectItem key={dt} value={dt}>
134
- {dt}
135
- </SelectItem>
136
- ))}
137
- </SelectContent>
138
- </Select>
139
- </div>
140
-
141
- <div className="space-y-1.5">
142
- <label className="text-xs font-medium text-foreground/80">
143
- {t("initialValue")}{" "}
144
- <span className="font-normal text-muted-foreground">({t("optional", "optional")})</span>
145
- </label>
146
- {renderInitialValueInput()}
147
- </div>
148
- </div>
149
-
150
- {!readOnly && (
151
- <>
152
- <Separator />
153
- <DeleteButton onClick={() => deleteDeclaredVariable(canvasId, variable.uid)}>
154
- {t("deleteVariable", "Delete variable")}
155
- </DeleteButton>
156
- </>
157
- )}
158
- </div>
159
- </div>
160
- );
161
- };
1
+ import { useEffect, useState } from "react";
2
+ import { useTranslation } from "react-i18next";
3
+ import { Button } from "../components/ui/button";
4
+ import { Input } from "../components/ui/input";
5
+ import { Separator } from "../components/ui/separator";
6
+ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "../components/ui/select";
7
+ import { ChevronRight } from "lucide-react";
8
+ import type { DeclaredVariable } from "@foresthubai/workflow-core/variable";
9
+ import type { DataType } from "@foresthubai/workflow-core";
10
+ import { useEditorStore } from "../stores/editorStore";
11
+ import { isReadOnly } from "../WorkflowBuilder";
12
+ import { ReadOnlyBanner } from "../components/ui/readonly-banner";
13
+ import { DeleteButton } from "../components/ui/delete-button";
14
+ import { deleteDeclaredVariable, setDeclaredVariableType, updateDeclaredVariable } from "../utils/variableOperations";
15
+
16
+ interface VariableConfigPanelProps {
17
+ canvasId: string;
18
+ variable: DeclaredVariable;
19
+ onClose: () => void;
20
+ }
21
+
22
+ const DATA_TYPES: DataType[] = ["int", "float", "bool", "string"];
23
+
24
+ export const VariableConfigPanel = ({ canvasId, variable, onClose }: VariableConfigPanelProps) => {
25
+ const { t } = useTranslation();
26
+ const readOnly = useEditorStore((s) => isReadOnly(s.builderMode));
27
+
28
+ // Local name state mirrors the other config panels — preserves cursor position
29
+ // while typing and resets when a different variable is opened.
30
+ const [localName, setLocalName] = useState(variable.name);
31
+ useEffect(() => {
32
+ setLocalName(variable.name);
33
+ }, [variable.uid, variable.name]);
34
+
35
+ const isEmptyName = variable.name.trim() === "";
36
+
37
+ // The initial-value widget is chosen by dataType: declared variables store
38
+ // `initialValue?: unknown` and the type is enforced here at the input layer,
39
+ // not in the data model (untyped DOM input + JSON round-trip would defeat a
40
+ // discriminated union). Switching dataType clears the value.
41
+ const renderInitialValueInput = () => {
42
+ switch (variable.dataType) {
43
+ case "bool":
44
+ return (
45
+ <Select
46
+ value={variable.initialValue != null ? String(variable.initialValue) : "false"}
47
+ onValueChange={(v) => updateDeclaredVariable(canvasId, variable.uid, { initialValue: v === "true" })}
48
+ >
49
+ <SelectTrigger className="h-8 text-sm">
50
+ <SelectValue />
51
+ </SelectTrigger>
52
+ <SelectContent>
53
+ <SelectItem value="false">false</SelectItem>
54
+ <SelectItem value="true">true</SelectItem>
55
+ </SelectContent>
56
+ </Select>
57
+ );
58
+ case "string":
59
+ return (
60
+ <Input
61
+ className="h-8 text-sm"
62
+ value={(variable.initialValue as string) ?? ""}
63
+ onChange={(e) => updateDeclaredVariable(canvasId, variable.uid, { initialValue: e.target.value })}
64
+ placeholder='""'
65
+ />
66
+ );
67
+ case "int":
68
+ case "float":
69
+ return (
70
+ <Input
71
+ type="number"
72
+ step={variable.dataType === "float" ? "any" : 1}
73
+ className="h-8 text-sm"
74
+ value={variable.initialValue != null ? Number(variable.initialValue) : ""}
75
+ onChange={(e) => {
76
+ const num = variable.dataType === "float" ? parseFloat(e.target.value) : parseInt(e.target.value, 10);
77
+ updateDeclaredVariable(canvasId, variable.uid, { initialValue: isNaN(num) ? undefined : num });
78
+ }}
79
+ placeholder="0"
80
+ />
81
+ );
82
+ default:
83
+ return null;
84
+ }
85
+ };
86
+
87
+ return (
88
+ <div className="p-4">
89
+ <div className="space-y-4">
90
+ <div className="flex items-center justify-between gap-2">
91
+ <div className="flex-1 min-w-0">
92
+ <div className="group flex items-center gap-1.5 rounded-md border border-transparent px-1.5 -mx-1.5 hover:border-input focus-within:border-input transition-colors">
93
+ <input
94
+ type="text"
95
+ title={t("variableName", "Variable name")}
96
+ className="font-semibold text-lg font-mono bg-transparent w-full outline-none cursor-text py-0.5"
97
+ value={localName}
98
+ readOnly={readOnly}
99
+ onChange={(e) => {
100
+ setLocalName(e.target.value);
101
+ updateDeclaredVariable(canvasId, variable.uid, { name: e.target.value });
102
+ }}
103
+ />
104
+ </div>
105
+ <p className="text-sm text-muted-foreground">
106
+ {t("variableDescription", "A declared variable you can read and write across this canvas")}
107
+ </p>
108
+ {isEmptyName && (
109
+ <p className="text-xs text-destructive mt-1">{t("variableNameRequired", "Name is required")}</p>
110
+ )}
111
+ </div>
112
+ <Button variant="ghost" size="icon" className="shrink-0" onClick={onClose}>
113
+ <ChevronRight className="h-4 w-4" />
114
+ </Button>
115
+ </div>
116
+
117
+ {readOnly && <ReadOnlyBanner />}
118
+
119
+ <Separator />
120
+
121
+ <div className={`space-y-4 ${readOnly ? "pointer-events-none opacity-60" : ""}`}>
122
+ <div className="space-y-1.5">
123
+ <label className="text-xs font-medium text-foreground/80">{t("dataType", "Data type")}</label>
124
+ <Select
125
+ value={variable.dataType}
126
+ onValueChange={(v) => setDeclaredVariableType(canvasId, variable.uid, v as DataType)}
127
+ >
128
+ <SelectTrigger className="h-8 text-sm">
129
+ <SelectValue />
130
+ </SelectTrigger>
131
+ <SelectContent>
132
+ {DATA_TYPES.map((dt) => (
133
+ <SelectItem key={dt} value={dt}>
134
+ {dt}
135
+ </SelectItem>
136
+ ))}
137
+ </SelectContent>
138
+ </Select>
139
+ </div>
140
+
141
+ <div className="space-y-1.5">
142
+ <label className="text-xs font-medium text-foreground/80">
143
+ {t("initialValue")}{" "}
144
+ <span className="font-normal text-muted-foreground">({t("optional", "optional")})</span>
145
+ </label>
146
+ {renderInitialValueInput()}
147
+ </div>
148
+ </div>
149
+
150
+ {!readOnly && (
151
+ <>
152
+ <Separator />
153
+ <DeleteButton onClick={() => deleteDeclaredVariable(canvasId, variable.uid)}>
154
+ {t("deleteVariable", "Delete variable")}
155
+ </DeleteButton>
156
+ </>
157
+ )}
158
+ </div>
159
+ </div>
160
+ );
161
+ };
@@ -1,145 +1,145 @@
1
- import React from "react";
2
- import { useTranslation } from "react-i18next";
3
- import { AddButton } from "../components/ui/add-button";
4
- import { Variable as VariableIcon } from "lucide-react";
5
- import { cn } from "../cn";
6
- import { useEditorStore } from "../stores/editorStore";
7
- import { isReadOnly } from "../WorkflowBuilder";
8
- import { useAvailableVariables } from "../hooks/useAvailableVariables";
9
- import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "../stores/canvasStore";
10
- import { type Variable, type DeclaredVariable } from "@foresthubai/workflow-core/variable";
11
- import { addDeclaredVariable } from "../utils/variableOperations";
12
-
13
- interface VariablesPanelProps {
14
- canvasId: string;
15
- onSelectNode: (nodeId: string) => void;
16
- }
17
-
18
- export const VariablesPanel = ({ canvasId, onSelectNode }: VariablesPanelProps) => {
19
- const readOnly = useEditorStore((s) => isReadOnly(s.builderMode));
20
- const { t } = useTranslation();
21
- const { list: variables } = useAvailableVariables(canvasId);
22
- const selection = useEditorStore((s) => s.selection);
23
- const selectVariable = useEditorStore((s) => s.selectVariable);
24
-
25
- const store = getOrCreateCanvasStore(canvasId);
26
- const allVariables = store((s) => s.variables);
27
- const isMainCanvas = canvasId === MAIN_CANVAS_ID;
28
-
29
- // Extract declared variables from the unified record
30
- const declaredVariables = React.useMemo(() => {
31
- const result: { uid: string; var: DeclaredVariable }[] = [];
32
- for (const v of Object.values(allVariables)) {
33
- if (v.kind === "declared") {
34
- result.push({ uid: v.uid, var: v });
35
- }
36
- }
37
- return result;
38
- }, [allVariables]);
39
-
40
- // Create a declared variable and immediately open its config panel.
41
- const handleAddVariable = () => {
42
- const uid = addDeclaredVariable(canvasId);
43
- selectVariable(uid);
44
- };
45
-
46
- // Filter variables into groups (each canvas is self-contained — no main-canvas leakage)
47
- const functionArgs = variables.filter((v) => v.kind === "fnarg");
48
- const nodeOutputs = variables.filter((v) => v.kind === "node");
49
-
50
- const hasContent = functionArgs.length > 0 || nodeOutputs.length > 0 || declaredVariables.length > 0;
51
-
52
- if (!hasContent) {
53
- return (
54
- <div className="flex flex-col items-center justify-center py-8 text-center">
55
- <VariableIcon className="w-10 h-10 text-muted-foreground/50 mb-3" />
56
- <p className="text-sm text-muted-foreground">{t("noVariables")}</p>
57
- <p className="text-xs text-muted-foreground/70 mt-1">{t("addNodesForVariables")}</p>
58
- {!readOnly && (
59
- <div className="mt-3 w-full px-2">
60
- <AddButton onClick={handleAddVariable}>{t("addVariable")}</AddButton>
61
- </div>
62
- )}
63
- </div>
64
- );
65
- }
66
-
67
- const renderVariableItem = (ref: Variable, onClick?: () => void, isSelected = false) => {
68
- const clickable = !!onClick;
69
-
70
- return (
71
- <div
72
- key={
73
- ref.kind === "node"
74
- ? `${ref.nodeId}-${ref.outputId}`
75
- : ref.kind === "declared"
76
- ? `declared-${ref.uid}`
77
- : `fnarg-${ref.uid}`
78
- }
79
- onClick={onClick}
80
- className={cn(
81
- "p-3 rounded-lg transition-all",
82
- isSelected
83
- ? "bg-accent shadow-md border border-primary/40 ring-1 ring-primary/40"
84
- : "bg-card shadow-sm border border-border",
85
- clickable ? "hover:shadow-md cursor-pointer" : "cursor-default",
86
- )}
87
- >
88
- <div className="flex items-center justify-between">
89
- <div className="flex items-center gap-2">
90
- <VariableIcon className="w-4 h-4 text-muted-foreground" />
91
- <span className="font-mono text-sm text-foreground">{ref.name}</span>
92
- </div>
93
- <span className="text-[10px] font-medium px-1.5 py-0.5 rounded border border-border/50 text-muted-foreground shrink-0">
94
- {ref.dataType}
95
- </span>
96
- </div>
97
- </div>
98
- );
99
- };
100
-
101
- const SectionHeader = ({ title }: { title: string }) => (
102
- <div className="flex items-center justify-between px-1 mb-2">
103
- <span className="text-sm font-medium text-foreground/80">{title}</span>
104
- </div>
105
- );
106
-
107
- return (
108
- <div className="space-y-5">
109
- {/* Function Arguments (function canvas only) — read-only, arrive by value */}
110
- {!isMainCanvas && functionArgs.length > 0 && (
111
- <div>
112
- <SectionHeader title={t("functionArguments")} />
113
- <div className="space-y-1.5">{functionArgs.map((v) => renderVariableItem(v))}</div>
114
- </div>
115
- )}
116
-
117
- {/* Node Output Variables — click opens the emitting node */}
118
- {nodeOutputs.length > 0 && (
119
- <div>
120
- <SectionHeader title={t("nodeOutputVariables")} />
121
- <div className="space-y-1.5">
122
- {nodeOutputs.map((v) =>
123
- renderVariableItem(v, v.kind === "node" ? () => onSelectNode(v.nodeId) : undefined),
124
- )}
125
- </div>
126
- </div>
127
- )}
128
-
129
- {/* Defined Variables — click opens the VariableConfigPanel */}
130
- <div>
131
- <SectionHeader title={t("definedVariables")} />
132
- <div className="space-y-1.5">
133
- {declaredVariables.map(({ uid, var: dv }) =>
134
- renderVariableItem(
135
- dv,
136
- readOnly ? undefined : () => selectVariable(uid),
137
- selection.kind === "variable" && selection.uid === uid,
138
- ),
139
- )}
140
- {!readOnly && <AddButton onClick={handleAddVariable}>{t("addVariable")}</AddButton>}
141
- </div>
142
- </div>
143
- </div>
144
- );
145
- };
1
+ import React from "react";
2
+ import { useTranslation } from "react-i18next";
3
+ import { AddButton } from "../components/ui/add-button";
4
+ import { Variable as VariableIcon } from "lucide-react";
5
+ import { cn } from "../cn";
6
+ import { useEditorStore } from "../stores/editorStore";
7
+ import { isReadOnly } from "../WorkflowBuilder";
8
+ import { useAvailableVariables } from "../hooks/useAvailableVariables";
9
+ import { getOrCreateCanvasStore, MAIN_CANVAS_ID } from "../stores/canvasStore";
10
+ import { type Variable, type DeclaredVariable } from "@foresthubai/workflow-core/variable";
11
+ import { addDeclaredVariable } from "../utils/variableOperations";
12
+
13
+ interface VariablesPanelProps {
14
+ canvasId: string;
15
+ onSelectNode: (nodeId: string) => void;
16
+ }
17
+
18
+ export const VariablesPanel = ({ canvasId, onSelectNode }: VariablesPanelProps) => {
19
+ const readOnly = useEditorStore((s) => isReadOnly(s.builderMode));
20
+ const { t } = useTranslation();
21
+ const { list: variables } = useAvailableVariables(canvasId);
22
+ const selection = useEditorStore((s) => s.selection);
23
+ const selectVariable = useEditorStore((s) => s.selectVariable);
24
+
25
+ const store = getOrCreateCanvasStore(canvasId);
26
+ const allVariables = store((s) => s.variables);
27
+ const isMainCanvas = canvasId === MAIN_CANVAS_ID;
28
+
29
+ // Extract declared variables from the unified record
30
+ const declaredVariables = React.useMemo(() => {
31
+ const result: { uid: string; var: DeclaredVariable }[] = [];
32
+ for (const v of Object.values(allVariables)) {
33
+ if (v.kind === "declared") {
34
+ result.push({ uid: v.uid, var: v });
35
+ }
36
+ }
37
+ return result;
38
+ }, [allVariables]);
39
+
40
+ // Create a declared variable and immediately open its config panel.
41
+ const handleAddVariable = () => {
42
+ const uid = addDeclaredVariable(canvasId);
43
+ selectVariable(uid);
44
+ };
45
+
46
+ // Filter variables into groups (each canvas is self-contained — no main-canvas leakage)
47
+ const functionArgs = variables.filter((v) => v.kind === "fnarg");
48
+ const nodeOutputs = variables.filter((v) => v.kind === "node");
49
+
50
+ const hasContent = functionArgs.length > 0 || nodeOutputs.length > 0 || declaredVariables.length > 0;
51
+
52
+ if (!hasContent) {
53
+ return (
54
+ <div className="flex flex-col items-center justify-center py-8 text-center">
55
+ <VariableIcon className="w-10 h-10 text-muted-foreground/50 mb-3" />
56
+ <p className="text-sm text-muted-foreground">{t("noVariables")}</p>
57
+ <p className="text-xs text-muted-foreground/70 mt-1">{t("addNodesForVariables")}</p>
58
+ {!readOnly && (
59
+ <div className="mt-3 w-full px-2">
60
+ <AddButton onClick={handleAddVariable}>{t("addVariable")}</AddButton>
61
+ </div>
62
+ )}
63
+ </div>
64
+ );
65
+ }
66
+
67
+ const renderVariableItem = (ref: Variable, onClick?: () => void, isSelected = false) => {
68
+ const clickable = !!onClick;
69
+
70
+ return (
71
+ <div
72
+ key={
73
+ ref.kind === "node"
74
+ ? `${ref.nodeId}-${ref.outputId}`
75
+ : ref.kind === "declared"
76
+ ? `declared-${ref.uid}`
77
+ : `fnarg-${ref.uid}`
78
+ }
79
+ onClick={onClick}
80
+ className={cn(
81
+ "p-3 rounded-lg transition-all",
82
+ isSelected
83
+ ? "bg-accent shadow-md border border-primary/40 ring-1 ring-primary/40"
84
+ : "bg-card shadow-sm border border-border",
85
+ clickable ? "hover:shadow-md cursor-pointer" : "cursor-default",
86
+ )}
87
+ >
88
+ <div className="flex items-center justify-between">
89
+ <div className="flex items-center gap-2">
90
+ <VariableIcon className="w-4 h-4 text-muted-foreground" />
91
+ <span className="font-mono text-sm text-foreground">{ref.name}</span>
92
+ </div>
93
+ <span className="text-[10px] font-medium px-1.5 py-0.5 rounded border border-border/50 text-muted-foreground shrink-0">
94
+ {ref.dataType}
95
+ </span>
96
+ </div>
97
+ </div>
98
+ );
99
+ };
100
+
101
+ const SectionHeader = ({ title }: { title: string }) => (
102
+ <div className="flex items-center justify-between px-1 mb-2">
103
+ <span className="text-sm font-medium text-foreground/80">{title}</span>
104
+ </div>
105
+ );
106
+
107
+ return (
108
+ <div className="space-y-5">
109
+ {/* Function Arguments (function canvas only) — read-only, arrive by value */}
110
+ {!isMainCanvas && functionArgs.length > 0 && (
111
+ <div>
112
+ <SectionHeader title={t("functionArguments")} />
113
+ <div className="space-y-1.5">{functionArgs.map((v) => renderVariableItem(v))}</div>
114
+ </div>
115
+ )}
116
+
117
+ {/* Node Output Variables — click opens the emitting node */}
118
+ {nodeOutputs.length > 0 && (
119
+ <div>
120
+ <SectionHeader title={t("nodeOutputVariables")} />
121
+ <div className="space-y-1.5">
122
+ {nodeOutputs.map((v) =>
123
+ renderVariableItem(v, v.kind === "node" ? () => onSelectNode(v.nodeId) : undefined),
124
+ )}
125
+ </div>
126
+ </div>
127
+ )}
128
+
129
+ {/* Defined Variables — click opens the VariableConfigPanel */}
130
+ <div>
131
+ <SectionHeader title={t("definedVariables")} />
132
+ <div className="space-y-1.5">
133
+ {declaredVariables.map(({ uid, var: dv }) =>
134
+ renderVariableItem(
135
+ dv,
136
+ readOnly ? undefined : () => selectVariable(uid),
137
+ selection.kind === "variable" && selection.uid === uid,
138
+ ),
139
+ )}
140
+ {!readOnly && <AddButton onClick={handleAddVariable}>{t("addVariable")}</AddButton>}
141
+ </div>
142
+ </div>
143
+ </div>
144
+ );
145
+ };