@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 329 lines 11.2 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 getModel,
16 lastSegment,
17 listOwnedModels,
18 listVersions,
19 modelOwnedByAlchemy,
20 modelResourceName,
21 normalizeRegions,
22 parseModelName,
23 parseOwnership,
24 replaceOnIdentity,
25 ResourceNotResolved,
26 sameStringList,
27 sameText,
28 toPhysicalId,
29 userLabels,
30 waitUntilExists,
31 waitUntilGone,
32 } from "./internal.ts";
33 import { waitForOperation } from "./operations.ts";
34
35 export type ModelProps = {
36 /**
37 * Model id (the `{model}` segment of `projects/{project}/models/{model}`).
38 * If omitted, a unique RFC1035 name is generated from the stack, stage,
39 * and logical id. Immutable — changing it replaces the model.
40 */
41 modelId?: string;
42 /**
43 * Human-readable description. Patch supports this field. Alchemy also
44 * stamps an `[alchemy …]` ownership prefix here (labels are not
45 * updatable on existing models) and strips it from attributes.
46 */
47 description?: string;
48 /**
49 * User labels. Alchemy ownership labels are merged in automatically
50 * on create. The patch API does not update labels.
51 */
52 labels?: Record<string, string>;
53 /**
54 * Regions where the model is deployed. Only one region is supported.
55 * Immutable — changing it replaces the model.
56 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
57 */
58 regions?: string[];
59 /**
60 * Send online-prediction access logs to Cloud Logging. Immutable —
61 * changing it replaces the model.
62 * @default false
63 */
64 onlinePredictionLogging?: boolean;
65 /**
66 * Send online-prediction stdout/stderr to Cloud Logging. Immutable —
67 * changing it replaces the model.
68 * @default false
69 */
70 onlinePredictionConsoleLogging?: boolean;
71 /**
72 * Default version id used when a predict request omits the version.
73 * Patchable as `defaultVersion.name`. The first created version becomes
74 * the default automatically.
75 */
76 defaultVersion?: string;
77 };
78
79 export type Model = Resource<
80 "GCP.ML.Model",
81 ModelProps,
82 {
83 /** Full resource name `projects/{project}/models/{model}`. */
84 name: string;
85 /** Model id (last path segment). */
86 modelId: string;
87 /** Project id. */
88 project: string;
89 /** User description with the Alchemy ownership prefix stripped. */
90 description: string | undefined;
91 /** User labels (Alchemy ownership labels stripped). */
92 labels: Record<string, string>;
93 /** Deployment regions. */
94 regions: string[];
95 /** Whether online-prediction access logs are enabled. */
96 onlinePredictionLogging: boolean;
97 /** Whether online-prediction console logs are enabled. */
98 onlinePredictionConsoleLogging: boolean;
99 /** Default version resource name, if any. */
100 defaultVersion: string | undefined;
101 /** Optimistic-concurrency etag. */
102 etag: string | undefined;
103 },
104 never,
105 Providers
106 >;
107
108 /**
109 * An AI Platform (legacy ML Engine) model — a named container for
110 * deployed {@link ModelsVersion}s.
111 *
112 * Labels are set at create time but are not patchable, so Alchemy also
113 * stamps ownership into `description` for `list` / nuke. Model id,
114 * regions, and logging flags are identity. Description and the default
115 * version update in place.
116 *
117 * The AI Platform Training and Prediction API (`ml.googleapis.com`)
118 * must be enabled. Create of a model is metadata-only; serving traffic
119 * requires at least one version.
120 *
121 * ### Creating a Model
122 * **Example:** Generated name
123 * ```typescript
124 * const model = yield* GCP.ML.Model("Classifier", {
125 * description: "image classifier",
126 * });
127 * ```
128 *
129 * **Example:** Explicit id, region, and labels
130 * ```typescript
131 * const model = yield* GCP.ML.Model("Classifier", {
132 * modelId: "image-classifier",
133 * regions: ["us-central1"],
134 * labels: { env: "prod" },
135 * onlinePredictionLogging: true,
136 * });
137 * ```
138 *
139 * ### Updating a Model
140 * **Example:** Change the description
141 * ```typescript
142 * const model = yield* GCP.ML.Model("Classifier", {
143 * description: "image classifier v2",
144 * });
145 * ```
146 *
147 * @resource
148 * @category ML
149 */
150 export const Model = Resource<Model>("GCP.ML.Model");
151
152 export { ResourceNotResolved as ModelNotResolved };
153
154 const toAttrs = (model: ml.GoogleCloudMlV1__Model, project: string) => {
155 const name = model.name ?? "";
156 const parsed = parseModelName(name, project);
157 return {
158 name,
159 modelId: parsed.modelId,
160 project: parsed.project || project,
161 description: parseOwnership(model.description).text,
162 labels: userLabels(model.labels),
163 regions: normalizeRegions(model.regions, []),
164 onlinePredictionLogging: model.onlinePredictionLogging === true,
165 onlinePredictionConsoleLogging: model.onlinePredictionConsoleLogging === true,
166 defaultVersion: model.defaultVersion?.name,
167 etag: model.etag,
168 };
169 };
170
171 const desiredDefaultVersion = (value: string | undefined) =>
172 value === undefined || value.length === 0 ? undefined : lastSegment(value);
173
174 const deleteVersions = (parent: string) =>
175 Effect.gen(function* () {
176 const versions = yield* listVersions(parent);
177 const ordered = [
178 ...versions.filter((version) => version.isDefault !== true),
179 ...versions.filter((version) => version.isDefault === true),
180 ];
181 yield* Effect.forEach(
182 ordered,
183 (version) =>
184 version.name
185 ? ml.deleteProjectsModelsVersions({ name: version.name }).pipe(
186 Effect.flatMap((operation) => waitForOperation(operation, { notFoundOk: true })),
187 Effect.catchTag("NotFound", () => Effect.void),
188 )
189 : Effect.void,
190 { concurrency: 1 },
191 );
192 });
193
194 export const ModelProvider = () =>
195 Provider.succeed(Model, {
196 stables: ["name", "modelId", "project"],
197
198 diff: Effect.fn(function* ({ news, olds, output }) {
199 if (!isResolved(news)) return undefined;
200 const env = yield* GcpEnvironment.current;
201 const previousRegions = normalizeRegions(olds?.regions ?? output?.regions, [env.region]);
202 const nextRegions = normalizeRegions(news.regions ?? olds?.regions ?? output?.regions, [
203 env.region,
204 ]);
205 const previousLogging =
206 olds?.onlinePredictionLogging ?? output?.onlinePredictionLogging ?? false;
207 const nextLogging = news.onlinePredictionLogging ?? previousLogging;
208 const previousConsole =
209 olds?.onlinePredictionConsoleLogging ?? output?.onlinePredictionConsoleLogging ?? false;
210 const nextConsole = news.onlinePredictionConsoleLogging ?? previousConsole;
211 return replaceOnIdentity({
212 previousId: olds?.modelId ?? output?.modelId,
213 nextId: news.modelId ?? olds?.modelId ?? output?.modelId,
214 extra:
215 !sameStringList(previousRegions, nextRegions) ||
216 previousLogging !== nextLogging ||
217 previousConsole !== nextConsole,
218 });
219 }),
220
221 read: Effect.fn(function* ({ id, olds, output }) {
222 const env = yield* GcpEnvironment.current;
223 const modelId = yield* toPhysicalId(id, olds?.modelId, output?.modelId, "model");
224 const name = output?.name ?? modelResourceName(env.project, modelId);
225 const existing = yield* getModel(name);
226 if (existing === undefined) return undefined;
227 const attrs = toAttrs(existing, env.project);
228 return (yield* modelOwnedByAlchemy(id, existing)) ? attrs : Unowned(attrs);
229 }),
230
231 list: () =>
232 Effect.gen(function* () {
233 const env = yield* GcpEnvironment.current;
234 const models = yield* listOwnedModels(env.project);
235 return models.map((model) => toAttrs(model, env.project));
236 }),
237
238 reconcile: Effect.fn(function* ({ id, news, output }) {
239 const env = yield* GcpEnvironment.current;
240 const modelId = yield* toPhysicalId(id, news.modelId, output?.modelId, "model");
241 const name = modelResourceName(env.project, modelId);
242 const ownership = yield* createInternalLabels(id);
243 const description = encodeOwnership(ownership, news.description);
244 const desiredLabels = {
245 ...toLabels(news.labels),
246 ...ownership,
247 };
248 const regions = normalizeRegions(news.regions ?? output?.regions, [env.region]);
249 const onlinePredictionLogging = news.onlinePredictionLogging === true;
250 const onlinePredictionConsoleLogging = news.onlinePredictionConsoleLogging === true;
251 const defaultVersion = desiredDefaultVersion(news.defaultVersion);
252
253 let current = yield* getModel(output?.name ?? name);
254
255 if (current === undefined) {
256 yield* ml
257 .createProjectsModels({
258 parent: `projects/${env.project}`,
259 body: {
260 name: modelId,
261 description,
262 labels: desiredLabels,
263 regions,
264 onlinePredictionLogging,
265 onlinePredictionConsoleLogging,
266 },
267 })
268 .pipe(Effect.catchTag("Conflict", () => Effect.void));
269 current = yield* waitUntilExists(getModel(name), name);
270 }
271
272 if (current === undefined) {
273 return yield* new ResourceNotResolved({ resourceName: name });
274 }
275
276 const currentName = current.name ?? name;
277 const descriptionChanged = !sameText(current.description, description);
278 const observedDefault = desiredDefaultVersion(current.defaultVersion?.name);
279 const defaultChanged = defaultVersion !== undefined && observedDefault !== defaultVersion;
280 const mask = fieldMask([
281 descriptionChanged && "description",
282 defaultChanged && "default_version.name",
283 ]);
284
285 if (mask.length > 0) {
286 const operation = yield* ml.patchProjectsModels({
287 name: currentName,
288 updateMask: mask,
289 body: {
290 name: currentName,
291 description,
292 defaultVersion:
293 defaultVersion !== undefined
294 ? {
295 name: defaultVersion.includes("/") ? defaultVersion : defaultVersion,
296 }
297 : undefined,
298 },
299 });
300 yield* waitForOperation(operation);
301 current = yield* waitUntilExists(getModel(currentName), currentName);
302 }
303
304 return toAttrs(current, env.project);
305 }),
306
307 delete: Effect.fn(function* ({ output }) {
308 const name = output.name;
309 if (!name) return;
310 yield* Effect.gen(function* () {
311 yield* deleteVersions(name);
312 const operation = yield* ml
313 .deleteProjectsModels({ name })
314 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
315 if (operation !== undefined) {
316 yield* waitForOperation(operation, { notFoundOk: true });
317 }
318 }).pipe(
319 Effect.retry({
320 while: (error) => error._tag === "Conflict" || error._tag === "BadRequest",
321 times: 8,
322 schedule: Schedule.spaced("2 seconds"),
323 }),
324 );
325 yield* waitUntilGone(getModel(name), name);
326 }),
327 });
328
329 export const modelNameOf = (project: string, model: string) => expandModel(project, model);