computeruse-rs 2.0.0

A Playwright-style SDK for automating desktop GUI applications
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
//! Desktop-dependent Computer Use functionality
//!
//! This module contains the Desktop-dependent parts of Computer Use:
//! - Window capture for screenshots
//! - Action execution that requires Desktop APIs
//! - The main `gemini_computer_use` method on Desktop
//!
//! Types and backend API are in the `computeruse-computer-use` crate.

use crate::Desktop;
use anyhow::Result;
use base64::{engine::general_purpose, Engine as _};
use chrono::Local;
use image::codecs::png::PngEncoder;
use image::imageops::FilterType;
use image::{ExtendedColorType, ImageBuffer, ImageEncoder, Rgba};
use std::fs;
use std::io::Cursor;
use std::path::PathBuf;
use std::time::Duration;
use sysinfo::{ProcessesToUpdate, System};
use computeruse_computer_use::{
    call_computer_use_backend, convert_normalized_to_screen, translate_gemini_keys,
    ComputerUseActionResponse, ComputerUsePreviousAction, ComputerUseResult, ComputerUseStep,
    ProgressCallback,
};
use tracing::{info, warn};

// ===== Internal Types =====

/// Window capture data for computer use
struct WindowCaptureData {
    /// Base64 encoded PNG screenshot
    base64_image: String,
    /// Window bounds (x, y, width, height)
    window_bounds: (f64, f64, f64, f64),
    /// DPI scale factor
    dpi_scale: f64,
    /// Resize scale factor (if image was resized)
    resize_scale: f64,
    /// Browser URL if available
    browser_url: Option<String>,
}

// ===== Screenshot Storage =====

/// Get the executions directory and ensure it exists.
/// Returns the path to %LOCALAPPDATA%/computeruse/executions/
fn get_executions_dir() -> Result<PathBuf, String> {
    let dir = dirs::data_local_dir()
        .ok_or_else(|| "Failed to get LOCALAPPDATA directory".to_string())?
        .join("computeruse")
        .join("executions");

    fs::create_dir_all(&dir)
        .map_err(|e| format!("Failed to create executions directory: {}", e))?;

    Ok(dir)
}

/// Generate execution ID from timestamp and process name.
fn generate_execution_id(process: &str) -> String {
    let timestamp = Local::now().format("%Y%m%d_%H%M%S").to_string();
    let safe_process = process
        .chars()
        .map(|c| {
            if c.is_alphanumeric() || c == '-' || c == '_' {
                c
            } else {
                '_'
            }
        })
        .collect::<String>();
    format!("{}_geminiComputerUse_{}", timestamp, safe_process)
}

/// Save a base64-encoded PNG screenshot to disk (async, non-blocking).
/// Spawns a background task so it doesn't slow down the computer use loop.
fn save_screenshot_async(base64_image: String, path: PathBuf) {
    tokio::spawn(async move {
        let result = tokio::task::spawn_blocking(move || {
            let png_data = general_purpose::STANDARD
                .decode(&base64_image)
                .map_err(|e| format!("Failed to decode base64 screenshot: {}", e))?;

            fs::write(&path, png_data)
                .map_err(|e| format!("Failed to write screenshot to {}: {}", path.display(), e))?;

            Ok::<(), String>(())
        })
        .await;

        if let Err(e) = result {
            warn!("[computer_use] Screenshot save task failed: {:?}", e);
        } else if let Ok(Err(e)) = result {
            warn!("[computer_use] Failed to save screenshot: {}", e);
        }
    });
}

/// Save the execution result as JSON (flat structure).
fn save_execution_result(
    result: &ComputerUseResult,
    executions_dir: &std::path::Path,
    execution_id: &str,
) -> Result<(), String> {
    let json = serde_json::to_string_pretty(result)
        .map_err(|e| format!("Failed to serialize execution result: {}", e))?;

    let path = executions_dir.join(format!("{}.json", execution_id));
    fs::write(&path, json).map_err(|e| format!("Failed to write {}: {}", path.display(), e))?;

    info!(
        "[computer_use] Saved execution result to {}",
        path.display()
    );

    Ok(())
}

// ===== Window Capture =====

/// Capture window screenshot for computer use
fn capture_window_for_computer_use(
    desktop: &Desktop,
    process: &str,
) -> Result<WindowCaptureData, String> {
    // Find the window element for this process using sysinfo to match process names
    let apps = desktop
        .applications()
        .map_err(|e| format!("Failed to get applications: {e}"))?;

    let mut system = System::new();
    system.refresh_processes(ProcessesToUpdate::All, true);

    let window_element = apps
        .into_iter()
        .find(|app| {
            let app_pid = app.process_id().unwrap_or(0);
            if app_pid > 0 {
                system
                    .process(sysinfo::Pid::from_u32(app_pid))
                    .map(|p| {
                        let process_name = p.name().to_string_lossy().to_string();
                        process_name
                            .to_lowercase()
                            .contains(&process.to_lowercase())
                    })
                    .unwrap_or(false)
            } else {
                false
            }
        })
        .ok_or_else(|| format!("No window found for process '{process}'"))?;

    // Get browser URL if available, or create synthetic URL for desktop apps
    // Gemini Computer Use API requires URL field for multi-step operations
    let browser_url = window_element.url().or_else(|| {
        // For desktop apps, create a synthetic URL: app://{process}/{window_name}
        let window_name = window_element
            .name()
            .unwrap_or_default()
            .replace([' ', '/'], "_");
        Some(format!("app://{}/{}", process, window_name))
    });

    // Get window bounds (absolute screen coordinates)
    let bounds = window_element
        .bounds()
        .map_err(|e| format!("Failed to get window bounds: {e}"))?;
    let (window_x, window_y, win_w, _win_h) = bounds;

    // Capture screenshot
    let screenshot = window_element
        .capture()
        .map_err(|e| format!("Failed to capture screenshot: {e}"))?;

    let original_width = screenshot.width;
    let original_height = screenshot.height;

    // Calculate DPI scale
    let dpi_scale_w = original_width as f64 / win_w;

    // Convert BGRA to RGBA
    let rgba_data: Vec<u8> = screenshot
        .image_data
        .chunks_exact(4)
        .flat_map(|bgra| [bgra[2], bgra[1], bgra[0], bgra[3]])
        .collect();

    // Resize if needed (max 1920px)
    const MAX_DIM: u32 = 1920;
    let (final_width, final_height, final_rgba_data, resize_scale) = if original_width > MAX_DIM
        || original_height > MAX_DIM
    {
        let scale = (MAX_DIM as f32 / original_width.max(original_height) as f32).min(1.0);
        let new_width = (original_width as f32 * scale).round() as u32;
        let new_height = (original_height as f32 * scale).round() as u32;

        let img = ImageBuffer::<Rgba<u8>, _>::from_raw(original_width, original_height, rgba_data)
            .ok_or("Failed to create image buffer")?;
        let resized = image::imageops::resize(&img, new_width, new_height, FilterType::Lanczos3);

        (new_width, new_height, resized.into_raw(), scale as f64)
    } else {
        (original_width, original_height, rgba_data, 1.0)
    };

    // Encode to PNG
    let mut png_data = Vec::new();
    let encoder = PngEncoder::new(Cursor::new(&mut png_data));
    encoder
        .write_image(
            &final_rgba_data,
            final_width,
            final_height,
            ExtendedColorType::Rgba8,
        )
        .map_err(|e| format!("Failed to encode PNG: {e}"))?;

    // Base64 encode
    let base64_image = general_purpose::STANDARD.encode(&png_data);

    Ok(WindowCaptureData {
        base64_image,
        window_bounds: (window_x, window_y, final_width as f64, final_height as f64),
        dpi_scale: dpi_scale_w,
        resize_scale,
        browser_url,
    })
}

// ===== Action Execution =====

/// Execute a computer use action
async fn execute_action(
    desktop: &Desktop,
    process: &str,
    action: &str,
    args: &serde_json::Value,
    window_bounds: (f64, f64, f64, f64),
    dpi_scale: f64,
    resize_scale: f64,
) -> Result<(), String> {
    let (window_x, window_y, screenshot_w, screenshot_h) = window_bounds;

    // Helper to get values from args
    let get_f64 = |key: &str| -> Option<f64> { args.get(key).and_then(|v| v.as_f64()) };
    let get_str = |key: &str| -> Option<&str> { args.get(key).and_then(|v| v.as_str()) };
    let get_bool = |key: &str| -> Option<bool> { args.get(key).and_then(|v| v.as_bool()) };

    // Helper to convert 0-999 normalized coords to absolute screen coords
    let convert_coord = |norm_x: f64, norm_y: f64| -> (f64, f64) {
        convert_normalized_to_screen(
            norm_x,
            norm_y,
            window_x,
            window_y,
            screenshot_w,
            screenshot_h,
            dpi_scale,
            resize_scale,
        )
    };

    match action {
        "click_at" => {
            let x = get_f64("x").ok_or("click_at requires x coordinate")?;
            let y = get_f64("y").ok_or("click_at requires y coordinate")?;
            let (screen_x, screen_y) = convert_coord(x, y);
            info!(
                "[computer_use] click_at ({}, {}) -> screen ({}, {})",
                x, y, screen_x, screen_y
            );
            desktop
                .click_at_coordinates(screen_x, screen_y, false)
                .map_err(|e| format!("Click failed: {e}"))?;
        }
        "type_text_at" => {
            let x = get_f64("x").ok_or("type_text_at requires x coordinate")?;
            let y = get_f64("y").ok_or("type_text_at requires y coordinate")?;
            let text = get_str("text").ok_or("type_text_at requires text")?;
            let press_enter = get_bool("press_enter").unwrap_or(false);
            let (screen_x, screen_y) = convert_coord(x, y);
            info!(
                "[computer_use] type_text_at ({}, {}) -> screen ({}, {}), text: {}",
                x, y, screen_x, screen_y, text
            );
            // Click first to focus
            desktop
                .click_at_coordinates(screen_x, screen_y, false)
                .map_err(|e| format!("Click before type failed: {e}"))?;
            tokio::time::sleep(Duration::from_millis(100)).await;
            // Select all (Ctrl+A) to clear existing text before typing
            desktop
                .press_key("{Ctrl}a")
                .await
                .map_err(|e| format!("Select all failed: {e}"))?;
            tokio::time::sleep(Duration::from_millis(50)).await;
            // Type text using root element (this will replace selected text)
            let root = desktop.root();
            root.type_text(text, false)
                .map_err(|e| format!("Type text failed: {e}"))?;
            // Press Enter if requested
            if press_enter {
                tokio::time::sleep(Duration::from_millis(50)).await;
                desktop
                    .press_key("{Enter}")
                    .await
                    .map_err(|e| format!("Press Enter failed: {e}"))?;
            }
        }
        "key_combination" => {
            let keys = get_str("keys").ok_or("key_combination requires keys")?;
            let translated = translate_gemini_keys(keys)?;
            info!("[computer_use] key_combination: {} -> {}", keys, translated);
            desktop
                .press_key(&translated)
                .await
                .map_err(|e| format!("Key press failed: {e}"))?;
        }
        "scroll_document" | "scroll_at" => {
            let direction = get_str("direction").ok_or("scroll requires direction")?;
            let magnitude = get_f64("magnitude").unwrap_or(3.0);
            let amount: f64 = match direction {
                "up" => -magnitude,
                "down" => magnitude,
                "left" => -magnitude,
                "right" => magnitude,
                _ => magnitude,
            };
            info!("[computer_use] scroll: {} (amount: {})", direction, amount);
            // If coordinates provided, click there first to focus
            if let (Some(x), Some(y)) = (get_f64("x"), get_f64("y")) {
                let (screen_x, screen_y) = convert_coord(x, y);
                desktop
                    .click_at_coordinates(screen_x, screen_y, false)
                    .map_err(|e| format!("Click before scroll failed: {e}"))?;
                tokio::time::sleep(Duration::from_millis(100)).await;
            }
            // Scroll using root element
            let root = desktop.root();
            root.scroll(direction, amount)
                .map_err(|e| format!("Scroll failed: {e}"))?;
        }
        "drag_and_drop" => {
            let start_x = get_f64("x")
                .or(get_f64("start_x"))
                .ok_or("drag_and_drop requires x/start_x")?;
            let start_y = get_f64("y")
                .or(get_f64("start_y"))
                .ok_or("drag_and_drop requires y/start_y")?;
            let end_x = get_f64("destination_x")
                .or(get_f64("end_x"))
                .ok_or("drag_and_drop requires destination_x/end_x")?;
            let end_y = get_f64("destination_y")
                .or(get_f64("end_y"))
                .ok_or("drag_and_drop requires destination_y/end_y")?;
            let (start_screen_x, start_screen_y) = convert_coord(start_x, start_y);
            let (end_screen_x, end_screen_y) = convert_coord(end_x, end_y);
            info!(
                "[computer_use] drag_and_drop from ({}, {}) to ({}, {})",
                start_screen_x, start_screen_y, end_screen_x, end_screen_y
            );
            let root = desktop.root();
            root.mouse_drag(start_screen_x, start_screen_y, end_screen_x, end_screen_y)
                .map_err(|e| format!("Drag failed: {e}"))?;
        }
        "wait_5_seconds" => {
            info!("[computer_use] waiting 5 seconds");
            tokio::time::sleep(Duration::from_secs(5)).await;
        }
        "hover_at" => {
            let x = get_f64("x").ok_or("hover_at requires x coordinate")?;
            let y = get_f64("y").ok_or("hover_at requires y coordinate")?;
            let (screen_x, screen_y) = convert_coord(x, y);
            info!(
                "[computer_use] hover_at ({}, {}) -> screen ({}, {})",
                x, y, screen_x, screen_y
            );
            let root = desktop.root();
            root.mouse_move(screen_x, screen_y)
                .map_err(|e| format!("Mouse move failed: {e}"))?;
        }
        "navigate" => {
            let url = get_str("url").ok_or("navigate requires url")?;
            info!("[computer_use] navigate to: {} (process: {})", url, process);
            // First, activate the browser window to ensure it has focus
            if let Err(e) = desktop.activate_application(process) {
                warn!("[computer_use] Failed to activate {}: {}", process, e);
            }
            tokio::time::sleep(Duration::from_millis(200)).await;
            // Use Ctrl+L to focus address bar, then type URL and press Enter
            // This navigates the current tab instead of opening a new one
            desktop
                .press_key("{Ctrl}l")
                .await
                .map_err(|e| format!("Focus address bar failed: {e}"))?;
            tokio::time::sleep(Duration::from_millis(150)).await;
            // Select all existing text in address bar
            desktop
                .press_key("{Ctrl}a")
                .await
                .map_err(|e| format!("Select all in address bar failed: {e}"))?;
            tokio::time::sleep(Duration::from_millis(50)).await;
            // Type the URL
            let root = desktop.root();
            root.type_text(url, false)
                .map_err(|e| format!("Type URL failed: {e}"))?;
            tokio::time::sleep(Duration::from_millis(100)).await;
            // Press Enter to navigate
            desktop
                .press_key("{Enter}")
                .await
                .map_err(|e| format!("Press Enter to navigate failed: {e}"))?;
            // Wait for navigation to start
            tokio::time::sleep(Duration::from_millis(1000)).await;
        }
        "search" => {
            let query = get_str("query")
                .or_else(|| get_str("text"))
                .or_else(|| get_str("q"))
                .unwrap_or("");
            info!("[computer_use] search: {}", query);
            if query.is_empty() {
                desktop
                    .press_key("{Enter}")
                    .await
                    .map_err(|e| format!("Press Enter for search failed: {e}"))?;
            } else {
                let encoded_query: String = query
                    .chars()
                    .map(|c| match c {
                        ' ' => "+".to_string(),
                        'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_' | '.' | '~' => c.to_string(),
                        _ => format!("%{:02X}", c as u32),
                    })
                    .collect();
                let search_url = format!("https://www.google.com/search?q={}", encoded_query);
                desktop
                    .open_url(&search_url, None)
                    .map_err(|e| format!("Open search URL failed: {e}"))?;
            }
        }
        _ => {
            warn!("[computer_use] Unknown action: {}", action);
            return Err(format!("Unknown action: {action}"));
        }
    }

    Ok(())
}

// ===== Main Implementation =====

impl Desktop {
    /// Run Gemini Computer Use agentic loop.
    ///
    /// Provide a goal and target process, and this will autonomously take actions
    /// (click, type, scroll, etc.) until the goal is achieved or max_steps is reached.
    ///
    /// # Arguments
    /// * `process` - Process name of the target application (e.g., "chrome", "notepad")
    /// * `goal` - What to achieve (e.g., "Open Notepad and type Hello World")
    /// * `max_steps` - Maximum number of steps before stopping (default: 20)
    /// * `on_step` - Optional callback for progress updates
    ///
    /// # Returns
    /// * `Ok(ComputerUseResult)` - Result with status, steps executed, and history
    /// * `Err(e)` - If the operation fails
    ///
    /// # Example
    /// ```ignore
    /// let desktop = Desktop::new(false, true)?;
    /// let result = desktop.gemini_computer_use(
    ///     "notepad",
    ///     "Type 'Hello World' and save the file",
    ///     Some(10),
    ///     Some(Box::new(|step| println!("Step {}: {}", step.step, step.action))),
    /// ).await?;
    /// println!("Status: {}", result.status);
    /// ```
    pub async fn gemini_computer_use(
        &self,
        process: &str,
        goal: &str,
        max_steps: Option<u32>,
        on_step: Option<ProgressCallback>,
    ) -> Result<ComputerUseResult> {
        let max_steps = max_steps.unwrap_or(20);
        let mut previous_actions: Vec<ComputerUsePreviousAction> = Vec::new();
        let mut steps: Vec<ComputerUseStep> = Vec::new();
        let mut final_status = "max_steps_reached";
        let mut final_action = String::new();
        let mut final_text: Option<String> = None;
        let mut pending_confirmation: Option<serde_json::Value> = None;

        // Setup executions directory for screenshots (flat structure)
        let execution_id = generate_execution_id(process);
        let executions_dir = match get_executions_dir() {
            Ok(dir) => Some(dir),
            Err(e) => {
                warn!("[computer_use] Failed to get executions dir: {}", e);
                None
            }
        };

        info!(
            "[computer_use] Starting agentic loop for goal: {} (max_steps: {}, execution_id: {})",
            goal, max_steps, execution_id
        );

        for step_num in 1..=max_steps {
            // Check for cancellation at start of each iteration
            if self.is_cancelled() {
                info!("[computer_use] Cancelled by stop_execution");
                final_status = "cancelled";
                break;
            }

            info!("[computer_use] Step {}/{}", step_num, max_steps);

            // 1. Capture screenshot of target window
            let capture_data = match capture_window_for_computer_use(self, process) {
                Ok(data) => data,
                Err(e) => {
                    warn!("[computer_use] Failed to capture screenshot: {}", e);
                    final_status = "failed";
                    break;
                }
            };

            // 1b. Save initial screenshot only (before any action) - async, non-blocking
            if step_num == 1 {
                if let Some(ref dir) = executions_dir {
                    let screenshot_path = dir.join(format!("{}_000_initial.png", execution_id));
                    save_screenshot_async(capture_data.base64_image.clone(), screenshot_path);
                }
            }

            // 2. Call backend to get next action
            let response = match call_computer_use_backend(
                &capture_data.base64_image,
                goal,
                if previous_actions.is_empty() {
                    None
                } else {
                    Some(&previous_actions)
                },
            )
            .await
            {
                Ok(r) => r,
                Err(e) => {
                    warn!("[computer_use] Backend error: {}", e);
                    final_status = "failed";
                    break;
                }
            };

            // Store text response
            if response.text.is_some() {
                final_text = response.text.clone();
            }

            // 3. Check for task completion
            if response.completed {
                final_status = "success";
                final_action = "completed".to_string();
                info!("[computer_use] Task completed. Text: {:?}", response.text);
                break;
            }

            // 4. Get function call
            let function_call = match response.function_call {
                Some(fc) => fc,
                None => {
                    final_status = "success";
                    final_action = "no_action".to_string();
                    break;
                }
            };

            final_action = function_call.name.clone();
            info!(
                "[computer_use] Action: {} (text: {:?})",
                function_call.name, response.text
            );

            // 5. Check for safety confirmation
            if response.safety_decision.as_deref() == Some("require_confirmation") {
                final_status = "needs_confirmation";
                pending_confirmation = Some(serde_json::json!({
                    "action": function_call.name,
                    "args": function_call.args,
                    "text": response.text,
                }));
                break;
            }

            // 6. Execute action
            let execute_result = execute_action(
                self,
                process,
                &function_call.name,
                &function_call.args,
                capture_data.window_bounds,
                capture_data.dpi_scale,
                capture_data.resize_scale,
            )
            .await;

            // 7. Record action result
            let (success, error_msg) = match &execute_result {
                Ok(_) => (true, None),
                Err(e) => (false, Some(e.to_string())),
            };

            let step = ComputerUseStep {
                step: step_num,
                action: function_call.name.clone(),
                args: function_call.args.clone(),
                success,
                error: error_msg.clone(),
                text: response.text.clone(),
            };

            // Call progress callback if provided
            if let Some(ref callback) = on_step {
                callback(&step);
            }

            steps.push(step);

            // 8. Wait for UI to settle before capturing post-action screenshot
            // This is critical for actions that cause page navigation (e.g., press Enter on search)
            // Use select! to allow cancellation during the wait
            let ct = self.cancellation_token();
            tokio::select! {
                _ = tokio::time::sleep(Duration::from_millis(1000)) => {},
                _ = ct.cancelled() => {
                    info!("[computer_use] Cancelled during wait by stop_execution");
                    final_status = "cancelled";
                    break;
                }
            }

            // 9. Capture new screenshot after action for next iteration
            let (post_action_screenshot, post_action_url) = match capture_window_for_computer_use(
                self, process,
            ) {
                Ok(data) => (data.base64_image, data.browser_url),
                Err(e) => {
                    warn!("[computer_use] Failed to capture post-action screenshot: {}. Skipping previous_actions update.", e);
                    // Don't fallback to pre-action screenshot - that would confuse Gemini
                    // Just continue to next iteration without adding to previous_actions
                    continue;
                }
            };

            // 9b. Save post-action screenshot (result of this step's action) - async, non-blocking
            if let Some(ref dir) = executions_dir {
                let screenshot_path =
                    dir.join(format!("{}_{:03}_after.png", execution_id, step_num));
                save_screenshot_async(post_action_screenshot.clone(), screenshot_path);
            }

            previous_actions.push(ComputerUsePreviousAction {
                name: function_call.name,
                response: ComputerUseActionResponse {
                    success,
                    error: error_msg,
                },
                screenshot: post_action_screenshot,
                url: post_action_url,
            });

            // 10. Limit previous_actions to last 3 to avoid payload too large errors
            if previous_actions.len() > 3 {
                previous_actions.remove(0);
            }
        }

        info!(
            "[computer_use] Completed with status: {} ({} steps)",
            final_status,
            steps.len()
        );

        let result = ComputerUseResult {
            status: final_status.to_string(),
            goal: goal.to_string(),
            steps_executed: steps.len() as u32,
            final_action,
            final_text,
            steps,
            pending_confirmation,
            execution_id: Some(execution_id.clone()),
        };

        // Save execution result as JSON (flat structure)
        if let Some(ref dir) = executions_dir {
            if let Err(e) = save_execution_result(&result, dir, &execution_id) {
                warn!("[computer_use] Failed to save execution result: {}", e);
            }
        }

        Ok(result)
    }
}