@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 62 lines 2.31 KB
Raw
1 import * as amp from "@distilled.cloud/aws/amp";
2 import { diffTags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Coerce an AMP wire tag map (values are `string | undefined`) into a plain
7 * `Record<string, string>`, dropping undefined values.
8 */
9 export const toTagRecord = (
10 tags: { [key: string]: string | undefined } | undefined,
11 ): Record<string, string> =>
12 Object.fromEntries(
13 Object.entries(tags ?? {}).filter(
14 (entry): entry is [string, string] => typeof entry[1] === "string",
15 ),
16 );
17
18 /**
19 * Read the observed tags of an AMP resource by ARN. Tag reads are
20 * best-effort — a failure (e.g. a race with deletion) reports no tags.
21 */
22 export const readAmpTags = Effect.fn(function* (arn: string) {
23 const response = yield* amp
24 .listTagsForResource({ resourceArn: arn })
25 .pipe(Effect.orElseSucceed(() => undefined));
26 return toTagRecord(response?.tags);
27 });
28
29 /**
30 * Sync tags on an AMP resource: diff the OBSERVED cloud tags against the
31 * desired set and apply only the delta. AMP's `tagResource` takes a tag
32 * map (not a list), so the `upsert` delta is folded back into a record.
33 */
34 export const syncAmpTags = Effect.fn(function* (arn: string, desiredTags: Record<string, string>) {
35 const observedTags = yield* readAmpTags(arn);
36 const { removed, upsert } = diffTags(observedTags, desiredTags);
37 if (upsert.length > 0) {
38 yield* amp.tagResource({
39 resourceArn: arn,
40 tags: Object.fromEntries(upsert.map((t) => [t.Key, t.Value])),
41 });
42 }
43 if (removed.length > 0) {
44 yield* amp.untagResource({ resourceArn: arn, tagKeys: removed });
45 }
46 });
47
48 /**
49 * AMP's logging APIs expect the destination CloudWatch Logs log group ARN
50 * with a trailing `:*` (the log-stream wildcard). Append it when missing so
51 * callers can pass a `Logs.LogGroup`'s `logGroupArn` directly.
52 */
53 export const normalizeAmpLogGroupArn = (arn: string): string =>
54 arn.endsWith(":*") ? arn : `${arn}:*`;
55
56 /** Encode a UTF-8 string definition into the wire blob AMP expects. */
57 export const encodeDefinition = (definition: string): Effect.Effect<Uint8Array> =>
58 Effect.sync(() => new TextEncoder().encode(definition));
59
60 /** Decode an AMP definition blob back into a UTF-8 string. */
61 export const decodeDefinition = (data: Uint8Array): Effect.Effect<string> =>
62 Effect.sync(() => new TextDecoder().decode(data));