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