@foresthubai/workflow-core 0.4.3 → 0.4.5

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