@foresthubai/workflow-core 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 (77) hide show
  1. package/LICENSE +202 -202
  2. package/NOTICE +14 -14
  3. package/README.md +63 -63
  4. package/dist/api/workflow.d.ts +2 -2
  5. package/dist/api/workflow.d.ts.map +1 -1
  6. package/package.json +1 -1
  7. package/src/api/index.ts +11 -11
  8. package/src/api/workflow.ts +607 -607
  9. package/src/channel/Channel.ts +11 -11
  10. package/src/channel/ChannelDefinition.ts +76 -76
  11. package/src/channel/index.ts +6 -6
  12. package/src/channel/serialization.ts +68 -68
  13. package/src/deploy/index.ts +1 -1
  14. package/src/deploy/requirements.test.ts +61 -61
  15. package/src/deploy/requirements.ts +41 -41
  16. package/src/diagnostics/__fixtures__/diagnosticFixtures.ts +158 -158
  17. package/src/diagnostics/diagnostics.test.ts +878 -878
  18. package/src/diagnostics/diagnostics.ts +936 -936
  19. package/src/diagnostics/index.ts +11 -11
  20. package/src/edge/Edge.ts +23 -23
  21. package/src/edge/EdgeDefinition.ts +45 -45
  22. package/src/edge/EdgeType.ts +19 -19
  23. package/src/edge/index.ts +8 -8
  24. package/src/edge/serialization.ts +83 -83
  25. package/src/expression/index.ts +4 -4
  26. package/src/expression/parser.ts +362 -362
  27. package/src/expression/types.ts +30 -30
  28. package/src/function/FunctionDeclaration.ts +54 -54
  29. package/src/function/index.ts +3 -3
  30. package/src/function/serialization.ts +40 -40
  31. package/src/globals.d.ts +9 -9
  32. package/src/id/index.ts +8 -8
  33. package/src/index.ts +22 -22
  34. package/src/memory/Memory.ts +15 -15
  35. package/src/memory/MemoryDefinition.ts +16 -16
  36. package/src/memory/MemoryFileDefinition.ts +37 -37
  37. package/src/memory/MemoryRegistry.ts +35 -35
  38. package/src/memory/VectorDatabaseDefinition.ts +21 -21
  39. package/src/memory/index.ts +8 -8
  40. package/src/memory/serialization.ts +47 -47
  41. package/src/migration/index.ts +4 -4
  42. package/src/migration/migrate.test.ts +44 -44
  43. package/src/migration/migrate.ts +58 -58
  44. package/src/migration/migrations.ts +24 -24
  45. package/src/migration/version.ts +9 -9
  46. package/src/model/LLMModelDefinition.ts +12 -12
  47. package/src/model/Model.ts +39 -39
  48. package/src/model/ModelDefinition.ts +15 -15
  49. package/src/model/ModelRegistry.ts +33 -33
  50. package/src/model/index.ts +7 -7
  51. package/src/model/serialization.ts +30 -30
  52. package/src/node/AgentNode.ts +82 -82
  53. package/src/node/DataNode.ts +41 -41
  54. package/src/node/FunctionNode.ts +76 -76
  55. package/src/node/InputNode.ts +185 -185
  56. package/src/node/LogicNode.ts +33 -33
  57. package/src/node/MqttNode.ts +127 -127
  58. package/src/node/Node.ts +61 -61
  59. package/src/node/NodeDefinition.ts +37 -37
  60. package/src/node/NodeRegistry.ts +85 -85
  61. package/src/node/OutputNode.ts +87 -87
  62. package/src/node/ToolNode.ts +32 -32
  63. package/src/node/TriggerNode.ts +272 -272
  64. package/src/node/constants.ts +16 -16
  65. package/src/node/index.ts +26 -26
  66. package/src/node/methods.ts +278 -278
  67. package/src/node/serialization.ts +544 -544
  68. package/src/parameter/OutputParameter.ts +68 -68
  69. package/src/parameter/Parameter.ts +243 -243
  70. package/src/parameter/index.ts +33 -33
  71. package/src/variable/Variable.ts +10 -10
  72. package/src/variable/index.ts +16 -16
  73. package/src/variable/operations.ts +106 -106
  74. package/src/workflow/Workflow.ts +41 -41
  75. package/src/workflow/index.ts +3 -3
  76. package/src/workflow/serialization.test.ts +240 -240
  77. package/src/workflow/serialization.ts +242 -242
@@ -1,240 +1,240 @@
1
- import { describe, it, expect } from "vitest";
2
- import { serialize, deserialize, computeVariablesFromNodes, buildCanvasVariables } from "./serialization";
3
- import { MAIN_CANVAS_ID, type Workflow, type Canvas } from "./Workflow";
4
- import type { Schemas } from "../api";
5
- import type { NodeData } from "../node";
6
-
7
- // ============================================================================
8
- // Reverse roundtrip: api JSON → deserialize → serialize → deep-equal JSON
9
- //
10
- // This direction is the strongest invariant — deserialize is the function
11
- // that reconstructs derivable state (variable records); if it gets the rest
12
- // right, re-serialize must produce the same JSON back. Tests every code path
13
- // in deserialize + serialize without separately constructing WorkflowState.
14
- // ============================================================================
15
-
16
- const empty: Schemas["Workflow"] = {
17
- schemaVersion: 1,
18
- nodes: [],
19
- edges: [],
20
- functions: [],
21
- declaredVariables: [],
22
- channels: [],
23
- memory: [],
24
- models: [],
25
- };
26
-
27
- const mainOnly: Schemas["Workflow"] = {
28
- schemaVersion: 1,
29
- nodes: [
30
- {
31
- id: "n1",
32
- type: "Agent",
33
- position: { x: 100, y: 200 },
34
- label: "My Agent",
35
- arguments: {
36
- name: "agent-1",
37
- model: "claude-opus-4-7",
38
- instructions: "be helpful",
39
- outputDeclarations: [],
40
- memoryRefs: [],
41
- answer: { active: true, mode: "emit", name: "answer" },
42
- },
43
- },
44
- ],
45
- edges: [],
46
- functions: [],
47
- declaredVariables: [
48
- { uid: "d1", name: "counter", dataType: "int", initialValue: 0 },
49
- ],
50
- channels: [],
51
- memory: [],
52
- models: [],
53
- };
54
-
55
- const allEdgeTypes: Schemas["Workflow"] = {
56
- schemaVersion: 1,
57
- nodes: [],
58
- edges: [
59
- { id: "edge-control", type: "control", from: { nodeId: "a", port: "out" }, to: { nodeId: "b", port: "in" } },
60
- { id: "edge-tool", type: "tool", from: { nodeId: "a", port: "tool-out" }, to: { nodeId: "b", port: "tool-in" } },
61
- {
62
- id: "edge-task",
63
- type: "agentTask",
64
- from: { nodeId: "agent", port: "tool" },
65
- to: { nodeId: "task", port: "trigger" },
66
- prompt: { expression: '"summarize"', references: [], dataType: "string" },
67
- },
68
- {
69
- id: "edge-choice",
70
- type: "agentChoice",
71
- from: { nodeId: "agent", port: "choice" },
72
- to: { nodeId: "branchA", port: "in" },
73
- description: "when the user asks about weather",
74
- },
75
- {
76
- id: "edge-delegate",
77
- type: "agentDelegate",
78
- from: { nodeId: "agent", port: "delegate" },
79
- to: { nodeId: "sub", port: "in" },
80
- prompt: { expression: '"continue"', references: [], dataType: "string" },
81
- description: "delegate everything else",
82
- },
83
- ],
84
- functions: [],
85
- declaredVariables: [],
86
- channels: [],
87
- memory: [],
88
- models: [],
89
- };
90
-
91
- const withFunctionCanvas: Schemas["Workflow"] = {
92
- schemaVersion: 1,
93
- nodes: [
94
- {
95
- id: "fcall",
96
- type: "FunctionCall",
97
- position: { x: 0, y: 0 },
98
- // The wire stores only the reference; the signature is resolved from
99
- // `functions[]` and the snapshot rebuilt on deserialize.
100
- functionId: "fn-uuid",
101
- arguments: {
102
- inputBindings: {
103
- "arg-x": { expression: "1", references: [], dataType: "int" },
104
- "arg-y": { expression: "2", references: [], dataType: "int" },
105
- },
106
- outputBindings: {
107
- "ret-sum": { active: true, mode: "emit", name: "result" },
108
- },
109
- },
110
- },
111
- ],
112
- edges: [],
113
- functions: [
114
- {
115
- functionInfo: {
116
- id: "fn-uuid",
117
- version: 1,
118
- name: "add",
119
- arguments: [
120
- { uid: "arg-x", name: "x", dataType: "int" },
121
- { uid: "arg-y", name: "y", dataType: "int" },
122
- ],
123
- returns: [{ uid: "ret-sum", name: "sum", dataType: "int" }],
124
- },
125
- outputAssignments: {
126
- "ret-sum": { expression: "x + y", references: [
127
- { srcId: "fnarg", varId: "arg-x" },
128
- { srcId: "fnarg", varId: "arg-y" },
129
- ], dataType: "int" },
130
- },
131
- nodes: [],
132
- edges: [],
133
- declaredVariables: [],
134
- },
135
- ],
136
- declaredVariables: [],
137
- channels: [],
138
- memory: [],
139
- models: [],
140
- };
141
-
142
- describe("workflowSerialization — reverse roundtrip (JSON → deserialize → serialize)", () => {
143
- it("empty workflow", () => {
144
- expect(serialize(deserialize(empty))).toEqual(empty);
145
- });
146
-
147
- it("main canvas with declared variables and one node", () => {
148
- expect(serialize(deserialize(mainOnly))).toEqual(mainOnly);
149
- });
150
-
151
- it("preserves all edge type variants verbatim, including ids", () => {
152
- const out = serialize(deserialize(allEdgeTypes));
153
- expect(out).toEqual(allEdgeTypes);
154
- // Specific guardrail: each edge id is preserved (the bug we fixed).
155
- const ids = out.edges.map((e) => e.id).sort();
156
- expect(ids).toEqual(["edge-choice", "edge-control", "edge-delegate", "edge-task", "edge-tool"]);
157
- });
158
-
159
- it("function canvas with fnargs, output assignments, and a FunctionCall on main", () => {
160
- const out = serialize(deserialize(withFunctionCanvas));
161
- expect(out).toEqual(withFunctionCanvas);
162
- });
163
- });
164
-
165
- // ============================================================================
166
- // Forward roundtrip: WorkflowState → serialize → deserialize → equality
167
- //
168
- // Variables are reconstructed from nodes + functionInfo + declared on every
169
- // deserialize. To get exact equality, build fixtures whose `variables` field
170
- // matches what reconstruction would produce (i.e., let buildCanvasVariables
171
- // produce the expected value for us).
172
- // ============================================================================
173
-
174
- function makeMainCanvas(nodes: Canvas["nodes"] = [], edges: Canvas["edges"] = [], declared: Schemas["Variable"][] = []): Canvas {
175
- return {
176
- nodes,
177
- edges,
178
- variables: buildCanvasVariables(nodes, [], declared),
179
- };
180
- }
181
-
182
- describe("workflowSerialization — forward roundtrip (state → serialize → deserialize)", () => {
183
- it("empty state roundtrips to a state with an empty main canvas", () => {
184
- const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas() }, functions: {}, channels: {}, memory: {}, models: {} };
185
- expect(deserialize(serialize(state))).toEqual(state);
186
- });
187
-
188
- it("preserves edge ids across serialize/deserialize", () => {
189
- const edges: Canvas["edges"] = [
190
- { id: "stable-id-A", type: "control", source: "n1", sourceHandle: "out", target: "n2", targetHandle: "in" },
191
- { id: "stable-id-B", type: "tool", source: "n1", sourceHandle: "tool", target: "n3", targetHandle: "in" },
192
- ];
193
- const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas([], edges) }, functions: {}, channels: {}, memory: {}, models: {} };
194
- const roundTripped = deserialize(serialize(state));
195
- expect(roundTripped.canvases[MAIN_CANVAS_ID]!.edges.map((e) => e.id)).toEqual(["stable-id-A", "stable-id-B"]);
196
- });
197
- });
198
-
199
- // ============================================================================
200
- // Variable reconstruction guardrails
201
- // ============================================================================
202
-
203
- describe("buildCanvasVariables", () => {
204
- it("reconstructs node-output variables from nodes via getNodeOutput", () => {
205
- const nodes: NodeData[] = [
206
- {
207
- id: "agent1",
208
- type: "Agent",
209
- arguments: {
210
- name: "a",
211
- model: "claude-opus-4-7",
212
- instructions: "",
213
- maxTurns: undefined,
214
- outputDeclarations: [],
215
- memoryRefs: [],
216
- answer: { active: true, mode: "emit", name: "answer" },
217
- },
218
- } as NodeData,
219
- ];
220
- const vars = computeVariablesFromNodes(nodes);
221
- // Agent emits at least one output (the answer); key is "<nodeId>:<outputId>".
222
- const keys = Object.keys(vars);
223
- expect(keys.length).toBeGreaterThan(0);
224
- expect(keys.every((k) => k.startsWith("agent1:"))).toBe(true);
225
- });
226
-
227
- it("merges declared + fnarg + node-output into disjoint key namespaces", () => {
228
- const declared: Schemas["Variable"][] = [{ uid: "d1", name: "x", dataType: "int" }];
229
- const fnInfo = {
230
- id: "fn",
231
- version: 1,
232
- name: "f",
233
- arguments: [{ uid: "a1", name: "arg1", dataType: "int" as const }],
234
- returns: [],
235
- };
236
- const merged = buildCanvasVariables([], fnInfo.arguments, declared);
237
- expect(merged["declared:d1"]).toMatchObject({ kind: "declared", uid: "d1" });
238
- expect(merged["fnarg:a1"]).toMatchObject({ kind: "fnarg", uid: "a1" });
239
- });
240
- });
1
+ import { describe, it, expect } from "vitest";
2
+ import { serialize, deserialize, computeVariablesFromNodes, buildCanvasVariables } from "./serialization";
3
+ import { MAIN_CANVAS_ID, type Workflow, type Canvas } from "./Workflow";
4
+ import type { Schemas } from "../api";
5
+ import type { NodeData } from "../node";
6
+
7
+ // ============================================================================
8
+ // Reverse roundtrip: api JSON → deserialize → serialize → deep-equal JSON
9
+ //
10
+ // This direction is the strongest invariant — deserialize is the function
11
+ // that reconstructs derivable state (variable records); if it gets the rest
12
+ // right, re-serialize must produce the same JSON back. Tests every code path
13
+ // in deserialize + serialize without separately constructing WorkflowState.
14
+ // ============================================================================
15
+
16
+ const empty: Schemas["Workflow"] = {
17
+ schemaVersion: 1,
18
+ nodes: [],
19
+ edges: [],
20
+ functions: [],
21
+ declaredVariables: [],
22
+ channels: [],
23
+ memory: [],
24
+ models: [],
25
+ };
26
+
27
+ const mainOnly: Schemas["Workflow"] = {
28
+ schemaVersion: 1,
29
+ nodes: [
30
+ {
31
+ id: "n1",
32
+ type: "Agent",
33
+ position: { x: 100, y: 200 },
34
+ label: "My Agent",
35
+ arguments: {
36
+ name: "agent-1",
37
+ model: "claude-opus-4-7",
38
+ instructions: "be helpful",
39
+ outputDeclarations: [],
40
+ memoryRefs: [],
41
+ answer: { active: true, mode: "emit", name: "answer" },
42
+ },
43
+ },
44
+ ],
45
+ edges: [],
46
+ functions: [],
47
+ declaredVariables: [
48
+ { uid: "d1", name: "counter", dataType: "int", initialValue: 0 },
49
+ ],
50
+ channels: [],
51
+ memory: [],
52
+ models: [],
53
+ };
54
+
55
+ const allEdgeTypes: Schemas["Workflow"] = {
56
+ schemaVersion: 1,
57
+ nodes: [],
58
+ edges: [
59
+ { id: "edge-control", type: "control", from: { nodeId: "a", port: "out" }, to: { nodeId: "b", port: "in" } },
60
+ { id: "edge-tool", type: "tool", from: { nodeId: "a", port: "tool-out" }, to: { nodeId: "b", port: "tool-in" } },
61
+ {
62
+ id: "edge-task",
63
+ type: "agentTask",
64
+ from: { nodeId: "agent", port: "tool" },
65
+ to: { nodeId: "task", port: "trigger" },
66
+ prompt: { expression: '"summarize"', references: [], dataType: "string" },
67
+ },
68
+ {
69
+ id: "edge-choice",
70
+ type: "agentChoice",
71
+ from: { nodeId: "agent", port: "choice" },
72
+ to: { nodeId: "branchA", port: "in" },
73
+ description: "when the user asks about weather",
74
+ },
75
+ {
76
+ id: "edge-delegate",
77
+ type: "agentDelegate",
78
+ from: { nodeId: "agent", port: "delegate" },
79
+ to: { nodeId: "sub", port: "in" },
80
+ prompt: { expression: '"continue"', references: [], dataType: "string" },
81
+ description: "delegate everything else",
82
+ },
83
+ ],
84
+ functions: [],
85
+ declaredVariables: [],
86
+ channels: [],
87
+ memory: [],
88
+ models: [],
89
+ };
90
+
91
+ const withFunctionCanvas: Schemas["Workflow"] = {
92
+ schemaVersion: 1,
93
+ nodes: [
94
+ {
95
+ id: "fcall",
96
+ type: "FunctionCall",
97
+ position: { x: 0, y: 0 },
98
+ // The wire stores only the reference; the signature is resolved from
99
+ // `functions[]` and the snapshot rebuilt on deserialize.
100
+ functionId: "fn-uuid",
101
+ arguments: {
102
+ inputBindings: {
103
+ "arg-x": { expression: "1", references: [], dataType: "int" },
104
+ "arg-y": { expression: "2", references: [], dataType: "int" },
105
+ },
106
+ outputBindings: {
107
+ "ret-sum": { active: true, mode: "emit", name: "result" },
108
+ },
109
+ },
110
+ },
111
+ ],
112
+ edges: [],
113
+ functions: [
114
+ {
115
+ functionInfo: {
116
+ id: "fn-uuid",
117
+ version: 1,
118
+ name: "add",
119
+ arguments: [
120
+ { uid: "arg-x", name: "x", dataType: "int" },
121
+ { uid: "arg-y", name: "y", dataType: "int" },
122
+ ],
123
+ returns: [{ uid: "ret-sum", name: "sum", dataType: "int" }],
124
+ },
125
+ outputAssignments: {
126
+ "ret-sum": { expression: "x + y", references: [
127
+ { srcId: "fnarg", varId: "arg-x" },
128
+ { srcId: "fnarg", varId: "arg-y" },
129
+ ], dataType: "int" },
130
+ },
131
+ nodes: [],
132
+ edges: [],
133
+ declaredVariables: [],
134
+ },
135
+ ],
136
+ declaredVariables: [],
137
+ channels: [],
138
+ memory: [],
139
+ models: [],
140
+ };
141
+
142
+ describe("workflowSerialization — reverse roundtrip (JSON → deserialize → serialize)", () => {
143
+ it("empty workflow", () => {
144
+ expect(serialize(deserialize(empty))).toEqual(empty);
145
+ });
146
+
147
+ it("main canvas with declared variables and one node", () => {
148
+ expect(serialize(deserialize(mainOnly))).toEqual(mainOnly);
149
+ });
150
+
151
+ it("preserves all edge type variants verbatim, including ids", () => {
152
+ const out = serialize(deserialize(allEdgeTypes));
153
+ expect(out).toEqual(allEdgeTypes);
154
+ // Specific guardrail: each edge id is preserved (the bug we fixed).
155
+ const ids = out.edges.map((e) => e.id).sort();
156
+ expect(ids).toEqual(["edge-choice", "edge-control", "edge-delegate", "edge-task", "edge-tool"]);
157
+ });
158
+
159
+ it("function canvas with fnargs, output assignments, and a FunctionCall on main", () => {
160
+ const out = serialize(deserialize(withFunctionCanvas));
161
+ expect(out).toEqual(withFunctionCanvas);
162
+ });
163
+ });
164
+
165
+ // ============================================================================
166
+ // Forward roundtrip: WorkflowState → serialize → deserialize → equality
167
+ //
168
+ // Variables are reconstructed from nodes + functionInfo + declared on every
169
+ // deserialize. To get exact equality, build fixtures whose `variables` field
170
+ // matches what reconstruction would produce (i.e., let buildCanvasVariables
171
+ // produce the expected value for us).
172
+ // ============================================================================
173
+
174
+ function makeMainCanvas(nodes: Canvas["nodes"] = [], edges: Canvas["edges"] = [], declared: Schemas["Variable"][] = []): Canvas {
175
+ return {
176
+ nodes,
177
+ edges,
178
+ variables: buildCanvasVariables(nodes, [], declared),
179
+ };
180
+ }
181
+
182
+ describe("workflowSerialization — forward roundtrip (state → serialize → deserialize)", () => {
183
+ it("empty state roundtrips to a state with an empty main canvas", () => {
184
+ const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas() }, functions: {}, channels: {}, memory: {}, models: {} };
185
+ expect(deserialize(serialize(state))).toEqual(state);
186
+ });
187
+
188
+ it("preserves edge ids across serialize/deserialize", () => {
189
+ const edges: Canvas["edges"] = [
190
+ { id: "stable-id-A", type: "control", source: "n1", sourceHandle: "out", target: "n2", targetHandle: "in" },
191
+ { id: "stable-id-B", type: "tool", source: "n1", sourceHandle: "tool", target: "n3", targetHandle: "in" },
192
+ ];
193
+ const state: Workflow = { canvases: { [MAIN_CANVAS_ID]: makeMainCanvas([], edges) }, functions: {}, channels: {}, memory: {}, models: {} };
194
+ const roundTripped = deserialize(serialize(state));
195
+ expect(roundTripped.canvases[MAIN_CANVAS_ID]!.edges.map((e) => e.id)).toEqual(["stable-id-A", "stable-id-B"]);
196
+ });
197
+ });
198
+
199
+ // ============================================================================
200
+ // Variable reconstruction guardrails
201
+ // ============================================================================
202
+
203
+ describe("buildCanvasVariables", () => {
204
+ it("reconstructs node-output variables from nodes via getNodeOutput", () => {
205
+ const nodes: NodeData[] = [
206
+ {
207
+ id: "agent1",
208
+ type: "Agent",
209
+ arguments: {
210
+ name: "a",
211
+ model: "claude-opus-4-7",
212
+ instructions: "",
213
+ maxTurns: undefined,
214
+ outputDeclarations: [],
215
+ memoryRefs: [],
216
+ answer: { active: true, mode: "emit", name: "answer" },
217
+ },
218
+ } as NodeData,
219
+ ];
220
+ const vars = computeVariablesFromNodes(nodes);
221
+ // Agent emits at least one output (the answer); key is "<nodeId>:<outputId>".
222
+ const keys = Object.keys(vars);
223
+ expect(keys.length).toBeGreaterThan(0);
224
+ expect(keys.every((k) => k.startsWith("agent1:"))).toBe(true);
225
+ });
226
+
227
+ it("merges declared + fnarg + node-output into disjoint key namespaces", () => {
228
+ const declared: Schemas["Variable"][] = [{ uid: "d1", name: "x", dataType: "int" }];
229
+ const fnInfo = {
230
+ id: "fn",
231
+ version: 1,
232
+ name: "f",
233
+ arguments: [{ uid: "a1", name: "arg1", dataType: "int" as const }],
234
+ returns: [],
235
+ };
236
+ const merged = buildCanvasVariables([], fnInfo.arguments, declared);
237
+ expect(merged["declared:d1"]).toMatchObject({ kind: "declared", uid: "d1" });
238
+ expect(merged["fnarg:a1"]).toMatchObject({ kind: "fnarg", uid: "a1" });
239
+ });
240
+ });