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.

feat(play): forge a prop from a prompt and place it in the world

nirholasd6e4b72fa690
diff --git a/src/game/forge-prop.js b/src/game/forge-prop.js
new file mode 100644
index 000000000..973500bcb
--- /dev/null
+++ b/src/game/forge-prop.js
@@ -0,0 +1,187 @@
+// Forge-in-world: turn a text prompt (or a reference photo) into a placeable
+// GLB prop without leaving /play. This module is the network half only; it
+// drives the same free draft lane the /forge page uses (POST /api/forge →
+// poll GET /api/forge?job=) and resolves to a durable R2-hosted GLB URL that
+// the multiplayer server's asset allow-list accepts, so the finished model can
+// ride the existing obj:spawn channel and appear for everyone in the world.
+// The UI/placement half lives in coincommunities.js (_forgeProp) and reuses
+// the uploaded-prop pipeline (world-objects.js registerUploadedProp).
+
+import { log } from '../shared/log.js';
+
+const SUBMIT_URL = '/api/forge';
+const UPLOAD_URL = '/api/forge-upload';
+const POLL_MS = 2500;
+// Draft-tier generations land in ~30-60s; leave generous headroom for a cold
+// GPU start (the submit response advertises 90s of cold-start alone).
+const MAX_WAIT_MS = 6 * 60 * 1000;
+const IMAGE_TYPES = new Set(['image/png', 'image/jpeg', 'image/webp']);
+const MAX_IMAGE_BYTES = 8 * 1024 * 1024; // mirrors /api/forge-upload's cap
+
+// Same anonymous handle the /forge page keys creations to, so items forged
+// in-world show up in the same gallery/history as the rest of this browser's
+// forges instead of forming a disconnected second identity.
+function forgeClientId() {
+ const KEY = 'forge:cid';
+ try {
+ let id = localStorage.getItem(KEY);
+ if (!id) {
+ id =
+ crypto?.randomUUID?.() ||
+ `c-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
+ localStorage.setItem(KEY, id);
+ }
+ return id;
+ } catch {
+ return `c-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`;
+ }
+}
+
+function headers(extra = {}) {
+ return { 'x-forge-client': forgeClientId(), ...extra };
+}
+
+// A short palette-friendly display name for the forged prop.
+export function forgePropName(prompt, file) {
+ const base = String(prompt || '').trim() || String(file?.name || '').replace(/\.[a-z0-9]+$/i, '');
+ return (base || 'Forged item').slice(0, 24);
+}
+
+const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
+
+function throwIfAborted(signal) {
+ if (signal?.aborted) throw new ForgeError('Forge cancelled.', { cancelled: true });
+}
+
+/** Error with user-facing message plus machine flags (rateLimited/cancelled). */
+export class ForgeError extends Error {
+ constructor(message, { rateLimited = false, cancelled = false } = {}) {
+ super(message);
+ this.name = 'ForgeError';
+ this.rateLimited = rateLimited;
+ this.cancelled = cancelled;
+ }
+}
+
+async function readJson(res) {
+ try { return await res.json(); } catch { return {}; }
+}
+
+function submitError(res, data) {
+ if (res.status === 429) {
+ return new ForgeError('The forge is busy right now. Give it a minute and try again.', { rateLimited: true });
+ }
+ const detail = typeof data?.error === 'string' && data.error.length < 200 ? data.error : '';
+ return new ForgeError(detail ? `The forge refused that: ${detail}` : 'The forge could not take that request.');
+}
+
+// Push the reference image into forge storage via the presigned-PUT path the
+// /forge page uses, returning a public URL /api/forge accepts in image_urls.
+async function uploadReferenceImage(file, { onStatus, signal } = {}) {
+ if (!IMAGE_TYPES.has(file.type)) {
+ throw new ForgeError('Reference images must be a PNG, JPEG, or WebP.');
+ }
+ if (file.size > MAX_IMAGE_BYTES) {
+ throw new ForgeError('That image is over 8 MB. Shrink it and try again.');
+ }
+ onStatus?.('Uploading your reference image…');
+ const presign = await fetch(UPLOAD_URL, {
+ method: 'POST',
+ headers: headers({ 'content-type': 'application/json' }),
+ body: JSON.stringify({ content_type: file.type, size_bytes: file.size }),
+ signal,
+ });
+ const grant = await readJson(presign);
+ if (!presign.ok || !grant.upload_url || !grant.public_url) {
+ if (presign.status === 503) {
+ throw new ForgeError('Image uploads are unavailable right now. Try a text prompt instead.');
+ }
+ throw submitError(presign, grant);
+ }
+ const put = await fetch(grant.upload_url, {
+ method: grant.method || 'PUT',
+ headers: grant.headers || { 'content-type': file.type },
+ body: file,
+ signal,
+ });
+ if (!put.ok) throw new ForgeError('The image upload failed. Check your connection and try again.');
+ return grant.public_url;
+}
+
+function statusLine(state, etaSeconds) {
+ const eta = Number.isFinite(etaSeconds) && etaSeconds > 0 ? ` (~${Math.round(etaSeconds)}s)` : '';
+ return state === 'queued' ? `Forging: waiting for a slot${eta}…` : `Forging your model${eta}…`;
+}
+
+/**
+ * Generate a placeable 3D prop from a prompt and/or reference image.
+ * Resolves once the free draft lane finishes and the GLB has a stable URL.
+ *
+ * @param {object} req
+ * @param {string} [req.prompt] what to forge (3+ chars; required without a file)
+ * @param {File} [req.file] optional reference image (png/jpeg/webp ≤ 8 MB)
+ * @param {(msg: string) => void} [req.onStatus] live progress line for the UI
+ * @param {AbortSignal} [req.signal]
+ * @returns {Promise<{url: string, name: string, durable: boolean, creationId: string|null}>}
+ * @throws {ForgeError} with a user-facing message on every failure path
+ */
+export async function forgeWorldProp({ prompt = '', file = null, onStatus, signal } = {}) {
+ const text = String(prompt || '').trim();
+ if (!file && text.length < 3) {
+ throw new ForgeError('Describe the item in a few words (or attach a photo).');
+ }
+
+ const body = { tier: 'draft', path: 'image' };
+ if (file) {
+ body.image_urls = [await uploadReferenceImage(file, { onStatus, signal })];
+ if (text) body.prompt = text;
+ } else {
+ body.prompt = text;
+ }
+
+ throwIfAborted(signal);
+ onStatus?.('Sending to the forge…');
+ const res = await fetch(SUBMIT_URL, {
+ method: 'POST',
+ headers: headers({ 'content-type': 'application/json' }),
+ body: JSON.stringify(body),
+ signal,
+ });
+ const data = await readJson(res);
+ if (!res.ok) throw submitError(res, data);
+
+ const name = forgePropName(text, file);
+ if (data.status === 'done' && data.glb_url) {
+ return { url: data.glb_url, name, durable: data.durable !== false, creationId: data.creation_id || null };
+ }
+ if (!data.job_id) throw new ForgeError('The forge did not accept that request. Try rephrasing it.');
+
+ onStatus?.(statusLine('queued', data.eta_seconds));
+ const deadline = Date.now() + MAX_WAIT_MS;
+ while (Date.now() < deadline) {
+ throwIfAborted(signal);
+ await sleep(POLL_MS);
+ throwIfAborted(signal);
+ let poll;
+ try {
+ poll = await fetch(`${SUBMIT_URL}?job=${encodeURIComponent(data.job_id)}`, { headers: headers(), signal });
+ } catch (err) {
+ if (signal?.aborted) throw new ForgeError('Forge cancelled.', { cancelled: true });
+ // A dropped poll is not a dropped job: the generation keeps running
+ // server-side, so ride out transient network blips and poll again.
+ log.warn('[forge-prop] poll transport error, retrying:', err?.message || err);
+ continue;
+ }
+ if (poll.status === 429) { onStatus?.('Forging: easing off the status checks…'); await sleep(POLL_MS * 2); continue; }
+ const job = await readJson(poll);
+ if (job.status === 'done' && job.glb_url) {
+ return { url: job.glb_url, name, durable: job.durable !== false, creationId: job.creation_id || null };
+ }
+ if (job.status === 'failed') {
+ const detail = typeof job.error === 'string' && job.error.length < 200 ? job.error : '';
+ throw new ForgeError(detail ? `The forge could not build that: ${detail}` : 'The forge could not build that. Try different wording.');
+ }
+ onStatus?.(statusLine(job.status === 'queued' ? 'queued' : 'running', job.eta_seconds));
+ }
+ throw new ForgeError('The forge is taking too long. Your item may still finish in the /forge gallery.');
+}
diff --git a/tests/forge-prop.test.js b/tests/forge-prop.test.js
new file mode 100644
index 000000000..b2b936c5a
--- /dev/null
+++ b/tests/forge-prop.test.js
@@ -0,0 +1,152 @@
+// Forge-in-world network client (src/game/forge-prop.js): the /play bridge
+// from "prompt or photo" to a durable GLB URL the multiplayer asset allow-list
+// accepts. These tests pin the wire contract against /api/forge:
+//
+// 1. A text forge submits {prompt, tier:'draft', path:'image'} with the
+// x-forge-client identity header, polls the job, and resolves the glb_url.
+// 2. A cache hit (submit answers status:'done' inline) never polls.
+// 3. A failed job surfaces the server's error in a user-facing ForgeError.
+// 4. A 429 submit reports the rate limit as retryable, not as a crash.
+// 5. An image forge presigns via /api/forge-upload, PUTs the bytes, and
+// submits the granted public_url in image_urls.
+// 6. An empty prompt with no image is refused before any network call.
+
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
+import { forgeWorldProp, forgePropName, ForgeError } from '../src/game/forge-prop.js';
+
+const GLB_URL = 'https://pub-abc.r2.dev/forge/f1/model.glb';
+
+function jsonResponse(status, body) {
+ return {
+ ok: status >= 200 && status < 300,
+ status,
+ json: async () => body,
+ };
+}
+
+let fetchMock;
+
+beforeEach(() => {
+ vi.useFakeTimers();
+ fetchMock = vi.fn();
+ vi.stubGlobal('fetch', fetchMock);
+ // Node has no localStorage: the client id helper must fall back cleanly.
+ vi.stubGlobal('localStorage', {
+ _m: new Map(),
+ getItem(k) { return this._m.get(k) ?? null; },
+ setItem(k, v) { this._m.set(k, v); },
+ });
+});
+
+afterEach(() => {
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+});
+
+// Drive a forgeWorldProp call and the fake timers together until it settles.
+async function settle(promise) {
+ let done = false;
+ let result;
+ let error;
+ promise.then((r) => { done = true; result = r; }, (e) => { done = true; error = e; });
+ for (let i = 0; i < 400 && !done; i++) {
+ await vi.advanceTimersByTimeAsync(2500);
+ }
+ if (!done) throw new Error('forgeWorldProp never settled under fake timers');
+ if (error) throw error;
+ return result;
+}
+
+describe('forgeWorldProp (text prompt)', () => {
+ it('submits the draft-tier request, polls, and resolves the finished GLB', async () => {
+ fetchMock
+ .mockResolvedValueOnce(jsonResponse(200, { status: 'queued', job_id: 'job123', eta_seconds: 5 }))
+ .mockResolvedValueOnce(jsonResponse(200, { status: 'running', job_id: 'job123' }))
+ .mockResolvedValueOnce(jsonResponse(200, { status: 'done', job_id: 'job123', glb_url: GLB_URL, durable: true, creation_id: 'c1' }));
+
+ const statuses = [];
+ const out = await settle(forgeWorldProp({ prompt: 'a glowing campfire', onStatus: (m) => statuses.push(m) }));
+
+ expect(out.url).toBe(GLB_URL);
+ expect(out.durable).toBe(true);
+ expect(out.creationId).toBe('c1');
+ expect(out.name).toBe('a glowing campfire');
+
+ const [submitUrl, submitInit] = fetchMock.mock.calls[0];
+ expect(submitUrl).toBe('/api/forge');
+ expect(submitInit.method).toBe('POST');
+ expect(submitInit.headers['x-forge-client']).toBeTruthy();
+ expect(JSON.parse(submitInit.body)).toEqual({ tier: 'draft', path: 'image', prompt: 'a glowing campfire' });
+
+ expect(fetchMock.mock.calls[1][0]).toBe('/api/forge?job=job123');
+ expect(statuses.some((s) => s.includes('Forging'))).toBe(true);
+ });
+
+ it('resolves a cache hit inline without polling', async () => {
+ fetchMock.mockResolvedValueOnce(jsonResponse(200, { status: 'done', glb_url: GLB_URL, durable: true, cached: true }));
+ const out = await settle(forgeWorldProp({ prompt: 'a wooden crate' }));
+ expect(out.url).toBe(GLB_URL);
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ });
+
+ it('surfaces a failed job as a user-facing ForgeError', async () => {
+ fetchMock
+ .mockResolvedValueOnce(jsonResponse(200, { status: 'queued', job_id: 'job123' }))
+ .mockResolvedValueOnce(jsonResponse(200, { status: 'failed', job_id: 'job123', error: 'nsfw prompt' }));
+ await expect(settle(forgeWorldProp({ prompt: 'something the lane refuses' })))
+ .rejects.toMatchObject({ name: 'ForgeError', message: expect.stringContaining('nsfw prompt') });
+ });
+
+ it('reports a 429 submit as rate limited', async () => {
+ fetchMock.mockResolvedValueOnce(jsonResponse(429, { error: 'rate_limited' }));
+ await expect(settle(forgeWorldProp({ prompt: 'a tenth teapot this hour' })))
+ .rejects.toMatchObject({ name: 'ForgeError', rateLimited: true });
+ });
+
+ it('refuses an empty request before any network call', async () => {
+ await expect(forgeWorldProp({ prompt: '' })).rejects.toBeInstanceOf(ForgeError);
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+});
+
+describe('forgeWorldProp (reference image)', () => {
+ it('presigns, uploads, and submits the granted public URL in image_urls', async () => {
+ const file = { name: 'chair.png', type: 'image/png', size: 1024 };
+ fetchMock
+ .mockResolvedValueOnce(jsonResponse(200, {
+ upload_url: 'https://r2.example/put/abc',
+ public_url: 'https://pub-abc.r2.dev/uploads/chair.png',
+ method: 'PUT',
+ headers: { 'content-type': 'image/png' },
+ }))
+ .mockResolvedValueOnce({ ok: true, status: 200, json: async () => ({}) })
+ .mockResolvedValueOnce(jsonResponse(200, { status: 'done', glb_url: GLB_URL, durable: true }));
+
+ const out = await settle(forgeWorldProp({ prompt: 'this chair', file }));
+ expect(out.url).toBe(GLB_URL);
+
+ expect(fetchMock.mock.calls[0][0]).toBe('/api/forge-upload');
+ expect(fetchMock.mock.calls[1][0]).toBe('https://r2.example/put/abc');
+ expect(fetchMock.mock.calls[1][1].method).toBe('PUT');
+ const submitBody = JSON.parse(fetchMock.mock.calls[2][1].body);
+ expect(submitBody.image_urls).toEqual(['https://pub-abc.r2.dev/uploads/chair.png']);
+ expect(submitBody.prompt).toBe('this chair');
+ });
+
+ it('rejects a non-image file before any network call', async () => {
+ const file = { name: 'model.glb', type: 'model/gltf-binary', size: 1024 };
+ await expect(settle(forgeWorldProp({ file }))).rejects.toMatchObject({
+ name: 'ForgeError', message: expect.stringContaining('PNG, JPEG, or WebP'),
+ });
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+});
+
+describe('forgePropName', () => {
+ it('prefers the prompt, falls back to the file name, and clamps length', () => {
+ expect(forgePropName('a glowing campfire', null)).toBe('a glowing campfire');
+ expect(forgePropName('', { name: 'red-chair.png' })).toBe('red-chair');
+ expect(forgePropName('x'.repeat(60), null)).toHaveLength(24);
+ expect(forgePropName('', null)).toBe('Forged item');
+ });
+});