@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, 07:15 UTC

main
ts 82 lines 2.75 KB
Raw
1 import * as Domain from "@alchemy.run/domain";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4
5 /** How long a hold lasts before it expires, unless it is released first. */
6 export const HOLD_WINDOW = "15 seconds";
7
8 /** The command id a hold's expiry is scheduled under. */
9 export const EXPIRY = "expire";
10
11 export class HoldPlaced extends Domain.Event("HoldPlaced") {}
12 export class HoldReleased extends Domain.Event("HoldReleased") {}
13 export class HoldExpired extends Domain.Event("HoldExpired") {}
14
15 export class PlaceHold extends Domain.Command("PlaceHold", {
16 target: Schema.String,
17 events: [HoldPlaced],
18 }) {}
19 export class ReleaseHold extends Domain.Command("ReleaseHold", {
20 target: Schema.String,
21 events: [HoldReleased],
22 }) {}
23 /**
24 * Records the expiry with no guard on state: only cancelling the schedule
25 * keeps a released hold from expiring.
26 */
27 export class ExpireHold extends Domain.Command("ExpireHold", {
28 target: Schema.String,
29 events: [HoldExpired],
30 }) {}
31
32 export class Hold extends Domain.Aggregate("Hold", {
33 id: Schema.String,
34 state: Schema.Struct({ status: Schema.Literals(["none", "held", "released", "expired"]) }),
35 initial: { status: "none" },
36 commands: [PlaceHold, ReleaseHold, ExpireHold],
37 decide: {
38 PlaceHold: () => [new HoldPlaced()],
39 ReleaseHold: () => [new HoldReleased()],
40 ExpireHold: () => [new HoldExpired()],
41 },
42 evolve: {
43 HoldPlaced: () => ({ status: "held" as const }),
44 HoldReleased: () => ({ status: "released" as const }),
45 HoldExpired: () => ({ status: "expired" as const }),
46 },
47 }) {}
48
49 /** Schedules a hold's expiry when it is placed, and cancels it when it is released. */
50 export class HoldDeadline extends Domain.Policy("HoldDeadline", {
51 from: Hold,
52 on: [HoldPlaced, HoldReleased],
53 }) {}
54
55 export const HoldDeadlineLive = HoldDeadline.toLayer(
56 Effect.gen(function* () {
57 const holds = yield* Hold;
58 return Effect.fn(function* ({ source, event }) {
59 if (event._tag === "HoldPlaced") {
60 yield* holds
61 .schedule(source, new ExpireHold(), { after: HOLD_WINDOW, commandId: EXPIRY })
62 // a redelivery after the expiry ran: nothing to arm
63 .pipe(Effect.catchTag("AlreadyExecuted", () => Effect.void));
64 } else {
65 yield* holds.unschedule(source, EXPIRY).pipe(
66 // the hold expired first, and the release is recorded after it; a
67 // redelivery, so the expiry is already cancelled; or the hold was
68 // never placed, so there's no expiry
69 Effect.catchTag(
70 ["AlreadyExecuted", "AlreadyCancelled", "NotScheduled"],
71 () => Effect.void,
72 ),
73 );
74 }
75 });
76 }),
77 );
78
79 export class Holds extends Domain.Model("Holds", {
80 aggregates: [Hold],
81 policies: [HoldDeadline],
82 }) {}