@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, 04:45 UTC

main
ts 173 lines 6.4 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 Stream from "effect/Stream";
8 import type { Providers } from "../Providers.ts";
9 import { pinNotificationsRegion } from "./internal.ts";
10
11 export interface ChannelAssociationProps {
12 /**
13 * The ARN of the {@link NotificationConfiguration} to deliver from.
14 * Changing the configuration replaces the association.
15 */
16 notificationConfigurationArn: string;
17
18 /**
19 * The ARN of the delivery channel to associate. Supported channels are
20 * email contacts (`AWS.NotificationsContacts.EmailContact`), Amazon Q
21 * Developer in chat applications (AWS Chatbot) channels, and AWS Console
22 * Mobile Application devices. Changing the channel replaces the
23 * association.
24 *
25 * An email contact can be associated while still `inactive` (unverified);
26 * AWS begins delivering to it once the address owner activates it.
27 */
28 channelArn: string;
29 }
30
31 export interface ChannelAssociation extends Resource<
32 "AWS.Notifications.ChannelAssociation",
33 ChannelAssociationProps,
34 {
35 /** The ARN of the associated delivery channel. */
36 channelArn: string;
37 /** The ARN of the parent notification configuration. */
38 notificationConfigurationArn: string;
39 },
40 never,
41 Providers
42 > {}
43
44 /**
45 * An AWS User Notifications **channel association** — attaches a delivery
46 * channel (an email contact, an Amazon Q Developer chat channel, or a
47 * Console Mobile Application device) to a
48 * {@link NotificationConfiguration}, so matching events are actually
49 * delivered somewhere beyond the Console notification center.
50 *
51 * The association is existence-only (there is nothing mutable): changing
52 * either ARN replaces it.
53 *
54 * ### Associating a Channel
55 * **Example:** Deliver notifications to an email contact
56 * ```typescript
57 * import * as Notifications from "@alchemy.run/aws/Notifications";
58 * import * as NotificationsContacts from "@alchemy.run/aws/NotificationsContacts";
59 *
60 * const config = yield* Notifications.NotificationConfiguration("Alerts", {
61 * description: "Deployment alerts",
62 * });
63 * const contact = yield* NotificationsContacts.EmailContact("OnCall", {
64 * emailAddress: "oncall@example.com",
65 * });
66 * const association = yield* Notifications.ChannelAssociation("OnCallEmail", {
67 * notificationConfigurationArn: config.notificationConfigurationArn,
68 * channelArn: contact.emailContactArn,
69 * });
70 * ```
71 *
72 * @resource
73 */
74 export const ChannelAssociation = Resource<ChannelAssociation>(
75 "AWS.Notifications.ChannelAssociation",
76 );
77
78 export const ChannelAssociationProvider = () =>
79 Provider.effect(
80 ChannelAssociation,
81 Effect.gen(function* () {
82 // The association has no ARN of its own — identity is the
83 // (configuration, channel) pair, observed via ListChannels.
84 const findAssociation = Effect.fn(function* (
85 notificationConfigurationArn: string,
86 channelArn: string,
87 ) {
88 return yield* pinNotificationsRegion(
89 notifications.listChannels.items({ notificationConfigurationArn }).pipe(
90 Stream.filter((arn) => arn === channelArn),
91 Stream.runHead,
92 Effect.map(Option.getOrUndefined),
93 // The parent configuration may already be gone.
94 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
95 ),
96 );
97 });
98
99 return ChannelAssociation.Provider.of({
100 stables: ["channelArn", "notificationConfigurationArn"],
101
102 // Sub-resource keyed by its parent notification configuration —
103 // there is no account-wide enumeration without a parent ARN.
104 list: () => Effect.succeed([]),
105
106 read: Effect.fn(function* ({ olds, output }) {
107 const configArn =
108 output?.notificationConfigurationArn ?? olds?.notificationConfigurationArn;
109 const channelArn = output?.channelArn ?? olds?.channelArn;
110 if (configArn === undefined || channelArn === undefined) {
111 return undefined;
112 }
113 const found = yield* findAssociation(configArn, channelArn);
114 // Associations are not taggable — identity is the pair itself.
115 return found !== undefined
116 ? { channelArn, notificationConfigurationArn: configArn }
117 : undefined;
118 }),
119
120 diff: Effect.fn(function* ({ news, olds }) {
121 if (!isResolved(news)) return undefined;
122 if (
123 news.notificationConfigurationArn !== olds.notificationConfigurationArn ||
124 news.channelArn !== olds.channelArn
125 ) {
126 return { action: "replace" } as const;
127 }
128 return undefined;
129 }),
130
131 reconcile: Effect.fn(function* ({ news, session }) {
132 // OBSERVE — existence-only: is the channel already associated?
133 const existing = yield* findAssociation(
134 news.notificationConfigurationArn,
135 news.channelArn,
136 );
137
138 // ENSURE — associate when missing; a ConflictException means a
139 // peer associated the same pair concurrently.
140 if (existing === undefined) {
141 yield* pinNotificationsRegion(
142 notifications
143 .associateChannel({
144 arn: news.channelArn,
145 notificationConfigurationArn: news.notificationConfigurationArn,
146 })
147 .pipe(Effect.catchTag("ConflictException", () => Effect.void)),
148 );
149 }
150
151 // No SYNC step — the association has no mutable aspects.
152 yield* session.note(news.channelArn);
153 return {
154 channelArn: news.channelArn,
155 notificationConfigurationArn: news.notificationConfigurationArn,
156 };
157 }),
158
159 delete: Effect.fn(function* ({ output }) {
160 // Idempotent — a missing association (or an already-deleted
161 // parent configuration) is not an error.
162 yield* pinNotificationsRegion(
163 notifications
164 .disassociateChannel({
165 arn: output.channelArn,
166 notificationConfigurationArn: output.notificationConfigurationArn,
167 })
168 .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)),
169 );
170 }),
171 });
172 }),
173 );