Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
feat(lume): add automatic clipboard sync from host to VM (#951)
Francesco Bonacci8612e62600ac
diff --git a/docs/content/docs/lume/guide/getting-started/quickstart.mdx b/docs/content/docs/lume/guide/getting-started/quickstart.mdxindex d4160dae83..19f5b5cdaa 100644--- a/docs/content/docs/lume/guide/getting-started/quickstart.mdx+++ b/docs/content/docs/lume/guide/getting-started/quickstart.mdx@@ -97,6 +97,10 @@ lume run my-vmA VNC window opens. For macOS VMs, you'll see the Setup Assistant—complete it manually or use [Unattended Setup](/lume/guide/fundamentals/unattended-setup) to automate it.+<Callout type="info">+**Automatic clipboard sync:** When Remote Login (SSH) is enabled on your VM, Lume automatically syncs clipboard between your Mac and the VM in both directions. Copy on your Mac, paste in the VM—or copy in the VM, paste on your Mac. VMs created with `--unattended` have SSH enabled by default.+</Callout>+## Create a VM with custom resourcesBy default, Lume creates VMs with 4 CPU cores, 8GB memory, and a 50GB disk. Override any of these:diff --git a/libs/lume/src/Clipboard/ClipboardWatcher.swift b/libs/lume/src/Clipboard/ClipboardWatcher.swiftnew file mode 100644index 0000000000..7974800bed--- /dev/null+++ b/libs/lume/src/Clipboard/ClipboardWatcher.swift@@ -0,0 +1,180 @@+import AppKit+import Foundation++/// Watches clipboard and syncs bidirectionally between host and VM via SSH.+/// Requires SSH/Remote Login to be enabled on the VM.+public actor ClipboardWatcher {+ private let vmName: String+ private let storage: String?+ private var watchTask: Task<Void, Never>?+ private var isRunning = false++ // Track last synced content to avoid sync loops+ private var lastHostContent: String = ""+ private var lastVMContent: String = ""+ private var lastHostChangeCount: Int = 0++ /// Polling interval for clipboard changes+ private static let pollInterval: TimeInterval = 1.0++ /// Delay before starting to watch (allows VM to boot and get IP)+ private static let startupDelay: TimeInterval = 10.0++ /// Max content size (1MB)+ private static let maxContentSize = 1_000_000++ public init(vmName: String, storage: String?) {+ self.vmName = vmName+ self.storage = storage+ }++ /// Start watching clipboard for bidirectional sync+ public func start() {+ guard !isRunning else { return }+ isRunning = true++ watchTask = Task { [weak self] in+ guard let self = self else { return }++ // Wait for VM to boot and potentially get an IP address+ try? await Task.sleep(nanoseconds: UInt64(Self.startupDelay * 1_000_000_000))++ // Initialize with current clipboard state to avoid syncing on start+ await self.initializeState()++ Logger.info("Clipboard sync started", metadata: ["vm": self.vmName])++ while !Task.isCancelled {+ await self.syncBidirectional()+ try? await Task.sleep(nanoseconds: UInt64(Self.pollInterval * 1_000_000_000))+ }++ Logger.info("Clipboard sync stopped", metadata: ["vm": self.vmName])+ }+ }++ /// Stop watching the clipboard+ public func stop() {+ watchTask?.cancel()+ watchTask = nil+ isRunning = false+ }++ private func initializeState() {+ lastHostChangeCount = NSPasteboard.general.changeCount+ if let content = NSPasteboard.general.string(forType: .string) {+ lastHostContent = content+ lastVMContent = content // Assume VM starts with same content+ }+ }++ private func syncBidirectional() async {+ // Check host clipboard for changes+ let hostChangeCount = NSPasteboard.general.changeCount+ if hostChangeCount != lastHostChangeCount {+ lastHostChangeCount = hostChangeCount+ if let content = NSPasteboard.general.string(forType: .string),+ !content.isEmpty,+ content != lastHostContent,+ content != lastVMContent, // Avoid sync loop+ content.utf8.count <= Self.maxContentSize {+ lastHostContent = content+ await syncToVM(content: content)+ }+ }++ // Check VM clipboard for changes+ await syncFromVM()+ }++ private func syncToVM(content: String) async {+ guard let sshClient = await getSSHClient() else { return }++ do {+ guard let data = content.data(using: .utf8) else { return }+ let base64Content = data.base64EncodedString()++ // Use printf to avoid issues with special characters in echo+ let command = "printf '%s' '\(base64Content)' | base64 -D | pbcopy"+ let result = try await sshClient.execute(command: command, timeout: 5)++ if result.exitCode == 0 {+ Logger.debug("Clipboard synced to VM", metadata: [+ "vm": vmName,+ "size": "\(content.utf8.count)"+ ])+ }+ } catch {+ Logger.debug("Failed to sync clipboard to VM", metadata: [+ "vm": vmName,+ "error": error.localizedDescription+ ])+ }+ }++ private func syncFromVM() async {+ guard let sshClient = await getSSHClient() else { return }++ do {+ // Get VM clipboard content+ let result = try await sshClient.execute(command: "pbpaste | base64", timeout: 5)++ guard result.exitCode == 0 else { return }++ let trimmedOutput = result.output.trimmingCharacters(in: .whitespacesAndNewlines)+ guard !trimmedOutput.isEmpty else { return }++ guard let data = Data(base64Encoded: trimmedOutput),+ let content = String(data: data, encoding: .utf8),+ !content.isEmpty,+ content != lastVMContent,+ content != lastHostContent, // Avoid sync loop+ content.utf8.count <= Self.maxContentSize else {+ return+ }++ // Update tracking+ lastVMContent = content++ // Write to host clipboard+ NSPasteboard.general.clearContents()+ NSPasteboard.general.setString(content, forType: .string)+ lastHostChangeCount = NSPasteboard.general.changeCount+ lastHostContent = content++ Logger.debug("Clipboard synced from VM", metadata: [+ "vm": vmName,+ "size": "\(content.utf8.count)"+ ])+ } catch {+ Logger.debug("Failed to sync clipboard from VM", metadata: [+ "vm": vmName,+ "error": error.localizedDescription+ ])+ }+ }++ private func getSSHClient() async -> SSHClient? {+ do {+ let vmDetails = try await MainActor.run {+ let controller = LumeController()+ return try controller.getDetails(name: vmName, storage: storage)+ }++ guard vmDetails.status == "running",+ let ipAddress = vmDetails.ipAddress, !ipAddress.isEmpty,+ vmDetails.sshAvailable == true else {+ return nil+ }++ return SSHClient(+ host: ipAddress,+ port: 22,+ user: "lume",+ password: "lume"+ )+ } catch {+ return nil+ }+ }+}diff --git a/libs/lume/src/VM/VM.swift b/libs/lume/src/VM/VM.swiftindex 9d8b694c39..dbd6f1519f 100644--- a/libs/lume/src/VM/VM.swift+++ b/libs/lume/src/VM/VM.swift@@ -40,6 +40,7 @@ class VM {@MainActorprivate var virtualizationService: VMVirtualizationService?internal let vncService: VNCService+ private var clipboardWatcher: ClipboardWatcher?internal let virtualizationServiceFactory:(VMVirtualizationServiceContext) throws -> VMVirtualizationServiceprivate let vncServiceFactory: (VMDirectory) -> VNCService@@ -255,6 +256,11 @@ class VM {try await service.start()Logger.info("VM started successfully", metadata: ["name": vmDirContext.name])+ // Start clipboard watcher for automatic host-to-VM clipboard sync+ // Requires SSH/Remote Login to be enabled on the VM+ clipboardWatcher = ClipboardWatcher(vmName: vmDirContext.name, storage: vmDirContext.storage)+ await clipboardWatcher?.start()+while true {try await Task.sleep(nanoseconds: UInt64(1e9))}@@ -266,6 +272,8 @@ class VM {"error": error.localizedDescription,"errorType": "\(type(of: error))",])+ await clipboardWatcher?.stop()+ clipboardWatcher = nilvirtualizationService = nilvncService.stop()@@ -298,6 +306,8 @@ class VM {Logger.info("Stopping VM via virtualization service", metadata: ["name": vmDirContext.name])try await service.stop()+ await clipboardWatcher?.stop()+ clipboardWatcher = nilvirtualizationService = nilvncService.stop()Logger.info(