The best way to control Claude Code, Codex and more from any computer or phone

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