@rkmodules/rules 0.0.71 → 0.0.72

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/dist/index.esm.js CHANGED
@@ -1545,8 +1545,36 @@ var primitives$1 = (_a$1 = {},
1545
1545
  _a$1[groupOr.name] = groupOr,
1546
1546
  _a$1);
1547
1547
 
1548
+ var output = {
1549
+ name: "output",
1550
+ label: "Output",
1551
+ description: "Data output.",
1552
+ nodeType: "Output",
1553
+ inputs: {},
1554
+ outputs: {},
1555
+ impl: function () { return __awaiter(void 0, void 0, void 0, function () {
1556
+ return __generator(this, function (_a) {
1557
+ return [2 /*return*/, {}];
1558
+ });
1559
+ }); },
1560
+ };
1561
+
1562
+ var inputs = {
1563
+ name: "inputs",
1564
+ label: "Input",
1565
+ description: "Data input.",
1566
+ nodeType: "Input",
1567
+ inputs: {},
1568
+ outputs: {},
1569
+ impl: function () { return __awaiter(void 0, void 0, void 0, function () {
1570
+ return __generator(this, function (_a) {
1571
+ return [2 /*return*/, {}];
1572
+ });
1573
+ }); },
1574
+ };
1575
+
1548
1576
  var Lib = { Util: primitives$6, Math: primitives$5, List: primitives$4, Sequence: primitives$3, Tree: primitives$2, Logic: primitives$1 };
1549
- var primitives = __assign(__assign(__assign(__assign(__assign(__assign({}, primitives$6), primitives$5), primitives$4), primitives$3), primitives$2), primitives$1);
1577
+ var primitives = __assign(__assign(__assign(__assign(__assign(__assign({ inputs: inputs, output: output }, primitives$6), primitives$5), primitives$4), primitives$3), primitives$2), primitives$1);
1550
1578
 
1551
1579
  function isPrimitive(node) {
1552
1580
  return node.impl !== undefined;
@@ -1645,6 +1673,14 @@ var Engine = /** @class */ (function () {
1645
1673
  case 0:
1646
1674
  _a = node.body, body = _a === void 0 ? {} : _a;
1647
1675
  executionId = uid$1();
1676
+ // add input defaults
1677
+ Object.entries(node.inputs || {}).forEach(function (_a) {
1678
+ var _b = __read(_a, 2), name = _b[0], def = _b[1];
1679
+ var normDef = normalizeVarDef(def);
1680
+ if (!inputs[name] && normDef.default !== undefined) {
1681
+ inputs[name] = normDef.default;
1682
+ }
1683
+ });
1648
1684
  context = {
1649
1685
  scope: { inputs: inputs },
1650
1686
  timings: {},
@@ -1860,6 +1896,13 @@ var Engine = /** @class */ (function () {
1860
1896
  // node handlers
1861
1897
  Engine.prototype.applyNodeDelete = function (oldFn, nodeIds) {
1862
1898
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1899
+ if (nodeIds.every(function (id) { return id === "output"; })) {
1900
+ // cannot delete output node
1901
+ return oldFn;
1902
+ }
1903
+ if (nodeIds.includes("inputs")) {
1904
+ delete newFn.inputs;
1905
+ }
1863
1906
  nodeIds.forEach(function (id) {
1864
1907
  delete newFn.body[id];
1865
1908
  });
@@ -1883,10 +1926,13 @@ var Engine = /** @class */ (function () {
1883
1926
  Engine.prototype.applyEdgeDelete = function (oldFn, fromNode, toNode, fromField, toField) {
1884
1927
  var _a;
1885
1928
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1886
- var from = newFn.body[fromNode];
1929
+ if (toNode === "output") {
1930
+ // no deleting of output edges
1931
+ return oldFn;
1932
+ }
1887
1933
  var to = newFn.body[toNode];
1888
1934
  var fn = this.fnIndex[to === null || to === void 0 ? void 0 : to.name];
1889
- if (!from || !to) {
1935
+ if (!to) {
1890
1936
  console.warn("Invalid nodes for edge deletion");
1891
1937
  return newFn;
1892
1938
  }
@@ -1907,6 +1953,11 @@ var Engine = /** @class */ (function () {
1907
1953
  };
1908
1954
  Engine.prototype.applyNodeAdd = function (oldFn, nodeName, callback) {
1909
1955
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1956
+ if (nodeName === "inputs") {
1957
+ // ensure inputs node
1958
+ newFn.inputs = newFn.inputs || {};
1959
+ return newFn;
1960
+ }
1910
1961
  var id = uid$1();
1911
1962
  // ensure unique id
1912
1963
  while (newFn.body[id]) {
@@ -1940,10 +1991,16 @@ var Engine = /** @class */ (function () {
1940
1991
  return topSort(newFn);
1941
1992
  };
1942
1993
  Engine.prototype.applyNodeConnect = function (oldFn, fromNode, toNode, fromField, toField) {
1994
+ var _a;
1943
1995
  var newFn = __assign(__assign({}, oldFn), { body: __assign({}, oldFn.body) });
1944
- var from = newFn.body[fromNode];
1996
+ if (toNode === "output") {
1997
+ newFn.outputs = __assign(__assign({}, oldFn.outputs), (_a = {}, _a[toField] = "<".concat(fromNode, ".").concat(fromField, ">"), _a));
1998
+ // no need to sort, as only output has changed
1999
+ return newFn;
2000
+ }
2001
+ // const from = newFn.body[fromNode];
1945
2002
  var to = __assign({}, newFn.body[toNode]);
1946
- if (!from || !to) {
2003
+ if (!to) {
1947
2004
  console.warn("Invalid nodes for connection");
1948
2005
  return newFn;
1949
2006
  }
@@ -4725,6 +4782,7 @@ function useRegisteredDropTarget(spec, monitor, connector) {
4725
4782
 
4726
4783
  var dummyEngine = new Engine();
4727
4784
  var FlowContext = React.createContext({
4785
+ graphedFn: {},
4728
4786
  engine: dummyEngine,
4729
4787
  });
4730
4788
  var useEngine = function () {
@@ -4734,6 +4792,9 @@ var empty = {};
4734
4792
  var useControls = function () {
4735
4793
  return React.useContext(FlowContext).controls || empty;
4736
4794
  };
4795
+ var useGraphedFunction = function () {
4796
+ return React.useContext(FlowContext).graphedFn;
4797
+ };
4737
4798
 
4738
4799
  var _a;
4739
4800
  // normalizing import
@@ -4768,7 +4829,7 @@ function Control(_a) {
4768
4829
  }
4769
4830
  }
4770
4831
 
4771
- var styles$1 = {"Port":"Port_XWl7K","required":"required_ofB6J","Icon":"Icon_YJDC5","Input":"Input_PHp3D","Output":"Output_FMRNv","ConnectionPath":"ConnectionPath_XkXr1","valid":"valid_LGNvc","Edge":"Edge_aNtpH","Hover":"Hover_1v8QT"};
4832
+ var styles$1 = {"Port":"Port_XWl7K","required":"required_ofB6J","Icon":"Icon_YJDC5","PortLabel":"PortLabel_wwLN0","Input":"Input_PHp3D","Output":"Output_FMRNv","ConnectionPath":"ConnectionPath_XkXr1","valid":"valid_LGNvc","Edge":"Edge_aNtpH","Hover":"Hover_1v8QT","Menu":"Menu_cusza","MenuCanvas":"MenuCanvas_3M2Ek","MenuItem":"MenuItem_UWLzD"};
4772
4833
 
4773
4834
  var Input = function (_a) {
4774
4835
  var _b;
@@ -4785,7 +4846,7 @@ var Input = function (_a) {
4785
4846
  return (React.createElement(Handle, { type: "target", position: Position.Left, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4786
4847
  "--type-color": "var(--color-".concat(normalized.type, ")"),
4787
4848
  }, onClick: onClick },
4788
- normalized.label || name,
4849
+ React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name),
4789
4850
  !isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleInputChange(name) }))));
4790
4851
  };
4791
4852
 
@@ -4801,10 +4862,10 @@ var Param = function (_a) {
4801
4862
  params: __assign(__assign({}, data.params), (_a = {}, _a[param] = value, _a)),
4802
4863
  });
4803
4864
  }; };
4804
- return (React.createElement("div", { className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4865
+ return (React.createElement("div", { className: classNames(styles$1.Param, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4805
4866
  "--type-color": "var(--color-".concat(normalized.type, ")"),
4806
4867
  }, onClick: onClick },
4807
- React.createElement("span", null, normalized.label || name),
4868
+ React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name),
4808
4869
  !isRef && (React.createElement(Control, { vardef: normalized, value: value, onChange: handleParamChange(name) }))));
4809
4870
  };
4810
4871
 
@@ -4814,16 +4875,16 @@ var Output = function (_a) {
4814
4875
  return (React.createElement(Handle, { type: "source", position: Position.Right, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Output, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
4815
4876
  "--type-color": "var(--color-".concat(normalized.type, ")"),
4816
4877
  }, onClick: onClick },
4817
- React.createElement("span", null,
4878
+ React.createElement("span", { className: styles$1.PortLabel },
4818
4879
  focus && "👁 ",
4819
4880
  normalized.label || name)));
4820
4881
  };
4821
4882
 
4822
- var styles = {"DefaultNode":"DefaultNode_R2P6c","selected":"selected_VmB-F","active":"active_zjUzx","Log":"Log_dsq5e","Label":"Label_wBwWA","DeleteButton":"DeleteButton_kglTP","Description":"Description_f9s4l","PreviewHead":"PreviewHead_oFT4K","expanded":"expanded_3vnbi","Preview":"Preview_HbkZ6","Duration":"Duration_0jqWx","GroupHead":"GroupHead_QJhST","GroupName":"GroupName_Qvu4r","GroupCnt":"GroupCnt_3ctdd","GroupMore":"GroupMore_D1Kiz","GroupChildren":"GroupChildren_NKx-B","GroupLeaf":"GroupLeaf_eliT8"};
4883
+ var styles = {"NodeContainer":"NodeContainer_gSdHY","selected":"selected_VmB-F","active":"active_zjUzx","Log":"Log_dsq5e","Label":"Label_wBwWA","Description":"Description_f9s4l","Body":"Body_ptEZP","PreviewHead":"PreviewHead_oFT4K","expanded":"expanded_3vnbi","Preview":"Preview_HbkZ6","Duration":"Duration_0jqWx","GroupHead":"GroupHead_QJhST","GroupName":"GroupName_Qvu4r","GroupCnt":"GroupCnt_3ctdd","GroupMore":"GroupMore_D1Kiz","GroupChildren":"GroupChildren_NKx-B","GroupLeaf":"GroupLeaf_eliT8"};
4823
4884
 
4824
4885
  function NodeContainer(_a) {
4825
4886
  var _b;
4826
- var id = _a.id, label = _a.label, className = _a.className, selected = _a.selected, children = _a.children;
4887
+ var id = _a.id, label = _a.label, className = _a.className, selected = _a.selected, children = _a.children, noDelete = _a.noDelete;
4827
4888
  var engine = useEngine();
4828
4889
  var _c = __read(React.useState(false), 2), active = _c[0], setActive = _c[1];
4829
4890
  var _d = __read(React.useState(false), 2), deleteMode = _d[0], setDeleteMode = _d[1];
@@ -4854,12 +4915,14 @@ function NodeContainer(_a) {
4854
4915
  };
4855
4916
  }, [id]);
4856
4917
  var handlers = useLongPress(function () {
4857
- setDeleteMode(true);
4918
+ if (!noDelete) {
4919
+ setDeleteMode(true);
4920
+ }
4858
4921
  });
4859
4922
  var handleDelete = function () {
4860
4923
  instance.deleteElements({ nodes: [{ id: id }] });
4861
4924
  };
4862
- return (React.createElement("div", { className: classNames(className, styles.DefaultNode, (_b = {},
4925
+ return (React.createElement("div", { className: classNames(className, styles.NodeContainer, (_b = {},
4863
4926
  _b[styles.selected] = selected,
4864
4927
  _b[styles.active] = active,
4865
4928
  _b)) },
@@ -5042,6 +5105,118 @@ var Default = React.memo(function (_a) {
5042
5105
  });
5043
5106
  Default.displayName = "DefaultNode";
5044
5107
 
5108
+ function Menu(_a) {
5109
+ var handle = _a.handle, children = _a.children;
5110
+ var _b = __read(React.useState(false), 2), open = _b[0], setOpen = _b[1];
5111
+ var handleOpen = function () {
5112
+ setOpen(!open);
5113
+ };
5114
+ var handleClose = function () {
5115
+ setOpen(false);
5116
+ };
5117
+ return (React.createElement("div", { className: styles$1.Menu },
5118
+ React.createElement("div", { onClick: handleOpen }, handle),
5119
+ open && (React.createElement("div", { className: styles$1.MenuCanvas, onClick: handleClose }, children))));
5120
+ }
5121
+ function MenuItem(_a) {
5122
+ var children = _a.children, props = __rest(_a, ["children"]);
5123
+ return (React.createElement("div", __assign({ className: styles$1.MenuItem }, props), children));
5124
+ }
5125
+
5126
+ /**
5127
+ * todo: get input data from graphed function
5128
+ */
5129
+ var InputNode = React.memo(function (_a) {
5130
+ var id = _a.id, data = _a.data, selected = _a.selected;
5131
+ var fn = useGraphedFunction();
5132
+ var outputs = Object.fromEntries(Object.keys(fn.inputs || {}).map(function (name) { return [
5133
+ name,
5134
+ normalizeVarDef(fn.inputs[name]),
5135
+ ]; }));
5136
+ var onRename = function (id, oldLabel) {
5137
+ var _a;
5138
+ var label = prompt("Enter new name", oldLabel);
5139
+ if (label !== null && label !== oldLabel) {
5140
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5141
+ var inputs = __assign({}, fn.inputs);
5142
+ var n = normalizeVarDef(inputs[id]);
5143
+ inputs[id] = __assign(__assign({}, n), { label: label });
5144
+ return { inputs: inputs };
5145
+ });
5146
+ }
5147
+ };
5148
+ var onAdd = function () {
5149
+ var _a;
5150
+ var label = prompt("Enter new name");
5151
+ if (label !== null) {
5152
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5153
+ var inputs = __assign({}, fn.inputs);
5154
+ inputs[uid$1()] = { type: "any", label: label };
5155
+ return { inputs: inputs };
5156
+ });
5157
+ }
5158
+ };
5159
+ var onDelete = function (name) {
5160
+ var _a;
5161
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5162
+ var inputs = __assign({}, fn.inputs);
5163
+ delete inputs[name];
5164
+ return { inputs: inputs };
5165
+ });
5166
+ };
5167
+ var onDefault = function (id) {
5168
+ var _a, _b;
5169
+ var value = prompt("Enter default value");
5170
+ if (value !== null && ((_a = fn.inputs) === null || _a === void 0 ? void 0 : _a[id])) {
5171
+ var n_1 = normalizeVarDef(fn.inputs[id]);
5172
+ if (n_1.type === "number") {
5173
+ value = Number(value);
5174
+ }
5175
+ else if (n_1.type === "boolean") {
5176
+ value = value.toLowerCase() === "true";
5177
+ }
5178
+ (_b = data.onIOChange) === null || _b === void 0 ? void 0 : _b.call(data, function (fn) {
5179
+ var inputs = __assign({}, fn.inputs);
5180
+ inputs[id] = __assign(__assign({}, n_1), { default: value });
5181
+ return { inputs: inputs };
5182
+ });
5183
+ }
5184
+ };
5185
+ var onType = function (id) {
5186
+ var _a, _b;
5187
+ var type = prompt("Enter type");
5188
+ if (type !== null && ((_a = fn.inputs) === null || _a === void 0 ? void 0 : _a[id])) {
5189
+ var n_2 = normalizeVarDef(fn.inputs[id]);
5190
+ (_b = data.onIOChange) === null || _b === void 0 ? void 0 : _b.call(data, function (fn) {
5191
+ var inputs = __assign({}, fn.inputs);
5192
+ inputs[id] = __assign(__assign({}, n_2), { type: type });
5193
+ return { inputs: inputs };
5194
+ });
5195
+ }
5196
+ };
5197
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id, noDelete: true },
5198
+ React.createElement("div", { className: styles.Description }, data.description),
5199
+ React.createElement("div", { className: styles.Ports },
5200
+ Object.entries(outputs || {}).map(function (_a) {
5201
+ var _b = __read(_a, 2), name = _b[0], normalized = _b[1];
5202
+ return (React.createElement(Handle, { key: name, type: "source", position: Position.Right, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Output, styles$1.Port, styles$1["type-".concat(normalized.type)]), style: {
5203
+ "--type-color": "var(--color-".concat(normalized.type, ")"),
5204
+ } },
5205
+ React.createElement(Menu, { handle: React.createElement("button", null, "...") },
5206
+ React.createElement(MenuItem, { onClick: function () {
5207
+ return onRename(name, normalized.label || name);
5208
+ } }, "Rename"),
5209
+ React.createElement(MenuItem, { onClick: function () { return onDelete(name); } }, "Delete"),
5210
+ React.createElement(MenuItem, { onClick: function () { return onDefault(name); } }, "Set Default"),
5211
+ React.createElement(MenuItem, { onClick: function () { return onType(name); } }, "Set Type")),
5212
+ React.createElement("span", { style: { flex: 1 } }),
5213
+ React.createElement("span", { className: styles$1.PortLabel }, normalized.label || name)));
5214
+ }),
5215
+ React.createElement("div", { className: styles.Body },
5216
+ React.createElement("button", { onClick: function () { return onAdd(); } }, "+")))));
5217
+ });
5218
+ InputNode.displayName = "InputNode";
5219
+
5045
5220
  function uid() {
5046
5221
  return Math.random().toString(36).substring(2, 15);
5047
5222
  }
@@ -5054,7 +5229,8 @@ var Merge = React.memo(function (_a) {
5054
5229
  var inputCount = Object.keys(inputs).length;
5055
5230
  inputs[uid()] = { type: "any", label: "D".concat(inputCount + 1) };
5056
5231
  var outputs = __assign({}, (data.outputDefs || {}));
5057
- return (React.createElement(NodeContainer, { label: data.name, selected: selected, id: id },
5232
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id },
5233
+ React.createElement("div", { className: styles.Description }, data.description),
5058
5234
  React.createElement("div", { className: styles.Ports },
5059
5235
  Object.entries(outputs || {}).map(function (_a) {
5060
5236
  var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
@@ -5067,11 +5243,93 @@ var Merge = React.memo(function (_a) {
5067
5243
  });
5068
5244
  Merge.displayName = "MergeNode";
5069
5245
 
5246
+ /**
5247
+ * todo: create a factory for output component
5248
+ * - allow to pass prompt handler (async)
5249
+ * - allow to pass custom UI for renaming/adding/deleting
5250
+ *
5251
+ * - create input node
5252
+ */
5253
+ var OutputNode = React.memo(function (_a) {
5254
+ var id = _a.id, data = _a.data, selected = _a.selected;
5255
+ var engine = useEngine();
5256
+ var fn = useGraphedFunction();
5257
+ var inputs = Object.fromEntries(Object.entries(data.inputs || {}).map(function (_a, i) {
5258
+ var _b, _c;
5259
+ var _d = __read(_a, 2), name = _d[0], ref = _d[1];
5260
+ var type = "any";
5261
+ if (isReference(ref)) {
5262
+ var _e = __read(parseReference(ref).split("."), 2), fnId = _e[0], outputName = _e[1];
5263
+ var fnName = (_b = fn.body[fnId]) === null || _b === void 0 ? void 0 : _b.name;
5264
+ if (fnName) {
5265
+ var calledFn = engine.getFunction(fnName);
5266
+ var outputDef = (_c = calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) === null || _c === void 0 ? void 0 : _c[outputName];
5267
+ if (outputDef) {
5268
+ var n = normalizeVarDef(outputDef);
5269
+ if (n === null || n === void 0 ? void 0 : n.type) {
5270
+ type = n.type;
5271
+ }
5272
+ }
5273
+ }
5274
+ }
5275
+ return [name, { type: type, label: name }];
5276
+ }));
5277
+ var onRename = function (oldName) {
5278
+ var _a;
5279
+ var newName = prompt("Enter new name", oldName);
5280
+ if (newName !== null && newName !== oldName) {
5281
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5282
+ var outputs = __assign({}, fn.outputs);
5283
+ outputs[newName] = outputs[oldName];
5284
+ delete outputs[oldName];
5285
+ return { outputs: outputs };
5286
+ });
5287
+ }
5288
+ };
5289
+ var onAdd = function () {
5290
+ var _a;
5291
+ var newName = prompt("Enter new name");
5292
+ if (newName !== null) {
5293
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5294
+ var outputs = __assign({}, fn.outputs);
5295
+ outputs[newName] = "";
5296
+ return { outputs: outputs };
5297
+ });
5298
+ }
5299
+ };
5300
+ var onDelete = function (name) {
5301
+ var _a;
5302
+ (_a = data.onIOChange) === null || _a === void 0 ? void 0 : _a.call(data, function (fn) {
5303
+ var outputs = __assign({}, fn.outputs);
5304
+ delete outputs[name];
5305
+ return { outputs: outputs };
5306
+ });
5307
+ };
5308
+ return (React.createElement(NodeContainer, { label: data.label, selected: selected, id: id, noDelete: true },
5309
+ React.createElement("div", { className: styles.Description }, data.description),
5310
+ React.createElement("div", { className: styles.Ports }, Object.entries(inputs || {}).map(function (_a) {
5311
+ var _b = __read(_a, 2), name = _b[0], varDef = _b[1];
5312
+ return (React.createElement(Handle, { key: name, type: "target", position: Position.Left, id: "".concat(id, "-").concat(name), className: classNames(styles$1.Input, styles$1.Port, styles$1["type-".concat(varDef.type)]), style: {
5313
+ "--type-color": "var(--color-".concat(varDef.type, ")"),
5314
+ } },
5315
+ React.createElement("span", { className: styles$1.PortLabel }, name),
5316
+ React.createElement("span", { style: { flex: 1 } }),
5317
+ React.createElement(Menu, { handle: React.createElement("button", null, "...") },
5318
+ React.createElement(MenuItem, { onClick: function () { return onRename(name); } }, "Rename"),
5319
+ React.createElement(MenuItem, { onClick: function () { return onDelete(name); } }, "Delete"))));
5320
+ })),
5321
+ React.createElement("div", { className: styles.Body },
5322
+ React.createElement("button", { onClick: function () { return onAdd(); } }, "+"))));
5323
+ });
5324
+ OutputNode.displayName = "OutputNode";
5325
+
5070
5326
  var nodeTypes = {
5071
5327
  Default: Default,
5072
5328
  Calc: Calc,
5073
5329
  Log: Log,
5074
5330
  Merge: Merge,
5331
+ Input: InputNode,
5332
+ Output: OutputNode,
5075
5333
  };
5076
5334
 
5077
5335
  var variableStore = create$1(function (set, get, api) {
@@ -5146,6 +5404,38 @@ function useUpdatePositions(fn) {
5146
5404
  function updateNodeArguments(node, inputs, params) {
5147
5405
  return __assign(__assign({}, node), { data: __assign(__assign({}, node.data), { inputs: inputs, params: params }) });
5148
5406
  }
5407
+ function createNode(engine, id, fncall, options) {
5408
+ var _a, _b;
5409
+ var calledFn = engine.getFunction(fncall.name);
5410
+ // create a node for each function call
5411
+ return {
5412
+ id: id,
5413
+ data: {
5414
+ name: fncall.name,
5415
+ label: fncall.label || (calledFn === null || calledFn === void 0 ? void 0 : calledFn.label) || fncall.name,
5416
+ description: calledFn === null || calledFn === void 0 ? void 0 : calledFn.description,
5417
+ inputs: fncall.inputs,
5418
+ params: fncall.params,
5419
+ inputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs) || {},
5420
+ paramDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.params) || {},
5421
+ outputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) || {},
5422
+ onChange: ((_a = options.onDataChange) === null || _a === void 0 ? void 0 : _a.call(options, id)) || (function () { }),
5423
+ onIOChange: options.onIOChange || (function (fn) { return fn; }),
5424
+ },
5425
+ position: ((_b = options.positions) === null || _b === void 0 ? void 0 : _b[id]) || { x: 0, y: 0 },
5426
+ dragHandle: options.dragHandle,
5427
+ type: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.nodeType) || "Default",
5428
+ };
5429
+ }
5430
+ function createEdge(nodeId, ref) {
5431
+ return {
5432
+ id: "".concat(ref.path, "-").concat(nodeId, ".").concat(ref.name),
5433
+ source: ref.refNode,
5434
+ target: nodeId,
5435
+ sourceHandle: "".concat(ref.refNode, "-").concat(ref.refField),
5436
+ targetHandle: "".concat(nodeId, "-").concat(ref.name),
5437
+ };
5438
+ }
5149
5439
  /**
5150
5440
  * flow state stores nodes and edges for the flow graph these can be updated when the function changes
5151
5441
  * using setFn
@@ -5154,17 +5444,22 @@ var useFlowState = create$1(function (set, get) { return ({
5154
5444
  engine: null,
5155
5445
  fn: null,
5156
5446
  options: {},
5157
- nodes: [],
5447
+ nodes: {},
5158
5448
  edges: [],
5159
5449
  setNodes: function (updater) {
5160
- set(function (state) { return ({ nodes: updater(state.nodes) }); });
5450
+ set(function (state) {
5451
+ var currentNodes = Object.values(state.nodes);
5452
+ var newNodes = updater(currentNodes);
5453
+ var nodes = Object.fromEntries(newNodes.map(function (n) { return [n.id, n]; }));
5454
+ return { nodes: nodes };
5455
+ });
5161
5456
  },
5162
5457
  setEdges: function (updater) {
5163
5458
  set(function (state) { return ({ edges: updater(state.edges) }); });
5164
5459
  },
5165
- setFn: function (fn) {
5460
+ setFn: function (fn, options) {
5166
5461
  // console.log("setFn", fn);
5167
- var _a = get(), oldFn = _a.fn, nodes = _a.nodes, edges = _a.edges, engine = _a.engine, options = _a.options;
5462
+ var _a = get(), oldFn = _a.fn, nodes = _a.nodes, edges = _a.edges, engine = _a.engine;
5168
5463
  // update the graph by shallow comparison
5169
5464
  if (!engine) {
5170
5465
  throw new Error("Engine not set");
@@ -5172,18 +5467,51 @@ var useFlowState = create$1(function (set, get) { return ({
5172
5467
  var oldKeys = Object.keys((oldFn === null || oldFn === void 0 ? void 0 : oldFn.body) || {});
5173
5468
  var newKeys = Object.keys(fn.body || {});
5174
5469
  // remove all the nodes that were removed
5175
- nodes = nodes === null || nodes === void 0 ? void 0 : nodes.filter(function (node) { return newKeys.includes(node.id); });
5470
+ Object.keys(nodes).forEach(function (key) {
5471
+ if (!newKeys.includes(key)) {
5472
+ delete nodes[key];
5473
+ }
5474
+ });
5176
5475
  // remove all the edges that were removed
5177
5476
  edges = edges === null || edges === void 0 ? void 0 : edges.filter(function (edge) {
5178
- return (newKeys.includes(edge.source) && newKeys.includes(edge.target));
5477
+ return ((newKeys.includes(edge.source) || edge.source === "inputs") &&
5478
+ newKeys.includes(edge.target));
5179
5479
  });
5480
+ // make sure there is an output node if outputs are defined
5481
+ if (fn.outputs && !nodes["output"]) {
5482
+ var key_1 = "output";
5483
+ var fncall = {
5484
+ name: "output",
5485
+ inputs: fn.outputs || {},
5486
+ params: {},
5487
+ };
5488
+ var newNode = createNode(engine, key_1, fncall, options);
5489
+ nodes[key_1] = newNode;
5490
+ // add edges
5491
+ var refs = getReferences(fncall.inputs || {});
5492
+ refs.forEach(function (ref) {
5493
+ edges.push(createEdge(key_1, ref));
5494
+ });
5495
+ }
5496
+ // make sure there is an input node if inputs are defined
5497
+ if (fn.inputs && !nodes["inputs"]) {
5498
+ var key = "inputs";
5499
+ var fncall = {
5500
+ name: "inputs",
5501
+ inputs: {},
5502
+ params: {},
5503
+ };
5504
+ var newNode = createNode(engine, key, fncall, options);
5505
+ nodes[key] = newNode;
5506
+ }
5180
5507
  // create the new nodes that were added
5181
5508
  var addedKeys = newKeys.filter(function (key) { return !oldKeys.includes(key); });
5182
5509
  addedKeys.forEach(function (key, index) {
5183
5510
  var _a;
5184
5511
  var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
5185
5512
  // add the node
5186
- nodes.push(createNode(engine, key, fncall, options));
5513
+ var newNode = createNode(engine, key, fncall, options);
5514
+ nodes[newNode.id] = newNode;
5187
5515
  // add edges
5188
5516
  var refs = getReferences(fncall.inputs || {});
5189
5517
  refs.forEach(function (ref) {
@@ -5201,12 +5529,8 @@ var useFlowState = create$1(function (set, get) { return ({
5201
5529
  var _a;
5202
5530
  // update the nodes that were changed
5203
5531
  var fncall = (_a = fn.body) === null || _a === void 0 ? void 0 : _a[key];
5204
- nodes = nodes === null || nodes === void 0 ? void 0 : nodes.map(function (n) {
5205
- if (n.id === key) {
5206
- return updateNodeArguments(n, fncall.inputs, fncall.params);
5207
- }
5208
- return n;
5209
- });
5532
+ var node = nodes[key];
5533
+ nodes[key] = updateNodeArguments(node, fncall.inputs, fncall.params);
5210
5534
  // update incoming edges
5211
5535
  var refs = getReferences(fncall.inputs || {});
5212
5536
  // remove all incoming edges
@@ -5223,7 +5547,7 @@ var useFlowState = create$1(function (set, get) { return ({
5223
5547
  init: function (fn, engine, options) {
5224
5548
  // console.log("initing");
5225
5549
  set({ engine: engine, options: options });
5226
- get().setFn(fn);
5550
+ get().setFn(fn, options);
5227
5551
  },
5228
5552
  }); });
5229
5553
  /**
@@ -5235,46 +5559,15 @@ function useFlow(fn, engine, options) {
5235
5559
  state.init(fn, engine, options);
5236
5560
  }, [engine]);
5237
5561
  React.useEffect(function () {
5238
- state.setFn(fn);
5239
- }, [fn]);
5562
+ state.setFn(fn, options);
5563
+ }, [fn, options]);
5240
5564
  return React.useMemo(function () { return ({
5241
- nodes: state.nodes,
5565
+ nodes: Object.values(state.nodes),
5242
5566
  edges: state.edges,
5243
5567
  setNodes: state.setNodes,
5244
5568
  setEdges: state.setEdges,
5245
5569
  }); }, [state]);
5246
5570
  }
5247
- function createNode(engine, id, fncall, options) {
5248
- var _a, _b;
5249
- var calledFn = engine.getFunction(fncall.name);
5250
- // create a node for each function call
5251
- return {
5252
- id: id,
5253
- data: {
5254
- name: fncall.name,
5255
- label: fncall.label || (calledFn === null || calledFn === void 0 ? void 0 : calledFn.label) || fncall.name,
5256
- description: calledFn === null || calledFn === void 0 ? void 0 : calledFn.description,
5257
- inputs: fncall.inputs,
5258
- params: fncall.params,
5259
- inputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.inputs) || {},
5260
- paramDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.params) || {},
5261
- outputDefs: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.outputs) || {},
5262
- onChange: ((_a = options.onDataChange) === null || _a === void 0 ? void 0 : _a.call(options, id)) || (function () { }),
5263
- },
5264
- position: ((_b = options.positions) === null || _b === void 0 ? void 0 : _b[id]) || { x: 0, y: 0 },
5265
- dragHandle: options.dragHandle,
5266
- type: (calledFn === null || calledFn === void 0 ? void 0 : calledFn.nodeType) || "Default",
5267
- };
5268
- }
5269
- function createEdge(nodeId, ref) {
5270
- return {
5271
- id: "".concat(ref.path, "-").concat(nodeId, ".").concat(ref.name),
5272
- source: ref.refNode,
5273
- target: nodeId,
5274
- sourceHandle: "".concat(ref.refNode, "-").concat(ref.refField),
5275
- targetHandle: "".concat(nodeId, "-").concat(ref.name),
5276
- };
5277
- }
5278
5571
 
5279
5572
  var ConnectionLine = (function (_a) {
5280
5573
  var _b;
@@ -5306,27 +5599,37 @@ var ConnectionLine = (function (_a) {
5306
5599
  });
5307
5600
 
5308
5601
  function FlowInner(_a) {
5309
- var fn = _a.function, engine = _a.engine, onChange = _a.onChange, onClick = _a.onClick, onSelect = _a.onSelect;
5310
- var _b = __read(React.useState(null), 2), instance = _b[0], setInstance = _b[1];
5602
+ var fn = _a.function, engine = _a.engine, _b = _a.center, center = _b === void 0 ? true : _b, fitView = _a.fitView, onChange = _a.onChange, onClick = _a.onClick, onSelect = _a.onSelect;
5603
+ var _c = __read(React.useState(null), 2), instance = _c[0], setInstance = _c[1];
5311
5604
  var updatePosition = useUpdatePositions(fn);
5312
- var _c = __read(usePositions(fn), 1), positions = _c[0];
5313
- var handleDataChange = function (id) { return function (newData) {
5314
- onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
5315
- // update the function definition
5316
- // console.log("before fn call update", Object.keys(fn.body));
5317
- var body = __assign({}, fn.body);
5318
- var fncall = __assign(__assign({}, (body[id] || {})), newData);
5319
- body[id] = fncall;
5320
- // console.log("after fn call update", Object.keys(body));
5321
- var updatedFn = __assign(__assign({}, fn), { body: body });
5322
- return updatedFn;
5323
- });
5324
- }; };
5325
- var _d = useFlow(fn, engine, {
5326
- dragHandle: ".".concat(styles.Label),
5327
- positions: positions,
5328
- onDataChange: handleDataChange,
5329
- }), nodes = _d.nodes, edges = _d.edges, setNodes = _d.setNodes, setEdges = _d.setEdges;
5605
+ var _d = __read(usePositions(fn), 1), positions = _d[0];
5606
+ var options = React.useMemo(function () {
5607
+ var handleDataChange = function (id) { return function (newData) {
5608
+ onChange === null || onChange === void 0 ? void 0 : onChange(function (fn) {
5609
+ // update the function definition
5610
+ // console.log("before fn call update", Object.keys(fn.body));
5611
+ var body = __assign({}, fn.body);
5612
+ var fncall = __assign(__assign({}, (body[id] || {})), newData);
5613
+ body[id] = fncall;
5614
+ // console.log("after fn call update", Object.keys(body));
5615
+ var updatedFn = __assign(__assign({}, fn), { body: body });
5616
+ return updatedFn;
5617
+ });
5618
+ }; };
5619
+ var handleIOChange = function (callback) {
5620
+ var changes = callback(fn);
5621
+ onChange === null || onChange === void 0 ? void 0 : onChange(function (oldFn) {
5622
+ return __assign(__assign({}, oldFn), changes);
5623
+ });
5624
+ };
5625
+ return {
5626
+ dragHandle: ".".concat(styles.Label),
5627
+ positions: positions,
5628
+ onDataChange: handleDataChange,
5629
+ onIOChange: handleIOChange,
5630
+ };
5631
+ }, [fn, onChange]);
5632
+ var _e = useFlow(fn, engine, options), nodes = _e.nodes, edges = _e.edges, setNodes = _e.setNodes, setEdges = _e.setEdges;
5330
5633
  // node selection handler
5331
5634
  var handleSelect = React.useCallback(function (_a) {
5332
5635
  var nodes = _a.nodes;
@@ -5336,7 +5639,7 @@ function FlowInner(_a) {
5336
5639
  onChange: handleSelect,
5337
5640
  });
5338
5641
  // dropping nodes
5339
- var _e = __read(useDrop({
5642
+ var _f = __read(useDrop({
5340
5643
  accept: "flow-node",
5341
5644
  drop: function (item, monitor) {
5342
5645
  var pos = monitor.getClientOffset();
@@ -5356,7 +5659,7 @@ function FlowInner(_a) {
5356
5659
  isOver: monitor.isOver(),
5357
5660
  };
5358
5661
  },
5359
- }), 2); _e[0].isOver; var drop = _e[1];
5662
+ }), 2); _f[0].isOver; var drop = _f[1];
5360
5663
  // connecting nodes
5361
5664
  var onConnect = React.useCallback(function (params) {
5362
5665
  var _a, _b;
@@ -5404,27 +5707,43 @@ function FlowInner(_a) {
5404
5707
  }
5405
5708
  };
5406
5709
  var onNodesChange = React.useCallback(function (changes) {
5710
+ changes = changes.filter(function (change) {
5711
+ if (change.id === "output" && change.type === "remove")
5712
+ return false;
5713
+ return true;
5714
+ });
5407
5715
  return setNodes(function (nodesSnapshot) {
5408
5716
  return applyNodeChanges(changes, nodesSnapshot);
5409
5717
  });
5410
5718
  }, []);
5411
5719
  var onEdgesChange = React.useCallback(function (changes) {
5720
+ changes = changes.filter(function (change) {
5721
+ if (change.id.includes("-output.") && change.type === "remove")
5722
+ return false;
5723
+ return true;
5724
+ });
5412
5725
  return setEdges(function (edgesSnapshot) {
5413
5726
  return applyEdgeChanges(changes, edgesSnapshot);
5414
5727
  });
5415
5728
  }, []);
5416
- return (React.createElement(ReactFlow, { ref: drop, nodes: nodes, edges: edges, nodeTypes: nodeTypes, onNodesChange: onNodesChange, onEdgesChange: onEdgesChange, onNodesDelete: onNodesDelete, onEdgesDelete: onEdgesDelete, onNodeDragStop: function (e, n) { return updatePosition(n.id, n.position); }, onConnect: onConnect, onInit: setInstance, onPaneClick: handleClick, fitView: true,
5729
+ return (React.createElement(ReactFlow, { ref: drop, nodes: nodes, edges: edges, nodeTypes: nodeTypes, onNodesChange: onNodesChange, onEdgesChange: onEdgesChange, onNodesDelete: onNodesDelete, onEdgesDelete: onEdgesDelete, onNodeDragStop: function (e, n) { return updatePosition(n.id, n.position); }, onConnect: onConnect, onInit: setInstance, onPaneClick: handleClick, fitView: center || fitView, fitViewOptions: {
5730
+ minZoom: fitView ? undefined : 1,
5731
+ maxZoom: fitView ? undefined : 1,
5732
+ },
5417
5733
  // panOnDrag={false}
5418
5734
  selectionOnDrag: true, minZoom: 0.1, maxZoom: 4, deleteKeyCode: "Delete", connectionLineComponent: ConnectionLine },
5419
5735
  React.createElement(Background, { variant: BackgroundVariant.Dots }),
5420
5736
  React.createElement(Controls, null)));
5421
5737
  }
5422
- function Flow(_a) {
5423
- var fn = _a.function, engine = _a.engine, _b = _a.onChange, onChange = _b === void 0 ? function () { } : _b, onClick = _a.onClick, onSelect = _a.onSelect, customControls = _a.customControls;
5424
- var contextValue = React.useMemo(function () { return ({ engine: engine, controls: customControls }); }, [engine, customControls]);
5738
+ function Flow(props) {
5739
+ var contextValue = React.useMemo(function () { return ({
5740
+ engine: props.engine,
5741
+ controls: props.customControls,
5742
+ graphedFn: props.function,
5743
+ }); }, [props.engine, props.customControls, props.function]);
5425
5744
  return (React.createElement(FlowContext.Provider, { value: contextValue },
5426
5745
  React.createElement(ReactFlowProvider, null,
5427
- React.createElement(FlowInner, { function: fn, engine: engine, onChange: onChange, onClick: onClick, onSelect: onSelect }))));
5746
+ React.createElement(FlowInner, __assign({}, props)))));
5428
5747
  }
5429
5748
 
5430
5749
  function useDraggableNode(name, fn) {