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.

29.4 KB
Raw Blame
<!doctype html>
<html lang="en">
	<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" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title data-i18n="agents_index.meta_title">Agents Index · three.ws</title>
		<meta name="description" content="Browse every agent registered on three.ws across Solana and every supported EVM chain. Inspect their services and bring them into 3D."  data-i18n-attr="content:agents_index.meta_description"/>
		<meta property="og:type" content="website" />
		<meta property="og:site_name" content="three.ws" />
		<meta property="og:url" content="https://three.ws/agents" />
		<meta property="og:title" content="Agents Index · three.ws"  data-i18n-attr="content:agents_index.meta_og_title"/>
		<meta property="og:description" content="Browse every agent registered on three.ws across Solana and every supported EVM chain. Inspect their services and bring them into 3D."  data-i18n-attr="content:agents_index.meta_og_description"/>
		<meta property="og:image" content="https://three.ws/api/page-og?s=agent-tools&amp;t=Agents+Index&amp;d=Browse+every+registered+agent+on+three.ws.&amp;p=%2Fagents" />
		<meta name="twitter:card" content="summary_large_image" />
		<meta name="twitter:site" content="@trythreews" />
		<meta name="theme-color" content="#0a0a0c" />
		<link rel="icon" href="/favicon.ico" sizes="any" />
		<link rel="icon" type="image/svg+xml" href="/favicon.svg" />
		<link rel="apple-touch-icon" href="/apple-touch-icon.png" />
		<link rel="manifest" href="/manifest.webmanifest" />
		<link rel="canonical" href="https://three.ws/agents" />
		<link rel="stylesheet" href="/fonts/fonts.css" />
		<link rel="stylesheet" href="/nav.css" />
		<link rel="stylesheet" href="/footer.css" />
		<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"Agents Index","description":"Browse every public 3D AI agent on three.ws.","url":"https://three.ws/agents","isPartOf":{"@type":"WebSite","name":"three.ws","url":"https://three.ws"}}</script>
		<style>
		:root {
			--bg: #05060c; --panel: rgba(13,15,23,0.72); --panel-solid: #0c0e16; --line: rgba(255,255,255,0.09);
			--ink: #eaeefa; --muted: #8a92a8; --faint: rgba(255,255,255,0.45);
			--cyan: #5fe3ff; --violet: #a07bff; --gold: #fbbf24; --up: #34d399; --down: #f87171;
			--r: 14px; --r-sm: 9px;
			--mono: ui-monospace, "JetBrains Mono", SFMono-Regular, Menlo, monospace;
		}
		* { box-sizing: border-box; }
		html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--ink);
			font: 15px/1.55 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
			-webkit-font-smoothing: antialiased; }
		body { background: radial-gradient(52% 44% at 88% -4%, color-mix(in srgb, var(--violet) 14%, transparent), transparent 68%),
			radial-gradient(44% 38% at 2% -2%, color-mix(in srgb, var(--cyan) 11%, transparent), transparent 62%), var(--bg); }
		a { color: inherit; text-decoration: none; }
		button { font: inherit; color: inherit; cursor: pointer; }
		.wrap { max-width: 1200px; margin: 0 auto; padding: 0 18px 90px; }

		/* hero */
		.hero { padding: 40px 0 24px; }
		.eyebrow { display: inline-flex; align-items: center; gap: 8px; font: 600 12px/1 var(--mono);
			letter-spacing: .04em; text-transform: uppercase; color: var(--cyan); padding: 6px 12px;
			border: 1px solid color-mix(in srgb, var(--cyan) 28%, var(--line)); border-radius: 999px;
			background: color-mix(in srgb, var(--cyan) 8%, transparent); margin-bottom: 18px; }
		.hero h1 { font-size: clamp(28px, 5vw, 44px); line-height: 1.05; letter-spacing: -0.025em;
			margin: 0 0 10px; font-weight: 800; }
		.hero h1 span { background: linear-gradient(100deg, var(--cyan), var(--violet));
			-webkit-background-clip: text; background-clip: text; color: transparent; }
		.hero p { max-width: 580px; font-size: 15.5px; color: var(--muted); margin: 0 0 22px; }
		.hero-ctas { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
		.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 18px; border-radius: 8px;
			font-weight: 600; font-size: 14px; border: 1px solid rgba(255,255,255,0.14); transition: all var(--duration-fast); cursor: pointer; }
		.btn-primary { background: linear-gradient(135deg, var(--cyan), var(--violet)); border-color: transparent; color: #050610; }
		.btn-primary:hover { opacity: .88; }
		.btn-ghost { background: rgba(255,255,255,0.05); color: var(--ink); }
		.btn-ghost:hover { background: rgba(255,255,255,0.1); }
		.btn:active { transform: translateY(1px); }
		.btn:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
		.btn[disabled] { opacity: .55; cursor: progress; }

		/* source chips */
		.src-chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }
		.chip { padding: 7px 15px; border-radius: 999px; font: 600 13px/1 inherit;
			background: var(--panel); border: 1px solid var(--line); color: var(--muted); transition: all var(--duration-fast); }
		.chip:hover { color: var(--ink); border-color: color-mix(in srgb, var(--cyan) 30%, var(--line)); }
		.chip.is-active { color: #050610; background: linear-gradient(135deg, var(--cyan), var(--violet)); border-color: transparent; }
		.chip:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }

		/* controls */
		.controls { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 20px; }
		.search-wrap { flex: 1; min-width: 200px; position: relative; }
		.search-wrap input { width: 100%; background: var(--panel); border: 1px solid var(--line);
			border-radius: 8px; padding: 9px 14px 9px 36px; font: 14px/1 inherit; color: var(--ink); outline: none;
			transition: border-color var(--duration-fast); }
		.search-wrap input:focus { border-color: color-mix(in srgb, var(--cyan) 50%, var(--line)); }
		.search-wrap svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 16px; height: 16px; stroke: var(--muted); }
		select.ctrl { background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
			padding: 9px 12px; font: 14px/1 inherit; color: var(--ink); outline: none; cursor: pointer; }
		select.ctrl:focus { border-color: color-mix(in srgb, var(--cyan) 50%, var(--line)); }
		/* Both controls set outline:none for the mouse-focus look, which left a
		   keyboard user with a 1px border tint as the only cue. Give them the same
		   ring the chips and buttons on this page already use. */
		.search-wrap input:focus-visible,
		select.ctrl:focus-visible { outline: 2px solid var(--cyan); outline-offset: 2px; }
		.count-label { font-size: 13px; color: var(--muted); white-space: nowrap; }

		/* grid */
		.agents-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
		@media (max-width: 480px) { .agents-grid { grid-template-columns: 1fr; } }

		/* agent card */
		.ag-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r);
			padding: 18px; display: flex; flex-direction: column; gap: 12px; transition: border-color var(--duration-fast), transform var(--duration-fast); position: relative; overflow: hidden; }
		.ag-card:hover { border-color: color-mix(in srgb, var(--cyan) 35%, var(--line)); transform: translateY(-2px); }
		/* The card's link is a transparent overlay, so its own focus ring would be
		   invisible: promote it to the card itself for keyboard users. */
		.ag-card:focus-within { border-color: color-mix(in srgb, var(--cyan) 55%, var(--line));
			outline: 2px solid var(--cyan); outline-offset: 2px; }
		.ag-card-link { position: absolute; inset: 0; z-index: 1; }
		.ag-card-link:focus { outline: none; }
		.ag-top { display: flex; gap: 12px; align-items: flex-start; }
		.ag-avatar { width: 52px; height: 52px; border-radius: 11px; object-fit: cover;
			background: rgba(255,255,255,0.07); border: 1px solid var(--line); flex-shrink: 0; }
		.ag-avatar-fallback { width: 52px; height: 52px; border-radius: 11px; background: linear-gradient(135deg, var(--violet), var(--cyan));
			display: flex; align-items: center; justify-content: center; font: 800 22px/1 var(--mono); color: #050610; flex-shrink: 0; }
		.ag-meta { min-width: 0; flex: 1; }
		.ag-name { font-weight: 700; font-size: 15.5px; line-height: 1.2; margin-bottom: 3px;
			white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
		.ag-badge-onchain { display: inline-flex; align-items: center; gap: 4px; font: 600 10.5px/1 var(--mono);
			color: var(--up); padding: 2px 7px; border-radius: 999px; background: color-mix(in srgb, var(--up) 12%, transparent);
			border: 1px solid color-mix(in srgb, var(--up) 30%, transparent); white-space: nowrap; }
		/* per-source badge tints */
		.ag-badge-onchain[data-src="evm"]      { color: var(--cyan);   background: color-mix(in srgb, var(--cyan) 12%, transparent);   border-color: color-mix(in srgb, var(--cyan) 30%, transparent); }
		.ag-badge-onchain[data-src="metaplex"] { color: var(--gold);   background: color-mix(in srgb, var(--gold) 12%, transparent);   border-color: color-mix(in srgb, var(--gold) 30%, transparent); }
		.ag-badge-onchain[data-src="agenc"]    { color: var(--violet); background: color-mix(in srgb, var(--violet) 14%, transparent); border-color: color-mix(in srgb, var(--violet) 32%, transparent); }
		.ag-badge-onchain[data-src="threews"]  { color: var(--up);     background: color-mix(in srgb, var(--up) 12%, transparent);     border-color: color-mix(in srgb, var(--up) 30%, transparent); }
		.ag-desc { font-size: 13px; color: var(--muted); line-height: 1.45;
			overflow: hidden; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; }
		.ag-footer { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 4px; }
		.ag-skill { font-size: 11px; padding: 2px 8px; border-radius: 999px;
			background: rgba(255,255,255,0.06); border: 1px solid var(--line); color: var(--muted); }
		.ag-chats { font-size: 12px; color: var(--faint); margin-left: auto; font-variant-numeric: tabular-nums; }

		/* skeleton */
		.ag-skel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 18px;
			display: flex; gap: 12px; }
		.sk { background: rgba(255,255,255,0.06); border-radius: 6px; animation: sk 1.4s ease-in-out infinite; }
		@keyframes sk { 0%,100%{opacity:.5} 50%{opacity:.9} }

		/* state */
		.state { text-align: center; padding: 60px 20px; color: var(--muted); }
		.state b { display: block; font-size: 17px; color: var(--ink); margin-bottom: 8px; }
		.state a { color: var(--cyan); }
		.state-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 18px; }

		/* visually hidden, still read by screen readers and still focusable */
		.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
			overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

		/* load more */
		.load-more-wrap { text-align: center; margin-top: 28px; }
		/* Busy shows as aria-busy plus a real spinner instead of a swapped label,
		   so a translated "Load more" survives a fetch round trip. */
		#ag-more[aria-busy="true"] { pointer-events: none; }
		#ag-more[aria-busy="true"]::after { content: ''; width: 13px; height: 13px; border: 2px solid currentColor;
			border-top-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
		@keyframes spin { to { transform: rotate(360deg); } }
		@media (prefers-reduced-motion: reduce) {
			#ag-more[aria-busy="true"]::after { animation-duration: 2.4s; }
			.sk { animation: none; opacity: .7; }
			.ag-card { transition: none; }
		}

		@media (max-width: 600px) {
			.controls { gap: 6px; }
			.hero h1 { font-size: 26px; }
			.src-chips { gap: 8px; }
		}
		/* Site-wide 44px tap-target floor. mobile.css sets it for the fields by id;
		   these chips and CTAs carry page-local classes it cannot know. Gated on
		   pointer type rather than width, for the reason mobile.css gives: a 768px
		   tablet is still touch-only, and a width gate left the source chips at
		   39px there. inline-flex keeps the label centred in the taller box. */
		@media (hover: none) and (pointer: coarse) {
			.chip, .btn { min-height: 44px; display: inline-flex; align-items: center; }
		}
		</style>
		<!-- Site-wide mobile polish: horizontal-scroll guard, iOS focus-zoom
		     fix, and the 44px tap-target floor. Loaded last so it wins on
		     equal specificity over this page's inline styles. -->
		<link rel="stylesheet" href="/mobile.css" />

	<meta name="twitter:title" content="Agents Index · three.ws" data-i18n-attr="content:agents_index.meta_twitter_title">
	<meta name="twitter:description" content="Browse every registered agent on three.ws." data-i18n-attr="content:agents_index.meta_twitter_description">
	<meta name="twitter:image" content="https://three.ws/api/page-og?s=agent-tools&amp;t=Agents+Index&amp;d=Browse+every+registered+agent+on+three.ws.&amp;p=%2Fagents">

	<meta name="robots" content="index, follow, max-image-preview:large, max-snippet:-1">
	<meta property="og:locale" content="en_US">
	<meta property="og:image:alt" content="Agents Index — three.ws">
</head>
	<body>
		<header><div id="nav-container"></div></header>
		<script src="/nav.js"></script>

		<main class="wrap">
			<section class="hero">
				<div class="eyebrow">3D AI Agents</div>
				<h1 data-i18n-html="agents_index.agent_index">Agent <span>Index</span></h1>
				<p data-i18n="agents_index.every_on_chain_agent_across_solana_and">Every on-chain agent, across Solana and every EVM chain, not just the ones launched on three.ws. We crawl the Metaplex Agent Registry, the AgenC protocol and ERC-8004 registries so the whole ecosystem lives in one place. Inspect their services and bring any of them into 3D.</p>
				<div class="hero-ctas">
					<a class="btn btn-primary" href="/create-agent" data-i18n="agents_index.create_an_agent">Create an agent</a>
					<a class="btn btn-ghost" href="/marketplace" data-i18n="agents_index.marketplace">Marketplace</a>
					<a class="btn btn-ghost" href="/spotlight" data-i18n="agents_index.spotlight">Community Spotlight</a>
				</div>
			</section>

			<div class="src-chips" role="group" aria-label="Filter by source">
				<button type="button" class="chip is-active" data-src="agents" aria-pressed="true" data-i18n="agents_index.all_sources">All sources</button>
				<button type="button" class="chip" data-src="solana" aria-pressed="false" data-i18n="agents_index.solana">Solana</button>
				<button type="button" class="chip" data-src="onchain" aria-pressed="false" data-i18n="agents_index.evm">EVM</button>
			</div>

			<div class="controls">
				<div class="search-wrap">
					<label class="sr-only" for="ag-search" data-i18n="agents_index.search_agents_label">Search agents by name or description</label>
					<svg viewBox="0 0 24 24" fill="none" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
						<circle cx="11" cy="11" r="8"/><line x1="21" y1="21" x2="16.65" y2="16.65"/>
					</svg>
					<input id="ag-search" type="search" placeholder="Search agents…" autocomplete="off" data-i18n-attr="placeholder:agents_index.search_placeholder" />
				</div>
				<label class="sr-only" for="ag-sort" data-i18n="agents_index.sort_label">Sort agents</label>
				<select id="ag-sort" class="ctrl">
					<option value="newest" data-i18n="agents_index.newest_first">Newest first</option>
					<option value="name" data-i18n="agents_index.name_a_z">Name A-Z</option>
				</select>
				<span id="ag-count" class="count-label" role="status" aria-live="polite"></span>
			</div>

			<div id="ag-grid" class="agents-grid" aria-label="Agents" aria-busy="true"></div>
			<!-- The empty and error panels are the only feedback a failed or fruitless
			     load gives, so announce them: without a live region a screen-reader
			     user hears the busy state end and nothing after it. -->
			<div id="ag-state" class="state" role="status" aria-live="polite" hidden></div>
			<div class="load-more-wrap" id="ag-more-wrap" hidden>
				<button type="button" id="ag-more" class="btn btn-ghost" data-i18n="agents_index.load_more">Load more</button>
			</div>
		</main>

		<script type="module">
		// One unified feed across every registry we crawl: ERC-8004 (EVM, via the
		// erc8004-crawl cron), the Metaplex Agent Registry + AgenC protocol (Solana,
		// via the solana-agents-crawl cron), and three.ws's own agents. The
		// source=agents meta-source returns every on-chain agent and no avatars.
		const API = '/api/explore';
		const $ = (id) => document.getElementById(id);
		const esc = (s) => String(s ?? '').replace(/[&<>"']/g, (c) => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));

		const grid   = $('ag-grid');
		const stateEl = $('ag-state');
		const countEl = $('ag-count');
		const moreWrap = $('ag-more-wrap');
		const moreBtn  = $('ag-more');
		const searchEl = $('ag-search');
		const sortEl   = $('ag-sort');

		const SOURCES = new Set(['agents', 'solana', 'onchain']);
		const params = new URLSearchParams(location.search);
		const state = {
			// ?source= and ?q= make a filtered view shareable and reload-safe.
			q: (params.get('q') || '').trim().slice(0, 80),
			sort: params.get('sort') === 'name' ? 'name' : 'newest',
			source: SOURCES.has(params.get('source')) ? params.get('source') : 'agents',
			cursor: null,
			loading: false,
			items: [],
			// Monotonic token: only the newest request may touch the DOM, so a
			// filter change mid-flight supersedes the request it replaced instead
			// of being dropped or racing it to render.
			req: 0,
		};

		function skeletons(n = 8) {
			return Array.from({length: n}, () => `
				<div class="ag-skel" aria-hidden="true">
					<div class="sk" style="width:52px;height:52px;border-radius:11px;flex-shrink:0"></div>
					<div style="flex:1;display:flex;flex-direction:column;gap:8px;padding-top:2px">
						<div class="sk" style="width:55%;height:15px"></div>
						<div class="sk" style="width:88%;height:12px"></div>
						<div class="sk" style="width:70%;height:12px"></div>
					</div>
				</div>`).join('');
		}

		function initial(name) {
			const n = (name || '?').trim();
			return (n[0] || '?').toUpperCase();
		}

		// Label + tint key for the source badge, derived from kind/source/chain.
		function badge(a) {
			if (a.kind === 'onchain') return { label: a.chainShortName || 'EVM', src: 'evm' };
			if (a.source === 'metaplex') return { label: 'Metaplex', src: 'metaplex' };
			if (a.source === 'agenc') return { label: 'AgenC', src: 'agenc' };
			if (a.source === 'three.ws') return { label: 'three.ws', src: 'threews' };
			return { label: 'Solana', src: 'metaplex' };
		}

		// Each kind links to its canonical detail page: EVM to /a/<chain>/<id>,
		// our Solana agents to /agents/<id>, external Solana to its detail page
		// when the API provides one, else the 3D viewer or the on-chain explorer
		// when there's nothing else to open. A registry row with no asset and no
		// ref has nowhere real to go, so it returns null and the card renders
		// without a link rather than with a dead stub link.
		function hrefFor(a) {
			if (a.kind === 'onchain' && a.chainId != null && a.agentId != null) {
				return `/a/${encodeURIComponent(a.chainId)}/${encodeURIComponent(a.agentId)}`;
			}
			if (a.source === 'three.ws' && a.agentId) return `/agents/${encodeURIComponent(a.agentId)}`;
			return a.detailUrl || a.viewerUrl || a.explorerUrl || null;
		}

		function cardHtml(a) {
			// Registry thumbnails live on arbitrary third-party hosts (IPFS, plain
			// http) — route them through the same-origin /api/img proxy so a dead
			// host degrades to the lettered fallback instead of a console error.
			const img = a.image ? `/api/img?url=${encodeURIComponent(a.image)}` : null;
			const avatar = img
				? `<img class="ag-avatar" src="${esc(img)}" alt="" loading="lazy" data-fallback="sibling" /><div class="ag-avatar-fallback" style="display:none">${esc(initial(a.name))}</div>`
				: `<div class="ag-avatar-fallback">${esc(initial(a.name))}</div>`;
			const services = (a.services || []).map((s) => s && s.name).filter(Boolean)
				.concat(a.skills || []).slice(0, 3);
			const x402 = a.x402Support ? `<span class="ag-skill" title="Accepts x402 payments">x402</span>` : '';
			const b = badge(a);
			const href = hrefFor(a);
			const offsite = href ? /^https?:/i.test(href) : false;
			const ext = offsite ? ' target="_blank" rel="noopener noreferrer"' : '';
			const label = offsite ? `${a.name || 'Agent'} (opens on ${b.label})` : (a.name || 'Agent');
			const link = href
				? `<a class="ag-card-link" href="${esc(href)}"${ext} aria-label="${esc(label)}"></a>`
				: '';

			return `<div class="ag-card">
				${link}
				<div class="ag-top">
					<div style="display:flex;flex-direction:column;gap:6px;align-items:center">
						${avatar}
						<span class="ag-badge-onchain" data-src="${esc(b.src)}">✦ ${esc(b.label)}</span>
					</div>
					<div class="ag-meta">
						<div class="ag-name">${esc(a.name)}</div>
						${a.description ? `<div class="ag-desc">${esc(a.description)}</div>` : ''}
					</div>
				</div>
				${services.length || x402 ? `<div class="ag-footer">
					${services.map((s) => `<span class="ag-skill">${esc(s)}</span>`).join('')}
					${x402}
				</div>` : ''}
			</div>`;
		}

		// `appendFrom` is the index the newest page starts at. In the server's own
		// order that page only has to be appended: rebuilding the whole grid on
		// every "load more" re-decodes every thumbnail already on screen and makes
		// the list flash once a few pages are in. Name sort interleaves the new
		// page into the existing one, so it still rebuilds.
		function renderList(appendFrom = null) {
			if (appendFrom !== null && state.sort === 'newest') {
				grid.insertAdjacentHTML('beforeend', state.items.slice(appendFrom).map(cardHtml).join(''));
			} else {
				const items = state.sort === 'name'
					? [...state.items].sort((a, b) => (a.name || '').localeCompare(b.name || ''))
					: state.items;
				grid.innerHTML = items.map(cardHtml).join('');
			}
			grid.setAttribute('aria-busy', 'false');
		}

		// Keep the address bar in step with the view so a filtered directory is a
		// shareable link and survives a refresh. replaceState, not pushState: a
		// per-keystroke history entry would trap the back button in the search box.
		function syncUrl() {
			const u = new URL(location.href);
			const set = (k, v) => (v ? u.searchParams.set(k, v) : u.searchParams.delete(k));
			set('source', state.source === 'agents' ? '' : state.source);
			set('q', state.q);
			set('sort', state.sort === 'newest' ? '' : state.sort);
			history.replaceState(null, '', u.pathname + (u.search || '') + u.hash);
		}

		function showState(html) {
			stateEl.innerHTML = html;
			stateEl.hidden = false;
		}

		function emptyStateHtml() {
			if (state.q) {
				return `<b>No agents match "${esc(state.q)}"</b>Nothing in this source matches that search.
					<div class="state-actions">
						<button type="button" class="btn btn-ghost" data-act="clear-search">Clear search</button>
						<a class="btn btn-ghost" href="/create-agent">Create an agent</a>
					</div>`;
			}
			return `<b>No agents in this source yet</b>The on-chain registries are crawled continuously, so this fills in as they land.
				<div class="state-actions">
					<button type="button" class="btn btn-ghost" data-act="retry">Check again</button>
					<a class="btn btn-primary" href="/create-agent">Create the first one</a>
				</div>`;
		}

		function errorStateHtml(detail) {
			return `<b>Couldn't load agents</b>The agent registry feed didn't respond (${esc(detail)}). Your connection or our API may be having a moment.
				<div class="state-actions">
					<button type="button" class="btn btn-primary" data-act="retry">Try again</button>
					<a class="btn btn-ghost" href="/marketplace">Browse the marketplace</a>
				</div>`;
		}

		// A page that fails mid-list is a different failure from a page that fails
		// on arrival: the agents already loaded are still good and the cursor still
		// points at the next page, so this offers a resume rather than the reset
		// "Try again" does. Retrying from page one after loading four pages threw
		// away everything the visitor had scrolled through.
		function moreErrorStateHtml(detail) {
			return `<b>Couldn't load more agents</b>The next page didn't arrive (${esc(detail)}). The ${state.items.length} already listed are still here.
				<div class="state-actions">
					<button type="button" class="btn btn-primary" data-act="retry-more">Resume loading</button>
					<button type="button" class="btn btn-ghost" data-act="retry">Start over</button>
				</div>`;
		}

		function setBusy(on) {
			moreBtn.disabled = on;
			moreBtn.setAttribute('aria-busy', on ? 'true' : 'false');
		}

		async function load(reset = false) {
			// A reset always wins over whatever is in flight; only a "load more"
			// defers, since two appends would duplicate the same cursor page.
			if (state.loading && !reset) return;
			const req = ++state.req;
			state.loading = true;
			if (reset) {
				state.cursor = null;
				state.items = [];
				grid.innerHTML = skeletons();
				grid.setAttribute('aria-busy', 'true');
				stateEl.hidden = true;
				moreWrap.hidden = true;
				countEl.textContent = '';
			} else {
				setBusy(true);
			}

			const url = new URL(API, location.origin);
			url.searchParams.set('source', state.source);
			url.searchParams.set('limit', '24');
			if (state.q) url.searchParams.set('q', state.q);
			if (state.cursor) url.searchParams.set('cursor', state.cursor);

			try {
				const r = await fetch(url, { headers: { accept: 'application/json' } });
				if (!r.ok) throw new Error('HTTP ' + r.status);
				const data = await r.json();
				if (req !== state.req) return;
				// Defensive: source=agents never returns avatars, but filter anyway so
				// a future server change can't leak non-agent rows into this view.
				const items = (data.items || []).filter((it) => it.kind === 'onchain' || it.kind === 'solana');

				const appendFrom = state.items.length;
				state.items.push(...items);
				state.cursor = data.nextCursor || null;
				renderList(reset ? null : appendFrom);

				const more = !!data.nextCursor;
				countEl.textContent = state.items.length > 0
					? `${state.items.length}${more ? '+' : ''} ${state.items.length === 1 && !more ? 'agent' : 'agents'}`
					: '';
				moreWrap.hidden = !more;

				// Clearing the panel is part of the success path: a resumed "load
				// more" used to leave the error it recovered from sitting under the
				// freshly loaded results.
				if (!state.items.length) showState(emptyStateHtml());
				else stateEl.hidden = true;
			} catch (err) {
				if (req !== state.req) return;
				const detail = err?.message || 'network error';
				if (reset) {
					grid.innerHTML = '';
					grid.setAttribute('aria-busy', 'false');
					showState(errorStateHtml(detail));
				} else {
					// A failed "load more" keeps every page already on screen and keeps
					// state.cursor, so the panel's resume picks up the interrupted page
					// instead of restarting the list.
					showState(moreErrorStateHtml(detail));
				}
				moreWrap.hidden = true;
			} finally {
				if (req === state.req) {
					state.loading = false;
					setBusy(false);
				}
			}
		}

		// The empty/error states are re-rendered on every load, so their buttons are
		// bound by delegation rather than re-wired each time.
		stateEl.addEventListener('click', (e) => {
			const act = e.target.closest('[data-act]')?.dataset.act;
			if (!act) return;
			if (act === 'clear-search') {
				searchEl.value = '';
				state.q = '';
				syncUrl();
			}
			// "retry-more" resumes the interrupted page off the surviving cursor;
			// every other action starts the list over.
			load(act !== 'retry-more');
		});

		let _t;
		searchEl.addEventListener('input', (e) => {
			clearTimeout(_t);
			_t = setTimeout(() => {
				const next = e.target.value.trim();
				// Trailing whitespace and an edit that lands back on the same term
				// both settle here; refetching the identical query would rebuild the
				// grid and throw away every page past the first for nothing.
				if (next === state.q) return;
				state.q = next;
				syncUrl();
				load(true);
			}, 320);
		});
		sortEl.addEventListener('change', (e) => { state.sort = e.target.value; syncUrl(); renderList(); });
		moreBtn.addEventListener('click', () => load(false));

		document.querySelectorAll('.src-chips .chip').forEach((chip) => {
			chip.addEventListener('click', () => {
				if (chip.classList.contains('is-active')) return;
				document.querySelectorAll('.src-chips .chip').forEach((c) => {
					c.classList.toggle('is-active', c === chip);
					c.setAttribute('aria-pressed', c === chip ? 'true' : 'false');
				});
				state.source = chip.dataset.src;
				syncUrl();
				load(true);
			});
		});

		// Reflect the URL-restored filters in the controls before the first fetch,
		// so a shared /agents?source=solana&q=trader link opens showing what it says.
		searchEl.value = state.q;
		sortEl.value = state.sort;
		document.querySelectorAll('.src-chips .chip').forEach((c) => {
			const on = c.dataset.src === state.source;
			c.classList.toggle('is-active', on);
			c.setAttribute('aria-pressed', on ? 'true' : 'false');
		});

		load(true);
		</script>
		<script src="/brand.js" defer></script>
		<div id="footer-container"></div>
		<script src="/footer.js"></script>
		<!-- Runtime i18n: detects locale, swaps annotated copy, mounts <lang-switcher>. -->
	<script type="module" src="/i18n.js"></script>
</body>
</html>