@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, 04:45 UTC

main
ts 427 lines 15 KB
Raw
1 import * as ml from "@distilled.cloud/gcp/ml_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 Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import { GcpEnvironment } from "../Environment.ts";
9 import { createInternalLabels, toLabels } from "../Labels.ts";
10 import type { Providers } from "../Providers.ts";
11 import {
12 encodeOwnership,
13 expandModel,
14 fieldMask,
15 getVersion,
16 listOwnedVersions,
17 parseOwnership,
18 parseVersionName,
19 replaceOnIdentity,
20 ResourceNotResolved,
21 sameText,
22 toPhysicalId,
23 userLabels,
24 versionOwnedByAlchemy,
25 versionResourceName,
26 waitUntilExists,
27 waitUntilGone,
28 waitUntilVersionReady,
29 } from "./internal.ts";
30 import { waitForOperation } from "./operations.ts";
31
32 export type AutoScaling = ml.GoogleCloudMlV1__AutoScaling;
33 export type ManualScaling = ml.GoogleCloudMlV1__ManualScaling;
34 export type ContainerSpec = ml.GoogleCloudMlV1__ContainerSpec;
35 export type RequestLoggingConfig = ml.GoogleCloudMlV1__RequestLoggingConfig;
36 export type AcceleratorConfig = ml.GoogleCloudMlV1__AcceleratorConfig;
37 export type ExplanationConfig = ml.GoogleCloudMlV1__ExplanationConfig;
38 export type RouteMap = ml.GoogleCloudMlV1__RouteMap;
39 export type VersionFramework = ml.GoogleCloudMlV1__VersionFrameworkEnum | (string & {});
40
41 export type ModelsVersionProps = {
42 /**
43 * Parent model. Full name `projects/{project}/models/{model}` or the
44 * model id. Immutable — changing it replaces the version.
45 */
46 model: string;
47 /**
48 * Version id (the `{version}` segment of
49 * `projects/{project}/models/{model}/versions/{version}`). If omitted,
50 * a unique RFC1035 name is generated. Immutable — changing it
51 * replaces the version.
52 */
53 versionId?: string;
54 /**
55 * Cloud Storage URI of a directory containing trained model artifacts
56 * (`gs://bucket/path`). Required unless `container` is set. Immutable.
57 */
58 deploymentUri?: string;
59 /**
60 * AI Platform runtime version (for example `"2.11"`). Immutable.
61 */
62 runtimeVersion?: string;
63 /**
64 * Python version used in prediction (`"3.7"`, `"3.5"`, `"2.7"`).
65 * Immutable.
66 */
67 pythonVersion?: string;
68 /**
69 * Framework: `TENSORFLOW`, `SCIKIT_LEARN`, or `XGBOOST`. Immutable.
70 */
71 framework?: VersionFramework;
72 /**
73 * Machine type for online prediction (for example `mls1-c1-m2` or
74 * `n1-standard-2`). Immutable.
75 */
76 machineType?: string;
77 /**
78 * Human-readable description. Patchable. Alchemy stamps an
79 * `[alchemy …]` ownership prefix here and strips it from attributes.
80 */
81 description?: string;
82 /**
83 * User labels. Merged with Alchemy ownership labels on create. The
84 * patch API does not update labels.
85 */
86 labels?: Record<string, string>;
87 /**
88 * Automatic node scaling. `minNodes` is patchable.
89 */
90 autoScaling?: AutoScaling;
91 /**
92 * Fixed node count. `nodes` is patchable on Compute Engine machine
93 * types.
94 */
95 manualScaling?: ManualScaling;
96 /**
97 * Custom serving container. Immutable. When set, `deploymentUri` is
98 * optional and `runtimeVersion` must be omitted.
99 */
100 container?: ContainerSpec;
101 /**
102 * Custom prediction-routine packages (`gs://…`). Immutable.
103 */
104 packageUris?: string[];
105 /**
106 * Fully qualified Predictor class (`module.Class`). Immutable.
107 */
108 predictionClass?: string;
109 /**
110 * Service account for resource access control. Immutable.
111 */
112 serviceAccount?: string;
113 /**
114 * GPU accelerator for N1 machine types. Immutable.
115 */
116 acceleratorConfig?: AcceleratorConfig;
117 /**
118 * Explainability configuration. Immutable.
119 */
120 explanationConfig?: ExplanationConfig;
121 /**
122 * Custom container HTTP routes. Immutable.
123 */
124 routes?: RouteMap;
125 /**
126 * Request-response pair logging to BigQuery. Patchable.
127 */
128 requestLoggingConfig?: RequestLoggingConfig;
129 /**
130 * When true, this version handles predict requests that omit a
131 * version. The first version of a model is default automatically;
132 * later versions call `versions.setDefault`.
133 */
134 isDefault?: boolean;
135 };
136
137 export type ModelsVersion = Resource<
138 "GCP.ML.ModelsVersion",
139 ModelsVersionProps,
140 {
141 /** Full resource name `projects/{project}/models/{model}/versions/{version}`. */
142 name: string;
143 /** Version id (last path segment). */
144 versionId: string;
145 /** Parent model resource name. */
146 model: string;
147 /** Parent model id. */
148 modelId: string;
149 /** Project id. */
150 project: string;
151 /** User description with the Alchemy ownership prefix stripped. */
152 description: string | undefined;
153 /** User labels (Alchemy ownership labels stripped). */
154 labels: Record<string, string>;
155 /** Model-artifact Cloud Storage URI. */
156 deploymentUri: string | undefined;
157 /** Runtime version. */
158 runtimeVersion: string | undefined;
159 /** Python version. */
160 pythonVersion: string | undefined;
161 /** Framework. */
162 framework: string | undefined;
163 /** Serving machine type. */
164 machineType: string | undefined;
165 /** Automatic scaling, if set. */
166 autoScaling: AutoScaling | undefined;
167 /** Manual scaling, if set. */
168 manualScaling: ManualScaling | undefined;
169 /** Whether this is the model's default version. */
170 isDefault: boolean;
171 /** Version state (`READY`, `CREATING`, `FAILED`, …). */
172 state: string | undefined;
173 /** Failure details, if the version failed. */
174 errorMessage: string | undefined;
175 /** RFC3339 creation timestamp. */
176 createTime: string | undefined;
177 },
178 never,
179 Providers
180 >;
181
182 /**
183 * A deployed version of an AI Platform (legacy ML Engine)
184 * {@link Model}. Each version serves online and batch predictions from
185 * a Cloud Storage SavedModel (or a custom container).
186 *
187 * Version id, parent model, `deploymentUri`, runtime, framework, and
188 * machine type are identity. Description, `autoScaling.minNodes`,
189 * `manualScaling.nodes`, and `requestLoggingConfig` update in place.
190 * Create and delete are long-running operations.
191 *
192 * ### Creating a ModelsVersion
193 * **Example:** TensorFlow SavedModel
194 * ```typescript
195 * const model = yield* GCP.ML.Model("Classifier", {});
196 * const version = yield* GCP.ML.ModelsVersion("V1", {
197 * model: model.name,
198 * deploymentUri: "gs://my-bucket/saved-model",
199 * runtimeVersion: "2.11",
200 * pythonVersion: "3.7",
201 * framework: "TENSORFLOW",
202 * });
203 * ```
204 *
205 * **Example:** Explicit version id and labels
206 * ```typescript
207 * const version = yield* GCP.ML.ModelsVersion("V1", {
208 * model: model.name,
209 * versionId: "v1",
210 * deploymentUri: "gs://my-bucket/saved-model",
211 * labels: { env: "prod" },
212 * autoScaling: { minNodes: 0 },
213 * });
214 * ```
215 *
216 * ### Updating a ModelsVersion
217 * **Example:** Raise min nodes
218 * ```typescript
219 * const version = yield* GCP.ML.ModelsVersion("V1", {
220 * model: model.name,
221 * autoScaling: { minNodes: 1 },
222 * });
223 * ```
224 *
225 * @resource
226 * @category ML
227 */
228 export const ModelsVersion = Resource<ModelsVersion>("GCP.ML.ModelsVersion");
229
230 export { ResourceNotResolved as ModelsVersionNotResolved };
231
232 const toAttrs = (version: ml.GoogleCloudMlV1__Version, project: string) => {
233 const name = version.name ?? "";
234 const parsed = parseVersionName(name, project);
235 return {
236 name,
237 versionId: parsed.versionId,
238 model: parsed.model,
239 modelId: parsed.modelId,
240 project: parsed.project || project,
241 description: parseOwnership(version.description).text,
242 labels: userLabels(version.labels),
243 deploymentUri: version.deploymentUri,
244 runtimeVersion: version.runtimeVersion,
245 pythonVersion: version.pythonVersion,
246 framework: version.framework,
247 machineType: version.machineType,
248 autoScaling: version.autoScaling,
249 manualScaling: version.manualScaling,
250 isDefault: version.isDefault === true,
251 state: version.state,
252 errorMessage: version.errorMessage,
253 createTime: version.createTime,
254 };
255 };
256
257 const sameMinNodes = (left: AutoScaling | undefined, right: AutoScaling | undefined) =>
258 (left?.minNodes ?? undefined) === (right?.minNodes ?? undefined);
259
260 const sameManualNodes = (left: ManualScaling | undefined, right: ManualScaling | undefined) =>
261 (left?.nodes ?? undefined) === (right?.nodes ?? undefined);
262
263 const sameLogging = (
264 left: RequestLoggingConfig | undefined,
265 right: RequestLoggingConfig | undefined,
266 ) =>
267 (left?.bigqueryTableName ?? "") === (right?.bigqueryTableName ?? "") &&
268 (left?.samplingPercentage ?? undefined) === (right?.samplingPercentage ?? undefined);
269
270 export const ModelsVersionProvider = () =>
271 Provider.succeed(ModelsVersion, {
272 stables: ["name", "versionId", "model", "modelId", "project", "createTime"],
273
274 diff: Effect.fn(function* ({ news, olds, output }) {
275 if (!isResolved(news)) return undefined;
276 const env = yield* GcpEnvironment.current;
277 const previousParent = expandModel(env.project, olds?.model ?? output?.model ?? "");
278 const nextParent = expandModel(env.project, news.model);
279 return replaceOnIdentity({
280 previousId: olds?.versionId ?? output?.versionId,
281 nextId: news.versionId ?? olds?.versionId ?? output?.versionId,
282 previousParent,
283 nextParent,
284 extra:
285 (news.deploymentUri !== undefined &&
286 (olds?.deploymentUri ?? output?.deploymentUri) !== undefined &&
287 news.deploymentUri !== (olds?.deploymentUri ?? output?.deploymentUri)) ||
288 (news.runtimeVersion !== undefined &&
289 (olds?.runtimeVersion ?? output?.runtimeVersion) !== undefined &&
290 news.runtimeVersion !== (olds?.runtimeVersion ?? output?.runtimeVersion)) ||
291 (news.pythonVersion !== undefined &&
292 (olds?.pythonVersion ?? output?.pythonVersion) !== undefined &&
293 news.pythonVersion !== (olds?.pythonVersion ?? output?.pythonVersion)) ||
294 (news.framework !== undefined &&
295 (olds?.framework ?? output?.framework) !== undefined &&
296 news.framework !== (olds?.framework ?? output?.framework)) ||
297 (news.machineType !== undefined &&
298 (olds?.machineType ?? output?.machineType) !== undefined &&
299 news.machineType !== (olds?.machineType ?? output?.machineType)),
300 });
301 }),
302
303 read: Effect.fn(function* ({ id, olds, output }) {
304 const env = yield* GcpEnvironment.current;
305 const versionId = yield* toPhysicalId(id, olds?.versionId, output?.versionId, "version");
306 const model = expandModel(env.project, olds?.model ?? output?.model ?? "");
307 const name = output?.name ?? (model.length > 0 ? versionResourceName(model, versionId) : "");
308 const existing = yield* getVersion(name);
309 if (existing === undefined) return undefined;
310 const attrs = toAttrs(existing, env.project);
311 return (yield* versionOwnedByAlchemy(id, existing)) ? attrs : Unowned(attrs);
312 }),
313
314 list: () =>
315 Effect.gen(function* () {
316 const env = yield* GcpEnvironment.current;
317 const versions = yield* listOwnedVersions(env.project);
318 return versions.map((version) => toAttrs(version, env.project));
319 }),
320
321 reconcile: Effect.fn(function* ({ id, news, output }) {
322 const env = yield* GcpEnvironment.current;
323 const versionId = yield* toPhysicalId(id, news.versionId, output?.versionId, "version");
324 const model = expandModel(env.project, news.model);
325 const name = versionResourceName(model, versionId);
326 const ownership = yield* createInternalLabels(id);
327 const description = encodeOwnership(ownership, news.description);
328 const desiredLabels = {
329 ...toLabels(news.labels),
330 ...ownership,
331 };
332
333 let current = yield* getVersion(output?.name ?? name);
334
335 if (current === undefined) {
336 const created = yield* ml
337 .createProjectsModelsVersions({
338 parent: model,
339 body: {
340 name: versionId,
341 description,
342 labels: desiredLabels,
343 deploymentUri: news.deploymentUri,
344 runtimeVersion: news.runtimeVersion,
345 pythonVersion: news.pythonVersion,
346 framework: news.framework,
347 machineType: news.machineType,
348 autoScaling: news.autoScaling,
349 manualScaling: news.manualScaling,
350 container: news.container,
351 packageUris: news.packageUris,
352 predictionClass: news.predictionClass,
353 serviceAccount: news.serviceAccount,
354 acceleratorConfig: news.acceleratorConfig,
355 explanationConfig: news.explanationConfig,
356 routes: news.routes,
357 requestLoggingConfig: news.requestLoggingConfig,
358 },
359 })
360 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
361 if (created !== undefined) {
362 yield* waitForOperation(created);
363 }
364 current = yield* waitUntilVersionReady(name);
365 }
366
367 if (current === undefined) {
368 return yield* new ResourceNotResolved({ resourceName: name });
369 }
370
371 const currentName = current.name ?? name;
372 const descriptionChanged = !sameText(current.description, description);
373 const minNodesChanged = !sameMinNodes(current.autoScaling, news.autoScaling);
374 const manualNodesChanged = !sameManualNodes(current.manualScaling, news.manualScaling);
375 const loggingChanged =
376 news.requestLoggingConfig !== undefined &&
377 !sameLogging(current.requestLoggingConfig, news.requestLoggingConfig);
378 const mask = fieldMask([
379 descriptionChanged && "description",
380 minNodesChanged && news.autoScaling !== undefined && "autoScaling.minNodes",
381 manualNodesChanged && news.manualScaling !== undefined && "manualScaling.nodes",
382 loggingChanged && "requestLoggingConfig",
383 ]);
384
385 if (mask.length > 0) {
386 const operation = yield* ml.patchProjectsModelsVersions({
387 name: currentName,
388 updateMask: mask,
389 body: {
390 name: currentName,
391 description,
392 autoScaling: news.autoScaling,
393 manualScaling: news.manualScaling,
394 requestLoggingConfig: news.requestLoggingConfig,
395 },
396 });
397 yield* waitForOperation(operation);
398 current = yield* waitUntilExists(getVersion(currentName), currentName);
399 }
400
401 if (news.isDefault === true && current.isDefault !== true) {
402 current = yield* ml.setDefaultProjectsModelsVersions({
403 name: currentName,
404 body: {},
405 });
406 }
407
408 return toAttrs(current, env.project);
409 }),
410
411 delete: Effect.fn(function* ({ output }) {
412 const name = output.name;
413 if (!name) return;
414 const operation = yield* ml.deleteProjectsModelsVersions({ name }).pipe(
415 Effect.retry({
416 while: (error) => error._tag === "Conflict",
417 times: 8,
418 schedule: Schedule.spaced("2 seconds"),
419 }),
420 Effect.catchTag("NotFound", () => Effect.undefined),
421 );
422 if (operation !== undefined) {
423 yield* waitForOperation(operation, { notFoundOk: true });
424 }
425 yield* waitUntilGone(getVersion(name), name);
426 }),
427 });