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.

1.1 KB
Raw Blame
export type Page = (el: HTMLElement, params: string[], onCleanup: (fn: () => void) => void) => Promise<void> | void;

export type Route = { pattern: RegExp; page: () => Promise<Page>; nav: string };

export function startRouter(el: HTMLElement, routes: Route[], onNav: (nav: string) => void): void {
  let cleanups: (() => void)[] = [];
  let seq = 0;
  async function render() {
    const id = ++seq;
    cleanups.forEach((fn) => fn());
    cleanups = [];
    const hash = location.hash.replace(/^#/, '') || '/';
    const [path = '/', query = ''] = hash.split('?');
    for (const r of routes) {
      const m = path.match(r.pattern);
      if (!m) continue;
      onNav(r.nav);
      const page = await r.page();
      if (id !== seq) return;
      const params = [...m.slice(1).map(decodeURIComponent)];
      if (query) params.unshift(query);
      el.scrollTop = 0;
      window.scrollTo(0, 0);
      await page(el, params, (fn) => cleanups.push(fn));
      return;
    }
    el.innerHTML = '<div class="empty"><p>Page not found.</p><p><a href="#/">Back to the overview</a></p></div>';
  }
  addEventListener('hashchange', () => void render());
  void render();
}