@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 149 lines 5.12 KB
Raw
1 import { createPhysicalName } from "alchemy/PhysicalName";
2 import { tagRecord } from "alchemy/Tags";
3 import * as Effect from "effect/Effect";
4 import { stripInternalLabels } from "../Labels.ts";
5
6 export const lastSegment = (value: string) => {
7 const trimmed = value.replace(/\/+$/, "");
8 const parts = trimmed.split("/");
9 return parts[parts.length - 1] || trimmed;
10 };
11
12 export const normalizeLocation = (location: string | undefined, fallback: string) =>
13 lastSegment(location ?? fallback).toLowerCase();
14
15 export const rfc1035 = (name: string, maxLength: number): string => {
16 let next = name
17 .toLowerCase()
18 .replace(/[^a-z0-9-]/g, "-")
19 .replace(/-+/g, "-")
20 .replace(/^-+|-+$/g, "");
21 if (!/^[a-z]/.test(next)) next = `a${next}`;
22 next = next.slice(0, maxLength).replace(/-+$/g, "");
23 if (next.length === 0) return "aiplatform";
24 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
25 return next.slice(0, maxLength);
26 };
27
28 export const snakeId = (name: string, maxLength: number): string => {
29 let next = name
30 .toLowerCase()
31 .replace(/[^a-z0-9_]/g, "_")
32 .replace(/_+/g, "_")
33 .replace(/^_+|_+$/g, "");
34 if (!/^[a-z]/.test(next)) next = `f${next}`;
35 next = next.slice(0, maxLength).replace(/_+$/g, "");
36 if (next.length === 0) return "feature";
37 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
38 return next.slice(0, maxLength);
39 };
40
41 export const toPhysicalRfc1035 = (
42 id: string,
43 explicit: string | undefined,
44 existing: string | undefined,
45 maxLength: number,
46 ) =>
47 Effect.gen(function* () {
48 if (explicit !== undefined) return rfc1035(explicit, maxLength);
49 if (existing !== undefined) return existing;
50 return rfc1035(yield* createPhysicalName({ id, maxLength, lowercase: true }), maxLength);
51 });
52
53 export const toPhysicalSnake = (
54 id: string,
55 explicit: string | undefined,
56 existing: string | undefined,
57 maxLength: number,
58 ) =>
59 Effect.gen(function* () {
60 if (explicit !== undefined) return snakeId(explicit, maxLength);
61 if (existing !== undefined) return existing;
62 return snakeId(
63 yield* createPhysicalName({
64 id,
65 maxLength,
66 lowercase: true,
67 delimiter: "_",
68 }),
69 maxLength,
70 );
71 });
72
73 export const parseResourceName = (name: string, collection: string) => {
74 const parts = name.split("/").filter((part) => part.length > 0);
75 const collectionAt = parts.lastIndexOf(collection);
76 const locationsAt = parts.lastIndexOf("locations");
77 const projectsAt = parts.lastIndexOf("projects");
78 return {
79 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
80 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
81 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
82 parent:
83 collectionAt > 0
84 ? parts.slice(0, collectionAt).join("/")
85 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
86 };
87 };
88
89 export const userLabels = (
90 labels: Record<string, string | undefined> | null | undefined,
91 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
92
93 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
94 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
95
96 export const expandParent = (
97 value: string,
98 project: string,
99 location: string,
100 collection: string,
101 ) => {
102 if (value.includes("/")) return value;
103 return `projects/${project}/locations/${location}/${collection}/${value}`;
104 };
105
106 export const canonical = (value: unknown): unknown => {
107 if (value === undefined || value === null) return undefined;
108 if (typeof value === "boolean" || typeof value === "number") return value;
109 if (typeof value === "string") {
110 return value.length === 0 ? undefined : value;
111 }
112 if (Array.isArray(value)) {
113 const items = value.map(canonical).filter((item) => item !== undefined);
114 return items.length === 0 ? undefined : items;
115 }
116 if (typeof value === "object") {
117 const entries = Object.entries(value as Record<string, unknown>)
118 .map(([key, item]) => [key, canonical(item)] as const)
119 .filter(([, item]) => item !== undefined)
120 .sort(([left], [right]) => left.localeCompare(right));
121 if (entries.length === 0) return undefined;
122 return Object.fromEntries(entries);
123 }
124 return undefined;
125 };
126
127 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
128
129 export const specifiedEquals = (desired: unknown, observed: unknown): boolean => {
130 if (desired === undefined) return true;
131 if (
132 typeof desired === "boolean" ||
133 typeof desired === "number" ||
134 typeof desired === "string" ||
135 Array.isArray(desired)
136 ) {
137 return fingerprint(desired) === fingerprint(observed);
138 }
139 if (desired !== null && typeof desired === "object") {
140 const obs =
141 observed !== null && typeof observed === "object"
142 ? (observed as Record<string, unknown>)
143 : {};
144 return Object.entries(desired as Record<string, unknown>).every(([key, value]) =>
145 specifiedEquals(value, obs[key]),
146 );
147 }
148 return fingerprint(desired) === fingerprint(observed);
149 };