footprint-explainable-ui 0.22.0 → 0.24.0

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/index.cjs CHANGED
@@ -57,6 +57,7 @@ __export(src_exports, {
57
57
  createSnapshots: () => createSnapshots,
58
58
  defaultTokens: () => defaultTokens,
59
59
  extractSubflowNarrative: () => extractSubflowNarrative,
60
+ mergeWritePatch: () => mergeWritePatch,
60
61
  rawDefaults: () => rawDefaults,
61
62
  subflowResultToSnapshots: () => subflowResultToSnapshots,
62
63
  themePresets: () => themePresets,
@@ -2357,11 +2358,103 @@ function extractSubflowNarrative(entries, subflowId, subflowName) {
2357
2358
  }
2358
2359
 
2359
2360
  // src/adapters/fromRuntimeSnapshot.ts
2361
+ var COMMIT_PATH_DELIM = "";
2362
+ var UNSAFE_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
2363
+ function isSummaryMarker(value) {
2364
+ return value !== null && typeof value === "object" && (value.__writeSummary === true || value.__readSummary === true);
2365
+ }
2366
+ function isPlainRecord(value) {
2367
+ return value !== null && typeof value === "object" && !Array.isArray(value);
2368
+ }
2369
+ function mergeWritePatch(base, patch) {
2370
+ if (isSummaryMarker(patch)) return patch;
2371
+ if (patch === null || typeof patch !== "object") return patch;
2372
+ if (Array.isArray(patch)) return patch;
2373
+ if (isSummaryMarker(base) || !isPlainRecord(base)) {
2374
+ base = {};
2375
+ }
2376
+ const out = { ...base };
2377
+ for (const [key, value] of Object.entries(patch)) {
2378
+ if (UNSAFE_KEYS.has(key)) continue;
2379
+ out[key] = mergeWritePatch(out[key], value);
2380
+ }
2381
+ return out;
2382
+ }
2383
+ function getPath(root, segs) {
2384
+ let cur = root;
2385
+ for (const seg of segs) {
2386
+ if (!isPlainRecord(cur) && !Array.isArray(cur)) return void 0;
2387
+ cur = cur[seg];
2388
+ }
2389
+ return cur;
2390
+ }
2391
+ function setPath(memory, segs, value) {
2392
+ if (segs.some((s) => UNSAFE_KEYS.has(s))) return;
2393
+ let obj = memory;
2394
+ for (let i = 0; i < segs.length - 1; i++) {
2395
+ const cur = obj[segs[i]];
2396
+ const next = Array.isArray(cur) ? cur.slice() : isPlainRecord(cur) && !isSummaryMarker(cur) ? { ...cur } : {};
2397
+ obj[segs[i]] = next;
2398
+ obj = next;
2399
+ }
2400
+ const last = segs[segs.length - 1];
2401
+ if (value === void 0) {
2402
+ delete obj[last];
2403
+ } else {
2404
+ obj[last] = value;
2405
+ }
2406
+ }
2407
+ function applyCommitBundle(memory, bundle) {
2408
+ const trace = Array.isArray(bundle.trace) ? bundle.trace : void 0;
2409
+ if (trace) {
2410
+ for (const op of trace) {
2411
+ if (!op || typeof op.path !== "string") continue;
2412
+ const segs = op.path.split(COMMIT_PATH_DELIM);
2413
+ if (op.verb === "merge") {
2414
+ setPath(memory, segs, mergeWritePatch(getPath(memory, segs), getPath(bundle.updates, segs)));
2415
+ } else if (op.verb === "append") {
2416
+ const tail = getPath(bundle.overwrite, segs);
2417
+ const current = getPath(memory, segs);
2418
+ setPath(memory, segs, Array.isArray(current) && Array.isArray(tail) ? [...current, ...tail] : tail);
2419
+ } else if (op.verb === "delete") {
2420
+ setPath(memory, segs, void 0);
2421
+ } else {
2422
+ setPath(memory, segs, getPath(bundle.overwrite, segs));
2423
+ }
2424
+ }
2425
+ return;
2426
+ }
2427
+ if (isPlainRecord(bundle.overwrite)) {
2428
+ for (const [key, value] of Object.entries(bundle.overwrite)) setPath(memory, [key], value);
2429
+ }
2430
+ if (isPlainRecord(bundle.updates)) {
2431
+ for (const [key, value] of Object.entries(bundle.updates)) {
2432
+ setPath(memory, [key], mergeWritePatch(memory[key], value));
2433
+ }
2434
+ }
2435
+ }
2436
+ function indexCommitLog(commitLog) {
2437
+ const index = /* @__PURE__ */ new Map();
2438
+ if (!Array.isArray(commitLog)) return index;
2439
+ for (const entry of commitLog) {
2440
+ if (!isPlainRecord(entry)) continue;
2441
+ const bundle = entry;
2442
+ if (typeof bundle.runtimeStageId !== "string" || bundle.runtimeStageId.length === 0) continue;
2443
+ if (!isPlainRecord(bundle.overwrite) && !isPlainRecord(bundle.updates) && !Array.isArray(bundle.trace)) {
2444
+ continue;
2445
+ }
2446
+ const list = index.get(bundle.runtimeStageId);
2447
+ if (list) list.push(bundle);
2448
+ else index.set(bundle.runtimeStageId, [bundle]);
2449
+ }
2450
+ return index;
2451
+ }
2360
2452
  function toVisualizationSnapshots(runtime, narrativeEntries) {
2361
2453
  const stageNarrativeMap = narrativeEntries?.length ? buildStageNarrativeMap(narrativeEntries) : /* @__PURE__ */ new Map();
2362
2454
  const stageTimings = extractStageTimings(runtime.recorders);
2455
+ const commitIndex = indexCommitLog(runtime.commitLog);
2363
2456
  const snapshots = [];
2364
- flattenTree(runtime.executionTree, snapshots, runtime.sharedState, 0, runtime.subflowResults, {}, stageNarrativeMap, stageTimings);
2457
+ flattenTree(runtime.executionTree, snapshots, runtime.sharedState, 0, runtime.subflowResults, {}, stageNarrativeMap, stageTimings, commitIndex);
2365
2458
  return snapshots;
2366
2459
  }
2367
2460
  function extractStageTimings(recorders) {
@@ -2405,7 +2498,7 @@ function buildStageNarrativeMap(entries) {
2405
2498
  }
2406
2499
  return map;
2407
2500
  }
2408
- function flattenTree(node, out, sharedState, accumulatedMs = 0, subflowResults, cumulativeMemory = {}, stageNarrativeMap = /* @__PURE__ */ new Map(), stageTimings = /* @__PURE__ */ new Map()) {
2501
+ function flattenTree(node, out, sharedState, accumulatedMs = 0, subflowResults, cumulativeMemory = {}, stageNarrativeMap = /* @__PURE__ */ new Map(), stageTimings = /* @__PURE__ */ new Map(), commitIndex = /* @__PURE__ */ new Map()) {
2409
2502
  const stageName = node.name ?? node.id;
2410
2503
  const durationMs = (stageName ? stageTimings.get(stageName) : void 0) ?? (typeof node.metrics?.durationMs === "number" ? node.metrics.durationMs : 0);
2411
2504
  const startMs = accumulatedMs;
@@ -2425,12 +2518,16 @@ function flattenTree(node, out, sharedState, accumulatedMs = 0, subflowResults,
2425
2518
  narrative = parts.join("\n");
2426
2519
  }
2427
2520
  const memory = { ...cumulativeMemory };
2428
- if (node.stageWrites) {
2521
+ const bundles = node.runtimeStageId ? commitIndex.get(node.runtimeStageId) : void 0;
2522
+ if (bundles && bundles.length > 0) {
2523
+ for (const bundle of bundles) applyCommitBundle(memory, bundle);
2524
+ } else if (node.stageWrites) {
2429
2525
  for (const [key, value] of Object.entries(node.stageWrites)) {
2526
+ if (UNSAFE_KEYS.has(key)) continue;
2430
2527
  if (value === void 0) {
2431
2528
  delete memory[key];
2432
2529
  } else {
2433
- memory[key] = value;
2530
+ memory[key] = mergeWritePatch(memory[key], value);
2434
2531
  }
2435
2532
  }
2436
2533
  }
@@ -2452,13 +2549,13 @@ function flattenTree(node, out, sharedState, accumulatedMs = 0, subflowResults,
2452
2549
  if (node.children && node.children.length > 0) {
2453
2550
  let maxChildEnd = nextMs;
2454
2551
  for (const child of node.children) {
2455
- const childEnd = flattenTree(child, out, sharedState, nextMs, subflowResults, memory, stageNarrativeMap, stageTimings);
2552
+ const childEnd = flattenTree(child, out, sharedState, nextMs, subflowResults, memory, stageNarrativeMap, stageTimings, commitIndex);
2456
2553
  maxChildEnd = Math.max(maxChildEnd, childEnd);
2457
2554
  }
2458
2555
  nextMs = maxChildEnd;
2459
2556
  }
2460
2557
  if (node.next) {
2461
- nextMs = flattenTree(node.next, out, sharedState, nextMs, subflowResults, memory, stageNarrativeMap, stageTimings);
2558
+ nextMs = flattenTree(node.next, out, sharedState, nextMs, subflowResults, memory, stageNarrativeMap, stageTimings, commitIndex);
2462
2559
  }
2463
2560
  return nextMs;
2464
2561
  }
@@ -6300,6 +6397,7 @@ function TraceViewer({
6300
6397
  createSnapshots,
6301
6398
  defaultTokens,
6302
6399
  extractSubflowNarrative,
6400
+ mergeWritePatch,
6303
6401
  rawDefaults,
6304
6402
  subflowResultToSnapshots,
6305
6403
  themePresets,