The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as storage from "@distilled.cloud/gcp/storage_v1"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import { GcpEnvironment } from "../Environment.ts"; |
| 6 | import { ALCHEMY_LABEL_PREFIX } from "../Labels.ts"; |
| 7 | |
| 8 | export const normalizeRole = (role: string) => role.trim().toUpperCase(); |
| 9 | |
| 10 | export const normalizeEntity = (entity: string) => entity.trim(); |
| 11 | |
| 12 | /** Default project-team ACL entries GCS stamps on fine-grained buckets. */ |
| 13 | export const isProjectAclEntity = (entity: string | undefined) => |
| 14 | (entity ?? "").toLowerCase().startsWith("project-"); |
| 15 | |
| 16 | /** |
| 17 | * ACL entities Alchemy may have created. Skips project-team defaults and |
| 18 | * numeric `user-{id}` object-owner entries so `list` / nuke do not try to |
| 19 | * delete GCS-managed ACLs. |
| 20 | */ |
| 21 | export const isUserManagedAclEntity = (entity: string | undefined) => { |
| 22 | const value = (entity ?? "").toLowerCase(); |
| 23 | if (value.length === 0 || isProjectAclEntity(value)) return false; |
| 24 | if (value === "allusers" || value === "allauthenticatedusers") return true; |
| 25 | if (value.startsWith("domain-") || value.startsWith("group-")) return true; |
| 26 | return value.startsWith("user-") && value.includes("@"); |
| 27 | }; |
| 28 | |
| 29 | export const withTrailingSlash = (name: string) => (name.endsWith("/") ? name : `${name}/`); |
| 30 | |
| 31 | export const toFolderName = (id: string, name: string | undefined, existing?: string) => |
| 32 | Effect.gen(function* () { |
| 33 | const raw = |
| 34 | name ?? |
| 35 | existing ?? |
| 36 | (yield* createPhysicalName({ |
| 37 | id, |
| 38 | maxLength: 200, |
| 39 | lowercase: true, |
| 40 | })); |
| 41 | return withTrailingSlash(raw.replace(/^\/+/, "")); |
| 42 | }); |
| 43 | |
| 44 | export const listAlchemyBuckets = () => |
| 45 | Effect.gen(function* () { |
| 46 | const env = yield* GcpEnvironment.current; |
| 47 | return yield* storage.listBuckets |
| 48 | .items({ |
| 49 | project: env.project, |
| 50 | projection: "full", |
| 51 | maxResults: 1000, |
| 52 | }) |
| 53 | .pipe( |
| 54 | Stream.filter((bucket) => |
| 55 | Object.keys(bucket.labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX)), |
| 56 | ), |
| 57 | Stream.runCollect, |
| 58 | Effect.map((chunk) => Array.from(chunk)), |
| 59 | ); |
| 60 | }); |