Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

forge(cron): backfill the phone-sized delivery variant for meshes forged before it existed

nirholas384f78f0f6e7
diff --git a/api/cron/forge-web-variants.js b/api/cron/forge-web-variants.js
new file mode 100644
index 000000000..cf1034502
--- /dev/null
+++ b/api/cron/forge-web-variants.js
@@ -0,0 +1,110 @@
+// GET /api/cron/forge-web-variants
+//
+// Steady-state backfill of the phone-sized delivery variant for meshes that
+// were forged before it existed.
+//
+// Every finished generation from 2026-09-04 onward gets its variant written
+// right after materialization (buildWebVariant in api/_lib/forge-store.js). The
+// meshes already in the bucket do not, and there are enough of them that a bulk
+// rewrite of storage is the wrong tool: it would be one long unattended job
+// re-uploading objects nobody has asked for. This drains the gap a bounded batch
+// at a time, newest first, because the newest models are the ones actually being
+// opened on a phone.
+//
+// Per row: read the stored original out of the bucket, run the delivery pass
+// (meshopt geometry + WebP textures capped at 2048 px), write the result to a
+// SECOND key, and record it. The original object is never touched, so a row that
+// fails here is exactly as serveable as it was before, and the download action
+// and every third-party API consumer keep the full-resolution mesh.
+//
+// A row whose variant would not actually be smaller is marked as attempted by
+// pointing web_glb_key at the original key, which both keeps it out of the
+// pending index and makes readers serve the original: `web_glb_url` stays null
+// in that case and every reader already treats null as "serve glb_url".
+//
+// Env:
+// CRON_SECRET required (Bearer)
+// FORGE_WEB_VARIANT_BATCH meshes per tick (default 6)
+//
+// Sizing: the pass measured 0.6-5.6 s per mesh on real production output, plus
+// the bucket read and write, so 6 lands well inside maxDuration=120 even on a
+// bad batch.
+
+import { json, wrapCron } from '../_lib/http.js';
+import { logger } from '../_lib/usage.js';
+import { requireCron } from '../_lib/cron-auth.js';
+import { sql } from '../_lib/db.js';
+import { getObjectBuffer, objectStorageConfigured } from '../_lib/r2.js';
+import { forgeStoreEnabled, buildWebVariant } from '../_lib/forge-store.js';
+
+export const maxDuration = 120;
+
+const log = logger('forge-web-variants');
+
+const BATCH = Math.max(1, Math.min(24, Number(process.env.FORGE_WEB_VARIANT_BATCH || 6)));
+
+export default wrapCron(async (req, res) => {
+ if (req.method !== 'GET') return json(res, 405, { error: 'method_not_allowed' });
+ if (!requireCron(req, res)) return;
+ if (!forgeStoreEnabled()) return json(res, 200, { ok: false, reason: 'forge_store_disabled' });
+ if (!objectStorageConfigured()) return json(res, 200, { ok: false, reason: 'object_storage_unconfigured' });
+
+ let rows;
+ try {
+ rows = await sql`
+ select id, glb_key, size_bytes
+ from forge_creations
+ where status = 'done' and glb_url is not null and glb_key is not null and web_glb_key is null
+ order by updated_at desc
+ limit ${BATCH}
+ `;
+ } catch (err) {
+ log.warn('claim_failed', { message: err?.message });
+ return json(res, 200, { ok: false, reason: `claim_failed: ${err?.message}` });
+ }
+
+ if (!rows.length) return json(res, 200, { ok: true, processed: 0, remaining: 0, done: true });
+
+ let built = 0;
+ let skipped = 0;
+ let failed = 0;
+ for (const row of rows) {
+ const keyPrefix = String(row.glb_key).replace(/\.glb$/i, '');
+ try {
+ const buffer = await getObjectBuffer(row.glb_key);
+ const result = await buildWebVariant({ buffer, creationId: row.id, keyPrefix });
+ if (result) {
+ built++;
+ log.info('variant_built', { creation_id: row.id, before: buffer.length, after: result.bytes });
+ } else {
+ // No gain (or too small to bother). Retire the row from the pending
+ // index by pointing its key at the original; web_glb_url stays null,
+ // so readers keep serving glb_url exactly as they do today.
+ await sql`
+ update forge_creations
+ set web_glb_key = ${row.glb_key}, updated_at = now()
+ where id = ${row.id}
+ `;
+ skipped++;
+ }
+ } catch (err) {
+ // Leave the row pending: a bucket read that failed once (a deleted
+ // object aside) is worth another tick, and the original is untouched.
+ failed++;
+ log.warn('variant_failed', { creation_id: row.id, message: err?.message });
+ }
+ }
+
+ let remaining = null;
+ try {
+ const [{ n }] = await sql`
+ select count(*)::int as n from forge_creations
+ where status = 'done' and glb_url is not null and glb_key is not null and web_glb_key is null
+ `;
+ remaining = n;
+ } catch {
+ remaining = null;
+ }
+
+ return json(res, 200, { ok: true, processed: rows.length, built, skipped, failed, remaining });
+});
diff --git a/package.json b/package.json
index 98662469c..57d51fcbe 100644
--- a/package.json
+++ b/package.json
@@ -316,6 +316,7 @@
"build:extension:prod": "node scripts/build-extension-icons.mjs && node scripts/build-extension.mjs --prod",
"optimize:glb": "node scripts/optimize-glb.mjs",
"compress:glbs": "node scripts/compress-glbs.mjs",
+ "vendor:meshopt": "node scripts/vendor-meshopt-decoder.mjs",
"fetch-animations": "node scripts/download-animations.mjs",
"fetch:mixamo-catalog": "node scripts/fetch-mixamo-catalog.mjs",
"mixamo:catalog": "node scripts/mixamo-all.mjs --catalog",
diff --git a/src/forge.js b/src/forge.js
index 0eb3b1b27..1b31ed221 100644
--- a/src/forge.js
+++ b/src/forge.js
@@ -1972,7 +1972,12 @@ function updateRefineButton() {
}
}
-function showResult(glbUrl, label, meta, { autoSaved = false } = {}) {
+// `glbUrl` is what the VIEWER loads and `downloadUrl` is what the Download
+// button hands over. They differ for a re-opened creation that has a delivery
+// variant (api/_lib/forge-store.js): the viewer takes the phone-sized meshopt +
+// WebP build, the download always takes the full-resolution original. For a
+// fresh generation there is only one URL and they are the same.
+function showResult(glbUrl, label, meta, { autoSaved = false, downloadUrl = null } = {}) {
stopElapsed();
// Every entry into the result state starts with no directed-prompt reveal:
// only a fresh generation whose response carried one re-opens it (run()
@@ -2032,7 +2037,7 @@ function showResult(glbUrl, label, meta, { autoSaved = false } = {}) {
els.materialize.href = `/materialize${handle}`;
els.materialize.hidden = false;
}
- els.download.href = glbUrl;
+ els.download.href = downloadUrl || glbUrl;
els.download.setAttribute(
'download',
`${
@@ -2296,14 +2301,19 @@ function removeCreationFromGallery(id, cell) {
function openCreation(c, { scroll = true } = {}) {
currentCreationId = c.id;
lastJob = { prompt: c.prompt || '', imageUrls: [] };
- showResult(c.glb_url, c.prompt || 'Forged model', {
- views_used: c.views_used,
- multiview: c.multiview,
- backend: c.backend,
- tier: c.tier,
- path: c.path,
- creatorUsername: c.creatorUsername || null,
- });
+ showResult(
+ c.web_glb_url || c.glb_url,
+ c.prompt || 'Forged model',
+ {
+ views_used: c.views_used,
+ multiview: c.multiview,
+ backend: c.backend,
+ tier: c.tier,
+ path: c.path,
+ creatorUsername: c.creatorUsername || null,
+ },
+ { downloadUrl: c.glb_url },
+ );
if (els.verdict) {
for (const b of els.verdict.querySelectorAll('button')) {
b.setAttribute('aria-pressed', String(b.dataset.outcome === c.outcome));