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.

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

// A Space's live desktop (or one window) for the page's WebCodecs player
// (`spaces.openStream {spaceId, tier, windowId?, epoch?, activate?}`, only this host: the Mac app draws
// video natively): a media ticket from this app's daemon
// (`media-bridge.ts`), started when it isn't running. The video goes from
// the page straight to the daemon's loopback listener, never through IPC.
import { cuaHome } from "../model/daemon";
import { createStreamOpener, StreamUnavailableError, type Fetch } from "../media-bridge";
import type { BridgeContext } from "./context";
import { Failure, type Handlers } from "./host";

export function streamMethods({ supervisor, env, model }: BridgeContext): Handlers {
  const open = createStreamOpener({
    cuaHome: cuaHome(env),
    fetch: globalThis.fetch as unknown as Fetch,
    hasCua: async () => supervisor !== null,
    // Success is daemon.json appearing; the opener waits for it.
    startDaemon: async () => {
      await supervisor?.start();
    },
    testStreamUrl: env.CUA_SPACES_TEST_STREAM_URL || null,
  });
  return {
    "spaces.openStream": async (args) => {
      const { spaceId, tier, windowId, epoch, activate } = args;
      if (typeof spaceId !== "string" || spaceId === "") throw Failure.badArgs("spaceId: string");
      if (tier !== "tile" && tier !== "full") throw Failure.badArgs("tier: tile | full");
      if (windowId !== undefined && (typeof windowId !== "string" || windowId === "")) throw Failure.badArgs("windowId: string");
      if (epoch !== undefined && (typeof epoch !== "number" || !Number.isInteger(epoch) || epoch < 0)) throw Failure.badArgs("epoch: number");
      try {
        const ticket = await open({ spaceId, tier, windowId, epoch, activate: activate === true });
        // A viewer opening is the `space_open_viewer` usage event the SwiftUI and Tauri apps record; this host
        // recorded none, so the daily report's viewer count fell to zero from 0.9.0. The grid's tiles are not viewers.
        if (tier === "full" && !epoch) {
          try {
            model.recordFeature("space_open_viewer");
          } catch {
            // Telemetry never fails a stream.
          }
        }
        return ticket;
      } catch (error) {
        if (error instanceof StreamUnavailableError) throw Failure.unsupported(error.message);
        throw error;
      }
    },
  };
}