@workglow/task-graph 0.0.99 → 0.0.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/node.js CHANGED
@@ -47,10 +47,8 @@ class Dataflow {
47
47
  if (!this.stream)
48
48
  return;
49
49
  const reader = this.stream.getReader();
50
- const accumulatedPorts = new Map;
51
50
  let lastSnapshotData = undefined;
52
51
  let finishData = undefined;
53
- let hasTextDelta = false;
54
52
  let streamError;
55
53
  try {
56
54
  while (true) {
@@ -58,16 +56,6 @@ class Dataflow {
58
56
  if (done)
59
57
  break;
60
58
  switch (event.type) {
61
- case "text-delta": {
62
- if (this.sourceTaskPortId !== DATAFLOW_ALL_PORTS && event.port !== this.sourceTaskPortId) {
63
- break;
64
- }
65
- hasTextDelta = true;
66
- accumulatedPorts.set(event.port, (accumulatedPorts.get(event.port) ?? "") + event.textDelta);
67
- break;
68
- }
69
- case "object-delta":
70
- break;
71
59
  case "snapshot":
72
60
  lastSnapshotData = event.data;
73
61
  break;
@@ -90,19 +78,8 @@ class Dataflow {
90
78
  }
91
79
  if (lastSnapshotData !== undefined) {
92
80
  this.setPortData(lastSnapshotData);
93
- } else if (finishData && Object.keys(finishData).length > 0) {
81
+ } else if (finishData !== undefined) {
94
82
  this.setPortData(finishData);
95
- } else if (hasTextDelta) {
96
- if (this.sourceTaskPortId === DATAFLOW_ALL_PORTS) {
97
- const obj = {};
98
- for (const [port, text] of accumulatedPorts) {
99
- obj[port] = text;
100
- }
101
- this.value = obj;
102
- } else {
103
- const text = accumulatedPorts.values().next().value ?? "";
104
- this.value = text;
105
- }
106
83
  }
107
84
  }
108
85
  reset() {
@@ -548,6 +525,7 @@ class TaskRunner {
548
525
  outputCache;
549
526
  registry = globalServiceRegistry;
550
527
  inputStreams;
528
+ shouldAccumulate = true;
551
529
  constructor(task) {
552
530
  this.task = task;
553
531
  this.own = this.own.bind(this);
@@ -656,7 +634,7 @@ class TaskRunner {
656
634
  throw new TaskError(`Task ${this.task.type} declares append streaming but no output port has x-stream: "append"`);
657
635
  }
658
636
  }
659
- const accumulated = new Map;
637
+ const accumulated = this.shouldAccumulate ? new Map : undefined;
660
638
  let chunkCount = 0;
661
639
  let finalOutput;
662
640
  this.task.emit("stream_start");
@@ -676,33 +654,40 @@ class TaskRunner {
676
654
  if (event.type === "snapshot") {
677
655
  this.task.runOutputData = event.data;
678
656
  }
679
- this.task.emit("stream_chunk", event);
680
657
  switch (event.type) {
681
658
  case "text-delta": {
682
- accumulated.set(event.port, (accumulated.get(event.port) ?? "") + event.textDelta);
659
+ if (accumulated) {
660
+ accumulated.set(event.port, (accumulated.get(event.port) ?? "") + event.textDelta);
661
+ }
662
+ this.task.emit("stream_chunk", event);
683
663
  const progress = Math.min(99, Math.round(100 * (1 - Math.exp(-0.05 * chunkCount))));
684
664
  await this.handleProgress(progress);
685
665
  break;
686
666
  }
687
667
  case "object-delta": {
668
+ this.task.emit("stream_chunk", event);
688
669
  const progress = Math.min(99, Math.round(100 * (1 - Math.exp(-0.05 * chunkCount))));
689
670
  await this.handleProgress(progress);
690
671
  break;
691
672
  }
692
673
  case "snapshot": {
674
+ this.task.emit("stream_chunk", event);
693
675
  const progress = Math.min(99, Math.round(100 * (1 - Math.exp(-0.05 * chunkCount))));
694
676
  await this.handleProgress(progress);
695
677
  break;
696
678
  }
697
679
  case "finish": {
698
- if (streamMode === "append") {
680
+ if (accumulated) {
699
681
  const merged = { ...event.data || {} };
700
682
  for (const [port, text] of accumulated) {
701
- merged[port] = text.length > 0 ? text : event.data?.[port] ?? "";
683
+ if (text.length > 0)
684
+ merged[port] = text;
702
685
  }
703
686
  finalOutput = merged;
704
- } else if (streamMode === "replace") {
687
+ this.task.emit("stream_chunk", { type: "finish", data: merged });
688
+ } else {
705
689
  finalOutput = event.data;
690
+ this.task.emit("stream_chunk", event);
706
691
  }
707
692
  break;
708
693
  }
@@ -741,6 +726,7 @@ class TaskRunner {
741
726
  } else if (cache instanceof TaskOutputRepository) {
742
727
  this.outputCache = cache;
743
728
  }
729
+ this.shouldAccumulate = config.shouldAccumulate !== false;
744
730
  if (config.updateProgress) {
745
731
  this.updateProgress = config.updateProgress;
746
732
  }
@@ -1552,6 +1538,7 @@ class TaskGraphRunner {
1552
1538
  reactiveRunning = false;
1553
1539
  graph;
1554
1540
  outputCache;
1541
+ accumulateLeafOutputs = true;
1555
1542
  registry = globalServiceRegistry2;
1556
1543
  abortController;
1557
1544
  inProgressTasks = new Map;
@@ -1771,6 +1758,29 @@ class TaskGraphRunner {
1771
1758
  }
1772
1759
  }
1773
1760
  }
1761
+ taskNeedsAccumulation(task) {
1762
+ if (this.outputCache)
1763
+ return true;
1764
+ const outEdges = this.graph.getTargetDataflows(task.config.id);
1765
+ if (outEdges.length === 0)
1766
+ return this.accumulateLeafOutputs;
1767
+ const outSchema = task.outputSchema();
1768
+ for (const df of outEdges) {
1769
+ if (df.sourceTaskPortId === DATAFLOW_ALL_PORTS) {
1770
+ if (getStreamingPorts(outSchema).length > 0)
1771
+ return true;
1772
+ continue;
1773
+ }
1774
+ const targetTask = this.graph.getTask(df.targetTaskId);
1775
+ if (!targetTask)
1776
+ continue;
1777
+ const inSchema = targetTask.inputSchema();
1778
+ if (edgeNeedsAccumulation(outSchema, df.sourceTaskPortId, inSchema, df.targetTaskPortId)) {
1779
+ return true;
1780
+ }
1781
+ }
1782
+ return false;
1783
+ }
1774
1784
  async runTask(task, input) {
1775
1785
  const isStreamable = isTaskStreamable(task);
1776
1786
  if (isStreamable) {
@@ -1793,7 +1803,7 @@ class TaskGraphRunner {
1793
1803
  return this.runStreamingTask(task, input);
1794
1804
  }
1795
1805
  const results = await task.runner.run(input, {
1796
- outputCache: this.outputCache,
1806
+ outputCache: this.outputCache ?? false,
1797
1807
  updateProgress: async (task2, progress, message, ...args) => await this.handleProgress(task2, progress, message, ...args),
1798
1808
  registry: this.registry
1799
1809
  });
@@ -1813,6 +1823,7 @@ class TaskGraphRunner {
1813
1823
  }
1814
1824
  async runStreamingTask(task, input) {
1815
1825
  const streamMode = getOutputStreamMode(task.outputSchema());
1826
+ const shouldAccumulate = this.taskNeedsAccumulation(task);
1816
1827
  let streamingNotified = false;
1817
1828
  const onStatus = (status) => {
1818
1829
  if (status === TaskStatus.STREAMING && !streamingNotified) {
@@ -1837,7 +1848,8 @@ class TaskGraphRunner {
1837
1848
  task.on("stream_end", onStreamEnd);
1838
1849
  try {
1839
1850
  const results = await task.runner.run(input, {
1840
- outputCache: this.outputCache,
1851
+ outputCache: this.outputCache ?? false,
1852
+ shouldAccumulate,
1841
1853
  updateProgress: async (task2, progress, message, ...args) => await this.handleProgress(task2, progress, message, ...args),
1842
1854
  registry: this.registry
1843
1855
  });
@@ -1945,6 +1957,7 @@ class TaskGraphRunner {
1945
1957
  } else {
1946
1958
  this.registry = new ServiceRegistry2(globalServiceRegistry2.container.createChildContainer());
1947
1959
  }
1960
+ this.accumulateLeafOutputs = config?.accumulateLeafOutputs !== false;
1948
1961
  if (config?.outputCache !== undefined) {
1949
1962
  if (typeof config.outputCache === "boolean") {
1950
1963
  if (config.outputCache === true) {
@@ -2276,7 +2289,7 @@ class GraphAsTask extends Task {
2276
2289
  }
2277
2290
  }
2278
2291
  });
2279
- const runPromise = this.subGraph.run(input, { parentSignal: context.signal }).then((results2) => {
2292
+ const runPromise = this.subGraph.run(input, { parentSignal: context.signal, accumulateLeafOutputs: false }).then((results2) => {
2280
2293
  subgraphDone = true;
2281
2294
  resolveWaiting?.();
2282
2295
  return results2;
@@ -3083,7 +3096,8 @@ class TaskGraph {
3083
3096
  run(input = {}, config = {}) {
3084
3097
  return this.runner.runGraph(input, {
3085
3098
  outputCache: config?.outputCache || this.outputCache,
3086
- parentSignal: config?.parentSignal || undefined
3099
+ parentSignal: config?.parentSignal || undefined,
3100
+ accumulateLeafOutputs: config?.accumulateLeafOutputs
3087
3101
  });
3088
3102
  }
3089
3103
  runReactive(input = {}) {
@@ -5187,4 +5201,4 @@ export {
5187
5201
  ConditionalTask
5188
5202
  };
5189
5203
 
5190
- //# debugId=D1D0735E2B61108764756E2164756E21
5204
+ //# debugId=58638F6F12E25A3464756E2164756E21