The best way to control Claude Code, Codex and more from any computer or phone

main
ts 48 lines 1.83 KB
Raw
1 import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts";
2
3 export { createInternalLabels, hasAlchemyLabels };
4
5 /**
6 * Stamp Alchemy ownership into a description. Used by Network Security
7 * resources that have no labels field (UrlList, TlsInspectionPolicy).
8 */
9 export const encodeDescription = (
10 labels: Record<string, string>,
11 description: string | undefined,
12 ): string => {
13 const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
14 return description ? `${marker}\n${description}` : marker;
15 };
16
17 export const parseDescription = (
18 description: string | undefined,
19 ): {
20 labels: Record<string, string>;
21 description: string | undefined;
22 } => {
23 if (!description?.startsWith("[alchemy ")) {
24 return { labels: {}, description };
25 }
26 const end = description.indexOf("]");
27 if (end < 0) return { labels: {}, description };
28 const labels: Record<string, string> = {};
29 for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) {
30 const eq = part.indexOf("=");
31 if (eq > 0) {
32 labels[part.slice(0, eq)] = part.slice(eq + 1);
33 }
34 }
35 const rest = description.slice(end + 1).replace(/^\n/, "");
36 return { labels, description: rest.length > 0 ? rest : undefined };
37 };
38
39 export const hasOwnershipMarker = (description: string | undefined) =>
40 Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-"));
41
42 export const sameJson = (left: unknown, right: unknown) =>
43 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
44
45 export const sameStringList = (
46 left: readonly string[] | undefined,
47 right: readonly string[] | undefined,
48 ) => JSON.stringify([...(left ?? [])]) === JSON.stringify([...(right ?? [])]);