The best way to control Claude Code, Codex and more from any computer or phone
| 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 { hasAlchemyLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { |
| 15 | createInternalLabels, |
| 16 | hasAlchemyPrefix, |
| 17 | labelsDiffer, |
| 18 | listLocations, |
| 19 | locationOf, |
| 20 | lastSegment, |
| 21 | projectOf, |
| 22 | stableJson, |
| 23 | toDisplayName, |
| 24 | toLabels, |
| 25 | toResourceId, |
| 26 | userLabels, |
| 27 | } from "./names.ts"; |
| 28 | import { waitForOperation } from "./operations.ts"; |
| 29 | |
| 30 | const DEFAULT_SCHEMA_TITLE = "system.Experiment"; |
| 31 | |
| 32 | export type MetadataStoresContextProps = { |
| 33 | /** |
| 34 | * Parent MetadataStore resource name |
| 35 | * `projects/{project}/locations/{location}/metadataStores/{metadatastore}`. |
| 36 | * Immutable — changing it replaces the context. |
| 37 | */ |
| 38 | metadataStore: string; |
| 39 | /** |
| 40 | * Context id. If omitted, a unique id is generated. Must be 4-128 |
| 41 | * characters of `a-z` and `-`. Immutable. |
| 42 | */ |
| 43 | contextId?: string; |
| 44 | /** |
| 45 | * Display name (max 128 Unicode characters). |
| 46 | */ |
| 47 | displayName?: string; |
| 48 | /** |
| 49 | * Human-readable description. |
| 50 | */ |
| 51 | description?: string; |
| 52 | /** |
| 53 | * User labels. Alchemy ownership labels are merged in automatically. |
| 54 | */ |
| 55 | labels?: Record<string, string>; |
| 56 | /** |
| 57 | * Schema title registered in the MetadataStore. Vertex AI rejects |
| 58 | * entries without one. |
| 59 | * @default "system.Experiment" |
| 60 | */ |
| 61 | schemaTitle?: string; |
| 62 | /** |
| 63 | * Schema version registered in the MetadataStore. |
| 64 | */ |
| 65 | schemaVersion?: string; |
| 66 | /** |
| 67 | * Arbitrary metadata properties. |
| 68 | */ |
| 69 | metadata?: aiplatform.DocumentMap; |
| 70 | }; |
| 71 | |
| 72 | export type MetadataStoresContext = Resource< |
| 73 | "GCP.AIPlatform.MetadataStoresContext", |
| 74 | MetadataStoresContextProps, |
| 75 | { |
| 76 | /** Full resource name. */ |
| 77 | name: string; |
| 78 | /** Context id (last path segment). */ |
| 79 | contextId: string; |
| 80 | /** Parent MetadataStore resource name. */ |
| 81 | metadataStore: string; |
| 82 | /** Project id. */ |
| 83 | project: string; |
| 84 | /** Location id. */ |
| 85 | location: string; |
| 86 | /** Display name. */ |
| 87 | displayName: string | undefined; |
| 88 | /** User-provided description. */ |
| 89 | description: string | undefined; |
| 90 | /** User labels (Alchemy ownership labels stripped). */ |
| 91 | labels: Record<string, string>; |
| 92 | /** Schema title. */ |
| 93 | schemaTitle: string | undefined; |
| 94 | /** Schema version. */ |
| 95 | schemaVersion: string | undefined; |
| 96 | /** Parent context resource names. */ |
| 97 | parentContexts: string[]; |
| 98 | /** RFC3339 creation timestamp. */ |
| 99 | createTime: string | undefined; |
| 100 | /** RFC3339 last-update timestamp. */ |
| 101 | updateTime: string | undefined; |
| 102 | }, |
| 103 | never, |
| 104 | Providers |
| 105 | >; |
| 106 | |
| 107 | /** |
| 108 | * A Vertex ML Metadata Context associated with a MetadataStore. |
| 109 | * |
| 110 | * Changing `metadataStore` or `contextId` replaces the context. Display |
| 111 | * name, description, labels, schema, and metadata update in place. |
| 112 | * |
| 113 | * ### Creating a Context |
| 114 | * **Example:** Experiment context |
| 115 | * ```typescript |
| 116 | * const context = yield* GCP.AIPlatform.MetadataStoresContext("Experiment", { |
| 117 | * metadataStore: store.name, |
| 118 | * displayName: "training-run", |
| 119 | * labels: { env: "prod" }, |
| 120 | * }); |
| 121 | * ``` |
| 122 | * |
| 123 | * @resource |
| 124 | * @category AIPlatform |
| 125 | */ |
| 126 | export const MetadataStoresContext = Resource<MetadataStoresContext>( |
| 127 | "GCP.AIPlatform.MetadataStoresContext", |
| 128 | ); |
| 129 | |
| 130 | export class MetadataStoresContextNotResolved extends Data.TaggedError( |
| 131 | "GCP.AIPlatform.MetadataStoresContextNotResolved", |
| 132 | )<{ |
| 133 | resourceName: string; |
| 134 | }> {} |
| 135 | |
| 136 | export class MetadataStoresContextStillExists extends Data.TaggedError( |
| 137 | "GCP.AIPlatform.MetadataStoresContextStillExists", |
| 138 | )<{ |
| 139 | resourceName: string; |
| 140 | }> {} |
| 141 | |
| 142 | const parentOf = (name: string) => { |
| 143 | const at = name.lastIndexOf("/contexts/"); |
| 144 | return at >= 0 ? name.slice(0, at) : name; |
| 145 | }; |
| 146 | |
| 147 | const resourceName = (metadataStore: string, contextId: string) => |
| 148 | `${metadataStore}/contexts/${contextId}`; |
| 149 | |
| 150 | const toAttrs = (context: aiplatform.GoogleCloudAiplatformV1Context, project: string) => { |
| 151 | const name = context.name ?? ""; |
| 152 | return { |
| 153 | name, |
| 154 | contextId: lastSegment(name), |
| 155 | metadataStore: parentOf(name), |
| 156 | project: projectOf(name, project), |
| 157 | location: locationOf(name), |
| 158 | displayName: context.displayName, |
| 159 | description: context.description, |
| 160 | labels: userLabels(context.labels), |
| 161 | schemaTitle: context.schemaTitle, |
| 162 | schemaVersion: context.schemaVersion, |
| 163 | parentContexts: context.parentContexts ?? [], |
| 164 | createTime: context.createTime, |
| 165 | updateTime: context.updateTime, |
| 166 | }; |
| 167 | }; |
| 168 | |
| 169 | const getByName = (name: string) => |
| 170 | aiplatform |
| 171 | .getProjectsLocationsMetadataStoresContexts({ name }) |
| 172 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 173 | |
| 174 | const listContexts = (parent: string) => |
| 175 | aiplatform.listProjectsLocationsMetadataStoresContexts.pages({ parent, pageSize: 100 }).pipe( |
| 176 | Stream.runCollect, |
| 177 | Effect.map((pages) => Array.from(pages).flatMap((page) => page.contexts ?? [])), |
| 178 | Effect.catchTag("NotFound", () => |
| 179 | Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1Context[]), |
| 180 | ), |
| 181 | ); |
| 182 | |
| 183 | const listStores = (parent: string) => |
| 184 | aiplatform.listProjectsLocationsMetadataStores.pages({ parent, pageSize: 100 }).pipe( |
| 185 | Stream.runCollect, |
| 186 | Effect.map((pages) => Array.from(pages).flatMap((page) => page.metadataStores ?? [])), |
| 187 | Effect.catchTag("NotFound", () => |
| 188 | Effect.succeed([] as aiplatform.GoogleCloudAiplatformV1MetadataStore[]), |
| 189 | ), |
| 190 | ); |
| 191 | |
| 192 | const waitUntilGone = (name: string) => |
| 193 | getByName(name).pipe( |
| 194 | Effect.filterOrFail( |
| 195 | (context) => context === undefined, |
| 196 | () => new MetadataStoresContextStillExists({ resourceName: name }), |
| 197 | ), |
| 198 | Effect.asVoid, |
| 199 | Effect.retry({ |
| 200 | while: (error) => error._tag === "GCP.AIPlatform.MetadataStoresContextStillExists", |
| 201 | times: 10, |
| 202 | schedule: Schedule.spaced("1 second"), |
| 203 | }), |
| 204 | ); |
| 205 | |
| 206 | export const MetadataStoresContextProvider = () => |
| 207 | Provider.succeed(MetadataStoresContext, { |
| 208 | stables: ["name", "contextId", "metadataStore", "project", "location", "createTime"], |
| 209 | |
| 210 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 211 | if (!isResolved(news)) return undefined; |
| 212 | const previousParent = olds?.metadataStore ?? output?.metadataStore; |
| 213 | const nextParent = news.metadataStore ?? previousParent; |
| 214 | const previousId = olds?.contextId ?? output?.contextId; |
| 215 | const nextId = news.contextId ?? previousId; |
| 216 | if ( |
| 217 | (previousParent !== undefined && |
| 218 | nextParent !== undefined && |
| 219 | nextParent !== previousParent) || |
| 220 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) |
| 221 | ) { |
| 222 | return { |
| 223 | action: "replace" as const, |
| 224 | deleteFirst: |
| 225 | previousParent === nextParent && previousId !== undefined && nextId === previousId, |
| 226 | }; |
| 227 | } |
| 228 | return undefined; |
| 229 | }), |
| 230 | |
| 231 | read: Effect.fn(function* ({ id, olds, output }) { |
| 232 | const env = yield* GcpEnvironment.current; |
| 233 | const contextId = yield* toResourceId(id, olds?.contextId, output?.contextId); |
| 234 | const metadataStore = olds?.metadataStore ?? output?.metadataStore; |
| 235 | const name = |
| 236 | output?.name ?? (metadataStore !== undefined ? resourceName(metadataStore, contextId) : ""); |
| 237 | if (name.length === 0) return undefined; |
| 238 | const existing = yield* getByName(name); |
| 239 | if (existing === undefined) return undefined; |
| 240 | const attrs = toAttrs(existing, env.project); |
| 241 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 242 | }), |
| 243 | |
| 244 | list: () => |
| 245 | Effect.gen(function* () { |
| 246 | const env = yield* GcpEnvironment.current; |
| 247 | const stores = yield* Effect.forEach( |
| 248 | listLocations(env.region), |
| 249 | (location) => listStores(`projects/${env.project}/locations/${location}`), |
| 250 | { concurrency: 4 }, |
| 251 | ); |
| 252 | const contexts = yield* Effect.forEach( |
| 253 | stores.flat().filter((store) => store.name !== undefined), |
| 254 | (store) => listContexts(store.name!), |
| 255 | { concurrency: 4 }, |
| 256 | ); |
| 257 | return contexts |
| 258 | .flat() |
| 259 | .filter((context) => hasAlchemyPrefix(context.labels)) |
| 260 | .map((context) => toAttrs(context, env.project)); |
| 261 | }), |
| 262 | |
| 263 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 264 | const env = yield* GcpEnvironment.current; |
| 265 | const contextId = yield* toResourceId(id, news.contextId, output?.contextId); |
| 266 | const metadataStore = news.metadataStore; |
| 267 | const name = resourceName(metadataStore, contextId); |
| 268 | const displayName = yield* toDisplayName(id, news.displayName, output?.displayName); |
| 269 | const desiredLabels = { |
| 270 | ...toLabels(news.labels), |
| 271 | ...(yield* createInternalLabels(id)), |
| 272 | }; |
| 273 | |
| 274 | let current = yield* getByName(output?.name ?? name); |
| 275 | |
| 276 | if (current === undefined) { |
| 277 | const created = yield* aiplatform |
| 278 | .createProjectsLocationsMetadataStoresContexts({ |
| 279 | parent: metadataStore, |
| 280 | contextId, |
| 281 | body: { |
| 282 | displayName, |
| 283 | description: news.description, |
| 284 | labels: desiredLabels, |
| 285 | schemaTitle: news.schemaTitle ?? DEFAULT_SCHEMA_TITLE, |
| 286 | schemaVersion: news.schemaVersion, |
| 287 | metadata: news.metadata, |
| 288 | }, |
| 289 | }) |
| 290 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 291 | current = created ?? undefined; |
| 292 | } |
| 293 | |
| 294 | if (current === undefined) { |
| 295 | return yield* new MetadataStoresContextNotResolved({ resourceName: name }); |
| 296 | } |
| 297 | |
| 298 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 299 | const displayChanged = (current.displayName ?? "") !== displayName; |
| 300 | const labelsChanged = labelsDiffer(current.labels, desiredLabels); |
| 301 | const schemaTitleChanged = |
| 302 | (current.schemaTitle ?? "") !== (news.schemaTitle ?? DEFAULT_SCHEMA_TITLE); |
| 303 | const schemaVersionChanged = (current.schemaVersion ?? "") !== (news.schemaVersion ?? ""); |
| 304 | const metadataChanged = |
| 305 | news.metadata !== undefined && stableJson(current.metadata) !== stableJson(news.metadata); |
| 306 | |
| 307 | if ( |
| 308 | descriptionChanged || |
| 309 | displayChanged || |
| 310 | labelsChanged || |
| 311 | schemaTitleChanged || |
| 312 | schemaVersionChanged || |
| 313 | metadataChanged |
| 314 | ) { |
| 315 | current = yield* aiplatform.patchProjectsLocationsMetadataStoresContexts({ |
| 316 | name, |
| 317 | updateMask: [ |
| 318 | descriptionChanged ? "description" : undefined, |
| 319 | displayChanged ? "display_name" : undefined, |
| 320 | labelsChanged ? "labels" : undefined, |
| 321 | schemaTitleChanged ? "schema_title" : undefined, |
| 322 | schemaVersionChanged ? "schema_version" : undefined, |
| 323 | metadataChanged ? "metadata" : undefined, |
| 324 | ] |
| 325 | .filter((field): field is string => field !== undefined) |
| 326 | .join(","), |
| 327 | body: { |
| 328 | name, |
| 329 | displayName, |
| 330 | description: news.description, |
| 331 | labels: desiredLabels, |
| 332 | schemaTitle: news.schemaTitle ?? DEFAULT_SCHEMA_TITLE, |
| 333 | schemaVersion: news.schemaVersion, |
| 334 | metadata: news.metadata, |
| 335 | etag: current.etag, |
| 336 | }, |
| 337 | }); |
| 338 | } |
| 339 | |
| 340 | return toAttrs(current, env.project); |
| 341 | }), |
| 342 | |
| 343 | delete: Effect.fn(function* ({ output }) { |
| 344 | const operation = yield* aiplatform |
| 345 | .deleteProjectsLocationsMetadataStoresContexts({ |
| 346 | name: output.name, |
| 347 | }) |
| 348 | .pipe( |
| 349 | Effect.retry({ |
| 350 | while: (error) => error._tag === "Conflict", |
| 351 | times: 8, |
| 352 | schedule: Schedule.spaced("2 seconds"), |
| 353 | }), |
| 354 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 355 | ); |
| 356 | if (operation !== undefined) { |
| 357 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 358 | } |
| 359 | yield* waitUntilGone(output.name); |
| 360 | }), |
| 361 | }); |