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 bigtable from "@distilled.cloud/gcp/bigtableadmin_v2"; |
| 2 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Stream from "effect/Stream"; |
| 5 | import { waitForOperation as waitForGcpOperation } from "../Operation.ts"; |
| 6 | |
| 7 | export const DEFAULT_ZONE = "us-central1-b"; |
| 8 | export const DEFAULT_STORAGE = "HDD"; |
| 9 | export const DEFAULT_SERVE_NODES = 1; |
| 10 | export const DEFAULT_INSTANCE_TYPE = "PRODUCTION"; |
| 11 | export const DEFAULT_EDITION = "ENTERPRISE"; |
| 12 | export const DEFAULT_CLUSTER_ID = "cluster"; |
| 13 | export const MAX_INSTANCE_ID_LENGTH = 33; |
| 14 | export const MIN_INSTANCE_ID_LENGTH = 6; |
| 15 | export const MAX_CLUSTER_ID_LENGTH = 30; |
| 16 | export const MIN_CLUSTER_ID_LENGTH = 6; |
| 17 | export const MAX_APP_PROFILE_ID_LENGTH = 50; |
| 18 | export const MAX_TABLE_ID_LENGTH = 50; |
| 19 | export const MAX_BACKUP_ID_LENGTH = 50; |
| 20 | export const MAX_LOGICAL_VIEW_ID_LENGTH = 50; |
| 21 | export const MAX_MATERIALIZED_VIEW_ID_LENGTH = 50; |
| 22 | export const MAX_AUTHORIZED_VIEW_ID_LENGTH = 50; |
| 23 | export const MAX_SCHEMA_BUNDLE_ID_LENGTH = 50; |
| 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 rfc1035 = (name: string, maxLength: number, minLength = 1): string => { |
| 32 | let next = name |
| 33 | .toLowerCase() |
| 34 | .replace(/[^a-z0-9-]/g, "-") |
| 35 | .replace(/-+/g, "-") |
| 36 | .replace(/^-+|-+$/g, ""); |
| 37 | if (!/^[a-z]/.test(next)) next = `b${next}`; |
| 38 | next = next.slice(0, maxLength).replace(/-+$/g, ""); |
| 39 | if (next.length === 0) next = "bigtable"; |
| 40 | if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, maxLength - 1)}0`; |
| 41 | if (next.length < minLength) { |
| 42 | next = `${next}${"x".repeat(minLength)}`.slice(0, minLength); |
| 43 | } |
| 44 | return next.slice(0, maxLength); |
| 45 | }; |
| 46 | |
| 47 | export const toPhysicalId = ( |
| 48 | id: string, |
| 49 | explicit: string | undefined, |
| 50 | existing: string | undefined, |
| 51 | maxLength: number, |
| 52 | minLength = 1, |
| 53 | ) => |
| 54 | Effect.gen(function* () { |
| 55 | if (explicit !== undefined) return explicit; |
| 56 | if (existing !== undefined) return existing; |
| 57 | return rfc1035( |
| 58 | yield* createPhysicalName({ |
| 59 | id, |
| 60 | maxLength, |
| 61 | lowercase: true, |
| 62 | }), |
| 63 | maxLength, |
| 64 | minLength, |
| 65 | ); |
| 66 | }); |
| 67 | |
| 68 | export const instanceName = (project: string, instanceId: string) => |
| 69 | `projects/${project}/instances/${instanceId}`; |
| 70 | |
| 71 | export const clusterName = (project: string, instanceId: string, clusterId: string) => |
| 72 | `${instanceName(project, instanceId)}/clusters/${clusterId}`; |
| 73 | |
| 74 | export const appProfileName = (project: string, instanceId: string, appProfileId: string) => |
| 75 | `${instanceName(project, instanceId)}/appProfiles/${appProfileId}`; |
| 76 | |
| 77 | export const tableName = (project: string, instanceId: string, tableId: string) => |
| 78 | `${instanceName(project, instanceId)}/tables/${tableId}`; |
| 79 | |
| 80 | export const backupName = ( |
| 81 | project: string, |
| 82 | instanceId: string, |
| 83 | clusterId: string, |
| 84 | backupId: string, |
| 85 | ) => `${clusterName(project, instanceId, clusterId)}/backups/${backupId}`; |
| 86 | |
| 87 | export const logicalViewName = (project: string, instanceId: string, logicalViewId: string) => |
| 88 | `${instanceName(project, instanceId)}/logicalViews/${logicalViewId}`; |
| 89 | |
| 90 | export const materializedViewName = ( |
| 91 | project: string, |
| 92 | instanceId: string, |
| 93 | materializedViewId: string, |
| 94 | ) => `${instanceName(project, instanceId)}/materializedViews/${materializedViewId}`; |
| 95 | |
| 96 | export const authorizedViewName = ( |
| 97 | project: string, |
| 98 | instanceId: string, |
| 99 | tableId: string, |
| 100 | authorizedViewId: string, |
| 101 | ) => `${tableName(project, instanceId, tableId)}/authorizedViews/${authorizedViewId}`; |
| 102 | |
| 103 | export const schemaBundleName = ( |
| 104 | project: string, |
| 105 | instanceId: string, |
| 106 | tableId: string, |
| 107 | schemaBundleId: string, |
| 108 | ) => `${tableName(project, instanceId, tableId)}/schemaBundles/${schemaBundleId}`; |
| 109 | |
| 110 | export const parseResourceName = (name: string) => { |
| 111 | const parts = name.split("/").filter((part) => part.length > 0); |
| 112 | const indexOf = (segment: string) => parts.lastIndexOf(segment); |
| 113 | const after = (segment: string) => { |
| 114 | const at = indexOf(segment); |
| 115 | return at >= 0 && parts[at + 1] ? parts[at + 1]! : ""; |
| 116 | }; |
| 117 | const project = after("projects"); |
| 118 | const instanceId = after("instances"); |
| 119 | const clusterId = after("clusters"); |
| 120 | const tableId = after("tables"); |
| 121 | return { |
| 122 | project, |
| 123 | instanceId, |
| 124 | clusterId, |
| 125 | appProfileId: after("appProfiles"), |
| 126 | tableId, |
| 127 | backupId: after("backups"), |
| 128 | logicalViewId: after("logicalViews"), |
| 129 | materializedViewId: after("materializedViews"), |
| 130 | authorizedViewId: after("authorizedViews"), |
| 131 | schemaBundleId: after("schemaBundles"), |
| 132 | instance: project && instanceId ? instanceName(project, instanceId) : "", |
| 133 | cluster: project && instanceId && clusterId ? clusterName(project, instanceId, clusterId) : "", |
| 134 | table: project && instanceId && tableId ? tableName(project, instanceId, tableId) : "", |
| 135 | }; |
| 136 | }; |
| 137 | |
| 138 | export const instanceNameOf = (project: string, instance: string) => { |
| 139 | if (instance.includes("/instances/")) { |
| 140 | const parsed = parseResourceName(instance); |
| 141 | return instanceName(parsed.project || project, parsed.instanceId); |
| 142 | } |
| 143 | return instanceName(project, lastSegment(instance)); |
| 144 | }; |
| 145 | |
| 146 | export const instanceIdOf = (value: string) => |
| 147 | value.includes("/instances/") ? parseResourceName(value).instanceId : lastSegment(value); |
| 148 | |
| 149 | export const clusterIdOf = (value: string) => |
| 150 | value.includes("/clusters/") ? parseResourceName(value).clusterId : lastSegment(value); |
| 151 | |
| 152 | export const tableIdOf = (value: string) => |
| 153 | value.includes("/tables/") ? parseResourceName(value).tableId : lastSegment(value); |
| 154 | |
| 155 | export const clusterNameOf = (project: string, instance: string, cluster: string) => { |
| 156 | if (cluster.includes("/clusters/")) { |
| 157 | const parsed = parseResourceName(cluster); |
| 158 | return clusterName( |
| 159 | parsed.project || project, |
| 160 | parsed.instanceId || instanceIdOf(instance), |
| 161 | parsed.clusterId, |
| 162 | ); |
| 163 | } |
| 164 | return clusterName(project, instanceIdOf(instance), lastSegment(cluster)); |
| 165 | }; |
| 166 | |
| 167 | export const tableNameOf = (project: string, instance: string, table: string) => { |
| 168 | if (table.includes("/tables/")) { |
| 169 | const parsed = parseResourceName(table); |
| 170 | return tableName( |
| 171 | parsed.project || project, |
| 172 | parsed.instanceId || instanceIdOf(instance), |
| 173 | parsed.tableId, |
| 174 | ); |
| 175 | } |
| 176 | return `${instanceNameOf(project, instance)}/tables/${lastSegment(table)}`; |
| 177 | }; |
| 178 | |
| 179 | export const clusterLocation = (project: string, location: string) => { |
| 180 | if (location.includes("/locations/")) { |
| 181 | const zone = lastSegment(location); |
| 182 | const parsed = parseResourceName(location); |
| 183 | return `projects/${parsed.project || project}/locations/${zone}`; |
| 184 | } |
| 185 | return `projects/${project}/locations/${lastSegment(location).toLowerCase()}`; |
| 186 | }; |
| 187 | |
| 188 | export const zoneOf = (location: string | undefined) => |
| 189 | lastSegment(location ?? DEFAULT_ZONE).toLowerCase(); |
| 190 | |
| 191 | export const hasAlchemyPrefix = (labels: Record<string, string | undefined> | null | undefined) => |
| 192 | Object.keys(labels ?? {}).some((key) => key.startsWith("alchemy-")); |
| 193 | |
| 194 | /** |
| 195 | * Wait for a Bigtable admin operation. Cluster resizes and materialized |
| 196 | * views are the slowest (several minutes). With `alreadyExistsOk`, an |
| 197 | * ALREADY_EXISTS (code 6) result counts as success (create race). |
| 198 | */ |
| 199 | export const waitForOperation = ( |
| 200 | operation: bigtable.Operation, |
| 201 | options?: { alreadyExistsOk?: boolean }, |
| 202 | ) => |
| 203 | waitForGcpOperation(operation, (name) => bigtable.getOperations({ name }), { |
| 204 | budget: "20 minutes", |
| 205 | }).pipe( |
| 206 | Effect.catchIf( |
| 207 | (error) => |
| 208 | options?.alreadyExistsOk === true && |
| 209 | error._tag === "GCP.OperationFailed" && |
| 210 | error.code === 6, |
| 211 | () => Effect.succeed(operation), |
| 212 | ), |
| 213 | ); |
| 214 | |
| 215 | export const getInstanceByName = (name: string) => |
| 216 | bigtable.getProjectsInstances({ name }).pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 217 | |
| 218 | export const collectPages = <Page, A, E, R>( |
| 219 | pages: Stream.Stream<Page, E, R>, |
| 220 | items: (page: Page) => readonly A[] | undefined, |
| 221 | ) => |
| 222 | pages.pipe( |
| 223 | Stream.flatMap((page) => Stream.fromIterable(items(page) ?? [])), |
| 224 | Stream.runCollect, |
| 225 | Effect.map((chunk) => Array.from(chunk)), |
| 226 | ); |
| 227 | |
| 228 | export const listAlchemyInstances = (project: string) => |
| 229 | collectPages( |
| 230 | bigtable.listProjectsInstances.pages({ parent: `projects/${project}` }), |
| 231 | (page) => page.instances, |
| 232 | ).pipe( |
| 233 | Effect.map((instances) => instances.filter((instance) => hasAlchemyPrefix(instance.labels))), |
| 234 | ); |
| 235 | |
| 236 | export const parentOwned = (instanceNameValue: string) => |
| 237 | getInstanceByName(instanceNameValue).pipe( |
| 238 | Effect.map((instance) => (instance === undefined ? true : hasAlchemyPrefix(instance.labels))), |
| 239 | ); |
| 240 | |
| 241 | export const listAlchemyTables = (project: string) => |
| 242 | Effect.gen(function* () { |
| 243 | const instances = (yield* listAlchemyInstances(project)).filter( |
| 244 | (instance): instance is bigtable.Instance & { name: string } => |
| 245 | typeof instance.name === "string" && instance.name.length > 0, |
| 246 | ); |
| 247 | const pages = yield* Effect.forEach( |
| 248 | instances, |
| 249 | (instance) => |
| 250 | collectPages( |
| 251 | bigtable.listProjectsInstancesTables.pages({ |
| 252 | parent: instance.name, |
| 253 | pageSize: 1000, |
| 254 | }), |
| 255 | (page) => page.tables, |
| 256 | ).pipe( |
| 257 | // The instance was deleted between the two list calls. |
| 258 | Effect.catchTag("NotFound", () => Effect.succeed([] as bigtable.Table[])), |
| 259 | ), |
| 260 | { concurrency: 4 }, |
| 261 | ); |
| 262 | return pages.flat(); |
| 263 | }); |