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
fix(upstream): put a deadline on the last outbound calls that could still hang a request
nirholas1407949cba70
diff --git a/api/_lib/granite-guardian.js b/api/_lib/granite-guardian.jsindex 78a60b54e..d7867c41b 100644--- a/api/_lib/granite-guardian.js+++ b/api/_lib/granite-guardian.js@@ -26,6 +26,7 @@import { createHash } from 'node:crypto';import { watsonxConfig, watsonxToken } from './watsonx.js';+import { fetchUpstream } from './upstream-fetch.js';// The canonical Granite Guardian risk taxonomy. Keys are the model's risk_name// values; `definition` is the safety-risk text the model card injects for that@@ -221,7 +222,12 @@ export async function assessRisk(cfg, { risk, input, signal } = {}) {top_logprobs: 5,};- const res = await fetch(`${cfg.wx.url}/ml/v1/text/chat?version=${cfg.wx.apiVersion}`, {+ // A caller signal is optional here, so without a deadline of our own a stalled+ // watsonx connection would hold a safety check open indefinitely. The wrapper+ // composes the caller's signal with its own timeout, and hands back every+ // status untouched so the precise errors below are unchanged. Classification+ // is idempotent, so one transient failure is retried.+ const res = await fetchUpstream(`${cfg.wx.url}/ml/v1/text/chat?version=${cfg.wx.apiVersion}`, {method: 'POST',headers: {Authorization: `Bearer ${token}`,@@ -230,7 +236,7 @@ export async function assessRisk(cfg, { risk, input, signal } = {}) {},body: JSON.stringify(body),signal,- });+ }, { timeoutMs: 20_000, attempts: 2, retryUnsafe: true, okWhen: () => true, label: 'granite-guardian' });const textBody = await res.text();if (!res.ok) {throw new Error(`granite-guardian ${riskKey} failed (${res.status}): ${textBody.slice(0, 300)}`);diff --git a/api/_mcp/tools/animations.js b/api/_mcp/tools/animations.jsindex 219be6e1a..3a11729c2 100644--- a/api/_mcp/tools/animations.js+++ b/api/_mcp/tools/animations.js@@ -31,6 +31,7 @@ import {slotFit,} from '../../../src/runtime/motion-signature.js';import { SLOTS, DEFAULT_ANIMATION_MAP } from '../../../src/runtime/animation-slots.js';+import { fetchUpstream } from '../../_lib/upstream-fetch.js';// Largest base64-encoded GLB we'll inline in a JSON-RPC response before falling// back to the (small) clip JSON. Keeps responses sane for very heavy avatars.@@ -44,8 +45,22 @@ function rpcError(code, message, data) {}// ── Catalogue helpers ────────────────────────────────────────────────────────++// The animation catalogue is static JSON served from our own origin, but it is+// still an HTTP call that can hang: without a deadline one stalled read holds+// the whole MCP tool invocation open. Idempotent GETs, so a transient blip is+// retried. Every non-2xx is handed back untouched so each caller keeps throwing+// the precise message its callers already match on.+function catalogueFetch(url) {+ return fetchUpstream(url, { cache: 'no-store' }, {+ timeoutMs: 10_000,+ attempts: 3,+ okWhen: () => true,+ label: 'animation-catalogue',+ });+}async function loadManifest(origin) {- const res = await fetch(`${origin}/animations/manifest.json`, { cache: 'no-store' });+ const res = await catalogueFetch(`${origin}/animations/manifest.json`);if (!res.ok) throw new Error(`manifest fetch failed (HTTP ${res.status})`);const manifest = await res.json();if (!Array.isArray(manifest) || !manifest.length)@@ -54,7 +69,7 @@ async function loadManifest(origin) {}async function loadSignatures(origin) {- const res = await fetch(`${origin}/animations/signatures.json`, { cache: 'no-store' });+ const res = await catalogueFetch(`${origin}/animations/signatures.json`);if (!res.ok) throw new Error(`signatures fetch failed (HTTP ${res.status})`);const index = await res.json();if (!index?.clips || !Object.keys(index.clips).length) throw new Error('signature index is empty');@@ -72,7 +87,7 @@ function signatureFor(index, clip) {async function loadClipJSON(origin, def) {// def.url is a site-relative path like /animations/clips/idle.json- const res = await fetch(`${origin}${def.url}`, { cache: 'no-store' });+ const res = await catalogueFetch(`${origin}${def.url}`);if (!res.ok) throw new Error(`clip fetch failed (HTTP ${res.status})`);return res.json();}diff --git a/api/forever/inscribe.js b/api/forever/inscribe.jsindex a4e65188b..9af895e6e 100644--- a/api/forever/inscribe.js+++ b/api/forever/inscribe.js@@ -25,6 +25,7 @@ import { bech32m } from '@scure/base';import { cors, error, json, readJson, rateLimited } from '../_lib/http.js';import { getSessionUser, authenticateBearer, extractBearer } from '../_lib/auth.js';import { limits, clientIp } from '../_lib/rate-limit.js';+import { fetchUpstream } from '../_lib/upstream-fetch.js';const ORDINALSBOT_BASE_URL =process.env.ORDINALSBOT_BASE_URL || 'https://api.ordinalsbot.com';@@ -145,11 +146,15 @@ async function createOrdinalsBotOrder({ message, receiveAddress, feeRate }) {if (process.env.ORDINALSBOT_API_KEY) {headers['x-api-key'] = process.env.ORDINALSBOT_API_KEY;}- const res = await fetch(`${ORDINALSBOT_BASE_URL}/order`, {+ // Placing an order is a paid, non-idempotent action: bound it with a deadline+ // but never retry it, because a retry after a request that actually landed+ // would buy a second inscription. Every status is returned untouched so the+ // existing non-JSON and error-status handling below is unchanged.+ const res = await fetchUpstream(`${ORDINALSBOT_BASE_URL}/order`, {method: 'POST',headers,body: JSON.stringify(body),- });+ }, { timeoutMs: 30_000, attempts: 1, okWhen: () => true, label: 'ordinalsbot-order' });const text = await res.text();let data;try {diff --git a/api/x402/fact-check.js b/api/x402/fact-check.jsindex 67f745cce..b3f262afd 100644--- a/api/x402/fact-check.js+++ b/api/x402/fact-check.js@@ -32,6 +32,7 @@ import { generateSearchQueries, analyzeResults } from '../../agents/fact-checkerimport { searchAll } from '../../agents/fact-checker/src/search-sources.js';import { authorityScore } from '../../agents/fact-checker/src/source-authority.js';import { imageEvidence } from '../../agents/fact-checker/src/image-evidence.js';+import { fetchUpstream } from '../_lib/upstream-fetch.js';const ROUTE = '/api/x402/fact-check';const CACHE_TTL_SECONDS = 7 * 24 * 60 * 60; // 7 days@@ -76,9 +77,12 @@ async function redisGet(key) {const { url, token } = getRedisCredentials();if (!url || !token) return null;try {- const r = await fetch(`${url}/get/${encodeURIComponent(key)}`, {+ // This is a PAID endpoint: a stalled cache read must never hold the+ // caller's request open. The cache is an optimisation, so a slow or dead+ // Upstash degrades to a miss (the catch below) rather than to a hang.+ const r = await fetchUpstream(`${url}/get/${encodeURIComponent(key)}`, {headers: { authorization: `Bearer ${token}` },- });+ }, { timeoutMs: 2_000, attempts: 2, label: 'fact-check-cache-get' });const d = await r.json();return d.result ? JSON.parse(d.result) : null;} catch {@@ -93,11 +97,11 @@ async function redisSet(key, value, ttlSeconds) {// Upstash REST: the raw request body IS the stored value; TTL goes in the// query string. A JSON envelope body would be stored verbatim and corrupt// every subsequent read.- await fetch(`${url}/set/${encodeURIComponent(key)}?EX=${ttlSeconds}`, {+ await fetchUpstream(`${url}/set/${encodeURIComponent(key)}?EX=${ttlSeconds}`, {method: 'POST',headers: { authorization: `Bearer ${token}` },body: JSON.stringify(value),- });+ }, { timeoutMs: 2_000, attempts: 1, label: 'fact-check-cache-set' });} catch {// Cache write failure is non-fatal.}