The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as analyticshub from "@distilled.cloud/gcp/analyticshub_v1"; |
| 2 | import { Retry as GcpRetry } from "@distilled.cloud/gcp/Retry"; |
| 3 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 4 | import * as Data from "effect/Data"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import { isTransientGcpError } from "../Errors.ts"; |
| 10 | import { alchemyLabelKeys, createInternalLabels, hasAlchemyLabels } from "../Labels.ts"; |
| 11 | |
| 12 | export const MAX_ID_LENGTH = 40; |
| 13 | export const MAX_DISPLAY_NAME_LENGTH = 63; |
| 14 | export const LIST_LOCATIONS = ["us-central1", "US", "EU"] as const; |
| 15 | |
| 16 | export class AnalyticshubNotResolved extends Data.TaggedError( |
| 17 | "GCP.AnalyticsHub.ResourceNotResolved", |
| 18 | )<{ |
| 19 | resourceName: string; |
| 20 | }> {} |
| 21 | |
| 22 | export const lastSegment = (value: string) => { |
| 23 | const trimmed = value.replace(/\/+$/, ""); |
| 24 | const parts = trimmed.split("/"); |
| 25 | return parts[parts.length - 1] || trimmed; |
| 26 | }; |
| 27 | |
| 28 | export const locationParent = (project: string, location: string) => |
| 29 | `projects/${project}/locations/${location}`; |
| 30 | |
| 31 | export const normalizeLocation = (location: string | undefined, defaultLocation: string) => |
| 32 | lastSegment(location ?? defaultLocation); |
| 33 | |
| 34 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 35 | (left ?? "") === (right ?? ""); |
| 36 | |
| 37 | export const sameBool = (left: boolean | undefined, right: boolean | undefined) => |
| 38 | (left === true) === (right === true); |
| 39 | |
| 40 | const canonical = (value: unknown): unknown => { |
| 41 | if (value === undefined || value === null) return undefined; |
| 42 | if (typeof value === "boolean" || typeof value === "number") return value; |
| 43 | if (typeof value === "string") return value.length === 0 ? undefined : value; |
| 44 | if (Array.isArray(value)) { |
| 45 | const items = value.map(canonical).filter((item) => item !== undefined); |
| 46 | return items.length === 0 ? undefined : items; |
| 47 | } |
| 48 | if (typeof value === "object") { |
| 49 | const entries = Object.entries(value as Record<string, unknown>) |
| 50 | .map(([key, item]) => [key, canonical(item)] as const) |
| 51 | .filter(([, item]) => item !== undefined) |
| 52 | .sort(([left], [right]) => left.localeCompare(right)); |
| 53 | if (entries.length === 0) return undefined; |
| 54 | return Object.fromEntries(entries); |
| 55 | } |
| 56 | return undefined; |
| 57 | }; |
| 58 | |
| 59 | export const sameJson = (left: unknown, right: unknown) => |
| 60 | JSON.stringify(canonical(left) ?? null) === JSON.stringify(canonical(right) ?? null); |
| 61 | |
| 62 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 63 | fields.filter((field): field is string => field !== undefined).join(","); |
| 64 | |
| 65 | export const parseResourceName = (name: string, collection: 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: locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : "", |
| 73 | id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name), |
| 74 | parent: |
| 75 | collectionAt > 0 |
| 76 | ? parts.slice(0, collectionAt).join("/") |
| 77 | : parts.slice(0, Math.max(0, parts.length - 1)).join("/"), |
| 78 | }; |
| 79 | }; |
| 80 | |
| 81 | export const expandParent = ( |
| 82 | value: string, |
| 83 | project: string, |
| 84 | location: string, |
| 85 | collection: string, |
| 86 | ) => { |
| 87 | if (value.includes("/")) return value.replace(/\/+$/, ""); |
| 88 | return `projects/${project}/locations/${location}/${collection}/${value}`; |
| 89 | }; |
| 90 | |
| 91 | export const toPhysicalId = ( |
| 92 | id: string, |
| 93 | explicit: string | undefined, |
| 94 | existing: string | undefined, |
| 95 | maxLength = MAX_ID_LENGTH, |
| 96 | ) => |
| 97 | Effect.gen(function* () { |
| 98 | if (explicit !== undefined) return explicit; |
| 99 | if (existing !== undefined) return existing; |
| 100 | const generated = yield* createPhysicalName({ |
| 101 | id, |
| 102 | maxLength, |
| 103 | lowercase: true, |
| 104 | delimiter: "_", |
| 105 | }); |
| 106 | let next = generated.replace(/[^a-z0-9_]/g, "_").replace(/_+/g, "_"); |
| 107 | if (!/^[a-z]/.test(next)) next = `a${next}`.slice(0, maxLength); |
| 108 | next = next.slice(0, maxLength).replace(/_+$/g, ""); |
| 109 | return next.length > 0 ? next : "exchange"; |
| 110 | }); |
| 111 | |
| 112 | export const displayNameOf = (value: string | undefined, fallback: string) => { |
| 113 | const next = (value ?? fallback).trim(); |
| 114 | return next.slice(0, MAX_DISPLAY_NAME_LENGTH); |
| 115 | }; |
| 116 | |
| 117 | export const encodeDescription = ( |
| 118 | labels: Record<string, string>, |
| 119 | description: string | undefined, |
| 120 | ): string => { |
| 121 | const marker = `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`; |
| 122 | return description && description.length > 0 ? `${marker}\n${description}` : marker; |
| 123 | }; |
| 124 | |
| 125 | export const parseDescription = ( |
| 126 | description: string | undefined, |
| 127 | ): { |
| 128 | labels: Record<string, string>; |
| 129 | description: string | undefined; |
| 130 | } => { |
| 131 | if (!description?.startsWith("[alchemy ")) { |
| 132 | return { labels: {}, description }; |
| 133 | } |
| 134 | const end = description.indexOf("]"); |
| 135 | if (end < 0) return { labels: {}, description }; |
| 136 | const labels: Record<string, string> = {}; |
| 137 | for (const part of description.slice("[alchemy ".length, end).split(/\s+/)) { |
| 138 | const eq = part.indexOf("="); |
| 139 | if (eq > 0) { |
| 140 | labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 141 | } |
| 142 | } |
| 143 | const rest = description.slice(end + 1).replace(/^\n/, ""); |
| 144 | return { labels, description: rest.length > 0 ? rest : undefined }; |
| 145 | }; |
| 146 | |
| 147 | export const hasOwnershipMarker = (description: string | undefined) => |
| 148 | Object.keys(parseDescription(description).labels).some((key) => key.startsWith("alchemy-")); |
| 149 | |
| 150 | export const ownedById = (id: string, description: string | undefined) => |
| 151 | Effect.gen(function* () { |
| 152 | const { labels } = parseDescription(description); |
| 153 | return yield* hasAlchemyLabels(id, labels); |
| 154 | }); |
| 155 | |
| 156 | export const sharingKind = (config: analyticshub.SharingEnvironmentConfig | undefined) => |
| 157 | config?.dcrExchangeConfig !== undefined ? "dcr" : "default"; |
| 158 | |
| 159 | export const replaceOnIdentity = (input: { |
| 160 | previousId?: string; |
| 161 | nextId?: string; |
| 162 | previousParent?: string; |
| 163 | nextParent?: string; |
| 164 | previousLocation?: string; |
| 165 | nextLocation?: string; |
| 166 | extra?: boolean; |
| 167 | }) => { |
| 168 | const idChanged = |
| 169 | input.previousId !== undefined && |
| 170 | input.nextId !== undefined && |
| 171 | input.previousId !== input.nextId; |
| 172 | const parentChanged = |
| 173 | (input.previousParent ?? "") !== "" && |
| 174 | (input.nextParent ?? "") !== "" && |
| 175 | (input.previousParent ?? "") !== (input.nextParent ?? ""); |
| 176 | const locationChanged = |
| 177 | (input.previousLocation ?? "") !== "" && |
| 178 | (input.nextLocation ?? "") !== "" && |
| 179 | (input.previousLocation ?? "").toLowerCase() !== (input.nextLocation ?? "").toLowerCase(); |
| 180 | if (!idChanged && !parentChanged && !locationChanged && !input.extra) { |
| 181 | return undefined; |
| 182 | } |
| 183 | return { action: "replace" as const, deleteFirst: false }; |
| 184 | }; |
| 185 | |
| 186 | export const retryTransient = <A, E extends { readonly _tag: string }, R>( |
| 187 | effect: Effect.Effect<A, E, R>, |
| 188 | ) => |
| 189 | effect.pipe( |
| 190 | Effect.retry({ |
| 191 | while: (error) => isTransientGcpError(error) || error._tag === "NotFound", |
| 192 | times: 8, |
| 193 | schedule: Schedule.exponential("250 millis"), |
| 194 | }), |
| 195 | ); |
| 196 | |
| 197 | export const ignoreGone = <A, E extends { readonly _tag: string }, R>( |
| 198 | effect: Effect.Effect<A, E, R>, |
| 199 | ) => |
| 200 | effect.pipe( |
| 201 | Effect.catchIf( |
| 202 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 203 | () => Effect.void, |
| 204 | ), |
| 205 | ); |
| 206 | |
| 207 | const noRetryLayer = Layer.succeed(GcpRetry, { while: () => false }); |
| 208 | |
| 209 | const isUnavailable = (error: { readonly _tag: string }) => |
| 210 | error._tag === "Conflict" || |
| 211 | error._tag === "InternalServerError" || |
| 212 | error._tag === "ServiceUnavailable" || |
| 213 | error._tag === "BadGateway" || |
| 214 | error._tag === "GatewayTimeout"; |
| 215 | |
| 216 | export const deleteRetry = <A, E extends { readonly _tag: string }, R>( |
| 217 | effect: Effect.Effect<A, E, R>, |
| 218 | ) => |
| 219 | effect.pipe( |
| 220 | Effect.provide(noRetryLayer), |
| 221 | Effect.retry({ |
| 222 | while: isUnavailable, |
| 223 | times: 12, |
| 224 | schedule: Schedule.spaced("3 seconds"), |
| 225 | }), |
| 226 | Effect.catchIf( |
| 227 | (error): error is E & { readonly _tag: "NotFound" } => error._tag === "NotFound", |
| 228 | () => Effect.void, |
| 229 | ), |
| 230 | ); |
| 231 | |
| 232 | export const missingGet = (name: string) => |
| 233 | name.length === 0 |
| 234 | ? Effect.undefined |
| 235 | : analyticshub |
| 236 | .getProjectsLocationsDataExchanges({ name }) |
| 237 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 238 | |
| 239 | const emptyList = <A>() => Effect.succeed<A[]>([]); |
| 240 | |
| 241 | export const collectPages = <Page, Item, E, R>( |
| 242 | pages: Stream.Stream<Page, E, R>, |
| 243 | items: (page: Page) => readonly Item[] | null | undefined, |
| 244 | ) => |
| 245 | pages.pipe( |
| 246 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 247 | Stream.runCollect, |
| 248 | Effect.map((chunk): Item[] => Array.from(chunk)), |
| 249 | ); |
| 250 | |
| 251 | export const listDataExchanges = (parent: string) => |
| 252 | parent.length === 0 |
| 253 | ? emptyList<analyticshub.DataExchange>() |
| 254 | : collectPages( |
| 255 | analyticshub.listProjectsLocationsDataExchanges.pages({ |
| 256 | parent, |
| 257 | pageSize: 1000, |
| 258 | }), |
| 259 | (page) => page.dataExchanges, |
| 260 | ).pipe( |
| 261 | Effect.retry({ |
| 262 | while: isUnavailable, |
| 263 | times: 8, |
| 264 | schedule: Schedule.spaced("2 seconds"), |
| 265 | }), |
| 266 | Effect.catchTag("NotFound", () => emptyList<analyticshub.DataExchange>()), |
| 267 | ); |
| 268 | |
| 269 | export const listListings = (parent: string) => |
| 270 | parent.length === 0 |
| 271 | ? emptyList<analyticshub.Listing>() |
| 272 | : collectPages( |
| 273 | analyticshub.listProjectsLocationsDataExchangesListings.pages({ |
| 274 | parent, |
| 275 | pageSize: 1000, |
| 276 | }), |
| 277 | (page) => page.listings, |
| 278 | ).pipe( |
| 279 | Effect.retry({ |
| 280 | while: isUnavailable, |
| 281 | times: 8, |
| 282 | schedule: Schedule.spaced("2 seconds"), |
| 283 | }), |
| 284 | Effect.catchTag("NotFound", () => emptyList<analyticshub.Listing>()), |
| 285 | ); |
| 286 | |
| 287 | export const listQueryTemplates = (parent: string) => |
| 288 | parent.length === 0 |
| 289 | ? emptyList<analyticshub.QueryTemplate>() |
| 290 | : collectPages( |
| 291 | analyticshub.listProjectsLocationsDataExchangesQueryTemplates.pages({ |
| 292 | parent, |
| 293 | pageSize: 1000, |
| 294 | }), |
| 295 | (page) => page.queryTemplates, |
| 296 | ).pipe( |
| 297 | Effect.retry({ |
| 298 | while: isUnavailable, |
| 299 | times: 8, |
| 300 | schedule: Schedule.spaced("2 seconds"), |
| 301 | }), |
| 302 | Effect.catchTag("NotFound", () => emptyList<analyticshub.QueryTemplate>()), |
| 303 | ); |
| 304 | |
| 305 | export const listExchangesInProject = (project: string, region: string) => |
| 306 | Effect.forEach( |
| 307 | [...new Set<string>([region, ...LIST_LOCATIONS])], |
| 308 | (location) => listDataExchanges(locationParent(project, location)), |
| 309 | { concurrency: 2 }, |
| 310 | ).pipe(Effect.map((groups) => groups.flat())); |
| 311 | |
| 312 | export const listChildResources = <A, E, R>( |
| 313 | parents: readonly { name?: string }[], |
| 314 | list: (name: string) => Effect.Effect<A[], E, R>, |
| 315 | ) => |
| 316 | Effect.forEach( |
| 317 | parents.filter((parent) => (parent.name ?? "").length > 0), |
| 318 | (parent) => list(parent.name!), |
| 319 | { concurrency: 4 }, |
| 320 | ).pipe(Effect.map((groups) => groups.flat())); |
| 321 | |
| 322 | export const namedOf = <T extends { name?: string }>(items: readonly T[]) => |
| 323 | items.filter((item) => (item.name ?? "").length > 0); |
| 324 | |
| 325 | export const ownershipLabels = (id: string) => createInternalLabels(id); |