@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 65 lines 2.4 KB
Raw
1 import * as forecast from "@distilled.cloud/aws/forecast";
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 Forecast `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 Forecast wire tag list (`{ Key, Value }[]`, values decode as
15 * `Redacted`) into a plain `Record<string, string>`.
16 */
17 export const toTagRecord = (tags: forecast.Tag[] | undefined): Record<string, string> =>
18 Object.fromEntries((tags ?? []).map((t) => [unredact(t.Key), unredact(t.Value)] as const));
19
20 /**
21 * Read the observed tags of a Forecast resource by ARN. Tag reads are
22 * best-effort — a failure (e.g. a race with deletion) reports no tags.
23 */
24 export const readForecastTags = Effect.fn(function* (arn: string) {
25 const response = yield* forecast
26 .listTagsForResource({ ResourceArn: arn })
27 .pipe(Effect.orElseSucceed(() => undefined));
28 return toTagRecord(response?.Tags);
29 });
30
31 /**
32 * Sync tags on a Forecast resource: diff the OBSERVED cloud tags against the
33 * desired set and apply only the delta.
34 */
35 export const syncForecastTags = Effect.fn(function* (
36 arn: string,
37 desiredTags: Record<string, string>,
38 ) {
39 const observedTags = yield* readForecastTags(arn);
40 const { removed, upsert } = diffTags(observedTags, desiredTags);
41 if (upsert.length > 0) {
42 yield* forecast.tagResource({
43 ResourceArn: arn,
44 Tags: upsert.map((t) => ({ Key: t.Key, Value: 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* forecast.untagResource({
51 ResourceArn: arn,
52 TagKeys: removed.map((key) => Redacted.make(key)),
53 });
54 }
55 });
56
57 /**
58 * Coerce a generated physical name into Forecast's identifier constraint
59 * (`^[a-zA-Z][a-zA-Z0-9_]*` — underscores only, must start with a letter).
60 * `createPhysicalName` emits DNS-style hyphens, which Forecast rejects.
61 */
62 export const toForecastName = (name: string) => {
63 const sanitized = name.replaceAll(/[^a-zA-Z0-9_]/g, "_");
64 return /^[a-zA-Z]/.test(sanitized) ? sanitized : `f${sanitized.slice(0, 62)}`;
65 };