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

main
ts 239 lines 8.36 KB
Raw
1 import * as smsvoice from "@distilled.cloud/aws/pinpoint-sms-voice-v2";
2 import { Unowned } from "alchemy/AdoptPolicy";
3 import { isResolved } from "alchemy/Diff";
4 import { createPhysicalName } from "alchemy/PhysicalName";
5 import * as Provider from "alchemy/Provider";
6 import { Resource } from "alchemy/Resource";
7 import { createInternalTags, hasAlchemyTags } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Stream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12 import {
13 readSmsVoiceTags,
14 retrySmsVoiceThrottled,
15 syncSmsVoiceTags,
16 toTagList,
17 } from "./internal.ts";
18
19 export interface ConfigurationSetProps {
20 /**
21 * Name of the configuration set (`[A-Za-z0-9_-]+`, 1-64 characters).
22 * Changing the name replaces the configuration set.
23 * @default ${app}-${stage}-${id}
24 */
25 configurationSetName?: string;
26 /**
27 * Default message type applied to messages sent through this
28 * configuration set. Use `TRANSACTIONAL` for time-sensitive messages
29 * (e.g. one-time passcodes) and `PROMOTIONAL` for marketing content.
30 * Omitting the prop clears the default.
31 * @default no default message type
32 */
33 defaultMessageType?: "TRANSACTIONAL" | "PROMOTIONAL";
34 /**
35 * Tags to apply to the configuration set. Merged with internal Alchemy
36 * tags.
37 */
38 tags?: Record<string, string>;
39 }
40
41 export interface ConfigurationSet extends Resource<
42 "AWS.PinpointSMSVoiceV2.ConfigurationSet",
43 ConfigurationSetProps,
44 {
45 /**
46 * Name of the configuration set.
47 */
48 configurationSetName: string;
49 /**
50 * ARN of the configuration set.
51 */
52 configurationSetArn: string;
53 },
54 never,
55 Providers
56 > {}
57
58 /**
59 * An AWS End User Messaging SMS (Pinpoint SMS Voice v2) configuration
60 * set — a named set of rules applied to SMS and voice messages sent
61 * through it.
62 *
63 * Attach `EventDestination`s to a configuration set to route message
64 * events (sends, deliveries, failures) to CloudWatch Logs, Kinesis Data
65 * Firehose, or SNS.
66 * ### Creating Configuration Sets
67 * **Example:** Basic Configuration Set
68 * ```typescript
69 * import * as PinpointSMSVoiceV2 from "@alchemy.run/aws/PinpointSMSVoiceV2";
70 *
71 * const configSet = yield* PinpointSMSVoiceV2.ConfigurationSet("Messaging");
72 * ```
73 *
74 * **Example:** Configuration Set with a Default Message Type
75 * ```typescript
76 * const configSet = yield* PinpointSMSVoiceV2.ConfigurationSet("Otp", {
77 * defaultMessageType: "TRANSACTIONAL",
78 * tags: { team: "auth" },
79 * });
80 * ```
81 *
82 * ### Event Destinations
83 * **Example:** Stream message events to SNS
84 * ```typescript
85 * const events = yield* SNS.Topic("SmsEvents");
86 * const destination = yield* PinpointSMSVoiceV2.EventDestination("Events", {
87 * configurationSetName: configSet.configurationSetName,
88 * matchingEventTypes: ["ALL"],
89 * snsDestination: { topicArn: events.topicArn },
90 * });
91 * ```
92 *
93 * @resource
94 */
95 export const ConfigurationSet = Resource<ConfigurationSet>(
96 "AWS.PinpointSMSVoiceV2.ConfigurationSet",
97 );
98
99 /**
100 * Raised when a configuration set cannot be observed immediately after
101 * it was created — the create call succeeded (or raced a peer) but the
102 * follow-up describe found nothing.
103 */
104 export class SmsVoiceConfigurationSetMissing extends Data.TaggedError(
105 "SmsVoiceConfigurationSetMissing",
106 )<{ message: string }> {}
107
108 export const ConfigurationSetProvider = () =>
109 Provider.effect(
110 ConfigurationSet,
111 Effect.gen(function* () {
112 const toName = (id: string, props: ConfigurationSetProps) =>
113 props.configurationSetName
114 ? Effect.succeed(props.configurationSetName)
115 : createPhysicalName({ id, maxLength: 64 });
116
117 /**
118 * Observe a configuration set by name. `DescribeConfigurationSets`
119 * raises a typed `ResourceNotFoundException` for an unknown name.
120 */
121 const getByName = Effect.fn(function* (name: string) {
122 const result = yield* smsvoice
123 .describeConfigurationSets({ ConfigurationSetNames: [name] })
124 .pipe(
125 retrySmsVoiceThrottled,
126 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
127 );
128 return result?.ConfigurationSets?.find((cs) => cs.ConfigurationSetName === name);
129 });
130
131 return {
132 stables: ["configurationSetName", "configurationSetArn"],
133
134 read: Effect.fn(function* ({ id, olds, output }) {
135 const name = output?.configurationSetName ?? (yield* toName(id, olds ?? {}));
136 const observed = yield* getByName(name);
137 if (observed === undefined) return undefined;
138 const attrs = {
139 configurationSetName: observed.ConfigurationSetName,
140 configurationSetArn: observed.ConfigurationSetArn,
141 };
142 const tags = yield* readSmsVoiceTags(observed.ConfigurationSetArn);
143 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
144 }),
145
146 diff: Effect.fn(function* ({ id, news, olds }) {
147 if (!isResolved(news)) return undefined;
148 const oldName = yield* toName(id, olds ?? {});
149 const newName = yield* toName(id, news);
150 if (oldName !== newName) return { action: "replace" } as const;
151 }),
152
153 reconcile: Effect.fn(function* ({ id, news, output, session }) {
154 const name = output?.configurationSetName ?? (yield* toName(id, news));
155 const internalTags = yield* createInternalTags(id);
156 const desiredTags = { ...news.tags, ...internalTags };
157
158 // 1. Observe — cloud state is authoritative.
159 let observed = yield* getByName(name);
160
161 // 2. Ensure — create if missing; a ConflictException is a race
162 // with a peer reconciler, so fall through to re-observe.
163 if (observed === undefined) {
164 yield* smsvoice
165 .createConfigurationSet({
166 ConfigurationSetName: name,
167 Tags: toTagList(desiredTags),
168 })
169 .pipe(
170 retrySmsVoiceThrottled,
171 Effect.catchTag("ConflictException", () => Effect.void),
172 Effect.asVoid,
173 );
174 observed = yield* getByName(name);
175 }
176 if (observed === undefined) {
177 return yield* new SmsVoiceConfigurationSetMissing({
178 message: `configuration set '${name}' not observable after create`,
179 });
180 }
181
182 // 3. Sync default message type — diff observed against desired;
183 // an absent prop clears any observed default.
184 if (
185 news.defaultMessageType !== undefined &&
186 observed.DefaultMessageType !== news.defaultMessageType
187 ) {
188 yield* smsvoice
189 .setDefaultMessageType({
190 ConfigurationSetName: name,
191 MessageType: news.defaultMessageType,
192 })
193 .pipe(retrySmsVoiceThrottled);
194 } else if (
195 news.defaultMessageType === undefined &&
196 observed.DefaultMessageType !== undefined
197 ) {
198 yield* smsvoice.deleteDefaultMessageType({ ConfigurationSetName: name }).pipe(
199 retrySmsVoiceThrottled,
200 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
201 Effect.asVoid,
202 );
203 }
204
205 // 3b. Sync tags — diff against OBSERVED cloud tags.
206 yield* syncSmsVoiceTags(observed.ConfigurationSetArn, desiredTags);
207
208 yield* session.note(observed.ConfigurationSetArn);
209 return {
210 configurationSetName: observed.ConfigurationSetName,
211 configurationSetArn: observed.ConfigurationSetArn,
212 };
213 }),
214
215 delete: Effect.fn(function* ({ output }) {
216 yield* smsvoice
217 .deleteConfigurationSet({
218 ConfigurationSetName: output.configurationSetName,
219 })
220 .pipe(
221 retrySmsVoiceThrottled,
222 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
223 Effect.asVoid,
224 );
225 }),
226
227 list: () =>
228 smsvoice.describeConfigurationSets.items({}).pipe(
229 Stream.runCollect,
230 Effect.map((chunk) =>
231 Array.from(chunk).map((cs) => ({
232 configurationSetName: cs.ConfigurationSetName,
233 configurationSetArn: cs.ConfigurationSetArn,
234 })),
235 ),
236 ),
237 };
238 }),
239 );