@bian-womp/spark-graph 0.3.2 → 0.3.4
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 +1380 -1106
- package/lib/cjs/index.cjs.map +1 -1
- package/lib/cjs/src/core/types.d.ts +4 -1
- package/lib/cjs/src/core/types.d.ts.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 +9 -0
- package/lib/cjs/src/misc/utils/json.d.ts.map +1 -1
- package/lib/cjs/src/runtime/GraphLifecycleApi.d.ts +4 -1
- package/lib/cjs/src/runtime/GraphLifecycleApi.d.ts.map +1 -1
- package/lib/cjs/src/runtime/GraphRuntime.d.ts +14 -15
- package/lib/cjs/src/runtime/GraphRuntime.d.ts.map +1 -1
- package/lib/cjs/src/runtime/LocalEngine.d.ts +2 -2
- package/lib/cjs/src/runtime/components/EdgePropagator.d.ts +101 -0
- package/lib/cjs/src/runtime/components/EdgePropagator.d.ts.map +1 -0
- package/lib/cjs/src/runtime/components/Graph.d.ts +31 -0
- package/lib/cjs/src/runtime/components/Graph.d.ts.map +1 -0
- package/lib/cjs/src/runtime/components/HandleResolver.d.ts +11 -8
- package/lib/cjs/src/runtime/components/HandleResolver.d.ts.map +1 -1
- package/lib/cjs/src/runtime/components/NodeExecutor.d.ts +108 -0
- package/lib/cjs/src/runtime/components/NodeExecutor.d.ts.map +1 -0
- package/lib/cjs/src/runtime/components/RunContextManager.d.ts +26 -13
- package/lib/cjs/src/runtime/components/RunContextManager.d.ts.map +1 -1
- package/lib/cjs/src/runtime/components/graph-utils.d.ts +22 -0
- package/lib/cjs/src/runtime/components/graph-utils.d.ts.map +1 -0
- package/lib/cjs/src/runtime/components/interfaces.d.ts +9 -13
- package/lib/cjs/src/runtime/components/interfaces.d.ts.map +1 -1
- package/lib/cjs/src/runtime/components/types.d.ts +1 -10
- package/lib/cjs/src/runtime/components/types.d.ts.map +1 -1
- package/lib/esm/index.js +1380 -1107
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/src/core/types.d.ts +4 -1
- package/lib/esm/src/core/types.d.ts.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 +9 -0
- package/lib/esm/src/misc/utils/json.d.ts.map +1 -1
- package/lib/esm/src/runtime/GraphLifecycleApi.d.ts +4 -1
- package/lib/esm/src/runtime/GraphLifecycleApi.d.ts.map +1 -1
- package/lib/esm/src/runtime/GraphRuntime.d.ts +14 -15
- package/lib/esm/src/runtime/GraphRuntime.d.ts.map +1 -1
- package/lib/esm/src/runtime/LocalEngine.d.ts +2 -2
- package/lib/esm/src/runtime/components/EdgePropagator.d.ts +101 -0
- package/lib/esm/src/runtime/components/EdgePropagator.d.ts.map +1 -0
- package/lib/esm/src/runtime/components/Graph.d.ts +31 -0
- package/lib/esm/src/runtime/components/Graph.d.ts.map +1 -0
- package/lib/esm/src/runtime/components/HandleResolver.d.ts +11 -8
- package/lib/esm/src/runtime/components/HandleResolver.d.ts.map +1 -1
- package/lib/esm/src/runtime/components/NodeExecutor.d.ts +108 -0
- package/lib/esm/src/runtime/components/NodeExecutor.d.ts.map +1 -0
- package/lib/esm/src/runtime/components/RunContextManager.d.ts +26 -13
- package/lib/esm/src/runtime/components/RunContextManager.d.ts.map +1 -1
- package/lib/esm/src/runtime/components/graph-utils.d.ts +22 -0
- package/lib/esm/src/runtime/components/graph-utils.d.ts.map +1 -0
- package/lib/esm/src/runtime/components/interfaces.d.ts +9 -13
- package/lib/esm/src/runtime/components/interfaces.d.ts.map +1 -1
- package/lib/esm/src/runtime/components/types.d.ts +1 -10
- package/lib/esm/src/runtime/components/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/lib/cjs/src/runtime/components/ExecutionScheduler.d.ts +0 -56
- package/lib/cjs/src/runtime/components/ExecutionScheduler.d.ts.map +0 -1
- package/lib/cjs/src/runtime/components/GraphStructure.d.ts +0 -36
- package/lib/cjs/src/runtime/components/GraphStructure.d.ts.map +0 -1
- package/lib/cjs/src/runtime/components/ValuePropagator.d.ts +0 -46
- package/lib/cjs/src/runtime/components/ValuePropagator.d.ts.map +0 -1
- package/lib/esm/src/runtime/components/ExecutionScheduler.d.ts +0 -56
- package/lib/esm/src/runtime/components/ExecutionScheduler.d.ts.map +0 -1
- package/lib/esm/src/runtime/components/GraphStructure.d.ts +0 -36
- package/lib/esm/src/runtime/components/GraphStructure.d.ts.map +0 -1
- package/lib/esm/src/runtime/components/ValuePropagator.d.ts +0 -46
- package/lib/esm/src/runtime/components/ValuePropagator.d.ts.map +0 -1
package/lib/esm/index.js
CHANGED
|
@@ -403,48 +403,14 @@ class Registry {
|
|
|
403
403
|
Registry.idCounter = 0;
|
|
404
404
|
|
|
405
405
|
/**
|
|
406
|
-
*
|
|
407
|
-
*/
|
|
408
|
-
/**
|
|
409
|
-
* Type guard to check if a value is a Promise
|
|
410
|
-
*/
|
|
411
|
-
function isPromise(value) {
|
|
412
|
-
return !!value && typeof value.then === "function";
|
|
413
|
-
}
|
|
414
|
-
/**
|
|
415
|
-
* Unwrap a value that might be a Promise
|
|
416
|
-
*/
|
|
417
|
-
async function unwrapMaybePromise(value) {
|
|
418
|
-
return isPromise(value) ? await value : value;
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Shallow/deep-ish equality check to avoid unnecessary runs on identical values
|
|
422
|
-
*/
|
|
423
|
-
function valuesEqual(a, b) {
|
|
424
|
-
if (a === b)
|
|
425
|
-
return true;
|
|
426
|
-
if (typeof a !== typeof b)
|
|
427
|
-
return false;
|
|
428
|
-
if (a && b && typeof a === "object") {
|
|
429
|
-
try {
|
|
430
|
-
return JSON.stringify(a) === JSON.stringify(b);
|
|
431
|
-
}
|
|
432
|
-
catch {
|
|
433
|
-
return false;
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
return false;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
/**
|
|
440
|
-
* GraphStructure component - manages nodes, edges, and handle resolution
|
|
406
|
+
* Graph component - manages nodes, edges, and handle resolution
|
|
441
407
|
*/
|
|
442
|
-
class
|
|
408
|
+
class Graph {
|
|
443
409
|
constructor(registry) {
|
|
410
|
+
this.registry = registry;
|
|
444
411
|
this.nodes = new Map();
|
|
445
412
|
this.edges = [];
|
|
446
413
|
this.resolvedByNode = new Map();
|
|
447
|
-
this.registry = registry;
|
|
448
414
|
}
|
|
449
415
|
// Node accessors
|
|
450
416
|
getNode(nodeId) {
|
|
@@ -486,99 +452,21 @@ class GraphStructure {
|
|
|
486
452
|
getResolvedHandlesMap() {
|
|
487
453
|
return this.resolvedByNode;
|
|
488
454
|
}
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
try {
|
|
503
|
-
if (typeof desc.resolveHandles === "function") {
|
|
504
|
-
const maybe = desc.resolveHandles({
|
|
505
|
-
nodeId: n.nodeId,
|
|
506
|
-
environment: environment || {},
|
|
507
|
-
params: n.params,
|
|
508
|
-
inputs: undefined,
|
|
509
|
-
});
|
|
510
|
-
// Only use sync results here; async results are applied via recompute later
|
|
511
|
-
if (isPromise(maybe)) {
|
|
512
|
-
// mark node as pending async recompute
|
|
513
|
-
pending.add(n.nodeId);
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
dyn = maybe || {};
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
}
|
|
520
|
-
catch {
|
|
521
|
-
// ignore dynamic resolution errors at this stage
|
|
522
|
-
}
|
|
523
|
-
// Merge base with dynamic and overrides (allow partial resolvedHandles)
|
|
524
|
-
const inputs = {
|
|
525
|
-
...desc.inputs,
|
|
526
|
-
...dyn.inputs,
|
|
527
|
-
...overrideInputs,
|
|
528
|
-
};
|
|
529
|
-
const outputs = {
|
|
530
|
-
...desc.outputs,
|
|
531
|
-
...dyn.outputs,
|
|
532
|
-
...overrideOutputs,
|
|
533
|
-
};
|
|
534
|
-
const inputDefaults = {
|
|
535
|
-
...desc.inputDefaults,
|
|
536
|
-
...dyn.inputDefaults,
|
|
537
|
-
...overrideDefaults,
|
|
538
|
-
};
|
|
539
|
-
out.set(n.nodeId, { inputs, outputs, inputDefaults });
|
|
455
|
+
/**
|
|
456
|
+
* Check if all inbound edges for a node have values
|
|
457
|
+
*/
|
|
458
|
+
allInboundHaveValue(nodeId) {
|
|
459
|
+
const node = this.nodes.get(nodeId);
|
|
460
|
+
if (!node)
|
|
461
|
+
return false;
|
|
462
|
+
const inbound = this.edges.filter((e) => e.target.nodeId === nodeId);
|
|
463
|
+
if (inbound.length === 0)
|
|
464
|
+
return true;
|
|
465
|
+
for (const e of inbound) {
|
|
466
|
+
if (!(e.target.handle in node.inputs))
|
|
467
|
+
return false;
|
|
540
468
|
}
|
|
541
|
-
return
|
|
542
|
-
}
|
|
543
|
-
static buildEdges(def, registry, resolvedByNode) {
|
|
544
|
-
return def.edges.map((e) => {
|
|
545
|
-
const srcNode = def.nodes.find((n) => n.nodeId === e.source.nodeId);
|
|
546
|
-
const dstNode = def.nodes.find((n) => n.nodeId === e.target.nodeId);
|
|
547
|
-
let effectiveTypeId = e.typeId; // Start with original
|
|
548
|
-
let srcDeclared;
|
|
549
|
-
let dstDeclared;
|
|
550
|
-
if (srcNode) {
|
|
551
|
-
const resolved = resolvedByNode.get(srcNode.nodeId);
|
|
552
|
-
if (resolved)
|
|
553
|
-
srcDeclared = resolved.outputs[e.source.handle];
|
|
554
|
-
}
|
|
555
|
-
if (!effectiveTypeId) {
|
|
556
|
-
// Infer if not explicitly set
|
|
557
|
-
effectiveTypeId = Array.isArray(srcDeclared)
|
|
558
|
-
? srcDeclared[0]
|
|
559
|
-
: srcDeclared;
|
|
560
|
-
}
|
|
561
|
-
if (dstNode) {
|
|
562
|
-
const resolved = resolvedByNode.get(dstNode.nodeId);
|
|
563
|
-
if (resolved)
|
|
564
|
-
dstDeclared = getInputTypeId(resolved.inputs, e.target.handle);
|
|
565
|
-
}
|
|
566
|
-
const { convert, convertAsync } = GraphStructure.buildEdgeConverters(srcDeclared, dstDeclared, registry, `buildEdges: ${srcNode?.typeId || ""}.${e.source.nodeId}.${e.source.handle} -> ${dstNode?.typeId || ""}.${e.target.nodeId}.${e.target.handle}`);
|
|
567
|
-
return {
|
|
568
|
-
id: e.id,
|
|
569
|
-
source: { ...e.source },
|
|
570
|
-
target: { ...e.target },
|
|
571
|
-
typeId: e.typeId, // Preserve original (may be undefined)
|
|
572
|
-
effectiveTypeId: effectiveTypeId ?? "untyped", // Always present
|
|
573
|
-
convert,
|
|
574
|
-
convertAsync,
|
|
575
|
-
srcUnionTypes: Array.isArray(srcDeclared)
|
|
576
|
-
? [...srcDeclared]
|
|
577
|
-
: undefined,
|
|
578
|
-
dstDeclared,
|
|
579
|
-
stats: { runs: 0, inFlight: false, progress: 0 },
|
|
580
|
-
};
|
|
581
|
-
});
|
|
469
|
+
return true;
|
|
582
470
|
}
|
|
583
471
|
// Clear all data
|
|
584
472
|
clear() {
|
|
@@ -586,70 +474,6 @@ class GraphStructure {
|
|
|
586
474
|
this.edges = [];
|
|
587
475
|
this.resolvedByNode.clear();
|
|
588
476
|
}
|
|
589
|
-
// Static helper: build edge converters for type coercion
|
|
590
|
-
static buildEdgeConverters(srcDeclared, dstDeclared, registry, edgeLabel) {
|
|
591
|
-
if (!dstDeclared || !srcDeclared) {
|
|
592
|
-
return {};
|
|
593
|
-
}
|
|
594
|
-
const isUnion = Array.isArray(srcDeclared);
|
|
595
|
-
const srcTypes = isUnion ? srcDeclared : [srcDeclared];
|
|
596
|
-
// Helper to get the coercion for a specific type
|
|
597
|
-
const getCoercion = (typeId) => {
|
|
598
|
-
return registry.resolveCoercion(typeId, dstDeclared);
|
|
599
|
-
};
|
|
600
|
-
// Resolve coercions for all source types
|
|
601
|
-
const coercions = srcTypes.map(getCoercion);
|
|
602
|
-
const hasAsync = coercions.some((r) => r?.kind === "async");
|
|
603
|
-
// Helper to extract and validate typed output for unions
|
|
604
|
-
const extractPayload = (v) => {
|
|
605
|
-
const typeId = getTypedOutputTypeId(v);
|
|
606
|
-
const payload = getTypedOutputValue(v);
|
|
607
|
-
if (isUnion) {
|
|
608
|
-
if (!typeId) {
|
|
609
|
-
throw new Error(`Typed output required for union source (${edgeLabel}); allowed: ${srcTypes.join("|")}`);
|
|
610
|
-
}
|
|
611
|
-
if (!srcTypes.includes(typeId)) {
|
|
612
|
-
throw new Error(`Invalid typed output ${typeId} (${edgeLabel}); allowed: ${srcTypes.join("|")}`);
|
|
613
|
-
}
|
|
614
|
-
}
|
|
615
|
-
else if (typeId) {
|
|
616
|
-
// Warn if typed output is used for non-union source
|
|
617
|
-
console.warn(`Typed output ${typeId} is fed even though source is not union (${edgeLabel}): ${srcDeclared} -> ${dstDeclared}`);
|
|
618
|
-
}
|
|
619
|
-
return { typeId: typeId || srcTypes[0], payload };
|
|
620
|
-
};
|
|
621
|
-
if (hasAsync) {
|
|
622
|
-
return {
|
|
623
|
-
convertAsync: async (v, signal) => {
|
|
624
|
-
const { typeId, payload } = extractPayload(v);
|
|
625
|
-
const res = getCoercion(typeId);
|
|
626
|
-
if (!res)
|
|
627
|
-
return payload;
|
|
628
|
-
if (res.kind === "async") {
|
|
629
|
-
return await res.convertAsync(payload, signal);
|
|
630
|
-
}
|
|
631
|
-
return res.convert(payload);
|
|
632
|
-
},
|
|
633
|
-
};
|
|
634
|
-
}
|
|
635
|
-
// Sync path
|
|
636
|
-
const firstCoercion = coercions.find((r) => r?.kind === "sync");
|
|
637
|
-
if (!firstCoercion) {
|
|
638
|
-
return {};
|
|
639
|
-
}
|
|
640
|
-
return {
|
|
641
|
-
convert: (v) => {
|
|
642
|
-
const { typeId, payload } = extractPayload(v);
|
|
643
|
-
const res = getCoercion(typeId);
|
|
644
|
-
if (!res)
|
|
645
|
-
return payload;
|
|
646
|
-
if (res.kind === "async") {
|
|
647
|
-
throw new Error(`Async coercion required but convert used (${edgeLabel})`);
|
|
648
|
-
}
|
|
649
|
-
return res.convert(payload);
|
|
650
|
-
},
|
|
651
|
-
};
|
|
652
|
-
}
|
|
653
477
|
}
|
|
654
478
|
|
|
655
479
|
/**
|
|
@@ -689,25 +513,30 @@ class EventEmitter {
|
|
|
689
513
|
* RunContextManager component - manages run-context lifecycle
|
|
690
514
|
*/
|
|
691
515
|
class RunContextManager {
|
|
692
|
-
constructor() {
|
|
516
|
+
constructor(graph) {
|
|
517
|
+
this.graph = graph;
|
|
693
518
|
this.runContexts = new Map();
|
|
694
519
|
this.runContextCounter = 0;
|
|
520
|
+
this.graph = graph;
|
|
695
521
|
}
|
|
696
522
|
/**
|
|
697
523
|
* Create a new run-context for runFromHere
|
|
698
524
|
*/
|
|
699
|
-
createRunContext(startNodeId, options) {
|
|
525
|
+
createRunContext(startNodeId, resolve, options) {
|
|
700
526
|
const id = `rc-${++this.runContextCounter}`;
|
|
701
527
|
const ctx = {
|
|
702
528
|
id,
|
|
703
529
|
startNodes: new Set([startNodeId]),
|
|
704
530
|
cancelledNodes: new Set(),
|
|
705
|
-
|
|
531
|
+
pendingNodes: 0,
|
|
532
|
+
pendingEdges: 0,
|
|
533
|
+
pendingResolvers: 0,
|
|
706
534
|
skipPropagateValues: options?.skipPropagateValues ?? false,
|
|
707
535
|
propagate: options?.propagate ?? true,
|
|
536
|
+
resolve,
|
|
708
537
|
};
|
|
709
538
|
this.runContexts.set(id, ctx);
|
|
710
|
-
return
|
|
539
|
+
return id;
|
|
711
540
|
}
|
|
712
541
|
/**
|
|
713
542
|
* Get a run-context by ID
|
|
@@ -727,18 +556,60 @@ class RunContextManager {
|
|
|
727
556
|
hasActiveRunContexts() {
|
|
728
557
|
return this.runContexts.size > 0;
|
|
729
558
|
}
|
|
559
|
+
startNodeRun(id, nodeId) {
|
|
560
|
+
const ctx = this.runContexts.get(id);
|
|
561
|
+
if (!ctx)
|
|
562
|
+
return;
|
|
563
|
+
ctx.pendingNodes++;
|
|
564
|
+
}
|
|
565
|
+
finishNodeRun(id, nodeId) {
|
|
566
|
+
const ctx = this.runContexts.get(id);
|
|
567
|
+
if (!ctx)
|
|
568
|
+
return;
|
|
569
|
+
ctx.pendingNodes--;
|
|
570
|
+
this.finishRunContextIfPossible(id);
|
|
571
|
+
}
|
|
572
|
+
startEdgeConversion(id, edgeId) {
|
|
573
|
+
const ctx = this.runContexts.get(id);
|
|
574
|
+
if (!ctx)
|
|
575
|
+
return;
|
|
576
|
+
ctx.pendingEdges++;
|
|
577
|
+
}
|
|
578
|
+
finishEdgeConversion(id, edgeId) {
|
|
579
|
+
const ctx = this.runContexts.get(id);
|
|
580
|
+
if (!ctx)
|
|
581
|
+
return;
|
|
582
|
+
ctx.pendingEdges--;
|
|
583
|
+
this.finishRunContextIfPossible(id);
|
|
584
|
+
}
|
|
585
|
+
startHandleResolution(id, nodeId) {
|
|
586
|
+
const ctx = this.runContexts.get(id);
|
|
587
|
+
if (!ctx)
|
|
588
|
+
return;
|
|
589
|
+
ctx.pendingResolvers++;
|
|
590
|
+
}
|
|
591
|
+
finishHandleResolution(id, nodeId) {
|
|
592
|
+
const ctx = this.runContexts.get(id);
|
|
593
|
+
if (!ctx)
|
|
594
|
+
return;
|
|
595
|
+
ctx.pendingResolvers--;
|
|
596
|
+
this.finishRunContextIfPossible(id);
|
|
597
|
+
}
|
|
730
598
|
/**
|
|
731
|
-
* Finish and remove a run-context when
|
|
599
|
+
* Finish and remove a run-context when all pending operations reach zero
|
|
732
600
|
*/
|
|
733
|
-
|
|
601
|
+
finishRunContextIfPossible(id) {
|
|
734
602
|
const ctx = this.runContexts.get(id);
|
|
735
603
|
if (!ctx)
|
|
736
604
|
return;
|
|
737
|
-
if (ctx.
|
|
605
|
+
if (ctx.pendingNodes > 0 ||
|
|
606
|
+
ctx.pendingEdges > 0 ||
|
|
607
|
+
ctx.pendingResolvers > 0) {
|
|
738
608
|
return; // Still has pending work
|
|
609
|
+
}
|
|
739
610
|
// Clean up activeRunContexts from all nodes
|
|
740
|
-
for (const node of
|
|
741
|
-
node.
|
|
611
|
+
for (const node of this.graph.getNodes().values()) {
|
|
612
|
+
node.activeRunContextIds.delete(id);
|
|
742
613
|
}
|
|
743
614
|
this.runContexts.delete(id);
|
|
744
615
|
if (ctx.resolve)
|
|
@@ -751,7 +622,7 @@ class RunContextManager {
|
|
|
751
622
|
* @param edges - All edges in the graph (for downstream traversal)
|
|
752
623
|
* @param nodes - All nodes in the graph (for clearing activeRunContexts)
|
|
753
624
|
*/
|
|
754
|
-
cancelNodeInRunContexts(nodeId, includeDownstream
|
|
625
|
+
cancelNodeInRunContexts(nodeId, includeDownstream) {
|
|
755
626
|
const toCancel = new Set([nodeId]);
|
|
756
627
|
if (includeDownstream) {
|
|
757
628
|
// Collect all downstream nodes
|
|
@@ -762,7 +633,7 @@ class RunContextManager {
|
|
|
762
633
|
if (visited.has(cur))
|
|
763
634
|
continue;
|
|
764
635
|
visited.add(cur);
|
|
765
|
-
for (const e of
|
|
636
|
+
for (const e of this.graph.getEdges()) {
|
|
766
637
|
if (e.source.nodeId === cur) {
|
|
767
638
|
const targetId = e.target.nodeId;
|
|
768
639
|
if (!visited.has(targetId)) {
|
|
@@ -781,9 +652,9 @@ class RunContextManager {
|
|
|
781
652
|
}
|
|
782
653
|
// Clear activeRunContexts for cancelled nodes
|
|
783
654
|
for (const id of toCancel) {
|
|
784
|
-
const node =
|
|
655
|
+
const node = this.graph.getNode(id);
|
|
785
656
|
if (node)
|
|
786
|
-
node.
|
|
657
|
+
node.activeRunContextIds.clear();
|
|
787
658
|
}
|
|
788
659
|
}
|
|
789
660
|
/**
|
|
@@ -811,17 +682,214 @@ const LOG_LEVEL_VALUES = {
|
|
|
811
682
|
silent: 4,
|
|
812
683
|
};
|
|
813
684
|
|
|
685
|
+
/**
|
|
686
|
+
* Shared utility functions for runtime components
|
|
687
|
+
*/
|
|
688
|
+
/**
|
|
689
|
+
* Type guard to check if a value is a Promise
|
|
690
|
+
*/
|
|
691
|
+
function isPromise(value) {
|
|
692
|
+
return !!value && typeof value.then === "function";
|
|
693
|
+
}
|
|
694
|
+
/**
|
|
695
|
+
* Unwrap a value that might be a Promise
|
|
696
|
+
*/
|
|
697
|
+
async function unwrapMaybePromise(value) {
|
|
698
|
+
return isPromise(value) ? await value : value;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Shallow/deep-ish equality check to avoid unnecessary runs on identical values
|
|
702
|
+
*/
|
|
703
|
+
function valuesEqual(a, b) {
|
|
704
|
+
if (a === b)
|
|
705
|
+
return true;
|
|
706
|
+
if (typeof a !== typeof b)
|
|
707
|
+
return false;
|
|
708
|
+
if (a && b && typeof a === "object") {
|
|
709
|
+
try {
|
|
710
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
711
|
+
}
|
|
712
|
+
catch {
|
|
713
|
+
return false;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
return false;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
function tryHandleResolving(def, registry, environment) {
|
|
720
|
+
const out = new Map();
|
|
721
|
+
const pending = new Set();
|
|
722
|
+
for (const n of def.nodes) {
|
|
723
|
+
const desc = registry.nodes.get(n.typeId);
|
|
724
|
+
if (!desc)
|
|
725
|
+
continue;
|
|
726
|
+
const overrideInputs = n.resolvedHandles?.inputs;
|
|
727
|
+
const overrideOutputs = n.resolvedHandles?.outputs;
|
|
728
|
+
const overrideDefaults = n.resolvedHandles?.inputDefaults;
|
|
729
|
+
// Resolve dynamic handles if available (initial pass: inputs may be undefined)
|
|
730
|
+
let dyn = {};
|
|
731
|
+
try {
|
|
732
|
+
if (typeof desc.resolveHandles === "function") {
|
|
733
|
+
const maybe = desc.resolveHandles({
|
|
734
|
+
nodeId: n.nodeId,
|
|
735
|
+
environment: environment || {},
|
|
736
|
+
params: n.params,
|
|
737
|
+
inputs: undefined,
|
|
738
|
+
});
|
|
739
|
+
// Only use sync results here; async results are applied via recompute later
|
|
740
|
+
if (isPromise(maybe)) {
|
|
741
|
+
// mark node as pending async recompute
|
|
742
|
+
pending.add(n.nodeId);
|
|
743
|
+
}
|
|
744
|
+
else {
|
|
745
|
+
dyn = maybe || {};
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
catch {
|
|
750
|
+
// ignore dynamic resolution errors at this stage
|
|
751
|
+
}
|
|
752
|
+
// Merge base with dynamic and overrides (allow partial resolvedHandles)
|
|
753
|
+
const inputs = {
|
|
754
|
+
...desc.inputs,
|
|
755
|
+
...dyn.inputs,
|
|
756
|
+
...overrideInputs,
|
|
757
|
+
};
|
|
758
|
+
const outputs = {
|
|
759
|
+
...desc.outputs,
|
|
760
|
+
...dyn.outputs,
|
|
761
|
+
...overrideOutputs,
|
|
762
|
+
};
|
|
763
|
+
const inputDefaults = {
|
|
764
|
+
...desc.inputDefaults,
|
|
765
|
+
...dyn.inputDefaults,
|
|
766
|
+
...overrideDefaults,
|
|
767
|
+
};
|
|
768
|
+
out.set(n.nodeId, { inputs, outputs, inputDefaults });
|
|
769
|
+
}
|
|
770
|
+
return { resolved: out, pending };
|
|
771
|
+
}
|
|
772
|
+
function buildEdges(def, registry, resolvedByNode) {
|
|
773
|
+
return def.edges.map((e) => {
|
|
774
|
+
const srcNode = def.nodes.find((n) => n.nodeId === e.source.nodeId);
|
|
775
|
+
const dstNode = def.nodes.find((n) => n.nodeId === e.target.nodeId);
|
|
776
|
+
const { srcDeclared, dstDeclared, effectiveTypeId } = extractEdgeTypes(e.source.nodeId, e.source.handle, e.target.nodeId, e.target.handle, resolvedByNode, e.typeId);
|
|
777
|
+
const { convert, convertAsync } = buildEdgeConverters(srcDeclared, dstDeclared, registry, `buildEdges: ${srcNode?.typeId || ""}.${e.source.nodeId}.${e.source.handle} -> ${dstNode?.typeId || ""}.${e.target.nodeId}.${e.target.handle}`);
|
|
778
|
+
return {
|
|
779
|
+
id: e.id,
|
|
780
|
+
source: { ...e.source },
|
|
781
|
+
target: { ...e.target },
|
|
782
|
+
typeId: e.typeId, // Preserve original (may be undefined)
|
|
783
|
+
effectiveTypeId, // Always present
|
|
784
|
+
convert,
|
|
785
|
+
convertAsync,
|
|
786
|
+
srcUnionTypes: Array.isArray(srcDeclared) ? [...srcDeclared] : undefined,
|
|
787
|
+
dstDeclared,
|
|
788
|
+
stats: { runs: 0, inFlight: false, progress: 0 },
|
|
789
|
+
};
|
|
790
|
+
});
|
|
791
|
+
}
|
|
792
|
+
/**
|
|
793
|
+
* Extract edge type information from resolved handles
|
|
794
|
+
* Used by both buildEdges and updateNodeHandles to avoid duplication
|
|
795
|
+
*/
|
|
796
|
+
function extractEdgeTypes(sourceNodeId, sourceHandle, targetNodeId, targetHandle, resolvedByNode, explicitTypeId) {
|
|
797
|
+
const srcResolved = resolvedByNode.get(sourceNodeId);
|
|
798
|
+
const dstResolved = resolvedByNode.get(targetNodeId);
|
|
799
|
+
const srcDeclared = srcResolved
|
|
800
|
+
? srcResolved.outputs[sourceHandle]
|
|
801
|
+
: undefined;
|
|
802
|
+
const dstDeclared = dstResolved
|
|
803
|
+
? getInputTypeId(dstResolved.inputs, targetHandle)
|
|
804
|
+
: undefined;
|
|
805
|
+
let effectiveTypeId = explicitTypeId;
|
|
806
|
+
if (!effectiveTypeId) {
|
|
807
|
+
// Infer if not explicitly set
|
|
808
|
+
effectiveTypeId = Array.isArray(srcDeclared) ? srcDeclared[0] : srcDeclared;
|
|
809
|
+
}
|
|
810
|
+
return {
|
|
811
|
+
srcDeclared,
|
|
812
|
+
dstDeclared,
|
|
813
|
+
effectiveTypeId: effectiveTypeId ?? "untyped",
|
|
814
|
+
};
|
|
815
|
+
}
|
|
816
|
+
// Static helper: build edge converters for type coercion
|
|
817
|
+
function buildEdgeConverters(srcDeclared, dstDeclared, registry, edgeLabel) {
|
|
818
|
+
if (!dstDeclared || !srcDeclared) {
|
|
819
|
+
return {};
|
|
820
|
+
}
|
|
821
|
+
const isUnion = Array.isArray(srcDeclared);
|
|
822
|
+
const srcTypes = isUnion ? srcDeclared : [srcDeclared];
|
|
823
|
+
// Helper to get the coercion for a specific type
|
|
824
|
+
const getCoercion = (typeId) => {
|
|
825
|
+
return registry.resolveCoercion(typeId, dstDeclared);
|
|
826
|
+
};
|
|
827
|
+
// Resolve coercions for all source types
|
|
828
|
+
const coercions = srcTypes.map(getCoercion);
|
|
829
|
+
const hasAsync = coercions.some((r) => r?.kind === "async");
|
|
830
|
+
// Helper to extract and validate typed output for unions
|
|
831
|
+
const extractPayload = (v) => {
|
|
832
|
+
const typeId = getTypedOutputTypeId(v);
|
|
833
|
+
const payload = getTypedOutputValue(v);
|
|
834
|
+
if (isUnion) {
|
|
835
|
+
if (!typeId) {
|
|
836
|
+
throw new Error(`Typed output required for union source (${edgeLabel}); allowed: ${srcTypes.join("|")}`);
|
|
837
|
+
}
|
|
838
|
+
if (!srcTypes.includes(typeId)) {
|
|
839
|
+
throw new Error(`Invalid typed output ${typeId} (${edgeLabel}); allowed: ${srcTypes.join("|")}`);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
else if (typeId) {
|
|
843
|
+
// Warn if typed output is used for non-union source
|
|
844
|
+
console.warn(`Typed output ${typeId} is fed even though source is not union (${edgeLabel}): ${srcDeclared} -> ${dstDeclared}`);
|
|
845
|
+
}
|
|
846
|
+
return { typeId: typeId || srcTypes[0], payload };
|
|
847
|
+
};
|
|
848
|
+
if (hasAsync) {
|
|
849
|
+
return {
|
|
850
|
+
convertAsync: async (v, signal) => {
|
|
851
|
+
const { typeId, payload } = extractPayload(v);
|
|
852
|
+
const res = getCoercion(typeId);
|
|
853
|
+
if (!res)
|
|
854
|
+
return payload;
|
|
855
|
+
if (res.kind === "async") {
|
|
856
|
+
return await res.convertAsync(payload, signal);
|
|
857
|
+
}
|
|
858
|
+
return res.convert(payload);
|
|
859
|
+
},
|
|
860
|
+
};
|
|
861
|
+
}
|
|
862
|
+
// Sync path
|
|
863
|
+
const firstCoercion = coercions.find((r) => r?.kind === "sync");
|
|
864
|
+
if (!firstCoercion) {
|
|
865
|
+
return {};
|
|
866
|
+
}
|
|
867
|
+
return {
|
|
868
|
+
convert: (v) => {
|
|
869
|
+
const { typeId, payload } = extractPayload(v);
|
|
870
|
+
const res = getCoercion(typeId);
|
|
871
|
+
if (!res)
|
|
872
|
+
return payload;
|
|
873
|
+
if (res.kind === "async") {
|
|
874
|
+
throw new Error(`Async coercion required but convert used (${edgeLabel})`);
|
|
875
|
+
}
|
|
876
|
+
return res.convert(payload);
|
|
877
|
+
},
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
|
|
814
881
|
/**
|
|
815
882
|
* HandleResolver component - manages dynamic handle resolution
|
|
816
883
|
*/
|
|
817
884
|
class HandleResolver {
|
|
818
|
-
constructor(
|
|
819
|
-
this.
|
|
820
|
-
this.environment = {};
|
|
821
|
-
this.graphStructure = graphStructure;
|
|
885
|
+
constructor(graph, eventEmitter, runContextManager, edgePropagator, registry, environment) {
|
|
886
|
+
this.graph = graph;
|
|
822
887
|
this.eventEmitter = eventEmitter;
|
|
823
|
-
this.
|
|
888
|
+
this.runContextManager = runContextManager;
|
|
889
|
+
this.edgePropagator = edgePropagator;
|
|
824
890
|
this.registry = registry;
|
|
891
|
+
this.recomputeTokenByNode = new Map();
|
|
892
|
+
this.environment = {};
|
|
825
893
|
this.environment = environment ?? {};
|
|
826
894
|
}
|
|
827
895
|
setRegistry(registry) {
|
|
@@ -837,106 +905,162 @@ class HandleResolver {
|
|
|
837
905
|
// If no registry or node not found, skip
|
|
838
906
|
if (!this.registry)
|
|
839
907
|
return;
|
|
840
|
-
const node = this.
|
|
908
|
+
const node = this.graph.getNode(nodeId);
|
|
841
909
|
if (!node)
|
|
842
910
|
return;
|
|
911
|
+
// Track resolver start for all active run-contexts
|
|
912
|
+
if (node.activeRunContextIds && node.activeRunContextIds.size > 0) {
|
|
913
|
+
for (const runContextId of node.activeRunContextIds) {
|
|
914
|
+
this.runContextManager.startHandleResolution(runContextId, nodeId);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
843
917
|
setTimeout(() => {
|
|
844
|
-
void this.recomputeHandlesForNode(nodeId);
|
|
918
|
+
void this.recomputeHandlesForNode(nodeId, node.activeRunContextIds);
|
|
845
919
|
}, 0);
|
|
846
920
|
}
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
async recomputeHandlesForNode(nodeId) {
|
|
851
|
-
const registry = this.registry;
|
|
852
|
-
const node = this.graphStructure.getNode(nodeId);
|
|
853
|
-
if (!node)
|
|
854
|
-
return;
|
|
855
|
-
const desc = registry.nodes.get(node.typeId);
|
|
856
|
-
if (!desc)
|
|
921
|
+
// Update resolved handles for a single node and refresh edge converters/types that touch it
|
|
922
|
+
updateNodeHandles(nodeId, handles) {
|
|
923
|
+
if (!this.registry)
|
|
857
924
|
return;
|
|
858
|
-
const
|
|
859
|
-
if (
|
|
925
|
+
const node = this.graph.getNode(nodeId);
|
|
926
|
+
if (!node)
|
|
860
927
|
return;
|
|
861
|
-
|
|
862
|
-
this.
|
|
863
|
-
|
|
864
|
-
const
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
928
|
+
this.graph.setResolvedHandles(nodeId, handles);
|
|
929
|
+
const edges = this.graph.getEdges();
|
|
930
|
+
const resolvedByNode = this.graph.getResolvedHandlesMap();
|
|
931
|
+
for (const e of edges) {
|
|
932
|
+
// Only update edges that touch the changed node
|
|
933
|
+
const touchesChangedNode = e.source.nodeId === nodeId || e.target.nodeId === nodeId;
|
|
934
|
+
if (!touchesChangedNode)
|
|
935
|
+
continue;
|
|
936
|
+
const srcNode = this.graph.getNode(e.source.nodeId);
|
|
937
|
+
const dstNode = this.graph.getNode(e.target.nodeId);
|
|
938
|
+
const oldDstDeclared = e.dstDeclared;
|
|
939
|
+
// Extract edge types using shared helper (handles both source and target updates)
|
|
940
|
+
const { srcDeclared, dstDeclared, effectiveTypeId } = extractEdgeTypes(e.source.nodeId, e.source.handle, e.target.nodeId, e.target.handle, resolvedByNode, e.typeId);
|
|
941
|
+
// Update edge properties
|
|
942
|
+
if (!e.typeId) {
|
|
943
|
+
e.effectiveTypeId = effectiveTypeId;
|
|
944
|
+
}
|
|
945
|
+
e.dstDeclared = dstDeclared;
|
|
946
|
+
e.srcUnionTypes = Array.isArray(srcDeclared)
|
|
947
|
+
? [...srcDeclared]
|
|
948
|
+
: undefined;
|
|
949
|
+
// Update converters
|
|
950
|
+
const conv = buildEdgeConverters(srcDeclared, dstDeclared, this.registry, `updateNodeHandles: ${srcNode?.typeId || ""}.${e.source.nodeId}.${e.source.handle} -> ${dstNode?.typeId || ""}.${e.target.nodeId}.${e.target.handle}`);
|
|
951
|
+
e.convert = conv.convert;
|
|
952
|
+
e.convertAsync = conv.convertAsync;
|
|
953
|
+
if (e.target.nodeId === nodeId &&
|
|
954
|
+
oldDstDeclared === undefined &&
|
|
955
|
+
dstDeclared !== undefined) {
|
|
956
|
+
const srcNode = this.graph.getNode(e.source.nodeId);
|
|
957
|
+
if (srcNode) {
|
|
958
|
+
const srcValue = srcNode.outputs[e.source.handle];
|
|
959
|
+
if (srcValue !== undefined) {
|
|
960
|
+
this.edgePropagator.propagate(e.source.nodeId, e.source.handle, srcValue, srcNode.activeRunContextIds);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
}
|
|
965
|
+
this.edgePropagator.invalidateDownstream(nodeId);
|
|
966
|
+
}
|
|
967
|
+
/**
|
|
968
|
+
* Recompute dynamic handles for a single node using current inputs/environment
|
|
969
|
+
*/
|
|
970
|
+
async recomputeHandlesForNode(nodeId, activeRunContextIds) {
|
|
971
|
+
try {
|
|
972
|
+
if (!this.registry)
|
|
973
|
+
return;
|
|
974
|
+
const node = this.graph.getNode(nodeId);
|
|
975
|
+
if (!node)
|
|
976
|
+
return;
|
|
977
|
+
const desc = this.registry.nodes.get(node.typeId);
|
|
978
|
+
if (!desc)
|
|
979
|
+
return;
|
|
980
|
+
const resolveHandles = desc.resolveHandles;
|
|
981
|
+
if (typeof resolveHandles !== "function")
|
|
982
|
+
return;
|
|
983
|
+
const token = (this.recomputeTokenByNode.get(nodeId) ?? 0) + 1;
|
|
984
|
+
this.recomputeTokenByNode.set(nodeId, token);
|
|
985
|
+
// Log resolveHandles-start
|
|
986
|
+
const nodeLogLevel = node.logLevel ?? "info";
|
|
987
|
+
const nodeLogValue = LOG_LEVEL_VALUES[nodeLogLevel] ?? 1;
|
|
988
|
+
const shouldLog = nodeLogValue <= LOG_LEVEL_VALUES.debug && nodeLogLevel !== "silent";
|
|
989
|
+
if (shouldLog) {
|
|
990
|
+
console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-start`);
|
|
991
|
+
}
|
|
992
|
+
let resolved;
|
|
993
|
+
try {
|
|
994
|
+
const res = resolveHandles({
|
|
995
|
+
nodeId,
|
|
996
|
+
environment: this.environment || {},
|
|
997
|
+
params: node.params,
|
|
998
|
+
inputs: node.inputs || {},
|
|
999
|
+
});
|
|
1000
|
+
resolved = await unwrapMaybePromise(res);
|
|
1001
|
+
}
|
|
1002
|
+
catch {
|
|
1003
|
+
// Log resolveHandles-done even on error
|
|
1004
|
+
if (shouldLog) {
|
|
1005
|
+
console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-done (error)`);
|
|
1006
|
+
}
|
|
1007
|
+
return;
|
|
1008
|
+
}
|
|
1009
|
+
// Log resolveHandles-done
|
|
1010
|
+
if (shouldLog) {
|
|
1011
|
+
console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-done`);
|
|
1012
|
+
}
|
|
1013
|
+
// If a newer recompute was scheduled, drop this result
|
|
1014
|
+
if ((this.recomputeTokenByNode.get(nodeId) ?? 0) !== token)
|
|
1015
|
+
return;
|
|
1016
|
+
const before = this.graph.getResolvedHandles(nodeId);
|
|
1017
|
+
if (!before)
|
|
1018
|
+
return;
|
|
1019
|
+
// Re-fetch desc to ensure we have the latest (node might have been updated)
|
|
1020
|
+
const nodeDesc = this.registry.nodes.get(node.typeId);
|
|
1021
|
+
if (!nodeDesc)
|
|
1022
|
+
return;
|
|
1023
|
+
const inputs = { ...nodeDesc.inputs, ...resolved?.inputs };
|
|
1024
|
+
const outputs = { ...nodeDesc.outputs, ...resolved?.outputs };
|
|
1025
|
+
const inputDefaults = {
|
|
1026
|
+
...nodeDesc.inputDefaults,
|
|
1027
|
+
...resolved?.inputDefaults,
|
|
1028
|
+
};
|
|
1029
|
+
const after = { inputs, outputs, inputDefaults };
|
|
1030
|
+
// Compare shallow-structurally via JSON
|
|
1031
|
+
if (JSON.stringify(before) === JSON.stringify(after))
|
|
1032
|
+
return;
|
|
1033
|
+
// Call GraphRuntime's updateNodeHandles to update edges and re-propagate values
|
|
1034
|
+
// Note: updateNodeHandles will set the resolved handles internally
|
|
1035
|
+
this.updateNodeHandles(nodeId, after);
|
|
1036
|
+
// Notify graph updated with the changed handles
|
|
1037
|
+
this.eventEmitter.emit("invalidate", {
|
|
1038
|
+
reason: "graph-updated",
|
|
1039
|
+
resolvedHandles: { [nodeId]: after },
|
|
1040
|
+
});
|
|
1041
|
+
}
|
|
1042
|
+
finally {
|
|
1043
|
+
// Track resolver finish after successful completion
|
|
1044
|
+
if (activeRunContextIds && activeRunContextIds.size > 0) {
|
|
1045
|
+
for (const runContextId of activeRunContextIds) {
|
|
1046
|
+
this.runContextManager.finishHandleResolution(runContextId, nodeId);
|
|
1047
|
+
}
|
|
884
1048
|
}
|
|
885
|
-
return;
|
|
886
|
-
}
|
|
887
|
-
// Log resolveHandles-done
|
|
888
|
-
if (shouldLog) {
|
|
889
|
-
console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-done`);
|
|
890
1049
|
}
|
|
891
|
-
// If a newer recompute was scheduled, drop this result
|
|
892
|
-
if ((this.recomputeTokenByNode.get(nodeId) ?? 0) !== token)
|
|
893
|
-
return;
|
|
894
|
-
const resolved = this.graphStructure.getResolvedHandles(nodeId);
|
|
895
|
-
if (!resolved)
|
|
896
|
-
return; // Node was deleted
|
|
897
|
-
// Re-fetch desc to ensure we have the latest (node might have been updated)
|
|
898
|
-
const nodeDesc = registry.nodes.get(node.typeId);
|
|
899
|
-
if (!nodeDesc)
|
|
900
|
-
return;
|
|
901
|
-
const inputs = { ...nodeDesc.inputs, ...r?.inputs };
|
|
902
|
-
const outputs = { ...nodeDesc.outputs, ...r?.outputs };
|
|
903
|
-
const inputDefaults = {
|
|
904
|
-
...nodeDesc.inputDefaults,
|
|
905
|
-
...r?.inputDefaults,
|
|
906
|
-
};
|
|
907
|
-
const next = { inputs, outputs, inputDefaults };
|
|
908
|
-
const before = resolved;
|
|
909
|
-
// Compare shallow-structurally via JSON
|
|
910
|
-
if (JSON.stringify(before) === JSON.stringify(next))
|
|
911
|
-
return;
|
|
912
|
-
this.graphStructure.setResolvedHandles(nodeId, next);
|
|
913
|
-
// Call GraphRuntime's updateNodeHandles to update edges and re-propagate values
|
|
914
|
-
this.runtimeCoordinator.updateNodeHandles(nodeId, next, registry);
|
|
915
|
-
// Notify graph updated with the changed handles
|
|
916
|
-
this.eventEmitter.emit("invalidate", {
|
|
917
|
-
reason: "graph-updated",
|
|
918
|
-
resolvedHandles: { [nodeId]: next },
|
|
919
|
-
});
|
|
920
1050
|
}
|
|
921
1051
|
}
|
|
922
1052
|
|
|
923
1053
|
/**
|
|
924
|
-
*
|
|
1054
|
+
* EdgePropagator component - handles value propagation through edges
|
|
925
1055
|
*/
|
|
926
|
-
class
|
|
927
|
-
constructor(
|
|
928
|
-
this.
|
|
929
|
-
this.graphStructure = graphStructure;
|
|
1056
|
+
class EdgePropagator {
|
|
1057
|
+
constructor(graph, eventEmitter, runContextManager, handleResolver, nodeExecutor) {
|
|
1058
|
+
this.graph = graph;
|
|
930
1059
|
this.eventEmitter = eventEmitter;
|
|
931
1060
|
this.runContextManager = runContextManager;
|
|
932
1061
|
this.handleResolver = handleResolver;
|
|
933
|
-
this.
|
|
934
|
-
|
|
935
|
-
/**
|
|
936
|
-
* Set the execution scheduler (called after construction to resolve circular dependency)
|
|
937
|
-
*/
|
|
938
|
-
setExecutionScheduler(executionScheduler) {
|
|
939
|
-
this.executionScheduler = executionScheduler;
|
|
1062
|
+
this.nodeExecutor = nodeExecutor;
|
|
1063
|
+
this.arrayInputBuckets = new Map();
|
|
940
1064
|
}
|
|
941
1065
|
/**
|
|
942
1066
|
* Propagate value through edges
|
|
@@ -945,10 +1069,24 @@ class ValuePropagator {
|
|
|
945
1069
|
*/
|
|
946
1070
|
propagate(srcNodeId, srcHandle, value, runContextIds) {
|
|
947
1071
|
// Set source output
|
|
948
|
-
|
|
1072
|
+
if (!this.setSourceOutput(srcNodeId, srcHandle, value)) {
|
|
1073
|
+
return; // Node was removed
|
|
1074
|
+
}
|
|
1075
|
+
// Find outgoing edges
|
|
1076
|
+
const outEdges = this.findOutgoingEdges(srcNodeId, srcHandle);
|
|
1077
|
+
// Process each edge
|
|
1078
|
+
for (const edge of outEdges) {
|
|
1079
|
+
this.propagateToEdge(edge, value, srcNodeId, runContextIds);
|
|
1080
|
+
}
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Set source output value and emit event
|
|
1084
|
+
*/
|
|
1085
|
+
setSourceOutput(srcNodeId, srcHandle, value) {
|
|
1086
|
+
const srcNode = this.graph.getNode(srcNodeId);
|
|
949
1087
|
if (!srcNode) {
|
|
950
1088
|
// Node was removed (e.g., graph updated) but an async emit arrived late; ignore
|
|
951
|
-
return;
|
|
1089
|
+
return false;
|
|
952
1090
|
}
|
|
953
1091
|
srcNode.outputs[srcHandle] = value;
|
|
954
1092
|
this.eventEmitter.emit("value", {
|
|
@@ -958,224 +1096,306 @@ class ValuePropagator {
|
|
|
958
1096
|
io: "output",
|
|
959
1097
|
runtimeTypeId: getTypedOutputTypeId(value),
|
|
960
1098
|
});
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
1099
|
+
return true;
|
|
1100
|
+
}
|
|
1101
|
+
/**
|
|
1102
|
+
* Find all outgoing edges from a source node handle
|
|
1103
|
+
*/
|
|
1104
|
+
findOutgoingEdges(srcNodeId, srcHandle) {
|
|
1105
|
+
const edges = this.graph.getEdges();
|
|
1106
|
+
return edges.filter((e) => e.source.nodeId === srcNodeId && e.source.handle === srcHandle);
|
|
1107
|
+
}
|
|
1108
|
+
/**
|
|
1109
|
+
* Propagate value to a single edge
|
|
1110
|
+
*/
|
|
1111
|
+
propagateToEdge(edge, value, srcNodeId, runContextIds) {
|
|
1112
|
+
// Filter run-contexts
|
|
1113
|
+
const effectiveRunContexts = runContextIds && runContextIds.size > 0
|
|
1114
|
+
? this.filterEffectiveRunContexts(edge, srcNodeId, runContextIds)
|
|
1115
|
+
: undefined;
|
|
1116
|
+
if (runContextIds &&
|
|
1117
|
+
runContextIds.size > 0 &&
|
|
1118
|
+
!(effectiveRunContexts && effectiveRunContexts.size > 0)) {
|
|
1119
|
+
return; // No valid run-contexts for this edge
|
|
1120
|
+
}
|
|
1121
|
+
// Validate union types
|
|
1122
|
+
if (!this.validateUnionType(edge, value, srcNodeId)) {
|
|
1123
|
+
return;
|
|
1124
|
+
}
|
|
1125
|
+
// Clone value per edge to isolate conversions
|
|
1126
|
+
let nextVal = structuredClone(value);
|
|
1127
|
+
// Apply conversion and propagate
|
|
1128
|
+
if (edge.convertAsync) {
|
|
1129
|
+
this.handleAsyncConversion(edge, nextVal, effectiveRunContexts);
|
|
1130
|
+
}
|
|
1131
|
+
else {
|
|
1132
|
+
this.handleSyncConversion(edge, nextVal, effectiveRunContexts);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
/**
|
|
1136
|
+
* Filter run-contexts to exclude cancelled nodes
|
|
1137
|
+
*/
|
|
1138
|
+
filterEffectiveRunContexts(edge, srcNodeId, runContextIds) {
|
|
1139
|
+
const effectiveRunContexts = new Set();
|
|
1140
|
+
for (const id of runContextIds) {
|
|
1141
|
+
const ctx = this.runContextManager.getRunContext(id);
|
|
1142
|
+
if (!ctx)
|
|
1143
|
+
continue;
|
|
1144
|
+
if (ctx.cancelledNodes.has(srcNodeId))
|
|
995
1145
|
continue;
|
|
1146
|
+
if (ctx.cancelledNodes.has(edge.target.nodeId))
|
|
1147
|
+
continue;
|
|
1148
|
+
effectiveRunContexts.add(id);
|
|
1149
|
+
}
|
|
1150
|
+
return effectiveRunContexts.size > 0 ? effectiveRunContexts : undefined;
|
|
1151
|
+
}
|
|
1152
|
+
/**
|
|
1153
|
+
* Validate union type requirements
|
|
1154
|
+
*/
|
|
1155
|
+
validateUnionType(edge, value, srcNodeId) {
|
|
1156
|
+
const isUnion = Array.isArray(edge.srcUnionTypes);
|
|
1157
|
+
const isTyped = isTypedOutput(value);
|
|
1158
|
+
if (isUnion && !isTyped) {
|
|
1159
|
+
const err = new Error(`Output ${srcNodeId}.${edge.source.handle} requires typed value for union output (allowed: ${edge.srcUnionTypes.join("|")})`);
|
|
1160
|
+
this.eventEmitter.emit("error", {
|
|
1161
|
+
kind: "edge-convert",
|
|
1162
|
+
edgeId: edge.id,
|
|
1163
|
+
source: { nodeId: edge.source.nodeId, handle: edge.source.handle },
|
|
1164
|
+
target: { nodeId: edge.target.nodeId, handle: edge.target.handle },
|
|
1165
|
+
err,
|
|
1166
|
+
});
|
|
1167
|
+
return false;
|
|
1168
|
+
}
|
|
1169
|
+
return true;
|
|
1170
|
+
}
|
|
1171
|
+
/**
|
|
1172
|
+
* Handle synchronous conversion
|
|
1173
|
+
*/
|
|
1174
|
+
handleSyncConversion(edge, value, effectiveRunContexts) {
|
|
1175
|
+
let convertedValue = value;
|
|
1176
|
+
if (edge.convert) {
|
|
1177
|
+
convertedValue = edge.convert(value);
|
|
1178
|
+
}
|
|
1179
|
+
this.applyToTarget(edge, convertedValue, effectiveRunContexts);
|
|
1180
|
+
}
|
|
1181
|
+
/**
|
|
1182
|
+
* Handle asynchronous conversion
|
|
1183
|
+
*/
|
|
1184
|
+
handleAsyncConversion(edge, value, effectiveRunContexts) {
|
|
1185
|
+
if (!edge.convertAsync)
|
|
1186
|
+
return;
|
|
1187
|
+
// Track edge run-context IDs for pendingEdges tracking
|
|
1188
|
+
const edgeRunContextIds = effectiveRunContexts
|
|
1189
|
+
? Array.from(effectiveRunContexts)
|
|
1190
|
+
: undefined;
|
|
1191
|
+
if (edgeRunContextIds) {
|
|
1192
|
+
for (const id of edgeRunContextIds) {
|
|
1193
|
+
this.runContextManager.startEdgeConversion(id, edge.id);
|
|
996
1194
|
}
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
let forHandle = forNode.get(e.target.handle);
|
|
1017
|
-
if (!forHandle) {
|
|
1018
|
-
forHandle = new Map();
|
|
1019
|
-
forNode.set(e.target.handle, forHandle);
|
|
1020
|
-
}
|
|
1021
|
-
forHandle.set(e.id, toArray(v));
|
|
1022
|
-
const merged = [];
|
|
1023
|
-
for (const ed of edges) {
|
|
1024
|
-
if (ed.target.nodeId === e.target.nodeId &&
|
|
1025
|
-
ed.target.handle === e.target.handle) {
|
|
1026
|
-
const part = forHandle.get(ed.id);
|
|
1027
|
-
if (part && part.length)
|
|
1028
|
-
merged.push(...part);
|
|
1029
|
-
}
|
|
1030
|
-
}
|
|
1031
|
-
next = merged;
|
|
1032
|
-
}
|
|
1033
|
-
const prev = dstNode.inputs[e.target.handle];
|
|
1034
|
-
const same = valuesEqual(prev, next);
|
|
1035
|
-
if (!same) {
|
|
1036
|
-
// Check skipPropagateValues (only in run-context mode)
|
|
1037
|
-
let shouldSkipPropagateValues = false;
|
|
1038
|
-
if (isRunContextAware && effectiveRunContexts) {
|
|
1039
|
-
for (const id of effectiveRunContexts) {
|
|
1040
|
-
const ctx = this.runContextManager.getRunContext(id);
|
|
1041
|
-
if (ctx && ctx.skipPropagateValues) {
|
|
1042
|
-
shouldSkipPropagateValues = true;
|
|
1043
|
-
break;
|
|
1044
|
-
}
|
|
1045
|
-
}
|
|
1046
|
-
}
|
|
1047
|
-
// Set input values unless skipPropagateValues is enabled
|
|
1048
|
-
if (!shouldSkipPropagateValues) {
|
|
1049
|
-
dstNode.inputs[e.target.handle] = next;
|
|
1050
|
-
this.eventEmitter.emit("value", {
|
|
1051
|
-
nodeId: e.target.nodeId,
|
|
1052
|
-
handle: e.target.handle,
|
|
1053
|
-
value: next,
|
|
1054
|
-
io: "input",
|
|
1055
|
-
runtimeTypeId: getTypedOutputTypeId(next),
|
|
1056
|
-
});
|
|
1057
|
-
this.handleResolver.scheduleRecomputeHandles(e.target.nodeId);
|
|
1058
|
-
}
|
|
1059
|
-
// Check propagate flag (only in run-context mode; auto mode always propagates)
|
|
1060
|
-
let shouldPropagate = true; // Default: always propagate in auto mode
|
|
1061
|
-
if (isRunContextAware && effectiveRunContexts) {
|
|
1062
|
-
shouldPropagate = false;
|
|
1063
|
-
for (const id of effectiveRunContexts) {
|
|
1064
|
-
const ctx = this.runContextManager.getRunContext(id);
|
|
1065
|
-
if (ctx && ctx.propagate) {
|
|
1066
|
-
shouldPropagate = true;
|
|
1067
|
-
break;
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
// Schedule downstream execution if propagation is enabled.
|
|
1072
|
-
// In manual mode we still allow scheduling for run-context-aware runs.
|
|
1073
|
-
const paused = this.runtimeCoordinator.isPaused();
|
|
1074
|
-
const canSchedule = (!paused || isRunContextAware) &&
|
|
1075
|
-
shouldPropagate &&
|
|
1076
|
-
this.executionScheduler.allInboundHaveValue(e.target.nodeId);
|
|
1077
|
-
if (canSchedule) {
|
|
1078
|
-
if (isRunContextAware && effectiveRunContexts) {
|
|
1079
|
-
this.executionScheduler.scheduleInputsChangedWithRunContexts(e.target.nodeId, effectiveRunContexts);
|
|
1080
|
-
}
|
|
1081
|
-
else {
|
|
1082
|
-
this.executionScheduler.scheduleInputsChanged(e.target.nodeId);
|
|
1083
|
-
}
|
|
1084
|
-
}
|
|
1085
|
-
}
|
|
1086
|
-
};
|
|
1087
|
-
// Handle async conversion
|
|
1088
|
-
if (e.convertAsync) {
|
|
1089
|
-
this.eventEmitter.emit("stats", {
|
|
1090
|
-
kind: "edge-start",
|
|
1091
|
-
edgeId: e.id,
|
|
1092
|
-
typeId: e.typeId,
|
|
1093
|
-
source: { nodeId: e.source.nodeId, handle: e.source.handle },
|
|
1094
|
-
target: { nodeId: e.target.nodeId, handle: e.target.handle },
|
|
1095
|
-
});
|
|
1096
|
-
const controller = new AbortController();
|
|
1097
|
-
const startAt = Date.now();
|
|
1098
|
-
e.stats.runs += 1;
|
|
1099
|
-
e.stats.inFlight = true;
|
|
1100
|
-
if (!isRunContextAware) {
|
|
1101
|
-
// Only track progress in auto mode
|
|
1102
|
-
e.stats.progress = 0;
|
|
1103
|
-
}
|
|
1104
|
-
const sig = controller.signal;
|
|
1105
|
-
e.convertAsync(nextVal, sig)
|
|
1106
|
-
.then((converted) => {
|
|
1107
|
-
if (!sig.aborted) {
|
|
1108
|
-
applyToTarget(converted);
|
|
1109
|
-
e.stats.inFlight = false;
|
|
1110
|
-
const duration = Date.now() - startAt;
|
|
1111
|
-
e.stats.lastDurationMs = duration;
|
|
1112
|
-
if (!isRunContextAware) {
|
|
1113
|
-
// More detailed stats in auto mode
|
|
1114
|
-
e.stats.lastEndAt = Date.now();
|
|
1115
|
-
e.stats.lastError = undefined;
|
|
1116
|
-
this.eventEmitter.emit("stats", {
|
|
1117
|
-
kind: "edge-done",
|
|
1118
|
-
edgeId: e.id,
|
|
1119
|
-
typeId: e.typeId,
|
|
1120
|
-
source: { nodeId: e.source.nodeId, handle: e.source.handle },
|
|
1121
|
-
target: { nodeId: e.target.nodeId, handle: e.target.handle },
|
|
1122
|
-
durationMs: duration,
|
|
1123
|
-
});
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
})
|
|
1127
|
-
.catch((err) => {
|
|
1128
|
-
if (sig.aborted)
|
|
1129
|
-
return;
|
|
1130
|
-
e.stats.inFlight = false;
|
|
1131
|
-
if (!isRunContextAware) {
|
|
1132
|
-
e.stats.lastError = err;
|
|
1133
|
-
}
|
|
1134
|
-
this.eventEmitter.emit("error", {
|
|
1135
|
-
kind: "edge-convert",
|
|
1136
|
-
edgeId: e.id,
|
|
1137
|
-
source: { nodeId: e.source.nodeId, handle: e.source.handle },
|
|
1138
|
-
target: { nodeId: e.target.nodeId, handle: e.target.handle },
|
|
1139
|
-
err,
|
|
1140
|
-
});
|
|
1141
|
-
});
|
|
1195
|
+
}
|
|
1196
|
+
this.eventEmitter.emit("stats", {
|
|
1197
|
+
kind: "edge-start",
|
|
1198
|
+
edgeId: edge.id,
|
|
1199
|
+
typeId: edge.typeId,
|
|
1200
|
+
source: { nodeId: edge.source.nodeId, handle: edge.source.handle },
|
|
1201
|
+
target: { nodeId: edge.target.nodeId, handle: edge.target.handle },
|
|
1202
|
+
});
|
|
1203
|
+
const controller = new AbortController();
|
|
1204
|
+
const startAt = Date.now();
|
|
1205
|
+
edge.stats.runs += 1;
|
|
1206
|
+
edge.stats.inFlight = true;
|
|
1207
|
+
edge.stats.progress = 0;
|
|
1208
|
+
edge
|
|
1209
|
+
.convertAsync(value, controller.signal)
|
|
1210
|
+
.then((converted) => {
|
|
1211
|
+
if (!controller.signal.aborted) {
|
|
1212
|
+
this.applyToTarget(edge, converted, effectiveRunContexts);
|
|
1213
|
+
this.updateEdgeStatsOnSuccess(edge, startAt);
|
|
1142
1214
|
}
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1215
|
+
})
|
|
1216
|
+
.catch((err) => {
|
|
1217
|
+
if (controller.signal.aborted)
|
|
1218
|
+
return;
|
|
1219
|
+
this.handleEdgeConversionError(edge, err);
|
|
1220
|
+
})
|
|
1221
|
+
.finally(() => {
|
|
1222
|
+
this.finishEdgeConversion(edgeRunContextIds, edge.id);
|
|
1223
|
+
});
|
|
1224
|
+
}
|
|
1225
|
+
/**
|
|
1226
|
+
* Apply value to target node input
|
|
1227
|
+
*/
|
|
1228
|
+
applyToTarget(edge, value, effectiveRunContexts) {
|
|
1229
|
+
const dstNode = this.graph.getNode(edge.target.nodeId);
|
|
1230
|
+
if (!dstNode)
|
|
1231
|
+
return;
|
|
1232
|
+
// Skip writing to unresolved handles
|
|
1233
|
+
if (edge.dstDeclared === undefined)
|
|
1234
|
+
return;
|
|
1235
|
+
// Handle array types
|
|
1236
|
+
const processedValue = this.processArrayInput(edge, value);
|
|
1237
|
+
// Check if value changed
|
|
1238
|
+
const prev = dstNode.inputs[edge.target.handle];
|
|
1239
|
+
if (valuesEqual(prev, processedValue)) {
|
|
1240
|
+
return; // No change
|
|
1241
|
+
}
|
|
1242
|
+
// Set input value (respecting skipPropagateValues)
|
|
1243
|
+
const shouldSetValue = this.shouldSetInputValue(effectiveRunContexts);
|
|
1244
|
+
if (shouldSetValue) {
|
|
1245
|
+
this.setTargetInput(edge, dstNode, processedValue);
|
|
1246
|
+
}
|
|
1247
|
+
// Schedule downstream execution
|
|
1248
|
+
this.executeDownstream(edge.target.nodeId, effectiveRunContexts);
|
|
1249
|
+
}
|
|
1250
|
+
/**
|
|
1251
|
+
* Process array input by merging values from all edges
|
|
1252
|
+
*/
|
|
1253
|
+
processArrayInput(edge, value) {
|
|
1254
|
+
const dstIsArray = typeof edge.dstDeclared === "string" && edge.dstDeclared.endsWith("[]");
|
|
1255
|
+
if (!dstIsArray) {
|
|
1256
|
+
return value;
|
|
1257
|
+
}
|
|
1258
|
+
const toArray = (x) => Array.isArray(x) ? x : x === undefined ? [] : [x];
|
|
1259
|
+
let forNode = this.arrayInputBuckets.get(edge.target.nodeId);
|
|
1260
|
+
if (!forNode) {
|
|
1261
|
+
forNode = new Map();
|
|
1262
|
+
this.arrayInputBuckets.set(edge.target.nodeId, forNode);
|
|
1263
|
+
}
|
|
1264
|
+
let forHandle = forNode.get(edge.target.handle);
|
|
1265
|
+
if (!forHandle) {
|
|
1266
|
+
forHandle = new Map();
|
|
1267
|
+
forNode.set(edge.target.handle, forHandle);
|
|
1268
|
+
}
|
|
1269
|
+
forHandle.set(edge.id, toArray(value));
|
|
1270
|
+
// Merge all parts for this handle
|
|
1271
|
+
const edges = this.graph.getEdges();
|
|
1272
|
+
const merged = [];
|
|
1273
|
+
for (const ed of edges) {
|
|
1274
|
+
if (ed.target.nodeId === edge.target.nodeId &&
|
|
1275
|
+
ed.target.handle === edge.target.handle) {
|
|
1276
|
+
const part = forHandle.get(ed.id);
|
|
1277
|
+
if (part && part.length)
|
|
1278
|
+
merged.push(...part);
|
|
1149
1279
|
}
|
|
1150
1280
|
}
|
|
1281
|
+
return merged;
|
|
1151
1282
|
}
|
|
1152
1283
|
/**
|
|
1153
|
-
*
|
|
1284
|
+
* Check if input value should be set (respecting skipPropagateValues)
|
|
1154
1285
|
*/
|
|
1155
|
-
|
|
1156
|
-
|
|
1286
|
+
shouldSetInputValue(effectiveRunContexts) {
|
|
1287
|
+
if (!effectiveRunContexts) {
|
|
1288
|
+
return true; // Auto mode always sets values
|
|
1289
|
+
}
|
|
1290
|
+
// Check skipPropagateValues (only in run-context mode)
|
|
1291
|
+
for (const id of effectiveRunContexts) {
|
|
1292
|
+
const ctx = this.runContextManager.getRunContext(id);
|
|
1293
|
+
if (ctx && ctx.skipPropagateValues) {
|
|
1294
|
+
return false;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
return true;
|
|
1298
|
+
}
|
|
1299
|
+
/**
|
|
1300
|
+
* Set target input value and emit event
|
|
1301
|
+
*/
|
|
1302
|
+
setTargetInput(edge, dstNode, value) {
|
|
1303
|
+
dstNode.inputs[edge.target.handle] = value;
|
|
1304
|
+
this.eventEmitter.emit("value", {
|
|
1305
|
+
nodeId: edge.target.nodeId,
|
|
1306
|
+
handle: edge.target.handle,
|
|
1307
|
+
value,
|
|
1308
|
+
io: "input",
|
|
1309
|
+
runtimeTypeId: getTypedOutputTypeId(value),
|
|
1310
|
+
});
|
|
1311
|
+
this.handleResolver.scheduleRecomputeHandles(edge.target.nodeId);
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Execute downstream if conditions are met
|
|
1315
|
+
*/
|
|
1316
|
+
executeDownstream(targetNodeId, effectiveRunContexts) {
|
|
1317
|
+
// Determine if we should propagate
|
|
1318
|
+
const shouldPropagate = this.shouldPropagateExecution(effectiveRunContexts);
|
|
1319
|
+
if (shouldPropagate && this.graph.allInboundHaveValue(targetNodeId)) {
|
|
1320
|
+
this.nodeExecutor.execute(targetNodeId, effectiveRunContexts);
|
|
1321
|
+
}
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Check if execution should propagate
|
|
1325
|
+
*/
|
|
1326
|
+
shouldPropagateExecution(effectiveRunContexts) {
|
|
1327
|
+
if (!effectiveRunContexts) {
|
|
1328
|
+
return true; // Auto mode always propagates
|
|
1329
|
+
}
|
|
1330
|
+
// Check propagate flag (only in run-context mode)
|
|
1331
|
+
for (const id of effectiveRunContexts) {
|
|
1332
|
+
const ctx = this.runContextManager.getRunContext(id);
|
|
1333
|
+
if (ctx && ctx.propagate) {
|
|
1334
|
+
return true;
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
return false;
|
|
1338
|
+
}
|
|
1339
|
+
/**
|
|
1340
|
+
* Update edge stats on successful conversion
|
|
1341
|
+
*/
|
|
1342
|
+
updateEdgeStatsOnSuccess(edge, startAt) {
|
|
1343
|
+
edge.stats.inFlight = false;
|
|
1344
|
+
const duration = Date.now() - startAt;
|
|
1345
|
+
edge.stats.lastDurationMs = duration;
|
|
1346
|
+
this.eventEmitter.emit("stats", {
|
|
1347
|
+
kind: "edge-done",
|
|
1348
|
+
edgeId: edge.id,
|
|
1349
|
+
typeId: edge.typeId,
|
|
1350
|
+
source: { nodeId: edge.source.nodeId, handle: edge.source.handle },
|
|
1351
|
+
target: { nodeId: edge.target.nodeId, handle: edge.target.handle },
|
|
1352
|
+
durationMs: duration,
|
|
1353
|
+
});
|
|
1354
|
+
edge.stats.lastEndAt = Date.now();
|
|
1355
|
+
edge.stats.lastError = undefined;
|
|
1356
|
+
}
|
|
1357
|
+
/**
|
|
1358
|
+
* Handle edge conversion error
|
|
1359
|
+
*/
|
|
1360
|
+
handleEdgeConversionError(edge, err) {
|
|
1361
|
+
edge.stats.inFlight = false;
|
|
1362
|
+
edge.stats.lastError = err;
|
|
1363
|
+
this.eventEmitter.emit("error", {
|
|
1364
|
+
kind: "edge-convert",
|
|
1365
|
+
edgeId: edge.id,
|
|
1366
|
+
source: { nodeId: edge.source.nodeId, handle: edge.source.handle },
|
|
1367
|
+
target: { nodeId: edge.target.nodeId, handle: edge.target.handle },
|
|
1368
|
+
err,
|
|
1369
|
+
});
|
|
1370
|
+
}
|
|
1371
|
+
/**
|
|
1372
|
+
* Finish edge conversion and decrement pending edges
|
|
1373
|
+
*/
|
|
1374
|
+
finishEdgeConversion(edgeRunContextIds, edgeId) {
|
|
1375
|
+
if (!edgeRunContextIds)
|
|
1376
|
+
return;
|
|
1377
|
+
for (const id of edgeRunContextIds) {
|
|
1378
|
+
this.runContextManager.finishEdgeConversion(id, edgeId);
|
|
1379
|
+
}
|
|
1157
1380
|
}
|
|
1158
1381
|
/**
|
|
1159
1382
|
* Re-emit all outputs from a node (used when graph updates)
|
|
1160
1383
|
* Only re-emits outputs that are valid according to resolved handles
|
|
1161
1384
|
*/
|
|
1162
|
-
|
|
1163
|
-
const node = this.
|
|
1385
|
+
invalidateDownstream(nodeId) {
|
|
1386
|
+
const node = this.graph.getNode(nodeId);
|
|
1164
1387
|
if (!node)
|
|
1165
1388
|
return;
|
|
1166
1389
|
// Get resolved handles to filter out invalid outputs
|
|
1167
|
-
const resolved = this.
|
|
1390
|
+
const resolved = this.graph.getResolvedHandles(nodeId);
|
|
1168
1391
|
const validOutputHandles = resolved?.outputs
|
|
1169
1392
|
? new Set(Object.keys(resolved.outputs))
|
|
1170
1393
|
: new Set();
|
|
1171
1394
|
// Use node's activeRunContexts to propagate to new nodes that were added
|
|
1172
|
-
const runContextIds = node.activeRunContexts.size > 0
|
|
1173
|
-
? new Set(node.activeRunContexts)
|
|
1174
|
-
: undefined;
|
|
1175
1395
|
for (const [handle, value] of Object.entries(node.outputs)) {
|
|
1176
1396
|
// Only re-emit if this handle is still valid
|
|
1177
1397
|
if (validOutputHandles.has(handle)) {
|
|
1178
|
-
this.propagate(nodeId, handle, value,
|
|
1398
|
+
this.propagate(nodeId, handle, value, node.activeRunContextIds);
|
|
1179
1399
|
}
|
|
1180
1400
|
}
|
|
1181
1401
|
}
|
|
@@ -1194,52 +1414,35 @@ class ValuePropagator {
|
|
|
1194
1414
|
}
|
|
1195
1415
|
|
|
1196
1416
|
/**
|
|
1197
|
-
*
|
|
1417
|
+
* NodeExecutor component - handles node execution scheduling and lifecycle
|
|
1198
1418
|
*/
|
|
1199
|
-
class
|
|
1200
|
-
constructor(
|
|
1201
|
-
this.
|
|
1202
|
-
this.graphStructure = graphStructure;
|
|
1419
|
+
class NodeExecutor {
|
|
1420
|
+
constructor(graph, eventEmitter, runContextManager, edgePropagator, runtime, environment) {
|
|
1421
|
+
this.graph = graph;
|
|
1203
1422
|
this.eventEmitter = eventEmitter;
|
|
1204
1423
|
this.runContextManager = runContextManager;
|
|
1205
|
-
this.
|
|
1206
|
-
this.
|
|
1424
|
+
this.edgePropagator = edgePropagator;
|
|
1425
|
+
this.runtime = runtime;
|
|
1426
|
+
this.environment = {};
|
|
1207
1427
|
this.environment = environment ?? {};
|
|
1208
1428
|
}
|
|
1209
1429
|
setEnvironment(environment) {
|
|
1210
1430
|
this.environment = environment;
|
|
1211
1431
|
}
|
|
1212
|
-
/**
|
|
1213
|
-
* Check if all inbound edges for a node have values
|
|
1214
|
-
*/
|
|
1215
|
-
allInboundHaveValue(nodeId) {
|
|
1216
|
-
const node = this.graphStructure.getNode(nodeId);
|
|
1217
|
-
if (!node)
|
|
1218
|
-
return false;
|
|
1219
|
-
const edges = this.graphStructure.getEdges();
|
|
1220
|
-
const inbound = edges.filter((e) => e.target.nodeId === nodeId);
|
|
1221
|
-
if (inbound.length === 0)
|
|
1222
|
-
return true;
|
|
1223
|
-
for (const e of inbound) {
|
|
1224
|
-
if (!(e.target.handle in node.inputs))
|
|
1225
|
-
return false;
|
|
1226
|
-
}
|
|
1227
|
-
return true;
|
|
1228
|
-
}
|
|
1229
1432
|
/**
|
|
1230
1433
|
* Compute effective inputs for a node by merging real inputs with defaults
|
|
1231
1434
|
*/
|
|
1232
1435
|
getEffectiveInputs(nodeId) {
|
|
1233
|
-
const node = this.
|
|
1436
|
+
const node = this.graph.getNode(nodeId);
|
|
1234
1437
|
if (!node)
|
|
1235
1438
|
return {};
|
|
1236
|
-
const registry = this.
|
|
1439
|
+
const registry = this.graph.getRegistry();
|
|
1237
1440
|
if (!registry)
|
|
1238
1441
|
return {};
|
|
1239
1442
|
const desc = registry.nodes.get(node.typeId);
|
|
1240
1443
|
if (!desc)
|
|
1241
1444
|
return {};
|
|
1242
|
-
const resolved = this.
|
|
1445
|
+
const resolved = this.graph.getResolvedHandles(nodeId);
|
|
1243
1446
|
const regDefaults = desc.inputDefaults ?? {};
|
|
1244
1447
|
const dynDefaults = resolved?.inputDefaults ?? {};
|
|
1245
1448
|
// Identify which handles are dynamically resolved (not in registry statics)
|
|
@@ -1253,7 +1456,7 @@ class ExecutionScheduler {
|
|
|
1253
1456
|
// Start with real inputs only (no defaults)
|
|
1254
1457
|
const effective = { ...node.inputs };
|
|
1255
1458
|
// Build set of inbound handles (wired inputs)
|
|
1256
|
-
const edges = this.
|
|
1459
|
+
const edges = this.graph.getEdges();
|
|
1257
1460
|
const inbound = new Set(edges
|
|
1258
1461
|
.filter((e) => e.target.nodeId === nodeId)
|
|
1259
1462
|
.map((e) => e.target.handle));
|
|
@@ -1280,7 +1483,7 @@ class ExecutionScheduler {
|
|
|
1280
1483
|
createExecutionContext(nodeId, node, inputs, runId, abortSignal, runContextIds, options) {
|
|
1281
1484
|
const emitHandler = options?.emitHandler ??
|
|
1282
1485
|
((handle, value) => {
|
|
1283
|
-
this.
|
|
1486
|
+
this.edgePropagator.propagate(nodeId, handle, value, runContextIds);
|
|
1284
1487
|
});
|
|
1285
1488
|
const reportProgress = options?.reportProgress ??
|
|
1286
1489
|
((p) => {
|
|
@@ -1315,20 +1518,24 @@ class ExecutionScheduler {
|
|
|
1315
1518
|
}
|
|
1316
1519
|
}
|
|
1317
1520
|
};
|
|
1318
|
-
// Store run-context IDs for use in scheduleInputsChanged
|
|
1319
|
-
const storedRunContextIds = runContextIds;
|
|
1320
1521
|
return {
|
|
1321
1522
|
nodeId,
|
|
1322
1523
|
state: node.state,
|
|
1323
1524
|
setState: (next) => Object.assign(node.state, next),
|
|
1324
1525
|
emit: emitHandler,
|
|
1325
1526
|
invalidateDownstream: () => {
|
|
1326
|
-
this.
|
|
1527
|
+
this.edgePropagator.invalidateDownstream(nodeId);
|
|
1327
1528
|
},
|
|
1328
|
-
|
|
1329
|
-
if (this.allInboundHaveValue(nodeId)) {
|
|
1330
|
-
|
|
1331
|
-
this.
|
|
1529
|
+
execute: (resolve, opts) => {
|
|
1530
|
+
if (this.graph.allInboundHaveValue(nodeId)) {
|
|
1531
|
+
let runContextIdsToUse = this.runtime.getRunMode() === "auto" ? undefined : runContextIds;
|
|
1532
|
+
if (this.runtime.getRunMode() === "manual" &&
|
|
1533
|
+
(!runContextIds || runContextIds.size === 0)) {
|
|
1534
|
+
runContextIdsToUse = new Set([
|
|
1535
|
+
this.runContextManager.createRunContext(nodeId, resolve, opts),
|
|
1536
|
+
]);
|
|
1537
|
+
}
|
|
1538
|
+
this.execute(nodeId, runContextIdsToUse);
|
|
1332
1539
|
}
|
|
1333
1540
|
},
|
|
1334
1541
|
getInput: (handle) => inputs[handle],
|
|
@@ -1340,233 +1547,345 @@ class ExecutionScheduler {
|
|
|
1340
1547
|
};
|
|
1341
1548
|
}
|
|
1342
1549
|
/**
|
|
1343
|
-
*
|
|
1550
|
+
* Internal method for executing inputs changed (also used by GraphRuntime)
|
|
1551
|
+
*/
|
|
1552
|
+
execute(nodeId, runContextIds) {
|
|
1553
|
+
const node = this.graph.getNode(nodeId);
|
|
1554
|
+
if (!node)
|
|
1555
|
+
return;
|
|
1556
|
+
const runMode = this.runtime.getRunMode();
|
|
1557
|
+
if (!runMode) {
|
|
1558
|
+
console.warn("NodeExecutor.execute: no runMode, skipping execution");
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
if (runMode === "manual" && (!runContextIds || runContextIds.size === 0)) {
|
|
1562
|
+
console.warn("NodeExecutor.execute: no runContextIds provided in manual mode, skipping execution");
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
if (runMode === "auto" && runContextIds && runContextIds.size > 0) {
|
|
1566
|
+
console.warn("NodeExecutor.execute: runContextIds provided in auto mode, ignoring");
|
|
1567
|
+
runContextIds = undefined;
|
|
1568
|
+
}
|
|
1569
|
+
// Early validation for auto-mode paused state
|
|
1570
|
+
if (this.runtime.isPaused())
|
|
1571
|
+
return;
|
|
1572
|
+
// Attach run-context IDs if provided
|
|
1573
|
+
this.attachRunContexts(node, runContextIds);
|
|
1574
|
+
// Handle debouncing
|
|
1575
|
+
const now = Date.now();
|
|
1576
|
+
if (this.shouldDebounce(node, now)) {
|
|
1577
|
+
this.handleDebouncedSchedule(node, nodeId, now);
|
|
1578
|
+
return;
|
|
1579
|
+
}
|
|
1580
|
+
// Prepare execution plan
|
|
1581
|
+
const executionPlan = this.prepareExecutionPlan(node, nodeId, runContextIds, now);
|
|
1582
|
+
// Route to appropriate concurrency handler
|
|
1583
|
+
this.routeToConcurrencyHandler(node, nodeId, executionPlan);
|
|
1584
|
+
}
|
|
1585
|
+
/**
|
|
1586
|
+
* Attach run-context IDs to the node
|
|
1587
|
+
*/
|
|
1588
|
+
attachRunContexts(node, runContextIds) {
|
|
1589
|
+
if (!runContextIds)
|
|
1590
|
+
return;
|
|
1591
|
+
node.activeRunContextIds = new Set([
|
|
1592
|
+
...node.activeRunContextIds,
|
|
1593
|
+
...runContextIds,
|
|
1594
|
+
]);
|
|
1595
|
+
}
|
|
1596
|
+
/**
|
|
1597
|
+
* Check if execution should be debounced
|
|
1598
|
+
*/
|
|
1599
|
+
shouldDebounce(node, now) {
|
|
1600
|
+
const policy = node.policy ?? {};
|
|
1601
|
+
return !!(policy.debounceMs &&
|
|
1602
|
+
node.lastScheduledAt &&
|
|
1603
|
+
now - node.lastScheduledAt < policy.debounceMs);
|
|
1604
|
+
}
|
|
1605
|
+
/**
|
|
1606
|
+
* Handle debounced scheduling by replacing the latest queued item
|
|
1607
|
+
*/
|
|
1608
|
+
handleDebouncedSchedule(node, nodeId, now) {
|
|
1609
|
+
const effectiveInputs = this.getEffectiveInputs(nodeId);
|
|
1610
|
+
node.queue.splice(0, node.queue.length);
|
|
1611
|
+
node.runSeq += 1;
|
|
1612
|
+
const rid = `${nodeId}:${node.runSeq}:${now}`;
|
|
1613
|
+
node.queue.push({ runId: rid, inputs: effectiveInputs });
|
|
1614
|
+
}
|
|
1615
|
+
/**
|
|
1616
|
+
* Prepare execution plan with all necessary information
|
|
1617
|
+
*/
|
|
1618
|
+
prepareExecutionPlan(node, nodeId, runContextIds, now) {
|
|
1619
|
+
node.lastScheduledAt = now;
|
|
1620
|
+
node.runSeq += 1;
|
|
1621
|
+
const runId = `${nodeId}:${node.runSeq}:${now}`;
|
|
1622
|
+
node.latestRunId = runId;
|
|
1623
|
+
const effectiveInputs = this.getEffectiveInputs(nodeId);
|
|
1624
|
+
// Take a shallow snapshot of the current policy for this run
|
|
1625
|
+
const policySnapshot = node.policy ? { ...node.policy } : undefined;
|
|
1626
|
+
return {
|
|
1627
|
+
runId,
|
|
1628
|
+
effectiveInputs,
|
|
1629
|
+
runContextIdsForRun: runContextIds,
|
|
1630
|
+
timestamp: now,
|
|
1631
|
+
policy: policySnapshot,
|
|
1632
|
+
};
|
|
1633
|
+
}
|
|
1634
|
+
/**
|
|
1635
|
+
* Route execution to appropriate concurrency handler
|
|
1636
|
+
*/
|
|
1637
|
+
routeToConcurrencyHandler(node, nodeId, plan) {
|
|
1638
|
+
const mode = plan.policy?.asyncConcurrency ?? "switch";
|
|
1639
|
+
switch (mode) {
|
|
1640
|
+
case "drop":
|
|
1641
|
+
this.handleDropMode(node, nodeId, plan);
|
|
1642
|
+
break;
|
|
1643
|
+
case "queue":
|
|
1644
|
+
this.handleQueueMode(node, nodeId, plan);
|
|
1645
|
+
break;
|
|
1646
|
+
case "switch":
|
|
1647
|
+
case "merge":
|
|
1648
|
+
default:
|
|
1649
|
+
this.startRun(node, nodeId, plan);
|
|
1650
|
+
break;
|
|
1651
|
+
}
|
|
1652
|
+
}
|
|
1653
|
+
/**
|
|
1654
|
+
* Handle drop mode - drop execution if node is already running, otherwise start run
|
|
1655
|
+
*/
|
|
1656
|
+
handleDropMode(node, nodeId, plan) {
|
|
1657
|
+
// Drop if node is already running
|
|
1658
|
+
if (node.activeControllers.size > 0) {
|
|
1659
|
+
return; // Don't increment pendingCount if we're dropping this run
|
|
1660
|
+
}
|
|
1661
|
+
// Start run if node is not running
|
|
1662
|
+
this.startRun(node, nodeId, plan);
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
* Handle queue mode - add to queue and process sequentially
|
|
1666
|
+
*/
|
|
1667
|
+
handleQueueMode(node, nodeId, plan) {
|
|
1668
|
+
const maxQ = plan.policy?.maxQueue ?? 8;
|
|
1669
|
+
node.queue.push({ runId: plan.runId, inputs: plan.effectiveInputs });
|
|
1670
|
+
if (node.queue.length > maxQ)
|
|
1671
|
+
node.queue.shift();
|
|
1672
|
+
this.processQueue(node, nodeId);
|
|
1673
|
+
}
|
|
1674
|
+
/**
|
|
1675
|
+
* Process queued executions sequentially
|
|
1676
|
+
*/
|
|
1677
|
+
processQueue(node, nodeId) {
|
|
1678
|
+
const processNext = () => {
|
|
1679
|
+
if (node.activeControllers.size > 0)
|
|
1680
|
+
return;
|
|
1681
|
+
const next = node.queue.shift();
|
|
1682
|
+
if (!next)
|
|
1683
|
+
return;
|
|
1684
|
+
node.latestRunId = next.runId;
|
|
1685
|
+
const policySnapshot = node.policy ? { ...node.policy } : undefined;
|
|
1686
|
+
const plan = {
|
|
1687
|
+
runId: next.runId,
|
|
1688
|
+
effectiveInputs: next.inputs,
|
|
1689
|
+
runContextIdsForRun: node.activeRunContextIds,
|
|
1690
|
+
timestamp: Date.now(),
|
|
1691
|
+
policy: policySnapshot,
|
|
1692
|
+
};
|
|
1693
|
+
this.startRun(node, nodeId, plan, () => {
|
|
1694
|
+
setTimeout(processNext, 0);
|
|
1695
|
+
});
|
|
1696
|
+
};
|
|
1697
|
+
processNext();
|
|
1698
|
+
}
|
|
1699
|
+
/**
|
|
1700
|
+
* Start a node execution run
|
|
1344
1701
|
*/
|
|
1345
|
-
|
|
1346
|
-
|
|
1702
|
+
startRun(node, nodeId, plan, onDone) {
|
|
1703
|
+
// Track run-contexts
|
|
1704
|
+
this.trackRunContextStart(nodeId, plan.runContextIdsForRun);
|
|
1705
|
+
// Setup execution controller
|
|
1706
|
+
const controller = this.createExecutionController(node, plan.runId);
|
|
1707
|
+
// Handle concurrency mode
|
|
1708
|
+
this.applyConcurrencyMode(node, controller, plan);
|
|
1709
|
+
// Setup timeout if needed
|
|
1710
|
+
const timeoutId = this.setupTimeout(node, controller, plan);
|
|
1711
|
+
// Create execution context
|
|
1712
|
+
const ctx = this.createExecutionContext(nodeId, node, plan.effectiveInputs, plan.runId, controller.signal, plan.runContextIdsForRun, this.createEmitAndProgressHandlers(node, nodeId, plan));
|
|
1713
|
+
// Execute
|
|
1714
|
+
this.executeNode(node, nodeId, ctx, plan, controller, timeoutId, onDone);
|
|
1347
1715
|
}
|
|
1348
1716
|
/**
|
|
1349
|
-
*
|
|
1717
|
+
* Track run-context start for pending nodes
|
|
1350
1718
|
*/
|
|
1351
|
-
|
|
1352
|
-
|
|
1719
|
+
trackRunContextStart(nodeId, runContextIdsForRun) {
|
|
1720
|
+
if (runContextIdsForRun && runContextIdsForRun.size > 0) {
|
|
1721
|
+
for (const id of runContextIdsForRun) {
|
|
1722
|
+
this.runContextManager.startNodeRun(id, nodeId);
|
|
1723
|
+
}
|
|
1724
|
+
}
|
|
1353
1725
|
}
|
|
1354
1726
|
/**
|
|
1355
|
-
*
|
|
1727
|
+
* Create execution controller and update node stats
|
|
1356
1728
|
*/
|
|
1357
|
-
|
|
1358
|
-
const
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1729
|
+
createExecutionController(node, runId) {
|
|
1730
|
+
const controller = new AbortController();
|
|
1731
|
+
node.stats.runs += 1;
|
|
1732
|
+
node.stats.active += 1;
|
|
1733
|
+
node.stats.lastStartAt = Date.now();
|
|
1734
|
+
node.stats.progress = 0;
|
|
1735
|
+
node.activeControllers.add(controller);
|
|
1736
|
+
node.controllerRunIds.set(controller, runId);
|
|
1737
|
+
return controller;
|
|
1738
|
+
}
|
|
1739
|
+
/**
|
|
1740
|
+
* Apply concurrency mode (switch mode aborts other controllers)
|
|
1741
|
+
*/
|
|
1742
|
+
applyConcurrencyMode(node, controller, plan) {
|
|
1743
|
+
const mode = plan.policy?.asyncConcurrency ?? "switch";
|
|
1744
|
+
if (mode === "switch") {
|
|
1745
|
+
for (const c of Array.from(node.activeControllers)) {
|
|
1746
|
+
if (c !== controller)
|
|
1747
|
+
c.abort("switch");
|
|
1368
1748
|
}
|
|
1369
1749
|
}
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
node.queue.splice(0, node.queue.length);
|
|
1379
|
-
node.runSeq += 1;
|
|
1380
|
-
const rid = `${nodeId}:${node.runSeq}:${now}`;
|
|
1381
|
-
node.queue.push({ runId: rid, inputs: effectiveInputs });
|
|
1382
|
-
return;
|
|
1750
|
+
}
|
|
1751
|
+
/**
|
|
1752
|
+
* Setup timeout for execution if configured
|
|
1753
|
+
*/
|
|
1754
|
+
setupTimeout(node, controller, plan) {
|
|
1755
|
+
const policy = plan.policy ?? {};
|
|
1756
|
+
if (policy.timeoutMs && policy.timeoutMs > 0) {
|
|
1757
|
+
return setTimeout(() => controller.abort("timeout"), policy.timeoutMs);
|
|
1383
1758
|
}
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
}
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1759
|
+
return undefined;
|
|
1760
|
+
}
|
|
1761
|
+
/**
|
|
1762
|
+
* Create emit and progress handlers for execution context
|
|
1763
|
+
*/
|
|
1764
|
+
createEmitAndProgressHandlers(node, nodeId, plan) {
|
|
1765
|
+
const policy = plan.policy ?? {};
|
|
1766
|
+
return {
|
|
1767
|
+
emitHandler: (handle, value) => {
|
|
1768
|
+
const m = policy.asyncConcurrency ?? "switch";
|
|
1769
|
+
// Drop emits from runs that were explicitly cancelled due to a
|
|
1770
|
+
// snapshot/undo/redo operation, regardless of asyncConcurrency.
|
|
1771
|
+
if (node.snapshotCancelledRunIds?.has(plan.runId))
|
|
1772
|
+
return;
|
|
1773
|
+
if (m !== "merge" && plan.runId !== node.latestRunId)
|
|
1774
|
+
return;
|
|
1775
|
+
this.edgePropagator.propagate(nodeId, handle, value, plan.runContextIdsForRun);
|
|
1776
|
+
},
|
|
1777
|
+
reportProgress: (p) => {
|
|
1778
|
+
node.stats.progress = Math.max(0, Math.min(1, Number(p) || 0));
|
|
1779
|
+
this.eventEmitter.emit("stats", {
|
|
1780
|
+
kind: "node-progress",
|
|
1781
|
+
nodeId,
|
|
1782
|
+
typeId: node.typeId,
|
|
1783
|
+
runId: plan.runId,
|
|
1784
|
+
progress: node.stats.progress,
|
|
1785
|
+
});
|
|
1786
|
+
},
|
|
1787
|
+
};
|
|
1788
|
+
}
|
|
1789
|
+
/**
|
|
1790
|
+
* Execute the node with retry logic and cleanup
|
|
1791
|
+
*/
|
|
1792
|
+
executeNode(node, nodeId, ctx, plan, controller, timeoutId, onDone) {
|
|
1793
|
+
// Fire node-start event
|
|
1794
|
+
this.eventEmitter.emit("stats", {
|
|
1795
|
+
kind: "node-start",
|
|
1796
|
+
nodeId,
|
|
1797
|
+
typeId: node.typeId,
|
|
1798
|
+
runId: plan.runId,
|
|
1799
|
+
});
|
|
1800
|
+
ctx.log("debug", "node-start");
|
|
1801
|
+
const exec = async (attempt) => {
|
|
1802
|
+
let hadError = false;
|
|
1803
|
+
try {
|
|
1804
|
+
if (node.lifecycle?.prepare) {
|
|
1805
|
+
ctx.log("debug", "prepare-start");
|
|
1806
|
+
node.lifecycle.prepare(node.params ?? {}, ctx);
|
|
1807
|
+
ctx.log("debug", "prepare-done");
|
|
1414
1808
|
}
|
|
1809
|
+
await node.runtime.onInputsChanged?.(plan.effectiveInputs, ctx);
|
|
1415
1810
|
}
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
if (node.snapshotCancelledRunIds?.has(runId))
|
|
1426
|
-
return;
|
|
1427
|
-
if (m !== "merge" && runId !== node.latestRunId)
|
|
1428
|
-
return;
|
|
1429
|
-
this.valuePropagator.propagate(nodeId, handle, value, runContextIdsForRun);
|
|
1430
|
-
},
|
|
1431
|
-
reportProgress: (p) => {
|
|
1432
|
-
node.stats.progress = Math.max(0, Math.min(1, Number(p) || 0));
|
|
1433
|
-
this.eventEmitter.emit("stats", {
|
|
1434
|
-
kind: "node-progress",
|
|
1435
|
-
nodeId,
|
|
1436
|
-
typeId: node.typeId,
|
|
1437
|
-
runId,
|
|
1438
|
-
progress: node.stats.progress,
|
|
1439
|
-
});
|
|
1440
|
-
},
|
|
1441
|
-
});
|
|
1442
|
-
const exec = async (attempt) => {
|
|
1443
|
-
let hadError = false;
|
|
1444
|
-
try {
|
|
1445
|
-
if (node.lifecycle?.prepare) {
|
|
1446
|
-
ctx.log("debug", "prepare-start");
|
|
1447
|
-
node.lifecycle.prepare(node.params ?? {}, ctx);
|
|
1448
|
-
ctx.log("debug", "prepare-done");
|
|
1449
|
-
}
|
|
1450
|
-
await node.runtime.onInputsChanged?.(capturedInputs, ctx);
|
|
1451
|
-
}
|
|
1452
|
-
catch (err) {
|
|
1453
|
-
// Suppress errors caused by expected cancellations
|
|
1454
|
-
if (controller.signal.aborted) {
|
|
1455
|
-
const reason = controller.signal.reason;
|
|
1456
|
-
if (reason === "switch" ||
|
|
1457
|
-
reason === "snapshot" ||
|
|
1458
|
-
reason === "node-deleted" ||
|
|
1459
|
-
reason === "user-cancelled") {
|
|
1460
|
-
return; // Cancellation events are emitted separately, skip error handling
|
|
1461
|
-
}
|
|
1462
|
-
}
|
|
1463
|
-
hadError = true;
|
|
1464
|
-
node.stats.lastError = err;
|
|
1465
|
-
const retry = policy.retry;
|
|
1466
|
-
if (retry && attempt < (retry.attempts ?? 0)) {
|
|
1467
|
-
const delay = retry.backoffMs ? retry.backoffMs(attempt) : 0;
|
|
1468
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
1469
|
-
return exec(attempt + 1);
|
|
1811
|
+
catch (err) {
|
|
1812
|
+
// Suppress errors caused by expected cancellations
|
|
1813
|
+
if (controller.signal.aborted) {
|
|
1814
|
+
const reason = controller.signal.reason;
|
|
1815
|
+
if (reason === "switch" ||
|
|
1816
|
+
reason === "snapshot" ||
|
|
1817
|
+
reason === "node-deleted" ||
|
|
1818
|
+
reason === "user-cancelled") {
|
|
1819
|
+
return; // Cancellation events are emitted separately, skip error handling
|
|
1470
1820
|
}
|
|
1471
|
-
this.eventEmitter.emit("error", {
|
|
1472
|
-
kind: "node-run",
|
|
1473
|
-
nodeId,
|
|
1474
|
-
runId,
|
|
1475
|
-
err,
|
|
1476
|
-
});
|
|
1477
1821
|
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
node.activeControllers.delete(controller);
|
|
1486
|
-
node.controllerRunIds.delete(controller);
|
|
1487
|
-
node.stats.active = Math.max(0, node.activeControllers.size);
|
|
1488
|
-
node.stats.lastEndAt = Date.now();
|
|
1489
|
-
node.stats.lastDurationMs =
|
|
1490
|
-
node.stats.lastStartAt && node.stats.lastEndAt
|
|
1491
|
-
? node.stats.lastEndAt - node.stats.lastStartAt
|
|
1492
|
-
: undefined;
|
|
1493
|
-
if (!hadError)
|
|
1494
|
-
node.stats.lastError = undefined;
|
|
1495
|
-
// Only emit node-done if not cancelled (cancellation events emitted separately)
|
|
1496
|
-
const isCancelled = controller.signal.aborted &&
|
|
1497
|
-
(controller.signal.reason === "snapshot" ||
|
|
1498
|
-
controller.signal.reason === "node-deleted" ||
|
|
1499
|
-
controller.signal.reason === "user-cancelled");
|
|
1500
|
-
if (!isCancelled) {
|
|
1501
|
-
this.eventEmitter.emit("stats", {
|
|
1502
|
-
kind: "node-done",
|
|
1503
|
-
nodeId,
|
|
1504
|
-
typeId: node.typeId,
|
|
1505
|
-
runId,
|
|
1506
|
-
durationMs: node.stats.lastDurationMs,
|
|
1507
|
-
});
|
|
1508
|
-
}
|
|
1509
|
-
ctx.log("debug", "node-done", {
|
|
1510
|
-
durationMs: node.stats.lastDurationMs,
|
|
1511
|
-
hadError,
|
|
1512
|
-
});
|
|
1513
|
-
// Decrement pending count for all relevant run-contexts
|
|
1514
|
-
if (runContextIdsForRun && runContextIdsForRun.size > 0) {
|
|
1515
|
-
for (const id of runContextIdsForRun) {
|
|
1516
|
-
const ctx = this.runContextManager.getRunContext(id);
|
|
1517
|
-
if (ctx) {
|
|
1518
|
-
ctx.pending--;
|
|
1519
|
-
if (ctx.pending === 0) {
|
|
1520
|
-
this.runContextManager.finishRunContext(id, this.graphStructure.getNodes());
|
|
1521
|
-
}
|
|
1522
|
-
}
|
|
1523
|
-
}
|
|
1524
|
-
}
|
|
1525
|
-
if (onDone)
|
|
1526
|
-
onDone();
|
|
1822
|
+
hadError = true;
|
|
1823
|
+
node.stats.lastError = err;
|
|
1824
|
+
const retry = plan.policy?.retry;
|
|
1825
|
+
if (retry && attempt < (retry.attempts ?? 0)) {
|
|
1826
|
+
const delay = retry.backoffMs ? retry.backoffMs(attempt) : 0;
|
|
1827
|
+
await new Promise((r) => setTimeout(r, delay));
|
|
1828
|
+
return exec(attempt + 1);
|
|
1527
1829
|
}
|
|
1528
|
-
|
|
1529
|
-
|
|
1830
|
+
this.eventEmitter.emit("error", {
|
|
1831
|
+
kind: "node-run",
|
|
1832
|
+
nodeId,
|
|
1833
|
+
runId: plan.runId,
|
|
1834
|
+
err,
|
|
1835
|
+
});
|
|
1836
|
+
}
|
|
1837
|
+
finally {
|
|
1838
|
+
this.cleanupExecution(node, nodeId, ctx, plan, controller, timeoutId, hadError, onDone);
|
|
1839
|
+
}
|
|
1840
|
+
};
|
|
1841
|
+
exec(0);
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Cleanup after execution completes
|
|
1845
|
+
*/
|
|
1846
|
+
cleanupExecution(node, nodeId, ctx, plan, controller, timeoutId, hadError, onDone) {
|
|
1847
|
+
// Decrement pendingNodes count for all relevant run-contexts
|
|
1848
|
+
if (plan.runContextIdsForRun && plan.runContextIdsForRun.size > 0) {
|
|
1849
|
+
for (const id of plan.runContextIdsForRun) {
|
|
1850
|
+
this.runContextManager.finishNodeRun(id, nodeId);
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
// Skip cleanup if node was deleted (cleanup already handled)
|
|
1854
|
+
if (!this.graph.hasNode(nodeId)) {
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
if (timeoutId)
|
|
1858
|
+
clearTimeout(timeoutId);
|
|
1859
|
+
node.activeControllers.delete(controller);
|
|
1860
|
+
node.controllerRunIds.delete(controller);
|
|
1861
|
+
node.stats.active = Math.max(0, node.activeControllers.size);
|
|
1862
|
+
node.stats.lastEndAt = Date.now();
|
|
1863
|
+
node.stats.lastDurationMs =
|
|
1864
|
+
node.stats.lastStartAt && node.stats.lastEndAt
|
|
1865
|
+
? node.stats.lastEndAt - node.stats.lastStartAt
|
|
1866
|
+
: undefined;
|
|
1867
|
+
if (!hadError)
|
|
1868
|
+
node.stats.lastError = undefined;
|
|
1869
|
+
// Only emit node-done if not cancelled (cancellation events emitted separately)
|
|
1870
|
+
const isCancelled = controller.signal.aborted &&
|
|
1871
|
+
(controller.signal.reason === "snapshot" ||
|
|
1872
|
+
controller.signal.reason === "node-deleted" ||
|
|
1873
|
+
controller.signal.reason === "user-cancelled");
|
|
1874
|
+
if (!isCancelled) {
|
|
1530
1875
|
this.eventEmitter.emit("stats", {
|
|
1531
|
-
kind: "node-
|
|
1876
|
+
kind: "node-done",
|
|
1532
1877
|
nodeId,
|
|
1533
1878
|
typeId: node.typeId,
|
|
1534
|
-
runId,
|
|
1879
|
+
runId: plan.runId,
|
|
1880
|
+
durationMs: node.stats.lastDurationMs,
|
|
1535
1881
|
});
|
|
1536
|
-
ctx.log("debug", "node-start");
|
|
1537
|
-
exec(0);
|
|
1538
|
-
};
|
|
1539
|
-
const mode = policy.asyncConcurrency ?? "switch";
|
|
1540
|
-
if (mode === "drop" && node.activeControllers.size > 0) {
|
|
1541
|
-
// Don't increment pendingCount if we're dropping this run
|
|
1542
|
-
return;
|
|
1543
|
-
}
|
|
1544
|
-
if (mode === "queue") {
|
|
1545
|
-
const maxQ = policy.maxQueue ?? 8;
|
|
1546
|
-
node.queue.push({ runId: rid, inputs: effectiveInputs });
|
|
1547
|
-
if (node.queue.length > maxQ)
|
|
1548
|
-
node.queue.shift();
|
|
1549
|
-
const processNext = () => {
|
|
1550
|
-
if (node.activeControllers.size > 0)
|
|
1551
|
-
return;
|
|
1552
|
-
const next = node.queue.shift();
|
|
1553
|
-
if (!next)
|
|
1554
|
-
return;
|
|
1555
|
-
node.latestRunId = next.runId;
|
|
1556
|
-
// Use node's activeRunContexts for queued items
|
|
1557
|
-
const queuedRunContextIds = node.activeRunContexts.size > 0
|
|
1558
|
-
? new Set(node.activeRunContexts)
|
|
1559
|
-
: undefined;
|
|
1560
|
-
startRun(next.runId, next.inputs, queuedRunContextIds, () => {
|
|
1561
|
-
// After finishing, schedule next
|
|
1562
|
-
setTimeout(processNext, 0);
|
|
1563
|
-
});
|
|
1564
|
-
};
|
|
1565
|
-
processNext();
|
|
1566
|
-
return;
|
|
1567
1882
|
}
|
|
1568
|
-
|
|
1569
|
-
|
|
1883
|
+
ctx.log("debug", "node-done", {
|
|
1884
|
+
durationMs: node.stats.lastDurationMs,
|
|
1885
|
+
hadError,
|
|
1886
|
+
});
|
|
1887
|
+
if (onDone)
|
|
1888
|
+
onDone();
|
|
1570
1889
|
}
|
|
1571
1890
|
/**
|
|
1572
1891
|
* Cancel all active runs for a node
|
|
@@ -1614,7 +1933,7 @@ class ExecutionScheduler {
|
|
|
1614
1933
|
const toCancel = new Set(nodeIds);
|
|
1615
1934
|
const visited = new Set();
|
|
1616
1935
|
const queue = [...nodeIds];
|
|
1617
|
-
const edges = this.
|
|
1936
|
+
const edges = this.graph.getEdges();
|
|
1618
1937
|
// Collect all downstream nodes to cancel
|
|
1619
1938
|
for (let i = 0; i < queue.length; i++) {
|
|
1620
1939
|
const nodeId = queue[i];
|
|
@@ -1633,7 +1952,7 @@ class ExecutionScheduler {
|
|
|
1633
1952
|
}
|
|
1634
1953
|
// Cancel runs for all affected nodes
|
|
1635
1954
|
for (const nodeId of toCancel) {
|
|
1636
|
-
const node = this.
|
|
1955
|
+
const node = this.graph.getNode(nodeId);
|
|
1637
1956
|
if (!node)
|
|
1638
1957
|
continue;
|
|
1639
1958
|
this.cancelNodeActiveRuns(node, reason);
|
|
@@ -1644,8 +1963,8 @@ class ExecutionScheduler {
|
|
|
1644
1963
|
}
|
|
1645
1964
|
// Cancel nodes in run-contexts (exclude them from active run-contexts)
|
|
1646
1965
|
for (const nodeId of toCancel) {
|
|
1647
|
-
|
|
1648
|
-
|
|
1966
|
+
// includeDownstream = false (already collected above)
|
|
1967
|
+
this.runContextManager.cancelNodeInRunContexts(nodeId, false);
|
|
1649
1968
|
}
|
|
1650
1969
|
}
|
|
1651
1970
|
}
|
|
@@ -1654,47 +1973,30 @@ class ExecutionScheduler {
|
|
|
1654
1973
|
class GraphRuntime {
|
|
1655
1974
|
constructor() {
|
|
1656
1975
|
// State
|
|
1657
|
-
this.paused = false;
|
|
1658
1976
|
this.environment = {};
|
|
1659
1977
|
this.runMode = null;
|
|
1660
1978
|
this.pauseRefCount = 0;
|
|
1661
1979
|
// Initialize components
|
|
1662
|
-
this.
|
|
1980
|
+
this.graph = new Graph();
|
|
1663
1981
|
this.eventEmitter = new EventEmitter();
|
|
1664
|
-
this.runContextManager = new RunContextManager();
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
undefined // environment set later
|
|
1670
|
-
);
|
|
1671
|
-
// ValuePropagator needs IHandleResolver and IRuntimeCoordinator
|
|
1672
|
-
// ExecutionScheduler needs IValuePropagator - circular dependency!
|
|
1673
|
-
// Solution: Create ValuePropagator first (without executionScheduler),
|
|
1674
|
-
// then create ExecutionScheduler with ValuePropagator,
|
|
1675
|
-
// then wire ValuePropagator with ExecutionScheduler via setExecutionScheduler
|
|
1676
|
-
this.valuePropagator = new ValuePropagator(this.graphStructure, this.eventEmitter, this.runContextManager, this.handleResolver, // IHandleResolver
|
|
1677
|
-
this // IRuntimeCoordinator
|
|
1678
|
-
);
|
|
1679
|
-
// Create ExecutionScheduler with ValuePropagator
|
|
1680
|
-
this.executionScheduler = new ExecutionScheduler(this.graphStructure, this.eventEmitter, this.runContextManager, this.valuePropagator, // IValuePropagator
|
|
1681
|
-
this, // IRuntimeCoordinator
|
|
1682
|
-
this.environment);
|
|
1683
|
-
// Wire ValuePropagator with ExecutionScheduler to resolve circular dependency
|
|
1684
|
-
this.valuePropagator.setExecutionScheduler(this.executionScheduler);
|
|
1982
|
+
this.runContextManager = new RunContextManager(this.graph);
|
|
1983
|
+
this.handleResolver = new HandleResolver(this.graph, this.eventEmitter, this.runContextManager, this);
|
|
1984
|
+
this.edgePropagator = new EdgePropagator(this.graph, this.eventEmitter, this.runContextManager, this.handleResolver, this);
|
|
1985
|
+
// Create NodeExecutor with EdgePropagator
|
|
1986
|
+
this.nodeExecutor = new NodeExecutor(this.graph, this.eventEmitter, this.runContextManager, this, this);
|
|
1685
1987
|
}
|
|
1686
1988
|
static create(def, registry, opts) {
|
|
1687
1989
|
const gr = new GraphRuntime();
|
|
1688
1990
|
gr.environment = opts?.environment ?? {};
|
|
1689
1991
|
// Set registry and environment on components
|
|
1690
|
-
gr.
|
|
1992
|
+
gr.graph.setRegistry(registry);
|
|
1691
1993
|
gr.handleResolver.setRegistry(registry);
|
|
1692
1994
|
gr.handleResolver.setEnvironment(gr.environment);
|
|
1693
|
-
gr.
|
|
1995
|
+
gr.nodeExecutor.setEnvironment(gr.environment);
|
|
1694
1996
|
// Precompute per-node resolved handles (use def-provided overrides; do not compute dynamically here)
|
|
1695
|
-
const initial =
|
|
1696
|
-
for (const [nodeId, handles] of initial.
|
|
1697
|
-
gr.
|
|
1997
|
+
const initial = tryHandleResolving(def, registry, gr.environment);
|
|
1998
|
+
for (const [nodeId, handles] of initial.resolved) {
|
|
1999
|
+
gr.graph.setResolvedHandles(nodeId, handles);
|
|
1698
2000
|
}
|
|
1699
2001
|
// Instantiate nodes
|
|
1700
2002
|
for (const n of def.nodes) {
|
|
@@ -1735,13 +2037,13 @@ class GraphRuntime {
|
|
|
1735
2037
|
queued: 0,
|
|
1736
2038
|
progress: 0,
|
|
1737
2039
|
},
|
|
1738
|
-
|
|
2040
|
+
activeRunContextIds: new Set(),
|
|
1739
2041
|
};
|
|
1740
|
-
gr.
|
|
2042
|
+
gr.graph.setNode(n.nodeId, rn);
|
|
1741
2043
|
}
|
|
1742
2044
|
// Instantiate edges
|
|
1743
|
-
const edges =
|
|
1744
|
-
gr.
|
|
2045
|
+
const edges = buildEdges(def, registry, gr.graph.getResolvedHandlesMap());
|
|
2046
|
+
gr.graph.setEdges(edges);
|
|
1745
2047
|
// Schedule async recompute only for nodes that indicated Promise-based resolveHandles
|
|
1746
2048
|
for (const nodeId of initial.pending) {
|
|
1747
2049
|
gr.handleResolver.scheduleRecomputeHandles(nodeId);
|
|
@@ -1752,12 +2054,12 @@ class GraphRuntime {
|
|
|
1752
2054
|
return this.eventEmitter.on(event, handler);
|
|
1753
2055
|
}
|
|
1754
2056
|
setInputs(nodeId, inputs) {
|
|
1755
|
-
const node = this.
|
|
2057
|
+
const node = this.graph.getNode(nodeId);
|
|
1756
2058
|
if (!node)
|
|
1757
2059
|
throw new Error(`Node not found: ${nodeId}`);
|
|
1758
2060
|
let anyChanged = false;
|
|
1759
|
-
const edges = this.
|
|
1760
|
-
const registry = this.
|
|
2061
|
+
const edges = this.graph.getEdges();
|
|
2062
|
+
const registry = this.graph.getRegistry();
|
|
1761
2063
|
for (const [handle, value] of Object.entries(inputs)) {
|
|
1762
2064
|
const hasInbound = edges.some((e) => e.target.nodeId === nodeId && e.target.handle === handle);
|
|
1763
2065
|
if (hasInbound)
|
|
@@ -1765,7 +2067,7 @@ class GraphRuntime {
|
|
|
1765
2067
|
// Validate input value against declared type
|
|
1766
2068
|
if (value !== undefined && registry) {
|
|
1767
2069
|
const desc = registry.nodes.get(node.typeId);
|
|
1768
|
-
const resolved = this.
|
|
2070
|
+
const resolved = this.graph.getResolvedHandles(nodeId);
|
|
1769
2071
|
// Get typeId from resolved handles first, then registry statics
|
|
1770
2072
|
const typeId = resolved
|
|
1771
2073
|
? getInputTypeId(resolved.inputs, handle)
|
|
@@ -1805,93 +2107,22 @@ class GraphRuntime {
|
|
|
1805
2107
|
}
|
|
1806
2108
|
}
|
|
1807
2109
|
// In auto mode, input updates can trigger execution; in manual mode they never should.
|
|
1808
|
-
const isAutoMode = this.runMode === "auto" || this.runMode === null;
|
|
1809
|
-
const canAutoSchedule = !this.paused && isAutoMode;
|
|
1810
|
-
if (canAutoSchedule) {
|
|
1811
|
-
if (anyChanged && this.executionScheduler.allInboundHaveValue(nodeId)) {
|
|
1812
|
-
this.executionScheduler.scheduleInputsChangedInternal(nodeId);
|
|
1813
|
-
}
|
|
1814
|
-
}
|
|
1815
2110
|
if (anyChanged) {
|
|
1816
2111
|
this.handleResolver.scheduleRecomputeHandles(nodeId);
|
|
2112
|
+
if (this.runMode === "auto" && this.graph.allInboundHaveValue(nodeId)) {
|
|
2113
|
+
this.execute(nodeId);
|
|
2114
|
+
}
|
|
1817
2115
|
}
|
|
1818
2116
|
}
|
|
1819
2117
|
getOutput(nodeId, output) {
|
|
1820
|
-
const node = this.
|
|
2118
|
+
const node = this.graph.getNode(nodeId);
|
|
1821
2119
|
return node?.outputs[output];
|
|
1822
2120
|
}
|
|
1823
|
-
// Update resolved handles for a single node and refresh edge converters/types that touch it
|
|
1824
|
-
updateNodeHandles(nodeId, handles, registry) {
|
|
1825
|
-
const node = this.graphStructure.getNode(nodeId);
|
|
1826
|
-
if (!node)
|
|
1827
|
-
return;
|
|
1828
|
-
const oldResolved = this.graphStructure.getResolvedHandles(nodeId);
|
|
1829
|
-
this.graphStructure.setResolvedHandles(nodeId, handles);
|
|
1830
|
-
const oldOutputs = oldResolved?.outputs ?? {};
|
|
1831
|
-
const newOutputs = handles.outputs ?? {};
|
|
1832
|
-
const oldOutputHandles = new Set(Object.keys(oldOutputs));
|
|
1833
|
-
const newOutputHandles = new Set(Object.keys(newOutputs));
|
|
1834
|
-
for (const handle of oldOutputHandles) {
|
|
1835
|
-
if (!newOutputHandles.has(handle)) {
|
|
1836
|
-
delete node.outputs[handle];
|
|
1837
|
-
this.eventEmitter.emit("value", {
|
|
1838
|
-
nodeId,
|
|
1839
|
-
handle,
|
|
1840
|
-
value: undefined,
|
|
1841
|
-
io: "output",
|
|
1842
|
-
});
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
const edges = this.graphStructure.getEdges();
|
|
1846
|
-
for (const e of edges) {
|
|
1847
|
-
const srcNode = this.graphStructure.getNode(e.source.nodeId);
|
|
1848
|
-
const dstNode = this.graphStructure.getNode(e.target.nodeId);
|
|
1849
|
-
let srcDeclared = e.effectiveTypeId;
|
|
1850
|
-
let dstDeclared = e.dstDeclared;
|
|
1851
|
-
const oldDstDeclared = dstDeclared;
|
|
1852
|
-
if (e.source.nodeId === nodeId) {
|
|
1853
|
-
const resolved = this.graphStructure.getResolvedHandles(nodeId);
|
|
1854
|
-
srcDeclared = resolved
|
|
1855
|
-
? resolved.outputs[e.source.handle]
|
|
1856
|
-
: srcDeclared;
|
|
1857
|
-
if (!e.typeId) {
|
|
1858
|
-
e.effectiveTypeId = Array.isArray(srcDeclared)
|
|
1859
|
-
? srcDeclared?.[0] ?? "untyped"
|
|
1860
|
-
: srcDeclared ?? "untyped";
|
|
1861
|
-
}
|
|
1862
|
-
}
|
|
1863
|
-
if (e.target.nodeId === nodeId) {
|
|
1864
|
-
const resolved = this.graphStructure.getResolvedHandles(nodeId);
|
|
1865
|
-
if (resolved) {
|
|
1866
|
-
dstDeclared = getInputTypeId(resolved.inputs, e.target.handle);
|
|
1867
|
-
e.dstDeclared = dstDeclared;
|
|
1868
|
-
}
|
|
1869
|
-
}
|
|
1870
|
-
const conv = GraphStructure.buildEdgeConverters(srcDeclared, dstDeclared, registry, `updateNodeHandles: ${srcNode?.typeId || ""}.${e.source.nodeId}.${e.source.handle} -> ${dstNode?.typeId || ""}.${e.target.nodeId}.${e.target.handle}`);
|
|
1871
|
-
e.convert = conv.convert;
|
|
1872
|
-
e.convertAsync = conv.convertAsync;
|
|
1873
|
-
if (e.target.nodeId === nodeId &&
|
|
1874
|
-
oldDstDeclared === undefined &&
|
|
1875
|
-
dstDeclared !== undefined) {
|
|
1876
|
-
const srcNode = this.graphStructure.getNode(e.source.nodeId);
|
|
1877
|
-
if (srcNode) {
|
|
1878
|
-
const srcValue = srcNode.outputs[e.source.handle];
|
|
1879
|
-
if (srcValue !== undefined) {
|
|
1880
|
-
const runContextIds = srcNode.activeRunContexts.size > 0
|
|
1881
|
-
? new Set(srcNode.activeRunContexts)
|
|
1882
|
-
: undefined;
|
|
1883
|
-
this.valuePropagator.propagate(e.source.nodeId, e.source.handle, srcValue, runContextIds);
|
|
1884
|
-
}
|
|
1885
|
-
}
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
|
-
this.valuePropagator.reemitNodeOutputs(nodeId);
|
|
1889
|
-
}
|
|
1890
2121
|
launch(invalidate = false) {
|
|
1891
|
-
for (const node of this.
|
|
1892
|
-
const effectiveInputs = this.
|
|
2122
|
+
for (const node of this.graph.getNodes().values()) {
|
|
2123
|
+
const effectiveInputs = this.nodeExecutor.getEffectiveInputs(node.nodeId);
|
|
1893
2124
|
const ctrl = new AbortController();
|
|
1894
|
-
const ctx = this.
|
|
2125
|
+
const ctx = this.nodeExecutor.createExecutionContext(node.nodeId, node, effectiveInputs, `${node.nodeId}:init`, ctrl.signal);
|
|
1895
2126
|
if (node.lifecycle?.prepare) {
|
|
1896
2127
|
ctx.log("debug", "prepare-start");
|
|
1897
2128
|
node.lifecycle.prepare(node.params ?? {}, ctx);
|
|
@@ -1899,36 +2130,27 @@ class GraphRuntime {
|
|
|
1899
2130
|
}
|
|
1900
2131
|
node.runtime.onActivated?.();
|
|
1901
2132
|
}
|
|
1902
|
-
if (invalidate) {
|
|
1903
|
-
for (const nodeId of this.
|
|
1904
|
-
if (this.
|
|
1905
|
-
this.
|
|
2133
|
+
if (this.runMode === "auto" && invalidate) {
|
|
2134
|
+
for (const nodeId of this.graph.getNodes().keys()) {
|
|
2135
|
+
if (this.graph.allInboundHaveValue(nodeId))
|
|
2136
|
+
this.execute(nodeId);
|
|
1906
2137
|
}
|
|
1907
2138
|
}
|
|
1908
2139
|
}
|
|
1909
2140
|
triggerExternal(nodeId, event) {
|
|
1910
|
-
const node = this.
|
|
2141
|
+
const node = this.graph.getNode(nodeId);
|
|
1911
2142
|
if (!node)
|
|
1912
2143
|
return;
|
|
1913
2144
|
node.runtime.onExternalEvent?.(event, node.state);
|
|
1914
2145
|
}
|
|
1915
|
-
|
|
1916
|
-
this.
|
|
1917
|
-
for (const node of this.graphStructure.getNodes().values()) {
|
|
1918
|
-
node.runtime.onDeactivated?.();
|
|
1919
|
-
node.runtime.dispose?.();
|
|
1920
|
-
node.lifecycle?.dispose?.({
|
|
1921
|
-
state: node.state,
|
|
1922
|
-
setState: (next) => Object.assign(node.state, next),
|
|
1923
|
-
});
|
|
1924
|
-
}
|
|
1925
|
-
this.graphStructure.clear();
|
|
2146
|
+
cancelNodeRuns(nodeIds) {
|
|
2147
|
+
this.nodeExecutor.cancelNodeRuns(nodeIds);
|
|
1926
2148
|
}
|
|
1927
2149
|
getNodeIds() {
|
|
1928
|
-
return Array.from(this.
|
|
2150
|
+
return Array.from(this.graph.getNodes().keys());
|
|
1929
2151
|
}
|
|
1930
2152
|
getNodeData(nodeId) {
|
|
1931
|
-
const node = this.
|
|
2153
|
+
const node = this.graph.getNode(nodeId);
|
|
1932
2154
|
if (!node)
|
|
1933
2155
|
return undefined;
|
|
1934
2156
|
return {
|
|
@@ -1945,14 +2167,14 @@ class GraphRuntime {
|
|
|
1945
2167
|
setEnvironment(env) {
|
|
1946
2168
|
this.environment = { ...env };
|
|
1947
2169
|
this.handleResolver.setEnvironment(this.environment);
|
|
1948
|
-
this.
|
|
1949
|
-
for (const nodeId of this.
|
|
2170
|
+
this.nodeExecutor.setEnvironment(this.environment);
|
|
2171
|
+
for (const nodeId of this.graph.getNodes().keys()) {
|
|
1950
2172
|
this.handleResolver.scheduleRecomputeHandles(nodeId);
|
|
1951
2173
|
}
|
|
1952
2174
|
}
|
|
1953
2175
|
getGraphDef() {
|
|
1954
|
-
const nodes = Array.from(this.
|
|
1955
|
-
const resolved = this.
|
|
2176
|
+
const nodes = Array.from(this.graph.getNodes().values()).map((n) => {
|
|
2177
|
+
const resolved = this.graph.getResolvedHandles(n.nodeId);
|
|
1956
2178
|
return {
|
|
1957
2179
|
nodeId: n.nodeId,
|
|
1958
2180
|
typeId: n.typeId,
|
|
@@ -1960,9 +2182,7 @@ class GraphRuntime {
|
|
|
1960
2182
|
resolvedHandles: resolved ? { ...resolved } : undefined,
|
|
1961
2183
|
};
|
|
1962
2184
|
});
|
|
1963
|
-
const edges = this.
|
|
1964
|
-
.getEdges()
|
|
1965
|
-
.map((e) => ({
|
|
2185
|
+
const edges = this.graph.getEdges().map((e) => ({
|
|
1966
2186
|
id: e.id,
|
|
1967
2187
|
source: { nodeId: e.source.nodeId, handle: e.source.handle },
|
|
1968
2188
|
target: { nodeId: e.target.nodeId, handle: e.target.handle },
|
|
@@ -1986,7 +2206,7 @@ class GraphRuntime {
|
|
|
1986
2206
|
});
|
|
1987
2207
|
}
|
|
1988
2208
|
const isIdle = () => {
|
|
1989
|
-
for (const n of this.
|
|
2209
|
+
for (const n of this.graph.getNodes().values()) {
|
|
1990
2210
|
if (n.activeControllers.size > 0)
|
|
1991
2211
|
return false;
|
|
1992
2212
|
if (n.queue.length > 0)
|
|
@@ -2007,78 +2227,46 @@ class GraphRuntime {
|
|
|
2007
2227
|
});
|
|
2008
2228
|
}
|
|
2009
2229
|
async runFromHereContext(startNodeId, options) {
|
|
2010
|
-
const node = this.
|
|
2230
|
+
const node = this.graph.getNode(startNodeId);
|
|
2011
2231
|
if (!node)
|
|
2012
2232
|
return;
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2233
|
+
return new Promise((resolve) => {
|
|
2234
|
+
const id = this.runContextManager.createRunContext(startNodeId, resolve, options);
|
|
2235
|
+
node.activeRunContextIds.add(id);
|
|
2236
|
+
this.execute(startNodeId, new Set([id]));
|
|
2016
2237
|
});
|
|
2017
|
-
node.activeRunContexts.add(ctx.id);
|
|
2018
|
-
this.scheduleInputsChangedWithRunContexts(startNodeId, new Set([ctx.id]));
|
|
2019
|
-
await promise;
|
|
2020
|
-
}
|
|
2021
|
-
scheduleInputsChangedWithRunContexts(nodeId, runContextIds) {
|
|
2022
|
-
const node = this.graphStructure.getNode(nodeId);
|
|
2023
|
-
if (!node)
|
|
2024
|
-
return;
|
|
2025
|
-
for (const id of runContextIds) {
|
|
2026
|
-
node.activeRunContexts.add(id);
|
|
2027
|
-
}
|
|
2028
|
-
this.executionScheduler.scheduleInputsChangedInternal(nodeId, runContextIds);
|
|
2029
2238
|
}
|
|
2030
2239
|
setRunMode(runMode) {
|
|
2031
2240
|
this.runMode = runMode;
|
|
2032
|
-
|
|
2241
|
+
}
|
|
2242
|
+
getRunMode() {
|
|
2243
|
+
return this.runMode;
|
|
2033
2244
|
}
|
|
2034
2245
|
requestPause() {
|
|
2035
2246
|
this.pauseRefCount++;
|
|
2036
|
-
this.updatePausedState();
|
|
2037
2247
|
let released = false;
|
|
2038
2248
|
return () => {
|
|
2039
2249
|
if (released)
|
|
2040
2250
|
return;
|
|
2041
2251
|
released = true;
|
|
2042
2252
|
this.pauseRefCount--;
|
|
2043
|
-
this.updatePausedState();
|
|
2044
2253
|
};
|
|
2045
2254
|
}
|
|
2046
|
-
updatePausedState() {
|
|
2047
|
-
if (this.pauseRefCount > 0) {
|
|
2048
|
-
this.paused = true;
|
|
2049
|
-
return;
|
|
2050
|
-
}
|
|
2051
|
-
if (this.runMode === "manual") {
|
|
2052
|
-
this.paused = true;
|
|
2053
|
-
}
|
|
2054
|
-
else if (this.runMode === "auto") {
|
|
2055
|
-
this.paused = false;
|
|
2056
|
-
}
|
|
2057
|
-
}
|
|
2058
2255
|
isPaused() {
|
|
2059
|
-
return this.
|
|
2060
|
-
}
|
|
2061
|
-
invalidateDownstream(nodeId) {
|
|
2062
|
-
this.valuePropagator.reemitNodeOutputs(nodeId);
|
|
2063
|
-
}
|
|
2064
|
-
scheduleInputsChanged(nodeId) {
|
|
2065
|
-
this.executionScheduler.scheduleInputsChangedInternal(nodeId);
|
|
2066
|
-
}
|
|
2067
|
-
cancelNodeRuns(nodeIds) {
|
|
2068
|
-
this.executionScheduler.cancelNodeRuns(nodeIds);
|
|
2256
|
+
return this.pauseRefCount > 0;
|
|
2069
2257
|
}
|
|
2070
2258
|
copyOutputs(fromNodeId, toNodeId, options) {
|
|
2071
2259
|
const fromNode = this.getNodeData(fromNodeId);
|
|
2072
2260
|
if (!fromNode?.outputs)
|
|
2073
2261
|
return;
|
|
2074
|
-
this.hydrate({ outputs: { [toNodeId]: { ...fromNode.outputs } } }, {
|
|
2262
|
+
this.hydrate({ outputs: { [toNodeId]: { ...fromNode.outputs } } }, { invalidate: !options?.dry });
|
|
2075
2263
|
}
|
|
2076
2264
|
hydrate(payload, opts) {
|
|
2077
2265
|
const releasePause = this.requestPause();
|
|
2078
2266
|
try {
|
|
2079
2267
|
const ins = payload?.inputs || {};
|
|
2080
2268
|
for (const [nodeId, map] of Object.entries(ins)) {
|
|
2081
|
-
const node = this.
|
|
2269
|
+
const node = this.graph.getNode(nodeId);
|
|
2082
2270
|
if (!node)
|
|
2083
2271
|
continue;
|
|
2084
2272
|
for (const [h, v] of Object.entries(map || {})) {
|
|
@@ -2094,7 +2282,7 @@ class GraphRuntime {
|
|
|
2094
2282
|
}
|
|
2095
2283
|
const outs = payload?.outputs || {};
|
|
2096
2284
|
for (const [nodeId, map] of Object.entries(outs)) {
|
|
2097
|
-
const node = this.
|
|
2285
|
+
const node = this.graph.getNode(nodeId);
|
|
2098
2286
|
if (!node)
|
|
2099
2287
|
continue;
|
|
2100
2288
|
for (const [h, v] of Object.entries(map || {})) {
|
|
@@ -2108,9 +2296,9 @@ class GraphRuntime {
|
|
|
2108
2296
|
});
|
|
2109
2297
|
}
|
|
2110
2298
|
}
|
|
2111
|
-
if (opts?.
|
|
2112
|
-
for (const nodeId of this.
|
|
2113
|
-
this.
|
|
2299
|
+
if (opts?.invalidate) {
|
|
2300
|
+
for (const nodeId of this.graph.getNodes().keys()) {
|
|
2301
|
+
this.invalidateDownstream(nodeId);
|
|
2114
2302
|
}
|
|
2115
2303
|
}
|
|
2116
2304
|
}
|
|
@@ -2119,201 +2307,234 @@ class GraphRuntime {
|
|
|
2119
2307
|
}
|
|
2120
2308
|
}
|
|
2121
2309
|
update(def, registry) {
|
|
2122
|
-
|
|
2123
|
-
|
|
2124
|
-
|
|
2125
|
-
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2310
|
+
{
|
|
2311
|
+
// Delete nodes that are no longer in the definition
|
|
2312
|
+
const afterIds = new Set(def.nodes.map((n) => n.nodeId));
|
|
2313
|
+
const beforeIds = new Set(this.graph.getNodes().keys());
|
|
2314
|
+
for (const nodeId of Array.from(beforeIds)) {
|
|
2315
|
+
if (!afterIds.has(nodeId)) {
|
|
2316
|
+
const node = this.graph.getNode(nodeId);
|
|
2317
|
+
this.nodeExecutor.cancelNodeActiveRuns(node, "node-deleted");
|
|
2318
|
+
this.runContextManager.cancelNodeInRunContexts(nodeId, true);
|
|
2319
|
+
node.runtime.onDeactivated?.();
|
|
2320
|
+
node.runtime.dispose?.();
|
|
2321
|
+
node.lifecycle?.dispose?.({
|
|
2322
|
+
state: node.state,
|
|
2323
|
+
setState: (next) => Object.assign(node.state, next),
|
|
2324
|
+
});
|
|
2325
|
+
this.graph.deleteNode(nodeId);
|
|
2326
|
+
this.edgePropagator.clearArrayBuckets(nodeId);
|
|
2134
2327
|
}
|
|
2135
|
-
node.runtime.onDeactivated?.();
|
|
2136
|
-
node.runtime.dispose?.();
|
|
2137
|
-
node.lifecycle?.dispose?.({
|
|
2138
|
-
state: node.state,
|
|
2139
|
-
setState: (next) => Object.assign(node.state, next),
|
|
2140
|
-
});
|
|
2141
|
-
this.graphStructure.deleteNode(nodeId);
|
|
2142
|
-
this.valuePropagator.clearArrayBuckets(nodeId);
|
|
2143
2328
|
}
|
|
2144
2329
|
}
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
const
|
|
2148
|
-
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
cat.validateImpl
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2163
|
-
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
this.graphStructure.setNode(n.nodeId, rn);
|
|
2188
|
-
const effectiveInputs = this.executionScheduler.getEffectiveInputs(rn.nodeId);
|
|
2189
|
-
const ctrl = new AbortController();
|
|
2190
|
-
const ctx = this.executionScheduler.createExecutionContext(rn.nodeId, rn, effectiveInputs, `${rn.nodeId}:init`, ctrl.signal);
|
|
2191
|
-
if (rn.lifecycle?.prepare) {
|
|
2192
|
-
ctx.log("debug", "prepare-start");
|
|
2193
|
-
rn.lifecycle.prepare(rn.params ?? {}, ctx);
|
|
2194
|
-
ctx.log("debug", "prepare-done");
|
|
2195
|
-
}
|
|
2196
|
-
rn.runtime.onActivated?.();
|
|
2197
|
-
}
|
|
2198
|
-
else {
|
|
2199
|
-
existing.params = n.params;
|
|
2200
|
-
if (!existing.stats) {
|
|
2201
|
-
existing.stats = {
|
|
2202
|
-
runs: 0,
|
|
2203
|
-
active: 0,
|
|
2204
|
-
queued: 0,
|
|
2205
|
-
progress: 0,
|
|
2330
|
+
{
|
|
2331
|
+
// Add or update nodes that are in the definition
|
|
2332
|
+
for (const n of def.nodes) {
|
|
2333
|
+
const existing = this.graph.getNode(n.nodeId);
|
|
2334
|
+
if (!existing) {
|
|
2335
|
+
const desc = registry.nodes.get(n.typeId);
|
|
2336
|
+
if (!desc)
|
|
2337
|
+
throw new Error(`Unknown node type: ${n.typeId}`);
|
|
2338
|
+
const cat = registry.categories.get(desc.categoryId);
|
|
2339
|
+
if (!cat)
|
|
2340
|
+
throw new Error(`Unknown category: ${desc.categoryId}`);
|
|
2341
|
+
if (cat.validateImpl)
|
|
2342
|
+
cat.validateImpl(desc.impl);
|
|
2343
|
+
const runtime = cat.createRuntime({
|
|
2344
|
+
nodeId: n.nodeId,
|
|
2345
|
+
impl: desc.impl,
|
|
2346
|
+
});
|
|
2347
|
+
const newNode = {
|
|
2348
|
+
typeId: n.typeId,
|
|
2349
|
+
nodeId: n.nodeId,
|
|
2350
|
+
lifecycle: desc.lifecycle,
|
|
2351
|
+
inputs: {},
|
|
2352
|
+
outputs: {},
|
|
2353
|
+
state: {},
|
|
2354
|
+
runtime,
|
|
2355
|
+
params: n.params,
|
|
2356
|
+
policy: {
|
|
2357
|
+
...cat.policy,
|
|
2358
|
+
...desc.policy,
|
|
2359
|
+
...n.params?.policy,
|
|
2360
|
+
},
|
|
2361
|
+
runSeq: 0,
|
|
2362
|
+
activeControllers: new Set(),
|
|
2363
|
+
controllerRunIds: new Map(),
|
|
2364
|
+
queue: [],
|
|
2365
|
+
stats: {
|
|
2366
|
+
runs: 0,
|
|
2367
|
+
active: 0,
|
|
2368
|
+
queued: 0,
|
|
2369
|
+
progress: 0,
|
|
2370
|
+
},
|
|
2371
|
+
activeRunContextIds: new Set(),
|
|
2206
2372
|
};
|
|
2373
|
+
this.graph.setNode(n.nodeId, newNode);
|
|
2374
|
+
const effectiveInputs = this.nodeExecutor.getEffectiveInputs(newNode.nodeId);
|
|
2375
|
+
const ctrl = new AbortController();
|
|
2376
|
+
const ctx = this.nodeExecutor.createExecutionContext(newNode.nodeId, newNode, effectiveInputs, `${newNode.nodeId}:init`, ctrl.signal);
|
|
2377
|
+
if (newNode.lifecycle?.prepare) {
|
|
2378
|
+
ctx.log("debug", "prepare-start");
|
|
2379
|
+
newNode.lifecycle.prepare(newNode.params ?? {}, ctx);
|
|
2380
|
+
ctx.log("debug", "prepare-done");
|
|
2381
|
+
}
|
|
2382
|
+
newNode.runtime.onActivated?.();
|
|
2383
|
+
}
|
|
2384
|
+
else {
|
|
2385
|
+
existing.params = n.params;
|
|
2386
|
+
if (!existing.stats) {
|
|
2387
|
+
existing.stats = {
|
|
2388
|
+
runs: 0,
|
|
2389
|
+
active: 0,
|
|
2390
|
+
queued: 0,
|
|
2391
|
+
progress: 0,
|
|
2392
|
+
};
|
|
2393
|
+
}
|
|
2207
2394
|
}
|
|
2208
2395
|
}
|
|
2209
2396
|
}
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
|
|
2213
|
-
const
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
const prevOutTargets = new Map();
|
|
2218
|
-
for (const e of edges) {
|
|
2219
|
-
const tmap = prevOutTargets.get(e.source.nodeId) ?? new Map();
|
|
2220
|
-
const tset = tmap.get(e.source.handle) ?? new Set();
|
|
2221
|
-
tset.add(`${e.target.nodeId}.${e.target.handle}`);
|
|
2222
|
-
tmap.set(e.source.handle, tset);
|
|
2223
|
-
prevOutTargets.set(e.source.nodeId, tmap);
|
|
2224
|
-
}
|
|
2225
|
-
const resolved = GraphStructure.computeResolvedHandleMap(def, registry, this.environment);
|
|
2226
|
-
const changedHandles = {};
|
|
2227
|
-
for (const [nodeId, newHandles] of resolved.map) {
|
|
2228
|
-
const oldHandles = this.graphStructure.getResolvedHandles(nodeId);
|
|
2229
|
-
if (!oldHandles ||
|
|
2230
|
-
JSON.stringify(oldHandles) !== JSON.stringify(newHandles)) {
|
|
2231
|
-
changedHandles[nodeId] = newHandles;
|
|
2397
|
+
{
|
|
2398
|
+
const beforeEdges = this.graph.getEdges();
|
|
2399
|
+
const beforeInbound = new Map();
|
|
2400
|
+
for (const e of beforeEdges) {
|
|
2401
|
+
const set = beforeInbound.get(e.target.nodeId) ?? new Set();
|
|
2402
|
+
set.add(e.target.handle);
|
|
2403
|
+
beforeInbound.set(e.target.nodeId, set);
|
|
2232
2404
|
}
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
let changed = false;
|
|
2252
|
-
for (const handle of Array.from(prevSet)) {
|
|
2253
|
-
if (!currSet.has(handle)) {
|
|
2254
|
-
if (handle in node.inputs) {
|
|
2255
|
-
delete node.inputs[handle];
|
|
2256
|
-
changed = true;
|
|
2405
|
+
const beforeOutTargets = new Map();
|
|
2406
|
+
for (const e of beforeEdges) {
|
|
2407
|
+
const tmap = beforeOutTargets.get(e.source.nodeId) ??
|
|
2408
|
+
new Map();
|
|
2409
|
+
const tset = tmap.get(e.source.handle) ?? new Set();
|
|
2410
|
+
tset.add(`${e.target.nodeId}.${e.target.handle}`);
|
|
2411
|
+
tmap.set(e.source.handle, tset);
|
|
2412
|
+
beforeOutTargets.set(e.source.nodeId, tmap);
|
|
2413
|
+
}
|
|
2414
|
+
{
|
|
2415
|
+
// Update handles and edges
|
|
2416
|
+
const result = tryHandleResolving(def, registry, this.environment);
|
|
2417
|
+
const changedHandles = {};
|
|
2418
|
+
for (const [nodeId, newHandles] of result.resolved) {
|
|
2419
|
+
const oldHandles = this.graph.getResolvedHandles(nodeId);
|
|
2420
|
+
if (!oldHandles ||
|
|
2421
|
+
JSON.stringify(oldHandles) !== JSON.stringify(newHandles)) {
|
|
2422
|
+
changedHandles[nodeId] = newHandles;
|
|
2257
2423
|
}
|
|
2258
2424
|
}
|
|
2425
|
+
for (const [nodeId, handles] of result.resolved) {
|
|
2426
|
+
this.graph.setResolvedHandles(nodeId, handles);
|
|
2427
|
+
}
|
|
2428
|
+
const afterEdges = buildEdges(def, registry, this.graph.getResolvedHandlesMap());
|
|
2429
|
+
this.graph.setEdges(afterEdges);
|
|
2430
|
+
for (const nodeId of result.pending) {
|
|
2431
|
+
this.handleResolver.scheduleRecomputeHandles(nodeId);
|
|
2432
|
+
}
|
|
2433
|
+
if (Object.keys(changedHandles).length > 0) {
|
|
2434
|
+
this.eventEmitter.emit("invalidate", {
|
|
2435
|
+
reason: "graph-updated",
|
|
2436
|
+
resolvedHandles: changedHandles,
|
|
2437
|
+
});
|
|
2438
|
+
}
|
|
2259
2439
|
}
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
|
|
2266
|
-
|
|
2267
|
-
|
|
2268
|
-
|
|
2269
|
-
|
|
2270
|
-
|
|
2271
|
-
|
|
2272
|
-
|
|
2273
|
-
|
|
2274
|
-
|
|
2275
|
-
|
|
2276
|
-
|
|
2277
|
-
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2297
|
-
|
|
2298
|
-
|
|
2299
|
-
|
|
2300
|
-
|
|
2301
|
-
|
|
2302
|
-
|
|
2303
|
-
|
|
2304
|
-
|
|
2440
|
+
{
|
|
2441
|
+
// Update inputs and propagate changes
|
|
2442
|
+
const afterInbound = new Map();
|
|
2443
|
+
const afterEdges = this.graph.getEdges();
|
|
2444
|
+
for (const e of afterEdges) {
|
|
2445
|
+
const set = afterInbound.get(e.target.nodeId) ?? new Set();
|
|
2446
|
+
set.add(e.target.handle);
|
|
2447
|
+
afterInbound.set(e.target.nodeId, set);
|
|
2448
|
+
}
|
|
2449
|
+
// Propagate changes on edges removed
|
|
2450
|
+
for (const [nodeId, beforeSet] of beforeInbound) {
|
|
2451
|
+
const currSet = afterInbound.get(nodeId) ?? new Set();
|
|
2452
|
+
const node = this.graph.getNode(nodeId);
|
|
2453
|
+
if (!node)
|
|
2454
|
+
continue;
|
|
2455
|
+
let changed = false;
|
|
2456
|
+
for (const handle of Array.from(beforeSet)) {
|
|
2457
|
+
if (!currSet.has(handle)) {
|
|
2458
|
+
if (handle in node.inputs) {
|
|
2459
|
+
delete node.inputs[handle];
|
|
2460
|
+
changed = true;
|
|
2461
|
+
}
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
if (changed) {
|
|
2465
|
+
this.edgePropagator.clearArrayBuckets(nodeId);
|
|
2466
|
+
if (this.runMode === "auto" &&
|
|
2467
|
+
this.graph.allInboundHaveValue(nodeId)) {
|
|
2468
|
+
this.execute(nodeId);
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
// Propagate changes on edges added
|
|
2473
|
+
const afterOutTargets = new Map();
|
|
2474
|
+
for (const e of afterEdges) {
|
|
2475
|
+
const targetMap = afterOutTargets.get(e.source.nodeId) ??
|
|
2476
|
+
new Map();
|
|
2477
|
+
const targetSet = targetMap.get(e.source.handle) ?? new Set();
|
|
2478
|
+
targetSet.add(`${e.target.nodeId}.${e.target.handle}`);
|
|
2479
|
+
targetMap.set(e.source.handle, targetSet);
|
|
2480
|
+
afterOutTargets.set(e.source.nodeId, targetMap);
|
|
2481
|
+
}
|
|
2482
|
+
const setsEqual = (a, b) => {
|
|
2483
|
+
if (!a && !b)
|
|
2484
|
+
return true;
|
|
2485
|
+
if (!a || !b)
|
|
2486
|
+
return false;
|
|
2487
|
+
if (a.size !== b.size)
|
|
2488
|
+
return false;
|
|
2489
|
+
for (const v of a)
|
|
2490
|
+
if (!b.has(v))
|
|
2491
|
+
return false;
|
|
2492
|
+
return true;
|
|
2493
|
+
};
|
|
2494
|
+
const nodesToCheck = new Set([
|
|
2495
|
+
...Array.from(beforeOutTargets.keys()),
|
|
2496
|
+
...Array.from(afterOutTargets.keys()),
|
|
2497
|
+
]);
|
|
2498
|
+
for (const nodeId of nodesToCheck) {
|
|
2499
|
+
const beforeMap = beforeOutTargets.get(nodeId) ?? new Map();
|
|
2500
|
+
const afterMap = afterOutTargets.get(nodeId) ?? new Map();
|
|
2501
|
+
const handles = new Set([
|
|
2502
|
+
...Array.from(beforeMap.keys()),
|
|
2503
|
+
...Array.from(afterMap.keys()),
|
|
2504
|
+
]);
|
|
2505
|
+
for (const handle of handles) {
|
|
2506
|
+
const beforeTargetSet = beforeMap.get(handle) ?? new Set();
|
|
2507
|
+
const afterTargetSet = afterMap.get(handle) ?? new Set();
|
|
2508
|
+
if (!setsEqual(beforeTargetSet, afterTargetSet)) {
|
|
2509
|
+
const val = this.getOutput(nodeId, handle);
|
|
2510
|
+
if (val !== undefined)
|
|
2511
|
+
this.propagate(nodeId, handle, val);
|
|
2512
|
+
}
|
|
2513
|
+
}
|
|
2305
2514
|
}
|
|
2306
2515
|
}
|
|
2307
2516
|
}
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2517
|
+
}
|
|
2518
|
+
dispose() {
|
|
2519
|
+
this.runContextManager.resolveAll();
|
|
2520
|
+
for (const node of this.graph.getNodes().values()) {
|
|
2521
|
+
node.runtime.onDeactivated?.();
|
|
2522
|
+
node.runtime.dispose?.();
|
|
2523
|
+
node.lifecycle?.dispose?.({
|
|
2524
|
+
state: node.state,
|
|
2525
|
+
setState: (next) => Object.assign(node.state, next),
|
|
2312
2526
|
});
|
|
2313
2527
|
}
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2528
|
+
this.graph.clear();
|
|
2529
|
+
}
|
|
2530
|
+
execute(nodeId, runContextIds) {
|
|
2531
|
+
this.nodeExecutor.execute(nodeId, runContextIds);
|
|
2532
|
+
}
|
|
2533
|
+
propagate(srcNodeId, srcHandle, value, runContextIds) {
|
|
2534
|
+
this.edgePropagator.propagate(srcNodeId, srcHandle, value, runContextIds);
|
|
2535
|
+
}
|
|
2536
|
+
invalidateDownstream(nodeId) {
|
|
2537
|
+
this.edgePropagator.invalidateDownstream(nodeId);
|
|
2317
2538
|
}
|
|
2318
2539
|
}
|
|
2319
2540
|
|
|
@@ -2537,8 +2758,8 @@ class GraphBuilder {
|
|
|
2537
2758
|
|
|
2538
2759
|
/**
|
|
2539
2760
|
* Unified Engine implementation that handles both manual and auto run modes.
|
|
2540
|
-
* - Manual mode:
|
|
2541
|
-
* - Auto mode:
|
|
2761
|
+
* - Manual mode: Nodes execute only when explicitly called via computeNode/runFromHere (unless paused)
|
|
2762
|
+
* - Auto mode: Nodes automatically execute when inputs change (unless paused)
|
|
2542
2763
|
*/
|
|
2543
2764
|
class LocalEngine {
|
|
2544
2765
|
constructor(graphRuntime, runMode) {
|
|
@@ -4167,6 +4388,58 @@ function setValueAtPath(obj, pathSegments, newValue) {
|
|
|
4167
4388
|
}
|
|
4168
4389
|
return true;
|
|
4169
4390
|
}
|
|
4391
|
+
/**
|
|
4392
|
+
* Sets a value at a path, creating intermediate objects as needed.
|
|
4393
|
+
* Mutates the root object in place.
|
|
4394
|
+
* @param root - The root object to modify (must be an object, will be initialized if needed)
|
|
4395
|
+
* @param pathSegments - The path segments to traverse
|
|
4396
|
+
* @param value - The value to set, or null to delete the path
|
|
4397
|
+
* @throws Error if path cannot be created (e.g., array indices not supported, invalid parent types)
|
|
4398
|
+
*/
|
|
4399
|
+
function setValueAtPathWithCreation(root, pathSegments, value) {
|
|
4400
|
+
if (value === null) {
|
|
4401
|
+
const result = getValueAtPath(root, pathSegments);
|
|
4402
|
+
if (result && result.parent !== null && !Array.isArray(result.parent)) {
|
|
4403
|
+
delete result.parent[result.key];
|
|
4404
|
+
}
|
|
4405
|
+
return;
|
|
4406
|
+
}
|
|
4407
|
+
if (!root || typeof root !== "object" || Array.isArray(root)) {
|
|
4408
|
+
throw new Error("Root must be an object");
|
|
4409
|
+
}
|
|
4410
|
+
let current = root;
|
|
4411
|
+
for (let i = 0; i < pathSegments.length - 1; i++) {
|
|
4412
|
+
const segment = pathSegments[i];
|
|
4413
|
+
if (typeof segment === "string") {
|
|
4414
|
+
if (!current ||
|
|
4415
|
+
typeof current !== "object" ||
|
|
4416
|
+
Array.isArray(current) ||
|
|
4417
|
+
!(segment in current) ||
|
|
4418
|
+
typeof current[segment] !== "object" ||
|
|
4419
|
+
current[segment] === null ||
|
|
4420
|
+
Array.isArray(current[segment])) {
|
|
4421
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) {
|
|
4422
|
+
throw new Error(`Cannot create path: parent at segment ${i} is not an object`);
|
|
4423
|
+
}
|
|
4424
|
+
current[segment] = {};
|
|
4425
|
+
}
|
|
4426
|
+
current = current[segment];
|
|
4427
|
+
}
|
|
4428
|
+
else {
|
|
4429
|
+
throw new Error("Array indices not supported in extData paths");
|
|
4430
|
+
}
|
|
4431
|
+
}
|
|
4432
|
+
const lastSegment = pathSegments[pathSegments.length - 1];
|
|
4433
|
+
if (typeof lastSegment === "string") {
|
|
4434
|
+
if (!current || typeof current !== "object" || Array.isArray(current)) {
|
|
4435
|
+
throw new Error(`Cannot set value: parent at final segment is not an object`);
|
|
4436
|
+
}
|
|
4437
|
+
current[lastSegment] = value;
|
|
4438
|
+
}
|
|
4439
|
+
else {
|
|
4440
|
+
throw new Error("Array indices not supported in extData paths");
|
|
4441
|
+
}
|
|
4442
|
+
}
|
|
4170
4443
|
function findMatchingPaths(obj, pathSegments, currentPath = []) {
|
|
4171
4444
|
if (pathSegments.length === 0) {
|
|
4172
4445
|
return [{ path: currentPath, value: obj }];
|
|
@@ -4701,5 +4974,5 @@ function buildValueConverter(config) {
|
|
|
4701
4974
|
};
|
|
4702
4975
|
}
|
|
4703
4976
|
|
|
4704
|
-
export { BaseCompareOperation, BaseLogicOperation, BaseMathOperation, CompositeCategory, ComputeCategory, GraphBuilder, GraphRuntime, LocalEngine, Registry, buildValueConverter, computeGraphCenter, createAsyncGraphDef, createAsyncGraphRegistry, createProgressGraphDef, createProgressGraphRegistry, createSimpleGraphDef, createSimpleGraphRegistry, createValidationGraphDef, createValidationGraphRegistry, findMatchingPaths, generateId, getInputTypeId, getTypedOutputTypeId, getTypedOutputValue, getValueAtPath, installLogging, isInputPrivate, isTypedOutput, mergeGraphDefinitions, mergeInputsOutputs, mergeRuntimeState, mergeSnapshotData, offsetImportedPositions, parseJsonPath, registerDelayNode, registerProgressNodes, setValueAtPath, typed };
|
|
4977
|
+
export { BaseCompareOperation, BaseLogicOperation, BaseMathOperation, CompositeCategory, ComputeCategory, GraphBuilder, GraphRuntime, LocalEngine, Registry, buildValueConverter, computeGraphCenter, createAsyncGraphDef, createAsyncGraphRegistry, createProgressGraphDef, createProgressGraphRegistry, createSimpleGraphDef, createSimpleGraphRegistry, createValidationGraphDef, createValidationGraphRegistry, findMatchingPaths, generateId, getInputTypeId, getTypedOutputTypeId, getTypedOutputValue, getValueAtPath, installLogging, isInputPrivate, isTypedOutput, mergeGraphDefinitions, mergeInputsOutputs, mergeRuntimeState, mergeSnapshotData, offsetImportedPositions, parseJsonPath, registerDelayNode, registerProgressNodes, setValueAtPath, setValueAtPathWithCreation, typed };
|
|
4705
4978
|
//# sourceMappingURL=index.js.map
|