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(agent-wallet): resolve the visitor's own agent when no agent is named in the URL
nirholas7edf6de8516b
diff --git a/pages/agent-wallet.html b/pages/agent-wallet.htmlindex c03f450cf..a6a23bcb6 100644--- a/pages/agent-wallet.html+++ b/pages/agent-wallet.html@@ -3,17 +3,17 @@<head><script>/* three.ws theme boot — no-flash; runtime in /theme-switcher.js */(function(){try{var m=localStorage.getItem('twx_theme');var l=m==='auto'?(window.matchMedia&&window.matchMedia('(prefers-color-scheme: light)').matches):m==='light';document.documentElement.setAttribute('data-theme',l?'light':'dark');}catch(e){document.documentElement.setAttribute('data-theme','dark');}})();</script><meta charset="utf-8" />- <title data-i18n="agent_wallet.meta_title">Agent wallet — three.ws</title>+ <title data-i18n="agent_wallet.meta_title">Agent Wallet · three.ws</title><meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="theme-color" content="#000000" /><metaname="description"- content="Manage a three.ws 3D AI agent's self-custodied Solana wallet — live balance, deposits, trading, sniping, x402 payments, and withdrawals, all in one place."+ content="Manage a three.ws 3D AI agent's self-custodied Solana wallet: live balance, deposits, trading, sniping, x402 payments, and withdrawals, all in one place."data-i18n-attr="content:agent_wallet.meta_description"/><meta name="robots" content="noindex" /><meta property="og:type" content="website" />- <meta property="og:title" content="Agent wallet — three.ws" data-i18n-attr="content:agent_wallet.meta_og_title"/>+ <meta property="og:title" content="Agent Wallet · three.ws" data-i18n-attr="content:agent_wallet.meta_og_title"/><metaproperty="og:description"content="Your 3D AI agent's own Solana wallet: balance, deposit, trade, snipe, pay, and withdraw."@@ -151,6 +151,133 @@animation: none;}}++ /* Agent picker: shown when the signed-in owner has more than one agent. */+ .awh-pick {+ max-width: 640px;+ margin: 0 auto;+ }+ .awh-pick-h {+ font-family: var(--font-display);+ font-size: var(--text-xl);+ color: var(--ink-bright);+ margin: 0 0 var(--space-xs);+ line-height: var(--leading-tight, 1.15);+ }+ .awh-pick-lede {+ color: var(--ink-dim);+ font: var(--text-md) var(--font-body);+ line-height: var(--leading-normal);+ margin: 0 0 var(--space-lg);+ }+ .awh-pick-list {+ list-style: none;+ margin: 0 0 var(--space-lg);+ padding: 0;+ display: flex;+ flex-direction: column;+ gap: var(--space-sm);+ }+ .awh-pick-row {+ display: flex;+ align-items: center;+ gap: var(--space-sm);+ padding: 12px 14px;+ border: 1px solid var(--stroke);+ border-radius: var(--radius-lg);+ background: var(--surface-1);+ text-decoration: none;+ color: inherit;+ transition:+ background var(--duration-fast) var(--ease-standard),+ border-color var(--duration-fast) var(--ease-standard),+ transform var(--duration-instant, 80ms) var(--ease-standard);+ }+ .awh-pick-row:hover {+ background: var(--surface-2);+ border-color: var(--stroke-strong);+ }+ .awh-pick-row:active {+ transform: translateY(1px);+ }+ .awh-pick-row:focus-visible {+ outline: var(--focus-ring-width) solid var(--focus-ring-color);+ outline-offset: var(--focus-ring-offset);+ }+ .awh-pick-av {+ width: 40px;+ height: 40px;+ flex: none;+ border-radius: var(--radius-md);+ object-fit: cover;+ background: var(--surface-2);+ border: 1px solid var(--stroke);+ }+ .awh-pick-id {+ display: flex;+ flex-direction: column;+ gap: 2px;+ min-width: 0;+ flex: 1 1 auto;+ }+ .awh-pick-name {+ font-size: var(--text-md);+ font-weight: 600;+ color: var(--ink-bright);+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+ .awh-pick-sub {+ font-family: var(--font-mono, ui-monospace, monospace);+ font-size: var(--text-sm);+ color: var(--ink-dim);+ overflow: hidden;+ text-overflow: ellipsis;+ white-space: nowrap;+ }+ .awh-pick-state {+ flex: none;+ font-size: var(--text-2xs, 0.6875rem);+ font-weight: 600;+ padding: 2px 8px;+ border-radius: var(--radius-pill, 999px);+ border: 1px solid transparent;+ white-space: nowrap;+ }+ .awh-pick-state[data-state='ready'] {+ color: var(--success, #4ade80);+ background: color-mix(in srgb, var(--success, #4ade80) 14%, transparent);+ border-color: color-mix(in srgb, var(--success, #4ade80) 35%, transparent);+ }+ .awh-pick-state[data-state='preparing'] {+ color: var(--warn, #fbbf24);+ background: color-mix(in srgb, var(--warn, #fbbf24) 12%, transparent);+ border-color: color-mix(in srgb, var(--warn, #fbbf24) 32%, transparent);+ }+ .awh-pick-go {+ flex: none;+ color: var(--ink-dim);+ transition: transform var(--duration-fast) var(--ease-standard);+ }+ .awh-pick-row:hover .awh-pick-go {+ transform: translateX(2px);+ color: var(--ink);+ }+ .awh-pick-foot {+ justify-content: flex-start;+ }+ @media (max-width: 420px) {+ .awh-pick-state {+ display: none;+ }+ }+ @media (prefers-reduced-motion: reduce) {+ .awh-pick-row,+ .awh-pick-go {+ transition: none;+ }+ }</style><link rel="canonical" href="https://three.ws/agent-wallet">diff --git a/src/agent-wallet.js b/src/agent-wallet.jsindex ca186a951..cae35292b 100644--- a/src/agent-wallet.js+++ b/src/agent-wallet.js@@ -1,12 +1,17 @@/**- * Agent Wallet hub — page entry.+ * Agent Wallet hub: page entry.*- * Route: /agent/:id/wallet (also accepts ?id=<uuid>). Resolves the agent, then- * mounts the tabbed hub (Balance · Deposit · Trade · Snipe · Pay · Withdraw).+ * Routes: /agent/:id/wallet and /agents/:id/wallet (agent named in the path),+ * plus /agent-wallet with an optional ?id=<uuid>.*- * Every page state is designed: loading skeleton, not-found, fetch error (with- * retry), and the live hub. Owner vs visitor is decided server-side by the- * agent record's `is_owner`.+ * When no agent is named, the page resolves the signed-in user's own agents+ * from GET /api/agents: exactly one agent mounts its hub straight away, several+ * render a picker, and none (or a signed-out visitor) gets a designed next step+ * instead of a dead end.+ *+ * Every page state is designed: loading skeleton, picker, signed out, no agents+ * yet, agent not found, fetch error (with retry), and the live hub. Owner vs+ * visitor is decided server-side by the agent record's `is_owner`.*/import { mountAgentWalletHub } from './agent-wallet-hub/index.js';@@ -30,76 +35,203 @@ function escapeHtml(s) {);}-function renderLoading() {+function renderLoading(label = 'Loading agent wallet') {root.innerHTML = `- <div class="awh-page-skel" aria-busy="true" aria-label="Loading agent wallet">+ <div class="awh-page-skel" aria-busy="true" aria-label="${escapeHtml(label)}"><div class="awh-page-skel-row"></div><div class="awh-page-skel-tabs"></div><div class="awh-page-skel-card"></div></div>`;}-function renderMessage({ title, body, actionHref, actionLabel, retry }) {+/**+ * A designed terminal state: heading, one line of guidance, and up to two real+ * actions. `retry` adds a button that re-runs the page load.+ */+function renderMessage({ title, body, actions = [], retry }) {root.innerHTML = `<div class="awh-page-msg" role="alert"><h1>${escapeHtml(title)}</h1><p>${escapeHtml(body)}</p><div class="awh-page-msg-actions">- ${actionHref ? `<a class="awh-page-btn awh-page-btn--primary" href="${escapeHtml(actionHref)}">${escapeHtml(actionLabel || 'Continue')}</a>` : ''}- ${retry ? `<button class="awh-page-btn" type="button" data-act="retry">Try again</button>` : ''}+ ${actions+ .map(+ (a) =>+ `<a class="awh-page-btn${a.primary ? ' awh-page-btn--primary' : ''}" href="${escapeHtml(a.href)}">${escapeHtml(a.label)}</a>`,+ )+ .join('')}+ ${retry ? '<button class="awh-page-btn" type="button" data-act="retry">Try again</button>' : ''}</div></div>`;if (retry) root.querySelector('[data-act="retry"]')?.addEventListener('click', () => load());}-async function load() {- const agentId = resolveAgentId();- if (!agentId || !UUID_RE.test(agentId)) {+/** Picker shown when the signed-in user owns more than one agent. */+function renderPicker(agents) {+ root.innerHTML = `+ <section class="awh-pick" aria-labelledby="awh-pick-h">+ <h1 id="awh-pick-h" class="awh-pick-h">Your agent wallets</h1>+ <p class="awh-pick-lede">Each agent holds its own self-custodied Solana wallet. Pick one to see its balance, fund it, and review its activity.</p>+ <ul class="awh-pick-list">+ ${agents+ .map((a) => {+ const ready = !!(a.wallet_ready ?? a.walletReady);+ const addr = a.solana_address || '';+ const sub = addr+ ? `${addr.slice(0, 4)}…${addr.slice(-4)}`+ : 'Wallet is being prepared';+ return `<li>+ <a class="awh-pick-row" href="/agent/${encodeURIComponent(a.id)}/wallet">+ ${+ a.avatar_thumbnail_url+ ? `<img class="awh-pick-av" src="${escapeHtml(a.avatar_thumbnail_url)}" alt="" loading="lazy" />`+ : '<span class="awh-pick-av" aria-hidden="true"></span>'+ }+ <span class="awh-pick-id">+ <span class="awh-pick-name">${escapeHtml(a.name || 'Untitled agent')}</span>+ <span class="awh-pick-sub">${escapeHtml(sub)}</span>+ </span>+ <span class="awh-pick-state" data-state="${ready ? 'ready' : 'preparing'}">${ready ? 'Ready' : 'Preparing'}</span>+ <span class="awh-pick-go" aria-hidden="true">→</span>+ </a>+ </li>`;+ })+ .join('')}+ </ul>+ <div class="awh-page-msg-actions awh-pick-foot">+ <a class="awh-page-btn" href="/my-agents">Manage agents</a>+ <a class="awh-page-btn" href="/create-agent">Create an agent</a>+ </div>+ </section>`;+ document.title = 'Your agent wallets · three.ws';+}++/** Fetch one agent by id. Returns { agent } or { state } for a designed stop. */+async function fetchAgent(agentId) {+ const res = await fetch(`/api/agents/${encodeURIComponent(agentId)}`, {+ credentials: 'include',+ headers: { accept: 'application/json' },+ });+ if (res.status === 404) return { state: 'not_found' };+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const data = await res.json();+ if (!data?.agent) throw new Error('empty agent payload');+ return { agent: data.agent };+}++/** Fetch the caller's own agents. Returns { agents } or { state: 'signed_out' }. */+async function fetchOwnAgents() {+ const res = await fetch('/api/agents', {+ credentials: 'include',+ headers: { accept: 'application/json' },+ });+ if (res.status === 401 || res.status === 403) return { state: 'signed_out' };+ if (!res.ok) throw new Error(`HTTP ${res.status}`);+ const data = await res.json();+ return { agents: Array.isArray(data?.agents) ? data.agents : [] };+}++function mountHub(agent) {+ document.title = `${agent.name || 'Agent'} wallet · three.ws`;+ mountAgentWalletHub({ mount: root, agent });+}++/** No agent in the URL: resolve the signed-in user's own agents. */+async function loadOwnWallets() {+ renderLoading('Loading your agent wallets');++ let result;+ try {+ result = await fetchOwnAgents();+ } catch {+ renderMessage({+ title: "Couldn't load your wallets",+ body: 'We could not reach the agent service. This is usually temporary, so check your connection and try again.',+ retry: true,+ });+ document.title = 'Agent Wallet · three.ws';+ return;+ }++ if (result.state === 'signed_out') {renderMessage({- title: 'No agent selected',- body: 'This page needs an agent. Open it from an agent profile, or create your first agent to get a self-custodied Solana wallet.',- actionHref: '/create-agent',- actionLabel: 'Create an agent',+ title: 'Sign in to open an agent wallet',+ body: 'Agent wallets are private to their owner. Sign in to see your agents and their balances, or create an account to get an agent with its own Solana wallet.',+ actions: [+ { href: '/login?next=/agent-wallet', label: 'Sign in', primary: true },+ { href: '/register', label: 'Create account' },+ ],});- document.title = 'Agent wallet — three.ws';+ document.title = 'Agent Wallet · three.ws';+ return;+ }++ const agents = result.agents;+ if (!agents.length) {+ renderMessage({+ title: 'No agents yet',+ body: 'Every agent you create gets a self-custodied Solana wallet it can fund, trade, and pay from. Create your first agent to open its wallet.',+ actions: [{ href: '/create-agent', label: 'Create an agent', primary: true }],+ });+ document.title = 'Agent Wallet · three.ws';+ return;+ }++ if (agents.length === 1) {+ mountHub(agents[0]);+ return;+ }++ renderPicker(agents);+}++/** An agent is named in the URL: load and mount its hub. */+async function loadNamedWallet(agentId) {+ if (!UUID_RE.test(agentId)) {+ renderMessage({+ title: 'That agent link is not valid',+ body: 'An agent wallet link carries the agent id. Open the wallet from the agent profile, or pick one of your own agents.',+ actions: [+ { href: '/agent-wallet', label: 'Your agent wallets', primary: true },+ { href: '/agents', label: 'Browse agents' },+ ],+ });+ document.title = 'Agent Wallet · three.ws';return;}renderLoading();- let agent;+ let result;try {- const res = await fetch(`/api/agents/${encodeURIComponent(agentId)}`, {- credentials: 'include',- headers: { accept: 'application/json' },- });- if (res.status === 404) {- renderMessage({- title: 'Agent not found',- body: 'This agent doesn’t exist or has been removed. It may have been deleted by its owner.',- actionHref: '/agents',- actionLabel: 'Browse agents',- });- document.title = 'Agent not found — three.ws';- return;- }- if (!res.ok) throw new Error(`HTTP ${res.status}`);- const data = await res.json();- agent = data.agent;- if (!agent) throw new Error('empty agent payload');+ result = await fetchAgent(agentId);} catch {renderMessage({- title: 'Couldn’t load this wallet',- body: 'We couldn’t reach the agent service. This is usually temporary — check your connection and try again.',+ title: "Couldn't load this wallet",+ body: 'We could not reach the agent service. This is usually temporary, so check your connection and try again.',retry: true,});- document.title = 'Agent wallet — three.ws';+ document.title = 'Agent Wallet · three.ws';+ return;+ }++ if (result.state === 'not_found') {+ renderMessage({+ title: 'Agent not found',+ body: 'This agent does not exist or has been removed. It may have been deleted by its owner.',+ actions: [{ href: '/agents', label: 'Browse agents', primary: true }],+ });+ document.title = 'Agent not found · three.ws';return;}- document.title = `${agent.name || 'Agent'} wallet — three.ws`;+ mountHub(result.agent);+}- mountAgentWalletHub({ mount: root, agent });+async function load() {+ const agentId = resolveAgentId();+ if (agentId) return loadNamedWallet(agentId);+ return loadOwnWallets();}load();