@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, 04:45 UTC

main
ts 82 lines 2.86 KB
Raw
1 import { Stack } from "alchemy/Stack";
2 import { createInternalTags, diffTags, hasTags, type Tags } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4
5 /**
6 * Stripe metadata keys may only contain alphanumeric characters, dashes,
7 * and underscores (max 40 chars). Alchemy ownership tags use `alchemy::stack`
8 * / `alchemy::stage` / `alchemy::id` — map those onto `alchemy_stack` /
9 * `alchemy_stage` / `alchemy_id` so they survive the Stripe API.
10 */
11 export const ALCHEMY_METADATA_PREFIX = "alchemy_";
12
13 export const alchemyMetadataKeys = {
14 stack: "alchemy_stack",
15 stage: "alchemy_stage",
16 id: "alchemy_id",
17 } as const;
18
19 const TAG_PREFIX = "alchemy::";
20
21 export const toMetadataKey = (tagKey: string): string =>
22 tagKey.startsWith(TAG_PREFIX)
23 ? `${ALCHEMY_METADATA_PREFIX}${tagKey.slice(TAG_PREFIX.length)}`
24 : tagKey;
25
26 export const toTagKey = (metadataKey: string): string =>
27 metadataKey.startsWith(ALCHEMY_METADATA_PREFIX)
28 ? `${TAG_PREFIX}${metadataKey.slice(ALCHEMY_METADATA_PREFIX.length)}`
29 : metadataKey;
30
31 /** Stripe metadata values are capped at 500 characters. */
32 export const sanitizeMetadataValue = (value: string): string => value.slice(0, 500);
33
34 export const toMetadata = (
35 tags: Record<string, string> | null | undefined,
36 ): Record<string, string> =>
37 Object.fromEntries(
38 Object.entries(tags ?? {}).map(([key, value]) => [
39 toMetadataKey(key).slice(0, 40),
40 sanitizeMetadataValue(value),
41 ]),
42 );
43
44 export const fromMetadata = (
45 metadata: Record<string, string> | null | undefined,
46 ): Record<string, string> =>
47 Object.fromEntries(Object.entries(metadata ?? {}).map(([key, value]) => [toTagKey(key), value]));
48
49 export const createInternalMetadata = Effect.fn(function* (id: string) {
50 return toMetadata(yield* createInternalTags(id));
51 });
52
53 export const stripInternalMetadata = (
54 metadata: Record<string, string> | null | undefined,
55 ): Record<string, string> =>
56 Object.fromEntries(
57 Object.entries(metadata ?? {}).filter(([key]) => !key.startsWith(ALCHEMY_METADATA_PREFIX)),
58 );
59
60 export const hasAlchemyMetadata = Effect.fn(function* (id: string, metadata: Tags | undefined) {
61 const expected = yield* createInternalMetadata(id);
62 return hasTags(expected, metadata);
63 });
64
65 /**
66 * Diff observed Stripe metadata against desired metadata. Always pass
67 * **observed** metadata as `oldTags` — never `olds.metadata` or
68 * `output.metadata` — so adoption converges.
69 */
70 export const diffMetadata = diffTags;
71
72 /**
73 * Stripe Search query matching Alchemy-owned objects on the current stack.
74 * Example: `metadata["alchemy_stack"]:"Nuke"`
75 *
76 * `list()` for nuke must still enumerate every object whose metadata has
77 * `alchemy_stack` (account-wide), not only the current stack name.
78 */
79 export const alchemyStackSearchQuery = Effect.fn(function* () {
80 const stack = yield* Stack;
81 return `metadata["${alchemyMetadataKeys.stack}"]:"${stack.name}"`;
82 });