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): retry the pump.fun, Hyperliquid and agent-search reads that had one attempt and no memory
nirholasdab4b7945b9d
diff --git a/api/_lib/agent-achievements-data.js b/api/_lib/agent-achievements-data.jsindex db038c00e..cdd227d4c 100644--- a/api/_lib/agent-achievements-data.js+++ b/api/_lib/agent-achievements-data.js@@ -16,6 +16,7 @@import { sql } from './db.js';import { getRedis } from './redis.js';import { computeAchievements, isLaunchGraduated } from './agent-achievements.js';+import { pumpFetchJson } from './pump-feed-fetch.js';const PUMP_FRONTEND_BASE = 'https://frontend-api-v3.pump.fun';const CACHE_TTL_S = 120;@@ -28,12 +29,12 @@ const MARKET_LOOKUP_CAP = 30;/** Live pump.fun coin object for a mint, or null on any failure (best-effort). */async function fetchCoin(mint) {try {- const resp = await fetch(new URL(`/coins/${mint}`, PUMP_FRONTEND_BASE), {- headers: { accept: 'application/json' },- signal: AbortSignal.timeout(7000),- });+ // Called once per mint for up to thirty mints in a request, so a throttle+ // silently un-awards a batch of achievements. Retrying on the lane's own+ // Retry-After is what keeps that from happening on a busy launch day.+ const resp = await pumpFetchJson(String(new URL(`/coins/${mint}`, PUMP_FRONTEND_BASE)), { timeoutMs: 7000 });if (!resp.ok) return null;- return await resp.json();+ return resp.body;} catch {return null;}diff --git a/api/_lib/agent-tools.js b/api/_lib/agent-tools.jsindex 65da9d0a0..6912450f7 100644--- a/api/_lib/agent-tools.js+++ b/api/_lib/agent-tools.js@@ -17,6 +17,7 @@ import { solanaPublicConnection } from './agent-pumpfun.js';import { assessTradeSafety } from './trade-firewall.js';import { getSmartMoneyForMint } from './smart-money.js';import { resolveSnsName } from '../../src/solana/sns.js';+import { fetchUpstreamJson } from './upstream-fetch.js';const LAMPORTS_PER_SOL = 1_000_000_000;const BASE58_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;@@ -42,12 +43,19 @@ export const AGENT_TOOLS = {async handler(args) {const query = str(args?.query, 400);if (!query) throw new Error('query is required');- const res = await fetch(- `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_html=1`,- { signal: AbortSignal.timeout(8000) },- );- if (!res.ok) throw new Error(`search upstream ${res.status}`);- const d = await res.json();+ // A tool that throws ends the agent's turn, so one blip on a free keyless+ // endpoint costs the whole answer. Retry, then degrade to an empty+ // result the model can narrate around.+ let d;+ try {+ d = await fetchUpstreamJson(+ `https://api.duckduckgo.com/?q=${encodeURIComponent(query)}&format=json&no_html=1`,+ {},+ { name: 'duckduckgo:instant', timeoutMs: 8000, attempts: 2 },+ );+ } catch (err) {+ return { abstract: '', source: '', topics: [], unavailable: `search upstream unreachable: ${err?.message || err}` };+ }return {abstract: d.AbstractText || '',source: d.AbstractURL || '',diff --git a/api/_lib/hyperliquid.js b/api/_lib/hyperliquid.jsindex e90579f7f..2dcb2f508 100644--- a/api/_lib/hyperliquid.js+++ b/api/_lib/hyperliquid.js@@ -8,6 +8,8 @@// Verified reachable from US datacenter IPs (unlike Binance/Bybit/OKX, which// geo-block them; see the note in api/_lib/sol-price.js). No key, no account.+import { fetchUpstreamJson, lastGood } from './upstream-fetch.js';+const HL_INFO_URL = 'https://api.hyperliquid.xyz/info';const num = (v) => {@@ -21,18 +23,21 @@ const num = (v) => {* @param {{ timeoutMs?: number }} [opts]*/export async function hlInfo(body, { timeoutMs = 8000 } = {}) {- const res = await fetch(HL_INFO_URL, {- method: 'POST',- headers: { 'content-type': 'application/json', accept: 'application/json' },- body: JSON.stringify(body),- signal: AbortSignal.timeout(timeoutMs),- });- if (!res.ok) {- const err = new Error(`hyperliquid ${res.status}`);- err.status = res.status;- throw err;- }- return res.json();+ // One venue, no alternative reporting the same contracts, and a throw here+ // empties the derivatives view entirely. So: retry the transient half, and+ // keep the last good answer per query shape to ride out the rest. The body is+ // the cache key because each `type` is a different question.+ const key = `hyperliquid:${JSON.stringify(body).slice(0, 200)}`;+ const { value } = await lastGood(+ key,+ () => fetchUpstreamJson(HL_INFO_URL, {+ method: 'POST',+ headers: { 'content-type': 'application/json', accept: 'application/json' },+ body: JSON.stringify(body),+ }, { name: 'hyperliquid:info', timeoutMs, attempts: 2 }),+ { maxAgeMs: 10 * 60_000 },+ );+ return value;}/**diff --git a/api/_lib/pump-curve-view.js b/api/_lib/pump-curve-view.jsindex c506e5752..8fa1cceef 100644--- a/api/_lib/pump-curve-view.js+++ b/api/_lib/pump-curve-view.js@@ -16,6 +16,8 @@ import { rpcFallbackFromEnv, getBondingCurveState, getTokenPrice, getGraduationPimport { createCache } from './mem-cache.js';import { cacheGet, cacheSet } from './cache.js';import { hasThreeWsMark } from '../../src/solana/vanity/brand.js';+import { fetchUpstreamJson } from './upstream-fetch.js';+import { fetchTokenPriceUsd } from './market/token-market.js';// Mints that can never carry a pump.fun bonding curve. These are coin-agnostic// payment-rail / native tokens, listed only so we can *exclude* them from curve@@ -152,12 +154,19 @@ async function recallGood(network, mint) {async function jupiterPriceFallback(mint) {try {- const r = await fetch(`https://lite-api.jup.ag/price/v3?ids=${mint}`, { signal: AbortSignal.timeout(6000) });- if (!r.ok) return null;- const data = await r.json();+ const data = await fetchUpstreamJson(+ `https://lite-api.jup.ag/price/v3?ids=${mint}`,+ {},+ { name: 'jupiter:price', timeoutMs: 6000, attempts: 2 },+ );const usd = data?.[mint]?.usdPrice ?? data?.[mint]?.price;const n = Number(usd);- return Number.isFinite(n) && n > 0 ? { priceUsd: n, source: 'jupiter' } : null;+ if (Number.isFinite(n) && n > 0) return { priceUsd: n, source: 'jupiter' };+ // Jupiter has no price for this mint (or could not answer). The shared+ // market chain covers the mints Jupiter misses, which for a bonding-curve+ // token is most of the interesting ones.+ const alt = Number(await fetchTokenPriceUsd(mint));+ return Number.isFinite(alt) && alt > 0 ? { priceUsd: alt, source: 'market-chain' } : null;} catch {return null;}diff --git a/api/_mcp3d/text-to-image.js b/api/_mcp3d/text-to-image.jsindex 06a3e10de..d01f7a0ff 100644--- a/api/_mcp3d/text-to-image.js+++ b/api/_mcp3d/text-to-image.js@@ -22,6 +22,7 @@// Replicate backstop: black-forest-labs/flux-schnell — same family, $0.003/run.import { markProviderCooldown, providersInCooldown } from '../_lib/provider-health.js';+import { fetchUpstream } from '../_lib/upstream-fetch.js';import { reserveProviderRateSlot, SCALE_LIMITS } from '../_lib/forge-scale.js';import { persistImageBase64, persistImageBytes, looksLikeImageBytes } from '../_lib/image-persist.js';@@ -248,8 +249,14 @@ const HF_IMAGE_PROVIDERS = Object.freeze([extract: async (data, signal) => {const url = data?.images?.[0]?.url;if (!url || !/^https:\/\//.test(url)) throw new Error('fal-ai returned no image url');- const res = await fetch(url, { signal });- if (!res.ok) throw new Error(`fal-ai image fetch returned ${res.status}`);+ // The caller's signal cancels an abandoned request, but it does not bound+ // a hosted URL that accepts the connection and then stalls, so the+ // download carries its own deadline and one retry on top.+ const res = await fetchUpstream(+ url,+ { signal },+ { name: 'fal:image-download', timeoutMs: 30_000, attempts: 2 },+ );return Buffer.from(await res.arrayBuffer());},},diff --git a/api/cron/[name].js b/api/cron/[name].jsindex 862aeab35..fbdbea562 100644--- a/api/cron/[name].js+++ b/api/cron/[name].js@@ -42,6 +42,7 @@ import { sql } from '../_lib/db.js';import { matchedSlurStem } from '../_lib/display-name-safety.js';import { submitProtected } from '../_lib/execution-engine.js';import { cors, error, json, method, wrapCron } from '../_lib/http.js';+import { fetchUpstream } from '../_lib/upstream-fetch.js';import { env } from '../_lib/env.js';import { llmComplete } from '../_lib/llm.js';import { CHAINS } from '../_lib/erc8004-chains.js';@@ -4709,11 +4710,12 @@ async function handleFetchXMetrics(req, res) {// X allows up to 100 ids per /2/tweets lookup.const ids = posts.map((p) => p.tweet_id).join(',');- const r = await fetch(+ // Unbounded, this held the whole cron invocation open whenever X was slow,+ // and the loop below runs it once per account.+ const r = await fetchUpstream(`https://api.twitter.com/2/tweets?ids=${ids}&tweet.fields=public_metrics`,- {- headers: { authorization: `Bearer ${accessToken}` },- },+ { headers: { authorization: `Bearer ${accessToken}` } },+ { name: 'x:tweets-lookup', timeoutMs: 15_000, attempts: 2, okWhen: () => true },);if (!r.ok) {report.errors++;diff --git a/api/cron/vanity-inventory-replenish.js b/api/cron/vanity-inventory-replenish.jsindex a906df808..b049104e3 100644--- a/api/cron/vanity-inventory-replenish.js+++ b/api/cron/vanity-inventory-replenish.js@@ -29,6 +29,7 @@ import { json, method, wrapCron } from '../_lib/http.js';import { sendOpsAlert } from '../_lib/alerts.js';import { inventoryStats, sweepExpiredSecrets, isDbUnavailableError } from '../_lib/vanity-inventory-store.js';import { getGcpAccessToken, gcpAuthConfigured } from '../_lib/gcp-auth.js';+import { fetchUpstream } from '../_lib/upstream-fetch.js';import { requireCron } from '../_lib/cron-auth.js';// Below this many available items platform-wide, fire a replenishment run.@@ -71,11 +72,14 @@ function jobConfigured() {async function triggerGrindJob() {const token = await getGcpAccessToken();const url = `https://run.googleapis.com/v2/projects/${PROJECT}/locations/${REGION}/jobs/${JOB}:run`;- const res = await fetch(url, {+ // Cloud Run's Admin API is normally quick, but an unbounded POST inside a cron+ // tick can hold the whole invocation until the platform kills it, which reads+ // as a dead cron rather than a slow API call.+ const res = await fetchUpstream(url, {method: 'POST',headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },body: JSON.stringify({}),- });+ }, { name: 'gcp:jobs-run', timeoutMs: 20_000, attempts: 2, okWhen: () => true });if (!res.ok) {const detail = await res.text().catch(() => res.status);throw new Error(`Cloud Run Jobs run failed (${res.status}): ${String(detail).slice(0, 300)}`);diff --git a/api/pump/launch-detail.js b/api/pump/launch-detail.jsindex 28dec2957..0cea207b5 100644--- a/api/pump/launch-detail.js+++ b/api/pump/launch-detail.js@@ -34,6 +34,7 @@ import { publicUrl as r2PublicUrl } from '../_lib/r2.js';import { normalizeGatewayURL } from '../../src/ipfs.js';import { getTraderStats } from '../_lib/trader-stats.js';import { withBreaker } from '../_lib/resilience.js';+import { pumpFetchJson } from '../_lib/pump-feed-fetch.js';const MINT_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;const lamportsToSol = (v) => (v == null ? null : Number(BigInt(v)) / 1e9);@@ -68,21 +69,22 @@ const PUMP_SWAP_API = 'https://swap-api.pump.fun';async function fetchCreatorFees(mint, network) {if (network !== 'mainnet') return null;- const metaResp = await fetch(`${PUMP_FRONTEND_V3}/coins-v2/${mint}`, {- headers: { accept: 'application/json' },- signal: AbortSignal.timeout(5000),- });- if (!metaResp.ok) throw new Error(`pump.fun coin meta ${metaResp.status}`);- const meta = await metaResp.json();+ // pump.fun answers a burst with 429 far more often than it goes down, and a+ // bare fetch treated the two identically. pumpFetchJson honours Retry-After+ // and retries once, which is the difference between a launch page rendering+ // and a launch page erroring during someone else's mint.+ const metaRes = await pumpFetchJson(`${PUMP_FRONTEND_V3}/coins-v2/${mint}`, { timeoutMs: 5000 });+ if (!metaRes.ok) throw new Error(`pump.fun coin meta ${metaRes.status}`);+ const meta = metaRes.body;const creator = meta?.creator || meta?.creator_address;if (!creator || typeof creator !== 'string') return null; // valid: no creator on file- const totResp = await fetch(+ const totRes = await pumpFetchJson(`${PUMP_SWAP_API}/v1/fee-sharing/account/${creator}/totals?mint=${mint}`,- { headers: { accept: 'application/json' }, signal: AbortSignal.timeout(5000) },+ { timeoutMs: 5000 },);- if (!totResp.ok) throw new Error(`pump.fun fee-sharing ${totResp.status}`);- const t = await totResp.json();+ if (!totRes.ok) throw new Error(`pump.fun fee-sharing ${totRes.status}`);+ const t = totRes.body;const earnedSol = Number(t?.shareholderTotalEarned?.sol);if (!Number.isFinite(earnedSol)) return null; // valid: creator has no fee-sharing earningsreturn {diff --git a/api/pump/launch-og.js b/api/pump/launch-og.jsindex 4ddfdf851..ba6ea08dd 100644--- a/api/pump/launch-og.js+++ b/api/pump/launch-og.js@@ -23,6 +23,7 @@import { cors, method, wrap } from '../_lib/http.js';import { sql } from '../_lib/db.js';+import { pumpFetchJson } from '../_lib/pump-feed-fetch.js';const MINT_RE = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;const PUMP_FRONTEND_V3 = 'https://frontend-api-v3.pump.fun';@@ -150,12 +151,12 @@ async function buildCardData(mint) {// the card falls back to the recorded outcome numbers, or omits the block.async function fetchLiveMarketCap(mint) {try {- const r = await fetch(`${PUMP_FRONTEND_V3}/coins-v2/${mint}`, {- headers: { accept: 'application/json' },- signal: AbortSignal.timeout(3000),- });+ // An OG card is rendered by a crawler that will not come back, so a 429+ // here permanently ships a card with no market cap on it. One retry costs+ // a few hundred milliseconds and saves the card.+ const r = await pumpFetchJson(`${PUMP_FRONTEND_V3}/coins-v2/${mint}`, { timeoutMs: 3000 });if (!r.ok) return null;- const d = await r.json();+ const d = r.body;const mcap = Number(d?.usd_market_cap ?? d?.market_cap_usd);return Number.isFinite(mcap) && mcap > 0 ? mcap : null;} catch {