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

main
ts 171 lines 5.92 KB
Raw
1 import * as notifications from "@distilled.cloud/aws/notifications";
2 import { isResolved } from "alchemy/Diff";
3 import * as Provider from "alchemy/Provider";
4 import { Resource } from "alchemy/Resource";
5 import * as Effect from "effect/Effect";
6 import * as Option from "effect/Option";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9 import type { Providers } from "../Providers.ts";
10 import { pinNotificationsRegion } from "./internal.ts";
11
12 export interface NotificationHubProps {
13 /**
14 * The AWS region to enable as a notification hub (where User
15 * Notifications stores and replicates notification events), e.g.
16 * `us-west-2`. Changing the region replaces the hub.
17 */
18 region: string;
19 }
20
21 export interface NotificationHub extends Resource<
22 "AWS.Notifications.NotificationHub",
23 NotificationHubProps,
24 {
25 /** The region registered as a notification hub. */
26 notificationHubRegion: string;
27 /** Hub status: `ACTIVE`, `REGISTERING`, `DEREGISTERING` or `INACTIVE`. */
28 status: string;
29 },
30 never,
31 Providers
32 > {}
33
34 /**
35 * An AWS User Notifications **notification hub** — a regional enablement
36 * that stores and replicates notification events. An account can register
37 * at most 3 hubs, and at least one ACTIVE hub must exist for notification
38 * configurations to deliver events.
39 *
40 * Registration is a true upsert keyed by region (re-registering an existing
41 * hub region is a no-op), so the hub behaves like a per-region singleton.
42 *
43 * **AWS refuses to deregister the last ACTIVE hub in the account**
44 * (`ConflictException`). Destroying a stack containing the account's only
45 * hub therefore fails — keep a baseline hub registered outside the stack,
46 * or register a second hub first.
47 *
48 * ### Registering a Notification Hub
49 * **Example:** Enable a region as a notification hub
50 * ```typescript
51 * import * as Notifications from "@alchemy.run/aws/Notifications";
52 *
53 * const hub = yield* Notifications.NotificationHub("Hub", {
54 * region: "us-east-2",
55 * });
56 * ```
57 *
58 * @resource
59 */
60 export const NotificationHub = Resource<NotificationHub>("AWS.Notifications.NotificationHub");
61
62 /**
63 * Poll until the hub reaches a terminal status (bounded and fail-open —
64 * registration is typically ACTIVE immediately).
65 *
66 * Explicitly typed so `Effect.repeat`'s conditional return type doesn't
67 * survive into declaration emit and widen the provider layer.
68 */
69 const untilActive = <A, E, R>(
70 self: Effect.Effect<A, E, R>,
71 status: (a: A) => string | undefined,
72 ): Effect.Effect<A, E, R> =>
73 Effect.repeat(self, {
74 schedule: Schedule.fixed("2 seconds"),
75 until: (a) => status(a) !== "REGISTERING",
76 times: 20,
77 });
78
79 export const NotificationHubProvider = () =>
80 Provider.effect(
81 NotificationHub,
82 Effect.gen(function* () {
83 const findByRegion = Effect.fn(function* (region: string) {
84 return yield* pinNotificationsRegion(
85 notifications.listNotificationHubs.items({}).pipe(
86 Stream.filter((hub) => hub.notificationHubRegion === region),
87 Stream.runHead,
88 Effect.map(Option.getOrUndefined),
89 ),
90 );
91 });
92
93 const toAttrs = (hub: {
94 notificationHubRegion: string;
95 statusSummary: { status: string };
96 }) => ({
97 notificationHubRegion: hub.notificationHubRegion,
98 status: hub.statusSummary.status,
99 });
100
101 return NotificationHub.Provider.of({
102 stables: ["notificationHubRegion"],
103
104 list: () =>
105 pinNotificationsRegion(
106 notifications.listNotificationHubs.items({}).pipe(
107 Stream.runCollect,
108 Effect.map((chunk) => Array.from(chunk).map(toAttrs)),
109 ),
110 ),
111
112 read: Effect.fn(function* ({ olds, output }) {
113 const region = output?.notificationHubRegion ?? olds?.region;
114 if (region === undefined) return undefined;
115 const found = yield* findByRegion(region);
116 // Hubs are not taggable — identity is the region itself.
117 return found ? toAttrs(found) : undefined;
118 }),
119
120 diff: Effect.fn(function* ({ news, olds }) {
121 if (!isResolved(news)) return undefined;
122 if (news.region !== olds.region) {
123 return { action: "replace" } as const;
124 }
125 return undefined;
126 }),
127
128 reconcile: Effect.fn(function* ({ news, session }) {
129 // OBSERVE — the hub is keyed by region; registration is an
130 // idempotent upsert so a concurrent create is harmless.
131 let live = yield* findByRegion(news.region);
132
133 // ENSURE — register when missing (or not yet ACTIVE; re-register
134 // of an existing region is a no-op returning current state).
135 if (live === undefined) {
136 const registered = yield* pinNotificationsRegion(
137 notifications.registerNotificationHub({
138 notificationHubRegion: news.region,
139 }),
140 );
141 live = registered;
142 }
143
144 // Wait for activation (bounded, fail-open — usually immediate).
145 if (live.statusSummary.status === "REGISTERING") {
146 const settled = yield* untilActive(
147 findByRegion(news.region),
148 (hub) => hub?.statusSummary.status,
149 );
150 live = settled ?? live;
151 }
152
153 yield* session.note(news.region);
154 return toAttrs(live);
155 }),
156
157 delete: Effect.fn(function* ({ output }) {
158 // Idempotent for an already-gone hub. A ConflictException
159 // ("Cannot deregister last ACTIVE notification hub") is a genuine
160 // account constraint and propagates.
161 yield* pinNotificationsRegion(
162 notifications
163 .deregisterNotificationHub({
164 notificationHubRegion: output.notificationHubRegion,
165 })
166 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
167 );
168 }),
169 });
170 }),
171 );