@agent-scope/runtime 1.1.0 → 1.3.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
@@ -697,6 +697,131 @@ function walkFiberRoot(fiberRoot, options = {}) {
697
697
  children
698
698
  };
699
699
  }
700
+ var HookLayout2 = 4;
701
+ function isLightweightEffectNode(node) {
702
+ const ms = node.memoizedState;
703
+ if (ms === null || typeof ms !== "object") return false;
704
+ const obj = ms;
705
+ return typeof obj.create === "function" && "deps" in obj && typeof obj.tag === "number";
706
+ }
707
+ function isLightweightRefNode(node) {
708
+ if (node.queue != null) return false;
709
+ const ms = node.memoizedState;
710
+ if (ms === null || typeof ms !== "object" || Array.isArray(ms)) return false;
711
+ const keys = Object.keys(ms);
712
+ return keys.length === 1 && keys[0] === "current";
713
+ }
714
+ function isLightweightMemoTuple(node) {
715
+ if (node.queue != null) return false;
716
+ const ms = node.memoizedState;
717
+ if (!Array.isArray(ms) || ms.length !== 2) return false;
718
+ return ms[1] === null || Array.isArray(ms[1]);
719
+ }
720
+ function isLightweightStateOrReducer(node) {
721
+ return node.queue != null && typeof node.queue === "object" && typeof node.queue.dispatch === "function";
722
+ }
723
+ function isLightweightReducer(node) {
724
+ if (!isLightweightStateOrReducer(node)) return false;
725
+ const q = node.queue;
726
+ if (typeof q.reducer === "function") return true;
727
+ const lrr = q.lastRenderedReducer;
728
+ if (typeof lrr !== "function") return false;
729
+ const name = lrr.name ?? "";
730
+ return name !== "basicStateReducer" && name !== "";
731
+ }
732
+ function classifyHookType(node) {
733
+ if (isLightweightEffectNode(node)) {
734
+ const ms = node.memoizedState;
735
+ return ms.tag & HookLayout2 ? "useLayoutEffect" : "useEffect";
736
+ }
737
+ if (isLightweightRefNode(node)) return "useRef";
738
+ if (isLightweightMemoTuple(node)) {
739
+ const [val] = node.memoizedState;
740
+ return typeof val === "function" ? "useCallback" : "useMemo";
741
+ }
742
+ if (isLightweightStateOrReducer(node)) {
743
+ return isLightweightReducer(node) ? "useReducer" : "useState";
744
+ }
745
+ return "custom";
746
+ }
747
+ function countAndClassifyHooks(fiber) {
748
+ const hookTypes = [];
749
+ let node = fiber.memoizedState ?? null;
750
+ if (node === null || typeof node !== "object" || !("next" in node)) {
751
+ return { hookCount: 0, hookTypes: [] };
752
+ }
753
+ while (node !== null) {
754
+ hookTypes.push(classifyHookType(node));
755
+ node = node.next ?? null;
756
+ }
757
+ return { hookCount: hookTypes.length, hookTypes };
758
+ }
759
+ function walkFiberLightweight(fiber, options = {}) {
760
+ if (fiber === null || fiber === void 0) return null;
761
+ const includeHost = options.includeHostElements ?? false;
762
+ return walkFiberLightweightInner(fiber, includeHost, /* @__PURE__ */ new Set(), 0);
763
+ }
764
+ function walkFiberLightweightInner(fiber, includeHost, visited, depth) {
765
+ if (fiber === null || fiber === void 0) return null;
766
+ if (visited.has(fiber)) return null;
767
+ if (shouldSkip(fiber, includeHost)) return null;
768
+ visited.add(fiber);
769
+ const id = typeof fiber._debugID === "number" ? fiber._debugID : nextId();
770
+ const { hookCount, hookTypes } = countAndClassifyHooks(fiber);
771
+ const children = collectLightweightChildren(fiber, includeHost, visited, depth + 1);
772
+ const node = {
773
+ id,
774
+ name: extractName(fiber),
775
+ type: classifyType(fiber),
776
+ hookCount,
777
+ hookTypes,
778
+ childCount: children.length,
779
+ depth,
780
+ children
781
+ };
782
+ return node;
783
+ }
784
+ function collectLightweightChildren(fiber, includeHost, visited, childDepth) {
785
+ const nodes = [];
786
+ let current = fiber.child ?? null;
787
+ while (current !== null) {
788
+ if (visited.has(current)) {
789
+ current = current.sibling ?? null;
790
+ continue;
791
+ }
792
+ if (shouldSkip(current, includeHost)) {
793
+ const promoted = collectLightweightChildren(current, includeHost, visited, childDepth);
794
+ nodes.push(...promoted);
795
+ } else {
796
+ const node = walkFiberLightweightInner(current, includeHost, visited, childDepth);
797
+ if (node !== null) {
798
+ nodes.push(node);
799
+ }
800
+ }
801
+ current = current.sibling ?? null;
802
+ }
803
+ return nodes;
804
+ }
805
+ function walkFiberRootLightweight(fiberRoot, options = {}) {
806
+ if (!fiberRoot) return null;
807
+ const hostRootFiber = fiberRoot.current ?? null;
808
+ if (!hostRootFiber) return null;
809
+ const includeHost = options.includeHostElements ?? false;
810
+ const visited = /* @__PURE__ */ new Set();
811
+ const children = collectLightweightChildren(hostRootFiber, includeHost, visited, 0);
812
+ if (children.length === 0) return null;
813
+ if (children.length === 1) return children[0] ?? null;
814
+ return {
815
+ id: nextId(),
816
+ name: "Root",
817
+ type: "function",
818
+ hookCount: 0,
819
+ hookTypes: [],
820
+ childCount: children.length,
821
+ depth: 0,
822
+ children
823
+ };
824
+ }
700
825
 
701
826
  // src/suspense-detector.ts
702
827
  var SuspenseComponent2 = 13;
@@ -788,18 +913,35 @@ async function capture(options = {}) {
788
913
  const walkOptions = {
789
914
  includeHostElements: options.includeHostElements ?? false
790
915
  };
791
- const tree = walkFiberRoot(fiberRoot, walkOptions);
792
- if (!tree) {
793
- throw new Error(
794
- "capture(): Fiber tree is empty. Make sure React has rendered at least one component."
795
- );
796
- }
797
916
  const hostRootFiber = fiberRoot.current ?? null;
798
917
  const rootChild = hostRootFiber?.child ?? null;
799
918
  const errors = detectErrors(rootChild);
800
919
  const suspenseBoundaries = detectSuspenseBoundaries(rootChild);
801
920
  const capturedIn = Date.now() - startTime;
802
921
  const consoleEntries = getConsoleEntries();
922
+ if (options.lightweight) {
923
+ const tree2 = walkFiberRootLightweight(fiberRoot, walkOptions);
924
+ if (!tree2) {
925
+ throw new Error(
926
+ "capture(): Fiber tree is empty. Make sure React has rendered at least one component."
927
+ );
928
+ }
929
+ return {
930
+ url,
931
+ timestamp: startTime,
932
+ capturedIn,
933
+ tree: tree2,
934
+ consoleEntries,
935
+ errors,
936
+ suspenseBoundaries
937
+ };
938
+ }
939
+ const tree = walkFiberRoot(fiberRoot, walkOptions);
940
+ if (!tree) {
941
+ throw new Error(
942
+ "capture(): Fiber tree is empty. Make sure React has rendered at least one component."
943
+ );
944
+ }
803
945
  return {
804
946
  url,
805
947
  timestamp: startTime,
@@ -874,6 +1016,8 @@ exports.resetProfilingData = resetProfilingData;
874
1016
  exports.uninstallConsoleInterceptor = uninstallConsoleInterceptor;
875
1017
  exports.waitForReact = waitForReact;
876
1018
  exports.walkFiber = walkFiber;
1019
+ exports.walkFiberLightweight = walkFiberLightweight;
877
1020
  exports.walkFiberRoot = walkFiberRoot;
1021
+ exports.walkFiberRootLightweight = walkFiberRootLightweight;
878
1022
  //# sourceMappingURL=index.cjs.map
879
1023
  //# sourceMappingURL=index.cjs.map