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

main
ts 405 lines 13.5 KB
Raw
1 import * as ml from "@distilled.cloud/gcp/ml_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import { tagRecord } from "alchemy/Tags";
4 import * as Data from "effect/Data";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7 import * as Stream from "effect/Stream";
8 import {
9 alchemyLabelKeys,
10 ALCHEMY_LABEL_PREFIX,
11 createInternalLabels,
12 hasAlchemyLabels,
13 stripInternalLabels,
14 } from "../Labels.ts";
15
16 export const MAX_NAME_LENGTH = 63;
17 export const MAX_DESCRIPTION_LENGTH = 8000;
18
19 export class ResourceNotResolved extends Data.TaggedError("GCP.ML.ResourceNotResolved")<{
20 resourceName: string;
21 }> {}
22
23 export class ResourceStillExists extends Data.TaggedError("GCP.ML.ResourceStillExists")<{
24 resourceName: string;
25 }> {}
26
27 export class VersionNotReady extends Data.TaggedError("GCP.ML.VersionNotReady")<{
28 resourceName: string;
29 state: string;
30 }> {}
31
32 export class VersionFailed extends Data.TaggedError("GCP.ML.VersionFailed")<{
33 resourceName: string;
34 message: string;
35 }> {}
36
37 export const lastSegment = (value: string) => {
38 const trimmed = value.replace(/\/+$/, "");
39 const parts = trimmed.split("/");
40 return parts[parts.length - 1] || trimmed;
41 };
42
43 export const parentOf = (name: string) => {
44 const parts = name.split("/").filter((part) => part.length > 0);
45 return parts.slice(0, -2).join("/");
46 };
47
48 export const rfc1035 = (name: string, fallback: string): string => {
49 let next = name
50 .toLowerCase()
51 .replace(/[^a-z0-9-]/g, "-")
52 .replace(/-+/g, "-")
53 .replace(/^-+|-+$/g, "");
54 if (!/^[a-z]/.test(next)) next = `m${next}`;
55 next = next.slice(0, MAX_NAME_LENGTH).replace(/-+$/g, "");
56 if (next.length === 0) return fallback;
57 if (!/[a-z0-9]$/.test(next)) next = `${next.slice(0, MAX_NAME_LENGTH - 1)}0`;
58 return next.slice(0, MAX_NAME_LENGTH);
59 };
60
61 export const toPhysicalId = (
62 id: string,
63 explicit: string | undefined,
64 existing: string | undefined,
65 fallback: string,
66 ) =>
67 Effect.gen(function* () {
68 if (explicit !== undefined && explicit.length > 0) {
69 return rfc1035(lastSegment(explicit), fallback);
70 }
71 if (existing !== undefined && existing.length > 0) {
72 return lastSegment(existing);
73 }
74 return rfc1035(
75 yield* createPhysicalName({
76 id,
77 maxLength: MAX_NAME_LENGTH,
78 lowercase: true,
79 }),
80 fallback,
81 );
82 });
83
84 export const projectParent = (project: string) => `projects/${project}`;
85
86 export const modelResourceName = (project: string, modelId: string) =>
87 `${projectParent(project)}/models/${modelId}`;
88
89 export const versionResourceName = (modelName: string, versionId: string) =>
90 `${modelName}/versions/${versionId}`;
91
92 export const expandModel = (project: string, value: string) => {
93 const next = value.replace(/\/+$/, "");
94 if (next.includes("/models/")) return next;
95 return modelResourceName(project, lastSegment(next));
96 };
97
98 export const parseModelName = (name: string, fallbackProject = "") => {
99 const parts = name.split("/").filter((part) => part.length > 0);
100 const modelsAt = parts.lastIndexOf("models");
101 const projectsAt = parts.lastIndexOf("projects");
102 return {
103 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : fallbackProject,
104 modelId: modelsAt >= 0 && parts[modelsAt + 1] ? parts[modelsAt + 1]! : lastSegment(name),
105 parent: modelsAt > 0 ? parts.slice(0, modelsAt).join("/") : projectParent(""),
106 };
107 };
108
109 export const parseVersionName = (name: string, fallbackProject = "") => {
110 const parts = name.split("/").filter((part) => part.length > 0);
111 const versionsAt = parts.lastIndexOf("versions");
112 const parsed = parseModelName(name, fallbackProject);
113 return {
114 ...parsed,
115 versionId:
116 versionsAt >= 0 && parts[versionsAt + 1] ? parts[versionsAt + 1]! : lastSegment(name),
117 model:
118 versionsAt > 0
119 ? parts.slice(0, versionsAt).join("/")
120 : modelResourceName(parsed.project, parsed.modelId),
121 };
122 };
123
124 export const userLabels = (
125 labels: Record<string, string | undefined> | null | undefined,
126 ): Record<string, string> => stripInternalLabels(tagRecord(labels));
127
128 export const hasAlchemyLabelMap = (labels: Record<string, string | undefined> | null | undefined) =>
129 Object.keys(labels ?? {}).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX));
130
131 export const sameText = (left: string | undefined, right: string | undefined) =>
132 (left ?? "") === (right ?? "");
133
134 export const sameStringList = (
135 left: readonly string[] | undefined,
136 right: readonly string[] | undefined,
137 ) =>
138 JSON.stringify([...(left ?? [])].map((value) => value.toLowerCase()).sort()) ===
139 JSON.stringify([...(right ?? [])].map((value) => value.toLowerCase()).sort());
140
141 export const normalizeRegions = (
142 regions: readonly string[] | undefined,
143 fallback: readonly string[],
144 ): string[] => {
145 const next = [...(regions ?? [])]
146 .map((region) => lastSegment(region).toLowerCase())
147 .filter((region) => region.length > 0);
148 return next.length > 0 ? next : [...fallback];
149 };
150
151 export const fieldMask = (fields: Array<string | false | undefined>) =>
152 fields.filter((field): field is string => typeof field === "string").join(",");
153
154 const markerOf = (labels: Record<string, string>, stack: string, stage: string, id: string) =>
155 `[alchemy ${alchemyLabelKeys.stack}=${stack} ${alchemyLabelKeys.stage}=${stage} ${alchemyLabelKeys.id}=${id}]`;
156
157 const fitMarker = (labels: Record<string, string>, maxLength: number) => {
158 let stack = labels[alchemyLabelKeys.stack] ?? "x";
159 let stage = labels[alchemyLabelKeys.stage] ?? "x";
160 let id = labels[alchemyLabelKeys.id] ?? "x";
161 let marker = markerOf(labels, stack, stage, id);
162 while (marker.length > maxLength && (stack.length > 1 || stage.length > 1 || id.length > 1)) {
163 if (stack.length >= stage.length && stack.length >= id.length) {
164 stack = stack.slice(0, -1);
165 } else if (stage.length >= id.length) {
166 stage = stage.slice(0, -1);
167 } else {
168 id = id.slice(0, -1);
169 }
170 marker = markerOf(labels, stack, stage, id);
171 }
172 return marker.length <= maxLength ? marker : `${marker.slice(0, Math.max(0, maxLength - 1))}]`;
173 };
174
175 export const encodeOwnership = (
176 labels: Record<string, string>,
177 text: string | undefined,
178 maxLength = MAX_DESCRIPTION_LENGTH,
179 ): string => {
180 const marker = fitMarker(labels, maxLength);
181 const trimmed = text?.trim();
182 if (!trimmed) return marker;
183 return `${marker}\n${trimmed}`.slice(0, maxLength);
184 };
185
186 export const parseOwnership = (
187 text: string | undefined,
188 ): {
189 labels: Record<string, string>;
190 text: string | undefined;
191 } => {
192 if (!text?.startsWith("[alchemy ")) {
193 return { labels: {}, text };
194 }
195 const end = text.indexOf("]");
196 if (end < 0) return { labels: {}, text };
197 const labels: Record<string, string> = {};
198 for (const part of text.slice("[alchemy ".length, end).split(/\s+/)) {
199 const eq = part.indexOf("=");
200 if (eq > 0) {
201 labels[part.slice(0, eq)] = part.slice(eq + 1);
202 }
203 }
204 const rest = text.slice(end + 1).replace(/^[\s\n]+/, "");
205 return { labels, text: rest.length > 0 ? rest : undefined };
206 };
207
208 export const hasOwnershipMarker = (text: string | undefined) =>
209 Object.keys(parseOwnership(text).labels).some((key) => key.startsWith(ALCHEMY_LABEL_PREFIX));
210
211 const prefixMatch = (expected: string, observed: string) =>
212 expected === observed || expected.startsWith(observed) || observed.startsWith(expected);
213
214 export const ownedByAlchemy = (id: string, text: string | undefined) =>
215 Effect.gen(function* () {
216 const expected = yield* createInternalLabels(id);
217 const { labels } = parseOwnership(text);
218 if (!hasOwnershipMarker(text)) return false;
219 const exact = yield* hasAlchemyLabels(id, labels);
220 if (exact) return true;
221 return (
222 prefixMatch(expected[alchemyLabelKeys.stack] ?? "", labels[alchemyLabelKeys.stack] ?? "") &&
223 prefixMatch(expected[alchemyLabelKeys.stage] ?? "", labels[alchemyLabelKeys.stage] ?? "") &&
224 prefixMatch(expected[alchemyLabelKeys.id] ?? "", labels[alchemyLabelKeys.id] ?? "")
225 );
226 });
227
228 export const modelOwnedByAlchemy = (id: string, model: ml.GoogleCloudMlV1__Model) =>
229 Effect.gen(function* () {
230 if (yield* hasAlchemyLabels(id, tagRecord(model.labels))) return true;
231 return yield* ownedByAlchemy(id, model.description);
232 });
233
234 export const versionOwnedByAlchemy = (id: string, version: ml.GoogleCloudMlV1__Version) =>
235 Effect.gen(function* () {
236 if (yield* hasAlchemyLabels(id, tagRecord(version.labels))) return true;
237 return yield* ownedByAlchemy(id, version.description);
238 });
239
240 export const modelHasOwnership = (model: ml.GoogleCloudMlV1__Model) =>
241 hasAlchemyLabelMap(model.labels) || hasOwnershipMarker(model.description);
242
243 export const versionHasOwnership = (version: ml.GoogleCloudMlV1__Version) =>
244 hasAlchemyLabelMap(version.labels) || hasOwnershipMarker(version.description);
245
246 export const replaceOnIdentity = (input: {
247 previousId?: string;
248 nextId?: string;
249 previousParent?: string;
250 nextParent?: string;
251 extra?: boolean;
252 }) => {
253 const idChanged =
254 input.previousId !== undefined &&
255 input.nextId !== undefined &&
256 input.previousId !== input.nextId;
257 const parentChanged =
258 (input.previousParent ?? "") !== "" &&
259 (input.nextParent ?? "") !== "" &&
260 (input.previousParent ?? "") !== (input.nextParent ?? "");
261 if (!idChanged && !parentChanged && input.extra !== true) {
262 return undefined;
263 }
264 return {
265 action: "replace" as const,
266 deleteFirst:
267 !idChanged &&
268 !parentChanged &&
269 input.extra === true &&
270 input.previousId !== undefined &&
271 input.nextId === input.previousId,
272 };
273 };
274
275 export const getModel = (name: string) =>
276 name.length === 0
277 ? Effect.undefined
278 : ml.getProjectsModels({ name }).pipe(Effect.catchTag("NotFound", () => Effect.undefined));
279
280 export const getVersion = (name: string) =>
281 name.length === 0
282 ? Effect.undefined
283 : ml
284 .getProjectsModelsVersions({ name })
285 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
286
287 const emptyList = <A>() => Effect.succeed([] as A[]);
288
289 export const listModels = (project: string) =>
290 ml.listProjectsModels
291 .pages({
292 parent: projectParent(project),
293 pageSize: 100,
294 })
295 .pipe(
296 Stream.runCollect,
297 Effect.map((pages) => Array.from(pages).flatMap((page) => page.models ?? [])),
298 Effect.catchTag("NotFound", () => emptyList<ml.GoogleCloudMlV1__Model>()),
299 );
300
301 export const listVersions = (parent: string) =>
302 parent.length === 0
303 ? emptyList<ml.GoogleCloudMlV1__Version>()
304 : ml.listProjectsModelsVersions
305 .pages({
306 parent,
307 pageSize: 100,
308 })
309 .pipe(
310 Stream.runCollect,
311 Effect.map((pages) => Array.from(pages).flatMap((page) => page.versions ?? [])),
312 Effect.catchTag("NotFound", () => emptyList<ml.GoogleCloudMlV1__Version>()),
313 );
314
315 export const listOwnedModels = (project: string) =>
316 listModels(project).pipe(Effect.map((models) => models.filter(modelHasOwnership)));
317
318 export const listOwnedVersions = (project: string) =>
319 Effect.gen(function* () {
320 const models = yield* listOwnedModels(project);
321 const pages = yield* Effect.forEach(
322 models,
323 (model) => (model.name ? listVersions(model.name) : emptyList<ml.GoogleCloudMlV1__Version>()),
324 { concurrency: 4 },
325 );
326 return pages.flat().filter((version) => versionHasOwnership(version));
327 });
328
329 type Tagged = { readonly _tag: string };
330
331 const versionState = (version: ml.GoogleCloudMlV1__Version) => (version.state ?? "").toUpperCase();
332
333 export const waitUntilGone = <A, R>(
334 get: Effect.Effect<A, Tagged, R>,
335 name: string,
336 ): Effect.Effect<void, Tagged, R> =>
337 get.pipe(
338 Effect.filterOrFail(
339 (value) => value === undefined,
340 () => new ResourceStillExists({ resourceName: name }),
341 ),
342 Effect.retry({
343 while: (error) => error._tag === "GCP.ML.ResourceStillExists",
344 times: 10,
345 schedule: Schedule.spaced("2 seconds"),
346 }),
347 Effect.asVoid,
348 );
349
350 export const waitUntilExists = <A, R>(
351 get: Effect.Effect<A, Tagged, R>,
352 name: string,
353 ): Effect.Effect<Exclude<A, undefined>, Tagged, R> =>
354 get.pipe(
355 Effect.filterOrFail(
356 (value): value is Exclude<A, undefined> => value !== undefined,
357 () => new ResourceNotResolved({ resourceName: name }),
358 ),
359 Effect.retry({
360 while: (error) => error._tag === "GCP.ML.ResourceNotResolved",
361 times: 8,
362 schedule: Schedule.spaced("1 second"),
363 }),
364 );
365
366 export const waitUntilVersionReady = (
367 name: string,
368 ): Effect.Effect<
369 ml.GoogleCloudMlV1__Version,
370 ml.Forbidden | ml.GcpOpError | ResourceNotResolved | VersionFailed | VersionNotReady,
371 ml.GcpOpContext
372 > =>
373 getVersion(name).pipe(
374 Effect.filterOrFail(
375 (version): version is ml.GoogleCloudMlV1__Version => version !== undefined,
376 () => new ResourceNotResolved({ resourceName: name }),
377 ),
378 Effect.filterOrFail(
379 (version) => {
380 const state = versionState(version);
381 return state === "" || state === "READY" || state === "FAILED";
382 },
383 (version) =>
384 new VersionNotReady({
385 resourceName: name,
386 state: version.state ?? "",
387 }),
388 ),
389 Effect.flatMap((version): Effect.Effect<ml.GoogleCloudMlV1__Version, VersionFailed> =>
390 versionState(version) === "FAILED"
391 ? Effect.fail(
392 new VersionFailed({
393 resourceName: name,
394 message: version.errorMessage ?? "version failed",
395 }),
396 )
397 : Effect.succeed(version),
398 ),
399 Effect.retry({
400 while: (error) =>
401 error._tag === "GCP.ML.ResourceNotResolved" || error._tag === "GCP.ML.VersionNotReady",
402 times: 10,
403 schedule: Schedule.spaced("5 seconds"),
404 }),
405 );