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, 04:45 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 * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { hasAlchemyLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | createInternalLabels, |
| 15 | encodeDescription, |
| 16 | hasOwnershipMarker, |
| 17 | listLocations, |
| 18 | locationOf, |
| 19 | lastSegment, |
| 20 | normalizeLocation, |
| 21 | parseDescription, |
| 22 | projectOf, |
| 23 | toResourceId, |
| 24 | } from "./names.ts"; |
| 25 | import { waitForOperation } from "./operations.ts"; |
| 26 | |
| 27 | export type MetadataStoreProps = { |
| 28 | /** |
| 29 | * MetadataStore id (the `{metadatastore}` segment of |
| 30 | * `projects/{project}/locations/{location}/metadataStores/{metadatastore}`). |
| 31 | * If omitted, a unique name is generated. Must be 4-128 characters of |
| 32 | * `a-z` and `-`. Immutable — changing it replaces the store. |
| 33 | */ |
| 34 | metadataStoreId?: string; |
| 35 | /** |
| 36 | * Vertex AI location. Immutable — changing it replaces the store. |
| 37 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 38 | */ |
| 39 | location?: string; |
| 40 | /** |
| 41 | * Human-readable description. Metadata stores have no labels field, so |
| 42 | * Alchemy ownership is stored in a `[alchemy …]` prefix and stripped |
| 43 | * from attributes. |
| 44 | */ |
| 45 | description?: string; |
| 46 | /** |
| 47 | * Customer-managed encryption key. Immutable. |
| 48 | */ |
| 49 | encryptionSpec?: aiplatform.GoogleCloudAiplatformV1EncryptionSpec; |
| 50 | /** |
| 51 | * Dataplex integration settings. |
| 52 | */ |
| 53 | dataplexConfig?: aiplatform.GoogleCloudAiplatformV1MetadataStoreDataplexConfig; |
| 54 | }; |
| 55 | |
| 56 | export type MetadataStore = Resource< |
| 57 | "GCP.AIPlatform.MetadataStore", |
| 58 | MetadataStoreProps, |
| 59 | { |
| 60 | /** Full resource name `projects/{project}/locations/{location}/metadataStores/{metadatastore}`. */ |
| 61 | name: string; |
| 62 | /** MetadataStore id (last path segment). */ |
| 63 | metadataStoreId: string; |
| 64 | /** Project id. */ |
| 65 | project: string; |
| 66 | /** Location id. */ |
| 67 | location: string; |
| 68 | /** User description with the Alchemy ownership prefix stripped. */ |
| 69 | description: string | undefined; |
| 70 | /** Disk utilization of the store, in bytes. */ |
| 71 | diskUtilizationBytes: string | undefined; |
| 72 | /** KMS key used for encryption, if any. */ |
| 73 | kmsKeyName: string | undefined; |
| 74 | /** RFC3339 creation timestamp. */ |
| 75 | createTime: string | undefined; |
| 76 | /** RFC3339 last-update timestamp. */ |
| 77 | updateTime: string | undefined; |
| 78 | }, |
| 79 | never, |
| 80 | Providers |
| 81 | >; |
| 82 | |
| 83 | /** |
| 84 | * A Vertex ML Metadata store for artifacts, contexts, and executions. |
| 85 | * |
| 86 | * Metadata stores have no labels field, so Alchemy stamps ownership into |
| 87 | * the description for `list` / nuke. There is no update API — changing |
| 88 | * identity, location, encryption, or Dataplex config replaces the store. |
| 89 | * |
| 90 | * ### Creating a MetadataStore |
| 91 | * **Example:** Generated id |
| 92 | * ```typescript |
| 93 | * const store = yield* GCP.AIPlatform.MetadataStore("Mlmd", { |
| 94 | * description: "pipeline metadata", |
| 95 | * }); |
| 96 | * ``` |
| 97 | * |
| 98 | * @resource |
| 99 | * @category AIPlatform |
| 100 | */ |
| 101 | export const MetadataStore = Resource<MetadataStore>("GCP.AIPlatform.MetadataStore"); |
| 102 | |
| 103 | export class MetadataStoreNotResolved extends Data.TaggedError( |
| 104 | "GCP.AIPlatform.MetadataStoreNotResolved", |
| 105 | )<{ |
| 106 | resourceName: string; |
| 107 | }> {} |
| 108 | |
| 109 | export class MetadataStoreStillExists extends Data.TaggedError( |
| 110 | "GCP.AIPlatform.MetadataStoreStillExists", |
| 111 | )<{ |
| 112 | resourceName: string; |
| 113 | }> {} |
| 114 | |
| 115 | const resourceName = (project: string, location: string, metadataStoreId: string) => |
| 116 | `projects/${project}/locations/${location}/metadataStores/${metadataStoreId}`; |
| 117 | |
| 118 | const toAttrs = (store: aiplatform.GoogleCloudAiplatformV1MetadataStore, project: string) => { |
| 119 | const name = store.name ?? ""; |
| 120 | const parsed = parseDescription(store.description); |
| 121 | return { |
| 122 | name, |
| 123 | metadataStoreId: lastSegment(name), |
| 124 | project: projectOf(name, project), |
| 125 | location: locationOf(name), |
| 126 | description: parsed.description, |
| 127 | diskUtilizationBytes: store.state?.diskUtilizationBytes, |
| 128 | kmsKeyName: store.encryptionSpec?.kmsKeyName, |
| 129 | createTime: store.createTime, |
| 130 | updateTime: store.updateTime, |
| 131 | }; |
| 132 | }; |
| 133 | |
| 134 | const getByName = (name: string) => |
| 135 | aiplatform |
| 136 | .getProjectsLocationsMetadataStores({ name }) |
| 137 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 138 | |
| 139 | const listPage = (parent: string) => |
| 140 | aiplatform.listProjectsLocationsMetadataStores.pages({ parent, pageSize: 100 }).pipe( |
| 141 | Stream.runCollect, |
| 142 | Effect.map((pages) => Array.from(pages).flatMap((page) => page.metadataStores ?? [])), |
| 143 | Effect.catchTag("NotFound", () => |
| 144 | Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1MetadataStore[]), |
| 145 | ), |
| 146 | ); |
| 147 | |
| 148 | const waitUntilExists = (name: string) => |
| 149 | getByName(name).pipe( |
| 150 | Effect.filterOrFail( |
| 151 | (store): store is aiplatform.GoogleCloudAiplatformV1MetadataStore => store !== undefined, |
| 152 | () => new MetadataStoreNotResolved({ resourceName: name }), |
| 153 | ), |
| 154 | Effect.retry({ |
| 155 | while: (error) => error._tag === "GCP.AIPlatform.MetadataStoreNotResolved", |
| 156 | times: 8, |
| 157 | schedule: Schedule.spaced("2 seconds"), |
| 158 | }), |
| 159 | ); |
| 160 | |
| 161 | const waitUntilGone = (name: string) => |
| 162 | getByName(name).pipe( |
| 163 | Effect.filterOrFail( |
| 164 | (store) => store === undefined, |
| 165 | () => new MetadataStoreStillExists({ resourceName: name }), |
| 166 | ), |
| 167 | Effect.asVoid, |
| 168 | Effect.retry({ |
| 169 | while: (error) => error._tag === "GCP.AIPlatform.MetadataStoreStillExists", |
| 170 | times: 10, |
| 171 | schedule: Schedule.spaced("2 seconds"), |
| 172 | }), |
| 173 | ); |
| 174 | |
| 175 | export const MetadataStoreProvider = () => |
| 176 | Provider.succeed(MetadataStore, { |
| 177 | stables: ["name", "metadataStoreId", "project", "location", "kmsKeyName", "createTime"], |
| 178 | |
| 179 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 180 | const env = yield* GcpEnvironment.current; |
| 181 | if (!isResolved(news)) return undefined; |
| 182 | const previousId = olds?.metadataStoreId ?? output?.metadataStoreId; |
| 183 | const nextId = news.metadataStoreId ?? previousId; |
| 184 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 185 | const nextLocation = normalizeLocation( |
| 186 | news.location ?? olds?.location ?? output?.location, |
| 187 | env.region, |
| 188 | ); |
| 189 | const previousKms = olds?.encryptionSpec?.kmsKeyName ?? output?.kmsKeyName ?? ""; |
| 190 | const nextKms = news.encryptionSpec?.kmsKeyName ?? previousKms; |
| 191 | if ( |
| 192 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 193 | previousLocation !== nextLocation || |
| 194 | nextKms !== previousKms |
| 195 | ) { |
| 196 | return { |
| 197 | action: "replace" as const, |
| 198 | deleteFirst: |
| 199 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 200 | }; |
| 201 | } |
| 202 | return undefined; |
| 203 | }), |
| 204 | |
| 205 | read: Effect.fn(function* ({ id, olds, output }) { |
| 206 | const env = yield* GcpEnvironment.current; |
| 207 | const metadataStoreId = yield* toResourceId( |
| 208 | id, |
| 209 | olds?.metadataStoreId, |
| 210 | output?.metadataStoreId, |
| 211 | ); |
| 212 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 213 | const name = output?.name ?? resourceName(env.project, location, metadataStoreId); |
| 214 | const existing = yield* getByName(name); |
| 215 | if (existing === undefined) return undefined; |
| 216 | const attrs = toAttrs(existing, env.project); |
| 217 | const { labels } = parseDescription(existing.description); |
| 218 | return (yield* hasAlchemyLabels(id, labels)) ? attrs : Unowned(attrs); |
| 219 | }), |
| 220 | |
| 221 | list: () => |
| 222 | Effect.gen(function* () { |
| 223 | const env = yield* GcpEnvironment.current; |
| 224 | const pages = yield* Effect.forEach( |
| 225 | listLocations(env.region), |
| 226 | (location) => listPage(`projects/${env.project}/locations/${location}`), |
| 227 | { concurrency: 4 }, |
| 228 | ); |
| 229 | return pages |
| 230 | .flat() |
| 231 | .filter((store) => hasOwnershipMarker(store.description)) |
| 232 | .map((store) => toAttrs(store, env.project)); |
| 233 | }), |
| 234 | |
| 235 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 236 | const env = yield* GcpEnvironment.current; |
| 237 | const metadataStoreId = yield* toResourceId( |
| 238 | id, |
| 239 | news.metadataStoreId, |
| 240 | output?.metadataStoreId, |
| 241 | ); |
| 242 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 243 | const name = resourceName(env.project, location, metadataStoreId); |
| 244 | const ownership = yield* createInternalLabels(id); |
| 245 | const desiredDescription = encodeDescription(ownership, news.description); |
| 246 | |
| 247 | let current = yield* getByName(output?.name ?? name); |
| 248 | |
| 249 | if (current === undefined) { |
| 250 | const created = yield* aiplatform |
| 251 | .createProjectsLocationsMetadataStores({ |
| 252 | parent: `projects/${env.project}/locations/${location}`, |
| 253 | metadataStoreId, |
| 254 | body: { |
| 255 | description: desiredDescription, |
| 256 | encryptionSpec: news.encryptionSpec, |
| 257 | dataplexConfig: news.dataplexConfig, |
| 258 | }, |
| 259 | }) |
| 260 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 261 | if (created !== undefined) { |
| 262 | yield* waitForOperation(created); |
| 263 | } |
| 264 | current = yield* waitUntilExists(name); |
| 265 | } |
| 266 | |
| 267 | return toAttrs(current, env.project); |
| 268 | }), |
| 269 | |
| 270 | delete: Effect.fn(function* ({ output }) { |
| 271 | const operation = yield* aiplatform |
| 272 | .deleteProjectsLocationsMetadataStores({ |
| 273 | name: output.name, |
| 274 | force: true, |
| 275 | }) |
| 276 | .pipe( |
| 277 | Effect.retry({ |
| 278 | while: (error) => error._tag === "Conflict", |
| 279 | times: 8, |
| 280 | schedule: Schedule.spaced("2 seconds"), |
| 281 | }), |
| 282 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 283 | ); |
| 284 | if (operation !== undefined) { |
| 285 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 286 | } |
| 287 | yield* waitUntilGone(output.name); |
| 288 | }), |
| 289 | }); |