@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 384 lines 13.2 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 * as Data from "effect/Data";
7 import * as Effect from "effect/Effect";
8 import * as Option from "effect/Option";
9 import * as Stream from "effect/Stream";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { createInternalLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13 import { listLocations } from "./names.ts";
14 import { waitForOperation } from "./operations.ts";
15 import {
16 encodeOwnership,
17 hasOwnershipMarker,
18 lastSegment,
19 locationOf,
20 locationParent,
21 ownedByAlchemy,
22 parentOf,
23 parseOwnership,
24 toResourceId,
25 } from "./ownership.ts";
26
27 export type TimeSeriesValueType = "SCALAR" | "TENSOR" | "BLOB_SEQUENCE" | (string & {});
28
29 export type TensorboardsExperimentsRunsTimeSeriesProps = {
30 /**
31 * Parent TensorboardRun resource name
32 * (`projects/{project}/locations/{location}/tensorboards/{tensorboard}/experiments/{experiment}/runs/{run}`).
33 * Immutable — changing it replaces the time series.
34 */
35 parent: string;
36 /**
37 * Time series id (the `{time_series}` segment, `[a-z0-9]{0,128}`). If
38 * omitted, Vertex AI assigns one. Immutable.
39 */
40 timeSeriesId?: string;
41 /**
42 * User-facing display name. Must be unique among time series of the
43 * parent run.
44 */
45 displayName?: string;
46 /**
47 * Human-readable description. TensorboardTimeSeries has no labels field,
48 * so Alchemy ownership is stored in a `[alchemy …]` prefix and stripped
49 * from attributes.
50 */
51 description?: string;
52 /**
53 * Value type. Immutable.
54 * @default "SCALAR"
55 */
56 valueType?: TimeSeriesValueType;
57 /**
58 * Plugin name this time series belongs to (`scalars`, `tensors`, …).
59 * Immutable.
60 */
61 pluginName?: string;
62 /**
63 * Plugin data, limited to 65KB.
64 */
65 pluginData?: string;
66 };
67
68 export type TensorboardsExperimentsRunsTimeSeries = Resource<
69 "GCP.AIPlatform.TensorboardsExperimentsRunsTimeSeries",
70 TensorboardsExperimentsRunsTimeSeriesProps,
71 {
72 /** Full resource name. */
73 name: string;
74 /** Time series id (last path segment). */
75 timeSeriesId: string;
76 /** Parent run resource name. */
77 parent: string;
78 /** Region id. */
79 location: string;
80 /** Project id. */
81 project: string;
82 /** User-facing display name. */
83 displayName: string | undefined;
84 /** User description with the Alchemy ownership prefix stripped. */
85 description: string | undefined;
86 /** Value type. */
87 valueType: string | undefined;
88 /** Plugin name. */
89 pluginName: string | undefined;
90 /** Plugin data. */
91 pluginData: string | undefined;
92 /** RFC3339 creation timestamp. */
93 createTime: string | undefined;
94 /** RFC3339 last-update timestamp. */
95 updateTime: string | undefined;
96 },
97 never,
98 Providers
99 >;
100
101 /**
102 * A Vertex AI TensorboardTimeSeries of scalar, tensor, or blob values.
103 *
104 * Time series have no labels field — Alchemy stamps ownership into the
105 * description so `read`, `list`, and `pnpm nuke:gcp` can find them.
106 * Parent run, id, value type, and plugin name are immutable.
107 *
108 * ### Creating a Time Series
109 * **Example:** Scalar series under a run
110 * ```typescript
111 * const series = yield* GCP.AIPlatform.TensorboardsExperimentsRunsTimeSeries(
112 * "Loss",
113 * {
114 * parent: run.name,
115 * displayName: "loss",
116 * valueType: "SCALAR",
117 * pluginName: "scalars",
118 * },
119 * );
120 * ```
121 *
122 * @resource
123 * @category AIPlatform
124 */
125 export const TensorboardsExperimentsRunsTimeSeries =
126 Resource<TensorboardsExperimentsRunsTimeSeries>(
127 "GCP.AIPlatform.TensorboardsExperimentsRunsTimeSeries",
128 );
129
130 /** Alias matching the factory catalog identifier. */
131 export const TensorboardsExperimentsRunsTimeSery = TensorboardsExperimentsRunsTimeSeries;
132
133 export class TensorboardsExperimentsRunsTimeSeriesNotResolved extends Data.TaggedError(
134 "GCP.AIPlatform.TensorboardsExperimentsRunsTimeSeriesNotResolved",
135 )<{
136 resourceName: string;
137 }> {}
138
139 const DEFAULT_VALUE_TYPE = "SCALAR";
140
141 // Time series ids allow only `[a-z0-9]`, unlike the dashed ids
142 // `toResourceId` generates.
143 const toTimeSeriesId = (id: string, requested: string | undefined, existing: string | undefined) =>
144 requested !== undefined || existing !== undefined
145 ? toResourceId(id, requested, existing)
146 : toResourceId(id, undefined, undefined).pipe(
147 Effect.map((generated) => generated.replace(/[^a-z0-9]/g, "")),
148 );
149
150 const resourceName = (parent: string, timeSeriesId: string) =>
151 `${parent}/timeSeries/${timeSeriesId}`;
152
153 const toAttrs = (
154 series: aiplatform.GoogleCloudAiplatformV1TensorboardTimeSeries,
155 project: string,
156 ) => {
157 const name = series.name ?? "";
158 const parsed = parseOwnership(series.description);
159 return {
160 name,
161 timeSeriesId: lastSegment(name),
162 parent: parentOf(name),
163 location: locationOf(name),
164 project,
165 displayName: series.displayName,
166 description: parsed.text,
167 valueType: series.valueType,
168 pluginName: series.pluginName,
169 pluginData: series.pluginData,
170 createTime: series.createTime,
171 updateTime: series.updateTime,
172 };
173 };
174
175 const getByName = (name: string) =>
176 name.length === 0
177 ? Effect.undefined
178 : aiplatform
179 .getProjectsLocationsTensorboardsExperimentsRunsTimeSeries({ name })
180 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
181
182 const listTensorboards = (project: string, location: string) =>
183 aiplatform.listProjectsLocationsTensorboards
184 .pages({
185 parent: locationParent(project, location),
186 pageSize: 100,
187 })
188 .pipe(
189 Stream.flatMap((page) => Stream.fromIterable(page.tensorboards ?? [])),
190 Stream.map((board) => board.name ?? ""),
191 Stream.filter((name) => name.length > 0),
192 Stream.runCollect,
193 Effect.map((chunk) => Array.from(chunk)),
194 Effect.catchTag("NotFound", () => Effect.succeed([] as string[])),
195 );
196
197 const listExperiments = (parent: string) =>
198 aiplatform.listProjectsLocationsTensorboardsExperiments.pages({ parent, pageSize: 100 }).pipe(
199 Stream.flatMap((page) => Stream.fromIterable(page.tensorboardExperiments ?? [])),
200 Stream.map((experiment) => experiment.name ?? ""),
201 Stream.filter((name) => name.length > 0),
202 Stream.runCollect,
203 Effect.map((chunk) => Array.from(chunk)),
204 Effect.catchTag("NotFound", () => Effect.succeed([] as string[])),
205 );
206
207 const listRuns = (parent: string) =>
208 aiplatform.listProjectsLocationsTensorboardsExperimentsRuns.pages({ parent, pageSize: 100 }).pipe(
209 Stream.flatMap((page) => Stream.fromIterable(page.tensorboardRuns ?? [])),
210 Stream.map((run) => run.name ?? ""),
211 Stream.filter((name) => name.length > 0),
212 Stream.runCollect,
213 Effect.map((chunk) => Array.from(chunk)),
214 Effect.catchTag("NotFound", () => Effect.succeed([] as string[])),
215 );
216
217 const listAtParent = (parent: string, project: string) =>
218 aiplatform.listProjectsLocationsTensorboardsExperimentsRunsTimeSeries
219 .pages({ parent, pageSize: 100 })
220 .pipe(
221 Stream.flatMap((page) => Stream.fromIterable(page.tensorboardTimeSeries ?? [])),
222 Stream.filter((series) => hasOwnershipMarker(series.description)),
223 Stream.map((series) => toAttrs(series, project)),
224 Stream.runCollect,
225 Effect.map((chunk) => Array.from(chunk)),
226 Effect.catchTag("NotFound", () => Effect.succeed([])),
227 );
228
229 const findOwned = (parent: string, id: string) =>
230 aiplatform.listProjectsLocationsTensorboardsExperimentsRunsTimeSeries
231 .pages({ parent, pageSize: 100 })
232 .pipe(
233 Stream.flatMap((page) => Stream.fromIterable(page.tensorboardTimeSeries ?? [])),
234 Stream.filterEffect((series) => ownedByAlchemy(id, series.description)),
235 Stream.runHead,
236 Effect.map(Option.getOrUndefined),
237 Effect.catchTag("NotFound", () => Effect.undefined),
238 );
239
240 export const TensorboardsExperimentsRunsTimeSeriesProvider = () =>
241 Provider.succeed(TensorboardsExperimentsRunsTimeSeries, {
242 stables: [
243 "name",
244 "timeSeriesId",
245 "parent",
246 "location",
247 "project",
248 "valueType",
249 "pluginName",
250 "createTime",
251 ],
252
253 diff: Effect.fn(function* ({ news, olds, output }) {
254 if (!isResolved(news)) return undefined;
255 const previousParent = olds?.parent ?? output?.parent;
256 if (previousParent !== undefined && news.parent !== previousParent) {
257 return { action: "replace" as const, deleteFirst: false };
258 }
259 const previousId = olds?.timeSeriesId ?? output?.timeSeriesId;
260 if (
261 previousId !== undefined &&
262 news.timeSeriesId !== undefined &&
263 news.timeSeriesId !== previousId
264 ) {
265 return { action: "replace" as const, deleteFirst: true };
266 }
267 const previousType = olds?.valueType ?? output?.valueType;
268 const nextType = news.valueType ?? DEFAULT_VALUE_TYPE;
269 if (previousType !== undefined && previousType !== nextType) {
270 return { action: "replace" as const, deleteFirst: true };
271 }
272 return undefined;
273 }),
274
275 read: Effect.fn(function* ({ id, olds, output, previous }) {
276 const env = yield* GcpEnvironment.current;
277 // Vertex AI rejects GETs for ids it did not assign ("Invalid
278 // TensorboardTimeSeries resource name"), so recover by listing.
279 const existing =
280 output?.name !== undefined
281 ? yield* getByName(output.name)
282 : typeof olds?.parent === "string"
283 ? yield* findOwned(olds.parent, id)
284 : undefined;
285 if (existing === undefined) return undefined;
286 const attrs = toAttrs(existing, env.project);
287 return (yield* ownedByAlchemy(id, existing.description))
288 ? attrs
289 : unownedUnlessPrevious(attrs, previous, "name");
290 }),
291
292 list: () =>
293 Effect.gen(function* () {
294 const env = yield* GcpEnvironment.current;
295 const boards = (yield* Effect.forEach(listLocations(env.region), (location) =>
296 listTensorboards(env.project, location),
297 )).flat();
298 const experiments = (yield* Effect.forEach(boards, listExperiments, {
299 concurrency: 4,
300 })).flat();
301 const runs = (yield* Effect.forEach(experiments, listRuns, {
302 concurrency: 4,
303 })).flat();
304 const pages = yield* Effect.forEach(runs, (parent) => listAtParent(parent, env.project), {
305 concurrency: 4,
306 });
307 return pages.flat();
308 }),
309
310 reconcile: Effect.fn(function* ({ id, news, output }) {
311 const env = yield* GcpEnvironment.current;
312 const timeSeriesId = yield* toTimeSeriesId(id, news.timeSeriesId, output?.timeSeriesId);
313 const name = resourceName(news.parent, timeSeriesId);
314 const ownership = yield* createInternalLabels(id);
315 const desiredDescription = encodeOwnership(ownership, news.description);
316 const displayName = news.displayName ?? timeSeriesId;
317 const valueType = news.valueType ?? DEFAULT_VALUE_TYPE;
318
319 let current =
320 (output?.name !== undefined ? yield* getByName(output.name) : undefined) ??
321 (yield* findOwned(news.parent, id));
322
323 if (current === undefined) {
324 const created = yield* aiplatform
325 .createProjectsLocationsTensorboardsExperimentsRunsTimeSeries({
326 parent: news.parent,
327 tensorboardTimeSeriesId: news.timeSeriesId,
328 body: {
329 displayName,
330 description: desiredDescription,
331 valueType,
332 pluginName: news.pluginName,
333 pluginData: news.pluginData,
334 },
335 })
336 .pipe(Effect.catchTag("Conflict", () => findOwned(news.parent, id)));
337 current = created ?? undefined;
338 }
339
340 if (current === undefined) {
341 return yield* new TensorboardsExperimentsRunsTimeSeriesNotResolved({
342 resourceName: name,
343 });
344 }
345
346 const displayChanged = (current.displayName ?? "") !== displayName;
347 const descriptionChanged = (current.description ?? "") !== desiredDescription;
348 const pluginDataChanged = (current.pluginData ?? "") !== (news.pluginData ?? "");
349
350 if (displayChanged || descriptionChanged || pluginDataChanged) {
351 current = yield* aiplatform.patchProjectsLocationsTensorboardsExperimentsRunsTimeSeries({
352 name,
353 updateMask: [
354 displayChanged ? "displayName" : undefined,
355 descriptionChanged ? "description" : undefined,
356 pluginDataChanged ? "pluginData" : undefined,
357 ]
358 .filter((field): field is string => field !== undefined)
359 .join(","),
360 body: {
361 name,
362 displayName,
363 description: desiredDescription,
364 pluginData: news.pluginData,
365 },
366 });
367 }
368
369 return toAttrs(current, env.project);
370 }),
371
372 delete: Effect.fn(function* ({ output }) {
373 const existing = yield* getByName(output.name);
374 if (existing === undefined) return;
375 const operation = yield* aiplatform
376 .deleteProjectsLocationsTensorboardsExperimentsRunsTimeSeries({
377 name: output.name,
378 })
379 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
380 if (operation !== undefined) {
381 yield* waitForOperation(operation, { notFoundOk: true });
382 }
383 }),
384 });