glass-browser 0.1.2

Lightweight browser agent for AI — raw CDP, no Playwright
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Compact page observation.
//!
//! Produces a bounded [`CompactAccessibilitySnapshot`] with visible text,
//! interactive element summaries, and optional full DOM / screenshot /
//! form-value overlays.

use super::*;

impl BrowserSession {
    /// Return the visible text content of the current page.
    ///
    /// Evaluates `document.body.innerText` and truncates the result to
    /// [`COMPACT_TEXT_MAX_BYTES`] (16 KiB).
    pub async fn text(&self) -> BrowserResult<String> {
        self.cdp
            .with_current_route(async {
                let value = self
                    .evaluate_value("document.body ? document.body.innerText : ''")
                    .await?;
                Ok(truncate_visible_text(
                    value.as_str().unwrap_or_default(),
                    COMPACT_TEXT_MAX_BYTES,
                ))
            })
            .await
    }

    /// Fetch the full DOM only for an explicit deep-inspection operation.
    pub async fn deep_dom(&self) -> BrowserResult<DomNode> {
        self.cdp
            .with_current_route(async {
                let raw = self.cdp.get_deep_document().await?;
                parse_dom_tree(&raw).ok_or_else(|| {
                    "CDP deep DOM response contained no parseable root node"
                        .to_string()
                        .into()
                })
            })
            .await
    }

    /// Collect compact page context without a deep DOM or screenshot.
    pub async fn observe(&self) -> BrowserResult<PageContext> {
        self.observe_internal(false, false, true, false, CompactRanking::Relevance)
            .await
    }

    /// Collect compact context and explicitly include the full DOM tree.
    pub async fn observe_with_dom(&self) -> BrowserResult<PageContext> {
        self.observe_internal(true, false, true, false, CompactRanking::Relevance)
            .await
    }

    /// Collect structured context and explicitly include a current screenshot.
    pub async fn observe_with_screenshot(&self) -> BrowserResult<PageContext> {
        self.observe_internal(false, true, true, false, CompactRanking::Relevance)
            .await
    }

    /// Collect context with both explicitly requested deep DOM and screenshot data.
    pub async fn observe_with_dom_and_screenshot(&self) -> BrowserResult<PageContext> {
        self.observe_internal(true, true, true, false, CompactRanking::Relevance)
            .await
    }

    /// Collect fresh compact context, bypassing the compact-context cache.
    pub async fn observe_fresh(&self) -> BrowserResult<PageContext> {
        self.observe_internal(false, false, false, false, CompactRanking::Relevance)
            .await
    }

    /// Collect compact context with form field values included.
    /// Requires ReadFormValues policy capability in hardened mode.
    pub async fn observe_with_form_values(&self) -> BrowserResult<PageContext> {
        self.observe_internal(false, false, false, true, CompactRanking::Relevance)
            .await
    }

    /// Collect fresh context and explicitly include the full DOM tree.
    pub async fn observe_fresh_with_dom(&self) -> BrowserResult<PageContext> {
        self.observe_internal(true, false, false, false, CompactRanking::Relevance)
            .await
    }

    /// Collect fresh structured context and explicitly include a screenshot.
    pub async fn observe_fresh_with_screenshot(&self) -> BrowserResult<PageContext> {
        self.observe_internal(false, true, false, false, CompactRanking::Relevance)
            .await
    }

    /// Collect fresh context with both explicitly requested deep DOM and screenshot data.
    pub async fn observe_fresh_with_dom_and_screenshot(&self) -> BrowserResult<PageContext> {
        self.observe_internal(true, true, false, false, CompactRanking::Relevance)
            .await
    }

    /// Collect compact context with an explicit truncation ordering.
    pub async fn observe_with_ranking(
        &self,
        ranking: ObservationRanking,
    ) -> BrowserResult<PageContext> {
        let ranking = match ranking {
            ObservationRanking::Relevance => CompactRanking::Relevance,
            ObservationRanking::DocumentOrder => CompactRanking::DocumentOrder,
        };
        self.observe_internal(false, false, false, false, ranking)
            .await
    }

    async fn observe_internal(
        &self,
        include_dom: bool,
        include_screenshot: bool,
        use_cache: bool,
        include_form_values: bool,
        ranking: CompactRanking,
    ) -> BrowserResult<PageContext> {
        if let Some(interception) = &self.policy_interception
            && let Some(error) = interception.take_denial().await
        {
            return Err(error.into());
        }
        self.cdp
            .with_current_route(async {
                let mut context = self
                    .compact_observation(use_cache, include_form_values, ranking)
                    .await?
                    .into_page_context();
                if include_dom {
                    context.dom = Some(self.deep_dom().await?);
                }
                if include_screenshot {
                    context.screenshot = Some(self.screenshot_base64().await?);
                }
                Ok(context)
            })
            .await
    }

    async fn compact_observation(
        &self,
        use_cache: bool,
        include_form_values: bool,
        ranking: CompactRanking,
    ) -> BrowserResult<CompactPageContext> {
        let revision = self.page_revision.load(Ordering::Relaxed);
        // Never use cache when form values are requested (cache doesn't store them)
        if use_cache && !include_form_values {
            let cached_context = {
                let cache = self.observation_cache.lock().await;
                cache
                    .as_ref()
                    .filter(|cached| cached.revision == revision)
                    .map(|cached| cached.context.clone())
            };
            if let Some(context) = cached_context {
                return Ok(context);
            }
        }

        let (target_id, frame_id) = self.route_identity().await?;
        let mut context_id = self.observation_context_id(&target_id, &frame_id).await?;
        let mut collected = None;
        let mut attempt = 1;
        let mut recovered_context = false;
        while attempt <= COMPACT_OBSERVATION_MAX_ATTEMPTS {
            let start_revision = self.page_revision.load(Ordering::Relaxed);
            let attempt_result =
                match tokio::time::timeout(COMPACT_OBSERVATION_ATTEMPT_TIMEOUT, async {
                    let start = self.compact_page_state(context_id).await?;
                    let accessibility = self.cdp.get_accessibility_tree().await?;
                    let end = self.compact_page_state(context_id).await?;
                    BrowserResult::Ok((start, accessibility, end))
                })
                .await
                {
                    Err(_) => {
                        return Err(
                            "compact observation attempt exceeded its one-second deadline".into(),
                        );
                    }
                    Ok(Ok(result)) => result,
                    Ok(Err(error))
                        if is_stale_observation_context(error.as_ref()) && !recovered_context =>
                    {
                        self.discard_observation_context(&target_id, &frame_id, context_id)
                            .await;
                        context_id = self.observation_context_id(&target_id, &frame_id).await?;
                        recovered_context = true;
                        continue;
                    }
                    Ok(Err(error)) => return Err(error),
                };
            let end_revision = self.page_revision.load(Ordering::Relaxed);
            let consistent = start_revision == end_revision
                && attempt_result.0.mutation_revision == attempt_result.2.mutation_revision;
            collected = Some((
                attempt,
                consistent,
                start_revision,
                end_revision,
                attempt_result,
            ));
            if consistent {
                break;
            }
            attempt += 1;
        }
        let (
            attempts,
            consistent,
            start_revision,
            end_revision,
            (start_state, accessibility_raw, page_state),
        ) = collected.expect("observation always performs at least one attempt");
        let page = PageInfo {
            url: page_state.url,
            title: page_state.title,
            ready_state: page_state.ready_state,
            target_id,
            frame_id,
        };
        let full_roots = parse_accessibility_tree(&accessibility_raw);
        let mut compact_accessibility =
            crate::browser::dom::project_compact_accessibility_with_ranking(
                &full_roots,
                end_revision,
                ranking,
            );
        let (mut text, locally_truncated) =
            truncate_visible_text_with_status(&page_state.text, COMPACT_TEXT_MAX_BYTES);
        let text_truncated = locally_truncated || page_state.boundaries.text_truncated;
        if page_state.boundaries.text_truncated && !text.ends_with(TEXT_TRUNCATION_MARKER) {
            let content_limit = COMPACT_TEXT_MAX_BYTES.saturating_sub(TEXT_TRUNCATION_MARKER.len());
            while text.len() > content_limit {
                text.pop();
            }
            text.push_str(TEXT_TRUNCATION_MARKER);
        }
        let mut incomplete = Vec::new();
        if text_truncated {
            incomplete.push(ObservationIncompleteReason::VisibleText);
        }
        if compact_accessibility.nodes_truncated {
            incomplete.push(ObservationIncompleteReason::AccessibilityNode);
        }
        if compact_accessibility.labels_truncated {
            incomplete.push(ObservationIncompleteReason::AccessibilityLabel);
        }
        if compact_accessibility.controls_truncated {
            incomplete.push(ObservationIncompleteReason::Control);
        }
        if page_state.boundaries.child_frames > 0 {
            incomplete.push(ObservationIncompleteReason::FrameBoundary);
        }
        if page_state.boundaries.canvases > 0 {
            incomplete.push(ObservationIncompleteReason::Canvas);
        }
        if page_state.boundaries.truncated {
            incomplete.push(ObservationIncompleteReason::BoundaryScan);
        }
        if !consistent {
            incomplete.push(ObservationIncompleteReason::MutationRace);
        }
        // Shadow piercing: discover which interactive controls are inside open shadow roots.
        let (shadow_paths, pierced_hosts) = if page_state.boundaries.shadow_roots > 0 {
            match self
                .cdp
                .get_flattened_document(crate::browser::dom::MAX_SHADOW_DEPTH as i64)
                .await
            {
                Ok(flattened) => {
                    let paths = crate::browser::dom::build_shadow_host_paths(&flattened);
                    let hosts = crate::browser::dom::count_pierced_shadow_hosts(&paths);
                    (paths, hosts)
                }
                Err(_) => (HashMap::new(), 0),
            }
        } else {
            (HashMap::new(), 0)
        };

        // Only flag ShadowBoundary when hosts were not all pierced
        if page_state.boundaries.shadow_roots > 0
            && pierced_hosts < page_state.boundaries.shadow_roots
        {
            incomplete.push(ObservationIncompleteReason::ShadowBoundary);
        }

        // Apply shadow host paths to interactive controls
        if !shadow_paths.is_empty() {
            for control in compact_accessibility.interactive.iter_mut() {
                if let Some(path) = shadow_paths.get(&control.backend_dom_node_id) {
                    control.shadow_host_path = Some(path.clone());
                }
            }
        }

        // Read form field values when explicitly requested
        if include_form_values {
            self.read_form_field_values(&mut compact_accessibility.interactive)
                .await?;
        }

        let interactive_len = compact_accessibility.interactive.len();
        let accessibility = CompactAccessibilitySnapshot {
            page: page.clone(),
            revision: end_revision,
            roots: compact_accessibility.roots,
            interactive: compact_accessibility.interactive,
            truncated: compact_accessibility.truncated,
            omitted_count: compact_accessibility.omitted_count,
            ranking_applied: compact_accessibility.ranking_applied,
            completeness: Some(ObservationCompleteness::compute(
                compact_accessibility.interactive_discovered,
                interactive_len,
                page_state.boundaries.shadow_roots,
                pierced_hosts,
                page_state.boundaries.canvases,
                page_state.boundaries.child_frames,
                !consistent,
            )),
        };
        let context = CompactPageContext {
            page,
            text,
            accessibility,
            consistency: ObservationConsistency {
                consistent,
                attempts,
                start_revision,
                end_revision,
                start_mutation_revision: start_state.mutation_revision,
                end_mutation_revision: page_state.mutation_revision,
            },
            boundaries: page_state.boundaries,
            incomplete,
        };
        if consistent && self.page_revision.load(Ordering::Relaxed) == end_revision {
            *self.observation_cache.lock().await = Some(CachedObservation {
                revision: end_revision,
                context: context.clone(),
            });
        }
        Ok(context)
    }

    /// Read current values of form controls and populate CompactInteractiveElement fields.
    /// Enforces ReadFormValues policy, max 16 fields, password/CC redaction.
    async fn read_form_field_values(
        &self,
        controls: &mut [CompactInteractiveElement],
    ) -> BrowserResult<()> {
        use crate::browser::dom::{
            FORM_VALUE_MAX_BYTES, FORM_VALUE_MAX_FIELDS, SELECT_OPTION_MAX_BYTES, truncate_utf8,
        };

        self.policy.require(PolicyCapability::ReadFormValues)?;
        let allow_sensitive = self.policy.allow_sensitive_form_values();

        const FORM_ROLES: &[&str] = &[
            "textbox",
            "searchbox",
            "combobox",
            "spinbutton",
            "listbox",
            "checkbox",
            "radio",
            "switch",
            "slider",
        ];

        // Prioritize controls with backend node IDs and form-relevant roles
        let mut candidates: Vec<&mut CompactInteractiveElement> = controls
            .iter_mut()
            .filter(|c| {
                FORM_ROLES.iter().any(|r| c.role.eq_ignore_ascii_case(r))
                    && c.backend_dom_node_id > 0
            })
            .take(FORM_VALUE_MAX_FIELDS)
            .collect();

        if candidates.is_empty() {
            return Ok(());
        }

        // Read values via CDP: resolve backend node IDs → object IDs → call function
        let expression = r#"function() {
            const el = this;
            const result = { empty: true };
            const tag = (el.tagName || '').toLowerCase();
            if (tag === 'input') {
                const type = (el.type || 'text').toLowerCase();
                if (type === 'checkbox' || type === 'radio') {
                    result.checked = el.checked;
                    result.value = el.value;
                } else {
                    result.value = el.value;
                }
            } else if (tag === 'select') {
                const opt = el.options[el.selectedIndex];
                result.selectedOption = opt ? (opt.label || opt.text || opt.value) : '';
                result.value = el.value;
            } else if (tag === 'textarea') {
                result.value = el.value;
            } else {
                result.value = el.value || el.textContent || '';
            }
            result.empty = !result.value && !result.selectedOption && !result.checked;
            result.readOnly = !!el.readOnly;
            result.required = !!el.required;
            result.autocomplete = el.getAttribute('autocomplete') || '';
            result.inputType = (el.type || '').toLowerCase();
            return JSON.stringify(result);
        }"#;

        for control in candidates.iter_mut() {
            let resolved = match self
                .cdp
                .send(
                    "DOM.resolveNode",
                    Some(serde_json::json!({
                        "backendNodeId": control.backend_dom_node_id,
                    })),
                )
                .await
            {
                Ok(resolved) => resolved,
                Err(_) => continue,
            };

            let Some(object_id) = resolved["object"]["objectId"].as_str() else {
                continue;
            };
            let remote = RemoteObjectGuard::new(self.cdp.clone(), object_id.to_string());

            let raw_result = self
                .cdp
                .send(
                    "Runtime.callFunctionOn",
                    Some(serde_json::json!({
                        "objectId": &remote.object_id,
                        "functionDeclaration": expression,
                        "returnByValue": true,
                        "awaitPromise": false,
                    })),
                )
                .await;
            let raw = match raw_result {
                Ok(raw) => raw,
                Err(_) => continue,
            };

            let value_str = raw["result"]["value"].as_str().unwrap_or("{}");
            let parsed: Value = match serde_json::from_str(value_str) {
                Ok(v) => v,
                Err(_) => continue,
            };

            let input_type = parsed["inputType"]
                .as_str()
                .map(String::from)
                .or_else(|| control.input_type.clone());

            let is_password = input_type.as_deref() == Some("password");
            let is_sensitive_autocomplete = parsed["autocomplete"]
                .as_str()
                .map(|ac| ac.starts_with("cc-") || ac == "current-password" || ac == "new-password")
                .unwrap_or(false);

            if let Some(val) = parsed["value"].as_str() {
                if (is_password || is_sensitive_autocomplete) && !allow_sensitive {
                    control.value = Some("<redacted>".to_string());
                } else {
                    let (truncated, _) = truncate_utf8(val, FORM_VALUE_MAX_BYTES);
                    control.value = Some(truncated.to_string());
                }
            }

            if let Some(checked) = parsed["checked"].as_bool() {
                control.checked = Some(checked);
            }

            if let Some(opt) = parsed["selectedOption"].as_str() {
                let (truncated, _) = truncate_utf8(opt, SELECT_OPTION_MAX_BYTES);
                control.selected_option = Some(truncated.to_string());
            }

            control.empty = parsed["empty"].as_bool().unwrap_or(true);
            control.read_only = parsed["readOnly"].as_bool().unwrap_or(false);
            control.required = parsed["required"].as_bool().unwrap_or(false);

            if let Some(it) = input_type {
                control.input_type = Some(it);
            }
        }

        Ok(())
    }

    async fn compact_page_state(&self, context_id: i64) -> BrowserResult<EvaluatedPageState> {
        let raw = self
            .cdp
            .evaluate_in_context(COMPACT_PAGE_STATE_EXPRESSION, Some(context_id))
            .await?;
        Ok(serde_json::from_value(runtime_value(&raw)?)?)
    }

    async fn observation_context_id(&self, target_id: &str, frame_id: &str) -> BrowserResult<i64> {
        let session_id = self.cdp.current_session_id();
        {
            let context = self.observation_context.lock().await;
            if let Some(cached) = context.as_ref()
                && cached.target_id == target_id
                && cached.session_id == session_id
                && cached.frame_id == frame_id
            {
                return Ok(cached.context_id);
            }
        }

        let world = self
            .cdp
            .send(
                "Page.createIsolatedWorld",
                Some(serde_json::json!({"frameId": frame_id, "worldName": "glass-observation"})),
            )
            .await?;
        let context_id = world["executionContextId"]
            .as_i64()
            .ok_or("Page.createIsolatedWorld returned no executionContextId")?;
        *self.observation_context.lock().await = Some(CachedObservationContext {
            target_id: target_id.to_string(),
            session_id,
            frame_id: frame_id.to_string(),
            context_id,
        });
        Ok(context_id)
    }

    async fn discard_observation_context(&self, target_id: &str, frame_id: &str, context_id: i64) {
        let mut context = self.observation_context.lock().await;
        if context.as_ref().is_some_and(|cached| {
            cached.target_id == target_id
                && cached.frame_id == frame_id
                && cached.context_id == context_id
        }) {
            context.take();
        }
    }
}

fn is_stale_observation_context(error: &(dyn std::error::Error + 'static)) -> bool {
    error
        .downcast_ref::<crate::browser::cdp::CdpError>()
        .is_some_and(|error| {
            error.message.contains("Cannot find context")
                || error.message.contains("Execution context was destroyed")
        })
}