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.

move per-instance caches to Upstash Redis (onchain agent registry, x402 spot price)

nirholas91da6692455f
diff --git a/api/_lib/onchain.js b/api/_lib/onchain.js
index 479e2c8de..7fcd2054a 100644
--- a/api/_lib/onchain.js
+++ b/api/_lib/onchain.js
@@ -11,6 +11,7 @@
*/
import { JsonRpcProvider, Contract, getAddress } from 'ethers';
+import { cacheGet, cacheSet } from './cache.js';
const IDENTITY_ABI = [
'function tokenURI(uint256 tokenId) external view returns (string)',
@@ -208,8 +209,9 @@ export const SERVER_CHAIN_META = {
const IPFS_GATEWAY = 'https://ipfs.io/ipfs/';
const AR_GATEWAY = 'https://arweave.net/';
-const _cache = new Map();
-const CACHE_TTL_MS = 10 * 60 * 1000;
+// Shared via Upstash when configured — see api/_lib/cache.js. Falls back to
+// an in-process Map for local/dev. Either way, TTL is 10 minutes.
+const CACHE_TTL_S = 10 * 60;
/** @param {string} uri */
export function resolveURI(uri) {
@@ -253,9 +255,9 @@ export async function resolveOnChainAgent({
return _emptyResult(chainId, agentId, 'unsupported_chain');
}
- const cacheKey = `${chainId}:${agentId}`;
- const cached = _cache.get(cacheKey);
- if (cached && cached.expiresAt > Date.now()) return cached.result;
+ const cacheKey = `onchain-agent:${chainId}:${agentId}:${fetchManifest ? '1' : '0'}`;
+ const cached = await cacheGet(cacheKey);
+ if (cached) return cached;
const base = {
chainId,
@@ -318,7 +320,7 @@ export async function resolveOnChainAgent({
}
if (!base.error) {
- _cache.set(cacheKey, { result: base, expiresAt: Date.now() + CACHE_TTL_MS });
+ await cacheSet(cacheKey, base, CACHE_TTL_S);
}
return base;
}
diff --git a/api/_lib/x402-spending-price.js b/api/_lib/x402-spending-price.js
index 0a2e9094b..950775c3f 100644
--- a/api/_lib/x402-spending-price.js
+++ b/api/_lib/x402-spending-price.js
@@ -11,7 +11,9 @@
// We deliberately avoid `import { fetch }` — Node 18+ + browsers ship
// global fetch, and this module needs to run unchanged in both.
-const PRICE_TTL_MS = 5 * 60 * 1000;
+import { cacheGet, cacheSet } from './cache.js';
+
+const PRICE_TTL_S = 5 * 60;
const PRICE_API = 'https://api.coinbase.com/v2/prices';
// Tokens known to be 1:1 USD pegged. The asset address on the requirement
@@ -29,7 +31,9 @@ const USD_STABLECOINS = new Set([
// Decimals defaults — when accept.extra.decimals isn't present.
const DEFAULT_TOKEN_DECIMALS = 6;
-const priceCache = new Map();
+// Per-process micro-cache to short-circuit the Redis round-trip when the same
+// symbol is hit twice in one function invocation (e.g. multi-leg settlement).
+const localCache = new Map();
function isStablecoin(name) {
if (!name) return false;
@@ -41,8 +45,14 @@ function isStablecoin(name) {
// Cached for PRICE_TTL_MS.
async function fetchSpotUsd(symbol) {
const key = symbol.toUpperCase();
- const cached = priceCache.get(key);
- if (cached && cached.expiresAt > Date.now()) return cached.value;
+ const local = localCache.get(key);
+ if (local && local.expiresAt > Date.now()) return local.value;
+ const cacheKey = `spot-usd:${key}`;
+ const shared = await cacheGet(cacheKey);
+ if (typeof shared === 'number' && Number.isFinite(shared) && shared > 0) {
+ localCache.set(key, { value: shared, expiresAt: Date.now() + PRICE_TTL_S * 1000 });
+ return shared;
+ }
let res;
try {
res = await fetch(`${PRICE_API}/${key}-USD/spot`, {
@@ -60,7 +70,8 @@ async function fetchSpotUsd(symbol) {
if (!Number.isFinite(value) || value <= 0) {
throw new Error(`spending-price: ${key}-USD invalid response: ${raw}`);
}
- priceCache.set(key, { value, expiresAt: Date.now() + PRICE_TTL_MS });
+ localCache.set(key, { value, expiresAt: Date.now() + PRICE_TTL_S * 1000 });
+ await cacheSet(cacheKey, value, PRICE_TTL_S);
return value;
}
@@ -120,6 +131,6 @@ export const _internal = {
rescaleAtomics,
inferSymbol,
resetCache() {
- priceCache.clear();
+ localCache.clear();
},
};