@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.
- package/LICENSE +202 -202
- package/NOTICE +14 -14
- package/README.md +63 -63
- package/dist/api/workflow.d.ts +2 -2
- package/dist/api/workflow.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/api/index.ts +11 -11
- package/src/api/workflow.ts +607 -607
- package/src/channel/Channel.ts +11 -11
- package/src/channel/ChannelDefinition.ts +76 -76
- package/src/channel/index.ts +6 -6
- package/src/channel/serialization.ts +68 -68
- package/src/deploy/index.ts +1 -1
- package/src/deploy/requirements.test.ts +61 -61
- package/src/deploy/requirements.ts +41 -41
- package/src/diagnostics/__fixtures__/diagnosticFixtures.ts +158 -158
- package/src/diagnostics/diagnostics.test.ts +878 -878
- package/src/diagnostics/diagnostics.ts +936 -936
- package/src/diagnostics/index.ts +11 -11
- package/src/edge/Edge.ts +23 -23
- package/src/edge/EdgeDefinition.ts +45 -45
- package/src/edge/EdgeType.ts +19 -19
- package/src/edge/index.ts +8 -8
- package/src/edge/serialization.ts +83 -83
- package/src/expression/index.ts +4 -4
- package/src/expression/parser.ts +362 -362
- package/src/expression/types.ts +30 -30
- package/src/function/FunctionDeclaration.ts +54 -54
- package/src/function/index.ts +3 -3
- package/src/function/serialization.ts +40 -40
- package/src/globals.d.ts +9 -9
- package/src/id/index.ts +8 -8
- package/src/index.ts +22 -22
- package/src/memory/Memory.ts +15 -15
- package/src/memory/MemoryDefinition.ts +16 -16
- package/src/memory/MemoryFileDefinition.ts +37 -37
- package/src/memory/MemoryRegistry.ts +35 -35
- package/src/memory/VectorDatabaseDefinition.ts +21 -21
- package/src/memory/index.ts +8 -8
- package/src/memory/serialization.ts +47 -47
- package/src/migration/index.ts +4 -4
- package/src/migration/migrate.test.ts +44 -44
- package/src/migration/migrate.ts +58 -58
- package/src/migration/migrations.ts +24 -24
- package/src/migration/version.ts +9 -9
- package/src/model/LLMModelDefinition.ts +12 -12
- package/src/model/Model.ts +39 -39
- package/src/model/ModelDefinition.ts +15 -15
- package/src/model/ModelRegistry.ts +33 -33
- package/src/model/index.ts +7 -7
- package/src/model/serialization.ts +30 -30
- package/src/node/AgentNode.ts +82 -82
- package/src/node/DataNode.ts +41 -41
- package/src/node/FunctionNode.ts +76 -76
- package/src/node/InputNode.ts +185 -185
- package/src/node/LogicNode.ts +33 -33
- package/src/node/MqttNode.ts +127 -127
- package/src/node/Node.ts +61 -61
- package/src/node/NodeDefinition.ts +37 -37
- package/src/node/NodeRegistry.ts +85 -85
- package/src/node/OutputNode.ts +87 -87
- package/src/node/ToolNode.ts +32 -32
- package/src/node/TriggerNode.ts +272 -272
- package/src/node/constants.ts +16 -16
- package/src/node/index.ts +26 -26
- package/src/node/methods.ts +278 -278
- package/src/node/serialization.ts +544 -544
- package/src/parameter/OutputParameter.ts +68 -68
- package/src/parameter/Parameter.ts +243 -243
- package/src/parameter/index.ts +33 -33
- package/src/variable/Variable.ts +10 -10
- package/src/variable/index.ts +16 -16
- package/src/variable/operations.ts +106 -106
- package/src/workflow/Workflow.ts +41 -41
- package/src/workflow/index.ts +3 -3
- package/src/workflow/serialization.test.ts +240 -240
- package/src/workflow/serialization.ts +242 -242
|
@@ -1,242 +1,242 @@
|
|
|
1
|
-
// Pure conversion between the api format (Schemas["Workflow"])
|
|
2
|
-
// and the domain shape (Workflow).
|
|
3
|
-
// Two producers feed serialize: the editor reads its live stores into
|
|
4
|
-
// a Workflow literal; the CLI calls deserialize on parsed JSON.
|
|
5
|
-
|
|
6
|
-
import type { Schemas } from "../api";
|
|
7
|
-
import type { NodeData } from "../node";
|
|
8
|
-
import type { FunctionDeclaration, FunctionInfo } from "../function";
|
|
9
|
-
import { serialize as serializeFunction, deserialize as deserializeFunction } from "../function";
|
|
10
|
-
import { getNodeOutput, isNodeUsedAsTool } from "../node/methods";
|
|
11
|
-
import { ALL_CHANNEL_TYPES, type ApiChannel, type Channel, type ChannelType } from "../channel";
|
|
12
|
-
import { serialize as serializeChannel, deserialize as deserializeChannel } from "../channel";
|
|
13
|
-
import type { Memory } from "../memory";
|
|
14
|
-
import { serialize as serializeMemory, deserialize as deserializeMemory } from "../memory";
|
|
15
|
-
import type { Model } from "../model";
|
|
16
|
-
import { serialize as serializeModel, deserialize as deserializeModel } from "../model";
|
|
17
|
-
import { serialize as serializeNode, deserialize as deserializeNode } from "../node";
|
|
18
|
-
import { serialize as serializeEdge, deserialize as deserializeEdge } from "../edge";
|
|
19
|
-
import type { Variable, NodeOutputVariable } from "../variable";
|
|
20
|
-
import { declaredVarKey, fnargKey, nodeOutputVarKey, ensureUids } from "../variable";
|
|
21
|
-
import { MAIN_CANVAS_ID, type Workflow, type Canvas } from "./Workflow";
|
|
22
|
-
import { CURRENT_SCHEMA_VERSION } from "../migration";
|
|
23
|
-
|
|
24
|
-
const KNOWN_CHANNEL_TYPES = new Set<ChannelType>(ALL_CHANNEL_TYPES);
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Serialize domain → api Workflow. Multi-canvas
|
|
28
|
-
* mapping: the `main` canvas's nodes/edges/declaredVariables land at the
|
|
29
|
-
* root of `Workflow`; every other canvas becomes a `Function` entry in
|
|
30
|
-
* `Workflow.functions[]`, carrying its own functionInfo + outputAssignments.
|
|
31
|
-
*/
|
|
32
|
-
export function serialize(state: Workflow): Schemas["Workflow"] {
|
|
33
|
-
const mainCanvas = state.canvases[MAIN_CANVAS_ID];
|
|
34
|
-
if (!mainCanvas) {
|
|
35
|
-
throw new Error("Main canvas missing");
|
|
36
|
-
}
|
|
37
|
-
const mainNodes = mainCanvas.nodes.map((n) => serializeNode(n, isNodeUsedAsTool(n.id, n, mainCanvas.edges)));
|
|
38
|
-
const mainEdges = mainCanvas.edges.map(serializeEdge);
|
|
39
|
-
const mainDeclared = extractDeclaredVariables(mainCanvas.variables);
|
|
40
|
-
|
|
41
|
-
// Each declaration + its body canvas (joined by id) becomes one wire Function.
|
|
42
|
-
// The declaration splits into functionInfo + outputAssignments via serializeFunction.
|
|
43
|
-
const functions: Schemas["Function"][] = [];
|
|
44
|
-
for (const [id, decl] of Object.entries(state.functions)) {
|
|
45
|
-
const body = state.canvases[id];
|
|
46
|
-
if (!body) {
|
|
47
|
-
throw new Error(`[workflow-core] function ${id} has no body canvas — cannot serialize`);
|
|
48
|
-
}
|
|
49
|
-
const { functionInfo, outputAssignments } = serializeFunction(decl);
|
|
50
|
-
functions.push({
|
|
51
|
-
functionInfo,
|
|
52
|
-
outputAssignments,
|
|
53
|
-
nodes: body.nodes.map((n) => serializeNode(n, isNodeUsedAsTool(n.id, n, body.edges))),
|
|
54
|
-
edges: body.edges.map(serializeEdge),
|
|
55
|
-
declaredVariables: extractDeclaredVariables(body.variables),
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const channels = Object.values(state.channels).map(serializeChannel);
|
|
60
|
-
const memory = Object.values(state.memory).map(serializeMemory);
|
|
61
|
-
const models = Object.values(state.models).map(serializeModel);
|
|
62
|
-
return {
|
|
63
|
-
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
64
|
-
nodes: mainNodes,
|
|
65
|
-
edges: mainEdges,
|
|
66
|
-
functions,
|
|
67
|
-
declaredVariables: mainDeclared,
|
|
68
|
-
channels,
|
|
69
|
-
memory,
|
|
70
|
-
models,
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Deserialize api → domain Workflow. Variable
|
|
76
|
-
* records are reconstructed per canvas (declared from the api; fnarg
|
|
77
|
-
* from `functionInfo.arguments`; node-output via {@link computeVariablesFromNodes})
|
|
78
|
-
* since the api intentionally carries only `declaredVariables` to avoid
|
|
79
|
-
* redundancy.
|
|
80
|
-
*
|
|
81
|
-
* Each node is a flat {@link Node} (domain `NodeData` + `position`);
|
|
82
|
-
* its `type` is the domain node type (e.g. "Agent"). Workflow-builder projects
|
|
83
|
-
* that into a React Flow display type during store hydration — editor-only.
|
|
84
|
-
*/
|
|
85
|
-
export function deserialize(workflow: Schemas["Workflow"]): Workflow {
|
|
86
|
-
const canvases: Record<string, Canvas> = {};
|
|
87
|
-
const functions: Record<string, FunctionDeclaration> = {};
|
|
88
|
-
|
|
89
|
-
// Pre-build the function-info table so FunctionCall nodes — on the main canvas or
|
|
90
|
-
// in any function body — can rebuild their signature snapshot from the wire's
|
|
91
|
-
// `functionId`. (The wire stores only the reference; the engine likewise resolves
|
|
92
|
-
// by id, so the snapshot is editor-side state reconstructed here.)
|
|
93
|
-
const functionInfos: Record<string, FunctionInfo> = {};
|
|
94
|
-
for (const fn of workflow.functions ?? []) {
|
|
95
|
-
functionInfos[fn.functionInfo.id] = {
|
|
96
|
-
...fn.functionInfo,
|
|
97
|
-
arguments: ensureUids(fn.functionInfo.arguments),
|
|
98
|
-
returns: ensureUids(fn.functionInfo.returns),
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
const resolveFunctionInfo = (id: string): FunctionInfo | undefined => functionInfos[id];
|
|
102
|
-
|
|
103
|
-
// Main canvas: data at the api root. No function declaration.
|
|
104
|
-
const mainNodes = workflow.nodes.map((n) => deserializeNode(n, resolveFunctionInfo));
|
|
105
|
-
const mainEdges = workflow.edges.map(deserializeEdge);
|
|
106
|
-
const mainDeclared = workflow.declaredVariables ?? [];
|
|
107
|
-
canvases[MAIN_CANVAS_ID] = {
|
|
108
|
-
nodes: mainNodes,
|
|
109
|
-
edges: mainEdges,
|
|
110
|
-
variables: buildCanvasVariables(mainNodes, [], mainDeclared),
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
// Each wire Function splits into a project-scoped declaration (functions[id]) and
|
|
114
|
-
// its body canvas (canvases[id]), joined by the function id.
|
|
115
|
-
for (const fn of workflow.functions ?? []) {
|
|
116
|
-
const functionInfo = functionInfos[fn.functionInfo.id]!;
|
|
117
|
-
const decl = deserializeFunction(functionInfo, fn.outputAssignments ?? {});
|
|
118
|
-
functions[decl.id] = decl;
|
|
119
|
-
const fnNodes = fn.nodes.map((n) => deserializeNode(n, resolveFunctionInfo));
|
|
120
|
-
const fnEdges = fn.edges.map(deserializeEdge);
|
|
121
|
-
canvases[decl.id] = {
|
|
122
|
-
nodes: fnNodes,
|
|
123
|
-
edges: fnEdges,
|
|
124
|
-
variables: buildCanvasVariables(fnNodes, decl.arguments, fn.declaredVariables ?? []),
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const channels: Record<string, Channel> = {};
|
|
129
|
-
for (const c of workflow.channels ?? []) {
|
|
130
|
-
if (!KNOWN_CHANNEL_TYPES.has(c.type as ChannelType)) continue;
|
|
131
|
-
const instance = deserializeChannel(c as ApiChannel);
|
|
132
|
-
channels[instance.id] = instance;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const memory: Record<string, Memory> = {};
|
|
136
|
-
for (const m of workflow.memory ?? []) {
|
|
137
|
-
const instance = deserializeMemory(m);
|
|
138
|
-
memory[instance.id] = instance;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
const models: Record<string, Model> = {};
|
|
142
|
-
for (const m of workflow.models ?? []) {
|
|
143
|
-
const instance = deserializeModel(m);
|
|
144
|
-
models[instance.id] = instance;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
canvases,
|
|
149
|
-
functions,
|
|
150
|
-
channels,
|
|
151
|
-
memory,
|
|
152
|
-
models,
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
// ============================================================================
|
|
157
|
-
// Variable reconstruction
|
|
158
|
-
// ============================================================================
|
|
159
|
-
|
|
160
|
-
/**
|
|
161
|
-
* Derive node-output variables (`{kind: "node", nodeId, outputId, ...}`)
|
|
162
|
-
* from an array of node instances. Calls {@link getNodeOutput} to inspect
|
|
163
|
-
* each node's declared outputs.
|
|
164
|
-
*
|
|
165
|
-
* Ported from workflow-builder's canvasStore to take NodeData[] directly
|
|
166
|
-
* (NodeData carries id at the top level — no React Flow wrapper needed).
|
|
167
|
-
*/
|
|
168
|
-
export function computeVariablesFromNodes(nodes: NodeData[]): Record<string, NodeOutputVariable> {
|
|
169
|
-
const out: Record<string, NodeOutputVariable> = {};
|
|
170
|
-
for (const node of nodes) {
|
|
171
|
-
for (const [outputId, variable] of Object.entries(getNodeOutput(node))) {
|
|
172
|
-
out[nodeOutputVarKey(node.id, outputId)] = {
|
|
173
|
-
kind: "node",
|
|
174
|
-
nodeId: node.id,
|
|
175
|
-
outputId,
|
|
176
|
-
name: variable.name,
|
|
177
|
-
dataType: variable.dataType,
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
return out;
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
/**
|
|
185
|
-
* Merge the three variable sources into a single per-canvas record:
|
|
186
|
-
* declared (from api)
|
|
187
|
-
* + fnarg (derived from the function's `arguments` — empty for the main canvas)
|
|
188
|
-
* + nodeOutput (derived from nodes via {@link computeVariablesFromNodes})
|
|
189
|
-
*
|
|
190
|
-
* Disjoint key namespaces (`declared:`, `fnarg:`, `<nodeId>:<outputId>`) so
|
|
191
|
-
* merge order is irrelevant.
|
|
192
|
-
*/
|
|
193
|
-
export function buildCanvasVariables(
|
|
194
|
-
nodes: NodeData[],
|
|
195
|
-
fnArgs: readonly Schemas["Variable"][],
|
|
196
|
-
declaredVariables: readonly Schemas["Variable"][],
|
|
197
|
-
): Record<string, Variable> {
|
|
198
|
-
const variables: Record<string, Variable> = computeVariablesFromNodes(nodes);
|
|
199
|
-
for (const arg of fnArgs) {
|
|
200
|
-
variables[fnargKey(arg.uid)] = {
|
|
201
|
-
kind: "fnarg",
|
|
202
|
-
uid: arg.uid,
|
|
203
|
-
name: arg.name,
|
|
204
|
-
dataType: arg.dataType,
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
for (const dv of declaredVariables) {
|
|
208
|
-
variables[declaredVarKey(dv.uid)] = {
|
|
209
|
-
kind: "declared",
|
|
210
|
-
uid: dv.uid,
|
|
211
|
-
name: dv.name,
|
|
212
|
-
dataType: dv.dataType,
|
|
213
|
-
...(dv.initialValue !== undefined ? { initialValue: dv.initialValue } : {}),
|
|
214
|
-
};
|
|
215
|
-
}
|
|
216
|
-
return variables;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
// ============================================================================
|
|
220
|
-
// Helpers — declared variables
|
|
221
|
-
// ============================================================================
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Filter a canvas's variables down to the declared-kind entries (the only
|
|
225
|
-
* variety the api persists). Node-output and fnarg variables are
|
|
226
|
-
* reconstructed on deserialize from nodes + functionInfo.
|
|
227
|
-
*/
|
|
228
|
-
function extractDeclaredVariables(variables: Record<string, Variable>): Schemas["Variable"][] {
|
|
229
|
-
const result: Schemas["Variable"][] = [];
|
|
230
|
-
for (const v of Object.values(variables)) {
|
|
231
|
-
if (v.kind === "declared") {
|
|
232
|
-
result.push({
|
|
233
|
-
uid: v.uid,
|
|
234
|
-
name: v.name,
|
|
235
|
-
dataType: v.dataType,
|
|
236
|
-
...(v.initialValue !== undefined ? { initialValue: v.initialValue } : {}),
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
return result;
|
|
241
|
-
}
|
|
242
|
-
|
|
1
|
+
// Pure conversion between the api format (Schemas["Workflow"])
|
|
2
|
+
// and the domain shape (Workflow).
|
|
3
|
+
// Two producers feed serialize: the editor reads its live stores into
|
|
4
|
+
// a Workflow literal; the CLI calls deserialize on parsed JSON.
|
|
5
|
+
|
|
6
|
+
import type { Schemas } from "../api";
|
|
7
|
+
import type { NodeData } from "../node";
|
|
8
|
+
import type { FunctionDeclaration, FunctionInfo } from "../function";
|
|
9
|
+
import { serialize as serializeFunction, deserialize as deserializeFunction } from "../function";
|
|
10
|
+
import { getNodeOutput, isNodeUsedAsTool } from "../node/methods";
|
|
11
|
+
import { ALL_CHANNEL_TYPES, type ApiChannel, type Channel, type ChannelType } from "../channel";
|
|
12
|
+
import { serialize as serializeChannel, deserialize as deserializeChannel } from "../channel";
|
|
13
|
+
import type { Memory } from "../memory";
|
|
14
|
+
import { serialize as serializeMemory, deserialize as deserializeMemory } from "../memory";
|
|
15
|
+
import type { Model } from "../model";
|
|
16
|
+
import { serialize as serializeModel, deserialize as deserializeModel } from "../model";
|
|
17
|
+
import { serialize as serializeNode, deserialize as deserializeNode } from "../node";
|
|
18
|
+
import { serialize as serializeEdge, deserialize as deserializeEdge } from "../edge";
|
|
19
|
+
import type { Variable, NodeOutputVariable } from "../variable";
|
|
20
|
+
import { declaredVarKey, fnargKey, nodeOutputVarKey, ensureUids } from "../variable";
|
|
21
|
+
import { MAIN_CANVAS_ID, type Workflow, type Canvas } from "./Workflow";
|
|
22
|
+
import { CURRENT_SCHEMA_VERSION } from "../migration";
|
|
23
|
+
|
|
24
|
+
const KNOWN_CHANNEL_TYPES = new Set<ChannelType>(ALL_CHANNEL_TYPES);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Serialize domain → api Workflow. Multi-canvas
|
|
28
|
+
* mapping: the `main` canvas's nodes/edges/declaredVariables land at the
|
|
29
|
+
* root of `Workflow`; every other canvas becomes a `Function` entry in
|
|
30
|
+
* `Workflow.functions[]`, carrying its own functionInfo + outputAssignments.
|
|
31
|
+
*/
|
|
32
|
+
export function serialize(state: Workflow): Schemas["Workflow"] {
|
|
33
|
+
const mainCanvas = state.canvases[MAIN_CANVAS_ID];
|
|
34
|
+
if (!mainCanvas) {
|
|
35
|
+
throw new Error("Main canvas missing");
|
|
36
|
+
}
|
|
37
|
+
const mainNodes = mainCanvas.nodes.map((n) => serializeNode(n, isNodeUsedAsTool(n.id, n, mainCanvas.edges)));
|
|
38
|
+
const mainEdges = mainCanvas.edges.map(serializeEdge);
|
|
39
|
+
const mainDeclared = extractDeclaredVariables(mainCanvas.variables);
|
|
40
|
+
|
|
41
|
+
// Each declaration + its body canvas (joined by id) becomes one wire Function.
|
|
42
|
+
// The declaration splits into functionInfo + outputAssignments via serializeFunction.
|
|
43
|
+
const functions: Schemas["Function"][] = [];
|
|
44
|
+
for (const [id, decl] of Object.entries(state.functions)) {
|
|
45
|
+
const body = state.canvases[id];
|
|
46
|
+
if (!body) {
|
|
47
|
+
throw new Error(`[workflow-core] function ${id} has no body canvas — cannot serialize`);
|
|
48
|
+
}
|
|
49
|
+
const { functionInfo, outputAssignments } = serializeFunction(decl);
|
|
50
|
+
functions.push({
|
|
51
|
+
functionInfo,
|
|
52
|
+
outputAssignments,
|
|
53
|
+
nodes: body.nodes.map((n) => serializeNode(n, isNodeUsedAsTool(n.id, n, body.edges))),
|
|
54
|
+
edges: body.edges.map(serializeEdge),
|
|
55
|
+
declaredVariables: extractDeclaredVariables(body.variables),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const channels = Object.values(state.channels).map(serializeChannel);
|
|
60
|
+
const memory = Object.values(state.memory).map(serializeMemory);
|
|
61
|
+
const models = Object.values(state.models).map(serializeModel);
|
|
62
|
+
return {
|
|
63
|
+
schemaVersion: CURRENT_SCHEMA_VERSION,
|
|
64
|
+
nodes: mainNodes,
|
|
65
|
+
edges: mainEdges,
|
|
66
|
+
functions,
|
|
67
|
+
declaredVariables: mainDeclared,
|
|
68
|
+
channels,
|
|
69
|
+
memory,
|
|
70
|
+
models,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Deserialize api → domain Workflow. Variable
|
|
76
|
+
* records are reconstructed per canvas (declared from the api; fnarg
|
|
77
|
+
* from `functionInfo.arguments`; node-output via {@link computeVariablesFromNodes})
|
|
78
|
+
* since the api intentionally carries only `declaredVariables` to avoid
|
|
79
|
+
* redundancy.
|
|
80
|
+
*
|
|
81
|
+
* Each node is a flat {@link Node} (domain `NodeData` + `position`);
|
|
82
|
+
* its `type` is the domain node type (e.g. "Agent"). Workflow-builder projects
|
|
83
|
+
* that into a React Flow display type during store hydration — editor-only.
|
|
84
|
+
*/
|
|
85
|
+
export function deserialize(workflow: Schemas["Workflow"]): Workflow {
|
|
86
|
+
const canvases: Record<string, Canvas> = {};
|
|
87
|
+
const functions: Record<string, FunctionDeclaration> = {};
|
|
88
|
+
|
|
89
|
+
// Pre-build the function-info table so FunctionCall nodes — on the main canvas or
|
|
90
|
+
// in any function body — can rebuild their signature snapshot from the wire's
|
|
91
|
+
// `functionId`. (The wire stores only the reference; the engine likewise resolves
|
|
92
|
+
// by id, so the snapshot is editor-side state reconstructed here.)
|
|
93
|
+
const functionInfos: Record<string, FunctionInfo> = {};
|
|
94
|
+
for (const fn of workflow.functions ?? []) {
|
|
95
|
+
functionInfos[fn.functionInfo.id] = {
|
|
96
|
+
...fn.functionInfo,
|
|
97
|
+
arguments: ensureUids(fn.functionInfo.arguments),
|
|
98
|
+
returns: ensureUids(fn.functionInfo.returns),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const resolveFunctionInfo = (id: string): FunctionInfo | undefined => functionInfos[id];
|
|
102
|
+
|
|
103
|
+
// Main canvas: data at the api root. No function declaration.
|
|
104
|
+
const mainNodes = workflow.nodes.map((n) => deserializeNode(n, resolveFunctionInfo));
|
|
105
|
+
const mainEdges = workflow.edges.map(deserializeEdge);
|
|
106
|
+
const mainDeclared = workflow.declaredVariables ?? [];
|
|
107
|
+
canvases[MAIN_CANVAS_ID] = {
|
|
108
|
+
nodes: mainNodes,
|
|
109
|
+
edges: mainEdges,
|
|
110
|
+
variables: buildCanvasVariables(mainNodes, [], mainDeclared),
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
// Each wire Function splits into a project-scoped declaration (functions[id]) and
|
|
114
|
+
// its body canvas (canvases[id]), joined by the function id.
|
|
115
|
+
for (const fn of workflow.functions ?? []) {
|
|
116
|
+
const functionInfo = functionInfos[fn.functionInfo.id]!;
|
|
117
|
+
const decl = deserializeFunction(functionInfo, fn.outputAssignments ?? {});
|
|
118
|
+
functions[decl.id] = decl;
|
|
119
|
+
const fnNodes = fn.nodes.map((n) => deserializeNode(n, resolveFunctionInfo));
|
|
120
|
+
const fnEdges = fn.edges.map(deserializeEdge);
|
|
121
|
+
canvases[decl.id] = {
|
|
122
|
+
nodes: fnNodes,
|
|
123
|
+
edges: fnEdges,
|
|
124
|
+
variables: buildCanvasVariables(fnNodes, decl.arguments, fn.declaredVariables ?? []),
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const channels: Record<string, Channel> = {};
|
|
129
|
+
for (const c of workflow.channels ?? []) {
|
|
130
|
+
if (!KNOWN_CHANNEL_TYPES.has(c.type as ChannelType)) continue;
|
|
131
|
+
const instance = deserializeChannel(c as ApiChannel);
|
|
132
|
+
channels[instance.id] = instance;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const memory: Record<string, Memory> = {};
|
|
136
|
+
for (const m of workflow.memory ?? []) {
|
|
137
|
+
const instance = deserializeMemory(m);
|
|
138
|
+
memory[instance.id] = instance;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const models: Record<string, Model> = {};
|
|
142
|
+
for (const m of workflow.models ?? []) {
|
|
143
|
+
const instance = deserializeModel(m);
|
|
144
|
+
models[instance.id] = instance;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
canvases,
|
|
149
|
+
functions,
|
|
150
|
+
channels,
|
|
151
|
+
memory,
|
|
152
|
+
models,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ============================================================================
|
|
157
|
+
// Variable reconstruction
|
|
158
|
+
// ============================================================================
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Derive node-output variables (`{kind: "node", nodeId, outputId, ...}`)
|
|
162
|
+
* from an array of node instances. Calls {@link getNodeOutput} to inspect
|
|
163
|
+
* each node's declared outputs.
|
|
164
|
+
*
|
|
165
|
+
* Ported from workflow-builder's canvasStore to take NodeData[] directly
|
|
166
|
+
* (NodeData carries id at the top level — no React Flow wrapper needed).
|
|
167
|
+
*/
|
|
168
|
+
export function computeVariablesFromNodes(nodes: NodeData[]): Record<string, NodeOutputVariable> {
|
|
169
|
+
const out: Record<string, NodeOutputVariable> = {};
|
|
170
|
+
for (const node of nodes) {
|
|
171
|
+
for (const [outputId, variable] of Object.entries(getNodeOutput(node))) {
|
|
172
|
+
out[nodeOutputVarKey(node.id, outputId)] = {
|
|
173
|
+
kind: "node",
|
|
174
|
+
nodeId: node.id,
|
|
175
|
+
outputId,
|
|
176
|
+
name: variable.name,
|
|
177
|
+
dataType: variable.dataType,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return out;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Merge the three variable sources into a single per-canvas record:
|
|
186
|
+
* declared (from api)
|
|
187
|
+
* + fnarg (derived from the function's `arguments` — empty for the main canvas)
|
|
188
|
+
* + nodeOutput (derived from nodes via {@link computeVariablesFromNodes})
|
|
189
|
+
*
|
|
190
|
+
* Disjoint key namespaces (`declared:`, `fnarg:`, `<nodeId>:<outputId>`) so
|
|
191
|
+
* merge order is irrelevant.
|
|
192
|
+
*/
|
|
193
|
+
export function buildCanvasVariables(
|
|
194
|
+
nodes: NodeData[],
|
|
195
|
+
fnArgs: readonly Schemas["Variable"][],
|
|
196
|
+
declaredVariables: readonly Schemas["Variable"][],
|
|
197
|
+
): Record<string, Variable> {
|
|
198
|
+
const variables: Record<string, Variable> = computeVariablesFromNodes(nodes);
|
|
199
|
+
for (const arg of fnArgs) {
|
|
200
|
+
variables[fnargKey(arg.uid)] = {
|
|
201
|
+
kind: "fnarg",
|
|
202
|
+
uid: arg.uid,
|
|
203
|
+
name: arg.name,
|
|
204
|
+
dataType: arg.dataType,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
for (const dv of declaredVariables) {
|
|
208
|
+
variables[declaredVarKey(dv.uid)] = {
|
|
209
|
+
kind: "declared",
|
|
210
|
+
uid: dv.uid,
|
|
211
|
+
name: dv.name,
|
|
212
|
+
dataType: dv.dataType,
|
|
213
|
+
...(dv.initialValue !== undefined ? { initialValue: dv.initialValue } : {}),
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
return variables;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ============================================================================
|
|
220
|
+
// Helpers — declared variables
|
|
221
|
+
// ============================================================================
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Filter a canvas's variables down to the declared-kind entries (the only
|
|
225
|
+
* variety the api persists). Node-output and fnarg variables are
|
|
226
|
+
* reconstructed on deserialize from nodes + functionInfo.
|
|
227
|
+
*/
|
|
228
|
+
function extractDeclaredVariables(variables: Record<string, Variable>): Schemas["Variable"][] {
|
|
229
|
+
const result: Schemas["Variable"][] = [];
|
|
230
|
+
for (const v of Object.values(variables)) {
|
|
231
|
+
if (v.kind === "declared") {
|
|
232
|
+
result.push({
|
|
233
|
+
uid: v.uid,
|
|
234
|
+
name: v.name,
|
|
235
|
+
dataType: v.dataType,
|
|
236
|
+
...(v.initialValue !== undefined ? { initialValue: v.initialValue } : {}),
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
return result;
|
|
241
|
+
}
|
|
242
|
+
|