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
| 1 | import * as Domain from "@alchemy.run/domain"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | |
| 5 | /** Tours belong to an org; there is no Org aggregate, the org views are keyed by its id. */ |
| 6 | export const OrgId = Schema.String.pipe(Schema.brand("OrgId")); |
| 7 | |
| 8 | export class TourBooked extends Domain.Event("TourBooked", { |
| 9 | orgId: OrgId, |
| 10 | unit: Schema.String, |
| 11 | }) {} |
| 12 | export class TourCancelled extends Domain.Event("TourCancelled", { orgId: OrgId }) {} |
| 13 | |
| 14 | export class BookTour extends Domain.Command("BookTour", { |
| 15 | target: Schema.String, |
| 16 | input: { orgId: OrgId, unit: Schema.String }, |
| 17 | events: [TourBooked], |
| 18 | }) {} |
| 19 | export class CancelTour extends Domain.Command("CancelTour", { |
| 20 | target: Schema.String, |
| 21 | input: { orgId: OrgId }, |
| 22 | events: [TourCancelled], |
| 23 | }) {} |
| 24 | |
| 25 | export class Tour extends Domain.Aggregate("Tour", { |
| 26 | id: Schema.String, |
| 27 | state: Schema.Struct({}), |
| 28 | initial: {}, |
| 29 | commands: [BookTour, CancelTour], |
| 30 | decide: { |
| 31 | BookTour: (_, { orgId, unit }) => [new TourBooked({ orgId, unit })], |
| 32 | CancelTour: (_, { orgId }) => [new TourCancelled({ orgId })], |
| 33 | }, |
| 34 | evolve: { TourBooked: (s) => s, TourCancelled: (s) => s }, |
| 35 | }) {} |
| 36 | |
| 37 | export class ScheduleChanged extends Domain.Event("ScheduleChanged", { open: Schema.Number }) {} |
| 38 | |
| 39 | /** One instance per org: its open tours, by tour id. */ |
| 40 | export class OrgSchedule extends Domain.View("OrgSchedule", { |
| 41 | from: [Tour], |
| 42 | key: OrgId, |
| 43 | keyOf: { Tour: ({ event }) => event.orgId }, |
| 44 | state: Schema.Struct({ org: Schema.String, tours: Schema.Record(Schema.String, Schema.String) }), |
| 45 | initial: { org: "", tours: {} }, |
| 46 | evolve: { |
| 47 | TourBooked: (s, e, { key, source }) => ({ |
| 48 | org: key, |
| 49 | tours: { ...s.tours, [source.id]: e.unit }, |
| 50 | }), |
| 51 | TourCancelled: (s, _, { source }) => { |
| 52 | const { [source.id]: _cancelled, ...tours } = s.tours; |
| 53 | return { ...s, tours }; |
| 54 | }, |
| 55 | }, |
| 56 | events: [ScheduleChanged], |
| 57 | emit: { |
| 58 | Tour: (_, { after }) => new ScheduleChanged({ open: Object.keys(after?.tours ?? {}).length }), |
| 59 | }, |
| 60 | }) {} |
| 61 | |
| 62 | /** Keyed by the same schema as OrgSchedule, so it follows it without `keyOf`. */ |
| 63 | export class OrgBusy extends Domain.View("OrgBusy", { |
| 64 | from: [OrgSchedule], |
| 65 | key: OrgId, |
| 66 | state: Schema.Struct({ org: Schema.String, busy: Schema.Boolean }), |
| 67 | initial: { org: "", busy: false }, |
| 68 | evolve: { |
| 69 | ScheduleChanged: (_, e, { source }) => ({ org: source, busy: e.open >= 2 }), |
| 70 | }, |
| 71 | }) {} |
| 72 | |
| 73 | export class FeedItem extends Domain.Event("FeedItem", { |
| 74 | tour: Schema.String, |
| 75 | unit: Schema.String, |
| 76 | }) {} |
| 77 | |
| 78 | /** One ordered log per org. */ |
| 79 | export class OrgFeed extends Domain.Stream("OrgFeed", { key: OrgId, entries: [FeedItem] }) {} |
| 80 | |
| 81 | /** Writes each org's feed; `key` is the org id. */ |
| 82 | export class OrgFeedWriter extends Domain.Projection("OrgFeedWriter", { |
| 83 | from: [Tour], |
| 84 | key: OrgId, |
| 85 | keyOf: { Tour: ({ event }) => event.orgId }, |
| 86 | on: [TourBooked], |
| 87 | writes: [OrgFeed], |
| 88 | }) {} |
| 89 | export const OrgFeedWriterLive = OrgFeedWriter.toLayer( |
| 90 | Effect.gen(function* () { |
| 91 | const feed = yield* OrgFeed; |
| 92 | return Effect.fn(function* (batch) { |
| 93 | for (const input of batch) { |
| 94 | yield* feed.append(input, new FeedItem({ tour: input.source.id, unit: input.event.unit })); |
| 95 | } |
| 96 | }); |
| 97 | }), |
| 98 | ); |
| 99 | |
| 100 | export class Noted extends Domain.Event("Noted", { tour: Schema.String }) {} |
| 101 | export class Note extends Domain.Command("Note", { |
| 102 | target: Schema.String, |
| 103 | input: { tour: Schema.String }, |
| 104 | events: [Noted], |
| 105 | }) {} |
| 106 | /** An org's bookings, as notes. Its ids are org ids. */ |
| 107 | export class Board extends Domain.Aggregate("Board", { |
| 108 | id: Schema.String, |
| 109 | state: Schema.Struct({ notes: Schema.Array(Schema.String) }), |
| 110 | initial: { notes: [] }, |
| 111 | commands: [Note], |
| 112 | decide: { Note: (_, { tour }) => [new Noted({ tour })] }, |
| 113 | evolve: { Noted: (s, e) => ({ notes: [...s.notes, e.tour] }) }, |
| 114 | }) {} |
| 115 | |
| 116 | /** A policy keyed by the org id: bookings of one org run in order. */ |
| 117 | export class NoteBookings extends Domain.Policy("NoteBookings", { |
| 118 | from: Tour, |
| 119 | key: OrgId, |
| 120 | keyOf: { TourBooked: ({ event }) => event.orgId }, |
| 121 | on: [TourBooked], |
| 122 | }) {} |
| 123 | export const NoteBookingsLive = NoteBookings.toLayer( |
| 124 | Effect.gen(function* () { |
| 125 | const boards = yield* Board; |
| 126 | return Effect.fn(function* ({ key, source }) { |
| 127 | yield* boards.send(key, new Note({ tour: source.id })); |
| 128 | }); |
| 129 | }), |
| 130 | ); |
| 131 | |
| 132 | export class Tours extends Domain.Model("Tours", { |
| 133 | aggregates: [Tour, Board], |
| 134 | views: [OrgSchedule, OrgBusy], |
| 135 | projections: [OrgFeedWriter], |
| 136 | streams: [OrgFeed], |
| 137 | policies: [NoteBookings], |
| 138 | }) {} |