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
| 1 | import * as biglake from "@distilled.cloud/gcp/biglake_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 | /** Locations `list` scans: the pre-`GCP.Region` default plus the stack region. */ |
| 18 | export const listLocations = (region: string): string[] => [...new Set(["us-central1", region])]; |
| 19 | export const MAX_ID_LENGTH = 63; |
| 20 | |
| 21 | export class BiglakeNotResolved extends Data.TaggedError("GCP.BigLake.ResourceNotResolved")<{ |
| 22 | resourceName: string; |
| 23 | }> {} |
| 24 | |
| 25 | export const lastSegment = (value: string) => { |
| 26 | const trimmed = value.replace(/\/+$/, ""); |
| 27 | const parts = trimmed.split("/"); |
| 28 | return parts[parts.length - 1] || trimmed; |
| 29 | }; |
| 30 | |
| 31 | export const locationParent = (project: string, location: string) => |
| 32 | `projects/${project}/locations/${location}`; |
| 33 | |
| 34 | export const normalizeLocation = (location: string | undefined, fallback: string) => { |
| 35 | const raw = lastSegment(location ?? fallback); |
| 36 | const upper = raw.toUpperCase(); |
| 37 | if (upper === "US" || upper === "EU") return upper; |
| 38 | return raw.toLowerCase(); |
| 39 | }; |
| 40 | |
| 41 | export const parseResourceName = (name: string, collection: string) => { |
| 42 | const parts = name.split("/").filter((part) => part.length > 0); |
| 43 | const collectionAt = parts.lastIndexOf(collection); |
| 44 | const locationsAt = parts.lastIndexOf("locations"); |
| 45 | const projectsAt = parts.lastIndexOf("projects"); |
| 46 | const catalogsAt = parts.lastIndexOf("catalogs"); |
| 47 | const databasesAt = parts.lastIndexOf("databases"); |
| 48 | return { |
| 49 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", |
| 50 | location: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "", |
| 51 | catalogId: catalogsAt >= 0 && parts[catalogsAt + 1] ? parts[catalogsAt + 1]! : "", |
| 52 | catalog: catalogsAt >= 0 ? parts.slice(0, catalogsAt + 2).join("/") : "", |
| 53 | databaseId: databasesAt >= 0 && parts[databasesAt + 1] ? parts[databasesAt + 1]! : "", |
| 54 | database: databasesAt >= 0 ? parts.slice(0, databasesAt + 2).join("/") : "", |
| 55 | id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name), |
| 56 | parent: |
| 57 | collectionAt > 0 |
| 58 | ? parts.slice(0, collectionAt).join("/") |
| 59 | : parts.slice(0, Math.max(0, parts.length - 1)).join("/"), |
| 60 | }; |
| 61 | }; |
| 62 | |
| 63 | export const expandCatalog = (value: string, project: string, location: string) => |
| 64 | value.includes("/catalogs/") |
| 65 | ? value.replace(/\/+$/, "") |
| 66 | : `${locationParent(project, location)}/catalogs/${value}`; |
| 67 | |
| 68 | export const expandDatabase = ( |
| 69 | value: string, |
| 70 | project: string, |
| 71 | location: string, |
| 72 | catalog?: string, |
| 73 | ) => { |
| 74 | if (value.includes("/databases/")) return value.replace(/\/+$/, ""); |
| 75 | const parent = |
| 76 | catalog !== undefined && catalog.length > 0 |
| 77 | ? expandCatalog(catalog, project, location) |
| 78 | : locationParent(project, location); |
| 79 | return `${parent}/databases/${value}`; |
| 80 | }; |
| 81 | |
| 82 | export const rfc1035 = (name: string, maxLength = MAX_ID_LENGTH): string => { |
| 83 | let next = name |
| 84 | .toLowerCase() |
| 85 | .replace(/[^a-z0-9_]/g, "_") |
| 86 | .replace(/_+/g, "_") |
| 87 | .replace(/^_+|_+$/g, ""); |
| 88 | if (!/^[a-z]/.test(next)) next = `a${next}`; |
| 89 | next = next.slice(0, maxLength).replace(/_+$/g, ""); |
| 90 | if (next.length === 0) return "catalog"; |
| 91 | if (next.length < 2) next = `${next}x`.slice(0, maxLength); |
| 92 | if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`; |
| 93 | return next.slice(0, maxLength); |
| 94 | }; |
| 95 | |
| 96 | export const toPhysicalId = ( |
| 97 | id: string, |
| 98 | explicit: string | undefined, |
| 99 | existing: string | undefined, |
| 100 | maxLength = MAX_ID_LENGTH, |
| 101 | ) => |
| 102 | Effect.gen(function* () { |
| 103 | if (explicit !== undefined) return explicit; |
| 104 | if (existing !== undefined) return existing; |
| 105 | return rfc1035( |
| 106 | yield* createPhysicalName({ |
| 107 | id, |
| 108 | maxLength, |
| 109 | lowercase: true, |
| 110 | delimiter: "_", |
| 111 | }), |
| 112 | maxLength, |
| 113 | ); |
| 114 | }); |
| 115 | |
| 116 | export const userLabels = ( |
| 117 | labels: Record<string, string | undefined> | null | undefined, |
| 118 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 119 | |
| 120 | export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) => |
| 121 | Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-")); |
| 122 | |
| 123 | export const ownershipMarker = (labels: Record<string, string>) => |
| 124 | `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`; |
| 125 | |
| 126 | export const parseOwnershipMarker = (text: string | undefined) => { |
| 127 | if (!text?.includes("[alchemy ")) return {} as Record<string, string>; |
| 128 | const start = text.indexOf("[alchemy "); |
| 129 | const end = text.indexOf("]", start); |
| 130 | if (end < 0) return {} as Record<string, string>; |
| 131 | const labels: Record<string, string> = {}; |
| 132 | for (const part of text.slice(start + "[alchemy ".length, end).split(/\s+/)) { |
| 133 | const eq = part.indexOf("="); |
| 134 | if (eq > 0) labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 135 | } |
| 136 | return labels; |
| 137 | }; |
| 138 | |
| 139 | export const hasOwnershipMarker = (text: string | undefined) => |
| 140 | Object.keys(parseOwnershipMarker(text)).some((key) => key.startsWith("alchemy-")); |
| 141 | |
| 142 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 143 | (left ?? "") === (right ?? ""); |
| 144 | |
| 145 | export const canonical = (value: unknown): unknown => { |
| 146 | if (value === undefined || value === null) return undefined; |
| 147 | if (typeof value === "boolean" || typeof value === "number") return value; |
| 148 | if (typeof value === "string") { |
| 149 | return value.length === 0 ? undefined : value; |
| 150 | } |
| 151 | if (Array.isArray(value)) { |
| 152 | const items = value.map(canonical).filter((item) => item !== undefined); |
| 153 | return items.length === 0 ? undefined : items; |
| 154 | } |
| 155 | if (typeof value === "object") { |
| 156 | const entries = Object.entries(value as Record<string, unknown>) |
| 157 | .map(([key, item]) => [key, canonical(item)] as const) |
| 158 | .filter(([, item]) => item !== undefined) |
| 159 | .sort(([left], [right]) => left.localeCompare(right)); |
| 160 | if (entries.length === 0) return undefined; |
| 161 | return Object.fromEntries(entries); |
| 162 | } |
| 163 | return undefined; |
| 164 | }; |
| 165 | |
| 166 | export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null); |
| 167 | |
| 168 | export const sameJson = (left: unknown, right: unknown) => fingerprint(left) === fingerprint(right); |
| 169 | |
| 170 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 171 | fields.filter((field): field is string => field !== undefined).join(","); |
| 172 | |
| 173 | export const replaceOnIdentity = (input: { |
| 174 | previousId?: string; |
| 175 | nextId?: string; |
| 176 | previousParent?: string; |
| 177 | nextParent?: string; |
| 178 | previousLocation?: string; |
| 179 | nextLocation?: string; |
| 180 | }) => { |
| 181 | const idChanged = |
| 182 | input.previousId !== undefined && |
| 183 | input.nextId !== undefined && |
| 184 | input.previousId !== input.nextId; |
| 185 | const parentChanged = |
| 186 | (input.previousParent ?? "") !== "" && |
| 187 | (input.nextParent ?? "") !== "" && |
| 188 | (input.previousParent ?? "") !== input.nextParent; |
| 189 | const locationChanged = |
| 190 | (input.previousLocation ?? "") !== "" && |
| 191 | (input.nextLocation ?? "") !== "" && |
| 192 | (input.previousLocation ?? "") !== input.nextLocation; |
| 193 | if (!idChanged && !parentChanged && !locationChanged) return undefined; |
| 194 | const samePhysical = |
| 195 | !parentChanged && |
| 196 | !locationChanged && |
| 197 | input.previousId !== undefined && |
| 198 | input.nextId === input.previousId; |
| 199 | return { |
| 200 | action: "replace" as const, |
| 201 | deleteFirst: samePhysical, |
| 202 | }; |
| 203 | }; |
| 204 | |
| 205 | export const retryTransient = <A, E extends { readonly _tag: string }, R>( |
| 206 | effect: Effect.Effect<A, E, R>, |
| 207 | ) => |
| 208 | effect.pipe( |
| 209 | Effect.retry({ |
| 210 | while: isTransientGcpError, |
| 211 | times: 8, |
| 212 | schedule: Schedule.exponential("250 millis"), |
| 213 | }), |
| 214 | ); |
| 215 | |
| 216 | /** |
| 217 | * BigLake answers a missing catalog, database or table with 403 "... (or it |
| 218 | * may not exist)", typed `BigLakeResourceNotFound`. |
| 219 | */ |
| 220 | const isMissing = (error: { readonly _tag: string }) => |
| 221 | error._tag === "NotFound" || error._tag === "BigLakeResourceNotFound"; |
| 222 | |
| 223 | export const ignoreGone = <A, E extends { readonly _tag: string }, R>( |
| 224 | effect: Effect.Effect<A, E, R>, |
| 225 | ) => |
| 226 | effect.pipe( |
| 227 | Effect.catchIf( |
| 228 | (error): error is Extract<E, { readonly _tag: "NotFound" | "BigLakeResourceNotFound" }> => |
| 229 | isMissing(error), |
| 230 | () => Effect.void, |
| 231 | ), |
| 232 | ); |
| 233 | |
| 234 | export const missingGet = |
| 235 | <A, E extends { readonly _tag: string }, R>( |
| 236 | effect: (input: { name: string }) => Effect.Effect<A, E, R>, |
| 237 | ) => |
| 238 | (name: string) => |
| 239 | name.length === 0 |
| 240 | ? Effect.undefined |
| 241 | : effect({ name }).pipe( |
| 242 | Effect.catchIf( |
| 243 | ( |
| 244 | error, |
| 245 | ): error is Extract<E, { readonly _tag: "NotFound" | "BigLakeResourceNotFound" }> => |
| 246 | isMissing(error), |
| 247 | () => Effect.undefined, |
| 248 | ), |
| 249 | ); |
| 250 | |
| 251 | const emptyList = <A>() => Effect.succeed<A[]>([]); |
| 252 | |
| 253 | export const collectPages = <Page, Item, E extends { readonly _tag: string }, R>( |
| 254 | pages: Stream.Stream<Page, E, R>, |
| 255 | items: (page: Page) => readonly Item[] | null | undefined, |
| 256 | ) => |
| 257 | pages.pipe( |
| 258 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 259 | Stream.runCollect, |
| 260 | Effect.map((chunk) => Array.from(chunk) as Item[]), |
| 261 | Effect.catchIf( |
| 262 | (error): error is Extract<E, { readonly _tag: "NotFound" | "BigLakeResourceNotFound" }> => |
| 263 | isMissing(error), |
| 264 | () => emptyList<Item>(), |
| 265 | ), |
| 266 | ); |
| 267 | |
| 268 | export const listCatalogs = (parent: string) => |
| 269 | parent.length === 0 |
| 270 | ? emptyList<biglake.Catalog>() |
| 271 | : collectPages( |
| 272 | biglake.listProjectsLocationsCatalogs.pages({ |
| 273 | parent, |
| 274 | pageSize: 1000, |
| 275 | }), |
| 276 | (page) => page.catalogs, |
| 277 | ); |
| 278 | |
| 279 | export const listDatabases = (parent: string) => |
| 280 | parent.length === 0 |
| 281 | ? emptyList<biglake.Database>() |
| 282 | : collectPages( |
| 283 | biglake.listProjectsLocationsCatalogsDatabases.pages({ |
| 284 | parent, |
| 285 | pageSize: 1000, |
| 286 | }), |
| 287 | (page) => page.databases, |
| 288 | ); |
| 289 | |
| 290 | export const listTables = (parent: string) => |
| 291 | parent.length === 0 |
| 292 | ? emptyList<biglake.Table>() |
| 293 | : collectPages( |
| 294 | biglake.listProjectsLocationsCatalogsDatabasesTables.pages({ |
| 295 | parent, |
| 296 | pageSize: 1000, |
| 297 | view: "FULL", |
| 298 | }), |
| 299 | (page) => page.tables, |
| 300 | ); |
| 301 | |
| 302 | export const namedOf = <T extends { name?: string }>(items: readonly T[]) => |
| 303 | items.filter((item) => (item.name ?? "").length > 0); |
| 304 | |
| 305 | export const listChildResources = <A, E, R>( |
| 306 | parents: readonly { name?: string }[], |
| 307 | list: (name: string) => Effect.Effect<A[], E, R>, |
| 308 | ) => |
| 309 | Effect.forEach(namedOf(parents), (parent) => list(parent.name!), { |
| 310 | concurrency: 4, |
| 311 | }).pipe(Effect.map((groups) => groups.flat())); |
| 312 | |
| 313 | export class DeleteNotConfirmed extends Data.TaggedError("GCP.BigLake.DeleteNotConfirmed")<{}> {} |
| 314 | |
| 315 | /** Poll until the resource is gone; fails if it is still readable after ~60s. */ |
| 316 | export const waitUntilGone = <A, E, R>(get: Effect.Effect<A | undefined, E, R>) => |
| 317 | get.pipe( |
| 318 | Effect.repeat({ |
| 319 | schedule: Schedule.spaced("2 seconds"), |
| 320 | until: (value) => value === undefined, |
| 321 | times: 30, |
| 322 | }), |
| 323 | Effect.flatMap((value): Effect.Effect<void, DeleteNotConfirmed> => |
| 324 | value === undefined ? Effect.void : Effect.fail(new DeleteNotConfirmed()), |
| 325 | ), |
| 326 | ); |
| 327 | |
| 328 | export const mergeParameters = ( |
| 329 | parameters: Record<string, string> | undefined, |
| 330 | ownership: Record<string, string>, |
| 331 | ): Record<string, string> => ({ |
| 332 | ...tagRecord(parameters), |
| 333 | ...ownership, |
| 334 | }); |
| 335 | |
| 336 | export { createInternalLabels, hasAlchemyLabels, toLabels }; |