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.
Give issues an id
Section titled “Give issues an id”An issue is known by its number on GitHub, as text:
import * as Schema from "effect/Schema";
export const IssueId = Schema.String.pipe(Schema.brand("IssueId"));export type IssueId = typeof IssueId.Type;Report an issue
Section titled “Report an issue”A command says what someone wants to happen, and an event says what did. ReportIssue records that an issue was opened:
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: [],}) {}Label an issue
Section titled “Label an issue”LabelIssue is the command a session will send. Its description is what the model reads about it:
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." },) {}Decide what each command does
Section titled “Decide what each command does”The Issue aggregate takes both commands and decides which events they produce:
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.
Show an issue
Section titled “Show an issue”A view is what a reader sees, built from the events:
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." },) {}Name the board
Section titled “Name the board”A Domain model lists what runs together. The board is the aggregate and its view:
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.
Mention the board in the prose
Section titled “Mention the board in the prose”A model’s commands and views are members of its class. Mention one, and a session may use it:
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.
Host the board
Section titled “Host the board”The prose now requires the board. Host it on Durable Objects, which makes each aggregate and view a class of the Worker:
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.
Give the sessions the board
Section titled “Give the sessions the board”Each session builds the prose inside its own object, so the board goes where the harness does:
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))),);Register Domain’s providers
Section titled “Register Domain’s providers”The Stack that deploys the Worker registers Domain’s providers beside Cloudflare’s:
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.
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.
pnpm add https://pkg.alchemy.run/@alchemy.run/domain/9c58f00bun add https://pkg.alchemy.run/@alchemy.run/domain/9c58f00npm install https://pkg.alchemy.run/@alchemy.run/domain/9c58f00yarn add https://pkg.alchemy.run/@alchemy.run/domain/9c58f00pnpm alchemy deploybun alchemy deploynpx alchemy deployyarn alchemy deployTry it
Section titled “Try it”Report an issue, then read it a few seconds later:
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.
What you have
Section titled “What you have”- A board with an
Issueaggregate, 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.