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.6 KB
Raw Blame
#!/usr/bin/env node
// SPDX-License-Identifier: FSL-1.1-MIT
// Copyright (c) 2026 Cua AI, Inc.

// Writes Sources/OpenKoalaBotExample/Spaces/SandboxImages.generated.swift from the
// shared image list, libs/images/sandbox-images.json (published entries only,
// in file order). `--check` exits 1 when the checked-in file has drifted.
import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const here = dirname(fileURLToPath(import.meta.url));
const pkg = resolve(here, "..");
const source = resolve(pkg, "../../libs/images/sandbox-images.json");
const target = resolve(pkg, "Sources/OpenKoalaBotExample/Spaces/SandboxImages.generated.swift");

function swiftString(s) {
  let out = "";
  for (const ch of String(s)) {
    const c = ch.codePointAt(0);
    if (ch === "\\") out += "\\\\";
    else if (ch === '"') out += '\\"';
    else if (ch === "\n") out += "\\n";
    else if (ch === "\t") out += "\\t";
    else if (c < 0x20 || c === 0x7f) out += `\\u{${c.toString(16)}}`;
    else out += ch;
  }
  return `"${out}"`;
}

const opt = (v) => (v === null || v === undefined ? "nil" : swiftString(v));

export function render(json) {
  // Published entries of groups offered in pickers (benchmark images set
  // `picker: false`: they are for cua-bench, not Spaces).
  const offered = new Set(json.groups.filter((g) => g.picker !== false).map((g) => g.id));
  const images = json.images.filter((i) => i.published === true && offered.has(i.group));
  const used = new Set(images.map((i) => i.group));
  const groups = json.groups.filter((g) => used.has(g.id));
  for (const i of images) {
    if (!json.groups.some((g) => g.id === i.group)) {
      throw new Error(`image ${i.ref} names unknown group ${i.group}`);
    }
  }
  const lines = [
    // The sample is FSL-1.1-MIT, so its sources carry the SPDX header.
    "// SPDX-License-Identifier: FSL-1.1-MIT",
    "// Copyright (c) 2026 Cua AI, Inc.",
    "",
    "// Generated by scripts/gen-images.mjs from libs/images/sandbox-images.json.",
    "// Do not edit: change the JSON and rerun `node scripts/gen-images.mjs`.",
    "",
    "extension SandboxImages {",
    "    /// The groups that have at least one published image, in file order.",
    "    static let groups: [SandboxImageGroup] = [",
    ...groups.map((g) => `        SandboxImageGroup(id: ${swiftString(g.id)}, label: ${swiftString(g.label)}),`),
    "    ]",
    "",
    "    /// Every published image, in file order.",
    "    static let published: [SandboxImage] = [",
    ...images.flatMap((i) => [
      "        SandboxImage(",
      `            ref: ${swiftString(i.ref)},`,
      `            group: ${swiftString(i.group)},`,
      `            os: ${swiftString(i.os)},`,
      `            name: ${swiftString(i.name)},`,
      `            variant: ${swiftString(i.variant)},`,
      `            summary: ${swiftString(i.summary)},`,
      `            spacesd: ${i.spacesd ? "true" : "false"},`,
      `            local: ${opt(i.local)},`,
      `            cloud: ${opt(i.cloud)},`,
      `            tier: ${opt(i.tier)}),`,
    ]),
    "    ]",
    "}",
    "",
  ];
  return lines.join("\n");
}

const out = render(JSON.parse(readFileSync(source, "utf8")));
if (process.argv.includes("--check")) {
  const current = existsSync(target) ? readFileSync(target, "utf8") : "";
  if (current !== out) {
    console.error(`${target} is out of date with ${source}. Run: node scripts/gen-images.mjs`);
    process.exit(1);
  }
  console.log("SandboxImages.generated.swift is up to date");
} else {
  writeFileSync(target, out);
  console.log(`wrote ${target}`);
}