iterate.com / ui

git clone https://git.iterate.com/repos/ui

ui / examples / agent-chat-page.tsmain at d68d7e1

// agent-chat-page.ts — A NO-BUILD PAGE WITH AN AGENT'S CHAT (packages/ui/src/components/agent-chat/AGENTS.md): the chat agents.iterate.com shows, `AgentChat`, on a mind's own host, for a signed-in
// member. The worker writes the mind's agents into the page. The page holds one agent's context
// with the host's session and hands `AgentChat` what `useIterateContext` reads of it. Under each
// thing a person said is a row of the page's own (`itemFooter`): here, a link that opens that
// event in the chat's Events tab.
// Copy this file into a mind's config repo beside worker.ts, and route a slug to it in `fetch`:
//
//   if (routingSlug === "chat") {
//     const denied = this.auth.require(request);
//     if (denied) return denied;
//     using itx = this.getItx();
//     return await agentChatPage(itx); // awaited: `using` releases itx when the function returns
//   }
//
// It is then `chat--<mind>.iterate.app` (or `/minds/<mind>/chat/` where minds are
// paths). test/playwright/ui/agent-chat.spec.ts commits it to a fresh mind and opens it. A page
// you keep pins an exact version of @iterate-com/ui in place of `@main`.

/** The page, with what only the worker knows written into it: which mind this host is, and its
 *  agents, for the path box. */
export async function agentChatPage(scope: {
  whoami(): Promise<{ mindId: string; mindSlug: string }>;
}) {
  // The mind's init case installs the agents app (`installAgents(itx)`, as the default
  // template's does), so its root has `itx.agents`, which a plain scope's type does not name.
  const itx = scope as typeof scope & { agents: { list(): Promise<{ path: string }[]> } };
  const [{ mindId, mindSlug }, agents] = await Promise.all([itx.whoami(), itx.agents.list()]);
  const mind = { id: mindId, slug: mindSlug, agents: agents.map((agent) => agent.path) };
  // JSON inside a script element, `<` escaped so no value can close the element
  const data = JSON.stringify(mind).replaceAll("<", "\\u003c");
  return new Response(
    `<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Chat</title>
    <script type="importmap">
      { "imports": { "@iterate-com/ui/": "https://esm.iterate.com/@iterate-com/ui@main/" } }
    </script>
    <link rel="stylesheet" href="https://esm.iterate.com/@iterate-com/ui@main/styles.css" />
    <script type="application/json" id="mind">${data}</script>
  </head>
  <body>
    <div id="root"><p class="p-4 text-sm text-muted-foreground">Connecting…</p></div>
    <script type="module">
      import { html, render, useCallback, useState } from "@iterate-com/ui/page";
      import { AgentChat } from "@iterate-com/ui/components/agent-chat/agent-chat";
      import { Button } from "@iterate-com/ui/components/ui/button";
      import { Input } from "@iterate-com/ui/components/ui/input";
      import { Toaster } from "@iterate-com/ui/components/ui/sonner";
      import { createIterateClient, useContextStub, useIterateContext } from "@iterate-com/ui/live";

      // what the worker knows and the page doesn't: which mind this host is, and its agents
      const mind = JSON.parse(document.getElementById("mind").textContent);
      // the session this host's sign-in set (the worker only serves a signed-in member)
      const { api, info } = await createIterateClient().authenticate(location.href);

      function Chat({ path }) {
        // the chat's own state: the tab, the open trace, the Events tab's choices
        const [state, setState] = useState({});
        const patch = (change) => setState((previous) => ({ ...previous, ...change }));
        const root = useContextStub(() => api.minds.get(mind.id), []);
        const agent = useContextStub(root.stub ? () => root.stub.cd(path) : null, [root.stub, path]);
        // ONE live read of the agent's context, with the events and the history the chat asks for
        const context = useIterateContext(agent.stub, AgentChat.contextOptions);
        // one function while the mind's handle is the same: a new one would draw every row again
        const signedUrl = useCallback(async (file) => {
          // asked of the mind: only an iterate agent's context links to the root, where files is
          if (!root.stub) throw new Error("not connected");
          return (await root.stub.files.get(file).url()).url;
        }, [root.stub]);
        return html\`<\${AgentChat}
          path=\${path}
          context=\${context}
          error=\${root.error || agent.error}
          state=\${state}
          onStateChange=\${patch}
          onMessage=\${async ({ message, files }) => {
            // a handle of its own: a person can send before the chat's contexts have opened
            const itx = await api.minds.get(mind.id);
            try {
              await itx.agents.get(path).message({ message, files });
            } finally {
              itx[Symbol.dispose]();
            }
          }}
          onAppend=\${agent.stub ? (events) => agent.stub.append(...events) : undefined}
          signedUrl=\${signedUrl}
          itemFooter=\${(item) =>
            item.kind === "user" &&
            html\`<div class="flex justify-end pb-1">
              <\${Button} variant="link" size="xs" className="text-muted-foreground" onClick=\${() => patch({ view: "events", event: item.offset })}>
                event #\${item.offset}
              <//>
            </div>\`}
        />\`;
      }

      function App() {
        const [path, setPath] = useState(new URLSearchParams(location.search).get("agent") || mind.agents[0] || "");
        const open = (event) => {
          event.preventDefault();
          const next = String(new FormData(event.currentTarget).get("agent") || "");
          history.replaceState(null, "", "?agent=" + encodeURIComponent(next));
          setPath(next);
        };
        // The chat fills a column of a set height. Its copy buttons answer with a toast, which
        // needs @iterate-com/ui's Toaster on the page, once.
        return html\`<div class="flex h-dvh flex-col">
          <header class="flex shrink-0 flex-wrap items-center gap-3 border-b px-4 py-2 text-sm">
            <span class="font-medium">\${mind.slug}</span>
            <span class="text-muted-foreground">as \${info.principal.email || info.principal.actor}</span>
            <form class="ml-auto flex items-center gap-2" onSubmit=\${open}>
              <\${Input} name="agent" list="agents" defaultValue=\${path} aria-label="Agent" className="h-8 w-64 font-mono" />
              <datalist id="agents">\${mind.agents.map((option) => html\`<option key=\${option} value=\${option} />\`)}</datalist>
              <\${Button} type="submit" size="sm">Open<//>
            </form>
            <form method="post" action="/.auth/logout">
              <\${Button} type="submit" variant="outline" size="sm">Sign out<//>
            </form>
          </header>
          \${path
            ? html\`<\${Chat} key=\${path} path=\${path} />\`
            : html\`<p class="p-4 text-sm text-muted-foreground">This mind has no agents yet.</p>\`}
          <\${Toaster} />
        </div>\`;
      }

      render(html\`<\${App} />\`, document.getElementById("root"));
    </script>
  </body>
</html>
`,
    {
      headers: { "content-type": "text/html; charset=utf-8", "cache-control": "no-store" },
    },
  );
}