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
feat: launcher funding and source helpers with agent config uniqueness migration
nirholas2d614ba9afcb
diff --git a/api/_lib/launcher-funding.js b/api/_lib/launcher-funding.jsnew file mode 100644index 000000000..3ca7d3a55--- /dev/null+++ b/api/_lib/launcher-funding.js@@ -0,0 +1,146 @@+// @ts-check+// Master → agent SOL funding for the autonomous coin launcher.+//+// The launcher never lets the master wallet mint a coin directly — that would+// detach the coin from an avatar identity. Instead the master ('coin-launcher-+// master' in SOLANA_SIGNERS) tops up the NEXT agent in the rotation with exactly+// the per-launch SOL it needs (deploy cost + dev-buy), and that agent signs its+// own pump.fun create. This module is the money-moving half of that handshake:+//+// loadMasterSigner() — the master Keypair (or null if unconfigured).+// masterBalanceSol() — current master SOL, for the circuit breaker.+// dailySpentSol(scope,uid) — SOL already spent today (enforces daily_sol_cap).+// fundAgentForLaunch(...) — the guarded transfer: caps, balance, send.+//+// Every transfer routes through sendSol → submitProtected, so it inherits the+// data-driven priority fee, blockhash-refresh rebroadcast, and hard-throw-on-+// revert behaviour — a fund step never drops silently under congestion.++import { sql } from './db.js';+import { decodeSecretKey } from './solana-signers.js';+import { sendSol, getSolBalance } from './avatar-wallet.js';+import { solanaConnection } from './agent-pumpfun.js';++const LAMPORTS_PER_SOL = 1_000_000_000;++// The master signer reuses the dedicated launcher env, falling back to the+// existing x402-launcher wallet so the feature works on current infra. Kept in+// sync with the 'coin-launcher-master' SignerSpec in solana-signers.js.+const MASTER_ENV = 'LAUNCHER_MASTER_SECRET_KEY_B64';+const MASTER_FALLBACK_ENV = 'PUMP_X402_LAUNCHER_SECRET_KEY_B64';++/**+ * Load the master launch wallet keypair, or null when neither env is set.+ * @returns {Promise<import('@solana/web3.js').Keypair|null>}+ */+export async function loadMasterSigner() {+ const secret = process.env[MASTER_ENV] || process.env[MASTER_FALLBACK_ENV] || '';+ if (!secret) return null;+ const bytes = await decodeSecretKey(secret);+ if (!bytes) {+ throw Object.assign(new Error('coin-launcher-master secret did not decode'), { code: 'bad_signer' });+ }+ const { Keypair } = await import('@solana/web3.js');+ return Keypair.fromSecretKey(bytes);+}++/**+ * Current master wallet SOL balance. Null when the master is unconfigured.+ * @param {'mainnet'|'devnet'} network+ * @returns {Promise<number|null>}+ */+export async function masterBalanceSol(network = 'mainnet') {+ const master = await loadMasterSigner();+ if (!master) return null;+ const conn = solanaConnection(network);+ const { sol } = await getSolBalance(conn, master.publicKey);+ return sol;+}++/**+ * SOL the launcher has already committed today (UTC) for a scope. Sums runs that+ * actually moved money (funded/launched/confirmed); dry-run + skipped rows don't+ * count. Drives the daily_sol_cap ceiling.+ * @param {'global'|'user'} scope+ * @param {string|null} userId+ * @returns {Promise<number>}+ */+export async function dailySpentSol(scope, userId = null) {+ const [row] = await sql`+ select coalesce(sum(sol_spent), 0)::float8 as spent+ from launcher_runs+ where scope = ${scope}+ and ${scope === 'user' ? sql`user_id = ${userId}` : sql`true`}+ and status in ('funded', 'launched', 'confirmed')+ and created_at >= date_trunc('day', now())+ `;+ return Number(row?.spent || 0);+}++/**+ * @typedef {Object} FundResult+ * @property {boolean} ok+ * @property {string} [signature] the funding transfer signature+ * @property {number} [lamports] lamports moved+ * @property {string} [reason] why the fund was refused (when ok=false)+ */++/**+ * Guarded master → agent SOL top-up for one launch. Refuses (never throws for a+ * business-rule stop) when a cap would be breached or the master can't cover it,+ * so the engine can record a clean 'skipped' run and trip the breaker. Only a+ * genuine on-chain/RPC failure throws.+ *+ * @param {Object} p+ * @param {string} p.agentAddress recipient agent Solana pubkey (base58)+ * @param {number} p.sol SOL to move (per_launch_sol)+ * @param {number} p.perLaunchCapSol hard ceiling for a single launch+ * @param {number} p.dailyCapSol remaining daily allowance already computed by caller+ * @param {'global'|'user'} [p.scope]+ * @param {'mainnet'|'devnet'} [p.network]+ * @param {string} [p.memo]+ * @returns {Promise<FundResult>}+ */+export async function fundAgentForLaunch({+ agentAddress,+ sol,+ perLaunchCapSol,+ dailyCapSol,+ network = 'mainnet',+ memo = 'three.ws launcher',+}) {+ const amount = Number(sol);+ if (!Number.isFinite(amount) || amount <= 0) {+ return { ok: false, reason: 'per_launch_sol must be positive' };+ }+ if (perLaunchCapSol > 0 && amount > perLaunchCapSol) {+ return { ok: false, reason: `per-launch ${amount} SOL exceeds cap ${perLaunchCapSol}` };+ }+ if (dailyCapSol != null && amount > dailyCapSol) {+ return { ok: false, reason: `daily SOL cap reached (${dailyCapSol} remaining)` };+ }++ const master = await loadMasterSigner();+ if (!master) return { ok: false, reason: 'master launch wallet not configured' };++ const conn = solanaConnection(network);+ // Keep a fee buffer so the master never empties below the cost of the very+ // transfer it is about to sign.+ const { sol: masterSol } = await getSolBalance(conn, master.publicKey);+ if (masterSol < amount + 0.002) {+ return { ok: false, reason: `master balance ${masterSol} SOL below ${amount} + fees` };+ }++ const lamports = Math.round(amount * LAMPORTS_PER_SOL);+ const signature = await sendSol({+ connection: conn,+ fromKeypair: master,+ to: agentAddress,+ lamports,+ memo,+ network,+ });+ return { ok: true, signature, lamports };+}++export { LAMPORTS_PER_SOL };diff --git a/api/_lib/launcher-sources.js b/api/_lib/launcher-sources.jsnew file mode 100644index 000000000..3a87e19a2--- /dev/null+++ b/api/_lib/launcher-sources.js@@ -0,0 +1,216 @@+// @ts-check+// Source providers for the autonomous coin launcher — the "what to launch" half.+//+// Three flavours, all returning the SAME shape so the engine treats them+// uniformly: { kind, name, symbol, description, trigger_source, trigger_detail }.+//+// random — wordlist salad. No network, no LLM. The cheap filler that keeps the+// hybrid cadence ticking when no real trend is live.+// meme — LLM synthesises an ORIGINAL meme coin from open meme culture.+// trend — reads the cultural signal already in our own pump_coin_intel+// (categories / tags / narratives of what is breaking out right now)+// plus recent X chatter, and asks the LLM to coin an ORIGINAL token+// riding that wave.+//+// Hard rule baked in: the LLM is instructed to invent original names/tickers and+// NEVER copy an existing token's identity. We mine themes (culture), not tickers+// (specific coins) — so this is trend-following, not cloning. The agent's avatar+// (attached later by the engine) is always the coin's visual identity.++import { sql } from './db.js';+import { llmComplete, llmConfigured } from './llm.js';++// ── sanitisers ──────────────────────────────────────────────────────────────+// pump.fun caps: name ≤ 32, symbol ≤ 10. Symbols are uppercased alphanumerics.++/** @param {string} s */+export function sanitizeName(s) {+ return String(s || '')+ .replace(/\s+/g, ' ')+ .trim()+ .slice(0, 32);+}++/** @param {string} s */+export function sanitizeSymbol(s) {+ const cleaned = String(s || '')+ .toUpperCase()+ .replace(/[^A-Z0-9]/g, '')+ .slice(0, 10);+ return cleaned || 'THREE3';+}++// ── random (no LLM) ───────────────────────────────────────────────────────────+const ADJECTIVES = [+ 'Turbo', 'Cosmic', 'Feral', 'Velvet', 'Quantum', 'Rogue', 'Lucid', 'Hyper',+ 'Molten', 'Neon', 'Phantom', 'Atomic', 'Wild', 'Solar', 'Frostbit', 'Electric',+ 'Savage', 'Mythic', 'Stellar', 'Chrome', 'Gilded', 'Vapor', 'Radiant', 'Drift',+];+const NOUNS = [+ 'Otter', 'Comet', 'Goblin', 'Yeti', 'Falcon', 'Mantis', 'Kraken', 'Pixel',+ 'Nomad', 'Bishop', 'Tiger', 'Sprout', 'Anvil', 'Specter', 'Lotus', 'Bandit',+ 'Phoenix', 'Walrus', 'Cobra', 'Maple', 'Raven', 'Bison', 'Koi', 'Hawk',+];++function pick(arr) {+ return arr[Math.floor(Math.random() * arr.length)];+}++/**+ * A complete, original coin from wordlists. Always succeeds — this is the+ * guaranteed fallback every other provider degrades to.+ * @returns {{kind:'random', name:string, symbol:string, description:string, trigger_source:string, trigger_detail:object}}+ */+export function randomCoin() {+ const adj = pick(ADJECTIVES);+ const noun = pick(NOUNS);+ const name = sanitizeName(`${adj} ${noun}`);+ const symbol = sanitizeSymbol((adj.slice(0, 4) + noun.slice(0, 4)).toUpperCase());+ return {+ kind: 'random',+ name,+ symbol,+ description: `${name} — an autonomous three.ws launch. Fully on-chain, avatar-fronted, $THREE-aligned.`,+ trigger_source: 'random',+ trigger_detail: { adj, noun },+ };+}++// ── cultural signal ───────────────────────────────────────────────────────────++/**+ * Mine what's culturally hot from our own intel: the categories, tags, and+ * narratives of recently observed, high-quality coins, plus recent X chatter.+ * Returns deduped theme strings (never specific tickers).+ * @param {{network?:string, categories?:string[], limit?:number}} opts+ * @returns {Promise<string[]>}+ */+export async function gatherThemes({ network = 'mainnet', categories = [], limit = 40 } = {}) {+ const themes = new Set();++ try {+ const rows = await sql`+ select category, tags, narrative+ from pump_coin_intel+ where network = ${network}+ and first_seen_at > now() - interval '24 hours'+ and quality_score is not null and quality_score >= 55+ ${categories.length ? sql`and category = any(${categories})` : sql``}+ order by quality_score desc nulls last+ limit ${limit}+ `;+ for (const r of rows) {+ if (r.category && r.category !== 'unknown') themes.add(String(r.category));+ if (Array.isArray(r.tags)) r.tags.slice(0, 4).forEach((t) => t && themes.add(String(t)));+ if (r.narrative) themes.add(String(r.narrative).slice(0, 80));+ }+ } catch {+ /* intel table absent or empty — themes stay sparse, engine falls back */+ }++ // Recent X chatter is an optional cultural input; its schema varies, so probe+ // defensively and skip cleanly when the column/table isn't there.+ try {+ const rows = await sql`+ select text from x_posts+ where text is not null and created_at > now() - interval '48 hours'+ order by created_at desc limit 15+ `;+ for (const r of rows) {+ const words = String(r.text || '')+ .replace(/https?:\/\/\S+/g, '')+ .match(/#?[A-Za-z][A-Za-z0-9]{3,18}/g);+ (words || []).slice(0, 3).forEach((w) => themes.add(w.replace(/^#/, '')));+ }+ } catch {+ /* no X signal available — fine */+ }++ return [...themes].slice(0, 24);+}++// ── LLM synthesis ─────────────────────────────────────────────────────────────++const SYNTH_SYSTEM =+ 'You are a memecoin naming engine for three.ws, a Solana launch platform. ' ++ 'You invent ORIGINAL, punchy, memeable coin identities. Absolute rules: ' ++ '(1) Never copy, reference, or imitate the name or ticker of any existing real ' ++ 'cryptocurrency or token — invent something new. (2) name ≤ 32 chars, ' ++ 'symbol 3-8 uppercase letters/digits, no spaces. (3) Keep it playful and ' ++ 'culturally aware, not offensive or hateful. ' ++ 'Respond with STRICT JSON only: {"name":"","symbol":"","description":""}.';++function parseCoinJson(text) {+ if (!text) return null;+ const m = text.match(/\{[\s\S]*\}/);+ if (!m) return null;+ try {+ const o = JSON.parse(m[0]);+ if (!o || typeof o !== 'object' || !o.name || !o.symbol) return null;+ return {+ name: sanitizeName(o.name),+ symbol: sanitizeSymbol(o.symbol),+ description: sanitizeName(String(o.description || '')).slice(0, 180) || `${sanitizeName(o.name)} on three.ws`,+ };+ } catch {+ return null;+ }+}++/**+ * Ask the LLM to coin an original token, optionally riding a set of themes.+ * Degrades to randomCoin() on any LLM failure — never throws, never blocks a tick.+ * @param {{themes?:string[], flavor?:'trend'|'meme', triggerSource?:string}} opts+ */+export async function synthesizeCoin({ themes = [], flavor = 'meme', triggerSource } = {}) {+ if (!llmConfigured()) return { ...randomCoin(), degraded: 'llm_unconfigured' };++ const userPrompt = themes.length+ ? `These cultural themes are trending on Solana right now: ${themes.join(', ')}. ` ++ `Coin ONE original memecoin riding this energy. Do not name it after any of these themes literally if they are existing tokens — riff, don't copy.`+ : 'Coin ONE original, funny, internet-native memecoin from current meme culture.';++ let out;+ try {+ out = await llmComplete({ system: SYNTH_SYSTEM, user: userPrompt, maxTokens: 200, timeoutMs: 12_000 });+ } catch {+ return { ...randomCoin(), degraded: 'llm_error' };+ }++ const coin = parseCoinJson(out?.text);+ if (!coin) return { ...randomCoin(), degraded: 'llm_unparseable' };++ return {+ kind: flavor === 'trend' ? 'trend' : 'meme',+ name: coin.name,+ symbol: coin.symbol,+ description: coin.description,+ trigger_source: triggerSource || (flavor === 'trend' ? 'coin_intel' : 'meme-llm'),+ trigger_detail: { themes: themes.slice(0, 12) },+ };+}++// ── dispatcher ────────────────────────────────────────────────────────────────++/**+ * Choose a coin for a tick given the configured mode. Hybrid prefers a live+ * trend, falls back to a meme, and injects random filler when no signal exists.+ * @param {{mode:string, network?:string, categories?:string[], sources?:string[]}} cfg+ */+export async function pickSource({ mode, network = 'mainnet', categories = [] } = {}) {+ if (mode === 'random') return randomCoin();+ if (mode === 'meme') return synthesizeCoin({ flavor: 'meme' });++ if (mode === 'trend') {+ const themes = await gatherThemes({ network, categories });+ if (themes.length >= 2) return synthesizeCoin({ themes, flavor: 'trend' });+ // No live trend → still honour 'trend' intent with a meme rather than stall.+ return synthesizeCoin({ flavor: 'meme', triggerSource: 'trend-fallback' });+ }++ // hybrid (default): trend first, meme second, occasional random filler.+ const themes = await gatherThemes({ network, categories });+ if (themes.length >= 2) return synthesizeCoin({ themes, flavor: 'trend' });+ if (Math.random() < 0.5) return synthesizeCoin({ flavor: 'meme', triggerSource: 'hybrid' });+ return randomCoin();+}diff --git a/api/_lib/migrations/20260629070000_agent_launcher_configs_unique.sql b/api/_lib/migrations/20260629070000_agent_launcher_configs_unique.sqlnew file mode 100644index 000000000..934c8e304--- /dev/null+++ b/api/_lib/migrations/20260629070000_agent_launcher_configs_unique.sql@@ -0,0 +1,10 @@+-- Roll-forward: add the UNIQUE (agent_id, network) constraint to+-- agent_launcher_configs that 20260626200000_agent_capabilities.sql was edited+-- to declare *after* it had already been applied. Because that constraint lives+-- inside a CREATE TABLE IF NOT EXISTS, databases that ran the original file never+-- received it — leaving the capabilities upsert's ON CONFLICT (agent_id, network)+-- with no matching unique index. This migration reconciles existing databases.+-- Idempotent. Safe on fresh installs (the constraint may already exist there).++CREATE UNIQUE INDEX IF NOT EXISTS agent_launcher_configs_agent_network_uniq+ ON agent_launcher_configs (agent_id, network);