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 aiplatform from "@distilled.cloud/gcp/aiplatform_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | import { GcpEnvironment } from "../Environment.ts"; |
| 12 | import { |
| 13 | createInternalLabels, |
| 14 | diffLabels, |
| 15 | hasAlchemyLabels, |
| 16 | stripInternalLabels, |
| 17 | toLabels, |
| 18 | } from "../Labels.ts"; |
| 19 | import type { Providers } from "../Providers.ts"; |
| 20 | import { listLocations } from "./names.ts"; |
| 21 | import { resourceNameFromOperation, waitForOperation } from "./operations.ts"; |
| 22 | import { lastSegment, locationOf, locationParent } from "./ownership.ts"; |
| 23 | import type { EncryptionSpec } from "./shared.ts"; |
| 24 | |
| 25 | export type TensorboardProps = { |
| 26 | /** |
| 27 | * Region (`us-central1`, …). Immutable — changing it replaces the |
| 28 | * Tensorboard. |
| 29 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 30 | */ |
| 31 | location?: string; |
| 32 | /** |
| 33 | * User-facing display name. If omitted, a unique name is generated from |
| 34 | * the stack, stage, and logical id. |
| 35 | */ |
| 36 | displayName?: string; |
| 37 | /** |
| 38 | * Human-readable description. |
| 39 | */ |
| 40 | description?: string; |
| 41 | /** |
| 42 | * User labels. Alchemy ownership labels are merged in automatically. |
| 43 | */ |
| 44 | labels?: Record<string, string>; |
| 45 | /** |
| 46 | * Customer-managed encryption key. Immutable. |
| 47 | */ |
| 48 | encryptionSpec?: EncryptionSpec; |
| 49 | /** |
| 50 | * Mark this Tensorboard as the default for the project and region. |
| 51 | * @default false |
| 52 | */ |
| 53 | isDefault?: boolean; |
| 54 | }; |
| 55 | |
| 56 | export type Tensorboard = Resource< |
| 57 | "GCP.AIPlatform.Tensorboard", |
| 58 | TensorboardProps, |
| 59 | { |
| 60 | /** Full resource name `projects/{project}/locations/{location}/tensorboards/{tensorboard}`. */ |
| 61 | name: string; |
| 62 | /** Tensorboard id (last path segment). */ |
| 63 | tensorboardId: string; |
| 64 | /** Region id. */ |
| 65 | location: string; |
| 66 | /** Project id. */ |
| 67 | project: string; |
| 68 | /** User-facing display name. */ |
| 69 | displayName: string | undefined; |
| 70 | /** Description. */ |
| 71 | description: string | undefined; |
| 72 | /** User labels (Alchemy ownership labels stripped). */ |
| 73 | labels: Record<string, string>; |
| 74 | /** CMEK resource name, if any. */ |
| 75 | encryptionSpec: EncryptionSpec | undefined; |
| 76 | /** Whether this is the default Tensorboard in the region. */ |
| 77 | isDefault: boolean; |
| 78 | /** Cloud Storage prefix used for blob data. */ |
| 79 | blobStoragePathPrefix: string | undefined; |
| 80 | /** Number of runs stored in this Tensorboard. */ |
| 81 | runCount: number | undefined; |
| 82 | /** RFC3339 creation timestamp. */ |
| 83 | createTime: string | undefined; |
| 84 | /** RFC3339 last-update timestamp. */ |
| 85 | updateTime: string | undefined; |
| 86 | }, |
| 87 | never, |
| 88 | Providers |
| 89 | >; |
| 90 | |
| 91 | /** |
| 92 | * A Vertex AI Tensorboard, the physical store for training metrics. |
| 93 | * |
| 94 | * Location and encryption key are immutable. Display name, description, |
| 95 | * labels, and the default flag update in place. |
| 96 | * |
| 97 | * ### Creating a Tensorboard |
| 98 | * **Example:** Generated display name |
| 99 | * ```typescript |
| 100 | * const board = yield* GCP.AIPlatform.Tensorboard("Metrics", {}); |
| 101 | * ``` |
| 102 | * |
| 103 | * **Example:** Named Tensorboard with labels |
| 104 | * ```typescript |
| 105 | * const board = yield* GCP.AIPlatform.Tensorboard("Metrics", { |
| 106 | * displayName: "training-metrics", |
| 107 | * description: "experiment store", |
| 108 | * labels: { env: "prod" }, |
| 109 | * }); |
| 110 | * ``` |
| 111 | * |
| 112 | * @resource |
| 113 | * @category AIPlatform |
| 114 | */ |
| 115 | export const Tensorboard = Resource<Tensorboard>("GCP.AIPlatform.Tensorboard"); |
| 116 | |
| 117 | export class TensorboardNotResolved extends Data.TaggedError( |
| 118 | "GCP.AIPlatform.TensorboardNotResolved", |
| 119 | )<{ |
| 120 | resourceName: string; |
| 121 | }> {} |
| 122 | |
| 123 | const userLabels = ( |
| 124 | labels: Record<string, string | undefined> | null | undefined, |
| 125 | ): Record<string, string> => stripInternalLabels(tagRecord(labels)); |
| 126 | |
| 127 | const toAttrs = (board: aiplatform.GoogleCloudAiplatformV1Tensorboard, project: string) => { |
| 128 | const name = board.name ?? ""; |
| 129 | return { |
| 130 | name, |
| 131 | tensorboardId: lastSegment(name), |
| 132 | location: locationOf(name), |
| 133 | project, |
| 134 | displayName: board.displayName, |
| 135 | description: board.description, |
| 136 | labels: userLabels(board.labels), |
| 137 | encryptionSpec: board.encryptionSpec?.kmsKeyName |
| 138 | ? { kmsKeyName: board.encryptionSpec.kmsKeyName } |
| 139 | : undefined, |
| 140 | isDefault: board.isDefault === true, |
| 141 | blobStoragePathPrefix: board.blobStoragePathPrefix, |
| 142 | runCount: board.runCount, |
| 143 | createTime: board.createTime, |
| 144 | updateTime: board.updateTime, |
| 145 | }; |
| 146 | }; |
| 147 | |
| 148 | const getByName = (name: string) => |
| 149 | name.length === 0 |
| 150 | ? Effect.undefined |
| 151 | : aiplatform |
| 152 | .getProjectsLocationsTensorboards({ name }) |
| 153 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 154 | |
| 155 | const waitUntilExists = (name: string) => |
| 156 | getByName(name).pipe( |
| 157 | Effect.flatMap((board) => |
| 158 | board |
| 159 | ? Effect.succeed(board) |
| 160 | : Effect.fail(new TensorboardNotResolved({ resourceName: name })), |
| 161 | ), |
| 162 | Effect.retry({ |
| 163 | while: (error) => error._tag === "GCP.AIPlatform.TensorboardNotResolved", |
| 164 | times: 8, |
| 165 | schedule: Schedule.spaced("2 seconds"), |
| 166 | }), |
| 167 | ); |
| 168 | |
| 169 | const listAt = (parent: string, project: string) => |
| 170 | aiplatform.listProjectsLocationsTensorboards.pages({ parent, pageSize: 100 }).pipe( |
| 171 | Stream.flatMap((page) => Stream.fromIterable(page.tensorboards ?? [])), |
| 172 | Stream.filter((board) => |
| 173 | Object.keys(board.labels ?? {}).some((key) => key.startsWith("alchemy-")), |
| 174 | ), |
| 175 | Stream.map((board) => toAttrs(board, project)), |
| 176 | Stream.runCollect, |
| 177 | Effect.map((chunk) => Array.from(chunk)), |
| 178 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 179 | ); |
| 180 | |
| 181 | const findByDisplayName = (parent: string, displayName: string, project: string) => |
| 182 | aiplatform.listProjectsLocationsTensorboards.pages({ parent, pageSize: 100 }).pipe( |
| 183 | Stream.flatMap((page) => Stream.fromIterable(page.tensorboards ?? [])), |
| 184 | Stream.filter((board) => board.displayName === displayName), |
| 185 | Stream.runHead, |
| 186 | Effect.map((option) => (option._tag === "Some" ? toAttrs(option.value, project) : undefined)), |
| 187 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 188 | ); |
| 189 | |
| 190 | export const TensorboardProvider = () => |
| 191 | Provider.succeed(Tensorboard, { |
| 192 | stables: ["name", "tensorboardId", "location", "project", "createTime"], |
| 193 | |
| 194 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 195 | if (!isResolved(news)) return undefined; |
| 196 | const previousLocation = olds?.location ?? output?.location; |
| 197 | const nextLocation = news.location ?? previousLocation; |
| 198 | if (previousLocation !== undefined && previousLocation !== nextLocation) { |
| 199 | return { action: "replace" as const, deleteFirst: false }; |
| 200 | } |
| 201 | const previousKey = olds?.encryptionSpec?.kmsKeyName ?? output?.encryptionSpec?.kmsKeyName; |
| 202 | const nextKey = news.encryptionSpec?.kmsKeyName; |
| 203 | if (previousKey !== undefined && nextKey !== undefined && previousKey !== nextKey) { |
| 204 | return { action: "replace" as const, deleteFirst: false }; |
| 205 | } |
| 206 | return undefined; |
| 207 | }), |
| 208 | |
| 209 | read: Effect.fn(function* ({ id, olds, output }) { |
| 210 | const env = yield* GcpEnvironment.current; |
| 211 | const existing = yield* getByName(output?.name ?? ""); |
| 212 | if (existing === undefined) { |
| 213 | if (output?.name !== undefined) return undefined; |
| 214 | const location = olds?.location ?? output?.location ?? env.region; |
| 215 | const parent = locationParent(env.project, location); |
| 216 | const match = yield* findByDisplayName(parent, olds?.displayName ?? "", env.project); |
| 217 | if (match === undefined) return undefined; |
| 218 | const fetched = yield* getByName(match.name); |
| 219 | if (fetched === undefined) return undefined; |
| 220 | const attrs = toAttrs(fetched, env.project); |
| 221 | return (yield* hasAlchemyLabels(id, tagRecord(fetched.labels))) ? attrs : Unowned(attrs); |
| 222 | } |
| 223 | const attrs = toAttrs(existing, env.project); |
| 224 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 225 | }), |
| 226 | |
| 227 | list: () => |
| 228 | Effect.gen(function* () { |
| 229 | const env = yield* GcpEnvironment.current; |
| 230 | return (yield* Effect.forEach(listLocations(env.region), (location) => |
| 231 | listAt(locationParent(env.project, location), env.project), |
| 232 | )).flat(); |
| 233 | }), |
| 234 | |
| 235 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 236 | const env = yield* GcpEnvironment.current; |
| 237 | const location = news.location ?? output?.location ?? env.region; |
| 238 | const parent = locationParent(env.project, location); |
| 239 | const desiredLabels = { |
| 240 | ...toLabels(news.labels), |
| 241 | ...(yield* createInternalLabels(id)), |
| 242 | }; |
| 243 | const displayName = news.displayName ?? output?.displayName ?? lastSegment(id); |
| 244 | const desiredDefault = news.isDefault === true; |
| 245 | const encryptionSpec = news.encryptionSpec?.kmsKeyName |
| 246 | ? { kmsKeyName: news.encryptionSpec.kmsKeyName } |
| 247 | : undefined; |
| 248 | |
| 249 | let current = yield* getByName(output?.name ?? ""); |
| 250 | |
| 251 | if (current === undefined) { |
| 252 | const created = yield* aiplatform |
| 253 | .createProjectsLocationsTensorboards({ |
| 254 | parent, |
| 255 | body: { |
| 256 | displayName, |
| 257 | description: news.description, |
| 258 | labels: desiredLabels, |
| 259 | encryptionSpec, |
| 260 | isDefault: desiredDefault ? true : undefined, |
| 261 | }, |
| 262 | }) |
| 263 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 264 | if (created !== undefined) { |
| 265 | const done = yield* waitForOperation(created, { |
| 266 | alreadyExistsOk: true, |
| 267 | }); |
| 268 | const createdName = resourceNameFromOperation(done) ?? output?.name ?? ""; |
| 269 | if (createdName.length > 0) { |
| 270 | current = yield* waitUntilExists(createdName); |
| 271 | } |
| 272 | } |
| 273 | if (current === undefined) { |
| 274 | const match = yield* findByDisplayName(parent, displayName, env.project); |
| 275 | if (match !== undefined) { |
| 276 | current = yield* getByName(match.name); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | if (current === undefined) { |
| 282 | return yield* new TensorboardNotResolved({ |
| 283 | resourceName: output?.name ?? `${parent}/tensorboards/-`, |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | const name = current.name ?? ""; |
| 288 | const observedLabels = tagRecord(current.labels); |
| 289 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 290 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 291 | const displayChanged = (current.displayName ?? "") !== displayName; |
| 292 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 293 | const defaultChanged = (current.isDefault === true) !== desiredDefault; |
| 294 | |
| 295 | if (labelsChanged || displayChanged || descriptionChanged || defaultChanged) { |
| 296 | const updateMask = [ |
| 297 | labelsChanged ? "labels" : undefined, |
| 298 | displayChanged ? "displayName" : undefined, |
| 299 | descriptionChanged ? "description" : undefined, |
| 300 | defaultChanged ? "isDefault" : undefined, |
| 301 | ].filter((field): field is string => field !== undefined); |
| 302 | const patched = yield* aiplatform.patchProjectsLocationsTensorboards({ |
| 303 | name, |
| 304 | updateMask: updateMask.join(","), |
| 305 | body: { |
| 306 | name, |
| 307 | displayName, |
| 308 | description: news.description, |
| 309 | labels: desiredLabels, |
| 310 | isDefault: desiredDefault, |
| 311 | }, |
| 312 | }); |
| 313 | yield* waitForOperation(patched); |
| 314 | current = yield* waitUntilExists(name); |
| 315 | } |
| 316 | |
| 317 | return toAttrs(current, env.project); |
| 318 | }), |
| 319 | |
| 320 | delete: Effect.fn(function* ({ output }) { |
| 321 | const existing = yield* getByName(output.name); |
| 322 | if (existing === undefined) return; |
| 323 | const operation = yield* aiplatform |
| 324 | .deleteProjectsLocationsTensorboards({ name: output.name }) |
| 325 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 326 | if (operation !== undefined) { |
| 327 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 328 | } |
| 329 | }), |
| 330 | }); |