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 type * as datacatalog from "@distilled.cloud/gcp/datacatalog_v1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Data from "effect/Data"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Schedule from "effect/Schedule"; |
| 6 | import * as Stream from "effect/Stream"; |
| 7 | import { isTransientGcpError } from "../Errors.ts"; |
| 8 | import { alchemyLabelKeys, hasAlchemyLabels } from "../Labels.ts"; |
| 9 | |
| 10 | export const MAX_TEMPLATE_ID_LENGTH = 64; |
| 11 | export const MAX_DISPLAY_NAME_LENGTH = 200; |
| 12 | export const MAX_DESCRIPTION_LENGTH = 2000; |
| 13 | export const OWNERSHIP_FIELD_ID = "alchemy_ownership"; |
| 14 | |
| 15 | export type FieldType = datacatalog.GoogleCloudDatacatalogV1FieldType; |
| 16 | export type TagTemplateField = datacatalog.GoogleCloudDatacatalogV1TagTemplateField; |
| 17 | export type TagTemplateFieldMap = datacatalog.GoogleCloudDatacatalogV1TagTemplateFieldMap; |
| 18 | export type TaxonomyActivatedPolicyType = |
| 19 | | datacatalog.GoogleCloudDatacatalogV1TaxonomyActivatedPolicyTypesItemEnum |
| 20 | | (string & {}); |
| 21 | |
| 22 | export class DatacatalogNotResolved extends Data.TaggedError( |
| 23 | "GCP.DataCatalog.ResourceNotResolved", |
| 24 | )<{ |
| 25 | resourceName: string; |
| 26 | }> {} |
| 27 | |
| 28 | export const lastSegment = (value: string) => { |
| 29 | const trimmed = value.replace(/\/+$/, ""); |
| 30 | const parts = trimmed.split("/"); |
| 31 | return parts[parts.length - 1] || trimmed; |
| 32 | }; |
| 33 | |
| 34 | export const normalizeLocation = (location: string | undefined, fallback: string) => |
| 35 | lastSegment(location ?? fallback).toLowerCase(); |
| 36 | |
| 37 | export const locationParent = (project: string, location: string) => |
| 38 | `projects/${project}/locations/${lastSegment(location).toLowerCase()}`; |
| 39 | |
| 40 | export const parseName = (name: string, collection: string, fallbackLocation: string) => { |
| 41 | const parts = name.split("/").filter((part) => part.length > 0); |
| 42 | const collectionAt = parts.lastIndexOf(collection); |
| 43 | const locationsAt = parts.lastIndexOf("locations"); |
| 44 | const projectsAt = parts.lastIndexOf("projects"); |
| 45 | return { |
| 46 | project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "", |
| 47 | location: |
| 48 | locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation, |
| 49 | id: collectionAt >= 0 && parts[collectionAt + 1] ? parts[collectionAt + 1]! : lastSegment(name), |
| 50 | parent: |
| 51 | collectionAt > 0 |
| 52 | ? parts.slice(0, collectionAt).join("/") |
| 53 | : parts.slice(0, Math.max(0, parts.length - 1)).join("/"), |
| 54 | }; |
| 55 | }; |
| 56 | |
| 57 | export const expandParent = ( |
| 58 | value: string, |
| 59 | project: string, |
| 60 | location: string, |
| 61 | collection: string, |
| 62 | ) => { |
| 63 | if (value.includes("/")) return value.replace(/\/+$/, ""); |
| 64 | return `projects/${project}/locations/${location}/${collection}/${value}`; |
| 65 | }; |
| 66 | |
| 67 | const sanitizeTemplateId = (name: string) => { |
| 68 | let next = name |
| 69 | .toLowerCase() |
| 70 | .replace(/[^a-z0-9_]/g, "_") |
| 71 | .replace(/_+/g, "_"); |
| 72 | if (!/^[a-z_]/.test(next)) next = `t${next}`; |
| 73 | next = next.slice(0, MAX_TEMPLATE_ID_LENGTH).replace(/_+$/g, ""); |
| 74 | return next.length > 0 ? next : "template"; |
| 75 | }; |
| 76 | |
| 77 | export const toTagTemplateId = ( |
| 78 | id: string, |
| 79 | explicit: string | undefined, |
| 80 | existing: string | undefined, |
| 81 | ) => |
| 82 | Effect.gen(function* () { |
| 83 | if (explicit !== undefined) return sanitizeTemplateId(explicit); |
| 84 | if (existing !== undefined) return existing; |
| 85 | return sanitizeTemplateId( |
| 86 | yield* createPhysicalName({ |
| 87 | id, |
| 88 | maxLength: MAX_TEMPLATE_ID_LENGTH, |
| 89 | lowercase: true, |
| 90 | delimiter: "_", |
| 91 | }), |
| 92 | ); |
| 93 | }); |
| 94 | |
| 95 | export const toDisplayName = ( |
| 96 | id: string, |
| 97 | explicit: string | undefined, |
| 98 | existing: string | undefined, |
| 99 | ) => |
| 100 | Effect.gen(function* () { |
| 101 | if (explicit !== undefined) { |
| 102 | return explicit.trim().slice(0, MAX_DISPLAY_NAME_LENGTH); |
| 103 | } |
| 104 | if (existing !== undefined) return existing; |
| 105 | const generated = yield* createPhysicalName({ |
| 106 | id, |
| 107 | maxLength: MAX_DISPLAY_NAME_LENGTH, |
| 108 | lowercase: true, |
| 109 | }); |
| 110 | const next = generated |
| 111 | .replace(/[^a-zA-Z0-9_ -]/g, "-") |
| 112 | .replace(/\s+/g, " ") |
| 113 | .trim() |
| 114 | .slice(0, MAX_DISPLAY_NAME_LENGTH); |
| 115 | return next.length > 0 ? next : "alchemy"; |
| 116 | }); |
| 117 | |
| 118 | const markerOf = (labels: Record<string, string>) => |
| 119 | `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`; |
| 120 | |
| 121 | export const encodeOwnership = ( |
| 122 | labels: Record<string, string>, |
| 123 | text: string | undefined, |
| 124 | maxLength = MAX_DESCRIPTION_LENGTH, |
| 125 | ): string => { |
| 126 | const marker = markerOf(labels); |
| 127 | const trimmed = text?.trim(); |
| 128 | if (!trimmed) return marker.slice(0, maxLength); |
| 129 | return `${marker}\n${trimmed}`.slice(0, maxLength); |
| 130 | }; |
| 131 | |
| 132 | export const parseOwnership = ( |
| 133 | text: string | undefined, |
| 134 | ): { |
| 135 | labels: Record<string, string>; |
| 136 | text: string | undefined; |
| 137 | } => { |
| 138 | if (!text?.startsWith("[alchemy ")) { |
| 139 | return { labels: {}, text }; |
| 140 | } |
| 141 | const end = text.indexOf("]"); |
| 142 | if (end < 0) return { labels: {}, text }; |
| 143 | const labels: Record<string, string> = {}; |
| 144 | for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) { |
| 145 | const eq = part.indexOf("="); |
| 146 | if (eq > 0) { |
| 147 | labels[part.slice(0, eq)] = part.slice(eq + 1); |
| 148 | } |
| 149 | } |
| 150 | const rest = text.slice(end + 1).replace(/^[\s\n]+/, ""); |
| 151 | return { labels, text: rest.length > 0 ? rest : undefined }; |
| 152 | }; |
| 153 | |
| 154 | export const hasOwnershipMarker = (text: string | undefined) => |
| 155 | Object.keys(parseOwnership(text).labels).some((key) => key.startsWith("alchemy-")); |
| 156 | |
| 157 | export const ownedByAlchemy = (id: string, text: string | undefined) => |
| 158 | Effect.gen(function* () { |
| 159 | const { labels } = parseOwnership(text); |
| 160 | return yield* hasAlchemyLabels(id, labels); |
| 161 | }); |
| 162 | |
| 163 | export const sameText = (left: string | undefined, right: string | undefined) => |
| 164 | (left ?? "") === (right ?? ""); |
| 165 | |
| 166 | export const sameBool = (left: boolean | undefined, right: boolean | undefined) => |
| 167 | (left === true) === (right === true); |
| 168 | |
| 169 | export const sameStringList = ( |
| 170 | left: readonly string[] | undefined, |
| 171 | right: readonly string[] | undefined, |
| 172 | ) => JSON.stringify([...(left ?? [])].sort()) === JSON.stringify([...(right ?? [])].sort()); |
| 173 | |
| 174 | const canonical = (value: unknown): unknown => { |
| 175 | if (value === undefined || value === null) return undefined; |
| 176 | if (typeof value === "boolean" || typeof value === "number") return value; |
| 177 | if (typeof value === "string") return value.length === 0 ? undefined : value; |
| 178 | if (Array.isArray(value)) { |
| 179 | const items = value.map(canonical).filter((item) => item !== undefined); |
| 180 | return items.length === 0 ? undefined : items; |
| 181 | } |
| 182 | if (typeof value === "object") { |
| 183 | const entries = Object.entries(value as Record<string, unknown>) |
| 184 | .map(([key, item]) => [key, canonical(item)] as const) |
| 185 | .filter(([, item]) => item !== undefined) |
| 186 | .sort(([left], [right]) => left.localeCompare(right)); |
| 187 | if (entries.length === 0) return undefined; |
| 188 | return Object.fromEntries(entries); |
| 189 | } |
| 190 | return undefined; |
| 191 | }; |
| 192 | |
| 193 | export const fingerprint = (value: unknown): string => JSON.stringify(canonical(value) ?? null); |
| 194 | |
| 195 | export const sameJson = (left: unknown, right: unknown) => fingerprint(left) === fingerprint(right); |
| 196 | |
| 197 | export const updateMaskOf = (...fields: Array<string | undefined>) => |
| 198 | fields.filter((field): field is string => field !== undefined).join(","); |
| 199 | |
| 200 | export const replaceOnIdentity = (input: { |
| 201 | previousId?: string; |
| 202 | nextId?: string; |
| 203 | previousParent?: string; |
| 204 | nextParent?: string; |
| 205 | previousLocation?: string; |
| 206 | nextLocation?: string; |
| 207 | extra?: boolean; |
| 208 | }) => { |
| 209 | const idChanged = |
| 210 | input.previousId !== undefined && |
| 211 | input.nextId !== undefined && |
| 212 | input.previousId !== input.nextId; |
| 213 | const parentChanged = |
| 214 | (input.previousParent ?? "") !== "" && |
| 215 | (input.nextParent ?? "") !== "" && |
| 216 | (input.previousParent ?? "") !== (input.nextParent ?? ""); |
| 217 | const locationChanged = |
| 218 | (input.previousLocation ?? "") !== "" && |
| 219 | (input.nextLocation ?? "") !== "" && |
| 220 | (input.previousLocation ?? "").toLowerCase() !== (input.nextLocation ?? "").toLowerCase(); |
| 221 | if (!idChanged && !parentChanged && !locationChanged && !input.extra) { |
| 222 | return undefined; |
| 223 | } |
| 224 | const samePhysical = |
| 225 | !parentChanged && |
| 226 | !locationChanged && |
| 227 | input.previousId !== undefined && |
| 228 | input.nextId === input.previousId; |
| 229 | return { action: "replace" as const, deleteFirst: samePhysical }; |
| 230 | }; |
| 231 | |
| 232 | export const retryTransient = <A, E extends { readonly _tag: string }, R>( |
| 233 | effect: Effect.Effect<A, E, R>, |
| 234 | ) => |
| 235 | effect.pipe( |
| 236 | Effect.retry({ |
| 237 | while: isTransientGcpError, |
| 238 | times: 8, |
| 239 | schedule: Schedule.exponential("250 millis"), |
| 240 | }), |
| 241 | ); |
| 242 | |
| 243 | export const ignoreGone = <A, E extends { readonly _tag: string }, R>( |
| 244 | effect: Effect.Effect<A, E, R>, |
| 245 | ) => |
| 246 | effect.pipe( |
| 247 | Effect.catchIf( |
| 248 | (error) => error._tag === "NotFound", |
| 249 | () => Effect.void, |
| 250 | ), |
| 251 | ); |
| 252 | |
| 253 | export const missingGet = |
| 254 | <A, E extends { readonly _tag: string }, R>( |
| 255 | effect: (input: { name: string }) => Effect.Effect<A, E, R>, |
| 256 | ) => |
| 257 | (name: string) => |
| 258 | name.length === 0 |
| 259 | ? Effect.undefined |
| 260 | : effect({ name }).pipe( |
| 261 | Effect.catchIf( |
| 262 | (error) => error._tag === "NotFound", |
| 263 | () => Effect.undefined, |
| 264 | ), |
| 265 | ); |
| 266 | |
| 267 | export const collectPages = <Page, A, E, R>( |
| 268 | pages: Stream.Stream<Page, E, R>, |
| 269 | items: (page: Page) => readonly A[] | undefined, |
| 270 | ): Effect.Effect<A[], E, R> => |
| 271 | pages.pipe( |
| 272 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 273 | Stream.runCollect, |
| 274 | Effect.map((chunk): A[] => Array.from(chunk)), |
| 275 | ); |
| 276 | |
| 277 | export const emptyOnMissing = <A, E extends { readonly _tag: string }, R>( |
| 278 | effect: Effect.Effect<A[], E, R>, |
| 279 | ) => |
| 280 | effect.pipe( |
| 281 | Effect.catchIf( |
| 282 | (error) => error._tag === "NotFound", |
| 283 | () => Effect.succeed([] as A[]), |
| 284 | ), |
| 285 | ); |
| 286 | |
| 287 | /** |
| 288 | * Data Catalog rejects the `locations/-` wildcard (404), so owned rows are |
| 289 | * listed in the default region only. |
| 290 | */ |
| 291 | export const listAtLocation = <A, E, R>( |
| 292 | project: string, |
| 293 | region: string, |
| 294 | list: (parent: string) => Effect.Effect<A[], E, R>, |
| 295 | ): Effect.Effect<A[], E, R> => list(locationParent(project, region)); |
| 296 | |
| 297 | export const findOwned = <A>( |
| 298 | items: readonly A[], |
| 299 | id: string, |
| 300 | descriptionOf: (item: A) => string | undefined, |
| 301 | ) => |
| 302 | Effect.gen(function* () { |
| 303 | for (const item of items) { |
| 304 | if (yield* ownedByAlchemy(id, descriptionOf(item))) { |
| 305 | return item; |
| 306 | } |
| 307 | } |
| 308 | return undefined; |
| 309 | }); |
| 310 | |
| 311 | export const ownershipField = (labels: Record<string, string>): TagTemplateField => ({ |
| 312 | displayName: "Alchemy ownership", |
| 313 | type: { primitiveType: "STRING" }, |
| 314 | isRequired: false, |
| 315 | description: encodeOwnership(labels, undefined), |
| 316 | order: 0, |
| 317 | }); |
| 318 | |
| 319 | export const userFields = (fields: TagTemplateFieldMap | undefined): TagTemplateFieldMap => { |
| 320 | const next: TagTemplateFieldMap = {}; |
| 321 | for (const [key, field] of Object.entries(fields ?? {})) { |
| 322 | if (key === OWNERSHIP_FIELD_ID || field === undefined) continue; |
| 323 | next[key] = { |
| 324 | displayName: field.displayName, |
| 325 | type: field.type, |
| 326 | isRequired: field.isRequired, |
| 327 | description: field.description, |
| 328 | order: field.order, |
| 329 | }; |
| 330 | } |
| 331 | return next; |
| 332 | }; |
| 333 | |
| 334 | export const desiredFields = ( |
| 335 | labels: Record<string, string>, |
| 336 | fields: TagTemplateFieldMap | undefined, |
| 337 | ): TagTemplateFieldMap => ({ |
| 338 | ...userFields(fields), |
| 339 | [OWNERSHIP_FIELD_ID]: ownershipField(labels), |
| 340 | }); |
| 341 | |
| 342 | export const fieldBody = (field: TagTemplateField): TagTemplateField => ({ |
| 343 | displayName: field.displayName, |
| 344 | type: field.type, |
| 345 | isRequired: field.isRequired === true ? true : undefined, |
| 346 | description: field.description, |
| 347 | order: field.order, |
| 348 | }); |
| 349 | |
| 350 | export const primitiveOf = (type: FieldType | undefined) => type?.primitiveType ?? ""; |
| 351 | |
| 352 | export const enumValuesOf = (type: FieldType | undefined) => |
| 353 | (type?.enumType?.allowedValues ?? []) |
| 354 | .map((value) => value.displayName ?? "") |
| 355 | .filter((value) => value.length > 0); |
| 356 | |
| 357 | export const fieldNeedsReplace = ( |
| 358 | observed: TagTemplateField | undefined, |
| 359 | desired: TagTemplateField, |
| 360 | ) => { |
| 361 | if (observed === undefined) return false; |
| 362 | const observedPrimitive = primitiveOf(observed.type); |
| 363 | const desiredPrimitive = primitiveOf(desired.type); |
| 364 | if (observedPrimitive !== desiredPrimitive) return true; |
| 365 | const observedHasEnum = (observed.type?.enumType?.allowedValues ?? []).length > 0; |
| 366 | const desiredHasEnum = (desired.type?.enumType?.allowedValues ?? []).length > 0; |
| 367 | if (observedHasEnum !== desiredHasEnum) return true; |
| 368 | if (desiredHasEnum) { |
| 369 | const observedValues = new Set(enumValuesOf(observed.type)); |
| 370 | const desiredValues = enumValuesOf(desired.type); |
| 371 | if (desiredValues.some((value) => !observedValues.has(value))) { |
| 372 | return false; |
| 373 | } |
| 374 | if (observedValues.size > desiredValues.length) return true; |
| 375 | } |
| 376 | return false; |
| 377 | }; |