@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,26 +1,26 @@
1
- // Shared category icons and colors for node categories in the workflow builder.
2
-
3
- import { NodeCategory } from "@foresthubai/workflow-core/node";
4
- import { Bot, Box, Brain, Inbox, Send, Variable, Wrench, Zap, type LucideIcon } from "lucide-react";
5
-
6
- export const categoryIcons: Record<string, LucideIcon> = {
7
- [NodeCategory.Input]: Inbox,
8
- [NodeCategory.Logic]: Brain,
9
- [NodeCategory.Data]: Variable,
10
- [NodeCategory.Output]: Send,
11
- [NodeCategory.AI]: Bot,
12
- [NodeCategory.Trigger]: Zap,
13
- [NodeCategory.Tool]: Wrench,
14
- [NodeCategory.Function]: Box,
15
- };
16
-
17
- export const categoryColors: Record<string, string> = {
18
- [NodeCategory.Input]: "bg-node-input/10 text-node-input border-node-input/20",
19
- [NodeCategory.Logic]: "bg-node-logic/10 text-node-logic border-node-logic/20",
20
- [NodeCategory.Data]: "bg-node-data/10 text-node-data border-node-data/20",
21
- [NodeCategory.Output]: "bg-node-output/10 text-node-output border-node-output/20",
22
- [NodeCategory.AI]: "bg-node-agent/10 text-node-agent border-node-agent/20",
23
- [NodeCategory.Trigger]: "bg-node-trigger/10 text-node-trigger border-node-trigger/20",
24
- [NodeCategory.Tool]: "bg-node-tool/10 text-node-tool border-node-tool/20",
25
- [NodeCategory.Function]: "bg-node-function/10 text-node-function border-node-function/20",
26
- };
1
+ // Shared category icons and colors for node categories in the workflow builder.
2
+
3
+ import { NodeCategory } from "@foresthubai/workflow-core/node";
4
+ import { Bot, Box, Brain, Inbox, Send, Variable, Wrench, Zap, type LucideIcon } from "lucide-react";
5
+
6
+ export const categoryIcons: Record<string, LucideIcon> = {
7
+ [NodeCategory.Input]: Inbox,
8
+ [NodeCategory.Logic]: Brain,
9
+ [NodeCategory.Data]: Variable,
10
+ [NodeCategory.Output]: Send,
11
+ [NodeCategory.AI]: Bot,
12
+ [NodeCategory.Trigger]: Zap,
13
+ [NodeCategory.Tool]: Wrench,
14
+ [NodeCategory.Function]: Box,
15
+ };
16
+
17
+ export const categoryColors: Record<string, string> = {
18
+ [NodeCategory.Input]: "bg-node-input/10 text-node-input border-node-input/20",
19
+ [NodeCategory.Logic]: "bg-node-logic/10 text-node-logic border-node-logic/20",
20
+ [NodeCategory.Data]: "bg-node-data/10 text-node-data border-node-data/20",
21
+ [NodeCategory.Output]: "bg-node-output/10 text-node-output border-node-output/20",
22
+ [NodeCategory.AI]: "bg-node-agent/10 text-node-agent border-node-agent/20",
23
+ [NodeCategory.Trigger]: "bg-node-trigger/10 text-node-trigger border-node-trigger/20",
24
+ [NodeCategory.Tool]: "bg-node-tool/10 text-node-tool border-node-tool/20",
25
+ [NodeCategory.Function]: "bg-node-function/10 text-node-function border-node-function/20",
26
+ };
@@ -1,86 +1,86 @@
1
- import { CHANNEL_DEFINITION, type ChannelType, type Channel } from "@foresthubai/workflow-core/channel";
2
- import { isParameterActive } from "@foresthubai/workflow-core/parameter";
3
- import { useEditorStore } from "../stores/editorStore";
4
- import { generateId } from "@foresthubai/workflow-core/id";
5
- import { uniqueName } from "./resourceHelpers";
6
-
7
- /**
8
- * Build the initial `arguments` record for a new channel: each parameter
9
- * that's active for the chosen `type` and has a `default` gets seeded.
10
- */
11
- function defaultArguments(type: ChannelType): Record<string, unknown> {
12
- const seed: Record<string, unknown> = { type };
13
- const args: Record<string, unknown> = {};
14
- for (const param of CHANNEL_DEFINITION.parameters) {
15
- if (param.id === "type") continue;
16
- if (param.activationRules?.length && !isParameterActive(param, seed, false)) continue;
17
- if ("default" in param && param.default !== undefined) {
18
- args[param.id] = param.default;
19
- }
20
- }
21
- return args;
22
- }
23
-
24
- /**
25
- * Create a new channel in the editor store. Returns the new instance.
26
- */
27
- export function addChannel(type: ChannelType = "GPIOIN"): Channel {
28
- const id = generateId();
29
- const existing = Object.values(useEditorStore.getState().channels).map((v) => v.label);
30
- const instance: Channel = {
31
- id,
32
- label: uniqueName("channel", existing),
33
- type,
34
- arguments: defaultArguments(type),
35
- };
36
- useEditorStore.getState().setChannels((vars) => ({ ...vars, [id]: instance }));
37
- return instance;
38
- }
39
-
40
- /**
41
- * Apply a partial patch to a channel. Inactive arguments are intentionally
42
- * retained in the store: the domain store is the superset and stripping happens
43
- * only at the api boundary (`serialize`), so switching `type` away and back
44
- * restores previously-entered values rather than resetting them. On a type
45
- * change we still seed defaults for params that are newly active and unset, so
46
- * the config panel shows sensible initial values. Top-level fields (label/type)
47
- * are merged separately from arguments.
48
- */
49
- export function updateChannel(id: string, patch: { label?: string; type?: ChannelType; arguments?: Record<string, unknown> }): void {
50
- const key = id;
51
- useEditorStore.getState().setChannels((vars) => {
52
- const existing = vars[key];
53
- if (!existing) return vars;
54
-
55
- const nextType = patch.type ?? existing.type;
56
- const mergedArgs = { ...existing.arguments, ...(patch.arguments ?? {}) };
57
-
58
- if (patch.type && patch.type !== existing.type) {
59
- for (const [k, v] of Object.entries(defaultArguments(nextType))) {
60
- if (mergedArgs[k] === undefined) mergedArgs[k] = v;
61
- }
62
- }
63
-
64
- return {
65
- ...vars,
66
- [key]: {
67
- ...existing,
68
- ...(patch.label !== undefined ? { label: patch.label } : {}),
69
- type: nextType,
70
- arguments: mergedArgs,
71
- },
72
- };
73
- });
74
- }
75
-
76
- export function deleteChannel(id: string): void {
77
- const key = id;
78
- useEditorStore.getState().setChannels((vars) => {
79
- const { [key]: _drop, ...rest } = vars;
80
- return rest;
81
- });
82
- const sel = useEditorStore.getState().selection;
83
- if (sel.kind === "channel" && sel.id === id) {
84
- useEditorStore.getState().clearSelection();
85
- }
86
- }
1
+ import { CHANNEL_DEFINITION, type ChannelType, type Channel } from "@foresthubai/workflow-core/channel";
2
+ import { isParameterActive } from "@foresthubai/workflow-core/parameter";
3
+ import { useEditorStore } from "../stores/editorStore";
4
+ import { generateId } from "@foresthubai/workflow-core/id";
5
+ import { uniqueName } from "./resourceHelpers";
6
+
7
+ /**
8
+ * Build the initial `arguments` record for a new channel: each parameter
9
+ * that's active for the chosen `type` and has a `default` gets seeded.
10
+ */
11
+ function defaultArguments(type: ChannelType): Record<string, unknown> {
12
+ const seed: Record<string, unknown> = { type };
13
+ const args: Record<string, unknown> = {};
14
+ for (const param of CHANNEL_DEFINITION.parameters) {
15
+ if (param.id === "type") continue;
16
+ if (param.activationRules?.length && !isParameterActive(param, seed, false)) continue;
17
+ if ("default" in param && param.default !== undefined) {
18
+ args[param.id] = param.default;
19
+ }
20
+ }
21
+ return args;
22
+ }
23
+
24
+ /**
25
+ * Create a new channel in the editor store. Returns the new instance.
26
+ */
27
+ export function addChannel(type: ChannelType = "GPIOIN"): Channel {
28
+ const id = generateId();
29
+ const existing = Object.values(useEditorStore.getState().channels).map((v) => v.label);
30
+ const instance: Channel = {
31
+ id,
32
+ label: uniqueName("channel", existing),
33
+ type,
34
+ arguments: defaultArguments(type),
35
+ };
36
+ useEditorStore.getState().setChannels((vars) => ({ ...vars, [id]: instance }));
37
+ return instance;
38
+ }
39
+
40
+ /**
41
+ * Apply a partial patch to a channel. Inactive arguments are intentionally
42
+ * retained in the store: the domain store is the superset and stripping happens
43
+ * only at the api boundary (`serialize`), so switching `type` away and back
44
+ * restores previously-entered values rather than resetting them. On a type
45
+ * change we still seed defaults for params that are newly active and unset, so
46
+ * the config panel shows sensible initial values. Top-level fields (label/type)
47
+ * are merged separately from arguments.
48
+ */
49
+ export function updateChannel(id: string, patch: { label?: string; type?: ChannelType; arguments?: Record<string, unknown> }): void {
50
+ const key = id;
51
+ useEditorStore.getState().setChannels((vars) => {
52
+ const existing = vars[key];
53
+ if (!existing) return vars;
54
+
55
+ const nextType = patch.type ?? existing.type;
56
+ const mergedArgs = { ...existing.arguments, ...(patch.arguments ?? {}) };
57
+
58
+ if (patch.type && patch.type !== existing.type) {
59
+ for (const [k, v] of Object.entries(defaultArguments(nextType))) {
60
+ if (mergedArgs[k] === undefined) mergedArgs[k] = v;
61
+ }
62
+ }
63
+
64
+ return {
65
+ ...vars,
66
+ [key]: {
67
+ ...existing,
68
+ ...(patch.label !== undefined ? { label: patch.label } : {}),
69
+ type: nextType,
70
+ arguments: mergedArgs,
71
+ },
72
+ };
73
+ });
74
+ }
75
+
76
+ export function deleteChannel(id: string): void {
77
+ const key = id;
78
+ useEditorStore.getState().setChannels((vars) => {
79
+ const { [key]: _drop, ...rest } = vars;
80
+ return rest;
81
+ });
82
+ const sel = useEditorStore.getState().selection;
83
+ if (sel.kind === "channel" && sel.id === id) {
84
+ useEditorStore.getState().clearSelection();
85
+ }
86
+ }
@@ -1,137 +1,137 @@
1
- // Editor connection rules — which ports may connect, which node types are
2
- // offered from a port, and whether a node can take another outgoing edge.
3
- //
4
- // These operate on React Flow `Node`/`Edge` and drive canvas interactions, so
5
- // they live here in the editor rather than in the headless @foresthubai/workflow-core.
6
- // Core exposes the pure primitive (`getPorts`); the React Flow coupling stays
7
- // on this side of the boundary.
8
-
9
- import { Edge, Node } from "@xyflow/react";
10
- import { getPorts, NodeRegistry, type NodeData, type NodeDefinition } from "@foresthubai/workflow-core/node";
11
- import { type EdgeType } from "@foresthubai/workflow-core/edge";
12
-
13
- /** Check whether a node already has tool-input edges (for mutual exclusion). */
14
- function hasToolInputEdge(nodeId: string, nodeData: NodeData, edges: Edge[]): boolean {
15
- const ports = getPorts(nodeData);
16
- return edges.some((e) => e.target === nodeId && ports.input.some((p) => p.type === "tool" && p.id === e.targetHandle));
17
- }
18
-
19
- /** Check whether a node already has control-flow edges (for mutual exclusion). */
20
- function hasControlFlowEdge(nodeId: string, nodeData: NodeData, edges: Edge[]): boolean {
21
- const ports = getPorts(nodeData);
22
- return edges.some((e) => {
23
- if (e.target === nodeId) {
24
- return ports.input.some((p) => p.type === "control" && p.id === e.targetHandle);
25
- }
26
- if (e.source === nodeId) {
27
- return ports.output.some((p) => p.type === "control" && p.id === e.sourceHandle);
28
- }
29
- return false;
30
- });
31
- }
32
-
33
- /**
34
- * Check whether an output port can accept at least one more outgoing edge.
35
- * Used by the contextual "+" button on output ports.
36
- */
37
- export function canPortAcceptEdge(nodeId: string, handleId: string, nodes: Node<NodeData>[], edges: Edge[]): boolean {
38
- const node = nodes.find((n) => n.id === nodeId);
39
- if (!node) return false;
40
-
41
- const ports = getPorts(node.data);
42
- const port = ports.output.find((p) => p.id === handleId);
43
- if (!port) return false;
44
-
45
- // Tool output ports always accept multiple edges (an agent wires up many tools).
46
- // A control output port accepts a single edge unless the node can branch.
47
- if (port.type === "control" && !NodeRegistry.getByType(node.data.type)?.canBranch) {
48
- if (edges.some((e) => e.source === nodeId && e.sourceHandle === handleId)) return false;
49
- }
50
-
51
- // Mutual exclusion: control output blocked when node has tool-input edges
52
- // (Tool output is exempt — never blocked)
53
- if (port.type === "control" && hasToolInputEdge(nodeId, node.data, edges)) return false;
54
-
55
- return true;
56
- }
57
-
58
- /**
59
- * Filter node definitions to those that can connect to a given output port.
60
- * Returns definitions whose nodes have an input port matching the origin's port type.
61
- */
62
- export function getCompatibleNodeDefs(
63
- originNodeId: string,
64
- originHandleId: string,
65
- nodes: Node<NodeData>[],
66
- edges: Edge[],
67
- allNodeDefs: NodeDefinition[],
68
- isFunctionCanvas: boolean,
69
- ): NodeDefinition[] {
70
- const originNode = nodes.find((n) => n.id === originNodeId);
71
- if (!originNode) return [];
72
-
73
- const originPorts = getPorts(originNode.data);
74
- const originPort = originPorts.output.find((p) => p.id === originHandleId);
75
- if (!originPort) return [];
76
-
77
- const originPortType = originPort.type; // "control" | "tool"
78
-
79
- return allNodeDefs.filter((def) => {
80
- if (def.isUnremovable) return false;
81
-
82
- if (def.isSingleton && nodes.some((n) => n.data.type === def.type)) return false;
83
-
84
- // Triggers have no inputs — skip on function canvas and when looking for input ports
85
- if (isFunctionCanvas && def.category === "Trigger") return false;
86
-
87
- // Check candidate has a matching input port
88
- const candidatePorts = getPorts({ type: def.type } as NodeData);
89
- return candidatePorts.input.some((p) => p.type === originPortType);
90
- });
91
- }
92
-
93
- export const isValidConnection = (
94
- sourceId: string | null,
95
- targetId: string | null,
96
- sourceHandleId: string | null | undefined,
97
- targetHandleId: string | null | undefined,
98
- nodes: Node<NodeData>[],
99
- edges: Edge[],
100
- ): false | EdgeType => {
101
- // All handles must be present
102
- if (sourceHandleId == null || targetHandleId == null || sourceId == null || targetId == null) return false;
103
-
104
- // Find source and target nodes
105
- const srcNode = nodes.find((n) => n.id === sourceId);
106
- const tgtNode = nodes.find((n) => n.id === targetId);
107
- if (!srcNode || !tgtNode) return false;
108
-
109
- // Source-side checks via canPortAcceptEdge (multiple-outgoing + mutual exclusion)
110
- if (!canPortAcceptEdge(sourceId, sourceHandleId, nodes, edges)) return false;
111
-
112
- // Get ports using centralized dispatcher
113
- const sourcePorts = getPorts(srcNode.data);
114
- const targetPorts = getPorts(tgtNode.data);
115
-
116
- const sourcePort = sourcePorts.output.find((p) => p.id === sourceHandleId);
117
- const targetPort = targetPorts.input.find((p) => p.id === targetHandleId);
118
- if (!sourcePort || !targetPort) return false;
119
-
120
- // Only allow connections between same port types
121
- if (sourcePort.type !== targetPort.type) return false;
122
-
123
- // Target-side mutual exclusion checks
124
- const portType = sourcePort.type as EdgeType;
125
-
126
- if (portType === "tool") {
127
- // Connecting a tool input on the target — reject if target already has control connections
128
- if (hasControlFlowEdge(tgtNode.id, tgtNode.data, edges)) return false;
129
- }
130
-
131
- if (portType === "control") {
132
- // Connecting a control port — reject if target already has tool INPUT connections
133
- if (hasToolInputEdge(tgtNode.id, tgtNode.data, edges)) return false;
134
- }
135
-
136
- return portType;
137
- };
1
+ // Editor connection rules — which ports may connect, which node types are
2
+ // offered from a port, and whether a node can take another outgoing edge.
3
+ //
4
+ // These operate on React Flow `Node`/`Edge` and drive canvas interactions, so
5
+ // they live here in the editor rather than in the headless @foresthubai/workflow-core.
6
+ // Core exposes the pure primitive (`getPorts`); the React Flow coupling stays
7
+ // on this side of the boundary.
8
+
9
+ import { Edge, Node } from "@xyflow/react";
10
+ import { getPorts, NodeRegistry, type NodeData, type NodeDefinition } from "@foresthubai/workflow-core/node";
11
+ import { type EdgeType } from "@foresthubai/workflow-core/edge";
12
+
13
+ /** Check whether a node already has tool-input edges (for mutual exclusion). */
14
+ function hasToolInputEdge(nodeId: string, nodeData: NodeData, edges: Edge[]): boolean {
15
+ const ports = getPorts(nodeData);
16
+ return edges.some((e) => e.target === nodeId && ports.input.some((p) => p.type === "tool" && p.id === e.targetHandle));
17
+ }
18
+
19
+ /** Check whether a node already has control-flow edges (for mutual exclusion). */
20
+ function hasControlFlowEdge(nodeId: string, nodeData: NodeData, edges: Edge[]): boolean {
21
+ const ports = getPorts(nodeData);
22
+ return edges.some((e) => {
23
+ if (e.target === nodeId) {
24
+ return ports.input.some((p) => p.type === "control" && p.id === e.targetHandle);
25
+ }
26
+ if (e.source === nodeId) {
27
+ return ports.output.some((p) => p.type === "control" && p.id === e.sourceHandle);
28
+ }
29
+ return false;
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Check whether an output port can accept at least one more outgoing edge.
35
+ * Used by the contextual "+" button on output ports.
36
+ */
37
+ export function canPortAcceptEdge(nodeId: string, handleId: string, nodes: Node<NodeData>[], edges: Edge[]): boolean {
38
+ const node = nodes.find((n) => n.id === nodeId);
39
+ if (!node) return false;
40
+
41
+ const ports = getPorts(node.data);
42
+ const port = ports.output.find((p) => p.id === handleId);
43
+ if (!port) return false;
44
+
45
+ // Tool output ports always accept multiple edges (an agent wires up many tools).
46
+ // A control output port accepts a single edge unless the node can branch.
47
+ if (port.type === "control" && !NodeRegistry.getByType(node.data.type)?.canBranch) {
48
+ if (edges.some((e) => e.source === nodeId && e.sourceHandle === handleId)) return false;
49
+ }
50
+
51
+ // Mutual exclusion: control output blocked when node has tool-input edges
52
+ // (Tool output is exempt — never blocked)
53
+ if (port.type === "control" && hasToolInputEdge(nodeId, node.data, edges)) return false;
54
+
55
+ return true;
56
+ }
57
+
58
+ /**
59
+ * Filter node definitions to those that can connect to a given output port.
60
+ * Returns definitions whose nodes have an input port matching the origin's port type.
61
+ */
62
+ export function getCompatibleNodeDefs(
63
+ originNodeId: string,
64
+ originHandleId: string,
65
+ nodes: Node<NodeData>[],
66
+ edges: Edge[],
67
+ allNodeDefs: NodeDefinition[],
68
+ isFunctionCanvas: boolean,
69
+ ): NodeDefinition[] {
70
+ const originNode = nodes.find((n) => n.id === originNodeId);
71
+ if (!originNode) return [];
72
+
73
+ const originPorts = getPorts(originNode.data);
74
+ const originPort = originPorts.output.find((p) => p.id === originHandleId);
75
+ if (!originPort) return [];
76
+
77
+ const originPortType = originPort.type; // "control" | "tool"
78
+
79
+ return allNodeDefs.filter((def) => {
80
+ if (def.isUnremovable) return false;
81
+
82
+ if (def.isSingleton && nodes.some((n) => n.data.type === def.type)) return false;
83
+
84
+ // Triggers have no inputs — skip on function canvas and when looking for input ports
85
+ if (isFunctionCanvas && def.category === "Trigger") return false;
86
+
87
+ // Check candidate has a matching input port
88
+ const candidatePorts = getPorts({ type: def.type } as NodeData);
89
+ return candidatePorts.input.some((p) => p.type === originPortType);
90
+ });
91
+ }
92
+
93
+ export const isValidConnection = (
94
+ sourceId: string | null,
95
+ targetId: string | null,
96
+ sourceHandleId: string | null | undefined,
97
+ targetHandleId: string | null | undefined,
98
+ nodes: Node<NodeData>[],
99
+ edges: Edge[],
100
+ ): false | EdgeType => {
101
+ // All handles must be present
102
+ if (sourceHandleId == null || targetHandleId == null || sourceId == null || targetId == null) return false;
103
+
104
+ // Find source and target nodes
105
+ const srcNode = nodes.find((n) => n.id === sourceId);
106
+ const tgtNode = nodes.find((n) => n.id === targetId);
107
+ if (!srcNode || !tgtNode) return false;
108
+
109
+ // Source-side checks via canPortAcceptEdge (multiple-outgoing + mutual exclusion)
110
+ if (!canPortAcceptEdge(sourceId, sourceHandleId, nodes, edges)) return false;
111
+
112
+ // Get ports using centralized dispatcher
113
+ const sourcePorts = getPorts(srcNode.data);
114
+ const targetPorts = getPorts(tgtNode.data);
115
+
116
+ const sourcePort = sourcePorts.output.find((p) => p.id === sourceHandleId);
117
+ const targetPort = targetPorts.input.find((p) => p.id === targetHandleId);
118
+ if (!sourcePort || !targetPort) return false;
119
+
120
+ // Only allow connections between same port types
121
+ if (sourcePort.type !== targetPort.type) return false;
122
+
123
+ // Target-side mutual exclusion checks
124
+ const portType = sourcePort.type as EdgeType;
125
+
126
+ if (portType === "tool") {
127
+ // Connecting a tool input on the target — reject if target already has control connections
128
+ if (hasControlFlowEdge(tgtNode.id, tgtNode.data, edges)) return false;
129
+ }
130
+
131
+ if (portType === "control") {
132
+ // Connecting a control port — reject if target already has tool INPUT connections
133
+ if (hasToolInputEdge(tgtNode.id, tgtNode.data, edges)) return false;
134
+ }
135
+
136
+ return portType;
137
+ };