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.

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

/**
 * Plain words for the host's raw failures. The `cua` command and the relay
 * word errors for logs ("cua: unauthenticated: host setup failed at
 * register: relay: ..."); a person gets one short line. The raw text goes to
 * the console log (the app's log file), never away.
 */

/** Internal prefixes the command and relay put in front of the part a person can use. */
const PREFIX = /^(?:(?:cua|relay|error|unauthenticated|not[ _]found|host setup failed at [\w-]+)\s*:\s*)+/i;

/** `text` without internal prefixes ("cua: relay: ..." becomes "..."), first letter capitalised. */
export function stripInternalPrefixes(text: string): string {
  const s = text.trim().replace(PREFIX, "").trim();
  return s ? s.charAt(0).toUpperCase() + s.slice(1) : "";
}

function rawText(e: unknown): string {
  return e instanceof Error ? e.message : String(e);
}

/**
 * The plain line for the server telling us to slow down: the host's own
 * sentence ("Too many attempts right now. Wait 3 seconds, then try again.")
 * or a bare HTTP 429 "Too Many Requests" from an older host. A 429 that says
 * why (a limit, a quota) is not this; null for anything else.
 */
export function plainThrottleError(raw: string): string | null {
  const own = /too many attempts right now\. wait (\d+) seconds?/i.exec(raw);
  if (own) return `Too many attempts right now. Wait ${own[1]} ${own[1] === "1" ? "second" : "seconds"}, then try again.`;
  if (/\bhttp 429\b[:\s]*too many requests\s*$/i.test(raw.trim())) return "Too many attempts right now. Wait a few seconds, then try again.";
  return null;
}

/** Seconds a throttled host asked to wait (its own sentence), else null. */
export function throttleSeconds(raw: string): number | null {
  const own = /too many attempts right now\. wait (\d+) seconds?/i.exec(raw);
  return own ? Number(own[1]) : null;
}

/** What "Enter a code" (Have a code? Enter it) says for a failure. */
export function plainJoinError(e: unknown): string {
  const raw = rawText(e);
  console.warn("[Cua Spaces] join with a code failed:", raw);
  const throttled = plainThrottleError(raw);
  if (throttled) return throttled;
  if (/pairing code|unauthenticated|not valid|expired|revoked|already used|invalid code/i.test(raw)) {
    return "That code didn't work. It may be used, expired or mistyped. Make a new one in Add a machine.";
  }
  if (/timed out/i.test(raw)) return "This took too long. Check the internet connection and try again.";
  if (/could not start|no bundled cua/i.test(raw)) return "Couldn't set this machine up. Update Cua Spaces and try again.";
  if (/network|connect|dns|unreachable|offline/i.test(raw)) return "Couldn't reach Cua. Check the internet connection and try again.";
  return stripInternalPrefixes(raw) || "Couldn't join with that code.";
}

/** What "Add a computer" says when it can't make a link. */
export function plainPairingError(e: unknown): string {
  const raw = rawText(e);
  console.warn("[Cua Spaces] making a pairing link failed:", raw);
  const throttled = plainThrottleError(raw);
  if (throttled) return throttled;
  if (/no such device|not enrolled|not registered|unknown device/i.test(raw)) {
    return "This machine isn't set up for access yet. Set it up first, then add machines.";
  }
  if (/unauthenticated|unauthori[sz]ed|\b40[13]\b/i.test(raw)) return "Sign in again to add a machine.";
  if (/network|connect|dns|unreachable|offline|timed out/i.test(raw)) return "Couldn't reach Cua. Check the internet connection.";
  const rest = stripInternalPrefixes(raw).replace(/\.$/, "");
  return rest ? `Couldn't make a link: ${rest}.` : "Couldn't make a link.";
}

/**
 * The This machine panel's limits line for this system. The core words it
 * for a Mac ("Up to 4 Spaces · 2 macOS VMs"); macOS VMs run on a Mac only,
 * so elsewhere that part is dropped.
 */
export function limitsFor(limits: string, mac: boolean): string {
  return mac ? limits : limits.replace(/\s*·\s*\d+ macOS VMs?\b.*$/, "");
}