@valerypopoff/rivet2-core 2.4.0 → 2.6.0

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.
Files changed (82) hide show
  1. package/dist/cjs/bundle.cjs +1800 -753
  2. package/dist/cjs/bundle.cjs.map +4 -4
  3. package/dist/cjs/interpolationRuntime.cjs +1578 -0
  4. package/dist/cjs/interpolationRuntime.cjs.map +7 -0
  5. package/dist/cjs/interpolationSyntax.cjs +186 -0
  6. package/dist/cjs/interpolationSyntax.cjs.map +7 -0
  7. package/dist/cjs/webAppRuntime.cjs.map +2 -2
  8. package/dist/esm/exports.js +2 -1
  9. package/dist/esm/integrations/CodeRunner.js +5 -0
  10. package/dist/esm/integrations/mcp/MCPBase.js +16 -6
  11. package/dist/esm/interpolationRuntime.js +5 -0
  12. package/dist/esm/model/AgentResponseTrace.js +8 -3
  13. package/dist/esm/model/GraphBoundaryCache.js +0 -6
  14. package/dist/esm/model/GraphBoundaryEffects.js +5 -1
  15. package/dist/esm/model/GraphOutputSelection.js +34 -0
  16. package/dist/esm/model/GraphProcessor.js +177 -50
  17. package/dist/esm/model/ProcessContextBuilder.js +4 -1
  18. package/dist/esm/model/RecordingPlayer.js +46 -14
  19. package/dist/esm/model/SplitRunProcessor.js +37 -16
  20. package/dist/esm/model/StoredValueStore.js +4 -3
  21. package/dist/esm/model/UiGraphRendererStyles.js +20 -0
  22. package/dist/esm/model/chat/aiSdkStreaming.js +3 -2
  23. package/dist/esm/model/chat-v2/aiSdkBridge.js +63 -5
  24. package/dist/esm/model/chat-v2/chatV2Pipeline.js +66 -3
  25. package/dist/esm/model/chat-v2/llmInvocationResultProjector.js +20 -7
  26. package/dist/esm/model/chat-v2/llmProfileFallback.js +12 -4
  27. package/dist/esm/model/nodes/CodeNewNode.js +16 -16
  28. package/dist/esm/model/nodes/DestructureNode.js +2 -2
  29. package/dist/esm/model/nodes/ExpressionNode.js +16 -16
  30. package/dist/esm/model/nodes/ExtractObjectPathNode.js +2 -2
  31. package/dist/esm/model/nodes/ExtractYamlNode.js +5 -5
  32. package/dist/esm/model/nodes/JSFilterNode.js +3 -1
  33. package/dist/esm/model/nodes/JSMapNode.js +3 -1
  34. package/dist/esm/model/nodes/LLMChatV2Node.js +30 -4
  35. package/dist/esm/model/nodes/MCPGetPromptNode.js +6 -2
  36. package/dist/esm/model/nodes/MCPToolCallNode.js +6 -2
  37. package/dist/esm/model/nodes/ObjectNode.js +19 -20
  38. package/dist/esm/model/nodes/PromptNode.js +10 -11
  39. package/dist/esm/model/nodes/ReferencedGraphAliasNode.js +2 -40
  40. package/dist/esm/model/nodes/SubGraphNode.js +40 -26
  41. package/dist/esm/model/nodes/TextNode.js +11 -32
  42. package/dist/esm/model/nodes/ToTreeNode.js +6 -13
  43. package/dist/esm/model/nodes/ToolNode.js +33 -16
  44. package/dist/esm/model/nodes/jsListCallbackHelpers.js +19 -15
  45. package/dist/esm/model/nodes/jsValueInterpolation.js +83 -30
  46. package/dist/esm/model/nodes/nodeBodyPreview.js +23 -0
  47. package/dist/esm/plugins/openai/nodes/ThreadMessageNode.js +14 -11
  48. package/dist/esm/recording/ExecutionRecorder.js +55 -23
  49. package/dist/esm/utils/index.js +2 -0
  50. package/dist/esm/utils/interpolation.js +478 -221
  51. package/dist/esm/utils/interpolationSyntax.js +175 -0
  52. package/dist/esm/utils/jsonPath.js +129 -0
  53. package/dist/types/exports.d.ts +3 -1
  54. package/dist/types/integrations/CodeRunnerOptions.d.ts +9 -0
  55. package/dist/types/integrations/mcp/MCPBase.d.ts +3 -1
  56. package/dist/types/interpolationRuntime.d.ts +5 -0
  57. package/dist/types/model/AgentResponseTrace.d.ts +2 -0
  58. package/dist/types/model/ExecutorProtocol.d.ts +4 -0
  59. package/dist/types/model/GraphBoundaryCache.d.ts +0 -2
  60. package/dist/types/model/GraphOutputSelection.d.ts +8 -0
  61. package/dist/types/model/GraphProcessor.d.ts +12 -3
  62. package/dist/types/model/ProcessContext.d.ts +9 -2
  63. package/dist/types/model/ProcessContextBuilder.d.ts +2 -1
  64. package/dist/types/model/SplitRunProcessor.d.ts +2 -1
  65. package/dist/types/model/UiGraphRendererStyles.d.ts +1 -1
  66. package/dist/types/model/chat/aiSdkStreaming.d.ts +1 -1
  67. package/dist/types/model/chat-v2/chatV2RequestPlan.d.ts +1 -1
  68. package/dist/types/model/chat-v2/chatV2Types.d.ts +29 -0
  69. package/dist/types/model/chat-v2/llmInvocationResultProjector.d.ts +5 -1
  70. package/dist/types/model/nodes/ObjectNode.d.ts +1 -1
  71. package/dist/types/model/nodes/SubGraphNode.d.ts +2 -0
  72. package/dist/types/model/nodes/ToTreeNode.d.ts +5 -2
  73. package/dist/types/model/nodes/ToolNode.d.ts +13 -1
  74. package/dist/types/model/nodes/jsValueInterpolation.d.ts +16 -8
  75. package/dist/types/model/nodes/nodeBodyPreview.d.ts +7 -0
  76. package/dist/types/recording/ExecutionRecorder.d.ts +6 -1
  77. package/dist/types/recording/RecordedEvents.d.ts +2 -0
  78. package/dist/types/utils/index.d.ts +2 -0
  79. package/dist/types/utils/interpolation.d.ts +93 -8
  80. package/dist/types/utils/interpolationSyntax.d.ts +21 -0
  81. package/dist/types/utils/jsonPath.d.ts +15 -0
  82. package/package.json +11 -1
@@ -9,6 +9,7 @@ import { coerceTypeOptional } from '../utils/coerceType.js';
9
9
  import { getPluginConfig } from '../utils/index.js';
10
10
  import { isGraphExecutionPlan, preprocessGraphState, toReusableGraphExecutionPlan, } from './GraphPreprocessor.js';
11
11
  import { getGraphBoundary } from './GraphBoundaryCache.js';
12
+ import { createGraphOutputSelection } from './GraphOutputSelection.js';
12
13
  import { applyFrozenGraphBoundaryEffects, ensureGraphCostOutput } from './GraphBoundaryEffects.js';
13
14
  import { replayExecutionRecording } from './RecordingPlayer.js';
14
15
  import { didLoopControllerBreak, LOOP_NOT_BROKEN_SENTINEL } from './loopControllerBreak.js';
@@ -185,6 +186,7 @@ export class GraphProcessor {
185
186
  warnOnInvalidGraph = false;
186
187
  // Per-process state
187
188
  #erroredNodes = undefined; // Values are strings in recordings
189
+ #failureOutputsByProcessId = undefined;
188
190
  #remainingNodes = undefined;
189
191
  #visitedNodes = undefined;
190
192
  #currentlyProcessing = undefined;
@@ -213,6 +215,7 @@ export class GraphProcessor {
213
215
  #graphExecutionPlan;
214
216
  #nodeProcessContextBase = undefined;
215
217
  #runToRelevantNodeIds;
218
+ #graphOutputSelection;
216
219
  #managedAsyncBranches;
217
220
  #managedAsyncBranchFailures = [];
218
221
  #runCompletionPromise;
@@ -444,7 +447,6 @@ export class GraphProcessor {
444
447
  const pending = this.#pendingUserInputs[nodeId];
445
448
  if (pending) {
446
449
  pending.resolve(values);
447
- delete this.#pendingUserInputs[nodeId];
448
450
  }
449
451
  for (const processor of this.#subprocessors) {
450
452
  processor.userInput(nodeId, values);
@@ -547,7 +549,7 @@ export class GraphProcessor {
547
549
  this.#hasPreloadedData = true;
548
550
  // Preserve the historical ability to preload an idle processor as well as
549
551
  // inject a boundary into a run that has already initialized.
550
- if (this.#lifecycle.isRunning) {
552
+ if (this.#lifecycle.isRunning && this.#isNodeSelected(nodeId)) {
551
553
  this.#nodeResults.set(nodeId, data);
552
554
  this.#visitedNodes.add(nodeId);
553
555
  }
@@ -597,13 +599,16 @@ export class GraphProcessor {
597
599
  }
598
600
  }
599
601
  async replayRecording(recorder) {
602
+ if (this.#lifecycle.isRunning) {
603
+ throw new Error('Cannot process graph while already processing');
604
+ }
600
605
  // Playback is a new root run, even though its node/graph events originate
601
606
  // from a historic recording. Giving the processor a real identity here is
602
607
  // essential if the user aborts while playback is in progress: abort()
603
608
  // emits its own graph terminal outside RecordingPlayer's event adapter.
604
609
  this.#initializeExecutionIdentity();
605
610
  this.#initProcessState();
606
- this.#graphOutputs = await replayExecutionRecording({
611
+ return await replayExecutionRecording({
607
612
  emitter: this.#emitter,
608
613
  erroredNodes: this.#erroredNodes,
609
614
  graphInputs: this.#graphInputs,
@@ -631,7 +636,6 @@ export class GraphProcessor {
631
636
  visitedNodes: this.#visitedNodes,
632
637
  waitUntilUnpaused: () => this.#waitUntilUnpaused(),
633
638
  });
634
- return this.#graphOutputs;
635
639
  }
636
640
  #initProcessState() {
637
641
  this.#lifecycle.begin();
@@ -639,6 +643,7 @@ export class GraphProcessor {
639
643
  this.#visitedNodes = new Set(this.#preloadedNodeResults.keys());
640
644
  this.#hasPreloadedData = this.#preloadedNodeResults.size > 0;
641
645
  this.#erroredNodes = new Map();
646
+ this.#failureOutputsByProcessId = new Map();
642
647
  this.#currentlyProcessing = new Set();
643
648
  // A processor may be constructed with a reusable execution plan. In that
644
649
  // case preprocessing happened before this run, so retain the compiled
@@ -665,6 +670,7 @@ export class GraphProcessor {
665
670
  this.#ignoreNodes = new Set();
666
671
  this.#nodeProcessContextBase = undefined;
667
672
  this.#runToRelevantNodeIds = undefined;
673
+ this.#graphOutputSelection = undefined;
668
674
  if (!this.#isSubProcessor) {
669
675
  this.#managedAsyncBranches = new ManagedAsyncBranches();
670
676
  this.#managedAsyncBranchFailures = [];
@@ -690,12 +696,11 @@ export class GraphProcessor {
690
696
  /** Inputs to the main graph. You should pass all inputs required by the GraphInputNodes of the graph. */
691
697
  inputs = {},
692
698
  /** Contextual data available to all graphs and subgraphs. Kind of like react context, avoids drilling down data into subgraphs. Be careful when using it. */
693
- contextValues = {},
694
- /** Allows callers such as web apps to receive root outputs while managed async branches continue settling. */
695
- options = {}) {
699
+ contextValues = {}, options = {}) {
696
700
  if (this.#lifecycle.isRunning) {
697
701
  throw new Error('Cannot process graph while already processing');
698
702
  }
703
+ const requestedGraphOutputIds = options.requestedGraphOutputIds?.slice();
699
704
  let resolveOutputsReady;
700
705
  let rejectOutputsReady;
701
706
  const outputsReadyPromise = options.returnWhenGraphOutputsReady === true
@@ -717,6 +722,18 @@ export class GraphProcessor {
717
722
  this.#preprocessGraph();
718
723
  }
719
724
  this.#assertNoDataBusRunTargets();
725
+ if (requestedGraphOutputIds !== undefined) {
726
+ if (this.runToNodeIds !== undefined) {
727
+ throw new Error('requestedGraphOutputIds cannot be combined with runToNodeIds');
728
+ }
729
+ this.#graphOutputSelection = createGraphOutputSelection({ nodes: this.#executionGraphNodes, connections: this.#getEffectiveConnections() }, requestedGraphOutputIds, (node) => getInputNodesTo(this.#executionState, node));
730
+ for (const nodeId of this.#nodeResults.keys()) {
731
+ if (!this.#isNodeSelected(nodeId)) {
732
+ this.#nodeResults.delete(nodeId);
733
+ this.#visitedNodes.delete(nodeId);
734
+ }
735
+ }
736
+ }
720
737
  this.#prepareAsyncBranchTopology();
721
738
  }
722
739
  catch (error) {
@@ -898,12 +915,31 @@ export class GraphProcessor {
898
915
  this.#emitTraceEvent(message);
899
916
  },
900
917
  setStoredValue: (key, value) => this.#storedValueController.set(key, value),
901
- waitForGlobal: async (id) => {
918
+ waitForGlobal: async (id, signal = this.#abortController.signal) => {
919
+ if (signal.aborted) {
920
+ throw createGraphAbortErrorFromSignal(signal);
921
+ }
902
922
  if (this.#globals.has(id)) {
903
923
  return this.#globals.get(id);
904
924
  }
905
- await this.getRootProcessor().#emitter.once(`globalSet:${id}`);
906
- return this.#globals.get(id);
925
+ return await new Promise((resolve, reject) => {
926
+ const abortListener = () => {
927
+ cleanup();
928
+ reject(createGraphAbortErrorFromSignal(signal));
929
+ };
930
+ const unsubscribe = this.getRootProcessor().#emitter.on(`globalSet:${id}`, () => {
931
+ cleanup();
932
+ resolve(this.#globals.get(id));
933
+ });
934
+ const cleanup = () => {
935
+ unsubscribe();
936
+ signal.removeEventListener('abort', abortListener);
937
+ };
938
+ signal.addEventListener('abort', abortListener, { once: true });
939
+ if (signal.aborted) {
940
+ abortListener();
941
+ }
942
+ });
907
943
  },
908
944
  waitForStoredValue: (key, signal) => this.#storedValueController.waitForSet(key, signal),
909
945
  };
@@ -937,7 +973,7 @@ export class GraphProcessor {
937
973
  return;
938
974
  }
939
975
  for (const node of this.#executionGraphNodes) {
940
- if (!this.#nodeResults.has(node.id)) {
976
+ if (!this.#isNodeSelected(node.id) || !this.#nodeResults.has(node.id)) {
941
977
  continue;
942
978
  }
943
979
  if (this.#suppressedPreloadedNodeIds.has(node.id)) {
@@ -966,7 +1002,8 @@ export class GraphProcessor {
966
1002
  }
967
1003
  }
968
1004
  async #processCompatibleGraph() {
969
- await this.#queueStartNodes(getStartNodes(this.#executionState, this.#executionGraphNodes, this.runToNodeIds));
1005
+ await this.#queueStartNodes(this.#graphOutputSelection?.startNodes ??
1006
+ getStartNodes(this.#executionState, this.#executionGraphNodes, this.runToNodeIds));
970
1007
  await this.#processingQueue.onIdle();
971
1008
  this.#markUnqueuedNodesIgnored();
972
1009
  }
@@ -974,7 +1011,11 @@ export class GraphProcessor {
974
1011
  if (this.#scheduler !== 'fast-acyclic') {
975
1012
  return false;
976
1013
  }
977
- if (this.#hasPreloadedData || this.runToNodeIds || this.slowMode || this.#includeTrace) {
1014
+ if (this.#graphOutputSelection ||
1015
+ this.#hasPreloadedData ||
1016
+ this.runToNodeIds ||
1017
+ this.slowMode ||
1018
+ this.#includeTrace) {
978
1019
  return false;
979
1020
  }
980
1021
  if (this.#nodesNotInCycle.length !== this.#executionGraphNodes.length) {
@@ -1121,7 +1162,6 @@ export class GraphProcessor {
1121
1162
  }
1122
1163
  async #finalizeGraphRun() {
1123
1164
  const outputValues = this.#graphOutputs;
1124
- this.#lifecycle.complete();
1125
1165
  if (this.#suppressGraphLifecycleEvents) {
1126
1166
  return outputValues;
1127
1167
  }
@@ -1129,7 +1169,6 @@ export class GraphProcessor {
1129
1169
  await this.#emitter.emit('graphFinish', this.#withExecution({ graph: this.#graph, outputs: outputValues }));
1130
1170
  if (!this.#isSubProcessor) {
1131
1171
  await this.#emitter.emit('done', { results: outputValues });
1132
- await this.#emitFinishIfNeeded();
1133
1172
  }
1134
1173
  return outputValues;
1135
1174
  }
@@ -1177,6 +1216,8 @@ export class GraphProcessor {
1177
1216
  }
1178
1217
  }
1179
1218
  async #fetchNodeDataAndProcessNode(node) {
1219
+ if (!this.#isNodeSelected(node.id))
1220
+ return;
1180
1221
  const profileStart = this.#startRuntimeProfile();
1181
1222
  try {
1182
1223
  if (this.#currentlyProcessing.has(node.id)) {
@@ -1228,6 +1269,8 @@ export class GraphProcessor {
1228
1269
  }
1229
1270
  /** If all inputs are present, all conditions met, processes the node. */
1230
1271
  async #processNodeIfAllInputsAvailable(node, options = {}) {
1272
+ if (!this.#isNodeSelected(node.id))
1273
+ return [];
1231
1274
  const { queueOutputNodes = true } = options;
1232
1275
  const builtInNode = node;
1233
1276
  const inputNodesProfileStart = this.#startRuntimeProfile();
@@ -1458,7 +1501,10 @@ export class GraphProcessor {
1458
1501
  }
1459
1502
  }
1460
1503
  #queueOutputNodes(node, outputNodes) {
1461
- void this.#processingQueue.addAll(outputNodes.map((outputNode) => async () => {
1504
+ const selectedOutputs = this.#graphOutputSelection
1505
+ ? outputNodes.filter((outputNode) => this.#isNodeSelected(outputNode.id))
1506
+ : outputNodes;
1507
+ void this.#processingQueue.addAll(selectedOutputs.map((outputNode) => async () => {
1462
1508
  this.#emitTraceEvent(`Trying to run output node from ${node.title}: ${outputNode.title} (${outputNode.id})`);
1463
1509
  await this.#processNodeIfAllInputsAvailable(outputNode);
1464
1510
  }));
@@ -1654,7 +1700,8 @@ export class GraphProcessor {
1654
1700
  accumulateCost: (output) => this.#accumulateCost(output),
1655
1701
  setNodeResults: (nodeId, outputs) => this.#nodeResults.set(nodeId, outputs),
1656
1702
  markNodeVisited: (nodeId) => this.#visitedNodes.add(nodeId),
1657
- nodeErrored: (n, err, pid, durationMs, splitRunDurationMs, resultOrigin) => this.#nodeErrored(n, err, pid, durationMs, splitRunDurationMs, resultOrigin),
1703
+ nodeErrored: (n, err, pid, durationMs, splitRunDurationMs, resultOrigin, splitOutputs) => this.#nodeErrored(n, err, pid, durationMs, splitRunDurationMs, resultOrigin, undefined, splitOutputs),
1704
+ takeFailureOutputs: (pid, index) => this.#takeFailureOutputs(pid, index),
1658
1705
  isAborted: () => this.#lifecycle.isAborted,
1659
1706
  getAbortError: () => createGraphAbortErrorFromSignal(this.#abortController.signal),
1660
1707
  emit: (event, data) => {
@@ -1736,11 +1783,15 @@ export class GraphProcessor {
1736
1783
  }, this.#finishNodeTiming(timingStart))));
1737
1784
  }
1738
1785
  catch (error) {
1739
- await this.#nodeErrored(node, error, processId, this.#finishNodeTiming(timingStart), undefined, resultOrigin);
1786
+ await this.#nodeErrored(node, error, processId, this.#finishNodeTiming(timingStart), undefined, resultOrigin, this.#takeFailureOutputs(processId, 0));
1740
1787
  }
1741
1788
  }
1742
- async #nodeErrored(node, e, processId, durationMs, splitRunDurationMs, resultOrigin = 'executed') {
1789
+ async #nodeErrored(node, e, processId, durationMs, splitRunDurationMs, resultOrigin = 'executed', outputs, splitOutputs) {
1743
1790
  const error = getError(e);
1791
+ // Most callers are ordinary (non-split) node executions. Let those retain
1792
+ // any checkpoint without requiring every error path to know about the
1793
+ // internal map. Split callers provide their per-index map explicitly.
1794
+ const retainedOutputs = splitOutputs === undefined ? (outputs ?? this.#takeFailureOutputs(processId, 0)) : outputs;
1744
1795
  const exclusionReason = this.#getErrorExclusionReason(node, error, processId);
1745
1796
  if (exclusionReason) {
1746
1797
  await this.#emitNodeExcluded(node, processId, this.#getInputValuesForNode(node), exclusionReason, resultOrigin);
@@ -1748,12 +1799,39 @@ export class GraphProcessor {
1748
1799
  return;
1749
1800
  }
1750
1801
  this.#erroredNodes.set(node.id, error);
1751
- await this.#emitter.emit('nodeError', this.#withExecution(withOptionalDuration({ node, error, processId, resultOrigin }, durationMs, splitRunDurationMs)));
1802
+ await this.#emitter.emit('nodeError', this.#withExecution(withOptionalDuration({
1803
+ node,
1804
+ error,
1805
+ processId,
1806
+ resultOrigin,
1807
+ ...(retainedOutputs === undefined ? {} : { outputs: retainedOutputs }),
1808
+ ...(splitOutputs === undefined ? {} : { splitOutputs }),
1809
+ }, durationMs, splitRunDurationMs)));
1752
1810
  this.#emitTraceEvent(`Node ${node.title} (${node.id}-${processId}) errored: ${error.stack}`);
1753
1811
  }
1754
1812
  #emitNodePartialOutput(node, outputs, index, processId) {
1755
1813
  emitDetached(this.#emitter, 'partialOutput', this.#withExecution({ node, outputs, index, processId, resultOrigin: 'executed' }));
1756
1814
  }
1815
+ #setFailureOutputs(processId, index, outputs) {
1816
+ let outputsByIndex = this.#failureOutputsByProcessId.get(processId);
1817
+ if (!outputsByIndex) {
1818
+ outputsByIndex = new Map();
1819
+ this.#failureOutputsByProcessId.set(processId, outputsByIndex);
1820
+ }
1821
+ outputsByIndex.set(index, outputs);
1822
+ }
1823
+ #takeFailureOutputs(processId, index) {
1824
+ const outputsByIndex = this.#failureOutputsByProcessId.get(processId);
1825
+ const outputs = outputsByIndex?.get(index);
1826
+ outputsByIndex?.delete(index);
1827
+ if (outputsByIndex?.size === 0) {
1828
+ this.#failureOutputsByProcessId.delete(processId);
1829
+ }
1830
+ return outputs;
1831
+ }
1832
+ #discardFailureOutputs(processId, index) {
1833
+ this.#takeFailureOutputs(processId, index);
1834
+ }
1757
1835
  #getErrorExclusionReason(node, error, processId) {
1758
1836
  if (this.#getAttachedDataTo(node).races?.completed) {
1759
1837
  return RACE_LOSER_EXCLUSION_REASON;
@@ -1856,6 +1934,11 @@ export class GraphProcessor {
1856
1934
  }
1857
1935
  }
1858
1936
  async #processNodeWithInputData(node, inputValues, index, processId, partialOutput, markResultAsEditorCacheHit) {
1937
+ // Split items enter here independently. A race can finish after the caller
1938
+ // started but before a queued item receives its own abort controller.
1939
+ if (this.#shouldSkipCompletedRaceNode(node, this.#getAttachedDataTo(node))) {
1940
+ throw createGraphAbortError(createGraphAbortReason(true, RACE_LOSER_EXCLUSION_REASON));
1941
+ }
1859
1942
  const instance = this.#nodeInstances[node.id];
1860
1943
  const nodeAbortController = this.#newAbortController();
1861
1944
  this.#registerNodeAbortController(node.id, nodeAbortController);
@@ -1873,6 +1956,9 @@ export class GraphProcessor {
1873
1956
  this.#finishRuntimeProfile('createNodeProcessContext', createContextProfileStart);
1874
1957
  }
1875
1958
  await this.#waitUntilUnpaused();
1959
+ if (nodeAbortController.signal.aborted) {
1960
+ throw createGraphAbortErrorFromSignal(nodeAbortController.signal);
1961
+ }
1876
1962
  const implementationProfileStart = this.#startRuntimeProfile();
1877
1963
  let results;
1878
1964
  try {
@@ -1885,6 +1971,7 @@ export class GraphProcessor {
1885
1971
  if (nodeAbortController.signal.aborted) {
1886
1972
  if (isSuccessfulNonRaceGraphAbortReason(abortReason)) {
1887
1973
  this.#successfulAbortTerminalProcessIds.add(processId);
1974
+ this.#discardFailureOutputs(processId, index);
1888
1975
  return results;
1889
1976
  }
1890
1977
  else {
@@ -1893,6 +1980,7 @@ export class GraphProcessor {
1893
1980
  }
1894
1981
  this.#finalizeToolCallContinuation(processId, index, results);
1895
1982
  continuationFinalized = true;
1983
+ this.#discardFailureOutputs(processId, index);
1896
1984
  return results;
1897
1985
  }
1898
1986
  finally {
@@ -1924,8 +2012,9 @@ export class GraphProcessor {
1924
2012
  partialOutput?.(node, partialOutputs, index);
1925
2013
  this.#emitGraphPartialOutputIfNeeded(node, partialOutputs);
1926
2014
  },
2015
+ setFailureOutputs: (outputs) => this.#setFailureOutputs(processId, index, outputs),
1927
2016
  processId,
1928
- requestUserInput: async (inputStrings, renderingType) => this.#requestUserInput(node, inputStrings, inputValues, renderingType, processId),
2017
+ requestUserInput: async (inputStrings, renderingType) => this.#requestUserInput(node, inputStrings, inputValues, renderingType, processId, nodeAbortController.signal),
1929
2018
  reportProgress: (progress) => {
1930
2019
  const normalized = normalizeGraphProgress(progress);
1931
2020
  if (normalized) {
@@ -1940,18 +2029,27 @@ export class GraphProcessor {
1940
2029
  toolCallContinuation,
1941
2030
  toolCallTraceSource,
1942
2031
  waitEvent: async (event) => {
2032
+ const signal = nodeAbortController.signal;
2033
+ if (signal.aborted) {
2034
+ throw createGraphAbortErrorFromSignal(signal, 'Process aborted');
2035
+ }
1943
2036
  return new Promise((resolve, reject) => {
1944
2037
  const abortListener = () => {
1945
- reject(createGraphAbortErrorFromSignal(nodeAbortController.signal, 'Process aborted'));
2038
+ cleanup();
2039
+ reject(createGraphAbortErrorFromSignal(signal, 'Process aborted'));
1946
2040
  };
1947
- this.#emitter
1948
- .once(`userEvent:${event}`)
1949
- .then(resolve)
1950
- .catch(reject)
1951
- .finally(() => {
1952
- nodeAbortController.signal.removeEventListener('abort', abortListener);
2041
+ const unsubscribe = this.#emitter.on(`userEvent:${event}`, (data) => {
2042
+ cleanup();
2043
+ resolve(data);
1953
2044
  });
1954
- nodeAbortController.signal.addEventListener('abort', abortListener, { once: true });
2045
+ const cleanup = () => {
2046
+ unsubscribe();
2047
+ signal.removeEventListener('abort', abortListener);
2048
+ };
2049
+ signal.addEventListener('abort', abortListener, { once: true });
2050
+ if (signal.aborted) {
2051
+ abortListener();
2052
+ }
1955
2053
  });
1956
2054
  },
1957
2055
  });
@@ -2207,7 +2305,7 @@ export class GraphProcessor {
2207
2305
  state: {
2208
2306
  erroredNodeIds: new Set(this.#erroredNodes.keys()),
2209
2307
  nodeOutputs: this.#nodeResults,
2210
- runToRelevantNodeIds: this.#getRunToRelevantNodeIds(),
2308
+ runToRelevantNodeIds: this.#getExecutionRelevantNodeIds(),
2211
2309
  visitedNodeIds: this.#visitedNodes,
2212
2310
  },
2213
2311
  });
@@ -2264,8 +2362,8 @@ export class GraphProcessor {
2264
2362
  const connections = this.#getEffectiveConnections().filter((connection) => this.#isDefinitionValidConnection(connection));
2265
2363
  const outgoingByNodeId = new Map();
2266
2364
  const incomingByNodeId = new Map();
2267
- const runToRelevantNodeIds = this.#getRunToRelevantNodeIds();
2268
- const isRunToRelevant = (nodeId) => !runToRelevantNodeIds || runToRelevantNodeIds.has(nodeId);
2365
+ const relevantNodeIds = this.#getExecutionRelevantNodeIds();
2366
+ const isRelevant = (nodeId) => !relevantNodeIds || relevantNodeIds.has(nodeId);
2269
2367
  for (const connection of connections) {
2270
2368
  const outgoing = outgoingByNodeId.get(connection.outputNodeId) ?? [];
2271
2369
  outgoing.push(connection);
@@ -2275,7 +2373,7 @@ export class GraphProcessor {
2275
2373
  incomingByNodeId.set(connection.inputNodeId, incoming);
2276
2374
  }
2277
2375
  for (const triggerNode of Object.values(this.#nodesById)) {
2278
- if (triggerNode.type !== 'startBackgroundBranch' || triggerNode.disabled || !isRunToRelevant(triggerNode.id)) {
2376
+ if (triggerNode.type !== 'startBackgroundBranch' || triggerNode.disabled || !isRelevant(triggerNode.id)) {
2279
2377
  continue;
2280
2378
  }
2281
2379
  if (triggerNode.id !== this.#consumedAsyncBranchTriggerNodeId && this.#nodeResults.has(triggerNode.id)) {
@@ -2284,7 +2382,7 @@ export class GraphProcessor {
2284
2382
  const nodeIds = new Set();
2285
2383
  const pendingNodeIds = (outgoingByNodeId.get(triggerNode.id) ?? [])
2286
2384
  .map((connection) => connection.inputNodeId)
2287
- .filter(isRunToRelevant);
2385
+ .filter(isRelevant);
2288
2386
  while (pendingNodeIds.length > 0) {
2289
2387
  const nodeId = pendingNodeIds.pop();
2290
2388
  if (nodeId === triggerNode.id) {
@@ -2305,7 +2403,7 @@ export class GraphProcessor {
2305
2403
  }
2306
2404
  nodeIds.add(nodeId);
2307
2405
  for (const connection of outgoingByNodeId.get(nodeId) ?? []) {
2308
- if (isRunToRelevant(connection.inputNodeId)) {
2406
+ if (isRelevant(connection.inputNodeId)) {
2309
2407
  pendingNodeIds.push(connection.inputNodeId);
2310
2408
  }
2311
2409
  }
@@ -2404,17 +2502,19 @@ export class GraphProcessor {
2404
2502
  if (outputConnections.length === 0) {
2405
2503
  return new Set();
2406
2504
  }
2407
- const runToRelevantNodeIds = this.#getRunToRelevantNodeIds();
2505
+ const relevantNodeIds = this.#getExecutionRelevantNodeIds();
2408
2506
  const activeOutputPortIds = new Set();
2409
2507
  for (const { node: outputNode, connections } of outputConnections) {
2410
2508
  if (outputNode.disabled) {
2411
2509
  continue;
2412
2510
  }
2413
- if (runToRelevantNodeIds && !runToRelevantNodeIds.has(outputNode.id)) {
2511
+ if (relevantNodeIds && !relevantNodeIds.has(outputNode.id)) {
2414
2512
  continue;
2415
2513
  }
2416
2514
  for (const connection of connections) {
2417
- activeOutputPortIds.add(connection.outputId);
2515
+ if (this.#isDefinitionValidConnection(connection)) {
2516
+ activeOutputPortIds.add(connection.outputId);
2517
+ }
2418
2518
  }
2419
2519
  }
2420
2520
  return activeOutputPortIds;
@@ -2430,6 +2530,15 @@ export class GraphProcessor {
2430
2530
  }
2431
2531
  }
2432
2532
  }
2533
+ #isNodeSelected(nodeId) {
2534
+ return !this.#graphOutputSelection || this.#graphOutputSelection.nodeIds.has(nodeId);
2535
+ }
2536
+ #getExecutionRelevantNodeIds() {
2537
+ if (this.#sameGraphRunOwnerOverride) {
2538
+ return this.#sameGraphRunOwnerOverride.#getExecutionRelevantNodeIds();
2539
+ }
2540
+ return this.#graphOutputSelection?.nodeIds ?? this.#getRunToRelevantNodeIds();
2541
+ }
2433
2542
  #getRunToRelevantNodeIds() {
2434
2543
  if (!this.runToNodeIds) {
2435
2544
  return undefined;
@@ -2551,27 +2660,45 @@ export class GraphProcessor {
2551
2660
  }
2552
2661
  return processor;
2553
2662
  }
2554
- async #requestUserInput(node, inputStrings, inputValues, renderingType, processId) {
2663
+ async #requestUserInput(node, inputStrings, inputValues, renderingType, processId, signal) {
2664
+ if (signal.aborted) {
2665
+ throw createGraphAbortErrorFromSignal(signal);
2666
+ }
2555
2667
  return await new Promise((resolve, reject) => {
2668
+ let settled = false;
2669
+ const cleanup = () => {
2670
+ signal.removeEventListener('abort', abortListener);
2671
+ if (this.#pendingUserInputs[node.id] === pending) {
2672
+ delete this.#pendingUserInputs[node.id];
2673
+ }
2674
+ };
2556
2675
  const abortListener = () => {
2557
- delete this.#pendingUserInputs[node.id];
2558
- reject(createGraphAbortErrorFromSignal(this.#abortController.signal));
2676
+ if (settled)
2677
+ return;
2678
+ settled = true;
2679
+ cleanup();
2680
+ reject(createGraphAbortErrorFromSignal(signal));
2559
2681
  };
2560
- this.#pendingUserInputs[node.id] = {
2561
- resolve,
2562
- reject,
2682
+ const respond = (results) => {
2683
+ if (settled)
2684
+ return;
2685
+ settled = true;
2686
+ cleanup();
2687
+ resolve(results);
2563
2688
  };
2564
- this.#abortController.signal.addEventListener('abort', abortListener, { once: true });
2689
+ const pending = { resolve: respond };
2690
+ this.#pendingUserInputs[node.id] = pending;
2691
+ signal.addEventListener('abort', abortListener, { once: true });
2692
+ if (signal.aborted) {
2693
+ abortListener();
2694
+ return;
2695
+ }
2565
2696
  emitDetached(this.#emitter, 'userInput', this.#withExecution({
2566
2697
  node,
2567
2698
  inputStrings,
2568
2699
  inputs: inputValues,
2569
2700
  renderingType,
2570
- callback: (results) => {
2571
- this.#abortController.signal.removeEventListener('abort', abortListener);
2572
- resolve(results);
2573
- delete this.#pendingUserInputs[node.id];
2574
- },
2701
+ callback: respond,
2575
2702
  processId,
2576
2703
  }));
2577
2704
  });
@@ -1,5 +1,5 @@
1
1
  export function buildNodeProcessContext(options) {
2
- const { attachedData, activeOutputPortIds, base, createSubProcessor, execution, externalFunctions, getPluginConfig, isDirectRunTarget, markResultAsEditorCacheHit, node, nodeAbortController, onPartialOutputs, processId, requestUserInput, reportProgress, setGlobal, splitIndex, toolCallContinuation, toolCallTraceSource, waitEvent, } = options;
2
+ const { attachedData, activeOutputPortIds, base, createSubProcessor, execution, externalFunctions, getPluginConfig, isDirectRunTarget, markResultAsEditorCacheHit, node, nodeAbortController, onPartialOutputs, setFailureOutputs, processId, requestUserInput, reportProgress, setGlobal, splitIndex, toolCallContinuation, toolCallTraceSource, waitEvent, } = options;
3
3
  return {
4
4
  ...base,
5
5
  node,
@@ -8,8 +8,11 @@ export function buildNodeProcessContext(options) {
8
8
  isDirectRunTarget,
9
9
  markResultAsEditorCacheHit,
10
10
  waitEvent,
11
+ waitForGlobal: (id, signal = nodeAbortController.signal) => base.waitForGlobal(id, signal),
12
+ waitForStoredValue: (key, signal = nodeAbortController.signal) => base.waitForStoredValue(key, signal),
11
13
  externalFunctions: { ...externalFunctions },
12
14
  onPartialOutputs,
15
+ setFailureOutputs,
13
16
  signal: nodeAbortController.signal,
14
17
  processId,
15
18
  splitIndex,