Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

5.9 KB
Raw Blame
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

import AppKit
import WebKit

/// `CUA_SPACES_WEBUI_CAPTURE=<dir>` (debug hooks only): after the first load, opens
/// every route in light and dark, checks which bridge the page is on
/// (`<html data-bridge>`), saves the web view's own snapshot of each to
/// `<dir>/<route>-<theme>.png` with a `report.json`, then quits. Window
/// captures from outside the app miss WKWebView's content (it draws in
/// another process), so the screenshots in docs/webui come from here. With
/// `CUA_SPACES_OFFSCREEN=1` too, nothing shows on screen (`OffscreenRun`).
extension WebUIWindowController {
    static let captureRoutes = ["onboarding", "spaces", "machines", "agents", "keyvault", "notifications", "settings",
                                 "settings/about"]

    /// The base page routes resolve under: the window may have opened at
    /// another route (the first run, at `/onboarding`).
    var captureBase: URL { Self.devURL ?? WebUISchemeHandler.startURL }

    static var captureDirectory: URL? {
        guard DevHooks.enabled, let dir = DevHooks.environment["CUA_SPACES_WEBUI_CAPTURE"], !dir.isEmpty else { return nil }
        return URL(fileURLWithPath: dir, isDirectory: true)
    }

    func captureRoutes(into dir: URL) async {
        try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
        var report: [[String: Any]] = []
        for theme in ["light", "dark"] {
            window?.appearance = NSAppearance(named: theme == "dark" ? .darkAqua : .aqua)
            _ = try? await webView.evaluateJavaScript("localStorage.setItem('cua-spaces:theme', '\(theme)')")
            for route in Self.captureRoutes {
                var page: [String: Any] = [:]
                // The first navigation can interrupt the initial load: try again.
                for _ in 0..<3 where !(page["bridge"] is String) {
                    webView.load(URLRequest(url: Self.url(route, in: captureBase)))
                    page = await settledPage()
                }
                // `CUA_SPACES_WEBUI_CAPTURE_SELECT=<machine id>`: the Machines page with that machine chosen.
                if route == "machines", let id = DevHooks.environment["CUA_SPACES_WEBUI_CAPTURE_SELECT"] {
                    try? await Task.sleep(for: .seconds(6))
                    _ = try? await bridge.handle("machines.checkHealth", [:])
                    try? await Task.sleep(for: .seconds(4))
                    _ = try? await webView.evaluateJavaScript("document.querySelector('[data-machine-id=\"\(id)\"]')?.click()")
                    try? await Task.sleep(for: .milliseconds(800))
                }
                let file = dir.appendingPathComponent("\(route.replacingOccurrences(of: "/", with: "-"))-\(theme).png")
                if let image = try? await webView.takeSnapshot(configuration: nil),
                   let tiff = image.tiffRepresentation,
                   let png = NSBitmapImageRep(data: tiff)?.representation(using: .png, properties: [:]) {
                    try? png.write(to: file)
                }
                report.append(["route": route, "theme": theme, "file": file.lastPathComponent].merging(page) { a, _ in a })
            }
        }
        // What the host answered, next to what the page drew.
        let spaces = (try? await bridge.handle("spaces.list", [:])) ?? NSNull()
        // How the routes beyond Spaces answered: ok (with a count), or the error code.
        var answers: [String: Any] = [:]
        for method in ["agents.list", "agents.setup", "host.status", "machines.list", "settings.get", "about.get",
                       "loginItem.get", "devices.get", "notifications.list", "volume.overview", "storage.get"] {
            do {
                let result = try await bridge.handle(method, [:])
                switch method {
                case "machines.list": answers[method] = ["ok": true, "host": (result as? [String: Any])?["host"] is [String: Any]]
                case "settings.get": answers[method] = ["ok": true, "updateChannel": (result as? [String: Any])?["updateChannel"] ?? NSNull()]
                default: answers[method] = ["ok": true, "count": (result as? [Any])?.count ?? (result is NSNull ? 0 : 1)]
                }
            } catch let f as WebUIBridge.Failure {
                answers[method] = ["ok": false, "code": f.code]
            } catch {
                answers[method] = ["ok": false, "code": "failed"]
            }
        }
        let output: [String: Any] = ["pages": report, "spaces.list": spaces, "answers": answers]
        if let data = try? JSONSerialization.data(withJSONObject: output, options: [.prettyPrinted, .sortedKeys]) {
            try? data.write(to: dir.appendingPathComponent("report.json"))
        }
        NSApp.terminate(nil)
    }

    /// Waits until the page names its bridge and its route has content.
    private func settledPage() async -> [String: Any] {
        let probe = """
        JSON.stringify({ bridge: document.documentElement.dataset.bridge || null,
          dark: document.documentElement.classList.contains('dark'),
          heading: document.querySelector('main h1')?.textContent || null,
          text: (document.querySelector('main')?.innerText || '').trim().length })
        """
        var last: [String: Any] = [:]
        for _ in 0..<60 {
            try? await Task.sleep(for: .milliseconds(150))
            guard let json = try? await webView.evaluateJavaScript(probe) as? String,
                  let page = try? JSONSerialization.jsonObject(with: Data(json.utf8)) as? [String: Any] else { continue }
            last = page
            if page["bridge"] is String, (page["text"] as? Int ?? 0) > 0 { break }
        }
        try? await Task.sleep(for: .milliseconds(700))
        // Where it landed: with the first run due, every page goes to /onboarding.
        if let path = try? await webView.evaluateJavaScript("location.pathname") as? String { last["path"] = path }
        return last
    }
}