onboarding / app.tsmain at b65083a
// app.ts — THE ONBOARDING APP, as config/worker.ts mounts it on onboarding.<domain>: a centered,
// no-AI setup wizard for members. This repo holds the whole app: the routes that act on the mind
// (this file and the files beside it), the page (index.html, public/), the guides that the
// onboarding agent reads (onboarding.md, onboarding/) and the first cards (todo/). Endpoints:
// GET /steps.json onboarding step cards, checked first (steps.ts `onboardingCards`)
// POST /steps/<step>/<action> one step press (steps.ts `runStep`), same as itx.config.onboardingStep
// GET /ai-status.json configured AI credentials and mind address (verify-ai.ts `aiStatus`)
// POST /collect-openai-key mind secret-collection link for /secrets/openai
// POST /verify-ai real server-side test LLM inference request (verify-ai.ts `verifyAi`)
// GET /open-cloud 303 to this mind's /sign-in/open, on to its page on iterate Cloud (open-cloud.ts)
// anything else the page's files, and the theme that every app of the mind shares
// config/worker.ts guards the host before this file runs: only a member gets here, and nothing of
// the mind is read or written for anyone else.
// `iterate/agents` names the root `itx.agents` for the type-checker (its `InstalledAppRoots`).
import type {} from "iterate/agents";
import type { IterateContextApiWith } from "iterate/api";
import { type AppDescription, serveFiles, type IterateConfigEntrypoint } from "iterate/sdk";
import { openFile } from "@repos/stdlib/open-files.ts";
import theme from "@repos/stdlib/theme.css";
import index from "./index.html";
import files from "./public/";
import { launchOnboardingAgent } from "./launch.ts";
import { openCloud } from "./open-cloud.ts";
import { onboardingCards, runStep, stepContext } from "./steps.ts";
import { aiStatus, collectOpenAiKey, verifyAi } from "./verify-ai.ts";
/** WHAT THE ONBOARDING APP SAYS OF ITSELF: the mind's sign-in pages show its name and its icon,
* and open on the passkey alone, since it is for members (open-files.ts of /repos/stdlib answers
* it to anyone). */
const ONBOARDING: AppDescription = { name: "Onboarding", icon: "/icon.svg", for: "members" };
const PRESS = /^\/steps\/([a-z0-9-]{1,60})\/([a-z-]{1,40})$/;
/** This repo in the mind: its files are the guides and the cards that the onboarding agent reads
* (launch.ts). A mind is born with it, since config's package.json names it. */
export const ONBOARDING_REPO = "/repos/onboarding";
/** The page's files, and the theme that every app of the mind shares. */
const FILES = { ...files, "theme.css": theme };
/** `icon`: the answer for the mind's icon, which every app serves as its own
* (open-files.ts of /repos/stdlib). */
export function onboardingApp({ icon }: { icon: () => Response }) {
return {
/** The app's description and icon, for anyone: worker.ts asks this before its guard. */
open: (request: Request) => openFile(request, ONBOARDING, icon),
/** Every request on the app's host. worker.ts guards it, so a member alone gets here. */
async fetch(request: Request, host: Pick<IterateConfigEntrypoint, "auth" | "getItx">) {
const { pathname } = new URL(request.url);
// `return await`, never `return <promise>`, inside a `using` block: the block releases the
// mind's handle at the return, so the steps must be done by then.
if (pathname === "/steps.json" && request.method === "GET") {
using itx = host.getItx();
return await answer(async () => onboardingCards(await stepContext(itx)));
}
if (pathname === "/ai-status.json" && request.method === "GET") {
using itx = host.getItx();
return await answer(async () => aiStatus(itx));
}
if (pathname === "/collect-openai-key" && request.method === "POST") {
using itx = host.getItx();
return await answer(async () => collectOpenAiKey(itx));
}
if (pathname === "/verify-ai" && request.method === "POST") {
using itx = host.getItx() as IterateContextApiWith<"agents"> & Disposable;
return await answer(async () => {
const result = await verifyAi(itx);
// The moment AI is proven working, create /agents/main with its bespoke
// instructions, generated tasks & dependencies overview, owner email, opener, and
// background subagents.
if (result.ok) {
const principal =
typeof host.auth.principal === "function" ? host.auth.principal(request) : null;
await launchOnboardingAgent(itx, {
email: principal?.email,
name: principal?.name,
});
}
return result;
});
}
if (pathname === "/open-cloud" && request.method === "GET") {
using itx = host.getItx();
return openCloud((await itx.whoami()).mindSlug);
}
const press = PRESS.exec(pathname);
if (press && request.method === "POST") {
using itx = host.getItx();
return await answer(async () => runStep(await stepContext(itx), press[1]!, press[2]!));
}
// The page's own socket (public/app.js opens it), and what @iterate-com/ui's shell asks of
// its host: the mind's issuer, the one mind this app serves, and its "Sign out".
const page = host.auth.memberPage(request);
if (page) return page;
return serveFiles(request, { index, files: FILES });
},
};
}
/** The call's answer as JSON; a throw as `{ error }`, with its words, for the card to show. */
async function answer(run: () => Promise<unknown>): Promise<Response> {
try {
return Response.json(await run(), { headers: { "cache-control": "no-store" } });
} catch (error) {
return Response.json(
{ error: error instanceof Error ? error.message : String(error) },
{ status: 400, headers: { "cache-control": "no-store" } },
);
}
}