@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 75 lines 2.68 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Effect from "effect/Effect";
3 import * as Layer from "effect/Layer";
4 import * as RpcMiddleware from "effect/rpc/RpcMiddleware";
5 import * as Schema from "effect/Schema";
6
7 export const CounterId = Schema.String.pipe(Schema.brand("CounterId"));
8 export class Limit extends Domain.Rejection("Limit", { amount: Schema.Number }) {}
9 export class Unauthenticated extends Schema.TaggedError<
10 Unauthenticated,
11 { readonly brand: unique symbol }
12 >()("Unauthenticated", {}) {}
13 export class Session extends RpcMiddleware.Service<Session>()("FoldkitTest/Session", {
14 error: Unauthenticated,
15 }) {}
16 export class Incremented extends Domain.Event("Incremented", { amount: Schema.Number }) {}
17 export class Increment extends Domain.Command("Increment", {
18 target: CounterId,
19 input: { amount: Schema.NumberFromString },
20 events: [Incremented],
21 rejects: [Limit],
22 reply: { total: Schema.Number },
23 }) {}
24 export class Reset extends Domain.Command("Reset", {
25 target: CounterId,
26 events: [Incremented],
27 middleware: [Session],
28 }) {}
29 export class Counter extends Domain.Aggregate("Counter", {
30 id: CounterId,
31 state: Schema.Struct({ total: Schema.Number }),
32 initial: { total: 0 },
33 commands: [Increment, Reset],
34 read: {},
35 decide: {
36 Increment: (_, { amount }) =>
37 amount > 10 ? new Limit({ amount }) : [Incremented.make({ amount })],
38 Reset: (state) => [Incremented.make({ amount: -state.total })],
39 },
40 evolve: { Incremented: (state, { amount }) => ({ total: state.total + amount }) },
41 reply: { Increment: (state) => ({ total: state.total }) },
42 }) {}
43 export class Total extends Domain.View("Total", {
44 from: [Counter],
45 key: Counter,
46 state: Schema.Struct({ total: Schema.Number }),
47 initial: { total: 0 },
48 evolve: { Incremented: (state, { amount }) => ({ total: state.total + amount }) },
49 events: [Incremented],
50 rejects: [Limit],
51 validate: (_, state) =>
52 state && state.total > 5 ? Effect.fail(new Limit({ amount: state.total })) : Effect.void,
53 }) {}
54 export class Outputs extends Domain.Stream("Outputs", { key: Counter, entries: [Incremented] }) {}
55 export class Counters extends Domain.Model("Counters", {
56 aggregates: [Counter],
57 views: [Total],
58 streams: [Outputs],
59 }) {}
60 export class CounterApi extends Domain.Api("CounterApi", {
61 model: Counters,
62 commands: ["Increment", "Reset"],
63 views: ["Total"],
64 streams: ["Outputs"],
65 aggregates: ["Counter"],
66 }) {}
67 export const Hosted = Counters.layer(Layer.empty).pipe(Layer.provide(Domain.InMemory));
68 export const Client = CounterApi.layerClientInProcess.pipe(
69 Layer.provide(
70 Layer.merge(
71 Hosted,
72 Layer.succeed(Session, () => Effect.fail(Unauthenticated.make({}))),
73 ),
74 ),
75 );