@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 55 lines 2.04 KB
Raw
1 import * as personalize from "@distilled.cloud/aws/personalize";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import * as Redacted from "effect/Redacted";
5
6 /**
7 * Unwrap a Personalize `SensitiveString` (decoded as `Redacted`) to its plain
8 * string value. `String(redacted)` would yield `"<redacted>"`, not the value.
9 */
10 export const unredact = (value: string | Redacted.Redacted<string>): string =>
11 Redacted.isRedacted(value) ? Redacted.value(value) : value;
12
13 /**
14 * Coerce a Personalize wire tag list (`{ tagKey, tagValue }[]`, values decode
15 * as `Redacted`) into a plain `Record<string, string>`.
16 */
17 export const toTagRecord = (tags: personalize.Tag[] | undefined): Record<string, string> =>
18 Object.fromEntries((tags ?? []).map((t) => [unredact(t.tagKey), unredact(t.tagValue)] as const));
19
20 /**
21 * Read the observed tags of a Personalize resource by ARN. Tag reads are
22 * best-effort — a failure (e.g. a race with deletion) reports no tags.
23 */
24 export const readPersonalizeTags = Effect.fn(function* (arn: string) {
25 const response = yield* personalize
26 .listTagsForResource({ resourceArn: arn })
27 .pipe(Effect.orElseSucceed(() => undefined));
28 return toTagRecord(response?.tags);
29 });
30
31 /**
32 * Sync tags on a Personalize resource: diff the OBSERVED cloud tags against
33 * the desired set and apply only the delta.
34 */
35 export const syncPersonalizeTags = Effect.fn(function* (
36 arn: string,
37 desiredTags: Record<string, string>,
38 ) {
39 const observedTags = yield* readPersonalizeTags(arn);
40 const { removed, upsert } = diffTags(observedTags, desiredTags);
41 if (upsert.length > 0) {
42 yield* personalize.tagResource({
43 resourceArn: arn,
44 tags: upsert.map((t) => ({ tagKey: t.Key, tagValue: t.Value })),
45 });
46 }
47 if (removed.length > 0) {
48 // tagKeys decodes as SensitiveString — wrap in Redacted for the encoder
49 // (which unwraps back to the plain string on the wire).
50 yield* personalize.untagResource({
51 resourceArn: arn,
52 tagKeys: removed.map((key) => Redacted.make(key)),
53 });
54 }
55 });