@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 374 lines 12.7 KB
Raw
1 import * as aiplatform from "@distilled.cloud/gcp/aiplatform_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 { tagRecord } from "alchemy/Tags";
7 import * as Data from "effect/Data";
8 import * as Effect from "effect/Effect";
9 import * as Schedule from "effect/Schedule";
10 import { GcpEnvironment } from "../Environment.ts";
11 import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts";
12 import type { Providers } from "../Providers.ts";
13 import { listLocations } from "./names.ts";
14 import { resourceNameFromOperation, waitForOperation } from "./operations.ts";
15 import {
16 AiPlatformNotResolved,
17 AiPlatformStillExists,
18 collectPages,
19 locationParent,
20 normalizeLocation,
21 parseResourceName,
22 toPhysicalId,
23 userLabels,
24 type EncryptionSpec,
25 type MachineSpec,
26 type NetworkSpec,
27 type PersistentDiskSpec,
28 } from "./shared.ts";
29
30 const COLLECTION = "notebookExecutionJobs";
31
32 export type DirectNotebookSource = {
33 /** Base64-encoded contents of the input notebook. */
34 content?: string;
35 };
36
37 export type GcsNotebookSource = {
38 /** Cloud Storage URI of the ipynb (`gs://bucket/notebook.ipynb`). */
39 uri?: string;
40 /** Object generation to read. Unset reads the live object. */
41 generation?: string;
42 };
43
44 export type DataformRepositorySource = {
45 /** Dataform repository resource name. */
46 dataformRepositoryResourceName?: string;
47 /** Commit SHA. Unset reads HEAD. */
48 commitSha?: string;
49 };
50
51 export type CustomEnvironmentSpec = {
52 /** Machine spec for the execution job. */
53 machineSpec?: MachineSpec;
54 /** Network spec. */
55 networkSpec?: NetworkSpec;
56 /** Persistent disk attached to the job. */
57 persistentDiskSpec?: PersistentDiskSpec;
58 };
59
60 export type NotebookExecutionJobProps = {
61 /**
62 * Job id. If omitted, a unique RFC1035 name is generated. Immutable —
63 * changing it replaces the job (a new execution is started).
64 */
65 notebookExecutionJobId?: string;
66 /**
67 * Vertex AI location. Immutable — changing it replaces the job.
68 * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`)
69 */
70 location?: string;
71 /**
72 * Display name (max 128 UTF-8 characters). Defaults to the job id.
73 */
74 displayName?: string;
75 /**
76 * User labels. Alchemy ownership labels are merged in automatically.
77 */
78 labels?: Record<string, string>;
79 /**
80 * NotebookRuntimeTemplate to source compute from.
81 */
82 notebookRuntimeTemplateResourceName?: string;
83 /**
84 * Custom compute configuration. Mutually exclusive with a template.
85 */
86 customEnvironmentSpec?: CustomEnvironmentSpec;
87 /**
88 * Inline notebook contents. Mutually exclusive with GCS / Dataform.
89 */
90 directNotebookSource?: DirectNotebookSource;
91 /**
92 * Cloud Storage notebook URI.
93 */
94 gcsNotebookSource?: GcsNotebookSource;
95 /**
96 * Dataform repository notebook source.
97 */
98 dataformRepositorySource?: DataformRepositorySource;
99 /**
100 * Cloud Storage location for results (`gs://bucket-name`).
101 */
102 gcsOutputUri?: string;
103 /**
104 * Service account the execution runs as.
105 */
106 serviceAccount?: string;
107 /**
108 * User email to run as (Colab runtimes only).
109 */
110 executionUser?: string;
111 /**
112 * Kernel name. Unset uses the default kernel.
113 */
114 kernelName?: string;
115 /**
116 * Max running time (e.g. `"86400s"`).
117 */
118 executionTimeout?: string;
119 /**
120 * Customer-managed encryption key.
121 */
122 encryptionSpec?: EncryptionSpec;
123 };
124
125 export type NotebookExecutionJob = Resource<
126 "GCP.AIPlatform.NotebookExecutionJob",
127 NotebookExecutionJobProps,
128 {
129 /** Full resource name. */
130 name: string;
131 /** Job id (last path segment). */
132 notebookExecutionJobId: string;
133 /** Project id. */
134 project: string;
135 /** Location id. */
136 location: string;
137 /** Display name. */
138 displayName: string | undefined;
139 /** User labels (Alchemy ownership labels stripped). */
140 labels: Record<string, string>;
141 /** Job state (`JOB_STATE_RUNNING`, `JOB_STATE_SUCCEEDED`, …). */
142 jobState: string | undefined;
143 /** Cloud Storage output URI. */
144 gcsOutputUri: string | undefined;
145 /** NotebookRuntimeTemplate resource name, if used. */
146 notebookRuntimeTemplateResourceName: string | undefined;
147 /** Service account. */
148 serviceAccount: string | undefined;
149 /** RFC3339 creation timestamp. */
150 createTime: string | undefined;
151 /** RFC3339 last-update timestamp. */
152 updateTime: string | undefined;
153 },
154 never,
155 Providers
156 >;
157
158 /**
159 * A Vertex AI notebook execution job — runs an ipynb once against a
160 * runtime template or custom environment.
161 *
162 * There is no update API. Changing identity (`notebookExecutionJobId`,
163 * `location`) or the notebook source replaces the job. Other fields are
164 * create-time only.
165 *
166 * ### Creating a Job
167 * **Example:** Execute a GCS notebook
168 * ```typescript
169 * const job = yield* GCP.AIPlatform.NotebookExecutionJob("Nightly", {
170 * notebookRuntimeTemplateResourceName: template.name,
171 * gcsNotebookSource: { uri: "gs://bucket/notebook.ipynb" },
172 * gcsOutputUri: "gs://bucket/output",
173 * });
174 * ```
175 *
176 * @resource
177 * @category AIPlatform
178 */
179 export const NotebookExecutionJob = Resource<NotebookExecutionJob>(
180 "GCP.AIPlatform.NotebookExecutionJob",
181 );
182
183 export class NotebookExecutionJobNotResolved extends Data.TaggedError(
184 "GCP.AIPlatform.NotebookExecutionJobNotResolved",
185 )<{
186 resourceName: string;
187 }> {}
188
189 const resourceName = (project: string, location: string, id: string) =>
190 `${locationParent(project, location)}/${COLLECTION}/${id}`;
191
192 const toAttrs = (job: aiplatform.GoogleCloudAiplatformV1NotebookExecutionJob, project: string) => {
193 const name = job.name ?? "";
194 const parsed = parseResourceName(name, COLLECTION);
195 return {
196 name,
197 notebookExecutionJobId: parsed.id,
198 project: parsed.project || project,
199 location: parsed.location,
200 displayName: job.displayName,
201 labels: userLabels(job.labels),
202 jobState: job.jobState,
203 gcsOutputUri: job.gcsOutputUri,
204 notebookRuntimeTemplateResourceName: job.notebookRuntimeTemplateResourceName,
205 serviceAccount: job.serviceAccount,
206 createTime: job.createTime,
207 updateTime: job.updateTime,
208 };
209 };
210
211 const getByName = (name: string) =>
212 aiplatform
213 .getProjectsLocationsNotebookExecutionJobs({ name })
214 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
215
216 const waitUntilExists = (name: string) =>
217 getByName(name).pipe(
218 Effect.filterOrFail(
219 (job): job is aiplatform.GoogleCloudAiplatformV1NotebookExecutionJob => job !== undefined,
220 () => new AiPlatformNotResolved({ resourceName: name }),
221 ),
222 Effect.retry({
223 while: (error) => error._tag === "GCP.AIPlatform.NotResolved",
224 times: 8,
225 schedule: Schedule.spaced("2 seconds"),
226 }),
227 );
228
229 const waitUntilGone = (name: string) =>
230 getByName(name).pipe(
231 Effect.filterOrFail(
232 (job) => job === undefined,
233 () => new AiPlatformStillExists({ resourceName: name }),
234 ),
235 Effect.asVoid,
236 Effect.retry({
237 while: (error) => error._tag === "GCP.AIPlatform.StillExists",
238 times: 10,
239 schedule: Schedule.spaced("2 seconds"),
240 }),
241 );
242
243 export const NotebookExecutionJobProvider = () =>
244 Provider.succeed(NotebookExecutionJob, {
245 stables: ["name", "notebookExecutionJobId", "project", "location", "createTime"],
246
247 diff: Effect.fn(function* ({ news, olds, output }) {
248 const env = yield* GcpEnvironment.current;
249 if (!isResolved(news)) return undefined;
250 const previousId = olds?.notebookExecutionJobId ?? output?.notebookExecutionJobId;
251 const nextId = news.notebookExecutionJobId ?? previousId;
252 const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region);
253 const nextLocation = normalizeLocation(news.location ?? output?.location, env.region);
254 const sourceChanged =
255 (news.gcsNotebookSource?.uri ?? "") !== (olds?.gcsNotebookSource?.uri ?? "") ||
256 (news.directNotebookSource?.content ?? "") !==
257 (olds?.directNotebookSource?.content ?? "") ||
258 (news.dataformRepositorySource?.dataformRepositoryResourceName ?? "") !==
259 (olds?.dataformRepositorySource?.dataformRepositoryResourceName ?? "");
260 const replace =
261 (previousId !== undefined && nextId !== undefined && nextId !== previousId) ||
262 previousLocation !== nextLocation ||
263 (olds !== undefined && sourceChanged);
264 if (!replace) return undefined;
265 return {
266 action: "replace" as const,
267 deleteFirst:
268 previousLocation === nextLocation && previousId !== undefined && nextId === previousId,
269 };
270 }),
271
272 read: Effect.fn(function* ({ id, olds, output }) {
273 const env = yield* GcpEnvironment.current;
274 const jobId = yield* toPhysicalId(
275 id,
276 olds?.notebookExecutionJobId,
277 output?.notebookExecutionJobId,
278 );
279 const location = normalizeLocation(olds?.location ?? output?.location, env.region);
280 const name = output?.name ?? resourceName(env.project, location, jobId);
281 const existing = yield* getByName(name);
282 if (existing === undefined) return undefined;
283 const attrs = toAttrs(existing, env.project);
284 // Vertex AI replaces a job's labels with its runtime template's, so a
285 // job at the generated (instance-unique) id is ours even when the
286 // ownership labels name the template.
287 const generated = olds?.notebookExecutionJobId === undefined;
288 return generated || (yield* hasAlchemyLabels(id, tagRecord(existing.labels)))
289 ? attrs
290 : Unowned(attrs);
291 }),
292
293 list: () =>
294 Effect.gen(function* () {
295 const env = yield* GcpEnvironment.current;
296 const pages = (yield* Effect.forEach(listLocations(env.region), (location) =>
297 collectPages(
298 aiplatform.listProjectsLocationsNotebookExecutionJobs.pages({
299 parent: locationParent(env.project, location),
300 pageSize: 100,
301 }),
302 ).pipe(Effect.catchTag("NotFound", () => Effect.succeed([]))),
303 )).flat();
304 return pages.flatMap((page) =>
305 (page.notebookExecutionJobs ?? [])
306 .filter((job) =>
307 Object.keys(job.labels ?? {}).some((key) => key.startsWith("alchemy-")),
308 )
309 .map((job) => toAttrs(job, env.project)),
310 );
311 }),
312
313 reconcile: Effect.fn(function* ({ id, news, output }) {
314 const env = yield* GcpEnvironment.current;
315 const jobId = yield* toPhysicalId(
316 id,
317 news.notebookExecutionJobId,
318 output?.notebookExecutionJobId,
319 );
320 const location = normalizeLocation(news.location ?? output?.location, env.region);
321 const name = resourceName(env.project, location, jobId);
322 const desiredLabels = {
323 ...toLabels(news.labels),
324 ...(yield* createInternalLabels(id)),
325 };
326
327 let current = yield* getByName(output?.name ?? name);
328
329 if (current === undefined) {
330 const created = yield* aiplatform
331 .createProjectsLocationsNotebookExecutionJobs({
332 parent: locationParent(env.project, location),
333 notebookExecutionJobId: jobId,
334 body: {
335 displayName: news.displayName ?? jobId,
336 labels: desiredLabels,
337 notebookRuntimeTemplateResourceName: news.notebookRuntimeTemplateResourceName,
338 customEnvironmentSpec: news.customEnvironmentSpec,
339 directNotebookSource: news.directNotebookSource,
340 gcsNotebookSource: news.gcsNotebookSource,
341 dataformRepositorySource: news.dataformRepositorySource,
342 gcsOutputUri: news.gcsOutputUri,
343 serviceAccount: news.serviceAccount,
344 executionUser: news.executionUser,
345 kernelName: news.kernelName,
346 executionTimeout: news.executionTimeout,
347 encryptionSpec: news.encryptionSpec,
348 },
349 })
350 .pipe(Effect.catchTag("Conflict", () => Effect.undefined));
351 if (created !== undefined) {
352 yield* waitForOperation(created, { alreadyExistsOk: true });
353 }
354 const createdName = resourceNameFromOperation(created ?? {}) ?? output?.name ?? name;
355 current = yield* waitUntilExists(createdName);
356 }
357
358 if (current === undefined) {
359 return yield* new NotebookExecutionJobNotResolved({ resourceName: name });
360 }
361
362 return toAttrs(current, env.project);
363 }),
364
365 delete: Effect.fn(function* ({ output }) {
366 const operation = yield* aiplatform
367 .deleteProjectsLocationsNotebookExecutionJobs({ name: output.name })
368 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
369 if (operation !== undefined) {
370 yield* waitForOperation(operation, { notFoundOk: true });
371 }
372 yield* waitUntilGone(output.name);
373 }),
374 });