Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

4.7 KB
Raw Blame
#!/usr/bin/env -S pnpm exec tsx
/** Generate the Rust chat-error catalog from the canonical frontend i18n maps. */

import { readFile, writeFile } from "node:fs/promises";
import { execFileSync } from "node:child_process";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
const i18nDir = path.join(root, "app/src/lib/i18n");
const localeTypesPath = path.join(i18nDir, "types.ts");
const outputPath = path.join(
  root,
  "crates/openhuman-core/src/inference/failure_copy/chat_error_catalog.rs",
);

function supportedLocales(source: string): string[] {
  const union = source.match(/export\s+type\s+Locale\s*=([\s\S]*?);/);
  if (!union) {
    throw new Error(`${localeTypesPath}: could not find the Locale union`);
  }
  const locales = [...union[1].matchAll(/'([^']+)'/g)].map((match) => match[1]);
  if (!locales.length || new Set(locales).size !== locales.length) {
    throw new Error(
      `${localeTypesPath}: Locale union is empty or contains duplicates`,
    );
  }
  return locales;
}

async function loadLocale(locale: string): Promise<Record<string, unknown>> {
  const file = path.join(i18nDir, `${locale}.ts`);
  const translations = (await import(pathToFileURL(file).href)).default;
  if (translations == null || typeof translations !== "object") {
    throw new Error(`${file}: expected a translation map`);
  }
  return translations as Record<string, unknown>;
}

function placeholders(value: string): string[] {
  return [...value.matchAll(/\{([A-Za-z][A-Za-z0-9_]*)\}/g)]
    .map((match) => match[1])
    .sort();
}

function rustString(value: string): string {
  return JSON.stringify(value);
}

async function renderCatalog(): Promise<string> {
  const locales = supportedLocales(await readFile(localeTypesPath, "utf8"));
  const maps = new Map<string, Record<string, unknown>>();
  for (const locale of locales) maps.set(locale, await loadLocale(locale));

  const english = maps.get("en");
  if (!english) throw new Error("Locale union must include en");
  const keys = Object.keys(english)
    .filter((key) => key.startsWith("chat_error."))
    .sort();
  if (!keys.length)
    throw new Error("English catalog contains no chat_error keys");

  const entries: Array<[string, string, string]> = [];
  for (const key of keys) {
    const englishValue = english[key];
    if (typeof englishValue !== "string") {
      throw new Error(`en:${key} is not a string`);
    }
    const expectedPlaceholders = placeholders(englishValue).join(",");
    for (const locale of locales) {
      const translated = maps.get(locale)?.[key];
      if (typeof translated !== "string") {
        throw new Error(`${locale}:${key} is missing or is not a string`);
      }
      if (placeholders(translated).join(",") !== expectedPlaceholders) {
        throw new Error(
          `${locale}:${key} has different interpolation placeholders`,
        );
      }
      entries.push([locale, key, translated]);
    }
  }

  const localeRows = locales
    .map((locale) => `    ${rustString(locale)},`)
    .join("\n");
  const translationRows = entries
    .map(
      ([locale, key, translated]) =>
        `        (${rustString(locale)}, ${rustString(key)}) => Some(${rustString(translated)}),`,
    )
    .join("\n");
  const source = `// Generated by scripts/generate-rust-chat-error-catalog.ts; do not edit.\n\
pub const SUPPORTED_LOCALES: &[&str] = &[\n${localeRows}\n];\n\
pub fn translation(locale: &str, key: &str) -> Option<&'static str> {\n\
    match (locale, key) {\n${translationRows}\n\
        _ => None,\n\
    }\n\
}\n`;
  return execFileSync("rustfmt", ["--edition", "2021", "--emit", "stdout"], {
    input: source,
    encoding: "utf8",
  });
}

async function main() {
  const [mode] = process.argv.slice(2);
  if (mode !== "--write" && mode !== "--check") {
    throw new Error(
      "usage: pnpm exec tsx scripts/generate-rust-chat-error-catalog.ts --write|--check",
    );
  }
  const rendered = await renderCatalog();
  if (mode === "--write") {
    await writeFile(outputPath, rendered);
    console.log(`wrote ${path.relative(root, outputPath)}`);
    return;
  }
  let current: string;
  try {
    current = await readFile(outputPath, "utf8");
  } catch {
    throw new Error(
      `${path.relative(root, outputPath)} is missing; run with --write`,
    );
  }
  if (current !== rendered) {
    throw new Error(
      `${path.relative(root, outputPath)} is stale; run with --write`,
    );
  }
  const localeCount = supportedLocales(
    await readFile(localeTypesPath, "utf8"),
  ).length;
  console.log(
    `verified ${path.relative(root, outputPath)} (${localeCount} locales)`,
  );
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});