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

main
ts 254 lines 8.46 KB
Raw
1 import * as datapipelines from "@distilled.cloud/gcp/datapipelines_v1";
2 import { createPhysicalName } from "alchemy/PhysicalName";
3 import { tagRecord } from "alchemy/Tags";
4 import * as Effect from "effect/Effect";
5 import * as Stream from "effect/Stream";
6 import { alchemyLabelKeys, hasAlchemyLabels, stripInternalLabels } from "../Labels.ts";
7
8 export const DEFAULT_TYPE = "PIPELINE_TYPE_BATCH";
9 export const DEFAULT_STATE = "STATE_ACTIVE";
10 export const MAX_ID_LENGTH = 63;
11 export const MAX_DISPLAY_NAME_LENGTH = 64;
12 // Data Pipelines rejects the `-` location wildcard on list with a bare
13 // INVALID_ARGUMENT, so enumerate regions explicitly.
14 export const LIST_LOCATIONS = [
15 "us-central1",
16 "us-east1",
17 "us-west1",
18 "europe-west1",
19 "asia-east1",
20 ] as const;
21
22 export const lastSegment = (value: string) => {
23 const trimmed = value.replace(/\/+$/, "");
24 const parts = trimmed.split("/");
25 return parts[parts.length - 1] || trimmed;
26 };
27
28 export const sameText = (left: string | undefined, right: string | undefined) =>
29 (left ?? "") === (right ?? "");
30
31 // Key-order-insensitive: the API echoes maps (e.g. launch `parameters`) in
32 // its own order, which would otherwise read as a workload change.
33 const canonical = (value: unknown): unknown =>
34 Array.isArray(value)
35 ? value.map(canonical)
36 : value !== null && typeof value === "object"
37 ? Object.fromEntries(
38 Object.entries(value)
39 .filter(([, entry]) => entry !== undefined)
40 .sort(([a], [b]) => a.localeCompare(b))
41 .map(([key, entry]) => [key, canonical(entry)]),
42 )
43 : value;
44
45 export const jsonEqual = (left: unknown, right: unknown) =>
46 JSON.stringify(canonical(left ?? null)) === JSON.stringify(canonical(right ?? null));
47
48 export const updateMaskOf = (...fields: Array<string | undefined>) =>
49 fields.filter((field): field is string => field !== undefined).join(",");
50
51 export const normalizeLocation = (location: string | undefined, fallback: string) =>
52 lastSegment(location ?? fallback).toLowerCase();
53
54 export const locationParent = (project: string, location: string) =>
55 `projects/${project}/locations/${location}`;
56
57 export const resourceName = (project: string, location: string, pipelineId: string) =>
58 `${locationParent(project, location)}/pipelines/${pipelineId}`;
59
60 export const parseName = (name: string, fallbackLocation: string) => {
61 const parts = name.split("/").filter((part) => part.length > 0);
62 const projectsAt = parts.lastIndexOf("projects");
63 const locationsAt = parts.lastIndexOf("locations");
64 const pipelinesAt = parts.lastIndexOf("pipelines");
65 return {
66 project: projectsAt >= 0 && parts[projectsAt + 1] ? parts[projectsAt + 1]! : "",
67 location:
68 locationsAt >= 0 && parts[locationsAt + 1] ? parts[locationsAt + 1]! : fallbackLocation,
69 pipelineId:
70 pipelinesAt >= 0 && parts[pipelinesAt + 1] ? parts[pipelinesAt + 1]! : lastSegment(name),
71 parent:
72 pipelinesAt > 0
73 ? parts.slice(0, pipelinesAt).join("/")
74 : parts.slice(0, Math.max(0, parts.length - 1)).join("/"),
75 };
76 };
77
78 export const rfc1035 = (name: string, maxLength = MAX_ID_LENGTH, fallback = "pipeline"): string => {
79 let next = name
80 .toLowerCase()
81 .replace(/[^a-z0-9-]/g, "-")
82 .replace(/-+/g, "-")
83 .replace(/^-+|-+$/g, "");
84 if (!/^[a-z]/.test(next)) next = `p${next}`;
85 next = next.slice(0, maxLength).replace(/-+$/g, "");
86 if (next.length === 0) return fallback.slice(0, maxLength);
87 if (!/[a-z0-9]$/.test(next)) {
88 next = `${next.slice(0, maxLength - 1)}0`;
89 }
90 return next.slice(0, maxLength);
91 };
92
93 export const sanitizeDisplayName = (value: string) => {
94 let next = value
95 .replace(/[^A-Za-z0-9_-]/g, "_")
96 .replace(/_+/g, "_")
97 .replace(/^_+|_+$/g, "")
98 .slice(0, MAX_DISPLAY_NAME_LENGTH);
99 if (next.length === 0) return "pipeline";
100 if (!/^[A-Za-z]/.test(next)) {
101 next = `p${next}`.slice(0, MAX_DISPLAY_NAME_LENGTH);
102 }
103 return next;
104 };
105
106 export const toPipelineId = (
107 id: string,
108 requested: string | undefined,
109 existing: string | undefined,
110 ) =>
111 Effect.gen(function* () {
112 if (requested !== undefined && requested.length > 0) {
113 return rfc1035(requested);
114 }
115 if (existing !== undefined && existing.length > 0) return existing;
116 return rfc1035(
117 yield* createPhysicalName({
118 id,
119 maxLength: MAX_ID_LENGTH,
120 lowercase: true,
121 }),
122 );
123 });
124
125 export const toDisplayName = (requested: string | undefined, fallback: string) =>
126 requested !== undefined && requested.length > 0
127 ? sanitizeDisplayName(requested)
128 : sanitizeDisplayName(fallback);
129
130 export const stringMapOf = (
131 value: Record<string, string | undefined> | null | undefined,
132 ): Record<string, string> => tagRecord(value);
133
134 export const userSources = (
135 sources: Record<string, string | undefined> | null | undefined,
136 ): Record<string, string> => stripInternalLabels(stringMapOf(sources));
137
138 export const ownershipSources = (
139 labels: Record<string, string>,
140 user?: Record<string, string>,
141 ): Record<string, string> => ({
142 ...user,
143 [alchemyLabelKeys.stack]: labels[alchemyLabelKeys.stack] ?? "x",
144 [alchemyLabelKeys.stage]: labels[alchemyLabelKeys.stage] ?? "x",
145 [alchemyLabelKeys.id]: labels[alchemyLabelKeys.id] ?? "x",
146 });
147
148 export const hasOwnershipMarker = (
149 sources: Record<string, string | undefined> | null | undefined,
150 ) => Object.keys(sources ?? {}).some((key) => key.startsWith("alchemy-"));
151
152 export const ownedByAlchemy = (
153 id: string,
154 sources: Record<string, string | undefined> | null | undefined,
155 ) => hasAlchemyLabels(id, stringMapOf(sources));
156
157 export const scheduleKey = (
158 info: datapipelines.GoogleCloudDatapipelinesV1ScheduleSpec | undefined,
159 ) => ({
160 schedule: info?.schedule ?? "",
161 timeZone: info?.timeZone ?? "",
162 });
163
164 export const replaceOnIdentity = (input: {
165 previousId?: string;
166 nextId?: string;
167 previousLocation?: string;
168 nextLocation?: string;
169 previousType?: string;
170 nextType?: string;
171 }) => {
172 if (
173 input.previousId !== undefined &&
174 input.nextId !== undefined &&
175 input.previousId !== input.nextId
176 ) {
177 return { action: "replace" as const, deleteFirst: false };
178 }
179 if (
180 input.previousLocation !== undefined &&
181 input.nextLocation !== undefined &&
182 input.previousLocation !== input.nextLocation
183 ) {
184 return { action: "replace" as const, deleteFirst: false };
185 }
186 if (
187 input.previousType !== undefined &&
188 input.nextType !== undefined &&
189 input.previousType !== input.nextType
190 ) {
191 return { action: "replace" as const, deleteFirst: false };
192 }
193 return undefined;
194 };
195
196 export const getPipeline = (name: string) =>
197 name.length === 0
198 ? Effect.undefined
199 : datapipelines
200 .getProjectsLocationsPipelines({ name })
201 .pipe(Effect.catchTag("NotFound", () => Effect.undefined));
202
203 export const ignoreMissing = <A, R>(
204 effect: Effect.Effect<A, datapipelines.DeleteProjectsLocationsPipelinesError, R>,
205 ) => effect.pipe(Effect.catchTag("NotFound", () => Effect.void));
206
207 const emptyList = <A>() => Effect.succeed([] as A[]);
208
209 export const listPipelinesAt = (parent: string) =>
210 datapipelines.listProjectsLocationsPipelines
211 .pages({
212 parent,
213 pageSize: 200,
214 })
215 .pipe(
216 Stream.flatMap((page) => Stream.fromIterable(page.pipelines ?? [])),
217 Stream.runCollect,
218 Effect.map((chunk) => Array.from(chunk)),
219 Effect.catchTag("NotFound", () =>
220 emptyList<datapipelines.GoogleCloudDatapipelinesV1Pipeline>(),
221 ),
222 );
223
224 export const listOwnedPipelines = (project: string, region: string) =>
225 Effect.gen(function* () {
226 const pages = yield* Effect.forEach(
227 [...new Set([region, ...LIST_LOCATIONS])],
228 (location) => listPipelinesAt(locationParent(project, location)),
229 { concurrency: 2 },
230 );
231 const seen = new Set<string>();
232 const owned: datapipelines.GoogleCloudDatapipelinesV1Pipeline[] = [];
233 for (const row of pages.flat()) {
234 const name = row.name ?? "";
235 if (name.length > 0 && seen.has(name)) continue;
236 if (!hasOwnershipMarker(row.pipelineSources)) continue;
237 if (name.length > 0) seen.add(name);
238 owned.push(row);
239 }
240 return owned;
241 });
242
243 export const findOwnedPipeline = (id: string, project: string, region: string, name?: string) =>
244 Effect.gen(function* () {
245 const existing = yield* getPipeline(name ?? "");
246 if (existing !== undefined) return existing;
247 const rows = yield* listOwnedPipelines(project, region);
248 for (const row of rows) {
249 if (yield* ownedByAlchemy(id, row.pipelineSources)) {
250 return row;
251 }
252 }
253 return undefined;
254 });