Skip to content

Commit d7c48d1

Browse files
committed
fix(browser): preserve deep MCP snapshots iteratively
1 parent 438090d commit d7c48d1

1 file changed

Lines changed: 28 additions & 13 deletions

File tree

‎extensions/browser/src/browser/chrome-mcp-result.ts‎

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import {
2121
redactChromeMcpProfileLabelForDiagnostic,
2222
} from "./chrome-mcp-diagnostics.js";
2323
import type { ChromeMcpSnapshotNode } from "./chrome-mcp.snapshot.js";
24-
import { ROLE_SNAPSHOT_MAX_DEPTH } from "./snapshot-depth-limit.js";
2524

2625
function asPages(value: unknown): ChromeMcpStructuredPage[] {
2726
if (!Array.isArray(value)) {
@@ -79,18 +78,8 @@ export function extractStructuredPages(result: ChromeMcpToolResult): ChromeMcpSt
7978
return structured.length > 0 ? structured : extractTextPages(result);
8079
}
8180

82-
function normalizeSnapshotNode(value: unknown, depth = 0): ChromeMcpSnapshotNode | null {
83-
const record = asNullableRecord(value);
84-
if (!record || depth > ROLE_SNAPSHOT_MAX_DEPTH) {
85-
return null;
86-
}
81+
function normalizeSnapshotFields(record: Record<string, unknown>): ChromeMcpSnapshotNode {
8782
const snapshotValue = record.value;
88-
const children = Array.isArray(record.children)
89-
? record.children.flatMap((child) => {
90-
const normalized = normalizeSnapshotNode(child, depth + 1);
91-
return normalized ? [normalized] : [];
92-
})
93-
: undefined;
9483
return {
9584
id: readStringValue(record.id),
9685
role: readStringValue(record.role),
@@ -101,10 +90,36 @@ function normalizeSnapshotNode(value: unknown, depth = 0): ChromeMcpSnapshotNode
10190
? { value: snapshotValue }
10291
: {}),
10392
description: readStringValue(record.description),
104-
...(children ? { children } : {}),
10593
};
10694
}
10795

96+
function normalizeSnapshotNode(value: unknown): ChromeMcpSnapshotNode | null {
97+
const rootRecord = asNullableRecord(value);
98+
if (!rootRecord) {
99+
return null;
100+
}
101+
const root = normalizeSnapshotFields(rootRecord);
102+
const pending = [{ record: rootRecord, node: root }];
103+
while (pending.length > 0) {
104+
const current = pending.pop();
105+
if (!current || !Array.isArray(current.record.children)) {
106+
continue;
107+
}
108+
const children: ChromeMcpSnapshotNode[] = [];
109+
for (const child of current.record.children) {
110+
const record = asNullableRecord(child);
111+
if (!record) {
112+
continue;
113+
}
114+
const node = normalizeSnapshotFields(record);
115+
children.push(node);
116+
pending.push({ record, node });
117+
}
118+
current.node.children = children;
119+
}
120+
return root;
121+
}
122+
108123
export function extractSnapshot(result: ChromeMcpToolResult): ChromeMcpSnapshotNode {
109124
const structured = extractStructuredContent(result);
110125
const snapshot = normalizeSnapshotNode(structured.snapshot);

0 commit comments

Comments
 (0)