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.

fix(forge-motion): stop AnimationClip.parse() from crashing on every generated clip

nirholas1705530bc671
diff --git a/api/_lib/motion-seed.js b/api/_lib/motion-seed.js
index 637799a58..fcba2a115 100644
--- a/api/_lib/motion-seed.js
+++ b/api/_lib/motion-seed.js
@@ -1674,5 +1674,10 @@ export function libraryReadyClip(workerClip) {
const rebased = needsRebase(workerClip) ? rebaseToCanonicalRest(workerClip).clip : workerClip;
const flattened = flattenRootDrift(despikeFootFlicks(rebased).clip).clip;
const clip = lockRootToContacts(flattened).clip;
- return { ...clip, userData: { ...(clip.userData || {}), basis: CLIP_BASIS } };
+ // This is the exact shape the client's AnimationClip.parse() reads (it's
+ // handed straight through as /api/forge-motion's `clip` field), and parse()
+ // passes userData to JSON.parse() unchanged, the same contract
+ // AnimationClip.toJSON() honors and every static library clip on disk already
+ // uses. An object here parses as the string "[object Object]" and throws.
+ return { ...clip, userData: JSON.stringify({ ...(clip.userData || {}), basis: CLIP_BASIS }) };
}
diff --git a/tests/api/forge-motion.test.js b/tests/api/forge-motion.test.js
index 74eeab4f8..260737cb4 100644
--- a/tests/api/forge-motion.test.js
+++ b/tests/api/forge-motion.test.js
@@ -157,7 +157,11 @@ describe('GET /api/forge-motion?job=', () => {
const res = makeRes();
await handler(makeReq({ method: 'GET', url: `/api/forge-motion?job=${'a'.repeat(24)}` }), res);
const body = JSON.parse(res.body);
- expect(body.clip.userData.basis).toBe('canonical-rest-v2');
+ // userData has to be a JSON string here, not an object: the client runs
+ // this clip straight through three.js's AnimationClip.parse(), which
+ // hands userData to JSON.parse() unchanged.
+ expect(typeof body.clip.userData).toBe('string');
+ expect(JSON.parse(body.clip.userData).basis).toBe('canonical-rest-v2');
const { forwardKinematicsFrame } = await import('../../api/_lib/motion-seed.js');
const w = forwardKinematicsFrame(body.clip, 0);
// Head over the neck and arms hanging, not thrown back and raised.
diff --git a/tests/motion-seed.test.js b/tests/motion-seed.test.js
index 533beafd2..bcf8e3abc 100644
--- a/tests/motion-seed.test.js
+++ b/tests/motion-seed.test.js
@@ -43,8 +43,10 @@ import {
forwardKinematicsFrame,
ROOT_LOCK,
LEGACY_CLIP_BASIS,
+ libraryReadyClip,
} from '../api/_lib/motion-seed.js';
import { CANONICAL_REST, CANONICAL_REST_WORLD, CANONICAL_PARENT } from '../src/animation-canonical-rest.js';
+import { AnimationClip } from 'three';
const BODY_BONES = [
'Hips', 'Spine', 'Spine1', 'Spine2', 'Neck', 'Head',
@@ -801,3 +803,25 @@ describe('source rest shape', () => {
expect(needsRebase({ userData: { basis: LEGACY_CLIP_BASIS } })).toBe(true);
});
});
+
+describe('libraryReadyClip', () => {
+ // AnimationClip.toJSON() always writes userData as a JSON string, and
+ // AnimationClip.parse() hands that string straight to JSON.parse(). A clip
+ // served with an object-shaped userData instead crashes the client's parse
+ // with "[object Object]" is not valid JSON, so the generated clip never
+ // gets to preview. /api/forge-motion hands its GET response's `clip` field
+ // straight to AnimationClip.parse() on the client, so this is the exact
+ // contract libraryReadyClip's output has to satisfy.
+ it('produces a clip AnimationClip.parse() can read without throwing', () => {
+ const raw = buildClip();
+ const ready = libraryReadyClip(raw);
+ expect(() => AnimationClip.parse(ready)).not.toThrow();
+ });
+
+ it('stamps the current basis onto userData, round-trippable through parse', () => {
+ const ready = libraryReadyClip(buildClip());
+ expect(typeof ready.userData).toBe('string');
+ const parsed = AnimationClip.parse(ready);
+ expect(parsed.userData.basis).toBe(CLIP_BASIS);
+ });
+});