Skip to content

Effect Cluster

Host a process on Effect Cluster, and each of its sessions is an entity id of one entity, on whichever runner holds its shard. Its state lives in the cluster’s Postgres, and its runners run in the pool you give it.

AICluster.ProcessSessions takes the name of the entity that holds the sessions, this file as main, the pool that runs it, and the process’s Layer with what a session runs on. Export it as the file’s default.

src/Sessions.ts
import * as AICluster from "@alchemy.run/ai/EffectCluster";
import * as ClusterDocker from "@alchemy.run/effect-cluster/Docker";
import * as Layer from "effect/Layer";
import { Harness } from "./Harness.ts";
import { EngineerLive } from "./Engineer.process.ts";
export const Database = ClusterDocker.Database("Cluster");
const Default = ClusterDocker.SwarmPool("Default").pipe(Layer.provide(Database));
export default AICluster.ProcessSessions(
"Workshop",
{ main: import.meta.url, pool: Default },
EngineerLive.pipe(Layer.provide(Harness)),
);

The pool’s nodes load this file’s default export and run the sessions. Each session is one entity id of that entity, named after the session’s key.

A Domain model goes on the same host as the sessions. AICluster.ProcessSessions is AI.layerObjects on Cluster.layerObjectHost: write those two out, so the model and the sessions are given one host. The model’s policy holds Engineer, so its Layer still needs one, and the hosted process is what it gets:

src/Live.ts
import * as AI from "@alchemy.run/ai";
import * as DomainCluster from "@alchemy.run/domain/EffectCluster";
import * as Cluster from "@alchemy.run/effect-cluster";
import * as ClusterDocker from "@alchemy.run/effect-cluster/Docker";
import * as Layer from "effect/Layer";
import { Harness } from "./Harness.ts";
import { EngineerLive } from "./Engineer.process.ts";
import { TaskWorkLive } from "./Workshop/TaskWork.policy.ts";
import { Workshop } from "./Workshop/Workshop.model.ts";
export const Database = ClusterDocker.Database("Cluster");
const Default = ClusterDocker.SwarmPool("Default").pipe(Layer.provide(Database));
const WorkshopLive = Workshop.layer(TaskWorkLive).pipe(Layer.provide(DomainCluster.layerEntities()));
const EngineerSessions = AI.layerObjects(
EngineerLive.pipe(Layer.provide(Layer.mergeAll(Harness, WorkshopLive))),
);
export default WorkshopLive.pipe(
Layer.provideMerge(EngineerSessions),
Layer.provide(Cluster.layerObjectHost("Workshop", { main: import.meta.url, pool: Default })),
);

The model’s objects and the sessions are entity ids of the one entity. The host is written once, and neither Layer above it names a file or a pool.

Whatever serves the app imports the default export and provides it:

src/Api.ts
import * as Docker from "@alchemy.run/docker";
import * as Effect from "effect/Effect";
import { HttpServerRequest } from "effect/http/HttpServerRequest";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Engineer } from "./Engineer.process.ts";
import Sessions from "./Sessions.ts";
export default class Api extends Docker.Service<Api>()(
"Api",
{ main: import.meta.url, port: 3000 },
Effect.gen(function* () {
const engineer = yield* Engineer;
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const id = new URL(request.url, "http://api").pathname.slice(1);
const reply = yield* engineer.get(id).prompt(yield* request.text);
return HttpServerResponse.text(reply.text);
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(Sessions)),
) {}

There the Layer deploys the entity and reaches it through the pool’s gateway. See Reach a session for what the client does.

A session needs a harness that keeps its conversation where the session runs. PiCloudflare.DurableObjectStorage and OpenCode’s storage are for a Cloudflare Durable Object, and neither harness has a storage Layer for an Effect Cluster entity yet. On Effect Cluster, Harness provides a harness of your own, or the process is played by AI.scripted.