@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 352 lines 12.5 KB
Raw
1 import * as healthcare from "@distilled.cloud/gcp/healthcare_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 { isTransientGcpError } from "../Errors.ts";
9 import {
10 alchemyLabelKeys,
11 createInternalLabels,
12 hasAlchemyLabels,
13 stripInternalLabels,
14 toLabels,
15 } from "../Labels.ts";
16
17 export const MAX_ID_LENGTH = 256;
18
19 export const lastSegment = (value: string) => {
20 const trimmed = value.replace(/\/+$/, "");
21 const parts = trimmed.split("/");
22 return parts[parts.length - 1] || trimmed;
23 };
24
25 export const parentOf = (name: string) => {
26 const parts = name.split("/").filter((part) => part.length > 0);
27 return parts.slice(0, -2).join("/");
28 };
29
30 export const locationParent = (project: string, location: string) =>
31 `projects/${project}/locations/${location}`;
32
33 export const normalizeLocation = (location: string | undefined, defaultLocation: string) =>
34 lastSegment(location ?? defaultLocation).toLowerCase();
35
36 export const sameText = (left: string | undefined, right: string | undefined) =>
37 (left ?? "") === (right ?? "");
38
39 export const sameJson = (left: unknown, right: unknown) =>
40 JSON.stringify(left ?? null) === JSON.stringify(right ?? null);
41
42 export const updateMaskOf = (...fields: Array<string | undefined>) =>
43 fields.filter((field): field is string => field !== undefined).join(",");
44
45 export const parseResourceName = (name: string, collection: string, defaultLocation: string) => {
46 const parts = name.split("/").filter((part) => part.length > 0);
47 const collectionAt = parts.lastIndexOf(collection);
48 const locationsAt = parts.lastIndexOf("locations");
49 const projectsAt = parts.lastIndexOf("projects");
50 return {
51 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
52 location:
53 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : defaultLocation,
54 id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name),
55 parent:
56 collectionAt > 0
57 ? parts.slice(0, collectionAt).join("/")
58 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
59 };
60 };
61
62 export const expandParent = (
63 value: string,
64 project: string,
65 location: string,
66 collection: string,
67 ) => {
68 if (value.includes("/")) return value.replace(/\/+$/, "");
69 return `projects/${project}/locations/${location}/${collection}/${value}`;
70 };
71
72 export const userLabels = (
73 labels: Record<string, string | undefined> | null | undefined,
74 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
75
76 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
77 Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-"));
78
79 export const withOwnershipMetadata = (
80 metadata: Record<string, string> | undefined,
81 ownership: Record<string, string>,
82 ) => ({ ...toLabels(metadata), ...ownership });
83
84 export const encodeDataId = (labels: Record<string, string>, dataId: string): string =>
85 `alc-${labels[alchemyLabelKeys.id] ?? "x"}-${dataId}`;
86
87 export const decodeDataId = (dataId: string | undefined): string | undefined => {
88 if (dataId === undefined) return undefined;
89 if (!dataId.startsWith("alc-")) return dataId;
90 const rest = dataId.slice("alc-".length);
91 const dash = rest.indexOf("-");
92 return dash >= 0 ? rest.slice(dash + 1) : dataId;
93 };
94
95 export const hasOwnedDataId = (dataId: string | undefined) => (dataId ?? "").startsWith("alc-");
96
97 export const toPhysicalId = (
98 id: string,
99 explicit: string | undefined,
100 existing: string | undefined,
101 maxLength = MAX_ID_LENGTH,
102 ) =>
103 Effect.gen(function* () {
104 if (explicit !== undefined) return explicit;
105 if (existing !== undefined) return existing;
106 const generated = yield* createPhysicalName({
107 id,
108 maxLength,
109 lowercase: true,
110 });
111 const next = /^[a-z]/.test(generated) ? generated : `h${generated}`.slice(0, maxLength);
112 return next.length >= 1 ? next : "h";
113 });
114
115 export const toPhysicalSnake = (
116 id: string,
117 explicit: string | undefined,
118 existing: string | undefined,
119 maxLength = MAX_ID_LENGTH,
120 ) =>
121 Effect.gen(function* () {
122 if (explicit !== undefined) return explicit;
123 if (existing !== undefined) return existing;
124 const generated = yield* createPhysicalName({
125 id,
126 maxLength,
127 lowercase: true,
128 delimiter: "_",
129 });
130 let next = generated.replace(/[^a-z0-9_]/g, "_").replace(/_+/g, "_");
131 if (!/^[a-z]/.test(next)) next = `a${next}`.slice(0, maxLength);
132 next = next.slice(0, maxLength).replace(/_+$/g, "");
133 if (next.length === 0) next = "attr";
134 if (!next.startsWith("attr_")) {
135 next = `attr_${next}`.slice(0, maxLength);
136 }
137 return next;
138 });
139
140 export const replaceOnIdentity = (input: {
141 previousId?: string;
142 nextId?: string;
143 previousParent?: string;
144 nextParent?: string;
145 extra?: boolean;
146 }) => {
147 if (input.extra === true) {
148 return { action: "replace" as const, deleteFirst: false };
149 }
150 if (
151 input.previousId !== undefined &&
152 input.nextId !== undefined &&
153 input.previousId !== input.nextId
154 ) {
155 return { action: "replace" as const, deleteFirst: false };
156 }
157 if (
158 input.previousParent !== undefined &&
159 input.nextParent !== undefined &&
160 input.previousParent !== input.nextParent
161 ) {
162 return { action: "replace" as const, deleteFirst: false };
163 }
164 return undefined;
165 };
166
167 export const retryTransient = <A, E extends { readonly _tag: string }, R>(
168 effect: Effect.Effect<A, E, R>,
169 ) =>
170 effect.pipe(
171 Effect.retry({
172 while: (error) => isTransientGcpError(error) || error._tag === "NotFound",
173 times: 8,
174 schedule: Schedule.exponential("250 millis"),
175 }),
176 );
177
178 export class DeleteNotConfirmed extends Data.TaggedError("GCP.Healthcare.DeleteNotConfirmed")<{}> {}
179
180 /** Poll until the resource is gone; fails if it is still readable after ~60s. */
181 export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>, _name: string) =>
182 get.pipe(
183 Effect.repeat({
184 schedule: Schedule.spaced("2 seconds"),
185 until: (value) => value === undefined,
186 times: 30,
187 }),
188 Effect.flatMap((value): Effect.Effect<void, DeleteNotConfirmed> =>
189 value === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed()),
190 ),
191 );
192
193 const emptyList = <A>() => Effect.succeed<A[]>([]);
194
195 export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>(
196 pages: Stream.Stream<Page, E, R>,
197 items: (page: Page) => readonly Item[] | null | undefined,
198 ) =>
199 pages.pipe(
200 Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])),
201 Stream.runCollect,
202 Effect.map((chunk) => Array.from(chunk) as Item[]),
203 Effect.catchIf(
204 (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound",
205 () => emptyList<Item>(),
206 ),
207 );
208
209 export const encodeHl7 = (value: string) =>
210 Effect.sync(() => Buffer.from(value, "utf8").toString("base64"));
211
212 export const decodeHl7 = (value: string | undefined) =>
213 Effect.sync(() => {
214 if (value === undefined || value.length === 0) return "";
215 return Buffer.from(value, "base64").toString("utf8");
216 });
217
218 export const listDatasets = (parent: string) =>
219 parent.length === 0
220 ? emptyList<healthcare.Dataset>()
221 : healthcare.listProjectsLocationsDatasets.pages({ parent, pageSize: 100 }).pipe(
222 Stream.flatMap((page) => Stream.fromIterable(page.datasets ?? [])),
223 Stream.runCollect,
224 Effect.map((chunk) => Array.from(chunk)),
225 Effect.catchTag("NotFound", () => emptyList<healthcare.Dataset>()),
226 );
227
228 export const listHl7V2Stores = (parent: string) =>
229 parent.length === 0
230 ? emptyList<healthcare.Hl7V2Store>()
231 : healthcare.listProjectsLocationsDatasetsHl7V2Stores.pages({ parent, pageSize: 100 }).pipe(
232 Stream.flatMap((page) => Stream.fromIterable(page.hl7V2Stores ?? [])),
233 Stream.runCollect,
234 Effect.map((chunk) => Array.from(chunk)),
235 Effect.catchTag("NotFound", () => emptyList<healthcare.Hl7V2Store>()),
236 );
237
238 export const listMessages = (parent: string) =>
239 parent.length === 0
240 ? emptyList<healthcare.Message>()
241 : healthcare.listProjectsLocationsDatasetsHl7V2StoresMessages
242 .pages({ parent, pageSize: 100, view: "FULL" })
243 .pipe(
244 Stream.flatMap((page) => Stream.fromIterable(page.hl7V2Messages ?? [])),
245 Stream.runCollect,
246 Effect.map((chunk) => Array.from(chunk)),
247 Effect.catchTag("NotFound", () => emptyList<healthcare.Message>()),
248 );
249
250 // `us-central1` was the fixed default before `GCP.Region`; list paths keep
251 // it alongside the stack region so datasets created under it are found.
252 export const listRegionDatasets = (project: string, region: string) =>
253 Effect.forEach([...new Set([region, "us-central1"])], (location) =>
254 listDatasets(locationParent(project, location)),
255 ).pipe(Effect.map((groups) => groups.flat()));
256
257 export const forEachDataset = <A, E, R>(
258 project: string,
259 region: string,
260 list: (parent: string) => Effect.Effect<A[], E, R>,
261 ) =>
262 Effect.gen(function* () {
263 const datasets = yield* listRegionDatasets(project, region);
264 const named = datasets.filter((dataset) => (dataset.name ?? "").length > 0);
265 const groups = yield* Effect.forEach(named, (dataset) => list(dataset.name!), {
266 concurrency: 4,
267 });
268 return groups.flat();
269 });
270
271 export const listAlchemyConsentStores = (project: string, region: string) =>
272 forEachDataset(project, region, (parent) =>
273 collectPages(
274 healthcare.listProjectsLocationsDatasetsConsentStores.pages({
275 parent,
276 pageSize: 1000,
277 }),
278 (page) => page.consentStores,
279 ),
280 ).pipe(Effect.map((stores) => stores.filter((store) => hasAlchemyLabelMap(store.labels))));
281
282 const markerOf = (labels: Record<string, string>, stack: string, stage: string, id: string) =>
283 `[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`;
284
285 const fitMarker = (labels: Record<string, string>, maxLength: number) => {
286 let stack = labels[alchemyLabelKeys.stack] ?? "x";
287 let stage = labels[alchemyLabelKeys.stage] ?? "x";
288 let id = labels[alchemyLabelKeys.id] ?? "x";
289 let marker = markerOf(labels, stack, stage, id);
290 while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) {
291 if (stack.length >= stage.length && stack.length >= id.length) {
292 stack = stack.slice(0, -1);
293 } else if (stage.length >= id.length) {
294 stage = stage.slice(0, -1);
295 } else {
296 id = id.slice(0, -1);
297 }
298 marker = markerOf(labels, stack, stage, id);
299 }
300 return marker.slice(0, maxLength);
301 };
302
303 export const encodeOwnership = (
304 labels: Record<string, string>,
305 text: string | undefined,
306 ): string => {
307 const marker = fitMarker(labels, 8000);
308 const trimmed = text?.trim();
309 return trimmed && trimmed.length > 0 ? `${marker}\n${trimmed}` : marker;
310 };
311
312 export const parseOwnership = (
313 text: string | undefined,
314 ): {
315 labels: Record<string, string>;
316 text: string | undefined;
317 } => {
318 if (!text?.startsWith("[alchemy ")) {
319 return { labels: {}, text };
320 }
321 const end = text.indexOf("]");
322 if (end < 0) return { labels: {}, text };
323 const labels: Record<string, string> = {};
324 for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
325 const eq = part.indexOf("=");
326 if (eq > 0) {
327 labels[part.slice(0, eq)] = part.slice(eq + 1);
328 }
329 }
330 const rest = text.slice(end + 1).replace(/^[\s\n]+/, "");
331 return { labels, text: rest.length > 0 ? rest : undefined };
332 };
333
334 export const hasOwnershipMarker = (text: string | undefined) =>
335 Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-"));
336
337 const prefixMatch = (expected: string, observed: string) =>
338 expected === observed || expected.startsWith(observed) || observed.startsWith(expected);
339
340 export const ownedByAlchemy = (id: string, text: string | undefined) =>
341 Effect.gen(function* () {
342 const expected = yield* createInternalLabels(id);
343 const { labels } = parseOwnership(text);
344 if (!hasOwnershipMarker(text)) return false;
345 const exact = yield* hasAlchemyLabels(id, labels);
346 if (exact) return true;
347 return (
348 prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") &&
349 prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") &&
350 prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "")
351 );
352 });