Merge the template's Dash at iterate/private 3de3de7d
Misha Kaletsky, 2026-11-25 15:23 UTC · 1de534a2d3fe0a1962ada5ce8cf544a44e771e36 · a merge of d8a80a8 and 2907dfd, shown against d8a80a8
6 files changed
- A agent-catalog.ts
- A framed-sign-in.ts
- M index.html
- M public/app.css
- M public/app.js
- A public/share-links.js
A agent-catalog.ts
@@ -0,0 +1,36 @@ +// agent-catalog.ts — WHICH CONTEXT IS AN AGENT: the catalog on /agents, read in one call, and the +// test of one path against it. api.ts `taskDeps` uses both, since a follower's row writes +// to the path it names. +// @ts-expect-error -- a plain browser module of the Dash, with no types; this file uses `agentPathOf` alone +import { agentPathOf } from "./public/share-links.js"; + +/** Whether `path` is an agent in the catalog on /agents (its live state `{ agents: { [path]: row } }`): + * a row that said what runs it or what it is called. A row with neither is a folder + * (@iterate-com/ui lib/agent-summary.ts `folderTest`), and the root and any path outside /agents/ + * are refused before (`agentPathOf`). */ +export function isAgentOf(catalog: unknown, path: string): boolean { + if (!agentPathOf(path)) return false; + // the catalog's live state is `unknown` to its reader: iterate/agents writes `agents` as a map + // from a path to its row, and a state of any other shape has no row for the path + const agents = ( + catalog as { agents?: Record<string, { kind?: string; title?: string } | null> } | null + )?.agents; + const row = agents && Object.hasOwn(agents, path) ? agents[path] : null; + return Boolean(row && (row.kind || row.title)); +} + +/** What `agentCatalogOf` reaches of the mind: the catalog's facet on /agents. */ +export type CatalogItx = { + cd(path: string): { + facets: { get(name: string): { liveSnapshot(): Promise<{ state: unknown }> } }; + }; +}; + +/** The catalog on /agents (its live state), or null. It touches /agents alone. */ +export async function agentCatalogOf(itx: CatalogItx): Promise<unknown> { + try { + return (await itx.cd("/agents").facets.get("agents").liveSnapshot()).state; + } catch { + return null; + } +}
A framed-sign-in.ts
@@ -0,0 +1,39 @@ +// framed-sign-in.ts — what the Dash's host answers a signed-out request that comes from a frame. +// worker.ts wraps the member guard's answer with it, on the Dash's host alone. + +/** A SIGNED-OUT REQUEST FOR THE DASH IN A FRAME (another app of the mind shows an agent's chat in + * one: the Tasks app's "Chat with …", `?embed=1`). Each host of the mind has a session of its + * own, and the platform signs a person in on a host only for a page load of the whole tab + * (`sec-fetch-dest: document`): a frame gets the guard's plain 401, the words "Sign in" with no + * link, and a person cannot go on from there. `refused` is the guard's answer, or null for a + * member. For a frame's GET this answers the SAME refusal, the same status and challenge, with + * words that say what to do: open the Dash once in a tab of its own, where the platform signs + * the person in, and come back; the frame then loads again by itself. Nobody is signed in here, + * and nothing of the mind is shown: the page names this host alone. Any other request gets the + * guard's answer as it is. */ +export function framedSignIn(request: Request, refused: Response | null): Response | null { + if ( + !refused || + refused.status !== 401 || + request.method !== "GET" || + request.headers.get("sec-fetch-dest") !== "iframe" + ) + return refused; + const headers = new Headers(refused.headers); + headers.set("content-type", "text/html; charset=utf-8"); + headers.set("cache-control", "no-store"); + const dash = `${new URL(request.url).origin}/`; + return new Response( + `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Sign in to the Dash</title> +<style>body{margin:0;padding:1.5rem;font:14px/1.5 system-ui,sans-serif;color:#18181b;background:#fff}@media (prefers-color-scheme:dark){body{color:#fafafa;background:#09090b}a{color:#fafafa}}p{margin:0 0 .75rem;max-width:34rem}</style> +<p><strong>This browser is not signed in to the Dash yet.</strong></p> +<p><a href="${dash}" target="_blank" rel="noopener">Open the Dash</a> once in a tab of its own. Then come back: this chat loads by itself.</p> +<script> + // back from the Dash's own tab: the session is there now, so load the frame again + addEventListener("visibilitychange", () => { if (document.visibilityState === "visible") location.reload(); }); + addEventListener("focus", () => location.reload()); +</script> +`, + { status: 401, headers }, + ); +}
M index.html
@@ -12,11 +12,11 @@ <script type="importmap"> { "imports": { - "@iterate-com/ui/": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T154040Z-83b4390/", - "react": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T154040Z-83b4390/react", - "react/": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T154040Z-83b4390/react/", - "react-dom": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T154040Z-83b4390/react-dom", - "react-dom/": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T154040Z-83b4390/react-dom/", + "@iterate-com/ui/": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T221123Z-9ecb4a7/", + "react": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T221123Z-9ecb4a7/react", + "react/": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T221123Z-9ecb4a7/react/", + "react-dom": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T221123Z-9ecb4a7/react-dom", + "react-dom/": "https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T221123Z-9ecb4a7/react-dom/", "lucide-react": "https://esm.iterate.com/lucide-react@1.48.0?external=react,react-dom" } } @@ -35,20 +35,29 @@ <!-- THE FRAME'S SHAPE, said before the first paint. The body draws the app's frame from HTML and app.css alone, in the places the shell will draw it, so nothing moves when the app takes over. These lines decide what the shell decides: the sidebar as the person left it - (the cookie `sidebar_state`, which the shell writes at each toggle), and whether the - page is an agent's, which has a header row. `data-frame` on the root element tells the - components that the person's wait began with this frame. --> + (the cookie `sidebar_state`, which the shell writes at each toggle), or an explicit + `#sidebar=collapsed` / `?sidebar=collapsed` (or `open`) in the URL when no choice is + stored, and whether the page is an agent's, which has a header row. app.js hands the Dash + the same default. `data-frame` on the root element tells the components that the person's + wait began with this frame. --> <script> { const query = new URLSearchParams(location.search); + const hash = new URLSearchParams(location.hash.replace(/^#/, "")); + const sidebarParam = hash.get("sidebar") || query.get("sidebar"); const agent = location.pathname.startsWith("/agents/") ? location.pathname.slice("/agents".length) : location.pathname === "/agents" ? query.get("contextPath") : null; const left = /(?:^|; )sidebar_state=(true|false)/.exec(document.cookie)?.[1]; - document.documentElement.dataset.sidebar = left === "false" ? "collapsed" : "open"; + const open = left ? left === "true" : sidebarParam !== "collapsed"; + document.documentElement.dataset.sidebar = open ? "open" : "collapsed"; if (agent) document.documentElement.dataset.page = "agent"; + // `?embed=1`: the page is shown in a frame of another app of the mind (the Tasks app shows + // an agent's chat in a sheet). The frame is narrow, so the shell draws no sidebar; its + // button is hidden too (public/app.css). The page is the same Dash, for the same member. + if (query.get("embed") === "1") document.documentElement.dataset.embed = ""; } </script> <!-- The stylesheet at the address the build serves it from: the shorter one answers with a @@ -56,7 +65,7 @@ <link rel="stylesheet" crossorigin - href="https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T154040Z-83b4390/dist/styles.css" + href="https://esm.iterate.com/@iterate-com/ui@0.1.0-main.20261124T221123Z-9ecb4a7/dist/styles.css" /> <!-- The look of this mind's apps: theme.css of /repos/stdlib, one file for every app. One line there makes them all dark. shadcn's components also ask for the class `dark`, so the
M public/app.css
@@ -321,3 +321,11 @@ padding: 1rem 1rem 0; } } + +/* An embedded view (index.html, `?embed=1`): no button for a sidebar that the frame has no room for. */ +[data-embed] [data-slot="sidebar-trigger"], +[data-embed] [data-slot="sidebar"], +[data-embed] [data-slot="sidebar-gap"], +[data-embed] .frame-sidebar { + display: none; +}
M public/app.js
@@ -140,7 +140,8 @@ // Personal items inside the account popover above Sign out. const accountLinks = [{ name: "Your sessions", href: `${stack}/sessions`, icon: HistoryIcon }]; -// The sidebar for a person who has not toggled it yet: as index.html drew it in its frame. A +// The sidebar for a person who has not toggled it yet: as index.html drew it in its frame (open +// by default, or collapsed when `#sidebar=collapsed` / `?sidebar=collapsed` is in the URL). A // choice they made wins. const sidebarDefaultOpen = document.documentElement.dataset.sidebar !== "collapsed";
A public/share-links.js
@@ -0,0 +1,80 @@ +// share-links.js — PURE: THE TEMPLATE'S ONE LINK-MAKING for guests, and the event that ends a link. +// The Dash's Share (the kit's, which app.js's `share` hands it to) and Meet's page append what it +// makes through the member's own stub; `itx.config.shareAgent` (../../../call/share-api.ts) appends it as the config. +// The events and their rules are the call's (../../../call/share.ts): a link lets a guest into that +// one agent's chat and call, through Meet's guest page (`https://meet.<zone>/join<agent>#<secret>`, +// ../../meet/app.ts), which checks the link by the call's state. It is a plain module because a +// page cannot import a .ts file; the config imports the Dash's file as it is. +// TWINS: apps/dash/public/share-links.js and apps/meet/public/share-links.js hold the same bytes, +// one in each app's folder, because an app follows the template whole. Change both together: +// core/lib's config-default-dash.test.ts holds them equal, and folds what they make through +// call/share.ts. + +export const LINK_MADE = "share/link-made"; +export const LINK_ENDED = "share/link-ended"; + +const HOUR = 3_600_000; +/** The longest life of a link: the share rules' own limit (call/share.ts `LONGEST_LIFETIME_MS`). */ +export const LONGEST_LIFETIME = 7 * 24 * HOUR; +/** How long a link lives when its maker says nothing. */ +export const DEFAULT_LIFETIME = 24 * HOUR; + +/** One part of an agent's path: a letter or digit first, then letters, digits and `._:@+-`. */ +const SEGMENT = /^[A-Za-z0-9][A-Za-z0-9._:@+-]{0,127}$/; + +/** The agent's path if `value` is one, exactly as written, else null: `/agents/<part>(/<part>)*`, + * at most 300 characters. Nothing is decoded or resolved, so the path checked is the path used. */ +export function agentPathOf(value) { + if (typeof value !== "string" || value.length > 300) return null; + const [empty, agents, ...rest] = value.split("/"); + if (empty !== "" || agents !== "agents" || rest.length === 0) return null; + return rest.every((part) => SEGMENT.test(part)) ? value : null; +} + +const base64url = (bytes) => + btoa(String.fromCharCode(...bytes)) + .replaceAll("+", "-") + .replaceAll("/", "_") + .replace(/=+$/, ""); + +/** A link's secret as the log knows it: the SHA-256 of its text, lower-case hex. */ +export const hashOfSecret = async (secret) => + [...new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(secret)))] + .map((byte) => byte.toString(16).padStart(2, "0")) + .join(""); + +/** A new link to `agent`, on the zone `zone`: 32 random bytes as its secret (base64url), its hash, + * a UUID as its id. It lives `lifetimeMs`. Answers the ONE event to append, and the link to show + * once, `https://meet.<zone>/join<agent>#<secret>`, with its id and its end: the secret is in the + * fragment alone, which no request carries, and the log keeps its hash alone. A path that is no + * agent's, a life that is no positive number, or one longer than `LONGEST_LIFETIME`, throws: a + * link never lives another time than the one its maker asked for. */ +export async function makeLink({ zone, agent, lifetimeMs, now }) { + if (!agentPathOf(agent)) throw new Error(`${agent} is no agent's path.`); + if (!(lifetimeMs > 0)) throw new Error("A link lives a positive number of milliseconds."); + if (lifetimeMs > LONGEST_LIFETIME) + throw new Error( + `A link lives 7 days at most (${LONGEST_LIFETIME} ms); ${lifetimeMs} ms was asked for.`, + ); + const secret = base64url(crypto.getRandomValues(new Uint8Array(32))); + const secretHash = await hashOfSecret(secret); + const link = crypto.randomUUID(); + const until = now + lifetimeMs; + return { + event: { + type: LINK_MADE, + idempotencyKey: `${LINK_MADE}:${link}`, + payload: { link, secretHash, until }, + }, + url: `https://meet.${zone}/join${agent}#${secret}`, + link, + until, + }; +} + +/** The event that ends `link`. */ +export const endLink = (link) => ({ + type: LINK_ENDED, + idempotencyKey: `${LINK_ENDED}:${link}`, + payload: { link }, +});