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 models the issue board with Domain, and lets a session read an issue from it and label it.

Terminal window
pnpm add https://pkg.alchemy.run/@alchemy.run/domain/pr:3743:56b4873

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." },
) {}
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],
}) {}

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. Triage now also requires the hosted Board.

Board.layer hosts the model, here on Durable Objects. It takes the model’s policies; there are none yet.

src/Board.ts
import * as DomainCloudflare from "@alchemy.run/domain/Cloudflare";
import * as Layer from "effect/Layer";
import { Board } from "./Board/Board.model.ts";
export const BoardLive = Board.layer(Layer.empty).pipe(
Layer.provide(DomainCloudflare.layerDurableObjects()),
);
src/Agents.ts
import * as Layer from "effect/Layer";
import { BoardLive } from "./Board.ts";
import { SmartLlama, Smart } from "./Models.ts";
const Live = Layer.mergeAll(HarnessLive, AI.DefaultModel(Smart)).pipe(Layer.provide(SmartLlama));
const Live = Layer.mergeAll(HarnessLive, AI.DefaultModel(Smart), BoardLive).pipe(
Layer.provide(SmartLlama),
);

The session now reports to the board, so nothing has to wait for its answer:

src/Agents.ts
import * as Layer from "effect/Layer";
import { BoardLive } from "./Board.ts";
import type { IssueId } from "./Board/Issue.id.ts";
return Effect.succeed({
ask: Effect.fn(function* (issue: string, text: string) {
const reply = yield* triage.get(issue).prompt(text);
return reply.text;
}, Effect.orDie),
start: (issue: IssueId) =>
triage
.get(issue)
.send(`The issue is ${issue}.`, { requestId: "start" })
.pipe(Effect.orDie),
});

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

Report from the Worker, and read the board

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

The Worker reports the issue to the board and starts its session. 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 IssueAgents from "./Agents.ts";
import { BoardLive } from "./Board.ts";
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";
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 agents = yield* IssueAgents;
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* agents.getByName(issueId).start(issueId);
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(BoardLive)),
) {}
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(), state: Alchemy.localState() },
{
providers: Layer.mergeAll(Cloudflare.providers(), DomainCloudflare.providers()),
state: Alchemy.localState(),
},
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: an Issue aggregate, two commands and a view.
  • 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 starts each session by hand. Part 3 lets the board do it, from GitHub’s events.