@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 195 lines 6.76 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 OptOutListProps {
20 /**
21 * Name of the opt-out list (`[A-Za-z0-9_-]+`, 1-64 characters).
22 * Changing the name replaces the opt-out list.
23 * @default ${app}-${stage}-${id}
24 */
25 optOutListName?: string;
26 /**
27 * Tags to apply to the opt-out list. Merged with internal Alchemy tags.
28 */
29 tags?: Record<string, string>;
30 }
31
32 export interface OptOutList extends Resource<
33 "AWS.PinpointSMSVoiceV2.OptOutList",
34 OptOutListProps,
35 {
36 /**
37 * Name of the opt-out list.
38 */
39 optOutListName: string;
40 /**
41 * ARN of the opt-out list.
42 */
43 optOutListArn: string;
44 },
45 never,
46 Providers
47 > {}
48
49 /**
50 * An AWS End User Messaging SMS (Pinpoint SMS Voice v2) opt-out list — a
51 * list of destination phone numbers that opted out of receiving your SMS
52 * or voice messages.
53 *
54 * When an end user replies with a supported opt-out keyword (STOP,
55 * CANCEL, OPTOUT, ...), their number is added to the list automatically
56 * and further messages to it are suppressed.
57 * ### Creating Opt-Out Lists
58 * **Example:** Basic Opt-Out List
59 * ```typescript
60 * import * as PinpointSMSVoiceV2 from "@alchemy.run/aws/PinpointSMSVoiceV2";
61 *
62 * const optOuts = yield* PinpointSMSVoiceV2.OptOutList("OptOuts");
63 * ```
64 *
65 * **Example:** Named Opt-Out List with Tags
66 * ```typescript
67 * const optOuts = yield* PinpointSMSVoiceV2.OptOutList("OptOuts", {
68 * optOutListName: "marketing-opt-outs",
69 * tags: { team: "growth" },
70 * });
71 * ```
72 *
73 * @resource
74 */
75 export const OptOutList = Resource<OptOutList>("AWS.PinpointSMSVoiceV2.OptOutList");
76
77 /**
78 * Raised when an opt-out list cannot be observed immediately after it
79 * was created — the create call succeeded (or raced a peer) but the
80 * follow-up describe found nothing.
81 */
82 export class SmsVoiceOptOutListMissing extends Data.TaggedError("SmsVoiceOptOutListMissing")<{
83 message: string;
84 }> {}
85
86 export const OptOutListProvider = () =>
87 Provider.effect(
88 OptOutList,
89 Effect.gen(function* () {
90 const toName = (id: string, props: OptOutListProps) =>
91 props.optOutListName
92 ? Effect.succeed(props.optOutListName)
93 : createPhysicalName({ id, maxLength: 64 });
94
95 /**
96 * Observe an opt-out list by name. `DescribeOptOutLists` raises a
97 * typed `ResourceNotFoundException` for an unknown name.
98 */
99 const getByName = Effect.fn(function* (name: string) {
100 const result = yield* smsvoice.describeOptOutLists({ OptOutListNames: [name] }).pipe(
101 retrySmsVoiceThrottled,
102 Effect.catchTag("ResourceNotFoundException", () => Effect.undefined),
103 );
104 return result?.OptOutLists?.find((l) => l.OptOutListName === name);
105 });
106
107 return {
108 stables: ["optOutListName", "optOutListArn"],
109
110 read: Effect.fn(function* ({ id, olds, output }) {
111 const name = output?.optOutListName ?? (yield* toName(id, olds ?? {}));
112 const observed = yield* getByName(name);
113 if (observed === undefined) return undefined;
114 const attrs = {
115 optOutListName: observed.OptOutListName,
116 optOutListArn: observed.OptOutListArn,
117 };
118 const tags = yield* readSmsVoiceTags(observed.OptOutListArn);
119 return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs);
120 }),
121
122 diff: Effect.fn(function* ({ id, news, olds }) {
123 if (!isResolved(news)) return undefined;
124 const oldName = yield* toName(id, olds ?? {});
125 const newName = yield* toName(id, news);
126 if (oldName !== newName) return { action: "replace" } as const;
127 }),
128
129 reconcile: Effect.fn(function* ({ id, news, output, session }) {
130 const name = output?.optOutListName ?? (yield* toName(id, news));
131 const internalTags = yield* createInternalTags(id);
132 const desiredTags = { ...news.tags, ...internalTags };
133
134 // 1. Observe — cloud state is authoritative.
135 let observed = yield* getByName(name);
136
137 // 2. Ensure — create if missing; a ConflictException is a race
138 // with a peer reconciler, so fall through to re-observe.
139 if (observed === undefined) {
140 yield* smsvoice
141 .createOptOutList({
142 OptOutListName: name,
143 Tags: toTagList(desiredTags),
144 })
145 .pipe(
146 retrySmsVoiceThrottled,
147 Effect.catchTag("ConflictException", () => Effect.void),
148 Effect.asVoid,
149 );
150 observed = yield* getByName(name);
151 }
152 if (observed === undefined) {
153 return yield* new SmsVoiceOptOutListMissing({
154 message: `opt-out list '${name}' not observable after create`,
155 });
156 }
157
158 // 3. Sync tags — diff against OBSERVED cloud tags.
159 yield* syncSmsVoiceTags(observed.OptOutListArn, desiredTags);
160
161 yield* session.note(observed.OptOutListArn);
162 return {
163 optOutListName: observed.OptOutListName,
164 optOutListArn: observed.OptOutListArn,
165 };
166 }),
167
168 delete: Effect.fn(function* ({ output }) {
169 yield* smsvoice.deleteOptOutList({ OptOutListName: output.optOutListName }).pipe(
170 retrySmsVoiceThrottled,
171 Effect.catchTag("ResourceNotFoundException", () => Effect.void),
172 Effect.asVoid,
173 );
174 }),
175
176 list: () =>
177 smsvoice.describeOptOutLists.items({}).pipe(
178 Stream.runCollect,
179 Effect.map((chunk) =>
180 Array.from(chunk)
181 // The `Default` opt-out list is service-managed: End User
182 // Messaging recreates it automatically and then rejects
183 // DeleteOptOutList with ConflictException
184 // RESOURCE_MODIFICATION_NOT_ALLOWED (verified live) — keep
185 // it out of enumeration for account-wide teardown (nuke).
186 .filter((l) => l.OptOutListName !== "Default")
187 .map((l) => ({
188 optOutListName: l.OptOutListName,
189 optOutListArn: l.OptOutListArn,
190 })),
191 ),
192 ),
193 };
194 }),
195 );