@foresthubai/workflow-core 0.3.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 (339) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +14 -0
  3. package/README.md +63 -0
  4. package/dist/api/index.d.ts +7 -0
  5. package/dist/api/index.d.ts.map +1 -0
  6. package/dist/api/index.js +2 -0
  7. package/dist/api/index.js.map +1 -0
  8. package/dist/api/workflow.d.ts +607 -0
  9. package/dist/api/workflow.d.ts.map +1 -0
  10. package/dist/api/workflow.js +6 -0
  11. package/dist/api/workflow.js.map +1 -0
  12. package/dist/channel/Channel.d.ts +10 -0
  13. package/dist/channel/Channel.d.ts.map +1 -0
  14. package/dist/channel/Channel.js +2 -0
  15. package/dist/channel/Channel.js.map +1 -0
  16. package/dist/channel/ChannelDefinition.d.ts +27 -0
  17. package/dist/channel/ChannelDefinition.d.ts.map +1 -0
  18. package/dist/channel/ChannelDefinition.js +50 -0
  19. package/dist/channel/ChannelDefinition.js.map +1 -0
  20. package/dist/channel/index.d.ts +7 -0
  21. package/dist/channel/index.d.ts.map +1 -0
  22. package/dist/channel/index.js +4 -0
  23. package/dist/channel/index.js.map +1 -0
  24. package/dist/channel/serialization.d.ts +17 -0
  25. package/dist/channel/serialization.d.ts.map +1 -0
  26. package/dist/channel/serialization.js +63 -0
  27. package/dist/channel/serialization.js.map +1 -0
  28. package/dist/deploy/index.d.ts +2 -0
  29. package/dist/deploy/index.d.ts.map +1 -0
  30. package/dist/deploy/index.js +2 -0
  31. package/dist/deploy/index.js.map +1 -0
  32. package/dist/deploy/requirements.d.ts +17 -0
  33. package/dist/deploy/requirements.d.ts.map +1 -0
  34. package/dist/deploy/requirements.js +41 -0
  35. package/dist/deploy/requirements.js.map +1 -0
  36. package/dist/diagnostics/__fixtures__/diagnosticFixtures.d.ts +28 -0
  37. package/dist/diagnostics/__fixtures__/diagnosticFixtures.d.ts.map +1 -0
  38. package/dist/diagnostics/__fixtures__/diagnosticFixtures.js +125 -0
  39. package/dist/diagnostics/__fixtures__/diagnosticFixtures.js.map +1 -0
  40. package/dist/diagnostics/diagnostics.d.ts +128 -0
  41. package/dist/diagnostics/diagnostics.d.ts.map +1 -0
  42. package/dist/diagnostics/diagnostics.js +783 -0
  43. package/dist/diagnostics/diagnostics.js.map +1 -0
  44. package/dist/diagnostics/index.d.ts +3 -0
  45. package/dist/diagnostics/index.d.ts.map +1 -0
  46. package/dist/diagnostics/index.js +2 -0
  47. package/dist/diagnostics/index.js.map +1 -0
  48. package/dist/edge/Edge.d.ts +22 -0
  49. package/dist/edge/Edge.d.ts.map +1 -0
  50. package/dist/edge/Edge.js +2 -0
  51. package/dist/edge/Edge.js.map +1 -0
  52. package/dist/edge/EdgeDefinition.d.ts +10 -0
  53. package/dist/edge/EdgeDefinition.d.ts.map +1 -0
  54. package/dist/edge/EdgeDefinition.js +36 -0
  55. package/dist/edge/EdgeDefinition.js.map +1 -0
  56. package/dist/edge/EdgeType.d.ts +6 -0
  57. package/dist/edge/EdgeType.d.ts.map +1 -0
  58. package/dist/edge/EdgeType.js +7 -0
  59. package/dist/edge/EdgeType.js.map +1 -0
  60. package/dist/edge/index.d.ts +6 -0
  61. package/dist/edge/index.d.ts.map +1 -0
  62. package/dist/edge/index.js +6 -0
  63. package/dist/edge/index.js.map +1 -0
  64. package/dist/edge/serialization.d.ts +18 -0
  65. package/dist/edge/serialization.d.ts.map +1 -0
  66. package/dist/edge/serialization.js +76 -0
  67. package/dist/edge/serialization.js.map +1 -0
  68. package/dist/expression/index.d.ts +5 -0
  69. package/dist/expression/index.d.ts.map +1 -0
  70. package/dist/expression/index.js +3 -0
  71. package/dist/expression/index.js.map +1 -0
  72. package/dist/expression/parser.d.ts +14 -0
  73. package/dist/expression/parser.d.ts.map +1 -0
  74. package/dist/expression/parser.js +309 -0
  75. package/dist/expression/parser.js.map +1 -0
  76. package/dist/expression/types.d.ts +11 -0
  77. package/dist/expression/types.d.ts.map +1 -0
  78. package/dist/expression/types.js +20 -0
  79. package/dist/expression/types.js.map +1 -0
  80. package/dist/function/FunctionDeclaration.d.ts +43 -0
  81. package/dist/function/FunctionDeclaration.d.ts.map +1 -0
  82. package/dist/function/FunctionDeclaration.js +17 -0
  83. package/dist/function/FunctionDeclaration.js.map +1 -0
  84. package/dist/function/index.d.ts +4 -0
  85. package/dist/function/index.d.ts.map +1 -0
  86. package/dist/function/index.js +3 -0
  87. package/dist/function/index.js.map +1 -0
  88. package/dist/function/serialization.d.ts +20 -0
  89. package/dist/function/serialization.d.ts.map +1 -0
  90. package/dist/function/serialization.js +26 -0
  91. package/dist/function/serialization.js.map +1 -0
  92. package/dist/id/index.d.ts +7 -0
  93. package/dist/id/index.d.ts.map +1 -0
  94. package/dist/id/index.js +9 -0
  95. package/dist/id/index.js.map +1 -0
  96. package/dist/index.d.ts +11 -0
  97. package/dist/index.d.ts.map +1 -0
  98. package/dist/index.js +14 -0
  99. package/dist/index.js.map +1 -0
  100. package/dist/memory/Memory.d.ts +12 -0
  101. package/dist/memory/Memory.d.ts.map +1 -0
  102. package/dist/memory/Memory.js +2 -0
  103. package/dist/memory/Memory.js.map +1 -0
  104. package/dist/memory/MemoryDefinition.d.ts +16 -0
  105. package/dist/memory/MemoryDefinition.d.ts.map +1 -0
  106. package/dist/memory/MemoryDefinition.js +2 -0
  107. package/dist/memory/MemoryDefinition.js.map +1 -0
  108. package/dist/memory/MemoryFileDefinition.d.ts +8 -0
  109. package/dist/memory/MemoryFileDefinition.d.ts.map +1 -0
  110. package/dist/memory/MemoryFileDefinition.js +36 -0
  111. package/dist/memory/MemoryFileDefinition.js.map +1 -0
  112. package/dist/memory/MemoryRegistry.d.ts +17 -0
  113. package/dist/memory/MemoryRegistry.d.ts.map +1 -0
  114. package/dist/memory/MemoryRegistry.js +29 -0
  115. package/dist/memory/MemoryRegistry.js.map +1 -0
  116. package/dist/memory/VectorDatabaseDefinition.d.ts +7 -0
  117. package/dist/memory/VectorDatabaseDefinition.d.ts.map +1 -0
  118. package/dist/memory/VectorDatabaseDefinition.js +20 -0
  119. package/dist/memory/VectorDatabaseDefinition.js.map +1 -0
  120. package/dist/memory/index.d.ts +9 -0
  121. package/dist/memory/index.d.ts.map +1 -0
  122. package/dist/memory/index.js +6 -0
  123. package/dist/memory/index.js.map +1 -0
  124. package/dist/memory/serialization.d.ts +11 -0
  125. package/dist/memory/serialization.d.ts.map +1 -0
  126. package/dist/memory/serialization.js +44 -0
  127. package/dist/memory/serialization.js.map +1 -0
  128. package/dist/migration/index.d.ts +5 -0
  129. package/dist/migration/index.d.ts.map +1 -0
  130. package/dist/migration/index.js +4 -0
  131. package/dist/migration/index.js.map +1 -0
  132. package/dist/migration/migrate.d.ts +11 -0
  133. package/dist/migration/migrate.d.ts.map +1 -0
  134. package/dist/migration/migrate.js +52 -0
  135. package/dist/migration/migrate.js.map +1 -0
  136. package/dist/migration/migrations.d.ts +22 -0
  137. package/dist/migration/migrations.d.ts.map +1 -0
  138. package/dist/migration/migrations.js +10 -0
  139. package/dist/migration/migrations.js.map +1 -0
  140. package/dist/migration/version.d.ts +9 -0
  141. package/dist/migration/version.d.ts.map +1 -0
  142. package/dist/migration/version.js +9 -0
  143. package/dist/migration/version.js.map +1 -0
  144. package/dist/model/LLMModelDefinition.d.ts +7 -0
  145. package/dist/model/LLMModelDefinition.d.ts.map +1 -0
  146. package/dist/model/LLMModelDefinition.js +11 -0
  147. package/dist/model/LLMModelDefinition.js.map +1 -0
  148. package/dist/model/Model.d.ts +21 -0
  149. package/dist/model/Model.d.ts.map +1 -0
  150. package/dist/model/Model.js +15 -0
  151. package/dist/model/Model.js.map +1 -0
  152. package/dist/model/ModelDefinition.d.ts +15 -0
  153. package/dist/model/ModelDefinition.d.ts.map +1 -0
  154. package/dist/model/ModelDefinition.js +2 -0
  155. package/dist/model/ModelDefinition.js.map +1 -0
  156. package/dist/model/ModelRegistry.d.ts +17 -0
  157. package/dist/model/ModelRegistry.d.ts.map +1 -0
  158. package/dist/model/ModelRegistry.js +27 -0
  159. package/dist/model/ModelRegistry.js.map +1 -0
  160. package/dist/model/index.d.ts +8 -0
  161. package/dist/model/index.d.ts.map +1 -0
  162. package/dist/model/index.js +5 -0
  163. package/dist/model/index.js.map +1 -0
  164. package/dist/model/serialization.d.ts +8 -0
  165. package/dist/model/serialization.d.ts.map +1 -0
  166. package/dist/model/serialization.js +25 -0
  167. package/dist/model/serialization.js.map +1 -0
  168. package/dist/node/AgentNode.d.ts +21 -0
  169. package/dist/node/AgentNode.d.ts.map +1 -0
  170. package/dist/node/AgentNode.js +60 -0
  171. package/dist/node/AgentNode.js.map +1 -0
  172. package/dist/node/DataNode.d.ts +14 -0
  173. package/dist/node/DataNode.d.ts.map +1 -0
  174. package/dist/node/DataNode.js +26 -0
  175. package/dist/node/DataNode.js.map +1 -0
  176. package/dist/node/FunctionNode.d.ts +24 -0
  177. package/dist/node/FunctionNode.d.ts.map +1 -0
  178. package/dist/node/FunctionNode.js +44 -0
  179. package/dist/node/FunctionNode.js.map +1 -0
  180. package/dist/node/InputNode.d.ts +46 -0
  181. package/dist/node/InputNode.d.ts.map +1 -0
  182. package/dist/node/InputNode.js +133 -0
  183. package/dist/node/InputNode.js.map +1 -0
  184. package/dist/node/LogicNode.d.ts +13 -0
  185. package/dist/node/LogicNode.d.ts.map +1 -0
  186. package/dist/node/LogicNode.js +19 -0
  187. package/dist/node/LogicNode.js.map +1 -0
  188. package/dist/node/MqttNode.d.ts +27 -0
  189. package/dist/node/MqttNode.d.ts.map +1 -0
  190. package/dist/node/MqttNode.js +96 -0
  191. package/dist/node/MqttNode.js.map +1 -0
  192. package/dist/node/Node.d.ts +49 -0
  193. package/dist/node/Node.d.ts.map +1 -0
  194. package/dist/node/Node.js +9 -0
  195. package/dist/node/Node.js.map +1 -0
  196. package/dist/node/NodeDefinition.d.ts +30 -0
  197. package/dist/node/NodeDefinition.d.ts.map +1 -0
  198. package/dist/node/NodeDefinition.js +2 -0
  199. package/dist/node/NodeDefinition.js.map +1 -0
  200. package/dist/node/NodeRegistry.d.ts +19 -0
  201. package/dist/node/NodeRegistry.d.ts.map +1 -0
  202. package/dist/node/NodeRegistry.js +65 -0
  203. package/dist/node/NodeRegistry.js.map +1 -0
  204. package/dist/node/OutputNode.d.ts +23 -0
  205. package/dist/node/OutputNode.d.ts.map +1 -0
  206. package/dist/node/OutputNode.js +62 -0
  207. package/dist/node/OutputNode.js.map +1 -0
  208. package/dist/node/ToolNode.d.ts +12 -0
  209. package/dist/node/ToolNode.d.ts.map +1 -0
  210. package/dist/node/ToolNode.js +18 -0
  211. package/dist/node/ToolNode.js.map +1 -0
  212. package/dist/node/TriggerNode.d.ts +67 -0
  213. package/dist/node/TriggerNode.d.ts.map +1 -0
  214. package/dist/node/TriggerNode.js +172 -0
  215. package/dist/node/TriggerNode.js.map +1 -0
  216. package/dist/node/constants.d.ts +16 -0
  217. package/dist/node/constants.d.ts.map +1 -0
  218. package/dist/node/constants.js +18 -0
  219. package/dist/node/constants.js.map +1 -0
  220. package/dist/node/index.d.ts +12 -0
  221. package/dist/node/index.d.ts.map +1 -0
  222. package/dist/node/index.js +9 -0
  223. package/dist/node/index.js.map +1 -0
  224. package/dist/node/methods.d.ts +73 -0
  225. package/dist/node/methods.d.ts.map +1 -0
  226. package/dist/node/methods.js +261 -0
  227. package/dist/node/methods.js.map +1 -0
  228. package/dist/node/serialization.d.ts +25 -0
  229. package/dist/node/serialization.d.ts.map +1 -0
  230. package/dist/node/serialization.js +525 -0
  231. package/dist/node/serialization.js.map +1 -0
  232. package/dist/parameter/OutputParameter.d.ts +69 -0
  233. package/dist/parameter/OutputParameter.d.ts.map +1 -0
  234. package/dist/parameter/OutputParameter.js +6 -0
  235. package/dist/parameter/OutputParameter.js.map +1 -0
  236. package/dist/parameter/Parameter.d.ts +152 -0
  237. package/dist/parameter/Parameter.d.ts.map +1 -0
  238. package/dist/parameter/Parameter.js +86 -0
  239. package/dist/parameter/Parameter.js.map +1 -0
  240. package/dist/parameter/index.d.ts +5 -0
  241. package/dist/parameter/index.d.ts.map +1 -0
  242. package/dist/parameter/index.js +3 -0
  243. package/dist/parameter/index.js.map +1 -0
  244. package/dist/variable/Variable.d.ts +25 -0
  245. package/dist/variable/Variable.d.ts.map +1 -0
  246. package/dist/variable/Variable.js +2 -0
  247. package/dist/variable/Variable.js.map +1 -0
  248. package/dist/variable/index.d.ts +3 -0
  249. package/dist/variable/index.d.ts.map +1 -0
  250. package/dist/variable/index.js +5 -0
  251. package/dist/variable/index.js.map +1 -0
  252. package/dist/variable/operations.d.ts +37 -0
  253. package/dist/variable/operations.d.ts.map +1 -0
  254. package/dist/variable/operations.js +88 -0
  255. package/dist/variable/operations.js.map +1 -0
  256. package/dist/workflow/Workflow.d.ts +38 -0
  257. package/dist/workflow/Workflow.d.ts.map +1 -0
  258. package/dist/workflow/Workflow.js +8 -0
  259. package/dist/workflow/Workflow.js.map +1 -0
  260. package/dist/workflow/index.d.ts +4 -0
  261. package/dist/workflow/index.d.ts.map +1 -0
  262. package/dist/workflow/index.js +3 -0
  263. package/dist/workflow/index.js.map +1 -0
  264. package/dist/workflow/serialization.d.ts +43 -0
  265. package/dist/workflow/serialization.d.ts.map +1 -0
  266. package/dist/workflow/serialization.js +215 -0
  267. package/dist/workflow/serialization.js.map +1 -0
  268. package/package.json +105 -0
  269. package/src/api/index.ts +11 -0
  270. package/src/api/workflow.ts +607 -0
  271. package/src/channel/Channel.ts +11 -0
  272. package/src/channel/ChannelDefinition.ts +76 -0
  273. package/src/channel/index.ts +6 -0
  274. package/src/channel/serialization.ts +68 -0
  275. package/src/deploy/index.ts +1 -0
  276. package/src/deploy/requirements.test.ts +61 -0
  277. package/src/deploy/requirements.ts +41 -0
  278. package/src/diagnostics/__fixtures__/diagnosticFixtures.ts +158 -0
  279. package/src/diagnostics/diagnostics.test.ts +878 -0
  280. package/src/diagnostics/diagnostics.ts +936 -0
  281. package/src/diagnostics/index.ts +11 -0
  282. package/src/edge/Edge.ts +23 -0
  283. package/src/edge/EdgeDefinition.ts +45 -0
  284. package/src/edge/EdgeType.ts +19 -0
  285. package/src/edge/index.ts +8 -0
  286. package/src/edge/serialization.ts +83 -0
  287. package/src/expression/index.ts +4 -0
  288. package/src/expression/parser.ts +362 -0
  289. package/src/expression/types.ts +30 -0
  290. package/src/function/FunctionDeclaration.ts +54 -0
  291. package/src/function/index.ts +3 -0
  292. package/src/function/serialization.ts +40 -0
  293. package/src/globals.d.ts +9 -0
  294. package/src/id/index.ts +8 -0
  295. package/src/index.ts +22 -0
  296. package/src/memory/Memory.ts +15 -0
  297. package/src/memory/MemoryDefinition.ts +16 -0
  298. package/src/memory/MemoryFileDefinition.ts +37 -0
  299. package/src/memory/MemoryRegistry.ts +35 -0
  300. package/src/memory/VectorDatabaseDefinition.ts +21 -0
  301. package/src/memory/index.ts +8 -0
  302. package/src/memory/serialization.ts +47 -0
  303. package/src/migration/index.ts +4 -0
  304. package/src/migration/migrate.test.ts +44 -0
  305. package/src/migration/migrate.ts +58 -0
  306. package/src/migration/migrations.ts +24 -0
  307. package/src/migration/version.ts +9 -0
  308. package/src/model/LLMModelDefinition.ts +12 -0
  309. package/src/model/Model.ts +39 -0
  310. package/src/model/ModelDefinition.ts +15 -0
  311. package/src/model/ModelRegistry.ts +33 -0
  312. package/src/model/index.ts +7 -0
  313. package/src/model/serialization.ts +30 -0
  314. package/src/node/AgentNode.ts +82 -0
  315. package/src/node/DataNode.ts +41 -0
  316. package/src/node/FunctionNode.ts +76 -0
  317. package/src/node/InputNode.ts +185 -0
  318. package/src/node/LogicNode.ts +33 -0
  319. package/src/node/MqttNode.ts +127 -0
  320. package/src/node/Node.ts +61 -0
  321. package/src/node/NodeDefinition.ts +37 -0
  322. package/src/node/NodeRegistry.ts +85 -0
  323. package/src/node/OutputNode.ts +87 -0
  324. package/src/node/ToolNode.ts +32 -0
  325. package/src/node/TriggerNode.ts +272 -0
  326. package/src/node/constants.ts +16 -0
  327. package/src/node/index.ts +26 -0
  328. package/src/node/methods.ts +278 -0
  329. package/src/node/serialization.ts +544 -0
  330. package/src/parameter/OutputParameter.ts +68 -0
  331. package/src/parameter/Parameter.ts +243 -0
  332. package/src/parameter/index.ts +33 -0
  333. package/src/variable/Variable.ts +10 -0
  334. package/src/variable/index.ts +16 -0
  335. package/src/variable/operations.ts +106 -0
  336. package/src/workflow/Workflow.ts +41 -0
  337. package/src/workflow/index.ts +3 -0
  338. package/src/workflow/serialization.test.ts +240 -0
  339. package/src/workflow/serialization.ts +242 -0
@@ -0,0 +1,544 @@
1
+ import type { Schemas } from "../api";
2
+ import type { NodeData, Node } from "./Node";
3
+ import type { Expression } from "../api";
4
+ import type { FunctionInfo } from "../function";
5
+ import type { OutputBinding, OutputDeclaration } from "../parameter";
6
+ import { pruneArguments } from "../parameter";
7
+ import { NodeRegistry } from "./NodeRegistry";
8
+
9
+ export type ApiNode = Schemas["Node"];
10
+
11
+ /**
12
+ * Resolve a function's signature snapshot by id. A `FunctionCall` on the wire stores
13
+ * only `functionId`; deserialize rebuilds the in-memory `functionInfo` snapshot
14
+ * (which core's variable helpers + staleness read) from the workflow's function
15
+ * table via this. Unknown id (e.g. a call to a since-deleted function) → a minimal
16
+ * stub so the node still round-trips and surfaces as deleted.
17
+ */
18
+ export type ResolveFunctionInfo = (functionId: string) => FunctionInfo | undefined;
19
+
20
+ /**
21
+ * Serialize a domain Node to the strict API format (Schemas["Node"]).
22
+ * Strips hidden parameters (those whose activationRules are not met). The
23
+ * `isToolInput` flag is threaded into activation evaluation so rules like
24
+ * `isControlFlow` / `isToolInput` resolve correctly per-instance.
25
+ */
26
+ export function serialize(node: Node, isToolInput: boolean): ApiNode {
27
+ const result = serializeNodeData(node, node.position, isToolInput);
28
+ if (node.label) {
29
+ result.label = node.label;
30
+ }
31
+
32
+ // serializeNode emits gated/optional params uniformly; this pass prunes the
33
+ // ones that must not reach the api — inactive for this instance, or empty
34
+ // optionals (so the consumer's presence check sees absent, not `""`/`null`).
35
+ // FunctionCall gates its own params inline (its bindings have a different api
36
+ // shape), so it's excluded here.
37
+ if ("arguments" in result && result.arguments) {
38
+ const def = node.type !== "FunctionCall" ? NodeRegistry.getByType(node.type) : undefined;
39
+ if (def) {
40
+ pruneArguments(result.arguments, def.parameters, isToolInput);
41
+ }
42
+ }
43
+
44
+ return result;
45
+ }
46
+
47
+ function serializeNodeData(data: NodeData, position: { x: number; y: number }, isToolInput: boolean): Schemas["Node"] {
48
+ switch (data.type) {
49
+ case "ReadPin":
50
+ return {
51
+ id: data.id,
52
+ type: data.type,
53
+ position: position,
54
+ arguments: {
55
+ pinReference: data.arguments.pinReference!,
56
+ signalType: data.arguments.signalType,
57
+ output: data.arguments.output,
58
+ toolDescription: data.arguments.toolDescription,
59
+ },
60
+ };
61
+ case "SerialRead":
62
+ return {
63
+ id: data.id,
64
+ type: data.type,
65
+ position: position,
66
+ arguments: {
67
+ portReference: data.arguments.portReference!,
68
+ ...(data.arguments.prompt !== undefined ? { prompt: data.arguments.prompt } : {}),
69
+ output: data.arguments.output,
70
+ },
71
+ };
72
+ case "WritePin":
73
+ return {
74
+ id: data.id,
75
+ type: data.type,
76
+ position: position,
77
+ arguments: {
78
+ pinReference: data.arguments.pinReference!,
79
+ signalType: data.arguments.signalType,
80
+ value: data.arguments.value,
81
+ },
82
+ };
83
+ case "SerialWrite":
84
+ return {
85
+ id: data.id,
86
+ type: data.type,
87
+ position: position,
88
+ arguments: {
89
+ portReference: data.arguments.portReference!,
90
+ value: data.arguments.value,
91
+ },
92
+ };
93
+ case "Agent": {
94
+ // outputDeclarations is a list both in domain and API. Each entry's `name`
95
+ // is the JSON property the LLM is asked to produce; uniqueness is enforced
96
+ // by diagnostics, not the schema. memoryRefs is also a 1:1 list — domain
97
+ // and API share the same MemoryRef shape.
98
+ return {
99
+ id: data.id,
100
+ type: data.type,
101
+ position: position,
102
+ arguments: {
103
+ name: data.arguments.name,
104
+ model: data.arguments.model,
105
+ instructions: data.arguments.instructions,
106
+ maxTurns: data.arguments.maxTurns,
107
+ outputDeclarations: data.arguments.outputDeclarations,
108
+ memoryRefs: data.arguments.memoryRefs ?? [],
109
+ answer: data.arguments.answer,
110
+ toolDescription: data.arguments.toolDescription,
111
+ },
112
+ };
113
+ }
114
+ case "If":
115
+ return {
116
+ id: data.id,
117
+ type: data.type,
118
+ position: position,
119
+ arguments: {
120
+ condition: data.arguments.condition,
121
+ },
122
+ };
123
+ case "OnFunctionCall":
124
+ return {
125
+ id: data.id,
126
+ type: data.type,
127
+ position: position,
128
+ };
129
+ case "OnStartup":
130
+ return {
131
+ id: data.id,
132
+ type: data.type,
133
+ position: position,
134
+ };
135
+ case "OnPinEdge":
136
+ return {
137
+ id: data.id,
138
+ type: data.type,
139
+ position: position,
140
+ arguments: {
141
+ pinReference: data.arguments.pinReference!,
142
+ edge: data.arguments.edge,
143
+ },
144
+ };
145
+ case "OnSerialReceive":
146
+ return {
147
+ id: data.id,
148
+ type: data.type,
149
+ position: position,
150
+ arguments: {
151
+ portReference: data.arguments.portReference!,
152
+ output: data.arguments.output,
153
+ },
154
+ };
155
+ case "OnThreshold":
156
+ return {
157
+ id: data.id,
158
+ type: data.type,
159
+ position: position,
160
+ arguments: {
161
+ variable: data.arguments.variable!,
162
+ threshold: data.arguments.threshold!,
163
+ direction: data.arguments.direction,
164
+ deadband: data.arguments.deadband,
165
+ output: data.arguments.output,
166
+ },
167
+ };
168
+ case "Delay":
169
+ return {
170
+ id: data.id,
171
+ type: data.type,
172
+ position: position,
173
+ arguments: {
174
+ delayMs: data.arguments.delayMs!,
175
+ },
176
+ };
177
+ case "Ticker":
178
+ return {
179
+ id: data.id,
180
+ type: data.type,
181
+ position: position,
182
+ arguments: {
183
+ intervalValue: data.arguments.intervalValue!,
184
+ intervalUnit: data.arguments.intervalUnit,
185
+ },
186
+ };
187
+ case "Alarm":
188
+ return {
189
+ id: data.id,
190
+ type: data.type,
191
+ position: position,
192
+ arguments: {
193
+ time: data.arguments.time,
194
+ days: data.arguments.days,
195
+ },
196
+ };
197
+ case "WebSearchTool":
198
+ return {
199
+ id: data.id,
200
+ type: data.type,
201
+ position: position,
202
+ arguments: {
203
+ maxResults: data.arguments.maxResults,
204
+ },
205
+ };
206
+ case "Retriever":
207
+ return {
208
+ id: data.id,
209
+ type: data.type,
210
+ position: position,
211
+ arguments: {
212
+ memoryReference: data.arguments.memoryReference,
213
+ topK: data.arguments.topK!,
214
+ query: data.arguments.query,
215
+ output: data.arguments.output,
216
+ toolDescription: data.arguments.toolDescription,
217
+ },
218
+ };
219
+ case "WebFetch":
220
+ return {
221
+ id: data.id,
222
+ type: data.type,
223
+ position: position,
224
+ arguments: {
225
+ url: data.arguments.url,
226
+ maxChars: data.arguments.maxChars,
227
+ output: data.arguments.output,
228
+ },
229
+ };
230
+ case "FunctionCall": {
231
+ // Frontend stores FunctionCall args flat (unified with every other node), but
232
+ // the API schema keeps the nested { inputBindings, outputBindings } shape.
233
+ // Translate here so the api format stays stable. `toolDescription` sits
234
+ // alongside the bindings at the api level and is only emitted when the
235
+ // node is currently wired as a tool (exec-mode calls don't need it).
236
+ const inputBindings: Record<string, Expression> = {};
237
+ const outputBindings: Record<string, OutputBinding> = {};
238
+ const args = data.arguments as Record<string, unknown>;
239
+ for (const arg of data.functionInfo.arguments) {
240
+ const key = arg.uid ?? arg.name;
241
+ const v = args[key];
242
+ if (v !== undefined) inputBindings[key] = v as Expression;
243
+ }
244
+ for (const ret of data.functionInfo.returns) {
245
+ const key = ret.uid ?? ret.name;
246
+ const v = args[key];
247
+ if (v !== undefined) outputBindings[key] = v as OutputBinding;
248
+ }
249
+ const toolDescription = args.toolDescription as string | undefined;
250
+ return {
251
+ id: data.id,
252
+ type: data.type,
253
+ functionId: data.functionInfo.id,
254
+ position: position,
255
+ arguments: {
256
+ inputBindings,
257
+ outputBindings,
258
+ ...(isToolInput && toolDescription !== undefined ? { toolDescription } : {}),
259
+ },
260
+ };
261
+ }
262
+ case "SetVariable":
263
+ return {
264
+ id: data.id,
265
+ type: data.type,
266
+ position: position,
267
+ arguments: {
268
+ variable: data.arguments.variable!,
269
+ value: data.arguments.value,
270
+ },
271
+ };
272
+ case "MqttPublish":
273
+ return {
274
+ id: data.id,
275
+ type: data.type,
276
+ position: position,
277
+ arguments: {
278
+ channelReference: data.arguments.channelReference ?? "",
279
+ dataType: data.arguments.dataType,
280
+ value: data.arguments.value,
281
+ qos: Number(data.arguments.qos) as 0 | 1 | 2,
282
+ retain: data.arguments.retain,
283
+ },
284
+ };
285
+ case "OnMqttMessage":
286
+ return {
287
+ id: data.id,
288
+ type: data.type,
289
+ position: position,
290
+ arguments: {
291
+ channelReference: data.arguments.channelReference ?? "",
292
+ dataType: data.arguments.dataType,
293
+ output: data.arguments.output,
294
+ },
295
+ };
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Convert a strict API Node to a domain Node (NodeData + position). `resolveFunctionInfo`
301
+ * is required only for `FunctionCall` nodes — see {@link ResolveFunctionInfo}.
302
+ */
303
+ export function deserialize(apiNode: ApiNode, resolveFunctionInfo?: ResolveFunctionInfo): Node {
304
+ return { ...deserializeNodeData(apiNode, resolveFunctionInfo), position: apiNode.position };
305
+ }
306
+
307
+ /** Build the NodeData payload from an API Node (no position). */
308
+ function deserializeNodeData(apiNode: Schemas["Node"], resolveFunctionInfo?: ResolveFunctionInfo): NodeData {
309
+ switch (apiNode.type) {
310
+ case "ReadPin":
311
+ return {
312
+ id: apiNode.id,
313
+ type: apiNode.type,
314
+ label: apiNode.label,
315
+ arguments: {
316
+ pinReference: apiNode.arguments.pinReference ?? "",
317
+ signalType: apiNode.arguments.signalType,
318
+ output: apiNode.arguments.output as OutputBinding,
319
+ toolDescription: apiNode.arguments.toolDescription,
320
+ },
321
+ };
322
+ case "SerialRead":
323
+ return {
324
+ id: apiNode.id,
325
+ type: apiNode.type,
326
+ label: apiNode.label,
327
+ arguments: {
328
+ portReference: apiNode.arguments.portReference ?? "",
329
+ prompt: apiNode.arguments.prompt ?? "",
330
+ output: apiNode.arguments.output as OutputBinding,
331
+ },
332
+ };
333
+ case "Retriever":
334
+ return {
335
+ id: apiNode.id,
336
+ type: apiNode.type,
337
+ label: apiNode.label,
338
+ arguments: {
339
+ memoryReference: apiNode.arguments.memoryReference ?? "",
340
+ topK: apiNode.arguments.topK ?? 0,
341
+ query: apiNode.arguments.query,
342
+ output: apiNode.arguments.output as OutputBinding,
343
+ toolDescription: apiNode.arguments.toolDescription,
344
+ },
345
+ };
346
+ case "WritePin":
347
+ return {
348
+ id: apiNode.id,
349
+ type: apiNode.type,
350
+ label: apiNode.label,
351
+ arguments: {
352
+ pinReference: apiNode.arguments.pinReference ?? "",
353
+ signalType: apiNode.arguments.signalType,
354
+ value: apiNode.arguments.value,
355
+ },
356
+ };
357
+ case "SerialWrite":
358
+ return {
359
+ id: apiNode.id,
360
+ type: apiNode.type,
361
+ label: apiNode.label,
362
+ arguments: {
363
+ portReference: apiNode.arguments.portReference ?? "",
364
+ value: apiNode.arguments.value,
365
+ },
366
+ };
367
+ case "Agent":
368
+ return {
369
+ id: apiNode.id,
370
+ type: apiNode.type,
371
+ label: apiNode.label,
372
+ arguments: {
373
+ name: apiNode.arguments.name ?? "",
374
+ model: apiNode.arguments.model ?? "",
375
+ instructions: apiNode.arguments.instructions ?? "",
376
+ maxTurns: apiNode.arguments.maxTurns,
377
+ outputDeclarations: apiNode.arguments.outputDeclarations as OutputDeclaration[],
378
+ memoryRefs: apiNode.arguments.memoryRefs ?? [],
379
+ answer: apiNode.arguments.answer as OutputBinding,
380
+ toolDescription: apiNode.arguments.toolDescription,
381
+ },
382
+ };
383
+ case "If":
384
+ return {
385
+ id: apiNode.id,
386
+ type: apiNode.type,
387
+ label: apiNode.label,
388
+ arguments: {
389
+ condition: apiNode.arguments.condition,
390
+ },
391
+ };
392
+ case "OnFunctionCall":
393
+ return { id: apiNode.id, type: apiNode.type, label: apiNode.label, arguments: {} };
394
+ case "OnStartup":
395
+ return { id: apiNode.id, type: apiNode.type, label: apiNode.label, arguments: {} };
396
+ case "OnPinEdge":
397
+ return {
398
+ id: apiNode.id,
399
+ type: apiNode.type,
400
+ label: apiNode.label,
401
+ arguments: {
402
+ pinReference: apiNode.arguments.pinReference ?? "",
403
+ edge: apiNode.arguments.edge,
404
+ },
405
+ };
406
+ case "OnSerialReceive":
407
+ return {
408
+ id: apiNode.id,
409
+ type: apiNode.type,
410
+ label: apiNode.label,
411
+ arguments: {
412
+ portReference: apiNode.arguments.portReference ?? "",
413
+ output: apiNode.arguments.output as OutputBinding,
414
+ },
415
+ };
416
+ case "OnThreshold":
417
+ return {
418
+ id: apiNode.id,
419
+ type: apiNode.type,
420
+ label: apiNode.label,
421
+ arguments: {
422
+ variable: apiNode.arguments.variable,
423
+ threshold: apiNode.arguments.threshold,
424
+ direction: apiNode.arguments.direction,
425
+ deadband: apiNode.arguments.deadband,
426
+ output: apiNode.arguments.output as OutputBinding,
427
+ },
428
+ };
429
+ case "Delay":
430
+ return {
431
+ id: apiNode.id,
432
+ type: apiNode.type,
433
+ label: apiNode.label,
434
+ arguments: {
435
+ delayMs: apiNode.arguments.delayMs ?? 0,
436
+ },
437
+ };
438
+ case "Ticker":
439
+ return {
440
+ id: apiNode.id,
441
+ type: apiNode.type,
442
+ label: apiNode.label,
443
+ arguments: {
444
+ intervalValue: apiNode.arguments.intervalValue ?? 0,
445
+ intervalUnit: apiNode.arguments.intervalUnit,
446
+ },
447
+ };
448
+ case "Alarm":
449
+ return {
450
+ id: apiNode.id,
451
+ type: apiNode.type,
452
+ label: apiNode.label,
453
+ arguments: {
454
+ time: apiNode.arguments.time ?? "",
455
+ days: apiNode.arguments.days,
456
+ },
457
+ };
458
+ case "WebSearchTool":
459
+ return {
460
+ id: apiNode.id,
461
+ type: apiNode.type,
462
+ label: apiNode.label,
463
+ arguments: {
464
+ maxResults: apiNode.arguments.maxResults,
465
+ },
466
+ };
467
+ case "WebFetch":
468
+ return {
469
+ id: apiNode.id,
470
+ type: apiNode.type,
471
+ label: apiNode.label,
472
+ arguments: {
473
+ url: apiNode.arguments.url,
474
+ maxChars: apiNode.arguments.maxChars,
475
+ output: apiNode.arguments.output as OutputBinding,
476
+ },
477
+ };
478
+ case "SetVariable":
479
+ return {
480
+ id: apiNode.id,
481
+ type: apiNode.type,
482
+ label: apiNode.label,
483
+ arguments: {
484
+ variable: apiNode.arguments.variable,
485
+ value: apiNode.arguments.value,
486
+ },
487
+ };
488
+ case "FunctionCall": {
489
+ // Lift the api's nested { inputBindings, outputBindings } into the flat
490
+ // domain arguments record. Uid collisions are impossible within a single
491
+ // function (one namespace across args + returns). `toolDescription`
492
+ // sits at the same level in the api and is folded into the flat bag
493
+ // under the reserved `toolDescription` key.
494
+ const flat: Record<string, Expression | OutputBinding | string> = {
495
+ ...((apiNode.arguments.inputBindings ?? {}) as Record<string, Expression>),
496
+ ...((apiNode.arguments.outputBindings ?? {}) as Record<string, OutputBinding>),
497
+ };
498
+ if (apiNode.arguments.toolDescription !== undefined) {
499
+ flat.toolDescription = apiNode.arguments.toolDescription;
500
+ }
501
+ // The wire carries only `functionId`; rebuild the in-memory signature snapshot
502
+ // from the workflow's function table. A missing function (deleted/hand-edited)
503
+ // gets a minimal stub so the node still loads and surfaces as deleted.
504
+ const functionInfo: FunctionInfo = resolveFunctionInfo?.(apiNode.functionId) ?? {
505
+ id: apiNode.functionId,
506
+ version: 0,
507
+ name: "",
508
+ arguments: [],
509
+ returns: [],
510
+ };
511
+ return {
512
+ id: apiNode.id,
513
+ type: apiNode.type,
514
+ label: apiNode.label,
515
+ functionInfo,
516
+ arguments: flat,
517
+ };
518
+ }
519
+ case "MqttPublish":
520
+ return {
521
+ id: apiNode.id,
522
+ type: apiNode.type,
523
+ label: apiNode.label,
524
+ arguments: {
525
+ channelReference: apiNode.arguments.channelReference ?? "",
526
+ dataType: apiNode.arguments.dataType,
527
+ value: apiNode.arguments.value,
528
+ qos: String(apiNode.arguments.qos) as "0" | "1" | "2",
529
+ retain: apiNode.arguments.retain,
530
+ },
531
+ };
532
+ case "OnMqttMessage":
533
+ return {
534
+ id: apiNode.id,
535
+ type: apiNode.type,
536
+ label: apiNode.label,
537
+ arguments: {
538
+ channelReference: apiNode.arguments.channelReference ?? "",
539
+ dataType: apiNode.arguments.dataType,
540
+ output: apiNode.arguments.output as OutputBinding,
541
+ },
542
+ };
543
+ }
544
+ }
@@ -0,0 +1,68 @@
1
+ import type { Reference } from "../api";
2
+ import { DataType, FromArgs, unwrapFromArgs } from "./Parameter";
3
+
4
+ // ============================================================================
5
+ // RUNTIME VALUES
6
+ // ============================================================================
7
+
8
+ /**
9
+ * Runtime binding stored for a static output. `active=false` means the output
10
+ * is discarded — no variable is produced or assigned. mode/name/target are kept
11
+ * as draft state when inactive so the row can round-trip through an off→on
12
+ * toggle without losing the user's prior choice. The slot's dataType comes from
13
+ * the StaticOutput definition — not carried here.
14
+ */
15
+ export type OutputBinding = { active: boolean; mode: "emit"; name: string } | { active: boolean; mode: "assign"; target: Reference };
16
+
17
+ /**
18
+ * Runtime entry stored in a list output. Each entry is a user-authored
19
+ * output declaration: either a new variable (emit) with its own uid/name/dataType,
20
+ * or a routing to an existing variable (assign). `name` doubles as the JSON
21
+ * property name in the LLM's structured response and (for emit) the new
22
+ * variable's display name in canvas scope; it must be non-empty and unique
23
+ * within the OutputList parameter (validated by diagnostics).
24
+ *
25
+ * Unlike OutputBinding, the slot's dataType is carried on the declaration
26
+ * itself as a contract — staleness against the target (assign mode) is a
27
+ * diagnostic, not a silent retype. No "discard" mode: removing the entry is
28
+ * how you discard it.
29
+ */
30
+ export type OutputDeclaration =
31
+ | { mode: "emit"; uid: string; name: string; dataType: DataType }
32
+ | { mode: "assign"; name: string; dataType: DataType; target: Reference };
33
+
34
+ // ============================================================================
35
+ // OUTPUT PARAMETER DEFINITIONS
36
+ // ============================================================================
37
+
38
+ /**
39
+ * A fixed output produced by every instance of this node type. The `id` is
40
+ * both the field name inside `node.arguments` where the OutputBinding lives
41
+ * and the default emit variable name (overridable via the binding's `name`).
42
+ * The dataType may be static or an args-derived lambda.
43
+ */
44
+ export interface StaticOutput {
45
+ id: string;
46
+ label: string;
47
+ type: "static";
48
+ dataType: FromArgs<DataType>;
49
+ }
50
+
51
+ /** Resolve a StaticOutput's dataType for the current node arguments. */
52
+ export function resolveStaticOutputDataType(output: StaticOutput, args: Record<string, unknown>): DataType {
53
+ return unwrapFromArgs(output.dataType, args);
54
+ }
55
+
56
+ /**
57
+ * A user-managed list of outputs. Entries live in `node.arguments[id]` as
58
+ * `OutputDeclaration[]` — the UI CRUDs the list directly, and each entry
59
+ * contributes one output. Used for Agent's outputDeclarations.
60
+ */
61
+ export interface OutputList {
62
+ id: string;
63
+ label: string;
64
+ type: "list";
65
+ }
66
+
67
+ /** Output parameters mirror input parameters: intersected with ParameterBase for id/label/etc. */
68
+ export type OutputParameter = StaticOutput | OutputList;