@workglow/task-graph 0.0.120 → 0.0.122

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 (48) hide show
  1. package/dist/browser.js +99 -58
  2. package/dist/browser.js.map +8 -8
  3. package/dist/bun.js +99 -58
  4. package/dist/bun.js.map +8 -8
  5. package/dist/node.js +99 -58
  6. package/dist/node.js.map +8 -8
  7. package/dist/storage/TaskGraphRepository.d.ts.map +1 -1
  8. package/dist/storage/TaskGraphTabularRepository.d.ts.map +1 -1
  9. package/dist/storage/TaskOutputRepository.d.ts.map +1 -1
  10. package/dist/storage/TaskOutputTabularRepository.d.ts.map +1 -1
  11. package/dist/task/ConditionalTask.d.ts.map +1 -1
  12. package/dist/task/FallbackTask.d.ts +44 -44
  13. package/dist/task/FallbackTask.d.ts.map +1 -1
  14. package/dist/task/FallbackTaskRunner.d.ts +0 -8
  15. package/dist/task/FallbackTaskRunner.d.ts.map +1 -1
  16. package/dist/task/GraphAsTask.d.ts +1 -1
  17. package/dist/task/GraphAsTask.d.ts.map +1 -1
  18. package/dist/task/GraphAsTaskRunner.d.ts.map +1 -1
  19. package/dist/task/IteratorTask.d.ts +12 -12
  20. package/dist/task/IteratorTask.d.ts.map +1 -1
  21. package/dist/task/IteratorTaskRunner.d.ts +9 -1
  22. package/dist/task/IteratorTaskRunner.d.ts.map +1 -1
  23. package/dist/task/JobQueueTask.d.ts +9 -9
  24. package/dist/task/JobQueueTask.d.ts.map +1 -1
  25. package/dist/task/MapTask.d.ts +18 -18
  26. package/dist/task/MapTask.d.ts.map +1 -1
  27. package/dist/task/ReduceTask.d.ts +13 -13
  28. package/dist/task/ReduceTask.d.ts.map +1 -1
  29. package/dist/task/Task.d.ts +2 -1
  30. package/dist/task/Task.d.ts.map +1 -1
  31. package/dist/task/TaskError.d.ts.map +1 -1
  32. package/dist/task/TaskEvents.d.ts +14 -0
  33. package/dist/task/TaskEvents.d.ts.map +1 -1
  34. package/dist/task/TaskJSON.d.ts +4 -4
  35. package/dist/task/TaskJSON.d.ts.map +1 -1
  36. package/dist/task/TaskQueueRegistry.d.ts.map +1 -1
  37. package/dist/task/TaskRunner.d.ts.map +1 -1
  38. package/dist/task/WhileTask.d.ts +21 -21
  39. package/dist/task/WhileTask.d.ts.map +1 -1
  40. package/dist/task/WhileTaskRunner.d.ts.map +1 -1
  41. package/dist/task/index.d.ts +1 -1
  42. package/dist/task-graph/Dataflow.d.ts.map +1 -1
  43. package/dist/task-graph/TaskGraph.d.ts.map +1 -1
  44. package/dist/task-graph/TaskGraphRunner.d.ts +8 -2
  45. package/dist/task-graph/TaskGraphRunner.d.ts.map +1 -1
  46. package/dist/task-graph/TaskGraphScheduler.d.ts.map +1 -1
  47. package/dist/task-graph/Workflow.d.ts.map +1 -1
  48. package/package.json +19 -14
package/dist/node.js CHANGED
@@ -82,7 +82,8 @@ var init_TaskTypes = __esm(() => {
82
82
  });
83
83
 
84
84
  // src/task-graph/Dataflow.ts
85
- import { areSemanticallyCompatible, EventEmitter } from "@workglow/util";
85
+ import { areSemanticallyCompatible } from "@workglow/schema";
86
+ import { EventEmitter } from "@workglow/util";
86
87
 
87
88
  class Dataflow {
88
89
  sourceTaskId;
@@ -913,7 +914,6 @@ function getStructuredOutputSchemas(schema) {
913
914
  function hasStructuredOutput(schema) {
914
915
  return getStructuredOutputSchemas(schema).size > 0;
915
916
  }
916
- var init_StreamTypes = () => {};
917
917
 
918
918
  // src/task/TaskRunner.ts
919
919
  import {
@@ -1292,22 +1292,21 @@ class TaskRunner {
1292
1292
  }
1293
1293
  async handleProgress(progress, message, ...args) {
1294
1294
  this.task.progress = progress;
1295
- await this.updateProgress(this.task, progress, message, ...args);
1296
1295
  this.task.emit("progress", progress, message, ...args);
1296
+ await this.updateProgress(this.task, progress, message, ...args);
1297
1297
  }
1298
1298
  }
1299
1299
  var init_TaskRunner = __esm(() => {
1300
1300
  init_TaskOutputRepository();
1301
1301
  init_Conversions();
1302
1302
  init_InputResolver();
1303
- init_StreamTypes();
1304
1303
  init_TaskError();
1305
1304
  init_TaskTypes();
1306
1305
  });
1307
1306
 
1308
1307
  // src/task/Task.ts
1308
+ import { compileSchema } from "@workglow/schema";
1309
1309
  import {
1310
- compileSchema,
1311
1310
  deepEqual,
1312
1311
  EventEmitter as EventEmitter3,
1313
1312
  uuid4 as uuid42
@@ -1600,7 +1599,7 @@ class Task {
1600
1599
  return this.events.waitOn(name);
1601
1600
  }
1602
1601
  emit(name, ...args) {
1603
- this._events?.emit(name, ...args);
1602
+ this.events.emit(name, ...args);
1604
1603
  }
1605
1604
  emitSchemaChange(inputSchema, outputSchema) {
1606
1605
  const finalInputSchema = inputSchema ?? this.inputSchema();
@@ -1742,13 +1741,15 @@ class Task {
1742
1741
  const extras = config.extras;
1743
1742
  if (!extras || Object.keys(extras).length === 0)
1744
1743
  delete config.extras;
1745
- const json = this.stripSymbols({
1744
+ const base = {
1746
1745
  id: this.id,
1747
1746
  type: this.type,
1748
- defaults: this.defaults,
1749
- config
1750
- });
1751
- return json;
1747
+ defaults: this.defaults
1748
+ };
1749
+ if (Object.keys(config).length > 0) {
1750
+ base.config = config;
1751
+ }
1752
+ return this.stripSymbols(base);
1752
1753
  }
1753
1754
  toDependencyJSON(options) {
1754
1755
  const json = this.toJSON(options);
@@ -1861,13 +1862,11 @@ function getNestedValue(obj, path) {
1861
1862
  }
1862
1863
  return current;
1863
1864
  }
1864
- var init_ConditionUtils = () => {};
1865
1865
 
1866
1866
  // src/task/ConditionalTask.ts
1867
1867
  import { getLogger } from "@workglow/util";
1868
1868
  var conditionalTaskConfigSchema, ConditionalTask;
1869
1869
  var init_ConditionalTask = __esm(() => {
1870
- init_ConditionUtils();
1871
1870
  init_Task();
1872
1871
  init_TaskTypes();
1873
1872
  conditionalTaskConfigSchema = {
@@ -2221,7 +2220,6 @@ class DependencyBasedScheduler {
2221
2220
  }
2222
2221
  }
2223
2222
  var init_TaskGraphScheduler = __esm(() => {
2224
- init_StreamTypes();
2225
2223
  init_TaskTypes();
2226
2224
  });
2227
2225
 
@@ -2235,6 +2233,10 @@ import {
2235
2233
  SpanStatusCode as SpanStatusCode2,
2236
2234
  uuid4 as uuid43
2237
2235
  } from "@workglow/util";
2236
+ function taskPrototypeHasOwnExecute(task) {
2237
+ const Ctor = task.constructor;
2238
+ return Object.hasOwn(Ctor.prototype, "execute");
2239
+ }
2238
2240
 
2239
2241
  class TaskGraphRunner {
2240
2242
  processScheduler;
@@ -2821,23 +2823,25 @@ class TaskGraphRunner {
2821
2823
  this.graph.emit("disabled");
2822
2824
  }
2823
2825
  async handleProgress(task, progress, message, ...args) {
2824
- const total = this.graph.getTasks().length;
2825
- if (total > 1) {
2826
- const completed = this.graph.getTasks().reduce((acc, t) => acc + t.progress, 0);
2827
- progress = Math.round(completed / total);
2826
+ const contributors = this.graph.getTasks().filter(taskPrototypeHasOwnExecute);
2827
+ if (contributors.length > 1) {
2828
+ const sum = contributors.reduce((acc, t) => acc + t.progress, 0);
2829
+ progress = Math.round(sum / contributors.length);
2830
+ } else if (contributors.length === 1) {
2831
+ const [only] = contributors;
2832
+ progress = only.progress;
2828
2833
  }
2829
2834
  this.pushStatusFromNodeToEdges(this.graph, task);
2835
+ this.graph.emit("graph_progress", progress, message, args);
2830
2836
  if (task.runOutputData && Object.keys(task.runOutputData).length > 0) {
2831
2837
  await this.pushOutputFromNodeToEdges(task, task.runOutputData);
2832
2838
  }
2833
- this.graph.emit("graph_progress", progress, message, args);
2834
2839
  }
2835
2840
  }
2836
2841
  var PROPERTY_ARRAY = "PROPERTY_ARRAY", GRAPH_RESULT_ARRAY = "GRAPH_RESULT_ARRAY";
2837
2842
  var init_TaskGraphRunner = __esm(() => {
2838
2843
  init_TaskOutputRepository();
2839
2844
  init_ConditionalTask();
2840
- init_StreamTypes();
2841
2845
  init_TaskError();
2842
2846
  init_TaskTypes();
2843
2847
  init_Dataflow();
@@ -2901,7 +2905,7 @@ __export(exports_GraphAsTask, {
2901
2905
  graphAsTaskConfigSchema: () => graphAsTaskConfigSchema,
2902
2906
  GraphAsTask: () => GraphAsTask
2903
2907
  });
2904
- import { compileSchema as compileSchema2 } from "@workglow/util";
2908
+ import { compileSchema as compileSchema2 } from "@workglow/schema";
2905
2909
  var graphAsTaskConfigSchema, GraphAsTask;
2906
2910
  var init_GraphAsTask = __esm(() => {
2907
2911
  init_GraphSchemaUtils();
@@ -3515,7 +3519,6 @@ init_Dataflow();
3515
3519
 
3516
3520
  // src/task-graph/Workflow.ts
3517
3521
  init_GraphAsTask();
3518
- init_StreamTypes();
3519
3522
  init_TaskError();
3520
3523
  init_Conversions();
3521
3524
  init_Dataflow();
@@ -4614,7 +4617,6 @@ function resetMethodNameCache() {
4614
4617
  }
4615
4618
  // src/task/index.ts
4616
4619
  init_ConditionalTask();
4617
- init_ConditionUtils();
4618
4620
 
4619
4621
  // src/task/FallbackTask.ts
4620
4622
  init_GraphAsTask();
@@ -4838,6 +4840,9 @@ init_GraphAsTaskRunner();
4838
4840
  import { uuid4 as uuid46 } from "@workglow/util";
4839
4841
 
4840
4842
  class IteratorTaskRunner extends GraphAsTaskRunner {
4843
+ aggregatingParentMapProgress = false;
4844
+ mapPartialProgress = [];
4845
+ mapPartialIterationCount = 0;
4841
4846
  async executeTask(input) {
4842
4847
  const analysis = this.task.analyzeIterationInput(input);
4843
4848
  if (analysis.iterationCount === 0) {
@@ -4859,30 +4864,43 @@ class IteratorTaskRunner extends GraphAsTaskRunner {
4859
4864
  const concurrency = Math.max(1, Math.min(requestedConcurrency, iterationCount));
4860
4865
  const orderedResults = preserveOrder ? new Array(iterationCount) : [];
4861
4866
  const completionOrderResults = [];
4862
- let completedCount = 0;
4863
- for (let batchStart = 0;batchStart < iterationCount; batchStart += batchSize) {
4864
- if (this.abortController?.signal.aborted) {
4865
- break;
4866
- }
4867
- const batchEnd = Math.min(batchStart + batchSize, iterationCount);
4868
- const batchIndices = Array.from({ length: batchEnd - batchStart }, (_, i) => batchStart + i);
4869
- const batchResults = await this.executeBatch(batchIndices, analysis, iterationCount, concurrency, async () => {
4870
- completedCount++;
4871
- const progress = Math.round(completedCount / iterationCount * 100);
4872
- await this.handleProgress(progress, `Completed ${completedCount}/${iterationCount} iterations`);
4873
- });
4874
- for (const { index, result } of batchResults) {
4875
- if (result === undefined)
4876
- continue;
4877
- if (preserveOrder) {
4878
- orderedResults[index] = result;
4879
- } else {
4880
- completionOrderResults.push(result);
4867
+ this.aggregatingParentMapProgress = true;
4868
+ this.mapPartialIterationCount = iterationCount;
4869
+ this.mapPartialProgress = new Array(iterationCount).fill(0);
4870
+ try {
4871
+ for (let batchStart = 0;batchStart < iterationCount; batchStart += batchSize) {
4872
+ if (this.abortController?.signal.aborted) {
4873
+ break;
4874
+ }
4875
+ const batchEnd = Math.min(batchStart + batchSize, iterationCount);
4876
+ const batchIndices = Array.from({ length: batchEnd - batchStart }, (_, i) => batchStart + i);
4877
+ const batchResults = await this.executeBatch(batchIndices, analysis, iterationCount, concurrency, undefined);
4878
+ for (const { index, result } of batchResults) {
4879
+ if (result === undefined)
4880
+ continue;
4881
+ if (preserveOrder) {
4882
+ orderedResults[index] = result;
4883
+ } else {
4884
+ completionOrderResults.push(result);
4885
+ }
4881
4886
  }
4882
4887
  }
4888
+ const collected = preserveOrder ? orderedResults.filter((result) => result !== undefined) : completionOrderResults;
4889
+ return this.task.collectResults(collected);
4890
+ } finally {
4891
+ this.aggregatingParentMapProgress = false;
4883
4892
  }
4884
- const collected = preserveOrder ? orderedResults.filter((result) => result !== undefined) : completionOrderResults;
4885
- return this.task.collectResults(collected);
4893
+ }
4894
+ emitMapParentProgressFromPartials(childMessage) {
4895
+ const n = this.mapPartialIterationCount;
4896
+ if (n <= 0)
4897
+ return;
4898
+ const sum = this.mapPartialProgress.reduce((a, b) => a + b, 0);
4899
+ const overall = Math.round(sum / n);
4900
+ const done = this.mapPartialProgress.filter((v) => v >= 100).length;
4901
+ const base = `Map ${done}/${n}`;
4902
+ const msg = childMessage ? `${base} — ${childMessage}` : `${base} iterations`;
4903
+ this.handleProgress(overall, msg);
4886
4904
  }
4887
4905
  async executeReduceIterations(analysis) {
4888
4906
  const iterationCount = analysis.iterationCount;
@@ -4894,7 +4912,7 @@ class IteratorTaskRunner extends GraphAsTaskRunner {
4894
4912
  const iterationInput = this.task.buildIterationRunInput(analysis, index, iterationCount, {
4895
4913
  accumulator
4896
4914
  });
4897
- const iterationResult = await this.executeSubgraphIteration(iterationInput);
4915
+ const iterationResult = await this.executeSubgraphIteration(iterationInput, index, iterationCount);
4898
4916
  accumulator = this.task.mergeIterationIntoAccumulator(accumulator, iterationResult, index);
4899
4917
  const progress = Math.round((index + 1) / iterationCount * 100);
4900
4918
  await this.handleProgress(progress, `Completed ${index + 1}/${iterationCount} iterations`);
@@ -4917,7 +4935,7 @@ class IteratorTaskRunner extends GraphAsTaskRunner {
4917
4935
  }
4918
4936
  const index = indices[position];
4919
4937
  const iterationInput = this.task.buildIterationRunInput(analysis, index, iterationCount);
4920
- const result = await this.executeSubgraphIteration(iterationInput);
4938
+ const result = await this.executeSubgraphIteration(iterationInput, index, iterationCount);
4921
4939
  results.push({ index, result });
4922
4940
  await onItemComplete?.();
4923
4941
  }
@@ -4944,20 +4962,44 @@ class IteratorTaskRunner extends GraphAsTaskRunner {
4944
4962
  }
4945
4963
  return clone;
4946
4964
  }
4947
- async executeSubgraphIteration(input) {
4965
+ async executeSubgraphIteration(input, index, iterationCount) {
4948
4966
  if (this.abortController?.signal.aborted) {
4949
4967
  return;
4950
4968
  }
4951
4969
  const graphClone = this.cloneGraph(this.task.subGraph);
4952
- const results = await graphClone.run(input, {
4953
- parentSignal: this.abortController?.signal,
4954
- outputCache: this.outputCache,
4955
- registry: this.registry
4956
- });
4957
- if (results.length === 0) {
4958
- return;
4970
+ this.task.emit("iteration_start", index, iterationCount);
4971
+ const taskProgressUnsubs = [];
4972
+ for (const t of graphClone.getTasks()) {
4973
+ const fn = (p, message) => {
4974
+ this.task.emit("iteration_progress", index, iterationCount, p, message);
4975
+ if (this.aggregatingParentMapProgress && this.mapPartialIterationCount > 0) {
4976
+ this.mapPartialProgress[index] = Math.max(this.mapPartialProgress[index] ?? 0, p);
4977
+ this.emitMapParentProgressFromPartials(message);
4978
+ }
4979
+ };
4980
+ t.events.on("progress", fn);
4981
+ taskProgressUnsubs.push({ task: t, fn });
4982
+ }
4983
+ try {
4984
+ const results = await graphClone.run(input, {
4985
+ parentSignal: this.abortController?.signal,
4986
+ outputCache: this.outputCache,
4987
+ registry: this.registry
4988
+ });
4989
+ if (results.length === 0) {
4990
+ return;
4991
+ }
4992
+ return graphClone.mergeExecuteOutputsToRunOutput(results, this.task.compoundMerge);
4993
+ } finally {
4994
+ for (const { task, fn } of taskProgressUnsubs) {
4995
+ task.events.off("progress", fn);
4996
+ }
4997
+ if (this.aggregatingParentMapProgress && this.mapPartialIterationCount > 0) {
4998
+ this.mapPartialProgress[index] = 100;
4999
+ this.emitMapParentProgressFromPartials();
5000
+ }
5001
+ this.task.emit("iteration_complete", index, iterationCount);
4959
5002
  }
4960
- return graphClone.mergeExecuteOutputsToRunOutput(results, this.task.compoundMerge);
4961
5003
  }
4962
5004
  }
4963
5005
 
@@ -5455,7 +5497,6 @@ class IteratorTask extends GraphAsTask {
5455
5497
  }
5456
5498
 
5457
5499
  // src/task/WhileTask.ts
5458
- init_ConditionUtils();
5459
5500
  init_GraphAsTask();
5460
5501
  init_TaskError();
5461
5502
 
@@ -6463,7 +6504,6 @@ queueMicrotask(() => {
6463
6504
  });
6464
6505
 
6465
6506
  // src/task/index.ts
6466
- init_StreamTypes();
6467
6507
  init_Task();
6468
6508
  init_TaskError();
6469
6509
 
@@ -6752,6 +6792,7 @@ class TaskOutputTabularRepository extends TaskOutputRepository {
6752
6792
  export {
6753
6793
  wrapSchemaInArray,
6754
6794
  whileTaskConfigSchema,
6795
+ taskPrototypeHasOwnExecute,
6755
6796
  setTaskQueueRegistry,
6756
6797
  setGlobalTaskConstructors,
6757
6798
  serialGraph,
@@ -6873,4 +6914,4 @@ export {
6873
6914
  ConditionalTask
6874
6915
  };
6875
6916
 
6876
- //# debugId=65F073C748987DA264756E2164756E21
6917
+ //# debugId=31149EAB579F695264756E2164756E21