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.

fix(signals): share one price and avatar formatter so a signed-out reader sees the feed

nirholascefb1dd24859
diff --git a/src/shared/signals-format.js b/src/shared/signals-format.js
new file mode 100644
index 000000000..683b5745f
--- /dev/null
+++ b/src/shared/signals-format.js
@@ -0,0 +1,51 @@
+/**
+ * Display helpers shared by the Signal Marketplace directory (/signals) and a
+ * feed's detail page (/signals/:slug).
+ *
+ * Both surfaces render the same three things and used to render them twice, one
+ * copy each, which is how they drifted: a feed priced at 12.345678 USDC printed
+ * its raw float on both pages, and a publisher with no profile image got an
+ * empty grey square because the identicon data-URI was assigned to a CSS
+ * `background` shorthand that cannot take a bare URL. One module, one answer.
+ */
+
+import { escapeHtml, identicon } from '../trader-format.js';
+
+const usdcFmt = new Intl.NumberFormat('en-US', { maximumFractionDigits: 2 });
+const usdcSubUnitFmt = new Intl.NumberFormat('en-US', { maximumFractionDigits: 6 });
+
+/**
+ * A USDC amount as a reader sees money: grouped thousands, at most two decimals,
+ * and no trailing zeros. Sub-cent prices are real here (a feed can charge
+ * 0.0005 USDC a signal), so anything under 1 keeps up to six decimals rather
+ * than rounding away the entire price.
+ */
+export function fmtUsdc(value) {
+ const n = Number(value);
+ if (!Number.isFinite(n) || n <= 0) return '0';
+ return n < 1 ? usdcSubUnitFmt.format(n) : usdcFmt.format(n);
+}
+
+/** A billing epoch in the shortest honest unit: 86400 -> "day", 900 -> "15m". */
+export function epochLabel(seconds) {
+ const sec = Number(seconds);
+ if (!Number.isFinite(sec) || sec <= 0) return 'epoch';
+ if (sec % 86400 === 0) { const d = sec / 86400; return d === 1 ? 'day' : `${d}d`; }
+ if (sec % 3600 === 0) { const h = sec / 3600; return h === 1 ? 'hour' : `${h}h`; }
+ return `${Math.round(sec / 60)}m`;
+}
+
+/**
+ * The publisher's avatar as an <img>, always.
+ *
+ * identicon() returns a data-URI meant for an `src`, so the fallback has to be
+ * an image too: rendering it into `style="background:..."` silently produced no
+ * image at all. A profile URL that 404s swaps to the identicon through
+ * /inline-behaviors.js (data-fallback-src) instead of leaving a broken glyph.
+ */
+export function publisherAvatarHtml(publisher, cls = 'sm-avatar') {
+ const seed = publisher?.agent_id || publisher?.name || '?';
+ const fallback = identicon(seed);
+ const src = publisher?.image || fallback;
+ return `<img class="${cls}" src="${escapeHtml(src)}" alt="" loading="lazy" decoding="async" data-fallback-src="${escapeHtml(fallback)}" data-fallback="keep" />`;
+}
diff --git a/src/signal-detail.js b/src/signal-detail.js
index c97b8e276..ce8927a55 100644
--- a/src/signal-detail.js
+++ b/src/signal-detail.js
@@ -9,7 +9,8 @@
*/
import { apiFetch } from './api.js';
-import { escapeHtml, fmtPct, compact, identicon } from './trader-format.js';
+import { escapeHtml, fmtPct, compact } from './trader-format.js';
+import { fmtUsdc, epochLabel, publisherAvatarHtml } from './shared/signals-format.js';
import { markNoindex } from './seo-meta.js';
const $ = (s, r = document) => r.querySelector(s);
@@ -29,11 +30,6 @@ const network = networkFromUrl();
let feed = null;
let myAgents = [];
-function epochLabel(sec) {
- if (sec % 86400 === 0) { const d = sec / 86400; return d === 1 ? 'day' : `${d}d`; }
- if (sec % 3600 === 0) { const h = sec / 3600; return h === 1 ? 'hour' : `${h}h`; }
- return `${Math.round(sec / 60)}m`;
-}
function latency(ms) {
if (ms == null) return '—';
if (ms < 1000) return `${ms}ms`;
@@ -47,9 +43,7 @@ function statBlock(label, value, { cls = '', sub = '' } = {}) {
function heroHtml(f) {
const p = f.publisher;
- const avatar = p.image
- ? `<img loading="lazy" decoding="async" class="sm-avatar" src="${escapeHtml(p.image)}" alt="" />`
- : `<span class="sm-avatar" aria-hidden="true" style="background:${identicon(p.agent_id)}"></span>`;
+ const avatar = publisherAvatarHtml(p);
const verified = p.verified ? `<span class="sm-verified">✓ Verified track record</span>` : `<span class="sm-thin">Unverified</span>`;
return `
<div class="sd-hero">
@@ -138,17 +132,22 @@ function logHtml(f) {
function subscribeHtml(f) {
const p = f.pricing;
let price;
- if (p.per_signal_usdc > 0 && p.per_epoch_usdc > 0) price = `<span class="amt">$${p.per_signal_usdc}</span><span class="per">/ signal · $${p.per_epoch_usdc}/${epochLabel(p.epoch_seconds)} option</span>`;
- else if (p.per_signal_usdc > 0) price = `<span class="amt">$${p.per_signal_usdc}</span><span class="per">USDC / signal</span>`;
- else if (p.per_epoch_usdc > 0) price = `<span class="amt">$${p.per_epoch_usdc}</span><span class="per">USDC / ${epochLabel(p.epoch_seconds)}</span>`;
+ if (p.per_signal_usdc > 0 && p.per_epoch_usdc > 0) price = `<span class="amt">$${fmtUsdc(p.per_signal_usdc)}</span><span class="per">/ signal · $${fmtUsdc(p.per_epoch_usdc)}/${epochLabel(p.epoch_seconds)} option</span>`;
+ else if (p.per_signal_usdc > 0) price = `<span class="amt">$${fmtUsdc(p.per_signal_usdc)}</span><span class="per">USDC / signal</span>`;
+ else if (p.per_epoch_usdc > 0) price = `<span class="amt">$${fmtUsdc(p.per_epoch_usdc)}</span><span class="per">USDC / ${epochLabel(p.epoch_seconds)}</span>`;
else price = `<span class="amt">Free</span><span class="per">no charge</span>`;
- const agentOpts = myAgents.length
- ? myAgents.map((a) => `<option value="${escapeHtml(a.id)}">${escapeHtml(a.name || a.id)}</option>`).join('')
- : '';
+ // loadAgents() sets myAgents to null on a 401, which is the signed-out gate
+ // below. Read the list through a normalized array so a null can never take
+ // the render down: this threw for every anonymous visitor and left the page
+ // blank, which is exactly the reader the subscribe panel exists to convert.
+ const agents = Array.isArray(myAgents) ? myAgents : [];
+ const agentOpts = agents
+ .map((a) => `<option value="${escapeHtml(a.id)}">${escapeHtml(a.name || a.id)}</option>`)
+ .join('');
const billingOpts = [
- p.per_signal_usdc > 0 ? `<option value="per_signal">Per signal ($${p.per_signal_usdc})</option>` : '',
- p.per_epoch_usdc > 0 ? `<option value="per_epoch">Per ${epochLabel(p.epoch_seconds)} ($${p.per_epoch_usdc})</option>` : '',
+ p.per_signal_usdc > 0 ? `<option value="per_signal">Per signal ($${fmtUsdc(p.per_signal_usdc)})</option>` : '',
+ p.per_epoch_usdc > 0 ? `<option value="per_epoch">Per ${epochLabel(p.epoch_seconds)} ($${fmtUsdc(p.per_epoch_usdc)})</option>` : '',
].filter(Boolean).join('');
const form = `
@@ -176,7 +175,7 @@ function subscribeHtml(f) {
const gate = `<div class="sd-gate"><p style="margin:0 0 8px;color:var(--ink-dim,#9aa)">Sign in and pick one of your agents to subscribe — its wallet pays the USDC and signs the mirror.</p><a href="/login?next=${encodeURIComponent(location.pathname)}">Sign in →</a></div>`;
const noAgents = `<div class="sd-gate"><p style="margin:0 0 8px;color:var(--ink-dim,#9aa)">You don't have an agent with a wallet yet.</p><a href="/create-agent">Create your first agent →</a></div>`;
- const body = myAgents === null ? gate : (myAgents.length ? form : noAgents);
+ const body = myAgents === null ? gate : (agents.length ? form : noAgents);
return `
<div class="sd-panel">
<h2>Subscribe</h2>
@@ -285,7 +284,12 @@ async function init() {
const data = await detailRes.json().catch(() => null);
if (!data?.feed) { errorState('Feed not found'); return; }
feed = data.feed;
- render();
+ try {
+ render();
+ } catch (err) {
+ console.error('signal detail render failed', err);
+ errorState('This feed could not be displayed');
+ }
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init);
diff --git a/src/signals.css b/src/signals.css
index 3a6f1207b..b8dd42881 100644
--- a/src/signals.css
+++ b/src/signals.css
@@ -25,11 +25,6 @@
letter-spacing: 0.04em; text-transform: uppercase;
color: var(--ink-dim, #9aa); margin: 0 0 14px;
}
-.sm-live-dot {
- width: 8px; height: 8px; border-radius: 50%; background: var(--sm-win);
- box-shadow: 0 0 0 0 rgba(52, 211, 153, 0.55); animation: sm-pulse 2s ease infinite;
-}
-@keyframes sm-pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(52,211,153,0.5); } 70% { box-shadow: 0 0 0 7px rgba(52,211,153,0); } }
.sm-title {
margin: 0 0 12px; font: 700 clamp(28px, 5vw, 46px)/1.05 var(--display, 'Space Grotesk', sans-serif);
color: var(--ink-bright, #fff); letter-spacing: -0.02em;
@@ -144,7 +139,7 @@
/* Skeleton loaders. */
.sm-skel { position: relative; overflow: hidden; }
.sm-skel::after { content: ''; position: absolute; inset: 0; background: linear-gradient(90deg, transparent, rgba(255,255,255,0.06), transparent); animation: sm-shimmer 1.2s infinite; }
-@keyframes sm-shimmer { 100% { transform: translateX(100%); } }
+@keyframes sm-shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
.sm-skel-line { height: 12px; border-radius: 6px; background: var(--surface-2, rgba(255,255,255,0.06)); }
/* ── Feed detail ──────────────────────────────────────────────────────────── */
@@ -223,7 +218,7 @@
.sd-gate a { color: var(--sm-accent); font-weight: 600; text-decoration: none; }
@media (prefers-reduced-motion: reduce) {
- .sm-card, .sm-live-dot, .sm-skel::after, .sm-edge-fill, .sd-mode, .sd-btn { animation: none; transition: none; }
+ .sm-card, .sm-skel::after, .sm-edge-fill, .sd-mode, .sd-btn { animation: none; transition: none; }
}
/* ── Interactive states (QB-07) ────────────────────────────────────────────
diff --git a/src/signals.js b/src/signals.js
index 47883effa..d24afb90d 100644
--- a/src/signals.js
+++ b/src/signals.js
@@ -7,7 +7,8 @@
* shareable; every card deep-links to the feed detail page.
*/
-import { escapeHtml, fmtPct, compact, identicon } from './trader-format.js';
+import { escapeHtml, fmtPct, compact } from './trader-format.js';
+import { fmtUsdc, epochLabel, publisherAvatarHtml } from './shared/signals-format.js';
import { updateValue, flipReorder, setLiveDot } from './ui-juice.js';
const API = '/api/signals/marketplace';
@@ -71,16 +72,21 @@ function skeletonGrid() {
function priceBlock(p) {
if (p.per_signal_usdc > 0 && p.per_epoch_usdc > 0) {
- return `<span class="amt">$${p.per_signal_usdc}</span><span class="per">/signal · $${p.per_epoch_usdc}/${epochLabel(p.epoch_seconds)}</span>`;
+ return `<span class="amt">$${fmtUsdc(p.per_signal_usdc)}</span><span class="per">/signal · $${fmtUsdc(p.per_epoch_usdc)}/${epochLabel(p.epoch_seconds)}</span>`;
}
- if (p.per_signal_usdc > 0) return `<span class="amt">$${p.per_signal_usdc}</span><span class="per">USDC / signal</span>`;
- if (p.per_epoch_usdc > 0) return `<span class="amt">$${p.per_epoch_usdc}</span><span class="per">USDC / ${epochLabel(p.epoch_seconds)}</span>`;
+ if (p.per_signal_usdc > 0) return `<span class="amt">$${fmtUsdc(p.per_signal_usdc)}</span><span class="per">USDC / signal</span>`;
+ if (p.per_epoch_usdc > 0) return `<span class="amt">$${fmtUsdc(p.per_epoch_usdc)}</span><span class="per">USDC / ${epochLabel(p.epoch_seconds)}</span>`;
return `<span class="amt">Free</span><span class="per">no charge</span>`;
}
-function epochLabel(sec) {
- if (sec % 86400 === 0) { const d = sec / 86400; return d === 1 ? 'day' : `${d}d`; }
- if (sec % 3600 === 0) { const h = sec / 3600; return h === 1 ? 'hour' : `${h}h`; }
- return `${Math.round(sec / 60)}m`;
+
+/** The card's price as one plain-language phrase for the link's accessible name. */
+function priceLabel(p) {
+ if (p.per_signal_usdc > 0 && p.per_epoch_usdc > 0) {
+ return `$${fmtUsdc(p.per_signal_usdc)} USDC per signal or $${fmtUsdc(p.per_epoch_usdc)} per ${epochLabel(p.epoch_seconds)}`;
+ }
+ if (p.per_signal_usdc > 0) return `$${fmtUsdc(p.per_signal_usdc)} USDC per signal`;
+ if (p.per_epoch_usdc > 0) return `$${fmtUsdc(p.per_epoch_usdc)} USDC per ${epochLabel(p.epoch_seconds)}`;
+ return 'free';
}
function metric(label, value, cls = '') {
@@ -89,9 +95,7 @@ function metric(label, value, cls = '') {
function card(f) {
const s = f.stats;
- const avatar = f.publisher.image
- ? `<img class="sm-avatar" src="${escapeHtml(f.publisher.image)}" alt="" loading="lazy" />`
- : `<span class="sm-avatar" aria-hidden="true" style="background:${identicon(f.publisher.agent_id)}"></span>`;
+ const avatar = publisherAvatarHtml(f.publisher);
const verified = f.publisher.verified
? `<span class="sm-verified" title="Verified on-chain track record">✓ Verified</span>`
: '';
@@ -99,10 +103,22 @@ function card(f) {
const roi = s.avg_realized_pct != null ? fmtPct(s.avg_realized_pct, { sign: true }) : '—';
const roiCls = s.avg_realized_pct == null ? 'muted' : s.avg_realized_pct > 0 ? 'win' : s.avg_realized_pct < 0 ? 'loss' : 'muted';
const thin = s.closed_signals < 10
- ? `<span class="sm-thin" title="Fewer than 10 closed signals — edge is regressed toward neutral until proven">Building track record</span>`
+ ? `<span class="sm-thin" title="Fewer than 10 closed signals: edge is regressed toward neutral until proven">Building track record</span>`
: '';
+ // An aria-label replaces a link's entire contents for assistive tech, so it
+ // has to restate every number the card shows, not just the headline score.
+ const label = [
+ `Rank ${f.rank}: ${f.title} by ${f.publisher.name}`,
+ f.publisher.verified ? 'verified publisher' : 'unverified publisher',
+ `proven edge ${f.edge_score} of 100`,
+ s.hit_rate != null ? `hit rate ${hit}` : 'hit rate not yet measured',
+ s.avg_realized_pct != null ? `average ROI ${roi}` : 'average ROI not yet measured',
+ `${compact(s.closed_signals)} closed of ${compact(s.total_entries)} signals`,
+ `${compact(s.subscribers)} subscriber${s.subscribers === 1 ? '' : 's'}`,
+ priceLabel(f.pricing),
+ ].join(', ');
return `
- <a class="sm-card" href="/signals/${encodeURIComponent(f.slug)}" data-key="${escapeHtml(String(f.slug))}" aria-label="${escapeHtml(f.title)} by ${escapeHtml(f.publisher.name)} — edge ${f.edge_score}">
+ <a class="sm-card" href="/signals/${encodeURIComponent(f.slug)}" data-key="${escapeHtml(String(f.slug))}" aria-label="${escapeHtml(label)}">
<span class="sm-rank">#${f.rank}</span>
<div class="sm-card-head">
${avatar}
@@ -122,7 +138,7 @@ function card(f) {
</div>
<div class="sm-card-foot">
<div class="sm-price">${priceBlock(f.pricing)}</div>
- <div style="display:flex;align-items:center;gap:8px">${thin}<span class="sm-view">${s.subscribers} sub${s.subscribers === 1 ? '' : 's'} · View →</span></div>
+ <div style="display:flex;align-items:center;gap:8px">${thin}<span class="sm-view">${compact(s.subscribers)} sub${s.subscribers === 1 ? '' : 's'} · View →</span></div>
</div>
</a>`;
}
@@ -190,7 +206,7 @@ async function load() {
setStatus('Could not load the marketplace. Check your connection and try again.', { error: true, retry: true });
setLiveDot($('#sm-live'), 'error', 'offline');
} else {
- setStatus('Reconnecting — showing the last known board.', { error: false });
+ setStatus('Reconnecting. Showing the last known board.', { error: false });
setLiveDot($('#sm-live'), 'connecting', 'reconnecting');
}
}
diff --git a/tests/signals-format.test.js b/tests/signals-format.test.js
new file mode 100644
index 000000000..53a38ebb4
--- /dev/null
+++ b/tests/signals-format.test.js
@@ -0,0 +1,70 @@
+// Display helpers behind /signals and /signals/:slug (src/shared/signals-format.js).
+//
+// Two of the three sit on the price the marketplace advertises, and the third
+// decides whether a publisher with no profile image gets an identicon or an
+// empty grey square. Both used to be duplicated per page and both were wrong on
+// one of them, so the edges are pinned here.
+
+import { describe, expect, it } from 'vitest';
+import { fmtUsdc, epochLabel, publisherAvatarHtml } from '../src/shared/signals-format.js';
+
+describe('fmtUsdc', () => {
+ it('caps a normal price at two decimals and drops trailing zeros', () => {
+ expect(fmtUsdc(0.25)).toBe('0.25');
+ expect(fmtUsdc(5)).toBe('5');
+ expect(fmtUsdc(12.345678)).toBe('12.35');
+ expect(fmtUsdc(2.5)).toBe('2.5');
+ });
+ it('groups thousands', () => {
+ expect(fmtUsdc(1234.5)).toBe('1,234.5');
+ });
+ it('keeps a sub-cent price instead of rounding it to zero', () => {
+ expect(fmtUsdc(0.0005)).toBe('0.0005');
+ expect(fmtUsdc(0.000125)).toBe('0.000125');
+ });
+ it('reads a missing or non-positive amount as zero', () => {
+ expect(fmtUsdc(null)).toBe('0');
+ expect(fmtUsdc(undefined)).toBe('0');
+ expect(fmtUsdc(NaN)).toBe('0');
+ expect(fmtUsdc(-3)).toBe('0');
+ });
+});
+
+describe('epochLabel', () => {
+ it('names whole days and hours', () => {
+ expect(epochLabel(86400)).toBe('day');
+ expect(epochLabel(3600)).toBe('hour');
+ expect(epochLabel(7 * 86400)).toBe('7d');
+ expect(epochLabel(4 * 3600)).toBe('4h');
+ });
+ it('falls back to minutes', () => {
+ expect(epochLabel(900)).toBe('15m');
+ expect(epochLabel(300)).toBe('5m');
+ });
+ it('never divides by a missing epoch', () => {
+ expect(epochLabel(0)).toBe('epoch');
+ expect(epochLabel(null)).toBe('epoch');
+ });
+});
+
+describe('publisherAvatarHtml', () => {
+ it('renders an img, never a CSS background, so the identicon actually paints', () => {
+ const html = publisherAvatarHtml({ agent_id: 'agent-1', name: 'Pub', image: null });
+ expect(html.startsWith('<img')).toBe(true);
+ expect(html).not.toContain('style="background');
+ expect(html).toContain('src="data:image/svg+xml');
+ });
+ it('prefers the publisher image and keeps the identicon as the swap target', () => {
+ const html = publisherAvatarHtml({ agent_id: 'agent-1', name: 'Pub', image: 'https://cdn.example/a.png' });
+ expect(html).toContain('src="https://cdn.example/a.png"');
+ expect(html).toContain('data-fallback-src="data:image/svg+xml');
+ });
+ it('escapes a hostile image url out of the attribute', () => {
+ const html = publisherAvatarHtml({ agent_id: 'a', name: 'p', image: '"><script>alert(1)</script>' });
+ expect(html).not.toContain('<script>');
+ expect(html).toContain('&quot;');
+ });
+ it('accepts a custom class for the detail hero', () => {
+ expect(publisherAvatarHtml({ agent_id: 'a' }, 'sm-avatar sd-av')).toContain('class="sm-avatar sd-av"');
+ });
+});