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(trader): give a leader a paste-ready embed snippet on their profile
nirholas588e86f95c21
diff --git a/src/shared/trader-embed.js b/src/shared/trader-embed.jsnew file mode 100644index 000000000..411a0587b--- /dev/null+++ b/src/shared/trader-embed.js@@ -0,0 +1,29 @@+/**+ * The paste-ready <trader-card> snippet, as a pure function.+ *+ * The profile page renders this into a field a leader copies, so it has to be+ * exactly what they should paste: no placeholder to fill in, no attribute that+ * is already the default, and the referral code carried through when the viewer+ * has one. Kept out of the page module so it can be tested without a DOM.+ */++const DEFAULT_ORIGIN = 'https://three.ws';+const DEFAULT_WINDOW = '30d';+const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;+const REF_RE = /^[A-Za-z0-9_-]{1,32}$/;+const WINDOWS = ['24h', '7d', '30d', 'all'];++/**+ * @returns {string|null} the snippet, or null when the agent id is not one.+ * A null is a caller error rather than a broken snippet someone might paste.+ */+export function embedSnippet({ agentId, window: win = DEFAULT_WINDOW, ref = null, origin = DEFAULT_ORIGIN } = {}) {+ if (!UUID_RE.test(String(agentId || ''))) return null;+ const attrs = [`agent="${agentId}"`];+ // Only non-default attributes are written: a snippet that spells out every+ // default is longer to read and no more correct.+ if (WINDOWS.includes(win) && win !== DEFAULT_WINDOW) attrs.push(`window="${win}"`);+ if (ref && REF_RE.test(ref)) attrs.push(`ref="${ref}"`);+ const src = `${String(origin || DEFAULT_ORIGIN).replace(/\/+$/, '')}/trader-card/element.js`;+ return `<script type="module" src="${src}"><\/script>\n<trader-card ${attrs.join(' ')}></trader-card>`;+}diff --git a/src/trader.js b/src/trader.jsindex 4157f12ab..9d2269f32 100644--- a/src/trader.js+++ b/src/trader.js@@ -15,6 +15,7 @@ import {import { mountCopyPanel } from './copy-panel.js';import { mountPassport, resetPassport } from './trader-passport.js';import { walletChipHTML, wireWalletChips } from './shared/agent-wallet-chip.js';+import { embedSnippet } from './shared/trader-embed.js';import { ring, playRings, countUp } from './ui-juice.js';const WINDOWS = ['24h', '7d', '30d', 'all'];@@ -351,11 +352,31 @@ function render(data) {<div class="tp-actions"><div class="lb-seg" role="tablist" aria-label="Time window" id="tp-window">${winSeg}</div><div style="display:flex;gap:var(--space-2xs)">+ <button class="lb-btn" id="tp-embed" aria-expanded="false" aria-controls="tp-embed-panel">Embed this trader</button><button class="lb-btn" id="tp-share">Share track record</button><button class="lb-btn lb-btn-primary" id="tp-card">Download PnL card</button></div></div>+ <section class="tp-embed" id="tp-embed-panel" hidden aria-label="Embed this trader">+ <div class="tp-embed-copy">+ <h3>Put this card on your own page</h3>+ <p>One script tag renders this trader's live record anywhere: a site, a blog post, a+ stream overlay, a link in bio. Visitors fork a coin or ghost-copy the trader from it,+ signing with their own wallet. Nothing here holds funds or asks for a key.</p>+ <label class="tp-embed-label" for="tp-embed-code">Paste this into your page</label>+ <textarea id="tp-embed-code" class="tp-embed-code" readonly rows="3" spellcheck="false"></textarea>+ <div class="tp-embed-actions">+ <button class="lb-btn lb-btn-primary" type="button" id="tp-embed-copy">Copy snippet</button>+ <a class="lb-btn" href="/docs/trader-card-embed" target="_blank" rel="noopener">Options and docs</a>+ </div>+ </div>+ <div class="tp-embed-preview">+ <span class="tp-embed-preview-label">Live preview</span>+ <div id="tp-embed-mount"></div>+ </div>+ </section>+<div class="tp-metrics">${metricsGrid(m)}</div>${madeOnBlock(m)}@@ -499,6 +520,60 @@ function wireShare() {catch { toast(url); }});content.querySelector('#tp-card')?.addEventListener('click', () => downloadCard());+ wireEmbed();+}++// --- Embeddable trader card --------------------------------------------------+//+// The leader's audience is already somewhere else, so this hands them the piece+// of three.ws that travels: a one-tag <trader-card> carrying their own referral+// code when they have one. The panel renders a real, live card rather than a+// picture of one, so what they paste is exactly what they just looked at.++function wireEmbed() {+ const btn = content.querySelector('#tp-embed');+ const panel = content.querySelector('#tp-embed-panel');+ const field = content.querySelector('#tp-embed-code');+ const mount = content.querySelector('#tp-embed-mount');+ if (!btn || !panel || !field || !mount) return;++ const snippet = embedSnippet({ agentId: ctx.agentId, window: ctx.window, ref: ctx.refCode });+ if (!snippet) return; // not a real agent id: no snippet is better than a broken one+ field.value = snippet;++ btn.addEventListener('click', async () => {+ const open = !panel.hidden;+ panel.hidden = open;+ btn.setAttribute('aria-expanded', String(!open));+ if (open) return;+ // The element is only fetched when someone actually asks to see the embed,+ // so the profile does not pay for a widget most visitors never open.+ if (!mount.firstElementChild) {+ try {+ await import(/* @vite-ignore */ '/trader-card/element.js');+ const card = document.createElement('trader-card');+ card.setAttribute('agent', ctx.agentId);+ card.setAttribute('window', ctx.window);+ card.setAttribute('origin', location.origin);+ if (ctx.refCode) card.setAttribute('ref', ctx.refCode);+ mount.appendChild(card);+ } catch {+ mount.innerHTML = '<p class="tp-embed-fallback">The preview could not load here, but the snippet above is still correct. '+ + 'Paste it into your page to see the card.</p>';+ }+ }+ panel.scrollIntoView({ behavior: 'smooth', block: 'nearest' });+ });++ content.querySelector('#tp-embed-copy')?.addEventListener('click', async () => {+ try {+ await navigator.clipboard.writeText(field.value);+ toast('Embed snippet copied');+ } catch {+ field.select();+ toast('Select the snippet above and copy it');+ }+ });}// --- Shareable PnL card (PNG) ------------------------------------------------