@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, 06:00 UTC

main
ts 78 lines 2.8 KB
Raw
1 import * as ivs from "@distilled.cloud/aws/ivs";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 /**
7 * IVS wire tags allow `undefined` values in the record type — flatten to a
8 * plain string record, dropping malformed entries.
9 */
10 export const toTagRecord = (
11 tags: { [key: string]: string | undefined } | undefined,
12 ): Record<string, string> =>
13 Object.fromEntries(
14 Object.entries(tags ?? {}).filter(
15 (entry): entry is [string, string] => typeof entry[1] === "string",
16 ),
17 );
18
19 /**
20 * Read the observed tags of an IVS resource. Tag reads are best-effort —
21 * a failure (e.g. a race with deletion) reports no tags.
22 */
23 export const readIvsTags = Effect.fn(function* (arn: string) {
24 const response = yield* ivs
25 .listTagsForResource({ resourceArn: arn })
26 .pipe(Effect.orElseSucceed(() => undefined));
27 return toTagRecord(response?.tags);
28 });
29
30 /**
31 * Sync tags on an IVS resource: diff the OBSERVED cloud tags against the
32 * desired set and apply only the delta.
33 */
34 export const syncIvsTags = Effect.fn(function* (arn: string, desiredTags: Record<string, string>) {
35 const observedTags = yield* readIvsTags(arn);
36 const { removed, upsert } = diffTags(observedTags, desiredTags);
37 if (upsert.length > 0) {
38 yield* ivs
39 .tagResource({
40 resourceArn: arn,
41 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
42 })
43 .pipe(retryWhileThrottled);
44 }
45 if (removed.length > 0) {
46 yield* ivs.untagResource({ resourceArn: arn, tagKeys: removed }).pipe(retryWhileThrottled);
47 }
48 });
49
50 /**
51 * Explicitly-typed pipeable retry helper. Inlining `Effect.retry` in a
52 * provider op lets `Retry.Return`'s conditional type survive into
53 * declaration emit and widens the provider layer to `unknown` for every
54 * `AWS.providers()` consumer — keep the annotation explicit.
55 *
56 * Retries `ConflictException` (e.g. deleting a channel while a stream is
57 * live, or a resource is mid-transition) on a bounded schedule.
58 */
59 export const retryWhileConflict = <A, E extends { readonly _tag: string }, R>(
60 self: Effect.Effect<A, E, R>,
61 ): Effect.Effect<A, E, R> =>
62 Effect.retry(self, {
63 while: (e) => e._tag === "ConflictException",
64 schedule: Schedule.max([Schedule.fixed("3 seconds"), Schedule.recurs(8)]),
65 });
66
67 /**
68 * Retries `ThrottlingException` on a bounded exponential schedule. IVS
69 * APIs have very low TPS limits (e.g. ListPlaybackKeyPairs is 1 TPS), so
70 * back-to-back reconciles routinely trip 429s.
71 */
72 export const retryWhileThrottled = <A, E extends { readonly _tag: string }, R>(
73 self: Effect.Effect<A, E, R>,
74 ): Effect.Effect<A, E, R> =>
75 Effect.retry(self, {
76 while: (e) => e._tag === "ThrottlingException",
77 schedule: Schedule.max([Schedule.exponential("1 second"), Schedule.recurs(6)]),
78 });