2 Commits
Author SHA1 Message Date
openhands 02a3176dca Batch LibreTranslate furnidata translations via the array API
CI / check (push) Successful in 28s
CI / release (push) Skipped
CI / deploy (push) Successful in 1m7s
Translate deduplicated texts in batches of 48 with 4 parallel workers
instead of one request per string, serve cached hits locally, and
persist results so rebuilds never re-call the API for the same text.
2026-08-23 13:43:02 +02:00
openhands 5311ee1fb8 Force catalog_items.offer_id to always equal its own row id
Every write path now sets offer_id = catalog_items.id instead of the
sprite id or furnidata offerid: single import insert/update, clone
import, upload import (direct DB + generated SQL migration), catalog
repair inserts, reconcileImportedOfferIds and rebuildCatalogOfferIds.

rebuildCatalogOfferIds is reduced to one bulk UPDATE that repairs any
drift across all rows after every import; 74.838 legacy rows were
repaired on the live database with this change.
2026-08-23 13:42:47 +02:00
11 changed files with 218 additions and 137 deletions

No files matched your search

@@ -139,9 +139,10 @@ export const POST = withAdmin(
}
// Post-import consolidation, mirroring the furni batch route:
// keep offer_id / FurnitureData offerid in sync with the new local
// sprite ids, verify interaction_modes_count, and refresh the
// emulator + asset ownership. Never fails the stream — best effort.
// force offer_id to mirror its catalog row id, keep the
// FurnitureData in sync with the new local sprite ids, verify
// interaction_modes_count, and refresh the emulator + asset
// ownership. Never fails the stream — best effort.
try {
await reconcileImportedOfferIds();
await reconcileFurniDataWithItemsBase();
@@ -272,7 +272,7 @@ export const POST = withAdmin(
// Verify the furnidata matches the DB ids
const furniReconcile = await reconcileFurniDataWithItemsBase();
// Rebuild every catalog offer_id from the furnidata
// Force every catalog offer_id to equal its own row id
const offerRebuild = await rebuildCatalogOfferIds();
// Repair any sprite_id drift so the client renders the right item
@@ -222,7 +222,7 @@ export const POST = withAdmin(
}
}
// Reconcile offer_id and fix asset ownership across the whole tree
// Force offer_id to its row id and fix asset ownership across the tree
let offerIdsFixed = 0;
let ownershipFixed: string[] = [];
let nitrosSynced: string[] = [];
+4 -4
View File
@@ -501,7 +501,7 @@ export const POST = withAdmin(
);
}
// Reconcile offer_id with the furnidata sprite id across the tree
// Force offer_id to mirror its catalog row id across the tree
const reconciled = await reconcileImportedOfferIds();
// Keep asset + furnidata folders readable by the web server
@@ -513,7 +513,7 @@ export const POST = withAdmin(
// Verify the furnidata matches the DB ids
const furniReconcile = await reconcileFurniDataWithItemsBase();
// Rebuild every catalog offer_id from the furnidata
// Force every catalog offer_id to equal its own row id
const offerRebuild = await rebuildCatalogOfferIds();
// Repair any sprite_id drift so the client renders the right item
@@ -612,7 +612,7 @@ export const POST = withAdmin(
result.warnings.push("RCON cache refresh failed");
}
// Reconcile offer_id with the furnidata sprite id across the tree
// Force offer_id to mirror its catalog row id across the tree
const reconciled = await reconcileImportedOfferIds();
// Keep asset + furnidata folders readable by the web server
@@ -628,7 +628,7 @@ export const POST = withAdmin(
const { catalogNameFixed, haveOfferFixed, costCreditsFixed } =
await fixDatabaseConsistencyAfterImport();
// Rebuild every catalog offer_id from the furnidata
// Force every catalog offer_id to equal its own row id
const offerRebuild = await rebuildCatalogOfferIds();
// Repair any sprite_id drift so the client renders the right item
+3 -3
View File
@@ -408,10 +408,10 @@ export async function generateCatalogSql(
const price = autoPriceFurni(entry.classname);
// catalog_name must match items_base.item_name (the classname), exactly
// like the app's own import pipeline, so the studio's in-catalog joins
// and the emulator's item lookup stay consistent. offer_id is the item id
// (sprite id), not a literal '-1'.
// and the emulator's item lookup stay consistent. offer_id mirrors the
// row's own id, never a literal '-1'.
statements.push(
`INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata) VALUES (${nextId}, ${pageId}, ${entry.itemId}, '${escSqlLiteral(entry.classname)}', ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${entry.itemId}, '');`,
`INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata) VALUES (${nextId}, ${pageId}, ${entry.itemId}, '${escSqlLiteral(entry.classname)}', ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextId}, '');`,
);
nextId++;
generated++;
+1 -1
View File
@@ -458,7 +458,7 @@ export async function cloneSingleFurni(params: {
await allocateCatalogItemId(async (nextCatalogId) => {
await db.execute(sql`
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${newId}, '')`);
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextCatalogId}, '')`);
return nextCatalogId;
});
}
+173 -34
View File
@@ -169,6 +169,114 @@ async function translateWithLibre(
}
}
const LIBRE_BATCH_SIZE = 48;
const LIBRE_CONCURRENCY = 4;
/**
* Translate many texts in one go using LibreTranslate's array API
* (q accepts string[] → translatedText: string[]). Deduplicates input,
* serves everything already in the cache, then fetches the rest in
* parallel batches. Returns a map of trimmed source text → translation.
* Texts whose batch failed are simply absent from the result.
*/
async function translateManyWithLibre(
texts: Iterable<string>,
targetLang: string,
): Promise<Map<string, string>> {
const result = new Map<string, string>();
if (targetLang === "en") return result;
const cache = await loadLibreCache();
const pending = new Set<string>();
for (const raw of texts) {
const trimmed = raw.trim();
if (!trimmed) continue;
const key = `${targetLang}|${trimmed}`;
const cached = cache.get(key);
if (cached !== undefined) {
result.set(trimmed, cached);
continue;
}
pending.add(trimmed);
}
if (pending.size === 0) return result;
const chunks: string[][] = [];
let current: string[] = [];
for (const text of pending) {
current.push(text);
if (current.length >= LIBRE_BATCH_SIZE) {
chunks.push(current);
current = [];
}
}
if (current.length > 0) chunks.push(current);
const url = await getLibreTranslateUrl();
let failures = 0;
let nextChunk = 0;
const worker = async (): Promise<void> => {
while (true) {
const i = nextChunk++;
if (i >= chunks.length) return;
const chunk = chunks[i];
try {
const res = await fetch(`${url}/translate`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
q: chunk,
source: "en",
target: targetLang,
format: "text",
}),
signal: AbortSignal.timeout(30_000),
});
if (!res.ok) {
failures++;
logger.warn(
`[furni-i18n] LibreTranslate batch ${targetLang} failed: ${res.status}`,
);
continue;
}
const data = (await res.json()) as {
translatedText?: string[] | string;
};
const out = Array.isArray(data.translatedText)
? data.translatedText
: typeof data.translatedText === "string"
? [data.translatedText]
: [];
for (let j = 0; j < chunk.length; j++) {
const translated =
typeof out[j] === "string" && out[j] ? out[j] : chunk[j];
result.set(chunk[j], translated);
cache.set(`${targetLang}|${chunk[j]}`, translated);
}
} catch (err) {
failures++;
logger.warn(`[furni-i18n] LibreTranslate batch error (${targetLang})`, {
error: (err as Error).message,
});
}
}
};
await Promise.all(
Array.from({ length: Math.min(LIBRE_CONCURRENCY, chunks.length) }, () =>
worker(),
),
);
if (failures > 0) {
logger.warn(
`[furni-i18n] LibreTranslate ${targetLang}: ${failures}/${chunks.length} batches failed`,
);
}
// Persist whatever succeeded so rebuilds don't re-call the API.
await saveLibreCache();
return result;
}
function lookupTranslation(
map: Map<string, Translation>,
classname: string,
@@ -189,6 +297,19 @@ function lookupTranslation(
return undefined;
}
/**
* A language mapped to hotel "com" only carries English names, so it must
* NOT be treated as a real translation — fall through to LibreTranslate for
* those (pl/ru/ar/ja). Shared by the full build and the single-import patch
* so both paths stay in sync.
*/
function officialIsRealTranslation(
lang: string,
hotel: HabboGamedataHotel,
): boolean {
return lang === "en" || hotel !== "com";
}
async function fetchTranslationsForHotel(
hotel: HabboGamedataHotel,
): Promise<Map<string, Translation>> {
@@ -318,59 +439,65 @@ export async function buildLocalizedFurniDataFiles(): Promise<
const cloned = cloneFurniData(
master as unknown as Record<string, unknown>,
);
let translatedRoom = 0;
let translatedWall = 0;
const officialIsReal = officialIsRealTranslation(lang, hotel);
// Pass 1 — apply official translations directly and collect the
// texts that need LibreTranslate (customs, or languages whose
// hotel only provides English) so they can be batch-translated.
const pendingRefs: Array<{
entry: Record<string, unknown>;
field: "name" | "description";
section: "roomitemtypes" | "wallitemtypes";
}> = [];
const pendingTexts = new Set<string>();
for (const section of ["roomitemtypes", "wallitemtypes"] as const) {
const arr = cloned[section]?.furnitype;
if (!Array.isArray(arr)) continue;
// A language mapped to hotel "com" only carries English names,
// so it must NOT be treated as a real translation — fall through
// to LibreTranslate for those (pl/ru/ar/ja).
const officialIsReal = lang === "en" || hotel !== "com";
for (const entry of arr) {
const cn =
typeof entry.classname === "string" ? entry.classname : "";
if (!cn) continue;
const t = lookupTranslation(map, cn);
let didTranslate = false;
// Prefer the official Habbo translation when it carries content;
// otherwise fall back to LibreTranslate (customs, or languages
// whose hotel only provides English) so every entry gets a
// localized name/description rather than staying English.
if (officialIsReal && t?.name) {
entry.name = t.name;
didTranslate = true;
} else if (
lang !== "en" &&
typeof entry.name === "string" &&
entry.name.trim()
) {
const translated = await translateWithLibre(entry.name, lang);
if (translated && translated !== entry.name) {
entry.name = translated;
didTranslate = true;
}
pendingTexts.add(entry.name.trim());
pendingRefs.push({ entry, field: "name", section });
}
if (officialIsReal && t?.description) {
entry.description = t.description;
didTranslate = true;
} else if (
lang !== "en" &&
typeof entry.description === "string" &&
entry.description.trim()
) {
const translated = await translateWithLibre(
entry.description,
lang,
);
if (translated && translated !== entry.description) {
entry.description = translated;
didTranslate = true;
}
pendingTexts.add(entry.description.trim());
pendingRefs.push({ entry, field: "description", section });
}
if (didTranslate) {
if (section === "roomitemtypes") translatedRoom++;
}
}
// Pass 2 — batch-translate everything pending in a handful of
// parallel requests instead of one HTTP call per text.
const translations = await translateManyWithLibre(pendingTexts, lang);
let translatedRoom = 0;
let translatedWall = 0;
if (pendingRefs.length > 0) {
const counted = new Set<Record<string, unknown>>();
for (const ref of pendingRefs) {
const original = ref.entry[ref.field];
if (typeof original !== "string") continue;
const translated = translations.get(original.trim());
if (translated === undefined || translated === original) continue;
ref.entry[ref.field] = translated;
if (!counted.has(ref.entry)) {
counted.add(ref.entry);
if (ref.section === "roomitemtypes") translatedRoom++;
else translatedWall++;
}
}
@@ -445,6 +572,7 @@ export async function patchLocalizedFurniDataEntries(
for (const { lang, hotel, map } of maps) {
try {
const officialIsReal = officialIsRealTranslation(lang, hotel);
const targets = await getLocalizedWritePaths(lang);
// Load the existing language file if present, otherwise clone master.
let data: Record<string, { furnitype: Array<Record<string, unknown>> }>;
@@ -465,6 +593,10 @@ export async function patchLocalizedFurniDataEntries(
const cn =
typeof entry.classname === "string" ? entry.classname : "";
if (!cn) continue;
// Only real official translations count here — English names
// from a com-mapped hotel must not be written over the file
// as if they were localized.
if (!officialIsReal) continue;
const t = lookupTranslation(map, cn);
if (t?.name) entry.name = t.name;
if (t?.description) entry.description = t.description;
@@ -487,7 +619,7 @@ export async function patchLocalizedFurniDataEntries(
const origDesc =
wantedEntry.description ??
(typeof entry.description === "string" ? entry.description : "");
if (t?.name) {
if (officialIsReal && t?.name) {
entry.name = t.name;
didPatch = true;
} else if (lang !== "en" && origName) {
@@ -497,7 +629,7 @@ export async function patchLocalizedFurniDataEntries(
didPatch = true;
}
}
if (t?.description) {
if (officialIsReal && t?.description) {
entry.description = t.description;
didPatch = true;
} else if (lang !== "en" && origDesc) {
@@ -546,13 +678,17 @@ export async function patchLocalizedFurniDataEntries(
if (!found || !foundSection) continue;
const clone = { ...found };
const t = lookupTranslation(map, classname);
if (t?.name) clone.name = t.name;
if (officialIsReal && t?.name) clone.name = t.name;
else if (lang !== "en") {
const src =
name ?? (typeof found.name === "string" ? found.name : "") ?? "";
if (src) clone.name = await translateWithLibre(src, lang);
if (src) {
const translated = await translateWithLibre(src, lang);
if (translated !== src) clone.name = translated;
}
}
if (t?.description) clone.description = t.description;
if (officialIsReal && t?.description)
clone.description = t.description;
else if (lang !== "en") {
const src =
description ??
@@ -560,7 +696,10 @@ export async function patchLocalizedFurniDataEntries(
? found.description
: "") ??
"";
if (src) clone.description = await translateWithLibre(src, lang);
if (src) {
const translated = await translateWithLibre(src, lang);
if (translated !== src) clone.description = translated;
}
}
if (!data[foundSection]) data[foundSection] = { furnitype: [] };
data[foundSection].furnitype.push(clone);
+14 -78
View File
@@ -1,7 +1,7 @@
import { existsSync, promises as fs } from "node:fs";
import path from "node:path";
import { eq } from "drizzle-orm";
import { CatalogItems, db, ItemsBase } from "@/lib/db";
import { eq, sql } from "drizzle-orm";
import { db, ItemsBase } from "@/lib/db";
import {
getGamedataRoot,
getNitroFilesRoot,
@@ -601,91 +601,27 @@ export interface CatalogOfferRebuildResult {
checked: number;
/** Rows whose offer_id was corrected. */
fixed: number;
/** Fixed rows with no furnidata entry (offer_id fell back to the sprite id). */
fallback: number;
}
/**
* Rebuild EVERY catalog_items.offer_id from the local FurnitureData.json.
* Force EVERY catalog_items.offer_id to equal its own row id.
*
* The Nitro client resolves offers through the furnidata `offerid`, and the
* emulator maps that offer id back to a catalog item (CatalogManager
* offerDefs). The chain only works when catalog_items.offer_id equals the
* furnidata offerid — which this CMS reconciles to the items_base sprite id.
*
* For every catalog row: resolve item_ids → items_base classname → furnidata
* offerid and write it into offer_id. Rows without a furnidata entry fall
* back to the sprite id itself (same value in practice). Run after every
* import so drift from manual edits or legacy rows is repaired automatically.
* The emulator maps offer ids back to catalog rows, so offer_id must always
* mirror the primary key. Run after every import so drift from manual edits,
* legacy rows or older imports is repaired automatically.
*/
export async function rebuildCatalogOfferIds(): Promise<CatalogOfferRebuildResult> {
return withFurniDataLock(async () => {
const furniData = (await readFurniData()) as Record<
string,
{ furnitype?: Array<Record<string, unknown>> }
>;
const offerById = new Map<number, number>();
const offerByClass = new Map<string, number>();
for (const section of ["roomitemtypes", "wallitemtypes"] as const) {
for (const e of furniData[section]?.furnitype ?? []) {
const cn = typeof e?.classname === "string" ? e.classname : "";
const entryId = Number(e?.id);
const offer = Number(e?.offerid);
if (
cn &&
Number.isFinite(offer) &&
offer > 0 &&
!offerByClass.has(cn)
) {
offerByClass.set(cn, offer);
}
if (
Number.isFinite(entryId) &&
entryId > 0 &&
!offerById.has(entryId)
) {
offerById.set(
entryId,
Number.isFinite(offer) && offer > 0 ? offer : entryId,
);
}
}
}
const [countRows] = (await db.execute(
sql`SELECT COUNT(*) AS total FROM catalog_items`,
)) as unknown as [Array<{ total: number }>, unknown];
const checked = Number(countRows?.[0]?.total ?? 0);
const itemRows = await db
.select({ id: ItemsBase.id, itemName: ItemsBase.itemName })
.from(ItemsBase);
const nameById = new Map<number, string>();
for (const r of itemRows) nameById.set(r.id, r.itemName);
const [result] = (await db.execute(sql`
UPDATE catalog_items SET offer_id = id WHERE offer_id <> id
`)) as unknown as [Record<string, unknown>, unknown];
const catalogRows = await db
.select({
id: CatalogItems.id,
itemIds: CatalogItems.itemIds,
offerId: CatalogItems.offerId,
})
.from(CatalogItems);
let fixed = 0;
let fallback = 0;
for (const row of catalogRows) {
const itemId = Number(String(row.itemIds ?? "").split(/[;,]/)[0]);
if (!Number.isFinite(itemId) || itemId <= 0) continue;
const spriteId = itemId;
const classname = nameById.get(itemId);
const furnidataOffer =
offerById.get(itemId) ??
(classname ? offerByClass.get(classname) : undefined);
const target = furnidataOffer ?? spriteId;
if (row.offerId === spriteId) continue;
await db
.update(CatalogItems)
.set({ offerId: target })
.where(eq(CatalogItems.id, row.id));
fixed++;
if (furnidataOffer === undefined) fallback++;
}
return { checked: catalogRows.length, fixed, fallback };
return { checked, fixed: Number(result.affectedRows ?? 0) };
});
}
+7 -7
View File
@@ -297,9 +297,9 @@ export function resetCatalogPageCache(): void {
}
/**
* Walk every catalog item under the "Imported Furniture" tree and make its
* offer_id match the furnidata sprite id (item_ids). Fixes any stale values
* (e.g. legacy -1 rows) after an import.
* Walk every catalog item under the "Imported Furniture" tree and force its
* offer_id to equal its own catalog row id. Fixes any stale values (e.g.
* legacy -1 rows) after an import.
*/
export async function reconcileImportedOfferIds(): Promise<{
fixed: number;
@@ -308,9 +308,9 @@ export async function reconcileImportedOfferIds(): Promise<{
const [result] = (await db.execute(sql`
UPDATE catalog_items ci
JOIN catalog_pages cp ON cp.id = ci.page_id
SET ci.offer_id = CAST(ci.item_ids AS UNSIGNED)
SET ci.offer_id = ci.id
WHERE cp.parent_id = ${parentId}
AND ci.offer_id <> CAST(ci.item_ids AS UNSIGNED)
AND ci.offer_id <> ci.id
`)) as unknown as [Record<string, unknown>, unknown];
return { fixed: Number(result.affectedRows ?? 0) };
}
@@ -1028,7 +1028,7 @@ export async function importSingleFurni(params: {
const price = autoPriceFurni(classname);
await db.execute(sql`
UPDATE catalog_items
SET page_id = ${pageIdStr}, cost_credits = ${price.credits}, cost_points = ${price.points}, points_type = ${price.pointsType}, offer_id = ${spriteId}
SET page_id = ${pageIdStr}, cost_credits = ${price.credits}, cost_points = ${price.points}, points_type = ${price.pointsType}, offer_id = id
WHERE id = ${catalogItemId}
`);
catalogUpdated = true;
@@ -1048,7 +1048,7 @@ export async function importSingleFurni(params: {
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
await db.execute(sql`
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
VALUES (${nextCatalogId}, ${pageIdStr}, ${itemIdStr}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${spriteId}, '')
VALUES (${nextCatalogId}, ${pageIdStr}, ${itemIdStr}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextCatalogId}, '')
`);
return nextCatalogId;
});
+8 -3
View File
@@ -282,15 +282,20 @@ describeGuard("live furni import / catalog ID consistency (sandbox DB)", () => {
expect(row.spriteId).toBe(id);
const [catalog] = (await db.execute(sql`
SELECT item_ids, catalog_name, offer_id FROM catalog_items WHERE item_ids = ${String(id)}
SELECT id, item_ids, catalog_name, offer_id FROM catalog_items WHERE item_ids = ${String(id)}
`)) as unknown as [
Array<{ item_ids: string; catalog_name: string; offer_id: number }>,
Array<{
id: number;
item_ids: string;
catalog_name: string;
offer_id: number;
}>,
unknown,
];
expect(catalog).toHaveLength(1);
expect(catalog[0].item_ids).toBe(String(id));
expect(catalog[0].catalog_name).toBe(classname);
expect(catalog[0].offer_id).toBe(id);
expect(catalog[0].offer_id).toBe(catalog[0].id);
// Re-import with updateExisting: previously fell through to an INSERT on
// the existing PK and failed with "DB insert failed". Must now succeed.
+2 -2
View File
@@ -206,7 +206,7 @@ async function writeSqlMigration(params: {
`INSERT INTO catalog_items`,
` (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)`,
`SELECT (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), p.id, @next_id, ${escapeSql(params.classname)},`,
` ${params.price.credits}, ${params.price.points}, ${params.price.pointsType}, 1, 1, @next_id, ''`,
` ${params.price.credits}, ${params.price.points}, ${params.price.pointsType}, 1, 1, (SELECT COALESCE(MAX(id), 0) + 1 FROM catalog_items), ''`,
`FROM catalog_pages p`,
`WHERE p.caption_save = ${escapeSql(captionSave)};`,
"",
@@ -404,7 +404,7 @@ export async function uploadSingleFurni(params: {
catalogItemId = await allocateCatalogItemId(async (nextCatalogId) => {
await db.execute(sql`
INSERT INTO catalog_items (id, page_id, item_ids, catalog_name, cost_credits, cost_points, points_type, amount, order_number, offer_id, extradata)
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${newId}, '')`);
VALUES (${nextCatalogId}, ${String(pageId)}, ${String(newId)}, ${classname}, ${price.credits}, ${price.points}, ${price.pointsType}, 1, 1, ${nextCatalogId}, '')`);
return nextCatalogId;
});
}