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.
JavaScript 3d-generationai-agentsanimationavatardigital-humangenerative-aiglbgltfimage-to-3dllmmcpmcp-servermodel-context-protocolsolanatext-to-3dthreejsweb-componentswebglwebxrx402
Refine NIM forge endpoint and studio MCP tool; add forge-nim tests
nirholas6e795bd2ac15
diff --git a/api/_mcp3d/tools/studio.js b/api/_mcp3d/tools/studio.jsindex 94ea9d058..bb0ec15fc 100644--- a/api/_mcp3d/tools/studio.js+++ b/api/_mcp3d/tools/studio.js@@ -336,27 +336,31 @@ async function submitGeometryJob({} else if (typeof gp.textToGeometry === 'function') {submitted = await gp.textToGeometry({ prompt, tier });} else {- // Image-only backend (e.g. Stable Fast 3D) asked to run text→3D.+ // Image-only engine asked to run text→3D.return {content: [{type: 'text',- text: `${BACKENDS[backendId].label} reconstructs from a reference image — use image_to_3d with this backend, or drop it to run text→3D.`,+ text: `The ${engineLabelFor(backendId)} reconstructs from a reference image — use image_to_3d with this engine, or drop it to run text→3D.`,},],- structuredContent: { status: 'failed', error: 'backend_image_only', backend: backendId },+ structuredContent: {+ status: 'failed',+ error: 'engine_image_only',+ engine: engineIdFor(backendId),+ },isError: true,};}- // Synchronous completion (Stable Fast 3D): the GLB is already persisted to R2,- // so there is no job to poll — return it done.+ // Synchronous completion (the instant single-image engine): the GLB is already+ // persisted to R2, so there is no job to poll — return it done.if (!submitted.taskId && submitted.resultGlbUrl) {return {content: [{type: 'text',- text: `Generated a 3D model on ${BACKENDS[backendId].label} (${path} path, ${tier.id} tier).\nGLB: ${submitted.resultGlbUrl}`,+ text: `Generated a 3D model on the ${engineLabelFor(backendId)} (${path} path, ${tier.id} tier).\nGLB: ${submitted.resultGlbUrl}`,},],structuredContent: {@@ -365,7 +369,7 @@ async function submitGeometryJob({mode: isImageMode ? 'image_to_3d' : 'text_to_3d',path,tier: tier.id,- backend: backendId,+ engine: engineIdFor(backendId),prompt: prompt || null,source_image_url: isImageMode ? primaryImage : null,},@@ -383,7 +387,7 @@ async function submitGeometryJob({{type: 'text',text:- `Started ${isImageMode ? 'image-to-3D' : 'text-to-3D'} on ${BACKENDS[backendId].label} ` ++ `Started ${isImageMode ? 'image-to-3D' : 'text-to-3D'} on the ${engineLabelFor(backendId)} ` +`(${path} path, ${tier.id} tier).\nJob ID: ${token}\n${POLL_HINT}`,},],@@ -393,7 +397,7 @@ async function submitGeometryJob({mode: isImageMode ? 'image_to_3d' : 'text_to_3d',path,tier: tier.id,- backend: backendId,+ engine: engineIdFor(backendId),prompt: prompt || null,source_image_url: isImageMode ? primaryImage : null,eta_seconds: estimateEtaSeconds({ backendId, tier }),@@ -516,7 +520,7 @@ export const toolDefs = [title: 'Generate a 3D model from a text prompt',annotations: GENERATIVE_ANNOTATIONS,description:- 'Turn a text description into a textured 3D model (GLB). Runs a fast text-to-image pass, then reconstructs a mesh from that image with Microsoft TRELLIS. Returns a job_id (poll with generation_status) plus the intermediate preview image. Best results: a single, clearly described object — "a worn leather armchair", "a low-poly red fox", "a sci-fi helmet".',+ 'Turn a text description into a textured 3D model (GLB). Runs a fast text-to-image pass, then reconstructs a mesh from that image with the three.ws image engine. Returns a job_id (poll with generation_status) plus the intermediate preview image. Best results: a single, clearly described object — "a worn leather armchair", "a low-poly red fox", "a sci-fi helmet".',inputSchema: {type: 'object',properties: {@@ -604,7 +608,7 @@ export const toolDefs = [title: 'Reconstruct a 3D model from one or more images',annotations: GENERATIVE_ANNOTATIONS,description:- 'Reconstruct a textured 3D model (GLB) from a reference image using Microsoft TRELLIS. Pass a single image_url, or image_urls (2–4 views of the SAME object from different angles — front/back/left/right) for multi-view reconstruction, which removes the back-of-object hallucination of single-image reconstruction. Returns a job_id to poll with generation_status, plus how many views were fused and which backend handled it. The cleaner the inputs — one subject, plain background, even lighting — the better the mesh.',+ 'Reconstruct a textured 3D model (GLB) from a reference image using the three.ws image engine. Pass a single image_url, or image_urls (2–4 views of the SAME object from different angles — front/back/left/right) for multi-view reconstruction, which removes the back-of-object hallucination of single-image reconstruction. Returns a job_id to poll with generation_status, plus how many views were fused and which engine handled it. The cleaner the inputs — one subject, plain background, even lighting — the better the mesh.',inputSchema: {type: 'object',properties: {diff --git a/api/forge-nim.js b/api/forge-nim.jsindex 03f08631b..221478cf8 100644--- a/api/forge-nim.js+++ b/api/forge-nim.js@@ -251,8 +251,11 @@ async function infer(req, res) {let payload;if (mode === 'text') {- const prompt = shapePrompt(body?.prompt);- if (!prompt) return error(res, 400, 'no_prompt', 'Describe the object in a few words first.');+ const rawPrompt = typeof body?.prompt === 'string' ? body.prompt.trim() : '';+ if (rawPrompt.length < 2) {+ return error(res, 400, 'no_prompt', 'Describe the object in a few words first.');+ }+ const prompt = shapePrompt(rawPrompt);payload = {mode: 'text',prompt,diff --git a/tests/api/forge-nim.test.js b/tests/api/forge-nim.test.jsnew file mode 100644index 000000000..11478f594--- /dev/null+++ b/tests/api/forge-nim.test.js@@ -0,0 +1,273 @@+// api/forge-nim — the self-hosted TRELLIS NIM demo proxy.+//+// This endpoint backs /forge-nim: it forwards a real photo (or text prompt) to a+// self-hosted NIM's POST /v1/infer and normalizes the synchronous+// { artifacts:[{ base64 }] } GLB straight back to the browser. The tests pin the+// contract that matters: the NIM URL is env-gated, caller-supplied baseUrl is+// SSRF-guarded, every documented artifact shape decodes to GLB bytes, and upstream+// failures surface as actionable boundary errors. fetch + rate-limit are mocked so+// the suite runs fully offline.++import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';+import { Readable } from 'node:stream';++vi.mock('../../api/_lib/rate-limit.js', () => ({+ limits: { forgeNim: async () => ({ success: true }) },+ clientIp: () => '127.0.0.1',+}));++const ORIGINAL_FETCH = globalThis.fetch;+const ENV_KEYS = ['MODEL_TRELLIS_URL', 'NVIDIA_API_KEY'];+const saved = {};++beforeEach(() => {+ for (const k of ENV_KEYS) {+ saved[k] = process.env[k];+ delete process.env[k];+ }+});+afterEach(() => {+ for (const k of ENV_KEYS) {+ if (saved[k] === undefined) delete process.env[k];+ else process.env[k] = saved[k];+ }+ globalThis.fetch = ORIGINAL_FETCH;+ vi.restoreAllMocks();+});++// Minimal binary glTF buffer — opaque bytes after a valid 12-byte header.+function fakeGlb(payload = 64) {+ const h = Buffer.alloc(12);+ h.writeUInt32LE(0x46546c67, 0); // 'glTF'+ h.writeUInt32LE(2, 4);+ h.writeUInt32LE(12 + payload, 8);+ return Buffer.concat([h, Buffer.alloc(payload)]);+}++function makeReq({ method = 'POST', url = '/api/forge-nim', body = null } = {}) {+ const raw = body == null ? '' : JSON.stringify(body);+ const stream = Readable.from(raw ? [Buffer.from(raw)] : []);+ stream.method = method;+ stream.url = url;+ stream.headers = { 'content-type': 'application/json' };+ return stream;+}++function makeRes() {+ return {+ statusCode: 200,+ _h: {},+ writableEnded: false,+ headersSent: false,+ setHeader(k, v) {+ this._h[k.toLowerCase()] = v;+ },+ getHeader(k) {+ return this._h[k.toLowerCase()];+ },+ end(body) {+ this._body = body;+ this.writableEnded = true;+ },+ };+}++async function dispatch(req, res) {+ const mod = await import('../../api/forge-nim.js');+ await mod.default(req, res);+ return { res, body: res._body ? JSON.parse(res._body) : null };+}++// A JSON Response stub matching the subset of WHATWG Response the handler reads.+function jsonResponse(obj, { status = 200 } = {}) {+ return {+ ok: status >= 200 && status < 300,+ status,+ headers: { get: (h) => (h.toLowerCase() === 'content-type' ? 'application/json' : null) },+ json: async () => obj,+ text: async () => JSON.stringify(obj),+ };+}+function binaryResponse(buf, { status = 200, ct = 'model/gltf-binary' } = {}) {+ return {+ ok: status >= 200 && status < 300,+ status,+ headers: { get: (h) => (h.toLowerCase() === 'content-type' ? ct : null) },+ arrayBuffer: async () => buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength),+ text: async () => '[binary]',+ };+}++describe('GET /api/forge-nim?action=health', () => {+ it('reports unconfigured when no NIM URL is set', async () => {+ const { res, body } = await dispatch(makeReq({ method: 'GET', url: '/api/forge-nim?action=health' }), makeRes());+ expect(res.statusCode).toBe(200);+ expect(body.configured).toBe(false);+ expect(body.reachable).toBe(false);+ });++ it('reports live when the configured NIM ready check passes', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ const fetchMock = vi.fn(async () => jsonResponse({ status: 'ready' }));+ globalThis.fetch = fetchMock;+ const { body } = await dispatch(makeReq({ method: 'GET', url: '/api/forge-nim?action=health' }), makeRes());+ expect(body.configured).toBe(true);+ expect(body.reachable).toBe(true);+ expect(body.baseUrl).toBe('https://nim.example.run.app');+ expect(fetchMock.mock.calls[0][0]).toBe('https://nim.example.run.app/v1/health/ready');+ });++ it('reports down when the ready check is unreachable', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ globalThis.fetch = vi.fn(async () => {+ throw new Error('ECONNREFUSED');+ });+ const { body } = await dispatch(makeReq({ method: 'GET', url: '/api/forge-nim?action=health' }), makeRes());+ expect(body.configured).toBe(true);+ expect(body.reachable).toBe(false);+ expect(body.detail).toMatch(/unreachable/i);+ });+});++describe('POST /api/forge-nim — image mode', () => {+ it('forwards a data-uri image to /v1/infer and returns the synchronous GLB', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app/';+ const glb = fakeGlb(96);+ const fetchMock = vi.fn(async () => jsonResponse({ artifacts: [{ base64: glb.toString('base64') }] }));+ globalThis.fetch = fetchMock;++ const { res, body } = await dispatch(+ makeReq({ body: { mode: 'image', tier: 'high', image: 'data:image/png;base64,iVBORw0KGgo=' } }),+ makeRes(),+ );++ expect(res.statusCode).toBe(200);+ expect(body.ok).toBe(true);+ expect(body.mode).toBe('image');+ expect(body.tier).toBe('high');+ expect(body.contract).toBe('artifacts[0].base64');+ expect(body.bytes).toBe(glb.length);+ expect(Buffer.from(body.glb_base64, 'base64').length).toBe(glb.length);+ expect(typeof body.ms).toBe('number');++ // Verify the wire contract sent to the NIM.+ const [url, init] = fetchMock.mock.calls[0];+ expect(url).toBe('https://nim.example.run.app/v1/infer');+ const sent = JSON.parse(init.body);+ expect(sent.mode).toBe('image');+ expect(sent.output_format).toBe('glb');+ expect(sent.ss_sampling_steps).toBe(40); // high tier+ expect(sent.image).toMatch(/^data:image\/png;base64,/);+ });++ it('decodes a raw binary (model/gltf-binary) NIM response', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ const glb = fakeGlb(48);+ globalThis.fetch = vi.fn(async () => binaryResponse(glb));+ const { body } = await dispatch(+ makeReq({ body: { mode: 'image', image: 'data:image/png;base64,iVBORw0KGgo=' } }),+ makeRes(),+ );+ expect(body.ok).toBe(true);+ expect(body.bytes).toBe(glb.length);+ });++ it('rejects image mode with no image', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ const { res, body } = await dispatch(makeReq({ body: { mode: 'image' } }), makeRes());+ expect(res.statusCode).toBe(400);+ expect(body.error || body.code).toBeTruthy();+ });+});++describe('POST /api/forge-nim — text mode', () => {+ it('shapes the prompt and posts a text invoke', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ const glb = fakeGlb(32);+ const fetchMock = vi.fn(async () => jsonResponse({ artifacts: [{ base64: glb.toString('base64') }] }));+ globalThis.fetch = fetchMock;+ const { body } = await dispatch(makeReq({ body: { mode: 'text', prompt: 'a tiny robot' } }), makeRes());+ expect(body.ok).toBe(true);+ const sent = JSON.parse(fetchMock.mock.calls[0][1].body);+ expect(sent.mode).toBe('text');+ expect(sent.prompt).toContain('a tiny robot');+ expect(sent.ss_sampling_steps).toBe(15); // default draft+ });++ it('rejects a too-short prompt', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ const { res } = await dispatch(makeReq({ body: { mode: 'text', prompt: 'a' } }), makeRes());+ expect(res.statusCode).toBe(400);+ });+});++describe('POST /api/forge-nim — configuration & SSRF', () => {+ it('returns 503 when no NIM is configured and none supplied', async () => {+ const { res, body } = await dispatch(+ makeReq({ body: { mode: 'image', image: 'data:image/png;base64,iVBORw0KGgo=' } }),+ makeRes(),+ );+ expect(res.statusCode).toBe(503);+ expect(body.error).toBe('nim_unconfigured');+ });++ it('blocks a private-network baseUrl override (SSRF guard)', async () => {+ const calls = [];+ globalThis.fetch = vi.fn(async (u) => {+ calls.push(u);+ return jsonResponse({ artifacts: [] });+ });+ for (const bad of ['https://10.0.0.1', 'https://192.168.1.10', 'https://169.254.169.254', 'http://nim.example.run.app']) {+ const { res } = await dispatch(+ makeReq({ body: { mode: 'image', image: 'data:image/png;base64,iVBORw0KGgo=', baseUrl: bad } }),+ makeRes(),+ );+ expect(res.statusCode).toBe(400);+ }+ expect(calls.length).toBe(0); // never reached the network+ });++ it('accepts a public https baseUrl override', async () => {+ const glb = fakeGlb(16);+ const fetchMock = vi.fn(async () => jsonResponse({ artifacts: [{ base64: glb.toString('base64') }] }));+ globalThis.fetch = fetchMock;+ const { body } = await dispatch(+ makeReq({+ body: { mode: 'image', image: 'data:image/png;base64,iVBORw0KGgo=', baseUrl: 'https://my-nim.example.com' },+ }),+ makeRes(),+ );+ expect(body.ok).toBe(true);+ expect(fetchMock.mock.calls[0][0]).toBe('https://my-nim.example.com/v1/infer');+ });+});++describe('POST /api/forge-nim — upstream failures', () => {+ it('maps a 401 from the NIM to an auth boundary error', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ globalThis.fetch = vi.fn(async () => ({+ ok: false,+ status: 401,+ headers: { get: () => 'application/json' },+ text: async () => 'unauthorized',+ json: async () => ({}),+ }));+ const { res, body } = await dispatch(+ makeReq({ body: { mode: 'image', image: 'data:image/png;base64,iVBORw0KGgo=' } }),+ makeRes(),+ );+ expect(res.statusCode).toBe(502);+ expect(body.error).toBe('nim_auth');+ });++ it('surfaces an empty-artifact response as a clean error', async () => {+ process.env.MODEL_TRELLIS_URL = 'https://nim.example.run.app';+ globalThis.fetch = vi.fn(async () => jsonResponse({ artifacts: [] }));+ const { res, body } = await dispatch(+ makeReq({ body: { mode: 'image', image: 'data:image/png;base64,iVBORw0KGgo=' } }),+ makeRes(),+ );+ expect(res.statusCode).toBe(502);+ expect(body.error).toBe('nim_no_artifact');+ });+});