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.

25.2 KB
Raw Blame
import Foundation

// Everything the Spaces MCP can do, reachable from this SDK by name.
//
// `SpacesConnection.callTool` stays — two judges asked for it, and `cua-driver`
// ships the same hatch. But it is the way to reach something *new*, before the
// SDK models it. It is not coverage. The tutorial having to drive teleport
// through `callTool` was the signal that this file was missing.
//
// The server's dispatch table (`spaces_mcp.py`, `def main()`) advertises 28
// tools. Every one of them has a typed call here or elsewhere in this package;
// the table is in the README.

// MARK: - The Space's own MCP services

/// An MCP service running **inside** a Space.
///
/// A Space is not only a desktop: it declares its own services in
/// `~/.cua/agent-mcp.json` — cua-driver's computer-use MCP over rcdp, and the
/// app MCPs (blender, unity, get-skills) over an attached stdio pipe. Driving
/// those is how a Space's applications are used, so it is a first-class part of
/// this surface rather than something to reach past the SDK for.
public struct SpaceService: Sendable, Hashable, Identifiable, ExpressibleByStringLiteral {
    public var id: String { name }
    public let name: String
    public init(_ name: String) { self.name = name }
    public init(stringLiteral value: String) { self.init(value) }

    /// cua-driver's computer-use MCP. The service a Space always has.
    public static let computer = SpaceService("computer-server")
}

/// One tool an in-Space service advertises.
public struct SpaceTool: Sendable, Hashable, Identifiable {
    public var id: String { "\(service.name)/\(name)" }
    public let service: SpaceService
    public let name: String
    public let summary: String
    /// The tool's own input schema, when the listing carried one. A filtered
    /// listing carries schemas; a bare one carries names and one-liners.
    public let inputSchema: JSONValue?

    public init(service: SpaceService, name: String, summary: String,
                inputSchema: JSONValue? = nil) {
        self.service = service
        self.name = name
        self.summary = summary
        self.inputSchema = inputSchema
    }
}

/// What one service advertises, and what else the Space has.
public struct ServiceCatalog: Sendable, Hashable {
    public let service: SpaceService
    public let tools: [SpaceTool]
    /// Every other service this Space exposes. Published even when you asked
    /// about one, because a caller has no way to guess that `blender` is a
    /// thing it could ask for.
    public let otherServices: [SpaceService]
    /// The service's own usage instructions, when it publishes them.
    public let instructions: String?
    /// **A reachable service that advertises zero tools exists; it is not
    /// ready.** Unity lists nothing until an Editor has a project open. The
    /// server says this out loud rather than letting an empty array read as
    /// "no such service", and the SDK carries it rather than flattening it.
    public let notReadyWarning: String?

    public var isEmptyButReachable: Bool { tools.isEmpty && notReadyWarning != nil }
}

/// A content part returned by an in-Space tool. cua-driver answers with an
/// image *and* text, so this is a list of parts and not a string.
public enum ToolContent: Sendable, Hashable {
    case text(String)
    case image(Data, mimeType: String)
    case other(JSONValue)

    /// Every text part joined, for a caller that only wants words.
    public static func text(of parts: [ToolContent]) -> String {
        parts.compactMap { if case let .text(s) = $0 { return s } else { return nil } }
            .joined(separator: "\n")
    }
}

/// The MCP services running inside a Space.
public struct Services: Sendable {
    let space: Space

    /// Every service this Space declares.
    ///
    /// The server publishes the service list on any `list_tools` call, so this
    /// is one round trip and never a guess.
    public func list() async throws -> [SpaceService] {
        let catalog = try await tools()
        return ([catalog.service] + catalog.otherServices).reduced()
    }

    /// What a service can do. `matching:` asks for full input schemas of the
    /// tools whose names contain it; without it you get names and one-liners,
    /// which is the scannable form.
    public func tools(of service: SpaceService? = nil,
                      matching filter: String? = nil) async throws -> ServiceCatalog {
        var args: [String: JSONValue] = ["space": .string(space.id.rawValue)]
        if let service { args["service"] = .string(service.name) }
        if let filter { args["name"] = .string(filter) }
        let d = try await space.connection.object("list_tools", args)
        let answered = SpaceService(d["service"]?.stringValue ?? service?.name ?? "")
        let tools = (d["tools"]?.arrayValue ?? []).compactMap(\.objectValue).map { row in
            SpaceTool(service: answered,
                      name: row["name"]?.stringValue ?? "",
                      summary: row["description"]?.stringValue ?? "",
                      inputSchema: row["inputSchema"])
        }.filter { !$0.name.isEmpty }
        let names: [String] = (d["services"]?.arrayValue ?? []).compactMap(\.stringValue)
        let others: [SpaceService] = names
            .filter { $0 != answered.name }
            .map { SpaceService($0) }
        return ServiceCatalog(service: answered, tools: tools, otherServices: others,
                              instructions: d["instructions"]?.stringValue,
                              notReadyWarning: d["warning"]?.stringValue)
    }

    /// Invoke a tool on an in-Space service.
    ///
    /// This is **not** `SpacesConnection.callTool`, and the difference matters:
    /// that one calls a tool on the Spaces MCP itself, on your machine; this
    /// one calls a tool on a service running inside the Space. An app-backed
    /// MCP starts its application on the first call, so there is no need to
    /// open the app first.
    @discardableResult
    public func call(_ tool: String,
                     on service: SpaceService? = nil,
                     _ arguments: [String: JSONValue] = [:]) async throws -> [ToolContent] {
        var args: [String: JSONValue] = [
            "space": .string(space.id.rawValue),
            "tool": .string(tool),
            "arguments": .object(arguments),
        ]
        if let service { args["service"] = .string(service.name) }
        let raw = try await space.connection.callTool("call_tool", args)
        return ToolContent.parts(from: raw)
    }
}

extension ToolContent {
    static func parts(from raw: JSONValue) -> [ToolContent] {
        guard let rows = raw.arrayValue else {
            return [raw.stringValue.map(ToolContent.text) ?? .other(raw)]
        }
        return rows.map { row in
            guard let object = row.objectValue else {
                return row.stringValue.map(ToolContent.text) ?? .other(row)
            }
            switch object["type"]?.stringValue {
            case "text":
                return .text(object["text"]?.stringValue ?? "")
            case "image":
                let mime = object["mimeType"]?.stringValue ?? "image/png"
                let data = object["data"]?.stringValue.flatMap {
                    Data(base64Encoded: $0)
                } ?? Data()
                return .image(data, mimeType: mime)
            default:
                return .other(row)
            }
        }
    }
}

extension Array where Element == SpaceService {
    func reduced() -> [SpaceService] {
        var seen: Set<String> = []
        return filter { !$0.name.isEmpty && seen.insert($0.name).inserted }
    }
}

// MARK: - Hotspot: the host Mac's network

/// Whether this Mac is sharing its network, and with which Space.
public struct HotspotStatus: Sendable, Hashable {
    public let isSharing: Bool
    public let space: SpaceID?
    /// Whatever the control server said, kept whole. Normalising never
    /// discards the original.
    public let detail: String

    init(_ raw: [String: JSONValue]) {
        // `hotspot_status` answers `{hotspots: [status…]}`, `hotspot_stop`
        // `{stopped: [ids]}`, `hotspot_start` one status with a `state`.
        var d = raw
        if let first = raw["hotspots"]?.arrayValue?.first?.objectValue { d = first }
        let state = d["state"]?.stringValue
        let sharing = d["sharing"]?.boolValue ?? d["active"]?.boolValue
            ?? d["running"]?.boolValue
            ?? state.map { $0 == "active" || $0 == "waiting_for_peer" } ?? false
        let id = d["space_id"]?.stringValue ?? d["space"]?.stringValue
        self.init(isSharing: sharing,
                  space: id.flatMap { $0.isEmpty ? nil : SpaceID($0) },
                  detail: d["status"]?.stringValue ?? d["note"]?.stringValue
                    ?? state ?? "\(raw)")
    }

    public init(isSharing: Bool, space: SpaceID?, detail: String) {
        self.isSharing = isSharing
        self.space = space
        self.detail = detail
    }
}

/// Give a Space this Mac's network, so it browses and egresses as this Mac.
///
/// The product plan calls this out: it is *"frequently the difference between a
/// working session and a fraud challenge page"*. It deserves a type.
///
/// Served by whichever process owns the Spaces runtime: the `cua daemon` (so
/// sharing outlives the app that started it) or this process when embedded.
/// Stopping it is explicit; `sharing(with:)` scopes it.
public struct Hotspot: Sendable {
    let connection: SpacesConnection

    /// Share this Mac's network with a Space.
    ///
    /// - Note: the server resolves the Space id through `parse_space_id`, which
    ///   is cloud-shaped — a `local:` id arrives at the control server
    ///   reinterpreted. The SDK does **not** refuse on that basis: a capability
    ///   asserted from reading code rather than from calling it is exactly the
    ///   mistake that made `teleport` read `false` for Local Spaces. If it
    ///   fails, the error carries what the control server said.
    @discardableResult
    public func start(sharingWith space: SpaceID) async throws -> HotspotStatus {
        HotspotStatus(try await connection.object(
            "hotspot_start", ["space": .string(space.rawValue)]))
    }

    @discardableResult
    public func stop() async throws -> HotspotStatus {
        HotspotStatus(try await connection.object("hotspot_stop", [:]))
    }

    public func status() async throws -> HotspotStatus {
        HotspotStatus(try await connection.object("hotspot_status", [:]))
    }

    /// Scoped: sharing stops on every exit path, including a throw and a
    /// cancellation. A hotspot left on is someone's Mac still routing a
    /// sandbox's traffic.
    @discardableResult
    public func sharing<T: Sendable>(with space: SpaceID,
                                     _ body: () async throws -> T) async throws -> T {
        _ = try await start(sharingWith: space)
        do {
            let value = try await body()
            _ = try? await stop()
            return value
        } catch {
            _ = try? await stop()
            throw error
        }
    }
}

extension SpacesConnection {
    /// This Mac's network sharing. A host capability, not a provider one.
    public var hotspot: Hotspot { Hotspot(connection: self) }
}

// MARK: - The operator's own desktop

/// Show a Space **on the machine running the backend** — the operator's own
/// Mac, not inside your application.
///
/// This is the distinction that costs people an afternoon: `show_space_pip`,
/// `open_space_viewer` and `stream_space_window` all have names that say "show
/// me the Space", and all three draw a window on the operator's desktop. An
/// app embedding a Space in its own UI wants `space.screen` in
/// `CuaSpacesStreaming` instead, and will see nothing at all from these calls.
///
/// So every method here says `onOperatorDesktop`, and none of them returns
/// anything you could render.
public struct OperatorDisplay: Sendable {
    let space: Space

    /// Pin this Space to the operator's desktop as an always-on-top overlay.
    public func pinPictureInPictureOnOperatorDesktop() async throws {
        try await space.present(.pictureInPicture)
    }

    public func unpinPictureInPictureFromOperatorDesktop() async throws {
        try await space.present(.hidePictureInPicture)
    }

    /// Open the full-desktop viewer window on the operator's desktop.
    public func openViewerOnOperatorDesktop() async throws {
        try await space.present(.viewer)
    }

    /// Stream one window back to the operator's desktop.
    public func streamWindowToOperatorDesktop(_ window: WindowID) async throws {
        try await space.present(.window(window))
    }
}

// MARK: - The rest of the tool surface

extension Space {
    /// Show this Space on the **operator's** desktop. Not your app's UI.
    public var operatorDisplay: OperatorDisplay { OperatorDisplay(space: self) }

    /// The MCP services running inside this Space.
    public var services: Services { Services(space: self) }

    /// Write literal text to a file in the Space.
    ///
    /// Not `upload` and not `bash`: there is no local file, and no shell
    /// quoting to get wrong. The server base64s the content, makes the parent
    /// directory, and decodes it in place.
    @discardableResult
    public func write(_ text: String, to path: String) async throws -> SpaceFile {
        let target = guestPath(path)
        _ = try await connection.callTool("space_write", [
            "space": .string(id.rawValue),
            "path": .string(target),
            "content": .string(text),
        ])
        return SpaceFile(path: target, name: (target as NSString).lastPathComponent,
                         byteCount: text.utf8.count, space: self)
    }

    /// Drop a host file or folder into the Space's `~/Downloads[/subdirectory]`
    /// — what dragging onto the Space does. Every file is SHA-256 verified on
    /// both ends; folders honour `.gitignore`-style ignore files unless told
    /// otherwise. Returns the guest paths that were written.
    @discardableResult
    public func sendFile(_ local: URL, intoDownloads subdirectory: String? = nil,
                         respectIgnoreFiles: Bool = true) async throws -> [RemoteFile] {
        guard FileManager.default.fileExists(atPath: local.path) else {
            throw SpacesError.localFileUnavailable(local.path)
        }
        var a: [String: JSONValue] = [
            "space": .string(id.rawValue),
            "path": .string(local.path),
            "respect_ignorefiles": .bool(respectIgnoreFiles),
        ]
        if let subdirectory { a["target_directory"] = .string(subdirectory) }
        let d = try await connection.object("send_file", a)
        return (d["files"]?.arrayValue ?? []).compactMap(\.objectValue).map { f in
            let path = f["path"]?.stringValue ?? ""
            return RemoteFile(path: path, name: (path as NSString).lastPathComponent,
                              byteCount: f["size"]?.intValue)
        }
    }

    /// Signs in to a site in this Space's browser with a password the user
    /// saved in the Cua Keyvault, without the caller ever seeing it.
    ///
    /// The first call files a Keyvault request (`status == .pending` with a
    /// `requestID`); the user approves it in Cua, once per sign-in by
    /// default. Call again with that `requestID`: the Keyvault checks the tab
    /// is on the saved login's exact origin, types the login through the
    /// Space's cua-driver and submits (`status == .filled`). A declined
    /// request throws.
    public func requestSiteLogin(url: String, username: String? = nil, agent: String? = nil,
                                 session: String? = nil, targetID: String? = nil,
                                 tabID: String? = nil, requestID: String? = nil,
                                 waitSeconds: Int? = nil) async throws -> SiteLogin {
        var a: [String: JSONValue] = ["space": .string(id.rawValue), "url": .string(url)]
        if let username { a["username"] = .string(username) }
        if let agent { a["agent"] = .string(agent) }
        if let session { a["session"] = .string(session) }
        if let targetID { a["target_id"] = .string(targetID) }
        if let tabID { a["tab_id"] = .string(tabID) }
        if let requestID { a["request_id"] = .string(requestID) }
        if let waitSeconds { a["wait_secs"] = .number(Double(waitSeconds)) }
        let d = try await connection.object("request_site_login", a)
        return SiteLogin(
            status: d["status"]?.stringValue == "filled" ? .filled : .pending,
            requestID: d["request_id"]?.stringValue,
            site: d["site"]?.stringValue,
            usernameHint: d["username_hint"]?.stringValue,
            submitted: d["submitted"]?.boolValue ?? false,
            pageURL: d["page_url"]?.stringValue,
            targetID: d["target_id"]?.stringValue,
            tabID: d["tab_id"]?.stringValue)
    }

    /// The media endpoint for this Space's in-app frames (the old name).
    ///
    /// Local Spaces only, and that is a capability rather than a surprise:
    /// `capabilities.rcdpStreaming` says so before the call. See
    /// `streamEndpoint(forceRefresh:)`, which this names.
    public func rcdpEndpoint(forceRefresh: Bool = false) async throws -> StreamEndpoint {
        try await streamEndpoint(forceRefresh: forceRefresh)
    }

    /// Delete this Space's sandbox and forget it. Irreversible for a Space
    /// `createSpace` made; a Space added by address is only forgotten.
    @discardableResult
    public func delete() async throws -> String {
        try await connection.deleteSpace(id)
    }

    /// Turn this Space off (suspended or stopped, as its provider can).
    @discardableResult
    public func stop() async throws -> JSONValue {
        try await connection.stopSpace(id)
    }

    /// Turn this Space back on (resumed or booted).
    @discardableResult
    public func start() async throws -> JSONValue {
        try await connection.startSpace(id)
    }
}

extension Agents {
    /// What every agent harness is, and what it can and cannot do.
    ///
    /// Published by the server so a caller can plan around a limitation rather
    /// than discover it by watching a run fail — which is the same reason
    /// `AgentKind.isProductionReady` exists, answered by the backend instead of
    /// by a constant in this package.
    public func harnessCapabilities() async throws -> HarnessCapabilities {
        HarnessCapabilities(try await space.connection.object("agent_capabilities", [:]))
    }
}

/// What the server says about its agent harnesses.
public struct HarnessCapabilities: Sendable, Hashable {
    /// Every status the harness vocabulary contains, from the server rather
    /// than from `AgentState`'s cases.
    public let statuses: [String]
    public let harnesses: [Harness]
    /// How a live interactive REPL is told from one waiting on a human — and
    /// whether that classifier is even configured. An unconfigured classifier
    /// reports `unknown`, which is not a guess dressed as a state.
    public let statusClassifier: String

    public struct Harness: Sendable, Hashable, Identifiable {
        public var id: String { name }
        public let name: String
        /// Everything the server published about it, unabridged.
        public let published: [String: JSONValue]
        public var kind: AgentKind { AgentKind(name) }
    }

    init(_ d: [String: JSONValue]) {
        statuses = (d["statuses"]?.arrayValue ?? []).compactMap(\.stringValue)
        harnesses = (d["harnesses"]?.arrayValue ?? []).compactMap(\.objectValue).map {
            Harness(name: $0["name"]?.stringValue ?? $0["agent"]?.stringValue ?? "",
                    published: $0)
        }
        statusClassifier = d["status_classifier"]?.stringValue ?? ""
    }
}

extension SpacesConnection {
    /// The tool names this backend offers, as a liveness probe and to check a
    /// tool exists before depending on it. Distinct from `Space.services`,
    /// which lists the MCP services running *inside* a Space.
    public func spacesToolNames() async throws -> [String] {
        try await availableTools()
    }
}

/// What `Space.requestSiteLogin` did. Never the password.
public struct SiteLogin: Sendable, Equatable {
    public enum Status: Sendable, Equatable {
        /// The user has not approved yet: retry with `requestID`.
        case pending
        /// Signed in.
        case filled
    }
    public let status: Status
    public let requestID: String?
    public let site: String?
    /// The username, masked (`a***@example.test`).
    public let usernameHint: String?
    public let submitted: Bool
    public let pageURL: String?
    /// The browser tab it signed in (cua-driver ids).
    public let targetID: String?
    public let tabID: String?
}

// MARK: - Keep running

/// Commands the Space keeps running: restarted by their policy when they end
/// or fail their health check, started when the Space boots, their output
/// kept in a log file in the Space.
public struct KeepRunning: Sendable {
    let space: Space

    /// Calls `space_service` with `action` and its arguments, and returns
    /// the tool's JSON answer (`services`, a service, `logs` or `removed`).
    @discardableResult
    public func service(_ action: String, _ arguments: [String: JSONValue] = [:]) async throws -> JSONValue {
        var args = arguments
        args["space"] = .string(space.id.rawValue)
        args["action"] = .string(action)
        return try await space.connection.callTool("space_service", args)
    }

    /// Keeps `command` running as `name` (replacing a service of that name).
    @discardableResult
    public func add(_ name: String, command: String, restart: String = "always") async throws -> JSONValue {
        try await service("add", ["name": .string(name), "command": .string(command),
                                  "restart": .string(restart)])
    }

    /// The Space's services.
    public func list() async throws -> JSONValue { try await service("list") }

    /// Stops a service and forgets it.
    public func remove(_ name: String) async throws {
        _ = try await service("remove", ["name": .string(name)])
    }
}

extension Space {
    /// Commands this Space keeps running (`space_service`).
    public var keepRunning: KeepRunning { KeepRunning(space: self) }
}

// MARK: - Background jobs

/// Long commands the Space runs in the background: they keep running when
/// the caller goes away, their output kept in a log in the Space.
public struct SpaceJobs: Sendable {
    let space: Space

    /// Calls `space_job` with `action` and its arguments, and returns the
    /// tool's JSON answer (a job's status, a log page, or `jobs`).
    @discardableResult
    public func job(_ action: String, _ arguments: [String: JSONValue] = [:]) async throws -> JSONValue {
        var args = arguments
        args["space"] = .string(space.id.rawValue)
        args["action"] = .string(action)
        return try await space.connection.callTool("space_job", args)
    }

    /// Starts `command` as a background job; `notify` wakes the caller when it ends.
    @discardableResult
    public func start(_ command: String, name: String? = nil, notify: Bool = false) async throws -> JSONValue {
        var args: [String: JSONValue] = ["command": .string(command), "notify": .bool(notify)]
        if let name { args["name"] = .string(name) }
        return try await job("start", args)
    }

    /// A job's state and the end of its log.
    public func status(_ id: String) async throws -> JSONValue {
        try await job("status", ["job": .string(id)])
    }

    /// Stops a job and returns its final status.
    @discardableResult
    public func cancel(_ id: String) async throws -> JSONValue {
        try await job("cancel", ["job": .string(id)])
    }

    /// This Space's jobs, newest first.
    public func list() async throws -> JSONValue { try await job("list") }
}

extension Space {
    /// Commands this Space runs as background jobs (`space_job`).
    public var jobs: SpaceJobs { SpaceJobs(space: self) }

    /// The Space's network: preset, what it opens, where it is enforced
    /// (`network_show`).
    public func network() async throws -> [String: JSONValue] {
        try await connection.object("network_show", ["space": .string(id.rawValue)])
    }

    /// What the Space tried to reach and its network blocked, most recent
    /// first (`network_log`). Each entry carries `reason`, `dst` and `count`,
    /// and `proto` and `last_unix` when known.
    public func networkLog() async throws -> [[String: JSONValue]] {
        try await connection.array("network_log", ["space": .string(id.rawValue)])
            .compactMap { $0.objectValue }
    }

    /// Tightens the Space's network to `internet`, `none` or `custom` with
    /// `allow` / `deny` lists (`network_set`). Opening more is refused: the
    /// user does that.
    @discardableResult
    public func setNetwork(_ preset: String, allow: [String]? = nil, deny: [String]? = nil,
                           tree: Bool = false) async throws -> [String: JSONValue] {
        var args: [String: JSONValue] = ["space": .string(id.rawValue),
                                         "network": .string(preset), "tree": .bool(tree)]
        if let allow { args["allow"] = .array(allow.map(JSONValue.string)) }
        if let deny { args["deny"] = .array(deny.map(JSONValue.string)) }
        return try await connection.object("network_set", args)
    }
}