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, 06:00 UTC
| 1 | import * as smsvoice from "@distilled.cloud/aws/pinpoint-sms-voice-v2"; |
| 2 | import { diffTags } from "alchemy/Tags"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Schedule from "effect/Schedule"; |
| 5 | |
| 6 | /** |
| 7 | * Flatten the wire `Tag[]` list into a plain string record. |
| 8 | */ |
| 9 | export const toTagRecord = (tags: readonly smsvoice.Tag[] | undefined): Record<string, string> => |
| 10 | Object.fromEntries((tags ?? []).map((t) => [t.Key, t.Value])); |
| 11 | |
| 12 | /** |
| 13 | * Convert a plain string record into the wire `Tag[]` list. |
| 14 | */ |
| 15 | export const toTagList = (tags: Record<string, string>): smsvoice.Tag[] => |
| 16 | Object.entries(tags).map(([Key, Value]) => ({ Key, Value })); |
| 17 | |
| 18 | /** |
| 19 | * Read the observed tags of an End User Messaging SMS resource. |
| 20 | * Best-effort — a failure (e.g. a race with deletion) reports no tags. |
| 21 | */ |
| 22 | export const readSmsVoiceTags = Effect.fn(function* (arn: string) { |
| 23 | const response = yield* smsvoice |
| 24 | .listTagsForResource({ ResourceArn: arn }) |
| 25 | .pipe(Effect.orElseSucceed(() => undefined)); |
| 26 | return toTagRecord(response?.Tags); |
| 27 | }); |
| 28 | |
| 29 | /** |
| 30 | * Sync tags on an End User Messaging SMS resource: diff the OBSERVED |
| 31 | * cloud tags against the desired set and apply only the delta. |
| 32 | */ |
| 33 | export const syncSmsVoiceTags = Effect.fn(function* ( |
| 34 | arn: string, |
| 35 | desiredTags: Record<string, string>, |
| 36 | ) { |
| 37 | const observedTags = yield* readSmsVoiceTags(arn); |
| 38 | const { removed, upsert } = diffTags(observedTags, desiredTags); |
| 39 | if (upsert.length > 0) { |
| 40 | yield* smsvoice.tagResource({ ResourceArn: arn, Tags: upsert }).pipe(retrySmsVoiceThrottled); |
| 41 | } |
| 42 | if (removed.length > 0) { |
| 43 | yield* smsvoice |
| 44 | .untagResource({ ResourceArn: arn, TagKeys: removed }) |
| 45 | .pipe(retrySmsVoiceThrottled); |
| 46 | } |
| 47 | }); |
| 48 | |
| 49 | /** |
| 50 | * Explicitly-typed pipeable retry helper (inlining `Effect.retry` in a |
| 51 | * provider op widens the layer type in declaration emit). Retries |
| 52 | * `ThrottlingException` on a bounded exponential schedule — the End User |
| 53 | * Messaging SMS control plane has low per-account TPS limits. |
| 54 | */ |
| 55 | export const retrySmsVoiceThrottled = <A, E extends { readonly _tag: string }, R>( |
| 56 | self: Effect.Effect<A, E, R>, |
| 57 | ): Effect.Effect<A, E, R> => |
| 58 | Effect.retry(self, { |
| 59 | while: (e) => e._tag === "ThrottlingException", |
| 60 | schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]), |
| 61 | }); |