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 { 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 { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 13 | import type { Providers } from "../Providers.ts"; |
| 14 | import { |
| 15 | expandParent, |
| 16 | hasAlchemyLabelMap, |
| 17 | lastSegment, |
| 18 | normalizeLocation, |
| 19 | parseResourceName, |
| 20 | toPhysicalSnake, |
| 21 | userLabels, |
| 22 | } from "./helpers.ts"; |
| 23 | import { listLocations } from "./names.ts"; |
| 24 | import { waitForOperation } from "./operations.ts"; |
| 25 | |
| 26 | const MAX_NAME_LENGTH = 63; |
| 27 | |
| 28 | export type FeatureGroupsFeatureProps = { |
| 29 | /** |
| 30 | * Parent Feature Group resource name |
| 31 | * `projects/{project}/locations/{location}/featureGroups/{featureGroup}` |
| 32 | * or the feature-group id (uses `location`). Immutable. |
| 33 | */ |
| 34 | featureGroup: string; |
| 35 | /** |
| 36 | * Feature id. Valid characters `[a-z0-9_]`, must start with a letter. |
| 37 | * Immutable — changing it replaces the feature. |
| 38 | */ |
| 39 | featureId?: string; |
| 40 | /** |
| 41 | * Region used when `featureGroup` is a bare id. |
| 42 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 43 | */ |
| 44 | location?: string; |
| 45 | /** |
| 46 | * Description of the feature. |
| 47 | */ |
| 48 | description?: string; |
| 49 | /** |
| 50 | * User labels. Alchemy ownership labels are merged in automatically. |
| 51 | */ |
| 52 | labels?: Record<string, string>; |
| 53 | /** |
| 54 | * BigQuery column hosting values for this feature. Defaults to the |
| 55 | * feature id. |
| 56 | */ |
| 57 | versionColumnName?: string; |
| 58 | /** |
| 59 | * Entity responsible for maintaining this feature (emails or URIs). |
| 60 | */ |
| 61 | pointOfContact?: string; |
| 62 | }; |
| 63 | |
| 64 | export type FeatureGroupsFeature = Resource< |
| 65 | "GCP.AIPlatform.FeatureGroupsFeature", |
| 66 | FeatureGroupsFeatureProps, |
| 67 | { |
| 68 | /** Full resource name. */ |
| 69 | name: string; |
| 70 | /** Feature id. */ |
| 71 | featureId: string; |
| 72 | /** Parent feature group resource name. */ |
| 73 | featureGroup: string; |
| 74 | /** Feature group id. */ |
| 75 | featureGroupId: string; |
| 76 | /** Project id. */ |
| 77 | project: string; |
| 78 | /** Location id. */ |
| 79 | location: string; |
| 80 | /** Description. */ |
| 81 | description: string | undefined; |
| 82 | /** User labels (Alchemy ownership labels stripped). */ |
| 83 | labels: Record<string, string>; |
| 84 | /** Version column name. */ |
| 85 | versionColumnName: string | undefined; |
| 86 | /** Point of contact. */ |
| 87 | pointOfContact: string | undefined; |
| 88 | /** Value type (legacy Feature Store only). */ |
| 89 | valueType: string | undefined; |
| 90 | /** RFC3339 creation timestamp. */ |
| 91 | createTime: string | undefined; |
| 92 | /** RFC3339 last-update timestamp. */ |
| 93 | updateTime: string | undefined; |
| 94 | /** Server etag. */ |
| 95 | etag: string | undefined; |
| 96 | }, |
| 97 | never, |
| 98 | Providers |
| 99 | >; |
| 100 | |
| 101 | /** |
| 102 | * A Feature Registry Feature belonging to a Feature Group. |
| 103 | * |
| 104 | * Parent group, feature id, and location are identity. Description, |
| 105 | * labels, version column, and point of contact update in place. |
| 106 | * |
| 107 | * ### Creating a Feature |
| 108 | * **Example:** Feature under a group |
| 109 | * ```typescript |
| 110 | * const feature = yield* GCP.AIPlatform.FeatureGroupsFeature("Age", { |
| 111 | * featureGroup: group.name, |
| 112 | * versionColumnName: "age", |
| 113 | * labels: { env: "prod" }, |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * @resource |
| 118 | * @category AIPlatform |
| 119 | */ |
| 120 | export const FeatureGroupsFeature = Resource<FeatureGroupsFeature>( |
| 121 | "GCP.AIPlatform.FeatureGroupsFeature", |
| 122 | ); |
| 123 | |
| 124 | export class FeatureGroupsFeatureNotResolved extends Data.TaggedError( |
| 125 | "GCP.AIPlatform.FeatureGroupsFeatureNotResolved", |
| 126 | )<{ |
| 127 | resourceName: string; |
| 128 | }> {} |
| 129 | |
| 130 | export class FeatureGroupsFeatureStillExists extends Data.TaggedError( |
| 131 | "GCP.AIPlatform.FeatureGroupsFeatureStillExists", |
| 132 | )<{ |
| 133 | resourceName: string; |
| 134 | }> {} |
| 135 | |
| 136 | const parentOf = (project: string, location: string, featureGroup: string) => |
| 137 | expandParent(featureGroup, project, location, "featureGroups"); |
| 138 | |
| 139 | const resourceName = (parent: string, featureId: string) => `${parent}/features/${featureId}`; |
| 140 | |
| 141 | const toAttrs = (feature: aiplatform.GoogleCloudAiplatformV1Feature, project: string) => { |
| 142 | const name = feature.name ?? ""; |
| 143 | const parsed = parseResourceName(name, "features"); |
| 144 | const group = parseResourceName(parsed.parent, "featureGroups"); |
| 145 | return { |
| 146 | name, |
| 147 | featureId: parsed.id, |
| 148 | featureGroup: parsed.parent, |
| 149 | featureGroupId: group.id, |
| 150 | project: parsed.project || project, |
| 151 | location: parsed.location, |
| 152 | description: feature.description, |
| 153 | labels: userLabels(feature.labels), |
| 154 | versionColumnName: feature.versionColumnName, |
| 155 | pointOfContact: feature.pointOfContact, |
| 156 | valueType: feature.valueType, |
| 157 | createTime: feature.createTime, |
| 158 | updateTime: feature.updateTime, |
| 159 | etag: feature.etag, |
| 160 | }; |
| 161 | }; |
| 162 | |
| 163 | const getByName = (name: string) => |
| 164 | aiplatform |
| 165 | .getProjectsLocationsFeatureGroupsFeatures({ name }) |
| 166 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 167 | |
| 168 | const waitUntilExists = (name: string) => |
| 169 | getByName(name).pipe( |
| 170 | Effect.flatMap((feature) => |
| 171 | feature |
| 172 | ? Effect.succeed(feature) |
| 173 | : Effect.fail(new FeatureGroupsFeatureNotResolved({ resourceName: name })), |
| 174 | ), |
| 175 | Effect.retry({ |
| 176 | while: (error) => error._tag === "GCP.AIPlatform.FeatureGroupsFeatureNotResolved", |
| 177 | times: 8, |
| 178 | schedule: Schedule.spaced("1 second"), |
| 179 | }), |
| 180 | ); |
| 181 | |
| 182 | const waitUntilGone = (name: string) => |
| 183 | getByName(name).pipe( |
| 184 | Effect.flatMap((feature) => |
| 185 | feature === undefined |
| 186 | ? Effect.void |
| 187 | : Effect.fail(new FeatureGroupsFeatureStillExists({ resourceName: name })), |
| 188 | ), |
| 189 | Effect.retry({ |
| 190 | while: (error) => error._tag === "GCP.AIPlatform.FeatureGroupsFeatureStillExists", |
| 191 | times: 10, |
| 192 | schedule: Schedule.spaced("2 seconds"), |
| 193 | }), |
| 194 | ); |
| 195 | |
| 196 | const listFeaturesUnder = (parent: string, project: string) => |
| 197 | aiplatform.listProjectsLocationsFeatureGroupsFeatures.pages({ parent, pageSize: 100 }).pipe( |
| 198 | Stream.flatMap((page) => Stream.fromIterable(page.features ?? [])), |
| 199 | Stream.filter((feature) => hasAlchemyLabelMap(feature.labels)), |
| 200 | Stream.map((feature) => toAttrs(feature, project)), |
| 201 | Stream.runCollect, |
| 202 | Effect.map((chunk) => Array.from(chunk)), |
| 203 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 204 | ); |
| 205 | |
| 206 | export const FeatureGroupsFeatureProvider = () => |
| 207 | Provider.succeed(FeatureGroupsFeature, { |
| 208 | stables: [ |
| 209 | "name", |
| 210 | "featureId", |
| 211 | "featureGroup", |
| 212 | "featureGroupId", |
| 213 | "project", |
| 214 | "location", |
| 215 | "createTime", |
| 216 | ], |
| 217 | |
| 218 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 219 | const env = yield* GcpEnvironment.current; |
| 220 | if (!isResolved(news)) return undefined; |
| 221 | const previousId = olds?.featureId ?? output?.featureId; |
| 222 | const nextId = news.featureId ?? previousId; |
| 223 | const previousParent = olds?.featureGroup ?? output?.featureGroup ?? ""; |
| 224 | const nextParent = news.featureGroup |
| 225 | ? lastSegment(news.featureGroup) === lastSegment(previousParent) || |
| 226 | news.featureGroup === previousParent |
| 227 | ? previousParent |
| 228 | : news.featureGroup |
| 229 | : previousParent; |
| 230 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 231 | const nextLocation = normalizeLocation(news.location ?? output?.location, env.region); |
| 232 | const parentChanged = |
| 233 | previousParent.length > 0 && lastSegment(nextParent) !== lastSegment(previousParent); |
| 234 | const replace = |
| 235 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 236 | parentChanged || |
| 237 | previousLocation !== nextLocation; |
| 238 | if (!replace) return undefined; |
| 239 | return { action: "replace" as const, deleteFirst: false }; |
| 240 | }), |
| 241 | |
| 242 | read: Effect.fn(function* ({ id, olds, output }) { |
| 243 | const env = yield* GcpEnvironment.current; |
| 244 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 245 | const parentRef = olds?.featureGroup ?? output?.featureGroup; |
| 246 | // A create interrupted before its parent resolved has nothing to find. |
| 247 | if (output?.name === undefined && typeof parentRef !== "string") { |
| 248 | return undefined; |
| 249 | } |
| 250 | const parent = parentOf(env.project, location, parentRef ?? ""); |
| 251 | const featureId = yield* toPhysicalSnake( |
| 252 | id, |
| 253 | olds?.featureId, |
| 254 | output?.featureId, |
| 255 | MAX_NAME_LENGTH, |
| 256 | ); |
| 257 | const name = output?.name ?? resourceName(parent, featureId); |
| 258 | const existing = yield* getByName(name); |
| 259 | if (existing === undefined) return undefined; |
| 260 | const attrs = toAttrs(existing, env.project); |
| 261 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 262 | }), |
| 263 | |
| 264 | list: () => |
| 265 | Effect.gen(function* () { |
| 266 | const env = yield* GcpEnvironment.current; |
| 267 | const groups = yield* Stream.fromIterable(listLocations(env.region)).pipe( |
| 268 | Stream.flatMap((location) => |
| 269 | aiplatform.listProjectsLocationsFeatureGroups.pages({ |
| 270 | parent: `projects/${env.project}/locations/${location}`, |
| 271 | pageSize: 100, |
| 272 | }), |
| 273 | ), |
| 274 | Stream.flatMap((page) => Stream.fromIterable(page.featureGroups ?? [])), |
| 275 | Stream.filter((group) => hasAlchemyLabelMap(group.labels)), |
| 276 | Stream.runCollect, |
| 277 | Effect.map((chunk) => Array.from(chunk)), |
| 278 | Effect.catchTag("NotFound", () => Effect.succeed([])), |
| 279 | ); |
| 280 | const nested = yield* Effect.forEach( |
| 281 | groups, |
| 282 | (group) => (group.name ? listFeaturesUnder(group.name, env.project) : Effect.succeed([])), |
| 283 | { concurrency: 4 }, |
| 284 | ); |
| 285 | return nested.flat(); |
| 286 | }), |
| 287 | |
| 288 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 289 | const env = yield* GcpEnvironment.current; |
| 290 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 291 | const parent = parentOf(env.project, location, news.featureGroup); |
| 292 | const featureId = yield* toPhysicalSnake( |
| 293 | id, |
| 294 | news.featureId, |
| 295 | output?.featureId, |
| 296 | MAX_NAME_LENGTH, |
| 297 | ); |
| 298 | const name = resourceName(parent, featureId); |
| 299 | const desiredLabels = { |
| 300 | ...toLabels(news.labels), |
| 301 | ...(yield* createInternalLabels(id)), |
| 302 | }; |
| 303 | |
| 304 | let current = yield* getByName(output?.name ?? name); |
| 305 | |
| 306 | if (current === undefined) { |
| 307 | const created = yield* aiplatform |
| 308 | .createProjectsLocationsFeatureGroupsFeatures({ |
| 309 | parent, |
| 310 | featureId, |
| 311 | body: { |
| 312 | description: news.description, |
| 313 | labels: desiredLabels, |
| 314 | versionColumnName: news.versionColumnName, |
| 315 | pointOfContact: news.pointOfContact, |
| 316 | }, |
| 317 | }) |
| 318 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 319 | if (created !== undefined) { |
| 320 | yield* waitForOperation(created); |
| 321 | } |
| 322 | current = yield* waitUntilExists(name); |
| 323 | } |
| 324 | |
| 325 | if (current === undefined) { |
| 326 | return yield* new FeatureGroupsFeatureNotResolved({ resourceName: name }); |
| 327 | } |
| 328 | |
| 329 | const observedLabels = tagRecord(current.labels); |
| 330 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 331 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 332 | const descriptionChanged = (current.description ?? "") !== (news.description ?? ""); |
| 333 | const columnChanged = |
| 334 | news.versionColumnName !== undefined && |
| 335 | (current.versionColumnName ?? "") !== news.versionColumnName; |
| 336 | const contactChanged = |
| 337 | news.pointOfContact !== undefined && (current.pointOfContact ?? "") !== news.pointOfContact; |
| 338 | |
| 339 | if (labelsChanged || descriptionChanged || columnChanged || contactChanged) { |
| 340 | const updateMask = [ |
| 341 | labelsChanged ? "labels" : undefined, |
| 342 | descriptionChanged ? "description" : undefined, |
| 343 | columnChanged ? "version_column_name" : undefined, |
| 344 | contactChanged ? "point_of_contact" : undefined, |
| 345 | ].filter((field): field is string => field !== undefined); |
| 346 | |
| 347 | const patched = yield* aiplatform |
| 348 | .patchProjectsLocationsFeatureGroupsFeatures({ |
| 349 | name, |
| 350 | updateMask: updateMask.join(","), |
| 351 | body: { |
| 352 | name, |
| 353 | labels: desiredLabels, |
| 354 | description: news.description, |
| 355 | versionColumnName: news.versionColumnName, |
| 356 | pointOfContact: news.pointOfContact, |
| 357 | }, |
| 358 | }) |
| 359 | .pipe( |
| 360 | Effect.retry({ |
| 361 | while: (error) => error._tag === "Conflict", |
| 362 | times: 8, |
| 363 | schedule: Schedule.spaced("5 seconds"), |
| 364 | }), |
| 365 | ); |
| 366 | yield* waitForOperation(patched); |
| 367 | current = yield* waitUntilExists(name); |
| 368 | } |
| 369 | |
| 370 | return toAttrs(current, env.project); |
| 371 | }), |
| 372 | |
| 373 | delete: Effect.fn(function* ({ output }) { |
| 374 | const operation = yield* aiplatform |
| 375 | .deleteProjectsLocationsFeatureGroupsFeatures({ name: output.name }) |
| 376 | .pipe( |
| 377 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 378 | Effect.retry({ |
| 379 | while: (error) => error._tag === "Conflict", |
| 380 | times: 8, |
| 381 | schedule: Schedule.spaced("5 seconds"), |
| 382 | }), |
| 383 | ); |
| 384 | if (operation !== undefined) { |
| 385 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 386 | } |
| 387 | yield* waitUntilGone(output.name); |
| 388 | }), |
| 389 | }); |