@bian-womp/spark-graph 0.2.56 → 0.2.58

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 CHANGED
@@ -402,6 +402,14 @@ class Registry {
402
402
  }
403
403
  }
404
404
 
405
+ const LOG_LEVEL_VALUES = {
406
+ debug: 0,
407
+ info: 1,
408
+ warn: 2,
409
+ error: 3,
410
+ silent: 4,
411
+ };
412
+
405
413
  // Helper: typed promise detection and unwrapping for T | Promise<T>
406
414
  function isPromise(value) {
407
415
  return !!value && typeof value.then === "function";
@@ -476,6 +484,7 @@ class GraphRuntime {
476
484
  ...desc.policy,
477
485
  ...n.params?.policy,
478
486
  },
487
+ logLevel: desc.logLevel,
479
488
  runSeq: 0,
480
489
  activeControllers: new Set(),
481
490
  queue: [],
@@ -643,6 +652,36 @@ class GraphRuntime {
643
652
  ((p) => {
644
653
  node.stats.progress = Math.max(0, Math.min(1, Number(p) || 0));
645
654
  });
655
+ // Create log function that respects node's logLevel
656
+ const log = (level, message, context) => {
657
+ const nodeLogLevel = node.logLevel ?? "info"; // Default to "info" if not set
658
+ const nodeLogValue = LOG_LEVEL_VALUES[nodeLogLevel] ?? 1;
659
+ const requestedValue = LOG_LEVEL_VALUES[level] ?? 1;
660
+ // Only log if requested level >= node's logLevel
661
+ if (requestedValue >= nodeLogValue && nodeLogLevel !== "silent") {
662
+ const contextStr = context
663
+ ? ` ${Object.entries(context)
664
+ .map(([k, v]) => `${k}=${JSON.stringify(v)}`)
665
+ .join(" ")}`
666
+ : "";
667
+ const fullMessage = `[node:${runId || nodeId}:${node.typeId}] ${message}${contextStr}`;
668
+ // For other levels, use appropriate console method
669
+ switch (level) {
670
+ case "debug":
671
+ console.info(fullMessage);
672
+ break;
673
+ case "info":
674
+ console.info(fullMessage);
675
+ break;
676
+ case "warn":
677
+ console.warn(fullMessage);
678
+ break;
679
+ case "error":
680
+ console.error(fullMessage);
681
+ break;
682
+ }
683
+ }
684
+ };
646
685
  return {
647
686
  state: node.state,
648
687
  setState: (next) => Object.assign(node.state, next),
@@ -658,6 +697,7 @@ class GraphRuntime {
658
697
  runId,
659
698
  abortSignal,
660
699
  reportProgress,
700
+ log,
661
701
  };
662
702
  }
663
703
  scheduleInputsChangedInternal(nodeId) {
@@ -722,7 +762,11 @@ class GraphRuntime {
722
762
  const exec = async (attempt) => {
723
763
  let hadError = false;
724
764
  try {
725
- node.lifecycle?.prepare?.(node.params ?? {}, ctx);
765
+ if (node.lifecycle?.prepare) {
766
+ ctx.log("debug", "prepare-start");
767
+ node.lifecycle.prepare(node.params ?? {}, ctx);
768
+ ctx.log("debug", "prepare-done");
769
+ }
726
770
  await node.runtime.onInputsChanged?.(capturedInputs, ctx);
727
771
  }
728
772
  catch (err) {
@@ -763,12 +807,17 @@ class GraphRuntime {
763
807
  runId,
764
808
  durationMs: node.stats.lastDurationMs,
765
809
  });
810
+ ctx.log("debug", "node-done", {
811
+ durationMs: node.stats.lastDurationMs,
812
+ hadError,
813
+ });
766
814
  if (onDone)
767
815
  onDone();
768
816
  }
769
817
  };
770
818
  // fire node-start event
771
819
  this.emit("stats", { kind: "node-start", nodeId, runId });
820
+ ctx.log("debug", "node-start");
772
821
  exec(0);
773
822
  };
774
823
  const mode = policy.asyncConcurrency ?? "switch";
@@ -1167,7 +1216,11 @@ class GraphRuntime {
1167
1216
  const effectiveInputs = this.getEffectiveInputs(node.nodeId);
1168
1217
  const ctrl = new AbortController();
1169
1218
  const ctx = this.createExecutionContext(node.nodeId, node, effectiveInputs, `${node.nodeId}:init`, ctrl.signal);
1170
- node.lifecycle?.prepare?.(node.params ?? {}, ctx);
1219
+ if (node.lifecycle?.prepare) {
1220
+ ctx.log("debug", "prepare-start");
1221
+ node.lifecycle.prepare(node.params ?? {}, ctx);
1222
+ ctx.log("debug", "prepare-done");
1223
+ }
1171
1224
  node.runtime.onActivated?.();
1172
1225
  }
1173
1226
  if (invalidate) {
@@ -1413,7 +1466,11 @@ class GraphRuntime {
1413
1466
  const effectiveInputs = this.getEffectiveInputs(rn.nodeId);
1414
1467
  const ctrl = new AbortController();
1415
1468
  const ctx = this.createExecutionContext(rn.nodeId, rn, effectiveInputs, `${rn.nodeId}:init`, ctrl.signal);
1416
- rn.lifecycle?.prepare?.(rn.params ?? {}, ctx);
1469
+ if (rn.lifecycle?.prepare) {
1470
+ ctx.log("debug", "prepare-start");
1471
+ rn.lifecycle.prepare(rn.params ?? {}, ctx);
1472
+ ctx.log("debug", "prepare-done");
1473
+ }
1417
1474
  rn.runtime.onActivated?.();
1418
1475
  }
1419
1476
  else {
@@ -1601,6 +1658,13 @@ class GraphRuntime {
1601
1658
  return;
1602
1659
  const token = (this.recomputeTokenByNode.get(nodeId) ?? 0) + 1;
1603
1660
  this.recomputeTokenByNode.set(nodeId, token);
1661
+ // Log resolveHandles-start
1662
+ const nodeLogLevel = node.logLevel ?? "info";
1663
+ const nodeLogValue = LOG_LEVEL_VALUES[nodeLogLevel] ?? 1;
1664
+ const shouldLog = nodeLogValue <= LOG_LEVEL_VALUES.debug && nodeLogLevel !== "silent";
1665
+ if (shouldLog) {
1666
+ console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-start`);
1667
+ }
1604
1668
  let r;
1605
1669
  try {
1606
1670
  const res = resolveHandles({
@@ -1611,8 +1675,16 @@ class GraphRuntime {
1611
1675
  r = await unwrapMaybePromise(res);
1612
1676
  }
1613
1677
  catch {
1678
+ // Log resolveHandles-done even on error
1679
+ if (shouldLog) {
1680
+ console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-done (error)`);
1681
+ }
1614
1682
  return;
1615
1683
  }
1684
+ // Log resolveHandles-done
1685
+ if (shouldLog) {
1686
+ console.info(`[node:${nodeId}:${node.typeId}] resolveHandles-done`);
1687
+ }
1616
1688
  // If a newer recompute was scheduled, drop this result
1617
1689
  if ((this.recomputeTokenByNode.get(nodeId) ?? 0) !== token)
1618
1690
  return;