@@ -78,13 +78,55 @@ export function extractStructuredPages(result: ChromeMcpToolResult): ChromeMcpSt
7878 return structured . length > 0 ? structured : extractTextPages ( result ) ;
7979}
8080
81+ function normalizeSnapshotFields ( record : Record < string , unknown > ) : ChromeMcpSnapshotNode {
82+ const snapshotValue = record . value ;
83+ return {
84+ id : readStringValue ( record . id ) ,
85+ role : readStringValue ( record . role ) ,
86+ name : readStringValue ( record . name ) ,
87+ ...( typeof snapshotValue === "string" ||
88+ typeof snapshotValue === "number" ||
89+ typeof snapshotValue === "boolean"
90+ ? { value : snapshotValue }
91+ : { } ) ,
92+ description : readStringValue ( record . description ) ,
93+ } ;
94+ }
95+
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+
81123export function extractSnapshot ( result : ChromeMcpToolResult ) : ChromeMcpSnapshotNode {
82124 const structured = extractStructuredContent ( result ) ;
83- const snapshot = asNullableRecord ( structured . snapshot ) ;
125+ const snapshot = normalizeSnapshotNode ( structured . snapshot ) ;
84126 if ( ! snapshot ) {
85127 throw new Error ( "Chrome MCP snapshot response was missing structured snapshot data." ) ;
86128 }
87- return snapshot as unknown as ChromeMcpSnapshotNode ;
129+ return snapshot ;
88130}
89131
90132function extractJsonBlock ( text : string ) : unknown {
0 commit comments