@bian-womp/spark-graph 0.3.20 → 0.3.22
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/lib/cjs/index.cjs +165 -81
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/cjs/src/index.d.ts +2 -2
- package/lib/cjs/src/index.d.ts.map +1 -1
- package/lib/cjs/src/misc/utils/json.d.ts +1 -2
- package/lib/cjs/src/misc/utils/json.d.ts.map +1 -1
- package/lib/cjs/src/misc/utils/merge.d.ts +2 -24
- package/lib/cjs/src/misc/utils/merge.d.ts.map +1 -1
- package/lib/cjs/src/runtime/GraphLifecycleApi.d.ts +6 -1
- package/lib/cjs/src/runtime/GraphLifecycleApi.d.ts.map +1 -1
- package/lib/cjs/src/runtime/GraphRuntime.d.ts +4 -1
- package/lib/cjs/src/runtime/GraphRuntime.d.ts.map +1 -1
- package/lib/cjs/src/runtime/LocalEngine.d.ts +1 -1
- package/lib/cjs/src/runtime/LocalEngine.d.ts.map +1 -1
- package/lib/esm/index.js +165 -82
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/src/index.d.ts +2 -2
- package/lib/esm/src/index.d.ts.map +1 -1
- package/lib/esm/src/misc/utils/json.d.ts +1 -2
- package/lib/esm/src/misc/utils/json.d.ts.map +1 -1
- package/lib/esm/src/misc/utils/merge.d.ts +2 -24
- package/lib/esm/src/misc/utils/merge.d.ts.map +1 -1
- package/lib/esm/src/runtime/GraphLifecycleApi.d.ts +6 -1
- package/lib/esm/src/runtime/GraphLifecycleApi.d.ts.map +1 -1
- package/lib/esm/src/runtime/GraphRuntime.d.ts +4 -1
- package/lib/esm/src/runtime/GraphRuntime.d.ts.map +1 -1
- package/lib/esm/src/runtime/LocalEngine.d.ts +1 -1
- package/lib/esm/src/runtime/LocalEngine.d.ts.map +1 -1
- package/package.json +2 -2
package/lib/cjs/index.cjs
CHANGED
|
@@ -2766,6 +2766,7 @@ class GraphRuntime {
|
|
|
2766
2766
|
this.environment = {};
|
|
2767
2767
|
this.runMode = null;
|
|
2768
2768
|
this.pauseRefCount = 0;
|
|
2769
|
+
this.persistentPauseToken = null;
|
|
2769
2770
|
// Initialize components
|
|
2770
2771
|
this.graph = new Graph();
|
|
2771
2772
|
this.eventEmitter = new EventEmitter();
|
|
@@ -2916,7 +2917,7 @@ class GraphRuntime {
|
|
|
2916
2917
|
const node = this.graph.getNode(nodeId);
|
|
2917
2918
|
return node?.outputs[output];
|
|
2918
2919
|
}
|
|
2919
|
-
launch(invalidate = false) {
|
|
2920
|
+
launch(invalidate = false, startPaused = false) {
|
|
2920
2921
|
this.graph.forEachNode((node) => {
|
|
2921
2922
|
const effectiveInputs = this.nodeExecutor.getEffectiveInputs(node.nodeId);
|
|
2922
2923
|
const ctrl = new AbortController();
|
|
@@ -2934,6 +2935,9 @@ class GraphRuntime {
|
|
|
2934
2935
|
this.execute(nodeId);
|
|
2935
2936
|
}
|
|
2936
2937
|
}
|
|
2938
|
+
if (startPaused) {
|
|
2939
|
+
this.pause();
|
|
2940
|
+
}
|
|
2937
2941
|
}
|
|
2938
2942
|
triggerExternal(nodeId, event) {
|
|
2939
2943
|
const node = this.graph.getNode(nodeId);
|
|
@@ -3076,6 +3080,17 @@ class GraphRuntime {
|
|
|
3076
3080
|
isPaused() {
|
|
3077
3081
|
return this.pauseRefCount > 0;
|
|
3078
3082
|
}
|
|
3083
|
+
pause() {
|
|
3084
|
+
if (!this.persistentPauseToken) {
|
|
3085
|
+
this.persistentPauseToken = this.requestPause();
|
|
3086
|
+
}
|
|
3087
|
+
}
|
|
3088
|
+
resume() {
|
|
3089
|
+
if (this.persistentPauseToken) {
|
|
3090
|
+
this.persistentPauseToken();
|
|
3091
|
+
this.persistentPauseToken = null;
|
|
3092
|
+
}
|
|
3093
|
+
}
|
|
3079
3094
|
copyOutputs(fromNodeId, toNodeId, options) {
|
|
3080
3095
|
const fromNode = this.getNodeData(fromNodeId);
|
|
3081
3096
|
if (!fromNode?.outputs)
|
|
@@ -3732,10 +3747,10 @@ class LocalEngine {
|
|
|
3732
3747
|
dispose() {
|
|
3733
3748
|
// this.graphRuntime.dispose();
|
|
3734
3749
|
}
|
|
3735
|
-
launch(invalidate, runMode) {
|
|
3750
|
+
launch(invalidate, runMode, startPaused) {
|
|
3736
3751
|
if (runMode)
|
|
3737
3752
|
this.setRunMode(runMode);
|
|
3738
|
-
this.graphRuntime.launch(invalidate);
|
|
3753
|
+
this.graphRuntime.launch(invalidate, startPaused);
|
|
3739
3754
|
}
|
|
3740
3755
|
/**
|
|
3741
3756
|
* Run only this node, no downstream propagation.
|
|
@@ -5535,22 +5550,138 @@ function offsetImportedPositions(targetPositions, sourcePositions, sourceDef, no
|
|
|
5535
5550
|
}
|
|
5536
5551
|
return newPositions;
|
|
5537
5552
|
}
|
|
5553
|
+
function buildTypeMaps(def) {
|
|
5554
|
+
const nodeTypeMap = new Map();
|
|
5555
|
+
const handleTypeMap = new Map();
|
|
5556
|
+
for (const node of def.nodes) {
|
|
5557
|
+
nodeTypeMap.set(node.nodeId, node.typeId);
|
|
5558
|
+
const nodeHandleTypes = new Map();
|
|
5559
|
+
if (node.resolvedHandles?.inputs) {
|
|
5560
|
+
for (const [handleId, handleDesc] of Object.entries(node.resolvedHandles.inputs)) {
|
|
5561
|
+
const typeId = typeof handleDesc === "string"
|
|
5562
|
+
? handleDesc
|
|
5563
|
+
: handleDesc?.typeId;
|
|
5564
|
+
if (typeId)
|
|
5565
|
+
nodeHandleTypes.set(handleId, typeId);
|
|
5566
|
+
}
|
|
5567
|
+
}
|
|
5568
|
+
if (node.resolvedHandles?.outputs) {
|
|
5569
|
+
for (const [handleId, handleDesc] of Object.entries(node.resolvedHandles.outputs)) {
|
|
5570
|
+
const typeId = typeof handleDesc === "string"
|
|
5571
|
+
? handleDesc
|
|
5572
|
+
: Array.isArray(handleDesc)
|
|
5573
|
+
? handleDesc[0]
|
|
5574
|
+
: undefined;
|
|
5575
|
+
if (typeId)
|
|
5576
|
+
nodeHandleTypes.set(handleId, typeId);
|
|
5577
|
+
}
|
|
5578
|
+
}
|
|
5579
|
+
if (nodeHandleTypes.size > 0) {
|
|
5580
|
+
handleTypeMap.set(node.nodeId, nodeHandleTypes);
|
|
5581
|
+
}
|
|
5582
|
+
}
|
|
5583
|
+
return { nodeTypeMap, handleTypeMap };
|
|
5584
|
+
}
|
|
5585
|
+
function applyConversion(items, values, converter, type) {
|
|
5586
|
+
for (const item of items) {
|
|
5587
|
+
const convertedValue = converter({
|
|
5588
|
+
nodeId: item.nodeId,
|
|
5589
|
+
handleId: item.handleId,
|
|
5590
|
+
value: item.value,
|
|
5591
|
+
type,
|
|
5592
|
+
nodeTypeId: item.nodeTypeId,
|
|
5593
|
+
handleDataType: item.handleDataType,
|
|
5594
|
+
runtimeTypeId: item.runtimeTypeId,
|
|
5595
|
+
});
|
|
5596
|
+
if (convertedValue === null) {
|
|
5597
|
+
delete values[item.nodeId]?.[item.handleId];
|
|
5598
|
+
if (values[item.nodeId] &&
|
|
5599
|
+
Object.keys(values[item.nodeId]).length === 0) {
|
|
5600
|
+
delete values[item.nodeId];
|
|
5601
|
+
}
|
|
5602
|
+
}
|
|
5603
|
+
else {
|
|
5604
|
+
if (!values[item.nodeId]) {
|
|
5605
|
+
values[item.nodeId] = {};
|
|
5606
|
+
}
|
|
5607
|
+
values[item.nodeId][item.handleId] = convertedValue;
|
|
5608
|
+
}
|
|
5609
|
+
}
|
|
5610
|
+
}
|
|
5611
|
+
function collectValuesToConvert(values, nodeTypeMap, handleTypeMap, originalNodeIdMap) {
|
|
5612
|
+
const converted = {};
|
|
5613
|
+
const toConvert = [];
|
|
5614
|
+
for (const [nodeId, nodeValues] of Object.entries(values)) {
|
|
5615
|
+
converted[nodeId] = { ...nodeValues };
|
|
5616
|
+
const originalNodeId = nodeId;
|
|
5617
|
+
const nodeTypeId = nodeTypeMap.get(originalNodeId);
|
|
5618
|
+
for (const [handleId, value] of Object.entries(nodeValues)) {
|
|
5619
|
+
const handleDataType = handleTypeMap.get(originalNodeId)?.get(handleId);
|
|
5620
|
+
const runtimeTypeId = isTypedOutput(value)
|
|
5621
|
+
? getTypedOutputTypeId(value)
|
|
5622
|
+
: undefined;
|
|
5623
|
+
toConvert.push({
|
|
5624
|
+
nodeId,
|
|
5625
|
+
originalNodeId,
|
|
5626
|
+
handleId,
|
|
5627
|
+
value,
|
|
5628
|
+
nodeTypeId,
|
|
5629
|
+
handleDataType,
|
|
5630
|
+
runtimeTypeId,
|
|
5631
|
+
});
|
|
5632
|
+
}
|
|
5633
|
+
}
|
|
5634
|
+
return { converted, toConvert };
|
|
5635
|
+
}
|
|
5636
|
+
function convertSnapshot(snapshot, converter) {
|
|
5637
|
+
const def = snapshot.def ?? { nodes: [], edges: [] };
|
|
5638
|
+
const { nodeTypeMap, handleTypeMap } = buildTypeMaps(def);
|
|
5639
|
+
const inputDefaults = {};
|
|
5640
|
+
for (const node of def.nodes) {
|
|
5641
|
+
if (node.resolvedHandles?.inputDefaults) {
|
|
5642
|
+
inputDefaults[node.nodeId] = { ...node.resolvedHandles.inputDefaults };
|
|
5643
|
+
}
|
|
5644
|
+
}
|
|
5645
|
+
const { converted: convertedInputs, toConvert: inputsToConvert } = collectValuesToConvert(snapshot.inputs ?? {}, nodeTypeMap, handleTypeMap);
|
|
5646
|
+
const { converted: convertedOutputs, toConvert: outputsToConvert } = collectValuesToConvert(snapshot.outputs ?? {}, nodeTypeMap, handleTypeMap);
|
|
5647
|
+
const { converted: convertedInputDefaults, toConvert: inputDefaultsToConvert, } = collectValuesToConvert(inputDefaults, nodeTypeMap, handleTypeMap);
|
|
5648
|
+
applyConversion(inputsToConvert, convertedInputs, converter, "input");
|
|
5649
|
+
applyConversion(outputsToConvert, convertedOutputs, converter, "output");
|
|
5650
|
+
applyConversion(inputDefaultsToConvert, convertedInputDefaults, converter, "inputDefault");
|
|
5651
|
+
const updatedNodes = def.nodes.map((node) => {
|
|
5652
|
+
const defaults = convertedInputDefaults[node.nodeId];
|
|
5653
|
+
if (!defaults || Object.keys(defaults).length === 0) {
|
|
5654
|
+
if (node.resolvedHandles?.inputDefaults) {
|
|
5655
|
+
const { inputDefaults, ...restHandles } = node.resolvedHandles;
|
|
5656
|
+
return {
|
|
5657
|
+
...node,
|
|
5658
|
+
resolvedHandles: Object.keys(restHandles).length > 0 ? restHandles : undefined,
|
|
5659
|
+
};
|
|
5660
|
+
}
|
|
5661
|
+
return node;
|
|
5662
|
+
}
|
|
5663
|
+
return {
|
|
5664
|
+
...node,
|
|
5665
|
+
resolvedHandles: {
|
|
5666
|
+
...node.resolvedHandles,
|
|
5667
|
+
inputDefaults: defaults,
|
|
5668
|
+
},
|
|
5669
|
+
};
|
|
5670
|
+
});
|
|
5671
|
+
return {
|
|
5672
|
+
...snapshot,
|
|
5673
|
+
def: { ...def, nodes: updatedNodes },
|
|
5674
|
+
inputs: convertedInputs,
|
|
5675
|
+
outputs: convertedOutputs,
|
|
5676
|
+
};
|
|
5677
|
+
}
|
|
5538
5678
|
function mergeInputsOutputs(targetInputs, targetOutputs, sourceInputs, sourceOutputs, targetInputDefaults, sourceInputDefaults, nodeIdMap, nodeTypeMap, handleTypeMap) {
|
|
5539
|
-
const mergedInputs = {};
|
|
5540
|
-
const mergedOutputs = {};
|
|
5541
|
-
const mergedInputDefaults = {};
|
|
5679
|
+
const mergedInputs = { ...targetInputs };
|
|
5680
|
+
const mergedOutputs = { ...targetOutputs };
|
|
5681
|
+
const mergedInputDefaults = { ...targetInputDefaults };
|
|
5542
5682
|
const inputsToConvert = [];
|
|
5543
5683
|
const outputsToConvert = [];
|
|
5544
5684
|
const inputDefaultsToConvert = [];
|
|
5545
|
-
for (const [nodeId, inputs] of Object.entries(targetInputs)) {
|
|
5546
|
-
mergedInputs[nodeId] = { ...inputs };
|
|
5547
|
-
}
|
|
5548
|
-
for (const [nodeId, outputs] of Object.entries(targetOutputs)) {
|
|
5549
|
-
mergedOutputs[nodeId] = { ...outputs };
|
|
5550
|
-
}
|
|
5551
|
-
for (const [nodeId, defaults] of Object.entries(targetInputDefaults)) {
|
|
5552
|
-
mergedInputDefaults[nodeId] = { ...defaults };
|
|
5553
|
-
}
|
|
5554
5685
|
for (const [oldId, inputs] of Object.entries(sourceInputs)) {
|
|
5555
5686
|
const newId = nodeIdMap[oldId];
|
|
5556
5687
|
if (newId) {
|
|
@@ -5629,35 +5760,7 @@ function mergeInputsOutputs(targetInputs, targetOutputs, sourceInputs, sourceOut
|
|
|
5629
5760
|
}
|
|
5630
5761
|
function mergeSnapshotData(targetSnapshot, sourceSnapshot, targetInputDefaults, sourceInputDefaults, nodeIdMap) {
|
|
5631
5762
|
const sourceDef = sourceSnapshot.def ?? { nodes: []};
|
|
5632
|
-
const nodeTypeMap =
|
|
5633
|
-
const handleTypeMap = new Map();
|
|
5634
|
-
for (const node of sourceDef.nodes) {
|
|
5635
|
-
nodeTypeMap.set(node.nodeId, node.typeId);
|
|
5636
|
-
const nodeHandleTypes = new Map();
|
|
5637
|
-
if (node.resolvedHandles?.inputs) {
|
|
5638
|
-
for (const [handleId, handleDesc] of Object.entries(node.resolvedHandles.inputs)) {
|
|
5639
|
-
const typeId = typeof handleDesc === "string"
|
|
5640
|
-
? handleDesc
|
|
5641
|
-
: handleDesc?.typeId;
|
|
5642
|
-
if (typeId)
|
|
5643
|
-
nodeHandleTypes.set(handleId, typeId);
|
|
5644
|
-
}
|
|
5645
|
-
}
|
|
5646
|
-
if (node.resolvedHandles?.outputs) {
|
|
5647
|
-
for (const [handleId, handleDesc] of Object.entries(node.resolvedHandles.outputs)) {
|
|
5648
|
-
const typeId = typeof handleDesc === "string"
|
|
5649
|
-
? handleDesc
|
|
5650
|
-
: Array.isArray(handleDesc)
|
|
5651
|
-
? handleDesc[0]
|
|
5652
|
-
: undefined;
|
|
5653
|
-
if (typeId)
|
|
5654
|
-
nodeHandleTypes.set(handleId, typeId);
|
|
5655
|
-
}
|
|
5656
|
-
}
|
|
5657
|
-
if (nodeHandleTypes.size > 0) {
|
|
5658
|
-
handleTypeMap.set(node.nodeId, nodeHandleTypes);
|
|
5659
|
-
}
|
|
5660
|
-
}
|
|
5763
|
+
const { nodeTypeMap, handleTypeMap } = buildTypeMaps(sourceDef);
|
|
5661
5764
|
return mergeInputsOutputs(targetSnapshot.inputs ?? {}, targetSnapshot.outputs ?? {}, sourceSnapshot.inputs ?? {}, sourceSnapshot.outputs ?? {}, targetInputDefaults, sourceInputDefaults, nodeIdMap, nodeTypeMap, handleTypeMap);
|
|
5662
5765
|
}
|
|
5663
5766
|
function mergeSnapshots(targetSnapshot, sourceSnapshot, converter) {
|
|
@@ -5666,35 +5769,9 @@ function mergeSnapshots(targetSnapshot, sourceSnapshot, converter) {
|
|
|
5666
5769
|
const { merged, nodeIdMap, targetInputDefaults, sourceInputDefaults } = mergeGraphDefinitions(targetDef, sourceDef);
|
|
5667
5770
|
const { mergedInputs, mergedOutputs, mergedInputDefaults, inputsToConvert, outputsToConvert, inputDefaultsToConvert, } = mergeSnapshotData(targetSnapshot, sourceSnapshot, targetInputDefaults, sourceInputDefaults, nodeIdMap);
|
|
5668
5771
|
if (converter) {
|
|
5669
|
-
|
|
5670
|
-
|
|
5671
|
-
|
|
5672
|
-
nodeId: item.nodeId,
|
|
5673
|
-
handleId: item.handleId,
|
|
5674
|
-
value: item.value,
|
|
5675
|
-
type,
|
|
5676
|
-
nodeTypeId: item.nodeTypeId,
|
|
5677
|
-
handleDataType: item.handleDataType,
|
|
5678
|
-
runtimeTypeId: item.runtimeTypeId,
|
|
5679
|
-
});
|
|
5680
|
-
if (convertedValue === null) {
|
|
5681
|
-
delete values[item.nodeId]?.[item.handleId];
|
|
5682
|
-
if (values[item.nodeId] &&
|
|
5683
|
-
Object.keys(values[item.nodeId]).length === 0) {
|
|
5684
|
-
delete values[item.nodeId];
|
|
5685
|
-
}
|
|
5686
|
-
}
|
|
5687
|
-
else {
|
|
5688
|
-
if (!values[item.nodeId]) {
|
|
5689
|
-
values[item.nodeId] = {};
|
|
5690
|
-
}
|
|
5691
|
-
values[item.nodeId][item.handleId] = convertedValue;
|
|
5692
|
-
}
|
|
5693
|
-
}
|
|
5694
|
-
};
|
|
5695
|
-
applyConversion(inputDefaultsToConvert, mergedInputDefaults, "inputDefault");
|
|
5696
|
-
applyConversion(inputsToConvert, mergedInputs, "input");
|
|
5697
|
-
applyConversion(outputsToConvert, mergedOutputs, "output");
|
|
5772
|
+
applyConversion(inputDefaultsToConvert, mergedInputDefaults, converter, "inputDefault");
|
|
5773
|
+
applyConversion(inputsToConvert, mergedInputs, converter, "input");
|
|
5774
|
+
applyConversion(outputsToConvert, mergedOutputs, converter, "output");
|
|
5698
5775
|
}
|
|
5699
5776
|
const nodeMap = new Map(merged.nodes.map((n) => [n.nodeId, n]));
|
|
5700
5777
|
for (const nodeId in mergedInputDefaults) {
|
|
@@ -5762,6 +5839,8 @@ function buildValueConverter(config) {
|
|
|
5762
5839
|
const { nodeId, handleId, value, type, nodeTypeId, handleDataType, runtimeTypeId, } = converterConfig;
|
|
5763
5840
|
const isTyped = isTypedOutput(value);
|
|
5764
5841
|
for (const mapping of config.mappings) {
|
|
5842
|
+
if (mapping.type !== type)
|
|
5843
|
+
continue;
|
|
5765
5844
|
if (!matchesPattern(handleId, mapping.handleId))
|
|
5766
5845
|
continue;
|
|
5767
5846
|
if (!matchesPattern(nodeId, mapping.nodeId))
|
|
@@ -5802,8 +5881,9 @@ function buildValueConverter(config) {
|
|
|
5802
5881
|
else {
|
|
5803
5882
|
if (typeof matchValue === "string" ||
|
|
5804
5883
|
typeof matchValue === "number") {
|
|
5805
|
-
|
|
5806
|
-
|
|
5884
|
+
const key = String(matchValue);
|
|
5885
|
+
if (key in mapping.valueMap) {
|
|
5886
|
+
newValue = mapping.valueMap[key];
|
|
5807
5887
|
changed = true;
|
|
5808
5888
|
}
|
|
5809
5889
|
}
|
|
@@ -5836,8 +5916,9 @@ function buildValueConverter(config) {
|
|
|
5836
5916
|
else {
|
|
5837
5917
|
if (typeof innerValue === "string" ||
|
|
5838
5918
|
typeof innerValue === "number") {
|
|
5839
|
-
|
|
5840
|
-
|
|
5919
|
+
const key = String(innerValue);
|
|
5920
|
+
if (key in mapping.valueMap) {
|
|
5921
|
+
newValue = mapping.valueMap[key];
|
|
5841
5922
|
// If mapped to null, return null to delete the output
|
|
5842
5923
|
if (newValue === null) {
|
|
5843
5924
|
return null;
|
|
@@ -5884,8 +5965,9 @@ function buildValueConverter(config) {
|
|
|
5884
5965
|
else {
|
|
5885
5966
|
if (typeof matchValue === "string" ||
|
|
5886
5967
|
typeof matchValue === "number") {
|
|
5887
|
-
|
|
5888
|
-
|
|
5968
|
+
const key = String(matchValue);
|
|
5969
|
+
if (key in mapping.valueMap) {
|
|
5970
|
+
newValue = mapping.valueMap[key];
|
|
5889
5971
|
changed = true;
|
|
5890
5972
|
}
|
|
5891
5973
|
}
|
|
@@ -5907,8 +5989,9 @@ function buildValueConverter(config) {
|
|
|
5907
5989
|
}
|
|
5908
5990
|
else {
|
|
5909
5991
|
if (typeof value === "string" || typeof value === "number") {
|
|
5910
|
-
|
|
5911
|
-
|
|
5992
|
+
const key = String(value);
|
|
5993
|
+
if (key in mapping.valueMap) {
|
|
5994
|
+
return mapping.valueMap[key];
|
|
5912
5995
|
}
|
|
5913
5996
|
}
|
|
5914
5997
|
}
|
|
@@ -5931,6 +6014,7 @@ exports.LocalEngine = LocalEngine;
|
|
|
5931
6014
|
exports.Registry = Registry;
|
|
5932
6015
|
exports.buildValueConverter = buildValueConverter;
|
|
5933
6016
|
exports.computeGraphCenter = computeGraphCenter;
|
|
6017
|
+
exports.convertSnapshot = convertSnapshot;
|
|
5934
6018
|
exports.createAsyncGraphDef = createAsyncGraphDef;
|
|
5935
6019
|
exports.createAsyncGraphRegistry = createAsyncGraphRegistry;
|
|
5936
6020
|
exports.createProgressGraphDef = createProgressGraphDef;
|