@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 483 lines 16.8 KB
Raw
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 hasAlchemyLabelMap,
16 normalizeLocation,
17 parseResourceName,
18 specifiedEquals,
19 toPhysicalSnake,
20 userLabels,
21 } from "./helpers.ts";
22 import { listLocations } from "./names.ts";
23 import { waitForOperation } from "./operations.ts";
24
25 const MAX_NAME_LENGTH = 60;
26
27 export type FeatureOnlineStoreBigtableAutoScaling = {
28 /** Minimum node count (>= 1). */
29 minNodeCount?: number;
30 /** Maximum node count (<= 10 * minNodeCount). */
31 maxNodeCount?: number;
32 /** Target CPU percent (10–80). Defaults to 50. */
33 cpuUtilizationTarget?: number;
34 };
35
36 export type FeatureOnlineStoreBigtable = {
37 /** Autoscaling applied to the backing Bigtable instance. */
38 autoScaling?: FeatureOnlineStoreBigtableAutoScaling;
39 /** Allow direct Bigtable access. */
40 enableDirectBigtableAccess?: boolean;
41 /** Zone for the primary Bigtable cluster (e.g. `us-central1-a`). Immutable. */
42 zone?: string;
43 };
44
45 export type FeatureOnlineStoreDedicatedServingEndpoint = {
46 /** Private Service Connect config (Optimized storage only). */
47 privateServiceConnectConfig?: {
48 enablePrivateServiceConnect?: boolean;
49 projectAllowlist?: string[];
50 };
51 };
52
53 export type FeatureOnlineStoreProps = {
54 /**
55 * Feature Online Store id. Valid characters `[a-z0-9_]`, must start
56 * with a letter, max 60. Immutable — changing it replaces the store.
57 */
58 featureOnlineStoreId?: string;
59 /**
60 * Region. Immutable — changing it replaces the store.
61 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
62 */
63 location?: string;
64 /**
65 * User labels. Alchemy ownership labels are merged in automatically.
66 */
67 labels?: Record<string, string>;
68 /**
69 * Bigtable storage. Mutually exclusive with `optimized`. Immutable
70 * storage type — switching to Optimized replaces the store.
71 * @default { autoScaling: { minNodeCount: 1, maxNodeCount: 1 } } when `optimized` is unset
72 */
73 bigtable?: FeatureOnlineStoreBigtable;
74 /**
75 * Optimized storage. When true or `{}`, Vertex provisions the
76 * Optimized serving backend. Immutable storage type.
77 */
78 optimized?: boolean | Record<string, never>;
79 /**
80 * Dedicated serving endpoint (Optimized storage).
81 */
82 dedicatedServingEndpoint?: FeatureOnlineStoreDedicatedServingEndpoint;
83 /**
84 * Customer-managed encryption. Immutable.
85 */
86 encryptionSpec?: { kmsKeyName?: string };
87 };
88
89 export type FeatureOnlineStore = Resource<
90 "GCP.AIPlatform.FeatureOnlineStore",
91 FeatureOnlineStoreProps,
92 {
93 /** Full resource name. */
94 name: string;
95 /** Store id. */
96 featureOnlineStoreId: string;
97 /** Project id. */
98 project: string;
99 /** Location id. */
100 location: string;
101 /** User labels (Alchemy ownership labels stripped). */
102 labels: Record<string, string>;
103 /** Bigtable config, if used. */
104 bigtable: FeatureOnlineStoreBigtable | undefined;
105 /** Whether Optimized storage is enabled. */
106 optimized: boolean;
107 /** Store state (`STABLE`, `UPDATING`). */
108 state: string | undefined;
109 /** Dedicated serving endpoint. */
110 dedicatedServingEndpoint:
111 | {
112 publicEndpointDomainName: string | undefined;
113 serviceAttachment: string | undefined;
114 privateServiceConnectConfig:
115 | {
116 enablePrivateServiceConnect?: boolean;
117 projectAllowlist?: string[];
118 }
119 | undefined;
120 }
121 | undefined;
122 /** Customer-managed encryption key. */
123 encryptionSpec: { kmsKeyName?: string } | undefined;
124 /** RFC3339 creation timestamp. */
125 createTime: string | undefined;
126 /** RFC3339 last-update timestamp. */
127 updateTime: string | undefined;
128 /** Server etag. */
129 etag: string | undefined;
130 },
131 never,
132 Providers
133 >;
134
135 /**
136 * A Vertex AI Feature Online Store for low-latency feature and embedding
137 * serving.
138 *
139 * Id, location, storage type (Bigtable vs Optimized), Bigtable zone, and
140 * encryption are identity. Labels and Bigtable autoscaling update in
141 * place. Defaults to a one-node Bigtable backend when neither backend is
142 * set (Vertex AI no longer creates new Optimized stores for most projects).
143 *
144 * ### Creating a Feature Online Store
145 * **Example:** Default one-node Bigtable store
146 * ```typescript
147 * const store = yield* GCP.AIPlatform.FeatureOnlineStore("Serving", {
148 * labels: { env: "prod" },
149 * });
150 * ```
151 *
152 * **Example:** Bigtable-backed store
153 * ```typescript
154 * const store = yield* GCP.AIPlatform.FeatureOnlineStore("Serving", {
155 * bigtable: {
156 * autoScaling: { minNodeCount: 1, maxNodeCount: 2 },
157 * },
158 * });
159 * ```
160 *
161 * @resource
162 * @category AIPlatform
163 */
164 export const FeatureOnlineStore = Resource<FeatureOnlineStore>("GCP.AIPlatform.FeatureOnlineStore");
165
166 export class FeatureOnlineStoreNotResolved extends Data.TaggedError(
167 "GCP.AIPlatform.FeatureOnlineStoreNotResolved",
168 )<{
169 resourceName: string;
170 }> {}
171
172 export class FeatureOnlineStoreStillExists extends Data.TaggedError(
173 "GCP.AIPlatform.FeatureOnlineStoreStillExists",
174 )<{
175 resourceName: string;
176 }> {}
177
178 const resourceName = (project: string, location: string, storeId: string) =>
179 `projects/${project}/locations/${location}/featureOnlineStores/${storeId}`;
180
181 // Vertex no longer creates Optimized stores unless asked explicitly, so
182 // the default backend is the smallest Bigtable one.
183 const wantsOptimized = (news: FeatureOnlineStoreProps) =>
184 news.bigtable === undefined && news.optimized !== undefined && news.optimized !== false;
185
186 const DEFAULT_BIGTABLE: FeatureOnlineStoreBigtable = {
187 autoScaling: { minNodeCount: 1, maxNodeCount: 1 },
188 };
189
190 const bigtableOf = (news: FeatureOnlineStoreProps) => news.bigtable ?? DEFAULT_BIGTABLE;
191
192 const toBigtable = (
193 config:
194 | aiplatform.GoogleCloudAiplatformV1FeatureOnlineStoreBigtable
195 | FeatureOnlineStoreBigtable
196 | undefined,
197 ): FeatureOnlineStoreBigtable | undefined => {
198 if (config === undefined) return undefined;
199 return {
200 autoScaling: config.autoScaling
201 ? {
202 minNodeCount: config.autoScaling.minNodeCount,
203 maxNodeCount: config.autoScaling.maxNodeCount,
204 cpuUtilizationTarget: config.autoScaling.cpuUtilizationTarget,
205 }
206 : undefined,
207 enableDirectBigtableAccess: config.enableDirectBigtableAccess,
208 zone: config.zone,
209 };
210 };
211
212 const toAttrs = (store: aiplatform.GoogleCloudAiplatformV1FeatureOnlineStore, project: string) => {
213 const name = store.name ?? "";
214 const parsed = parseResourceName(name, "featureOnlineStores");
215 const endpoint = store.dedicatedServingEndpoint;
216 return {
217 name,
218 featureOnlineStoreId: parsed.id,
219 project: parsed.project || project,
220 location: parsed.location,
221 labels: userLabels(store.labels),
222 bigtable: toBigtable(store.bigtable),
223 optimized: store.optimized !== undefined,
224 state: store.state,
225 dedicatedServingEndpoint:
226 endpoint === undefined
227 ? undefined
228 : {
229 publicEndpointDomainName: endpoint.publicEndpointDomainName,
230 serviceAttachment: endpoint.serviceAttachment,
231 privateServiceConnectConfig: endpoint.privateServiceConnectConfig
232 ? {
233 enablePrivateServiceConnect:
234 endpoint.privateServiceConnectConfig.enablePrivateServiceConnect === true,
235 projectAllowlist: endpoint.privateServiceConnectConfig.projectAllowlist,
236 }
237 : undefined,
238 },
239 encryptionSpec: store.encryptionSpec?.kmsKeyName
240 ? { kmsKeyName: store.encryptionSpec.kmsKeyName }
241 : undefined,
242 createTime: store.createTime,
243 updateTime: store.updateTime,
244 etag: store.etag,
245 };
246 };
247
248 const getByName = (name: string) =>
249 aiplatform
250 .getProjectsLocationsFeatureOnlineStores({ name })
251 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
252
253 const waitUntilExists = (name: string) =>
254 getByName(name).pipe(
255 Effect.flatMap((store) =>
256 store
257 ? Effect.succeed(store)
258 : Effect.fail(new FeatureOnlineStoreNotResolved({ resourceName: name })),
259 ),
260 Effect.retry({
261 while: (error) => error._tag === "GCP.AIPlatform.FeatureOnlineStoreNotResolved",
262 times: 8,
263 schedule: Schedule.spaced("1 second"),
264 }),
265 );
266
267 const waitUntilGone = (name: string) =>
268 getByName(name).pipe(
269 Effect.flatMap((store) =>
270 store === undefined
271 ? Effect.void
272 : Effect.fail(new FeatureOnlineStoreStillExists({ resourceName: name })),
273 ),
274 Effect.retry({
275 while: (error) => error._tag === "GCP.AIPlatform.FeatureOnlineStoreStillExists",
276 times: 10,
277 schedule: Schedule.spaced("2 seconds"),
278 }),
279 );
280
281 const isReady = (state: string | undefined) => {
282 const value = (state ?? "").toUpperCase();
283 return value === "STABLE" || value === "";
284 };
285
286 const waitUntilReady = (name: string) =>
287 getByName(name).pipe(
288 Effect.filterOrFail(
289 (store): store is aiplatform.GoogleCloudAiplatformV1FeatureOnlineStore => store !== undefined,
290 () => new FeatureOnlineStoreNotResolved({ resourceName: name }),
291 ),
292 Effect.filterOrFail(
293 (store) => isReady(store.state),
294 () => new FeatureOnlineStoreNotResolved({ resourceName: name }),
295 ),
296 Effect.retry({
297 while: (error) => error._tag === "GCP.AIPlatform.FeatureOnlineStoreNotResolved",
298 times: 10,
299 schedule: Schedule.spaced("8 seconds"),
300 }),
301 );
302
303 export const FeatureOnlineStoreProvider = () =>
304 Provider.succeed(FeatureOnlineStore, {
305 stables: ["name", "featureOnlineStoreId", "project", "location", "createTime"],
306
307 diff: Effect.fn(function* ({ news, olds, output }) {
308 const env = yield* GcpEnvironment.current;
309 if (!isResolved(news)) return undefined;
310 const previousId = olds?.featureOnlineStoreId ?? output?.featureOnlineStoreId;
311 const nextId = news.featureOnlineStoreId ?? previousId;
312 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
313 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
314 const previousOptimized =
315 olds?.optimized !== undefined ? olds.optimized !== false : (output?.optimized ?? false);
316 const nextOptimized =
317 news.bigtable !== undefined
318 ? false
319 : news.optimized !== undefined
320 ? news.optimized !== false
321 : previousOptimized;
322 const previousZone = olds?.bigtable?.zone ?? output?.bigtable?.zone ?? "";
323 const nextZone = news.bigtable?.zone ?? previousZone;
324 const previousKey =
325 olds?.encryptionSpec?.kmsKeyName ?? output?.encryptionSpec?.kmsKeyName ?? "";
326 const nextKey = news.encryptionSpec?.kmsKeyName ?? previousKey;
327 const replace =
328 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
329 previousLocation !== nextLocation ||
330 previousOptimized !== nextOptimized ||
331 previousZone !== nextZone ||
332 previousKey !== nextKey;
333 if (!replace) return undefined;
334 return {
335 action: "replace" as const,
336 deleteFirst:
337 previousLocation === nextLocation && previousId !== undefined && nextId === previousId,
338 };
339 }),
340
341 read: Effect.fn(function* ({ id, olds, output }) {
342 const env = yield* GcpEnvironment.current;
343 const storeId = yield* toPhysicalSnake(
344 id,
345 olds?.featureOnlineStoreId,
346 output?.featureOnlineStoreId,
347 MAX_NAME_LENGTH,
348 );
349 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
350 const name = output?.name ?? resourceName(env.project, location, storeId);
351 const existing = yield* getByName(name);
352 if (existing === undefined) return undefined;
353 const attrs = toAttrs(existing, env.project);
354 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs);
355 }),
356
357 list: () =>
358 Effect.gen(function* () {
359 const env = yield* GcpEnvironment.current;
360 return yield* Stream.fromIterable(listLocations(env.region)).pipe(
361 Stream.flatMap((location) =>
362 aiplatform.listProjectsLocationsFeatureOnlineStores.pages({
363 parent: `projects/${env.project}/locations/${location}`,
364 pageSize: 100,
365 }),
366 ),
367 Stream.flatMap((page) => Stream.fromIterable(page.featureOnlineStores ?? [])),
368 Stream.filter((store) => hasAlchemyLabelMap(store.labels)),
369 Stream.map((store) => toAttrs(store, env.project)),
370 Stream.runCollect,
371 Effect.map((chunk) => Array.from(chunk)),
372 Effect.catchTag("NotFound", () => Effect.succeed([])),
373 );
374 }),
375
376 reconcile: Effect.fn(function* ({ id, news, output }) {
377 const env = yield* GcpEnvironment.current;
378 const storeId = yield* toPhysicalSnake(
379 id,
380 news.featureOnlineStoreId,
381 output?.featureOnlineStoreId,
382 MAX_NAME_LENGTH,
383 );
384 const location = normalizeLocation(news.location ?? output?.location, env.region);
385 const name = resourceName(env.project, location, storeId);
386 const desiredLabels = {
387 ...toLabels(news.labels),
388 ...(yield* createInternalLabels(id)),
389 };
390 const optimized = wantsOptimized(news);
391
392 let current = yield* getByName(output?.name ?? name);
393
394 if (current === undefined) {
395 const created = yield* aiplatform
396 .createProjectsLocationsFeatureOnlineStores({
397 parent: `projects/${env.project}/locations/${location}`,
398 featureOnlineStoreId: storeId,
399 body: {
400 labels: desiredLabels,
401 bigtable: optimized ? undefined : bigtableOf(news),
402 optimized: optimized ? {} : undefined,
403 dedicatedServingEndpoint: news.dedicatedServingEndpoint,
404 encryptionSpec: news.encryptionSpec,
405 },
406 })
407 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
408 if (created !== undefined) {
409 yield* waitForOperation(created);
410 }
411 current = yield* waitUntilExists(name);
412 }
413
414 if (current === undefined) {
415 return yield* new FeatureOnlineStoreNotResolved({ resourceName: name });
416 }
417
418 if (!isReady(current.state)) {
419 current = yield* waitUntilReady(name);
420 }
421
422 const observedLabels = tagRecord(current.labels);
423 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
424 const labelsChanged = upsert.length > 0 || removed.length > 0;
425 const scalingChanged =
426 news.bigtable?.autoScaling !== undefined &&
427 !specifiedEquals(news.bigtable.autoScaling, current.bigtable?.autoScaling);
428 const directChanged =
429 news.bigtable?.enableDirectBigtableAccess !== undefined &&
430 (current.bigtable?.enableDirectBigtableAccess === true) !==
431 (news.bigtable.enableDirectBigtableAccess === true);
432
433 if (labelsChanged || scalingChanged || directChanged) {
434 const updateMask = [
435 labelsChanged ? "labels" : undefined,
436 scalingChanged ? "bigtable.auto_scaling" : undefined,
437 directChanged ? "bigtable" : undefined,
438 ].filter((field): field is string => field !== undefined);
439
440 const patched = yield* aiplatform
441 .patchProjectsLocationsFeatureOnlineStores({
442 name,
443 updateMask: updateMask.join(","),
444 body: {
445 name,
446 labels: desiredLabels,
447 bigtable: news.bigtable,
448 },
449 })
450 .pipe(
451 Effect.retry({
452 while: (error) => error._tag === "Conflict",
453 times: 8,
454 schedule: Schedule.spaced("5 seconds"),
455 }),
456 );
457 yield* waitForOperation(patched);
458 current = yield* waitUntilReady(name);
459 }
460
461 return toAttrs(current, env.project);
462 }),
463
464 delete: Effect.fn(function* ({ output }) {
465 const operation = yield* aiplatform
466 .deleteProjectsLocationsFeatureOnlineStores({
467 name: output.name,
468 force: true,
469 })
470 .pipe(
471 Effect.catchTag("NotFound", () => Effect.undefined),
472 Effect.retry({
473 while: (error) => error._tag === "Conflict",
474 times: 8,
475 schedule: Schedule.spaced("5 seconds"),
476 }),
477 );
478 if (operation !== undefined) {
479 yield* waitForOperation(operation, { notFoundOk: true });
480 }
481 yield* waitUntilGone(output.name);
482 }),
483 });