@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 291 lines 9.75 KB
Raw
1 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_v1";
2 import { unownedUnlessPrevious } 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 Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import {
12 createInternalLabels,
13 diffLabels,
14 hasAlchemyLabels,
15 stripInternalLabels,
16 toLabels,
17 } from "../Labels.ts";
18 import type { Providers } from "../Providers.ts";
19 import { listLocations } from "./names.ts";
20 import { waitForOperation } from "./operations.ts";
21 import { lastSegment, locationOf, locationParent, parentOf, toResourceId } from "./ownership.ts";
22
23 export type TensorboardsExperimentProps = {
24 /**
25 * Parent Tensorboard resource name
26 * (`projects/{project}/locations/{location}/tensorboards/{tensorboard}`).
27 * Immutable — changing it replaces the experiment.
28 */
29 parent: string;
30 /**
31 * Experiment id (the `{experiment}` segment). If omitted, a unique id is
32 * generated from the stack, stage, and logical id. Immutable.
33 */
34 experimentId?: string;
35 /**
36 * User-facing display name.
37 */
38 displayName?: string;
39 /**
40 * Human-readable description.
41 */
42 description?: string;
43 /**
44 * User labels. Alchemy ownership labels are merged in automatically.
45 */
46 labels?: Record<string, string>;
47 /**
48 * Immutable source of the experiment (for example a custom training job).
49 */
50 source?: string;
51 };
52
53 export type TensorboardsExperiment = Resource<
54 "GCP.AIPlatform.TensorboardsExperiment",
55 TensorboardsExperimentProps,
56 {
57 /** Full resource name. */
58 name: string;
59 /** Experiment id (last path segment). */
60 experimentId: string;
61 /** Parent Tensorboard resource name. */
62 parent: string;
63 /** Region id. */
64 location: string;
65 /** Project id. */
66 project: string;
67 /** User-facing display name. */
68 displayName: string | undefined;
69 /** Description. */
70 description: string | undefined;
71 /** User labels (Alchemy ownership labels stripped). */
72 labels: Record<string, string>;
73 /** Immutable source, if set. */
74 source: string | undefined;
75 /** RFC3339 creation timestamp. */
76 createTime: string | undefined;
77 /** RFC3339 last-update timestamp. */
78 updateTime: string | undefined;
79 },
80 never,
81 Providers
82 >;
83
84 /**
85 * A Vertex AI TensorboardExperiment grouping runs from a training job.
86 *
87 * Parent Tensorboard, experiment id, and source are immutable. Display
88 * name, description, and labels update in place.
89 *
90 * ### Creating an Experiment
91 * **Example:** Experiment under a Tensorboard
92 * ```typescript
93 * const experiment = yield* GCP.AIPlatform.TensorboardsExperiment("RunGroup", {
94 * parent: board.name,
95 * displayName: "baseline",
96 * });
97 * ```
98 *
99 * @resource
100 * @category AIPlatform
101 */
102 export const TensorboardsExperiment = Resource<TensorboardsExperiment>(
103 "GCP.AIPlatform.TensorboardsExperiment",
104 );
105
106 export class TensorboardsExperimentNotResolved extends Data.TaggedError(
107 "GCP.AIPlatform.TensorboardsExperimentNotResolved",
108 )<{
109 resourceName: string;
110 }> {}
111
112 const userLabels = (
113 labels: Record<string, string | undefined> | null | undefined,
114 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
115
116 const resourceName = (parent: string, experimentId: string) =>
117 `${parent}/experiments/${experimentId}`;
118
119 const toAttrs = (
120 experiment: aiplatform.GoogleCloudAiplatformV1TensorboardExperiment,
121 project: string,
122 ) => {
123 const name = experiment.name ?? "";
124 return {
125 name,
126 experimentId: lastSegment(name),
127 parent: parentOf(name),
128 location: locationOf(name),
129 project,
130 displayName: experiment.displayName,
131 description: experiment.description,
132 labels: userLabels(experiment.labels),
133 source: experiment.source,
134 createTime: experiment.createTime,
135 updateTime: experiment.updateTime,
136 };
137 };
138
139 const getByName = (name: string) =>
140 name.length === 0
141 ? Effect.undefined
142 : aiplatform
143 .getProjectsLocationsTensorboardsExperiments({ name })
144 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
145
146 const listParents = (project: string, location: string) =>
147 aiplatform.listProjectsLocationsTensorboards
148 .pages({
149 parent: locationParent(project, location),
150 pageSize: 100,
151 })
152 .pipe(
153 Stream.flatMap((page) => Stream.fromIterable(page.tensorboards ?? [])),
154 Stream.map((board) => board.name ?? ""),
155 Stream.filter((name) => name.length > 0),
156 Stream.runCollect,
157 Effect.map((chunk) => Array.from(chunk)),
158 Effect.catchTag("NotFound", () => Effect.succeed([] as string[])),
159 );
160
161 const listAtParent = (parent: string, project: string) =>
162 aiplatform.listProjectsLocationsTensorboardsExperiments.pages({ parent, pageSize: 100 }).pipe(
163 Stream.flatMap((page) => Stream.fromIterable(page.tensorboardExperiments ?? [])),
164 Stream.filter((experiment) =>
165 Object.keys(experiment.labels ?? {}).some((key) => key.startsWith("alchemy-")),
166 ),
167 Stream.map((experiment) => toAttrs(experiment, project)),
168 Stream.runCollect,
169 Effect.map((chunk) => Array.from(chunk)),
170 Effect.catchTag("NotFound", () => Effect.succeed([])),
171 );
172
173 export const TensorboardsExperimentProvider = () =>
174 Provider.succeed(TensorboardsExperiment, {
175 stables: ["name", "experimentId", "parent", "location", "project", "createTime"],
176
177 diff: Effect.fn(function* ({ news, olds, output }) {
178 if (!isResolved(news)) return undefined;
179 const previousParent = olds?.parent ?? output?.parent;
180 if (previousParent !== undefined && news.parent !== previousParent) {
181 return { action: "replace" as const, deleteFirst: false };
182 }
183 const previousId = olds?.experimentId ?? output?.experimentId;
184 if (
185 previousId !== undefined &&
186 news.experimentId !== undefined &&
187 news.experimentId !== previousId
188 ) {
189 return { action: "replace" as const, deleteFirst: true };
190 }
191 return undefined;
192 }),
193
194 read: Effect.fn(function* ({ id, olds, output, previous }) {
195 const env = yield* GcpEnvironment.current;
196 const experimentId = yield* toResourceId(id, olds?.experimentId, output?.experimentId);
197 const name =
198 output?.name ?? (olds?.parent !== undefined ? resourceName(olds.parent, experimentId) : "");
199 const existing = yield* getByName(name);
200 if (existing === undefined) return undefined;
201 const attrs = toAttrs(existing, env.project);
202 return (yield* hasAlchemyLabels(id, tagRecord(existing.labels)))
203 ? attrs
204 : unownedUnlessPrevious(attrs, previous, "name");
205 }),
206
207 list: () =>
208 Effect.gen(function* () {
209 const env = yield* GcpEnvironment.current;
210 const parents = (yield* Effect.forEach(listLocations(env.region), (location) =>
211 listParents(env.project, location),
212 )).flat();
213 const pages = yield* Effect.forEach(
214 parents,
215 (parent) => listAtParent(parent, env.project),
216 { concurrency: 4 },
217 );
218 return pages.flat();
219 }),
220
221 reconcile: Effect.fn(function* ({ id, news, output }) {
222 const env = yield* GcpEnvironment.current;
223 const experimentId = yield* toResourceId(id, news.experimentId, output?.experimentId);
224 const name = resourceName(news.parent, experimentId);
225 const desiredLabels = {
226 ...toLabels(news.labels),
227 ...(yield* createInternalLabels(id)),
228 };
229 const displayName = news.displayName ?? experimentId;
230
231 let current = yield* getByName(output?.name ?? name);
232
233 if (current === undefined) {
234 const created = yield* aiplatform
235 .createProjectsLocationsTensorboardsExperiments({
236 parent: news.parent,
237 tensorboardExperimentId: experimentId,
238 body: {
239 displayName,
240 description: news.description,
241 labels: desiredLabels,
242 source: news.source,
243 },
244 })
245 .pipe(Effect.catchTag("Conflict", () => getByName(name)));
246 current = created ?? undefined;
247 }
248
249 if (current === undefined) {
250 return yield* new TensorboardsExperimentNotResolved({ resourceName: name });
251 }
252
253 const observedLabels = tagRecord(current.labels);
254 const { upsert, removed } = diffLabels(observedLabels, desiredLabels);
255 const labelsChanged = upsert.length > 0 || removed.length > 0;
256 const displayChanged = (current.displayName ?? "") !== displayName;
257 const descriptionChanged = (current.description ?? "") !== (news.description ?? "");
258
259 if (labelsChanged || displayChanged || descriptionChanged) {
260 current = yield* aiplatform.patchProjectsLocationsTensorboardsExperiments({
261 name,
262 updateMask: [
263 labelsChanged ? "labels" : undefined,
264 displayChanged ? "displayName" : undefined,
265 descriptionChanged ? "description" : undefined,
266 ]
267 .filter((field): field is string => field !== undefined)
268 .join(","),
269 body: {
270 name,
271 displayName,
272 description: news.description,
273 labels: desiredLabels,
274 },
275 });
276 }
277
278 return toAttrs(current, env.project);
279 }),
280
281 delete: Effect.fn(function* ({ output }) {
282 const existing = yield* getByName(output.name);
283 if (existing === undefined) return;
284 const operation = yield* aiplatform
285 .deleteProjectsLocationsTensorboardsExperiments({ name: output.name })
286 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
287 if (operation !== undefined) {
288 yield* waitForOperation(operation, { notFoundOk: true });
289 }
290 }),
291 });