Skip to content

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

refactor(script): update file path handling in catalog generator

Steven Enamakelc414c034b7ee
diff --git a/scripts/generate-rust-chat-error-catalog.ts b/scripts/generate-rust-chat-error-catalog.ts
index ef07fa2e2f..3f5a0a204b 100644
--- a/scripts/generate-rust-chat-error-catalog.ts
+++ b/scripts/generate-rust-chat-error-catalog.ts
@@ -1,22 +1,24 @@
#!/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 path from 'node:path';
-import { fileURLToPath, pathToFileURL } from 'node:url';
+import { readFile, writeFile } from "node:fs/promises";
+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 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'
+ "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 (!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`);
}
@@ -34,7 +36,7 @@ async function loadLocale(locale: string): Promise<Record<string, unknown>> {
function placeholders(value: string): string[] {
return [...value.matchAll(/\{([A-Za-z][A-Za-z0-9_]*)\}/g)]
- .map(match => match[1])
+ .map((match) => match[1])
.sort();
}
@@ -43,39 +45,45 @@ function rustString(value: string): string {
}
async function renderCatalog(): Promise<string> {
- const locales = supportedLocales(await readFile(localeTypesPath, 'utf8'));
+ 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 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(',');
+ 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') {
+ if (typeof translated !== "string") {
throw new Error(`${locale}:${key} is missing or is not a string`);
}
- if (placeholders(translated).join(',') !== expectedPlaceholders) {
+ 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 localeRows = locales
+ .map((locale) => ` ${rustString(locale)},`)
+ .join("\n");
const translationRows = entries
.map(
([locale, key, translated]) =>
- ` (${rustString(locale)}, ${rustString(key)}) => Some(${rustString(translated)}),`
+ ` (${rustString(locale)}, ${rustString(key)}) => Some(${rustString(translated)}),`,
)
- .join('\n');
+ .join("\n");
return `// 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\
@@ -87,25 +95,28 @@ pub fn translation(locale: &str, key: &str) -> Option<&'static str> {\n\
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');
+ 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') {
+ if (mode === "--write") {
await writeFile(outputPath, rendered);
console.log(`wrote ${path.relative(root, outputPath)}`);
return;
}
let current: string;
try {
- current = await readFile(outputPath, 'utf8');
+ 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`);
}
- console.log(`verified ${path.relative(root, outputPath)} (${supportedLocales(await readFile(localeTypesPath, 'utf8')).length} locales)`);
+ const localeCount = supportedLocales(await readFile(localeTypesPath, "utf8")).length;
+ console.log(`verified ${path.relative(root, outputPath)} (${localeCount} locales)`);
}
main().catch(error => {