@workglow/task-graph 0.2.9 → 0.2.11

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/browser.js CHANGED
@@ -5365,30 +5365,41 @@ class FallbackTaskRunner extends GraphAsTaskRunner {
5365
5365
  }
5366
5366
  const errors = [];
5367
5367
  const totalAttempts = alternatives.length;
5368
- for (let i = 0;i < alternatives.length; i++) {
5369
- if (this.abortController?.signal.aborted) {
5370
- throw new TaskAbortedError("Fallback aborted");
5371
- }
5372
- const alternative = alternatives[i];
5373
- const attemptNumber = i + 1;
5374
- await this.handleProgress(Math.round((i + 0.5) / totalAttempts * 100), `Trying data alternative ${attemptNumber}/${totalAttempts}`);
5375
- try {
5376
- this.resetSubgraph();
5377
- const mergedInput = { ...input, ...alternative };
5378
- const results = await this.task.subGraph.run(mergedInput, {
5379
- parentSignal: this.abortController?.signal,
5380
- outputCache: this.outputCache,
5381
- registry: this.registry
5382
- });
5383
- const mergedOutput = this.task.subGraph.mergeExecuteOutputsToRunOutput(results, this.task.compoundMerge);
5384
- await this.handleProgress(100, `Data alternative ${attemptNumber}/${totalAttempts} succeeded`);
5385
- return await this.executeTaskReactive(input, mergedOutput);
5386
- } catch (error) {
5387
- if (error instanceof TaskAbortedError && !(error instanceof TaskTimeoutError)) {
5388
- throw error;
5368
+ let currentAttemptIndex = 0;
5369
+ const onSubgraphProgress = (innerProgress, message) => {
5370
+ const blended = Math.round((currentAttemptIndex + innerProgress / 100) / totalAttempts * 100);
5371
+ this.handleProgress(blended, message ?? `Data alternative ${currentAttemptIndex + 1}/${totalAttempts}`);
5372
+ };
5373
+ const unsubscribeSubgraphProgress = this.task.subGraph.subscribe("graph_progress", onSubgraphProgress);
5374
+ try {
5375
+ for (let i = 0;i < alternatives.length; i++) {
5376
+ if (this.abortController?.signal.aborted) {
5377
+ throw new TaskAbortedError("Fallback aborted");
5378
+ }
5379
+ currentAttemptIndex = i;
5380
+ const alternative = alternatives[i];
5381
+ const attemptNumber = i + 1;
5382
+ await this.handleProgress(Math.round((i + 0.5) / totalAttempts * 100), `Trying data alternative ${attemptNumber}/${totalAttempts}`);
5383
+ try {
5384
+ this.resetSubgraph();
5385
+ const mergedInput = { ...input, ...alternative };
5386
+ const results = await this.task.subGraph.run(mergedInput, {
5387
+ parentSignal: this.abortController?.signal,
5388
+ outputCache: this.outputCache,
5389
+ registry: this.registry
5390
+ });
5391
+ const mergedOutput = this.task.subGraph.mergeExecuteOutputsToRunOutput(results, this.task.compoundMerge);
5392
+ await this.handleProgress(100, `Data alternative ${attemptNumber}/${totalAttempts} succeeded`);
5393
+ return await this.executeTaskReactive(input, mergedOutput);
5394
+ } catch (error) {
5395
+ if (error instanceof TaskAbortedError && !(error instanceof TaskTimeoutError)) {
5396
+ throw error;
5397
+ }
5398
+ errors.push({ alternative, error });
5389
5399
  }
5390
- errors.push({ alternative, error });
5391
5400
  }
5401
+ } finally {
5402
+ unsubscribeSubgraphProgress();
5392
5403
  }
5393
5404
  throw this.buildAggregateError(errors, "data");
5394
5405
  }
@@ -5729,18 +5740,14 @@ class IteratorTaskRunner extends GraphAsTaskRunner {
5729
5740
  }
5730
5741
  const graphClone = this.cloneGraph(this.task.subGraph);
5731
5742
  this.task.emit("iteration_start", index, iterationCount);
5732
- const taskProgressUnsubs = [];
5733
- for (const t of graphClone.getTasks()) {
5734
- const fn = (p, message) => {
5735
- this.task.emit("iteration_progress", index, iterationCount, p, message);
5736
- if (this.aggregatingParentMapProgress && this.mapPartialIterationCount > 0) {
5737
- this.mapPartialProgress[index] = Math.max(this.mapPartialProgress[index] ?? 0, p);
5738
- this.emitMapParentProgressFromPartials(message);
5739
- }
5740
- };
5741
- t.events.on("progress", fn);
5742
- taskProgressUnsubs.push({ task: t, fn });
5743
- }
5743
+ const onGraphProgress = (p, message) => {
5744
+ this.task.emit("iteration_progress", index, iterationCount, p, message);
5745
+ if (this.aggregatingParentMapProgress && this.mapPartialIterationCount > 0) {
5746
+ this.mapPartialProgress[index] = Math.max(this.mapPartialProgress[index] ?? 0, p);
5747
+ this.emitMapParentProgressFromPartials(message);
5748
+ }
5749
+ };
5750
+ const unsubscribeGraphProgress = graphClone.subscribe("graph_progress", onGraphProgress);
5744
5751
  try {
5745
5752
  const results = await graphClone.run(input, {
5746
5753
  parentSignal: this.abortController?.signal,
@@ -5753,9 +5760,7 @@ class IteratorTaskRunner extends GraphAsTaskRunner {
5753
5760
  }
5754
5761
  return graphClone.mergeExecuteOutputsToRunOutput(results, this.task.compoundMerge);
5755
5762
  } finally {
5756
- for (const { task, fn } of taskProgressUnsubs) {
5757
- task.events.off("progress", fn);
5758
- }
5763
+ unsubscribeGraphProgress();
5759
5764
  if (this.aggregatingParentMapProgress && this.mapPartialIterationCount > 0) {
5760
5765
  this.mapPartialProgress[index] = 100;
5761
5766
  this.emitMapParentProgressFromPartials();
@@ -6421,55 +6426,65 @@ class WhileTask extends GraphAsTask {
6421
6426
  let currentInput = { ...input };
6422
6427
  let currentOutput = {};
6423
6428
  const effectiveMax = arrayAnalysis ? Math.min(this.maxIterations, arrayAnalysis.iterationCount) : this.maxIterations;
6424
- while (this._currentIteration < effectiveMax) {
6425
- if (context.signal?.aborted) {
6426
- break;
6427
- }
6428
- let iterationInput;
6429
- if (arrayAnalysis) {
6430
- iterationInput = {
6431
- ...this.buildIterationInput(currentInput, arrayAnalysis, this._currentIteration),
6432
- _iterationIndex: this._currentIteration
6433
- };
6434
- } else {
6435
- iterationInput = {
6436
- ...currentInput,
6437
- _iterationIndex: this._currentIteration
6438
- };
6439
- }
6440
- const results = await this.subGraph.run(iterationInput, {
6441
- parentSignal: context.signal
6442
- });
6443
- currentOutput = this.subGraph.mergeExecuteOutputsToRunOutput(results, this.compoundMerge);
6444
- let shouldContinue;
6445
- try {
6446
- shouldContinue = condition(currentOutput, this._currentIteration);
6447
- } catch (err) {
6448
- if (err instanceof TaskFailedError) {
6449
- throw err;
6429
+ const onInnerGraphProgress = (innerProgress, innerMessage) => {
6430
+ const blended = Math.min(Math.round((this._currentIteration + innerProgress / 100) / effectiveMax * 100), 99);
6431
+ const message = innerMessage ? `Iteration ${this._currentIteration + 1}/${effectiveMax}: ${innerMessage}` : `Iteration ${this._currentIteration + 1}/${effectiveMax}`;
6432
+ context.updateProgress(blended, message);
6433
+ };
6434
+ const unsubscribeInnerProgress = this.subGraph.subscribe("graph_progress", onInnerGraphProgress);
6435
+ try {
6436
+ while (this._currentIteration < effectiveMax) {
6437
+ if (context.signal?.aborted) {
6438
+ break;
6439
+ }
6440
+ let iterationInput;
6441
+ if (arrayAnalysis) {
6442
+ iterationInput = {
6443
+ ...this.buildIterationInput(currentInput, arrayAnalysis, this._currentIteration),
6444
+ _iterationIndex: this._currentIteration
6445
+ };
6446
+ } else {
6447
+ iterationInput = {
6448
+ ...currentInput,
6449
+ _iterationIndex: this._currentIteration
6450
+ };
6450
6451
  }
6451
- const message = `${this.type}: Condition function threw at iteration ${this._currentIteration}: ${err instanceof Error ? err.message : String(err)}`;
6452
- const wrappedError = new TaskFailedError(message);
6453
- if (err instanceof Error && err.stack) {
6454
- if (wrappedError.stack) {
6455
- wrappedError.stack += `
6452
+ const results = await this.subGraph.run(iterationInput, {
6453
+ parentSignal: context.signal
6454
+ });
6455
+ currentOutput = this.subGraph.mergeExecuteOutputsToRunOutput(results, this.compoundMerge);
6456
+ let shouldContinue;
6457
+ try {
6458
+ shouldContinue = condition(currentOutput, this._currentIteration);
6459
+ } catch (err) {
6460
+ if (err instanceof TaskFailedError) {
6461
+ throw err;
6462
+ }
6463
+ const message = `${this.type}: Condition function threw at iteration ${this._currentIteration}: ${err instanceof Error ? err.message : String(err)}`;
6464
+ const wrappedError = new TaskFailedError(message);
6465
+ if (err instanceof Error && err.stack) {
6466
+ if (wrappedError.stack) {
6467
+ wrappedError.stack += `
6456
6468
  Caused by original error:
6457
6469
  ${err.stack}`;
6458
- } else {
6459
- wrappedError.stack = err.stack;
6470
+ } else {
6471
+ wrappedError.stack = err.stack;
6472
+ }
6460
6473
  }
6474
+ throw wrappedError;
6461
6475
  }
6462
- throw wrappedError;
6463
- }
6464
- if (!shouldContinue) {
6465
- break;
6466
- }
6467
- if (this.chainIterations) {
6468
- currentInput = { ...currentInput, ...currentOutput };
6476
+ if (!shouldContinue) {
6477
+ break;
6478
+ }
6479
+ if (this.chainIterations) {
6480
+ currentInput = { ...currentInput, ...currentOutput };
6481
+ }
6482
+ this._currentIteration++;
6483
+ const progress = Math.min(Math.round(this._currentIteration / effectiveMax * 100), 99);
6484
+ await context.updateProgress(progress, `Completed ${this._currentIteration}/${effectiveMax} iterations`);
6469
6485
  }
6470
- this._currentIteration++;
6471
- const progress = Math.min(Math.round(this._currentIteration / effectiveMax * 100), 99);
6472
- await context.updateProgress(progress, `Completed ${this._currentIteration}/${effectiveMax} iterations`);
6486
+ } finally {
6487
+ unsubscribeInnerProgress();
6473
6488
  }
6474
6489
  return currentOutput;
6475
6490
  }
@@ -6486,40 +6501,50 @@ ${err.stack}`;
6486
6501
  let currentInput = { ...input };
6487
6502
  let currentOutput = {};
6488
6503
  const effectiveMax = arrayAnalysis ? Math.min(this.maxIterations, arrayAnalysis.iterationCount) : this.maxIterations;
6489
- while (this._currentIteration < effectiveMax) {
6490
- if (context.signal?.aborted)
6491
- break;
6492
- let iterationInput;
6493
- if (arrayAnalysis) {
6494
- iterationInput = {
6495
- ...this.buildIterationInput(currentInput, arrayAnalysis, this._currentIteration),
6496
- _iterationIndex: this._currentIteration
6497
- };
6498
- } else {
6499
- iterationInput = {
6500
- ...currentInput,
6501
- _iterationIndex: this._currentIteration
6502
- };
6503
- }
6504
- const results = await this.subGraph.run(iterationInput, {
6505
- parentSignal: context.signal
6506
- });
6507
- currentOutput = this.subGraph.mergeExecuteOutputsToRunOutput(results, this.compoundMerge);
6508
- let shouldContinue;
6509
- try {
6510
- shouldContinue = condition(currentOutput, this._currentIteration);
6511
- } catch (err) {
6512
- throw new TaskFailedError(`${this.type}: Condition function threw at iteration ${this._currentIteration}: ${err instanceof Error ? err.message : String(err)}`);
6513
- }
6514
- if (!shouldContinue) {
6515
- break;
6516
- }
6517
- if (this.chainIterations) {
6518
- currentInput = { ...currentInput, ...currentOutput };
6504
+ const onInnerGraphProgress = (innerProgress, innerMessage) => {
6505
+ const blended = Math.min(Math.round((this._currentIteration + innerProgress / 100) / effectiveMax * 100), 99);
6506
+ const message = innerMessage ? `Iteration ${this._currentIteration + 1}/${effectiveMax}: ${innerMessage}` : `Iteration ${this._currentIteration + 1}/${effectiveMax}`;
6507
+ context.updateProgress(blended, message);
6508
+ };
6509
+ const unsubscribeInnerProgress = this.subGraph.subscribe("graph_progress", onInnerGraphProgress);
6510
+ try {
6511
+ while (this._currentIteration < effectiveMax) {
6512
+ if (context.signal?.aborted)
6513
+ break;
6514
+ let iterationInput;
6515
+ if (arrayAnalysis) {
6516
+ iterationInput = {
6517
+ ...this.buildIterationInput(currentInput, arrayAnalysis, this._currentIteration),
6518
+ _iterationIndex: this._currentIteration
6519
+ };
6520
+ } else {
6521
+ iterationInput = {
6522
+ ...currentInput,
6523
+ _iterationIndex: this._currentIteration
6524
+ };
6525
+ }
6526
+ const results = await this.subGraph.run(iterationInput, {
6527
+ parentSignal: context.signal
6528
+ });
6529
+ currentOutput = this.subGraph.mergeExecuteOutputsToRunOutput(results, this.compoundMerge);
6530
+ let shouldContinue;
6531
+ try {
6532
+ shouldContinue = condition(currentOutput, this._currentIteration);
6533
+ } catch (err) {
6534
+ throw new TaskFailedError(`${this.type}: Condition function threw at iteration ${this._currentIteration}: ${err instanceof Error ? err.message : String(err)}`);
6535
+ }
6536
+ if (!shouldContinue) {
6537
+ break;
6538
+ }
6539
+ if (this.chainIterations) {
6540
+ currentInput = { ...currentInput, ...currentOutput };
6541
+ }
6542
+ this._currentIteration++;
6543
+ const progress = Math.min(Math.round(this._currentIteration / effectiveMax * 100), 99);
6544
+ await context.updateProgress(progress, `Completed ${this._currentIteration}/${effectiveMax} iterations`);
6519
6545
  }
6520
- this._currentIteration++;
6521
- const progress = Math.min(Math.round(this._currentIteration / effectiveMax * 100), 99);
6522
- await context.updateProgress(progress, `Completed ${this._currentIteration}/${effectiveMax} iterations`);
6546
+ } finally {
6547
+ unsubscribeInnerProgress();
6523
6548
  }
6524
6549
  yield { type: "finish", data: currentOutput };
6525
6550
  }
@@ -8242,4 +8267,4 @@ export {
8242
8267
  BROWSER_GRANTS
8243
8268
  };
8244
8269
 
8245
- //# debugId=108A162D578570C764756E2164756E21
8270
+ //# debugId=216D117BD58091BC64756E2164756E21