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