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(tty): align the terminal and preflight handlers with their page contracts

nirholasdc96627ee162
diff --git a/api/tty.js b/api/tty.js
index bef7948f9..c6a75bf36 100644
--- a/api/tty.js
+++ b/api/tty.js
@@ -14,6 +14,9 @@
// arrive progressively and start animating before the model has even finished
// sending, which is the difference between a demo and a trick.
+import { existsSync } from 'node:fs';
+import { join } from 'node:path';
+
import { createRenderer, loadModel } from '../packages/tty-3d/src/index.js';
import { ansi, ColorMode } from '../packages/tty-3d/src/term.js';
import { getAvatar } from './_lib/avatars.js';
@@ -25,7 +28,21 @@ export const maxDuration = 60;
// The signature model, used when the caller names nothing. It is the one GLB
// every deploy is guaranteed to ship, so `curl three.ws/tty` can never 404.
-const DEFAULT_MODEL = '/avatars/default.glb';
+//
+// Read off disk, NOT fetched from our own origin. Self-fetching looked simpler
+// and was wrong twice over: it has to guess its own scheme from headers (which
+// fails outright behind a proxy that does not set x-forwarded-proto, and failed
+// immediately in local testing against https://localhost), and it spends a
+// network round trip to read a file that is already in the image.
+const DEFAULT_MODEL_CANDIDATES = ['dist/avatars/default.glb', 'public/avatars/default.glb'];
+
+export function defaultModelPath(cwd = process.cwd()) {
+ for (const rel of DEFAULT_MODEL_CANDIDATES) {
+ const full = join(cwd, rel);
+ if (existsSync(full)) return full;
+ }
+ return null;
+}
const MAX_FRAMES = 600;
const MAX_SECONDS = 45;
@@ -61,7 +78,7 @@ export function colorModeFor(query, headers = {}) {
}
/** Resolve what to draw: an explicit GLB URL, an avatar id, or the default. */
-export async function resolveSource(query, origin) {
+export async function resolveSource(query) {
if (query.src) {
// Caller-supplied URLs are fetched by this server, so they are an SSRF
// vector like any other. Same guard the render endpoints use.
@@ -77,7 +94,9 @@ export async function resolveSource(query, origin) {
if (!avatar.model_url) return null;
return { url: avatar.model_url, label: avatar.name || 'avatar' };
}
- return { url: new URL(DEFAULT_MODEL, origin).toString(), label: 'three.ws' };
+ const local = defaultModelPath();
+ if (!local) return null;
+ return { url: local, label: 'default agent' };
}
function banner(label, info, cols) {
@@ -105,16 +124,14 @@ export default wrap(async function handler(req, res) {
return rateLimited(res, rate, `Too many terminal renders. Limit: ${rate.limit} per 10m.`);
}
- const origin = `${req.headers['x-forwarded-proto'] || 'https'}://${req.headers.host || 'three.ws'}`;
-
let source;
try {
- source = await resolveSource(query, origin);
+ source = await resolveSource(query);
} catch (err) {
if (err instanceof SsrfBlockedError) return error(res, 400, 'blocked_url', 'that model URL is not reachable');
throw err;
}
- if (!source) return error(res, 404, 'not_found', 'no such avatar');
+ if (!source) return error(res, 404, 'not_found', 'no model to render');
const cols = clampInt(query.w ?? query.cols, MIN_COLS, MAX_COLS, 76);
const rows = clampInt(query.h ?? query.rows, MIN_ROWS, MAX_ROWS, 30);
diff --git a/api/x402/preflight.js b/api/x402/preflight.js
index a227eae49..acdbde59b 100644
--- a/api/x402/preflight.js
+++ b/api/x402/preflight.js
@@ -166,8 +166,12 @@ async function buildEnvelope({ ttl, endpointPath, origin }) {
}
export default wrap(async function handler(req, res) {
- if (cors(req, res, { methods: 'GET,OPTIONS' })) return;
- if (method(req, res, ['GET'])) return;
+ // Open to every origin on purpose. A browser-side agent, a third-party
+ // dashboard and the /preflight page all need to read this cross-origin, and an
+ // attestation is public, signed, and carries nothing secret. Locking it down
+ // would only stop the clients it exists to serve.
+ if (cors(req, res, { origins: '*', methods: 'GET,OPTIONS' })) return;
+ if (!method(req, res, ['GET'])) return;
const url = new URL(req.url, `https://${req.headers.host || 'three.ws'}`);
const ttl = Math.min(
diff --git a/src/portal/main.js b/src/portal/main.js
index 6dff4a764..0cb83f4cb 100644
--- a/src/portal/main.js
+++ b/src/portal/main.js
@@ -139,6 +139,9 @@ async function openWorld(rawUrl, { push = true } = {}) {
markStepsUpTo('render');
mountWorld(payload.world);
+ // A tiny read-only probe: the e2e spec walks the avatar and asserts the world
+ // actually moved under it, which no DOM assertion can see.
+ window.__portalPlayer = () => active?.player || null;
paintHud(payload.world, payload.outline, payload.cached);
show(els.loading, false);
}
@@ -173,10 +176,15 @@ function paintHud(world, outline, cached) {
els.siteMeta.append(span);
}
if (outline?.icon) {
+ // A site's declared icon is often missing or unreadable; the chip keeps its
+ // shape either way rather than showing a broken-image glyph.
+ els.siteIcon.onerror = () => els.siteIcon.classList.add('is-blank');
+ els.siteIcon.classList.remove('is-blank');
els.siteIcon.src = `/api/img?url=${encodeURIComponent(outline.icon)}`;
els.siteIcon.alt = '';
} else {
els.siteIcon.removeAttribute('src');
+ els.siteIcon.classList.add('is-blank');
}
show(els.site, true);
show(els.actions, true);