config-template / worker.tsmain at 3b8ad1b
// worker.ts — THIS FILE IS THE MIND'S WIRING.
//
// The mind loads this module first (package.json `main`). A file of another repo is imported by
// the repo's name: `@repos/stdlib/init.ts` is the file init.ts of /repos/stdlib. package.json
// lists each repo that this one needs as the folder next door, which is where an install on a
// laptop finds it. A push to `main` of any repo that this file's imports reach publishes the mind
// a few seconds later (AGENTS.md).
//
// The platform calls two methods of the class below:
//
// fetch(request) every request to a hostname of the mind: its apps, its website
// processEvent({ event }) every event of every context: a publish, a mail, a person's words
//
// Every other public method of the class is the mind's own API, `itx.config.<name>(…)`, for its
// agents and its pages. Those are in api.ts.
import type { IterateConfigProcessEventArgs } from "iterate/sdk";
import { dashApp } from "@repos/dash/app.ts";
import { framedSignIn } from "@repos/dash/framed-sign-in.ts";
import { docsApp } from "@repos/docs/app.ts";
import { gitApp } from "@repos/git/app.ts";
import { onboardingApp } from "@repos/onboarding/app.ts";
import { onboardingEvents, onboardingMindEvents } from "@repos/onboarding/events.ts";
import { everyEvent } from "@repos/stdlib/every-event.ts";
import { init, step } from "@repos/stdlib/init.ts";
import { mailReceived } from "@repos/stdlib/mail.ts";
import { tasksApp } from "@repos/tasks/app.ts";
// @ts-expect-error -- a plain module of the Tasks app, with no types
import { taskChangesEvent } from "@repos/tasks/feed.js";
import { ConfigApi, TASKS_REPO } from "./api.ts";
import { homepage } from "./homepage.ts";
import { mindIcon, serveProfile } from "./profile.ts";
import profileFiles from "./profile/";
// the mind's icon (profile/icon.svg), which every app serves as its own
const icon = () => mindIcon(profileFiles);
const dash = dashApp({ icon });
// Docs, for members: the files of the mind's repos, written together in a browser. The page, and
// the processors that keep an open doc, are /repos/docs.
const docs = docsApp({ icon });
// The first steps of a new mind, for its owner: connect to iterate Cloud, prove that AI answers,
// and start the first agent. The page, its steps and the agent's guides are /repos/onboarding.
// To retire it: take out its lines here and in api.ts, and its line of package.json.
const onboarding = onboardingApp({ icon });
// The Tasks board shows the markdown files under todo/ of the tasks repo (api.ts `TASKS_REPO`),
// which holds nothing else. No code is loaded from it, so package.json does not list it and it is
// cloned from nowhere: the publish hook below makes it, empty, and the board's first task is its
// first commit. The calls that an agent keeps a card with are in api.ts.
const tasks = tasksApp({ repo: TASKS_REPO, icon });
// git.<domain>/repos/<name>, for git and for a browser. A repo listed in `public`, by its path
// (`"/repos/stdlib"`), is for anyone to read and clone, with its pulls. None is listed: a new
// mind shows its code to its members alone. Every other repo's pages are for members, and git
// needs the caller's own token for it, as a push to any repo does. A member alone opens a pull,
// comments and merges.
const git = gitApp({ icon, public: [] });
export default class extends ConfigApi {
// EVERY REQUEST, on every hostname of the mind but its control-plane hostname, where the
// platform answers (sign-in, the API, MCP).
async fetch(request: Request) {
// An app is on `<name>.<domain>`. Each call below is a partial fetcher: it answers, or it says
// null and the next one gets the request. `this.auth.require` is one too: null for a member,
// the sign-in for anyone else. An app's name and icon are for anyone (`open`).
switch (new URL(request.url).hostname.split(".")[0]) {
case "dash": // the Dash, for members: every agent and a chat with each
return (
dash.open(request) ||
framedSignIn(request, this.auth.require(request)) ||
dash.fetch(request, this)
);
case "docs": // Docs, for members: the files of the mind's repos, written together
return docs.open(request) || this.auth.require(request) || docs.fetch(request, this);
case "onboarding": // the first steps of a new mind, for members
return (
onboarding.open(request) || this.auth.require(request) || onboarding.fetch(request, this)
);
case "tasks": // Tasks, for members: the board of task files
return tasks.open(request) || this.auth.require(request) || tasks.fetch(request, this);
case "git": // the mind's repos, for git and for a browser: the app says who may do what
return git.open(request) || git.fetch(request, this);
}
// THE PUBLIC WEBSITE, for anyone, on every other hostname
return (
serveProfile(request, profileFiles) || // the mind's name, description and icon (profile/)
(await homepage(request, this)) || // homepage.ts: change that file to make the site yours
new Response("Not found\n", { status: 404 })
);
}
// EVERY EVENT of every context of the mind, one at a time, in no order, at least once: each
// case is idempotent, and nothing is kept here. `itx` is the mind's root.
async processEvent({ event, itx }: IterateConfigProcessEventArgs<"agents">) {
switch (event.type) {
case "events.iterate.com/mind/worker-updated":
// The mind's code was published. It happens after every push, so every line is
// idempotent. A `step` that throws is reported under its name, and the lines after it
// still run.
await init(itx); // the agents (init.ts of /repos/stdlib)
// Where the Dash finds what it draws for each kind of agent: a file of THIS repo, by its
// path in it (agent-kinds/agent.ts says why it is here).
await step("agent-kinds", () =>
itx.kv.put("agent-kinds", JSON.stringify({ agent: "agent-kinds/agent.ts" })),
);
// The repo whose task files the board shows. A repo that is there already is left as it is.
await step("tasks-repo", () => itx.repos.create(TASKS_REPO));
break;
case "events.iterate.com/email/received": // a mail came: it goes to the agent it is for
await mailReceived(itx, event);
break;
}
// What looks at every event, side by side: one that fails does not stop the others, and the
// first failure is thrown after all settled.
const hooks = await Promise.allSettled([
// An update of a repo's main: one `tasks/task-changed` for each task file that it changed,
// on that repo's feed log, /task-files/<repo>. The followers of a task are told from there
// (feed.js of /repos/tasks).
taskChangesEvent(itx, event),
// the onboarding app's part: what the owner told iterate Cloud, and what moves a step's card
onboardingEvents(itx, event),
// the agent's inputs, its prompt, each context's description
everyEvent(itx, event),
]);
const failed = hooks.find((hook) => hook.status === "rejected");
if (failed) throw failed.reason;
}
// An event that ANOTHER MIND added here, signed with its own key, never reaches `processEvent`:
// its words are data, never a member's instruction. iterate's mind says this way that it let
// this mind in and granted its credit, and the onboarding step goes on from it.
async processMindEvent({ event, itx }: IterateConfigProcessEventArgs) {
await onboardingMindEvents(itx, event);
}
}