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

main
ts 25 lines 885 Bytes
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4 import { CurrentUser } from "./CurrentUser.context.ts";
5 import { NotYours } from "./NotYours.rejection.ts";
6 import { UserId } from "./User.id.ts";
7 import { NoteWritten, WriteNote } from "./WriteNote.command.ts";
8
9 /** Each user's notebook, keyed by their id. */
10 export class Notebook extends Domain.Aggregate("Notebook", {
11 id: UserId,
12 state: Schema.Struct({ notes: Schema.Int }),
13 initial: { notes: 0 },
14 commands: [WriteNote],
15 decide: {
16 WriteNote: Effect.fn(function* (_s, cmd, { id }) {
17 const user = yield* CurrentUser;
18 if (user.userId !== id) return yield* new NotYours();
19 return [new NoteWritten({ text: cmd.text, author: user.name })];
20 }),
21 },
22 evolve: {
23 NoteWritten: (s) => ({ notes: s.notes + 1 }),
24 },
25 }) {}