agentchrome 1.17.0

A CLI tool for browser automation via the Chrome DevTools Protocol
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
use std::time::Duration;

use serde::Serialize;

use agentchrome::cdp::{CdpClient, CdpConfig};
use agentchrome::connection::{ManagedSession, resolve_connection, resolve_target};
use agentchrome::error::{AppError, ExitCode};

use crate::cli::{GlobalOpts, OutputFormat};
use crate::emulate::apply_emulate_state;
use crate::snapshot;

// =============================================================================
// Constants
// =============================================================================

/// Default large-response threshold in bytes (16 KB).
pub const DEFAULT_THRESHOLD: usize = 16_384;

// =============================================================================
// Temp file output struct
// =============================================================================

#[derive(Serialize)]
pub struct TempFileOutput {
    pub output_file: String,
    pub size_bytes: u64,
    pub command: String,
    pub summary: serde_json::Value,
}

// =============================================================================
// Helpers
// =============================================================================

/// Write content to a UUID-named temp file and return the file path.
pub fn write_temp_file(content: &str, extension: &str) -> Result<String, AppError> {
    let id = uuid::Uuid::new_v4();
    let filename = format!("agentchrome-{id}.{extension}");
    let path = std::env::temp_dir().join(filename);
    std::fs::write(&path, content).map_err(|e| AppError {
        message: format!("failed to write temp file {}: {e}", path.display()),
        code: ExitCode::GeneralError,
        custom_json: None,
    })?;
    Ok(path.to_string_lossy().into_owned())
}

#[allow(clippy::needless_pass_by_value)]
fn serialization_error(e: serde_json::Error) -> AppError {
    AppError {
        message: format!("serialization error: {e}"),
        code: ExitCode::GeneralError,
        custom_json: None,
    }
}

/// Format a byte count as a human-readable string.
#[cfg(test)]
#[allow(clippy::cast_precision_loss)]
pub fn format_human_size(bytes: u64) -> String {
    if bytes >= 1_048_576 {
        format!("{:.1} MB", bytes as f64 / 1_048_576.0)
    } else if bytes >= 1024 {
        format!("{} KB", bytes / 1024)
    } else {
        format!("{bytes} bytes")
    }
}

// =============================================================================
// Shared output helpers
// =============================================================================

/// Serialize a value to JSON and print to stdout.
///
/// Uses compact or pretty formatting based on `OutputFormat` flags.
pub fn print_output(value: &impl Serialize, output: &OutputFormat) -> Result<(), AppError> {
    let json = if output.pretty {
        serde_json::to_string_pretty(value)
    } else {
        serde_json::to_string(value)
    };
    let json = json.map_err(serialization_error)?;
    println!("{json}");
    Ok(())
}

// =============================================================================
// CDP config helper
// =============================================================================

/// Build a `CdpConfig` from the global CLI options.
pub fn cdp_config(global: &GlobalOpts) -> CdpConfig {
    let mut config = CdpConfig::default();
    if let Some(timeout_ms) = global.timeout {
        config.command_timeout = Duration::from_millis(timeout_ms);
    }
    config
}

// =============================================================================
// Session setup
// =============================================================================

/// Connect to Chrome, attach to a target, and apply emulation state.
pub async fn setup_session(global: &GlobalOpts) -> Result<(CdpClient, ManagedSession), AppError> {
    let conn = resolve_connection(&global.host, global.port, global.ws_url.as_deref()).await?;
    let target = resolve_target(
        &conn.host,
        conn.port,
        global.tab.as_deref(),
        global.page_id.as_deref(),
    )
    .await?;

    let config = cdp_config(global);
    let client = CdpClient::connect(&conn.ws_url, config).await?;
    let session = client.create_session(&target.id).await?;
    let mut managed = ManagedSession::new(session);
    apply_emulate_state(&mut managed).await?;

    Ok((client, managed))
}

/// Like `setup_session`, but without applying emulation state.
///
/// Used by emulate commands that set (rather than apply) emulation state.
pub async fn setup_session_bare(
    global: &GlobalOpts,
) -> Result<(CdpClient, ManagedSession), AppError> {
    let conn = resolve_connection(&global.host, global.port, global.ws_url.as_deref()).await?;
    let target = resolve_target(
        &conn.host,
        conn.port,
        global.tab.as_deref(),
        global.page_id.as_deref(),
    )
    .await?;

    let config = cdp_config(global);
    let client = CdpClient::connect(&conn.ws_url, config).await?;
    let session = client.create_session(&target.id).await?;
    let managed = ManagedSession::new(session);

    Ok((client, managed))
}

/// Like `setup_session`, but also installs dialog interceptors.
pub async fn setup_session_with_interceptors(
    global: &GlobalOpts,
) -> Result<(CdpClient, ManagedSession), AppError> {
    let (client, managed) = setup_session(global).await?;
    managed.install_dialog_interceptors().await;
    Ok((client, managed))
}

// =============================================================================
// Frame resolution
// =============================================================================

/// Resolve the optional `--frame` argument and return a `FrameContext`.
///
/// `uid` is the target element identifier; it is required when `frame` is
/// `"auto"` so that `resolve_frame_auto` can locate the correct frame.
pub async fn resolve_optional_frame(
    client: &CdpClient,
    managed: &mut ManagedSession,
    frame: Option<&str>,
    uid: Option<&str>,
) -> Result<Option<agentchrome::frame::FrameContext>, AppError> {
    if let Some(frame_str) = frame {
        let arg = agentchrome::frame::parse_frame_arg(frame_str)?;
        if matches!(arg, agentchrome::frame::FrameArg::Auto) {
            let target_uid = uid.unwrap_or_default();
            let state = snapshot::read_snapshot_state().ok().flatten();
            let hint = state
                .as_ref()
                .and_then(|s| s.frame_index.map(|idx| (idx, &s.uid_map)));
            let (ctx, _frame_idx) =
                agentchrome::frame::resolve_frame_auto(client, managed, target_uid, hint).await?;
            Ok(Some(ctx))
        } else {
            let ctx = agentchrome::frame::resolve_frame(client, managed, &arg).await?;
            Ok(Some(ctx))
        }
    } else {
        Ok(None)
    }
}

// =============================================================================
// Emit functions
// =============================================================================

/// Emit plain text through the large-response gate.
///
/// If the text exceeds the threshold, it is written to a temp file and
/// the file path is printed on stdout instead.
pub fn emit_plain(text: &str, output: &OutputFormat) -> Result<(), AppError> {
    let threshold = output.large_response_threshold.unwrap_or(DEFAULT_THRESHOLD);

    if text.len() <= threshold {
        print!("{text}");
        return Ok(());
    }

    let path = write_temp_file(text, "txt")?;
    println!("{path}");
    Ok(())
}

/// Emit a serializable value through the large-response gate.
///
/// If the serialized JSON exceeds the threshold, the full output is written
/// to a UUID-named temp file and a `TempFileOutput` object is printed instead.
pub fn emit<T, F>(
    value: &T,
    output: &OutputFormat,
    command_name: &str,
    summary_fn: F,
) -> Result<(), AppError>
where
    T: Serialize,
    F: FnOnce(&T) -> serde_json::Value,
{
    // 1. Serialize to JSON string (once)
    let json_string = if output.pretty {
        serde_json::to_string_pretty(value)
    } else {
        serde_json::to_string(value)
    }
    .map_err(serialization_error)?;

    // 2. Determine effective threshold
    let threshold = output.large_response_threshold.unwrap_or(DEFAULT_THRESHOLD);

    // 3. If under threshold, print and return
    if json_string.len() <= threshold {
        println!("{json_string}");
        return Ok(());
    }

    // 4. Write to temp file
    let path = write_temp_file(&json_string, "json")?;

    // 5. Build and print TempFileOutput
    let summary = summary_fn(value);
    #[allow(clippy::cast_possible_truncation)]
    let size_bytes = json_string.len() as u64;

    let temp_output = TempFileOutput {
        output_file: path,
        size_bytes,
        command: command_name.to_string(),
        summary,
    };

    let output_json = serde_json::to_string(&temp_output).map_err(serialization_error)?;
    println!("{output_json}");
    Ok(())
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn format_human_size_bytes() {
        assert_eq!(format_human_size(500), "500 bytes");
        assert_eq!(format_human_size(0), "0 bytes");
        assert_eq!(format_human_size(1023), "1023 bytes");
    }

    #[test]
    fn format_human_size_kb() {
        assert_eq!(format_human_size(1024), "1 KB");
        assert_eq!(format_human_size(16_384), "16 KB");
        assert_eq!(format_human_size(1_048_575), "1023 KB");
    }

    #[test]
    fn format_human_size_mb() {
        assert_eq!(format_human_size(1_048_576), "1.0 MB");
        assert_eq!(format_human_size(5_242_880), "5.0 MB");
    }

    #[test]
    fn temp_file_output_serialization() {
        let output = TempFileOutput {
            output_file: "/tmp/agentchrome-abc.json".to_string(),
            size_bytes: 32_768,
            command: "page snapshot".to_string(),
            summary: serde_json::json!({"total_nodes": 5000}),
        };
        let json: serde_json::Value = serde_json::to_value(&output).unwrap();
        assert_eq!(json["output_file"], "/tmp/agentchrome-abc.json");
        assert_eq!(json["size_bytes"], 32_768);
        assert_eq!(json["command"], "page snapshot");
        assert_eq!(json["summary"]["total_nodes"], 5000);
    }

    #[test]
    fn temp_file_output_has_exactly_four_keys() {
        let output = TempFileOutput {
            output_file: "/tmp/test.json".to_string(),
            size_bytes: 100,
            command: "test".to_string(),
            summary: serde_json::json!({}),
        };
        let json: serde_json::Value = serde_json::to_value(&output).unwrap();
        let keys = json.as_object().unwrap();
        assert_eq!(keys.len(), 4);
        assert!(keys.contains_key("output_file"));
        assert!(keys.contains_key("size_bytes"));
        assert!(keys.contains_key("command"));
        assert!(keys.contains_key("summary"));
    }

    #[test]
    fn write_temp_file_creates_readable_file() {
        let content = "hello temp file";
        let path = write_temp_file(content, "txt").unwrap();
        let read_back = std::fs::read_to_string(&path).unwrap();
        assert_eq!(read_back, content);
        assert!(path.contains("agentchrome-"));
        assert!(
            std::path::Path::new(&path)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("txt"))
        );
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn write_temp_file_json_extension() {
        let content = r#"{"key":"value"}"#;
        let path = write_temp_file(content, "json").unwrap();
        assert!(
            std::path::Path::new(&path)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("json"))
        );
        let _ = std::fs::remove_file(&path);
    }

    #[test]
    fn write_temp_file_uuid_uniqueness() {
        let path1 = write_temp_file("a", "txt").unwrap();
        let path2 = write_temp_file("b", "txt").unwrap();
        assert_ne!(path1, path2);
        let _ = std::fs::remove_file(&path1);
        let _ = std::fs::remove_file(&path2);
    }

    #[test]
    fn emit_below_threshold_prints_json() {
        let value = serde_json::json!({"key": "value"});
        let output = OutputFormat {
            json: true,
            pretty: false,
            plain: false,
            large_response_threshold: Some(1_000_000),
        };
        let result = emit(&value, &output, "test", |_| serde_json::json!({}));
        assert!(result.is_ok());
    }

    #[test]
    fn emit_above_threshold_creates_temp_file() {
        // Create a large value that exceeds the threshold
        let large_string: String = "x".repeat(1000);
        let value = serde_json::json!({"data": large_string});
        let output = OutputFormat {
            json: true,
            pretty: false,
            plain: false,
            large_response_threshold: Some(10), // Very low threshold
        };
        let result = emit(
            &value,
            &output,
            "test cmd",
            |_| serde_json::json!({"test": true}),
        );
        assert!(result.is_ok());
        // The function prints to stdout; we verify no error occurred.
        // Full integration behavior is tested via BDD.
    }

    #[test]
    fn emit_plain_below_threshold() {
        let output = OutputFormat {
            json: false,
            pretty: false,
            plain: true,
            large_response_threshold: Some(1_000_000),
        };
        let result = emit_plain("short text", &output);
        assert!(result.is_ok());
    }

    #[test]
    fn emit_plain_above_threshold() {
        let output = OutputFormat {
            json: false,
            pretty: false,
            plain: true,
            large_response_threshold: Some(5), // Very low
        };
        let result = emit_plain("this text is longer than 5 bytes", &output);
        assert!(result.is_ok());
    }
}