Skip to content

Part 2: A board to report to

In Part 1 a session could only answer whoever prompted it. A process has no output, so it needs somewhere to report. This part declares a board of issues, and lets a session read one and label it.

An issue is known by its number on GitHub, as text:

src/Board/Issue.id.ts
import * as Schema from "effect/Schema";
export const IssueId = Schema.String.pipe(Schema.brand("IssueId"));
export type IssueId = typeof IssueId.Type;

A command says what someone wants to happen, and an event says what did. ReportIssue records that an issue was opened:

src/Board/ReportIssue.command.ts
import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
import { IssueId } from "./Issue.id.ts";
export class IssueReported extends Domain.Event("IssueReported", {
title: Schema.String,
body: Schema.String,
}) {}
export class ReportIssue extends Domain.Command("ReportIssue", {
input: { issueId: IssueId, title: Schema.String, body: Schema.String },
events: [IssueReported],
rejects: [],
}) {}

LabelIssue is the command a session will send. Its description is what the model reads about it:

src/Board/LabelIssue.command.ts
import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
import { IssueId } from "./Issue.id.ts";
export const Label = Schema.Literals(["bug", "feature", "question"]);
export class IssueLabeled extends Domain.Event("IssueLabeled", { label: Label }) {}
export class LabelIssue extends Domain.Command(
"LabelIssue",
{
input: { issueId: IssueId, label: Label },
events: [IssueLabeled],
rejects: [],
},
{ description: "Record what kind of issue this is: a bug, a feature or a question." },
) {}

The Issue aggregate takes both commands and decides which events they produce:

src/Board/Issue.aggregate.ts
import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
import { IssueId } from "./Issue.id.ts";
import { IssueLabeled, LabelIssue } from "./LabelIssue.command.ts";
import { IssueReported, ReportIssue } from "./ReportIssue.command.ts";
export class Issue extends Domain.Aggregate("Issue", {
id: IssueId,
state: Schema.Struct({ reported: Schema.Boolean }),
initial: { reported: false },
commands: [ReportIssue, LabelIssue],
keyOf: (command) => command.issueId,
decide: {
ReportIssue: (issue, command) =>
issue.reported ? [] : [new IssueReported({ title: command.title, body: command.body })],
LabelIssue: (_, command) => [new IssueLabeled({ label: command.label })],
},
evolve: {
IssueReported: () => ({ reported: true }),
IssueLabeled: (issue) => issue,
},
}) {}

An issue reported twice produces one event. See Aggregates.

A view is what a reader sees, built from the events:

src/Board/IssueDetail.view.ts
import * as Domain from "@alchemy.run/domain";
import * as Schema from "effect/Schema";
import { Issue } from "./Issue.aggregate.ts";
import { Label } from "./LabelIssue.command.ts";
export class IssueDetail extends Domain.View(
"IssueDetail",
{
from: [Issue],
key: Issue,
state: Schema.Struct({
title: Schema.String,
body: Schema.String,
label: Schema.NullOr(Label),
}),
initial: { title: "", body: "", label: null },
evolve: {
IssueReported: (detail, event) => ({ ...detail, title: event.title, body: event.body }),
IssueLabeled: (detail, event) => ({ ...detail, label: event.label }),
},
},
{ description: "An issue's title and body, and its label." },
) {}

A Domain model lists what runs together. The board is the aggregate and its view:

src/Board/Board.model.ts
import * as Domain from "@alchemy.run/domain";
import { Issue } from "./Issue.aggregate.ts";
import { IssueDetail } from "./IssueDetail.view.ts";
export class Board extends Domain.Model("Board", {
aggregates: [Issue],
views: [IssueDetail],
}) {}

Triage is not on the list. A process is not a member of a model: it works with one, or several, through what its prose mentions.

A model’s commands and views are members of its class. Mention one, and a session may use it:

src/Triage.process.ts
import * as AI from "@alchemy.run/ai";
import { Board } from "./Board/Board.model.ts";
You look after one issue of a GitHub repository.
When you are told what the issue says, reply with one word:
bug when something is broken, feature when something new is asked for,
question otherwise.
You look after one issue of a GitHub repository. Your first message says
which.
Get the issue with ${Board.IssueDetail}: its key is the issue. Then send
${Board.LabelIssue}: bug when something is broken, feature when something
new is asked for, question otherwise.

A session is told which issue is its own in its first message, because the prose is the same for every session. A Triage session can read an IssueDetail and send LabelIssue. ReportIssue is not mentioned, so it is out of the session’s reach.

The prose now requires the board. Host it on Durable Objects, which makes each aggregate and view a class of the Worker:

src/Sessions.ts
import * as AI from "@alchemy.run/ai";
import * as AICloudflare from "@alchemy.run/ai/Cloudflare";
import * as DomainCloudflare from "@alchemy.run/domain/Cloudflare";
import * as Pi from "@alchemy.run/pi";
import * as PiCloudflare from "@alchemy.run/pi/Cloudflare";
import * as Layer from "effect/Layer";
import { Board } from "./Board/Board.model.ts";
import { Smart, SmartLlama } from "./Models.ts";
import { TriageLive } from "./Triage.process.ts";
const Harness = Layer.mergeAll(HarnessLive, AI.DefaultModel(Smart)).pipe(
Layer.provide(SmartLlama),
);
export const BoardLive = Board.layer(Layer.empty).pipe(
Layer.provide(DomainCloudflare.layerDurableObjects()),
);

Board.layer takes what the board’s policies do. It has none yet.

Each session builds the prose inside its own object, so the board goes where the harness does:

src/Sessions.ts
export const BoardLive = Board.layer(Layer.empty).pipe(
Layer.provide(DomainCloudflare.layerDurableObjects()),
);
export const TriageSessions = AICloudflare.ProcessSessions(
TriageLive.pipe(Layer.provide(Harness)),
TriageLive.pipe(Layer.provide(Layer.mergeAll(Harness, BoardLive))),
);

The Stack that deploys the Worker registers Domain’s providers beside Cloudflare’s:

alchemy.run.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as DomainCloudflare from "@alchemy.run/domain/Cloudflare";
import * as Alchemy from "alchemy";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import Worker from "./src/Worker.ts";
export default Alchemy.Stack(
"CloudflareAiIssues",
{
providers: Cloudflare.providers(),
providers: Layer.mergeAll(Cloudflare.providers(), DomainCloudflare.providers()),
state: Alchemy.localState(),
},

Report from the Worker, and read the board

Section titled “Report from the Worker, and read the board”

The session now reports to the board, so nothing has to wait for its answer. The Worker reports the issue and sends its session a first message. A GET reads the issue as the board shows it.

src/Worker.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import * as Option from "effect/Option";
import * as Layer from "effect/Layer";
import { Issue } from "./Board/Issue.aggregate.ts";
import { IssueId } from "./Board/Issue.id.ts";
import { IssueDetail } from "./Board/IssueDetail.view.ts";
import { ReportIssue } from "./Board/ReportIssue.command.ts";
import { BoardLive, TriageSessions } from "./Sessions.ts";
import { Triage } from "./Triage.process.ts";
export default class Worker extends Cloudflare.Worker<Worker>()(
"Worker",
{ main: import.meta.url },
Effect.gen(function* () {
const issues = yield* Issue;
const details = yield* IssueDetail;
const triage = yield* Triage;
return {
// POST /issues/<number> with the issue's title reports it; GET shows it.
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const [, , number] = new URL(request.url, "http://board").pathname.split("/");
if (number === undefined) return HttpServerResponse.text("not found", { status: 404 });
const issueId = IssueId.make(number);
if (request.method === "POST") {
yield* issues.send(new ReportIssue({ issueId, title: yield* request.text, body: "" }));
yield* triage.get(issueId).send(`The issue is ${issueId}.`, { requestId: "start" });
return HttpServerResponse.text("reported");
}
const detail = yield* details.get(Issue.ref(issueId));
return yield* HttpServerResponse.json(Option.getOrNull(detail));
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(Layer.mergeAll(BoardLive, TriageSessions))),
) {}

send returns once the message is stored. The same requestId on every call means a session is given its issue once.

Terminal window
pnpm add https://pkg.alchemy.run/@alchemy.run/domain/9c58f00
Terminal window
pnpm alchemy deploy

Report an issue, then read it a few seconds later:

Terminal window
curl -X POST "$URL/issues/2" -d "Please add a dark mode."
# reported
curl "$URL/issues/2"
# {"title":"Please add a dark mode.","body":"","label":"feature"}

The label was put there by the session.

  • A board with an Issue aggregate, two commands and a view, hosted on Durable Objects.
  • A process that mentions the one view and the one command it may use.
  • A session that reports by sending a command.

The Worker still sends each session its first message by hand. Part 3 lets the board do it, from GitHub’s events.