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.
Install
Section titled “Install”pnpm add https://pkg.alchemy.run/@alchemy.run/domain/pr:3743:56b4873bun add https://pkg.alchemy.run/@alchemy.run/domain/pr:3743:56b4873npm install https://pkg.alchemy.run/@alchemy.run/domain/pr:3743:56b4873yarn add https://pkg.alchemy.run/@alchemy.run/domain/pr:3743:56b4873Give 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." },) {}Put them in a model
Section titled “Put them in a model”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],}) {}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. Triage now also requires the hosted Board.
Host the board
Section titled “Host the board”Board.layer hosts the model, here on Durable Objects. It takes the model’s policies; there are none yet.
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()),);Give the session the board
Section titled “Give the session the board”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),);Start a session without waiting for it
Section titled “Start a session without waiting for it”The session now reports to the board, so nothing has to wait for its answer:
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.
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)),) {}Add Domain’s providers
Section titled “Add Domain’s providers”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(), },pnpm 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: an
Issueaggregate, 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.