@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 264 lines 9.14 KB
Raw
1 import * as ds from "@distilled.cloud/gcp/datastream_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 { stripInternalLabels } from "../Labels.ts";
9 import { waitForOperation as waitForLongRunning } from "../Operation.ts";
10
11 export const MAX_NAME_LENGTH = 63;
12
13 export class ResourceNotResolved extends Data.TaggedError("GCP.Datastream.ResourceNotResolved")<{
14 resourceName: string;
15 }> {}
16
17 export class ResourceStillExists extends Data.TaggedError("GCP.Datastream.ResourceStillExists")<{
18 resourceName: string;
19 }> {}
20
21 export const lastSegment = (value: string) => {
22 const trimmed = value.replace(/\/+$/, "");
23 const parts = trimmed.split("/");
24 return parts[parts.length - 1] || trimmed;
25 };
26
27 export const rfc1035 = (name: string, fallback = "ds"): string => {
28 let next = name
29 .toLowerCase()
30 .replace(/[^a-z0-9-]/g, "-")
31 .replace(/-+/g, "-")
32 .replace(/^-+|-+$/g, "");
33 if (!/^[a-z]/.test(next)) next = `d${next}`;
34 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
35 if (next.length === 0) return fallback;
36 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
37 return next.slice(0, MAX_NAME_LENGTH);
38 };
39
40 export const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
41 lastSegment(location ?? defaultLocation).toLowerCase();
42
43 export const locationParent = (project: string, location: string) =>
44 `projects/${project}/locations/${lastSegment(location).toLowerCase()}`;
45
46 export const toPhysicalId = (
47 id: string,
48 explicit: string | undefined,
49 existing: string | undefined,
50 fallback = "ds",
51 ) =>
52 Effect.gen(function* () {
53 if (explicit !== undefined) return rfc1035(explicit, fallback);
54 if (existing !== undefined) return existing;
55 return rfc1035(
56 yield* createPhysicalName({
57 id,
58 maxLength: MAX_NAME_LENGTH,
59 lowercase: true,
60 }),
61 fallback,
62 );
63 });
64
65 export const parseName = (name: string, collection: string, defaultLocation: string) => {
66 const parts = name.split("/").filter((part) => part.length > 0);
67 const collectionAt = parts.lastIndexOf(collection);
68 const locationsAt = parts.lastIndexOf("locations");
69 const projectsAt = parts.lastIndexOf("projects");
70 return {
71 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
72 location:
73 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation,
74 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
75 parent:
76 collectionAt > 0
77 ? parts.slice(0, collectionAt).join("/")
78 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
79 };
80 };
81
82 export const expandParent = (
83 value: string,
84 project: string,
85 location: string,
86 collection: string,
87 ) => {
88 if (value.includes("/")) return value.replace(/\/+$/, "");
89 return `projects/${project}/locations/${location}/${collection}/${value}`;
90 };
91
92 export const connectionProfileOf = (value: string, project: string, location: string) =>
93 expandParent(value, project, location, "connectionProfiles");
94
95 export const privateConnectionOf = (value: string, project: string, location: string) =>
96 expandParent(value, project, location, "privateConnections");
97
98 export const networkOf = (value: string, project: string) => {
99 const trimmed = value.replace(/\/+$/, "");
100 const projectsAt = trimmed.indexOf("projects/");
101 if (projectsAt >= 0) return trimmed.slice(projectsAt);
102 return `projects/${project}/global/networks/${trimmed}`;
103 };
104
105 export const userLabels = (
106 labels: Record<string, string | undefined> | null | undefined,
107 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
108
109 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
110 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
111
112 export const canonical = (value: unknown): unknown => {
113 if (value === undefined || value === null) return undefined;
114 if (typeof value === "boolean" || typeof value === "number") return value;
115 if (typeof value === "string") {
116 return value.length === 0 ? undefined : value;
117 }
118 if (Array.isArray(value)) {
119 const items = value.map(canonical).filter((item) => item !== undefined);
120 return items.length === 0 ? undefined : items;
121 }
122 if (typeof value === "object") {
123 const entries = Object.entries(value as Record<string, unknown>)
124 .map(([key, item]) => [key, canonical(item)] as const)
125 .filter(([, item]) => item !== undefined)
126 .sort(([left], [right]) => left.localeCompare(right));
127 if (entries.length === 0) return undefined;
128 return Object.fromEntries(entries);
129 }
130 return undefined;
131 };
132
133 export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null);
134
135 export const fieldMask = (fields: Array<string | false | undefined>) =>
136 fields.filter((field): field is string => typeof field === "string").join(",");
137
138 export const emptyMessage = (value: unknown) => (value === undefined ? undefined : {});
139
140 export const replaceOnIdentity = (input: {
141 previousId: string | undefined;
142 nextId: string | undefined;
143 previousLocation: string;
144 nextLocation: string;
145 extra?: boolean;
146 previousParent?: string;
147 nextParent?: string;
148 }) => {
149 const parentChanged =
150 (input.previousParent ?? "") !== "" &&
151 (input.nextParent ?? "") !== "" &&
152 (input.previousParent ?? "") !== (input.nextParent ?? "");
153 const replace =
154 (input.extra ?? false) ||
155 parentChanged ||
156 (input.previousId !== undefined &&
157 input.nextId !== undefined &&
158 input.nextId !== input.previousId) ||
159 input.previousLocation !== input.nextLocation;
160 if (!replace) return undefined;
161 const samePhysical =
162 input.previousLocation === input.nextLocation &&
163 !parentChanged &&
164 input.previousId !== undefined &&
165 input.nextId === input.previousId;
166 return {
167 action: "replace" as const,
168 deleteFirst: samePhysical,
169 };
170 };
171
172 /**
173 * Wait for a Datastream long-running operation. Private connections peer a
174 * VPC and can take 10–20 minutes.
175 * `ALREADY_EXISTS` (a concurrent create won) always succeeds;
176 * `notFoundOk` also accepts a `NOT_FOUND` result or an operation that has
177 * already been garbage-collected (deletes).
178 */
179 export const waitForOperation = (operation: ds.Operation, options?: { notFoundOk?: boolean }) =>
180 waitForLongRunning(operation, (name) => ds.getProjectsLocationsOperations({ name }), {
181 budget: "30 minutes",
182 }).pipe(
183 Effect.catchIf(
184 (error) =>
185 error._tag === "GCP.OperationFailed" &&
186 (error.code === 6 || (options?.notFoundOk === true && error.code === 5)),
187 () => Effect.void,
188 ),
189 Effect.catchIf(
190 (error) => options?.notFoundOk === true && error._tag === "NotFound",
191 () => Effect.void,
192 ),
193 );
194
195 export const waitUntilExists = <A, E extends { readonly _tag: string }, R>(
196 get: Effect.Effect<A | undefined, E, R>,
197 name: string,
198 ) =>
199 get.pipe(
200 Effect.flatMap((value) =>
201 value ? Effect.succeed(value) : Effect.fail(new ResourceNotResolved({ resourceName: name })),
202 ),
203 Effect.retry({
204 while: (error) => error._tag === "GCP.Datastream.ResourceNotResolved",
205 times: 8,
206 schedule: Schedule.spaced("1 second"),
207 }),
208 );
209
210 export const waitUntilGone = <A, E extends { readonly _tag: string }, R>(
211 get: Effect.Effect<A | undefined, E, R>,
212 name: string,
213 options?: { times?: number; interval?: `${number} seconds` },
214 ) =>
215 get.pipe(
216 Effect.flatMap((value) =>
217 value === undefined
218 ? Effect.void
219 : Effect.fail(new ResourceStillExists({ resourceName: name })),
220 ),
221 Effect.retry({
222 while: (error) => error._tag === "GCP.Datastream.ResourceStillExists",
223 times: options?.times ?? 10,
224 schedule: Schedule.spaced(options?.interval ?? "4 seconds"),
225 }),
226 );
227
228 export const settleOperation = (
229 operation: ds.Operation | undefined,
230 options?: { notFoundOk?: boolean },
231 ) => (operation === undefined ? Effect.void : waitForOperation(operation, options));
232
233 export const collectPages = <Page, A, E, R>(
234 pages: Stream.Stream<Page, E, R>,
235 items: (page: Page) => readonly A[] | undefined,
236 ) =>
237 pages.pipe(
238 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
239 Stream.runCollect,
240 Effect.map((chunk) => Array.from(chunk)),
241 );
242
243 /**
244 * List across every location via the `locations/-` wildcard, falling back to
245 * the default region where the API rejects the wildcard. A missing parent
246 * (`NotFound`) lists as empty; any other error fails.
247 */
248 export const listAtLocation = <A, E extends { readonly _tag: string }, R>(
249 project: string,
250 region: string,
251 list: (parent: string) => Effect.Effect<A[], E, R>,
252 ) =>
253 list(`projects/${project}/locations/-`).pipe(
254 Effect.catchIf(
255 (error) => error._tag === "NotFound" || error._tag === "BadRequest",
256 () =>
257 list(`projects/${project}/locations/${region}`).pipe(
258 Effect.catchIf(
259 (error) => error._tag === "NotFound",
260 () => Effect.succeed<A[]>([]),
261 ),
262 ),
263 ),
264 );