The best way to control Claude Code, Codex and more from any computer or phone

main
ts 142 lines 4.45 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Layer from "effect/Layer";
6 import * as Option from "effect/Option";
7 import * as Schema from "effect/Schema";
8 import { Activity } from "effect/workflow";
9
10 const CounterId = Schema.String.pipe(Schema.brand("CounterId"));
11
12 class Incremented extends Domain.Event("Incremented", { by: Schema.Int }) {}
13
14 class Increment extends Domain.Command("Increment", {
15 target: CounterId,
16 input: { by: Schema.Int },
17 events: [Incremented],
18 }) {}
19
20 class Counter extends Domain.Aggregate("Counter", {
21 id: CounterId,
22 state: Schema.Struct({ total: Schema.Int }),
23 initial: { total: 0 },
24 commands: [Increment],
25 decide: {
26 Increment: (_, command) => [new Incremented({ by: command.by })],
27 },
28 evolve: {
29 Incremented: (state, event) => ({ total: state.total + event.by }),
30 },
31 }) {}
32
33 class Mirrored extends Domain.Event("Mirrored", { by: Schema.Int }) {}
34
35 class Mirror extends Domain.Command("Mirror", {
36 target: CounterId,
37 input: { by: Schema.Int },
38 events: [Mirrored],
39 }) {}
40
41 /** What the saga writes, so a test can see that it ran. */
42 class Echo extends Domain.Aggregate("Echo", {
43 id: CounterId,
44 state: Schema.Struct({ total: Schema.Int }),
45 initial: { total: 0 },
46 commands: [Mirror],
47 decide: {
48 Mirror: (_, command) => [new Mirrored({ by: command.by })],
49 },
50 evolve: {
51 Mirrored: (state, event) => ({ total: state.total + event.by }),
52 },
53 }) {}
54
55 class CounterTotal extends Domain.View("CounterTotal", {
56 from: [Counter],
57 key: Counter,
58 state: Schema.Struct({ total: Schema.Int }),
59 initial: { total: 0 },
60 evolve: {
61 Incremented: (view, event) => ({ total: view.total + event.by }),
62 },
63 }) {}
64
65 /** Mirrors a counter's first increment onto its Echo; one run per counter. */
66 class MirrorFirst extends Domain.Saga("MirrorFirst", {
67 key: CounterId,
68 input: { by: Schema.Int },
69 }) {}
70
71 const MirrorFirstLive = MirrorFirst.toLayer(
72 Effect.gen(function* () {
73 const echoes = yield* Echo;
74 return Effect.fn(function* ({ key, by }) {
75 yield* Activity.make({
76 name: "mirror",
77 execute: echoes.send(key, new Mirror({ by })).pipe(Effect.orDie),
78 });
79 });
80 }),
81 );
82
83 class StartMirror extends Domain.Policy("StartMirror", {
84 from: Counter,
85 on: [Incremented],
86 }) {}
87
88 const StartMirrorLive = StartMirror.toLayer(
89 Effect.gen(function* () {
90 const mirrors = yield* MirrorFirst;
91 return Effect.fn(function* ({ source, event }) {
92 yield* mirrors.start(source, { by: event.by });
93 });
94 }),
95 );
96
97 /**
98 * An aggregate, a view, a policy and a saga, so the model declares every
99 * kind of Durable Object the platform hosts, and runs the workflow engine.
100 */
101 export class Counters extends Domain.Model("Counters", {
102 aggregates: [Counter, Echo],
103 views: [CounterTotal],
104 policies: [StartMirror],
105 sagas: [MirrorFirst],
106 }) {}
107
108 export const CountersLive = Counters.layer(Layer.mergeAll(StartMirrorLive, MirrorFirstLive));
109
110 /**
111 * Serves `/increment/:id` and `/state/:id` for a counter, `/total/:id` for
112 * its view, `/echo/:id` for what the saga wrote, and `/platform` for which
113 * platform the deployed version runs on.
114 */
115 export const serve = (platform: "eu" | "unrestricted") =>
116 Effect.gen(function* () {
117 const counters = yield* Counter;
118 const echoes = yield* Echo;
119 const totals = yield* CounterTotal;
120 return {
121 fetch: Effect.gen(function* () {
122 const request = yield* HttpServerRequest;
123 const [, action, id] = new URL(request.url, "http://localhost").pathname.split("/");
124 const counter = CounterId.make(id ?? "c-1");
125 switch (action) {
126 case "platform":
127 return yield* HttpServerResponse.json({ platform });
128 case "increment":
129 yield* counters.send(counter, new Increment({ by: 2 })).pipe(Effect.orDie);
130 return yield* HttpServerResponse.json(yield* counters.state(counter));
131 case "total": {
132 const total = yield* totals.query(Counter.ref(counter)).pipe(Effect.orDie);
133 return yield* HttpServerResponse.json(Option.getOrElse(total, () => ({ total: 0 })));
134 }
135 case "echo":
136 return yield* HttpServerResponse.json(yield* echoes.state(counter));
137 default:
138 return yield* HttpServerResponse.json(yield* counters.state(counter));
139 }
140 }),
141 };
142 });