@bian-womp/spark-workbench 0.2.31 → 0.2.33

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
@@ -477,7 +477,7 @@ class LocalGraphRunner extends AbstractGraphRunner {
477
477
  this.engine.on("error", (e) => this.emit("error", e));
478
478
  this.engine.on("invalidate", (e) => this.emit("invalidate", e));
479
479
  this.engine.on("stats", (e) => this.emit("stats", e));
480
- this.engine.launch();
480
+ this.engine.launch(opts.invalidate);
481
481
  this.runningKind = opts.engine;
482
482
  this.emit("status", { running: true, engine: this.runningKind });
483
483
  for (const [nodeId, map] of Object.entries(this.stagedInputs)) {
@@ -710,7 +710,7 @@ class RemoteGraphRunner extends AbstractGraphRunner {
710
710
  this.listenersBound = true;
711
711
  }
712
712
  this.engine = eng;
713
- this.engine.launch();
713
+ this.engine.launch(opts.invalidate);
714
714
  this.runningKind = "push";
715
715
  this.emit("status", { running: true, engine: this.runningKind });
716
716
  for (const [nodeId, map] of Object.entries(this.stagedInputs)) {
@@ -754,9 +754,39 @@ class RemoteGraphRunner extends AbstractGraphRunner {
754
754
  }
755
755
  }
756
756
  async applySnapshotFull(payload) {
757
+ // Hydrate local cache first so UI can display values immediately
758
+ this.hydrateCacheFromSnapshot(payload);
759
+ // Then sync with backend
757
760
  const runner = await this.ensureRemoteRunner();
758
761
  await runner.applySnapshotFull(payload);
759
762
  }
763
+ /**
764
+ * Hydrates the local valueCache from a snapshot and emits value events.
765
+ * This ensures the UI can display inputs/outputs immediately without waiting
766
+ * for value events from the remote backend.
767
+ */
768
+ hydrateCacheFromSnapshot(snapshot) {
769
+ // Hydrate inputs
770
+ for (const [nodeId, map] of Object.entries(snapshot.inputs || {})) {
771
+ for (const [handle, value] of Object.entries(map || {})) {
772
+ this.valueCache.set(`${nodeId}.${handle}`, {
773
+ io: "input",
774
+ value,
775
+ });
776
+ this.emit("value", { nodeId, handle, value, io: "input" });
777
+ }
778
+ }
779
+ // Hydrate outputs
780
+ for (const [nodeId, map] of Object.entries(snapshot.outputs || {})) {
781
+ for (const [handle, value] of Object.entries(map || {})) {
782
+ this.valueCache.set(`${nodeId}.${handle}`, {
783
+ io: "output",
784
+ value,
785
+ });
786
+ this.emit("value", { nodeId, handle, value, io: "output" });
787
+ }
788
+ }
789
+ }
760
790
  setEnvironment(env, opts) {
761
791
  const t = this.transport;
762
792
  if (!t)
@@ -1766,8 +1796,16 @@ function WorkbenchProvider({ wb, runner, registry, setRegistry, overrides, uiVer
1766
1796
  const next = { ...prev };
1767
1797
  for (const n of def.nodes) {
1768
1798
  const cur = next[n.nodeId] ?? (next[n.nodeId] = {});
1799
+ const updates = {};
1769
1800
  if (cur.invalidated === undefined) {
1770
- next[n.nodeId] = { ...cur, invalidated: true };
1801
+ updates.invalidated = true;
1802
+ }
1803
+ // Ensure activeRunIds is always initialized as an array
1804
+ if (cur.activeRunIds === undefined) {
1805
+ updates.activeRunIds = [];
1806
+ }
1807
+ if (Object.keys(updates).length > 0) {
1808
+ next[n.nodeId] = { ...cur, ...updates };
1771
1809
  }
1772
1810
  }
1773
1811
  return next;
@@ -1971,13 +2009,23 @@ function WorkbenchProvider({ wb, runner, registry, setRegistry, overrides, uiVer
1971
2009
  return;
1972
2010
  if (s.kind === "node-start") {
1973
2011
  const id = s.nodeId;
2012
+ const runId = s.runId;
2013
+ // Validate runId is a non-empty string
2014
+ const isValidRunId = runId && typeof runId === "string" && runId.length > 0;
2015
+ if (!isValidRunId) {
2016
+ console.warn(`[WorkbenchContext] node-start event missing or invalid runId for node ${id}`, { runId, event: s });
2017
+ }
1974
2018
  setNodeStatus((prev) => {
1975
2019
  const current = prev[id]?.activeRuns ?? 0;
2020
+ const currentRunIds = prev[id]?.activeRunIds ?? [];
1976
2021
  return {
1977
2022
  ...prev,
1978
2023
  [id]: {
1979
2024
  ...prev[id],
1980
2025
  activeRuns: current + 1,
2026
+ activeRunIds: isValidRunId
2027
+ ? [...currentRunIds, runId]
2028
+ : currentRunIds,
1981
2029
  progress: 0,
1982
2030
  invalidated: false,
1983
2031
  },
@@ -1999,13 +2047,19 @@ function WorkbenchProvider({ wb, runner, registry, setRegistry, overrides, uiVer
1999
2047
  else if (s.kind === "node-done") {
2000
2048
  const id = s.nodeId;
2001
2049
  const runId = s.runId;
2050
+ // Validate runId is a non-empty string
2051
+ const isValidRunId = runId && typeof runId === "string" && runId.length > 0;
2002
2052
  setNodeStatus((prev) => {
2003
2053
  const current = prev[id]?.activeRuns ?? 0;
2004
2054
  const nextActive = current - 1;
2005
- const hadError = !!(runId && errorRunsRef.current[id]?.[runId]);
2055
+ const currentRunIds = prev[id]?.activeRunIds ?? [];
2056
+ const nextRunIds = isValidRunId
2057
+ ? currentRunIds.filter((rid) => rid !== runId)
2058
+ : currentRunIds;
2059
+ const hadError = !!(isValidRunId && errorRunsRef.current[id]?.[runId]);
2006
2060
  const keepProgress = hadError || nextActive > 0;
2007
2061
  // Clear error flag for this runId
2008
- if (runId && errorRunsRef.current[id]?.[runId]) {
2062
+ if (isValidRunId && errorRunsRef.current[id]?.[runId]) {
2009
2063
  delete errorRunsRef.current[id]?.[runId];
2010
2064
  }
2011
2065
  return {
@@ -2013,6 +2067,7 @@ function WorkbenchProvider({ wb, runner, registry, setRegistry, overrides, uiVer
2013
2067
  [id]: {
2014
2068
  ...prev[id],
2015
2069
  activeRuns: nextActive,
2070
+ activeRunIds: nextRunIds,
2016
2071
  progress: keepProgress ? prev[id]?.progress : 0,
2017
2072
  lastError: hadError ? prev[id]?.lastError : undefined,
2018
2073
  },
@@ -2324,7 +2379,7 @@ function Inspector({ debug, autoScroll, hideWorkbench, onAutoScrollChange, onHid
2324
2379
  return String(value ?? "");
2325
2380
  }
2326
2381
  };
2327
- const { registry, def, selectedNodeId, selectedEdgeId, inputsMap, outputsMap, outputTypesMap, nodeStatus, validationByNode, validationByEdge, validationGlobal, valuesTick, updateEdgeType, systemErrors, registryErrors, clearSystemErrors, clearRegistryErrors, removeSystemError, removeRegistryError, } = useWorkbenchContext();
2382
+ const { registry, def, selectedNodeId, selectedEdgeId, inputsMap, outputsMap, outputTypesMap, nodeStatus, edgeStatus, validationByNode, validationByEdge, validationGlobal, valuesTick, updateEdgeType, systemErrors, registryErrors, clearSystemErrors, clearRegistryErrors, removeSystemError, removeRegistryError, } = useWorkbenchContext();
2328
2383
  const nodeValidationIssues = validationByNode.issues;
2329
2384
  const edgeValidationIssues = validationByEdge.issues;
2330
2385
  const nodeValidationHandles = validationByNode;
@@ -2413,7 +2468,10 @@ function Inspector({ debug, autoScroll, hideWorkbench, onAutoScrollChange, onHid
2413
2468
  return (jsxRuntime.jsxs("div", { className: `${widthClass} border-l border-gray-300 p-3 flex flex-col h-full min-h-0 overflow-auto`, children: [jsxRuntime.jsxs("div", { className: "flex-1 overflow-auto", children: [contextPanel && jsxRuntime.jsx("div", { className: "mb-2", children: contextPanel }), systemErrors.length > 0 && (jsxRuntime.jsxs("div", { className: "mb-2 space-y-1", children: [systemErrors.map((err, i) => (jsxRuntime.jsxs("div", { className: "text-xs text-red-700 bg-red-50 border border-red-200 rounded px-2 py-1 flex items-start justify-between gap-2", children: [jsxRuntime.jsxs("div", { className: "flex-1", children: [jsxRuntime.jsx("div", { className: "font-semibold", children: err.code ? `Error ${err.code}` : "Error" }), jsxRuntime.jsx("div", { className: "break-words", children: err.message })] }), jsxRuntime.jsx("button", { className: "text-red-500 hover:text-red-700 text-[10px] px-1", onClick: () => removeSystemError(i), title: "Dismiss", children: jsxRuntime.jsx(react$1.XIcon, { size: 10 }) })] }, i))), systemErrors.length > 1 && (jsxRuntime.jsx("button", { className: "text-xs text-red-600 hover:text-red-800 underline", onClick: clearSystemErrors, children: "Clear all" }))] })), registryErrors.length > 0 && (jsxRuntime.jsxs("div", { className: "mb-2 space-y-1", children: [registryErrors.map((err, i) => (jsxRuntime.jsxs("div", { className: "text-xs text-amber-700 bg-amber-50 border border-amber-200 rounded px-2 py-1 flex items-start justify-between gap-2", children: [jsxRuntime.jsxs("div", { className: "flex-1", children: [jsxRuntime.jsx("div", { className: "font-semibold", children: "Registry Error" }), jsxRuntime.jsx("div", { className: "break-words", children: err.message }), err.attempt && err.maxAttempts && (jsxRuntime.jsxs("div", { className: "text-[10px] text-amber-600 mt-1", children: ["Attempt ", err.attempt, " of ", err.maxAttempts] }))] }), jsxRuntime.jsx("button", { className: "text-amber-500 hover:text-amber-700 text-[10px] px-1", onClick: () => removeRegistryError(i), title: "Dismiss", children: jsxRuntime.jsx(react$1.XIcon, { size: 10 }) })] }, i))), registryErrors.length > 1 && (jsxRuntime.jsx("button", { className: "text-xs text-amber-600 hover:text-amber-800 underline", onClick: clearRegistryErrors, children: "Clear all" }))] })), jsxRuntime.jsx("div", { className: "font-semibold mb-2", children: "Inspector" }), jsxRuntime.jsxs("div", { className: "text-xs text-gray-500 mb-2", children: ["valuesTick: ", valuesTick] }), jsxRuntime.jsx("div", { className: "flex-1", children: !selectedNode && !selectedEdge ? (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsx("div", { className: "text-gray-500", children: "Select a node or edge." }), globalValidationIssues && globalValidationIssues.length > 0 && (jsxRuntime.jsxs("div", { className: "mt-2 text-xs bg-red-50 border border-red-200 rounded px-2 py-1", children: [jsxRuntime.jsx("div", { className: "font-semibold mb-1", children: "Validation" }), jsxRuntime.jsx("ul", { className: "list-disc ml-4", children: globalValidationIssues.map((m, i) => (jsxRuntime.jsxs("li", { className: "flex items-center gap-1", children: [jsxRuntime.jsx(IssueBadge, { level: m.level, size: 24, className: "w-6 h-6" }), jsxRuntime.jsx("span", { children: `${m.code}: ${m.message}` }), !!m.data?.edgeId && (jsxRuntime.jsx("button", { className: "ml-2 text-[10px] px-1 py-[2px] border border-red-300 rounded text-red-700 hover:bg-red-50", onClick: (e) => {
2414
2469
  e.stopPropagation();
2415
2470
  deleteEdgeById(m.data?.edgeId);
2416
- }, title: "Delete referenced edge", children: "Delete edge" }))] }, i))) })] }))] })) : selectedEdge ? (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsxs("div", { className: "mb-2", children: [jsxRuntime.jsxs("div", { children: ["Edge: ", selectedEdge.id] }), jsxRuntime.jsxs("div", { children: [selectedEdge.source.nodeId, ".", selectedEdge.source.handle, " \u2192", " ", selectedEdge.target.nodeId, ".", selectedEdge.target.handle] }), jsxRuntime.jsx("div", { className: "mt-1", children: jsxRuntime.jsx("button", { className: "text-xs px-2 py-1 border border-red-300 rounded text-red-700 hover:bg-red-50", onClick: (e) => {
2471
+ }, title: "Delete referenced edge", children: "Delete edge" }))] }, i))) })] }))] })) : selectedEdge ? (jsxRuntime.jsxs("div", { children: [jsxRuntime.jsxs("div", { className: "mb-2", children: [jsxRuntime.jsxs("div", { children: ["Edge: ", selectedEdge.id] }), jsxRuntime.jsxs("div", { children: [selectedEdge.source.nodeId, ".", selectedEdge.source.handle, " \u2192", " ", selectedEdge.target.nodeId, ".", selectedEdge.target.handle] }), (() => {
2472
+ const status = edgeStatus?.[selectedEdge.id];
2473
+ return status?.activeRuns > 0 ? (jsxRuntime.jsxs("div", { className: "mt-1 text-xs text-blue-700 bg-blue-50 border border-blue-200 rounded px-2 py-1", children: [jsxRuntime.jsxs("div", { className: "font-semibold", children: ["Running (", status.activeRuns, ")"] }), jsxRuntime.jsx("div", { className: "text-[10px] text-blue-600 mt-1", children: "Note: Edge runIds are not available in stats events" })] })) : null;
2474
+ })(), jsxRuntime.jsx("div", { className: "mt-1", children: jsxRuntime.jsx("button", { className: "text-xs px-2 py-1 border border-red-300 rounded text-red-700 hover:bg-red-50", onClick: (e) => {
2417
2475
  e.stopPropagation();
2418
2476
  deleteEdgeById(selectedEdge.id);
2419
2477
  }, title: "Delete this edge", children: "Delete edge" }) }), jsxRuntime.jsxs("div", { className: "flex items-center gap-2 mt-1", children: [jsxRuntime.jsxs("label", { className: "w-20 flex flex-col", children: [jsxRuntime.jsx("span", { children: "Type" }), jsxRuntime.jsx("span", { className: "text-gray-500 text-[11px]", children: "DataTypeId" })] }), jsxRuntime.jsxs("select", { className: "border border-gray-300 rounded px-2 py-1 focus:outline-none focus:ring-2 focus:ring-blue-500 w-full", value: selectedEdge.typeId ?? "", onChange: (e) => {
@@ -2423,7 +2481,9 @@ function Inspector({ debug, autoScroll, hideWorkbench, onAutoScrollChange, onHid
2423
2481
  }, children: [jsxRuntime.jsx("option", { value: "", children: "(infer from source)" }), Array.from(registry.types.keys()).map((tid) => (jsxRuntime.jsx("option", { value: tid, children: tid }, tid)))] })] })] }), selectedEdgeValidation.length > 0 && (jsxRuntime.jsxs("div", { className: "mt-2 text-xs bg-red-50 border border-red-200 rounded px-2 py-1", children: [jsxRuntime.jsx("div", { className: "font-semibold mb-1", children: "Validation" }), jsxRuntime.jsx("ul", { className: "list-disc ml-4", children: selectedEdgeValidation.map((m, i) => (jsxRuntime.jsxs("li", { className: "flex items-center gap-1", children: [jsxRuntime.jsx(IssueBadge, { level: m.level, size: 24, className: "w-6 h-6" }), jsxRuntime.jsx("span", { children: `${m.code}: ${m.message}` }), jsxRuntime.jsx("button", { className: "ml-2 text-[10px] px-1 py-[2px] border border-red-300 rounded text-red-700 hover:bg-red-50", onClick: (e) => {
2424
2482
  e.stopPropagation();
2425
2483
  deleteEdgeById(selectedEdge.id);
2426
- }, title: "Delete this edge", children: "Delete edge" })] }, i))) })] }))] })) : (jsxRuntime.jsxs("div", { children: [selectedNode && (jsxRuntime.jsxs("div", { className: "mb-2", children: [jsxRuntime.jsxs("div", { children: ["Node: ", selectedNode.nodeId] }), jsxRuntime.jsxs("div", { children: ["Type: ", selectedNode.typeId] }), !!selectedNodeStatus?.lastError && (jsxRuntime.jsx("div", { className: "mt-2 text-sm text-red-700 bg-red-50 border border-red-200 rounded px-2 py-1 break-words", children: String(selectedNodeStatus.lastError?.message ??
2484
+ }, title: "Delete this edge", children: "Delete edge" })] }, i))) })] }))] })) : (jsxRuntime.jsxs("div", { children: [selectedNode && (jsxRuntime.jsxs("div", { className: "mb-2", children: [jsxRuntime.jsxs("div", { children: ["Node: ", selectedNode.nodeId] }), jsxRuntime.jsxs("div", { children: ["Type: ", selectedNode.typeId] }), selectedNodeStatus?.activeRuns &&
2485
+ selectedNodeStatus.activeRuns > 0 && (jsxRuntime.jsxs("div", { className: "mt-1 text-xs text-blue-700 bg-blue-50 border border-blue-200 rounded px-2 py-1", children: [jsxRuntime.jsxs("div", { className: "font-semibold", children: ["Running (", selectedNodeStatus.activeRuns, ")"] }), selectedNodeStatus.activeRunIds &&
2486
+ selectedNodeStatus.activeRunIds.length > 0 ? (jsxRuntime.jsxs("div", { className: "mt-1", children: [jsxRuntime.jsx("div", { className: "text-[10px] text-blue-600", children: "RunIds:" }), jsxRuntime.jsx("div", { className: "flex flex-wrap gap-1 mt-1", children: selectedNodeStatus.activeRunIds.map((runId, idx) => (jsxRuntime.jsx("span", { className: "text-[10px] px-1.5 py-0.5 bg-blue-100 border border-blue-300 rounded font-mono", children: runId }, idx))) })] })) : (jsxRuntime.jsx("div", { className: "text-[10px] text-blue-600 mt-1", children: "RunIds not available (some runs may have started without runId)" }))] })), !!selectedNodeStatus?.lastError && (jsxRuntime.jsx("div", { className: "mt-2 text-sm text-red-700 bg-red-50 border border-red-200 rounded px-2 py-1 break-words", children: String(selectedNodeStatus.lastError?.message ??
2427
2487
  selectedNodeStatus.lastError) }))] })), jsxRuntime.jsxs("div", { className: "mb-2", children: [jsxRuntime.jsx("div", { className: "font-semibold mb-1", children: "Inputs" }), inputHandles.length === 0 ? (jsxRuntime.jsx("div", { className: "text-gray-500", children: "No inputs" })) : (inputHandles.map((h) => {
2428
2488
  const typeId = sparkGraph.getInputTypeId(effectiveHandles.inputs, h);
2429
2489
  const isLinked = def.edges.some((e) => e.target.nodeId === selectedNodeId &&
@@ -3659,12 +3719,17 @@ function WorkbenchStudioCanvas({ setRegistry, autoScroll, onAutoScrollChange, ex
3659
3719
  : undefined, children: [jsxRuntime.jsx("option", { value: "local", children: "Local" }), jsxRuntime.jsx("option", { value: "remote-http", children: "Remote (HTTP)" }), jsxRuntime.jsx("option", { value: "remote-ws", children: "Remote (WebSocket)" })] }), backendKind === "remote-http" && !!onHttpBaseUrlChange && (jsxRuntime.jsx("input", { className: "ml-2 border border-gray-300 rounded px-2 py-1 w-72", placeholder: "http://127.0.0.1:18080", value: httpBaseUrl, onChange: (e) => onHttpBaseUrlChange(e.target.value) })), backendKind === "remote-ws" && !!onWsUrlChange && (jsxRuntime.jsx("input", { className: "ml-2 border border-gray-300 rounded px-2 py-1 w-72", placeholder: "ws://127.0.0.1:18081", value: wsUrl, onChange: (e) => onWsUrlChange(e.target.value) })), jsxRuntime.jsxs("select", { className: "border border-gray-300 rounded px-2 py-1", value: runner.getRunningEngine() ?? engine ?? "", onChange: (e) => {
3660
3720
  const kind = e.target.value || undefined;
3661
3721
  onEngineChange?.(kind);
3662
- }, children: [jsxRuntime.jsx("option", { value: "", children: "Select Engine\u2026" }), jsxRuntime.jsx("option", { value: "push", children: "Push" }), jsxRuntime.jsx("option", { value: "batched", children: "Batched" }), jsxRuntime.jsx("option", { value: "pull", children: "Pull" }), jsxRuntime.jsx("option", { value: "hybrid", children: "Hybrid" }), jsxRuntime.jsx("option", { value: "step", children: "Step" })] }), runner.getRunningEngine() === "step" && (jsxRuntime.jsx("button", { className: "ml-2 border border-gray-300 rounded p-1", onClick: () => runner.step(), disabled: !runner.isRunning(), title: "Step", children: jsxRuntime.jsx(react$1.PlayPauseIcon, { size: 24 }) })), runner.getRunningEngine() === "batched" && (jsxRuntime.jsx("button", { className: "ml-2 border border-gray-300 rounded p-1", onClick: () => runner.flush(), disabled: !runner.isRunning(), title: "Flush", children: jsxRuntime.jsx(react$1.LightningIcon, { size: 24 }) })), runner.isRunning() ? (jsxRuntime.jsxs("button", { className: "border rounded px-2 py-1.5 text-red-700 border-red-600 flex items-center gap-1", onClick: () => runner.dispose(), title: "Stop engine", children: [jsxRuntime.jsx(react$1.StopIcon, { size: 16, weight: "fill" }), jsxRuntime.jsx("span", { className: "font-medium ml-1", children: "Stop" })] })) : (jsxRuntime.jsxs("button", { className: "border rounded px-2 py-1.5 text-green-700 border-green-600 flex items-center gap-1 disabled:text-gray-400 disabled:border-gray-300", onClick: () => {
3722
+ }, children: [jsxRuntime.jsx("option", { value: "", children: "Select Engine\u2026" }), jsxRuntime.jsx("option", { value: "push", children: "Push" }), jsxRuntime.jsx("option", { value: "batched", children: "Batched" }), jsxRuntime.jsx("option", { value: "pull", children: "Pull" }), jsxRuntime.jsx("option", { value: "hybrid", children: "Hybrid" }), jsxRuntime.jsx("option", { value: "step", children: "Step" })] }), runner.getRunningEngine() === "step" && (jsxRuntime.jsx("button", { className: "ml-2 border border-gray-300 rounded p-1", onClick: () => runner.step(), disabled: !runner.isRunning(), title: "Step", children: jsxRuntime.jsx(react$1.PlayPauseIcon, { size: 24 }) })), runner.getRunningEngine() === "batched" && (jsxRuntime.jsx("button", { className: "ml-2 border border-gray-300 rounded p-1", onClick: () => runner.flush(), disabled: !runner.isRunning(), title: "Flush", children: jsxRuntime.jsx(react$1.LightningIcon, { size: 24 }) })), runner.isRunning() ? (jsxRuntime.jsxs("button", { className: "border rounded px-2 py-1.5 text-red-700 border-red-600 flex items-center gap-1", onClick: () => runner.dispose(), title: "Stop engine", children: [jsxRuntime.jsx(react$1.StopIcon, { size: 16, weight: "fill" }), jsxRuntime.jsx("span", { className: "font-medium ml-1", children: "Stop" })] })) : (jsxRuntime.jsxs("button", { className: "border rounded px-2 py-1.5 text-green-700 border-green-600 flex items-center gap-1 disabled:text-gray-400 disabled:border-gray-300", onClick: (evt) => {
3663
3723
  const kind = engine;
3664
3724
  if (!kind)
3665
3725
  return alert("Select an engine first.");
3726
+ if (evt.shiftKey && !confirm("Invalidate and re-run graph?"))
3727
+ return;
3666
3728
  try {
3667
- runner.launch(wb.export(), { engine: kind });
3729
+ runner.launch(wb.export(), {
3730
+ engine: kind,
3731
+ invalidate: evt.shiftKey,
3732
+ });
3668
3733
  }
3669
3734
  catch (err) {
3670
3735
  const message = err instanceof Error ? err.message : String(err);