@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 328 lines 11 KB
Raw
1 import * as dataplex from "@distilled.cloud/gcp/dataplex_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import { tagRecord } from "alchemy/Tags";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import { createInternalLabels, hasAlchemyLabels, stripInternalLabels } from "../Labels.ts";
9 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
10
11 export const MAX_NAME_LENGTH = 63;
12
13 export const GENERIC_ENTRY_TYPE = "projects/dataplex-types/locations/global/entryTypes/generic";
14
15 export const RELATED_ENTRY_LINK_TYPE =
16 "projects/dataplex-types/locations/global/entryLinkTypes/related";
17
18 export class DataplexNotResolved extends Data.TaggedError("GCP.Dataplex.ResourceNotResolved")<{
19 resourceName: string;
20 }> {}
21
22 export class DataplexStillExists extends Data.TaggedError("GCP.Dataplex.ResourceStillExists")<{
23 resourceName: string;
24 }> {}
25
26 export const lastSegment = (value: string) => {
27 const trimmed = value.replace(/\/+$/, "");
28 const parts = trimmed.split("/");
29 return parts[parts.length - 1] || trimmed;
30 };
31
32 export const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
33 lastSegment(location ?? defaultLocation).toLowerCase();
34
35 export const rfc1035 = (name: string, fallbackOrMax: string | number = "dataplex"): string => {
36 const maxLength = typeof fallbackOrMax === "number" ? fallbackOrMax : MAX_NAME_LENGTH;
37 const fallback = typeof fallbackOrMax === "string" ? fallbackOrMax : "dataplex";
38 let next = name
39 .toLowerCase()
40 .replace(/[^a-z0-9-]/g, "-")
41 .replace(/-+/g, "-")
42 .replace(/^-+|-+$/g, "");
43 if (!/^[a-z]/.test(next)) next = `d${next}`;
44 next = next.slice(0, maxLength).replace(/-+$/g, "");
45 if (next.length === 0) return fallback;
46 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
47 return next.slice(0, maxLength);
48 };
49
50 export const toPhysicalId = (
51 id: string,
52 explicit: string | undefined,
53 existing: string | undefined,
54 fallback: string,
55 ) =>
56 Effect.gen(function* () {
57 if (explicit !== undefined) return rfc1035(explicit, fallback);
58 if (existing !== undefined) return existing;
59 return rfc1035(
60 yield* createPhysicalName({
61 id,
62 maxLength: MAX_NAME_LENGTH,
63 lowercase: true,
64 }),
65 fallback,
66 );
67 });
68
69 export const parseName = (name: string, collection: string) => {
70 const parts = name.split("/").filter((part) => part.length > 0);
71 const collectionAt = parts.lastIndexOf(collection);
72 const locationsAt = parts.lastIndexOf("locations");
73 const projectsAt = parts.lastIndexOf("projects");
74 const orgsAt = parts.lastIndexOf("organizations");
75 return {
76 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
77 organization: orgsAt >= 0 && parts[orgsAt + 1] ? parts[orgsAt + 1]! : "",
78 // API resource names always carry a `locations/{location}` segment.
79 location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "",
80 id: collectionAt >= 0 ? parts.slice(collectionAt + 1).join("/") : lastSegment(name),
81 parent:
82 collectionAt > 0
83 ? parts.slice(0, collectionAt).join("/")
84 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
85 };
86 };
87
88 export const expandParent = (
89 value: string,
90 project: string,
91 location: string,
92 collection: string,
93 ) => {
94 if (value.includes("/")) return value.replace(/\/+$/, "");
95 return `projects/${project}/locations/${location}/${collection}/${value}`;
96 };
97
98 export const parentOf = (project: string, location: string) =>
99 `projects/${project}/locations/${lastSegment(location).toLowerCase()}`;
100
101 export const userLabels = (
102 labels: Record<string, string | undefined> | null | undefined,
103 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
104
105 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
106 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
107
108 export const canonical = (value: unknown): unknown => {
109 if (value === undefined || value === null) return undefined;
110 if (typeof value === "boolean" || typeof value === "number") return value;
111 if (typeof value === "string") {
112 return value.length === 0 ? undefined : value;
113 }
114 if (Array.isArray(value)) {
115 const items = value.map(canonical).filter((item) => item !== undefined);
116 return items.length === 0 ? undefined : items;
117 }
118 if (typeof value === "object") {
119 const entries = Object.entries(value as Record<string, unknown>)
120 .map(([key, item]) => [key, canonical(item)] as const)
121 .filter(([, item]) => item !== undefined)
122 .sort(([left], [right]) => left.localeCompare(right));
123 if (entries.length === 0) return undefined;
124 return Object.fromEntries(entries);
125 }
126 return undefined;
127 };
128
129 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
130
131 export const retryQuota = <A, E extends { _tag: string }, R>(effect: Effect.Effect<A, E, R>) =>
132 effect.pipe(
133 Effect.retry({
134 while: (error) => error._tag === "TooManyRequests",
135 times: 8,
136 schedule: Schedule.exponential("2 seconds"),
137 }),
138 );
139
140 /**
141 * Wait for a Dataplex operation; lakes, zones, and assets provision for
142 * several minutes. ALREADY_EXISTS (code 6) counts as success (create
143 * race); with `notFoundOk`, so does NOT_FOUND (code 5, delete race) and an
144 * operation that is already gone.
145 */
146 export const waitForOperation = (
147 operation: dataplex.GoogleLongrunningOperation,
148 options?: { notFoundOk?: boolean },
149 ) =>
150 waitForGcpOperation(
151 operation,
152 (name) =>
153 retryQuota(
154 name.startsWith("organizations/")
155 ? dataplex.getOrganizationsLocationsOperations({ name })
156 : dataplex.getProjectsLocationsOperations({ name }),
157 ),
158 { budget: "20 minutes", interval: "2 seconds" },
159 ).pipe(
160 Effect.catchIf(
161 (error) =>
162 (error._tag === "GCP.OperationFailed" &&
163 (error.code === 6 || (options?.notFoundOk === true && error.code === 5))) ||
164 (options?.notFoundOk === true && error._tag === "NotFound"),
165 () => Effect.succeed(operation),
166 ),
167 );
168
169 export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
170 get: Effect.Effect<A | undefined, E, R>,
171 name: string,
172 ) =>
173 get.pipe(
174 Effect.flatMap((value) =>
175 value ? Effect.succeed(value) : Effect.fail(new DataplexNotResolved({ resourceName: name })),
176 ),
177 Effect.retry({
178 while: (error) => error._tag === "GCP.Dataplex.ResourceNotResolved",
179 times: 8,
180 schedule: Schedule.spaced("1 second"),
181 }),
182 );
183
184 export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
185 get: Effect.Effect<A | undefined, E, R>,
186 name: string,
187 ) =>
188 get.pipe(
189 Effect.flatMap((value) =>
190 value === undefined
191 ? Effect.void
192 : Effect.fail(new DataplexStillExists({ resourceName: name })),
193 ),
194 Effect.retry({
195 while: (error) => error._tag === "GCP.Dataplex.ResourceStillExists",
196 times: 10,
197 schedule: Schedule.spaced("1 second"),
198 }),
199 );
200
201 export const collectPages = <Page, A, E, R>(
202 pages: Stream.Stream<Page, E, R>,
203 items: (page: Page) => readonly A[] | undefined,
204 ) =>
205 pages.pipe(
206 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
207 Stream.runCollect,
208 Effect.map((chunk) => Array.from(chunk)),
209 );
210
211 export const listAtLocation = <A, E, R>(
212 project: string,
213 region: string,
214 list: (parent: string) => Effect.Effect<A[], E, R>,
215 ) =>
216 // Prefer the all-locations wildcard; fall back to the default region
217 // (whose error, if any, propagates).
218 list(`projects/${project}/locations/-`).pipe(
219 Effect.catch(() => list(`projects/${project}/locations/${region}`)),
220 );
221
222 export const replaceOnIdentity = (input: {
223 previousId: string | undefined;
224 nextId: string | undefined;
225 previousLocation: string;
226 nextLocation: string;
227 extra?: boolean;
228 previousParent?: string;
229 nextParent?: string;
230 }) => {
231 const parentChanged =
232 (input.previousParent ?? "") !== "" &&
233 (input.nextParent ?? "") !== "" &&
234 lastSegment(input.previousParent ?? "") !== lastSegment(input.nextParent ?? "") &&
235 (input.previousParent ?? "") !== (input.nextParent ?? "");
236 const replace =
237 (input.extra ?? false) ||
238 parentChanged ||
239 (input.previousId !== undefined &&
240 input.nextId !== undefined &&
241 input.nextId !== input.previousId) ||
242 input.previousLocation !== input.nextLocation;
243 if (!replace) return undefined;
244 const samePhysical =
245 input.previousLocation === input.nextLocation &&
246 !parentChanged &&
247 input.previousId !== undefined &&
248 input.nextId === input.previousId;
249 return {
250 action: "replace" as const,
251 deleteFirst: samePhysical,
252 };
253 };
254
255 export const isPendingState = (state: string | undefined) =>
256 state === "CREATING" ||
257 state === "DELETING" ||
258 state === "UPDATING" ||
259 state === "STATE_UNSPECIFIED" ||
260 state === undefined ||
261 state === "";
262
263 export const snakeId = (name: string, maxLength = 256): string => {
264 let next = name
265 .toLowerCase()
266 .replace(/[^a-z0-9_]/g, "_")
267 .replace(/_+/g, "_")
268 .replace(/^_+|_+$/g, "");
269 if (!/^[a-z]/.test(next)) next = `e${next}`;
270 next = next.slice(0, maxLength).replace(/_+$/g, "");
271 if (next.length === 0) return "entity";
272 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`;
273 return next.slice(0, maxLength);
274 };
275
276 export const toPhysicalSnake = (
277 id: string,
278 explicit: string | undefined,
279 existing: string | undefined,
280 maxLength = 256,
281 ) =>
282 Effect.gen(function* () {
283 if (explicit !== undefined) return snakeId(explicit, maxLength);
284 if (existing !== undefined) return existing;
285 return snakeId(
286 yield* createPhysicalName({
287 id,
288 maxLength,
289 lowercase: true,
290 delimiter: "_",
291 }),
292 maxLength,
293 );
294 });
295
296 export const createOwnership = (id: string) => createInternalLabels(id);
297
298 export const ownedLabels = (id: string, labels: Record<string, string>) =>
299 hasAlchemyLabels(id, labels);
300
301 export const parseResourceName = parseName;
302 export const locationParent = parentOf;
303 export const hasAlchemyLabelKeys = hasAlchemyLabelMap;
304 export const LABELS_FILTER = "labels.alchemy-id:*";
305 export const sameJson = (left: unknown, right: unknown) => fingerprint(left) === fingerprint(right);
306 export const sameStringList = (
307 left: readonly string[] | undefined,
308 right: readonly string[] | undefined,
309 ) => JSON.stringify([...(left ?? [])].sort()) === JSON.stringify([...(right ?? [])].sort());
310 export const toResourceId = (
311 id: string,
312 explicit: string | undefined,
313 existing: string | undefined,
314 ) => toPhysicalId(id, explicit, existing, "dataplex");
315 export const updateMaskOf = (fields: ReadonlyArray<readonly [boolean, string]>): string =>
316 fields
317 .filter(([changed]) => changed)
318 .map(([, field]) => field)
319 .join(",");
320 export const emptyOnMissing = <A, E extends { readonly _tag: string }, R>(
321 effect: Effect.Effect<A[], E, R>,
322 ) =>
323 effect.pipe(
324 Effect.catchIf(
325 (error) => error._tag === "NotFound",
326 () => Effect.succeed([] as A[]),
327 ),
328 );