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
Expose VNC port
f-trycua065f4181dd8e
diff --git a/README.md b/README.mdindex 1bdea635df..54420a1f46 100644--- a/README.md+++ b/README.md@@ -68,6 +68,9 @@ Command Options:--no-display Do not start the VNC client app--shared-dir <dir> Share directory with VM (format: path[:ro|rw])--mount <path> For Linux VMs only, attach a read-only disk image+ --registry <url> Container registry URL (default: ghcr.io)+ --organization <org> Organization to pull from (default: trycua)+ --vnc-port <port> Port to use for the VNC server (default: 0 for auto-assign)set:--cpu <cores> New number of CPU cores (e.g., 4)diff --git a/src/Commands/Run.swift b/src/Commands/Run.swiftindex f8faaf4a84..a8c9f3ae74 100644--- a/src/Commands/Run.swift+++ b/src/Commands/Run.swift@@ -25,6 +25,9 @@ struct Run: AsyncParsableCommand {@Option(help: "Organization to pull the images from. Defaults to trycua")var organization: String = "trycua"+ @Option(name: [.customLong("vnc-port")], help: "Port to use for the VNC server. Defaults to 0 (auto-assign)")+ var vncPort: Int = 0+private var parsedSharedDirectories: [SharedDirectory] {get throws {try sharedDirectories.map { dirString -> SharedDirectory in@@ -75,7 +78,8 @@ struct Run: AsyncParsableCommand {sharedDirectories: dirs,mount: mount,registry: registry,- organization: organization+ organization: organization,+ vncPort: vncPort)}}\ No newline at end of filediff --git a/src/Errors/Errors.swift b/src/Errors/Errors.swiftindex 15f779543f..d52a8e47bd 100644--- a/src/Errors/Errors.swift+++ b/src/Errors/Errors.swift@@ -125,6 +125,7 @@ enum VMError: Error, LocalizedError {case stopTimeout(String)case resizeTooSmall(current: UInt64, requested: UInt64)case vncNotConfigured+ case vncPortBindingFailed(requested: Int, actual: Int)case internalError(String)case unsupportedOS(String)case invalidDisplayResolution(String)@@ -148,6 +149,11 @@ enum VMError: Error, LocalizedError {return "Cannot resize disk to \(requested) bytes, current size is \(current) bytes"case .vncNotConfigured:return "VNC is not configured for this virtual machine"+ case .vncPortBindingFailed(let requested, let actual):+ if actual == -1 {+ return "Could not bind to VNC port \(requested) (port already in use). Try a different port or use port 0 for auto-assign."+ }+ return "Could not bind to VNC port \(requested) (port already in use). System assigned port \(actual) instead. Try a different port or use port 0 for auto-assign."case .internalError(let message):return "Internal error: \(message)"case .unsupportedOS(let os):diff --git a/src/FileSystem/VMConfig.swift b/src/FileSystem/VMConfig.swiftindex 98d03a33c2..7a7144d002 100644--- a/src/FileSystem/VMConfig.swift+++ b/src/FileSystem/VMConfig.swift@@ -25,7 +25,6 @@ struct VMConfig: Codable {private var _display: VMDisplayResolutionprivate var _hardwareModel: Data?private var _machineIdentifier: Data?- private var _vncPort: Int?// MARK: - Initializationinit(@@ -36,8 +35,7 @@ struct VMConfig: Codable {macAddress: String? = nil,display: String,hardwareModel: Data? = nil,- machineIdentifier: Data? = nil,- vncPort: Int? = nil+ machineIdentifier: Data? = nil) throws {self.os = osself._cpuCount = cpuCount@@ -47,7 +45,6 @@ struct VMConfig: Codable {self._display = VMDisplayResolution(string: display) ?? VMDisplayResolution(string: "1024x768")!self._hardwareModel = hardwareModelself._machineIdentifier = machineIdentifier- self._vncPort = vncPort}var display: VMDisplayResolution {@@ -84,11 +81,6 @@ struct VMConfig: Codable {get { _macAddress }set { _macAddress = newValue }}-- var vncPort: Int? {- get { _vncPort }- set { _vncPort = newValue }- }mutating func setCpuCount(_ count: Int) {_cpuCount = count@@ -118,10 +110,6 @@ struct VMConfig: Codable {self._display = newDisplay}- mutating func setVNCPort(_ port: Int) {- _vncPort = port- }-// MARK: - Codableenum CodingKeys: String, CodingKey {case _cpuCount = "cpuCount"@@ -132,7 +120,6 @@ struct VMConfig: Codable {case _hardwareModel = "hardwareModel"case _machineIdentifier = "machineIdentifier"case os- case _vncPort = "vncPort"}init(from decoder: Decoder) throws {@@ -146,7 +133,6 @@ struct VMConfig: Codable {_display = VMDisplayResolution(string: try container.decode(String.self, forKey: .display))!_hardwareModel = try container.decodeIfPresent(Data.self, forKey: ._hardwareModel)_machineIdentifier = try container.decodeIfPresent(Data.self, forKey: ._machineIdentifier)- _vncPort = try container.decodeIfPresent(Int.self, forKey: ._vncPort)}func encode(to encoder: Encoder) throws {@@ -160,6 +146,5 @@ struct VMConfig: Codable {try container.encode(display.string, forKey: .display)try container.encodeIfPresent(_hardwareModel, forKey: ._hardwareModel)try container.encodeIfPresent(_machineIdentifier, forKey: ._machineIdentifier)- try container.encodeIfPresent(_vncPort, forKey: ._vncPort)}}diff --git a/src/LumeController.swift b/src/LumeController.swiftindex 74d22859dc..b4b0410674 100644--- a/src/LumeController.swift+++ b/src/LumeController.swift@@ -255,7 +255,8 @@ final class LumeController {sharedDirectories: [SharedDirectory] = [],mount: Path? = nil,registry: String = "ghcr.io",- organization: String = "trycua"+ organization: String = "trycua",+ vncPort: Int = 0) async throws {let normalizedName = normalizeVMName(name: name)Logger.info(@@ -265,6 +266,7 @@ final class LumeController {"no_display": "\(noDisplay)","shared_directories": "\(sharedDirectories.map( { $0.string } ).joined(separator: ", "))","mount": mount?.path ?? "none",+ "vnc_port": "\(vncPort)",])do {@@ -284,7 +286,7 @@ final class LumeController {let vm = try get(name: normalizedName)SharedVM.shared.setVM(name: normalizedName, vm: vm)- try await vm.run(noDisplay: noDisplay, sharedDirectories: sharedDirectories, mount: mount)+ try await vm.run(noDisplay: noDisplay, sharedDirectories: sharedDirectories, mount: mount, vncPort: vncPort)Logger.info("VM started successfully", metadata: ["name": normalizedName])} catch {SharedVM.shared.removeVM(name: normalizedName)diff --git a/src/VM/VM.swift b/src/VM/VM.swiftindex 8e73ed3c90..8f79cead47 100644--- a/src/VM/VM.swift+++ b/src/VM/VM.swift@@ -88,7 +88,7 @@ class VM {// MARK: - VM Lifecycle Management- func run(noDisplay: Bool, sharedDirectories: [SharedDirectory], mount: Path?) async throws {+ func run(noDisplay: Bool, sharedDirectories: [SharedDirectory], mount: Path?, vncPort: Int = 0) async throws {guard vmDirContext.initialized else {throw VMError.notInitialized(vmDirContext.name)}@@ -111,7 +111,8 @@ class VM {"diskSize": "\(vmDirContext.config.diskSize ?? 0)","sharedDirectories": sharedDirectories.map({ $0.string }- ).joined(separator: ", ")+ ).joined(separator: ", "),+ "vncPort": "\(vncPort)"])// Create and configure the VM@@ -125,7 +126,7 @@ class VM {)virtualizationService = try virtualizationServiceFactory(config)- let vncInfo = try await setupVNC(noDisplay: noDisplay)+ let vncInfo = try await setupVNC(noDisplay: noDisplay, port: vncPort)Logger.info("VNC info", metadata: ["vncInfo": vncInfo])// Start the VM@@ -337,13 +338,11 @@ class VM {return vncService.url}- private func setupVNC(noDisplay: Bool) async throws -> String {+ private func setupVNC(noDisplay: Bool, port: Int = 0) async throws -> String {guard let service = virtualizationService else {throw VMError.internalError("Virtualization service not initialized")}- // Use configured port or default to 0 (auto-assign)- let port = vmDirContext.config.vncPort ?? 0try await vncService.start(port: port, virtualMachine: service.getVirtualMachine())guard let url = vncService.url else {@@ -401,13 +400,4 @@ class VM {func finalize(to name: String, home: Home) throws {try vmDirContext.finalize(to: name)}-- // Add method to set VNC port- func setVNCPort(_ port: Int) throws {- guard !isRunning else {- throw VMError.alreadyRunning(vmDirContext.name)- }- vmDirContext.config.setVNCPort(port)- try vmDirContext.saveConfig()- }}\ No newline at end of filediff --git a/src/VNC/PassphraseGenerator.swift b/src/VNC/PassphraseGenerator.swiftindex 9f54ad367d..089e97d3a1 100644--- a/src/VNC/PassphraseGenerator.swift+++ b/src/VNC/PassphraseGenerator.swift@@ -1,4 +1,5 @@import Foundation+import CryptoKitfinal class PassphraseGenerator {private let words: [String]@@ -9,11 +10,39 @@ final class PassphraseGenerator {func prefix(_ count: Int) -> [String] {guard count > 0 else { return [] }- return (0..<count).map { _ in words.randomElement() ?? words[0] }++ // Use secure random number generation+ var result: [String] = []+ for _ in 0..<count {+ let randomBytes = (0..<4).map { _ in UInt8.random(in: 0...255) }+ let randomNumber = Data(randomBytes).withUnsafeBytes { bytes in+ bytes.load(as: UInt32.self)+ }+ let index = Int(randomNumber % UInt32(words.count))+ result.append(words[index])+ }+ return result}+ // A much larger set of common, easy-to-type wordsprivate static let defaultWords = [- "apple", "banana", "cherry", "date",- "elder", "fig", "grape", "honey"+ "alpha", "bravo", "charlie", "delta", "echo", "foxtrot", "golf", "hotel",+ "india", "juliet", "kilo", "lima", "mike", "november", "oscar", "papa",+ "quebec", "romeo", "sierra", "tango", "uniform", "victor", "whiskey", "xray",+ "yankee", "zulu", "zero", "one", "two", "three", "four", "five",+ "six", "seven", "eight", "nine", "apple", "banana", "cherry", "date",+ "elder", "fig", "grape", "honey", "iris", "jade", "kiwi", "lemon",+ "mango", "nectarine", "orange", "peach", "quince", "raspberry", "strawberry", "tangerine",+ "red", "blue", "green", "yellow", "purple", "orange", "pink", "brown",+ "black", "white", "gray", "silver", "gold", "copper", "bronze", "steel",+ "north", "south", "east", "west", "spring", "summer", "autumn", "winter",+ "river", "ocean", "mountain", "valley", "forest", "desert", "island", "beach",+ "sun", "moon", "star", "cloud", "rain", "snow", "wind", "storm",+ "happy", "brave", "calm", "swift", "wise", "kind", "bold", "free",+ "safe", "strong", "bright", "clear", "light", "soft", "warm", "cool",+ "eagle", "falcon", "hawk", "owl", "robin", "sparrow", "swan", "dove",+ "tiger", "lion", "bear", "wolf", "deer", "horse", "dolphin", "whale",+ "maple", "oak", "pine", "birch", "cedar", "fir", "palm", "willow",+ "rose", "lily", "daisy", "tulip", "lotus", "orchid", "violet", "jasmine"]}\ No newline at end of filediff --git a/src/VNC/VNCService.swift b/src/VNC/VNCService.swiftindex 8ec3b2b85a..971ab0110b 100644--- a/src/VNC/VNCService.swift+++ b/src/VNC/VNCService.swift@@ -42,25 +42,45 @@ final class DefaultVNCService: VNCService {vncServer = server- // Wait for port to be assigned if using port 0 (auto-assign)- while port == 0 {- if let assignedPort: UInt16 = server.port.asUInt16, assignedPort != 0 {- // Get the local IP address for the URL - prefer IPv4- let hostIP = try getLocalIPAddress() ?? "127.0.0.1"- let url = "vnc://:\(password)@127.0.0.1:\(assignedPort)" // Use localhost for local connections- let externalUrl = "vnc://:\(password)@\(hostIP):\(assignedPort)" // External URL for remote connections-- Logger.info("VNC server started", metadata: [- "local": url,- "external": externalUrl- ])-- // Save session information with local URL for the client- let session = VNCSession(url: url)- try vmDirectory.saveSession(session)- break+ // Wait for port to be assigned (both for auto-assign and specific port)+ var attempts = 0+ let maxAttempts = 20 // 1 second total wait time+ while true {+ if let assignedPort: UInt16 = server.port.asUInt16 {+ // If we got a non-zero port, check if it matches our request+ if assignedPort != 0 {+ // For specific port requests, verify we got the requested port+ if port != 0 && Int(assignedPort) != port {+ throw VMError.vncPortBindingFailed(requested: port, actual: Int(assignedPort))+ }++ // Get the local IP address for the URL - prefer IPv4+ let hostIP = try getLocalIPAddress() ?? "127.0.0.1"+ let url = "vnc://:\(password)@127.0.0.1:\(assignedPort)" // Use localhost for local connections+ let externalUrl = "vnc://:\(password)@\(hostIP):\(assignedPort)" // External URL for remote connections++ Logger.info("VNC server started", metadata: [+ "local": url,+ "external": externalUrl+ ])++ // Save session information with local URL for the client+ let session = VNCSession(url: url)+ try vmDirectory.saveSession(session)+ break+ }+ }++ attempts += 1+ if attempts >= maxAttempts {+ // If we've timed out and we requested a specific port, it likely means binding failed+ vncServer = nil+ if port != 0 {+ throw VMError.vncPortBindingFailed(requested: port, actual: -1)+ }+ throw VMError.internalError("Timeout waiting for VNC server to start")}- try await Task.sleep(nanoseconds: 50_000_000)+ try await Task.sleep(nanoseconds: 50_000_000) // 50ms delay between checks}}diff --git a/tests/Mocks/MockVM.swift b/tests/Mocks/MockVM.swiftindex cad394d20b..3ff2d0e77b 100644--- a/tests/Mocks/MockVM.swift+++ b/tests/Mocks/MockVM.swift@@ -18,9 +18,9 @@ class MockVM: VM {try vmDirContext.saveConfig()}- override func run(noDisplay: Bool, sharedDirectories: [SharedDirectory], mount: Path?) async throws {+ override func run(noDisplay: Bool, sharedDirectories: [SharedDirectory], mount: Path?, vncPort: Int = 0) async throws {mockIsRunning = true- try await super.run(noDisplay: noDisplay, sharedDirectories: sharedDirectories, mount: mount)+ try await super.run(noDisplay: noDisplay, sharedDirectories: sharedDirectories, mount: mount, vncPort: vncPort)}override func stop() async throws {diff --git a/tests/VMTests.swift b/tests/VMTests.swiftindex d197806e5a..50961a9a0e 100644--- a/tests/VMTests.swift+++ b/tests/VMTests.swift@@ -95,7 +95,7 @@ func testVMRunAndStop() async throws {// Test running VMlet runTask = Task {- try await vm.run(noDisplay: false, sharedDirectories: [], mount: nil)+ try await vm.run(noDisplay: false, sharedDirectories: [], mount: nil, vncPort: 0)}// Give the VM time to start